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

From: Pekka Jylhä-Ollila <pekka.jylha.ollila_AT_gmail.com>
Date: Thu, 14 Apr 2016 15:15:14 +0300

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

---
 cc2/arch/qbe/code.c | 39 ++++++++++++++++++++++++---------------
 cc2/cc2.h           |  1 +
 cc2/symbol.c        |  3 ++-
 3 files changed, 27 insertions(+), 16 deletions(-)
diff --git a/cc2/arch/qbe/code.c b/cc2/arch/qbe/code.c
index 037b1ab..89823d4 100644
--- a/cc2/arch/qbe/code.c
+++ b/cc2/arch/qbe/code.c
_AT_@ -6,6 +6,8 @@
 #include "../../cc2.h"
 #include "../../../inc/sizes.h"
 
+extern Symbol *locals;
+
 /*
  * : is for user-defined Aggregate Types
  * $ is for globals (represented by a pointer)
_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;
 }
 
 void
_AT_@ -149,6 +146,7 @@ defglobal(Symbol *sym)
 void
 defpar(Symbol *sym)
 {
+	sym->type.flags |= PARF;
 }
 
 void
_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');
_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("}");
 }
diff --git a/cc2/cc2.h b/cc2/cc2.h
index e0de200..310fb6c 100644
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
_AT_@ -5,6 +5,7 @@ enum tflags {
 	STRF    =    8,
 	UNIONF  =    16,
 	FUNF    =    32,
+	PARF    =    64,
 	INITF   =   128
 };
 
diff --git a/cc2/symbol.c b/cc2/symbol.c
index 0a54ae1..a761abf 100644
--- a/cc2/symbol.c
+++ b/cc2/symbol.c
_AT_@ -11,8 +11,9 @@
 
 #define NR_SYMHASH  64
 
+Symbol *locals;
+
 static Symbol *symtab[NR_SYMHASH], *curlocal;
-static Symbol *locals;
 static int infunction;
 
 
-- 
2.1.4
Received on Thu Apr 14 2016 - 14:15:14 CEST

This archive was generated by hypermail 2.3.0 : Thu Apr 14 2016 - 14:24:15 CEST