scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit 90c4f9ccb0adff2fd1a188567d4553abdc13094a
parent 3eb074604d8c04734b97de4ef489f2a94431c5a8
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 19 Jan 2016 07:29:18 +0100

Emit constant arrays

This is a first step to emit compuund literals. This patch adds
the basic support for arrays.

Diffstat:
cc1/code.c | 33+++++++++++++++++++++++++--------
cc1/init.c | 5++++-
2 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/cc1/code.c b/cc1/code.c @@ -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: @@ -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; @@ -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 @@ -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);