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) - Berry `webclient.url_encode()` is now a static class method, no change required to existing code (#18775)
- Matter Bridge mode always on - Matter Bridge mode always on
- ESP32 Framework (Core) from v2.0.9 to v2.0.10 - ESP32 Framework (Core) from v2.0.9 to v2.0.10
- Berry code size optimizations
### Fixed ### Fixed
- Interaction of ``SetOption92``, ``VirtualCT``, and ``RGBWWTable`` (#18768) - Interaction of ``SetOption92``, ``VirtualCT``, and ``RGBWWTable`` (#18768)

View File

@ -60,6 +60,14 @@
**/ **/
#define BE_USE_PRECOMPILED_OBJECT 1 #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 /* Macro: BE_DEBUG_RUNTIME_INFO
* Set runtime error debugging information. * Set runtime error debugging information.
* 0: unable to output source file and line number at runtime. * 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); const char *data = str(s);
save_word(fp, length); save_word(fp, length);
be_fwrite(fp, data, 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) { if (proto) {
save_string(fp, proto->name); /* name */ save_string(fp, proto->name); /* name */
#if BE_DEBUG_SOURCE_FILE
save_string(fp, proto->source); /* source */ save_string(fp, proto->source); /* source */
#else
save_string(fp, NULL); /* source */
#endif
save_byte(fp, proto->argc); /* argc */ save_byte(fp, proto->argc); /* argc */
save_byte(fp, proto->nstack); /* nstack */ save_byte(fp, proto->nstack); /* nstack */
save_byte(fp, proto->varg); /* varg */ 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)) { if (str_len(name)) {
*proto = be_newproto(vm); *proto = be_newproto(vm);
(*proto)->name = name; (*proto)->name = name;
#if BE_DEBUG_SOURCE_FILE
(*proto)->source = load_string(vm, fp); (*proto)->source = load_string(vm, fp);
#else
load_string(vm, fp); /* discard name */
#endif
(*proto)->argc = load_byte(fp); (*proto)->argc = load_byte(fp);
(*proto)->nstack = load_byte(fp); (*proto)->nstack = load_byte(fp);
if (version > 1) { if (version > 1) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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