shithub: scc

Download patch

ref: 7f4ad68df834c5d81893b164acc97dd03441fc01
parent: 40b00c1de02895be74d04bf1dd415920dc2256c4
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Jun 2 05:01:39 EDT 2016

[cc2-qbe] Add parameters to function calls

This patch adds the parameters to the function calls, although
it is not perfect yet, because it does not pass the type of the
parameter to the call, but it is a big step to have the
implementation of functions in qbe.

--- a/cc2/arch/qbe/arch.h
+++ b/cc2/arch/qbe/arch.h
@@ -139,4 +139,6 @@
 	ASCALLL,
 	ASCALLD,
 	ASCALL,
+	ASPAR,
+	ASPARE,
 };
--- a/cc2/arch/qbe/cgen.c
+++ b/cc2/arch/qbe/cgen.c
@@ -237,10 +237,10 @@
 {
 	int n, op;
 	Type *tp = &np->type;
-	Node *tmp, *p, *pars[NR_FUNPARAM];
+	Node **q, *tmp, *p, *pars[NR_FUNPARAM];
 
 	for (n = 0, p = np->right; p; p = p->right)
-		pars[n] = cgen(p->left);
+		pars[n++] = cgen(p->left);
 
 	switch (tp->size) {
 	case 0:
@@ -263,6 +263,11 @@
 		abort();
 	}
 	code(op, tmpnode(np), np->left, NULL);
+
+	for (q = pars; q < &pars[n]; ++q) {
+		op = (q == &pars[n-1]) ? ASPARE : ASPAR;
+		code(op, NULL, *q, NULL);
+	}
 	code(ASCALL, NULL, NULL, NULL);
 
 	return np;
--- a/cc2/arch/qbe/code.c
+++ b/cc2/arch/qbe/code.c
@@ -10,7 +10,7 @@
 #define ADDR_LEN (INTIDENTSIZ+64)
 
 static void binary(void), unary(void), store(void), jmp(void), ret(void),
-            branch(void), call(void), ecall(void);
+            branch(void), call(void), ecall(void), param(void);
 
 static struct opdata {
 	void (*fun)(void);
@@ -133,6 +133,8 @@
 	[ASCALLL] = {.fun = call, .letter = 'l'},
 	[ASCALLD] = {.fun = call, .letter = 'd'},
 	[ASCALL] = {.fun = ecall},
+	[ASPAR] = {.fun = param, .txt = "\t\t%s,\n"},
+	[ASPARE] = {.fun = param, .txt = "\t\t%s\n"},
 };
 
 static char buff[ADDR_LEN];
@@ -413,13 +415,19 @@
 
        strcpy(to, addr2txt(&pc->to));
        strcpy(from, addr2txt(&pc->from1));
-       printf("\t%s =%c\tcall\t%s(", to, p->letter, from);
+       printf("\t%s =%c\tcall\t%s(\n", to, p->letter, from);
 }
 
 static void
+param(void)
+{
+	printf(optbl[pc->op].txt, addr2txt(&pc->from1));
+}
+
+static void
 ecall(void)
 {
-	puts(")");
+	puts("\t\t)");
 }
 
 static void