tests and fixes

This commit is contained in:
Yaossg 2024-11-30 21:09:08 +08:00
parent 4dc291b27e
commit d1d1c88934
2 changed files with 18 additions and 11 deletions

11
boot.c
View File

@ -1033,7 +1033,7 @@ int asm_sub(int lhs, int rhs) {
exit(1); exit(1);
} }
int diff = asm_rr(TYPE_INT, "sub", lhs, rhs); int diff = asm_rr(TYPE_INT, "sub", lhs, rhs);
_asm_ri("slli", diff, diff, log_step_of(lhs_type)); _asm_ri("srai", diff, diff, log_step_of(lhs_type));
return diff; return diff;
} }
if (type1) { if (type1) {
@ -1818,7 +1818,7 @@ void parse_global_variable(int id, const char* name, int type) {
printf("%s:\n", name); printf("%s:\n", name);
if (token_type == TOKEN_ASSIGN) { if (token_type == TOKEN_ASSIGN) {
expect_token(TOKEN_NUMBER); expect_token(TOKEN_NUMBER);
printf(" .word %d\n", token_data); printf(" .dword %d\n", token_data);
} else if (token_type == TOKEN_BRACKET_LEFT) { } else if (token_type == TOKEN_BRACKET_LEFT) {
if (type & TYPE_PTR_MASK) { if (type & TYPE_PTR_MASK) {
eprintf("array of pointers is not supported\n"); eprintf("array of pointers is not supported\n");
@ -1827,14 +1827,11 @@ 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 = 4 * size; int array_size = type == TYPE_CHAR ? size : 4 * size;
if (type == TYPE_CHAR) {
array_size = 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 {
printf(" .zero %d\n", 4); printf(" .zero %d\n", 8);
unget_token(); unget_token();
} }
expect_token(TOKEN_SEMICOLON); expect_token(TOKEN_SEMICOLON);

View File

@ -1,11 +1,18 @@
int printf(const char format[], ...); int printf(const char format[], ...);
int scanf(const char format[], ...); int scanf(const char format[], ...);
int putchar(int ch);
int* p; void should_be(int expected, int actual) {
if (expected != actual) {
printf("Expected %d, but got %d\n", expected, actual);
} else {
printf("Passed\n");
}
}
int* p = 0;
int f1() { int f1() {
int a = 1; int a = 1;
return *(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(p))))))))))); // a[10] return *(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(p))))))))))); // p[10]
} }
@ -13,5 +20,8 @@ int main() {
int a[15]; int a[15];
p = a; p = a;
for (int i = 0; i < 15; a[i] = i, ++i); for (int i = 0; i < 15; a[i] = i, ++i);
return f1(); p -= 5;
should_be(5, f1());
should_be(5, a - p);
should_be(10, 5);
} }