string table and zero

This commit is contained in:
Yaossg 2024-11-29 11:16:58 +08:00
parent 57837abbba
commit 967a203414

31
boot.c
View File

@ -145,6 +145,24 @@ int parse_string() {
return string_lut_size++;
}
void rewind_string(int new_data) {
string_offset = string_lut[token_data];
token_data = new_data;
--string_lut_size;
}
void dedup_string() {
int last_string = string_lut_size - 1;
char* latest = string_table + string_lut[last_string];
for (int i = 0; i < last_string; i++) {
char* candidate = string_table + string_lut[i];
if (streq(candidate, latest)) {
rewind_string(i);
return;
}
}
}
char id_table[65536];
int id_offset;
int id_lut[4096];
@ -361,6 +379,7 @@ void next_token() {
} else if (ch == '"') {
token_type = TOKEN_STRING;
token_data = parse_string();
dedup_string();
} else if (ch == '.') {
int ch2 = getchar();
if (ch2 == '.') {
@ -488,6 +507,7 @@ void reset_reg() {
indirection[i] = 0;
overflow[i] = 0;
}
reg_type[REG_ZERO] = TYPE_INT;
}
const char* reg_name(int reg) {
@ -764,17 +784,21 @@ void _asm_i(const char* op, int rd, const char* prefix1, const char* prefix2, in
}
}
int is_not_reusable(int rs1, int expected_type) {
return indirection[rs1] || reg_type[rs1] != expected_type || rs1 == REG_ZERO;
}
int asm_r(int type, const char* op, int rs1) {
int rd = rs1;
if (indirection[rs1] || reg_type[rs1] != type) rd = next_reg(type);
if (is_not_reusable(rs1, type)) rd = next_reg(type);
_asm_r(op, rd, rs1);
return rd;
}
int asm_rr(int type, const char* op, int rs1, int rs2) {
int rd = rs1;
if (indirection[rs1] || reg_type[rs1] != type) rd = rs2;
if (indirection[rs2] || reg_type[rs2] != type) rd = next_reg(type);
if (is_not_reusable(rs1, type)) rd = rs2;
if (is_not_reusable(rs2, type)) rd = next_reg(type);
_asm_rr(op, rd, rs1, rs2);
return rd;
}
@ -1042,6 +1066,7 @@ int parse_primary_expr() {
if (token_type == TOKEN_EOF) {
exit(1);
} else if (token_type == TOKEN_NUMBER) {
if (token_data == 0) return REG_ZERO;
int reg = next_reg(TYPE_INT);
_asm_i("li", reg, "", "", token_data);
return reg;