commit 9e8a53fee324554e6d0dea50071ab51d729096b3
parent ffc1d8068bbe61041c23aaccfa2457280ed97c27
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Mon, 16 Mar 2015 06:12:51 +0000
Add MEM addressing
MEM addressing is intended for static variables.
Diffstat:
3 files changed, 27 insertions(+), 7 deletions(-)
diff --git a/cc2/cc2.h b/cc2/cc2.h
@@ -56,6 +56,7 @@ struct symbol {
bool public : 1;
bool extrn : 1;
char type;
+ unsigned short id;
union {
struct {
Type type;
diff --git a/cc2/code.c b/cc2/code.c
@@ -97,6 +97,9 @@ addr(char op, Node *np, Addr *addr)
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 @@ writeout(void)
static void
addr2txt(Addr *a)
-{
+{
+ Symbol *sym;
+
switch (a->kind) {
case REG:
fputs(regnames[a->u.reg], stdout);
@@ -135,11 +140,17 @@ addr2txt(Addr *a)
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();
}
diff --git a/cc2/parser.c b/cc2/parser.c
@@ -175,38 +175,47 @@ static Symbol *
parameter(char *num)
{
static Symbol tbl[NR_FUNPARAM];
+ Symbol *sym;
unsigned i = atoi(num);
if (!curfun)
error(ESYNTAX);
if (i >= NR_FUNPARAM)
error(EPARNUM);
- return &tbl[i];
+ sym = &tbl[i];
+ sym->id = i;
+ return sym;
}
static Symbol *
local(char *num)
{
static Symbol tbl[NR_INT_IDENT];
+ Symbol *sym;
unsigned i = atoi(num);
if (!curfun)
error(ESYNTAX);
if (i >= NR_INT_IDENT)
error(EINTNUM);
- return &tbl[i];
+ sym = &tbl[i];
+ sym->id = i;
+ return sym;
}
static Symbol *
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 @@ declaration(uint8_t t, char class, char *token)
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;