shithub: scc

Download patch

ref: 483b5ac034a0e696ef04927d1766db58852493c6
parent: ee0956cca3f5a2e8ea346336086e37296b3767d9
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sun Oct 31 02:00:41 EDT 2021

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.

--- a/src/cmd/cc/cc1/fold.c
+++ b/src/cmd/cc/cc1/fold.c
@@ -422,7 +422,6 @@
 	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 @@
 		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))
@@ -671,6 +671,8 @@
 	case ONEG:
 		assert(!r);
 		if ((p = foldunary(np, l)) != NULL)
+			return p;
+		if ((p = fold(np)) != NULL)
 			return p;
 		return np;
 	default: