[hackers] [scc] Simplify definitions in the IR || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Tue, 26 Jan 2016 10:57:43 +0100 (CET)

commit 6124426ed00d4e0802766a6100b8032155f0bb44
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Mon Jan 25 21:39:19 2016 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Tue Jan 26 10:19:28 2016 +0100

    Simplify definitions in the IR
    
    Having the definition of initializers in the nex line was causing
    that was impossible to be able to emit variables until parsing
    the next element, which forced us to deal with a lastsym variable
    and several cases. It is better to know in the same line if
    the symbol is initialized or not.

diff --git a/cc1/cc1.h b/cc1/cc1.h
index d929aab..b0316b2 100644
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
_AT_@ -187,7 +187,8 @@ enum {
         ISDEFINED = 2048,
         ISSTRING = 4096,
         ISTYPEDEF = 8192,
- ISINITLST = 16384
+ ISINITLST = 16384,
+ ISINIT = 32768
 };
 
 /* lexer mode, compiler or preprocessor directive */
diff --git a/cc1/code.c b/cc1/code.c
index 88d45af..e993671 100644
--- a/cc1/code.c
+++ b/cc1/code.c
_AT_@ -365,7 +365,7 @@ emitinit(unsigned op, void *arg)
 {
         Node *np = arg;
 
- puts("(");
+ puts("\t(");
         emitdesig(np, np->type);
         puts(")");
 }
_AT_@ -384,8 +384,9 @@ emitdcl(unsigned op, void *arg)
         printf("\t\"%s", (sym->name) ? sym->name : "");
         if (sym->flags & ISFIELD)
                 printf("\t#%c%llX", sizettype->letter, sym->u.i);
- putchar('\n');
         sym->flags |= ISEMITTED;
+ if ((sym->flags & ISINIT) == 0)
+ putchar('\n');
 }
 
 static void
diff --git a/cc1/expr.c b/cc1/expr.c
index a50ada8..0af463f 100644
--- a/cc1/expr.c
+++ b/cc1/expr.c
_AT_@ -619,6 +619,7 @@ primary(void)
         switch (yytoken) {
         case STRING:
                 np = constnode(sym);
+ sym->flags |= ISINIT;
                 emit(ODECL, sym);
                 emit(OINIT, np);
                 np = decay(varnode(sym));
diff --git a/cc1/init.c b/cc1/init.c
index d7b59ae..4d06dde 100644
--- a/cc1/init.c
+++ b/cc1/init.c
_AT_@ -300,7 +300,6 @@ initializer(Symbol *sym, Type *tp)
         }
         np = initialize(tp);
 
- emit(ODECL, sym);
         if (flags & ISDEFINED) {
                 errorp("redeclaration of '%s'", sym->name);
         } else if ((flags & (ISGLOBAL|ISLOCAL|ISPRIVATE)) != 0) {
_AT_@ -308,12 +307,15 @@ initializer(Symbol *sym, Type *tp)
                         errorp("initializer element is not constant");
                         return;
                 }
+ sym->flags |= ISINIT;
+ emit(ODECL, sym);
                 emit(OINIT, np);
                 sym->flags |= ISDEFINED;
         } else if ((flags & (ISEXTERN|ISTYPEDEF)) != 0) {
                 errorp("'%s' has both '%s' and initializer",
                        sym->name, (flags&ISEXTERN) ? "extern" : "typedef");
         } else {
+ emit(ODECL, sym);
                 np = node(OASSIGN, tp, varnode(sym), np);
                 emit(OEXPR, np);
         }
