diff options
| -rw-r--r-- | pu2/pu2.c | 71 |
1 files changed, 54 insertions, 17 deletions
@@ -346,11 +346,11 @@ __ir2_get_var_loc(slice_t name, label_t variables[ 1 << 11 ], string error_I, st } string _ir2( - string I, - macro_t macros[ 1 << 9 ], - label_t labels[ 1 << 10 ], - label_delegated_t forward_refs[ 1 << 10 ], - label_t variables[ 1 << 11 ]) { + string I, + macro_t *macros, + label_t *labels, + label_delegated_t *forward_refs, + label_t *variables) { char T; string init_I = I; ERR = "all good"; @@ -397,9 +397,24 @@ string _ir2( string backname_I = I; slice_t name = scanword(); space; + int *m_index = --Data; int start_index = m_index - m; + if (*I == ':') { + I++; + space; + checkskip('@', "expected memory index in variable declaration"); + if (!isdigit(*I)) { + run_err( + I, + "expected memory index in variable declaration (i think i remember this " + "one...)"); + } + start_index = strtol(I, &I, 10); + space; + } + // comment out this flag to initialize values at runtime instead of compile-time #define IR2_INIT_COMPILE_TIME @@ -749,9 +764,9 @@ string _ir2( int *Data_B = Data; // completely eliminate code generated in useless if statement - int m2[ S / 4 ]; + int *m2 = calloc(S, sizeof(int)); + label_t *variables_G = calloc(1 << 5, sizeof(label_t)); Data = Code = m2; - label_t variables_G[ 1 << 5 ]; // get new I location string new_I = _ir2(I, macros, labels, forward_refs, variables_G); @@ -759,6 +774,9 @@ string _ir2( // restore values Code = Code_B; Data = Data_B; + + free(m2); + free(variables_G); } else { // just... i mean, i guess run the code..? string new_I = _ir2(I, macros, labels, forward_refs, variables); @@ -830,9 +848,9 @@ string _ir2( int *Data_B = Data; // completely eliminate code generated in useless while loop - int m2[ S / 4 ]; + int *m2 = calloc(S, sizeof(int)); + label_t *variables_G = calloc(1 << 5, sizeof(label_t)); Data = Code = m2; - label_t variables_G[ 1 << 5 ]; // get new I location string new_I = _ir2(I, macros, labels, forward_refs, variables_G); @@ -840,6 +858,9 @@ string _ir2( // restore values Code = Code_B; Data = Data_B; + + free(m2); + free(variables_G); } else { // run forever! @@ -1176,20 +1197,31 @@ string _ir2( } ir2(string I) { - macro_t macros[ 1 << 9 ] = { 0 }; - label_t labels[ 1 << 10 ] = { 0 }; - label_t variables[ 1 << 11 ] = { 0 }; - label_delegated_t forward_refs[ 1 << 10 ] = { 0 }; + const int macros_n = 1 << 9; + const int labels_n = 1 << 10; + const int vars_n = 1 << 11; + const int fwd_n = 1 << 10; + + macro_t *macros = calloc(macros_n, sizeof(*macros)); + label_t *labels = calloc(labels_n, sizeof(*labels)); + label_t *variables = calloc(vars_n, sizeof(*variables)); + label_delegated_t *forward_refs = calloc(fwd_n, sizeof(*forward_refs)); + + if (!macros || !labels || !variables || !forward_refs) { + perror("calloc"); + exit(1); + } + _ir2(I, macros, labels, forward_refs, variables); - label_delegated_t *_forward_refs = forward_refs; + label_delegated_t *curf = forward_refs; // handle forward references! - while (_forward_refs->name.ref != NULL) { - label_delegated_t cur = *_forward_refs++; + while (curf->name.ref != NULL) { + label_delegated_t cur = *curf++; // find label int j; - for (j = 0; j < static_len(labels); j++) { + for (j = 0; j < labels_n; j++) { label_t clab = labels[ j ]; if (clab.name.len == cur.name.len && strncmp(clab.name.ref, cur.name.ref, cur.name.len) == 0) { @@ -1204,6 +1236,11 @@ ir2(string I) { REP_DONE:; } + + free(macros); + free(labels); + free(variables); + free(forward_refs); } string op_names[] = { "hlt", "imm", "lod", "str", "swp", "add", "sub", "and", "not", "acc", |
