shithub: scc

Download patch

ref: b68bbbe579e3180a890a0b676f5b08a533a450ae
parent: 3ef4900759c298520e75dd89019740350973b665
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Aug 12 19:01:07 EDT 2015

Implement function calls

Ok. We have now a good subset of C language implemented. We can
begin with the fun.

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -272,6 +272,8 @@
 	OELOOP,
 	OBLOOP,
 	OFUN,
+	OPAR,
+	OCALL,
 	ORET,
 	ODECL,
 	OSWITCH,
--- a/cc1/code.c
+++ b/cc1/code.c
@@ -60,7 +60,9 @@
 	[OBRANCH] = "\tj\tL%d",
 	[OEFUN] = "}",
 	[OELOOP] = "\tb",
-	[OBLOOP] = "\td"
+	[OBLOOP] = "\td",
+	[OPAR] = "p",
+	[OCALL] = "c"
 };
 
 void (*opcode[])(unsigned, void *) = {
@@ -117,7 +119,9 @@
 	[OFUN] = emitfun,
 	[ORET] = emitret,
 	[ODECL] = emitdcl,
-	[OSWITCH] = emitswitch
+	[OSWITCH] = emitswitch,
+	[OPAR] = emitbin,
+	[OCALL] = emitbin
 };
 
 void
--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -746,8 +746,9 @@
 static Node *
 arguments(Node *np)
 {
-	Node *par;
-	Type *tp = np->type;
+	int n;
+	Node *par = NULL, *arg;
+	Type **targs, *tp = np->type;
 
 	if (tp->op == PTR && tp->type->op == FTN) {
 		np = content(OPTR, np);
@@ -755,19 +756,25 @@
 	}
 	if (tp->op != FTN)
 		error("function or function pointer expected");
-	/* TODO: implement function calls */
-	abort();
+	targs = tp->pars;
+
 	expect('(');
-	if (accept(')'))
-		return np;
 
-	do {
-		if ((par = eval(assign())) == NULL)
-			unexpected();
-	} while (accept(','));
+	if ((n = tp->n.elem) > 0) {
+		do {
+			if ((arg = eval(assign())) == NULL)
+				unexpected();
+			if ((arg = convert(arg, *targs++, 0)) == NULL)
+				error("bad type");
+			par = node(OPAR, arg->type, par, arg);
+		} while (--n && accept(','));
+	}
 
+	if (n > 0)
+		error("insuficient number of parameters...");
+
 	expect(')');
-	return np;
+	return node(OCALL, np->type->type, np, par);
 }
 
 static Node *