shithub: scc

Download patch

ref: 32cd89a7925cae16302959273a853fe5048cb3c4
parent: cf3e88bc51dcd186f7857113929720b6cd4f61b7
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu Jul 7 12:26:32 EDT 2016

[cc2-qbe] Add basic function definition support

With this small commit we can begin to do tests, because
we can parse full functions and see what is the code generated.

--- a/cc2/arch/qbe/cgen.c
+++ b/cc2/arch/qbe/cgen.c
@@ -130,6 +130,30 @@
 }
 
 static Node *
+function(void)
+{
+	Symbol *p;
+
+	/* allocate stack space for parameters */
+	for (p = locals; p && (p->type.flags & PARF) != 0; p = p->next)
+		code(ASALLOC, label2node(p), NULL, NULL);
+
+	/* allocate stack space for local variables) */
+	for ( ; p && p->id != TMPSYM; p = p->next) {
+		if (p->kind != SAUTO)
+			continue;
+		code(ASALLOC, label2node(p), NULL, NULL);
+	}
+	/* store formal parameters in parameters */
+	for (p = locals; p; p = p->next) {
+		if ((p->type.flags & PARF) == 0)
+			break;
+		code(ASFORM, label2node(p), NULL, NULL);
+	}
+	return NULL;
+}
+
+static Node *
 rhs(Node *np, Node *new)
 {
 	Node aux;
@@ -136,6 +160,10 @@
 	Symbol *label1, *label2;
 
 	switch (np->op) {
+	case OBFUN:
+		return function();
+	case OEFUN:
+		return NULL;
 	case OMEM:
 	case OAUTO:
 		return load(np, new);