Squashed 'lispBM/lispBM/' changes from ffbedbdf..ced143b0

ced143b0 set!
3e054ca0 troubleshooting segfaults, heap was freed (on C side before evaluation thread finished running GC

git-subtree-dir: lispBM/lispBM
git-subtree-split: ced143b0f6bb2cdc2e7a1c7575055a5fb8e0fab6
This commit is contained in:
Benjamin Vedder 2022-03-01 19:10:34 +01:00
parent 3a3ab9dae1
commit e9e8db8597
11 changed files with 239 additions and 118 deletions

View File

@ -53,6 +53,7 @@
#define SYM_MACRO_EXPAND 0x10
#define SYM_CALLCC 0x11
#define SYM_CONT 0x12
#define SYM_SETVAR 0x13
// 0x20 - 0x2F are errors
@ -64,14 +65,14 @@
#define SYM_DIVZERO 0x25
#define SYM_FATAL_ERROR 0x26 /* Runtime system is corrupt */
#define SYM_STACK_ERROR 0x27
#define SYM_RECOVERED 0x28
#define SYM_ARRAY_TYPE 0x30
#define SYM_BOXED_I_TYPE 0x31
#define SYM_BOXED_U_TYPE 0x32
#define SYM_BOXED_F_TYPE 0x33
#define SYM_REF_TYPE 0x34
#define SYM_RECOVERED 0x36
#define SYM_STREAM_TYPE 0x34
#define SYM_BYTECODE_TYPE 0x37
#define SYM_NONSENSE 0x38
@ -170,8 +171,6 @@
#define SYM_BITWISE_XOR 0x174
#define SYM_BITWISE_NOT 0x175
#define SYM_SETVAR 0x180
#define SYM_TYPE_OF 0x200
#define FUNDAMENTALS_END 0x200

View File

@ -34,83 +34,161 @@
- exec(open('gen_codes.py').read())
- print(make_c())
*/
//Total number of bits 433
#define NUM_CODES 69
#define NUM_CODES 70
#define MAX_KEY_LENGTH 6
#define MAX_CODE_LENGTH 10
#define MAX_CODE_LENGTH 7
char *codes[NUM_CODES][2] = {
{ "nil", "0011001110" },
{ "cdr", "0011001111" },
{ "car", "001100000" },
{ "cons", "001100001" },
{ "let", "001100010" },
{ "define", "001100011" },
{ "progn", "001100100" },
{ "quote", "0011001010" },
{ "list", "0011001011" },
{ "if", "0011001100" },
{ "lambda", "0011001101" },
{ "((", "1101" },
{ "))", "1110" },
{ ")", "000" },
{ "(", "1111" },
{ "?", "101010" },
{ "z", "011000" },
{ "y", "101100" },
{ "x", "101011" },
{ "w", "101001" },
{ "v", "100000" },
{ "u", "011111" },
{ "t", "110001" },
{ "s", "010110" },
{ "r", "110011" },
{ "q", "100100" },
{ "p", "100001" },
{ "o", "110000" },
{ "n", "101111" },
{ "m", "00100" },
{ "l", "101110" },
{ "k", "00101" },
{ "j", "100010" },
{ "i", "011010" },
{ "h", "100110" },
{ "g", "010111" },
{ "f", "011011" },
{ "e", "100101" },
{ "d", "101101" },
{ "c", "011110" },
{ "b", "101000" },
{ "a", "011001" },
{ "9", "1000110" },
{ "8", "1000111" },
{ "7", "1100100" },
{ "6", "1100101" },
{ "5", "1001110" },
{ "4", "1001111" },
{ "3", "0111010" },
{ "2", "0111011" },
{ "1", "0111000" },
{ "0", "0111001" },
{ "_", "0101001" },
{ ",@", "0101000" },
{ ",", "0011010" },
{ "`", "0100011" },
{ " ", "0011100" },
{ "'", "0100100" },
{ "\\", "0100101" },
{ "\"", "0011111" },
{ "#", "0100111" },
{ ".", "0100110" },
{ ">", "0011011" },
{ "<", "0011101" },
{ "=", "0011110" },
{ "/", "0101011" },
{ "*", "0100000" },
{ "-", "0101010" },
{ "+", "0100010" }
{ "9", "101001" },
{ "8", "110100" },
{ "7", "100110" },
{ "6", "100000" },
{ "5", "100111" },
{ "4", "100010" },
{ "3", "111000" },
{ "2", "110000" },
{ "1", "101100" },
{ "0", "101101" },
{ "_", "000011" },
{ ",@", "000000" },
{ ",", "000001" },
{ "`", "1111010" },
{ " ", "1111011" },
{ "'", "1110010" },
{ "\\", "1110011" },
{ "\"", "1110110" },
{ "#", "1110111" },
{ ".", "000010" },
{ ">", "1110101" },
{ "<", "1111100" },
{ "=", "1111101" },
{ "/", "1111110" },
{ "*", "1111111" },
{ "-", "1111000" },
{ "+", "1111001" },
{ "nil", "110001" },
{ "cdr", "101010" },
{ "car", "101011" },
{ "cons", "101110" },
{ "let", "101111" },
{ "define", "110010" },
{ "progn", "110011" },
{ "quote", "101000" },
{ "list", "100101" },
{ "if", "100100" },
{ "lambda", "100001" },
{ "((", "110101" },
{ "))", "100011" },
{ ")", "110110" },
{ "(", "110111" },
{ "?", "001011" },
{ "!", "011100" },
{ "z", "000111" },
{ "y", "001000" },
{ "x", "011000" },
{ "w", "011001" },
{ "v", "010000" },
{ "u", "011110" },
{ "t", "000100" },
{ "s", "000101" },
{ "r", "010100" },
{ "q", "010101" },
{ "p", "011011" },
{ "o", "001100" },
{ "n", "010111" },
{ "m", "000110" },
{ "l", "001001" },
{ "k", "001111" },
{ "j", "011111" },
{ "i", "010010" },
{ "h", "011010" },
{ "g", "001101" },
{ "f", "011101" },
{ "e", "001110" },
{ "d", "010011" },
{ "c", "010110" },
{ "b", "010001" },
{ "a", "001010" }
};
/* #define NUM_CODES 69 */
/* #define MAX_KEY_LENGTH 6 */
/* #define MAX_CODE_LENGTH 10 */
/* char *codes[NUM_CODES][2] = { */
/* { "nil", "0011001110" }, */
/* { "cdr", "0011001111" }, */
/* { "car", "001100000" }, */
/* { "cons", "001100001" }, */
/* { "let", "001100010" }, */
/* { "define", "001100011" }, */
/* { "progn", "001100100" }, */
/* { "quote", "0011001010" }, */
/* { "list", "0011001011" }, */
/* { "if", "0011001100" }, */
/* { "lambda", "0011001101" }, */
/* { "((", "1101" }, */
/* { "))", "1110" }, */
/* { ")", "000" }, */
/* { "(", "1111" }, */
/* { "?", "101010" }, */
/* { "z", "011000" }, */
/* { "y", "101100" }, */
/* { "x", "101011" }, */
/* { "w", "101001" }, */
/* { "v", "100000" }, */
/* { "u", "011111" }, */
/* { "t", "110001" }, */
/* { "s", "010110" }, */
/* { "r", "110011" }, */
/* { "q", "100100" }, */
/* { "p", "100001" }, */
/* { "o", "110000" }, */
/* { "n", "101111" }, */
/* { "m", "00100" }, */
/* { "l", "101110" }, */
/* { "k", "00101" }, */
/* { "j", "100010" }, */
/* { "i", "011010" }, */
/* { "h", "100110" }, */
/* { "g", "010111" }, */
/* { "f", "011011" }, */
/* { "e", "100101" }, */
/* { "d", "101101" }, */
/* { "c", "011110" }, */
/* { "b", "101000" }, */
/* { "a", "011001" }, */
/* { "9", "1000110" }, */
/* { "8", "1000111" }, */
/* { "7", "1100100" }, */
/* { "6", "1100101" }, */
/* { "5", "1001110" }, */
/* { "4", "1001111" }, */
/* { "3", "0111010" }, */
/* { "2", "0111011" }, */
/* { "1", "0111000" }, */
/* { "0", "0111001" }, */
/* { "_", "0101001" }, */
/* { ",@", "0101000" }, */
/* { ",", "0011010" }, */
/* { "`", "0100011" }, */
/* { " ", "0011100" }, */
/* { "'", "0100100" }, */
/* { "\\", "0100101" }, */
/* { "\"", "0011111" }, */
/* { "#", "0100111" }, */
/* { ".", "0100110" }, */
/* { ">", "0011011" }, */
/* { "<", "0011101" }, */
/* { "=", "0011110" }, */
/* { "/", "0101011" }, */
/* { "*", "0100000" }, */
/* { "-", "0101010" }, */
/* { "+", "0100010" } */
/* }; */
int match_longest_key(char *string) {
int longest_match_ix = -1;

View File

@ -585,6 +585,7 @@ static void finish_ctx(void) {
lbm_memory_free((uint32_t*)ctx_running);
ctx_running = NULL;
gc(NIL,NIL);
}
static void context_exists(eval_context_t *ctx, void *cid, void *b) {
@ -1575,6 +1576,32 @@ static inline void cont_application(eval_context_t *ctx) {
lbm_uint dfun = lbm_dec_sym(fun);
switch(dfun) {
case SYM_SETVAR: {
if (lbm_dec_u(count) == 2 && lbm_is_symbol(fun_args[1])) {
lbm_uint s = lbm_dec_sym(fun_args[1]);
if (s >= VARIABLE_SYMBOLS_START &&
s < VARIABLE_SYMBOLS_END) {
ctx->r = lbm_set_var(s, fun_args[2]);
} else {
lbm_value new_env = lbm_env_modify_binding(ctx->curr_env, fun_args[1], fun_args[2]);
if (lbm_type_of(new_env) == LBM_VAL_TYPE_SYMBOL &&
lbm_dec_sym(new_env) == SYM_NOT_FOUND) {
new_env = lbm_env_modify_binding(lbm_get_env(), fun_args[1], fun_args[2]);
}
if (lbm_type_of(new_env) == LBM_VAL_TYPE_SYMBOL &&
lbm_dec_sym(new_env) == SYM_NOT_FOUND) {
ctx->r = NIL;
} else {
ctx->r = fun_args[2];
}
}
} else {
error_ctx(lbm_enc_sym(SYM_EERROR));
return;
}
lbm_stack_drop(&ctx->K, lbm_dec_u(count)+1);
ctx->app_cont = true;
} break;
case SYM_READ: /* fall through */
case SYM_READ_PROGRAM:
if (lbm_dec_u(count) == 1) {
@ -1689,7 +1716,7 @@ static inline void cont_application(eval_context_t *ctx) {
lbm_value status = lbm_enc_sym(SYM_EERROR);
if (lbm_dec_u(count) == 2) {
if (lbm_type_of(fun_args[1]) == LBM_VAL_TYPE_U) { /* CID is of U type */
if (lbm_type_of(fun_args[1]) == LBM_VAL_TYPE_I) { /* CID is of U type */
lbm_cid cid = (lbm_cid)lbm_dec_i(fun_args[1]);
lbm_value msg = fun_args[2];

View File

@ -581,17 +581,17 @@ lbm_value lbm_fundamental(lbm_value* args, lbm_uint nargs, lbm_value op) {
int cmp_res = -1;
switch (lbm_dec_sym(op)) {
case SYM_SETVAR:
if (nargs == 2 && lbm_is_symbol(args[0])) {
lbm_uint s = lbm_dec_sym(args[0]);
if (s >= VARIABLE_SYMBOLS_START &&
s < VARIABLE_SYMBOLS_END) {
result = lbm_set_var(s, args[1]);
} else {
*lbm_get_env_ptr() = lbm_env_set(lbm_get_env(), args[0], args[1]);
result = args[1];
}
} break;
/* case SYM_SETVAR: */
/* if (nargs == 2 && lbm_is_symbol(args[0])) { */
/* lbm_uint s = lbm_dec_sym(args[0]); */
/* if (s >= VARIABLE_SYMBOLS_START && */
/* s < VARIABLE_SYMBOLS_END) { */
/* result = lbm_set_var(s, args[1]); */
/* } else { */
/* *lbm_get_env_ptr() = lbm_env_set(lbm_get_env(), args[0], args[1]); */
/* result = args[1]; */
/* } */
/* } break; */
case SYM_IX:
if (nargs == 2 && lbm_is_number(args[0])) {
result = index_list(args[1], lbm_dec_as_u(args[0]));

View File

@ -426,7 +426,7 @@ int lbm_gc_mark_freelist() {
while (lbm_is_ptr(curr)){
t = ref_cell(curr);
set_gc_mark(t);
curr = read_cdr(t);
curr = lbm_cdr(curr);
heap_state.gc_marked ++;
}
@ -471,14 +471,26 @@ int lbm_gc_sweep_phase(void) {
// Check if this cell is a pointer to an array
// and free it.
if (lbm_type_of(heap[i].cdr) == LBM_VAL_TYPE_SYMBOL &&
lbm_dec_sym(heap[i].cdr) == SYM_ARRAY_TYPE) {
if (lbm_type_of(heap[i].cdr) == LBM_VAL_TYPE_SYMBOL) {
switch(lbm_dec_sym(heap[i].cdr)) {
case SYM_ARRAY_TYPE:{
lbm_array_header_t *arr = (lbm_array_header_t*)heap[i].car;
if (lbm_memory_ptr_inside((uint32_t*)arr->data)) {
lbm_memory_free((uint32_t *)arr->data);
heap_state.gc_recovered_arrays++;
}
lbm_memory_free((uint32_t *)arr);
} break;
case SYM_STREAM_TYPE:{
lbm_stream_t *stream = (lbm_stream_t*)heap[i].car;
if (lbm_memory_ptr_inside((uint32_t*)stream)) {
lbm_memory_free((uint32_t*)stream);
}
} break;
default:
break;
}
}
// create pointer to use as new freelist

View File

@ -39,7 +39,7 @@ lbm_value lbm_stream_put(lbm_stream_t *str, lbm_value v) {
}
lbm_value lbm_stream_create(lbm_stream_t *str) {
lbm_value s = lbm_cons((lbm_value)str, lbm_enc_sym(SYM_TYPE_STREAM));
lbm_value s = lbm_cons((lbm_value)str, lbm_enc_sym(SYM_STREAM_TYPE));
if (lbm_type_of(s) == LBM_PTR_TYPE_CONS) {
s = s | LBM_PTR_TYPE_STREAM;
}

View File

@ -57,6 +57,8 @@ special_sym const special_symbols[NUM_SPECIAL_SYMBOLS] = {
{"call-cc" , SYM_CALLCC},
{"continuation" , SYM_CONT},
{"set!" , SYM_SETVAR},
// pattern matching
{"?" , SYM_MATCH_ANY},
{"?i28" , SYM_MATCH_I28},
@ -79,7 +81,7 @@ special_sym const special_symbols[NUM_SPECIAL_SYMBOLS] = {
{"sym_boxed_i" , SYM_BOXED_I_TYPE},
{"sym_boxed_u" , SYM_BOXED_U_TYPE},
{"sym_boxed_f" , SYM_BOXED_F_TYPE},
{"sym_ref" , SYM_REF_TYPE},
{"sym_stream" , SYM_STREAM_TYPE},
{"sym_recovered" , SYM_RECOVERED},
{"sym_bytecode" , SYM_BYTECODE_TYPE},
{"sym_nonsense" , SYM_NONSENSE},
@ -151,8 +153,6 @@ special_sym const special_symbols[NUM_SPECIAL_SYMBOLS] = {
{"bitwise-xor" , SYM_BITWISE_XOR},
{"bitwise-not" , SYM_BITWISE_NOT},
{"setvar" , SYM_SETVAR},
// Streams
// {"stream-get" , SYM_STREAM_GET},
// {"stream-more" , SYM_STREAM_MORE},

View File

@ -154,7 +154,7 @@ bool symchar0(char c) {
}
bool symchar(char c) {
const char *allowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-*/=<>";
const char *allowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-*/=<>!";
int i = 0;
while (allowed[i] != 0) {

View File

@ -17,16 +17,6 @@ expected_fails=("test_lisp_code_cps -h 512 test_qq_4.lisp"
"test_lisp_code_cps -c -h 512 test_sumtree_0.lisp"
"test_lisp_code_cps -c -h 512 test_sumtree_1.lisp"
"test_lisp_code_cps -c -h 512 test_sumtree_2.lisp"
"test_lisp_code_cps_nc -h 512 test_qq_4.lisp"
"test_lisp_code_cps_nc -h 512 test_qq_5.lisp"
"test_lisp_code_cps_nc -h 512 test_sumtree_0.lisp"
"test_lisp_code_cps_nc -h 512 test_sumtree_1.lisp"
"test_lisp_code_cps_nc -h 512 test_sumtree_2.lisp"
"test_lisp_code_cps_nc -c -h 512 test_qq_4.lisp"
"test_lisp_code_cps_nc -c -h 512 test_qq_5.lisp"
"test_lisp_code_cps_nc -c -h 512 test_sumtree_0.lisp"
"test_lisp_code_cps_nc -c -h 512 test_sumtree_1.lisp"
"test_lisp_code_cps_nc -c -h 512 test_sumtree_2.lisp"
"test_lisp_code_cps -h 1024 test_take_iota_0.lisp"
"test_lisp_code_cps -c -h 1024 test_take_iota_0.lisp"
"test_lisp_code_cps -h 512 test_take_iota_0.lisp"

View File

@ -178,6 +178,8 @@ int main(int argc, char **argv) {
}
fseek(fp, 0, SEEK_SET);
char *code_buffer = malloc((unsigned long)size * sizeof(char) + 1);
if (!code_buffer) return 0;
memset(code_buffer, 0, (unsigned long)size * sizeof(char) + 1);
size_t r = fread (code_buffer, 1, (unsigned int)size, fp);
if (r == 0) {
@ -273,6 +275,7 @@ int main(int argc, char **argv) {
lbm_set_timestamp_us_callback(timestamp_callback);
lbm_set_usleep_callback(sleep_callback);
lbm_set_printf_callback(printf);
lbm_variables_init(variable_storage, VARIABLE_STORAGE_SIZE);
@ -287,6 +290,11 @@ int main(int argc, char **argv) {
printf("Waiting for prelude timed out.\n");
}
lbm_pause_eval();
while (lbm_get_eval_state() != EVAL_CPS_STATE_PAUSED) {
sleep_callback(1000);
}
char *compressed_code;
if (compress_decompress) {
uint32_t compressed_size = 0;
@ -314,10 +322,17 @@ int main(int argc, char **argv) {
lbm_set_ctx_done_callback(context_done_callback);
cid = lbm_load_and_eval_program(&string_tok);
lbm_continue_eval();
while (!experiment_done) {
sleep_callback(1000);
}
lbm_pause_eval();
while(lbm_get_eval_state() != EVAL_CPS_STATE_PAUSED);
if (compress_decompress) {
free(compressed_code);
}

View File

@ -18,7 +18,7 @@
import huffman
import collections
symchars = 'abcdefghijklmnopqrstuvwxyz?'
symchars = 'abcdefghijklmnopqrstuvwxyz!?'
numchars = '0123456789'
funchars = ['+','-','*','/','=','<','>','.','#','\\\"','\\\\', '\'', ' ', '`', ',',',@', '_', '.']
short_lispnames = ['(', ')', '))', '((']