From 0ac5d0945b6d1d8a1cacb5bf3f9d5cd360180093 Mon Sep 17 00:00:00 2001 From: s-hadinger <49731213+s-hadinger@users.noreply.github.com> Date: Fri, 23 Jun 2023 23:14:51 +0200 Subject: [PATCH] Berry add option to remove source file name and save flash space (#18948) --- CHANGELOG.md | 1 + lib/libesp32/berry/default/berry_conf.h | 8 ++++++++ lib/libesp32/berry/src/be_bytecode.c | 10 ++++++++++ lib/libesp32/berry/src/be_debug.c | 4 ++++ lib/libesp32/berry/src/be_func.c | 2 ++ lib/libesp32/berry/src/be_gc.c | 2 ++ lib/libesp32/berry/src/be_module.c | 4 ++++ lib/libesp32/berry/src/be_object.h | 2 ++ lib/libesp32/berry/src/be_parser.c | 4 ++++ lib/libesp32/berry/src/berry.h | 14 +++++++++++++- 10 files changed, 50 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16cd6dec1..006537d3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/libesp32/berry/default/berry_conf.h b/lib/libesp32/berry/default/berry_conf.h index 18d64ad81..13200f530 100644 --- a/lib/libesp32/berry/default/berry_conf.h +++ b/lib/libesp32/berry/default/berry_conf.h @@ -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. diff --git a/lib/libesp32/berry/src/be_bytecode.c b/lib/libesp32/berry/src/be_bytecode.c index 14a5950fc..133a96618 100644 --- a/lib/libesp32/berry/src/be_bytecode.c +++ b/lib/libesp32/berry/src/be_bytecode.c @@ -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) { diff --git a/lib/libesp32/berry/src/be_debug.c b/lib/libesp32/berry/src/be_debug.c index 887872cae..39a4cd61c 100644 --- a/lib/libesp32/berry/src/be_debug.c +++ b/lib/libesp32/berry/src/be_debug.c @@ -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(":"); diff --git a/lib/libesp32/berry/src/be_func.c b/lib/libesp32/berry/src/be_func.c index d49229954..b1280d9ec 100644 --- a/lib/libesp32/berry/src/be_func.c +++ b/lib/libesp32/berry/src/be_func.c @@ -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; diff --git a/lib/libesp32/berry/src/be_gc.c b/lib/libesp32/berry/src/be_gc.c index f7532e239..e66afa56d 100644 --- a/lib/libesp32/berry/src/be_gc.c +++ b/lib/libesp32/berry/src/be_gc.c @@ -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; diff --git a/lib/libesp32/berry/src/be_module.c b/lib/libesp32/berry/src/be_module.c index ff5503011..99b9bbc08 100644 --- a/lib/libesp32/berry/src/be_module.c +++ b/lib/libesp32/berry/src/be_module.c @@ -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); diff --git a/lib/libesp32/berry/src/be_object.h b/lib/libesp32/berry/src/be_object.h index 72627f65c..fd29090ad 100644 --- a/lib/libesp32/berry/src/be_object.h +++ b/lib/libesp32/berry/src/be_object.h @@ -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; diff --git a/lib/libesp32/berry/src/be_parser.c b/lib/libesp32/berry/src/be_parser.c index 107ae5522..59e57d067 100644 --- a/lib/libesp32/berry/src/be_parser.c +++ b/lib/libesp32/berry/src/be_parser.c @@ -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); diff --git a/lib/libesp32/berry/src/berry.h b/lib/libesp32/berry/src/berry.h index 98c589aef..206d35437 100644 --- a/lib/libesp32/berry/src/berry.h +++ b/lib/libesp32/berry/src/berry.h @@ -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 /**< */ \ }