+typedef struct compound_graph_path_entry_t compound_graph_path_entry_t;
+
+enum compound_graph_entry_type_t {
+ COMPOUND_GRAPH_ENTRY_ARRAY,
+ COMPOUND_GRAPH_ENTRY_COMPOUND
+};
+
+struct compound_graph_path_entry_t {
+ int type;
+ union {
+ ir_entity *entity;
+ int array_index;
+ } v;
+ compound_graph_path_entry_t *prev;
+};
+
+static void create_initializer_object(initializer_t *initializer, type_t *type,
+ ir_entity *entity, compound_graph_path_entry_t *entry, int len);
+
+static compound_graph_path *create_compound_path(ir_type *type,
+ compound_graph_path_entry_t *entry, int len)
+{
+ compound_graph_path *path = new_compound_graph_path(type, len);
+
+ int i = len - 1;
+ for( ; entry != NULL; entry = entry->prev, --i) {
+ assert(i >= 0);
+ if(entry->type == COMPOUND_GRAPH_ENTRY_COMPOUND) {
+ set_compound_graph_path_node(path, i, entry->v.entity);
+ } else {
+ assert(entry->type == COMPOUND_GRAPH_ENTRY_ARRAY);
+ set_compound_graph_path_array_index(path, i, entry->v.array_index);
+ }
+ }
+ assert(i == -1);
+
+ return path;
+}
+
+static void create_initializer_value(initializer_value_t *initializer,
+ ir_entity *entity,
+ compound_graph_path_entry_t *entry,
+ int len)
+{
+ ir_node *node = expression_to_firm(initializer->value);
+ ir_type *type = get_entity_type(entity);
+ compound_graph_path *path = create_compound_path(type, entry, len);
+ add_compound_ent_value_w_path(entity, node, path);
+}
+
+static void create_initializer_compound(initializer_list_t *initializer,
+ compound_type_t *type,
+ ir_entity *entity,
+ compound_graph_path_entry_t *last_entry,
+ int len)
+{
+ declaration_t *compound_declaration = type->declaration;
+
+ declaration_t *compound_entry = compound_declaration->context.declarations;
+
+ compound_graph_path_entry_t entry;
+ entry.type = COMPOUND_GRAPH_ENTRY_COMPOUND;
+ entry.prev = last_entry;
+ ++len;
+
+ size_t i = 0;
+ for( ; compound_entry != NULL; compound_entry = compound_entry->next) {
+ if(compound_entry->symbol == NULL)
+ continue;
+ if(compound_entry->namespc != NAMESPACE_NORMAL)
+ continue;
+
+ if(i >= initializer->len)
+ break;
+
+ entry.v.entity = compound_entry->v.entity;
+
+ initializer_t *sub_initializer = initializer->initializers[i];
+
+ assert(compound_entry != NULL);
+ assert(compound_entry->declaration_type
+ == DECLARATION_TYPE_COMPOUND_MEMBER);
+
+ if(sub_initializer->type == INITIALIZER_VALUE) {
+ create_initializer_value(&sub_initializer->value,
+ entity, &entry, len);
+ } else {
+ type_t *entry_type = skip_typeref(compound_entry->type);
+ create_initializer_object(sub_initializer, entry_type, entity,
+ &entry, len);
+ }
+
+ ++i;
+ }
+}
+
+static void create_initializer_array(initializer_list_t *initializer,
+ array_type_t *type, ir_entity *entity,
+ compound_graph_path_entry_t *last_entry,
+ int len)
+{
+ type_t *element_type = type->element_type;
+ element_type = skip_typeref(element_type);
+
+ compound_graph_path_entry_t entry;
+ entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
+ entry.prev = last_entry;
+ ++len;
+
+ size_t i;
+ for(i = 0; i < initializer->len; ++i) {
+ entry.v.array_index = i;
+
+ initializer_t *sub_initializer = initializer->initializers[i];
+
+ if(sub_initializer->type == INITIALIZER_VALUE) {
+ create_initializer_value(&sub_initializer->value,
+ entity, &entry, len);
+ } else {
+ create_initializer_object(sub_initializer, element_type, entity,
+ &entry, len);
+ }
+ }
+
+#if 0
+ /* TODO: initialize rest... */
+ if(type->size_expression != NULL) {
+ size_t array_len = fold_constant(type->size_expression);
+ for( ; i < array_len; ++i) {
+
+ }
+ }
+#endif
+}
+
+static void create_initializer_string(initializer_string_t *initializer,
+ array_type_t *type, ir_entity *entity,
+ compound_graph_path_entry_t *last_entry,
+ int len)
+{
+ type_t *element_type = type->element_type;
+ element_type = skip_typeref(element_type);
+
+ compound_graph_path_entry_t entry;
+ entry.type = COMPOUND_GRAPH_ENTRY_ARRAY;
+ entry.prev = last_entry;
+ ++len;
+
+ ir_type *irtype = get_entity_type(entity);
+ size_t arr_len = get_array_type_size(type);
+ const char *p = initializer->string;
+ size_t i = 0;
+ for(i = 0; i < arr_len; ++i, ++p) {
+ entry.v.array_index = i;
+
+ ir_node *node = new_Const_long(mode_Bs, *p);
+ compound_graph_path *path = create_compound_path(irtype, &entry, len);
+ add_compound_ent_value_w_path(entity, node, path);
+
+ if(*p == '\0')
+ break;
+ }
+}
+
+static void create_initializer_object(initializer_t *initializer, type_t *type,
+ ir_entity *entity, compound_graph_path_entry_t *entry, int len)
+{
+ if(type->type == TYPE_ARRAY) {
+ array_type_t *array_type = &type->array;
+
+ if(initializer->type == INITIALIZER_STRING) {
+ initializer_string_t *string = &initializer->string;
+ create_initializer_string(string, array_type, entity, entry, len);
+ } else {
+ assert(initializer->type == INITIALIZER_LIST);
+ initializer_list_t *list = &initializer->list;
+ create_initializer_array(list, array_type, entity, entry, len);
+ }
+ } else {
+ assert(initializer->type == INITIALIZER_LIST);
+ initializer_list_t *list = &initializer->list;
+
+ assert(type->type == TYPE_COMPOUND_STRUCT
+ || type->type == TYPE_COMPOUND_UNION);
+ compound_type_t *compound_type = &type->compound;
+ create_initializer_compound(list, compound_type, entity, entry, len);
+ }
+}
+
+static void create_initializer_local_variable_entity(declaration_t *declaration)
+{
+ initializer_t *initializer = declaration->init.initializer;
+ dbg_info *dbgi = get_dbg_info(&declaration->source_position);
+ ir_entity *entity = declaration->v.entity;
+ 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(is_atomic_entity(entity)) {
+ assert(initializer->type == INITIALIZER_VALUE);
+ initializer_value_t *initializer_value = &initializer->value;
+
+ ir_node *value = expression_to_firm(initializer_value->value);
+ ir_node *store = new_d_Store(dbgi, memory, addr, value);
+ ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
+ set_store(store_mem);
+ return;
+ }
+
+ /* create a "template" entity which is copied to the entity on the stack */
+ ident *id = unique_ident("initializer");
+ ir_type *irtype = get_ir_type(declaration->type);
+ ir_type *global_type = get_glob_type();
+ ir_entity *init_entity = new_entity(global_type, id, irtype);
+ set_entity_ld_ident(init_entity, id);
+
+ set_entity_variability(init_entity, variability_initialized);
+ set_entity_visibility(init_entity, visibility_local);
+
+ ir_graph *old_current_ir_graph = current_ir_graph;
+ current_ir_graph = get_const_code_irg();
+
+ type_t *type = skip_typeref(declaration->type);
+ create_initializer_object(initializer, type, init_entity, NULL, 0);
+
+ assert(current_ir_graph == get_const_code_irg());
+ current_ir_graph = old_current_ir_graph;
+
+ ir_node *src_addr = create_symconst(dbgi, init_entity);
+ ir_node *copyb = new_d_CopyB(dbgi, memory, addr, src_addr, irtype);
+
+ ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
+ set_store(copyb_mem);
+}
+