Berry add option to remove source file name and save flash space (#18948)

This commit is contained in:
s-hadinger 2023-06-23 23:14:51 +02:00 committed by GitHub
parent 5792dad12b
commit 0ac5d0945b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 50 additions and 1 deletions

View File

@ -21,6 +21,7 @@ All notable changes to this project will be documented in this file.
- Berry `webclient.url_encode()` is now a static class method, no change required to existing code (#18775)
- Matter Bridge mode always on
- ESP32 Framework (Core) from v2.0.9 to v2.0.10
- Berry code size optimizations
### Fixed
- Interaction of ``SetOption92``, ``VirtualCT``, and ``RGBWWTable`` (#18768)

View File

@ -60,6 +60,14 @@
**/
#define BE_USE_PRECOMPILED_OBJECT 1
/* Macro: BE_DEBUG_SOURCE_FILE
* Indicate if each function remembers its source file name
* 0: do not keep the file name (saves 4 bytes per function)
* 1: keep the source file name
* Default: 1
**/
#define BE_DEBUG_SOURCE_FILE 0
/* Macro: BE_DEBUG_RUNTIME_INFO
* Set runtime error debugging information.
* 0: unable to output source file and line number at runtime.

View File

@ -114,6 +114,8 @@ static void save_string(void *fp, bstring *s)
const char *data = str(s);
save_word(fp, length);
be_fwrite(fp, data, length);
} else {
save_word(fp, 0);
}
}
@ -247,7 +249,11 @@ static void save_proto(bvm *vm, void *fp, bproto *proto)
{
if (proto) {
save_string(fp, proto->name); /* name */
#if BE_DEBUG_SOURCE_FILE
save_string(fp, proto->source); /* source */
#else
save_string(fp, NULL); /* source */
#endif
save_byte(fp, proto->argc); /* argc */
save_byte(fp, proto->nstack); /* nstack */
save_byte(fp, proto->varg); /* varg */
@ -551,7 +557,11 @@ static bbool load_proto(bvm *vm, void *fp, bproto **proto, int info, int version
if (str_len(name)) {
*proto = be_newproto(vm);
(*proto)->name = name;
#if BE_DEBUG_SOURCE_FILE
(*proto)->source = load_string(vm, fp);
#else
load_string(vm, fp); /* discard name */
#endif
(*proto)->argc = load_byte(fp);
(*proto)->nstack = load_byte(fp);
if (version > 1) {

View File

@ -156,7 +156,9 @@ void be_dumpclosure(bclosure *cl)
#if BE_DEBUG_RUNTIME_INFO
blineinfo *lineinfo = proto->lineinfo;
#endif
#if BE_DEBUG_SOURCE_FILE
logfmt("source '%s', ", str(proto->source));
#endif
logfmt("function '%s':\n", str(proto->name));
#if BE_DEBUG_RUNTIME_INFO
if (lineinfo) { /* first line */
@ -185,7 +187,9 @@ static void sourceinfo(bproto *proto, binstruction *ip)
int pc = cast_int(ip - proto->code - 1); /* now vm->ip has been increased */
for (; it < end && pc > it->endpc; ++it);
snprintf(buf, sizeof(buf), ":%d:", it->linenumber);
#if BE_DEBUG_SOURCE_FILE
be_writestring(str(proto->source));
#endif
be_writestring(buf);
} else {
be_writestring("<unknown source>:");

View File

@ -108,7 +108,9 @@ bproto* be_newproto(bvm *vm)
p->codesize = 0;
p->argc = 0;
p->varg = 0;
#if BE_DEBUG_SOURCE_FILE
p->source = NULL;
#endif
#if BE_DEBUG_RUNTIME_INFO
p->lineinfo = NULL;
p->nlineinfo = 0;

View File

@ -231,7 +231,9 @@ static void mark_proto(bvm *vm, bgcobject *obj)
mark_gray(vm, gc_object(*ptab));
}
gc_setdark_safe(p->name);
#if BE_DEBUG_SOURCE_FILE
gc_setdark_safe(p->source);
#endif
#if BE_DEBUG_VAR_INFO
if (p->nvarinfo) {
bvarinfo *vinfo = p->varinfo;

View File

@ -128,11 +128,15 @@ static char* fixpath(bvm *vm, bstring *path, size_t *size)
const char *split, *base;
bvalue *func = vm->cf->func;
bclosure *cl = var_toobj(func);
#if BE_DEBUG_SOURCE_FILE
if (var_isclosure(func)) {
base = str(cl->proto->source); /* get the source file path */
} else {
base = "/";
}
#else
base = "/";
#endif
split = be_splitpath(base);
*size = split - base + (size_t)str_len(path) + SUFFIX_LEN;
buffer = be_malloc(vm, *size);

View File

@ -160,7 +160,9 @@ typedef struct bproto {
struct bproto **ptab; /* proto table */
binstruction *code; /* instructions sequence */
bstring *name; /* function name */
#if BE_DEBUG_SOURCE_FILE
bstring *source; /* source file name */
#endif
#if BE_DEBUG_RUNTIME_INFO /* debug information */
blineinfo *lineinfo;
int nlineinfo;

View File

@ -228,9 +228,11 @@ static void end_block(bparser *parser)
`stdin` or the name of the current function */
static bstring* parser_source(bparser *parser)
{
#if BE_DEBUG_SOURCE_FILE
if (parser->finfo) {
return parser->finfo->proto->source;
}
#endif
return be_newstr(parser->vm, parser->lexer.fname);
}
@ -250,7 +252,9 @@ static void begin_func(bparser *parser, bfuncinfo *finfo, bblockinfo *binfo)
be_vector_init(vm, &finfo->pvec, sizeof(bproto*)); /* vector for subprotos */
proto->ptab = be_vector_data(&finfo->pvec);
proto->nproto = be_vector_capacity(&finfo->pvec);
#if BE_DEBUG_SOURCE_FILE
proto->source = parser_source(parser); /* keep a copy of source for function */
#endif
finfo->local = be_list_new(vm); /* list for local variables */
var_setlist(vm->top, finfo->local); /* push list of local variables on the stack (avoid gc) */
be_stackpush(vm);

View File

@ -477,6 +477,18 @@ typedef bclass_ptr bclass_array[];
#define be_local_const_upval(ins, idx) { ins, idx }
#endif
/**
* @def PROTO_RUNTIME_BLOCK
* @brief conditional block in bproto depending on compilation options
*
*/
#if BE_SOURCE_FILE
#define PROTO_SOURCE_FILE(n) \
((bstring*) _source), /**< source */
#else
#define PROTO_SOURCE_FILE(n)
#endif
/**
* @def PROTO_RUNTIME_BLOCK
* @brief conditional block in bproto depending on compilation options
@ -554,7 +566,7 @@ typedef bclass_ptr bclass_array[];
(struct bproto**) _protos, /**< bproto **ptab */ \
(binstruction*) _code, /**< code */ \
((bstring*) _fname), /**< name */ \
((bstring*) _source), /**< source */ \
PROTO_SOURCE_FILE(_source) /**< source */ \
PROTO_RUNTIME_BLOCK /**< */ \
PROTO_VAR_INFO_BLOCK /**< */ \
}