From d763d0d51afddceeb251bc6dd6e9458cc4ec0484 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 19 Sep 2026 16:28:41 -0500 Subject: [PATCH] Move optimiize out of pop_funcdef. --- src/conf/janetconf.h | 8 ++++---- src/core/bytecode.c | 10 ++++++++++ src/core/compile.c | 7 +------ src/core/specials.c | 7 +++++++ 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/conf/janetconf.h b/src/conf/janetconf.h index 67834f5b..da02e938 100644 --- a/src/conf/janetconf.h +++ b/src/conf/janetconf.h @@ -4,10 +4,10 @@ #define JANETCONF_H #define JANET_VERSION_MAJOR 1 -#define JANET_VERSION_MINOR 42 -#define JANET_VERSION_PATCH 1 -#define JANET_VERSION_EXTRA "" -#define JANET_VERSION "1.42.1" +#define JANET_VERSION_MINOR 43 +#define JANET_VERSION_PATCH 0 +#define JANET_VERSION_EXTRA "-dev" +#define JANET_VERSION "1.43.0-dev" /* #define JANET_BUILD "local" */ diff --git a/src/core/bytecode.c b/src/core/bytecode.c index 7032b507..9790a248 100644 --- a/src/core/bytecode.c +++ b/src/core/bytecode.c @@ -971,6 +971,16 @@ void janet_bytecode_remove_unused_constants(JanetFuncDef *def) { janet_assert(words > 0, "bad length"); uint32_t *bitset = array_allocate(sizeof(uint32_t), words); memset(bitset, 0, words * sizeof(uint32_t)); + + /* Quirk: We rely on the first N constants being the named arguments of a function for warnings + * while making function calls. Preserve these in the case we have named arguments so we don't get + * spurious warnings for unused named arguments (or miss warnings!) */ + if (def->flags & JANET_FUNCDEF_FLAG_NAMEDARGS) { + for (int32_t index = 0; index < def->named_args_count; index++) { + bs_set_bit(bitset, index); + } + } + for (int32_t pc = 0; pc < def->bytecode_length; pc++) { uint32_t E = code[pc] >> 16; switch (code[pc] & 0x7F) { diff --git a/src/core/compile.c b/src/core/compile.c index 38ac63ff..5cf40cba 100644 --- a/src/core/compile.c +++ b/src/core/compile.c @@ -1012,9 +1012,6 @@ JanetFuncDef *janetc_pop_funcdef(JanetCompiler *c, JanetString name) { JanetFuncDef *def = janet_funcdef_alloc(); def->slotcount = scope->ra.max + 1; - /* TODO - turn this on even for unused code for verification purposes */ - int optimize = (scope->flags & JANET_SCOPE_UNUSED) ? 0 : c->optimize; - janet_assert(scope->flags & JANET_SCOPE_FUNCTION, "expected function scope"); /* Copy envs */ @@ -1141,9 +1138,6 @@ JanetFuncDef *janetc_pop_funcdef(JanetCompiler *c, JanetString name) { /* Add a name _before_ optimization for debugging */ def->name = name; - /* Do basic optimization */ - janet_bytecode_optimize(def, optimize); - #ifdef JANET_DEBUG janet_verify(def); #endif @@ -1209,6 +1203,7 @@ JanetCompileResult janet_compile_lint(Janet source, if (c.result.status == JANET_COMPILE_OK) { JanetFuncDef *def = janetc_pop_funcdef(&c, janet_cstring("thunk")); janet_def_addflags(def); + janet_bytecode_optimize(def, c.optimize); c.result.funcdef = def; } else { c.result.error_mapping = c.current_mapping; diff --git a/src/core/specials.c b/src/core/specials.c index 18b3aee1..736752c7 100644 --- a/src/core/specials.c +++ b/src/core/specials.c @@ -963,6 +963,9 @@ static JanetSlot janetc_while(JanetFopts opts, int32_t argn, const Janet *argv) JanetFuncDef *def = janetc_pop_funcdef(c, janet_cstring("while")); janet_def_addflags(def); int32_t defindex = janetc_addfuncdef(c, def); + /* Do basic optimization */ + int32_t optimize = (c->scope->flags & JANET_SCOPE_UNUSED) ? -1 : c->optimize; + janet_bytecode_optimize(def, optimize); /* And then load the closure and call it. */ int32_t cloreg = janetc_regalloc_temp(&c->scope->ra, JANETC_REGTEMP_0); janetc_emit(c, JOP_CLOSURE | ((uint32_t) cloreg << 8) | ((uint32_t) defindex << 16)); @@ -1023,6 +1026,7 @@ static JanetSlot janetc_fn(JanetFopts opts, int32_t argn, const Janet *argv) { /* Begin function */ c->scope->flags |= JANET_SCOPE_CLOSURE; + int32_t optimize = (c->scope->flags & JANET_SCOPE_UNUSED) ? -1 : c->optimize; janetc_scope(&fnscope, c, JANET_SCOPE_FUNCTION, "function"); if (argn == 0) { @@ -1212,6 +1216,9 @@ static JanetSlot janetc_fn(JanetFopts opts, int32_t argn, const Janet *argv) { /* Ensure enough slots for vararg function. */ if (arity + vararg > def->slotcount) def->slotcount = arity + vararg; + /* Do basic optimization */ + janet_bytecode_optimize(def, optimize); + /* Instantiate closure */ ret = janetc_gettarget(opts); janetc_emit_su(c, JOP_CLOSURE, ret, defindex, 1);