scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit e339e01209952473af23cc6fd8ab1519cb930a3c
parent ffb83304fd6f90cfa5c6f476ad6d192f3887357a
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu,  4 Nov 2021 14:51:16 +0100

cc2/qbe: Use sethi numbers in all of assign ops

Before this commit only normal and abbreviation assign operators
were using the Sethi complexity numbers to drive the code
generation. This commit unifies all the operators and apply the
Sethi numbers to all the operators.

Diffstat:
Msrc/cmd/cc/cc2/target/qbe/cgen.c | 127++++++++++++++++++++++++++++++++++++++++---------------------------------------
1 file changed, 64 insertions(+), 63 deletions(-)

diff --git a/src/cmd/cc/cc2/target/qbe/cgen.c b/src/cmd/cc/cc2/target/qbe/cgen.c @@ -235,32 +235,6 @@ call(Node *np, Node *fun) } static Node * -assign(Type *tp, Node *to, Node *from) -{ - int op; - - switch (tp->size) { - case 1: - op = ASSTB; - break; - case 2: - op = ASSTH; - break; - case 4: - op = (tp->flags & FLOATF) ? ASSTS : ASSTW; - break; - case 8: - op = (tp->flags & FLOATF) ? ASSTD : ASSTL; - break; - default: - op = ASSTM; - break; - } - code(op, to, from, NULL); - return from; -} - -static Node * copy(Type *tp, Node *to, Node *from) { int op; @@ -450,6 +424,69 @@ swtch_if(Node *idx) } } +static int +assignop(Type *tp) +{ + switch (tp->size) { + case 1: + return ASSTB; + case 2: + return ASSTH; + case 4: + return (tp->flags & FLOATF) ? ASSTS : ASSTW; + case 8: + return (tp->flags & FLOATF) ? ASSTD : ASSTL; + default: + return ASSTM; + } +} + +static Node * +assign(Node *np) +{ + Node *tmp, aux; + Node *l = np->left, *r = np->right; + int op; + + switch (np->u.subop) { + case OINC: + op = OADD; + goto post_oper; + case ODEC: + op = OSUB; + post_oper: + tmp = rhs(l); + aux.op = op; + aux.left = tmp; + aux.right = r; + aux.type = np->type; + r = complex(&aux); + break; + default: + /* assign abbreviation */ + aux.type = np->type; + aux.op = np->u.subop; + aux.right = np->right; + aux.left = np->left; + r = rhs(complex(&aux)); + case 0: + op = 0; + break; + } + + if (l->complex >= r->complex) { + l = lhs(l); + r = rhs(r); + } else { + r = rhs(r); + l = lhs(l); + } + + code(assignop(&np->type), l, r, NULL); + + return (op == 0) ? r : tmp; +} + static Node * rhs(Node *np) { @@ -552,43 +589,7 @@ rhs(Node *np) case OCAST: return cast(tp, rhs(l)); case OASSIG: - switch (np->u.subop) { - case OINC: - op = OADD; - goto post_oper; - case ODEC: - op = OSUB; - post_oper: - tmp = rhs(l); - - aux1.op = op; - aux1.left = tmp; - aux1.right = r; - aux1.type = np->type; - r = &aux1; - - r = rhs(r); - l = lhs(l); - assign(tp, l, r); - - return tmp; - default: - aux2.type = np->type; - aux2.op = np->u.subop; - aux2.right = np->right; - aux2.left = np->left; - r = rhs(&aux2); - case 0: - if (l->complex >= r->complex) { - l = lhs(l); - r = rhs(r); - } else { - r = rhs(r); - l = lhs(l); - } - - return assign(tp, l, r); - } + return assign(np); case OASK: return ternary(np); case OCOMMA: