shithub: scc

Download patch

ref: 90c4f9ccb0adff2fd1a188567d4553abdc13094a
parent: 3eb074604d8c04734b97de4ef489f2a94431c5a8
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue Jan 19 02:29:18 EST 2016

Emit constant arrays

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

--- a/cc1/code.c
+++ b/cc1/code.c
@@ -188,6 +188,8 @@
 	Symbol *sym = np->sym;
 	Type *tp = np->type;
 	TUINT u;
+	size_t n;
+	Node **p;
 
 	switch (tp->op) {
 	case PTR:
@@ -199,13 +201,19 @@
 		       (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 @@
 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);
 }
 
--- a/cc1/init.c
+++ b/cc1/init.c
@@ -118,7 +118,10 @@
 
 	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);