From cb0065e35278b0ed2f8d51b608d1c6faebbca4bc Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Sun, 6 Sep 2020 08:09:09 +0900 Subject: [PATCH] Change size of int from 8 to 4 --- chibicc.h | 3 +++ codegen.c | 28 ++++++++++++++++++++++------ test/struct.c | 23 +++++++++++++---------- test/variable.c | 26 +++++++++++++------------- type.c | 2 +- 5 files changed, 52 insertions(+), 30 deletions(-) diff --git a/chibicc.h b/chibicc.h index e93dd4b..800175a 100644 --- a/chibicc.h +++ b/chibicc.h @@ -47,6 +47,9 @@ Token *skip(Token *tok, char *op); bool consume(Token **rest, Token *tok, char *str); Token *tokenize_file(char *filename); +#define unreachable() \ + error("internal error at %s:%d", __FILE__, __LINE__) + // // parse.c // diff --git a/codegen.c b/codegen.c index feea39c..d9842e9 100644 --- a/codegen.c +++ b/codegen.c @@ -3,6 +3,7 @@ static FILE *output_file; static int depth; static char *argreg8[] = {"%dil", "%sil", "%dl", "%cl", "%r8b", "%r9b"}; +static char *argreg32[] = {"%edi", "%esi", "%edx", "%ecx", "%r8d", "%r9d"}; static char *argreg64[] = {"%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9"}; static Var *current_fn; @@ -80,6 +81,8 @@ static void load(Type *ty) { if (ty->size == 1) println(" movsbq (%%rax), %%rax"); + else if (ty->size == 4) + println(" movsxd (%%rax), %%rax"); else println(" mov (%%rax), %%rax"); } @@ -98,6 +101,8 @@ static void store(Type *ty) { if (ty->size == 1) println(" mov %%al, (%%rdi)"); + else if (ty->size == 4) + println(" mov %%eax, (%%rdi)"); else println(" mov %%rax, (%%rdi)"); } @@ -278,6 +283,21 @@ static void emit_data(Var *prog) { } } +static void store_gp(int r, int offset, int sz) { + switch (sz) { + case 1: + println(" mov %s, %d(%%rbp)", argreg8[r], offset); + return; + case 4: + println(" mov %s, %d(%%rbp)", argreg32[r], offset); + return; + case 8: + println(" mov %s, %d(%%rbp)", argreg64[r], offset); + return; + } + unreachable(); +} + static void emit_text(Var *prog) { for (Var *fn = prog; fn; fn = fn->next) { if (!fn->is_function) @@ -295,12 +315,8 @@ static void emit_text(Var *prog) { // Save passed-by-register arguments to the stack int i = 0; - for (Var *var = fn->params; var; var = var->next) { - if (var->ty->size == 1) - println(" mov %s, %d(%%rbp)", argreg8[i++], var->offset); - else - println(" mov %s, %d(%%rbp)", argreg64[i++], var->offset); - } + for (Var *var = fn->params; var; var = var->next) + store_gp(i++, var->offset, var->ty->size); // Emit code gen_stmt(fn->body); diff --git a/test/struct.c b/test/struct.c index 2e76b68..8975f43 100644 --- a/test/struct.c +++ b/test/struct.c @@ -17,19 +17,19 @@ int main() { ASSERT(6, ({ struct { struct { char b; } a; } x; x.a.b=6; x.a.b; })); - ASSERT(8, ({ struct {int a;} x; sizeof(x); })); - ASSERT(16, ({ struct {int a; int b;} x; sizeof(x); })); - ASSERT(16, ({ struct {int a, b;} x; sizeof(x); })); - ASSERT(24, ({ struct {int a[3];} x; sizeof(x); })); - ASSERT(32, ({ struct {int a;} x[4]; sizeof(x); })); - ASSERT(48, ({ struct {int a[3];} x[2]; sizeof(x); })); + ASSERT(4, ({ struct {int a;} x; sizeof(x); })); + ASSERT(8, ({ struct {int a; int b;} x; sizeof(x); })); + ASSERT(8, ({ struct {int a, b;} x; sizeof(x); })); + ASSERT(12, ({ struct {int a[3];} x; sizeof(x); })); + ASSERT(16, ({ struct {int a;} x[4]; sizeof(x); })); + ASSERT(24, ({ struct {int a[3];} x[2]; sizeof(x); })); ASSERT(2, ({ struct {char a; char b;} x; sizeof(x); })); ASSERT(0, ({ struct {} x; sizeof(x); })); - ASSERT(16, ({ struct {char a; int b;} x; sizeof(x); })); - ASSERT(16, ({ struct {int a; char b;} x; sizeof(x); })); + ASSERT(8, ({ struct {char a; int b;} x; sizeof(x); })); + ASSERT(8, ({ struct {int a; char b;} x; sizeof(x); })); - ASSERT(16, ({ struct t {int a; int b;} x; struct t y; sizeof(y); })); - ASSERT(16, ({ struct t {int a; int b;}; struct t y; sizeof(y); })); + ASSERT(8, ({ struct t {int a; int b;} x; struct t y; sizeof(y); })); + ASSERT(8, ({ struct t {int a; int b;}; struct t y; sizeof(y); })); ASSERT(2, ({ struct t {char a[2];}; { struct t {char a[4];}; } struct t y; sizeof(y); })); ASSERT(3, ({ struct t {int x;}; int t=1; struct t y; y.x=2; t+y.x; })); @@ -46,6 +46,9 @@ int main() { ASSERT(7, ({ struct t {int a,b;}; struct t x; x.a=7; struct t y, *p=&x, *q=&y; *q=*p; y.a; })); ASSERT(5, ({ struct t {char a, b;} x, y; x.a=5; y=x; y.a; })); + ASSERT(8, ({ struct t {int a; int b;} x; struct t y; sizeof(y); })); + ASSERT(8, ({ struct t {int a; int b;}; struct t y; sizeof(y); })); + printf("OK\n"); return 0; } diff --git a/test/variable.c b/test/variable.c index 0e95391..ee8e4ee 100644 --- a/test/variable.c +++ b/test/variable.c @@ -13,17 +13,17 @@ int main() { ASSERT(3, ({ int foo=3; foo; })); ASSERT(8, ({ int foo123=3; int bar=5; foo123+bar; })); - ASSERT(8, ({ int x; sizeof(x); })); - ASSERT(8, ({ int x; sizeof x; })); + ASSERT(4, ({ int x; sizeof(x); })); + ASSERT(4, ({ int x; sizeof x; })); ASSERT(8, ({ int *x; sizeof(x); })); - ASSERT(32, ({ int x[4]; sizeof(x); })); - ASSERT(96, ({ int x[3][4]; sizeof(x); })); - ASSERT(32, ({ int x[3][4]; sizeof(*x); })); - ASSERT(8, ({ int x[3][4]; sizeof(**x); })); - ASSERT(9, ({ int x[3][4]; sizeof(**x) + 1; })); - ASSERT(9, ({ int x[3][4]; sizeof **x + 1; })); - ASSERT(8, ({ int x[3][4]; sizeof(**x + 1); })); - ASSERT(8, ({ int x=1; sizeof(x=2); })); + ASSERT(16, ({ int x[4]; sizeof(x); })); + ASSERT(48, ({ int x[3][4]; sizeof(x); })); + ASSERT(16, ({ int x[3][4]; sizeof(*x); })); + ASSERT(4, ({ int x[3][4]; sizeof(**x); })); + ASSERT(5, ({ int x[3][4]; sizeof(**x) + 1; })); + ASSERT(5, ({ int x[3][4]; sizeof **x + 1; })); + ASSERT(4, ({ int x[3][4]; sizeof(**x + 1); })); + ASSERT(4, ({ int x=1; sizeof(x=2); })); ASSERT(1, ({ int x=1; sizeof(x=2); x; })); ASSERT(0, g1); @@ -33,8 +33,8 @@ int main() { ASSERT(2, ({ g2[0]=0; g2[1]=1; g2[2]=2; g2[3]=3; g2[2]; })); ASSERT(3, ({ g2[0]=0; g2[1]=1; g2[2]=2; g2[3]=3; g2[3]; })); - ASSERT(8, sizeof(g1)); - ASSERT(32, sizeof(g2)); + ASSERT(4, sizeof(g1)); + ASSERT(16, sizeof(g2)); ASSERT(1, ({ char x=1; x; })); ASSERT(1, ({ char x=1; char y=2; x; })); @@ -47,7 +47,7 @@ int main() { ASSERT(2, ({ int x=2; { int x=3; } int y=4; x; })); ASSERT(3, ({ int x=2; { x=3; } x; })); - ASSERT(15, ({ int x; int y; char z; char *a=&y; char *b=&z; b-a; })); + ASSERT(7, ({ int x; int y; char z; char *a=&y; char *b=&z; b-a; })); ASSERT(1, ({ int x; char y; int z; char *a=&y; char *b=&z; b-a; })); printf("OK\n"); diff --git a/type.c b/type.c index c9a6770..d36a449 100644 --- a/type.c +++ b/type.c @@ -1,7 +1,7 @@ #include "chibicc.h" Type *ty_char = &(Type){TY_CHAR, 1, 1}; -Type *ty_int = &(Type){TY_INT, 8, 8}; +Type *ty_int = &(Type){TY_INT, 4, 4}; static Type *new_type(TypeKind kind, int size, int align) { Type *ty = calloc(1, sizeof(Type)); -- GitLab