[hackers] [scc] Simplify before of creating nodes || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Thu, 23 Jul 2015 16:46:32 +0200 (CEST)

commit 4f3e4465ccb3e7c611c7996793abefe399787983
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Thu Jul 23 15:31:05 2015 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Thu Jul 23 15:46:07 2015 +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.

diff --git a/cc1/cc1.h b/cc1/cc1.h
index 544122e..d088a8f 100644
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
_AT_@ -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
index 20be4b9..05e329b 100644
--- a/cc1/code.c
+++ b/cc1/code.c
_AT_@ -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;
_AT_@ -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
index 000f2d5..f582a33 100644
--- a/cc1/expr.c
+++ b/cc1/expr.c
_AT_@ -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 *
_AT_@ -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 *
_AT_@ -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 *
_AT_@ -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 *
_AT_@ -667,7 +667,6 @@ mul(void)
                 }
                 next();
                 np = (*fun)(op, np, cast());
- np = simplify(np);
         }
 }
 
_AT_@ -686,7 +685,6 @@ add(void)
                 }
                 next();
                 np = arithmetic(op, np, mul());
- np = simplify(np);
         }
 }
 
_AT_@ -705,7 +703,6 @@ shift(void)
                 }
                 next();
                 np = integerop(op, np, add());
- np = simplify(np);
         }
 }
 
_AT_@ -726,7 +723,6 @@ relational(void)
                 }
                 next();
                 np = compare(op, np, shift());
- np = simplify(np);
         }
 }
 
_AT_@ -745,7 +741,6 @@ eq(void)
                 }
                 next();
                 np = compare(op, np, relational());
- np = simplify(np);
         }
 }
 
_AT_@ -757,7 +752,7 @@ bit_and(void)
         np = eq();
         while (accept('&'))
                 np = integerop(OBAND, np, eq());
- return simplify(np);
+ return np;
 }
 
 static Node *
_AT_@ -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 *
_AT_@ -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 *
_AT_@ -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 *
_AT_@ -801,7 +796,7 @@ or(void)
         np = and();
         while (accept(OR))
                 np = logic(OOR, np, and());
- return simplify(np);
+ return np;
 }
 
 static Node *
Received on Thu Jul 23 2015 - 16:46:32 CEST

This archive was generated by hypermail 2.3.0 : Thu Jul 23 2015 - 16:48:11 CEST