+ if (dest_mode == mode_b) {
+ ir_node *zero = new_Const(get_mode_null(value_mode));
+ ir_node *cmp = new_d_Cmp(dbgi, value, zero);
+ ir_node *proj = new_d_Proj(dbgi, cmp, mode_b, pn_Cmp_Lg);
+ return proj;
+ }
+
+ return new_d_Conv(dbgi, value, dest_mode);
+}
+
+/**
+ * Creates a Const node representing a constant.
+ */
+static ir_node *const_to_firm(const const_expression_t *cnst)
+{
+ dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
+ type_t *type = skip_typeref(cnst->base.type);
+ ir_mode *mode = get_ir_mode_storage(type);
+
+ char buf[128];
+ tarval *tv;
+ size_t len;
+ if (mode_is_float(mode)) {
+ tv = new_tarval_from_double(cnst->v.float_value, mode);
+ } else {
+ if (mode_is_signed(mode)) {
+ len = snprintf(buf, sizeof(buf), "%lld", cnst->v.int_value);
+ } else {
+ len = snprintf(buf, sizeof(buf), "%llu",
+ (unsigned long long) cnst->v.int_value);
+ }
+ tv = new_tarval_from_str(buf, len, mode);
+ }
+
+ ir_node *res = new_d_Const(dbgi, tv);
+ ir_mode *mode_arith = get_ir_mode_arithmetic(type);
+ return create_conv(dbgi, res, mode_arith);
+}
+
+/**
+ * Creates a Const node representing a character constant.
+ */
+static ir_node *character_constant_to_firm(const const_expression_t *cnst)
+{
+ dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
+ ir_mode *mode = get_ir_mode_arithmetic(cnst->base.type);
+
+ long long int v;
+ size_t const size = cnst->v.character.size;
+ if (size == 1 && char_is_signed) {
+ v = (signed char)cnst->v.character.begin[0];
+ } else {
+ v = 0;
+ for (size_t i = 0; i < size; ++i) {
+ v = (v << 8) | ((unsigned char)cnst->v.character.begin[i]);
+ }
+ }
+ char buf[128];
+ size_t len = snprintf(buf, sizeof(buf), "%lld", v);
+ tarval *tv = new_tarval_from_str(buf, len, mode);
+
+ return new_d_Const(dbgi, tv);
+}
+
+/**
+ * Creates a Const node representing a wide character constant.
+ */
+static ir_node *wide_character_constant_to_firm(const const_expression_t *cnst)
+{
+ dbg_info *dbgi = get_dbg_info(&cnst->base.source_position);
+ ir_mode *mode = get_ir_mode_arithmetic(cnst->base.type);
+
+ long long int v = cnst->v.wide_character.begin[0];
+
+ char buf[128];
+ size_t len = snprintf(buf, sizeof(buf), "%lld", v);
+ tarval *tv = new_tarval_from_str(buf, len, mode);
+
+ return new_d_Const(dbgi, tv);
+}
+
+/*
+ * Allocate an area of size bytes aligned at alignment
+ * at a frame type.
+ */
+static ir_entity *alloc_trampoline(ir_type *frame_type, int size, unsigned alignment) {
+ static unsigned area_cnt = 0;
+ char buf[32];
+
+ snprintf(buf, sizeof(buf), "trampolin%u", area_cnt++);
+ ident *name = new_id_from_str(buf);
+
+ ir_type *tp = new_type_array(id_mangle_u(get_type_ident(frame_type), name), 1, ir_type_char);
+ set_array_bounds_int(tp, 0, 0, size);
+ set_type_alignment_bytes(tp, alignment);
+
+ ir_entity *area = new_entity(frame_type, name, tp);
+
+ /* mark this entity as compiler generated */
+ set_entity_compiler_generated(area, 1);
+ return area;
+}
+
+/**
+ * Return a node representing a trampoline reagion
+ * for a given entity.
+ *
+ * @param dbgi debug info
+ * @param entity the entity
+ */
+static ir_node *get_trampoline_region(dbg_info *dbgi, ir_entity *entity)
+{
+ ir_entity *region = NULL;
+ int i;
+
+ if (current_trampolines != NULL) {
+ for (i = ARR_LEN(current_trampolines) - 1; i >= 0; --i) {
+ if (current_trampolines[i].function == entity) {
+ region = current_trampolines[i].region;
+ break;
+ }
+ }
+ } else {
+ current_trampolines = NEW_ARR_F(trampoline_region, 0);
+ }
+ ir_graph *irg = current_ir_graph;
+ if (region == NULL) {
+ /* create a new region */
+ ir_type *frame_tp = get_irg_frame_type(irg);
+ trampoline_region reg;
+ reg.function = entity;
+
+ reg.region = alloc_trampoline(frame_tp,
+ be_params->trampoline_size,
+ be_params->trampoline_align);
+ ARR_APP1(trampoline_region, current_trampolines, reg);
+ region = reg.region;
+ }
+ return new_d_simpleSel(dbgi, get_irg_no_mem(irg), get_irg_frame(irg),
+ region);
+}
+
+
+/**
+ * Creates a SymConst for a given entity.
+ *
+ * @param dbgi debug info
+ * @param mode the (reference) mode for the SymConst
+ * @param entity the entity
+ */
+static ir_node *create_symconst(dbg_info *dbgi, ir_mode *mode,
+ ir_entity *entity)
+{
+ assert(entity != NULL);
+ union symconst_symbol sym;
+ sym.entity_p = entity;
+ return new_d_SymConst(dbgi, mode, sym, symconst_addr_ent);
+}
+
+/**
+ * Creates a SymConst for a given trampoline of an entity.
+ *
+ * @param dbgi debug info
+ * @param mode the (reference) mode for the SymConst
+ * @param entity the entity
+ */
+static ir_node *create_trampoline(dbg_info *dbgi, ir_mode *mode,
+ ir_entity *entity)
+{
+ assert(entity != NULL);
+ ir_node *in[3];
+ in[0] = get_trampoline_region(dbgi, entity);
+ in[1] = create_symconst(dbgi, mode, entity);
+ in[2] = get_irg_frame(current_ir_graph);
+
+ ir_node *irn = new_d_Builtin(dbgi, get_store(), ir_bk_inner_trampoline, 3, in, get_unknown_type());
+ set_store(new_Proj(irn, mode_M, pn_Builtin_M));
+ return new_Proj(irn, mode, pn_Builtin_1_result);
+}
+
+/**
+ * Creates a SymConst node representing a string constant.
+ *
+ * @param src_pos the source position of the string constant
+ * @param id_prefix a prefix for the name of the generated string constant
+ * @param value the value of the string constant
+ */
+static ir_node *string_to_firm(const source_position_t *const src_pos,
+ const char *const id_prefix,
+ const string_t *const value)
+{
+ ir_type *const global_type = get_glob_type();
+ dbg_info *const dbgi = get_dbg_info(src_pos);
+ ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
+ ir_type_const_char, dbgi);
+
+ ident *const id = id_unique(id_prefix);
+ ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
+ set_entity_ld_ident(entity, id);
+ set_entity_variability(entity, variability_constant);
+ set_entity_allocation(entity, allocation_static);
+ set_entity_visibility(entity, visibility_local);
+
+ ir_type *const elem_type = ir_type_const_char;
+ ir_mode *const mode = get_type_mode(elem_type);
+
+ const char* const string = value->begin;
+ const size_t slen = value->size;
+
+ set_array_lower_bound_int(type, 0, 0);
+ set_array_upper_bound_int(type, 0, slen);
+ set_type_size_bytes(type, slen);
+ set_type_state(type, layout_fixed);
+
+ tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
+ for (size_t i = 0; i < slen; ++i) {
+ tvs[i] = new_tarval_from_long(string[i], mode);
+ }
+
+ set_array_entity_values(entity, tvs, slen);
+ free(tvs);
+
+ return create_symconst(dbgi, mode_P_data, entity);
+}
+
+/**
+ * Creates a SymConst node representing a string literal.
+ *
+ * @param literal the string literal
+ */
+static ir_node *string_literal_to_firm(
+ const string_literal_expression_t* literal)
+{
+ return string_to_firm(&literal->base.source_position, "Lstr.%u",
+ &literal->value);
+}
+
+/**
+ * Creates a SymConst node representing a wide string literal.
+ *
+ * @param literal the wide string literal
+ */
+static ir_node *wide_string_literal_to_firm(
+ const wide_string_literal_expression_t* const literal)
+{
+ ir_type *const global_type = get_glob_type();
+ ir_type *const elem_type = ir_type_wchar_t;
+ dbg_info *const dbgi = get_dbg_info(&literal->base.source_position);
+ ir_type *const type = new_d_type_array(id_unique("strtype.%u"), 1,
+ elem_type, dbgi);
+
+ ident *const id = id_unique("Lstr.%u");
+ ir_entity *const entity = new_d_entity(global_type, id, type, dbgi);
+ set_entity_ld_ident(entity, id);
+ set_entity_variability(entity, variability_constant);
+ set_entity_allocation(entity, allocation_static);
+
+ ir_mode *const mode = get_type_mode(elem_type);
+
+ const wchar_rep_t *const string = literal->value.begin;
+ const size_t slen = literal->value.size;
+
+ set_array_lower_bound_int(type, 0, 0);
+ set_array_upper_bound_int(type, 0, slen);
+ set_type_size_bytes(type, slen);
+ set_type_state(type, layout_fixed);
+
+ tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
+ for (size_t i = 0; i < slen; ++i) {
+ tvs[i] = new_tarval_from_long(string[i], mode);
+ }
+
+ set_array_entity_values(entity, tvs, slen);
+ free(tvs);
+
+ return create_symconst(dbgi, mode_P_data, entity);
+}
+
+/**
+ * Dereference an address.
+ *
+ * @param dbgi debug info
+ * @param type the type of the dereferenced result (the points_to type)
+ * @param addr the address to dereference
+ */
+static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
+ ir_node *const addr)
+{
+ ir_type *irtype = get_ir_type(type);
+ if (is_compound_type(irtype)
+ || is_Method_type(irtype)
+ || is_Array_type(irtype)) {
+ return addr;
+ }
+
+ ir_cons_flags flags = type->base.qualifiers & TYPE_QUALIFIER_VOLATILE
+ ? cons_volatile : cons_none;
+ ir_mode *const mode = get_type_mode(irtype);
+ ir_node *const memory = get_store();
+ ir_node *const load = new_d_Load(dbgi, memory, addr, mode, flags);
+ ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
+ ir_node *const load_res = new_d_Proj(dbgi, load, mode, pn_Load_res);
+
+ set_store(load_mem);
+
+ ir_mode *const mode_arithmetic = get_ir_mode_arithmetic(type);
+ return create_conv(dbgi, load_res, mode_arithmetic);
+}
+
+/**
+ * Creates a strict Conv (to the node's mode) if necessary.
+ *
+ * @param dbgi debug info
+ * @param node the node to strict conv
+ */
+static ir_node *do_strict_conv(dbg_info *dbgi, ir_node *node)
+{
+ ir_mode *mode = get_irn_mode(node);
+
+ if (!(get_irg_fp_model(current_ir_graph) & fp_explicit_rounding))
+ return node;
+ if (!mode_is_float(mode))
+ return node;
+
+ /* check if there is already a Conv */
+ if (is_Conv(node)) {
+ /* convert it into a strict Conv */
+ set_Conv_strict(node, 1);
+ return node;
+ }
+
+ /* otherwise create a new one */
+ return new_d_strictConv(dbgi, node, mode);
+}
+
+/**
+ * Returns the address of a global variable.
+ *
+ * @param dbgi debug info
+ * @param variable the variable
+ */
+static ir_node *get_global_var_address(dbg_info *const dbgi,
+ const variable_t *const variable)
+{
+ ir_entity *const irentity = variable->v.entity;
+ if (variable->thread_local) {
+ ir_node *const no_mem = new_NoMem();
+ ir_node *const tls = get_irg_tls(current_ir_graph);
+ return new_d_simpleSel(dbgi, no_mem, tls, irentity);
+ } else {
+ return create_symconst(dbgi, mode_P_data, irentity);
+ }
+}
+
+/**
+ * Returns the correct base address depending on whether it is a parameter or a
+ * normal local variable.
+ */
+static ir_node *get_local_frame(ir_entity *const ent)
+{
+ ir_graph *const irg = current_ir_graph;
+ const ir_type *const owner = get_entity_owner(ent);
+ if (owner == current_outer_frame || owner == current_outer_value_type) {
+ assert(current_static_link != NULL);
+ return current_static_link;
+ } else {
+ return get_irg_frame(irg);
+ }
+}
+
+/**
+ * Keep all memory edges of the given block.
+ */
+static void keep_all_memory(ir_node *block)
+{
+ ir_node *old = get_cur_block();
+
+ set_cur_block(block);
+ keep_alive(get_store());
+ /* TODO: keep all memory edges from restricted pointers */
+ set_cur_block(old);
+}
+
+static ir_node *reference_expression_enum_value_to_firm(
+ const reference_expression_t *ref)
+{
+ entity_t *entity = ref->entity;
+ type_t *type = skip_typeref(entity->enum_value.enum_type);
+ /* make sure the type is constructed */
+ (void) get_ir_type(type);
+
+ return new_Const(entity->enum_value.tv);
+}
+
+static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
+{
+ dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
+ entity_t *entity = ref->entity;
+ assert(is_declaration(entity));
+ type_t *type = skip_typeref(entity->declaration.type);
+
+ /* make sure the type is constructed */
+ (void) get_ir_type(type);
+
+ switch ((declaration_kind_t) entity->declaration.kind) {
+ case DECLARATION_KIND_UNKNOWN:
+ break;
+
+ case DECLARATION_KIND_LOCAL_VARIABLE: {
+ ir_mode *const mode = get_ir_mode_storage(type);
+ ir_node *const value = get_value(entity->variable.v.value_number, mode);
+ return create_conv(NULL, value, get_ir_mode_arithmetic(type));
+ }
+ case DECLARATION_KIND_PARAMETER: {
+ ir_mode *const mode = get_ir_mode_storage(type);
+ ir_node *const value = get_value(entity->parameter.v.value_number,mode);
+ return create_conv(NULL, value, get_ir_mode_arithmetic(type));
+ }
+ case DECLARATION_KIND_FUNCTION: {
+ ir_mode *const mode = get_ir_mode_storage(type);
+
+ if (entity->function.btk != bk_none) {
+ /* for gcc compatibility we have to produce (dummy) addresses for some
+ * builtins */
+ if (warning.other) {
+ warningf(&ref->base.source_position,
+ "taking address of builtin '%Y'", ref->entity->base.symbol);
+ }
+
+ /* simply create a NULL pointer */
+ ir_mode *mode = get_ir_mode_arithmetic(type_void_ptr);
+ ir_node *res = new_Const_long(mode, 0);
+
+ return res;
+ }
+ return create_symconst(dbgi, mode, entity->function.irentity);
+ }
+ case DECLARATION_KIND_INNER_FUNCTION: {
+ ir_mode *const mode = get_ir_mode_storage(type);
+ if (!entity->function.goto_to_outer && !entity->function.need_closure) {
+ /* inner function not using the closure */
+ return create_symconst(dbgi, mode, entity->function.irentity);
+ } else {
+ /* need trampoline here */
+ return create_trampoline(dbgi, mode, entity->function.irentity);
+ }
+ }
+ case DECLARATION_KIND_GLOBAL_VARIABLE: {
+ const variable_t *variable = &entity->variable;
+ ir_node *const addr = get_global_var_address(dbgi, variable);
+ return deref_address(dbgi, variable->base.type, addr);
+ }
+
+ case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
+ ir_entity *irentity = entity->variable.v.entity;
+ ir_node *frame = get_local_frame(irentity);
+ ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
+ return deref_address(dbgi, entity->declaration.type, sel);
+ }
+ case DECLARATION_KIND_PARAMETER_ENTITY: {
+ ir_entity *irentity = entity->parameter.v.entity;
+ ir_node *frame = get_local_frame(irentity);
+ ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
+ return deref_address(dbgi, entity->declaration.type, sel);
+ }
+
+ case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
+ return entity->variable.v.vla_base;
+
+ case DECLARATION_KIND_COMPOUND_MEMBER:
+ panic("not implemented reference type");
+ }
+
+ panic("reference to declaration with unknown type found");
+}
+
+static ir_node *reference_addr(const reference_expression_t *ref)
+{
+ dbg_info *dbgi = get_dbg_info(&ref->base.source_position);
+ entity_t *entity = ref->entity;
+ assert(is_declaration(entity));
+
+ switch((declaration_kind_t) entity->declaration.kind) {
+ case DECLARATION_KIND_UNKNOWN:
+ break;
+ case DECLARATION_KIND_PARAMETER:
+ case DECLARATION_KIND_LOCAL_VARIABLE:
+ /* you can store to a local variable (so we don't panic but return NULL
+ * as an indicator for no real address) */
+ return NULL;
+ case DECLARATION_KIND_GLOBAL_VARIABLE: {
+ ir_node *const addr = get_global_var_address(dbgi, &entity->variable);
+ return addr;
+ }
+ case DECLARATION_KIND_LOCAL_VARIABLE_ENTITY: {
+ ir_entity *irentity = entity->variable.v.entity;
+ ir_node *frame = get_local_frame(irentity);
+ ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
+
+ return sel;
+ }
+ case DECLARATION_KIND_PARAMETER_ENTITY: {
+ ir_entity *irentity = entity->parameter.v.entity;
+ ir_node *frame = get_local_frame(irentity);
+ ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), frame, irentity);
+
+ return sel;
+ }
+
+ case DECLARATION_KIND_VARIABLE_LENGTH_ARRAY:
+ return entity->variable.v.vla_base;
+
+ case DECLARATION_KIND_FUNCTION: {
+ type_t *const type = skip_typeref(entity->declaration.type);
+ ir_mode *const mode = get_ir_mode_storage(type);
+ return create_symconst(dbgi, mode, entity->function.irentity);
+ }
+
+ case DECLARATION_KIND_INNER_FUNCTION: {
+ type_t *const type = skip_typeref(entity->declaration.type);
+ ir_mode *const mode = get_ir_mode_storage(type);
+ if (!entity->function.goto_to_outer && !entity->function.need_closure) {
+ /* inner function not using the closure */
+ return create_symconst(dbgi, mode, entity->function.irentity);
+ } else {
+ /* need trampoline here */
+ return create_trampoline(dbgi, mode, entity->function.irentity);
+ }
+ }
+
+ case DECLARATION_KIND_COMPOUND_MEMBER:
+ panic("not implemented reference type");
+ }
+
+ 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;