scc

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

commit 11111b2afe6b8510fdd8cf21a6058f7a60820117
parent e380352af6d775f0f9aefe3623658ad72ce80c1a
Author: Pekka Jylhä-Ollila <pekka.jylha.ollila@gmail.com>
Date:   Thu, 14 Apr 2016 15:15:14 +0300

[cc2-qbe] Print function parameter types and names

Print out function parameter names and types in the qbe writeout function.
I changed the 'locals' variable to global so it could be referenced,
and the size2asm function to return a pointer instead of printing.

I had to disable the calls to apply(sethi) and apply(cgen) in cc2/main.c
while testing because they were causing segfaults.

Example output:

$ cat test.c
int test(char a, int b, long c)
{
        return a * b * c;
}

int main(int argc, char **argv)
{
        int a = 1;
        int b = 2;
        int c = 3;

        return test(a, b, c);
}
$ ./cc1/cc1 test.c | ./cc2/cc2
export function $test(b %.1, w %.2, l %.3){
}
export function $main(w %.4, l %.5){
}

Diffstat:
cc2/arch/qbe/code.c | 31++++++++++++++-----------------
cc2/cc2.h | 4++++
cc2/symbol.c | 3++-
3 files changed, 20 insertions(+), 18 deletions(-)

diff --git a/cc2/arch/qbe/code.c b/cc2/arch/qbe/code.c @@ -103,34 +103,25 @@ emittree(Node *np) } } -static void +static char * size2asm(Type *tp) { - char *s; - - /* In qbe we can ignore the aligment because it handles it */ - if (tp->flags & STRF) { - s = "b\t"; + return "b"; } else { switch (tp->size) { case 1: - s = "b\t"; - break; + return "b"; case 2: - s = "h\t"; - break; + return "h"; case 4: - s = "w\t"; - break; + return "w"; case 8: - s = "l\t"; - break; + return "l"; default: abort(); } } - fputs(s, stdout); } void @@ -149,6 +140,7 @@ defglobal(Symbol *sym) void defpar(Symbol *sym) { + sym->type.flags |= PARF; } void @@ -159,8 +151,7 @@ defvar(Symbol *sym) void data(Node *np) { - putchar('\t'); - size2asm(&np->type); + printf("\t%s\t", size2asm(&np->type)); emittree(np); putchar(','); putchar('\n'); @@ -169,9 +160,15 @@ data(Node *np) void writeout(void) { + Symbol *p; + if (curfun->kind == GLOB) fputs("export ", stdout); printf("function %s(", symname(curfun)); + + for (p = locals; p && p->type.flags & PARF; p = p->next) + printf("%s %s,", size2asm(&p->type), symname(p)); + puts("){"); puts("}"); } diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -5,6 +5,7 @@ enum tflags { STRF = 8, UNIONF = 16, FUNF = 32, + PARF = 64, INITF = 128 }; @@ -211,3 +212,6 @@ extern Symbol *getsym(unsigned id); extern void popctx(void); extern void pushctx(void); extern void freesym(Symbol *sym); + +/* globals */ +extern Symbol *locals; diff --git a/cc2/symbol.c b/cc2/symbol.c @@ -11,8 +11,9 @@ #define NR_SYMHASH 64 +Symbol *locals; + static Symbol *symtab[NR_SYMHASH], *curlocal; -static Symbol *locals; static int infunction;