ia32: Merge Test and Test8Bit.
[libfirm] / ir / tr / entity.c
index e7541d1..455c16c 100644 (file)
@@ -21,7 +21,6 @@
  * @file
  * @brief   Representation of all program known entities.
  * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
- * @version $Id$
  */
 #include "config.h"
 
@@ -32,7 +31,7 @@
 #include "xmalloc.h"
 #include "entity_t.h"
 #include "array.h"
-#include "irtools.h"
+#include "util.h"
 #include "irhooks.h"
 #include "irprintf.h"
 
 #include "irgraph_t.h"
 #include "callgraph.h"
 #include "error.h"
-#include "compound_path.h"
-
-/*-----------------------------------------------------------------*/
-/** general                                                       **/
-/*-----------------------------------------------------------------*/
-
-ir_entity *unknown_entity = NULL;
-
-ir_entity *get_unknown_entity(void) { return unknown_entity; }
 
 /** The name of the unknown entity. */
 #define UNKNOWN_ENTITY_NAME "unknown_entity"
 
+ir_entity *get_unknown_entity(void)
+{
+       return irp->unknown_entity;
+}
+
 /*-----------------------------------------------------------------*/
 /* ENTITY                                                          */
 /*-----------------------------------------------------------------*/
 
-ir_entity *new_d_entity(ir_type *owner, ident *name, ir_type *type,
-                        dbg_info *db)
+static ir_entity *intern_new_entity(ir_type *owner, ir_entity_kind kind,
+                                    ident *name, ir_type *type, dbg_info *dbgi)
 {
-       ir_entity *res;
-
-       assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
-
-       res = XMALLOCZ(ir_entity);
+       ir_entity *res = XMALLOCZ(ir_entity);
 
        res->kind    = k_entity;
        res->name    = name;
@@ -75,55 +66,102 @@ ir_entity *new_d_entity(ir_type *owner, ident *name, ir_type *type,
        res->type    = type;
        res->owner   = owner;
 
+       res->entity_kind          = kind;
        res->volatility           = volatility_non_volatile;
        res->aligned              = align_is_aligned;
        res->usage                = ir_usage_unknown;
        res->compiler_gen         = 0;
-       res->visibility           = ir_visibility_default;
+       res->visibility           = ir_visibility_external;
        res->offset               = -1;
        res->offset_bit_remainder = 0;
        res->alignment            = 0;
        res->link                 = NULL;
        res->repr_class           = NULL;
+#ifdef DEBUG_libfirm
+       res->nr = get_irp_new_node_nr();
+#endif
+
+       /* Remember entity in its owner. */
+       if (owner != NULL)
+               add_compound_member(owner, res);
+
+       res->visit = 0;
+       set_entity_dbg_info(res, dbgi);
+
+       return res;
+}
+
+ir_entity *new_d_entity(ir_type *owner, ident *name, ir_type *type,
+                        dbg_info *db)
+{
+       ir_entity *res;
 
        if (is_Method_type(type)) {
                ir_graph *irg = get_const_code_irg();
                symconst_symbol sym;
-               ir_mode *mode = is_Method_type(type) ? mode_P_code : mode_P_data;
+               res = intern_new_entity(owner, IR_ENTITY_METHOD, name, type, db);
                sym.entity_p            = res;
-               set_atomic_ent_value(res, new_r_SymConst(irg, mode, sym, symconst_addr_ent));
-               res->linkage            = IR_LINKAGE_CONSTANT;
-               res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
-               res->attr.mtd_attr.vtable_number      = IR_VTABLE_NUM_NOT_SET;
-               res->attr.mtd_attr.param_access       = NULL;
-               res->attr.mtd_attr.param_weight       = NULL;
-               res->attr.mtd_attr.irg                = NULL;
-       } else if (is_compound_type(type)) {
-               res->attr.cmpd_attr.values    = NULL;
-               res->attr.cmpd_attr.val_paths = NULL;
-       } else if (is_code_type(type)) {
-               res->attr.code_attr.label = (ir_label_t) -1;
+               set_atomic_ent_value(res, new_r_SymConst(irg, mode_P_code, sym, symconst_addr_ent));
+               res->linkage                     = IR_LINKAGE_CONSTANT;
+               res->attr.mtd_attr.properties    = get_method_additional_properties(type);
+               res->attr.mtd_attr.vtable_number = IR_VTABLE_NUM_NOT_SET;
+               res->attr.mtd_attr.param_access  = NULL;
+               res->attr.mtd_attr.param_weight  = NULL;
+               res->attr.mtd_attr.irg           = NULL;
+       } else if (owner != NULL
+                  && (is_compound_type(owner) && !(owner->flags & tf_segment))) {
+               res = intern_new_entity(owner, IR_ENTITY_COMPOUND_MEMBER, name, type, db);
+       } else {
+               res = intern_new_entity(owner, IR_ENTITY_NORMAL, name, type, db);
        }
 
-       /* Remember entity in its owner. */
-       if (owner != NULL)
-               add_compound_member(owner, res);
+       hook_new_entity(res);
+       return res;
+}
 
-#ifdef DEBUG_libfirm
-       res->nr = get_irp_new_node_nr();
-#endif
+ir_entity *new_entity(ir_type *owner, ident *name, ir_type *type)
+{
+       return new_d_entity(owner, name, type, NULL);
+}
 
-       res->visit = 0;
-       set_entity_dbg_info(res, db);
+static ident *make_parameter_entity_name(size_t pos)
+{
+       char buf[64];
+       snprintf(buf, sizeof(buf), "parameter.%lu", (unsigned long) pos);
+       return new_id_from_str(buf);
+}
 
+ir_entity *new_d_parameter_entity(ir_type *owner, size_t pos, ir_type *type,
+                                  dbg_info *dbgi)
+{
+       ident     *name = make_parameter_entity_name(pos);
+       ir_entity *res
+               = intern_new_entity(owner, IR_ENTITY_PARAMETER, name, type, dbgi);
+       res->attr.parameter.number = pos;
        hook_new_entity(res);
+       return res;
+}
 
+ir_entity *new_parameter_entity(ir_type *owner, size_t pos, ir_type *type)
+{
+       return new_d_parameter_entity(owner, pos, type, NULL);
+}
+
+ir_entity *new_d_label_entity(ir_label_t label, dbg_info *dbgi)
+{
+       ident *name = id_unique("label_%u");
+       ir_type *global_type = get_glob_type();
+       ir_entity *res
+               = intern_new_entity(global_type, IR_ENTITY_LABEL, name, get_code_type(),
+                                   dbgi);
+       res->attr.code_attr.label = label;
+       hook_new_entity(res);
        return res;
 }
 
-ir_entity *new_entity(ir_type *owner, ident *name, ir_type *type)
+ir_entity *new_label_entity(ir_label_t label)
 {
-       return new_d_entity(owner, name, type, NULL);
+       return new_d_label_entity(label, NULL);
 }
 
 /**
@@ -144,14 +182,8 @@ static void free_entity_attrs(ir_entity *ent)
 
        if (ent->initializer != NULL) {
                /* TODO: free initializers */
-       } else if (entity_has_compound_ent_values(ent)) {
-               /* can't free compound graph path as it might be used
-                * multiple times */
-               ent->attr.cmpd_attr.val_paths = NULL;
        }
