Squashed 'lispBM/lispBM/' changes from 55553f58..d2f9c684

d2f9c684 removed the variable memory as the new hash-env seems to perform well enough
128813bf coercing all 64bit tests to pass
fc62e27a adding an nfibs example in experiment_repl
ecdb2720 extensions table outside of lbm memory, rearrange of lbm_init arguments
a95f3c94 moved variable storage to lbm memory
6d3af68d tiny tweak
c789fcc4 Work in progress moving stacks into lbm_memory

git-subtree-dir: lispBM/lispBM
git-subtree-split: d2f9c68466cc0cb19446bc3a9a06e5855b1690b0
This commit is contained in:
Benjamin Vedder 2024-01-08 21:54:33 +01:00
parent 999e87e80d
commit 070e5cde9b
29 changed files with 211 additions and 450 deletions

View File

@ -134,7 +134,6 @@ LBMSRC = ../../src/env.c \
../../src/tokpar.c \
../../src/lispbm.c \
../../src/lbm_c_interop.c \
../../src/lbm_variables.c \
../../src/lbm_custom_type.c \
../../src/lbm_channel.c \
../../src/lbm_flags.c \

View File

@ -40,12 +40,10 @@
#define GC_STACK_SIZE 256
#define PRINT_STACK_SIZE 256
#define HEAP_SIZE 4096
#define VARIABLE_STORAGE_SIZE 256
#define EXTENSION_STORAGE_SIZE 256
extension_fptr extensions[EXTENSION_STORAGE_SIZE];
uint32_t print_stack_storage[PRINT_STACK_SIZE];
lbm_value variable_storage[VARIABLE_STORAGE_SIZE];
extension_fptr extension_storage[EXTENSION_STORAGE_SIZE];
static lbm_cons_t heap[HEAP_SIZE] __attribute__ ((aligned (8)));
@ -199,11 +197,12 @@ int main(void) {
chThdSleepMilliseconds(2000);
if (!lbm_init(heap, HEAP_SIZE,
GC_STACK_SIZE,
memory_array, LBM_MEMORY_SIZE_8K,
bitmap_array, LBM_MEMORY_BITMAP_SIZE_8K,
print_stack_storage, PRINT_STACK_SIZE,
extension_storage, EXTENSION_STORAGE_SIZE)) {
GC_STACK_SIZE,
PRINT_STACK_SIZE,
extensions,
EXTENSION_STORAGE_SIZE)) {
chprintf(chp,"LispBM Init failed.\r\n");
return 0;
@ -213,8 +212,6 @@ int main(void) {
lbm_set_timestamp_us_callback(timestamp_callback);
lbm_set_usleep_callback(sleep_callback);
lbm_variables_init(variable_storage, VARIABLE_STORAGE_SIZE);
res = lbm_add_extension("print", ext_print);
if (res)
chprintf(chp,"Extension added.\r\n");
@ -304,13 +301,12 @@ int main(void) {
}
lbm_init(heap, HEAP_SIZE,
GC_STACK_SIZE,
memory_array, LBM_MEMORY_SIZE_8K,
bitmap_array, LBM_MEMORY_BITMAP_SIZE_8K,
print_stack_storage, PRINT_STACK_SIZE,
extension_storage, EXTENSION_STORAGE_SIZE);
lbm_variables_init(variable_storage, VARIABLE_STORAGE_SIZE);
GC_STACK_SIZE,
PRINT_STACK_SIZE,
extensions,
EXTENSION_STORAGE_SIZE);
lbm_add_extension("print", ext_print);

View File

@ -7,10 +7,10 @@
(cons l (cons a xs))
(cons a (insert l xs)))))))
(define sort (lambda (ls)
(define isort (lambda (ls)
(match ls
(nil nil)
( ((? a) . (? xs))
(insert a (sort xs))))))
(insert a (isort xs))))))
(sort '(4 2 0 9 1 56 2 4 7))
(isort '(4 2 0 9 1 56 2 4 7))

View File

@ -9,7 +9,7 @@ headers = ('File','Eval time (s)')
benches = ['q2.lisp', 'fibonacci_tail.lisp', 'dec_cnt3.lisp',
'dec_cnt1.lisp', 'fibonacci.lisp', 'tak.lisp',
'dec_cnt2.lisp', 'insertionsort.lisp', 'tail_call_200k.lisp',
'loop_200k.lisp', 'sort500.lisp' ]
'loop_200k.lisp', 'sort500.lisp', 'env_lookup.lisp' ]
data = []

View File

@ -30,14 +30,10 @@
#define GC_STACK_SIZE 256
#define PRINT_STACK_SIZE 256
#define EXTENSION_STORAGE_SIZE 256
#define VARIABLE_STORAGE_SIZE 256
#define WAIT_TIMEOUT 2500
uint32_t print_stack_storage[PRINT_STACK_SIZE];
extension_fptr extension_storage[EXTENSION_STORAGE_SIZE];
lbm_value variable_storage[VARIABLE_STORAGE_SIZE];
/* Tokenizer state for strings */
static lbm_string_channel_state_t string_tok_state;
@ -221,10 +217,10 @@ int main(int argc, char **argv) {
}
lbm_init(heap_storage, heap_size,
GC_STACK_SIZE,
memory, LBM_MEMORY_SIZE_16K,
bitmap, LBM_MEMORY_BITMAP_SIZE_16K,
print_stack_storage, PRINT_STACK_SIZE,
GC_STACK_SIZE,
PRINT_STACK_SIZE,
extension_storage, EXTENSION_STORAGE_SIZE);
lbm_set_ctx_done_callback(done_callback);

View File

@ -0,0 +1,27 @@
(define fib (lambda (n)
(if (< n 2) 1
(+ (fib (- n 2)) (fib (- n 1)) 1))))
(def num (fib 20))
(def start (time))
(loop (( a 0 )) (< a 1000)
{
(fib 20)
(setq a (+ a 1))
}
)
(def end (time))
(print (/ ( * 1000 num) (/ (- end start) 1000000.0)) " fibs / seconds")

View File

@ -1,5 +1,5 @@
/*
Copyright 2018, 2021, 2022 Joel Svensson svenssonjoel@yahoo.se
Copyright 2018, 2021, 2022, 2024 Joel Svensson svenssonjoel@yahoo.se
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -41,15 +41,12 @@
#define GC_STACK_SIZE 256
#define PRINT_STACK_SIZE 256
#define EXTENSION_STORAGE_SIZE 256
#define VARIABLE_STORAGE_SIZE 256
#define WAIT_TIMEOUT 2500
#define STR_SIZE 1024
#define CONSTANT_MEMORY_SIZE 32*1024
#define PROF_DATA_NUM 100
lbm_uint print_stack_storage[PRINT_STACK_SIZE];
extension_fptr extension_storage[EXTENSION_STORAGE_SIZE];
lbm_value variable_storage[VARIABLE_STORAGE_SIZE];
extension_fptr extensions[EXTENSION_STORAGE_SIZE];
lbm_uint constants_memory[CONSTANT_MEMORY_SIZE];
lbm_prof_t prof_data[100];
@ -627,11 +624,12 @@ int main(int argc, char **argv) {
}
if (!lbm_init(heap_storage, heap_size,
GC_STACK_SIZE,
memory, LBM_MEMORY_SIZE_1M,
bitmap, LBM_MEMORY_BITMAP_SIZE_1M,
print_stack_storage, PRINT_STACK_SIZE,
extension_storage, EXTENSION_STORAGE_SIZE)) {
GC_STACK_SIZE,
PRINT_STACK_SIZE,
extensions,
EXTENSION_STORAGE_SIZE)) {
printf("Failed to initialize LispBM\n");
return 0;
}
@ -657,8 +655,6 @@ int main(int argc, char **argv) {
lbm_set_dynamic_load_callback(dyn_load);
lbm_set_printf_callback(error_print);
lbm_variables_init(variable_storage, VARIABLE_STORAGE_SIZE);
if (lbm_array_extensions_init()) {
printf("Array extensions loaded\n");
} else {
@ -825,13 +821,6 @@ int main(int argc, char **argv) {
printf(" %s\r\n",output);
}
}
printf("Variables:\r\n");
for (int i = 0; i < lbm_get_num_variables(); i ++) {
const char *name = lbm_get_variable_name_by_index(i);
lbm_print_value(output,1024, lbm_get_variable_by_index(i));
printf(" %s = %s\r\n", name ? name : "error", output);
}
free(str);
}else if (n >= 5 && strncmp(str, ":load", 5) == 0) {
@ -895,11 +884,12 @@ int main(int argc, char **argv) {
}
lbm_init(heap_storage, heap_size,
GC_STACK_SIZE,
memory, LBM_MEMORY_SIZE_1M,
bitmap, LBM_MEMORY_BITMAP_SIZE_1M,
print_stack_storage, PRINT_STACK_SIZE,
extension_storage, EXTENSION_STORAGE_SIZE);
GC_STACK_SIZE,
PRINT_STACK_SIZE,
extensions,
EXTENSION_STORAGE_SIZE);
if (!lbm_const_heap_init(const_heap_write,
&const_heap,constants_memory,
@ -909,8 +899,6 @@ int main(int argc, char **argv) {
printf("Constants memory initialized\n");
}
lbm_variables_init(variable_storage, VARIABLE_STORAGE_SIZE);
if (lbm_array_extensions_init()) {
printf("Array extensions loaded\n");
} else {
@ -945,11 +933,12 @@ int main(int argc, char **argv) {
}
lbm_init(heap_storage, heap_size,
GC_STACK_SIZE,
memory, LBM_MEMORY_SIZE_1M,
bitmap, LBM_MEMORY_BITMAP_SIZE_1M,
print_stack_storage, PRINT_STACK_SIZE,
extension_storage, EXTENSION_STORAGE_SIZE);
GC_STACK_SIZE,
PRINT_STACK_SIZE,
extensions,
EXTENSION_STORAGE_SIZE);
if (!lbm_const_heap_init(const_heap_write,
&const_heap,constants_memory,
@ -959,8 +948,6 @@ int main(int argc, char **argv) {
printf("Constants memory initialized\n");
}
lbm_variables_init(variable_storage, VARIABLE_STORAGE_SIZE);
if (lbm_array_extensions_init()) {
printf("Array extensions loaded\n");
} else {

View File

@ -1,6 +1,6 @@
/** \file extensions.h */
/*
Copyright 2019, 2022 Joel Svensson svenssonjoel@yahoo.se
Copyright 2019, 2022, 2024 Joel Svensson svenssonjoel@yahoo.se
2022 Benjamin Vedder
This program is free software: you can redistribute it and/or modify
@ -39,13 +39,12 @@ extern "C" {
*/
typedef lbm_value (*extension_fptr)(lbm_value*,lbm_uint);
/** Initialize the extensions subsystem.
/** Initialize the extensions subsystem. Extension storage is allocated on lbm_memory.
*
* \param extension_storage Pointer to array of extension_fptr.
* \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, int extension_storage_size);
int lbm_extensions_init(extension_fptr *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.
*/

View File

@ -26,7 +26,6 @@
#include "heap.h"
#include "tokpar.h"
#include "lbm_memory.h"
#include "lbm_variables.h"
#include "heap.h"
#include "lbm_types.h"
#include "lbm_channel.h"

View File

@ -83,6 +83,7 @@
#define LBM_VAL_MASK (lbm_uint)0xFFFFFFFFFFFFFF00
#define LBM_VAL_TYPE_MASK (lbm_uint)0xFC
#define LBM_TYPE_MASK (lbm_uint)0xF8000000000000FC
#define LBM_NUMBER_MASK (lbm_uint)0x0800000000000000
// gc ptr
#define LBM_TYPE_SYMBOL (lbm_uint)0x0 // 00 00 00 0 0
#define LBM_TYPE_CHAR (lbm_uint)0x4 // 00 00 01 0 0

View File

@ -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_uint sym);
bool f_sym_string(lbm_flat_value_t *v, lbm_uint sym);
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_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);

