shithub: scc

Download patch

ref: d2c30b9ebacee9c0ea90050133641e8800727e4d
parent: cb489b71868e30f3f1d04e152be2bc106f27b060
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Sep 21 08:34:12 EDT 2017

[as] Add temporaries symbols

These symbols are used for constants in the code and
for folded values. They are freed at the end of every
pass.

--- a/as/as.h
+++ b/as/as.h
@@ -102,6 +102,8 @@
 extern void writeout(char *name);
 extern void emit(Section *sec, char *bytes, int nbytes);
 extern Section *section(char *name);
+extern Symbol *tmpsym(TUINT val);
+extern void killtmp(void);
 
 /* main.c */
 extern Symbol *lookup(char *name);
--- a/as/expr.c
+++ b/as/expr.c
@@ -14,7 +14,6 @@
 	IDEN = 1,
 	NUMBER,
 	REG,
-	CHAR,
 	STRING,
 	SHL,
 	SHR,
@@ -22,10 +21,16 @@
 	LE,
 };
 
+union yylval {
+	TUINT val;
+	Symbol *sym;
+};
+
 static Alloc *arena;
 static int yytoken;
 static char yytext[INTIDENTSIZ+1], *textp, *endp;
 static size_t yylen;
+static union yylval yylval;
 
 #define accept(t) (yytoken == (t) ? next() : 0)
 
@@ -121,7 +126,7 @@
 	}
 
 	np = node(NUMBER, l, r);
-	/* np->sym = tmpsym(val); */
+	np->sym = tmpsym(val);
 	return np;
 
 division_by_zero:
@@ -172,6 +177,9 @@
 
 	for (endp = textp; isalnum(c = *endp) || c == '_' || c == '.'; ++endp)
 		/* nothing */;
+	tok2str();
+	yylval.sym = lookup(yytext);
+
 	return IDEN;
 }
 
@@ -183,6 +191,9 @@
 
 	for (endp = textp; isxdigit(*endp); ++endp)
 		/* nothing */;
+	tok2str();
+	yylval.sym = tmpsym(atoi(yytext));  /* TODO: parse the string */
+
 	return NUMBER;
 }
 
@@ -194,7 +205,7 @@
 
 	for (endp = textp+1; *endp != '\''; ++endp)
 		/* nothing */;
-	return CHAR;
+	return NUMBER;
 }
 
 static int
@@ -261,9 +272,9 @@
 	switch (yytoken) {
 	case NUMBER:
 	case IDEN:
-	case CHAR:
 	case STRING:
 		np = node(yytoken, NULL, NULL);
+		np->sym = yylval.sym;
 		next();
 		break;
 	case '(':
--- a/as/main.c
+++ b/as/main.c
@@ -72,6 +72,13 @@
 
 	if (fclose(fp))
 		die("as: error reading from input file '%s'", fname);
+	if (pass == 2)
+		writeout("a.out");
+	/*
+	 * kill tmp symbols because they are not needed anymore
+	 */
+	killtmp();
+
 	return nerrors == 0;
 }
 
@@ -89,9 +96,10 @@
 		usage();
 
 	filename = argv[1];
-	for (pass = 1; pass <= 2 && dopass(filename); pass++)
-		/* nothing */;
-	writeout("a.out");
+	for (pass = 1; pass <= 2; pass++) {
+		if (!dopass(filename))
+			return 1;
+	}
 
 	return 0;
 }
--- a/as/symbol.c
+++ b/as/symbol.c
@@ -8,6 +8,7 @@
 #include "as.h"
 
 #define HASHSIZ 64
+#define NALLOC  10
 
 static Section abss = {
 	.name  = "abs",
@@ -37,6 +38,8 @@
 int pass;
 
 static Symbol *hashtbl[HASHSIZ];
+static Alloc *tmpalloc;
+
 Symbol *linesym;
 
 Symbol *
@@ -199,6 +202,27 @@
 	if (sec->mem)
 		memcpy(&sec->mem[sec->pc - sec->base], bytes, n);
 	incpc(n);
+}
+
+Symbol *
+tmpsym(TUINT val)
+{
+	Symbol *sym;
+
+	if (!tmpalloc)
+		tmpalloc = alloc(sizeof(*sym), NALLOC);
+	sym = new(tmpalloc);
+	sym->value = val;
+	sym->flags = TABS;
+
+	return sym;
+}
+
+void
+killtmp(void)
+{
+	dealloc(tmpalloc);
+	tmpalloc = NULL;
 }
 
 void