-       if (is_compound_entity(ent)) {
-               ent->attr.cmpd_attr.values = NULL;
-       } else if (is_method_entity(ent)) {
+       if (ent->entity_kind == IR_ENTITY_METHOD) {
                if (ent->attr.mtd_attr.param_access) {
                        DEL_ARR_F(ent->attr.mtd_attr.param_access);
                        ent->attr.mtd_attr.param_access = NULL;
@@ -173,15 +205,6 @@ static ir_entity *deep_entity_copy(ir_entity *old)
        *newe = *old;
        if (old->initializer != NULL) {
                /* FIXME: the initializers are NOT copied */
-       } else if (entity_has_compound_ent_values(old)) {
-               newe->attr.cmpd_attr.values    = NULL;
-               newe->attr.cmpd_attr.val_paths = NULL;
-               if (old->attr.cmpd_attr.values)
-                       newe->attr.cmpd_attr.values = DUP_ARR_F(ir_node *, old->attr.cmpd_attr.values);
-
-               /* FIXME: the compound graph paths are NOT copied */
-               if (old->attr.cmpd_attr.val_paths)
-                       newe->attr.cmpd_attr.val_paths = DUP_ARR_F(compound_graph_path *, old->attr.cmpd_attr.val_paths);
        } else if (is_method_entity(old)) {
                /* do NOT copy them, reanalyze. This might be the best solution */
                newe->attr.mtd_attr.param_access = NULL;
@@ -197,10 +220,6 @@ static ir_entity *deep_entity_copy(ir_entity *old)
        return newe;
 }
 
-/*
- * Copies the entity if the new_owner is different from the
- * owner of the old entity,  else returns the old entity.
- */
 ir_entity *copy_entity_own(ir_entity *old, ir_type *new_owner)
 {
        ir_entity *newe;
@@ -242,11 +261,12 @@ void free_entity(ir_entity *ent)
 
        assert(ent && ent->kind == k_entity);
        free_entity_attrs(ent);
+#ifdef DEBUG_libfirm
        ent->kind = k_BAD;
+#endif
        xfree(ent);
 }
 
-/* Outputs a unique number for this node */
 long get_entity_nr(const ir_entity *ent)
 {
        assert(ent && ent->kind == k_entity);
@@ -312,9 +332,22 @@ ir_type *(get_entity_type)(const ir_entity *ent)
        return _get_entity_type(ent);
 }
 
-void (set_entity_type)(ir_entity *ent, ir_type *type)
+void set_entity_type(ir_entity *ent, ir_type *type)
 {
-       _set_entity_type(ent, type);
+       switch (ent->entity_kind) {
+       case IR_ENTITY_METHOD:
+               assert(is_Method_type(type));
+               break;
+       case IR_ENTITY_NORMAL:
+               assert(!is_Method_type(type));
+               break;
+       case IR_ENTITY_LABEL:
+               assert(type == get_code_type());
+               break;
+       case IR_ENTITY_COMPOUND_MEMBER:
+               break;
+       }
+       ent->type = type;
 }
 
 ir_volatility (get_entity_volatility)(const ir_entity *ent)
@@ -327,7 +360,6 @@ void (set_entity_volatility)(ir_entity *ent, ir_volatility vol)
        _set_entity_volatility(ent, vol);
 }
 
-/* Return the name of the volatility. */
 const char *get_volatility_name(ir_volatility var)
 {
 #define X(a)    case a: return #a
@@ -359,7 +391,6 @@ void (set_entity_alignment)(ir_entity *ent, unsigned alignment)
        _set_entity_alignment(ent, alignment);
 }
 
-/* Return the name of the alignment. */
 const char *get_align_name(ir_align a)
 {
 #define X(a)    case a: return #a
@@ -373,26 +404,19 @@ const char *get_align_name(ir_align a)
 
 void set_entity_label(ir_entity *ent, ir_label_t label)
 {
+       assert(ent->entity_kind == IR_ENTITY_LABEL);
        ent->attr.code_attr.label = label;
 }
 
 ir_label_t get_entity_label(const ir_entity *ent)
 {
+       assert(ent->entity_kind == IR_ENTITY_LABEL);
        return ent->attr.code_attr.label;
 }
 
-static void verify_visibility(const ir_entity *entity)
-{
-       if (get_entity_visibility(entity) == ir_visibility_external
-                       && !is_method_entity(entity)) {
-               assert(!entity_has_definition(entity));
-       }
-}
-
 void set_entity_visibility(ir_entity *entity, ir_visibility visibility)
 {
        entity->visibility = visibility;
-       verify_visibility(entity);
 }
 
 ir_visibility get_entity_visibility(const ir_entity *entity)
@@ -420,13 +444,11 @@ void remove_entity_linkage(ir_entity *entity, ir_linkage linkage)
        entity->linkage &= ~linkage;
 }
 
-/* Checks if an entity is compiler generated */
 int (is_entity_compiler_generated)(const ir_entity *ent)
 {
        return _is_entity_compiler_generated(ent);
 }
 
-/* Sets/resets the compiler generated flag */
 void (set_entity_compiler_generated)(ir_entity *ent, int flag)
 {
        _set_entity_compiler_generated(ent, flag);
@@ -442,8 +464,7 @@ void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags)
        _set_entity_usage(ent, flags);
 }
 
-/* Set has no effect for existent entities of type method. */
-ir_node *get_atomic_ent_value(ir_entity *entity)
+ir_node *get_atomic_ent_value(const ir_entity *entity)
 {
        ir_initializer_t *initializer = get_entity_initializer(entity);
 
@@ -469,7 +490,7 @@ ir_node *get_atomic_ent_value(ir_entity *entity)
                panic("compound initializer in atomic entity not allowed (%+F)", entity);
        }
 
-       panic("invalid initializer kind in get_atomic_ent_value(%+F)", entity);
+       panic("invalid initializer kind (%+F)", entity);
 }
 
 void set_atomic_ent_value(ir_entity *entity, ir_node *val)
