scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit cfd9181ebc248b4eeb49e80fa82efbe95404e87b
parent abf79a2fe6621ed253aa4559e06cc20d1189ff4b
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun, 10 Aug 2014 10:55:35 +0200

Add offset field in symbols

When symbols are stored in the stack they have associated an offset
in the same that will be used in the instructions.

Diffstat:
cc2/cc2.h | 6+++---
cc2/cgen.c | 13+++++++++----
cc2/parser.c | 2+-
3 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -12,7 +12,8 @@ typedef struct symbol { union { struct { Type *type; - char storage; + char sclass; + short off; } v; struct { short addr; @@ -68,4 +69,4 @@ enum nerrors { extern void error(unsigned nerror, ...); extern void genaddable(Node *list[]); extern void cgen(Symbol *sym, Node *list[]); -extern void genstack(Symbol *fun);- \ No newline at end of file +extern void genstack(Symbol *fun); diff --git a/cc2/cgen.c b/cc2/cgen.c @@ -12,11 +12,16 @@ void genstack(Symbol *fun) { Symbol *p; - short siz; + short size; - for (siz = 0, p = fun->u.f.vars; p; p = p->next) - siz += p->u.v.type->size; - fun->u.f.stack = siz; + for (size = 0, p = fun->u.f.vars; p; p = p->next) { + if (p->u.v.sclass == AUTO) { + p->u.v.off = size; + size += p->u.v.type->size; + } + } + + fun->u.f.stack = size; } enum { diff --git a/cc2/parser.c b/cc2/parser.c @@ -273,7 +273,7 @@ declaration(char *token) break; } sym->type = VAR; - sym->u.v.storage = class; + sym->u.v.sclass = class; sym->u.v.type = gettype(strtok(NULL, "\t")); }