fix prefix and div-assign

This commit is contained in:
Yaossg 2024-12-08 15:40:44 +08:00
parent 9a2a1b00be
commit a9054f0a64

18
boot.c
View File

@ -427,7 +427,7 @@ void next_token() {
int ch2 = getchar(); int ch2 = getchar();
if (ch2 == '=') { if (ch2 == '=') {
token_type = TOKEN_DIV_ASSIGN; token_type = TOKEN_DIV_ASSIGN;
} if (ch2 == '/') { } else if (ch2 == '/') {
do ch = getchar(); while (ch != -1 && ch != '\n'); do ch = getchar(); while (ch != -1 && ch != '\n');
next_token(); next_token();
return; return;
@ -1253,7 +1253,7 @@ int parse_postfix_expr() {
int parse_prefix_expr() { int parse_prefix_expr() {
next_token(); next_token();
if (token_type == TOKEN_AND) { if (token_type == TOKEN_AND) {
int reg = parse_postfix_expr(); int reg = parse_prefix_expr();
int type = reg_type[reg]; int type = reg_type[reg];
if (type & TYPE_PTR_MASK) { if (type & TYPE_PTR_MASK) {
fprintf(stderr, "cannot take address of a pointer\n"); fprintf(stderr, "cannot take address of a pointer\n");
@ -1261,7 +1261,7 @@ int parse_prefix_expr() {
} }
return addressof(reg); return addressof(reg);
} else if (token_type == TOKEN_MUL) { } else if (token_type == TOKEN_MUL) {
int reg = parse_postfix_expr(); int reg = parse_prefix_expr();
int type = reg_type[reg]; int type = reg_type[reg];
if (!(type & TYPE_PTR_MASK)) { if (!(type & TYPE_PTR_MASK)) {
fprintf(stderr, "cannot dereference a non-pointer\n"); fprintf(stderr, "cannot dereference a non-pointer\n");
@ -1273,20 +1273,20 @@ int parse_prefix_expr() {
} }
return dereference(reg); return dereference(reg);
} else if (token_type == TOKEN_SUB) { } else if (token_type == TOKEN_SUB) {
int reg = parse_postfix_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_COMPL) {
int reg = parse_postfix_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) {
int reg = parse_postfix_expr(); int reg = parse_prefix_expr();
return asm_r(TYPE_INT, "seqz", reg); return asm_r(TYPE_INT, "seqz", reg);
} else if (token_type == TOKEN_INC) { } else if (token_type == TOKEN_INC) {
int reg = parse_postfix_expr(); int reg = parse_prefix_expr();
_asm_ri("addi", reg, reg, step_of(reg_type[reg])); _asm_ri("addi", reg, reg, step_of(reg_type[reg]));
return reg; return reg;
} else if (token_type == TOKEN_DEC) { } else if (token_type == TOKEN_DEC) {
int reg = parse_postfix_expr(); int reg = parse_prefix_expr();
_asm_ri("addi", reg, reg, -step_of(reg_type[reg])); _asm_ri("addi", reg, reg, -step_of(reg_type[reg]));
return reg; return reg;
} else { } else {
@ -1989,8 +1989,6 @@ void dump_string_table() {
printf("\\t"); printf("\\t");
} else if (ch == '\r') { } else if (ch == '\r') {
printf("\\r"); printf("\\r");
} else if (ch == '\0') {
printf("\\0");
} else if (ch == '\\') { } else if (ch == '\\') {
printf("\\\\"); printf("\\\\");
} else if (ch == '\'') { } else if (ch == '\'') {