+ 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);
+ }
+}
+
+static void descend_into_subtype(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));
+
+ ir_initializer_t *initializer = get_initializer_entry(path);
+
+ type_path_entry_t *top = append_to_type_path(path);
+ top->type = top_type;
+
+ size_t len;
+
+ if (is_type_compound(top_type)) {
+ compound_t *compound = top_type->compound.compound;
+ entity_t *entry = compound->members.entities;
+
+ top->compound_entry = entry;
+ top->index = 0;
+ len = get_compound_member_count(&top_type->compound);
+ if (entry != NULL) {
+ assert(entry->kind == ENTITY_COMPOUND_MEMBER);
+ path->top_type = entry->declaration.type;
+ }
+ } else {
+ assert(is_type_array(top_type));
+ assert(top_type->array.size > 0);
+
+ top->index = 0;
+ path->top_type = top_type->array.element_type;
+ len = top_type->array.size;
+ }
+ if (initializer == NULL
+ || get_initializer_kind(initializer) == IR_INITIALIZER_NULL) {
+ initializer = create_initializer_compound(len);
+ /* we have to set the entry at the 2nd latest path entry... */
+ size_t path_len = ARR_LEN(path->path);
+ assert(path_len >= 1);
+ if (path_len > 1) {
+ type_path_entry_t *entry = & path->path[path_len-2];
+ ir_initializer_t *tinitializer = entry->initializer;
+ set_initializer_compound_value(tinitializer, entry->index,
+ initializer);
+ }
+ }
+ top->initializer = initializer;
+}
+
+static void ascend_from_subtype(type_path_t *path)
+{
+ type_path_entry_t *top = get_type_path_top(path);
+
+ path->top_type = top->type;
+
+ size_t len = ARR_LEN(path->path);
+ ARR_RESIZE(type_path_entry_t, path->path, len-1);
+}
+
+static void walk_designator(type_path_t *path, const designator_t *designator)
+{
+ /* designators start at current object type */
+ ARR_RESIZE(type_path_entry_t, path->path, 1);
+
+ for ( ; designator != NULL; designator = designator->next) {
+ type_path_entry_t *top = get_type_path_top(path);
+ type_t *orig_type = top->type;
+ type_t *type = skip_typeref(orig_type);
+
+ if (designator->symbol != NULL) {
+ assert(is_type_compound(type));
+ size_t index = 0;
+ symbol_t *symbol = designator->symbol;
+
+ compound_t *compound = type->compound.compound;
+ entity_t *iter = compound->members.entities;
+ for ( ; iter != NULL; iter = iter->base.next, ++index) {
+ if (iter->base.symbol == symbol) {
+ assert(iter->kind == ENTITY_COMPOUND_MEMBER);
+ break;
+ }
+ }
+ assert(iter != NULL);
+
+ top->type = orig_type;
+ top->compound_entry = iter;
+ top->index = index;
+ 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);
+ assert(index >= 0);
+#ifndef NDEBUG
+ if (type->array.size_constant) {
+ long array_size = type->array.size;
+ assert(index < array_size);
+ }
+#endif
+
+ top->type = orig_type;
+ top->index = (size_t) index;
+ orig_type = type->array.element_type;
+ }
+ path->top_type = orig_type;
+
+ if (designator->next != NULL) {
+ descend_into_subtype(path);
+ }
+ }
+
+ path->invalid = false;
+}
+
+static void advance_current_object(type_path_t *path)
+{
+ if (path->invalid) {
+ /* TODO: handle this... */
+ panic("invalid initializer in ast2firm (excessive elements)");
+ }
+
+ type_path_entry_t *top = get_type_path_top(path);