further improve initializers
[cparser] / ast2firm.c
index b75ddc1..515c7cc 100644 (file)
@@ -21,6 +21,7 @@
 #define MAGIC_DEFAULT_PN_NUMBER            (long) -314159265
 
 static ir_type *ir_type_const_char;
+static ir_type *ir_type_wchar_t;
 static ir_type *ir_type_void;
 static ir_type *ir_type_int;
 
@@ -28,9 +29,6 @@ static type_t *type_const_char;
 static type_t *type_void;
 static type_t *type_int;
 
-static symbol_t *symbol_builtin_alloca;
-static symbol_t *symbol_builtin_nanf;
-
 static int       next_value_number_function;
 static ir_node  *continue_label;
 static ir_node  *break_label;
@@ -89,19 +87,6 @@ const char *retrieve_dbg(const dbg_info *dbg, unsigned *line)
 
 void init_ast2firm(void)
 {
-       type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
-       type_void       = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
-       type_int        = make_atomic_type(ATOMIC_TYPE_INT,  TYPE_QUALIFIER_NONE);
-
-       ir_type_int        = get_ir_type(type_int);
-       ir_type_const_char = get_ir_type(type_const_char);
-       ir_type_void       = get_ir_type(type_int); /* we don't have a real void
-                                                      type in firm */
-
-       type_void->base.firm_type = ir_type_void;
-
-       symbol_builtin_alloca = symbol_table_insert("__builtin_alloca");
-       symbol_builtin_nanf   = symbol_table_insert("__builtin_nanf");
 }
 
 void exit_ast2firm(void)
