pos array size

This commit is contained in:
Yaossg 2024-12-12 23:21:34 +08:00
parent 5784a90d7e
commit 4e591092db
4 changed files with 46 additions and 28 deletions

64
boot.c
View File

@ -38,27 +38,18 @@ enum {
// constants // constants
enum { enum {
TOKEN_EOF, TOKEN_EOF = -1,
TOKEN_SEMICOLON,
TOKEN_ADD, TOKEN_ADD,
TOKEN_SUB, TOKEN_SUB,
TOKEN_MUL, TOKEN_MUL,
TOKEN_DIV, TOKEN_DIV,
TOKEN_REM, TOKEN_REM,
TOKEN_ASSIGN,
TOKEN_COMMA,
TOKEN_LSHIFT,
TOKEN_RSHIFT,
TOKEN_AND, TOKEN_AND,
TOKEN_OR, TOKEN_OR,
TOKEN_XOR, TOKEN_XOR,
TOKEN_COMPL, TOKEN_LSHIFT,
TOKEN_NOT, TOKEN_RSHIFT,
TOKEN_LAND, TOKEN_ASSIGN,
TOKEN_LOR,
TOKEN_ELLIPSIS,
TOKEN_INC,
TOKEN_DEC,
TOKEN_ADD_ASSIGN, TOKEN_ADD_ASSIGN,
TOKEN_SUB_ASSIGN, TOKEN_SUB_ASSIGN,
TOKEN_MUL_ASSIGN, TOKEN_MUL_ASSIGN,
@ -69,15 +60,23 @@ enum {
TOKEN_XOR_ASSIGN, TOKEN_XOR_ASSIGN,
TOKEN_LSHIFT_ASSIGN, TOKEN_LSHIFT_ASSIGN,
TOKEN_RSHIFT_ASSIGN, TOKEN_RSHIFT_ASSIGN,
TOKEN_INV,
TOKEN_NOT,
TOKEN_LAND,
TOKEN_LOR,
TOKEN_INC,
TOKEN_DEC,
TOKEN_QUESTION, TOKEN_QUESTION,
TOKEN_COLON, TOKEN_COLON,
TOKEN_SEMICOLON,
TOKEN_COMMA,
TOKEN_ELLIPSIS,
TOKEN_EQ, TOKEN_EQ,
TOKEN_NE, TOKEN_NE,
TOKEN_LT, TOKEN_LT,
TOKEN_GT, TOKEN_GT,
TOKEN_LE, TOKEN_LE,
TOKEN_GE, TOKEN_GE,
TOKEN_PAREN_LEFT = 50, TOKEN_PAREN_LEFT = 50,
TOKEN_PAREN_RIGHT, TOKEN_PAREN_RIGHT,
TOKEN_BRACKET_LEFT, TOKEN_BRACKET_LEFT,
@ -544,7 +543,7 @@ void next_token() {
token_type = TOKEN_XOR; token_type = TOKEN_XOR;
} }
} else if (ch == '~') { } else if (ch == '~') {
token_type = TOKEN_COMPL; token_type = TOKEN_INV;
} else if (ch == '\'') { } else if (ch == '\'') {
token_type = TOKEN_NUMBER; token_type = TOKEN_NUMBER;
token_data = getchar(); token_data = getchar();
@ -641,14 +640,31 @@ char is_const[ID_LUT_SIZE];
int expect_const() { int expect_const() {
next_token(); next_token();
int value = 1;
if (token_type == TOKEN_ADD) {
next_token();
} else if (token_type == TOKEN_SUB) {
value = -1;
next_token();
}
if (token_type == TOKEN_NUMBER) { if (token_type == TOKEN_NUMBER) {
return token_data; value *= token_data;
} else if (token_type == TOKEN_ID && !local_table[token_data] && is_const[token_data]) {
value *= const_table[token_data];
} else {
fprintf(stderr, "expecting a constant\n");
exit(1);
} }
if (token_type == TOKEN_ID && !local_table[token_data] && is_const[token_data]) { return value;
return const_table[token_data]; }
int expect_array_size() {
int size = expect_const();
if (size <= 0) {
fprintf(stderr, "array size must be positive\n");
exit(1);
} }
fprintf(stderr, "expecting a constant\n"); return size;
exit(1);
} }
void reset_reg() { void reset_reg() {
@ -1272,10 +1288,12 @@ int parse_prefix_expr() {
exit(1); exit(1);
} }
return dereference(reg); return dereference(reg);
} else if (token_type == TOKEN_ADD) {
return parse_prefix_expr();
} else if (token_type == TOKEN_SUB) { } else if (token_type == TOKEN_SUB) {
int reg = parse_prefix_expr(); int reg = parse_prefix_expr();
return asm_r_arith("neg", reg); return asm_r_arith("neg", reg);
} else if (token_type == TOKEN_COMPL) { } else if (token_type == TOKEN_INV) {
int reg = parse_prefix_expr(); int reg = parse_prefix_expr();
return asm_r_arith("not", reg); return asm_r_arith("not", reg);
} else if (token_type == TOKEN_NOT) { } else if (token_type == TOKEN_NOT) {
@ -1620,7 +1638,7 @@ void parse_local_variable(int type) {
fprintf(stderr, "array of pointers is not supported\n"); fprintf(stderr, "array of pointers is not supported\n");
exit(1); exit(1);
} }
int size = expect_const(); int size = expect_array_size();
expect_token(TOKEN_BRACKET_RIGHT); expect_token(TOKEN_BRACKET_RIGHT);
declare_local_array(id, type, size); declare_local_array(id, type, size);
return; return;
@ -1886,7 +1904,7 @@ void parse_global_variable(int id, char* name, int type) {
fprintf(stderr, "array of pointers is not supported\n"); fprintf(stderr, "array of pointers is not supported\n");
exit(1); exit(1);
} }
int size = expect_const(); int size = expect_array_size();
expect_token(TOKEN_BRACKET_RIGHT); expect_token(TOKEN_BRACKET_RIGHT);
int array_size = array_size_of(type, size); int array_size = array_size_of(type, size);
printf(" .zero %d\n", array_size); printf(" .zero %d\n", array_size);

View File

@ -1,8 +1,8 @@
enum { enum {
A, B, C = 1, D, E = A, F, A, B, C = -B, D, E = +1, F,
}; };
int main() { int main() {
return A + B + C + D + E + F; return A + B + C + D + E + F;
// 0 + 1 + 1 + 2 + 0 + 1 = 5 // 0 + 1 + -1 + 0 + 1 + 2 = 3
} }

View File

@ -1 +1 @@
5 3

View File

@ -37,5 +37,5 @@ int main() {
char placeholder[4096]; char placeholder[4096];
int a = 1; int a = 1;
dummy((a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(! !0))))))))))))))))))))), 3, a, a, a, a, a, a); dummy((a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(! !0))))))))))))))))))))), 3, a, a, a, a, a, a);
return (a=(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(~ ~0)))))))))))))))))))))), (a = a); return (a=(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(a+(~ ~0)))))))))))))))))))))), (a = +a);
} }