From c0c8ab25e68dfb0b2a9f06a0b19449a1698ae59e Mon Sep 17 00:00:00 2001 From: Jona Ekenberg Date: Wed, 1 Feb 2023 21:12:42 +0100 Subject: [PATCH] added symbolslots to asm --- src/core/asm.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/core/compile.c | 2 +- test/suite0015.janet | 22 +++++++++++++--------- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/core/asm.c b/src/core/asm.c index be596e81..b182cbfc 100644 --- a/src/core/asm.c +++ b/src/core/asm.c @@ -721,6 +721,46 @@ static JanetAssembleResult janet_asm1(JanetAssembler *parent, Janet source, int } } + /* Set symbolslots */ + def->symbolslots = NULL; + def->symbolslots_length = 0; + + x = janet_get1(s, janet_ckeywordv("symbolslots")); + if (janet_indexed_view(x, &arr, &count)) { + def->symbolslots_length = count; + def->symbolslots = janet_malloc(sizeof(JanetSymbolSlot) * (size_t) count); + if (NULL == def->symbolslots) { + JANET_OUT_OF_MEMORY; + } + for (i = 0; i < count; i++) { + const Janet *tup; + Janet entry = arr[i]; + JanetSymbolSlot ss; + if (!janet_checktype(entry, JANET_TUPLE)) { + janet_asm_error(&a, "expected tuple"); + } + tup = janet_unwrap_tuple(entry); + if (!janet_checkint(tup[0])) { + janet_asm_error(&a, "expected integer"); + } + if (!janet_checkint(tup[1])) { + janet_asm_error(&a, "expected integer"); + } + if (!janet_checkint(tup[2])) { + janet_asm_error(&a, "expected integer"); + } + if (!janet_checktype(tup[3], JANET_STRING)) { + janet_asm_error(&a, "expected string"); + } + ss.birth_pc = janet_unwrap_integer(tup[0]); + ss.death_pc = janet_unwrap_integer(tup[1]); + ss.slot_index = janet_unwrap_integer(tup[2]); + ss.symbol = janet_unwrap_string(tup[3]); + + def->symbolslots[i] = ss; + } + } + /* Set environments */ def->environments = janet_realloc(def->environments, def->environments_length * sizeof(int32_t)); x = janet_get1(s, janet_ckeywordv("environments")); diff --git a/src/core/compile.c b/src/core/compile.c index cff26a91..e41fa5bf 100644 --- a/src/core/compile.c +++ b/src/core/compile.c @@ -998,8 +998,8 @@ static void janetc_init(JanetCompiler *c, JanetTable *env, const uint8_t *where, static void janetc_deinit(JanetCompiler *c) { janet_v_free(c->buffer); janet_v_free(c->mapbuffer); - c->env = NULL; janet_v_free(c->local_symbols); + c->env = NULL; } /* Compile a form. */ diff --git a/test/suite0015.janet b/test/suite0015.janet index 62a0dad7..e407a78c 100644 --- a/test/suite0015.janet +++ b/test/suite0015.janet @@ -12,15 +12,19 @@ "symbolslots when *debug* is true") (setdyn *debug* false) -# need to fix assembling functions -(comment - (setdyn *debug* true) - (def f (asm (disasm (fn [x] (fn [y] (+ x y)))))) - (assert (deep= (in (disasm f) :symbolslots) - @[[0 2147483647 0 "a"] [1 2147483647 1 "x"]]) - "symbolslots survive disasm/asm") - (setdyn *debug* false) - ) +(setdyn *debug* true) +(defn a [arg] + (def x 10) + (do + (def y 20) + (def z 30) + (+ x y z))) +(def symbolslots (in (disasm a) :symbolslots)) +(def f (asm (disasm a))) +(assert (deep= (in (disasm f) :symbolslots) + symbolslots) + "symbolslots survive disasm/asm") +(setdyn *debug* false) (setdyn *debug* true) (assert (deep= (in (disasm (defn a [arg]