Remove unnecessary test in ast2firm: A for-statement always has a body.
[cparser] / ast2firm.c
index 109ece6..829a4d2 100644 (file)
@@ -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,32 +4249,68 @@ static ir_initializer_t *create_ir_initializer(
        panic("unknown initializer");
 }
 
-static void create_dynamic_initializer_sub(ir_initializer_t *initializer,
-               ir_entity *entity, ir_type *type, dbg_info *dbgi, ir_node *base_addr)
+/** 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)
 {
-       switch(get_initializer_kind(initializer)) {
-       case IR_INITIALIZER_NULL: {
-               /* 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. */
-               ir_type *ent_type = get_entity_type(entity);
-               ir_mode *value_mode = get_type_mode(ent_type);
-               ir_node *node = new_Const_long(value_mode, 0);
+       /* for unions we must NOT do anything for null initializers */
+       ir_type *owner = get_entity_owner(entity);
+       if (is_Union_type(owner)) {
+               return;
+       }
 
-               /* 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_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;
+       }
 
-               assert(get_type_mode(type) == get_irn_mode(node));
-               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);
+       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:
+               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);
@@ -4830,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) */
@@ -4860,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);
@@ -4887,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);
        }
 
@@ -4914,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);
        }
 
@@ -5049,28 +5080,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;
+       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 body_block = new_immBlock();
+       set_cur_block(body_block);
+       statement_to_firm(statement->body);
 
-               assert(continue_label == step_block);
-               assert(break_label    == false_block);
-               continue_label = old_continue_label;
-               break_label    = old_break_label;
+       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);
-               }
-       } else {
-               body_block = step_block;
+       if (get_cur_block() != NULL) {
+               jmp = new_Jmp();
+               add_immBlock_pred(step_block, jmp);
        }
 
        /* create the condition */
@@ -5139,8 +5165,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 */
@@ -5148,18 +5174,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 */
@@ -5185,11 +5211,15 @@ 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 = 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->default_proj_nr = def_nr;
 
        if (statement->body != NULL) {
                statement_to_firm(statement->body);
@@ -5202,8 +5232,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);
        }
 
@@ -5244,8 +5273,8 @@ 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);
                }
@@ -5808,7 +5837,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);
 }