Remove the unused STATEMENT_LOCAL_LABEL/struct local_label_statement_t. It never...
[cparser] / ast2firm.c
index f89c926..c30463c 100644 (file)
@@ -201,7 +201,7 @@ static void init_atomic_modes(void)
        mode_uint = atomic_modes[ATOMIC_TYPE_UINT];
 
        /* there's no real void type in firm */
-       atomic_modes[ATOMIC_TYPE_VOID] = mode_int;
+       atomic_modes[ATOMIC_TYPE_VOID] = atomic_modes[ATOMIC_TYPE_CHAR];
 
        /* initialize pointer modes */
        char            name[64];
@@ -279,18 +279,22 @@ static unsigned get_type_size_const(type_t *type)
        panic("Trying to determine size of invalid type");
 }
 
+static ir_node *get_vla_size(array_type_t *const type)
+{
+       ir_node *size_node = type->size_node;
+       if (size_node == NULL) {
+               size_node = expression_to_firm(type->size_expression);
+               type->size_node = size_node;
+       }
+       return size_node;
+}
+
 static ir_node *get_type_size(type_t *type)
 {
        type = skip_typeref(type);
 
        if (is_type_array(type) && type->array.is_vla) {
-               ir_node *size_node = type->array.size_node;
-               if (size_node == NULL) {
-                       size_node = expression_to_firm(type->array.size_expression);
-                       assert(!is_Const(size_node));
-                       type->array.size_node = size_node;
-               }
-
+               ir_node *size_node = get_vla_size(&type->array);
                ir_node *elem_size = get_type_size(type->array.element_type);
                ir_mode *mode      = get_irn_mode(size_node);
                ir_node *real_size = new_d_Mul(NULL, size_node, elem_size, mode);
@@ -1321,7 +1325,7 @@ static ir_node *deref_address(dbg_info *const dbgi, type_t *const type,
        ir_node *const load_mem = new_d_Proj(dbgi, load, mode_M, pn_Load_M);
        ir_node *const load_res = new_d_Proj(dbgi, load, mode,   pn_Load_res);
 
-       if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE) {
+       if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE && !is_Bad(load)) {
                set_Load_volatility(load, volatility_is_volatile);
        }
 
@@ -1597,9 +1601,11 @@ static ir_node *process_builtin_call(const call_expression_t *call)
                return _expression_to_firm(argument);
        }
        case T___builtin_va_end:
+               /* evaluate the argument of va_end for its side effects */
+               _expression_to_firm(call->arguments->expression);
                return NULL;
        default:
-               panic("Unsupported builtin found\n");
+               panic("unsupported builtin found");
        }
 }
 