diff --git a/cc1/tests/test001.c b/cc1/tests/test001.c
index d3748fa..24563fa 100644
--- a/cc1/tests/test001.c
+++ b/cc1/tests/test001.c
_AT_@ -7,8 +7,7 @@ G6 F "main
 {
 \
 V8 K #N13
-Y7 V8 "
-(
+Y7 V8 " (
         #"hello world
         #K0A
         #K00
diff --git a/cc1/tests/test026.c b/cc1/tests/test026.c
index c759fb9..1c64ae4 100644
--- a/cc1/tests/test026.c
+++ b/cc1/tests/test026.c
_AT_@ -10,13 +10,12 @@ G3 F "main
 A4 I "y
 A6 P "p
 V8 K #N10
-Y7 V8 "
-(
+Y7 V8 " (
         #"test026.c
         #K00
 )
         A6 Y7 'P :P
- A4 #I24 :I
+ A4 #I23 :I
         A4 #I1 :I
         A4 #I1 :I
         A4 #I1 :I
diff --git a/cc1/tests/test027.c b/cc1/tests/test027.c
index 0c9b6b1..79ac32c 100644
--- a/cc1/tests/test027.c
+++ b/cc1/tests/test027.c
_AT_@ -9,8 +9,7 @@ G3 F "main
 \
 A5 P "p
 V7 K #N25
-Y6 V7 "
-(
+Y6 V7 " (
         #"hello is better than bye
         #K00
 )
diff --git a/cc1/tests/test028.c b/cc1/tests/test028.c
index 8e3e98b..2b89d02 100644
--- a/cc1/tests/test028.c
+++ b/cc1/tests/test028.c
_AT_@ -8,8 +8,7 @@ G6 F "foo
 {
 \
 V8 K #N3
-Y10 V8 "
-(
+Y10 V8 " (
         #"hi
         #K00
 )
diff --git a/cc1/tests/test032.c b/cc1/tests/test032.c
index 31493af..072c811 100644
--- a/cc1/tests/test032.c
+++ b/cc1/tests/test032.c
_AT_@ -8,8 +8,7 @@ G5 F "main
 {
 \
 V9 K #N44
-Y8 V9 "
-(
+Y8 V9 " (
         #"This is a string $ or # or ##and it is ok !
         #K00
 )
diff --git a/cc1/tests/test038.c b/cc1/tests/test038.c
index e683c90..edc0a75 100644
--- a/cc1/tests/test038.c
+++ b/cc1/tests/test038.c
_AT_@ -6,7 +6,7 @@ error:
 test038.c:43: error: redeclaration of 'x'
 output:
 G1 I "x
-(
+ (
         #I0
 )
 G5 F "foo
diff --git a/cc1/tests/test045.c b/cc1/tests/test045.c
index 2106d3d..3d48eda 100644
--- a/cc1/tests/test045.c
+++ b/cc1/tests/test045.c
_AT_@ -3,8 +3,7 @@ name: TEST045
 description: Basic test of initializers
 error:
 output:
-G1 I "x
-(
+G1 I "x (
         #I5
 )
 G3 F "main
diff --git a/cc1/tests/test046.c b/cc1/tests/test046.c
index dc9340b..4b1d3b1 100644
--- a/cc1/tests/test046.c
+++ b/cc1/tests/test046.c
_AT_@ -4,8 +4,7 @@ description: Basic test for initializators
 error:
 output:
 V1 I #N3
-G2 V1 "x
-(
+G2 V1 "x (
         #I1
         #I2
         #I3
diff --git a/cc1/tests/test047.c b/cc1/tests/test047.c
index 948a018..6f3ef1e 100644
--- a/cc1/tests/test047.c
+++ b/cc1/tests/test047.c
_AT_@ -7,8 +7,7 @@ S2 "S #N6 #N1
 M3 I "a #N0
 M4 I "b #N2
 M5 I "c #N4
-G6 S2 "x
-(
+G6 S2 "x (
         #I1
         #I2
         #I3
diff --git a/cc1/tests/test048.c b/cc1/tests/test048.c
index 16f4d97..e0230df 100644
--- a/cc1/tests/test048.c
+++ b/cc1/tests/test048.c
_AT_@ -7,8 +7,7 @@ S2 "S #N4 #N1
 M3 I "a #N0
 M4 I "b #N2
 V5 S2 #N1
-G6 V5 "x
-(
+G6 V5 "x (
         #I1
         #I2
 )
diff --git a/cc1/tests/test049.c b/cc1/tests/test049.c
index 9050a17..febd514 100644
--- a/cc1/tests/test049.c
+++ b/cc1/tests/test049.c
_AT_@ -3,12 +3,10 @@ name: TEST049
 description: Basic test for initializer
 error:
 output:
-G1 I "x
-(
+G1 I "x (
         #I5
 )
-G3 P "p
-(
+G3 P "p (
         G1 'P
 )
 G5 F "main
diff --git a/cc1/tests/test051.c b/cc1/tests/test051.c
index 2e2aab2..6b81814 100644
--- a/cc1/tests/test051.c
+++ b/cc1/tests/test051.c
_AT_@ -4,8 +4,7 @@ description: Basic test for initializer
 error:
 output:
 V1 I #N3
-G2 V1 "arr
-(
+G2 V1 "arr (
         #I0
         #I1
         #I2
diff --git a/cc1/tests/test052.c b/cc1/tests/test052.c
index 164d43b..ac62914 100644
--- a/cc1/tests/test052.c
+++ b/cc1/tests/test052.c
_AT_@ -7,8 +7,7 @@ S2 "S #N4 #N1
 M3 I "a #N0
 M4 I "b #N2
 V5 S2 #N2
-G6 V5 "arr
-(
+G6 V5 "arr (
         #I1
         #I2
         #I3
diff --git a/cc1/tests/test053.c b/cc1/tests/test053.c
index bd3e1af..319a8f6 100644
--- a/cc1/tests/test053.c
+++ b/cc1/tests/test053.c
_AT_@ -6,8 +6,7 @@ output:
 S2 "S #N4 #N1
 M3 I "a #N0
 M4 I "b #N2
-G5 S2 "s
-(
+G5 S2 "s (
         #I1
         #I2
 )
diff --git a/cc1/tests/test056.c b/cc1/tests/test056.c
index 3b62cc1..bf97936 100644
--- a/cc1/tests/test056.c
+++ b/cc1/tests/test056.c
_AT_@ -10,8 +10,7 @@ M4 I "b #N2
 M5 I "c #N4
 M7 V6 "d #N6
 M8 I "e #N9
-G9 S2 "s
-(
+G9 S2 "s (
         #I1
         #I2
         #I0
_AT_@ -21,8 +20,7 @@ G9 S2 "s
         #I0
 )
 V10 K #N0
-G11 V10 "m
-(
+G11 V10 "m (
 )
 G13 F "main
 {
diff --git a/cc1/tests/test057.c b/cc1/tests/test057.c
index 9fe72c3..a0b8d4b 100644
--- a/cc1/tests/test057.c
+++ b/cc1/tests/test057.c
_AT_@ -6,8 +6,7 @@ error:
 output:
 V1 I #N3
 V2 V1 #N2
-G3 V2 "arr1
-(
+G3 V2 "arr1 (
         #I2
         #I7
         #I5
_AT_@ -15,8 +14,7 @@ G3 V2 "arr1
         #I1
         #I2
 )
-G4 V2 "arr2
-(
+G4 V2 "arr2 (
         #I2
         #I7
         #I5
diff --git a/cc1/tests/test058.c b/cc1/tests/test058.c
index 870b897..f76908f 100644
--- a/cc1/tests/test058.c
+++ b/cc1/tests/test058.c
_AT_@ -6,8 +6,7 @@ output:
 V1 I #N5
 V2 V1 #N3
 V3 V2 #N2
-G4 V3 "arr
-(
+G4 V3 "arr (
         #I0
         #I0
         #I3
diff --git a/cc2/parser.c b/cc2/parser.c
index ac9a3e5..1234c76 100644
--- a/cc2/parser.c
+++ b/cc2/parser.c
_AT_@ -32,11 +32,11 @@ union tokenop {
 
 typedef void parsefun(char *, union tokenop);
 static parsefun type, symbol, getname, unary, binary, ternary, call,
- parameter, constant, composed;
+ parameter, constant, composed, begininit, endinit;
 
 typedef void evalfun(void);
-static evalfun vardecl, beginfun, endfun, endpars, stmt, begininit,
- endinit, array, aggregate, flddecl;
+static evalfun vardecl, beginfun, endfun, endpars, stmt,
+ array, aggregate, flddecl;
 
 static struct decoc {
         void (*eval)(void);
_AT_@ -76,8 +76,8 @@ static struct decoc {
         [ONAME] = {NULL, getname},
         ['{'] = {beginfun},
         ['}'] = {endfun},
- ['('] = {begininit},
- [')'] = {endinit},
+ ['('] = {NULL, begininit},
+ [')'] = {NULL, endinit},
         [OEPARS] = {endpars},
         [OSTMT] = {stmt},
 
_AT_@ -280,15 +280,13 @@ binary(char *token, union tokenop u)
 }
 
 static void
-begininit(void)
+begininit(char *token, union tokenop u)
 {
         ininit = 1;
- lastsym->type.flags |= INITF;
- label(lastsym);
 }
 
 static void
-endinit(void)
+endinit(char *token, union tokenop u)
 {
         ininit = 0;
 }
_AT_@ -368,6 +366,32 @@ array(void)
 }
 
 static void
+decl(Symbol *sym)
+{
+ switch (sym->kind) {
+ case EXTRN:
+ label(sym);
+ break;
+ case GLOB:
+ case PRIVAT:
+ case LOCAL:
+ label(sym);
+ if (!ininit)
+ allocdata(&sym->type);
+ break;
+ case AUTO:
+ case REG:
+ if (funpars >= 0) {
+ if (funpars == NR_FUNPARAM)
+ error(EOUTPAR);
+ params[funpars++] = sym;
+ break;
+ }
+ break;
+ }
+}
+
+static void
 vardecl(void)
 {
         Type *tp;
_AT_@ -375,20 +399,6 @@ vardecl(void)
         Symbol *sym;
         char *name;
 
- if (lastsym && (lastsym->type.flags & INITF) == 0) {
- switch (lastsym->kind) {
- case EXTRN:
- label(lastsym);
- break;
- case GLOB:
- case PRIVAT:
- case LOCAL:
- label(lastsym);
- allocdata(&lastsym->type);
- break;
- }
- }
-
         name = pop();
         tp = pop();
         np = pop();
_AT_@ -397,13 +407,11 @@ vardecl(void)
         sym->name = name;
         sym->type = *tp;
         sym->kind = sclass;
+ if (ininit)
+ sym->type.flags |= INITF;
         lastsym = sym;
+ decl(sym);
 
- if (funpars >= 0) {
- if (funpars == NR_FUNPARAM)
- error(EOUTPAR);
- params[funpars++] = sym;
- }
         delnode(np);
 }
 
_AT_@ -457,6 +465,7 @@ parse(void)
         size_t len;
         int c;
         struct decoc *dp;
+ void (*fun)(void);
 
         for (;;) {
                 if (!fgets(line, sizeof(line), stdin))
_AT_@ -475,7 +484,8 @@ parse(void)
                         (*dp->parse)(t, dp->u);
                 }
 
- (*optbl[c].eval)();
+ if ((fun = *optbl[c].eval) != NULL)
+ (*fun)();
                 if (sp != stack)
                         error(ESTACKA);
                 if (c == '}')
Received on Tue Jan 26 2016 - 10:57:43 CET

This archive was generated by hypermail 2.3.0 : Tue Jan 26 2016 - 11:00:44 CET