@@ -483,8 +504,6 @@ void set_atomic_ent_value(ir_entity *entity, ir_node *val)
        entity->initializer = initializer;
 }
 
-/* Returns true if the the node is representable as code on
- *  const_code_irg. */
 int is_irn_const_expression(ir_node *n)
 {
        /* we are in danger iff an exception will arise. TODO: be more precisely,
@@ -507,10 +526,6 @@ int is_irn_const_expression(ir_node *n)
        return 0;
 }
 
-/*
- * Copies a firm subgraph that complies to the restrictions for
- * constant expressions to block.
- */
 ir_node *copy_const_value(dbg_info *dbg, ir_node *n, ir_node *block)
 {
        ir_graph *irg = get_irn_irg(block);
@@ -567,15 +582,22 @@ ir_node *copy_const_value(dbg_info *dbg, ir_node *n, ir_node *block)
                nn = new_rd_Conv(dbg, block,
                                 copy_const_value(dbg, get_Conv_op(n), block), m);
                break;
+       case iro_Minus:
+               nn = new_rd_Minus(dbg, block,
+                                 copy_const_value(dbg, get_Minus_op(n), block), m);
+               break;
+       case iro_Not:
+               nn = new_rd_Not(dbg, block,
+                               copy_const_value(dbg, get_Not_op(n), block), m);
+               break;
        case iro_Unknown:
                nn = new_r_Unknown(irg, m); break;
        default:
-               panic("opcode invalid or not implemented");
+               panic("opcode invalid or not implemented %+F", n);
        }
        return nn;
 }
 
