ia32: Merge Test and Test8Bit.
[libfirm] / ir / tr / entity.c
index 65af77b..455c16c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
+ * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
  *
  * This file is part of libFirm.
  *
@@ -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                                                          */
 /*-----------------------------------------------------------------*/
 
-/**
- * Add an entity to it's already set owner type.
- */
-static inline void insert_entity_in_owner(ir_entity *ent)
+static ir_entity *intern_new_entity(ir_type *owner, ir_entity_kind kind,
+                                    ident *name, ir_type *type, dbg_info *dbgi)
 {
-       ir_type *owner = ent->owner;
-       switch (get_type_tpop_code(owner)) {
-       case tpo_class:
-               add_class_member(owner, ent);
-               break;
-       case tpo_struct:
-               add_struct_member(owner, ent);
-               break;
-       case tpo_union:
-               add_union_member(owner, ent);
-               break;
-       case tpo_array:
-               set_array_element_entity(owner, ent);
-               break;
-       default:
-               panic("Unsupported type kind");
-       }
-}  /* insert_entity_in_owner */
-
-/**
- * Creates a new entity. This entity is NOT inserted in the owner type.
- *
- * @param db     debug info for this entity
- * @param owner  the owner type of the new entity
- * @param name   the name of the new entity
- * @param type   the type of the new entity
- *
- * @return the new created entity
- */
-static inline ir_entity *new_rd_entity(dbg_info *db, ir_type *owner,
-                                       ident *name, ir_type *type)
-{
-       ir_entity *res;
-       ir_graph *rem;
-
-       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;
@@ -110,73 +66,103 @@ static inline ir_entity *new_rd_entity(dbg_info *db, ir_type *owner,
        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;
-
-       if (is_Method_type(type)) {
-               symconst_symbol sym;
-               ir_mode *mode = is_Method_type(type) ? mode_P_code : mode_P_data;
-               sym.entity_p            = res;
-               rem                     = current_ir_graph;
-               current_ir_graph        = get_const_code_irg();
-               set_atomic_ent_value(res, new_SymConst(mode, sym, symconst_addr_ent));
-               current_ir_graph        = rem;
-               res->linkage            = IR_LINKAGE_CONSTANT;
-               res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
-               res->attr.mtd_attr.vtable_number      = 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;
-       }
-
-       if (is_Class_type(owner)) {
-               res->overwrites    = NEW_ARR_F(ir_entity *, 0);
-               res->overwrittenby = NEW_ARR_F(ir_entity *, 0);
-       } else {
-               res->overwrites    = NULL;
-               res->overwrittenby = NULL;
-       }
-
 #ifdef DEBUG_libfirm
        res->nr = get_irp_new_node_nr();
-#endif /* DEBUG_libfirm */
+#endif
+
+       /* Remember entity in its owner. */
+       if (owner != NULL)
+               add_compound_member(owner, res);
 
        res->visit = 0;
-       set_entity_dbg_info(res, db);
+       set_entity_dbg_info(res, dbgi);
 
        return res;
-}  /* new_rd_entity */
+}
 
-ir_entity *new_d_entity(ir_type *owner, ident *name, ir_type *type, dbg_info *db)
+ir_entity *new_d_entity(ir_type *owner, ident *name, ir_type *type,
+                        dbg_info *db)
 {
        ir_entity *res;
 
-       assert(is_compound_type(owner));
-       res = new_rd_entity(db, owner, name, type);
-       /* Remember entity in it's owner. */
-       insert_entity_in_owner(res);
+       if (is_Method_type(type)) {
+               ir_graph *irg = get_const_code_irg();
+               symconst_symbol sym;
+               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_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);
+       }
 
        hook_new_entity(res);
        return res;
