+ 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);
+
+ type_t *type = skip_typeref(top->type);
+ if (is_type_union(type)) {
+ top->compound_entry = NULL;
+ } else if (is_type_struct(type)) {
+ entity_t *entry = top->compound_entry;
+
+ top->index++;
+ entry = entry->base.next;
+ top->compound_entry = entry;
+ if (entry != NULL) {
+ assert(entry->kind == ENTITY_COMPOUND_MEMBER);
+ path->top_type = entry->declaration.type;
+ return;
+ }
+ } else {
+ assert(is_type_array(type));
+
+ top->index++;
+ if (!type->array.size_constant || top->index < type->array.size) {
+ return;
+ }
+ }
+
+ /* we're past the last member of the current sub-aggregate, try if we
+ * can ascend in the type hierarchy and continue with another subobject */
+ size_t len = ARR_LEN(path->path);
+
+ if (len > 1) {
+ ascend_from_subtype(path);
+ advance_current_object(path);
+ } else {
+ path->invalid = true;
+ }
+}
+
+
+static ir_initializer_t *create_ir_initializer(
+ const initializer_t *initializer, type_t *type);
+
+static ir_initializer_t *create_ir_initializer_value(
+ const initializer_value_t *initializer)
+{
+ if (is_type_compound(initializer->value->base.type)) {
+ panic("initializer creation for compounds not implemented yet");
+ }
+ ir_node *value = expression_to_firm(initializer->value);
+ type_t *type = initializer->value->base.type;
+ ir_mode *mode = get_ir_mode_storage(type);
+ value = create_conv(NULL, value, mode);
+ return create_initializer_const(value);
+}
+
+/** test wether type can be initialized by a string constant */
+static bool is_string_type(type_t *type)
+{
+ type_t *inner;
+ if (is_type_pointer(type)) {
+ inner = skip_typeref(type->pointer.points_to);
+ } else if(is_type_array(type)) {
+ inner = skip_typeref(type->array.element_type);
+ } else {
+ return false;
+ }
+
+ return is_type_integer(inner);
+}
+
+static ir_initializer_t *create_ir_initializer_list(
+ const initializer_list_t *initializer, type_t *type)
+{
+ type_path_t path;
+ memset(&path, 0, sizeof(path));
+ path.top_type = type;
+ path.path = NEW_ARR_F(type_path_entry_t, 0);
+
+ descend_into_subtype(&path);
+
+ for (size_t i = 0; i < initializer->len; ++i) {
+ const initializer_t *sub_initializer = initializer->initializers[i];
+
+ if (sub_initializer->kind == INITIALIZER_DESIGNATOR) {
+ walk_designator(&path, sub_initializer->designator.designator);
+ continue;
+ }
+
+ if (sub_initializer->kind == INITIALIZER_VALUE) {
+ /* we might have to descend into types until we're at a scalar
+ * type */
+ while(true) {
+ type_t *orig_top_type = path.top_type;
+ type_t *top_type = skip_typeref(orig_top_type);
+
+ if (is_type_scalar(top_type))
+ break;
+ descend_into_subtype(&path);
+ }
+ } else if (sub_initializer->kind == INITIALIZER_STRING
+ || sub_initializer->kind == INITIALIZER_WIDE_STRING) {
+ /* we might have to descend into types until we're at a scalar
+ * type */
+ while (true) {
+ type_t *orig_top_type = path.top_type;
+ type_t *top_type = skip_typeref(orig_top_type);
+
+ if (is_string_type(top_type))
+ break;
+ descend_into_subtype(&path);
+ }
+ }
+
+ ir_initializer_t *sub_irinitializer
+ = create_ir_initializer(sub_initializer, path.top_type);
+
+ size_t path_len = ARR_LEN(path.path);
+ assert(path_len >= 1);
+ type_path_entry_t *entry = & path.path[path_len-1];
+ ir_initializer_t *tinitializer = entry->initializer;
+ set_initializer_compound_value(tinitializer, entry->index,
+ sub_irinitializer);
+
+ advance_current_object(&path);
+ }
+
+ assert(ARR_LEN(path.path) >= 1);
+ ir_initializer_t *result = path.path[0].initializer;
+ DEL_ARR_F(path.path);
+
+ return result;
+}
+
+static ir_initializer_t *create_ir_initializer_string(
+ const initializer_string_t *initializer, type_t *type)
+{
+ type = skip_typeref(type);
+
+ size_t string_len = initializer->string.size;
+ assert(type->kind == TYPE_ARRAY);
+ assert(type->array.size_constant);
+ size_t len = type->array.size;
+ ir_initializer_t *irinitializer = create_initializer_compound(len);
+
+ const char *string = initializer->string.begin;
+ ir_mode *mode = get_ir_mode_storage(type->array.element_type);
+
+ for (size_t i = 0; i < len; ++i) {
+ char c = 0;
+ if (i < string_len)
+ c = string[i];
+
+ tarval *tv = new_tarval_from_long(c, mode);
+ ir_initializer_t *char_initializer = create_initializer_tarval(tv);
+
+ set_initializer_compound_value(irinitializer, i, char_initializer);
+ }
+
+ return irinitializer;
+}
+
+static ir_initializer_t *create_ir_initializer_wide_string(
+ const initializer_wide_string_t *initializer, type_t *type)
+{
+ size_t string_len = initializer->string.size;
+ assert(type->kind == TYPE_ARRAY);
+ assert(type->array.size_constant);
+ size_t len = type->array.size;
+ ir_initializer_t *irinitializer = create_initializer_compound(len);
+
+ const wchar_rep_t *string = initializer->string.begin;
+ ir_mode *mode = get_type_mode(ir_type_wchar_t);
+
+ for (size_t i = 0; i < len; ++i) {
+ wchar_rep_t c = 0;
+ if (i < string_len) {
+ c = string[i];
+ }
+ tarval *tv = new_tarval_from_long(c, mode);
+ ir_initializer_t *char_initializer = create_initializer_tarval(tv);
+
+ set_initializer_compound_value(irinitializer, i, char_initializer);
+ }
+
+ return irinitializer;
+}
+
+static ir_initializer_t *create_ir_initializer(
+ const initializer_t *initializer, type_t *type)
+{
+ switch(initializer->kind) {
+ case INITIALIZER_STRING:
+ return create_ir_initializer_string(&initializer->string, type);
+
+ case INITIALIZER_WIDE_STRING:
+ return create_ir_initializer_wide_string(&initializer->wide_string,
+ type);
+
+ case INITIALIZER_LIST:
+ return create_ir_initializer_list(&initializer->list, type);
+
+ case INITIALIZER_VALUE:
+ return create_ir_initializer_value(&initializer->value);
+
+ case INITIALIZER_DESIGNATOR:
+ panic("unexpected designator initializer found");
+ }
+ panic("unknown initializer");
+}
+
+static void create_dynamic_null_initializer(ir_type *type, dbg_info *dbgi,
+ ir_node *base_addr)
+{
+ if (is_atomic_type(type)) {
+ ir_mode *mode = get_type_mode(type);
+ tarval *zero = get_mode_null(mode);
+ ir_node *cnst = new_d_Const(dbgi, zero);
+
+ /* TODO: bitfields */
+ ir_node *mem = get_store();
+ ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst, cons_none);
+ ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
+ set_store(proj_m);
+ } else {
+ assert(is_compound_type(type));
+
+ int n_members;
+ if (is_Array_type(type)) {
+ assert(has_array_upper_bound(type, 0));
+ n_members = get_array_upper_bound_int(type, 0);
+ } else {
+ n_members = get_compound_n_members(type);
+ }
+
+ for (int i = 0; i < n_members; ++i) {
+ ir_node *addr;
+ ir_type *irtype;
+ if (is_Array_type(type)) {
+ ir_entity *entity = get_array_element_entity(type);
+ tarval *index_tv = new_tarval_from_long(i, mode_uint);
+ ir_node *cnst = new_d_Const(dbgi, index_tv);
+ ir_node *in[1] = { cnst };
+ irtype = get_array_element_type(type);
+ addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, entity);
+ } else {
+ ir_entity *member = get_compound_member(type, i);
+
+ irtype = get_entity_type(member);
+ addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, member);
+ }
+
+ create_dynamic_null_initializer(irtype, dbgi, addr);
+ }
+ }
+}
+
+static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
+ ir_entity *entity, ir_type *type, dbg_info *dbgi, ir_node *base_addr)
+{
+ switch(get_initializer_kind(initializer)) {
+ case IR_INITIALIZER_NULL: {
+ create_dynamic_null_initializer(type, dbgi, base_addr);
+ return;
+ }
+ case IR_INITIALIZER_CONST: {
+ ir_node *node = get_initializer_const_value(initializer);
+ ir_mode *mode = get_irn_mode(node);
+ ir_type *ent_type = get_entity_type(entity);
+
+ /* is it a bitfield type? */
+ if (is_Primitive_type(ent_type) &&
+ get_primitive_base_type(ent_type) != NULL) {
+ bitfield_store_to_firm(dbgi, entity, base_addr, node, false);
+ return;
+ }
+
+ assert(get_type_mode(type) == mode);
+ ir_node *mem = get_store();
+ ir_node *store = new_d_Store(dbgi, mem, base_addr, node, cons_none);
+ ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
+ set_store(proj_m);
+ return;
+ }
+ case IR_INITIALIZER_TARVAL: {
+ tarval *tv = get_initializer_tarval_value(initializer);
+ ir_mode *mode = get_tarval_mode(tv);
+ ir_node *cnst = new_d_Const(dbgi, tv);
+ ir_type *ent_type = get_entity_type(entity);
+
+ /* is it a bitfield type? */
+ if (is_Primitive_type(ent_type) &&
+ get_primitive_base_type(ent_type) != NULL) {
+ bitfield_store_to_firm(dbgi, entity, base_addr, cnst, false);
+ return;
+ }
+
+ assert(get_type_mode(type) == mode);
+ ir_node *mem = get_store();
+ ir_node *store = new_d_Store(dbgi, mem, base_addr, cnst, cons_none);
+ ir_node *proj_m = new_Proj(store, mode_M, pn_Store_M);
+ set_store(proj_m);
+ return;
+ }
+ case IR_INITIALIZER_COMPOUND: {
+ assert(is_compound_type(type));
+ int n_members;
+ if (is_Array_type(type)) {
+ assert(has_array_upper_bound(type, 0));
+ n_members = get_array_upper_bound_int(type, 0);
+ } else {
+ n_members = get_compound_n_members(type);
+ }
+
+ if (get_initializer_compound_n_entries(initializer)
+ != (unsigned) n_members)
+ panic("initializer doesn't match compound type");
+
+ for (int i = 0; i < n_members; ++i) {
+ ir_node *addr;
+ ir_type *irtype;
+ ir_entity *sub_entity;
+ if (is_Array_type(type)) {
+ tarval *index_tv = new_tarval_from_long(i, mode_uint);
+ ir_node *cnst = new_d_Const(dbgi, index_tv);
+ ir_node *in[1] = { cnst };
+ irtype = get_array_element_type(type);
+ sub_entity = get_array_element_entity(type);
+ addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in,
+ sub_entity);
+ } else {
+ sub_entity = get_compound_member(type, i);
+ irtype = get_entity_type(sub_entity);
+ addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr,
+ sub_entity);
+ }
+
+ ir_initializer_t *sub_init
+ = get_initializer_compound_value(initializer, i);
+
+ create_dynamic_initializer_sub(sub_init, sub_entity, irtype, dbgi,
+ addr);
+ }
+ return;
+ }
+ }
+
+ panic("invalid IR_INITIALIZER found");
+}
+
+static void create_dynamic_initializer(ir_initializer_t *initializer,
+ dbg_info *dbgi, ir_entity *entity)
+{
+ ir_node *frame = get_irg_frame(current_ir_graph);
+ ir_node *base_addr = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
+ ir_type *type = get_entity_type(entity);
+
+ create_dynamic_initializer_sub(initializer, entity, type, dbgi, base_addr);
+}
+
+static void create_local_initializer(initializer_t *initializer, dbg_info *dbgi,
+ ir_entity *entity, type_t *type)
+{
+ ir_node *memory = get_store();
+ ir_node *nomem = new_NoMem();
+ ir_node *frame = get_irg_frame(current_ir_graph);
+ ir_node *addr = new_d_simpleSel(dbgi, nomem, frame, entity);
+
+ if (initializer->kind == INITIALIZER_VALUE) {
+ initializer_value_t *initializer_value = &initializer->value;
+
+ ir_node *value = expression_to_firm(initializer_value->value);
+ type = skip_typeref(type);
+ assign_value(dbgi, addr, type, value);
+ return;
+ }
+
+ if (!is_constant_initializer(initializer)) {
+ ir_initializer_t *irinitializer
+ = create_ir_initializer(initializer, type);
+
+ create_dynamic_initializer(irinitializer, dbgi, entity);
+ return;
+ }
+
+ /* create the ir_initializer */
+ ir_graph *const old_current_ir_graph = current_ir_graph;
+ current_ir_graph = get_const_code_irg();
+
+ ir_initializer_t *irinitializer = create_ir_initializer(initializer, type);
+
+ assert(current_ir_graph == get_const_code_irg());
+ current_ir_graph = old_current_ir_graph;
+
+ /* create a "template" entity which is copied to the entity on the stack */
+ ident *const id = id_unique("initializer.%u");
+ ir_type *const irtype = get_ir_type(type);
+ ir_type *const global_type = get_glob_type();
+ ir_entity *const init_entity = new_d_entity(global_type, id, irtype, dbgi);
+ set_entity_ld_ident(init_entity, id);
+
+ set_entity_variability(init_entity, variability_initialized);
+ set_entity_visibility(init_entity, visibility_local);
+ set_entity_allocation(init_entity, allocation_static);
+
+ set_entity_initializer(init_entity, irinitializer);
+
+ ir_node *const src_addr = create_symconst(dbgi, mode_P_data, init_entity);
+ ir_node *const copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
+
+ ir_node *const copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
+ set_store(copyb_mem);
+}
+
+static void create_initializer_local_variable_entity(entity_t *entity)
+{
+ assert(entity->kind == ENTITY_VARIABLE);
+ initializer_t *initializer = entity->variable.initializer;
+ dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
+ ir_entity *irentity = entity->variable.v.entity;
+ type_t *type = entity->declaration.type;
+
+ create_local_initializer(initializer, dbgi, irentity, type);
+}
+
+static void create_variable_initializer(entity_t *entity)
+{
+ assert(entity->kind == ENTITY_VARIABLE);
+ initializer_t *initializer = entity->variable.initializer;
+ if (initializer == NULL)
+ return;
+
+ declaration_kind_t declaration_kind
+ = (declaration_kind_t) entity->declaration.kind;
+ if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY) {
+ create_initializer_local_variable_entity(entity);
+ return;
+ }
+
+ type_t *type = entity->declaration.type;
+ type_qualifiers_t tq = get_type_qualifier(type, true);
+
+ if (initializer->kind == INITIALIZER_VALUE) {
+ initializer_value_t *initializer_value = &initializer->value;
+ dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
+
+ ir_node *value = expression_to_firm(initializer_value->value);
+
+ type_t *type = initializer_value->value->base.type;
+ ir_mode *mode = get_ir_mode_storage(type);
+ value = create_conv(dbgi, value, mode);
+ value = do_strict_conv(dbgi, value);
+
+ if (declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE) {
+ set_value(entity->variable.v.value_number, value);
+ } else {
+ assert(declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
+
+ ir_entity *irentity = entity->variable.v.entity;
+
+ if (tq & TYPE_QUALIFIER_CONST) {
+ set_entity_variability(irentity, variability_constant);
+ } else {
+ set_entity_variability(irentity, variability_initialized);
+ }
+ set_atomic_ent_value(irentity, value);
+ }
+ } else {
+ assert(declaration_kind == DECLARATION_KIND_LOCAL_VARIABLE_ENTITY ||
+ declaration_kind == DECLARATION_KIND_GLOBAL_VARIABLE);
+
+ ir_entity *irentity = entity->variable.v.entity;
+ ir_initializer_t *irinitializer
+ = create_ir_initializer(initializer, type);
+
+ if (tq & TYPE_QUALIFIER_CONST) {
+ set_entity_variability(irentity, variability_constant);
+ } else {
+ set_entity_variability(irentity, variability_initialized);
+ }
+ set_entity_initializer(irentity, irinitializer);
+ }
+}
+
+static void create_variable_length_array(entity_t *entity)
+{
+ assert(entity->kind == ENTITY_VARIABLE);
+ assert(entity->variable.initializer == NULL);
+
+ entity->declaration.kind = DECLARATION_KIND_VARIABLE_LENGTH_ARRAY;
+ entity->variable.v.vla_base = NULL;
+
+ /* TODO: record VLA somewhere so we create the free node when we leave
+ * it's scope */
+}
+
+static void allocate_variable_length_array(entity_t *entity)
+{
+ assert(entity->kind == ENTITY_VARIABLE);
+ assert(entity->variable.initializer == NULL);
+ assert(get_cur_block() != NULL);
+
+ dbg_info *dbgi = get_dbg_info(&entity->base.source_position);
+ type_t *type = entity->declaration.type;
+ ir_type *el_type = get_ir_type(type->array.element_type);
+
+ /* make sure size_node is calculated */
+ get_type_size_node(type);
+ ir_node *elems = type->array.size_node;
+ ir_node *mem = get_store();
+ ir_node *alloc = new_d_Alloc(dbgi, mem, elems, el_type, stack_alloc);
+
+ ir_node *proj_m = new_d_Proj(dbgi, alloc, mode_M, pn_Alloc_M);
+ ir_node *addr = new_d_Proj(dbgi, alloc, mode_P_data, pn_Alloc_res);
+ set_store(proj_m);
+
+ assert(entity->declaration.kind == DECLARATION_KIND_VARIABLE_LENGTH_ARRAY);
+ entity->variable.v.vla_base = addr;
+}
+
+/**
+ * Creates a Firm local variable from a declaration.
+ */
+static void create_local_variable(entity_t *entity)
+{
+ assert(entity->kind == ENTITY_VARIABLE);
+ assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
+
+ bool needs_entity = entity->variable.address_taken;
+ type_t *type = skip_typeref(entity->declaration.type);
+
+ /* is it a variable length array? */
+ if (is_type_array(type) && !type->array.size_constant) {
+ create_variable_length_array(entity);
+ return;
+ } else if (is_type_array(type) || is_type_compound(type)) {
+ needs_entity = true;
+ } else if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
+ needs_entity = true;
+ }
+
+ if (needs_entity) {
+ ir_type *frame_type = get_irg_frame_type(current_ir_graph);
+ create_variable_entity(entity,
+ DECLARATION_KIND_LOCAL_VARIABLE_ENTITY,
+ frame_type);
+ } else {
+ entity->declaration.kind = DECLARATION_KIND_LOCAL_VARIABLE;
+ entity->variable.v.value_number = next_value_number_function;
+ set_irg_loc_description(current_ir_graph, next_value_number_function,
+ entity);
+ ++next_value_number_function;
+ }
+}
+
+static void create_local_static_variable(entity_t *entity)
+{
+ assert(entity->kind == ENTITY_VARIABLE);
+ assert(entity->declaration.kind == DECLARATION_KIND_UNKNOWN);
+
+ type_t *type = skip_typeref(entity->declaration.type);
+ ir_type *const var_type = entity->variable.thread_local ?
+ get_tls_type() : get_glob_type();
+ ir_type *const irtype = get_ir_type(type);
+ dbg_info *const dbgi = get_dbg_info(&entity->base.source_position);
+
+ size_t l = strlen(entity->base.symbol->string);
+ char buf[l + sizeof(".%u")];
+ snprintf(buf, sizeof(buf), "%s.%%u", entity->base.symbol->string);
+ ident *const id = id_unique(buf);