scc

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

commit e6205dfae4d600aa978040fdec8596a4ac495ffb
parent 0d701eb4c740238b1b6ac629f4a57004a251e3ab
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed, 11 Jan 2023 13:12:13 +0100

cc1: Add associative transformations to simplify()

Using the associative property allows to fold consecutive
nodes using the same operator and the same type.

Diffstat:
Msrc/cmd/cc/cc1/fold.c | 26++++++++++++++++++++++++++
1 file changed, 26 insertions(+), 0 deletions(-)

diff --git a/src/cmd/cc/cc1/fold.c b/src/cmd/cc/cc1/fold.c @@ -685,6 +685,31 @@ reduce(Node *np) DBG("FOLD reduce %d->%d", op, np->op); } +static void +associative(Node *np) +{ + Node *l = np->left, *r = np->right; + + switch (np->op) { + case OADD: + case OMUL: + case OBAND: + case OBXOR: + case OBOR: + if (np->op != l->op + || l->right->op != OSYM + || !(l->right->sym->flags&SCONSTANT)) { + return; + } + + DBG("FOLD associative %d", np->op); + np->left = l->left; + l->left = r; + np->right = fold(l); + break; + } +} + /* TODO: fold OCOMMA */ static Node * xxsimplify(Node *np) @@ -728,6 +753,7 @@ repeat: break; default: commutative(np); + associative(np); np = fold(np); np = identity(np); reduce(np);