implement LOCAL_VARIBALE_ENTITY in reference_expression_addr
[cparser] / ast2firm.c
index 999e75a..334ac62 100644 (file)
@@ -12,6 +12,7 @@
 #include "ast2firm.h"
 
 #include "adt/error.h"
+#include "adt/array.h"
 #include "token_t.h"
 #include "type_t.h"
 #include "ast_t.h"
@@ -27,10 +28,11 @@ static type_t *type_const_char;
 static type_t *type_void;
 static type_t *type_int;
 
-static int next_value_number_function;
-static ir_node *continue_label;
-static ir_node *break_label;
-static ir_node *current_switch_cond;
+static int       next_value_number_function;
+static ir_node  *continue_label;
+static ir_node  *break_label;
+static ir_node  *current_switch_cond;
+static ir_node **imature_blocks;
 
 typedef enum declaration_type_t {
        DECLARATION_TYPE_UNKNOWN,
@@ -38,7 +40,8 @@ typedef enum declaration_type_t {
        DECLARATION_TYPE_GLOBAL_VARIABLE,
        DECLARATION_TYPE_LOCAL_VARIABLE,
        DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY,
-       DECLARATION_TYPE_COMPOUND_MEMBER
+       DECLARATION_TYPE_COMPOUND_MEMBER,
+       DECLARATION_TYPE_LABEL_BLOCK,
 } declaration_type_t;
 
 static ir_type *get_ir_type(type_t *type);
@@ -253,6 +256,8 @@ static unsigned count_parameters(const function_type_t *function_type)
 
 
 
+static long fold_constant(const expression_t *expression);
+
 static ir_type *create_atomic_type(const atomic_type_t *type)
 {
        ir_mode *mode   = get_atomic_mode(type);
@@ -314,21 +319,23 @@ static ir_type *create_array_type(array_type_t *type)
        type_t  *element_type    = type->element_type;
        ir_type *ir_element_type = get_ir_type(element_type);
 
-       /* TODO... */
-       int n_elements = 0;
-       panic("TODO arraytpye size not implemented yet");
+       ident   *id      = unique_ident("array");
+       ir_type *ir_type = new_type_array(id, 1, ir_element_type);
+
+       if(type->size != NULL) {
+               int n_elements = fold_constant(type->size);
 
-       ir_type *ir_type = new_type_array(unique_ident("array"), 1, ir_element_type);
-       set_array_bounds_int(ir_type, 0, 0, n_elements);
+               set_array_bounds_int(ir_type, 0, 0, n_elements);
 
-       size_t elemsize = get_type_size_bytes(ir_element_type);
-       int align = get_type_alignment_bytes(ir_element_type);
-       if(elemsize % align > 0) {
-               elemsize += align - (elemsize % align);
+               size_t elemsize = get_type_size_bytes(ir_element_type);
+               int align = get_type_alignment_bytes(ir_element_type);
+               if(elemsize % align > 0) {
+                       elemsize += align - (elemsize % align);
+               }
+               set_type_size_bytes(ir_type, n_elements * elemsize);
+               set_type_alignment_bytes(ir_type, align);
+               set_type_state(ir_type, layout_fixed);
        }
-       set_type_size_bytes(ir_type, n_elements * elemsize);
-       set_type_alignment_bytes(ir_type, align);
-       set_type_state(ir_type, layout_fixed);
 
        return ir_type;
 }
@@ -488,6 +495,12 @@ static ir_type *get_ir_type(type_t *type)
 static inline ir_mode *get_ir_mode(type_t *type)
 {
        ir_type *irtype = get_ir_type(type);
+
+       /* firm doesn't report a mode for arrays somehow... */
+       if(is_Array_type(irtype)) {
+               return mode_P;
+       }
+
        ir_mode *mode   = get_type_mode(irtype);
        assert(mode != NULL);
        return mode;
@@ -506,11 +519,10 @@ static ir_entity* get_function_entity(declaration_t *declaration)
        ir_type  *ir_type_method = get_ir_type(declaration->type);
        assert(is_Method_type(ir_type_method));
 
-       type_t    *type   = declaration->type;
        ir_entity *entity = new_entity(global_type, id, ir_type_method);
        set_entity_ld_ident(entity, id);
-       if(declaration->storage_class & STORAGE_CLASS_STATIC
-                       || type->qualifiers & TYPE_QUALIFIER_INLINE) {
+       if(declaration->storage_class == STORAGE_CLASS_STATIC
+                       || declaration->is_inline) {
                set_entity_visibility(entity, visibility_local);
        } else if(declaration->init.statement != NULL) {
                set_entity_visibility(entity, visibility_external_visible);
@@ -609,7 +621,7 @@ static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
 {
        dbg_info      *dbgi        = get_dbg_info(&ref->expression.source_position);
        declaration_t *declaration = ref->declaration;
-       type_t        *type        = declaration->type;
+       type_t        *type        = skip_typeref(declaration->type);
        ir_mode       *mode        = get_ir_mode(type);
 
        switch((declaration_type_t) declaration->declaration_type) {
@@ -623,10 +635,27 @@ static ir_node *reference_expression_to_firm(const reference_expression_t *ref)
        case DECLARATION_TYPE_GLOBAL_VARIABLE: {
                ir_entity *entity   = declaration->v.entity;
                ir_node   *symconst = create_symconst(dbgi, entity);
-               return load_from_expression_addr(type, symconst, dbgi);
+
+               if(type->type == TYPE_ARRAY) {
+                       return symconst;
+               } else {
+                       return load_from_expression_addr(type, symconst, dbgi);
+               }
+       }
+       case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
+               ir_entity *entity = declaration->v.entity;
+               ir_node   *frame  = get_irg_frame(current_ir_graph);
+               ir_node   *sel    = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
+
+               if(type->type == TYPE_ARRAY) {
+                       return sel;
+               } else {
+                       return load_from_expression_addr(type, sel, dbgi);
+               }
        }
-       case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY:
+
        case DECLARATION_TYPE_COMPOUND_MEMBER:
+       case DECLARATION_TYPE_LABEL_BLOCK:
                panic("not implemented reference type");
        }
 
@@ -651,8 +680,15 @@ static ir_node *reference_addr(const reference_expression_t *ref)
                ir_node   *symconst = create_symconst(dbgi, entity);
                return symconst;
        }
-       case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY:
+       case DECLARATION_TYPE_LOCAL_VARIABLE_ENTITY: {
+               ir_entity *entity = declaration->v.entity;
+               ir_node   *frame  = get_irg_frame(current_ir_graph);
+               ir_node   *sel    = new_d_simpleSel(dbgi, new_NoMem(), frame, entity);
+
+               return sel;
+       }
        case DECLARATION_TYPE_COMPOUND_MEMBER:
+       case DECLARATION_TYPE_LABEL_BLOCK:
                panic("not implemented reference type");
        }
 
@@ -1023,6 +1059,42 @@ static ir_node *create_sub(const binary_expression_t *expression)
        return res;
 }
 
+static ir_node *create_shift(const binary_expression_t *expression)
+{
+       dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
+       ir_node  *left  = expression_to_firm(expression->left);
+       ir_node  *right = expression_to_firm(expression->right);
+       type_t   *type  = expression->expression.datatype;
+       ir_mode  *mode  = get_ir_mode(type);
+
+       /* firm always wants the shift count to be unsigned */
+       right = create_conv(dbgi, right, mode_Iu);
+
+       ir_node *res;
+
+       switch(expression->type) {
+       case BINEXPR_SHIFTLEFT:
+               res = new_d_Shl(dbgi, left, right, mode);
+               break;
+       case BINEXPR_SHIFTRIGHT: {
+                expression_t *expr_left = expression->left;
+                type_t       *type_left = skip_typeref(expr_left->datatype);
+
+                if(is_type_signed(type_left)) {
+                       res = new_d_Shrs(dbgi, left, right, mode);
+                } else {
+                        res = new_d_Shr(dbgi, left, right, mode);
+                }
+                break;
+       }
+       default:
+               panic("create shift op called for non-shift op");
+       }
+
+       return res;
+}
+
+
 static ir_node *create_divmod(const binary_expression_t *expression)
 {
        dbg_info *dbgi  = get_dbg_info(&expression->expression.source_position);
@@ -1090,9 +1162,8 @@ static ir_node *binary_expression_to_firm(const binary_expression_t *expression)
        case BINEXPR_BITWISE_XOR:
                return create_arithmetic_binop(expression, new_d_Eor);
        case BINEXPR_SHIFTLEFT:
-               return create_arithmetic_binop(expression, new_d_Shl);
        case BINEXPR_SHIFTRIGHT:
-               return create_arithmetic_binop(expression, new_d_Shr);
+               return create_shift(expression);
        case BINEXPR_DIV:
        case BINEXPR_MOD:
                return create_divmod(expression);
@@ -1143,7 +1214,6 @@ static ir_node *array_access_addr(const array_access_expression_t *expression)
 static ir_node *array_access_to_firm(
                const array_access_expression_t *expression)
 {
-
        dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
        ir_node  *addr = array_access_addr(expression);
        type_t   *type = expression->expression.datatype;
@@ -1205,6 +1275,38 @@ static ir_node *conditional_to_firm(const conditional_expression_t *expression)
        return val;
 }
 
+static ir_node *select_addr(const select_expression_t *expression)
+{
+       dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
+
+       ir_node *compound_addr = expression_to_firm(expression->compound);
+
+       declaration_t *entry = expression->compound_entry;
+       assert(entry->declaration_type == DECLARATION_TYPE_COMPOUND_MEMBER);
+       ir_entity     *entity = entry->v.entity;
+
+       assert(entity != NULL);
+
+       ir_node *sel = new_d_simpleSel(dbgi, new_NoMem(), compound_addr, entity);
+
+       return sel;
+}
+
+static ir_node *select_to_firm(const select_expression_t *expression)
+{
+       dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
+       ir_node  *addr = select_addr(expression);
+       type_t   *type = expression->expression.datatype;
+
+       return load_from_expression_addr(type, addr, dbgi);
+}
+
+static ir_node *dereference_addr(const unary_expression_t *const expression)
+{
+       assert(expression->type == UNEXPR_DEREFERENCE);
+       return expression_to_firm(expression->value);
+}
+
 static ir_node *expression_to_addr(const expression_t *expression)
 {
        switch(expression->type) {
@@ -1212,6 +1314,16 @@ static ir_node *expression_to_addr(const expression_t *expression)
                return reference_addr((const reference_expression_t*) expression);
        case EXPR_ARRAY_ACCESS:
                return array_access_addr((const array_access_expression_t*) expression);
+       case EXPR_SELECT:
+               return select_addr((const select_expression_t*) expression);
+       case EXPR_UNARY: {
+               const unary_expression_t *const unary_expr =
+                       (const unary_expression_t*)expression;
+               if (unary_expr->type == UNEXPR_DEREFERENCE) {
+                       return dereference_addr(unary_expr);
+               }
+               break;
+       }
        default:
                break;
        }
@@ -1242,10 +1354,21 @@ static ir_node *_expression_to_firm(const expression_t *expression)
                return sizeof_to_firm((const sizeof_expression_t*) expression);
        case EXPR_CONDITIONAL:
                return conditional_to_firm((const conditional_expression_t*)expression);
-       default:
+       case EXPR_SELECT:
+               return select_to_firm((const select_expression_t*) expression);
+       case EXPR_FUNCTION:
+       case EXPR_OFFSETOF:
+       case EXPR_PRETTY_FUNCTION:
+       case EXPR_VA_ARG:
+       case EXPR_STATEMENT:
+       case EXPR_BUILTIN_SYMBOL:
+               panic("unimplemented expression found");
+
+       case EXPR_UNKNOWN:
+       case EXPR_INVALID:
                break;
        }
-       panic("unsupported expression found");
+       panic("invalid expression found");
 }
 
 static ir_node *expression_to_firm(const expression_t *expression)
@@ -1272,6 +1395,9 @@ static void statement_to_firm(statement_t *statement);
 
 static void return_statement_to_firm(return_statement_t *statement)
 {
+       if(get_cur_block() == NULL)
+               return;
+
        dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
        ir_node  *ret;
 
@@ -1301,6 +1427,9 @@ static void compound_statement_to_firm(compound_statement_t *compound)
 
 static void expression_statement_to_firm(expression_statement_t *statement)
 {
+       if(get_cur_block() == NULL)
+               return;
+
        expression_to_firm(statement->expression);
 }
 
@@ -1435,6 +1564,7 @@ static void do_while_statement_to_firm(do_while_statement_t *statement)
        if(get_cur_block() == NULL) {
                mature_immBlock(header_block);
                mature_immBlock(body_block);
+               mature_immBlock(false_block);
                return;
        }
 
@@ -1443,6 +1573,7 @@ static void do_while_statement_to_firm(do_while_statement_t *statement)
        mature_immBlock(header_block);
 
        /* create the condition */
+       set_cur_block(header_block);
        ir_node *condition  = expression_to_modeb(statement->condition);
        ir_node *cond       = new_d_Cond(dbgi, condition);
        ir_node *true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
@@ -1471,7 +1602,9 @@ static void for_statement_to_firm(for_statement_t *statement)
 
        /* create the step block */
        ir_node *const step_block = new_immBlock();
-       expression_to_firm(statement->step);
+       if (statement->step != NULL) {
+               expression_to_firm(statement->step);
+       }
        ir_node *const step_jmp   = new_Jmp();
 
        /* create the header block */
@@ -1482,14 +1615,24 @@ static void for_statement_to_firm(for_statement_t *statement)
        add_immBlock_pred(header_block, step_jmp);
 
        /* create the condition */
-       ir_node *const condition  = expression_to_modeb(statement->condition);
-       ir_node *const cond       = new_d_Cond(dbgi, condition);
-       ir_node *const true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
-       ir_node *const false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
+       ir_node *true_proj;
+       ir_node *false_proj;
+       if (statement->condition != NULL) {
+               ir_node *const condition  = expression_to_modeb(statement->condition);
+               ir_node *const cond       = new_d_Cond(dbgi, condition);
+               true_proj  = new_d_Proj(dbgi, cond, mode_X, pn_Cond_true);
+               false_proj = new_d_Proj(dbgi, cond, mode_X, pn_Cond_false);
+       } else {
+               keep_alive(header_block);
+               true_proj  = new_Jmp();
+               false_proj = NULL;
+       }
 
        /* the false block */
        ir_node *const false_block = new_immBlock();
-       add_immBlock_pred(false_block, false_proj);
+       if (false_proj != NULL) {
+               add_immBlock_pred(false_block, false_proj);
+       }
 
        /* the loop body */
        ir_node *const body_block = new_immBlock();
@@ -1550,8 +1693,6 @@ static void create_initializer(declaration_t *declaration)
                        set_value(declaration->v.value_number, init_node);
                } else {
                        ir_entity *entity = declaration->v.entity;
-                       ir_mode   *mode   = get_ir_mode(declaration->type);
-                       init_node         = create_conv(NULL, init_node, mode);
 
                        set_entity_variability(entity, variability_initialized);
                        set_atomic_ent_value(entity, init_node);
@@ -1564,10 +1705,16 @@ static void create_initializer(declaration_t *declaration)
 
 static void create_local_variable(declaration_t *declaration)
 {
-       bool needs_entity = declaration->address_taken;
-
        assert(declaration->declaration_type == DECLARATION_TYPE_UNKNOWN);
 
+       bool needs_entity = declaration->address_taken;
+       type_t *type = declaration->type;
+       if(type->type == TYPE_ARRAY
+                       || type->type == TYPE_COMPOUND_STRUCT
+                       || type->type == TYPE_COMPOUND_UNION) {
+               needs_entity = true;
+       }
+
        if(needs_entity) {
                ir_type *frame_type = get_irg_frame_type(current_ir_graph);
                create_declaration_entity(declaration,
@@ -1615,6 +1762,9 @@ static void declaration_statement_to_firm(declaration_statement_t *statement)
 static void create_jump_statement(const statement_t *statement,
                                   ir_node *target_block)
 {
+       if(get_cur_block() == NULL)
+               return;
+
        dbg_info *dbgi = get_dbg_info(&statement->source_position);
        ir_node  *jump = new_d_Jmp(dbgi);
        add_immBlock_pred(target_block, jump);
@@ -1653,6 +1803,26 @@ static void switch_statement_to_firm(const switch_statement_t *statement)
        set_cur_block(break_block);
 }
 
+static long fold_constant(const expression_t *expression)
+{
+       ir_graph *old_current_ir_graph = current_ir_graph;
+       current_ir_graph = get_const_code_irg();
+
+       ir_node *cnst = expression_to_firm(expression);
+       if(!is_Const(cnst)) {
+               panic("couldn't fold constantl");
+       }
+       tarval *tv = get_Const_tarval(cnst);
+       if(!tarval_is_long(tv)) {
+               panic("folded constant not an integer");
+       }
+
+       long res = get_tarval_long(tv);
+
+       current_ir_graph = old_current_ir_graph;
+       return res;
+}
+
 static void case_label_to_firm(const case_label_statement_t *statement)
 {
        dbg_info *dbgi = get_dbg_info(&statement->statement.source_position);
@@ -1662,16 +1832,7 @@ static void case_label_to_firm(const case_label_statement_t *statement)
        ir_node *proj;
        set_cur_block(get_nodes_block(current_switch_cond));
        if(statement->expression) {
-               ir_node *cnst = expression_to_firm(statement->expression);
-               if(!is_Const(cnst)) {
-                       panic("couldn't fold constant for case label");
-               }
-               tarval *tv = get_Const_tarval(cnst);
-               if(!mode_is_int(get_tarval_mode(tv))) {
-                       panic("case label not an integer");
-               }
-
-               long pn = get_tarval_long(tv);
+               long pn = fold_constant(statement->expression);
                if(pn == MAGIC_DEFAULT_PN_NUMBER) {
                        /* oops someone detected our cheating... */
                        panic("magic default pn used");
@@ -1687,6 +1848,54 @@ static void case_label_to_firm(const case_label_statement_t *statement)
        mature_immBlock(block);
 }
 
+static ir_node *get_label_block(declaration_t *label)
+{
+       assert(label->namespace == NAMESPACE_LABEL);
+
+       if(label->declaration_type == DECLARATION_TYPE_LABEL_BLOCK) {
+               return label->v.block;
+       }
+       assert(label->declaration_type == DECLARATION_TYPE_UNKNOWN);
+
+       ir_node *old_cur_block = get_cur_block();
+       ir_node *block         = new_immBlock();
+       set_cur_block(old_cur_block);
+
+       label->declaration_type = DECLARATION_TYPE_LABEL_BLOCK;
+       label->v.block          = block;
+
+       ARR_APP1(imature_blocks, block);
+
+       return block;
+}
+
+static void label_to_firm(const label_statement_t *statement)
+{
+       ir_node *block = get_label_block(statement->label);
+
+       if(get_cur_block() != NULL) {
+               ir_node *jmp = new_Jmp();
+               add_immBlock_pred(block, jmp);
+       }
+
+       set_cur_block(block);
+       keep_alive(block);
+
+       statement_to_firm(statement->label_statement);
+}
+
+static void goto_to_firm(const goto_statement_t *statement)
+{
+       if(get_cur_block() == NULL)
+               return;
+
+       ir_node *block = get_label_block(statement->label);
+       ir_node *jmp   = new_Jmp();
+       add_immBlock_pred(block, jmp);
+
+       set_cur_block(NULL);
+}
+
 static void statement_to_firm(statement_t *statement)
 {
        switch(statement->type) {
@@ -1726,6 +1935,12 @@ static void statement_to_firm(statement_t *statement)
        case STATEMENT_FOR:
                for_statement_to_firm((for_statement_t*) statement);
                return;
+       case STATEMENT_LABEL:
+               label_to_firm((label_statement_t*) statement);
+               return;
+       case STATEMENT_GOTO:
+               goto_to_firm((goto_statement_t*) statement);
+               return;
        default:
                break;
        }
@@ -1774,6 +1989,9 @@ static void create_function(declaration_t *declaration)
        if(declaration->init.statement == NULL)
                return;
 
+       assert(imature_blocks == NULL);
+       imature_blocks = NEW_ARR_F(ir_node*, 0);
+
        int       n_local_vars = get_function_n_local_vars(declaration);
        ir_graph *irg          = new_ir_graph(entity, n_local_vars);
        ir_node  *first_block  = get_cur_block();
@@ -1788,7 +2006,8 @@ static void create_function(declaration_t *declaration)
        /* do we have a return statement yet? */
        if(get_cur_block() != NULL) {
                assert(declaration->type->type == TYPE_FUNCTION);
-               const function_type_t* const func_type = (const function_type_t*)declaration->type;
+               const function_type_t* const func_type
+                       = (const function_type_t*) declaration->type;
                ir_node *ret;
                if (func_type->result_type == type_void) {
                        ret = new_Return(get_store(), 0, NULL);
@@ -1806,6 +2025,12 @@ static void create_function(declaration_t *declaration)
                add_immBlock_pred(end_block, ret);
        }
 
+       for(int i = 0; i < ARR_LEN(imature_blocks); ++i) {
+               mature_immBlock(imature_blocks[i]);
+       }
+       DEL_ARR_F(imature_blocks);
+       imature_blocks = NULL;
+
        mature_immBlock(first_block);
        mature_immBlock(end_block);
 
@@ -1866,6 +2091,8 @@ static void context_to_firm(context_t *context)
                if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY
                                || declaration->storage_class == STORAGE_CLASS_TYPEDEF)
                        continue;
+               if(declaration->symbol == NULL)
+                       continue;
 
                type_t *type = declaration->type;
                if(type->type == TYPE_FUNCTION) {