scc

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

commit 6e1706f9eb1bc7e9fd6669ed256da1233d44c554
parent 5a9c8fe5457c845a087e7c1e18f44ed5d3961095
Author: Pekka Jylhä-Ollila <pekka.jylha.ollila@gmail.com>
Date:   Fri, 15 Apr 2016 13:54:42 +0300

[cc1 - cc2] Output function return type in cc1 and cc2

Print function return type in cc1 and cc2.
In cc1 the return type is printed before the symbol type 'F', so that in cc2
we can first pop 'F' from the stack and see if we need to pop the return type.

The return type needs to be stored somewhere in cc2, so I added it
to Symbol as Symbol.rtype.

Diffstat:
cc1/code.c | 5+++++
cc2/arch/qbe/code.c | 2+-
cc2/cc2.h | 1+
cc2/parser.c | 6+++++-
4 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/cc1/code.c b/cc1/code.c @@ -226,6 +226,11 @@ emitsym(unsigned op, void *arg) static void emitletter(Type *tp) { + if (tp->op == FTN) { + emitletter(tp->type); + putchar('\t'); + } + putchar(tp->letter); switch (tp->op) { case ARY: diff --git a/cc2/arch/qbe/code.c b/cc2/arch/qbe/code.c @@ -167,7 +167,7 @@ writeout(void) if (curfun->kind == GLOB) fputs("export ", stdout); - printf("function w %s(", symname(curfun)); + printf("function %s $%s(", size2asm(&curfun->rtype), symname(curfun)); for (p = locals; p && p->type.flags & PARF; p = p->next) printf("%s %s,", size2asm(&p->type), symname(p)); diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -131,6 +131,7 @@ struct type { struct symbol { Type type; + Type rtype; unsigned short id; unsigned short numid; char *name; diff --git a/cc2/parser.c b/cc2/parser.c @@ -524,13 +524,15 @@ decl(Symbol *sym) static void vardecl(void) { - Type *tp; + Type *tp, *rp; Node *np; Symbol *sym; char *name; name = pop(); tp = pop(); + if (tp->flags & FUNF) + rp = pop(); np = pop(); sym = np->u.sym; @@ -542,6 +544,8 @@ vardecl(void) free(sym->name); sym->name = name; sym->type = *tp; + if (tp->flags & FUNF) + sym->rtype = *rp; sym->kind = sclass; if (ininit)