[hackers] [scc] Emit constant arrays || Roberto E. Vargas Caballero
commit 90c4f9ccb0adff2fd1a188567d4553abdc13094a
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Tue Jan 19 07:29:18 2016 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Tue Jan 19 07:46:21 2016 +0100
Emit constant arrays
This is a first step to emit compuund literals. This patch adds
the basic support for arrays.
diff --git a/cc1/code.c b/cc1/code.c
index 9535004..6143bc2 100644
--- a/cc1/code.c
+++ b/cc1/code.c
_AT_@ -188,6 +188,8 @@ emitconst(Node *np)
Symbol *sym = np->sym;
Type *tp = np->type;
TUINT u;
+ size_t n;
+ Node **p;
switch (tp->op) {
case PTR:
_AT_@ -199,13 +201,19 @@ emitconst(Node *np)
(long long) sym->u.i & ones(tp->size));
break;
case ARY:
- /*
- * FIX: At this point we are going to assume
- * that all the arrays are strings
- */
- putchar('"');
- for (bp = sym->u.s; c = *bp; ++bp)
- printf("%02X", c & 0xFF);
+ if (sym->flags & ISSTRING) {
+ putchar('"');
+ for (bp = sym->u.s; c = *bp; ++bp)
+ printf("%02X", c & 0xFF);
+ /* TODO: Why we don't free here? */
+ } else if (sym->flags & ISINITLST) {
+ n = tp->n.elem;
+ for (p = sym->u.init; n--; ++p)
+ emitexp(OEXPR, *p);
+ free(sym->u.init);
+ } else {
+ abort();
+ }
break;
case STRUCT:
return;
_AT_@ -219,7 +227,16 @@ static void
emitsym(unsigned op, void *arg)
{
Node *np = arg;
- putchar('\t');
+
+ if ((np->sym->flags & ISINITLST) == 0) {
+ /*
+ * When we have a compound literal we are going
+ * to call to emitnode for every element of it,
+ * and it means that we will have two '\t'
+ * for the first element
+ */
+ putchar('\t');
+ }
(np->constant) ? emitconst(np) : emitvar(np->sym);
}
diff --git a/cc1/init.c b/cc1/init.c
index 079b4cc..6e15af2 100644
--- a/cc1/init.c
+++ b/cc1/init.c
_AT_@ -118,7 +118,10 @@ mkcompound(Init *ip)
for (dp = ip->head; dp; dp = next) {
p = &v[dp->pos];
- freetree(*p);
+ if (*p) {
+ warn("double initialization in compound literal");
+ freetree(*p);
+ }
*p = dp->expr;
next = dp->next;
free(dp);
Received on Tue Jan 19 2016 - 10:55:46 CET
This archive was generated by hypermail 2.3.0
: Tue Jan 19 2016 - 11:00:24 CET