shithub: scc

Download patch

ref: a8217de16179e42b083475bd3a4d02d5ba58bdfc
parent: d053b0f8fb80ef6066f26b330f9a94991cf9abe6
author: Pekka Jylhä-Ollila <[email protected]>
date: Thu Apr 14 11:15:14 EDT 2016

[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){
}

--- a/cc2/arch/qbe/code.c
+++ b/cc2/arch/qbe/code.c
@@ -103,34 +103,25 @@
 	}
 }
 
-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 @@
 void
 defpar(Symbol *sym)
 {
+	sym->type.flags |= PARF;
 }
 
 void
@@ -159,8 +151,7 @@
 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 @@
 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("}");
 }
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -5,6 +5,7 @@
 	STRF    =    8,
 	UNIONF  =    16,
 	FUNF    =    32,
+	PARF    =    64,
 	INITF   =   128
 };
 
@@ -211,3 +212,6 @@
 extern void popctx(void);
 extern void pushctx(void);
 extern void freesym(Symbol *sym);
+
+/* globals */
+extern Symbol *locals;
--- 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;