aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2020-12-17 01:39:43 +0100
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2021-04-19 00:56:42 +0200
commit94750498b1d3ef03fd78920b70e7330072dbb4ed (patch)
tree3b31f4eac1903318fdc76bbd765a787167231313
parent98075702ca78ff79e568a6b8004ee5fad7b0bfbe (diff)
downloadsparse-94750498b1d3ef03fd78920b70e7330072dbb4ed.tar.gz
remove early simplification of casts during evaluation
The current code will simplify away some casts at evaluation time but doesn't take in account some special cases: * (bool)~<int> is not equivalent to ~(bool)<int> (anything not all 0 or 1) * (float)~<int> is not equivalent to ~(float)<int> which doesn't make sense. * (int)(float)<int> is not a no-op if the (float) overflows This kind of simplification is better done on the IR where the different kind of casts correspond to distinct instructions. Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r--evaluate.c44
-rw-r--r--validation/eval/not-cast-bool.c1
-rw-r--r--validation/eval/not-cast-float.c1
3 files changed, 1 insertions, 45 deletions
diff --git a/evaluate.c b/evaluate.c
index eea6b7ad..61f59ee3 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -226,12 +226,6 @@ static struct symbol *bigger_int_type(struct symbol *left, struct symbol *right)
return utype;
}
-static int same_cast_type(struct symbol *orig, struct symbol *new)
-{
- return orig->bit_size == new->bit_size &&
- orig->bit_offset == new->bit_offset;
-}
-
static struct symbol *base_type(struct symbol *node, unsigned long *modp, struct ident **asp)
{
unsigned long mod = 0;
@@ -316,10 +310,7 @@ static struct symbol *cast_to_bool(struct expression *expr);
/*
* This gets called for implicit casts in assignments and
- * integer promotion. We often want to try to move the
- * cast down, because the ops involved may have been
- * implicitly cast up, and we can get rid of the casts
- * early.
+ * integer promotion.
*/
static struct expression * cast_to(struct expression *old, struct symbol *type)
{
@@ -330,39 +321,6 @@ static struct expression * cast_to(struct expression *old, struct symbol *type)
if (old->ctype != &null_ctype && is_same_type(old, type))
return old;
- /*
- * See if we can simplify the op. Move the cast down.
- */
- switch (old->type) {
- case EXPR_PREOP:
- if (old->ctype->bit_size < type->bit_size)
- break;
- if (old->op == '~') {
- old->ctype = type;
- old->unop = cast_to(old->unop, type);
- return old;
- }
- break;
-
- case EXPR_IMPLIED_CAST:
- warn_for_different_enum_types(old->pos, old->ctype, type);
-
- if (old->ctype->bit_size >= type->bit_size) {
- struct expression *orig = old->cast_expression;
- if (same_cast_type(orig->ctype, type))
- return orig;
- if (old->ctype->bit_offset == type->bit_offset) {
- old->ctype = type;
- old->cast_type = type;
- return old;
- }
- }
- break;
-
- default:
- /* nothing */;
- }
-
expr = alloc_expression(old->pos, EXPR_IMPLIED_CAST);
expr->ctype = type;
expr->cast_type = type;
diff --git a/validation/eval/not-cast-bool.c b/validation/eval/not-cast-bool.c
index af422412..acd8bbf2 100644
--- a/validation/eval/not-cast-bool.c
+++ b/validation/eval/not-cast-bool.c
@@ -8,7 +8,6 @@ static _Bool foo(void)
/*
* check-name: not-cast-bool
* check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
*
* check-output-ignore
* check-output-returns: 1
diff --git a/validation/eval/not-cast-float.c b/validation/eval/not-cast-float.c
index 445b91d7..d474d69b 100644
--- a/validation/eval/not-cast-float.c
+++ b/validation/eval/not-cast-float.c
@@ -8,7 +8,6 @@ static int foo(void)
/*
* check-name: eval-bool-zext-neg
* check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
*
* check-output-ignore
* check-output-returns: 1