scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit 4f3e4465ccb3e7c611c7996793abefe399787983
parent f95f5641563eef4c97d091606fff9707dee90b1a
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 23 Jul 2015 15:31:05 +0200

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.

Diffstat:
cc1/cc1.h | 2+-
cc1/code.c | 20+++++++++++---------
cc1/expr.c | 23+++++++++--------------
3 files changed, 21 insertions(+), 24 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -326,7 +326,7 @@ extern Node *varnode(Symbol *sym); 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); diff --git a/cc1/code.c b/cc1/code.c @@ -392,20 +392,19 @@ sizeofnode(Type *tp) ((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 @@ simplify(Node *np) 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); } diff --git a/cc1/expr.c b/cc1/expr.c @@ -78,7 +78,7 @@ integerop(char op, Node *lp, Node *rp) 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 @@ arithmetic(char op, Node *lp, Node *rp) error("incorrect arithmetic operands"); } - return node(op, lp->type, lp, rp); + return simplify(op, lp->type, lp, rp); } static Node * @@ -274,7 +274,7 @@ compare(char op, Node *lp, Node *rp) error("incompatibles type in comparision"); } - return node(op, inttype, lp, rp); + return simplify(op, inttype, lp, rp); } Node * @@ -327,7 +327,7 @@ logic(char op, Node *lp, Node *rp) { 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 @@ mul(void) } next(); np = (*fun)(op, np, cast()); - np = simplify(np); } } @@ -686,7 +685,6 @@ add(void) } next(); np = arithmetic(op, np, mul()); - np = simplify(np); } } @@ -705,7 +703,6 @@ shift(void) } next(); np = integerop(op, np, add()); - np = simplify(np); } } @@ -726,7 +723,6 @@ relational(void) } next(); np = compare(op, np, shift()); - np = simplify(np); } } @@ -745,7 +741,6 @@ eq(void) } next(); np = compare(op, np, relational()); - np = simplify(np); } } @@ -757,7 +752,7 @@ bit_and(void) np = eq(); while (accept('&')) np = integerop(OBAND, np, eq()); - return simplify(np); + return np; } static Node * @@ -768,7 +763,7 @@ bit_xor(void) np = bit_and(); while (accept('^')) np = integerop(OBXOR, np, bit_and()); - return simplify(np); + return np; } static Node * @@ -779,7 +774,7 @@ bit_or(void) np = bit_xor(); while (accept('|')) np = integerop(OBOR, np, bit_xor()); - return simplify(np); + return np; } static Node * @@ -790,7 +785,7 @@ and(void) np = bit_or(); while (accept(AND)) np = logic(OAND, np, bit_or()); - return simplify(np); + return np; } static Node * @@ -801,7 +796,7 @@ or(void) np = and(); while (accept(OR)) np = logic(OOR, np, and()); - return simplify(np); + return np; } static Node *