@@ -278,15 +263,15 @@ static ir_type *create_atomic_type(const atomic_type_t *type)
 
 static ir_type *create_method_type(const function_type_t *function_type)
 {
-       type_t  *result_type  = function_type->result_type;
+       type_t  *return_type  = function_type->return_type;
 
        ident   *id           = unique_ident("functiontype");
        int      n_parameters = count_parameters(function_type);
-       int      n_results    = result_type == type_void ? 0 : 1;
+       int      n_results    = return_type == type_void ? 0 : 1;
        ir_type *irtype       = new_type_method(id, n_parameters, n_results);
 
-       if(result_type != type_void) {
-               ir_type *restype = get_ir_type(result_type);
+       if(return_type != type_void) {
+               ir_type *restype = get_ir_type(return_type);
                set_method_res_type(irtype, 0, restype);
        }
 
@@ -658,6 +643,42 @@ static ir_node *string_literal_to_firm(
                              literal->value);
 }
 
+static ir_node *wide_string_literal_to_firm(
+       const wide_string_literal_expression_t* const literal)
+{
+       ir_type *const global_type = get_glob_type();
+       ir_type *const elem_type   = ir_type_wchar_t;
+       ir_type *const type        = new_type_array(unique_ident("strtype"), 1,
+                                                   elem_type);
+
+       ident     *const id     = unique_ident("Lstr");
+       ir_entity *const entity = new_entity(global_type, id, type);
+       set_entity_ld_ident(entity, id);
+       set_entity_variability(entity, variability_constant);
+
+       ir_mode *const mode      = get_type_mode(elem_type);
+
+       const wchar_rep_t *const string = literal->value.begin;
+       const size_t             slen   = literal->value.size;
+
+       set_array_lower_bound_int(type, 0, 0);
+       set_array_upper_bound_int(type, 0, slen);
+       set_type_size_bytes(type, slen);
+       set_type_state(type, layout_fixed);
+
+       tarval **const tvs = xmalloc(slen * sizeof(tvs[0]));
+       for(size_t i = 0; i < slen; ++i) {
+               tvs[i] = new_tarval_from_long(string[i], mode);
+       }
+
+       set_array_entity_values(entity, tvs, slen);
+       free(tvs);
+
+       dbg_info *const dbgi = get_dbg_info(&literal->expression.source_position);
+
+       return create_symconst(dbgi, entity);
+}
+
 static ir_node *deref_address(ir_type *const irtype, ir_node *const addr,
                               dbg_info *const dbgi)
 {
@@ -803,11 +824,16 @@ static ir_node *process_builtin_call(const call_expression_t *call)
        dbg_info *dbgi = get_dbg_info(&call->expression.source_position);
 
        assert(call->function->type == EXPR_BUILTIN_SYMBOL);
-       builtin_symbol_expression_t *builtin
-               = (builtin_symbol_expression_t*) call->function;
-       symbol_t *symbol = builtin->symbol;
+       builtin_symbol_expression_t *builtin = &call->function->builtin_symbol;
+
+       type_t *type = skip_typeref(builtin->expression.datatype);
+       assert(is_type_pointer(type));
+
+       type_t   *function_type = skip_typeref(type->pointer.points_to);
+       symbol_t *symbol        = builtin->symbol;
 
-       if(symbol == symbol_builtin_alloca) {
+       switch(symbol->ID) {
+       case T___builtin_alloca: {
                if(call->arguments == NULL || call->arguments->next != NULL) {
                        panic("invalid number of parameters on __builtin_alloca");
                }
@@ -822,13 +848,18 @@ static ir_node *process_builtin_call(const call_expression_t *call)
                ir_node *res    = new_Proj(alloca, mode_P_data, pn_Alloc_res);
 
                return res;
-       } else if(symbol == symbol_builtin_nanf) {
+       }
+       case T___builtin_nan:
+       case T___builtin_nanf:
+       case T___builtin_nand: {
                /* Ignore string for now... */
-               ir_mode *mode = mode_D;
+               assert(is_type_function(function_type));
+               ir_mode *mode = get_ir_mode(function_type->function.return_type);
                tarval  *tv   = get_mode_NAN(mode);
                ir_node *res  = new_d_Const(dbgi, mode, tv);
                return res;
-       } else {
+       }
+       default:
                panic("Unsupported builtin found\n");
        }
 }
@@ -843,11 +874,12 @@ static ir_node *call_expression_to_firm(const call_expression_t *call)
        }
        ir_node *callee = expression_to_firm(function);
 
-       type_t *type = function->base.datatype;
-       assert(type->type == TYPE_POINTER);
-       pointer_type_t *const ptr_type = &type->pointer;
-       assert(ptr_type->points_to->type == TYPE_FUNCTION);
-       function_type_t *function_type = &ptr_type->points_to->function;
+       type_t *type = skip_typeref(function->base.datatype);
+       assert(is_type_pointer(type));
+       pointer_type_t *pointer_type = &type->pointer;
+       type_t         *points_to    = skip_typeref(pointer_type->points_to);
+       assert(is_type_function(points_to));
+       function_type_t *function_type = &points_to->function;
 
        int              n_parameters = 0;
        call_argument_t *argument     = call->arguments;
@@ -904,13 +936,13 @@ static ir_node *call_expression_to_firm(const call_expression_t *call)
        ir_node  *mem   = new_d_Proj(dbgi, node, mode_M, pn_Call_M_regular);
        set_store(mem);
 
-       type_t  *result_type = skip_typeref(function_type->result_type);
+       type_t  *return_type = skip_typeref(function_type->return_type);
        ir_node *result      = NULL;
 
-       if(!is_type_atomic(result_type, ATOMIC_TYPE_VOID)) {
+       if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
                ir_mode *mode;
-               if(is_type_scalar(result_type)) {
-                       mode = get_ir_mode(result_type);
+               if(is_type_scalar(return_type)) {
+                       mode = get_ir_mode(return_type);
                } else {
                        mode = mode_P_data;
                }
@@ -929,6 +961,25 @@ static void create_condition_evaluation(const expression_t *expression,
                                         ir_node *true_block,
                                         ir_node *false_block);
 
+static void assign_value(dbg_info *dbgi, ir_node *addr, type_t *type,
+                         ir_node *value)
+{
+       value = do_strict_conv(dbgi, value);
+
+       ir_node  *memory = get_store();
+
+       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);
+               set_store(store_mem);
+       } else {
+               ir_type *irtype    = get_ir_type(type);
+               ir_node *copyb     = new_d_CopyB(dbgi, memory, addr, value, irtype);
+               ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
+               set_store(copyb_mem);
+       }
+}
+
 static void set_value_for_expression(const expression_t *expression,
                                      ir_node *value)
 {
@@ -946,20 +997,9 @@ static void set_value_for_expression(const expression_t *expression,
                }
        }
 
-       ir_node  *addr   = expression_to_addr(expression);
-       ir_node  *memory = get_store();
-
-       type_t *type = skip_typeref(expression->base.datatype);
-       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);
-               set_store(store_mem);
-       } else {
-               ir_type *irtype    = get_ir_type(type);
-               ir_node *copyb     = new_d_CopyB(dbgi, memory, addr, value, irtype);
-               ir_node *copyb_mem = new_Proj(copyb, mode_M, pn_CopyB_M_regular);
-               set_store(copyb_mem);
-       }
+       ir_node *addr = expression_to_addr(expression);
+       type_t  *type = skip_typeref(expression->base.datatype);
+       assign_value(dbgi, addr, type, value);
 }
 
 static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
