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);
}
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);
}
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 {
*/
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
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 {
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));
}
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);
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;
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)
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;
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) {
/* 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);
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)) {
return
is_type_integer(type) &&
is_constant_expression(expression) &&
- fold_constant(expression) == 0;
+ !fold_constant_to_bool(expression);
}
/**
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,
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
{
return
!is_constant_expression(cond) ? 0 :
- fold_constant(cond) != 0 ? 1 :
+ fold_constant_to_bool(cond) ? 1 :
-1;
}
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) {
}
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);
/* 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");
}
}
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");
{
return
!is_constant_expression(expr) ||
- fold_constant(expr) < 0;
+ fold_constant_to_int(expr) < 0;
}
/**
}
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;
}
}
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) {
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)