Change size of int from 8 to 4

This commit is contained in:
Rui Ueyama 2020-09-06 08:09:09 +09:00
parent bef05432c9
commit 5831edaab3
5 changed files with 52 additions and 30 deletions

View File

@ -54,6 +54,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
//

View File

@ -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 Obj *current_fn;
@ -81,6 +82,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");
}
@ -99,6 +102,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)");
}
@ -283,6 +288,21 @@ static void emit_data(Obj *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(Obj *prog) {
for (Obj *fn = prog; fn; fn = fn->next) {
if (!fn->is_function)
@ -300,12 +320,8 @@ static void emit_text(Obj *prog) {
// Save passed-by-register arguments to the stack
int i = 0;
for (Obj *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 (Obj *var = fn->params; var; var = var->next)
store_gp(i++, var->offset, var->ty->size);
// Emit code
gen_stmt(fn->body);

View File

@ -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;
}

View File

@ -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");

2
type.c
View File

@ -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));