shithub: scc

Download patch

ref: bcb8fd114a9d34c4b06a9f5ed02c5d6f54f35366
parent: 88d8d2ff08a1a1dd7d67655b9ff6869ef295ce61
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Jan 22 09:53:02 EST 2016

Emit initializers for strings

This modification removes strings from the expressions, which simplifies
the parsing of them, because we can be sure that lines are not too long.

--- a/cc1/code.c
+++ b/cc1/code.c
@@ -1,4 +1,5 @@
 
+#include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
@@ -189,7 +190,6 @@
 	Type *tp = np->type;
 	TUINT u;
 	size_t n;
-	Node **p;
 
 	switch (tp->op) {
 	case PTR:
@@ -200,27 +200,7 @@
 		       np->type->letter,
 		       (long long) sym->u.i & ones(tp->size));
 		break;
-	case ARY:
-		/* TODO: All this code must go out */
-		if (sym->flags & ISSTRING) {
-			putchar('"');
-			n = tp->n.elem;
-			for (bp = sym->u.s; n-- > 0; ++bp)
-				printf("%02X", (*bp) & 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;
 	default:
-		/* TODO: Handle other kind of constants */
 		abort();
 	}
 }
@@ -308,6 +288,32 @@
 }
 
 static void
+emitstring(Symbol *sym, Type *tp)
+{
+	char *bp, *s, *lim;
+	int n;
+
+	bp = bp = sym->u.s;
+	lim = &sym->u.s[tp->n.elem];
+	while (bp < lim) {
+		s = bp;
+		while (isprint(*bp) && bp < lim)
+			++bp;
+		if ((n = bp - s) > 1)
+			printf("\t#\"%.*s\n", n, s);
+		else
+			bp = s;
+		if (bp == lim)
+			break;
+		do {
+			printf("\t#%c%02X\n",
+			       chartype->letter, (*bp++) & 0xFF);
+		} while (!isprint(*bp) && bp < lim);
+	}
+	/* TODO: Why we don't free here? */
+}
+
+static void
 emitdesig(Node *np, Type *tp)
 {
 	Symbol *sym;
@@ -321,6 +327,10 @@
 		if (!np->sym)
 			goto emit_expression;
 		sym = np->sym;
+		if (sym->flags & ISSTRING) {
+			emitstring(sym, tp);
+			return;
+		}
 		if ((sym->flags & ISINITLST) == 0)
 			goto emit_expression;
 	}
--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -617,6 +617,12 @@
 	sym = yylval.sym;
 	switch (yytoken) {
 	case STRING:
+		np = constnode(sym);
+		emit(ODECL, sym);
+		emit(OINIT, np);
+		np = decay(varnode(sym));
+		next();
+		break;
 	case CONSTANT:
 		np = constnode(sym);
 		next();
--- a/cc1/init.c
+++ b/cc1/init.c
@@ -107,11 +107,13 @@
 	}
 
 	np = (yytoken == '{') ? initlist(tp) : assign();
-	sym = np->sym;
 
-	if (sym && sym->flags&ISSTRING && tp->op == ARY) {
+	if (np->left &&
+	    np->left->sym->flags & ISSTRING &&
+	    tp->op == ARY) {
+		sym = np->left->sym;
 		btp = tp->type;
-		if (btp != chartype &&
+		if (btp != chartype  &&
 		    btp != uchartype &&
 		    btp != schartype) {
 			errorp("array of inappropriate type initialized from string constant");
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
@@ -230,6 +230,7 @@
 {
 	Symbol *sym = newsym(NS_IDEN);
 
+	sym->id = newid();
 	sym->flags |= ISSTRING | ISCONSTANT | ISPRIVATE;
 	sym->u.s = xmalloc(len);
 	if (s)
--- a/cc1/tests/test001.c
+++ b/cc1/tests/test001.c
@@ -9,7 +9,14 @@
 G6	F5	main
 {
 \
-	X4	"68656C6C6F20776F726C640A00	'P	pP	cI
+V8	K	#13
+Y7	V8
+(
+	#"hello world
+	#K0A
+	#K00
+)
+	X4	Y7	'P	pP	cI
 	r	#I0
 }
 */
--- a/cc1/tests/test026.c
+++ b/cc1/tests/test026.c
@@ -10,8 +10,14 @@
 \
 A4	I	y
 A6	P	p
-	A6	"746573743032362E6300	'P	:P
-	A4	#I1F	:I
+V8	K	#10
+Y7	V8
+(
+	#"test026.c
+	#K00
+)
+	A6	Y7	'P	:P
+	A4	#I25	:I
 	A4	#I1	:I
 	A4	#I1	:I
 	A4	#I1	:I
--- a/cc1/tests/test027.c
+++ b/cc1/tests/test027.c
@@ -9,7 +9,13 @@
 {
 \
 A5	P	p
-	A5	"68656C6C6F20697320626574746572207468616E2062796500	'P	:P
+V7	K	#25
+Y6	V7
+(
+	#"hello is better than bye
+	#K00
+)
+	A5	Y6	'P	:P
 	r	A5	@K	gI
 }
 */
@@ -24,4 +30,3 @@
 
 	return *p;
 }
-
--- a/cc1/tests/test028.c
+++ b/cc1/tests/test028.c
@@ -8,7 +8,13 @@
 G6	F5	foo
 {
 \
-	r	"686900	'P
+V8	K	#3
+Y10	V8
+(
+	#"hi
+	#K00
+)
+	r	Y10	'P
 }
 */
 
--- a/cc1/tests/test032.c
+++ b/cc1/tests/test032.c
@@ -4,13 +4,19 @@
 description: test special characters @ and $ in macro definitions
 error:
 output:
-F3	I
-G4	F3	main
+F4	I
+G5	F4	main
 {
 \
-A6	P	p
-	A6	"54686973206973206120737472696E672024206F722023206F72202323616E64206974206973206F6B202100	'P	:P
-	r	A6	#P0	!I
+V9	K	#44
+Y8	V9
+(
+	#"This is a string $ or # or ##and it is ok !
+	#K00
+)
+A7	P	p
+	A7	Y8	'P	:P
+	r	A7	#P0	!I
 }
 */
 
--- a/cc1/tests/test056.c
+++ b/cc1/tests/test056.c
@@ -20,13 +20,14 @@
 	#K0
 	#I0
 )
-F10	I
-G11	F10	main
+V10	K	#0
+G11	V10	m
+(
+)
+F12	I
+G13	F12	main
 {
 \
-V12	K	#0
-A13	V12	m
-	A13	:V12
 	r	G9	M7	.V6	'P	#P2	+P	@K	gI	gN	#N0	!I
 }
 */
@@ -42,10 +43,10 @@
 	.d = {[0] = 4, [1] = 6}
 };
 
+char m[] = {};
+
 int
 main(void)
 {
-	char m[] = {};
-
 	return sizeof(m) == s.d[2];
 }