-/** Return the name of the initializer kind. */
 const char *get_initializer_kind_name(ir_initializer_kind_t ini)
 {
 #define X(a)    case a: return #a
@@ -864,6 +886,22 @@ void set_entity_irg(ir_entity *ent, ir_graph *irg)
        ent->attr.mtd_attr.irg = irg;
 }
 
+int (is_parameter_entity)(const ir_entity *entity)
+{
+       return _is_parameter_entity(entity);
+}
+
+size_t (get_entity_parameter_number)(const ir_entity *entity)
+{
+       return _get_entity_parameter_number(entity);
+}
+
+void set_entity_parameter_number(ir_entity *entity, size_t n)
+{
+       assert(is_parameter_entity(entity));
+       entity->attr.parameter.number = n;
+}
+
 unsigned get_entity_vtable_number(const ir_entity *ent)
 {
        assert(is_method_entity((ir_entity *)ent));
@@ -876,6 +914,11 @@ void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number)
        ent->attr.mtd_attr.vtable_number = vtable_number;
 }
 
+int is_unknown_entity(const ir_entity *entity)
+{
+       return entity->entity_kind == IR_ENTITY_UNKNOWN;
+}
+
 int (is_entity)(const void *thing)
 {
        return _is_entity(thing);
@@ -894,7 +937,7 @@ int is_compound_entity(const ir_entity *ent)
        ir_type     *t  = get_entity_type(ent);
        const tp_op *op = get_type_tpop(t);
        return (op == type_class || op == type_struct ||
-               op == type_array || op == type_union);
+               op == type_array || op == type_union);
 }
 
 int is_method_entity(const ir_entity *ent)
