scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 123d9473c025b4846e8f3b215bcc42606290c77e
parent bcbd44d025bb2e2d00054ea0a1826380964692ac
Author: Michael Forney <mforney@mforney.org>
Date:   Fri, 17 Feb 2017 11:08:26 -0800

[cc2-qbe] Fix generated qbe for ternary expressions

Diffstat:
Mcc2/arch/qbe/arch.h | 6++++++
Mcc2/arch/qbe/cgen.c | 30++++++++++++++++++++++++++++--
Mcc2/arch/qbe/code.c | 5+++++
Atests/execute/0112-cond.c | 11+++++++++++
Mtests/execute/scc-tests.lst | 1+
5 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/cc2/arch/qbe/arch.h b/cc2/arch/qbe/arch.h @@ -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 @@ -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 * @@ -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 @@ -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 @@ -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 @@ -102,3 +102,4 @@ 0109-struct.c 0110-typedefcast.c 0111-doubledef.c +0112-cond.c