create fold_constant_to_bool function to avoid some of the problems with long long...
authorMatthias Braun <matze@braunis.de>
Sun, 8 Mar 2009 00:21:52 +0000 (00:21 +0000)
committerMatthias Braun <matze@braunis.de>
Sun, 8 Mar 2009 00:21:52 +0000 (00:21 +0000)
[r25630]

ast.c
ast.h
ast2firm.c
attribute.c
parser.c

diff --git a/ast.c b/ast.c
index 108e157..4d1b89c 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -1943,7 +1943,7 @@ bool is_constant_expression(const expression_t *expression)
                expression_t const *const left = expression->binary.left;
                if (!is_constant_expression(left))
                        return false;
-               if (fold_constant(left) == 0)
+               if (fold_constant_to_bool(left) == false)
                        return true;
                return is_constant_expression(expression->binary.right);
        }
@@ -1952,7 +1952,7 @@ bool is_constant_expression(const expression_t *expression)
                expression_t const *const left = expression->binary.left;
                if (!is_constant_expression(left))
                        return false;
-               if (fold_constant(left) != 0)
+               if (fold_constant_to_bool(left) == true)
                        return true;
                return is_constant_expression(expression->binary.right);
        }
@@ -1965,8 +1965,7 @@ bool is_constant_expression(const expression_t *expression)
                if (!is_constant_expression(condition))
                        return false;
 
