scc

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

commit 483b5ac034a0e696ef04927d1766db58852493c6
parent ee0956cca3f5a2e8ea346336086e37296b3767d9
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun, 31 Oct 2021 06:00:41 +0100

cc1: Fold unary expressions

Simplify was just calling foldunary() in case of a unary
operator, which tries simple unary simplifications, like
for example double negation. The code was missing callng
fold() to enable constant folding.

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

diff --git a/src/cmd/cc/cc1/fold.c b/src/cmd/cc/cc1/fold.c @@ -422,7 +422,6 @@ fold(Node *np) Node *p, *lp = np->left, *rp = np->right; Type *tp = np->type; - assert(lp && rp); if ((op == ODIV || op == OMOD) && cmpnode(rp, 0)) { warn("division by 0"); return NULL; @@ -442,12 +441,13 @@ fold(Node *np) rs = rp->sym; } - if (!(lp->flags & NCONST) || !lp->sym) + if ((lp->flags & NCONST) == 0 || !lp->sym) return NULL; optype = lp->type; ls = lp->sym; - switch (type = optype->op) { + type = optype->op; + switch (type) { case ENUM: case INT: if (!(optype->prop & TSIGNED)) @@ -672,6 +672,8 @@ simplify(Node *np) assert(!r); if ((p = foldunary(np, l)) != NULL) return p; + if ((p = fold(np)) != NULL) + return p; return np; default: commutative(np, l, r);