shithub: scc

Download patch

ref: 0b39941d7622ebf6733e8d626c4b57404383d42f
parent: 3421169ce3c39355690ad27754764dff1e76af2a
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon May 11 06:23:24 EDT 2015

Create different functions for constant and variable nodes

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -274,7 +274,8 @@
 /* code.c */
 extern void emit(uint8_t, void *);
 extern Node *node(uint8_t op, Type *tp, Node *left, Node *rigth);
-extern Node *symbol(Symbol *sym);
+extern Node *varnode(Symbol *sym);
+extern Node *constnode(Symbol *sym);
 extern void freetree(Node *np);
 
 /* expr.c */
--- a/cc1/code.c
+++ b/cc1/code.c
@@ -330,7 +330,20 @@
 }
 
 Node *
-symbol(Symbol *sym)
+varnode(Symbol *sym)
+{
+	Node *np;
+
+	np = node(OSYM, sym->type, NULL, NULL);
+	np->lvalue = 1;
+	np->constant = 0;
+	np->symbol = 1;
+	np->sym = sym;
+	return np;
+}
+
+Node *
+constnode(Symbol *sym)
 {
 	Node *np;
 
--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -67,7 +67,7 @@
 		return NULL;
 	if (np->op != OAND && np->op != OOR)
 		return np;
-	p = node(OCOLON, inttype, symbol(one), symbol(zero));
+	p = node(OCOLON, inttype, constnode(one), constnode(zero));
 	return node(OASK, inttype, np, p);
 }
 
@@ -315,7 +315,7 @@
 {
 	if (isnodecmp(np))
 		return (neg) ? negate(np) : np;
-	return compare(ONE ^ neg, np, symbol(zero));
+	return compare(ONE ^ neg, np, constnode(zero));
 }
 
 static Node *
@@ -342,7 +342,7 @@
 			error("incorrect field in struct/union");
 		lex_ns = NS_IDEN;
 		next();
-		return node(OFIELD, sym->type, symbol(sym), np);
+		return node(OFIELD, sym->type, varnode(sym), np);
 	default:
 		error("struct or union expected");
 	}
@@ -369,7 +369,7 @@
 {
 	if (isnodecmp(np))
 		return np;
-	return compare(ONE, np, symbol(zero));
+	return compare(ONE, np, constnode(zero));
 }
 
 static Node *
@@ -403,7 +403,7 @@
 		break;
 	case INT:
 	case FLOAT:
-		inc = symbol(one);
+		inc = constnode(one);
 		break;
 	default:
 		error("incorrect type in arithmetic operation");
@@ -459,18 +459,19 @@
 primary(void)
 {
 	Node *np;
-	Symbol *sym;
 
 	switch (yytoken) {
 	case CONSTANT:
+		np = constnode(yylval.sym);
+		next();
+		break;
 	case IDEN:
-		if ((sym = yylval.sym) == NULL)
+		if (yylval.sym == NULL) {
+			yylval.sym = install(yytext, NS_IDEN);
+			yylval.sym->type = inttype;
 			error("'%s' undeclared", yytext);
-		np = symbol(yylval.sym);
-		if (yytoken == IDEN) {
-			np->lvalue = 1;
-			np->constant = 0;
 		}
+		np = varnode(yylval.sym);
 		next();
 		break;
 	case '(':
--- a/cc1/stmt.c
+++ b/cc1/stmt.c
@@ -57,7 +57,7 @@
 	if (!setjmp(recover))
 		np = expr();
 	else
-		np = symbol(zero);
+		np = constnode(zero);
 	np = iszero(np);
 	expect(')');
 	return np;