compact local array

This commit is contained in:
Yaossg 2024-12-07 00:41:24 +08:00
parent 70a78b282a
commit 61b41ee713

25
boot.c
View File

@ -484,12 +484,15 @@ void next_token() {
fprintf(stderr, "unexpected character: %c(%d)\n", ch, ch); fprintf(stderr, "unexpected character: %c(%d)\n", ch, ch);
exit(1); exit(1);
} }
fprintf(stderr, "token: %d\n", token_type); // debug info
if (token_type == TOKEN_ID) { if (0) {
const char* name = id_table + id_lut[token_data]; fprintf(stderr, "token: %d\n", token_type);
fprintf(stderr, " id: %s\n", name); if (token_type == TOKEN_ID) {
} else if (token_type == TOKEN_NUMBER) { const char* name = id_table + id_lut[token_data];
fprintf(stderr, " number: %d\n", token_data); fprintf(stderr, " id: %s\n", name);
} else if (token_type == TOKEN_NUMBER) {
fprintf(stderr, " number: %d\n", token_data);
}
} }
} }
@ -670,11 +673,17 @@ int declare_local(int id, int type) {
return local_table[id] = slot; return local_table[id] = slot;
} }
int array_size_of(int type, int size) {
return type == TYPE_CHAR ? size : 4 * size;
}
int declare_local_array(int id, int type, int size) { int declare_local_array(int id, int type, int size) {
if (local_table[id] != 0) return local_table[id]; if (local_table[id] != 0) return local_table[id];
int slot = next_local_slot(type); 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; local_marker[slot] = MARKER_ARRAY;
for (int i = 1; i < size; ++i) local_marker[next_local_slot(type)] = MARKER_ARRAY; for (int i = 1; i < slot_size; ++i) local_marker[next_local_slot(type)] = MARKER_ARRAY;
return local_table[id] = slot; return local_table[id] = slot;
} }
@ -1841,7 +1850,7 @@ void parse_global_variable(int id, const char* name, int type) {
expect_token(TOKEN_NUMBER); expect_token(TOKEN_NUMBER);
int size = token_data; int size = token_data;
expect_token(TOKEN_BRACKET_RIGHT); expect_token(TOKEN_BRACKET_RIGHT);
int array_size = type == TYPE_CHAR ? size : 4 * size; int array_size = array_size_of(type, size);
printf(" .zero %d\n", array_size); printf(" .zero %d\n", array_size);
declare_global(id, MARKER_ARRAY, type); declare_global(id, MARKER_ARRAY, type);
} else { } else {