[hackers] [scc] [cc2-qbe] Fix generated qbe for ternary expressions || Michael Forney

From: <git_AT_suckless.org>
Date: Fri, 17 Feb 2017 21:58:11 +0100 (CET)

commit 123d9473c025b4846e8f3b215bcc42606290c77e
Author: Michael Forney <mforney_AT_mforney.org>
AuthorDate: Fri Feb 17 11:08:26 2017 -0800
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Fri Feb 17 21:51:50 2017 +0100

    [cc2-qbe] Fix generated qbe for ternary expressions

diff --git a/cc2/arch/qbe/arch.h b/cc2/arch/qbe/arch.h
index ff2167c..1f20649 100644
--- a/cc2/arch/qbe/arch.h
+++ b/cc2/arch/qbe/arch.h
_AT_@ -141,7 +141,13 @@ enum asmop {
         ASALLOC,
         ASFORM,
 
+ ASCOPYB,
+ ASCOPYH,
         ASCOPYW,
+ ASCOPYL,
+ ASCOPYS,
+ ASCOPYD,
+
         ASVSTAR,
         ASVARG,
 };
diff --git a/cc2/arch/qbe/cgen.c b/cc2/arch/qbe/cgen.c
index 4bfa45d..c30ae8d 100644
--- a/cc2/arch/qbe/cgen.c
+++ b/cc2/arch/qbe/cgen.c
_AT_@ -280,6 +280,32 @@ assign(Type *tp, Node *to, Node *from)
         return from;
 }
 
+static Node *
+copy(Type *tp, Node *to, Node *from)
+{
+ int op;
+
+ switch (tp->size) {
+ case 1:
+ op = ASCOPYB;
+ break;
+ case 2:
+ op = ASCOPYH;
+ break;
+ case 4:
+ op = (tp->flags & FLOATF) ? ASCOPYS : ASCOPYW;
+ break;
+ case 8:
+ op = (tp->flags & FLOATF) ? ASCOPYD : ASCOPYL;
+ break;
+ default:
+ /* TODO: Need to handle the general case */
+ abort();
+ }
+ code(op, to, from, NULL);
+ return from;
+}
+
 /* TODO: Do field() transformation in sethi */
 
 static Node *
_AT_@ -368,11 +394,11 @@ ternary(Node *np, Node *ret)
         code(ASBRANCH, rhs(np->left, &aux1), &ifyes, &ifno);
 
         setlabel(ifyes.u.sym);
- assign(&ret->type, ret, rhs(colon->left, &aux2));
+ copy(&ret->type, ret, rhs(colon->left, &aux2));
         code(ASJMP, NULL, &phi, NULL);
 
         setlabel(ifno.u.sym);
- assign(&ret->type, ret, rhs(colon->right, &aux3));
+ copy(&ret->type, ret, rhs(colon->right, &aux3));
         setlabel(phi.u.sym);
 
         return ret;
diff --git a/cc2/arch/qbe/code.c b/cc2/arch/qbe/code.c
index d2cd66b..e92e98f 100644
--- a/cc2/arch/qbe/code.c
+++ b/cc2/arch/qbe/code.c
_AT_@ -32,7 +32,12 @@ static struct opdata {
         [ASLDS] = {.fun = unary, .txt = "loads", .letter = 's'},
         [ASLDD] = {.fun = unary, .txt = "loadd", .letter = 'd'},
 
+ [ASCOPYB] = {.fun = unary, .txt = "copy", .letter = 'b'},
+ [ASCOPYH] = {.fun = unary, .txt = "copy", .letter = 'h'},
         [ASCOPYW] = {.fun = unary, .txt = "copy", .letter = 'w'},
+ [ASCOPYL] = {.fun = unary, .txt = "copy", .letter = 'l'},
+ [ASCOPYS] = {.fun = unary, .txt = "copy", .letter = 's'},
+ [ASCOPYD] = {.fun = unary, .txt = "copy", .letter = 'd'},
 
         [ASSTB] = {.fun = store, .txt = "store", .letter = 'b'},
         [ASSTH] = {.fun = store, .txt = "store", .letter = 'h'},
diff --git a/tests/execute/0112-cond.c b/tests/execute/0112-cond.c
new file mode 100644
index 0000000..77c1b04
--- /dev/null
+++ b/tests/execute/0112-cond.c
_AT_@ -0,0 +1,11 @@
+int
+main()
+{
+ int x = 0;
+ int y = 1;
+ if(x ? 1 : 0)
+ return 1;
+ if(y ? 0 : 1)
+ return 2;
+ return 0;
+}
diff --git a/tests/execute/scc-tests.lst b/tests/execute/scc-tests.lst
index a73bd14..974bd4f 100644
--- a/tests/execute/scc-tests.lst
+++ b/tests/execute/scc-tests.lst
_AT_@ -102,3 +102,4 @@
 0109-struct.c
 0110-typedefcast.c
 0111-doubledef.c
+0112-cond.c
Received on Fri Feb 17 2017 - 21:58:11 CET

This archive was generated by hypermail 2.3.0 : Fri Feb 17 2017 - 22:00:21 CET