[hackers] [scc] Emit recursively the types in dcl || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Tue, 21 Jul 2015 19:27:39 +0200 (CEST)

commit 10945b12361ca806d3d80c424063418331669947
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Tue Jul 21 19:26:25 2015 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Tue Jul 21 19:26:25 2015 +0200

    Emit recursively the types in dcl
    
    Beofre declaring a variable it is important to be sure
    that the type is emited.

diff --git a/cc1/cc1.h b/cc1/cc1.h
index eb65613..a41e0ac 100644
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
_AT_@ -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 */
_AT_@ -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
index c4f43c2..b1fdbe9 100644
--- a/cc1/code.c
+++ b/cc1/code.c
_AT_@ -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
_AT_@ -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');
 }
 
_AT_@ -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
index d7f1b19..0734a27 100644
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
_AT_@ -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
index 851a03d..59100e8 100644
--- a/cc1/types.c
+++ b/cc1/types.c
_AT_@ -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,
_AT_@ -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,
_AT_@ -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,
_AT_@ -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,
_AT_@ -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,
_AT_@ -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,
_AT_@ -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,
_AT_@ -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,
_AT_@ -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,
_AT_@ -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,
_AT_@ -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,
_AT_@ -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,
_AT_@ -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,
_AT_@ -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,
_AT_@ -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,
_AT_@ -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,
_AT_@ -160,7 +177,8 @@ static Type types[] = {
                 .defined = 1,
                 .size = 2,
                 .align = 1,
- .n.rank = RANK_UINT
+ .n.rank = RANK_UINT,
+ .printed = 1
         }
 };
 
_AT_@ -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;
 }
Received on Tue Jul 21 2015 - 19:27:39 CEST

This archive was generated by hypermail 2.3.0 : Tue Jul 21 2015 - 19:36:21 CEST