scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 10945b12361ca806d3d80c424063418331669947
parent 36fb5f2f51f08b9c78c31277f7ef9475608fb4ab
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 21 Jul 2015 19:26:25 +0200

Emit recursively the types in dcl

Beofre declaring a variable it is important to be sure
that the type is emited.

Diffstat:
Mcc1/cc1.h | 7+++++--
Mcc1/code.c | 30+++++++++++++++++++++++++++---
Mcc1/symbol.c | 10++++++++++
Mcc1/types.c | 57+++++++++++++++++++++++++++++++++++++--------------------
4 files changed, 79 insertions(+), 25 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -21,9 +21,11 @@ typedef struct input Input; struct type { unsigned char op; /* type builder operator */ unsigned char ns; + short id; /* type id, used in dcls */ char letter; /* letter of the type */ - bool defined; /* type defined */ - bool sign; /* signess of the type */ + bool defined : 1; /* type defined */ + bool sign : 1; /* signess of the type */ + bool printed : 1; /* the type already was printed */ size_t size; /* sizeof the type */ size_t align; /* align of the type */ Type *type; /* base type */ @@ -286,6 +288,7 @@ extern void printerr(char *fmt, ...); extern bool eqtype(Type *tp1, Type *tp2); extern Type *ctype(unsigned type, unsigned sign, unsigned size); extern Type *mktype(Type *tp, unsigned op, short nelem, void *data); +extern Type *duptype(Type *base); /* symbol.c */ extern void dumpstab(char *msg); diff --git a/cc1/code.c b/cc1/code.c @@ -214,9 +214,32 @@ emitsym(unsigned op, void *arg) } static void -emittype(Type *tp) +emitletter(Type *tp) { putchar(tp->letter); + if (tp->op == ARY) + printf("%d", tp->id); +} + +static void +emittype(Type *tp) +{ + if (tp->printed) + return; + + switch (tp->op) { + case ARY: + emittype(tp->type); + printf("V%d\t", tp->id); + emitletter(tp->type); + printf("\t#%d\n", tp->n.elem); + return; + case PTR: + emittype(tp->type); + return; + default: + abort(); + } } static void @@ -224,9 +247,10 @@ emitdcl(unsigned op, void *arg) { Symbol *sym = arg; + emittype(sym->type); emitvar(sym); putchar('\t'); - emittype(sym->type); + emitletter(sym->type); putchar('\n'); } @@ -276,7 +300,7 @@ emitret(unsigned op, void *arg) Type *tp = arg; fputs("\ty", stdout); - emittype(tp); + emitletter(tp); } static void diff --git a/cc1/symbol.c b/cc1/symbol.c @@ -97,6 +97,16 @@ popctx(void) head = dummy.next; } +Type * +duptype(Type *base) +{ + Type *tp = xmalloc(sizeof(*tp)); + + *tp = *base; + tp->id = (curctx) ? ++localcnt : ++globalcnt; + return tp; +} + Symbol * newsym(unsigned ns) { diff --git a/cc1/types.c b/cc1/types.c @@ -20,13 +20,15 @@ static Type types[] = { { /* 0 = voidtype */ .op = VOID, - .letter = L_VOID + .letter = L_VOID, + .printed = 1 }, { /* 1 = pvoidtype */ .op = PTR, .letter = L_POINTER, .size = 2, - .align = 2 + .align = 2, + .printed = 1 }, { /* 2 = booltype */ .op = INT, @@ -34,7 +36,8 @@ static Type types[] = { .defined = 1, .size = 1, .align = 1, - .n.rank = RANK_BOOL + .n.rank = RANK_BOOL, + .printed = 1 }, { /* 3 = schartype */ .op = INT, @@ -43,7 +46,8 @@ static Type types[] = { .size = 1, .align = 1, .sign = 1, - .n.rank = RANK_SCHAR + .n.rank = RANK_SCHAR, + .printed = 1 }, { /* 4 = uchartype */ .op = INT, @@ -51,7 +55,8 @@ static Type types[] = { .defined = 1, .size = 1, .align = 1, - .n.rank = RANK_UCHAR + .n.rank = RANK_UCHAR, + .printed = 1 }, { /* 5 = chartype */ .op = INT, @@ -60,7 +65,8 @@ static Type types[] = { .size = 1, .align = 1, .sign = 1, - .n.rank = RANK_CHAR + .n.rank = RANK_CHAR, + .printed = 1 }, { /* 6 = ushortype */ .op = INT, @@ -68,7 +74,8 @@ static Type types[] = { .defined = 1, .size = 2, .align = 1, - .n.rank = RANK_USHORT + .n.rank = RANK_USHORT, + .printed = 1 }, { /* 7 = shortype */ .op = INT, @@ -77,7 +84,8 @@ static Type types[] = { .size = 2, .align = 1, .sign = 1, - .n.rank = RANK_SHORT + .n.rank = RANK_SHORT, + .printed = 1 }, { /* 8 = uinttype */ .op = INT, @@ -85,7 +93,8 @@ static Type types[] = { .defined = 1, .size = 2, .align = 1, - .n.rank = RANK_UINT + .n.rank = RANK_UINT, + .printed = 1 }, { /* 9 = inttype */ .op = INT, @@ -94,7 +103,8 @@ static Type types[] = { .size = 2, .align = 1, .sign = 1, - .n.rank = RANK_INT + .n.rank = RANK_INT, + .printed = 1 }, { /* 10 = longtype */ .op = INT, @@ -103,7 +113,8 @@ static Type types[] = { .size = 4, .align = 1, .sign = 1, - .n.rank = RANK_LONG + .n.rank = RANK_LONG, + .printed = 1 }, { /* 11 = ulongtype */ .op = INT, @@ -111,7 +122,8 @@ static Type types[] = { .defined = 1, .size = 4, .align = 1, - .n.rank = RANK_ULONG + .n.rank = RANK_ULONG, + .printed = 1 }, { /* 12 = ullongtype */ .op = INT, @@ -119,7 +131,8 @@ static Type types[] = { .defined = 1, .size = 8, .align = 1, - .n.rank = RANK_ULLONG + .n.rank = RANK_ULLONG, + .printed = 1 }, { /* 13 = llongtype */ .op = INT, @@ -128,7 +141,8 @@ static Type types[] = { .size = 8, .align = 1, .sign = 1, - .n.rank = RANK_LLONG + .n.rank = RANK_LLONG, + .printed = 1 }, { /* 14 = floattype */ .op = FLOAT, @@ -136,7 +150,8 @@ static Type types[] = { .defined = 1, .size = 4, .align = 1, - .n.rank = RANK_FLOAT + .n.rank = RANK_FLOAT, + .printed = 1 }, { /* 15 = doubletype */ .op = FLOAT, @@ -144,7 +159,8 @@ static Type types[] = { .defined = 1, .size = 8, .align = 1, - .n.rank = RANK_DOUBLE + .n.rank = RANK_DOUBLE, + .printed = 1 }, { /* 16 = ldoubletype */ .op = FLOAT, @@ -152,7 +168,8 @@ static Type types[] = { .defined = 1, .size = 16, .align = 1, - .n.rank = RANK_LDOUBLE + .n.rank = RANK_LDOUBLE, + .printed = 1 }, { /* 17 = sizettype */ .op = INT, @@ -160,7 +177,8 @@ static Type types[] = { .defined = 1, .size = 2, .align = 1, - .n.rank = RANK_UINT + .n.rank = RANK_UINT, + .printed = 1 } }; @@ -289,8 +307,7 @@ mktype(Type *tp, unsigned op, short nelem, void *data) } } - bp = xmalloc(sizeof(*bp)); - *bp = type; + bp = duptype(&type); bp->next = *tbl; return *tbl = bp; }