commit b68bbbe579e3180a890a0b676f5b08a533a450ae
parent 3ef4900759c298520e75dd89019740350973b665
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Wed, 12 Aug 2015 23:01:07 +0200
Implement function calls
Ok. We have now a good subset of C language implemented. We can
begin with the fun.
Diffstat:
3 files changed, 26 insertions(+), 13 deletions(-)
diff --git a/cc1/cc1.h b/cc1/cc1.h
@@ -272,6 +272,8 @@ enum {
OELOOP,
OBLOOP,
OFUN,
+ OPAR,
+ OCALL,
ORET,
ODECL,
OSWITCH,
diff --git a/cc1/code.c b/cc1/code.c
@@ -60,7 +60,9 @@ char *optxt[] = {
[OBRANCH] = "\tj\tL%d",
[OEFUN] = "}",
[OELOOP] = "\tb",
- [OBLOOP] = "\td"
+ [OBLOOP] = "\td",
+ [OPAR] = "p",
+ [OCALL] = "c"
};
void (*opcode[])(unsigned, void *) = {
@@ -117,7 +119,9 @@ void (*opcode[])(unsigned, void *) = {
[OFUN] = emitfun,
[ORET] = emitret,
[ODECL] = emitdcl,
- [OSWITCH] = emitswitch
+ [OSWITCH] = emitswitch,
+ [OPAR] = emitbin,
+ [OCALL] = emitbin
};
void
diff --git a/cc1/expr.c b/cc1/expr.c
@@ -746,8 +746,9 @@ static Node *assign(void);
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 @@ arguments(Node *np)
}
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 *