[hackers] [scc] Simplify unary expressions || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Thu, 23 Jul 2015 18:33:56 +0200 (CEST)

commit 18071c940a707f9de54540e4ddbb405bbf8cba19
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Thu Jul 23 17:44:23 2015 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Thu Jul 23 17:44:23 2015 +0200

    Simplify unary expressions

diff --git a/cc1/cc1.h b/cc1/cc1.h
index d088a8f..e5273bf 100644
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
_AT_@ -327,6 +327,7 @@ extern Node *constnode(Symbol *sym);
 extern Node *sizeofnode(Type *tp);
 extern void freetree(Node *np);
 extern Node *simplify(unsigned char, Type *tp, Node *lp, Node *rp);
+extern Node *usimplify(unsigned char op, Type *tp, Node *np);
 
 /* expr.c */
 extern Node *expr(void), *negate(Node *np), *constexpr(void);
diff --git a/cc1/code.c b/cc1/code.c
index 05e329b..025a33a 100644
--- a/cc1/code.c
+++ b/cc1/code.c
_AT_@ -463,8 +463,6 @@ simplify(unsigned char op, Type *tp, Node *lp, Node *rp)
                 case OOR:
                         FOLDINT(sym, ls, rs, ||);
                         break;
- default:
- abort();
                 }
                 break;
         case FLOAT:
_AT_@ -481,3 +479,45 @@ division_by_0:
 no_simplify:
         return node(op, tp, lp, rp);
 }
+
+#define UFOLDINT(sym, ls, op) (((sym)->type->sign) ? \
+ ((sym)->u.i = (op (ls)->u.i)) : \
+ ((sym)->u.u = (op (ls)->u.u)))
+
+Node *
+usimplify(unsigned char op, Type *tp, Node *np)
+{
+ Symbol *sym, *ns;
+
+ if (!np->constant)
+ goto no_simplify;
+ ns = np->sym;
+
+ switch (tp->op) {
+ case INT:
+ switch (op) {
+ case ONEG:
+ sym = newsym(NS_IDEN);
+ sym->type = tp;
+ UFOLDINT(sym, ns, -);
+ break;
+ case OCPL:
+ sym = newsym(NS_IDEN);
+ sym->type = tp;
+ UFOLDINT(sym, ns, ~);
+ break;
+ default:
+ goto no_simplify;
+ }
+ break;
+ case FLOAT:
+ /* TODO: implement floats */
+ default:
+ goto no_simplify;
+ }
+
+ return constnode(sym);
+
+no_simplify:
+ return node(op, tp, np, NULL);
+}
diff --git a/cc1/expr.c b/cc1/expr.c
index f582a33..42a3a3b 100644
--- a/cc1/expr.c
+++ b/cc1/expr.c
_AT_@ -90,7 +90,7 @@ numericaluop(char op, Node *np)
         case FLOAT:
                 if (op == OADD)
                         return np;
- return node(op, np->type, np, NULL);
+ return usimplify(op, np->type, np);
         default:
                 error("unary operator requires integer operand");
         }
_AT_@ -102,7 +102,7 @@ integeruop(char op, Node *np)
         np = eval(np);
         if (BTYPE(np) != INT)
                 error("unary operator requires integer operand");
- return node(op, np->type, np, NULL);
+ return usimplify(op, np->type, np);
 }
 
 static Node *
Received on Thu Jul 23 2015 - 18:33:56 CEST

This archive was generated by hypermail 2.3.0 : Thu Jul 23 2015 - 18:36:12 CEST