shithub: scc

Download patch

ref: 9e8a53fee324554e6d0dea50071ab51d729096b3
parent: ffc1d8068bbe61041c23aaccfa2457280ed97c27
author: Roberto E. Vargas Caballero <[email protected]>
date: Mon Mar 16 02:12:51 EDT 2015

Add MEM addressing

MEM addressing is intended for static variables.

--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -56,6 +56,7 @@
 	bool public : 1;
 	bool extrn : 1;
 	char type;
+	unsigned short id;
 	union {
 		struct {
 			Type type;
--- a/cc2/code.c
+++ b/cc2/code.c
@@ -97,6 +97,9 @@
 	case AUTO:
 		addr->u.i = np->u.sym->u.v.off;
 		break;
+	case MEM:
+		addr->u.sym = np->u.sym;
+		break;
 	default:
 		abort();
 	}
@@ -127,7 +130,9 @@
 
 static void
 addr2txt(Addr *a)
-{	
+{
+	Symbol *sym;
+
 	switch (a->kind) {
 	case REG:
 		fputs(regnames[a->u.reg], stdout);
@@ -135,11 +140,17 @@
 	case CONST:
 		printf("%d", a->u.i);
 		break;
+	case PAR:
 	case AUTO:
 		printf("(IX+%d)", a->u.i);
 		break;
 	case MEM:
-	case PAR:
+		sym = a->u.sym;
+		if (sym->name)
+			printf("(%s)", sym);
+		else
+			printf("(T%u)", sym->id);
+		break;
 	default:
 		abort();
 	}
--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -175,6 +175,7 @@
 parameter(char *num)
 {
 	static Symbol tbl[NR_FUNPARAM];
+	Symbol *sym;
 	unsigned i = atoi(num);
 
 	if (!curfun)
@@ -181,7 +182,9 @@
 		error(ESYNTAX);
 	if (i >= NR_FUNPARAM)
 		error(EPARNUM);
-	return &tbl[i];
+	sym = &tbl[i];
+	sym->id = i;
+	return sym;
 }
 
 static Symbol *
@@ -188,6 +191,7 @@
 local(char *num)
 {
 	static Symbol tbl[NR_INT_IDENT];
+	Symbol *sym;
 	unsigned i = atoi(num);
 
 	if (!curfun)
@@ -194,7 +198,9 @@
 		error(ESYNTAX);
 	if (i >= NR_INT_IDENT)
 		error(EINTNUM);
-	return &tbl[i];
+	sym = &tbl[i];
+	sym->id = i;
+	return sym;
 }
 
 static Symbol *
@@ -201,12 +207,15 @@
 global(char *num)
 {
 	static Symbol tbl[NR_EXT_IDENT];
+	Symbol *sym;
 	unsigned i = atoi(num);
 
 	if (i >= NR_EXT_IDENT)
 		error(EEXTNUM);
 
-	return &tbl[i];
+	sym = &tbl[i];
+	sym->id = i;
+	return sym;
 }
 
 static Node *
@@ -446,8 +455,7 @@
 	Symbol *sym = symbol(t, token);
 	char *s;
 
-	if (sym->name)
-		free(sym->name);
+	free(sym->name);
 	memset(sym, 0, sizeof(*sym));
 	sym->type = VAR;
 	sym->u.v.sclass = class;