@@ -1752,7 +1758,7 @@ static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
        if (is_type_scalar(type)) {
                ir_node  *store     = new_d_Store(dbgi, memory, addr, value);
                ir_node  *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
-               if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE)
+               if (type->base.qualifiers & TYPE_QUALIFIER_VOLATILE && !is_Bad(store))
                        set_Store_volatility(store, volatility_is_volatile);
                set_store(store_mem);
        } else {
@@ -1823,8 +1829,10 @@ static void bitfield_store_to_firm(dbg_info *dbgi,
        set_store(store_mem);
 
        if (set_volatile) {
-               set_Load_volatility(load, volatility_is_volatile);
-               set_Store_volatility(store, volatility_is_volatile);
+               if (!is_Bad(load))
+                       set_Load_volatility(load, volatility_is_volatile);
+               if (!is_Bad(store))
+                       set_Store_volatility(store, volatility_is_volatile);
        }
 }
 
@@ -2212,16 +2220,17 @@ static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
  * produces a 0/1 depending of the value of a mode_b node
  */
 static ir_node *produce_condition_result(const expression_t *expression,
-                                         dbg_info *dbgi)
+                                         ir_mode *mode, dbg_info *dbgi)
 {
-       ir_mode *mode      = get_ir_mode(expression->base.type);
        ir_node *cur_block = get_cur_block();
 
        ir_node *one_block = new_immBlock();
+       set_cur_block(one_block);
        ir_node *one       = new_Const(get_mode_one(mode));
        ir_node *jmp_one   = new_d_Jmp(dbgi);
 
        ir_node *zero_block = new_immBlock();
+       set_cur_block(zero_block);
        ir_node *zero       = new_Const(get_mode_null(mode));
        ir_node *jmp_zero   = new_d_Jmp(dbgi);
 
@@ -2242,10 +2251,14 @@ static ir_node *produce_condition_result(const expression_t *expression,
 static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
                ir_node *value, type_t *type)
 {
+       ir_mode        *const mode         = get_ir_mode(type_ptrdiff_t);
+       assert(is_type_pointer(type));
        pointer_type_t *const pointer_type = &type->pointer;
        type_t         *const points_to    = skip_typeref(pointer_type->points_to);
        unsigned              elem_size    = get_type_size_const(points_to);
 
+       value = create_conv(dbgi, value, mode);
+
        /* gcc extension: allow arithmetic with void * and function * */
        if ((elem_size == 0 && is_type_atomic(points_to, ATOMIC_TYPE_VOID)) ||
            is_type_function(points_to))  {
@@ -2256,9 +2269,8 @@ static ir_node *adjust_for_pointer_arithmetic(dbg_info *dbgi,
        if (elem_size == 1)
                return value;
 
-       value = create_conv(dbgi, value, mode_int);
-       ir_node *const cnst = new_Const_long(mode_int, (long)elem_size);
-       ir_node *const mul  = new_d_Mul(dbgi, value, cnst, mode_int);
+       ir_node *const cnst = new_Const_long(mode, (long)elem_size);
+       ir_node *const mul  = new_d_Mul(dbgi, value, cnst, mode);
        return mul;
 }
 
@@ -2317,7 +2329,7 @@ static ir_node *create_op(dbg_info *dbgi, const binary_expression_t *expression,
 
        default:
 normal_node:
-               mode = get_irn_mode(right);
+               mode = get_ir_mode(type_right);
                left = create_conv(dbgi, left, mode);
                break;
        }
@@ -2389,15 +2401,28 @@ static ir_node *create_lazy_op(const binary_expression_t *expression)
                long val = fold_constant(expression->left);
                expression_kind_t ekind = expression->base.kind;
                assert(ekind == EXPR_BINARY_LOGICAL_AND || ekind == EXPR_BINARY_LOGICAL_OR);
-               if ((ekind == EXPR_BINARY_LOGICAL_AND && val != 0) ||
-                   (ekind == EXPR_BINARY_LOGICAL_OR  && val == 0)) {
-                       return expression_to_firm(expression->right);
+               if (ekind == EXPR_BINARY_LOGICAL_AND) {
+                       if (val == 0) {
+                               return new_Const(get_mode_null(mode));
+                       }
                } else {
-                       return new_Const(get_mode_one(mode));
+                       if (val != 0) {
+                               return new_Const(get_mode_one(mode));
+                       }
                }
+
+               if (is_constant_expression(expression->right)) {
+                       long const valr = fold_constant(expression->left);
+                       return valr != 0 ?
+                               new_Const(get_mode_one(mode)) :
+                               new_Const(get_mode_null(mode));
+               }
+
+               return produce_condition_result(expression->right, mode, dbgi);
        }
 
-       return produce_condition_result((const expression_t*) expression, dbgi);
+       return produce_condition_result((const expression_t*) expression, mode,
+                                       dbgi);
 }
 
 typedef ir_node * (*create_arithmetic_func)(dbg_info *dbgi, ir_node *left,
@@ -2495,28 +2520,12 @@ static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
 
 static ir_node *array_access_addr(const array_access_expression_t *expression)
 {
-       dbg_info *dbgi      = get_dbg_info(&expression->base.source_position);
-       ir_node  *base_addr = expression_to_firm(expression->array_ref);
-       ir_node  *offset    = expression_to_firm(expression->index);
-
-       type_t  *offset_type = skip_typeref(expression->index->base.type);
-       ir_mode *mode;
-       if (is_type_signed(offset_type)) {
-               mode = get_ir_mode(type_ssize_t);
-       } else {
-               mode = get_ir_mode(type_size_t);
-       }
-       offset = create_conv(dbgi, offset, mode);
-
-       type_t *ref_type = skip_typeref(expression->array_ref->base.type);
-       assert(is_type_pointer(ref_type));
-       pointer_type_t *pointer_type = &ref_type->pointer;
-
-       ir_node *elem_size_const = get_type_size(pointer_type->points_to);
-       elem_size_const          = create_conv(dbgi, elem_size_const, mode);
-       ir_node *real_offset     = new_d_Mul(dbgi, offset, elem_size_const,
-                                            mode);
-       ir_node *result          = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
+       dbg_info *dbgi        = get_dbg_info(&expression->base.source_position);
+       ir_node  *base_addr   = expression_to_firm(expression->array_ref);
+       ir_node  *offset      = expression_to_firm(expression->index);
+       type_t   *ref_type    = skip_typeref(expression->array_ref->base.type);
+       ir_node  *real_offset = adjust_for_pointer_arithmetic(dbgi, offset, ref_type);
+       ir_node  *result      = new_d_Add(dbgi, base_addr, real_offset, mode_P_data);
 
        return result;
 }
@@ -2688,12 +2697,12 @@ long fold_constant(const expression_t *expression)
        current_ir_graph = old_current_ir_graph;
 
        if (!is_Const(cnst)) {
-               panic("couldn't fold constant\n");
+               panic("couldn't fold constant");
        }
 
        tarval *tv = get_Const_tarval(cnst);
        if (!tarval_is_long(tv)) {
-               panic("result of constant folding is not integer\n");
+               panic("result of constant folding is not integer");
        }
 
        constant_folding = constant_folding_old;
@@ -2722,6 +2731,7 @@ static ir_node *conditional_to_firm(const conditional_expression_t *expression)
 
        /* create the true block */
        ir_node *true_block  = new_immBlock();
+       set_cur_block(true_block);
 
        ir_node *true_val = expression->true_expression != NULL ?
                expression_to_firm(expression->true_expression) : NULL;
@@ -2729,6 +2739,7 @@ static ir_node *conditional_to_firm(const conditional_expression_t *expression)
 
        /* create the false block */
        ir_node *false_block = new_immBlock();
+       set_cur_block(false_block);
 
        ir_node *false_val = expression_to_firm(expression->false_expression);
        ir_node *false_jmp = new_Jmp();
@@ -2778,13 +2789,17 @@ static ir_node *select_addr(const select_expression_t *expression)
        entity_t *entry = expression->compound_entry;
        assert(entry->kind == ENTITY_COMPOUND_MEMBER);
        assert(entry->declaration.kind == DECLARATION_KIND_COMPOUND_MEMBER);
-       ir_entity *irentity = entry->compound_member.entity;
-
-       assert(irentity != NULL);
 
-       ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, irentity);
-
-       return sel;
+       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)
@@ -2854,6 +2869,7 @@ static ir_node *classify_type_to_firm(const classify_type_expression_t *const ex
                                                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 */
@@ -2994,17 +3010,18 @@ static ir_node *dereference_addr(const unary_expression_t *const expression)
 static ir_node *expression_to_addr(const expression_t *expression)
 {
        switch(expression->kind) {
-       case EXPR_REFERENCE:
-               return reference_addr(&expression->reference);
        case EXPR_ARRAY_ACCESS:
                return array_access_addr(&expression->array_access);
-       case EXPR_SELECT:
-               return select_addr(&expression->select);
        case EXPR_CALL:
                return call_expression_to_firm(&expression->call);
-       case EXPR_UNARY_DEREFERENCE: {
+       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;
        }
@@ -3044,9 +3061,7 @@ static ir_node *get_label_block(label_t *label)
        ir_graph *rem    = current_ir_graph;
        current_ir_graph = current_function;
 
-       ir_node *old_cur_block = get_cur_block();
-       ir_node *block         = new_immBlock();
-       set_cur_block(old_cur_block);
+       ir_node *block = new_immBlock();
 
        label->block = block;
 
@@ -3234,7 +3249,8 @@ static ir_node *expression_to_firm(const expression_t *expression)
 
        /* we have to produce a 0/1 from the mode_b expression */
        dbg_info *dbgi = get_dbg_info(&expression->base.source_position);
-       return produce_condition_result(expression, dbgi);
+       ir_mode  *mode = get_ir_mode(expression->base.type);
+       return produce_condition_result(expression, mode, dbgi);
 }
 
 /**
@@ -3255,9 +3271,7 @@ static ir_node *create_condition_evaluation(const expression_t *expression,
        case EXPR_BINARY_LOGICAL_AND: {
                const binary_expression_t *binary_expression = &expression->binary;
 
-               ir_node *cur_block   = get_cur_block();
                ir_node *extra_block = new_immBlock();
-               set_cur_block(cur_block);
                create_condition_evaluation(binary_expression->left, extra_block,
                                            false_block);
                mature_immBlock(extra_block);
@@ -3269,9 +3283,7 @@ static ir_node *create_condition_evaluation(const expression_t *expression,
        case EXPR_BINARY_LOGICAL_OR: {
                const binary_expression_t *binary_expression = &expression->binary;
 
-               ir_node *cur_block   = get_cur_block();
                ir_node *extra_block = new_immBlock();
-               set_cur_block(cur_block);
                create_condition_evaluation(binary_expression->left, true_block,
                                            extra_block);
                mature_immBlock(extra_block);
@@ -3308,9 +3320,7 @@ static ir_node *create_condition_evaluation(const expression_t *expression,
        }
 
        add_immBlock_pred(true_block, true_proj);
-       if (false_block != NULL) {
-               add_immBlock_pred(false_block, false_proj);
-       }
+       add_immBlock_pred(false_block, false_proj);
 
        set_cur_block(NULL);
        return cond_expr;
@@ -4315,8 +4325,13 @@ static void declaration_statement_to_firm(declaration_statement_t *statement)
        entity_t *const last   = statement->declarations_end;
        if (entity != NULL) {
                for ( ;; entity = entity->base.next) {
-                       if (is_declaration(entity))
+                       if (is_declaration(entity)) {
                                initialize_local_declaration(entity);
+                       } else if (entity->kind == ENTITY_TYPEDEF) {
+                               type_t *const type = entity->typedefe.type;
+                               if (is_type_array(type) && type->array.is_vla)
+                                       get_vla_size(&type->array);
+                       }
                        if (entity == last)
                                break;
                }
@@ -4333,6 +4348,7 @@ static void if_statement_to_firm(if_statement_t *statement)
        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();
@@ -4346,6 +4362,7 @@ static void if_statement_to_firm(if_statement_t *statement)
        ir_node *false_block = NULL;
        if (statement->false_statement != NULL) {
                false_block = new_immBlock();
+               set_cur_block(false_block);
 
                statement_to_firm(statement->false_statement);
                if (get_cur_block() != NULL) {
@@ -4403,6 +4420,7 @@ static void while_statement_to_firm(while_statement_t *statement)
        break_label                 = NULL;
 
        ir_node *body_block = new_immBlock();
+       set_cur_block(body_block);
        statement_to_firm(statement->body);
        ir_node *false_block = break_label;
 
@@ -4467,6 +4485,7 @@ static void do_while_statement_to_firm(do_while_statement_t *statement)
        continue_label              = header_block;
        break_label                 = NULL;
 
+       set_cur_block(body_block);
        statement_to_firm(statement->body);
        ir_node *false_block = break_label;
 
@@ -4490,9 +4509,7 @@ static void do_while_statement_to_firm(do_while_statement_t *statement)
        create_condition_evaluation(statement->condition, body_block, false_block);
        mature_immBlock(body_block);
        mature_immBlock(header_block);
-       if (false_block != NULL) {
-               mature_immBlock(false_block);
-       }
+       mature_immBlock(false_block);
 
        set_cur_block(false_block);
 }
@@ -4529,6 +4546,7 @@ static void for_statement_to_firm(for_statement_t *statement)
 
        /* create the step block */
        ir_node *const step_block = new_immBlock();
+       set_cur_block(step_block);
        if (statement->step != NULL) {
                expression_to_firm(statement->step);
        }
@@ -4536,6 +4554,7 @@ static void for_statement_to_firm(for_statement_t *statement)
 
        /* create the header block */
        ir_node *const header_block = new_immBlock();
+       set_cur_block(header_block);
        if (jmp != NULL) {
                add_immBlock_pred(header_block, jmp);
        }
@@ -4545,7 +4564,7 @@ static void for_statement_to_firm(for_statement_t *statement)
        ir_node *const false_block = new_immBlock();
 
        /* the loop body */
-       ir_node * body_block;
+       ir_node *body_block;
        if (statement->body != NULL) {
                ir_node *const old_continue_label = continue_label;
                ir_node *const old_break_label    = break_label;
@@ -4553,6 +4572,7 @@ static void for_statement_to_firm(for_statement_t *statement)
                break_label    = false_block;
 
                body_block = new_immBlock();
+               set_cur_block(body_block);
                statement_to_firm(statement->body);
 
                assert(continue_label == step_block);
@@ -4605,9 +4625,7 @@ static void create_jump_statement(const statement_t *statement,
 static ir_node *get_break_label(void)
 {
        if (break_label == NULL) {
-               ir_node *cur_block = get_cur_block();
                break_label = new_immBlock();
-               set_cur_block(cur_block);
        }
        return break_label;
 }
@@ -4721,10 +4739,9 @@ static void case_label_to_firm(const case_label_statement_t *statement)
        ir_node *const fallthrough = (get_cur_block() == NULL ? NULL : new_Jmp());
 
        ir_node *proj;
-       ir_node *old_block = get_nodes_block(current_switch_cond);
        ir_node *block     = new_immBlock();
 
-       set_cur_block(old_block);
+       set_cur_block(get_nodes_block(current_switch_cond));
        if (statement->expression != NULL) {
                long pn     = statement->first_case;
                long end_pn = statement->last_case;
@@ -5098,10 +5115,6 @@ static void statement_to_firm(statement_t *statement)
        case STATEMENT_LABEL:
                label_to_firm(&statement->label);
                return;
-       case STATEMENT_LOCAL_LABEL:
-               /* local labels transform the semantics of labels while parsing
-                * they don't need any special treatment here */
-               return;
        case STATEMENT_GOTO:
                goto_to_firm(&statement->gotos);
                return;
@@ -5115,7 +5128,7 @@ static void statement_to_firm(statement_t *statement)
                leave_statement_to_firm(&statement->leave);
                return;
        }
-       panic("Statement not implemented\n");
+       panic("statement not implemented");
 }
 
 static int count_local_variables(const entity_t *entity,
@@ -5239,6 +5252,10 @@ static void initialize_function_parameters(entity_t *entity)
  */
 static void handle_decl_modifier_irg(ir_graph_ptr irg, decl_modifiers_t decl_modifiers)
 {
+       if (decl_modifiers & DM_RETURNS_TWICE) {
+               /* TRUE if the declaration includes __attribute__((returns_twice)) */
+               set_irg_additional_property(irg, mtp_property_returns_twice);
+       }
        if (decl_modifiers & DM_NORETURN) {
                /* TRUE if the declaration includes the Microsoft
                   __declspec(noreturn) specifier. */