shithub: scc

Download patch

ref: 2149f9e083fe7d75fa41e011ba8923b5c8a7da2e
parent: 0681c99653ef080b235082875a2b6e5490ec4ff8
author: Roberto E. Vargas Caballero <[email protected]>
date: Mon Apr 21 03:58:40 EDT 2014

Unify symbol and constant code

The only difference in this case is what code is called, so
it is a good idea merge them.

--- a/cc1.h
+++ b/cc1.h
@@ -195,6 +195,7 @@
 	struct {
 		bool lvalue : 1;
 		bool symbol: 1;
+		bool constant : 1;
 	} b;
 	union unode {
 		Symbol *sym;
@@ -223,7 +224,7 @@
 extern void
 	emitdcl(Symbol *), emitsframe(Symbol *), emiteframe(Symbol *),
 	emitsym(Node *), emitunary(Node *),
-	emitbin(Node *), emitexp(Node *), emitconst(Node *np),
+	emitbin(Node *), emitexp(Node *),
 	emitprint(Node *);
 
 extern Node
@@ -233,7 +234,7 @@
 	*castcode(Node *child, Type *tp),
 	*sizeofcode(Type *tp), 
 	*ternarycode(Node *cond, Node *ifyes, Node *ifno),
-	*constcode(Symbol *sym);
+	*symcode(Symbol *sym);
 
 #define SYM(s) ((union unode) {.sym = s})
 #define OP(s) ((union unode) {.op = s})
--- a/code.c
+++ b/code.c
@@ -78,11 +78,17 @@
 	printf("%c%d", c, sym->id);
 }
 
+static void
+emitconst(Node *np)
+{
+	printf("#%x", np->u.sym->u.i);
+}
+
 void
 emitsym(Node *np)
 {
 	putchar('\t');
-	emitsym2(np->u.sym);
+	(np->b.constant) ? emitconst(np) : emitsym2(np->u.sym);
 }
 
 static void
@@ -101,12 +107,6 @@
 }
 
 void
-emitconst(Node *np)
-{
-	printf("\t#%x", np->u.sym->u.i);
-}
-
-void
 emitcast(Node *np)
 {
 	Node *child = np->childs[0];
@@ -244,11 +244,12 @@
 }
 
 Node *
-constcode(Symbol *sym)
+symcode(Symbol *sym)
 {
 	Node *np;
 
-	np = node(emitconst, inttype, SYM(sym), 0);
+	np = node(emitsym, inttype, SYM(sym), 0);
 	np->b.symbol = 1;
+	np->b.constant = 1;
 	return np;
 }
--- a/expr.c
+++ b/expr.c
@@ -226,7 +226,7 @@
 		return np;
 	}
 
-	return compare(ONE ^ neg, np, constcode(zero));
+	return compare(ONE ^ neg, np, symcode(zero));
 }
 
 static Node *
@@ -270,7 +270,7 @@
 {
 	if (ISNODECMP(np))
 		return np;
-	return compare(ONE, np, constcode(zero));
+	return compare(ONE, np, symcode(zero));
 }
 
 static Node *
@@ -278,7 +278,7 @@
 {
 	if (!ISNODECMP(np))
 		return np;
-	return ternarycode(np, constcode(one), constcode(zero));
+	return ternarycode(np, symcode(one), symcode(zero));
 }
 
 static Node *
@@ -356,15 +356,14 @@
 	Symbol *sym;
 
 	switch (yytoken) {
-	case IDEN:
+	case CONSTANT: case IDEN:
 		if ((sym = yylval.sym) == NULL)
 			error("'%s' undeclared", yytext);
-		np = node(emitsym, sym->type, SYM(sym), 0);
-		np->b.symbol = np->b.lvalue = 1;
-		next();
-		break;
-	case CONSTANT:
-		np = constcode(yylval.sym);
+		np = symcode(yylval.sym);
+		if (yytoken == IDEN) {
+			np->b.lvalue = 1;
+			np->b.constant = 0;
+		}
 		next();
 		break;
 	/* TODO: case STRING: */