make unique types/entities part of irprog
[libfirm] / ir / tr / entity.c
index e7541d1..5981e3c 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 "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,6 +67,7 @@ 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;
@@ -85,11 +78,30 @@ ir_entity *new_d_entity(ir_type *owner, ident *name, ir_type *type,
        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;
@@ -98,32 +110,62 @@ ir_entity *new_d_entity(ir_type *owner, ident *name, ir_type *type,
                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)) {
+       } else if (owner != NULL
+                  && (is_compound_type(owner) && !(owner->flags & tf_segment))) {
+               res = intern_new_entity(owner, IR_ENTITY_COMPOUND_MEMBER, name, type, db);
                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;
+       } 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);
 }
 
 /**
@@ -149,9 +191,9 @@ static void free_entity_attrs(ir_entity *ent)
                 * multiple times */
                ent->attr.cmpd_attr.val_paths = NULL;
        }
-       if (is_compound_entity(ent)) {
+       if (ent->entity_kind == IR_ENTITY_COMPOUND_MEMBER) {
                ent->attr.cmpd_attr.values = NULL;
-       } else if (is_method_entity(ent)) {
+       } else 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;
@@ -197,10 +239,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 +280,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 +351,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 +379,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 +410,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,11 +423,13 @@ 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;
 }
 
@@ -420,13 +472,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,7 +492,6 @@ 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_initializer_t *initializer = get_entity_initializer(entity);
@@ -483,8 +532,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 +554,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);
@@ -575,7 +618,6 @@ ir_node *copy_const_value(dbg_info *dbg, ir_node *n, ir_node *block)
        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 +906,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 +934,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 +957,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)
@@ -985,8 +1048,6 @@ void add_entity_additional_properties(ir_entity *ent, mtp_additional_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);
@@ -1015,23 +1076,19 @@ int entity_has_definition(const ir_entity *entity)
                || entity_has_compound_ent_values(entity);
 }
 
-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)