-}  /* new_d_entity */
+}
 
 ir_entity *new_entity(ir_type *owner, ident *name, ir_type *type)
 {
        return new_d_entity(owner, name, type, NULL);
-}  /* new_entity */
+}
+
+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_label_entity(ir_label_t label)
+{
+       return new_d_label_entity(label, NULL);
+}
 
 /**
  * Free entity attributes.
@@ -185,33 +171,19 @@ ir_entity *new_entity(ir_type *owner, ident *name, ir_type *type)
  */
 static void free_entity_attrs(ir_entity *ent)
 {
-       int i;
-       if (get_type_tpop(get_entity_owner(ent)) == type_class) {
-               DEL_ARR_F(ent->overwrites);    ent->overwrites = NULL;
-               DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
-       } else {
-               assert(ent->overwrites == NULL);
-               assert(ent->overwrittenby == NULL);
+       if (ent->overwrites != NULL) {
+               DEL_ARR_F(ent->overwrites);
+               ent->overwrites = NULL;
+       }
+       if (ent->overwrittenby != NULL) {
+               DEL_ARR_F(ent->overwrittenby);
+               ent->overwrittenby = NULL;
        }
+
        if (ent->initializer != NULL) {
                /* TODO: free initializers */
-       } else if (entity_has_compound_ent_values(ent)) {
-               if (ent->attr.cmpd_attr.val_paths) {
-                       for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i)
-                               if (ent->attr.cmpd_attr.val_paths[i]) {
-                                       /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ;  * @@@ warum nich? */
-                                       /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
-                                       /* DEL_ARR_F(ent->attr.cmpd_attr.val_paths); */
-                               }
-                               ent->attr.cmpd_attr.val_paths = NULL;
-               }
        }
-       if (is_compound_entity(ent)) {
-               if (ent->attr.cmpd_attr.values) {
-                       /*DEL_ARR_F(ent->attr.cmpd_attr.values)*/;
-               }
-               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;
@@ -221,7 +193,7 @@ static void free_entity_attrs(ir_entity *ent)
                        ent->attr.mtd_attr.param_weight = NULL;
                }
        }
-}  /* free_entity_attrs */
+}
 
 /**
  * Creates a deep copy of an entity.
@@ -233,30 +205,21 @@ 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;
                newe->attr.mtd_attr.param_weight = NULL;
        }
+       newe->overwrites    = NULL;
+       newe->overwrittenby = NULL;
 
 #ifdef DEBUG_libfirm
        newe->nr = get_irp_new_node_nr();
 #endif
+       hook_new_entity(newe);
        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;
@@ -268,46 +231,42 @@ ir_entity *copy_entity_own(ir_entity *old, ir_type *new_owner)
                return old;
 
        /* create a deep copy so we are safe of aliasing and double-freeing. */
-       newe = deep_entity_copy(old);
+       newe        = deep_entity_copy(old);
        newe->owner = new_owner;
+       add_compound_member(new_owner, newe);
 
-       if (is_Class_type(new_owner)) {
-               newe->overwrites    = NEW_ARR_F(ir_entity *, 0);
-               newe->overwrittenby = NEW_ARR_F(ir_entity *, 0);
-       }
-
-       insert_entity_in_owner(newe);
        return newe;
-}  /* copy_entity_own */
+}
 
 ir_entity *copy_entity_name(ir_entity *old, ident *new_name)
 {
        ir_entity *newe;
        assert(old && old->kind == k_entity);
 
-       if (old->name == new_name) return old;
-       newe = deep_entity_copy(old);
+       if (old->name == new_name)
+               return old;
+
+       newe       = deep_entity_copy(old);
        newe->name = new_name;
        newe->ld_name = NULL;
-
-       if (is_Class_type(newe->owner)) {
-               newe->overwrites    = DUP_ARR_F(ir_entity *, old->overwrites);
-               newe->overwrittenby = DUP_ARR_F(ir_entity *, old->overwrittenby);
-       }
-       insert_entity_in_owner(newe);
+       add_compound_member(old->owner, newe);
 
        return newe;
-}  /* copy_entity_name */
+}
 
 void free_entity(ir_entity *ent)
 {
+       if (ent->owner != NULL && !is_Array_type(ent->owner))
+               remove_compound_member(ent->owner, ent);
+
        assert(ent && ent->kind == k_entity);
        free_entity_attrs(ent);
+#ifdef DEBUG_libfirm
        ent->kind = k_BAD;
-       free(ent);
-}  /* free_entity */
+#endif
+       xfree(ent);
+}
 
