Simplify case_label_to_firm() a bit, which can be done since new_immBlock() does...
[cparser] / ast2firm.c
index f28509c..a7d60dd 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,12 +4249,18 @@ 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)
 {
-       /* 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. */
+       /* 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)) {
@@ -4262,6 +4273,20 @@ static void create_dynamic_null_initializer(ir_entity *entity, dbg_info *dbgi,
                }
                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);
@@ -4846,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) */
@@ -4876,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);
@@ -4903,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);
        }
 
@@ -4930,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);
        }
 
@@ -4967,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;
@@ -5065,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 */
@@ -5211,9 +5216,7 @@ static void switch_statement_to_firm(switch_statement_t *statement)
                set_Cond_default_proj(cond, default_proj_nr);
        }
 
-       if (statement->body != NULL) {
-               statement_to_firm(statement->body);
-       }
+       statement_to_firm(statement->body);
 
        if (get_cur_block() != NULL) {
                ir_node *jmp = new_Jmp();
@@ -5245,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) {
@@ -5270,15 +5276,10 @@ static void case_label_to_firm(const case_label_statement_t *statement)
                }
        }
 
-       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)
@@ -5294,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)
@@ -5827,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);
 }