[hackers] [scc] [cc2] Generate code for static variables no initialized || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Mon, 25 Jan 2016 11:00:38 +0100 (CET)

commit d2b15f1ea22699cecc79a9cf2a10c236b807a191
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Sun Jan 24 22:17:20 2016 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Sun Jan 24 22:17:20 2016 +0100

    [cc2] Generate code for static variables no initialized
    
    These variables must go to BSS.

diff --git a/cc2/arch/amd64-sysv/code.c b/cc2/arch/amd64-sysv/code.c
index 3fb051c..566d470 100644
--- a/cc2/arch/amd64-sysv/code.c
+++ b/cc2/arch/amd64-sysv/code.c
_AT_@ -3,6 +3,11 @@
 #include "../../cc2.h"
 
 void
+allocdata(Type *tp)
+{
+}
+
+void
 data(Node *np)
 {
 }
diff --git a/cc2/arch/i386-sysv/code.c b/cc2/arch/i386-sysv/code.c
index 3fb051c..566d470 100644
--- a/cc2/arch/i386-sysv/code.c
+++ b/cc2/arch/i386-sysv/code.c
_AT_@ -3,6 +3,11 @@
 #include "../../cc2.h"
 
 void
+allocdata(Type *tp)
+{
+}
+
+void
 data(Node *np)
 {
 }
diff --git a/cc2/arch/z80/code.c b/cc2/arch/z80/code.c
index ce0d2fe..448b988 100644
--- a/cc2/arch/z80/code.c
+++ b/cc2/arch/z80/code.c
_AT_@ -104,15 +104,16 @@ emittree(Node *np)
         }
 }
 
-void
-data(Node *np)
+
+static void
+size2asm(Type *tp)
 {
         char *s;
 
         /*
          * In z80 we can ignore the alignment
          */
- switch (np->type.size) {
+ switch (tp->size) {
         case 1:
                 s = "\tDB\t";
                 break;
_AT_@ -123,10 +124,23 @@ data(Node *np)
                 s = "\tDD\t";
                 break;
         default:
- s = "\tDS\t";
+ s = (tp->flags & STRF) ? "\tTEXT\t" : "\tDS\t%llu,";
                 break;
         }
- fputs(s, stdout);
+ printf(s, (unsigned long long) tp->size);
+}
+
+void
+allocdata(Type *tp)
+{
+ size2asm(tp);
+ puts("0");
+}
+
+void
+data(Node *np)
+{
+ size2asm(&np->type);
         emittree(np);
         putchar('\n');
 }
diff --git a/cc2/cc2.h b/cc2/cc2.h
index b2eb4c3..b59e402 100644
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
_AT_@ -153,6 +153,7 @@ extern void peephole(void);
 /* code.c */
 extern void label(Symbol *sym);
 extern void data(Node *np);
+extern void allocdata(Type *tp);
 extern void writeout(void);
 
 /* node.c */
diff --git a/cc2/parser.c b/cc2/parser.c
index 796212c..6fb8028 100644
--- a/cc2/parser.c
+++ b/cc2/parser.c
_AT_@ -208,9 +208,11 @@ constant(char *token, union tokenop u)
 
         ++token;
         if (*token == OSTRING) {
+ ++token;
                 np->op = OSTRING;
                 np->type.flags = STRF;
- np->u.s = xstrdup(++token);
+ np->type.size = strlen(token);
+ np->u.s = xstrdup(token);
         } else {
                 np->op = OCONST;
                 np->type = *gettype(token++);
_AT_@ -349,6 +351,20 @@ 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();
Received on Mon Jan 25 2016 - 11:00:38 CET

This archive was generated by hypermail 2.3.0 : Mon Jan 25 2016 - 11:12:24 CET