View File

@ -47,6 +47,7 @@ typedef uint32_t lbm_type;
typedef uint32_t lbm_uint;
typedef int32_t lbm_int;
typedef float lbm_float;
typedef double lbm_double;
#define PRI_VALUE PRIu32
#define PRI_TYPE PRIu32
@ -67,7 +68,9 @@ typedef uint64_t lbm_type;
typedef uint64_t lbm_uint;
typedef int64_t lbm_int;
typedef double lbm_float;
typedef float lbm_float;
typedef double lbm_double;
#define PRI_VALUE PRIu64
#define PRI_TYPE PRIu64

View File

@ -1,75 +0,0 @@
/** \file lbm_variables.h */
/*
Copyright 2022 Joel Svensson svenssonjoel@yahoo.se
Copyright 2022 Benjamin Vedder
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LBM_VARIABLES_H_
#define LBM_VARIABLES_H_
#include "lbm_types.h"
#ifdef __cplusplus
extern "C" {
#endif
/** Initialize the variable storage area
*
* \param variable_storage Pointer to array where variables are stored.
* \param variable_storage_size Number of variables that can be stored in the array.
* \return 1 on success and 0 on failure.
*/
int lbm_variables_init(lbm_value *variable_storage, int variable_storage_size);
/** Get a pointer to the variable storage area.
*
* \return Pointer to storage area or NULL on failure.
*/
lbm_value *lbm_get_variable_table(void);
/** Get the value of a variable index.
*
* \param i Index of variable to access.
* \return Value of variable at index.
*/
lbm_value lbm_get_variable_by_index(int i);
/** Lookup what the name of the variable associated with a specific
* index in the variable storage is.
*
* \param index Index of variable of interes.
* \return Pointer to a string on success or null if no variable is associated with that index.
*/
const char *lbm_get_variable_name_by_index(int index);
/* internal use by evaluator (mostly)*/
/** Get value of variable at index.
*
* \param index variable index to access.
* \return Value of variable at index. This value if NIL if there is no binding.
*/
lbm_value lbm_get_var(lbm_uint index);
/** Set value of a variable
*
* \param index Index of variable to set.
* \paran value Value to set the variable to.
* \return Value of variable or NIL if index is out of range.
*/
lbm_value lbm_set_var(lbm_uint index, lbm_value value);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,5 +1,5 @@
/*
Copyright 2022 Joel Svensson svenssonjoel@yahoo.se
Copyright 2022, 2024 Joel Svensson svenssonjoel@yahoo.se
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -30,7 +30,6 @@
#include "lbm_memory.h"
#include "lbm_types.h"
#include "lbm_c_interop.h"
#include "lbm_variables.h"
#include "lbm_custom_type.h"
#include "lbm_channel.h"
@ -53,18 +52,20 @@ extern "C" {
* \param memory_size Size of the memory array.
* \param memory_bitmap Pointer to lbm_uint array to use for the memory subsystem meta-data.
* \param bitmap_size Size of the memory meta-data array.
* \param print_stack_storage Pointer to lbm_uint array to use as print_value stack.
* \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 Pointer to array of extension_fptr.
* \param extension_storage Array of extension function pointers.
* \param extension_storage_size Size of extension array.
* \return 1 on success and 0 on failure.
*/
int lbm_init(lbm_cons_t *heap_storage, lbm_uint heap_size,
lbm_uint gc_stack_size,
lbm_uint *memory, lbm_uint memory_size,
lbm_uint *memory_bitmap, lbm_uint bitmap_size,
lbm_uint *print_stack_storage, lbm_uint print_stack_size,
extension_fptr *extension_storage, int extension_storage_size );
lbm_uint gc_stack_size,
lbm_uint print_stack_size,
extension_fptr *extension_storage,
lbm_uint extension_storage_size);
#ifdef __cplusplus
}

