commit 0781fcec52a98d9fe50df2d55a1c59c3935de603 from: Benjamin Stürz date: Wed Nov 20 20:11:48 2024 UTC cc1: refactor expressions commit - a3884752fe0ed1480d2598d0a64e392ec63752a0 commit + 0781fcec52a98d9fe50df2d55a1c59c3935de603 blob - 98d806e4a17ac8e5eb6474d197054837888b50ef blob + f97c19afbdb37010ead99b47f0d295e7d3da46c8 --- cc/cc1/cc1.h +++ cc/cc1/cc1.h @@ -95,13 +95,16 @@ struct decl_spec { }; enum expr_type { + EX_PAR, EX_INT, }; struct expr { enum expr_type type; + struct dtype *dt; union { + struct expr *e; int i; }; }; @@ -173,6 +176,6 @@ struct function { extern struct scope gscope, fscope; extern yyerror (), yylex (); -extern reg; +extern reg, linenum; #endif // FILE_CC1_H blob - b2e78d9a6918ed04ddf2726764cbd96430e3f972 blob + 314b50ddb21774e4fed79fafa391d4b6cb5c0be1 --- cc/cc1/gen.c +++ cc/cc1/gen.c @@ -10,6 +10,8 @@ eval_expr (e) struct expr *e; { switch (e->type) { + case EX_PAR: + return eval_expr (e->e); case EX_INT: return e->i; } @@ -107,17 +109,46 @@ char *name; return NULL; } -gen_expr (dt, e) +dt_is_eq (a, b) +struct dtype *a, *b; +{ + if (a->type != b->type) + return 0; + + switch (a->type) { + case DT_PTR: + case DT_FUNC: + return dt_is_eq (a->inner, b->inner); + default: + return 1; + } +} + +struct expr * +cast (e, dt, imp) +struct expr *e; struct dtype *dt; +{ + // TODO: casting + if (!dt_is_eq (e->dt, dt)) + errx (1, "%d: TODO: cast(): %d, %d", linenum, e->dt->type, dt->type); + + return e; +} + +gen_expr (e) struct expr *e; { int r; switch (e->type) { + case EX_PAR: + r = gen_expr (e->e); + break; case EX_INT: r = reg++; printf ("\tlet $%d: ", r); - print_dt (dt); + print_dt (e->dt); printf (" = %d;\n", e->i); break; } @@ -145,6 +176,7 @@ struct function *fn; { struct symbol *sym; struct stmt **st; + struct expr *ex; int r, ch; for (sym = fn->ahead; sym != NULL; sym = sym->xnext) @@ -203,7 +235,8 @@ struct function *fn; printf (";\n"); if (sym->sc == SC_AUTO && sym->init.type != IN_NONE) { - r = gen_expr (sym->dt, sym->init.ex); + ex = cast (sym->init.ex, sym->dt, 0); + r = gen_expr (ex); printf ("\twrite $%d, $%d;\n", sym->reg, r); } printf ("\n"); blob - 3dd3f14de96292b938a521ffe2640fb793d20be1 blob + 615264ca4e0c9356061e5d0af7ee4ef1df8c61f9 --- cc/cc1/parse.y +++ cc/cc1/parse.y @@ -112,8 +112,8 @@ type_spec : basic_type { $$.type = TS_BASIC; $$.bt = ; basic_type : CHAR { $$ = BT_CHAR; } - | INT { $$ = BT_SHORT; } - | SHORT { $$ = BT_INT; } + | SHORT { $$ = BT_SHORT; } + | INT { $$ = BT_INT; } | LONG { $$ = BT_LONG; } | SIGNED { $$ = BT_SIGNED; } | UNSIGNED { $$ = BT_UNSIGNED; } @@ -146,14 +146,28 @@ statement : expression ';' { $$ = new (struct stmt); | ';' { $$ = new (struct stmt); $$->type = ST_NULL; } ; -expression : INTEGER { $$ = new (struct expr); $$->type = EX_INT; $$->i = lval.i; } +expression : '(' expression ')' { $$ = new_expr (EX_PAR, $2->dt); $$->e = $2; } + | INTEGER { $$ = new_expr (EX_INT, &default_int); $$->i = lval.i; } ; %% -static struct dtype default_int = { DT_INT }; +static struct dtype default_int = { .type = DT_INT }; +struct expr * +new_expr (type, dt) +struct dtype *dt; +{ + struct expr *e; + + e = new (struct expr); + e->type = type; + e->dt = dt; + + return e; +} + lst_new (lst) struct list *lst; { @@ -301,6 +315,7 @@ struct type_spec *ts; dt->type = us ? DT_USHORT : DT_SHORT; } else if (bt & DT_LONG) { dt->type = us ? DT_ULONG : DT_LONG; + puts ("LOOOOOONG"); } else { dt->type = us ? DT_UINT : DT_INT; } blob - 773a67f29a0d5739a1993b2612d86c7fdb18ea39 blob + 9b0117904614f051f994b0a7d37a629f48b04981 --- cc/cc1/test.c +++ cc/cc1/test.c @@ -19,9 +19,7 @@ int i, j; main (argc, argv) char **argv; { - int i = 0, j, k = 42; - - ;;; + int i, j, k = 0; } extern h ();