1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-18 11:19:56 +00:00

symbolslots now use janet_v vectors, flat structure

This commit is contained in:
Jona Ekenberg 2023-02-01 11:06:33 +01:00
parent 88813c4f87
commit 587aa87d28
9 changed files with 91 additions and 106 deletions

View File

@ -2769,18 +2769,14 @@
# define variables available at breakpoint
(def frame ((debug/stack fiber) 0))
(def slotsyms ((disasm (frame :function)) :slotsyms))
(def symbolslots ((disasm (frame :function)) :symbolslots))
(def pc (frame :pc))
(loop [[start stop syms] :in slotsyms]
(when (and (or (= start :top)
(<= start pc))
(or (= stop :top)
(< pc stop)))
(loop [[sym instances] :pairs syms
[def-index slot] :in instances
:when (<= def-index pc)]
(put nextenv (symbol sym) @{:value (get-in frame [:slots slot])}))))
(loop [[birth death slot sym] :in symbolslots]
(when (and (<= birth pc)
(or (= death 0)
(< pc death)))
(put nextenv (symbol sym) @{:value (get-in frame [:slots slot])})))
(merge-into nextenv debugger-env)
(defn debugger-chunks [buf p]

View File

@ -882,34 +882,19 @@ static Janet janet_disasm_slotcount(JanetFuncDef *def) {
return janet_wrap_integer(def->slotcount);
}
/*
example structure:
:slotsyms @[(:top :top @{"o1" @[(-1 0)] "outer" @[(0 1)])])
(2 8
# ^ beginning of scope
# ^ end of scope
@{"inner" @[(6 6)] "odo4" @[(2 4) (10 7)]})]
# ^ ^ bytecode index of def
# ^ ^ slot index
^ redefinition
beginning of scope is the bytecode index of when a scope begins.
if the program counter is >= beginning of scope and < end of scope, it is
currently in that scope.
bytecode index of def is the bytecode index from where the slot / sym is actually defined. before this point the slot might have a value, but it's just residual data from earlier slot usage.
the redefinition is an example of what happens if the same symbol is used twice, e.g. due to calling `def` twice with the same symbol in the same scope. in that case the first slot (4) is valid from the first bytecode index until the bytecode index in the redefinition, after which the second slot (4) is valid.
:top means the function's scope, e.g. parameters and `def` calls outside of e.g. a `do`-block
*/
static Janet janet_disasm_slotsyms(JanetFuncDef *def) {
JanetArray *slotsyms = janet_array(def->slotsyms_length);
for (int32_t i = 0; i < def->slotsyms_length; i++) {
slotsyms->data[i] = def->slotsyms[i];
static Janet janet_disasm_symbolslots(JanetFuncDef *def) {
JanetArray *symbolslots = janet_array(def->symbolslots_length);
for (int32_t i = 0; i < def->symbolslots_length; i++) {
JanetSymbolSlot ss = def->symbolslots[i];
Janet *t = janet_tuple_begin(4);
t[0] = janet_wrap_integer(ss.birth_pc);
t[1] = janet_wrap_integer(ss.death_pc);
t[2] = janet_wrap_integer(ss.slot_index);
t[3] = janet_cstringv((const char *) ss.symbol);
symbolslots->data[i] = janet_wrap_tuple(janet_tuple_end(t));
}
slotsyms->count = def->slotsyms_length;
return janet_wrap_array(slotsyms);
symbolslots->count = def->symbolslots_length;
return janet_wrap_array(symbolslots);
}
@ -992,7 +977,7 @@ Janet janet_disasm(JanetFuncDef *def) {
janet_table_put(ret, janet_ckeywordv("structarg"), janet_disasm_structarg(def));
janet_table_put(ret, janet_ckeywordv("name"), janet_disasm_name(def));
janet_table_put(ret, janet_ckeywordv("slotcount"), janet_disasm_slotcount(def));
janet_table_put(ret, janet_ckeywordv("slotsyms"), janet_disasm_slotsyms(def));
janet_table_put(ret, janet_ckeywordv("symbolslots"), janet_disasm_symbolslots(def));
janet_table_put(ret, janet_ckeywordv("constants"), janet_disasm_constants(def));
janet_table_put(ret, janet_ckeywordv("sourcemap"), janet_disasm_sourcemap(def));
janet_table_put(ret, janet_ckeywordv("environments"), janet_disasm_environments(def));
@ -1029,7 +1014,7 @@ JANET_CORE_FN(cfun_disasm,
"* :source - name of source file that this function was compiled from.\n"
"* :name - name of function.\n"
"* :slotcount - how many virtual registers, or slots, this function uses. Corresponds to stack space used by function.\n"
"* :slotsyms - all slots and their symbols.\n"
"* :symbolslots - all symbols and their slots.\n"
"* :constants - an array of constants referenced by this function.\n"
"* :sourcemap - a mapping of each bytecode instruction to a line and column in the source file.\n"
"* :environments - an internal mapping of which enclosing functions are referenced for bindings.\n"

View File

@ -218,7 +218,7 @@ JanetFuncDef *janet_funcdef_alloc(void) {
def->closure_bitset = NULL;
def->flags = 0;
def->slotcount = 0;
def->slotsyms = 0;
def->symbolslots = 0;
def->arity = 0;
def->min_arity = 0;
def->max_arity = INT32_MAX;
@ -230,6 +230,7 @@ JanetFuncDef *janet_funcdef_alloc(void) {
def->constants_length = 0;
def->bytecode_length = 0;
def->environments_length = 0;
def->symbolslots_length = 0;
return def;
}

View File

@ -177,47 +177,27 @@ void janetc_popscope(JanetCompiler *c) {
}
if (janet_truthy(janet_dyn("debug"))) {
/* push symbols */
if (true || (!(oldscope->flags & (JANET_SCOPE_FUNCTION | JANET_SCOPE_UNUSED)) && newscope)) {
Janet *t = janet_tuple_begin(3);
t[0] = janet_wrap_integer(oldscope->bytecode_start);
t[1] = janet_wrap_integer(janet_v_count(c->buffer));
bool top_level = (oldscope->flags & (JANET_SCOPE_FUNCTION | JANET_SCOPE_UNUSED));
/* push symbol slots */
JanetSymbolSlot ss;
int32_t scope_end = top_level ? INT32_MAX : janet_v_count(c->buffer);
JanetTable *sym_table = janet_table(janet_v_count(oldscope->syms));
janet_assert(janet_v_count(c->local_symbols) > 0, "c->local_symbols should not be empty");
for (int32_t i = 0; i < janet_v_count(oldscope->syms); i++) {
SymPair pair = oldscope->syms[i];
// due to scopes being added "in reverse" (filo), we will reverse all symbols later. therefore we must first reverse the order of symbols inside each scope as well.
for (int32_t i = janet_v_count(oldscope->syms) - 1; i >= 0; i--) {
SymPair pair = oldscope->syms[i];
if (pair.sym != NULL) {
Janet *symbol_tuple = janet_tuple_begin(2);
symbol_tuple[0] = janet_wrap_integer(pair.bytecode_pos);
symbol_tuple[1] = janet_wrap_integer(pair.slot.index);
if (pair.sym != NULL) {
ss.birth_pc = pair.bytecode_pos;
ss.death_pc = scope_end;
ss.slot_index = pair.slot.index;
ss.symbol = pair.sym;
Janet k = janet_cstringv((const char *) pair.sym);
Janet arr = janet_table_get(sym_table, k);
if (janet_checktype(arr, JANET_NIL)) {
JanetArray *a = janet_array(1);
janet_array_push(a, janet_wrap_tuple(janet_tuple_end(symbol_tuple)));
janet_table_put(sym_table, k, janet_wrap_array(a));
} else {
JanetArray *a = janet_unwrap_array(arr);
janet_array_push(a, janet_wrap_tuple(janet_tuple_end(symbol_tuple)));
}
}
}
t[2] = janet_wrap_table(sym_table);
if (c->local_binds->count > 0) {
janet_array_push(janet_unwrap_array(c->local_binds->data[c->local_binds->count - 1]), janet_wrap_tuple(janet_tuple_end(t)));
} else {
// this shouldn't occur -- local_binds should always have at least
// 1 element when a scope is popped
fprintf(stderr, "no local_binds pushed\n");
janet_v_push(janet_v_last(c->local_symbols), ss);
}
}
/* end push symbols */
}
/* Free the old scope */
@ -972,23 +952,23 @@ JanetFuncDef *janetc_pop_funcdef(JanetCompiler *c) {
/* Pop the scope */
janetc_popscope(c);
if (janet_truthy(janet_dyn("debug"))) {
JanetArray *last_binds = janet_unwrap_array(c->local_binds->data[c->local_binds->count - 1]);
JanetSymbolSlot *last_symbols = janet_v_last(c->local_symbols);
def->slotsyms_length = last_binds->count;
def->slotsyms = janet_malloc(sizeof(Janet) * (size_t)def->slotsyms_length);
def->symbolslots_length = janet_v_count(last_symbols);
// just gotta modify some tuples
Janet *top_level_tuple = (Janet *)janet_unwrap_tuple(last_binds->data[last_binds->count - 1]);
top_level_tuple[0] = janet_ckeywordv("top");
top_level_tuple[1] = janet_ckeywordv("top");
for (int i = 0; i < last_binds->count; i++) {
def->slotsyms[def->slotsyms_length - i - 1] = last_binds->data[i];
def->symbolslots = janet_malloc(sizeof(JanetSymbolSlot) * def->symbolslots_length);
if (NULL == def->bytecode) {
JANET_OUT_OF_MEMORY;
}
// add in reverse, because scopes have been added filo
for (int i = 0; i < janet_v_count(last_symbols); i++) {
def->symbolslots[def->symbolslots_length - i - 1] = last_symbols[i];
}
}
janet_array_pop(c->local_binds);
janet_v_pop(c->local_symbols);
return def;
}
@ -1000,7 +980,7 @@ static void janetc_init(JanetCompiler *c, JanetTable *env, const uint8_t *where,
c->mapbuffer = NULL;
c->recursion_guard = JANET_RECURSION_GUARD;
c->env = env;
c->local_binds = janet_array(0);
c->local_symbols = NULL;
c->source = where;
c->current_mapping.line = -1;
c->current_mapping.column = -1;
@ -1019,7 +999,7 @@ static void janetc_deinit(JanetCompiler *c) {
janet_v_free(c->buffer);
janet_v_free(c->mapbuffer);
c->env = NULL;
c->local_binds = NULL;
janet_v_free(c->local_symbols);
}
/* Compile a form. */
@ -1033,7 +1013,7 @@ JanetCompileResult janet_compile_lint(Janet source,
/* Push a function scope */
if (janet_truthy(janet_dyn("debug"))) {
janet_array_push(c.local_binds, janet_wrap_array(janet_array(0)));
janet_v_push(c.local_symbols, NULL);
}
janetc_scope(&rootscope, &c, JANET_SCOPE_FUNCTION | JANET_SCOPE_TOP, "root");
@ -1054,7 +1034,7 @@ JanetCompileResult janet_compile_lint(Janet source,
c.result.error_mapping = c.current_mapping;
janetc_popscope(&c);
if (janet_truthy(janet_dyn("debug"))) {
janet_array_pop(c.local_binds);
janet_v_pop(c.local_symbols);
}
}

View File

@ -161,7 +161,7 @@ struct JanetCompiler {
/* Hold the environment */
JanetTable *env;
JanetArray *local_binds;
JanetSymbolSlot **local_symbols;
/* Name of source to attach to generated functions */
const uint8_t *source;

View File

@ -755,7 +755,7 @@ static JanetSlot janetc_while(JanetFopts opts, int32_t argn, const Janet *argv)
if (janet_truthy(janet_dyn("debug"))) {
janet_array_push(c->local_binds, janet_wrap_array(janet_array(0)));
janet_v_push(c->local_symbols, NULL);
}
janetc_scope(&tempscope, c, JANET_SCOPE_FUNCTION, "while-iife");
@ -834,7 +834,7 @@ static JanetSlot janetc_fn(JanetFopts opts, int32_t argn, const Janet *argv) {
/* Begin function */
c->scope->flags |= JANET_SCOPE_CLOSURE;
if (janet_truthy(janet_dyn("debug"))) {
janet_array_push(c->local_binds, janet_wrap_array(janet_array(0)));
janet_v_push(c->local_symbols, NULL);
}
janetc_scope(&fnscope, c, JANET_SCOPE_FUNCTION, "function");

View File

@ -448,6 +448,7 @@ typedef struct JanetReg JanetReg;
typedef struct JanetRegExt JanetRegExt;
typedef struct JanetMethod JanetMethod;
typedef struct JanetSourceMapping JanetSourceMapping;
typedef struct JanetSymbolSlot JanetSymbolSlot;
typedef struct JanetView JanetView;
typedef struct JanetByteView JanetByteView;
typedef struct JanetDictView JanetDictView;
@ -1018,6 +1019,14 @@ struct JanetSourceMapping {
int32_t column;
};
/* Symbol to slot mapping & lifetime structure. */
struct JanetSymbolSlot {
uint32_t birth_pc;
uint32_t death_pc;
uint32_t slot_index;
const uint8_t *symbol;
};
/* A function definition. Contains information needed to instantiate closures. */
struct JanetFuncDef {
JanetGCObject gc;
@ -1031,7 +1040,7 @@ struct JanetFuncDef {
JanetSourceMapping *sourcemap;
JanetString source;
JanetString name;
Janet *slotsyms;
JanetSymbolSlot *symbolslots;
int32_t flags;
int32_t slotcount; /* The amount of stack space required for the function */
@ -1042,7 +1051,7 @@ struct JanetFuncDef {
int32_t bytecode_length;
int32_t environments_length;
int32_t defs_length;
int32_t slotsyms_length;
int32_t symbolslots_length;
};
/* A function environment */

View File

@ -106,4 +106,3 @@
(assert (= ((f 10) 37) 47) "asm environment tables")
(end-suite)

View File

@ -3,25 +3,40 @@
(import ./helper :prefix "" :exit true)
(start-suite 15)
(assert (deep= (in (disasm (defn a [] (def x 10) x)) :slotsyms)
(assert (deep= (in (disasm (defn a [] (def x 10) x)) :symbolslots)
@[])
"no slotsyms when *debug* is false")
"no symbolslots when *debug* is false")
(setdyn *debug* true)
(assert (deep= (in (disasm (defn a [] (def x 10) x)) :slotsyms)
@[[:top :top @{"a" @[[0 0]] "x" @[[1 1]]}]])
"slotsyms when *debug* is true")
(assert (deep= (in (disasm (defn a [] (def x 10) x)) :symbolslots)
@[[0 2147483647 0 "a"] [1 2147483647 1 "x"]])
"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)
(assert (deep= (in (disasm (defn a []
(assert (deep= (in (disasm (defn a [arg]
(def x 10)
(do
(def y 20)
(+ x y)))) :slotsyms)
@[[:top :top @{"a" @[[0 0]] "x" @[[1 1]]}]
[2 5 @{"y" @[[2 2]]}]])
"inner slotsyms")
(def z 30)
(+ x y z)))) :symbolslots)
@[[-1 2147483647 0 "arg"]
[0 2147483647 1 "a"]
[1 2147483647 2 "x"]
[2 7 3 "y"]
[3 7 4 "z"]])
"arg & inner symbolslots")
(setdyn *debug* false)
(end-suite)