ref: c6c9ed1a15c4682ae04f2f867cad16f0eaae03a7
parent: d952dc074de7640075f05cdca052bd552b9988a4
author: Pekka Jylhä-Ollila <pekka.jylha.ollila@gmail.com>
date: Fri Apr 15 09:54:42 EDT 2016
[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.
--- a/cc1/code.c
+++ b/cc1/code.c
@@ -226,6 +226,11 @@
static void
emitletter(Type *tp)
{
+ if (tp->op == FTN) {
+ emitletter(tp->type);
+ putchar('\t');
+ }
+
putchar(tp->letter);
switch (tp->op) {
case ARY:
--- a/cc2/arch/qbe/code.c
+++ b/cc2/arch/qbe/code.c
@@ -167,7 +167,7 @@
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));
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -131,6 +131,7 @@
struct symbol {
Type type;
+ Type rtype;
unsigned short id;
unsigned short numid;
char *name;
--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -524,7 +524,7 @@
static void
vardecl(void)
{
- Type *tp;
+ Type *tp, *rp;
Node *np;
Symbol *sym;
char *name;
@@ -531,6 +531,8 @@
name = pop();
tp = pop();
+ if (tp->flags & FUNF)
+ rp = pop();
np = pop();
sym = np->u.sym;
@@ -542,6 +544,8 @@
free(sym->name);
sym->name = name;
sym->type = *tp;
+ if (tp->flags & FUNF)
+ sym->rtype = *rp;
sym->kind = sclass;
if (ininit)
--
⑨