shithub: scc

Download patch

ref: 00f81a75f35cbb0e33e92a0a3bf4486aa742427d
parent: 09ca91a889f294f7cc61677ac57c16d8303248f2
author: Roberto E. Vargas Caballero <[email protected]>
date: Wed Sep 10 18:58:10 EDT 2014

Remove list of locals and parameters of cc2

These list is a non sense because we can calculate the
offset in each declaration. We lost know how much
arguments and locals has the function, but I think it
is not important (we can recover it later).

--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -11,7 +11,6 @@
 	bool public : 1;
 	bool extrn : 1;
 	char type;
-	struct symbol *next;
 	union {
 		struct {
 			Type *type;
@@ -22,9 +21,8 @@
 			short addr;
 		} l;
 		struct {
-			short stack;
-			struct symbol *pars;
-			struct symbol *vars;
+			short locals;
+			short params;
 		} f;
 	} u;
 } Symbol;
--- a/cc2/cgen.c
+++ b/cc2/cgen.c
@@ -9,22 +9,7 @@
 
 #include <stdio.h>
 
-void
-genstack(Symbol *fun)
-{
-	Symbol *p;
-	short off;
 
-	for (off = 0, p = fun->u.f.vars; p; p = p->next) {
-		if (p->u.v.sclass == AUTO) {
-			p->u.v.off = off;
-			off += p->u.v.type->align;
-		}
-	}
-
-	fun->u.f.stack = off;
-}
-
 enum {
 	PUSH, POP, LD, ADD, RET, ADDI, LDI, ADDR, ADDX, ADCX, LDX
 };
@@ -144,13 +129,13 @@
 {
 	extern char odebug;
 	Node *np;
-	char frame = sym->u.f.stack != 0 || odebug;
+	char frame = sym->u.f.locals != 0 || odebug;
 
 	emit(ADDR, sym->name);
 	if (frame) {
 		emit(PUSH, IX);
 		emit(LD, IX, SP);
-		emit(LDI, HL, -sym->u.f.stack);
+		emit(LDI, HL, -sym->u.f.locals);
 		emit(ADD, HL, SP);
 		emit(LD, SP, HL);
 	}
--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -83,6 +83,8 @@
 	static Symbol tbl[NR_FUNPARAM];
 	unsigned i = atoi(num);
 
+	if (!curfun)
+		error(ESYNTAX);
 	if (i >= NR_FUNPARAM)
 		error(EPARNUM);
 	return &tbl[i];
@@ -94,6 +96,8 @@
 	static Symbol tbl[NR_INT_IDENT];
 	unsigned i = atoi(num);
 
+	if (!curfun)
+		error(ESYNTAX);
 	if (i >= NR_INT_IDENT)
 		error(EINTNUM);
 	return &tbl[i];
@@ -366,7 +370,6 @@
 	if (!curfun)
 		error(ESYNTAX);
 	listp = NULL;
-	genstack(curfun);
 	genaddable(listexp);
 	cgen(curfun, listexp);
 	curfun = NULL;
@@ -378,8 +381,6 @@
 	Symbol *sym = symbol(t, token);
 	char *s;
 
-	if (t == LOCAL && !curfun)
-		error(ESYNTAX);
 	if (sym->name)
 		free(sym->name);
 	memset(sym, 0, sizeof(*sym));
@@ -416,8 +417,6 @@
 		error(ESYNTAX);
 
 	sym->type = FUN;
-	sym->u.f.vars = NULL;
-	sym->u.f.pars = NULL;
 	curfun = sym;
 	listp = listexp;
 	newp = nodepool;
@@ -427,8 +426,8 @@
 paramdcl(char *token)
 {
 	Symbol *sym = declaration(PARAMETER, AUTO, token);
-	sym->next = curfun->u.f.pars;
-	curfun->u.f.pars = sym;
+	sym->u.v.off = -curfun->u.f.params;
+	curfun->u.f.params += sym->u.v.type->size;
 }
 
 static void
@@ -435,8 +434,8 @@
 localdcl(char *token)
 {
 	Symbol *sym = declaration(LOCAL, token[0], token);
-	sym->next = curfun->u.f.vars;
-	curfun->u.f.vars = sym;
+	sym->u.v.off = -curfun->u.f.locals;
+	curfun->u.f.locals += sym->u.v.type->size;
 }
 
 void