scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit 63284fa474d0474e85fd63a0b2fb00878f7efeee
parent 04bf7a4f601788f822f90698fc8f18d52b76f5e8
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon, 20 Jul 2015 19:23:42 +0200

Remove sizeof nodes

We need calculate sizeof expressions in cc1, because in other case
we will not be able of calculate types like:

int a[sizeof(int)];

Diffstat:
cc1/cc1.h | 10++++++++--
cc1/code.c | 18++++++------------
cc1/types.c | 36+++++++++++++++++++++++++++++++++++-
3 files changed, 49 insertions(+), 15 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -13,11 +13,18 @@ typedef struct caselist Caselist; typedef struct node Node; typedef struct input Input; +/* + * TODO: Some of the data stored in type is shared with + * cc2, so it should be stored in a table shared + * between both programs, and dependant of the target. + */ struct type { unsigned char op; /* type builder operator */ unsigned char ns; char letter; /* letter of the type */ bool defined; /* type defined */ + size_t size; /* sizeof the type */ + size_t align; /* align of the type */ Type *type; /* base type */ Type *next; /* next element in the hash */ Type **pars; /* type parameters */ @@ -211,7 +218,6 @@ enum tokens { enum { OPTR, OADD, - OSIZE, OMUL, OSUB, OINC, @@ -337,7 +343,7 @@ extern int lexmode; extern Type *voidtype, *pvoidtype, *booltype, *uchartype, *chartype, - *uinttype, *inttype, + *uinttype, *inttype, *sizetp, *ushortype, *shortype, *longtype, *ulongtype, *ullongtype, *llongtype, diff --git a/cc1/code.c b/cc1/code.c @@ -10,7 +10,7 @@ static void emitbin(unsigned, void *), emitcast(unsigned, void *), emitswitch(unsigned, void *), emitsym(unsigned, void *), emitfield(unsigned, void *), - emitsizeof(unsigned, void *), emitexp(unsigned, void *), + emitexp(unsigned, void *), emitsymid(unsigned, void *), emittext(unsigned, void *), emitprint(unsigned, void *), emitfun(unsigned, void *), emitret(unsigned, void *), emitdcl(unsigned, void *); @@ -21,7 +21,6 @@ char *optxt[] = { [OMUL] = "*", [OINC] = ";+", [ODEC] = ";-", - [OSIZE] = "#", [OPTR] = "@", [OMOD] = "%", [ODIV] = "/", @@ -72,7 +71,6 @@ void (*opcode[])(unsigned, void *) = { [OMUL] = emitbin, [OINC] = emitbin, [ODEC] = emitbin, - [OSIZE] = emitsizeof, [OPTR] = emitbin, [OMOD] = emitbin, [ODIV] = emitbin, @@ -235,13 +233,6 @@ emitbin(unsigned op, void *arg) } static void -emitsizeof(unsigned op, void *arg) -{ - Node *np = arg; - printf("\t#%c", np->left->type->letter); -} - -static void emitexp(unsigned op, void *arg) { Node *np = arg; @@ -351,7 +342,10 @@ Node * sizeofnode(Type *tp) { Node *np; + Symbol *sym; - np = node(0, tp, NULL, NULL); - return node(OSIZE, inttype, np, NULL); + sym = newsym(NS_IDEN); + sym->type = sizetp; + sym->u.i = tp->size; + return constnode(sym); } diff --git a/cc1/types.c b/cc1/types.c @@ -24,96 +24,128 @@ static Type types[] = { }, { /* 1 = pvoidtype */ .op = PTR, - .letter = L_POINTER + .letter = L_POINTER, + .size = 2, + .align = 2 }, { /* 2 = booltype */ .op = INT, .letter = L_BOOL, .defined = 1, + .size = 1, + .align = 1, .n.rank = RANK_BOOL }, { /* 3 = schartype */ .op = INT, .letter = L_SCHAR, .defined = 1, + .size = 1, + .align = 1, .n.rank = RANK_SCHAR }, { /* 4 = uchartype */ .op = INT, .letter = L_UCHAR, .defined = 1, + .size = 1, + .align = 1, .n.rank = RANK_UCHAR }, { /* 5 = chartype */ .op = INT, .letter = L_CHAR, .defined = 1, + .size = 1, + .align = 1, .n.rank = RANK_CHAR }, { /* 6 = ushortype */ .op = INT, .letter = L_USHORT, .defined = 1, + .size = 2, + .align = 1, .n.rank = RANK_USHORT }, { /* 7 = shortype */ .op = INT, .letter = L_SHORT, .defined = 1, + .size = 2, + .align = 1, .n.rank = RANK_SHORT }, { /* 8 = uinttype */ .op = INT, .letter = L_UINT, .defined = 1, + .size = 2, + .align = 1, .n.rank = RANK_UINT }, { /* 9 = inttype */ .op = INT, .letter = L_INT, .defined = 1, + .size = 2, + .align = 1, .n.rank = RANK_INT }, { /* 10 = longtype */ .op = INT, .letter = L_LONG, .defined = 1, + .size = 4, + .align = 1, .n.rank = RANK_LONG }, { /* 11 = ulongtype */ .op = INT, .letter = L_ULONG, .defined = 1, + .size = 4, + .align = 1, .n.rank = RANK_ULONG }, { /* 12 = ullongtype */ .op = INT, .letter = L_ULLONG, .defined = 1, + .size = 8, + .align = 1, .n.rank = RANK_ULLONG }, { /* 13 = llongtype */ .op = INT, .letter = L_LLONG, .defined = 1, + .size = 8, + .align = 1, .n.rank = RANK_LLONG }, { /* 14 = floattype */ .op = FLOAT, .letter = L_FLOAT, .defined = 1, + .size = 4, + .align = 1, .n.rank = RANK_FLOAT }, { /* 15 = doubletype */ .op = FLOAT, .letter = L_DOUBLE, .defined = 1, + .size = 8, + .align = 1, .n.rank = RANK_DOUBLE }, { /* 16 = ldoubletype */ .op = FLOAT, .letter = L_LDOUBLE, .defined = 1, + .size = 16, + .align = 1, .n.rank = RANK_LDOUBLE } }; @@ -128,6 +160,8 @@ Type *voidtype = &types[0], *pvoidtype = &types[1], *floattype = &types[14], *doubletype = &types[15], *ldoubletype = &types[16]; +Type *sizetp = &types[8]; /* TODO: This depend of the target */ + static Symbol dummy0 = {.u.i = 0, .type = &types[9]}, dummy1 = {.u.i = 1, .type = &types[9]}; Symbol *zero = &dummy0, *one = &dummy1;