Re: [hackers] [PATCH] [scc] [cc2-qbe] Print function parameter types and names

From: <k0ga_AT_shike2.com>
Date: Thu, 14 Apr 2016 22:02:06 +0200

Thanks, I will apply it tomorrow with small changes:


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

Yes, I am doing big modifications to scc-qbe now, and I hope I will
push tomorrow some changes I am doing and that will be the base for
the code generation. After that, I will be able to test, and I it
will be stable again. Sorry for the inconvenience.



>
> +extern Symbol *locals;

I will add it to cc2.h. I added a section of globals in cc2.h, and I think
locals is an important part of the interface that the arch codes can use.


> _AT_@ -103,34 +105,29 @@ 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);
> +
> + return NULL;
> }
>

Perfect. I only think that the return NULL can be removed, because
abort will not return ever (well, technically talking, abort can return,
but it will not happen ever in cc2).


> _AT_@ -160,7 +158,8 @@ void
> data(Node *np)
> {
> putchar('\t');
> - size2asm(&np->type);
> + fputs(size2asm(&np->type), stdout);
> + putchar('\t');
> emittree(np);
> putchar(',');
> putchar('\n');

What about: "printf("\t%s\t", size2asm(&np->type))" ?

> _AT_@ -169,9 +168,19 @@ data(Node *np)
> void
> writeout(void)
> {
> + Symbol *sym = locals;
> +
> if (curfun->kind == GLOB)
> fputs("export ", stdout);
> - printf("function %s(", symname(curfun));
> + printf("function $%s(", symname(curfun));
> +
> + if (sym && (sym->type.flags & PARF)) {
> + printf("%s %s", size2asm(&sym->type), symname(sym));
> + sym = sym->next;
> + for (; sym && sym->type.flags & PARF; sym = sym->next)
> + printf(", %s %s", size2asm(&sym->type), symname(sym));
> + }
> +
> puts("){");
> puts("}");
> }

I imagine you wtite this code in this form because you don't want to
write a trailing ',' in the last parameter, but this is not a problem
because qbe accepts it. We can write in this form:

        for (sym = locals; sym; sym = sym->next) {
                if ((sym->flags & PARF) == 0)
                        break;
                printf("%s %s,", size2asm(&sym->type), symname(sym));
        }

Or, why not, something like:

        for (sym = locals; sym; sym = sym->next) {
                if ((sym->flags & PARF) == 0)
                        break;
                printf("%s %s%c\n",
                       size2asm(&sym->type), symname(sym),
                       (sym->next) ? ',' : ')');
        }
        
Regards,
Received on Thu Apr 14 2016 - 22:02:06 CEST

This archive was generated by hypermail 2.3.0 : Thu Apr 14 2016 - 22:12:19 CEST