@@ -982,14 +1022,14 @@ static ir_node *create_conv(dbg_info *dbgi, ir_node *value, ir_mode *dest_mode)
 static ir_node *create_incdec(const unary_expression_t *expression)
 {
        dbg_info     *dbgi  = get_dbg_info(&expression->expression.source_position);
-       type_t       *type  = expression->expression.datatype;
+       type_t       *type  = skip_typeref(expression->expression.datatype);
        ir_mode      *mode  = get_ir_mode(type);
        expression_t *value = expression->value;
 
        ir_node *value_node = expression_to_firm(value);
 
        ir_node *offset;
-       if(type->type == TYPE_POINTER) {
+       if(is_type_pointer(type)) {
                pointer_type_t *pointer_type = &type->pointer;
                unsigned        elem_size    = get_type_size(pointer_type->points_to);
                offset = new_Const_long(mode_Is, elem_size);
@@ -1029,6 +1069,7 @@ static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
 {
        dbg_info *dbgi = get_dbg_info(&expression->expression.source_position);
        type_t   *type = skip_typeref(expression->expression.datatype);
+       ir_mode  *mode = get_ir_mode(type);
 
        if(expression->type == UNEXPR_TAKE_ADDRESS)
                return expression_to_addr(expression->value);
@@ -1038,25 +1079,27 @@ static ir_node *unary_expression_to_firm(const unary_expression_t *expression)
 
        switch(expression->type) {
        case UNEXPR_NEGATE:
-               return new_d_Minus(dbgi, value_node, get_ir_mode(type));
+               return new_d_Minus(dbgi, value_node, mode);
        case UNEXPR_PLUS:
                return value_node;
        case UNEXPR_BITWISE_NEGATE:
-               return new_d_Not(dbgi, value_node, get_ir_mode(type));
+               return new_d_Not(dbgi, value_node, mode);
        case UNEXPR_NOT: {
                if(get_irn_mode(value_node) != mode_b) {
                        value_node = create_conv(dbgi, value_node, mode_b);
                }
                value_node = new_d_Not(dbgi, value_node, mode_b);
-               ir_mode *const mode = get_ir_mode(type);
                if(mode != mode_b) {
                        value_node = create_conv(dbgi, value_node, mode);
                }
                return value_node;
        }
        case UNEXPR_DEREFERENCE: {
-               ir_type *irtype = get_ir_type(type);
-               return deref_address(irtype, value_node, dbgi);
+               type_t  *value_type = skip_typeref(value->base.datatype);
+               ir_type *irtype     = get_ir_type(value_type);
+               assert(is_Pointer_type(irtype));
+               ir_type *points_to  = get_pointer_points_to_type(irtype);
+               return deref_address(points_to, value_node, dbgi);
        }
        case UNEXPR_POSTFIX_INCREMENT:
        case UNEXPR_POSTFIX_DECREMENT:
@@ -1171,10 +1214,10 @@ static ir_node *create_arithmetic_assign_binop(
                const binary_expression_t *expression, create_arithmetic_func func)
 {
        dbg_info *const dbgi = get_dbg_info(&expression->expression.source_position);
-       type_t   *const type = expression->expression.datatype;
+       type_t   *const type = skip_typeref(expression->expression.datatype);
        ir_node  *value;
 
-       if (type->type == TYPE_POINTER) {
+       if (is_type_pointer(type)) {
                ir_node        *const pointer = expression_to_firm(expression->left);
                ir_node        *      integer = expression_to_firm(expression->right);
                value = pointer_arithmetic(pointer, integer, type, dbgi, func);
@@ -1206,10 +1249,10 @@ static ir_node *create_add(const binary_expression_t *expression)
                return new_d_Add(dbgi, left, right, mode);
        }
 
-       if (type_left->type == TYPE_POINTER || type_left->type == TYPE_ARRAY) {
+       if (is_type_pointer(type_left)) {
                return pointer_arithmetic(left, right, type, dbgi, new_d_Add);
        } else {
-               assert(type_right->type == TYPE_POINTER || type_right->type == TYPE_ARRAY);
+               assert(is_type_pointer(type_right));
                return pointer_arithmetic(right, left, type, dbgi, new_d_Add);
        }
 }
@@ -1228,8 +1271,7 @@ static ir_node *create_sub(const binary_expression_t *expression)
        if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
                ir_mode *const mode = get_ir_mode(type);
                return new_d_Sub(dbgi, left, right, mode);
-       } else if (type_left->type == TYPE_POINTER
-                       && type_right->type == TYPE_POINTER) {
+       } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
                const pointer_type_t *const ptr_type = &type_left->pointer;
                const unsigned elem_size             = get_type_size(ptr_type->points_to);
                ir_mode *const mode   = get_ir_mode(type);
@@ -1241,7 +1283,7 @@ static ir_node *create_sub(const binary_expression_t *expression)
                return new_d_Proj(dbgi, div, mode, pn_Div_res);
        }
 
-       assert(type_left->type == TYPE_POINTER);
+       assert(is_type_pointer(type_left));
        return pointer_arithmetic(left, right, type_left, dbgi, new_d_Sub);
 }
 
@@ -1501,7 +1543,8 @@ static ir_node *conditional_to_firm(const conditional_expression_t *expression)
        mature_immBlock(common_block);
 
        /* TODO improve static semantics, so either both or no values are NULL */
-       if (true_val == NULL || false_val == NULL) return NULL;
+       if (true_val == NULL || false_val == NULL)
+               return NULL;
 
        ir_node *in[2] = { true_val, false_val };
        ir_mode *mode  = get_irn_mode(true_val);
@@ -1702,6 +1745,8 @@ static ir_node *_expression_to_firm(const expression_t *expression)
                return const_to_firm(&expression->conste);
        case EXPR_STRING_LITERAL:
                return string_literal_to_firm(&expression->string);
+       case EXPR_WIDE_STRING_LITERAL:
+               return wide_string_literal_to_firm(&expression->wide_string);
        case EXPR_REFERENCE:
                return reference_expression_to_firm(&expression->reference);
        case EXPR_CALL:
@@ -2300,7 +2345,7 @@ static void create_initializer_string(initializer_string_t *initializer,
 static void create_initializer_object(initializer_t *initializer, type_t *type,
                ir_entity *entity, compound_graph_path_entry_t *entry, int len)
 {
-       if(type->type == TYPE_ARRAY) {
+       if(is_type_array(type)) {
                array_type_t *array_type = &type->array;
 
                if(initializer->type == INITIALIZER_STRING) {
@@ -2315,8 +2360,7 @@ static void create_initializer_object(initializer_t *initializer, type_t *type,
                assert(initializer->type == INITIALIZER_LIST);
                initializer_list_t *list = &initializer->list;
 
-               assert(type->type == TYPE_COMPOUND_STRUCT
-                               || type->type == TYPE_COMPOUND_UNION);
+               assert(is_type_compound(type));
                compound_type_t *compound_type = &type->compound;
                create_initializer_compound(list, compound_type, entity, entry, len);
        }
@@ -2332,14 +2376,12 @@ static void create_initializer_local_variable_entity(declaration_t *declaration)
        ir_node       *frame       = get_irg_frame(current_ir_graph);
        ir_node       *addr        = new_d_simpleSel(dbgi, nomem, frame, entity);
 
-       if(is_atomic_entity(entity)) {
-               assert(initializer->type == INITIALIZER_VALUE);
+       if(initializer->type == INITIALIZER_VALUE) {
                initializer_value_t *initializer_value = &initializer->value;
 
-               ir_node *value     = expression_to_firm(initializer_value->value);
-               ir_node *store     = new_d_Store(dbgi, memory, addr, value);
-               ir_node *store_mem = new_d_Proj(dbgi, store, mode_M, pn_Store_M);
-               set_store(store_mem);
+               ir_node *value = expression_to_firm(initializer_value->value);
+               type_t  *type  = skip_typeref(declaration->type);
+               assign_value(dbgi, addr, type, value);
                return;
        }
 
@@ -2416,9 +2458,7 @@ static void create_local_variable(declaration_t *declaration)
        bool needs_entity = declaration->address_taken;
        type_t *type = skip_typeref(declaration->type);
 
-       if(type->type == TYPE_ARRAY
-                       || type->type == TYPE_COMPOUND_STRUCT
-                       || type->type == TYPE_COMPOUND_UNION) {
+       if(is_type_array(type) || is_type_compound(type)) {
                needs_entity = true;
        }
 
@@ -2466,9 +2506,9 @@ static void declaration_statement_to_firm(declaration_statement_t *statement)
        declaration_t *declaration = statement->declarations_begin;
        declaration_t *end         = statement->declarations_end->next;
        for( ; declaration != end; declaration = declaration->next) {
-               type_t *type = declaration->type;
+               type_t *type = skip_typeref(declaration->type);
 
-               switch ((storage_class_tag_t)declaration->storage_class) {
+               switch ((storage_class_tag_t) declaration->storage_class) {
                case STORAGE_CLASS_TYPEDEF:
                        continue;
                case STORAGE_CLASS_STATIC:
@@ -2481,7 +2521,7 @@ static void declaration_statement_to_firm(declaration_statement_t *statement)
                case STORAGE_CLASS_NONE:
                case STORAGE_CLASS_AUTO:
                case STORAGE_CLASS_REGISTER:
-                       if(type->type == TYPE_FUNCTION) {
+                       if(is_type_function(type)) {
                                panic("nested functions not supported yet");
                        } else {
                                create_local_variable(declaration);
@@ -2831,8 +2871,8 @@ static void initialize_function_parameters(declaration_t *declaration)
                type_t *type = skip_typeref(parameter->type);
 
                bool needs_entity = parameter->address_taken;
-               if(type->type == TYPE_COMPOUND_STRUCT
-                               || type->type == TYPE_COMPOUND_UNION) {
+               assert(!is_type_array(type));
+               if(is_type_compound(type)) {
                        needs_entity = true;
                }
 
@@ -2885,17 +2925,19 @@ 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 = &declaration->type->function;
-               const type_t *result_type = skip_typeref(func_type->result_type);
+               type_t *type = skip_typeref(declaration->type);
+               assert(is_type_function(type));
+               const function_type_t *func_type   = &type->function;
+               const type_t          *return_type
+                       = skip_typeref(func_type->return_type);
 
                ir_node *ret;
-               if (is_type_atomic(result_type, ATOMIC_TYPE_VOID)) {
+               if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
                        ret = new_Return(get_store(), 0, NULL);
                } else {
                        ir_mode *mode;
-                       if(is_type_scalar(result_type)) {
-                               mode = get_ir_mode(func_type->result_type);
+                       if(is_type_scalar(return_type)) {
+                               mode = get_ir_mode(func_type->return_type);
                        } else {
                                mode = mode_P_data;
                        }
@@ -3021,8 +3063,8 @@ static void context_to_firm(context_t *context)
                if(declaration->symbol == NULL)
                        continue;
 
-               type_t *type = declaration->type;
-               if(type->type == TYPE_FUNCTION) {
+               type_t *type = skip_typeref(declaration->type);
+               if(is_type_function(type)) {
                        get_function_entity(declaration);
                } else {
                        create_global_variable(declaration);
@@ -3050,6 +3092,18 @@ static void context_to_firm(context_t *context)
 
 void translation_unit_to_firm(translation_unit_t *unit)
 {
+       type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
+       type_void       = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
+       type_int        = make_atomic_type(ATOMIC_TYPE_INT,  TYPE_QUALIFIER_NONE);
+
+       ir_type_int        = get_ir_type(type_int);
+       ir_type_const_char = get_ir_type(type_const_char);
+       ir_type_wchar_t    = get_ir_type(type_wchar_t);
+       ir_type_void       = get_ir_type(type_int); /* we don't have a real void
+                                                      type in firm */
+
+       type_void->base.firm_type = ir_type_void;
+
        /* just to be sure */
        continue_label      = NULL;
        break_label         = NULL;