[hackers] [scc] Implement function calls || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Wed, 12 Aug 2015 23:02:34 +0200 (CEST)

commit b68bbbe579e3180a890a0b676f5b08a533a450ae
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Wed Aug 12 23:01:07 2015 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Wed Aug 12 23:01:07 2015 +0200

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

diff --git a/cc1/cc1.h b/cc1/cc1.h
index 8bbd87e..62781e6 100644
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
_AT_@ -272,6 +272,8 @@ enum {
         OELOOP,
         OBLOOP,
         OFUN,
+ OPAR,
+ OCALL,
         ORET,
         ODECL,
         OSWITCH,
diff --git a/cc1/code.c b/cc1/code.c
index 78a3517..b8ef034 100644
--- a/cc1/code.c
+++ b/cc1/code.c
_AT_@ -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 *) = {
_AT_@ -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
index 3c29934..86b1b11 100644
--- a/cc1/expr.c
+++ b/cc1/expr.c
_AT_@ -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);
_AT_@ -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 *
Received on Wed Aug 12 2015 - 23:02:34 CEST

This archive was generated by hypermail 2.3.0 : Wed Aug 12 2015 - 23:12:10 CEST