-/* Outputs a unique number for this node */
 long get_entity_nr(const ir_entity *ent)
 {
        assert(ent && ent->kind == k_entity);
@@ -316,7 +275,7 @@ long get_entity_nr(const ir_entity *ent)
 #else
        return (long)PTR_TO_INT(ent);
 #endif
-}  /* get_entity_nr */
+}
 
 const char *(get_entity_name)(const ir_entity *ent)
 {
@@ -342,6 +301,9 @@ void set_entity_owner(ir_entity *ent, ir_type *owner)
 {
        assert(is_entity(ent));
        assert(is_compound_type(owner));
+
+       remove_compound_member(ent->owner, ent);
+       add_compound_member(owner, ent);
        ent->owner = owner;
 }
 
@@ -360,14 +322,32 @@ const char *(get_entity_ld_name)(const ir_entity *ent)
        return _get_entity_ld_name(ent);
 }
 
+int entity_has_ld_ident(const ir_entity *entity)
+{
+       return entity->ld_name != NULL;
+}
+
 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)
@@ -380,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
@@ -390,7 +369,7 @@ const char *get_volatility_name(ir_volatility var)
     default: return "BAD VALUE";
        }
 #undef X
-}  /* get_volatility_name */
+}
 
 ir_align (get_entity_aligned)(const ir_entity *ent)
 {
@@ -412,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
@@ -422,35 +400,28 @@ const char *get_align_name(ir_align a)
        default: return "BAD VALUE";
        }
 #undef X
-}  /* get_align_name */
+}
 
 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)
 {
-       return entity->visibility;
+       return (ir_visibility)entity->visibility;
 }
 
 void set_entity_linkage(ir_entity *entity, ir_linkage linkage)
@@ -473,17 +444,15 @@ 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);
-}  /* is_entity_compiler_generated */
+}
 
-/* Sets/resets the compiler generated flag */
 void (set_entity_compiler_generated)(ir_entity *ent, int flag)
 {
        _set_entity_compiler_generated(ent, flag);
-}  /* set_entity_compiler_generated */
+}
 
 ir_entity_usage (get_entity_usage)(const ir_entity *ent)
 {
@@ -495,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);
 
@@ -513,7 +481,7 @@ ir_node *get_atomic_ent_value(ir_entity *entity)
                return new_r_Const(get_const_code_irg(), get_mode_null(mode));
        }
        case IR_INITIALIZER_TARVAL: {
-               tarval *tv = get_initializer_tarval_value(initializer);
+               ir_tarval *tv = get_initializer_tarval_value(initializer);
                return new_r_Const(get_const_code_irg(), tv);
        }
        case IR_INITIALIZER_CONST:
@@ -522,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)
@@ -536,19 +504,14 @@ 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)
 {
-       ir_mode *m;
-
        /* we are in danger iff an exception will arise. TODO: be more precisely,
         * for instance Div. will NOT rise if divisor != 0
         */
        if (is_binop(n) && !is_fragile_op(n))
                return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
 
-       m = get_irn_mode(n);
        switch (get_irn_opcode(n)) {
        case iro_Const:
        case iro_SymConst:
@@ -561,14 +524,11 @@ int is_irn_const_expression(ir_node *n)
                break;
        }
        return 0;
-}  /* is_irn_const_expression */
+}
 
-/*
- * Copies a firm subgraph that complies to the restrictions for
- * constant expressions to current_block in current_ir_graph.
- */
-ir_node *copy_const_value(dbg_info *dbg, ir_node *n)
+ir_node *copy_const_value(dbg_info *dbg, ir_node *n, ir_node *block)
 {
+       ir_graph *irg = get_irn_irg(block);
        ir_node *nn;
        ir_mode *m;
 
@@ -578,43 +538,66 @@ ir_node *copy_const_value(dbg_info *dbg, ir_node *n)
        m = get_irn_mode(n);
        switch (get_irn_opcode(n)) {
        case iro_Const:
-               nn = new_d_Const_type(dbg, get_Const_tarval(n), get_Const_type(n));
+               nn = new_rd_Const(dbg, irg, get_Const_tarval(n));
                break;
        case iro_SymConst:
-               nn = new_d_SymConst_type(dbg, get_irn_mode(n), get_SymConst_symbol(n), get_SymConst_kind(n),
-                       get_SymConst_value_type(n));
+               nn = new_rd_SymConst(dbg, irg, get_irn_mode(n), get_SymConst_symbol(n), get_SymConst_kind(n));
                break;
        case iro_Add:
-               nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
-                       copy_const_value(dbg, get_Add_right(n)), m); break;
+               nn = new_rd_Add(dbg, block,
+                               copy_const_value(dbg, get_Add_left(n), block),
+                               copy_const_value(dbg, get_Add_right(n), block), m);
+               break;
        case iro_Sub:
-               nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
-                       copy_const_value(dbg, get_Sub_right(n)), m); break;
+               nn = new_rd_Sub(dbg, block,
+                               copy_const_value(dbg, get_Sub_left(n), block),
+                               copy_const_value(dbg, get_Sub_right(n), block), m);
+               break;
        case iro_Mul:
-               nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
-                       copy_const_value(dbg, get_Mul_right(n)), m); break;
+               nn = new_rd_Mul(dbg, block,
+                               copy_const_value(dbg, get_Mul_left(n), block),
+                               copy_const_value(dbg, get_Mul_right(n), block), m);
+               break;
        case iro_And:
-               nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
-                       copy_const_value(dbg, get_And_right(n)), m); break;
+               nn = new_rd_And(dbg, block,
+                               copy_const_value(dbg, get_And_left(n), block),
+                               copy_const_value(dbg, get_And_right(n), block), m);
+               break;
        case iro_Or:
-               nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
-                       copy_const_value(dbg, get_Or_right(n)), m); break;
+               nn = new_rd_Or(dbg, block,
+                              copy_const_value(dbg, get_Or_left(n), block),
+                              copy_const_value(dbg, get_Or_right(n), block), m);
+               break;
        case iro_Eor:
-               nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
-                       copy_const_value(dbg, get_Eor_right(n)), m); break;
+               nn = new_rd_Eor(dbg, block,
+                               copy_const_value(dbg, get_Eor_left(n), block),
+                               copy_const_value(dbg, get_Eor_right(n), block), m);
+               break;
        case iro_Cast:
-               nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
+               nn = new_rd_Cast(dbg, block,
+                                copy_const_value(dbg, get_Cast_op(n), block),
+                                get_Cast_type(n));
+               break;
        case iro_Conv:
-               nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
+               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_Unknown(m); break;
+               nn = new_r_Unknown(irg, m); break;
        default:
-               panic("opcode invalid or not implemented");
+               panic("opcode invalid or not implemented %+F", n);
        }
        return nn;
-}  /* copy_const_value */
+}
 
