shithub: scc

Download patch

ref: 690e4af100608ab79133ca0743235ad8b9d9ad15
parent: 688c48119a25ceef7429ee6cc280cfbe1b2c017a
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue Apr 15 04:44:21 EDT 2014

Install constant as symbols

It is necessary do this task, because in other case it is impossible
to know what value a previous constant had.

--- a/cc.h
+++ b/cc.h
@@ -227,7 +227,7 @@
 
 extern void
 	emitsym(Node *), emitunary(Node *),
-	emitbin(Node *), emitexp(Node *);
+	emitbin(Node *), emitexp(Node *), emitconst(Node *np);
 
 extern Node
 	*node(Inst code, Type *tp, union unode u, uint8_t nchilds),
--- a/code.c
+++ b/code.c
@@ -71,6 +71,12 @@
 }
 
 void
+emitconst(Node *np)
+{
+	printf("\t#%X", np->u.sym->u.i);
+}
+
+void
 emitcast(Node *np)
 {
 	Node *child = np->childs[0];
--- a/expr.c
+++ b/expr.c
@@ -235,8 +235,9 @@
 		next();
 		break;
 	case CONSTANT:
+		sym = yylval.sym;
+		np = node(emitconst, sym->type, SYM(sym), 0);
 		next();
-		/* TODO: do something */
 		break;
 	case '(':
 		next();
--- a/lex.c
+++ b/lex.c
@@ -24,11 +24,13 @@
 integer(char *s, char base)
 {
 	static Type *tp;
+	static Symbol *sym;
 	static char ch;
 
 	/* TODO: implement again */
 
-type:	switch (ch = toupper(getc(yyin))) {
+type:
+	switch (ch = toupper(getc(yyin))) {
 	case 'L':
 		goto type;
 	case 'U':
@@ -37,6 +39,10 @@
 		ungetc(ch, yyin);
 	}
 
+	sym = install("", NS_IDEN);
+	sym->type = inttype;
+	sym->u.i = atoi(yytext);
+	yynlval.sym = sym;
 	return CONSTANT;
 }
 
--