+ panic("reference to declaration with unknown type found");
+}
+
+/**
+ * Generate an unary builtin.
+ *
+ * @param kind the builtin kind to generate
+ * @param op the operand
+ * @param function_type the function type for the GNU builtin routine
+ * @param db debug info
+ */
+static ir_node *gen_unary_builtin(ir_builtin_kind kind, expression_t *op, type_t *function_type, dbg_info *db)
+{
+ ir_node *in[1];
+ in[0] = expression_to_firm(op);
+
+ ir_type *tp = get_ir_type(function_type);
+ ir_type *res = get_method_res_type(tp, 0);
+ ir_node *irn = new_d_Builtin(db, get_irg_no_mem(current_ir_graph), kind, 1, in, tp);
+ set_irn_pinned(irn, op_pin_state_floats);
+ return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
+}
+
+/**
+ * Generate a pinned unary builtin.
+ *
+ * @param kind the builtin kind to generate
+ * @param op the operand
+ * @param function_type the function type for the GNU builtin routine
+ * @param db debug info
+ */
+static ir_node *gen_unary_builtin_pinned(ir_builtin_kind kind, expression_t *op, type_t *function_type, dbg_info *db)
+{
+ ir_node *in[1];
+ in[0] = expression_to_firm(op);
+
+ ir_type *tp = get_ir_type(function_type);
+ ir_type *res = get_method_res_type(tp, 0);
+ ir_node *mem = get_store();
+ ir_node *irn = new_d_Builtin(db, mem, kind, 1, in, tp);
+ set_store(new_Proj(irn, mode_M, pn_Builtin_M));
+ return new_Proj(irn, get_type_mode(res), pn_Builtin_1_result);
+}
+
+
+/**
+ * Generate an binary-void-return builtin.
+ *
+ * @param kind the builtin kind to generate
+ * @param op1 the first operand
+ * @param op2 the second operand
+ * @param function_type the function type for the GNU builtin routine
+ * @param db debug info
+ */
+static ir_node *gen_binary_builtin_mem(ir_builtin_kind kind, expression_t *op1, expression_t *op2,
+ type_t *function_type, dbg_info *db)
+{
+ ir_node *in[2];
+ in[0] = expression_to_firm(op1);
+ in[1] = expression_to_firm(op2);
+
+ ir_type *tp = get_ir_type(function_type);
+ ir_node *mem = get_store();
+ ir_node *irn = new_d_Builtin(db, mem, kind, 2, in, tp);
+ set_store(new_Proj(irn, mode_M, pn_Builtin_M));
+ return NULL;
+}
+
+/**
+ * Transform calls to builtin functions.
+ */
+static ir_node *process_builtin_call(const call_expression_t *call)
+{
+ dbg_info *dbgi = get_dbg_info(&call->base.source_position);
+
+ assert(call->function->kind == EXPR_REFERENCE);
+ reference_expression_t *builtin = &call->function->reference;
+
+ type_t *type = skip_typeref(builtin->base.type);
+ assert(is_type_pointer(type));
+
+ type_t *function_type = skip_typeref(type->pointer.points_to);
+
+ switch (builtin->entity->function.btk) {
+ case bk_gnu_builtin_alloca: {
+ if (call->arguments == NULL || call->arguments->next != NULL) {
+ panic("invalid number of parameters on __builtin_alloca");
+ }
+ expression_t *argument = call->arguments->expression;
+ ir_node *size = expression_to_firm(argument);
+
+ ir_node *store = get_store();
+ ir_node *alloca = new_d_Alloc(dbgi, store, size, firm_unknown_type,
+ stack_alloc);
+ ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
+ set_store(proj_m);
+ ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
+
+ return res;
+ }
+
+ case bk_gnu_builtin_huge_val:
+ case bk_gnu_builtin_inf:
+ case bk_gnu_builtin_inff:
+ case bk_gnu_builtin_infl: {
+ type_t *type = function_type->function.return_type;
+ ir_mode *mode = get_ir_mode_arithmetic(type);
+ tarval *tv = get_mode_infinite(mode);
+ ir_node *res = new_d_Const(dbgi, tv);
+ return res;
+ }
+ case bk_gnu_builtin_nan:
+ case bk_gnu_builtin_nanf:
+ case bk_gnu_builtin_nanl: {
+ /* Ignore string for now... */
+ assert(is_type_function(function_type));
+ type_t *type = function_type->function.return_type;
+ ir_mode *mode = get_ir_mode_arithmetic(type);
+ tarval *tv = get_mode_NAN(mode);
+ ir_node *res = new_d_Const(dbgi, tv);
+ return res;
+ }
+ case bk_gnu_builtin_expect: {
+ expression_t *argument = call->arguments->expression;
+ return _expression_to_firm(argument);
+ }
+ case bk_gnu_builtin_va_end:
+ /* evaluate the argument of va_end for its side effects */
+ _expression_to_firm(call->arguments->expression);
+ return NULL;
+ case bk_gnu_builtin_frame_address: {
+ expression_t *const expression = call->arguments->expression;
+ bool val = fold_constant_to_bool(expression);
+ if (!val) {
+ /* the nice case */
+ return get_irg_frame(current_ir_graph);
+ } else {
+ /* get the argument */
+ ir_node *in[2];
+
+ in[0] = expression_to_firm(expression);
+ in[1] = get_irg_frame(current_ir_graph);
+ ir_type *tp = get_ir_type(function_type);
+ ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), ir_bk_frame_addess, 2, in, tp);
+ return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
+ }
+ }
+ case bk_gnu_builtin_return_address: {
+
+ expression_t *const expression = call->arguments->expression;
+ ir_node *in[2];
+
+ in[0] = expression_to_firm(expression);
+ in[1] = get_irg_frame(current_ir_graph);
+ ir_type *tp = get_ir_type(function_type);
+ ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), ir_bk_return_address, 2, in, tp);
+ return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
+ }
+ case bk_gnu_builtin_ffs:
+ return gen_unary_builtin(ir_bk_ffs, call->arguments->expression, function_type, dbgi);
+ case bk_gnu_builtin_clz:
+ return gen_unary_builtin(ir_bk_clz, call->arguments->expression, function_type, dbgi);
+ case bk_gnu_builtin_ctz:
+ return gen_unary_builtin(ir_bk_ctz, call->arguments->expression, function_type, dbgi);
+ case bk_gnu_builtin_popcount:
+ case bk_ms__popcount:
+ return gen_unary_builtin(ir_bk_popcount, call->arguments->expression, function_type, dbgi);
+ case bk_gnu_builtin_parity:
+ return gen_unary_builtin(ir_bk_parity, call->arguments->expression, function_type, dbgi);
+ case bk_gnu_builtin_prefetch: {
+ call_argument_t *const args = call->arguments;
+ expression_t *const addr = args->expression;
+ ir_node *in[3];
+
+ in[0] = _expression_to_firm(addr);
+ if (args->next != NULL) {
+ expression_t *const rw = args->next->expression;
+
+ in[1] = _expression_to_firm(rw);
+
+ if (args->next->next != NULL) {
+ expression_t *const locality = args->next->next->expression;
+
+ in[2] = expression_to_firm(locality);
+ } else {
+ in[2] = new_Const_long(mode_int, 3);
+ }
+ } else {
+ in[1] = new_Const_long(mode_int, 0);
+ in[2] = new_Const_long(mode_int, 3);
+ }
+ ir_type *tp = get_ir_type(function_type);
+ ir_node *irn = new_d_Builtin(dbgi, get_store(), ir_bk_prefetch, 3, in, tp);
+ set_store(new_Proj(irn, mode_M, pn_Builtin_M));
+ return NULL;
+ }
+ case bk_gnu_builtin_trap:
+ case bk_ms__ud2:
+ {
+ ir_type *tp = get_ir_type(function_type);
+ ir_node *irn = new_d_Builtin(dbgi, get_store(), ir_bk_trap, 0, NULL, tp);
+ set_store(new_Proj(irn, mode_M, pn_Builtin_M));
+ return NULL;
+ }
+ case bk_ms__debugbreak: {
+ ir_type *tp = get_ir_type(function_type);
+ ir_node *irn = new_d_Builtin(dbgi, get_store(), ir_bk_debugbreak, 0, NULL, tp);
+ set_store(new_Proj(irn, mode_M, pn_Builtin_M));
+ return NULL;
+ }
+ case bk_ms_ReturnAddress: {
+ ir_node *in[2];
+
+ in[0] = new_Const_long(mode_int, 0);
+ in[1] = get_irg_frame(current_ir_graph);
+ ir_type *tp = get_ir_type(function_type);
+ ir_node *irn = new_d_Builtin(dbgi, get_irg_no_mem(current_ir_graph), ir_bk_return_address, 2, in, tp);
+ return new_Proj(irn, mode_P_data, pn_Builtin_1_result);
+ }
+ case bk_ms_rotl:
+ case bk_ms_rotl64: {
+ ir_node *val = expression_to_firm(call->arguments->expression);
+ ir_node *shf = expression_to_firm(call->arguments->next->expression);
+ ir_mode *mode = get_irn_mode(val);
+ return new_d_Rotl(dbgi, val, create_conv(dbgi, shf, mode_uint), mode);
+ }
+ case bk_ms_rotr:
+ case bk_ms_rotr64: {
+ ir_node *val = expression_to_firm(call->arguments->expression);
+ ir_node *shf = expression_to_firm(call->arguments->next->expression);
+ ir_mode *mode = get_irn_mode(val);
+ ir_node *c = new_Const_long(mode_uint, get_mode_size_bits(mode));
+ ir_node *sub = new_d_Sub(dbgi, c, create_conv(dbgi, shf, mode_uint), mode_uint);
+ return new_d_Rotl(dbgi, val, sub, mode);
+ }
+ case bk_ms_byteswap_ushort:
+ case bk_ms_byteswap_ulong:
+ case bk_ms_byteswap_uint64:
+ return gen_unary_builtin(ir_bk_bswap, call->arguments->expression, function_type, dbgi);
+ case bk_ms__inbyte:
+ case bk_ms__inword:
+ case bk_ms__indword:
+ return gen_unary_builtin_pinned(ir_bk_inport, call->arguments->expression, function_type, dbgi);
+ case bk_ms__outbyte:
+ case bk_ms__outword:
+ case bk_ms__outdword:
+ return gen_binary_builtin_mem(ir_bk_outport, call->arguments->expression,
+ call->arguments->next->expression, function_type, dbgi);
+ default:
+ panic("unsupported builtin found");
+ }
+}
+
+/**
+ * Transform a call expression.
+ * Handles some special cases, like alloca() calls, which must be resolved
+ * BEFORE the inlines runs. Inlining routines calling alloca() is dangerous,
+ * 176.gcc for instance might allocate 2GB instead of 256 MB if alloca is not
+ * handled right...
+ */
+static ir_node *call_expression_to_firm(const call_expression_t *const call)
+{
+ dbg_info *const dbgi = get_dbg_info(&call->base.source_position);
+ assert(get_cur_block() != NULL);
+
+ expression_t *function = call->function;
+ if (function->kind == EXPR_REFERENCE) {
+ const reference_expression_t *ref = &function->reference;
+ entity_t *entity = ref->entity;
+
+ if (entity->kind == ENTITY_FUNCTION) {
+ if (entity->function.btk != bk_none) {
+ return process_builtin_call(call);
+ }
+
+ ir_entity *irentity = entity->function.irentity;
+ if (irentity == NULL)
+ irentity = get_function_entity(entity, NULL);
+
+ if (irentity == rts_entities[rts_alloca]) {
+ /* handle alloca() call */
+ expression_t *argument = call->arguments->expression;
+ ir_node *size = expression_to_firm(argument);
+ ir_mode *mode = get_ir_mode_arithmetic(type_size_t);
+
+ size = create_conv(dbgi, size, mode);
+
+ ir_node *store = get_store();
+ ir_node *alloca = new_d_Alloc(dbgi, store, size,
+ firm_unknown_type, stack_alloc);
+ ir_node *proj_m = new_Proj(alloca, mode_M, pn_Alloc_M);
+ set_store(proj_m);
+ ir_node *res = new_Proj(alloca, mode_P_data, pn_Alloc_res);
+
+ return res;
+ }
+ }
+ }
+ ir_node *callee = expression_to_firm(function);
+
+ type_t *type = skip_typeref(function->base.type);
+ assert(is_type_pointer(type));
+ pointer_type_t *pointer_type = &type->pointer;
+ type_t *points_to = skip_typeref(pointer_type->points_to);
+ assert(is_type_function(points_to));
+ function_type_t *function_type = &points_to->function;
+
+ int n_parameters = 0;
+ ir_type *ir_method_type = get_ir_type((type_t*) function_type);
+ ir_type *new_method_type = NULL;
+ if (function_type->variadic || function_type->unspecified_parameters) {
+ const call_argument_t *argument = call->arguments;
+ for ( ; argument != NULL; argument = argument->next) {
+ ++n_parameters;
+ }
+
+ /* we need to construct a new method type matching the call
+ * arguments... */
+ int n_res = get_method_n_ress(ir_method_type);
+ new_method_type = new_d_type_method(id_unique("calltype.%u"),
+ n_parameters, n_res, dbgi);
+ set_method_calling_convention(new_method_type,
+ get_method_calling_convention(ir_method_type));
+ set_method_additional_properties(new_method_type,
+ get_method_additional_properties(ir_method_type));
+ set_method_variadicity(new_method_type,
+ get_method_variadicity(ir_method_type));
+
+ for (int i = 0; i < n_res; ++i) {
+ set_method_res_type(new_method_type, i,
+ get_method_res_type(ir_method_type, i));
+ }
+ argument = call->arguments;
+ for (int i = 0; i < n_parameters; ++i, argument = argument->next) {
+ expression_t *expression = argument->expression;
+ ir_type *irtype = get_ir_type(expression->base.type);
+ set_method_param_type(new_method_type, i, irtype);
+ }
+ ir_method_type = new_method_type;
+ } else {
+ n_parameters = get_method_n_params(ir_method_type);
+ }
+
+ ir_node *in[n_parameters];
+
+ const call_argument_t *argument = call->arguments;
+ for (int n = 0; n < n_parameters; ++n) {
+ expression_t *expression = argument->expression;
+ ir_node *arg_node = expression_to_firm(expression);
+
+ type_t *type = skip_typeref(expression->base.type);
+ if (!is_type_compound(type)) {
+ ir_mode *mode = get_ir_mode_storage(expression->base.type);
+ arg_node = create_conv(dbgi, arg_node, mode);
+ arg_node = do_strict_conv(dbgi, arg_node);
+ }
+
+ in[n] = arg_node;
+
+ argument = argument->next;
+ }
+
+ ir_node *store = get_store();
+ ir_node *node = new_d_Call(dbgi, store, callee, n_parameters, in,
+ ir_method_type);
+ ir_node *mem = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
+ set_store(mem);
+
+ type_t *return_type = skip_typeref(function_type->return_type);
+ ir_node *result = NULL;
+
+ if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
+ ir_node *resproj = new_d_Proj(dbgi, node, mode_T, pn_Call_T_result);
+
+ if (is_type_scalar(return_type)) {
+ ir_mode *mode = get_ir_mode_storage(return_type);
+ result = new_d_Proj(dbgi, resproj, mode, 0);
+ ir_mode *mode_arith = get_ir_mode_arithmetic(return_type);
+ result = create_conv(NULL, result, mode_arith);
+ } else {
+ ir_mode *mode = mode_P_data;
+ result = new_d_Proj(dbgi, resproj, mode, 0);
+ }
+ }
+
+ if (function->kind == EXPR_REFERENCE &&
+ function->reference.entity->declaration.modifiers & DM_NORETURN) {
+ /* A dead end: Keep the Call and the Block. Also place all further
+ * nodes into a new and unreachable block. */
+ keep_alive(node);
+ keep_alive(get_cur_block());
+ new_Block(0, NULL);
+ }
+
+ return result;
+}
+
+static void statement_to_firm(statement_t *statement);
+static ir_node *compound_statement_to_firm(compound_statement_t *compound);
+
+static ir_node *expression_to_addr(const expression_t *expression);
+static ir_node *create_condition_evaluation(const expression_t *expression,
+ ir_node *true_block,
+ ir_node *false_block);
+
+static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
+ ir_node *value)
+{
+ if (!is_type_compound(type)) {
+ ir_mode *mode = get_ir_mode_storage(type);
+ value = create_conv(dbgi, value, mode);
+ value = do_strict_conv(dbgi, value);
+ }
+
+ ir_node *memory = get_store();
+
+ if (is_type_scalar(type)) {
+ ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
+ ? cons_volatile : cons_none;
+ ir_node *store = new_d_Store(dbgi, memory, addr, value, flags);
+ ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
+ set_store(store_mem);
+ } else {
+ ir_type *irtype = get_ir_type(type);
+ ir_node *copyb = new_d_CopyB(dbgi, memory, addr, value, irtype);
+ ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
+ set_store(copyb_mem);
+ }
+}
+
+static tarval *create_bitfield_mask(ir_mode *mode, int offset, int size)
+{
+ tarval *all_one = get_mode_all_one(mode);
+ int mode_size = get_mode_size_bits(mode);
+
+ assert(offset >= 0);
+ assert(size >= 0);
+ assert(offset + size <= mode_size);
+ if (size == mode_size) {
+ return all_one;
+ }
+
+ long shiftr = get_mode_size_bits(mode) - size;
+ long shiftl = offset;
+ tarval *tv_shiftr = new_tarval_from_long(shiftr, mode_uint);
+ tarval *tv_shiftl = new_tarval_from_long(shiftl, mode_uint);
+ tarval *mask0 = tarval_shr(all_one, tv_shiftr);
+ tarval *mask1 = tarval_shl(mask0, tv_shiftl);
+
+ return mask1;
+}
+
+static ir_node *bitfield_store_to_firm(dbg_info *dbgi,
+ ir_entity *entity, ir_node *addr, ir_node *value, bool set_volatile)
+{
+ ir_type *entity_type = get_entity_type(entity);
+ ir_type *base_type = get_primitive_base_type(entity_type);
+ assert(base_type != NULL);
+ ir_mode *mode = get_type_mode(base_type);
+
+ value = create_conv(dbgi, value, mode);
+
+ /* kill upper bits of value and shift to right position */
+ int bitoffset = get_entity_offset_bits_remainder(entity);
+ int bitsize = get_mode_size_bits(get_type_mode(entity_type));
+
+ tarval *mask = create_bitfield_mask(mode, 0, bitsize);
+ ir_node *mask_node = new_d_Const(dbgi, mask);
+ ir_node *value_masked = new_d_And(dbgi, value, mask_node, mode);
+ tarval *shiftl = new_tarval_from_long(bitoffset, mode_uint);
+ ir_node *shiftcount = new_d_Const(dbgi, shiftl);
+ ir_node *value_maskshift = new_d_Shl(dbgi, value_masked, shiftcount, mode);
+
+ /* load current value */
+ ir_node *mem = get_store();
+ ir_node *load = new_d_Load(dbgi, mem, addr, mode,
+ set_volatile ? cons_volatile : cons_none);
+ ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
+ ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
+ tarval *shift_mask = create_bitfield_mask(mode, bitoffset, bitsize);
+ tarval *inv_mask = tarval_not(shift_mask);
+ ir_node *inv_mask_node = new_d_Const(dbgi, inv_mask);
+ ir_node *load_res_masked = new_d_And(dbgi, load_res, inv_mask_node, mode);
+
+ /* construct new value and store */
+ ir_node *new_val = new_d_Or(dbgi, load_res_masked, value_maskshift, mode);
+ ir_node *store = new_d_Store(dbgi, load_mem, addr, new_val,
+ set_volatile ? cons_volatile : cons_none);
+ ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
+ set_store(store_mem);
+
+ return value_masked;
+}
+
+static ir_node *bitfield_extract_to_firm(const select_expression_t *expression,
+ ir_node *addr)
+{
+ dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
+ type_t *type = expression->base.type;
+ ir_mode *mode = get_ir_mode_storage(type);
+ ir_node *mem = get_store();
+ ir_node *load = new_d_Load(dbgi, mem, addr, mode, cons_none);
+ ir_node *load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
+ ir_node *load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
+
+ load_res = create_conv(dbgi, load_res, mode_int);
+
+ set_store(load_mem);
+
+ /* kill upper bits */
+ assert(expression->compound_entry->kind == ENTITY_COMPOUND_MEMBER);
+ ir_entity *entity = expression->compound_entry->compound_member.entity;
+ int bitoffset = get_entity_offset_bits_remainder(entity);
+ ir_type *entity_type = get_entity_type(entity);
+ int bitsize = get_mode_size_bits(get_type_mode(entity_type));
+ long shift_bitsl = machine_size - bitoffset - bitsize;
+ assert(shift_bitsl >= 0);
+ tarval *tvl = new_tarval_from_long(shift_bitsl, mode_uint);
+ ir_node *countl = new_d_Const(dbgi, tvl);
+ ir_node *shiftl = new_d_Shl(dbgi, load_res, countl, mode_int);
+
+ long shift_bitsr = bitoffset + shift_bitsl;
+ assert(shift_bitsr <= (long) machine_size);
+ tarval *tvr = new_tarval_from_long(shift_bitsr, mode_uint);
+ ir_node *countr = new_d_Const(dbgi, tvr);
+ ir_node *shiftr;
+ if (mode_is_signed(mode)) {
+ shiftr = new_d_Shrs(dbgi, shiftl, countr, mode_int);
+ } else {
+ shiftr = new_d_Shr(dbgi, shiftl, countr, mode_int);
+ }
+
+ return create_conv(dbgi, shiftr, mode);
+}
+
+/* make sure the selected compound type is constructed */
+static void construct_select_compound(const select_expression_t *expression)
+{
+ type_t *type = skip_typeref(expression->compound->base.type);
+ if (is_type_pointer(type)) {
+ type = type->pointer.points_to;
+ }
+ (void) get_ir_type(type);
+}
+
+static ir_node *set_value_for_expression_addr(const expression_t *expression,
+ ir_node *value, ir_node *addr)
+{
+ dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
+ type_t *type = skip_typeref(expression->base.type);
+
+ if (!is_type_compound(type)) {
+ ir_mode *mode = get_ir_mode_storage(type);
+ value = create_conv(dbgi, value, mode);
+ value = do_strict_conv(dbgi, value);
+ }
+
+ if (expression->kind == EXPR_REFERENCE) {
+ const reference_expression_t *ref = &expression->reference;
+
+ entity_t *entity = ref->entity;
+ assert(is_declaration(entity));
+ assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
+ if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
+ set_value(entity->variable.v.value_number, value);
+ return value;
+ } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
+ set_value(entity->parameter.v.value_number, value);
+ return value;
+ }
+ }
+
+ if (addr == NULL)
+ addr = expression_to_addr(expression);
+ assert(addr != NULL);
+
+ if (expression->kind == EXPR_SELECT) {
+ const select_expression_t *select = &expression->select;
+
+ construct_select_compound(select);
+
+ entity_t *entity = select->compound_entry;
+ assert(entity->kind == ENTITY_COMPOUND_MEMBER);
+ if (entity->declaration.type->kind == TYPE_BITFIELD) {
+ ir_entity *irentity = entity->compound_member.entity;
+ bool set_volatile
+ = select->base.type->base.qualifiers & TYPE_QUALIFIER_VOLATILE;
+ value = bitfield_store_to_firm(dbgi, irentity, addr, value,
+ set_volatile);
+ return value;
+ }
+ }
+
+ assign_value(dbgi, addr, type, value);
+ return value;
+}
+
+static void set_value_for_expression(const expression_t *expression,
+ ir_node *value)
+{
+ set_value_for_expression_addr(expression, value, NULL);
+}
+
+static ir_node *get_value_from_lvalue(const expression_t *expression,
+ ir_node *addr)
+{
+ if (expression->kind == EXPR_REFERENCE) {
+ const reference_expression_t *ref = &expression->reference;
+
+ entity_t *entity = ref->entity;
+ assert(entity->kind == ENTITY_VARIABLE
+ || entity->kind == ENTITY_PARAMETER);
+ assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
+ int value_number;
+ if (entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE) {
+ value_number = entity->variable.v.value_number;
+ assert(addr == NULL);
+ type_t *type = skip_typeref(expression->base.type);
+ ir_mode *mode = get_ir_mode_storage(type);
+ ir_node *res = get_value(value_number, mode);
+ return create_conv(NULL, res, get_ir_mode_arithmetic(type));
+ } else if (entity->declaration.kind == DECLARATION_KIND_PARAMETER) {
+ value_number = entity->parameter.v.value_number;
+ assert(addr == NULL);
+ type_t *type = skip_typeref(expression->base.type);
+ ir_mode *mode = get_ir_mode_storage(type);
+ ir_node *res = get_value(value_number, mode);
+ return create_conv(NULL, res, get_ir_mode_arithmetic(type));
+ }
+ }
+
+ assert(addr != NULL);
+ dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
+
+ ir_node *value;
+ if (expression->kind == EXPR_SELECT &&
+ expression->select.compound_entry->declaration.type->kind == TYPE_BITFIELD){
+ construct_select_compound(&expression->select);
+ value = bitfield_extract_to_firm(&expression->select, addr);
+ } else {
+ value = deref_address(dbgi, expression->base.type, addr);
+ }
+
+ return value;
+}
+
+
+static ir_node *create_incdec(const unary_expression_t *expression)
+{
+ dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
+ const expression_t *value_expr = expression->value;
+ ir_node *addr = expression_to_addr(value_expr);
+ ir_node *value = get_value_from_lvalue(value_expr, addr);
+
+ type_t *type = skip_typeref(expression->base.type);
+ ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
+
+ ir_node *offset;
+ if (is_type_pointer(type)) {
+ pointer_type_t *pointer_type = &type->pointer;
+ offset = get_type_size_node(pointer_type->points_to);
+ } else {
+ assert(is_type_arithmetic(type));
+ offset = new_Const(get_mode_one(mode));
+ }
+
+ ir_node *result;
+ ir_node *store_value;
+ switch(expression->base.kind) {
+ case EXPR_UNARY_POSTFIX_INCREMENT:
+ result = value;
+ store_value = new_d_Add(dbgi, value, offset, mode);
+ break;
+ case EXPR_UNARY_POSTFIX_DECREMENT:
+ result = value;
+ store_value = new_d_Sub(dbgi, value, offset, mode);
+ break;
+ case EXPR_UNARY_PREFIX_INCREMENT:
+ result = new_d_Add(dbgi, value, offset, mode);
+ store_value = result;
+ break;
+ case EXPR_UNARY_PREFIX_DECREMENT:
+ result = new_d_Sub(dbgi, value, offset, mode);
+ store_value = result;
+ break;
+ default:
+ panic("no incdec expr in create_incdec");
+ }
+
+ set_value_for_expression_addr(value_expr, store_value, addr);
+
+ return result;
+}
+
+static bool is_local_variable(expression_t *expression)
+{
+ if (expression->kind != EXPR_REFERENCE)
+ return false;
+ reference_expression_t *ref_expr = &expression->reference;
+ entity_t *entity = ref_expr->entity;
+ if (entity->kind != ENTITY_VARIABLE)
+ return false;
+ assert(entity->declaration.kind != DECLARATION_KIND_UNKNOWN);
+ return entity->declaration.kind == DECLARATION_KIND_LOCAL_VARIABLE;
+}
+
+static pn_Cmp get_pnc(const expression_kind_t kind, type_t *const type)
+{
+ switch(kind) {
+ case EXPR_BINARY_EQUAL: return pn_Cmp_Eq;
+ case EXPR_BINARY_ISLESSGREATER: return pn_Cmp_Lg;
+ case EXPR_BINARY_NOTEQUAL:
+ return is_type_float(skip_typeref(type)) ? pn_Cmp_Ne : pn_Cmp_Lg;
+ case EXPR_BINARY_ISLESS:
+ case EXPR_BINARY_LESS: return pn_Cmp_Lt;
+ case EXPR_BINARY_ISLESSEQUAL:
+ case EXPR_BINARY_LESSEQUAL: return pn_Cmp_Le;
+ case EXPR_BINARY_ISGREATER:
+ case EXPR_BINARY_GREATER: return pn_Cmp_Gt;
+ case EXPR_BINARY_ISGREATEREQUAL:
+ case EXPR_BINARY_GREATEREQUAL: return pn_Cmp_Ge;
+ case EXPR_BINARY_ISUNORDERED: return pn_Cmp_Uo;
+
+ default:
+ break;
+ }
+ panic("trying to get pn_Cmp from non-comparison binexpr type");
+}
+
+/**
+ * Handle the assume optimizer hint: check if a Confirm
+ * node can be created.
+ *
+ * @param dbi debug info
+ * @param expr the IL assume expression
+ *
+ * we support here only some simple cases:
+ * - var rel const
+ * - const rel val
+ * - var rel var
+ */
+static ir_node *handle_assume_compare(dbg_info *dbi,
+ const binary_expression_t *expression)
+{
+ expression_t *op1 = expression->left;
+ expression_t *op2 = expression->right;
+ entity_t *var2, *var = NULL;
+ ir_node *res = NULL;
+ pn_Cmp cmp_val;
+
+ cmp_val = get_pnc(expression->base.kind, op1->base.type);
+
+ if (is_local_variable(op1) && is_local_variable(op2)) {
+ var = op1->reference.entity;
+ var2 = op2->reference.entity;
+
+ type_t *const type = skip_typeref(var->declaration.type);
+ ir_mode *const mode = get_ir_mode_storage(type);
+
+ ir_node *const irn1 = get_value(var->variable.v.value_number, mode);
+ ir_node *const irn2 = get_value(var2->variable.v.value_number, mode);
+
+ res = new_d_Confirm(dbi, irn2, irn1, get_inversed_pnc(cmp_val));
+ set_value(var2->variable.v.value_number, res);
+
+ res = new_d_Confirm(dbi, irn1, irn2, cmp_val);
+ set_value(var->variable.v.value_number, res);
+
+ return res;
+ }
+
+ expression_t *con;
+ if (is_local_variable(op1) && is_constant_expression(op2)) {
+ var = op1->reference.entity;
+ con = op2;
+ } else if (is_constant_expression(op1) && is_local_variable(op2)) {
+ cmp_val = get_inversed_pnc(cmp_val);
+ var = op2->reference.entity;
+ con = op1;
+ }
+
+ if (var != NULL) {
+ type_t *const type = skip_typeref(var->declaration.type);
+ ir_mode *const mode = get_ir_mode_storage(type);
+
+ res = get_value(var->variable.v.value_number, mode);
+ res = new_d_Confirm(dbi, res, expression_to_firm(con), cmp_val);
+ set_value(var->variable.v.value_number, res);
+ }
+ return res;
+}
+
+/**
+ * Handle the assume optimizer hint.
+ *
+ * @param dbi debug info
+ * @param expr the IL assume expression
+ */
+static ir_node *handle_assume(dbg_info *dbi, const expression_t *expression)
+{
+ switch(expression->kind) {
+ case EXPR_BINARY_EQUAL:
+ case EXPR_BINARY_NOTEQUAL:
+ case EXPR_BINARY_LESS:
+ case EXPR_BINARY_LESSEQUAL:
+ case EXPR_BINARY_GREATER:
+ case EXPR_BINARY_GREATEREQUAL:
+ return handle_assume_compare(dbi, &expression->binary);
+ default:
+ return NULL;
+ }
+}
+
+static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
+{
+ dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
+ type_t *type = skip_typeref(expression->base.type);
+
+ if (expression->base.kind == EXPR_UNARY_TAKE_ADDRESS)
+ return expression_to_addr(expression->value);
+
+ const expression_t *value = expression->value;
+
+ switch(expression->base.kind) {
+ case EXPR_UNARY_NEGATE: {
+ ir_node *value_node = expression_to_firm(value);
+ ir_mode *mode = get_ir_mode_arithmetic(type);
+ return new_d_Minus(dbgi, value_node, mode);
+ }
+ case EXPR_UNARY_PLUS:
+ return expression_to_firm(value);
+ case EXPR_UNARY_BITWISE_NEGATE: {
+ ir_node *value_node = expression_to_firm(value);
+ ir_mode *mode = get_ir_mode_arithmetic(type);
+ return new_d_Not(dbgi, value_node, mode);
+ }
+ case EXPR_UNARY_NOT: {
+ ir_node *value_node = _expression_to_firm(value);
+ value_node = create_conv(dbgi, value_node, mode_b);
+ ir_node *res = new_d_Not(dbgi, value_node, mode_b);
+ return res;
+ }
+ case EXPR_UNARY_DEREFERENCE: {
+ ir_node *value_node = expression_to_firm(value);
+ type_t *value_type = skip_typeref(value->base.type);
+ assert(is_type_pointer(value_type));
+
+ /* check for __based */
+ const variable_t *const base_var = value_type->pointer.base_variable;
+ if (base_var != NULL) {
+ ir_node *const addr = get_global_var_address(dbgi, base_var);
+ ir_node *const base = deref_address(dbgi, base_var->base.type, addr);
+ value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(value_type));
+ }
+ type_t *points_to = value_type->pointer.points_to;
+ return deref_address(dbgi, points_to, value_node);
+ }
+ case EXPR_UNARY_POSTFIX_INCREMENT:
+ case EXPR_UNARY_POSTFIX_DECREMENT:
+ case EXPR_UNARY_PREFIX_INCREMENT:
+ case EXPR_UNARY_PREFIX_DECREMENT:
+ return create_incdec(expression);
+ case EXPR_UNARY_CAST: {
+ ir_node *value_node = expression_to_firm(value);
+ if (is_type_scalar(type)) {
+ ir_mode *mode = get_ir_mode_storage(type);
+ type_t *from_type = skip_typeref(value->base.type);
+ /* check for conversion from / to __based types */
+ if (is_type_pointer(type) && is_type_pointer(from_type)) {
+ const variable_t *from_var = from_type->pointer.base_variable;
+ const variable_t *to_var = type->pointer.base_variable;
+ if (from_var != to_var) {
+ if (from_var != NULL) {
+ ir_node *const addr = get_global_var_address(dbgi, from_var);
+ ir_node *const base = deref_address(dbgi, from_var->base.type, addr);
+ value_node = new_d_Add(dbgi, value_node, base, get_ir_mode_storage(from_type));
+ }
+ if (to_var != NULL) {
+ ir_node *const addr = get_global_var_address(dbgi, to_var);
+ ir_node *const base = deref_address(dbgi, to_var->base.type, addr);
+ value_node = new_d_Sub(dbgi, value_node, base, mode);
+ }
+ }
+ }
+ ir_node *node = create_conv(dbgi, value_node, mode);
+ node = do_strict_conv(dbgi, node);
+ ir_mode *mode_arith = get_ir_mode_arithmetic(type);
+ node = create_conv(dbgi, node, mode_arith);
+ return node;
+ } else {
+ /* make sure firm type is constructed */
+ (void) get_ir_type(type);
+ return value_node;
+ }
+ }
+ case EXPR_UNARY_CAST_IMPLICIT: {
+ ir_node *value_node = expression_to_firm(value);
+ if (is_type_scalar(type)) {
+ ir_mode *mode = get_ir_mode_storage(type);
+ ir_node *res = create_conv(dbgi, value_node, mode);
+ ir_mode *mode_arith = get_ir_mode_arithmetic(type);
+ res = create_conv(dbgi, res, mode_arith);
+ return res;
+ } else {
+ return value_node;
+ }
+ }
+ case EXPR_UNARY_ASSUME:
+ if (firm_opt.confirm)
+ return handle_assume(dbgi, value);
+ else
+ return NULL;
+
+ default:
+ break;
+ }
+ panic("invalid UNEXPR type found");
+}
+
+/**
+ * produces a 0/1 depending of the value of a mode_b node
+ */
+static ir_node *produce_condition_result(const expression_t *expression,
+ ir_mode *mode, dbg_info *dbgi)
+{
+ ir_node *cur_block = get_cur_block();
+
+ ir_node *one_block = new_immBlock();
+ set_cur_block(one_block);
+ ir_node *one = new_Const(get_mode_one(mode));
+ ir_node *jmp_one = new_d_Jmp(dbgi);
+
+ ir_node *zero_block = new_immBlock();
+ set_cur_block(zero_block);
+ ir_node *zero = new_Const(get_mode_null(mode));
+ ir_node *jmp_zero = new_d_Jmp(dbgi);
+
+ set_cur_block(cur_block);
+ create_condition_evaluation(expression, one_block, zero_block);
+ mature_immBlock(one_block);
+ mature_immBlock(zero_block);
+
+ ir_node *in_cf[2] = { jmp_one, jmp_zero };
+ new_Block(2, in_cf);
+
+ ir_node *in[2] = { one, zero };
+ ir_node *val = new_d_Phi(dbgi, 2, in, mode);
+
+ return val;
+}
+
+static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
+ ir_node *value, type_t *type)
+{
+ ir_mode *const mode = get_ir_mode_arithmetic(type_ptrdiff_t);
+ assert(is_type_pointer(type));
+ pointer_type_t *const pointer_type = &type->pointer;
+ type_t *const points_to = skip_typeref(pointer_type->points_to);
+ ir_node * elem_size = get_type_size_node(points_to);
+ elem_size = create_conv(dbgi, elem_size, mode);
+ value = create_conv(dbgi, value, mode);
+ ir_node *const mul = new_d_Mul(dbgi, value, elem_size, mode);
+ return mul;
+}
+
+static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
+ ir_node *left, ir_node *right)
+{
+ ir_mode *mode;
+ type_t *type_left = skip_typeref(expression->left->base.type);
+ type_t *type_right = skip_typeref(expression->right->base.type);
+
+ expression_kind_t kind = expression->base.kind;
+
+ switch (kind) {
+ case EXPR_BINARY_SHIFTLEFT:
+ case EXPR_BINARY_SHIFTRIGHT:
+ case EXPR_BINARY_SHIFTLEFT_ASSIGN:
+ case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
+ mode = get_irn_mode(left);
+ right = create_conv(dbgi, right, mode_uint);
+ break;
+
+ case EXPR_BINARY_SUB:
+ if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
+ const pointer_type_t *const ptr_type = &type_left->pointer;
+
+ mode = get_ir_mode_arithmetic(expression->base.type);
+ ir_node *const elem_size = get_type_size_node(ptr_type->points_to);
+ ir_node *const conv_size = new_d_Conv(dbgi, elem_size, mode);
+ ir_node *const sub = new_d_Sub(dbgi, left, right, mode);
+ ir_node *const no_mem = new_NoMem();
+ ir_node *const div = new_d_DivRL(dbgi, no_mem, sub, conv_size,
+ mode, op_pin_state_floats);
+ return new_d_Proj(dbgi, div, mode, pn_Div_res);
+ }
+ /* fallthrough */
+ case EXPR_BINARY_SUB_ASSIGN:
+ if (is_type_pointer(type_left)) {
+ right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
+ mode = get_ir_mode_arithmetic(type_left);
+ break;
+ }
+ goto normal_node;
+
+ case EXPR_BINARY_ADD:
+ case EXPR_BINARY_ADD_ASSIGN:
+ if (is_type_pointer(type_left)) {
+ right = adjust_for_pointer_arithmetic(dbgi, right, type_left);
+ mode = get_ir_mode_arithmetic(type_left);
+ break;
+ } else if (is_type_pointer(type_right)) {
+ left = adjust_for_pointer_arithmetic(dbgi, left, type_right);
+ mode = get_ir_mode_arithmetic(type_right);
+ break;
+ }
+ goto normal_node;
+
+ default:
+normal_node:
+ mode = get_ir_mode_arithmetic(type_right);
+ left = create_conv(dbgi, left, mode);
+ break;
+ }
+
+ switch (kind) {
+ case EXPR_BINARY_ADD_ASSIGN:
+ case EXPR_BINARY_ADD:
+ return new_d_Add(dbgi, left, right, mode);
+ case EXPR_BINARY_SUB_ASSIGN:
+ case EXPR_BINARY_SUB:
+ return new_d_Sub(dbgi, left, right, mode);
+ case EXPR_BINARY_MUL_ASSIGN:
+ case EXPR_BINARY_MUL:
+ return new_d_Mul(dbgi, left, right, mode);
+ case EXPR_BINARY_BITWISE_AND:
+ case EXPR_BINARY_BITWISE_AND_ASSIGN:
+ return new_d_And(dbgi, left, right, mode);
+ case EXPR_BINARY_BITWISE_OR:
+ case EXPR_BINARY_BITWISE_OR_ASSIGN:
+ return new_d_Or(dbgi, left, right, mode);
+ case EXPR_BINARY_BITWISE_XOR:
+ case EXPR_BINARY_BITWISE_XOR_ASSIGN:
+ return new_d_Eor(dbgi, left, right, mode);
+ case EXPR_BINARY_SHIFTLEFT:
+ case EXPR_BINARY_SHIFTLEFT_ASSIGN:
+ return new_d_Shl(dbgi, left, right, mode);
+ case EXPR_BINARY_SHIFTRIGHT:
+ case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
+ if (mode_is_signed(mode)) {
+ return new_d_Shrs(dbgi, left, right, mode);
+ } else {
+ return new_d_Shr(dbgi, left, right, mode);
+ }
+ case EXPR_BINARY_DIV:
+ case EXPR_BINARY_DIV_ASSIGN: {
+ ir_node *pin = new_Pin(new_NoMem());
+ ir_node *op;
+ ir_node *res;
+ if (mode_is_float(mode)) {
+ op = new_d_Quot(dbgi, pin, left, right, mode, op_pin_state_floats);
+ res = new_d_Proj(dbgi, op, mode, pn_Quot_res);
+ } else {
+ op = new_d_Div(dbgi, pin, left, right, mode, op_pin_state_floats);
+ res = new_d_Proj(dbgi, op, mode, pn_Div_res);
+ }
+ return res;
+ }
+ case EXPR_BINARY_MOD:
+ case EXPR_BINARY_MOD_ASSIGN: {
+ ir_node *pin = new_Pin(new_NoMem());
+ assert(!mode_is_float(mode));
+ ir_node *op = new_d_Mod(dbgi, pin, left, right, mode,
+ op_pin_state_floats);
+ ir_node *res = new_d_Proj(dbgi, op, mode, pn_Mod_res);
+ return res;
+ }
+ default:
+ panic("unexpected expression kind");
+ }
+}
+
+static ir_node *create_lazy_op(const binary_expression_t *expression)
+{
+ dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
+ type_t *type = skip_typeref(expression->base.type);
+ ir_mode *mode = get_ir_mode_arithmetic(type);
+
+ if (is_constant_expression(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) {
+ return new_Const(get_mode_null(mode));
+ }
+ } else {
+ if (val) {
+ return new_Const(get_mode_one(mode));
+ }
+ }
+
+ if (is_constant_expression(expression->right)) {
+ bool valr = fold_constant_to_bool(expression->right);
+ return valr ?
+ new_Const(get_mode_one(mode)) :
+ new_Const(get_mode_null(mode));
+ }
+
+ return produce_condition_result(expression->right, mode, dbgi);
+ }
+
+ return produce_condition_result((const expression_t*) expression, mode,
+ dbgi);
+}
+
+typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
+ ir_node *right, ir_mode *mode);
+
+static ir_node *create_assign_binop(const binary_expression_t *expression)
+{
+ dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
+ const expression_t *left_expr = expression->left;
+ type_t *type = skip_typeref(left_expr->base.type);
+ ir_mode *left_mode = get_ir_mode_storage(type);
+ ir_node *right = expression_to_firm(expression->right);
+ ir_node *left_addr = expression_to_addr(left_expr);
+ ir_node *left = get_value_from_lvalue(left_expr, left_addr);
+ ir_node *result = create_op(dbgi, expression, left, right);
+
+ result = create_conv(dbgi, result, left_mode);
+ result = do_strict_conv(dbgi, result);
+
+ result = set_value_for_expression_addr(left_expr, result, left_addr);
+
+ if (!is_type_compound(type)) {
+ ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
+ result = create_conv(dbgi, result, mode_arithmetic);
+ }
+ return result;
+}
+
+static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
+{
+ expression_kind_t kind = expression->base.kind;
+
+ switch(kind) {
+ case EXPR_BINARY_EQUAL:
+ case EXPR_BINARY_NOTEQUAL:
+ case EXPR_BINARY_LESS:
+ case EXPR_BINARY_LESSEQUAL:
+ case EXPR_BINARY_GREATER:
+ case EXPR_BINARY_GREATEREQUAL:
+ case EXPR_BINARY_ISGREATER:
+ case EXPR_BINARY_ISGREATEREQUAL:
+ case EXPR_BINARY_ISLESS:
+ case EXPR_BINARY_ISLESSEQUAL:
+ case EXPR_BINARY_ISLESSGREATER:
+ case EXPR_BINARY_ISUNORDERED: {
+ dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
+ ir_node *left = expression_to_firm(expression->left);
+ ir_node *right = expression_to_firm(expression->right);
+ ir_node *cmp = new_d_Cmp(dbgi, left, right);
+ long pnc = get_pnc(kind, expression->left->base.type);
+ ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pnc);
+ return proj;
+ }
+ case EXPR_BINARY_ASSIGN: {
+ ir_node *addr = expression_to_addr(expression->left);
+ ir_node *right = expression_to_firm(expression->right);
+ ir_node *res
+ = set_value_for_expression_addr(expression->left, right, addr);
+
+ type_t *type = skip_typeref(expression->base.type);
+ if (!is_type_compound(type)) {
+ ir_mode *mode_arithmetic = get_ir_mode_arithmetic(type);
+ res = create_conv(NULL, res, mode_arithmetic);
+ }
+ return res;
+ }
+ case EXPR_BINARY_ADD:
+ case EXPR_BINARY_SUB:
+ case EXPR_BINARY_MUL:
+ case EXPR_BINARY_DIV:
+ case EXPR_BINARY_MOD:
+ case EXPR_BINARY_BITWISE_AND:
+ case EXPR_BINARY_BITWISE_OR:
+ case EXPR_BINARY_BITWISE_XOR:
+ case EXPR_BINARY_SHIFTLEFT:
+ case EXPR_BINARY_SHIFTRIGHT:
+ {
+ dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
+ ir_node *left = expression_to_firm(expression->left);
+ ir_node *right = expression_to_firm(expression->right);
+ return create_op(dbgi, expression, left, right);
+ }
+ case EXPR_BINARY_LOGICAL_AND:
+ case EXPR_BINARY_LOGICAL_OR:
+ return create_lazy_op(expression);
+ case EXPR_BINARY_COMMA:
+ /* create side effects of left side */
+ (void) expression_to_firm(expression->left);
+ return _expression_to_firm(expression->right);
+
+ case EXPR_BINARY_ADD_ASSIGN:
+ case EXPR_BINARY_SUB_ASSIGN:
+ case EXPR_BINARY_MUL_ASSIGN:
+ case EXPR_BINARY_MOD_ASSIGN:
+ case EXPR_BINARY_DIV_ASSIGN:
+ case EXPR_BINARY_BITWISE_AND_ASSIGN:
+ case EXPR_BINARY_BITWISE_OR_ASSIGN:
+ case EXPR_BINARY_BITWISE_XOR_ASSIGN:
+ case EXPR_BINARY_SHIFTLEFT_ASSIGN:
+ case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
+ return create_assign_binop(expression);
+ default:
+ panic("TODO binexpr type");
+ }
+}
+
+static ir_node *array_access_addr(const array_access_expression_t *expression)
+{
+ dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
+ ir_node *base_addr = expression_to_firm(expression->array_ref);
+ ir_node *offset = expression_to_firm(expression->index);
+ type_t *ref_type = skip_typeref(expression->array_ref->base.type);
+ ir_node *real_offset = adjust_for_pointer_arithmetic(dbgi, offset, ref_type);
+ ir_node *result = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
+
+ return result;
+}
+
+static ir_node *array_access_to_firm(
+ const array_access_expression_t *expression)
+{
+ dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
+ ir_node *addr = array_access_addr(expression);
+ type_t *type = revert_automatic_type_conversion(
+ (const expression_t*) expression);
+ type = skip_typeref(type);
+
+ return deref_address(dbgi, type, addr);
+}
+
+static long get_offsetof_offset(const offsetof_expression_t *expression)
+{
+ type_t *orig_type = expression->type;
+ long offset = 0;
+
+ designator_t *designator = expression->designator;
+ for ( ; designator != NULL; designator = designator->next) {
+ type_t *type = skip_typeref(orig_type);
+ /* be sure the type is constructed */
+ (void) get_ir_type(type);
+
+ if (designator->symbol != NULL) {
+ assert(is_type_compound(type));
+ symbol_t *symbol = designator->symbol;
+
+ compound_t *compound = type->compound.compound;
+ entity_t *iter = compound->members.entities;
+ for ( ; iter != NULL; iter = iter->base.next) {
+ if (iter->base.symbol == symbol) {
+ break;
+ }
+ }
+ assert(iter != NULL);
+
+ assert(iter->kind == ENTITY_COMPOUND_MEMBER);
+ assert(iter->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
+ offset += get_entity_offset(iter->compound_member.entity);
+
+ orig_type = iter->declaration.type;
+ } else {
+ expression_t *array_index = designator->array_index;
+ assert(designator->array_index != NULL);
+ assert(is_type_array(type));
+
+ 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);
+
+ offset += index * elem_size;
+
+ orig_type = type->array.element_type;
+ }
+ }
+
+ return offset;
+}
+
+static ir_node *offsetof_to_firm(const offsetof_expression_t *expression)
+{
+ ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
+ long offset = get_offsetof_offset(expression);
+ tarval *tv = new_tarval_from_long(offset, mode);
+ dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
+
+ return new_d_Const(dbgi, tv);
+}
+
+static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
+ ir_entity *entity, type_t *type);
+
+static ir_node *compound_literal_to_firm(
+ const compound_literal_expression_t *expression)
+{
+ type_t *type = expression->type;
+
+ /* create an entity on the stack */
+ ir_type *frame_type = get_irg_frame_type(current_ir_graph);
+
+ ident *const id = id_unique("CompLit.%u");
+ ir_type *const irtype = get_ir_type(type);
+ dbg_info *const dbgi = get_dbg_info(&expression->base.source_position);
+ ir_entity *const entity = new_d_entity(frame_type, id, irtype, dbgi);
+ set_entity_ld_ident(entity, id);
+
+ set_entity_variability(entity, variability_uninitialized);
+
+ /* create initialisation code */
+ initializer_t *initializer = expression->initializer;
+ create_local_initializer(initializer, dbgi, entity, type);
+
+ /* create a sel for the compound literal address */
+ ir_node *frame = get_irg_frame(current_ir_graph);
+ ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
+ return sel;
+}
+
+/**
+ * Transform a sizeof expression into Firm code.
+ */
+static ir_node *sizeof_to_firm(const typeprop_expression_t *expression)
+{
+ type_t *type = expression->type;
+ if (type == NULL) {
+ type = expression->tp_expression->base.type;
+ assert(type != NULL);
+ }
+
+ type = skip_typeref(type);
+ /* §6.5.3.4:2 if the type is a VLA, evaluate the expression. */
+ if (is_type_array(type) && type->array.is_vla
+ && expression->tp_expression != NULL) {
+ expression_to_firm(expression->tp_expression);
+ }
+
+ return get_type_size_node(type);
+}
+
+static entity_t *get_expression_entity(const expression_t *expression)
+{
+ if (expression->kind != EXPR_REFERENCE)
+ return NULL;
+
+ return expression->reference.entity;
+}
+
+static unsigned get_cparser_entity_alignment(const entity_t *entity)
+{
+ switch(entity->kind) {
+ DECLARATION_KIND_CASES
+ return entity->declaration.alignment;
+ case ENTITY_STRUCT:
+ case ENTITY_UNION:
+ return entity->compound.alignment;
+ case ENTITY_TYPEDEF:
+ return entity->typedefe.alignment;
+ default:
+ break;
+ }
+ return 0;
+}
+
+/**
+ * Transform an alignof expression into Firm code.
+ */
+static ir_node *alignof_to_firm(const typeprop_expression_t *expression)
+{
+ unsigned alignment = 0;
+
+ const expression_t *tp_expression = expression->tp_expression;
+ if (tp_expression != NULL) {
+ entity_t *entity = get_expression_entity(tp_expression);
+ if (entity != NULL) {
+ alignment = get_cparser_entity_alignment(entity);
+ }
+ }
+
+ if (alignment == 0) {
+ type_t *type = expression->type;
+ alignment = get_type_alignment(type);
+ }
+
+ dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
+ ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
+ tarval *tv = new_tarval_from_long(alignment, mode);
+ return new_d_Const(dbgi, tv);
+}
+
+static void init_ir_types(void);
+
+static tarval *fold_constant_to_tarval(const expression_t *expression)
+{
+ assert(is_type_valid(skip_typeref(expression->base.type)));
+
+ bool constant_folding_old = constant_folding;
+ constant_folding = true;
+
+ init_ir_types();
+
+ assert(is_constant_expression(expression));
+
+ ir_graph *old_current_ir_graph = current_ir_graph;
+ current_ir_graph = get_const_code_irg();
+
+ ir_node *cnst = expression_to_firm(expression);
+ current_ir_graph = old_current_ir_graph;
+
+ if (!is_Const(cnst)) {
+ 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");
+ }
+
+ 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)) {
+ bool val = fold_constant_to_bool(expression->condition);
+ if (val) {
+ expression_t *true_expression = expression->true_expression;
+ if (true_expression == NULL)
+ true_expression = expression->condition;
+ return expression_to_firm(true_expression);
+ } else {
+ return expression_to_firm(expression->false_expression);
+ }
+ }
+
+ ir_node *cur_block = get_cur_block();
+
+ /* create the true block */
+ ir_node *true_block = new_immBlock();
+ set_cur_block(true_block);
+
+ ir_node *true_val = expression->true_expression != NULL ?
+ expression_to_firm(expression->true_expression) : NULL;
+ ir_node *true_jmp = new_Jmp();
+
+ /* create the false block */
+ ir_node *false_block = new_immBlock();
+ set_cur_block(false_block);
+
+ ir_node *false_val = expression_to_firm(expression->false_expression);
+ ir_node *false_jmp = new_Jmp();
+
+ /* create the condition evaluation */
+ set_cur_block(cur_block);
+ ir_node *const cond_expr = create_condition_evaluation(expression->condition, true_block, false_block);
+ if (expression->true_expression == NULL) {
+ if (cond_expr != NULL && get_irn_mode(cond_expr) != mode_b) {
+ true_val = cond_expr;
+ } else {
+ /* Condition ended with a short circuit (&&, ||, !) operation or a
+ * comparison. Generate a "1" as value for the true branch. */
+ true_val = new_Const(get_mode_one(mode_Is));
+ }
+ }
+ mature_immBlock(true_block);
+ mature_immBlock(false_block);
+
+ /* create the common block */
+ ir_node *in_cf[2] = { true_jmp, false_jmp };
+ new_Block(2, in_cf);
+
+ /* TODO improve static semantics, so either both or no values are NULL */
+ if (true_val == NULL || false_val == NULL)
+ return NULL;
+
+ ir_node *in[2] = { true_val, false_val };
+ ir_mode *mode = get_irn_mode(true_val);
+ assert(get_irn_mode(false_val) == mode);
+ ir_node *val = new_d_Phi(dbgi, 2, in, mode);
+
+ return val;
+}
+
+/**
+ * Returns an IR-node representing the address of a field.
+ */
+static ir_node *select_addr(const select_expression_t *expression)
+{
+ dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
+
+ construct_select_compound(expression);
+
+ ir_node *compound_addr = expression_to_firm(expression->compound);
+
+ entity_t *entry = expression->compound_entry;
+ assert(entry->kind == ENTITY_COMPOUND_MEMBER);
+ assert(entry->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
+
+ if (constant_folding) {
+ ir_mode *mode = get_irn_mode(compound_addr);
+ /* FIXME: here, we need an integer mode with the same number of bits as mode */
+ ir_node *ofs = new_Const_long(mode_uint, entry->compound_member.offset);
+ return new_d_Add(dbgi, compound_addr, ofs, mode);
+ } else {
+ ir_entity *irentity = entry->compound_member.entity;
+ assert(irentity != NULL);
+ return new_d_simpleSel(dbgi, new_NoMem(), compound_addr, irentity);
+ }
+}
+
+static ir_node *select_to_firm(const select_expression_t *expression)
+{
+ dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
+ ir_node *addr = select_addr(expression);
+ type_t *type = revert_automatic_type_conversion(
+ (const expression_t*) expression);
+ type = skip_typeref(type);
+
+ entity_t *entry = expression->compound_entry;
+ assert(entry->kind == ENTITY_COMPOUND_MEMBER);
+ type_t *entry_type = skip_typeref(entry->declaration.type);
+
+ if (entry_type->kind == TYPE_BITFIELD) {
+ return bitfield_extract_to_firm(expression, addr);
+ }
+
+ return deref_address(dbgi, type, addr);
+}
+
+/* Values returned by __builtin_classify_type. */
+typedef enum gcc_type_class
+{
+ no_type_class = -1,
+ void_type_class,
+ integer_type_class,
+ char_type_class,
+ enumeral_type_class,
+ boolean_type_class,
+ pointer_type_class,
+ reference_type_class,
+ offset_type_class,
+ real_type_class,
+ complex_type_class,
+ function_type_class,
+ method_type_class,
+ record_type_class,
+ union_type_class,
+ array_type_class,
+ string_type_class,
+ set_type_class,
+ file_type_class,
+ lang_type_class
+} gcc_type_class;
+
+static ir_node *classify_type_to_firm(const classify_type_expression_t *const expr)
+{
+ type_t *type = expr->type_expression->base.type;
+
+ /* FIXME gcc returns different values depending on whether compiling C or C++
+ * e.g. int x[10] is pointer_type_class in C, but array_type_class in C++ */
+ gcc_type_class tc;
+ for (;;) {
+ type = skip_typeref(type);
+ switch (type->kind) {
+ case TYPE_ATOMIC: {
+ const atomic_type_t *const atomic_type = &type->atomic;
+ switch (atomic_type->akind) {
+ /* should not be reached */
+ case ATOMIC_TYPE_INVALID:
+ tc = no_type_class;
+ goto make_const;
+
+ /* gcc cannot do that */
+ case ATOMIC_TYPE_VOID:
+ tc = void_type_class;
+ goto make_const;
+
+ case ATOMIC_TYPE_WCHAR_T: /* gcc handles this as integer */
+ case ATOMIC_TYPE_CHAR: /* gcc handles this as integer */
+ case ATOMIC_TYPE_SCHAR: /* gcc handles this as integer */
+ case ATOMIC_TYPE_UCHAR: /* gcc handles this as integer */
+ case ATOMIC_TYPE_SHORT:
+ case ATOMIC_TYPE_USHORT:
+ case ATOMIC_TYPE_INT:
+ case ATOMIC_TYPE_UINT:
+ case ATOMIC_TYPE_LONG:
+ case ATOMIC_TYPE_ULONG:
+ case ATOMIC_TYPE_LONGLONG:
+ case ATOMIC_TYPE_ULONGLONG:
+ case ATOMIC_TYPE_BOOL: /* gcc handles this as integer */
+ tc = integer_type_class;
+ goto make_const;
+
+ case ATOMIC_TYPE_FLOAT:
+ case ATOMIC_TYPE_DOUBLE:
+ case ATOMIC_TYPE_LONG_DOUBLE:
+ tc = real_type_class;
+ goto make_const;
+ }
+ panic("Unexpected atomic type in classify_type_to_firm().");
+ }
+
+ case TYPE_COMPLEX: tc = complex_type_class; goto make_const;
+ case TYPE_IMAGINARY: tc = complex_type_class; goto make_const;
+ case TYPE_BITFIELD: tc = integer_type_class; goto make_const;
+ case TYPE_ARRAY: /* gcc handles this as pointer */
+ case TYPE_FUNCTION: /* gcc handles this as pointer */
+ case TYPE_POINTER: tc = pointer_type_class; goto make_const;
+ case TYPE_COMPOUND_STRUCT: tc = record_type_class; goto make_const;
+ case TYPE_COMPOUND_UNION: tc = union_type_class; goto make_const;
+
+ /* gcc handles this as integer */
+ case TYPE_ENUM: tc = integer_type_class; goto make_const;
+
+ /* gcc classifies the referenced type */
+ case TYPE_REFERENCE: type = type->reference.refers_to; continue;
+
+ case TYPE_BUILTIN:
+ /* typedef/typeof should be skipped already */
+ case TYPE_TYPEDEF:
+ case TYPE_TYPEOF:
+ case TYPE_INVALID:
+ case TYPE_ERROR:
+ break;
+ }
+ panic("unexpected TYPE classify_type_to_firm().");
+ }
+
+make_const:;
+ dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
+ tarval *const tv = new_tarval_from_long(tc, mode_int);
+ return new_d_Const(dbgi, tv);
+}
+
+static ir_node *function_name_to_firm(
+ const funcname_expression_t *const expr)
+{
+ switch(expr->kind) {
+ case FUNCNAME_FUNCTION:
+ case FUNCNAME_PRETTY_FUNCTION:
+ case FUNCNAME_FUNCDNAME:
+ if (current_function_name == NULL) {
+ const source_position_t *const src_pos = &expr->base.source_position;
+ const char *name = current_function_entity->base.symbol->string;
+ const string_t string = { name, strlen(name) + 1 };
+ current_function_name = string_to_firm(src_pos, "__func__.%u", &string);
+ }
+ return current_function_name;
+ case FUNCNAME_FUNCSIG:
+ if (current_funcsig == NULL) {
+ const source_position_t *const src_pos = &expr->base.source_position;
+ ir_entity *ent = get_irg_entity(current_ir_graph);
+ const char *const name = get_entity_ld_name(ent);
+ const string_t string = { name, strlen(name) + 1 };
+ current_funcsig = string_to_firm(src_pos, "__FUNCSIG__.%u", &string);
+ }
+ return current_funcsig;
+ }
+ panic("Unsupported function name");
+}
+
+static ir_node *statement_expression_to_firm(const statement_expression_t *expr)
+{
+ statement_t *statement = expr->statement;
+
+ assert(statement->kind == STATEMENT_COMPOUND);
+ return compound_statement_to_firm(&statement->compound);
+}
+
+static ir_node *va_start_expression_to_firm(
+ const va_start_expression_t *const expr)
+{
+ type_t *const type = current_function_entity->declaration.type;
+ ir_type *const method_type = get_ir_type(type);
+ int const n = get_method_n_params(method_type) - 1;
+ ir_entity *const parm_ent = get_method_value_param_ent(method_type, n);
+ ir_node *const frame = get_irg_frame(current_ir_graph);
+ dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
+ ir_node *const no_mem = new_NoMem();
+ ir_node *const arg_sel =
+ new_d_simpleSel(dbgi, no_mem, frame, parm_ent);
+
+ type_t *const param_type = expr->parameter->base.type;
+ ir_node *const cnst = get_type_size_node(param_type);
+ ir_mode *const mode = get_irn_mode(cnst);
+ ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
+ ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
+ ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
+ ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
+ ir_node *const add = new_d_Add(dbgi, arg_sel, c4, mode_P_data);
+ set_value_for_expression(expr->ap, add);
+
+ return NULL;
+}
+
+static ir_node *va_arg_expression_to_firm(const va_arg_expression_t *const expr)
+{
+ type_t *const type = expr->base.type;
+ expression_t *const ap_expr = expr->ap;
+ ir_node *const ap_addr = expression_to_addr(ap_expr);
+ ir_node *const ap = get_value_from_lvalue(ap_expr, ap_addr);
+ dbg_info *const dbgi = get_dbg_info(&expr->base.source_position);
+ ir_node *const res = deref_address(dbgi, type, ap);
+
+ ir_node *const cnst = get_type_size_node(expr->base.type);
+ ir_mode *const mode = get_irn_mode(cnst);
+ ir_node *const c1 = new_Const_long(mode, stack_param_align - 1);
+ ir_node *const c2 = new_d_Add(dbgi, cnst, c1, mode);
+ ir_node *const c3 = new_Const_long(mode, -(long)stack_param_align);
+ ir_node *const c4 = new_d_And(dbgi, c2, c3, mode);
+ ir_node *const add = new_d_Add(dbgi, ap, c4, mode_P_data);
+
+ set_value_for_expression_addr(ap_expr, add, ap_addr);
+
+ return res;
+}
+
+/**
+ * Generate Firm for a va_copy expression.
+ */
+static ir_node *va_copy_expression_to_firm(const va_copy_expression_t *const expr)
+{
+ ir_node *const src = expression_to_firm(expr->src);
+ set_value_for_expression(expr->dst, src);
+ return NULL;
+}
+
+static ir_node *dereference_addr(const unary_expression_t *const expression)
+{
+ assert(expression->base.kind == EXPR_UNARY_DEREFERENCE);
+ return expression_to_firm(expression->value);
+}
+
+/**
+ * Returns a IR-node representing an lvalue of the given expression.
+ */
+static ir_node *expression_to_addr(const expression_t *expression)
+{
+ switch(expression->kind) {
+ case EXPR_ARRAY_ACCESS:
+ return array_access_addr(&expression->array_access);
+ case EXPR_CALL:
+ return call_expression_to_firm(&expression->call);
+ case EXPR_COMPOUND_LITERAL:
+ return compound_literal_to_firm(&expression->compound_literal);
+ case EXPR_REFERENCE:
+ return reference_addr(&expression->reference);
+ case EXPR_SELECT:
+ return select_addr(&expression->select);
+ case EXPR_UNARY_DEREFERENCE:
+ return dereference_addr(&expression->unary);
+ default:
+ break;
+ }
+ panic("trying to get address of non-lvalue");
+}
+
+static ir_node *builtin_constant_to_firm(
+ const builtin_constant_expression_t *expression)
+{
+ ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
+ long v;
+
+ if (is_constant_expression(expression->value)) {
+ v = 1;
+ } else {
+ v = 0;
+ }
+ return new_Const_long(mode, v);
+}
+
+static ir_node *builtin_types_compatible_to_firm(
+ const builtin_types_compatible_expression_t *expression)
+{
+ type_t *const left = get_unqualified_type(skip_typeref(expression->left));
+ type_t *const right = get_unqualified_type(skip_typeref(expression->right));
+ long const value = types_compatible(left, right) ? 1 : 0;
+ ir_mode *const mode = get_ir_mode_arithmetic(expression->base.type);
+ return new_Const_long(mode, value);
+}
+
+static ir_node *get_label_block(label_t *label)
+{
+ if (label->block != NULL)
+ return label->block;
+
+ /* beware: might be called from create initializer with current_ir_graph
+ * set to const_code_irg. */
+ ir_graph *rem = current_ir_graph;
+ current_ir_graph = current_function;
+
+ ir_node *block = new_immBlock();
+
+ label->block = block;
+
+ ARR_APP1(label_t *, all_labels, label);
+
+ current_ir_graph = rem;
+ return block;
+}
+
+/**
+ * Pointer to a label. This is used for the
+ * GNU address-of-label extension.
+ */
+static ir_node *label_address_to_firm(
+ const label_address_expression_t *label)
+{
+ ir_node *block = get_label_block(label->label);
+ ir_label_t nr = get_Block_label(block);
+
+ if (nr == 0) {
+ nr = get_irp_next_label_nr();
+ set_Block_label(block, nr);
+ }
+ symconst_symbol value;
+ value.label = nr;
+ return new_SymConst(mode_P_code, value, symconst_label);
+}
+
+/**
+ * creates firm nodes for an expression. The difference between this function
+ * and expression_to_firm is, that this version might produce mode_b nodes
+ * instead of mode_Is.
+ */
+static ir_node *_expression_to_firm(const expression_t *expression)
+{
+#ifndef NDEBUG
+ if (!constant_folding) {
+ assert(!expression->base.transformed);
+ ((expression_t*) expression)->base.transformed = true;
+ }
+#endif
+
+ switch (expression->kind) {
+ case EXPR_CHARACTER_CONSTANT:
+ return character_constant_to_firm(&expression->conste);
+ case EXPR_WIDE_CHARACTER_CONSTANT:
+ return wide_character_constant_to_firm(&expression->conste);
+ case EXPR_CONST:
+ return const_to_firm(&expression->conste);
+ case EXPR_STRING_LITERAL:
+ return string_literal_to_firm(&expression->string);
+ case EXPR_WIDE_STRING_LITERAL:
+ return wide_string_literal_to_firm(&expression->wide_string);
+ case EXPR_REFERENCE:
+ return reference_expression_to_firm(&expression->reference);
+ case EXPR_REFERENCE_ENUM_VALUE:
+ return reference_expression_enum_value_to_firm(&expression->reference);
+ case EXPR_CALL:
+ return call_expression_to_firm(&expression->call);
+ EXPR_UNARY_CASES
+ return unary_expression_to_firm(&expression->unary);
+ EXPR_BINARY_CASES
+ return binary_expression_to_firm(&expression->binary);
+ case EXPR_ARRAY_ACCESS:
+ return array_access_to_firm(&expression->array_access);
+ case EXPR_SIZEOF:
+ return sizeof_to_firm(&expression->typeprop);
+ case EXPR_ALIGNOF:
+ return alignof_to_firm(&expression->typeprop);
+ case EXPR_CONDITIONAL:
+ return conditional_to_firm(&expression->conditional);
+ case EXPR_SELECT:
+ return select_to_firm(&expression->select);
+ case EXPR_CLASSIFY_TYPE:
+ return classify_type_to_firm(&expression->classify_type);
+ case EXPR_FUNCNAME:
+ return function_name_to_firm(&expression->funcname);
+ case EXPR_STATEMENT:
+ return statement_expression_to_firm(&expression->statement);
+ case EXPR_VA_START:
+ return va_start_expression_to_firm(&expression->va_starte);
+ case EXPR_VA_ARG:
+ return va_arg_expression_to_firm(&expression->va_arge);
+ case EXPR_VA_COPY:
+ return va_copy_expression_to_firm(&expression->va_copye);
+ case EXPR_BUILTIN_CONSTANT_P:
+ return builtin_constant_to_firm(&expression->builtin_constant);
+ case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
+ return builtin_types_compatible_to_firm(&expression->builtin_types_compatible);
+ case EXPR_OFFSETOF:
+ return offsetof_to_firm(&expression->offsetofe);
+ case EXPR_COMPOUND_LITERAL:
+ return compound_literal_to_firm(&expression->compound_literal);
+ case EXPR_LABEL_ADDRESS:
+ return label_address_to_firm(&expression->label_address);
+
+ case EXPR_UNKNOWN:
+ case EXPR_INVALID:
+ break;
+ }
+ panic("invalid expression found");
+}
+
+/**
+ * Check if a given expression is a GNU __builtin_expect() call.
+ */
+static bool is_builtin_expect(const expression_t *expression)
+{
+ if (expression->kind != EXPR_CALL)
+ return false;
+
+ expression_t *function = expression->call.function;
+ if (function->kind != EXPR_REFERENCE)
+ return false;
+ reference_expression_t *ref = &function->reference;
+ if (ref->entity->kind != ENTITY_FUNCTION ||
+ ref->entity->function.btk != bk_gnu_builtin_expect)
+ return false;
+
+ return true;
+}
+
+static bool produces_mode_b(const expression_t *expression)
+{
+ switch (expression->kind) {
+ case EXPR_BINARY_EQUAL:
+ case EXPR_BINARY_NOTEQUAL:
+ case EXPR_BINARY_LESS:
+ case EXPR_BINARY_LESSEQUAL:
+ case EXPR_BINARY_GREATER:
+ case EXPR_BINARY_GREATEREQUAL:
+ case EXPR_BINARY_ISGREATER:
+ case EXPR_BINARY_ISGREATEREQUAL:
+ case EXPR_BINARY_ISLESS:
+ case EXPR_BINARY_ISLESSEQUAL:
+ case EXPR_BINARY_ISLESSGREATER:
+ case EXPR_BINARY_ISUNORDERED:
+ case EXPR_UNARY_NOT:
+ return true;
+
+ case EXPR_CALL:
+ if (is_builtin_expect(expression)) {
+ expression_t *argument = expression->call.arguments->expression;
+ return produces_mode_b(argument);
+ }
+ return false;
+ case EXPR_BINARY_COMMA:
+ return produces_mode_b(expression->binary.right);
+
+ default:
+ return false;
+ }
+}
+
+static ir_node *expression_to_firm(const expression_t *expression)
+{
+ if (!produces_mode_b(expression)) {
+ ir_node *res = _expression_to_firm(expression);
+ assert(res == NULL || get_irn_mode(res) != mode_b);
+ return res;
+ }
+
+ if (is_constant_expression(expression)) {
+ ir_node *res = _expression_to_firm(expression);
+ ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
+ assert(is_Const(res));
+ if (is_Const_null(res)) {
+ return new_Const_long(mode, 0);
+ } else {
+ return new_Const_long(mode, 1);
+ }
+ }
+
+ /* we have to produce a 0/1 from the mode_b expression */
+ dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
+ ir_mode *mode = get_ir_mode_arithmetic(expression->base.type);
+ return produce_condition_result(expression, mode, dbgi);
+}
+
+/**
+ * create a short-circuit expression evaluation that tries to construct
+ * efficient control flow structures for &&, || and ! expressions
+ */
+static ir_node *create_condition_evaluation(const expression_t *expression,
+ ir_node *true_block,
+ ir_node *false_block)
+{
+ switch(expression->kind) {
+ case EXPR_UNARY_NOT: {
+ const unary_expression_t *unary_expression = &expression->unary;
+ create_condition_evaluation(unary_expression->value, false_block,
+ true_block);
+ return NULL;
+ }
+ case EXPR_BINARY_LOGICAL_AND: {
+ const binary_expression_t *binary_expression = &expression->binary;
+
+ ir_node *extra_block = new_immBlock();
+ create_condition_evaluation(binary_expression->left, extra_block,
+ false_block);
+ mature_immBlock(extra_block);
+ set_cur_block(extra_block);
+ create_condition_evaluation(binary_expression->right, true_block,
+ false_block);
+ return NULL;
+ }
+ case EXPR_BINARY_LOGICAL_OR: {
+ const binary_expression_t *binary_expression = &expression->binary;
+
+ ir_node *extra_block = new_immBlock();
+ create_condition_evaluation(binary_expression->left, true_block,
+ extra_block);
+ mature_immBlock(extra_block);
+ set_cur_block(extra_block);
+ create_condition_evaluation(binary_expression->right, true_block,
+ false_block);
+ return NULL;
+ }
+ default:
+ break;
+ }
+
+ dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
+ ir_node *cond_expr = _expression_to_firm(expression);
+ ir_node *condition = create_conv(dbgi, cond_expr, mode_b);
+ ir_node *cond = new_d_Cond(dbgi, condition);
+ ir_node *true_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
+ ir_node *false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
+
+ /* set branch prediction info based on __builtin_expect */
+ if (is_builtin_expect(expression) && is_Cond(cond)) {
+ call_argument_t *argument = expression->call.arguments->next;
+ if (is_constant_expression(argument->expression)) {
+ bool cnst = fold_constant_to_bool(argument->expression);
+ cond_jmp_predicate pred;
+
+ if (cnst == false) {
+ pred = COND_JMP_PRED_FALSE;
+ } else {
+ pred = COND_JMP_PRED_TRUE;
+ }
+ set_Cond_jmp_pred(cond, pred);
+ }
+ }
+
+ add_immBlock_pred(true_block, true_proj);
+ add_immBlock_pred(false_block, false_proj);
+
+ set_cur_block(NULL);
+ return cond_expr;
+}
+
+static void create_variable_entity(entity_t *variable,
+ declaration_kind_t declaration_kind,
+ ir_type *parent_type)
+{
+ assert(variable->kind == ENTITY_VARIABLE);
+ type_t *type = skip_typeref(variable->declaration.type);
+
+ ident *const id = new_id_from_str(variable->base.symbol->string);
+ ir_type *const irtype = get_ir_type(type);
+ dbg_info *const dbgi = get_dbg_info(&variable->base.source_position);
+ ir_entity *const irentity = new_d_entity(parent_type, id, irtype, dbgi);
+ unsigned alignment = variable->declaration.alignment;
+
+ set_entity_alignment(irentity, alignment);
+
+ handle_decl_modifiers(irentity, variable);
+
+ variable->declaration.kind = (unsigned char) declaration_kind;
+ variable->variable.v.entity = irentity;
+ set_entity_variability(irentity, variability_uninitialized);
+ set_entity_ld_ident(irentity, create_ld_ident(variable));
+
+ if (parent_type == get_tls_type())
+ set_entity_allocation(irentity, allocation_automatic);
+ else if (declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE)
+ set_entity_allocation(irentity, allocation_static);
+
+ if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
+ set_entity_volatility(irentity, volatility_is_volatile);
+ }
+}
+
+
+typedef struct type_path_entry_t type_path_entry_t;
+struct type_path_entry_t {
+ type_t *type;
+ ir_initializer_t *initializer;
+ size_t index;
+ entity_t *compound_entry;
+};
+
+typedef struct type_path_t type_path_t;
+struct type_path_t {
+ type_path_entry_t *path;
+ type_t *top_type;
+ bool invalid;
+};
+
+static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
+{
+ size_t len = ARR_LEN(path->path);
+
+ for (size_t i = 0; i < len; ++i) {
+ const type_path_entry_t *entry = & path->path[i];
+
+ type_t *type = skip_typeref(entry->type);
+ if (is_type_compound(type)) {
+ fprintf(stderr, ".%s", entry->compound_entry->base.symbol->string);
+ } else if (is_type_array(type)) {
+ fprintf(stderr, "[%u]", (unsigned) entry->index);
+ } else {
+ fprintf(stderr, "-INVALID-");
+ }
+ }
+ fprintf(stderr, " (");
+ print_type(path->top_type);
+ fprintf(stderr, ")");
+}
+
+static type_path_entry_t *get_type_path_top(const type_path_t *path)
+{
+ size_t len = ARR_LEN(path->path);
+ assert(len > 0);
+ return & path->path[len-1];
+}
+
+static type_path_entry_t *append_to_type_path(type_path_t *path)
+{
+ size_t len = ARR_LEN(path->path);
+ ARR_RESIZE(type_path_entry_t, path->path, len+1);
+
+ type_path_entry_t *result = & path->path[len];
+ memset(result, 0, sizeof(result[0]));
+ return result;
+}
+
+static size_t get_compound_member_count(const compound_type_t *type)
+{
+ compound_t *compound = type->compound;
+ size_t n_members = 0;
+ entity_t *member = compound->members.entities;
+ for ( ; member != NULL; member = member->base.next) {
+ ++n_members;
+ }
+
+ return n_members;
+}
+
+static ir_initializer_t *get_initializer_entry(type_path_t *path)
+{
+ type_t *orig_top_type = path->top_type;
+ type_t *top_type = skip_typeref(orig_top_type);
+
+ assert(is_type_compound(top_type) || is_type_array(top_type));
+
+ if (ARR_LEN(path->path) == 0) {
+ return NULL;
+ } else {
+ type_path_entry_t *top = get_type_path_top(path);
+ ir_initializer_t *initializer = top->initializer;
+ return get_initializer_compound_value(initializer, top->index);
+ }