From 94750498b1d3ef03fd78920b70e7330072dbb4ed Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Thu, 17 Dec 2020 01:39:43 +0100 Subject: 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)~ is not equivalent to ~(bool) (anything not all 0 or 1) * (float)~ is not equivalent to ~(float) which doesn't make sense. * (int)(float) 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 --- evaluate.c | 44 +--------------------------------------- validation/eval/not-cast-bool.c | 1 - validation/eval/not-cast-float.c | 1 - 3 files changed, 1 insertion(+), 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 -- cgit 1.2.3-korg