[hackers] [scc] [cc2-qbe] Add basic support for calls in qbe || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Tue, 31 May 2016 08:42:00 +0200 (CEST)

commit c9609c601b47b56b6e1843141a121fd207be61f1
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Tue May 31 08:39:44 2016 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Tue May 31 08:41:17 2016 +0200

    [cc2-qbe] Add basic support for calls in qbe
    
    This is a preliminar patch which add supports for calls in qbe,
    but it does not pass the parameters to the function call, so it
    only can be used for functions without parameters. It also
    does not support void functions.

diff --git a/cc2/arch/qbe/arch.h b/cc2/arch/qbe/arch.h
index 3403fff..01545c7 100644
--- a/cc2/arch/qbe/arch.h
+++ b/cc2/arch/qbe/arch.h
_AT_@ -132,4 +132,11 @@ enum asmop {
         ASJMP,
         ASBRANCH,
         ASRET,
+ ASCALLB,
+ ASCALLH,
+ ASCALLW,
+ ASCALLS,
+ ASCALLL,
+ ASCALLD,
+ ASCALL,
 };
diff --git a/cc2/arch/qbe/cgen.c b/cc2/arch/qbe/cgen.c
index 3827e7d..200c22e 100644
--- a/cc2/arch/qbe/cgen.c
+++ b/cc2/arch/qbe/cgen.c
_AT_@ -4,6 +4,7 @@
 
 #include "arch.h"
 #include "../../cc2.h"
+#include "../../../inc/sizes.h"
 
 enum lflags {
         FORCE = 1 << 0,
_AT_@ -232,6 +233,38 @@ cast(Node *nd)
 }
 
 static Node *
+call(Node *np)
+{
+ int n, op;
+ Type *tp = &np->type;
+ Node *tmp, *p, *pars[NR_FUNPARAM];
+
+ for (n = 0, p = np->right; p; p = p->right)
+ pars[n] = cgen(p->left);
+
+ switch (tp->size) {
+ case 1:
+ op = ASCALLB;
+ break;
+ case 2:
+ op = ASCALLH;
+ break;
+ case 4:
+ op = (tp->flags & INTF) ? ASCALLW : ASCALLS;
+ break;
+ case 8:
+ op = (tp->flags & INTF) ? ASCALLL : ASCALLD;
+ break;
+ default:
+ abort();
+ }
+ code(op, tmpnode(np), np->left, NULL);
+ code(ASCALL, NULL, NULL, NULL);
+
+ return np;
+}
+
+static Node *
 abbrev(Node *np)
 {
         Node *tmp;
_AT_@ -259,8 +292,10 @@ cgen(Node *np)
                 return NULL;
 
         setlabel(np->label);
- np->left = cgen(np->left);
- np->right = cgen(np->right);
+ if (np->op != OCALL) {
+ np->left = cgen(np->left);
+ np->right = cgen(np->right);
+ }
         tp = &np->type;
 
         switch (np->op) {
_AT_@ -354,6 +389,7 @@ cgen(Node *np)
         case OCOMMA:
                 return np->right;
         case OCALL:
+ return call(np);
         case OFIELD:
         case OASK:
         case OCOLON:
diff --git a/cc2/arch/qbe/code.c b/cc2/arch/qbe/code.c
index c8fee70..91e78f1 100644
--- a/cc2/arch/qbe/code.c
+++ b/cc2/arch/qbe/code.c
_AT_@ -10,7 +10,7 @@
 #define ADDR_LEN (INTIDENTSIZ+64)
 
 static void binary(void), unary(void), store(void), jmp(void), ret(void),
- branch(void);
+ branch(void), call(void), ecall(void);
 
 static struct opdata {
         void (*fun)(void);
_AT_@ -126,6 +126,13 @@ static struct opdata {
         [ASBRANCH] = {.fun = branch},
         [ASJMP] = {.fun = jmp},
         [ASRET] = {.fun = ret},
+ [ASCALLB] = {.fun = call, .letter = 'b'},
+ [ASCALLH] = {.fun = call, .letter = 'h'},
+ [ASCALLW] = {.fun = call, .letter = 'w'},
+ [ASCALLS] = {.fun = call, .letter = 's'},
+ [ASCALLL] = {.fun = call, .letter = 'l'},
+ [ASCALLD] = {.fun = call, .letter = 'd'},
+ [ASCALL] = {.fun = ecall},
 };
 
 static char buff[ADDR_LEN];
_AT_@ -396,6 +403,23 @@ unary(void)
 }
 
 static void
+call(void)
+{
+ struct opdata *p = &optbl[pc->op];
+ char to[ADDR_LEN], from[ADDR_LEN];
+
+ strcpy(to, addr2txt(&pc->to));
+ strcpy(from, addr2txt(&pc->from1));
+ printf("\t%s =%c\tcall\t%s(", to, p->letter, from);
+}
+
+static void
+ecall(void)
+{
+ puts(")");
+}
+
+static void
 ret(void)
 {
         if (pc->from1.kind == SNONE)
Received on Tue May 31 2016 - 08:42:00 CEST

This archive was generated by hypermail 2.3.0 : Tue May 31 2016 - 08:48:20 CEST