@@ -930,63 +973,30 @@ int (entity_not_visited)(const ir_entity *ent)
 
 mtp_additional_properties get_entity_additional_properties(const ir_entity *ent)
 {
-       ir_graph *irg;
-
        assert(is_method_entity(ent));
-
-       /* first check, if the graph has additional properties */
-       irg = get_entity_irg(ent);
-
-       if (irg)
-               return get_irg_additional_properties(irg);
-
-       if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
-               return get_method_additional_properties(get_entity_type(ent));
-
-       return ent->attr.mtd_attr.irg_add_properties;
+       return ent->attr.mtd_attr.properties;
 }
 
 void set_entity_additional_properties(ir_entity *ent, mtp_additional_properties property_mask)
 {
-       ir_graph *irg;
-
        assert(is_method_entity(ent));
+       /* you mustn't set less properties than the entities type */
+       assert((get_method_additional_properties(get_entity_type(ent)) & ~property_mask) == 0);
 
-       /* first check, if the graph exists */
-       irg = get_entity_irg(ent);
-       if (irg)
-               set_irg_additional_properties(irg, property_mask);
-       else {
-               /* do not allow to set the mtp_property_inherited flag or
-                * the automatic inheritance of flags will not work */
-               ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
-       }
+       /* do not allow to set the mtp_property_inherited flag or
+        * the automatic inheritance of flags will not work */
+       ent->attr.mtd_attr.properties = property_mask;
 }
 
 void add_entity_additional_properties(ir_entity *ent, mtp_additional_properties properties)
 {
-       ir_graph *irg;
-
        assert(is_method_entity(ent));
 
-       /* first check, if the graph exists */
-       irg = get_entity_irg(ent);
-       if (irg)
-               add_irg_additional_properties(irg, properties);
-       else {
-               mtp_additional_properties mask = ent->attr.mtd_attr.irg_add_properties;
-
-               if (mask & mtp_property_inherited)
-                       mask = get_method_additional_properties(get_entity_type(ent));
-
-               /* do not allow to set the mtp_property_inherited flag or
-                * the automatic inheritance of flags will not work */
-               ent->attr.mtd_attr.irg_add_properties = mask | (properties & ~mtp_property_inherited);
-       }
+       /* do not allow to set the mtp_property_inherited flag or
+        * the automatic inheritance of flags will not work */
+       ent->attr.mtd_attr.properties |= properties;
 }
 
-/* Returns the class type that this type info entity represents or NULL
-   if ent is no type info entity. */
 ir_type *(get_entity_repr_class)(const ir_entity *ent)
 {
        return _get_entity_repr_class(ent);
@@ -1010,28 +1020,27 @@ int entity_is_externally_visible(const ir_entity *entity)
 
 int entity_has_definition(const ir_entity *entity)
 {
-       return entity->initializer != NULL
-               || get_entity_irg(entity) != NULL
-               || entity_has_compound_ent_values(entity);
+       if (is_method_entity(entity)) {
+               return get_entity_irg(entity) != NULL
+                   && (get_entity_linkage(entity) & IR_LINKAGE_NO_CODEGEN) == 0;
+       } else {
+               return entity->initializer != NULL;
+       }
 }
 
-void ir_init_entity(void)
+void ir_init_entity(ir_prog *irp)
 {
-       assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
-       assert(!unknown_entity && "Call firm_init_entity() only once!");
-
-       unknown_entity = new_d_entity(NULL, new_id_from_str(UNKNOWN_ENTITY_NAME),
-                                     firm_unknown_type, NULL);
-       set_entity_visibility(unknown_entity, ir_visibility_external);
-       set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
+       ident *id = new_id_from_str(UNKNOWN_ENTITY_NAME);
+       irp->unknown_entity = intern_new_entity(NULL, IR_ENTITY_UNKNOWN, id,
+                                               irp->unknown_type, NULL);
+       set_entity_visibility(irp->unknown_entity, ir_visibility_external);
+       set_entity_ld_ident(irp->unknown_entity, id);
+       hook_new_entity(irp->unknown_entity);
 }
 
-void ir_finish_entity(void)
+void ir_finish_entity(ir_prog *irp)
 {
-       if (unknown_entity != NULL) {
-               free_entity(unknown_entity);
-               unknown_entity = NULL;
-       }
+       free_entity(irp->unknown_entity);
 }
 
 ir_allocation get_entity_allocation(const ir_entity *entity)