-               long val = fold_constant(condition);
-               if (val != 0) {
+               if (fold_constant_to_bool(condition) == true) {
                        expression_t const *const t = expression->conditional.true_expression;
                        return t == NULL || is_constant_expression(t);
                } else {
diff --git a/ast.h b/ast.h
index 453fc4d..6187c94 100644 (file)
--- a/ast.h
+++ b/ast.h
@@ -123,6 +123,7 @@ bool is_constant_expression(const expression_t *expression);
  */
 bool is_address_constant(const expression_t *expression);
 
-long fold_constant(const expression_t *expression);
+long fold_constant_to_int(const expression_t *expression);
+bool fold_constant_to_bool(const expression_t *expression);
 
 #endif
index f827514..99fdca0 100644 (file)
@@ -1818,8 +1818,8 @@ static ir_node *process_builtin_call(const call_expression_t *call)
                return NULL;
        case bk_gnu_builtin_frame_address: {
                expression_t *const expression = call->arguments->expression;
-               long val = fold_constant(expression);
-               if (val == 0) {
+               bool val = fold_constant_to_bool(expression);
+               if (!val) {
                        /* the nice case */
                        return get_irg_frame(current_ir_graph);
                } else {
@@ -2771,22 +2771,22 @@ static ir_node *create_lazy_op(const binary_expression_t *expression)
        ir_mode  *mode = get_ir_mode_arithmetic(type);
 
        if (is_constant_expression(expression->left)) {
-               long val = fold_constant(expression->left);
+               bool val = fold_constant_to_bool(expression->left);
                expression_kind_t ekind = expression->base.kind;
                assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
                if (ekind == EXPR_BINARY_LOGICAL_AND) {
-                       if (val == 0) {
+                       if (!val) {
                                return new_Const(get_mode_null(mode));
                        }
                } else {
-                       if (val != 0) {
+                       if (val) {
                                return new_Const(get_mode_one(mode));
                        }
                }
 
                if (is_constant_expression(expression->right)) {
-                       long const valr = fold_constant(expression->right);
-                       return valr != 0 ?
+                       bool valr = fold_constant_to_bool(expression->right);
+                       return valr ?
                                new_Const(get_mode_one(mode)) :
                                new_Const(get_mode_null(mode));
                }
@@ -2960,7 +2960,7 @@ static long get_offsetof_offset(const offsetof_expression_t *expression)
                        assert(designator->array_index != NULL);
                        assert(is_type_array(type));
 
-                       long index         = fold_constant(array_index);
+                       long index         = fold_constant_to_int(array_index);
                        ir_type *arr_type  = get_ir_type(type);
                        ir_type *elem_type = get_array_element_type(arr_type);
                        long     elem_size = get_type_size_bytes(elem_type);
@@ -3086,11 +3086,8 @@ static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
 
 static void init_ir_types(void);
 
-long fold_constant(const expression_t *expression)
+static tarval *fold_constant_to_tarval(const expression_t *expression)
 {
-       if (expression->kind == EXPR_INVALID)
-               return 0;
-
        assert(is_type_valid(skip_typeref(expression->base.type)));
 
        bool constant_folding_old = constant_folding;
@@ -3110,23 +3107,40 @@ long fold_constant(const expression_t *expression)
                panic("couldn't fold constant");
        }
 
+       constant_folding = constant_folding_old;
+
        tarval *tv = get_Const_tarval(cnst);
+       return tv;
+}
+
+long fold_constant_to_int(const expression_t *expression)
+{
+       if (expression->kind == EXPR_INVALID)
+               return 0;
+
+       tarval *tv = fold_constant_to_tarval(expression);
        if (!tarval_is_long(tv)) {
                panic("result of constant folding is not integer");
        }
 
-       constant_folding = constant_folding_old;
-
        return get_tarval_long(tv);
 }
 
+bool fold_constant_to_bool(const expression_t *expression)
+{
+       if (expression->kind == EXPR_INVALID)
+               return false;
+       tarval *tv = fold_constant_to_tarval(expression);
+       return !tarval_is_null(tv);
+}
+
 static ir_node *conditional_to_firm(const conditional_expression_t *expression)
 {
        dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
 
        /* first try to fold a constant condition */
        if (is_constant_expression(expression->condition)) {
-               long val = fold_constant(expression->condition);
+               bool val = fold_constant_to_bool(expression->condition);
                if (val) {
                        expression_t *true_expression = expression->true_expression;
                        if (true_expression == NULL)
@@ -3727,10 +3741,10 @@ static ir_node *create_condition_evaluation(const expression_t *expression,
        if (is_builtin_expect(expression) && is_Cond(cond)) {
                call_argument_t *argument = expression->call.arguments->next;
                if (is_constant_expression(argument->expression)) {
-                       long               cnst = fold_constant(argument->expression);
+                       bool             cnst = fold_constant_to_bool(argument->expression);
                        cond_jmp_predicate pred;
 
-                       if (cnst == 0) {
+                       if (cnst == false) {
                                pred = COND_JMP_PRED_FALSE;
                        } else {
                                pred = COND_JMP_PRED_TRUE;
@@ -3953,7 +3967,7 @@ static void walk_designator(type_path_t *path, const designator_t *designator)
                        assert(designator->array_index != NULL);
                        assert(is_type_array(type));
 
-                       long index = fold_constant(array_index);
+                       long index = fold_constant_to_int(array_index);
                        assert(index >= 0);
 #ifndef NDEBUG
                        if (type->array.size_constant) {
@@ -4883,7 +4897,7 @@ static void while_statement_to_firm(while_statement_t *statement)
 
        /* shortcut for while(true) */
        if (is_constant_expression(statement->condition)
-                       && fold_constant(statement->condition) != 0) {
+                       && fold_constant_to_bool(statement->condition) != 0) {
                set_cur_block(header_block);
                ir_node *header_jmp = new_Jmp();
                add_immBlock_pred(body_block, header_jmp);
index 7ed121e..8bca90b 100644 (file)
@@ -196,7 +196,7 @@ static void handle_attribute_aligned(const attribute_t *attribute,
                                                   target machine */
        if (attribute->a.arguments) {
                attribute_argument_t *argument = attribute->a.arguments;
-               alignment = fold_constant(argument->v.expression);
+               alignment = fold_constant_to_int(argument->v.expression);
        }
 
        if (!is_po2(alignment)) {
index e4b159e..078291d 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -1001,7 +1001,7 @@ static bool is_null_pointer_constant(const expression_t *expression)
        return
                is_type_integer(type)              &&
                is_constant_expression(expression) &&
-               fold_constant(expression) == 0;
+               !fold_constant_to_bool(expression);
 }
 
 /**
@@ -2113,7 +2113,7 @@ static bool walk_designator(type_path_t *path, const designator_t *designator,
                                goto failed;
                        }
 
-                       long index = fold_constant(array_index);
+                       long index = fold_constant_to_int(array_index);
                        if (!used_in_offsetof) {
                                if (index < 0) {
                                        errorf(&designator->source_position,
@@ -4060,7 +4060,8 @@ static type_t *construct_declarator_type(construct_type_t *construct_list, type_
 
                        if (size_expression != NULL) {
                                if (is_constant_expression(size_expression)) {
-                                       long const size                 = fold_constant(size_expression);
+                                       long const size
+                                               = fold_constant_to_int(size_expression);
                                        array_type->array.size          = size;
                                        array_type->array.size_constant = true;
                                        /* ยง6.7.5.2:1  If the expression is a constant expression, it shall
@@ -5082,7 +5083,7 @@ static int determine_truth(expression_t const* const cond)
 {
        return
                !is_constant_expression(cond) ? 0 :
-               fold_constant(cond) != 0      ? 1 :
+               fold_constant_to_bool(cond)   ? 1 :
                -1;
 }
 
@@ -5295,7 +5296,7 @@ static void check_reachable(statement_t *const stmt)
                                return;
 
                        if (is_constant_expression(expr)) {
-                               long                    const val      = fold_constant(expr);
+                               long                    const val      = fold_constant_to_int(expr);
                                case_label_statement_t *      defaults = NULL;
                                for (case_label_statement_t *i = switchs->first_case; i != NULL; i = i->next) {
                                        if (i->expression == NULL) {
@@ -5882,7 +5883,7 @@ static type_t *make_bitfield_type(type_t *base_type, expression_t *size,
        }
 
        if (is_constant_expression(size)) {
-               long v = fold_constant(size);
+               long v = fold_constant_to_int(size);
 
                if (v < 0) {
                        errorf(source_position, "negative width in bit-field '%Y'", symbol);
@@ -8320,7 +8321,7 @@ static void warn_div_by_zero(binary_expression_t const *const expression)
        /* The type of the right operand can be different for /= */
        if (is_type_integer(right->base.type) &&
            is_constant_expression(right)     &&
-           fold_constant(right) == 0) {
+           !fold_constant_to_bool(right)) {
                warningf(&expression->base.source_position, "division by zero");
        }
 }
@@ -8371,7 +8372,7 @@ static bool semantic_shift(binary_expression_t *expression)
        type_left = promote_integer(type_left);
 
        if (is_constant_expression(right)) {
-               long count = fold_constant(right);
+               long count = fold_constant_to_int(right);
                if (count < 0) {
                        warningf(&right->base.source_position,
                                        "shift count must be non-negative");
@@ -8520,7 +8521,7 @@ static bool maybe_negative(expression_t const *const expr)
 {
        return
                !is_constant_expression(expr) ||
-               fold_constant(expr) < 0;
+               fold_constant_to_int(expr) < 0;
 }
 
 /**
@@ -9378,7 +9379,7 @@ static statement_t *parse_case_statement(void)
                }
                statement->case_label.is_bad = true;
        } else {
-               long const val = fold_constant(expression);
+               long const val = fold_constant_to_int(expression);
                statement->case_label.first_case = val;
                statement->case_label.last_case  = val;
        }
@@ -9397,7 +9398,7 @@ static statement_t *parse_case_statement(void)
                                }
                                statement->case_label.is_bad = true;
                        } else {
-                               long const val = fold_constant(end_range);
+                               long const val = fold_constant_to_int(end_range);
                                statement->case_label.last_case = val;
 
                                if (warning.other && val < statement->case_label.first_case) {
@@ -9625,7 +9626,7 @@ static void check_enum_cases(const switch_statement_t *statement)
        for (; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
             entry = entry->base.next) {
                const expression_t *expression = entry->enum_value.value;
-               long                value      = expression != NULL ? fold_constant(expression) : last_value + 1;
+               long                value      = expression != NULL ? fold_constant_to_int(expression) : last_value + 1;
                bool                found      = false;
                for (const case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
                        if (l->expression == NULL)