scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit a914d8f2874b128360789b7c2e2511b0aa078978
parent 0480870c8d4e3c273758fc36f149d9634ea516b3
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 22 Apr 2016 13:32:34 +0200

[cc2-qbe] Add allocation of parameters

Qbe needs explicitely allocate the stack space for the parameters,
which are considered only the values passed as parameters, not
the local variables that parameter are to.

Diffstat:
Mcc2/arch/qbe/code.c | 36+++++++++++++++++++++++++++++-------
1 file changed, 29 insertions(+), 7 deletions(-)

diff --git a/cc2/arch/qbe/code.c b/cc2/arch/qbe/code.c @@ -246,31 +246,53 @@ data(Node *np) putchar('\n'); } +static void +alloc(Symbol *sym) +{ + Type *tp = &sym->type; + + printf("\t%s %s=\talloc%lld\t%lld\n", + symname(sym), size2asm(tp), + (long long) tp->size, (long long) tp->align); +} + void writeout(void) { Symbol *p; Type *tp; - char *sep; + char *sep, *name; if (curfun->kind == SGLOB) fputs("export ", stdout); printf("function %s %s(", size2asm(&curfun->rtype), symname(curfun)); + /* declare formal parameters */ for (sep = "", p = locals; p; p = p->next, sep = ",") { if ((p->type.flags & PARF) == 0) break; - printf("%s%s %s", sep, size2asm(&p->type), symname(p)); + printf("%s%s %s.val", sep, size2asm(&p->type), symname(p)); } - puts(")"); + puts(")\n{"); + + /* allocate stack space for parameters */ + for (p = locals; p && (p->type.flags & PARF) == 0; p = p->next) + alloc(p); - for ( ; p && p->id != TMPSYM; p = p->next) { + /* allocate stack space for local variables) */ + for ( ; p && p->id != TMPSYM; p = p->next) + alloc(p); + + /* store formal parameters in parameters */ + for (p = locals; p; p = p->next) { tp = &p->type; - printf("\t%s %s=\talloc%lld\t%lld\n", - symname(p), size2asm(tp), - (long long) tp->size, (long long) tp->align); + if ((tp->flags & PARF) == 0) + break; + name = symname(p); + printf("\t\tstore%s\t%s.val,%s\n", size2asm(tp), name, name); } + /* emit assembler instructions */ for (pc = prog; pc; pc = pc->next) { if (pc->label) printf("%s:\n", symname(pc->label));