shithub: scc

Download patch

ref: 4f3e4465ccb3e7c611c7996793abefe399787983
parent: f95f5641563eef4c97d091606fff9707dee90b1a
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Jul 23 11:31:05 EDT 2015

Simplify before of creating nodes

It is stupid create the node and later simplify it, and
it is also stupid call simplify() in every place where
integerop(), logic() or arithmetic() are called instead
of putting simplify() inside of them.

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -326,7 +326,7 @@
 extern Node *constnode(Symbol *sym);
 extern Node *sizeofnode(Type *tp);
 extern void freetree(Node *np);
-extern Node *simplify(Node *np);
+extern Node *simplify(unsigned char, Type *tp, Node *lp, Node *rp);
 
 /* expr.c */
 extern Node *expr(void), *negate(Node *np), *constexpr(void);
--- a/cc1/code.c
+++ b/cc1/code.c
@@ -392,20 +392,19 @@
 	((sym)->u.u = ((ls)->u.u op (rs)->u.u)))
 
 Node *
-simplify(Node *np)
+simplify(unsigned char op, Type *tp, Node *lp, Node *rp)
 {
-	Node *lp = np->left, *rp = np->right;
-	Symbol *sym, *ls = lp->sym, *rs = rp->sym;
-	Type *tp = np->type;
+	Symbol *sym, *ls, *rs;
 
 	if (!lp->constant || !rp->constant)
-		return np;
+		goto no_simplify;
+	ls = lp->sym, rs = rp->sym;
 
 	switch (tp->op) {
 	case INT:
 		sym = newsym(NS_IDEN);
 		sym->type = tp;
-		switch (np->op) {
+		switch (op) {
 		case OADD:
 			FOLDINT(sym, ls, rs, +);
 			break;
@@ -468,14 +467,17 @@
 			abort();
 		}
 		break;
+	case FLOAT:
+		/* TODO: Add simplification of floats */
 	default:
-		return np;
+		goto no_simplify;
 	}
 
-	freetree(np);
 	return constnode(sym);
 
 division_by_0:
 	warn("division by 0");
-	return np;
+
+no_simplify:
+	return node(op, tp, lp, rp);
 }
--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -78,7 +78,7 @@
 	if (BTYPE(lp) != INT || BTYPE(rp) != INT)
 		error("operator requires integer operands");
 	typeconv(&lp, &rp);
-	return node(op, lp->type, lp, rp);
+	return simplify(op, lp->type, lp, rp);
 }
 
 static Node *
@@ -222,7 +222,7 @@
 		error("incorrect arithmetic operands");
 	}
 
-	return node(op, lp->type, lp, rp);
+	return simplify(op, lp->type, lp, rp);
 }
 
 static Node *
@@ -274,7 +274,7 @@
 		error("incompatibles type in comparision");
 	}
 
-	return node(op, inttype, lp, rp);
+	return simplify(op, inttype, lp, rp);
 }
 
 Node *
@@ -327,7 +327,7 @@
 {
 	lp = exp2cond(lp, 0);
 	rp = exp2cond(rp, 0);
-	return node(op, inttype, lp, rp);
+	return simplify(op, inttype, lp, rp);
 }
 
 static Node *
@@ -667,7 +667,6 @@
 		}
 		next();
 		np = (*fun)(op, np, cast());
-		np = simplify(np);
 	}
 }
 
@@ -686,7 +685,6 @@
 		}
 		next();
 		np = arithmetic(op, np, mul());
-		np = simplify(np);
 	}
 }
 
@@ -705,7 +703,6 @@
 		}
 		next();
 		np = integerop(op, np, add());
-		np = simplify(np);
 	}
 }
 
@@ -726,7 +723,6 @@
 		}
 		next();
 		np = compare(op, np, shift());
-		np = simplify(np);
 	}
 }
 
@@ -745,7 +741,6 @@
 		}
 		next();
 		np = compare(op, np, relational());
-		np = simplify(np);
 	}
 }
 
@@ -757,7 +752,7 @@
 	np = eq();
 	while (accept('&'))
 		np = integerop(OBAND, np, eq());
-	return simplify(np);
+	return np;
 }
 
 static Node *
@@ -768,7 +763,7 @@
 	np = bit_and();
 	while (accept('^'))
 		np = integerop(OBXOR,  np, bit_and());
-	return simplify(np);
+	return np;
 }
 
 static Node *
@@ -779,7 +774,7 @@
 	np = bit_xor();
 	while (accept('|'))
 		np = integerop(OBOR, np, bit_xor());
-	return simplify(np);
+	return np;
 }
 
 static Node *
@@ -790,7 +785,7 @@
 	np = bit_or();
 	while (accept(AND))
 		np = logic(OAND, np, bit_or());
-	return simplify(np);
+	return np;
 }
 
 static Node *
@@ -801,7 +796,7 @@
 	np = and();
 	while (accept(OR))
 		np = logic(OOR, np, and());
-	return simplify(np);
+	return np;
 }
 
 static Node *