X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;ds=inline;f=ast2firm.c;h=a7d60dd7346864eb3a592786b20736ddac5d3342;hb=99ae9203c924c96590375ffb7791dd77a75804d1;hp=28f45f9e24d7ea1ddca662d8ea72a8d7e3b54a5e;hpb=efd9489cc7c5aeeef959473d5883c17885533224;p=cparser diff --git a/ast2firm.c b/ast2firm.c index 28f45f9..a7d60dd 100644 --- a/ast2firm.c +++ b/ast2firm.c @@ -2524,6 +2524,11 @@ static ir_node *create_cast(dbg_info *dbgi, ir_node *value_node, type_t *from_type, type_t *type) { type = skip_typeref(type); + if (type == type_void) { + /* make sure firm type is constructed */ + (void) get_ir_type(type); + return NULL; + } if (!is_type_scalar(type)) { /* make sure firm type is constructed */ (void) get_ir_type(type); @@ -4052,7 +4057,7 @@ static void advance_current_object(type_path_t *path) /* 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); + size_t len = ARR_LEN(path->path); if (len > 1) { ascend_from_subtype(path); @@ -4244,14 +4249,68 @@ static ir_initializer_t *create_ir_initializer( panic("unknown initializer"); } +/** ANSI C §6.7.8:21: If there are fewer initializers [..] than there + * are elements [...] the remainder of the aggregate shall be initialized + * implicitly the same as objects that have static storage duration. */ +static void create_dynamic_null_initializer(ir_entity *entity, dbg_info *dbgi, + ir_node *base_addr) +{ + /* for unions we must NOT do anything for null initializers */ + ir_type *owner = get_entity_owner(entity); + if (is_Union_type(owner)) { + return; + } + + ir_type *ent_type = get_entity_type(entity); + /* create sub-initializers for a compound type */ + if (is_compound_type(ent_type)) { + unsigned n_members = get_compound_n_members(ent_type); + for (unsigned n = 0; n < n_members; ++n) { + ir_entity *member = get_compound_member(ent_type, n); + ir_node *addr = new_d_simpleSel(dbgi, new_NoMem(), base_addr, + member); + create_dynamic_null_initializer(member, dbgi, addr); + } + return; + } + if (is_Array_type(ent_type)) { + assert(has_array_upper_bound(ent_type, 0)); + long n = get_array_upper_bound_int(ent_type, 0); + for (long i = 0; i < n; ++i) { + ir_tarval *index_tv = new_tarval_from_long(i, mode_uint); + ir_node *cnst = new_d_Const(dbgi, index_tv); + ir_node *in[1] = { cnst }; + ir_entity *arrent = get_array_element_entity(ent_type); + ir_node *addr = new_d_Sel(dbgi, new_NoMem(), base_addr, 1, in, + arrent); + create_dynamic_null_initializer(arrent, dbgi, addr); + } + return; + } + + ir_mode *value_mode = get_type_mode(ent_type); + ir_node *node = new_Const_long(value_mode, 0); + + /* 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; + } + + 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); +} + 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: { - /* NULL is undefined for dynamic initializers */ + case IR_INITIALIZER_NULL: + create_dynamic_null_initializer(entity, dbgi, base_addr); return; - } case IR_INITIALIZER_CONST: { ir_node *node = get_initializer_const_value(initializer); ir_type *ent_type = get_entity_type(entity); @@ -4812,17 +4871,14 @@ static void if_statement_to_firm(if_statement_t *statement) ir_node *fallthrough_block = NULL; /* the true (blocks) */ - ir_node *true_block = NULL; - if (statement->true_statement != NULL) { - true_block = new_immBlock(); - set_cur_block(true_block); - statement_to_firm(statement->true_statement); - if (get_cur_block() != NULL) { - ir_node *jmp = new_Jmp(); - if (fallthrough_block == NULL) - fallthrough_block = new_immBlock(); - add_immBlock_pred(fallthrough_block, jmp); - } + ir_node *const true_block = new_immBlock(); + set_cur_block(true_block); + statement_to_firm(statement->true_statement); + if (get_cur_block() != NULL) { + ir_node *jmp = new_Jmp(); + if (fallthrough_block == NULL) + fallthrough_block = new_immBlock(); + add_immBlock_pred(fallthrough_block, jmp); } /* the false (blocks) */ @@ -4842,13 +4898,10 @@ static void if_statement_to_firm(if_statement_t *statement) /* create the condition */ if (cur_block != NULL) { - if (true_block == NULL || false_block == NULL) { + if (false_block == NULL) { if (fallthrough_block == NULL) fallthrough_block = new_immBlock(); - if (true_block == NULL) - true_block = fallthrough_block; - if (false_block == NULL) - false_block = fallthrough_block; + false_block = fallthrough_block; } set_cur_block(cur_block); @@ -4869,14 +4922,10 @@ static void if_statement_to_firm(if_statement_t *statement) static void while_statement_to_firm(while_statement_t *statement) { - ir_node *jmp = NULL; - if (get_cur_block() != NULL) { - jmp = new_Jmp(); - } - /* create the header block */ ir_node *header_block = new_immBlock(); - if (jmp != NULL) { + if (get_cur_block() != NULL) { + ir_node *const jmp = new_Jmp(); add_immBlock_pred(header_block, jmp); } @@ -4896,7 +4945,7 @@ static void while_statement_to_firm(while_statement_t *statement) break_label = old_break_label; if (get_cur_block() != NULL) { - jmp = new_Jmp(); + ir_node *const jmp = new_Jmp(); add_immBlock_pred(header_block, jmp); } @@ -4933,18 +4982,13 @@ static void while_statement_to_firm(while_statement_t *statement) static void do_while_statement_to_firm(do_while_statement_t *statement) { - ir_node *jmp = NULL; - if (get_cur_block() != NULL) { - jmp = new_Jmp(); - } - /* create the header block */ ir_node *header_block = new_immBlock(); /* the loop body */ ir_node *body_block = new_immBlock(); - if (jmp != NULL) { - add_immBlock_pred(body_block, jmp); + if (get_cur_block() != NULL) { + add_immBlock_pred(body_block, new_Jmp()); } ir_node *old_continue_label = continue_label; @@ -5031,28 +5075,23 @@ static void for_statement_to_firm(for_statement_t *statement) ir_node *const false_block = new_immBlock(); /* the loop body */ - ir_node *body_block; - if (statement->body != NULL) { - ir_node *const old_continue_label = continue_label; - ir_node *const old_break_label = break_label; - continue_label = step_block; - break_label = false_block; - - body_block = new_immBlock(); - set_cur_block(body_block); - statement_to_firm(statement->body); + ir_node *const old_continue_label = continue_label; + ir_node *const old_break_label = break_label; + continue_label = step_block; + break_label = false_block; - assert(continue_label == step_block); - assert(break_label == false_block); - continue_label = old_continue_label; - break_label = old_break_label; + ir_node *const body_block = new_immBlock(); + set_cur_block(body_block); + statement_to_firm(statement->body); - if (get_cur_block() != NULL) { - jmp = new_Jmp(); - add_immBlock_pred(step_block, jmp); - } - } else { - body_block = step_block; + assert(continue_label == step_block); + assert(break_label == false_block); + continue_label = old_continue_label; + break_label = old_break_label; + + if (get_cur_block() != NULL) { + jmp = new_Jmp(); + add_immBlock_pred(step_block, jmp); } /* create the condition */ @@ -5121,8 +5160,8 @@ static void switch_statement_to_firm(switch_statement_t *statement) current_switch = statement; /* determine a free number for the default label */ - unsigned long num_cases = 0; - long def_nr = 0; + unsigned long num_cases = 0; + long default_proj_nr = 0; for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) { if (l->expression == NULL) { /* default case */ @@ -5130,18 +5169,18 @@ static void switch_statement_to_firm(switch_statement_t *statement) } if (l->last_case >= l->first_case) num_cases += l->last_case - l->first_case + 1; - if (l->last_case > def_nr) - def_nr = l->last_case; + if (l->last_case > default_proj_nr) + default_proj_nr = l->last_case; } - if (def_nr == INT_MAX) { + if (default_proj_nr == INT_MAX) { /* Bad: an overflow will occur, we cannot be sure that the * maximum + 1 is a free number. Scan the values a second * time to find a free number. */ unsigned char *bits = xmalloc((num_cases + 7) >> 3); - memset(bits, 0, (num_cases + 7) >> 3); + memset(bits, 0, (num_cases + 7) >> 3); for (case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) { if (l->expression == NULL) { /* default case */ @@ -5167,16 +5206,18 @@ static void switch_statement_to_firm(switch_statement_t *statement) break; free(bits); - def_nr = i; + default_proj_nr = i; } else { - ++def_nr; + ++default_proj_nr; } - statement->default_proj_nr = def_nr; - - if (statement->body != NULL) { - statement_to_firm(statement->body); + statement->default_proj_nr = default_proj_nr; + /* safety check: cond might already be folded to a Bad */ + if (cond != NULL && is_Cond(cond)) { + set_Cond_default_proj(cond, default_proj_nr); } + statement_to_firm(statement->body); + if (get_cur_block() != NULL) { ir_node *jmp = new_Jmp(); add_immBlock_pred(get_break_label(), jmp); @@ -5184,8 +5225,7 @@ static void switch_statement_to_firm(switch_statement_t *statement) if (!saw_default_label && first_block != NULL) { set_cur_block(first_block); - ir_node *const proj = new_d_defaultProj(dbgi, cond, - statement->default_proj_nr); + ir_node *const proj = new_d_Proj(dbgi, cond, mode_X, default_proj_nr); add_immBlock_pred(get_break_label(), proj); } @@ -5208,11 +5248,14 @@ static void case_label_to_firm(const case_label_statement_t *statement) dbg_info *dbgi = get_dbg_info(&statement->base.source_position); - ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp()); - ir_node *proj; ir_node *block = new_immBlock(); + if (get_cur_block() != NULL) { + /* Fallthrough from previous case */ + add_immBlock_pred(block, new_Jmp()); + } + if (current_switch_cond != NULL) { set_cur_block(get_nodes_block(current_switch_cond)); if (statement->expression != NULL) { @@ -5226,22 +5269,17 @@ static void case_label_to_firm(const case_label_statement_t *statement) } while (pn++ < end_pn); } else { saw_default_label = true; - proj = new_d_defaultProj(dbgi, current_switch_cond, - current_switch->default_proj_nr); + proj = new_d_Proj(dbgi, current_switch_cond, mode_X, + current_switch->default_proj_nr); add_immBlock_pred(block, proj); } } - if (fallthrough != NULL) { - add_immBlock_pred(block, fallthrough); - } mature_immBlock(block); set_cur_block(block); - if (statement->statement != NULL) { - statement_to_firm(statement->statement); - } + statement_to_firm(statement->statement); } static void label_to_firm(const label_statement_t *statement) @@ -5257,9 +5295,7 @@ static void label_to_firm(const label_statement_t *statement) keep_alive(block); keep_all_memory(block); - if (statement->statement != NULL) { - statement_to_firm(statement->statement); - } + statement_to_firm(statement->statement); } static void goto_to_firm(const goto_statement_t *statement) @@ -5790,7 +5826,7 @@ static void add_function_pointer(ir_type *segment, ir_entity *method, set_entity_ld_ident(ptr, new_id_from_chars("", 0)); set_entity_compiler_generated(ptr, 1); - set_entity_visibility(ptr, ir_visibility_local); + set_entity_visibility(ptr, ir_visibility_private); add_entity_linkage(ptr, IR_LINKAGE_CONSTANT|IR_LINKAGE_HIDDEN_USER); set_atomic_ent_value(ptr, val); }