diff options
Diffstat (limited to 'gcc-4.6/gcc/c-family/c-common.c')
-rw-r--r-- | gcc-4.6/gcc/c-family/c-common.c | 56 |
1 files changed, 28 insertions, 28 deletions
diff --git a/gcc-4.6/gcc/c-family/c-common.c b/gcc-4.6/gcc/c-family/c-common.c index 011df8f..1429eee 100644 --- a/gcc-4.6/gcc/c-family/c-common.c +++ b/gcc-4.6/gcc/c-family/c-common.c @@ -1295,7 +1295,15 @@ c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands, STRIP_TYPE_NOPS (op0); if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR) op0 = decl_constant_value_for_optimization (op0); - if (op0 != orig_op0 || in_init) + /* ??? Cope with user tricks that amount to offsetof. The middle-end is + not prepared to deal with them if they occur in initializers. */ + if (op0 != orig_op0 + && code == ADDR_EXPR + && (op1 = get_base_address (op0)) != NULL_TREE + && TREE_CODE (op1) == INDIRECT_REF + && TREE_CONSTANT (TREE_OPERAND (op1, 0))) + ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0)); + else if (op0 != orig_op0 || in_init) ret = in_init ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0) : fold_build1_loc (loc, code, TREE_TYPE (expr), op0); @@ -9285,20 +9293,15 @@ c_common_to_target_charset (HOST_WIDE_INT c) return uc; } -/* Build the result of __builtin_offsetof. EXPR is a nested sequence of - component references, with STOP_REF, or alternatively an INDIRECT_REF of - NULL, at the bottom; much like the traditional rendering of offsetof as a - macro. Returns the folded and properly cast result. */ +/* Fold an offsetof-like expression. EXPR is a nested sequence of component + references with an INDIRECT_REF of a constant at the bottom; much like the + traditional rendering of offsetof as a macro. Return the folded result. */ -static tree -fold_offsetof_1 (tree expr, tree stop_ref) +tree +fold_offsetof_1 (tree expr) { - enum tree_code code = PLUS_EXPR; tree base, off, t; - if (expr == stop_ref && TREE_CODE (expr) != ERROR_MARK) - return size_zero_node; - switch (TREE_CODE (expr)) { case ERROR_MARK: @@ -9315,15 +9318,15 @@ fold_offsetof_1 (tree expr, tree stop_ref) case NOP_EXPR: case INDIRECT_REF: - if (!integer_zerop (TREE_OPERAND (expr, 0))) + if (!TREE_CONSTANT (TREE_OPERAND (expr, 0))) { error ("cannot apply %<offsetof%> to a non constant address"); return error_mark_node; } - return size_zero_node; + return TREE_OPERAND (expr, 0); case COMPONENT_REF: - base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref); + base = fold_offsetof_1 (TREE_OPERAND (expr, 0)); if (base == error_mark_node) return base; @@ -9341,21 +9344,14 @@ fold_offsetof_1 (tree expr, tree stop_ref) break; case ARRAY_REF: - base = fold_offsetof_1 (TREE_OPERAND (expr, 0), stop_ref); + base = fold_offsetof_1 (TREE_OPERAND (expr, 0)); if (base == error_mark_node) return base; t = TREE_OPERAND (expr, 1); - if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) < 0) - { - code = MINUS_EXPR; - t = fold_build1_loc (input_location, NEGATE_EXPR, TREE_TYPE (t), t); - } - t = convert (sizetype, t); - off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t); /* Check if the offset goes beyond the upper bound of the array. */ - if (code == PLUS_EXPR && TREE_CODE (t) == INTEGER_CST) + if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0) { tree upbound = array_ref_up_bound (expr); if (upbound != NULL_TREE @@ -9395,26 +9391,30 @@ fold_offsetof_1 (tree expr, tree stop_ref) } } } + + t = convert (sizetype, t); + off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t); break; case COMPOUND_EXPR: /* Handle static members of volatile structs. */ t = TREE_OPERAND (expr, 1); gcc_assert (TREE_CODE (t) == VAR_DECL); - return fold_offsetof_1 (t, stop_ref); + return fold_offsetof_1 (t); default: gcc_unreachable (); } - return size_binop (code, base, off); + return fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (base), base, off); } +/* Likewise, but convert it to the return type of offsetof. */ + tree -fold_offsetof (tree expr, tree stop_ref) +fold_offsetof (tree expr) { - /* Convert back from the internal sizetype to size_t. */ - return convert (size_type_node, fold_offsetof_1 (expr, stop_ref)); + return convert (size_type_node, fold_offsetof_1 (expr)); } /* Warn for A ?: C expressions (with B omitted) where A is a boolean |