commit 79ccb978a8d4f8c9c9700bb16d2b97625f3f1dd4
parent 101edd9a3b5d71aecd5ed30c70b2ad011e27a66d
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Wed, 26 Aug 2015 22:25:35 +0200
Simplify expression like *& or &*
This kind of expressions are common in array expressions
and the can simplify a lot the IR generated.
Diffstat:
1 file changed, 22 insertions(+), 5 deletions(-)
diff --git a/cc1/expr.c b/cc1/expr.c
@@ -358,6 +358,12 @@ decay(Node *np)
switch (tp->op) {
case ARY:
tp = tp->type;
+ if (np->op == OPTR) {
+ Node *new = np->left;
+ free(np);
+ new->type = mktype(tp, PTR, 0, NULL);
+ return new;
+ }
case FTN:
break;
default:
@@ -539,14 +545,16 @@ parithmetic(char op, Node *lp, Node *rp)
}
if (BTYPE(rp) != INT)
goto incorrect;
+
rp = convert(promote(rp), sizettype, 0);
- rp = node(OMUL, sizettype, rp, size);
- rp = node(OCAST, tp, rp, NULL);
+ rp = simplify(OMUL, sizettype, rp, size);
+ rp = convert(rp, tp, 1);
- return node(OADD, tp, lp, rp);
+ return simplify(OADD, tp, lp, rp);
incorrect:
- error("incorrect arithmetic operands");
+ errorp("incorrect arithmetic operands");
+ return node(OADD, tp, lp, rp);
}
static Node *
@@ -752,6 +760,11 @@ address(char op, Node *np)
error("lvalue required in unary expression");
if (np->symbol && (np->sym->flags & ISREGISTER))
error("address of register variable '%s' requested", yytext);
+ if (np->op == OPTR) {
+ Node *new = np->left;
+ free(np);
+ return new;
+ }
return node(op, mktype(np->type, PTR, 0, NULL), np, NULL);
}
@@ -761,8 +774,12 @@ content(char op, Node *np)
switch (BTYPE(np)) {
case ARY:
case FTN:
- np = decay(np);
case PTR:
+ if (np->op == OADDR) {
+ Node *new = np->left;
+ free(np);
+ return new;
+ }
np = node(op, np->type->type, np, NULL);
np->lvalue = 1;
return np;