diff --git a/boot.c b/boot.c index eb71c13..61df49d 100644 --- a/boot.c +++ b/boot.c @@ -527,30 +527,30 @@ int parse_type() { return -1; } -// asm +// assembly context + +const int KIND_TEMP = 0; +const int KIND_SCALAR = 1; +const int KIND_ARRAY = 2; +const int KIND_FUNCTION = 3; int local_table[4096]; // id -> local id int next_local_id = 1; int max_local_id = 1; - -const int MARKER_TEMP = 0; -const int MARKER_SCALAR = 1; -const int MARKER_ARRAY = 2; -const int MARKER_FUNCTION = 3; - -int local_marker[4096]; -int global_marker[4096]; +int local_kind[4096]; int local_type[4096]; + +int global_kind[4096]; int global_type[4096]; -int reg_type[4096]; int next_reg_id = 18; int max_reg_id = 18; -int indirection[4096]; -int overflow[4096]; +int reg_type[4096]; +char indirection[4096]; +int overflow[4096]; // reg -> local id int const_table[4096]; // id -> value -int is_const[4096]; +char is_const[4096]; const int REG_ZERO = 0; const int REG_RA = 1; @@ -644,14 +644,14 @@ void reset_local() { max_reg_id = REG_S2; for (int i = 0; i < 4096; ++i) { local_table[i] = 0; - local_marker[i] = MARKER_TEMP; + local_kind[i] = KIND_TEMP; local_type[i] = TYPE_VOID; } reset_reg(); } void reset_temp() { - while (next_local_id > 1 && local_marker[next_local_id - 1] == MARKER_TEMP) { + while (next_local_id > 1 && local_kind[next_local_id - 1] == KIND_TEMP) { --next_local_id; } reset_reg(); @@ -669,7 +669,7 @@ int next_local_slot(int type) { int declare_local(int id, int type) { if (local_table[id] != 0) return local_table[id]; int slot = next_local_slot(type); - local_marker[slot] = MARKER_SCALAR; + local_kind[slot] = KIND_SCALAR; return local_table[id] = slot; } @@ -682,13 +682,13 @@ int declare_local_array(int id, int type, int size) { int slot = next_local_slot(type); int array_size = array_size_of(type, size); int slot_size = (array_size + 7) / 8; - local_marker[slot] = MARKER_ARRAY; - for (int i = 1; i < slot_size; ++i) local_marker[next_local_slot(type)] = MARKER_ARRAY; + local_kind[slot] = KIND_ARRAY; + for (int i = 1; i < slot_size; ++i) local_kind[next_local_slot(type)] = KIND_ARRAY; return local_table[id] = slot; } -void declare_global(int id, int marker, int type) { - global_marker[id] = marker; +void declare_global(int id, int kind, int type) { + global_kind[id] = kind; global_type[id] = type; } @@ -696,7 +696,7 @@ int next_reg(int type) { int reg = next_reg_id++; if (is_overflow(reg)) { int slot = next_local_slot(type); - local_marker[slot] = MARKER_TEMP; + local_kind[slot] = KIND_TEMP; overflow[reg] = slot; } reg_type[reg] = type; @@ -890,12 +890,12 @@ void store_into_local(int rs1, int slot) { printf(" %s %s, 0(t2)\n", store_op_of_type(local_type[slot]), rs1_name); } -int materialize_address(int rd, int type, int marker) { - if (marker == MARKER_ARRAY) { +int materialize_address(int rd, int type, int kind) { + if (kind == KIND_ARRAY) { type |= TYPE_PTR_MASK; } reg_type[rd] = type; - indirection[rd] = marker == MARKER_SCALAR; + indirection[rd] = kind == KIND_SCALAR; return rd; } @@ -907,7 +907,7 @@ int lookup_from_slot(int slot) { } else { load_local_address(rd, slot); } - return materialize_address(rd, local_type[slot], local_marker[slot]); + return materialize_address(rd, local_type[slot], local_kind[slot]); } int load_imm(int imm) { @@ -925,14 +925,14 @@ int lookup(int id) { return load_imm(const_table[id]); } const char* name = id_table + id_lut[id]; - if (global_marker[id]) { - if (global_marker[id] == MARKER_FUNCTION) { + if (global_kind[id]) { + if (global_kind[id] == KIND_FUNCTION) { fprintf(stderr, "function name must not appear outside function call: %s\n", name); exit(1); } int rd = next_reg(TYPE_VOID_PTR); _asm_i("la", rd, name, " # id: ", id); - return materialize_address(rd, global_type[id], global_marker[id]); + return materialize_address(rd, global_type[id], global_kind[id]); } fprintf(stderr, "unresolved identifier: %s\n", name); exit(1); @@ -1108,7 +1108,7 @@ int parse_assign_expr(); int parse_function_call(int id) { const char* name = id_table + id_lut[id]; - if (global_marker[id] != MARKER_FUNCTION) { + if (global_kind[id] != KIND_FUNCTION) { fprintf(stderr, "not a function name: %s\n", name); exit(1); } @@ -1852,7 +1852,7 @@ void parse_global_variable(int id, const char* name, int type) { expect_token(TOKEN_BRACKET_RIGHT); int array_size = array_size_of(type, size); printf(" .zero %d\n", array_size); - declare_global(id, MARKER_ARRAY, type); + declare_global(id, KIND_ARRAY, type); } else { printf(" .zero %d\n", 8); unget_token(); @@ -1887,14 +1887,14 @@ void parse_global_declaration() { is_const[id] = 1; expect_token(TOKEN_SEMICOLON); } else if (token_type == TOKEN_PAREN_LEFT) { - declare_global(id, MARKER_FUNCTION, type); + declare_global(id, KIND_FUNCTION, type); parse_function(name); } else { if (type == TYPE_VOID) { fprintf(stderr, "variable cannot be of void type\n"); exit(1); } - declare_global(id, MARKER_SCALAR, type); + declare_global(id, KIND_SCALAR, type); if (external) { unget_token(); } else {