diff --git a/microcontroller/core/include/c-runtime.h b/microcontroller/core/include/c-runtime.h index 14718bf..13cbd6a 100644 --- a/microcontroller/core/include/c-runtime.h +++ b/microcontroller/core/include/c-runtime.h @@ -109,14 +109,18 @@ struct gc_root_set { value_t values[1]; }; -#define ROOT_SET(name,n) struct { struct gc_root_set* next; uint32_t length; value_t values[n]; } name;\ -gc_init_rootset((struct gc_root_set*)&name, n); - -#define ROOT_SET_DECL(name,n) struct { struct gc_root_set* next; uint32_t length; value_t values[n]; } name; +#define ROOT_SET_DECL(name,n) struct { struct gc_root_set* next; uint32_t length; value_t values[n]; } name #define ROOT_SET_INIT(name,n) gc_init_rootset((struct gc_root_set*)&name, n); #define DELETE_ROOT_SET(name) { gc_root_set_head = name.next; } +#define ROOT_SET(name,n) ROOT_SET_DECL(name,n); ROOT_SET_INIT(name,n) + +#define VALUE_UNDEF_2 VALUE_UNDEF, VALUE_UNDEF +#define VALUE_UNDEF_3 VALUE_UNDEF, VALUE_UNDEF, VALUE_UNDEF +#define ROOT_SET_N(name,n,initv) ROOT_SET_DECL(name,n) \ + = { .next = gc_root_set_head, .length = n, .values = { initv }}; gc_root_set_head = (struct gc_root_set*)&name; + extern int32_t CR_SECTION try_and_catch(void (*main_function)()); extern int32_t CR_SECTION safe_value_to_int(value_t v); diff --git a/microcontroller/core/src/c-runtime.c b/microcontroller/core/src/c-runtime.c index 28c5dd5..30aeb8f 100644 --- a/microcontroller/core/src/c-runtime.c +++ b/microcontroller/core/src/c-runtime.c @@ -2,10 +2,7 @@ /* To run on a 64bit machine (for testing/debugging purpose only), - compile with -DTEST64. To include test code, compile with -DTEST. - So, - cc -DTEST -DTEST64 gc.c -lm - will produce ./a.out that runs test code on a 64bit machine. + compile with -DTEST64. Typical usecase: @@ -1479,6 +1476,7 @@ uint32_t gc_test_run() { } #endif +// when you modify this function, also modify ROOT_SET_N macro. void gc_init_rootset(struct gc_root_set* set, uint32_t length) { set->next = gc_root_set_head; if (length > 0) { diff --git a/server/src/transpiler/code-generator/c-runtime.ts b/server/src/transpiler/code-generator/c-runtime.ts index 67a9ed8..f88a1f5 100644 --- a/server/src/transpiler/code-generator/c-runtime.ts +++ b/server/src/transpiler/code-generator/c-runtime.ts @@ -260,14 +260,20 @@ export const minusAnyValue = 'minus_any_value' export const globalRootSetName = 'global_rootset' export function makeRootSet(n: number) { - if (n > 0) - return `ROOT_SET(func_rootset, ${n})` - else + if (n < 1) return '' + else if (n == 1) + return 'ROOT_SET_N(func_rootset,1,VALUE_UNDEF)' + else if (n == 2) + return 'ROOT_SET_N(func_rootset,2,VALUE_UNDEF_2)' + else if (n == 3) + return 'ROOT_SET_N(func_rootset,3,VALUE_UNDEF_3)' + else + return `ROOT_SET(func_rootset,${n})` } export function declareRootSet(name: string, n: number) { - return `ROOT_SET_DECL(${name}, ${n})` + return `ROOT_SET_DECL(${name}, ${n});` } export function initRootSet(name: string, n: number) {