-/** 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
@@ -640,34 +623,36 @@ ir_initializer_t *create_initializer_const(ir_node *value)
        struct obstack *obst = get_irg_obstack(get_const_code_irg());
 
        ir_initializer_t *initializer
-               = obstack_alloc(obst, sizeof(ir_initializer_const_t));
+               = (ir_initializer_t*)OALLOC(obst, ir_initializer_const_t);
        initializer->kind         = IR_INITIALIZER_CONST;
        initializer->consti.value = value;
 
        return initializer;
 }
 
-ir_initializer_t *create_initializer_tarval(tarval *tv)
+ir_initializer_t *create_initializer_tarval(ir_tarval *tv)
 {
        struct obstack *obst = get_irg_obstack(get_const_code_irg());
 
        ir_initializer_t *initializer
-               = obstack_alloc(obst, sizeof(ir_initializer_tarval_t));
+               = (ir_initializer_t*)OALLOC(obst, ir_initializer_tarval_t);
        initializer->kind         = IR_INITIALIZER_TARVAL;
        initializer->tarval.value = tv;
 
        return initializer;
 }
 
-ir_initializer_t *create_initializer_compound(unsigned n_entries)
+ir_initializer_t *create_initializer_compound(size_t n_entries)
 {
        struct obstack *obst = get_irg_obstack(get_const_code_irg());
 
        size_t i;
        size_t size  = sizeof(ir_initializer_compound_t)
-                    + (n_entries-1) * sizeof(ir_initializer_t*);
+                    + n_entries * sizeof(ir_initializer_t*)
+                    - sizeof(ir_initializer_t*);
 
-       ir_initializer_t *initializer = obstack_alloc(obst, size);
+       ir_initializer_t *initializer
+               = (ir_initializer_t*)obstack_alloc(obst, size);
        initializer->kind                    = IR_INITIALIZER_COMPOUND;
        initializer->compound.n_initializers = n_entries;
 
@@ -684,20 +669,20 @@ ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
        return skip_Id(initializer->consti.value);
 }
 
-tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
+ir_tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
 {
        assert(initializer->kind == IR_INITIALIZER_TARVAL);
        return initializer->tarval.value;
 }
 
-unsigned get_initializer_compound_n_entries(const ir_initializer_t *initializer)
+size_t get_initializer_compound_n_entries(const ir_initializer_t *initializer)
 {
        assert(initializer->kind == IR_INITIALIZER_COMPOUND);
        return initializer->compound.n_initializers;
 }
 
 void set_initializer_compound_value(ir_initializer_t *initializer,
-                                    unsigned index, ir_initializer_t *value)
+                                    size_t index, ir_initializer_t *value)
 {
        assert(initializer->kind == IR_INITIALIZER_COMPOUND);
        assert(index < initializer->compound.n_initializers);
@@ -706,7 +691,7 @@ void set_initializer_compound_value(ir_initializer_t *initializer,
 }
 
 ir_initializer_t *get_initializer_compound_value(
-               const ir_initializer_t *initializer, unsigned index)
+               const ir_initializer_t *initializer, size_t index)
 {
        assert(initializer->kind == IR_INITIALIZER_COMPOUND);
        assert(index < initializer->compound.n_initializers);
@@ -726,7 +711,7 @@ static void check_entity_initializer(ir_entity *entity)
        ir_type          *entity_tp   = get_entity_type(entity);
        switch (initializer->kind) {
        case IR_INITIALIZER_COMPOUND:
-               assert(is_compound_type(entity_tp));
+               assert(is_compound_type(entity_tp) || is_Array_type(entity_tp));
                break;
        case IR_INITIALIZER_CONST:
                /* methods are initialized by a SymConst */
@@ -779,54 +764,50 @@ void (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset)
 
 void add_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
 {
-#ifndef NDEBUG
-       ir_type *owner     = get_entity_owner(ent);
-       ir_type *ovw_ovner = get_entity_owner(overwritten);
-       assert(is_Class_type(owner));
-       assert(is_Class_type(ovw_ovner));
-       assert(! is_class_final(ovw_ovner));
-#endif /* NDEBUG */
+       if (ent->overwrites == NULL) {
+               ent->overwrites = NEW_ARR_F(ir_entity*, 0);
+       }
        ARR_APP1(ir_entity *, ent->overwrites, overwritten);
+       if (overwritten->overwrittenby == NULL) {
+               overwritten->overwrittenby = NEW_ARR_F(ir_entity*, 0);
+       }
        ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
 }
 
-int get_entity_n_overwrites(const ir_entity *ent)
+size_t get_entity_n_overwrites(const ir_entity *ent)
 {
-       assert(is_Class_type(get_entity_owner(ent)));
-       return (ARR_LEN(ent->overwrites));
+       if (ent->overwrites == NULL)
+               return 0;
+       return ARR_LEN(ent->overwrites);
 }
 
-int get_entity_overwrites_index(const ir_entity *ent, ir_entity *overwritten)
+size_t get_entity_overwrites_index(const ir_entity *ent, ir_entity *overwritten)
 {
-       int i, n;
-       assert(is_Class_type(get_entity_owner(ent)));
-       n = get_entity_n_overwrites(ent);
+       size_t i;
+       size_t n = get_entity_n_overwrites(ent);
        for (i = 0; i < n; ++i) {
                if (get_entity_overwrites(ent, i) == overwritten)
                        return i;
        }
-       return -1;
+       return (size_t)-1;
 }
 
-ir_entity *get_entity_overwrites(const ir_entity *ent, int pos)
+ir_entity *get_entity_overwrites(const ir_entity *ent, size_t pos)
 {
-       assert(is_Class_type(get_entity_owner(ent)));
        assert(pos < get_entity_n_overwrites(ent));
        return ent->overwrites[pos];
 }
 
