From 6d53c08df06a9e5bd70546f6937be7b079d8e853 Mon Sep 17 00:00:00 2001 From: Benjamin Vedder Date: Wed, 10 Jan 2024 21:15:00 +0100 Subject: [PATCH] Squashed 'lispBM/lispBM/' changes from 9c2023ac..2542797b 2542797b extension storage changed 055ec467 f_sym takes a symbol_id not an encoded symbol. f_sym_string takes a c string representing a symbol 1475b1a7 added functions for retrieving storage locations for symbols and symbol list entry d234d21d Added functions for accessing the data in array, ro and rw kinds 5e009396 re-enabling the callcc gc bugfix with a modification to rule out following non-pointers into memory git-subtree-dir: lispBM/lispBM git-subtree-split: 2542797b8124eac6dc2a9d0671c7d9c9711b6968 --- benchmarks/bench_chibi/main.c | 2 +- examples/evaluator.c | 2 +- experiment_repl/repl.c | 9 ++++- include/extensions.h | 27 ++++++++++--- include/heap.h | 9 ++++- include/lbm_flat_value.h | 4 +- include/lispbm.h | 4 +- include/symrepr.h | 29 +++++++------- src/eval_cps.c | 16 +++++++- src/extensions.c | 58 ++++++++++++++++++---------- src/fundamental.c | 3 +- src/heap.c | 29 +++++++++++++- src/lbm_flat_value.c | 35 ++++++++--------- src/lispbm.c | 2 +- src/symrepr.c | 71 ++++++++++++++++++----------------- tests/test_lisp_code_cps.c | 10 ++--- 16 files changed, 197 insertions(+), 113 deletions(-) diff --git a/benchmarks/bench_chibi/main.c b/benchmarks/bench_chibi/main.c index be30cd3a..2be3b3dd 100644 --- a/benchmarks/bench_chibi/main.c +++ b/benchmarks/bench_chibi/main.c @@ -42,7 +42,7 @@ #define HEAP_SIZE 4096 #define EXTENSION_STORAGE_SIZE 256 -extension_fptr extensions[EXTENSION_STORAGE_SIZE]; +lbm_extension_t extensions[EXTENSION_STORAGE_SIZE]; uint32_t print_stack_storage[PRINT_STACK_SIZE]; static lbm_cons_t heap[HEAP_SIZE] __attribute__ ((aligned (8))); diff --git a/examples/evaluator.c b/examples/evaluator.c index 741d6909..1c889dca 100644 --- a/examples/evaluator.c +++ b/examples/evaluator.c @@ -33,7 +33,7 @@ #define WAIT_TIMEOUT 2500 -extension_fptr extension_storage[EXTENSION_STORAGE_SIZE]; +lbm_extension_t extension_storage[EXTENSION_STORAGE_SIZE]; /* Tokenizer state for strings */ static lbm_string_channel_state_t string_tok_state; diff --git a/experiment_repl/repl.c b/experiment_repl/repl.c index ed6b5b07..33598dfa 100644 --- a/experiment_repl/repl.c +++ b/experiment_repl/repl.c @@ -46,7 +46,7 @@ #define CONSTANT_MEMORY_SIZE 32*1024 #define PROF_DATA_NUM 100 -extension_fptr extensions[EXTENSION_STORAGE_SIZE]; +lbm_extension_t extensions[EXTENSION_STORAGE_SIZE]; lbm_uint constants_memory[CONSTANT_MEMORY_SIZE]; lbm_prof_t prof_data[100]; @@ -590,7 +590,12 @@ void lookup_local(eval_context_t *ctx, void *arg1, void *arg2) { void sym_it(const char *str) { - printf("%s\n", str); + bool sym_name_flash = lbm_symbol_in_flash((char *)str); + bool sym_entry_flash = lbm_symbol_list_entry_in_flash((char *)str); + printf("[%s, %s]: %s\n", + sym_name_flash ? "FLASH" : "LBM_MEM", + sym_entry_flash ? "FLASH" : "LBM_MEM", + str); } static lbm_uint memory[LBM_MEMORY_SIZE_1M]; diff --git a/include/extensions.h b/include/extensions.h index e77c6b54..6e486520 100644 --- a/include/extensions.h +++ b/include/extensions.h @@ -20,7 +20,6 @@ #ifndef EXTENSIONS_H_ #define EXTENSIONS_H_ -#include "symrepr.h" #include "heap.h" #include "lbm_types.h" #include "lbm_constants.h" @@ -29,9 +28,6 @@ extern "C" { #endif -#define LBM_EXTENSION(name, argv, argn) \ - __attribute__((aligned(LBM_STORABLE_ADDRESS_ALIGNMENT))) lbm_value name(lbm_value *(argv), lbm_uint (argn)) - /** Type representing an extension function. * \param Pointer to array of lbm_values. * \param Number of arguments. @@ -39,12 +35,25 @@ extern "C" { */ typedef lbm_value (*extension_fptr)(lbm_value*,lbm_uint); +/** Type representing an entry in the extension table + */ +typedef struct { + extension_fptr fptr; + char *name; +} lbm_extension_t; + + +extern lbm_extension_t *extension_table; + +#define LBM_EXTENSION(name, argv, argn) \ + __attribute__((aligned(LBM_STORABLE_ADDRESS_ALIGNMENT))) lbm_value name(lbm_value *(argv), lbm_uint (argn)) + /** Initialize the extensions subsystem. Extension storage is allocated on lbm_memory. * * \param extension_storage_size Size of function pointer array. * \return 1 on success and 0 for failure */ - int lbm_extensions_init(extension_fptr *extension_storage, lbm_uint extension_storage_size); + int lbm_extensions_init(lbm_extension_t *extension_storage, lbm_uint extension_storage_size); /** The number of extensions that can be allocated. * \return The maximum number of extensions that can be added. */ @@ -53,6 +62,12 @@ lbm_uint lbm_get_max_extensions(void); * \return The number of extensions that have been added. */ lbm_uint lbm_get_num_extensions(void); +/** Lookup an extensions index. + * \param sym_str Extension name to look up. + * \param ix Pointer used to store result. + * \return true on success, false otherwise. + */ +bool lbm_lookup_extension_id(char *sym_str, lbm_uint *ix); /** Look up an extension associated with a key symbol. * * \param sym Symbol bound to the extension to look for. @@ -113,6 +128,8 @@ bool lbm_check_argn_number(lbm_value *args, lbm_uint argn, lbm_uint n); #define LBM_CHECK_ARGN(n) if (!lbm_check_argn(argn, n)) {return ENC_SYM_EERROR;} #define LBM_CHECK_ARGN_NUMBER(n) if (!lbm_check_argn_number(args, argn, n)) {return ENC_SYM_EERROR;} +lbm_value lbm_extensions_default(lbm_value *args, lbm_uint argn); + #ifdef __cplusplus } #endif diff --git a/include/heap.h b/include/heap.h index f6fe29ad..0ca7fa2a 100644 --- a/include/heap.h +++ b/include/heap.h @@ -622,11 +622,16 @@ int lbm_lift_array(lbm_value *value, char *data, lbm_uint num_elt); * \return -1 for failure or length of array. */ lbm_int lbm_heap_array_get_size(lbm_value arr); -/** Get a pointer to the data of an array. +/** Get a pointer to the data of an array for read only purposes. * \param arr lbm_value array to get pointer from. * \return NULL or valid pointer. */ -uint8_t *lbm_heap_array_get_data(lbm_value arr); +const uint8_t *lbm_heap_array_get_data_ro(lbm_value arr); +/** Get a pointer to the data of an array for read/write purposes. + * \param arr lbm_value array to get pointer from. + * \return NULL or valid pointer. + */ +uint8_t *lbm_heap_array_get_data_rw(lbm_value arr); /** Explicitly free an array. * This function needs to be used with care and knowledge. * \param arr Array value. diff --git a/include/lbm_flat_value.h b/include/lbm_flat_value.h index 25e5fe67..eb57c1a0 100644 --- a/include/lbm_flat_value.h +++ b/include/lbm_flat_value.h @@ -57,8 +57,8 @@ typedef struct { bool lbm_start_flatten(lbm_flat_value_t *v, size_t buffer_size); bool lbm_finish_flatten(lbm_flat_value_t *v); bool f_cons(lbm_flat_value_t *v); -bool f_sym(lbm_flat_value_t *v, lbm_value sym); -bool f_sym_string(lbm_flat_value_t *v, lbm_value sym); +bool f_sym(lbm_flat_value_t *v, lbm_uint sym_id); +bool f_sym_string(lbm_flat_value_t *v, char *str); bool f_i(lbm_flat_value_t *v, lbm_int i); bool f_u(lbm_flat_value_t *v, lbm_uint u); bool f_b(lbm_flat_value_t *v, uint8_t b); diff --git a/include/lispbm.h b/include/lispbm.h index f5feb186..16e41b6b 100644 --- a/include/lispbm.h +++ b/include/lispbm.h @@ -54,7 +54,7 @@ extern "C" { * \param bitmap_size Size of the memory meta-data array. * \param gc_stack_size Size in number of lbm_uint values to use for the GC stack. * \param print_stack_size Size in number of lbm_uint values of the print stack. - * \param extension_storage Array of extension function pointers. + * \param extension_storage Array of lbm_extension_t pointers. * \param extension_storage_size Size of extension array. * \return 1 on success and 0 on failure. */ @@ -64,7 +64,7 @@ int lbm_init(lbm_cons_t *heap_storage, lbm_uint heap_size, lbm_uint *memory_bitmap, lbm_uint bitmap_size, lbm_uint gc_stack_size, lbm_uint print_stack_size, - extension_fptr *extension_storage, + lbm_extension_t *extension_storage, lbm_uint extension_storage_size); #ifdef __cplusplus diff --git a/include/symrepr.h b/include/symrepr.h index 49fd4b8b..9f7c4a43 100644 --- a/include/symrepr.h +++ b/include/symrepr.h @@ -93,22 +93,12 @@ int lbm_add_variable_symbol_const(char *name, lbm_uint* id); * \return 1 for success and 0 for failure. */ int lbm_add_symbol_const(char *name, lbm_uint *id); -/** Add an extension symbol to the symbol table. - * The name is assumed to be a statically allocated constant. +/** Get a pointer to the list entry holding a symbol name, id mapping. * - * \param name Name of the symbol. - * \param id Resulting id is returned through this argument. - * \return 1 for success and 0 for failure. + * \param name Name string to look up. + * \return Pointer to list entry or NULL. */ -int lbm_add_extension_symbol(char *name, lbm_uint* id); -/** Add an extension symbol to the symbol table. - * The name is assumed to be statically allocated. - * - * \param name Statically allocated name string. - * \param id Resulting id is returned through this argument. - * \return 1 for success and 0 for failure. - */ -int lbm_add_extension_symbol_const(char *name, lbm_uint* id); +lbm_uint *lbm_get_symbol_list_entry_by_name(char *name); /** Look up an id from the symbol table given a name. * * \param name Name string to look up. @@ -144,6 +134,17 @@ lbm_uint lbm_get_symbol_table_size_names(void); */ lbm_uint lbm_get_symbol_table_size_names_flash(void); +/** Check if a symbol name is stored in flash. + * \param str Symbol name string. + * \return True if symbol name is stored in flash, otherwise False. + */ +bool lbm_symbol_in_flash(char *str); +/** Check if a symbol name id mapping list entry is stored in flash. + * \param str Symbol name string. + * \return True if symbol name/id mapping list entry is stored in flash, otherwise False. + */ +bool lbm_symbol_list_entry_in_flash(char *str); + extern lbm_value symbol_x; extern lbm_value symbol_y; diff --git a/src/eval_cps.c b/src/eval_cps.c index b72c13b8..d2b90aa1 100644 --- a/src/eval_cps.c +++ b/src/eval_cps.c @@ -3557,7 +3557,21 @@ static void cont_read_next_token(eval_context_t *ctx) { } else { int r = 0; if (strncmp(tokpar_sym_str,"ext-",4) == 0) { - r = lbm_add_extension_symbol(tokpar_sym_str, &symbol_id); + if (!lbm_lookup_extension_id(tokpar_sym_str, &symbol_id)) { + char *ext_name = lbm_malloc(strlen(tokpar_sym_str) + 1); + if (!ext_name) { + gc(); + ext_name = lbm_malloc(strlen(tokpar_sym_str) + 1); + } + if (ext_name) { + lbm_uint ext_id; + r = lbm_add_extension(ext_name, lbm_extensions_default); + if (!lbm_lookup_extension_id(ext_name, &ext_id)) { + error_ctx(ENC_SYM_FATAL_ERROR); + } + symbol_id = ext_id + EXTENSION_SYMBOLS_START; + } + } } else { if (ctx->flags & EVAL_CPS_CONTEXT_FLAG_CONST_SYMBOL_STRINGS && ctx->flags & EVAL_CPS_CONTEXT_FLAG_INCREMENTAL_READ) { diff --git a/src/extensions.c b/src/extensions.c index 0f1e1ea3..34102816 100644 --- a/src/extensions.c +++ b/src/extensions.c @@ -28,7 +28,9 @@ static lbm_uint ext_offset = EXTENSION_SYMBOLS_START; static lbm_uint ext_max = 0; static lbm_uint ext_num = 0; -static extension_fptr *extension_table = NULL; +static lbm_uint next_extension_ix = 0; + +lbm_extension_t *extension_table = NULL; lbm_value lbm_extensions_default(lbm_value *args, lbm_uint argn) { (void)args; @@ -36,16 +38,17 @@ lbm_value lbm_extensions_default(lbm_value *args, lbm_uint argn) { return ENC_SYM_EERROR; } -int lbm_extensions_init(extension_fptr *extension_storage, lbm_uint extension_storage_size) { +int lbm_extensions_init(lbm_extension_t *extension_storage, lbm_uint extension_storage_size) { if (extension_storage == NULL || extension_storage_size == 0) return 0; extension_table = extension_storage; - memset(extension_table, 0, sizeof(extension_fptr) * extension_storage_size); + memset(extension_table, 0, sizeof(lbm_extension_t) * extension_storage_size); for (lbm_uint i = 0; i < extension_storage_size; i ++) { - extension_storage[i] = lbm_extensions_default; + extension_storage[i].fptr = lbm_extensions_default; } + next_extension_ix = 0; ext_max = (lbm_uint)extension_storage_size; return 1; @@ -64,7 +67,7 @@ extension_fptr lbm_get_extension(lbm_uint sym) { if (ext_next >= ext_max) { return NULL; } - return extension_table[ext_next]; + return extension_table[ext_next].fptr; } bool lbm_clr_extension(lbm_uint sym_id) { @@ -72,34 +75,49 @@ bool lbm_clr_extension(lbm_uint sym_id) { if (ext_id >= ext_max) { return false; } - extension_table[ext_id] = lbm_extensions_default; + extension_table[ext_id].name = NULL; + extension_table[ext_id].fptr = lbm_extensions_default; return true; } +bool lbm_lookup_extension_id(char *sym_str, lbm_uint *ix) { + for (lbm_uint i = 0; i < ext_max; i ++) { + if(extension_table[i].name) { + if (strcmp(extension_table[i].name, sym_str) == 0) { + *ix = i; + return true; + } + } + } + return false; +} + bool lbm_add_extension(char *sym_str, extension_fptr ext) { lbm_value symbol; - lbm_uint ext_ix = 0; + // Check if symbol is in traditional symbol table. if (lbm_get_symbol_by_name(sym_str, &symbol)) { - // symbol already exists and may or may not be an extension. if (lbm_is_extension(lbm_enc_sym(symbol))) { - ext_ix = symbol - ext_offset; - } else return false; - } else { - int res = lbm_add_extension_symbol_const(sym_str, &symbol); - if (!res) return false; - ext_ix = symbol - ext_offset; + // update the extension entry. + if (strcmp(extension_table[symbol - ext_offset].name, sym_str) == 0) { + // Do not replace name ptr. + extension_table[symbol - ext_offset].fptr = ext; + return true; + } + } + return false; } - if (ext_ix >= ext_max) { - return false; + if (next_extension_ix < ext_max) { + lbm_uint sym_ix = next_extension_ix; + next_extension_ix ++; + extension_table[sym_ix].name = sym_str; + extension_table[sym_ix].fptr = ext; + return true; } - ext_num = ext_ix + 1; - extension_table[ext_ix] = ext; - return true; + return false; } - // Helpers for extension developers: static bool lbm_is_number_all(lbm_value *args, lbm_uint argn) { diff --git a/src/fundamental.c b/src/fundamental.c index 6f6166c0..e30219e8 100644 --- a/src/fundamental.c +++ b/src/fundamental.c @@ -736,8 +736,9 @@ static lbm_value fundamental_undefine(lbm_value *args, lbm_uint nargs, eval_cont } curr = lbm_cdr(curr); } + return ENC_SYM_TRUE; } - return ENC_SYM_TRUE; + return ENC_SYM_TERROR; } static lbm_value fundamental_buf_create(lbm_value *args, lbm_uint nargs, eval_context_t *ctx) { diff --git a/src/heap.c b/src/heap.c index 1c5fdd9e..b35eb279 100644 --- a/src/heap.c +++ b/src/heap.c @@ -722,6 +722,19 @@ void lbm_gc_mark_phase(lbm_value root) { if (t_ptr >= LBM_NON_CONS_POINTER_TYPE_FIRST && t_ptr <= LBM_NON_CONS_POINTER_TYPE_LAST) continue; + if (cell->car == ENC_SYM_CONT) { + lbm_value cont = cell->cdr; + lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(cont); + lbm_value *arrdata = (lbm_value *)arr->data; + for (lbm_uint i = 0; i < arr->size / 4; i ++) { + if (lbm_is_ptr(arrdata[i]) && + !((arrdata[i] & LBM_CONTINUATION_INTERNAL) == LBM_CONTINUATION_INTERNAL)) { + if (!lbm_push (s, arrdata[i])) { + lbm_critical_error(); + } + } + } + } if (lbm_is_ptr(cell->cdr)) { if (!lbm_push(s, cell->cdr)) { lbm_critical_error(); @@ -1168,7 +1181,7 @@ int lbm_lift_array(lbm_value *value, char *data, lbm_uint num_elt) { lbm_int lbm_heap_array_get_size(lbm_value arr) { int r = -1; - if (lbm_is_array_rw(arr)) { + if (lbm_is_array_r(arr)) { lbm_array_header_t *header = (lbm_array_header_t*)lbm_car(arr); if (header == NULL) { return r; @@ -1178,7 +1191,19 @@ lbm_int lbm_heap_array_get_size(lbm_value arr) { return r; } -uint8_t *lbm_heap_array_get_data(lbm_value arr) { +const uint8_t *lbm_heap_array_get_data_ro(lbm_value arr) { + uint8_t *r = NULL; + if (lbm_is_array_r(arr)) { + lbm_array_header_t *header = (lbm_array_header_t*)lbm_car(arr); + if (header == NULL) { + return r; + } + r = (uint8_t*)header->data; + } + return r; +} + +uint8_t *lbm_heap_array_get_data_rw(lbm_value arr) { uint8_t *r = NULL; if (lbm_is_array_rw(arr)) { lbm_array_header_t *header = (lbm_array_header_t*)lbm_car(arr); diff --git a/src/lbm_flat_value.c b/src/lbm_flat_value.c index 26a79713..23c33ba7 100644 --- a/src/lbm_flat_value.c +++ b/src/lbm_flat_value.c @@ -100,9 +100,8 @@ bool f_cons(lbm_flat_value_t *v) { return false; } -bool f_sym(lbm_flat_value_t *v, lbm_value sym) { +bool f_sym(lbm_flat_value_t *v, lbm_uint sym_id) { bool res = true; - lbm_uint sym_id = lbm_dec_sym(sym); res = res && write_byte(v,S_SYM_VALUE); #ifndef LBM64 res = res && write_word(v,sym_id); @@ -112,21 +111,16 @@ bool f_sym(lbm_flat_value_t *v, lbm_value sym) { return res; } -bool f_sym_string(lbm_flat_value_t *v, lbm_value sym) { +bool f_sym_string(lbm_flat_value_t *v, char *str) { bool res = true; - char *sym_str; - if (lbm_is_symbol(sym)) { - lbm_uint s = lbm_dec_sym(sym); - sym_str = (char*)lbm_get_name_by_symbol(s); - if (sym_str) { - lbm_uint sym_bytes = strlen(sym_str) + 1; - res = res && write_byte(v, S_SYM_STRING); - if (res && v->buf_size >= v->buf_pos + sym_bytes) { - for (lbm_uint i = 0; i < sym_bytes; i ++ ) { - res = res && write_byte(v, (uint8_t)sym_str[i]); - } - return res; + if (str) { + lbm_uint sym_bytes = strlen(str) + 1; + res = res && write_byte(v, S_SYM_STRING); + if (res && v->buf_size >= v->buf_pos + sym_bytes) { + for (lbm_uint i = 0; i < sym_bytes; i ++) { + res = res && write_byte(v, (uint8_t)str[i]); } + return res; } } return false; @@ -358,16 +352,17 @@ int flatten_value_internal(lbm_flat_value_t *fv, lbm_value v) { return FLATTEN_VALUE_OK; } break; - case LBM_TYPE_SYMBOL: - if (f_sym_string(fv, v)) { + case LBM_TYPE_SYMBOL: { + char *sym_str = (char*)lbm_get_name_by_symbol(lbm_dec_sym(v)); + if (f_sym_string(fv, sym_str)) { return FLATTEN_VALUE_OK; } - break; + } break; case LBM_TYPE_ARRAY: { lbm_int s = lbm_heap_array_get_size(v); - uint8_t *d = lbm_heap_array_get_data(v); + const uint8_t *d = lbm_heap_array_get_data_ro(v); if (s > 0 && d != NULL) { - if (f_lbm_array(fv, (lbm_uint)s, d)) { + if (f_lbm_array(fv, (lbm_uint)s, (uint8_t*)d)) { return FLATTEN_VALUE_OK; } } else { diff --git a/src/lispbm.c b/src/lispbm.c index 66144d6c..a7977537 100644 --- a/src/lispbm.c +++ b/src/lispbm.c @@ -22,7 +22,7 @@ int lbm_init(lbm_cons_t *heap_storage, lbm_uint heap_size, lbm_uint *memory_bitmap, lbm_uint bitmap_size, lbm_uint gc_stack_size, lbm_uint print_stack_size, - extension_fptr *extension_storage, + lbm_extension_t *extension_storage, lbm_uint extension_storage_size) { if (lbm_memory_init(memory, memory_size, diff --git a/src/symrepr.c b/src/symrepr.c index 9e156b1e..4226e2ca 100644 --- a/src/symrepr.c +++ b/src/symrepr.c @@ -24,6 +24,7 @@ #include #include #include "symrepr.h" +#include "extensions.h" #define NUM_SPECIAL_SYMBOLS (sizeof(special_symbols) / sizeof(special_sym)) #define NAME 0 @@ -49,8 +50,6 @@ special_sym const special_symbols[] = { {"read" , SYM_READ}, {"read-program" , SYM_READ_PROGRAM}, {"read-eval-program", SYM_READ_AND_EVAL_PROGRAM}, - //{"comma" , SYM_COMMA}, // should not be accessible to programmer - //{"splice" , SYM_COMMAAT}, {"match" , SYM_MATCH}, {"_" , SYM_DONTCARE}, {"send" , SYM_SEND}, @@ -228,9 +227,7 @@ special_sym const special_symbols[] = { static lbm_uint *symlist = NULL; static lbm_uint next_symbol_id = RUNTIME_SYMBOLS_START; -static lbm_uint next_extension_symbol_id = EXTENSION_SYMBOLS_START; static lbm_uint next_variable_symbol_id = VARIABLE_SYMBOLS_START; - static lbm_uint symbol_table_size_list = 0; static lbm_uint symbol_table_size_list_flash = 0; static lbm_uint symbol_table_size_strings = 0; @@ -242,7 +239,6 @@ lbm_value symbol_y = ENC_SYM_NIL; int lbm_symrepr_init(void) { symlist = NULL; next_symbol_id = RUNTIME_SYMBOLS_START; - next_extension_symbol_id = EXTENSION_SYMBOLS_START; next_variable_symbol_id = VARIABLE_SYMBOLS_START; symbol_table_size_list = 0; symbol_table_size_list_flash = 0; @@ -287,10 +283,29 @@ const char *lbm_get_name_by_symbol(lbm_uint id) { return (special_symbols[i].name); } } + return NULL; + } else if (id - EXTENSION_SYMBOLS_START < EXTENSION_SYMBOLS_END) { + unsigned int ext_id = id - EXTENSION_SYMBOLS_START; + if (ext_id < lbm_get_max_extensions()) { + return extension_table[ext_id].name; + } + return NULL; } return lookup_symrepr_name_memory(id); } +lbm_uint *lbm_get_symbol_list_entry_by_name(char *name) { + lbm_uint *curr = symlist; + while (curr) { + char *str = (char*)curr[NAME]; + if (strcmp(name, str) == 0) { + return (lbm_uint *)curr; + } + curr = (lbm_uint*)curr[NEXT]; + } + return NULL; +} + // Lookup symbol id given symbol name int lbm_get_symbol_by_name(char *name, lbm_uint* id) { @@ -302,6 +317,14 @@ int lbm_get_symbol_by_name(char *name, lbm_uint* id) { } } + // loop through extensions + for (unsigned int i = 0; i < lbm_get_max_extensions(); i ++) { + if (extension_table[i].name && strcmp(name, extension_table[i].name) == 0) { + *id = EXTENSION_SYMBOLS_START + i; + return 1; + } + } + lbm_uint *curr = symlist; while (curr) { char *str = (char*)curr[NAME]; @@ -424,35 +447,6 @@ int lbm_str_to_symbol(char *name, lbm_uint *sym_id) { return 0; } -int lbm_add_extension_symbol(char *name, lbm_uint* id) { - - if (next_extension_symbol_id >= EXTENSION_SYMBOLS_END) return 0; - lbm_uint symbol_name_storage; - if (!store_symbol_name(name, &symbol_name_storage)) return 0; - - if (!add_symbol_to_symtab(symbol_name_storage, next_extension_symbol_id)) { - lbm_memory_free((lbm_uint*)symbol_name_storage); - return 0; - } - - *id = next_extension_symbol_id ++; - - return 1; -} - -int lbm_add_extension_symbol_const(char *name, lbm_uint* id) { - - if (next_extension_symbol_id >= EXTENSION_SYMBOLS_END) return 0; - - if (!add_symbol_to_symtab((lbm_uint)name, next_extension_symbol_id)) { - return 0; - } - - *id = next_extension_symbol_id ++; - - return 1; -} - lbm_uint lbm_get_symbol_table_size(void) { return (symbol_table_size_list + symbol_table_size_strings) * sizeof(lbm_uint); @@ -474,3 +468,12 @@ lbm_uint lbm_get_symbol_table_size_names_flash(void) { lbm_uint lbm_get_num_variables(void) { return next_variable_symbol_id - VARIABLE_SYMBOLS_START; } + +bool lbm_symbol_in_flash(char *str) { + return !lbm_memory_ptr_inside((lbm_uint*)str); +} + +bool lbm_symbol_list_entry_in_flash(char *str) { + lbm_uint *entry = lbm_get_symbol_list_entry_by_name(str); + return (entry == NULL || !lbm_memory_ptr_inside(entry)); +} diff --git a/tests/test_lisp_code_cps.c b/tests/test_lisp_code_cps.c index 56fb458c..4f0cd7b0 100644 --- a/tests/test_lisp_code_cps.c +++ b/tests/test_lisp_code_cps.c @@ -46,7 +46,7 @@ #define FAIL 0 #define SUCCESS 1 -extension_fptr extensions[EXTENSION_STORAGE_SIZE]; +lbm_extension_t extensions[EXTENSION_STORAGE_SIZE]; lbm_uint constants_memory[CONSTANT_MEMORY_SIZE]; @@ -223,7 +223,7 @@ LBM_EXTENSION(ext_event_sym, args, argn) { if (argn == 1 && lbm_is_symbol(args[0])) { lbm_flat_value_t v; if (lbm_start_flatten(&v, 1 + sizeof(lbm_uint) + 20)) { - f_sym(&v, args[0]); + f_sym(&v, lbm_dec_sym(args[0])); lbm_finish_flatten(&v); lbm_event(&v); res = ENC_SYM_TRUE; @@ -274,7 +274,7 @@ LBM_EXTENSION(ext_event_array, args, argn) { lbm_flat_value_t v; if (lbm_start_flatten(&v, 100)) { f_cons(&v); - f_sym(&v,args[0]); + f_sym(&v,lbm_dec_sym(args[0])); f_lbm_array(&v, 12, (uint8_t*)hello); lbm_finish_flatten(&v); lbm_event(&v); @@ -298,7 +298,7 @@ LBM_EXTENSION(ext_unblock, args, argn) { lbm_cid c = lbm_dec_as_i32(args[0]); lbm_flat_value_t v; if (lbm_start_flatten(&v, 1 + sizeof(lbm_uint))) { - f_sym(&v, ENC_SYM_TRUE); + f_sym_string(&v, "t"); lbm_finish_flatten(&v); lbm_unblock_ctx(c,&v); res = ENC_SYM_TRUE; @@ -313,7 +313,7 @@ LBM_EXTENSION(ext_unblock_error, args, argn) { lbm_cid c = lbm_dec_as_i32(args[0]); lbm_flat_value_t v; if (lbm_start_flatten(&v, 1 + sizeof(lbm_uint))) { - f_sym(&v, ENC_SYM_EERROR); + f_sym(&v, SYM_EERROR); lbm_finish_flatten(&v); lbm_unblock_ctx(c,&v); res = ENC_SYM_TRUE;