View File

@ -1,5 +1,5 @@
/*
Copyright 2018, 2022 Joel Svensson svenssonjoel@yahoo.se
Copyright 2018, 2022, 2024 Joel Svensson svenssonjoel@yahoo.se
2022 Benjamin Vedder
This program is free software: you can redistribute it and/or modify
@ -37,11 +37,10 @@ bool lbm_value_is_printable_string(lbm_value v, char **str);
/** Initialize the print_value subsystem.
* print value depends on a stack and that stack is initialized here using a storage array provided by the user.
* \param print_stack_storage Array to use as storage for stack data.
* \param print_stack_size The number of uint32_t elements in the array.
* \return 1 for success and 0 for failure.
*/
int lbm_print_init(lbm_uint *print_stack_storage, lbm_uint print_stack_size);
int lbm_print_init(lbm_uint print_stack_size);
/** Print an lbm_value into a buffer provided by the user.
* If printing fails, the buffer may contain an error message.

View File

@ -123,7 +123,7 @@ int lbm_get_symbol_by_name(char *name, lbm_uint *id);
*/
const char* lbm_get_name_by_symbol(lbm_uint id);
int lbm_get_num_variables(void);
lbm_uint lbm_get_num_variables(void);
/**
*

View File

@ -12,7 +12,6 @@ LISPBM_SRC = $(LISPBM)/src/env.c \
$(LISPBM)/src/lispbm.c \
$(LISPBM)/src/eval_cps.c \
$(LISPBM)/src/lbm_c_interop.c \
$(LISPBM)/src/lbm_variables.c \
$(LISPBM)/src/lbm_custom_type.c \
$(LISPBM)/src/lbm_channel.c \
$(LISPBM)/src/lbm_flat_value.c\

View File

@ -26,7 +26,6 @@
#include "extensions.h"
#include "exp_kind.h"
#include "tokpar.h"
#include "lbm_variables.h"
#include "lbm_channel.h"
#include "print.h"
#include "platform_mutex.h"
@ -60,41 +59,40 @@ static jmp_buf critical_error_jmp_buf;
#define MATCH CONTINUATION(9)
#define APPLICATION_START CONTINUATION(10)
#define EVAL_R CONTINUATION(11)
#define SET_VARIABLE CONTINUATION(12)
#define RESUME CONTINUATION(13)
#define CLOSURE_ARGS CONTINUATION(14)
#define EXIT_ATOMIC CONTINUATION(15)
#define READ_NEXT_TOKEN CONTINUATION(16)
#define READ_APPEND_CONTINUE CONTINUATION(17)
#define READ_EVAL_CONTINUE CONTINUATION(18)
#define READ_EXPECT_CLOSEPAR CONTINUATION(19)
#define READ_DOT_TERMINATE CONTINUATION(20)
#define READ_DONE CONTINUATION(21)
#define READ_QUOTE_RESULT CONTINUATION(22)
#define READ_COMMAAT_RESULT CONTINUATION(23)
#define READ_COMMA_RESULT CONTINUATION(24)
#define READ_START_ARRAY CONTINUATION(25)
#define READ_APPEND_ARRAY CONTINUATION(26)
#define MAP CONTINUATION(27)
#define MATCH_GUARD CONTINUATION(28)
#define TERMINATE CONTINUATION(29)
#define PROGN_VAR CONTINUATION(30)
#define SETQ CONTINUATION(31)
#define MOVE_TO_FLASH CONTINUATION(32)
#define MOVE_VAL_TO_FLASH_DISPATCH CONTINUATION(33)
#define MOVE_LIST_TO_FLASH CONTINUATION(34)
#define CLOSE_LIST_IN_FLASH CONTINUATION(35)
#define QQ_EXPAND_START CONTINUATION(36)
#define QQ_EXPAND CONTINUATION(37)
#define QQ_APPEND CONTINUATION(38)
#define QQ_EXPAND_LIST CONTINUATION(39)
#define QQ_LIST CONTINUATION(40)
#define KILL CONTINUATION(41)
#define LOOP CONTINUATION(42)
#define LOOP_CONDITION CONTINUATION(43)
#define MERGE_REST CONTINUATION(44)
#define MERGE_LAYER CONTINUATION(45)
#define NUM_CONTINUATIONS 46
#define RESUME CONTINUATION(12)
#define CLOSURE_ARGS CONTINUATION(13)
#define EXIT_ATOMIC CONTINUATION(14)
#define READ_NEXT_TOKEN CONTINUATION(15)
#define READ_APPEND_CONTINUE CONTINUATION(16)
#define READ_EVAL_CONTINUE CONTINUATION(17)
#define READ_EXPECT_CLOSEPAR CONTINUATION(18)
#define READ_DOT_TERMINATE CONTINUATION(19)
#define READ_DONE CONTINUATION(20)
#define READ_QUOTE_RESULT CONTINUATION(21)
#define READ_COMMAAT_RESULT CONTINUATION(22)
#define READ_COMMA_RESULT CONTINUATION(23)
#define READ_START_ARRAY CONTINUATION(24)
#define READ_APPEND_ARRAY CONTINUATION(25)
#define MAP CONTINUATION(26)
#define MATCH_GUARD CONTINUATION(27)
#define TERMINATE CONTINUATION(28)
#define PROGN_VAR CONTINUATION(29)
#define SETQ CONTINUATION(30)
#define MOVE_TO_FLASH CONTINUATION(31)
#define MOVE_VAL_TO_FLASH_DISPATCH CONTINUATION(32)
#define MOVE_LIST_TO_FLASH CONTINUATION(33)
#define CLOSE_LIST_IN_FLASH CONTINUATION(34)
#define QQ_EXPAND_START CONTINUATION(35)
#define QQ_EXPAND CONTINUATION(36)
#define QQ_APPEND CONTINUATION(37)
#define QQ_EXPAND_LIST CONTINUATION(38)
#define QQ_LIST CONTINUATION(39)
#define KILL CONTINUATION(40)
#define LOOP CONTINUATION(41)
#define LOOP_CONDITION CONTINUATION(42)
#define MERGE_REST CONTINUATION(43)
#define MERGE_LAYER CONTINUATION(44)
#define NUM_CONTINUATIONS 45
#define FM_NEED_GC -1
#define FM_NO_MATCH -2
@ -678,7 +676,6 @@ void print_environments(char *buf, unsigned int size) {
lbm_value curr_l = ctx_running->curr_env;
printf_callback("\tCurrent local environment:\n");
while (lbm_type_of(curr_l) == LBM_TYPE_CONS) {
lbm_print_value(buf, (size/2) - 1, lbm_caar(curr_l));
lbm_print_value(buf + (size/2),size/2, lbm_cdr(lbm_car(curr_l)));
printf_callback("\t%s = %s\n", buf, buf+(size/2));
@ -689,7 +686,6 @@ void print_environments(char *buf, unsigned int size) {
lbm_value *glob_env = lbm_get_global_env();
for (int i = 0; i < GLOBAL_ENV_ROOTS; i ++) {
printf("Global Environment Ix: %d\n", i);
lbm_value curr_g = glob_env[i];;
while (lbm_type_of(curr_g) == LBM_TYPE_CONS) {
@ -1498,12 +1494,6 @@ static int gc(void) {
gc_requested = false;
lbm_gc_state_inc();
lbm_value *variables = lbm_get_variable_table();
if (variables) {
for (int i = 0; i < lbm_get_num_variables(); i ++) {
lbm_gc_mark_phase(variables[i]);
}
}
// The freelist should generally be NIL when GC runs.
lbm_nil_freelist();
lbm_value *env = lbm_get_global_env();
@ -1543,7 +1533,6 @@ int lbm_perform_gc(void) {
/* Evaluation functions */
static void eval_symbol(eval_context_t *ctx) {
lbm_uint s = lbm_dec_sym(ctx->curr_exp);
if (s >= RUNTIME_SYMBOLS_START) {
@ -1554,19 +1543,12 @@ static void eval_symbol(eval_context_t *ctx) {
ctx->app_cont = true;
return;
}
} else {
} else if (s <= EXTENSION_SYMBOLS_END) {
//special symbols and extensions can be handled the same way.
if (s <= EXTENSION_SYMBOLS_END) {
ctx->r = ctx->curr_exp;
ctx->app_cont = true;
return;
}
if (s <= VARIABLE_SYMBOLS_END) {
ctx->r = lbm_get_var(s);
ctx->app_cont = true;
return;
}
}
// Dynamic load attempt
const char *sym_str = lbm_get_name_by_symbol(s);
const char *code_str = NULL;
@ -1674,12 +1656,7 @@ static void eval_define(eval_context_t *ctx) {
sptr[0] = key;
if ((sym_val >= VARIABLE_SYMBOLS_START) &&
(sym_val < VARIABLE_SYMBOLS_END)) {
sptr[1] = SET_VARIABLE;
ctx->curr_exp = val_exp;
return;
} else if (sym_val >= RUNTIME_SYMBOLS_START) {
if (sym_val >= RUNTIME_SYMBOLS_START) {
sptr[1] = SET_GLOBAL_ENV;
if (ctx->flags & EVAL_CPS_CONTEXT_FLAG_CONST) {
stack_push(&ctx->K, MOVE_VAL_TO_FLASH_DISPATCH);
@ -2020,16 +1997,6 @@ static void cont_set_global_env(eval_context_t *ctx){
return;
}
static void cont_set_var(eval_context_t *ctx) {
lbm_value key;
lbm_value val = ctx->r;
lbm_pop(&ctx->K, &key);
ctx->r = lbm_set_var(lbm_dec_sym(key), val);
ctx->app_cont = true;
return;
}
static void cont_resume(eval_context_t *ctx) {
lbm_value exp;
lbm_value env;
@ -2089,10 +2056,7 @@ static lbm_value perform_setvar(lbm_value key, lbm_value val, lbm_value env) {
lbm_uint s = lbm_dec_sym(key);
lbm_value res = val;
if (s >= VARIABLE_SYMBOLS_START &&
s < VARIABLE_SYMBOLS_END) {
return lbm_set_var(s, val);
} else if (s >= RUNTIME_SYMBOLS_START) {
if (s >= RUNTIME_SYMBOLS_START) {
lbm_value new_env = lbm_env_modify_binding(env, key, val);
if (lbm_is_symbol(new_env) && new_env == ENC_SYM_NOT_FOUND) {
lbm_uint ix_key = lbm_dec_sym(key) & GLOBAL_ENV_MASK;
@ -3593,8 +3557,6 @@ static void cont_read_next_token(eval_context_t *ctx) {
int r = 0;
if (strncmp(tokpar_sym_str,"ext-",4) == 0) {
r = lbm_add_extension_symbol(tokpar_sym_str, &symbol_id);
} else if (tokpar_sym_str[0] == '#') {
r = lbm_add_variable_symbol(tokpar_sym_str, &symbol_id);
} else {
if (ctx->flags & EVAL_CPS_CONTEXT_FLAG_CONST_SYMBOL_STRINGS &&
ctx->flags & EVAL_CPS_CONTEXT_FLAG_INCREMENTAL_READ) {
@ -4445,7 +4407,6 @@ static const cont_fun continuations[NUM_CONTINUATIONS] =
cont_match,
cont_application_start,
cont_eval_r,
cont_set_var,
cont_resume,
cont_closure_application_args,
cont_exit_atomic,

View File

@ -1,5 +1,5 @@
/*
Copyright 2019, 2021, 2022 Joel Svensson svenssonjoel@yahoo.se
Copyright 2019, 2021, 2022, 2024 Joel Svensson svenssonjoel@yahoo.se
2022 Benjamin Vedder
This program is free software: you can redistribute it and/or modify
@ -36,13 +36,13 @@ lbm_value lbm_extensions_default(lbm_value *args, lbm_uint argn) {
return ENC_SYM_EERROR;
}
int lbm_extensions_init(extension_fptr *extension_storage, int extension_storage_size) {
if (extension_storage == NULL || extension_storage_size <= 0) return 0;
int lbm_extensions_init(extension_fptr *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) * (unsigned int)extension_storage_size);
memset(extension_table, 0, sizeof(extension_fptr) * extension_storage_size);
for (int i = 0; i < extension_storage_size; i ++) {
for (lbm_uint i = 0; i < extension_storage_size; i ++) {
extension_storage[i] = lbm_extensions_default;
}

View File

@ -20,7 +20,6 @@
#include "stack.h"
#include "heap.h"
#include "eval_cps.h"
#include "lbm_variables.h"
#include "env.h"
#include "lbm_utils.h"
#include "lbm_custom_type.h"

View File

@ -176,7 +176,7 @@ lbm_value lbm_enc_double(double x) {
return res;
#else
lbm_uint t;
memcpy(&t, &x, sizeof(lbm_float));
memcpy(&t, &x, sizeof(double));
lbm_value f = lbm_cons(t, lbm_enc_sym(SYM_RAW_F_TYPE));
if (lbm_type_of(f) == LBM_TYPE_SYMBOL) return f;
return lbm_set_ptr_type(f, LBM_TYPE_DOUBLE);
@ -435,7 +435,11 @@ double lbm_dec_as_double(lbm_value a) {
bool lbm_is_number(lbm_value x) {
lbm_uint t = lbm_type_of(x);
#ifndef LBM64
return (t & 0xC || t & LBM_NUMBER_MASK);
#else
return (t & ((uint64_t)0x1C) || t & LBM_NUMBER_MASK);
#endif
}
/****************************************************/

View File

@ -199,15 +199,6 @@ int lbm_define(char *symbol, lbm_value value) {
lbm_uint sym_id;
if (lbm_get_eval_state() == EVAL_CPS_STATE_PAUSED) {
if (strncmp(symbol, "#",1) == 0) {
if (!lbm_get_symbol_by_name(symbol, &sym_id)) {
if (!lbm_add_variable_symbol_const(symbol, &sym_id)) {
return 0;
}
}
lbm_set_var(sym_id, value);
} else {
if (!lbm_get_symbol_by_name(symbol, &sym_id)) {
if (!lbm_add_symbol_const(symbol, &sym_id)) {
return 0;
@ -217,7 +208,6 @@ int lbm_define(char *symbol, lbm_value value) {
lbm_value *glob_env = lbm_get_global_env();
glob_env[ix_key] = lbm_env_set(glob_env[ix_key], lbm_enc_sym(sym_id), value);
}
}
return res;
}

View File

@ -52,8 +52,8 @@ bool lbm_finish_flatten(lbm_flat_value_t *v) {
} else {
size_words = (v->buf_pos / sizeof(lbm_uint)) + 1;
}
if (v->buf_size <= size_words * sizeof(lbm_uint)) return true;
v->buf_size = size_words * sizeof(lbm_uint);
return (lbm_memory_shrink((lbm_uint*)v->buf, size_words) >= 0);
}
@ -100,18 +100,19 @@ bool f_cons(lbm_flat_value_t *v) {
return false;
}
bool f_sym(lbm_flat_value_t *v, lbm_uint sym) {
bool f_sym(lbm_flat_value_t *v, lbm_value sym) {
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);
res = res && write_word(v,sym_id);
#else
res = res && write_dword(v,sym);
res = res && write_dword(v,sym_id);
#endif
return res;
}
bool f_sym_string(lbm_flat_value_t *v, lbm_uint sym) {
bool f_sym_string(lbm_flat_value_t *v, lbm_value sym) {
bool res = true;
char *sym_str;
if (lbm_is_symbol(sym)) {
@ -131,7 +132,7 @@ bool f_sym_string(lbm_flat_value_t *v, lbm_uint sym) {
return false;
}
int f_sym_string_bytes(lbm_uint sym) {
int f_sym_string_bytes(lbm_value sym) {
char *sym_str;
if (lbm_is_symbol(sym)) {
lbm_uint s = lbm_dec_sym(sym);
@ -147,14 +148,22 @@ int f_sym_string_bytes(lbm_uint sym) {
bool f_i(lbm_flat_value_t *v, lbm_int i) {
bool res = true;
res = res && write_byte(v,S_I_VALUE);
#ifndef LBM64
res = res && write_word(v,(uint32_t)i);
#else
res = res && write_dword(v, (uint64_t)i);
#endif
return res;
}
bool f_u(lbm_flat_value_t *v, lbm_uint u) {
bool res = true;
res = res && write_byte(v,S_U_VALUE);
#ifndef LBM64
res = res && write_word(v,(uint32_t)u);
#else
res = res && write_dword(v,(uint64_t)u);
#endif
return res;
}
@ -558,11 +567,7 @@ static int lbm_unflatten_value_internal(lbm_flat_value_t *v, lbm_value *res) {
case S_FLOAT_VALUE: {
lbm_uint tmp;
bool b;
#ifndef LBM64
b = extract_word(v, &tmp);
#else
b = extract_dword(v, &tmp);
#endif
if (b) {
lbm_float f;
memcpy(&f, &tmp, sizeof(lbm_float));

View File

@ -1,79 +0,0 @@
/*
Copyright 2022 Joel Svensson svenssonjoel@yahoo.se
Copyright 2022 Benjamin Vedder
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lbm_variables.h"
#include "symrepr.h"
#include "heap.h"
lbm_value *variable_table = NULL;
int variable_table_size = 0;
int lbm_variables_init(lbm_value *variable_storage, int variable_storage_size) {
if (variable_storage == NULL || variable_storage_size <= 0)
return 0;
variable_table = variable_storage;
variable_table_size = variable_storage_size;
for (int i = 0; i < variable_table_size; i ++) {
variable_table[i] = ENC_SYM_NIL;
}
return 1;
}
lbm_value *lbm_get_variable_table(void) {
return variable_table;
}
lbm_value lbm_get_var(lbm_uint sym_val) {
int i = (int)sym_val - VARIABLE_SYMBOLS_START;
return lbm_get_variable_by_index(i);
}
lbm_value lbm_get_variable_by_index(int i) {
if (variable_table &&
i >= 0 &&
i < variable_table_size) {
return variable_table[i];
} else {
return ENC_SYM_NIL;
}
}
const char *lbm_get_variable_name_by_index(int index) {
if (index < 0 ||
index >= lbm_get_num_variables()) return NULL;
lbm_uint sym_id = (lbm_uint)index + VARIABLE_SYMBOLS_START;
return lbm_get_name_by_symbol(sym_id);
}
lbm_value lbm_set_var(lbm_uint index, lbm_value value) {
int i = (int)index - VARIABLE_SYMBOLS_START;
if (variable_table &&
i >= 0 &&
i < variable_table_size) {
variable_table[i] = value;
} else {
return ENC_SYM_NIL;
}
return value;
}

View File

@ -1,5 +1,5 @@
/*
Copyright 2018, 2020 Joel Svensson svenssonjoel@yahoo.se
Copyright 2018, 2020, 2024 Joel Svensson svenssonjoel@yahoo.se
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -18,14 +18,12 @@
#include "lispbm.h"
int lbm_init(lbm_cons_t *heap_storage, lbm_uint heap_size,
lbm_uint gc_stack_size,
lbm_uint *memory, lbm_uint memory_size,
lbm_uint *memory_bitmap, lbm_uint bitmap_size,
lbm_uint *print_stack_storage, lbm_uint print_stack_size,
extension_fptr *extension_storage, int extension_storage_size ) {
if (lbm_print_init(print_stack_storage, print_stack_size) == 0)
return 0;
lbm_uint gc_stack_size,
lbm_uint print_stack_size,
extension_fptr *extension_storage,
lbm_uint extension_storage_size) {
if (lbm_memory_init(memory, memory_size,
memory_bitmap, bitmap_size) == 0)
@ -37,15 +35,18 @@ int lbm_init(lbm_cons_t *heap_storage, lbm_uint heap_size,
if (lbm_heap_init(heap_storage, heap_size, gc_stack_size) == 0)
return 0;
if (lbm_print_init(print_stack_size) == 0)
return 0;
if (lbm_extensions_init(extension_storage, extension_storage_size) == 0)
return 0;
if (lbm_init_env() == 0)
return 0;
if (lbm_eval_init() == 0)
return 0;
if (lbm_extensions_init(extension_storage, extension_storage_size) == 0)
return 0;
return 1;
}

View File

@ -1,5 +1,5 @@
/*
Copyright 2018, 2020 - 2023 Joel Svensson svenssonjoel@yahoo.se
Copyright 2018, 2020 - 2024 Joel Svensson svenssonjoel@yahoo.se
2022 Benjamin Vedder
This program is free software: you can redistribute it and/or modify
@ -74,11 +74,14 @@ bool lbm_value_is_printable_string(lbm_value v, char **str) {
}
int lbm_print_init(lbm_uint *print_stack_storage, lbm_uint print_stack_size) {
int lbm_print_init(lbm_uint print_stack_size) {
if (!print_stack_storage || print_stack_size == 0)
if (print_stack_size == 0)
return 0;
lbm_uint *print_stack_storage = (lbm_uint*)lbm_malloc(print_stack_size * sizeof(lbm_uint));
if (!print_stack_storage) return 0;
if (lbm_stack_create(&print_stack, print_stack_storage, print_stack_size)) {
print_has_stack = true;
return 1;

View File

@ -424,35 +424,6 @@ int lbm_str_to_symbol(char *name, lbm_uint *sym_id) {
return 0;
}
int lbm_add_variable_symbol(char *name, lbm_uint* id) {
if (next_variable_symbol_id >= VARIABLE_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_variable_symbol_id)) {
lbm_memory_free((lbm_uint*)symbol_name_storage);
return 0;
}
*id = next_variable_symbol_id ++;
return 1;
}
int lbm_add_variable_symbol_const(char *name, lbm_uint* id) {
if (next_variable_symbol_id >= VARIABLE_SYMBOLS_END) return 0;
if (!add_symbol_to_symtab((lbm_uint)name, next_variable_symbol_id)) {
return 0;
}
*id = next_variable_symbol_id ++;
return 1;
}
int lbm_add_extension_symbol(char *name, lbm_uint* id) {
if (next_extension_symbol_id >= EXTENSION_SYMBOLS_END) return 0;
@ -500,6 +471,6 @@ lbm_uint lbm_get_symbol_table_size_names_flash(void) {
return symbol_table_size_strings_flash * sizeof(lbm_uint);
}
int lbm_get_num_variables(void) {
return (int)next_variable_symbol_id - VARIABLE_SYMBOLS_START;
lbm_uint lbm_get_num_variables(void) {
return next_variable_symbol_id - VARIABLE_SYMBOLS_START;
}

View File

@ -37,19 +37,16 @@
#define WAIT_TIMEOUT 2500
#define GC_STACK_SIZE 256
#define GC_STACK_SIZE 96
#define PRINT_STACK_SIZE 256
#define EXTENSION_STORAGE_SIZE 256
#define VARIABLE_STORAGE_SIZE 256
#define EXTENSION_STORAGE_SIZE 100
#define CONSTANT_MEMORY_SIZE 32*1024
#define FAIL 0
#define SUCCESS 1
lbm_uint print_stack_storage[PRINT_STACK_SIZE];
extension_fptr extension_storage[EXTENSION_STORAGE_SIZE];
lbm_value variable_storage[VARIABLE_STORAGE_SIZE];
extension_fptr extensions[EXTENSION_STORAGE_SIZE];
lbm_uint constants_memory[CONSTANT_MEMORY_SIZE];
@ -225,8 +222,8 @@ LBM_EXTENSION(ext_event_sym, args, argn) {
lbm_value res = ENC_SYM_EERROR;
if (argn == 1 && lbm_is_symbol(args[0])) {
lbm_flat_value_t v;
if (lbm_start_flatten(&v, 1 + sizeof(lbm_uint))) {
f_sym(&v, lbm_dec_sym(args[0]));
if (lbm_start_flatten(&v, 1 + sizeof(lbm_uint) + 20)) {
f_sym(&v, args[0]);
lbm_finish_flatten(&v);
lbm_event(&v);
res = ENC_SYM_TRUE;
@ -240,7 +237,7 @@ LBM_EXTENSION(ext_event_float, args, argn) {
if (argn == 1 && lbm_is_number(args[0])) {
float f = lbm_dec_as_float(args[0]);
lbm_flat_value_t v;
if (lbm_start_flatten(&v, 1 + sizeof(float))) {
if (lbm_start_flatten(&v, 1 + sizeof(float) + 20)) {
f_float(&v, f);
lbm_finish_flatten(&v);
lbm_event(&v);
@ -277,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,lbm_dec_sym(args[0]));
f_sym(&v,args[0]);
f_lbm_array(&v, 12, (uint8_t*)hello);
lbm_finish_flatten(&v);
lbm_event(&v);
@ -300,8 +297,8 @@ LBM_EXTENSION(ext_unblock, args, argn) {
if (argn == 1 && lbm_is_number(args[0])) {
lbm_cid c = lbm_dec_as_i32(args[0]);
lbm_flat_value_t v;
if (lbm_start_flatten(&v, 8)) {
f_sym(&v, SYM_TRUE);
if (lbm_start_flatten(&v, 1 + sizeof(lbm_uint))) {
f_sym(&v, ENC_SYM_TRUE);
lbm_finish_flatten(&v);
lbm_unblock_ctx(c,&v);
res = ENC_SYM_TRUE;
@ -315,8 +312,8 @@ LBM_EXTENSION(ext_unblock_error, args, argn) {
if (argn == 1 && lbm_is_number(args[0])) {
lbm_cid c = lbm_dec_as_i32(args[0]);
lbm_flat_value_t v;
if (lbm_start_flatten(&v, 8)) {
f_sym(&v, SYM_EERROR);
if (lbm_start_flatten(&v, 1 + sizeof(lbm_uint))) {
f_sym(&v, ENC_SYM_EERROR);
lbm_finish_flatten(&v);
lbm_unblock_ctx(c,&v);
res = ENC_SYM_TRUE;
@ -447,12 +444,12 @@ int main(int argc, char **argv) {
lbm_uint *memory = NULL;
lbm_uint *bitmap = NULL;
if (sizeof(lbm_uint) == 4) {
memory = malloc(sizeof(lbm_uint) * LBM_MEMORY_SIZE_14K);
memory = malloc(sizeof(lbm_uint) * LBM_MEMORY_SIZE_16K);
if (memory == NULL) return 0;
bitmap = malloc(sizeof(lbm_uint) * LBM_MEMORY_BITMAP_SIZE_14K);
bitmap = malloc(sizeof(lbm_uint) * LBM_MEMORY_BITMAP_SIZE_16K);
if (bitmap == NULL) return 0;
res = lbm_memory_init(memory, LBM_MEMORY_SIZE_14K,
bitmap, LBM_MEMORY_BITMAP_SIZE_14K);
res = lbm_memory_init(memory, LBM_MEMORY_SIZE_16K,
bitmap, LBM_MEMORY_BITMAP_SIZE_16K);
} else {
memory = malloc(sizeof(lbm_uint) * LBM_MEMORY_SIZE_1M);
if (memory == NULL) return 0;
@ -469,32 +466,22 @@ int main(int argc, char **argv) {
return FAIL;
}
res = lbm_print_init(print_stack_storage, PRINT_STACK_SIZE);
if (res)
printf("Printing initialized.\n");
else {
printf("Error initializing printing!\n");
return FAIL;
}
res = lbm_symrepr_init();
if (res)
printf("Symrepr initialized.\n");
else {
printf("Error initializing symrepr!\n");
return FAIL;
}
heap_storage = (lbm_cons_t*)malloc(sizeof(lbm_cons_t) * heap_size);
if (heap_storage == NULL) {
return FAIL;
}
res = lbm_heap_init(heap_storage, heap_size, GC_STACK_SIZE);
if (res)
printf("Heap initialized. Heap size: %"PRI_FLOAT" MiB. Free cons cells: %"PRI_INT"\n", (double)lbm_heap_size_bytes() / 1024.0 / 1024.0, lbm_heap_num_free());
else {
printf("Error initializing heap!\n");
if (lbm_init(heap_storage, heap_size,
memory, LBM_MEMORY_SIZE_16K,
bitmap, LBM_MEMORY_BITMAP_SIZE_16K,
GC_STACK_SIZE,
PRINT_STACK_SIZE,
extensions,
EXTENSION_STORAGE_SIZE)
) {
printf ("LBM Initialized\n");
} else {
printf ("FAILED to initialize LBM\n");
return FAIL;
}
@ -506,22 +493,6 @@ int main(int argc, char **argv) {
printf("Constants memory initialized\n");
}
res = lbm_eval_init();
if (res)
printf("Evaluator initialized.\n");
else {
printf("Error initializing evaluator.\n");
return FAIL;
}
/* res = lbm_init_env(); */
/* if (res) */
/* printf("Environment initialized.\n"); */
/* else { */
/* printf("Error initializing environment.\n"); */
/* return FAIL; */
/* } */
res = lbm_eval_init_events(20);
if (res)
printf("Events initialized.\n");
@ -530,14 +501,6 @@ int main(int argc, char **argv) {
return FAIL;
}
res = lbm_extensions_init(extension_storage, EXTENSION_STORAGE_SIZE);
if (res)
printf("Extensions initialized.\n");
else {
printf("Error initializing extensions.\n");
return FAIL;
}
if (lbm_array_extensions_init()) {
printf("Array extensions initialized.\n");
} else {
@ -688,10 +651,10 @@ int main(int argc, char **argv) {
lbm_set_usleep_callback(sleep_callback);
lbm_set_printf_callback(printf);
lbm_variables_init(variable_storage, VARIABLE_STORAGE_SIZE);
lbm_set_verbose(true);
printf("LBM memory free: %u words, %u bytes \n", lbm_memory_num_free(), lbm_memory_num_free() * sizeof(lbm_uint));
if (pthread_create(&lispbm_thd, NULL, eval_thd_wrapper, NULL)) {
printf("Error creating evaluation thread\n");
return FAIL;
@ -739,6 +702,7 @@ int main(int argc, char **argv) {
uint32_t stream_i = 0;
if (stream_source) {
int stuck_count = 0;
int i = 0;
while (true) {
if (code_buffer[i] == 0) {
@ -755,6 +719,8 @@ int main(int argc, char **argv) {
} else {
if ((stream_i % 100) == 99) {
printf("stuck streaming\n");
stuck_count ++;
if (stuck_count == 10) return 0;
}
stream_i ++;
sleep_callback(2);

View File

@ -11,7 +11,16 @@
(defun code (x) (def apa (eval `(read (str-merge "bepa" (str-from-n x))))))
;; 4 + 1 (2 or 3) bytes per string, padded to 8 bytes
;; 3 * 4 bytes per symtable entry
;; tot = (3 * 4) + 8 = 20 bytes;
;; 20bytes * 415 = 8300bytes
;; I cannot quite remember what this test is all about.
;; - It fills up lbm_memory with symbols.
;; - > 415 results in a read error if 16k lbm mem is given to test_list_code_cps.
;; Doesn't feel right.
; Create just enough symbols and symbols and arrays to trigger GC.
(repeat_eval code 432)
(repeat_eval code 415)
(check (eq apa 'bepa1))