-void set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten)
+void set_entity_overwrites(ir_entity *ent, size_t pos, ir_entity *overwritten)
 {
-       assert(is_Class_type(get_entity_owner(ent)));
        assert(pos < get_entity_n_overwrites(ent));
        ent->overwrites[pos] = overwritten;
 }
 
 void remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
 {
-       int i, n;
-       assert(is_Class_type(get_entity_owner(ent)));
-       n = ARR_LEN(ent->overwrites);
+       size_t i;
+       size_t n = get_entity_n_overwrites(ent);
        for (i = 0; i < n; ++i) {
                if (ent->overwrites[i] == overwritten) {
                        for (; i < n - 1; i++)
@@ -837,49 +818,42 @@ void remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
        }
 }
 
-void add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites)
-{
-       add_entity_overwrites(overwrites, ent);
-}
 
-int get_entity_n_overwrittenby(const ir_entity *ent)
+size_t get_entity_n_overwrittenby(const ir_entity *ent)
 {
-       assert(is_Class_type(get_entity_owner(ent)));
+       if (ent->overwrittenby == NULL)
+               return 0;
        return ARR_LEN(ent->overwrittenby);
 }
 
-int get_entity_overwrittenby_index(const ir_entity *ent, ir_entity *overwrites)
+size_t get_entity_overwrittenby_index(const ir_entity *ent,
+                                      ir_entity *overwrites)
 {
-       int i, n;
-       assert(is_Class_type(get_entity_owner(ent)));
-       n = get_entity_n_overwrittenby(ent);
+       size_t i;
+       size_t n = get_entity_n_overwrittenby(ent);
        for (i = 0; i < n; ++i) {
                if (get_entity_overwrittenby(ent, i) == overwrites)
                        return i;
        }
-       return -1;
+       return (size_t)-1;
 }
 
-ir_entity *get_entity_overwrittenby(const ir_entity *ent, int pos)
+ir_entity *get_entity_overwrittenby(const ir_entity *ent, size_t pos)
 {
-       assert(is_Class_type(get_entity_owner(ent)));
        assert(pos < get_entity_n_overwrittenby(ent));
        return ent->overwrittenby[pos];
 }
 
-void set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites)
+void set_entity_overwrittenby(ir_entity *ent, size_t pos, ir_entity *overwrites)
 {
-       assert(is_Class_type(get_entity_owner(ent)));
        assert(pos < get_entity_n_overwrittenby(ent));
        ent->overwrittenby[pos] = overwrites;
 }
 
 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites)
 {
-       int i, n;
-       assert(is_Class_type(get_entity_owner(ent)));
-
-       n = ARR_LEN(ent->overwrittenby);
+       size_t i;
+       size_t n = get_entity_n_overwrittenby(ent);
        for (i = 0; i < n; ++i) {
                if (ent->overwrittenby[i] == overwrites) {
                        for (; i < n - 1; ++i)
@@ -912,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));
@@ -924,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);
@@ -942,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)
@@ -976,65 +971,32 @@ int (entity_not_visited)(const ir_entity *ent)
        return _entity_not_visited(ent);
 }
 
-unsigned get_entity_additional_properties(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, unsigned property_mask)
+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 set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
+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)
-               set_irg_additional_property(irg, flag);
-       else {
-               unsigned 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 | (flag & ~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);
@@ -1058,30 +1020,32 @@ 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 firm_init_entity(void)
+void ir_init_entity(ir_prog *irp)
 {
-       symconst_symbol sym;
-
-       assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
-       assert(!unknown_entity && "Call firm_init_entity() only once!");
-
-       unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
-       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);
+}
 
-       current_ir_graph = get_const_code_irg();
-       sym.entity_p     = unknown_entity;
+void ir_finish_entity(ir_prog *irp)
+{
+       free_entity(irp->unknown_entity);
 }
 
 ir_allocation get_entity_allocation(const ir_entity *entity)
 {
-       return entity->allocation;
+       return (ir_allocation)entity->allocation;
 }
 
 void set_entity_allocation(ir_entity *entity, ir_allocation allocation)
@@ -1091,7 +1055,7 @@ void set_entity_allocation(ir_entity *entity, ir_allocation allocation)
 
 ir_peculiarity get_entity_peculiarity(const ir_entity *entity)
 {
-       return entity->peculiarity;
+       return (ir_peculiarity)entity->peculiarity;
 }
 
 void set_entity_peculiarity(ir_entity *entity, ir_peculiarity peculiarity)