fix wrong assert, expor copy_node_attr function to public API
[libfirm] / ir / tr / type.c
index a9fc299..77020be 100644 (file)
  * @see  type_t.h type tpop
  */
 
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
-
-#ifdef HAVE_STRING_H
-# include <string.h>
-#endif
-#ifdef HAVE_STDLIB_H
-# include <stdlib.h>
-#endif
+#include "config.h"
 
+#include <string.h>
+#include <stdlib.h>
 #include <stddef.h>
 
 #include "type_t.h"
@@ -76,6 +69,7 @@
 /*-----------------------------------------------------------------*/
 
 ir_type *firm_none_type;    ir_type *get_none_type(void)    { return firm_none_type;    }
+ir_type *firm_code_type;    ir_type *get_code_type(void)    { return firm_code_type;    }
 ir_type *firm_unknown_type; ir_type *get_unknown_type(void) { return firm_unknown_type; }
 
 
@@ -103,6 +97,10 @@ void firm_init_type(dbg_info *builtin_db, unsigned def_cc_mask) {
        set_type_state (firm_none_type, layout_fixed);
        remove_irp_type(firm_none_type);
 
+       firm_code_type    = new_type(tpop_code, mode_ANY, new_id_from_str("type_code"), builtin_db);
+       set_type_state(firm_code_type, layout_fixed);
+       remove_irp_type(firm_code_type);
+
        firm_unknown_type = new_type(tpop_unknown, mode_ANY, new_id_from_str("type_unknown"), builtin_db);
        set_type_size_bytes(firm_unknown_type, 0);
        set_type_state (firm_unknown_type, layout_fixed);
@@ -110,17 +108,17 @@ void firm_init_type(dbg_info *builtin_db, unsigned def_cc_mask) {
 }
 
 /** the global type visited flag */
-unsigned long firm_type_visited;
+ir_visited_t firm_type_visited;
 
-void (set_master_type_visited)(unsigned long val) { _set_master_type_visited(val); }
-unsigned long (get_master_type_visited)(void)     { return _get_master_type_visited(); }
-void (inc_master_type_visited)(void)              { _inc_master_type_visited(); }
+void (set_master_type_visited)(ir_visited_t val) { _set_master_type_visited(val); }
+ir_visited_t (get_master_type_visited)(void)     { return _get_master_type_visited(); }
+void (inc_master_type_visited)(void)             { _inc_master_type_visited(); }
 
 /*
  * Creates a new type representation.
  */
 ir_type *
-new_type(tp_op *type_op, ir_mode *mode, ident *name, dbg_info *db) {
+new_type(const tp_op *type_op, ir_mode *mode, ident *name, dbg_info *db) {
        ir_type *res;
        int node_size;
 
@@ -155,7 +153,8 @@ new_type(tp_op *type_op, ir_mode *mode, ident *name, dbg_info *db) {
 void free_type(ir_type *tp) {
        const tp_op *op = get_type_tpop(tp);
 
-       if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown))
+       if ((get_type_tpop(tp) == tpop_none) || (get_type_tpop(tp) == tpop_unknown)
+                       || (get_type_tpop(tp) == tpop_code))
                return;
        /* Remove from list of all types */
        remove_irp_type(tp);
@@ -423,11 +422,11 @@ set_type_state(ir_type *tp, ir_type_state state) {
                tp->flags &= ~tf_layout_fixed;
 }
 
-unsigned long (get_type_visited)(const ir_type *tp) {
+ir_visited_t (get_type_visited)(const ir_type *tp) {
        return _get_type_visited(tp);
 }
 
-void (set_type_visited)(ir_type *tp, unsigned long num) {
+void (set_type_visited)(ir_type *tp, ir_visited_t num) {
        _set_type_visited(tp, num);
 }
 
@@ -480,8 +479,7 @@ int equal_type(ir_type *typ1, ir_type *typ2) {
                if (get_class_n_supertypes(typ1) != get_class_n_supertypes(typ2)) return 0;
                if (get_class_peculiarity(typ1) != get_class_peculiarity(typ2)) return 0;
                /** Compare the members **/
-               m = alloca(sizeof(ir_entity *) * get_class_n_members(typ1));
-               memset(m, 0, sizeof(ir_entity *) * get_class_n_members(typ1));
+               m = ALLOCANZ(ir_entity*, get_class_n_members(typ1));
                /* First sort the members of typ2 */
                for (i = 0; i < get_class_n_members(typ1); i++) {
                        ir_entity *e1 = get_class_member(typ1, i);
@@ -497,8 +495,7 @@ int equal_type(ir_type *typ1, ir_type *typ2) {
                                return 0;
                }
                /** Compare the supertypes **/
-               t = alloca(sizeof(ir_entity *) * get_class_n_supertypes(typ1));
-               memset(t, 0, sizeof(ir_entity *) * get_class_n_supertypes(typ1));
+               t = ALLOCANZ(ir_type*, get_class_n_supertypes(typ1));
                /* First sort the supertypes of typ2 */
                for (i = 0; i < get_class_n_supertypes(typ1); i++) {
                        ir_type *t1 = get_class_supertype(typ1, i);
@@ -517,8 +514,7 @@ int equal_type(ir_type *typ1, ir_type *typ2) {
 
        case tpo_struct:
                if (get_struct_n_members(typ1) != get_struct_n_members(typ2)) return 0;
-               m = alloca(sizeof(ir_entity *) * get_struct_n_members(typ1));
-               memset(m, 0, sizeof(ir_entity *) * get_struct_n_members(typ1));
+               m = ALLOCANZ(ir_entity*, get_struct_n_members(typ1));
                /* First sort the members of lt */
                for (i = 0; i < get_struct_n_members(typ1); i++) {
                        ir_entity *e1 = get_struct_member(typ1, i);
@@ -565,8 +561,7 @@ int equal_type(ir_type *typ1, ir_type *typ2) {
 
        case tpo_union:
                if (get_union_n_members(typ1) != get_union_n_members(typ2)) return 0;
-               m = alloca(sizeof(ir_entity *) * get_union_n_members(typ1));
-               memset(m, 0, sizeof(ir_entity *) * get_union_n_members(typ1));
+               m = ALLOCANZ(ir_entity*, get_union_n_members(typ1));
                /* First sort the members of lt */
                for (i = 0; i < get_union_n_members(typ1); i++) {
                        ir_entity *e1 = get_union_member(typ1, i);
@@ -633,8 +628,7 @@ int smaller_type(ir_type *st, ir_type *lt) {
                if (n_st_members != get_struct_n_members(lt))
                        return 0;
 
-               m = alloca(sizeof(ir_entity *) * n_st_members);
-               memset(m, 0, sizeof(ir_entity *) * n_st_members);
+               m = ALLOCANZ(ir_entity*, n_st_members);
                /* First sort the members of lt */
                for (i = 0; i < n_st_members; ++i) {
                        ir_entity *se = get_struct_member(st, i);
@@ -684,8 +678,7 @@ int smaller_type(ir_type *st, ir_type *lt) {
        case tpo_union:
                n_st_members = get_union_n_members(st);
                if (n_st_members != get_union_n_members(lt)) return 0;
-               m = alloca(sizeof(ir_entity *) * n_st_members);
-               memset(m, 0, sizeof(ir_entity *) * n_st_members);
+               m = ALLOCANZ(ir_entity*, n_st_members);
                /* First sort the members of lt */
                for (i = 0; i < n_st_members; ++i) {
                        ir_entity *se = get_union_member(st, i);
@@ -796,6 +789,7 @@ void free_class_attrs(ir_type *clss) {
 void add_class_member(ir_type *clss, ir_entity *member) {
        assert(clss && (clss->type_op == type_class));
        assert(clss != get_entity_type(member) && "recursive type");
+       assert(get_type_state(clss) != layout_fixed);
        ARR_APP1 (ir_entity *, clss->attr.ca.members, member);
 }
 
@@ -1091,6 +1085,7 @@ void add_struct_member(ir_type *strct, ir_entity *member) {
        assert(strct && (strct->type_op == type_struct));
        assert(get_type_tpop(get_entity_type(member)) != type_method);
        assert(strct != get_entity_type(member) && "recursive type");
+       assert(get_type_state(strct) != layout_fixed);
        ARR_APP1 (ir_entity *, strct->attr.sa.members, member);
 }
 
@@ -1172,7 +1167,7 @@ build_value_type(ident *name, int len, tp_ent_pair *tps) {
 
                /* use the parameter name if specified */
                if (! id)
-                       id = mangle_u(name, get_type_ident(elt_type));
+                       id = id_mangle_u(name, get_type_ident(elt_type));
                tps[i].ent = new_entity(res, id, elt_type);
                set_entity_allocation(tps[i].ent, allocation_parameter);
        }
@@ -1189,10 +1184,10 @@ ir_type *new_d_type_method(ident *name, int n_param, int n_res, dbg_info *db) {
        res->flags                       |= tf_layout_fixed;
        res->size                         = get_mode_size_bytes(mode_P_code);
        res->attr.ma.n_params             = n_param;
-       res->attr.ma.params               = xcalloc(n_param, sizeof(res->attr.ma.params[0]));
+       res->attr.ma.params               = XMALLOCNZ(tp_ent_pair, n_param);
        res->attr.ma.value_params         = NULL;
        res->attr.ma.n_res                = n_res;
-       res->attr.ma.res_type             = xcalloc(n_res, sizeof(res->attr.ma.res_type[0]));
+       res->attr.ma.res_type             = XMALLOCNZ(tp_ent_pair, n_res);
        res->attr.ma.value_ress           = NULL;
        res->attr.ma.variadicity          = variadicity_non_variadic;
        res->attr.ma.first_variadic_param = -1;
@@ -1218,7 +1213,7 @@ ir_type *clone_type_method(ir_type *tp, ident *prefix) {
 
        name = tp->name;
        if (prefix != NULL)
-               name = mangle(prefix, name);
+               name = id_mangle(prefix, name);
 
        mode     = tp->mode;
        n_params = tp->attr.ma.n_params;
@@ -1228,13 +1223,14 @@ ir_type *clone_type_method(ir_type *tp, ident *prefix) {
        res = new_type(type_method, mode, name, db);
 
        res->flags                         = tp->flags;
+       res->assoc_type                    = tp->assoc_type;
        res->size                          = tp->size;
        res->attr.ma.n_params              = n_params;
-       res->attr.ma.params                = xcalloc(n_params, sizeof(res->attr.ma.params[0]));
+       res->attr.ma.params                = XMALLOCN(tp_ent_pair, n_params);
        memcpy(res->attr.ma.params, tp->attr.ma.params, n_params * sizeof(res->attr.ma.params[0]));
        res->attr.ma.value_params          = tp->attr.ma.value_params;
        res->attr.ma.n_res                 = n_res;
-       res->attr.ma.res_type              = xcalloc(n_res, sizeof(res->attr.ma.res_type[0]));
+       res->attr.ma.res_type              = XMALLOCN(tp_ent_pair, n_res);
        memcpy(res->attr.ma.res_type, tp->attr.ma.res_type, n_res * sizeof(res->attr.ma.res_type[0]));
        res->attr.ma.value_ress            = tp->attr.ma.value_ress;
        res->attr.ma.variadicity           = tp->attr.ma.variadicity;
@@ -1324,7 +1320,7 @@ ir_entity *get_method_value_param_ent(ir_type *method, int pos) {
        if (!method->attr.ma.value_params) {
                /* parameter value type not created yet, build */
                method->attr.ma.value_params
-                       = build_value_type(mangle_u(get_type_ident(method), value_params_suffix),
+                       = build_value_type(id_mangle_u(get_type_ident(method), value_params_suffix),
                        get_method_n_params(method), method->attr.ma.params);
        }
        /*
@@ -1336,6 +1332,26 @@ ir_entity *get_method_value_param_ent(ir_type *method, int pos) {
        return method->attr.ma.params[pos].ent;
 }
 
+/*
+ * Sets the type that represents the copied value arguments.
+ */
+void set_method_value_param_type(ir_type *method, ir_type *tp) {
+       int i, n;
+
+       assert(method && (method->type_op == type_method));
+       assert(is_value_param_type(tp));
+       assert(get_method_n_params(method) == get_struct_n_members(tp));
+
+       method->attr.ma.value_params = tp;
+
+       n = get_struct_n_members(tp);
+       for (i = 0; i < n; i++)
+       {
+               ir_entity *ent = get_struct_member(tp, i);
+               method->attr.ma.params[i].ent = ent;
+       }
+}
+
 /*
  * Returns a type that represents the copied value arguments.
  */
@@ -1378,7 +1394,7 @@ ir_entity *get_method_value_res_ent(ir_type *method, int pos) {
        if (!method->attr.ma.value_ress) {
                /* result value type not created yet, build */
                method->attr.ma.value_ress
-                       = build_value_type(mangle_u(get_type_ident(method), value_ress_suffix),
+                       = build_value_type(id_mangle_u(get_type_ident(method), value_ress_suffix),
                        get_method_n_ress(method), method->attr.ma.res_type);
        }
        /*
@@ -1400,7 +1416,7 @@ ir_type *get_method_value_res_type(const ir_type *method) {
 }
 
 /* Returns the null-terminated name of this variadicity. */
-const char *get_variadicity_name(variadicity vari) {
+const char *get_variadicity_name(ir_variadicity vari) {
 #define X(a)    case a: return #a
        switch (vari) {
        X(variadicity_non_variadic);
@@ -1411,12 +1427,12 @@ const char *get_variadicity_name(variadicity vari) {
 #undef X
 }
 
-variadicity get_method_variadicity(const ir_type *method) {
+ir_variadicity get_method_variadicity(const ir_type *method) {
        assert(method && (method->type_op == type_method));
        return method->attr.ma.variadicity;
 }
 
-void set_method_variadicity(ir_type *method, variadicity vari) {
+void set_method_variadicity(ir_type *method, ir_variadicity vari) {
        assert(method && (method->type_op == type_method));
        method->attr.ma.variadicity = vari;
 }
@@ -1532,6 +1548,7 @@ int get_union_n_members(const ir_type *uni) {
 void add_union_member(ir_type *uni, ir_entity *member) {
        assert(uni && (uni->type_op == type_union));
        assert(uni != get_entity_type(member) && "recursive type");
+       assert(get_type_state(uni) != layout_fixed);
        ARR_APP1(ir_entity *, uni->attr.ua.members, member);
 }
 
@@ -1593,9 +1610,9 @@ ir_type *new_d_type_array(ident *name, int n_dimensions, ir_type *element_type,
 
        res = new_type(type_array, NULL, name, db);
        res->attr.aa.n_dimensions = n_dimensions;
-       res->attr.aa.lower_bound  = xcalloc(n_dimensions, sizeof(*res->attr.aa.lower_bound));
-       res->attr.aa.upper_bound  = xcalloc(n_dimensions, sizeof(*res->attr.aa.upper_bound));
-       res->attr.aa.order        = xcalloc(n_dimensions, sizeof(*res->attr.aa.order));
+       res->attr.aa.lower_bound  = XMALLOCNZ(ir_node*, n_dimensions);
+       res->attr.aa.upper_bound  = XMALLOCNZ(ir_node*, n_dimensions);
+       res->attr.aa.order        = XMALLOCNZ(int,      n_dimensions);
 
        current_ir_graph = get_const_code_irg();
        unk = new_Unknown(mode_Iu);
@@ -1607,7 +1624,7 @@ ir_type *new_d_type_array(ident *name, int n_dimensions, ir_type *element_type,
        current_ir_graph = rem;
 
        res->attr.aa.element_type = element_type;
-       new_entity(res, mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
+       new_entity(res, id_mangle_u(name, new_id_from_chars("elem_ent", 8)), element_type);
        hook_new_type(res);
        return res;
 }
@@ -1654,8 +1671,8 @@ set_array_bounds_int(ir_type *array, int dimension, int lower_bound, int upper_b
        ir_graph *rem = current_ir_graph;
        current_ir_graph = get_const_code_irg();
        set_array_bounds(array, dimension,
-                 new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)),
-                 new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu )));
+                 new_Const_long(mode_Iu, lower_bound),
+                 new_Const_long(mode_Iu, upper_bound));
        current_ir_graph = rem;
 }
 
@@ -1670,7 +1687,7 @@ void set_array_lower_bound_int(ir_type *array, int dimension, int lower_bound) {
        ir_graph *rem = current_ir_graph;
        current_ir_graph = get_const_code_irg();
        set_array_lower_bound(array, dimension,
-            new_Const(mode_Iu, new_tarval_from_long (lower_bound, mode_Iu)));
+            new_Const_long(mode_Iu, lower_bound));
        current_ir_graph = rem;
 }
 void
@@ -1683,13 +1700,13 @@ void set_array_upper_bound_int(ir_type *array, int dimension, int upper_bound) {
        ir_graph *rem = current_ir_graph;
        current_ir_graph = get_const_code_irg();
        set_array_upper_bound(array, dimension,
-                   new_Const(mode_Iu, new_tarval_from_long (upper_bound, mode_Iu)));
+                   new_Const_long(mode_Iu, upper_bound));
        current_ir_graph = rem;
 }
 
 int has_array_lower_bound(const ir_type *array, int dimension) {
        assert(array && (array->type_op == type_array));
-       return (get_irn_op(array->attr.aa.lower_bound[dimension]) != op_Unknown);
+       return !is_Unknown(array->attr.aa.lower_bound[dimension]);
 }
 
 ir_node *get_array_lower_bound(const ir_type *array, int dimension) {
@@ -1701,13 +1718,13 @@ long get_array_lower_bound_int(const ir_type *array, int dimension) {
        ir_node *node;
        assert(array && (array->type_op == type_array));
        node = array->attr.aa.lower_bound[dimension];
-       assert(get_irn_op(node) == op_Const);
+       assert(is_Const(node));
        return get_tarval_long(get_Const_tarval(node));
 }
 
 int has_array_upper_bound(const ir_type *array, int dimension) {
        assert(array && (array->type_op == type_array));
-       return get_irn_op(array->attr.aa.upper_bound[dimension]) != op_Unknown;
+       return !is_Unknown(array->attr.aa.upper_bound[dimension]);
 }
 
 ir_node *get_array_upper_bound(const ir_type *array, int dimension) {
@@ -1719,7 +1736,7 @@ long get_array_upper_bound_int(const ir_type *array, int dimension) {
        ir_node *node;
        assert(array && (array->type_op == type_array));
        node = array->attr.aa.upper_bound[dimension];
-       assert(get_irn_op(node) == op_Const);
+       assert(is_Const(node));
        return get_tarval_long(get_Const_tarval(node));
 }
 
@@ -2035,6 +2052,11 @@ int is_compound_type(const ir_type *tp) {
        return tp->type_op->flags & TP_OP_FLAG_COMPOUND;
 }
 
+int is_code_type(const ir_type *tp) {
+       assert(tp && tp->kind == k_type);
+       return tp->type_op == tpop_code;
+}
+
 /* Checks, whether a type is a frame type */
 int is_frame_type(const ir_type *tp) {
        return tp->flags & tf_frame_type;
@@ -2050,6 +2072,18 @@ int is_lowered_type(const ir_type *tp) {
        return tp->flags & tf_lowered_type;
 }
 
+/* Makes a new value type. */
+ir_type *new_type_value(ident *name) {
+       ir_type *res = new_type_struct(name);
+
+       res->flags |= tf_value_param_type;
+
+       /* Remove type from type list.  Must be treated differently than other types. */
+       remove_irp_type(res);
+
+       return res;
+}
+
 /* Makes a new frame type. */
 ir_type *new_type_frame(ident *name) {
        ir_type *res = new_type_class(name);
@@ -2065,6 +2099,25 @@ ir_type *new_type_frame(ident *name) {
        return res;
 }
 
+/* Makes a clone of a frame type. */
+ir_type *clone_frame_type(ir_type *type) {
+       ir_type *res;
+       int     i, n;
+
+       assert(is_frame_type(type));
+       /* the entity link resource should be allocated if this function is called */
+       assert(irp_resources_reserved(irp) & IR_RESOURCE_ENTITY_LINK);
+
+       res = new_type_frame(type->name);
+       for (i = 0, n = get_class_n_members(type); i < n; ++i) {
+               ir_entity *ent  = get_class_member(type, i);
+               ir_entity *nent = copy_entity_own(ent, res);
+               set_entity_link(ent, nent);
+               set_entity_link(nent, ent);
+       }
+       return res;
+}
+
 /* Sets a lowered type for a type. This sets both associations. */
 void set_lowered_type(ir_type *tp, ir_type *lowered_type) {
        assert(is_type(tp) && is_type(lowered_type));
@@ -2104,6 +2157,7 @@ ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment, i
        assert(is_frame_type(frame_type));
        assert(get_type_state(frame_type) == layout_fixed);
        assert(get_type_alignment_bytes(frame_type) > 0);
+       set_type_state(frame_type, layout_undefined);
 
        if (! a_byte)
                a_byte = new_type_primitive(new_id_from_chars("byte", 4), mode_Bu);
@@ -2115,7 +2169,7 @@ ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment, i
        frame_align = get_type_alignment_bytes(frame_type);
        size = (size + frame_align - 1) & ~(frame_align - 1);
 
-       tp = new_type_array(mangle_u(get_type_ident(frame_type), name), 1, a_byte);
+       tp = new_type_array(id_mangle_u(get_type_ident(frame_type), name), 1, a_byte);
        set_array_bounds_int(tp, 0, 0, size);
        set_type_alignment_bytes(tp, alignment);
 
@@ -2130,6 +2184,13 @@ ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment, i
                /* calculate offset and new type size */
                offset = 0;
                frame_size += size;
+
+               /* increase size to match alignment... */
+               if (alignment > frame_align) {
+                       frame_align = alignment;
+                       set_type_alignment_bytes(frame_type, frame_align);
+                       frame_size  = (frame_size + frame_align - 1) & ~(frame_align - 1);
+               }
        } else {
                /* calculate offset and new type size */
                offset = (frame_size + alignment - 1) & ~(alignment - 1);
@@ -2142,5 +2203,7 @@ ir_entity *frame_alloc_area(ir_type *frame_type, int size, unsigned alignment, i
 
        /* mark this entity as compiler generated */
        set_entity_compiler_generated(area, 1);
+
+       set_type_state(frame_type, layout_fixed);
        return area;
 }