[hackers] [scc] [cc2-qbe] Add first version of rhs() and lhs() || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Thu, 7 Jul 2016 16:05:29 +0200 (CEST)

commit 9abe992c8b535e5c2f5cb574016d7dcdced33870
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Thu Jul 7 16:04:29 2016 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Thu Jul 7 16:04:29 2016 +0200

    [cc2-qbe] Add first version of rhs() and lhs()
    
    This is not a working version yet, but it has the minimal
    to be considered an improvement.

diff --git a/cc2/arch/qbe/cgen.c b/cc2/arch/qbe/cgen.c
index bcd51ca..e70ee2b 100644
--- a/cc2/arch/qbe/cgen.c
+++ b/cc2/arch/qbe/cgen.c
_AT_@ -6,24 +6,164 @@
 #include "../../cc2.h"
 #include "../../../inc/sizes.h"
 
-Node
-rhs(Node *np)
+enum sflags {
+ ISTMP = 1,
+ ISCONS = 2
+};
+
+static Node *
+tmpnode(Node *np)
 {
+ Symbol *sym;
+
+ sym = getsym(TMPSYM);
+ sym->type = np->type;
+ sym->kind = STMP;
+ np->u.sym = sym;
+ np->op = OTMP;
+ np->flags |= ISTMP;
+ return np;
 }
 
-Node
-lhs(Node *np)
+static Node *
+load(Node *np, Node *new)
 {
+ int op;
+ Type *tp;
+
+ tp = &np->type;
+ new->type = *tp;
+ tmpnode(new);
+
+ switch (tp->size) {
+ case 1:
+ op = ASLDB;
+ break;
+ case 2:
+ op = ASLDH;
+ break;
+ case 4:
+ op = (tp->flags & INTF) ? ASLDW : ASLDS;
+ break;
+ case 8:
+ op = (tp->flags & INTF) ? ASLDL : ASLDD;
+ break;
+ default:
+ abort();
+ }
+ code(op, new, np, NULL);
+ return new;
 }
 
-Node
-expr(Node *np)
+static Node *
+assign(Node *np, Node *new)
 {
+ Node *to, *from;
+ Type *tp;
+ int op;
+
+ to = np->left;
+ from = np->right;
+
+ tp = &to->type;
+ switch (tp->size) {
+ case 1:
+ op = ASSTB;
+ break;
+ case 2:
+ op = ASSTH;
+ break;
+ case 4:
+ op = (tp->flags & INTF) ? ASSTW : ASSTS;
+ break;
+ case 8:
+ op = (tp->flags & INTF) ? ASSTL : ASSTD;
+ break;
+ default:
+ abort();
+ }
+ code(op, to, from, NULL);
+ return from;
+}
+
+static Node *rhs(Node *np, Node *new);
+
+static Node *
+lhs(Node *np, Node *new)
+{
+ switch (np->op) {
+ case OMEM:
+ case OAUTO:
+ return np;
+ case OPTR:
+ return rhs(np->left, new);
+ default:
+ abort();
+ }
+}
+
+static Node *
+bool(Node *np, Node *new, Symbol *true, Symbol *false)
+{
+ Node *l = np->left, *r = np->right;
+ Symbol *label;
+
+ switch (np->op) {
+ case OAND:
+ label = newlabel();
+ bool(l, new, label, true);
+ setlabel(label);
+ bool(r, new, true, false);
+ break;
+ case OOR:
+ label = newlabel();
+ bool(l, new, true, label);
+ setlabel(label);
+ bool(r, new, true, false);
+ break;
+ default:
+ rhs(np, new);
+ code(ASBRANCH, new, label2node(true), label2node(false));
+ break;
+ }
+ return new;
+}
+
+static Node *
+rhs(Node *np, Node *new)
+{
+ Node aux;
+ Symbol *label1, *label2;
+
+ switch (np->op) {
+ case OMEM:
+ case OAUTO:
+ return load(np, new);
+ case OJMP:
+ case OBRANCH:
+ case ORET:
+ label1 = newlabel();
+ label2 = newlabel();
+ bool(np, new, label1, label2);
+ setlabel(label1);
+ return NULL;
+ case OASSIG:
+ lhs(np->left, new);
+ rhs(np->right, &aux);
+ assign(&aux, new);
+ return new;
+ default:
+ abort();
+ }
 }
 
 Node *
 cgen(Node *np)
 {
+ Node n;
+
+ rhs(np, &n);
+ return NULL;
 }
 
 /*
Received on Thu Jul 07 2016 - 16:05:29 CEST

This archive was generated by hypermail 2.3.0 : Thu Jul 07 2016 - 16:12:13 CEST