- fixed one Win32 "deprecated posix name" warning
[libfirm] / ir / tr / entity.c
index e10348a..b3f5808 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
  *
  * This file is part of libFirm.
  *
 #include "irhooks.h"
 #include "irprintf.h"
 
-/* All this is needed to build the constant node for methods: */
 #include "irprog_t.h"
 #include "ircons.h"
 #include "tv_t.h"
-#include "irdump.h"  /* for output if errors occur. */
+#include "irdump.h"
+#include "irgraph_t.h"
 
-#include "callgraph.h"  /* for dumping debug output */
+#include "callgraph.h"
 
 /**
  * An interval initializer.
@@ -157,10 +157,11 @@ new_rd_entity(dbg_info *db, ir_type *owner, ident *name, ir_type *type)
 
        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();
-               res->value              = new_SymConst(sym, symconst_addr_ent);
+               res->value              = new_SymConst(mode, sym, symconst_addr_ent);
                current_ir_graph        = rem;
                res->allocation         = allocation_static;
                res->variability        = variability_constant;
@@ -169,7 +170,6 @@ new_rd_entity(dbg_info *db, 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;
-               res->attr.mtd_attr.section            = section_text;
        } else if (is_compound_type(type)) {
                res->variability = variability_uninitialized;
                res->value       = NULL;
@@ -254,11 +254,16 @@ static void free_entity_attrs(ir_entity *ent) {
        }
 }  /* free_entity_attrs */
 
+/*
+ * 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;
        assert(is_entity(old));
        assert(is_compound_type(new_owner));
+       assert(get_type_state(new_owner) != layout_fixed);
 
        if (old->owner == new_owner) return old;
        newe = xmalloc(sizeof(*newe));
@@ -651,7 +656,7 @@ ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
                nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
                break;
        case iro_SymConst:
-               nn = new_d_SymConst_type(dbg, get_SymConst_symbol(n), get_SymConst_kind(n),
+               nn = new_d_SymConst_type(dbg, get_irn_mode(n), get_SymConst_symbol(n), get_SymConst_kind(n),
                        get_SymConst_value_type(n));
                break;
        case iro_Add:
@@ -686,6 +691,116 @@ ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
        return nn;
 }  /* copy_const_value */
 
+static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };
+
+ir_initializer_t *get_initializer_null(void)
+{
+       return &null_initializer;
+}
+
+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));
+       initializer->kind         = IR_INITIALIZER_CONST;
+       initializer->consti.value = value;
+
+       return initializer;
+}
+
+ir_initializer_t *create_initializer_tarval(tarval *tv)
+{
+       struct obstack *obst = get_irg_obstack(get_const_code_irg());
+
+       ir_initializer_t *initializer
+               = obstack_alloc(obst, sizeof(ir_initializer_tarval_t));
+       initializer->kind         = IR_INITIALIZER_TARVAL;
+       initializer->tarval.value = tv;
+
+       return initializer;
+}
+
+ir_initializer_t *create_initializer_compound(unsigned 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*);
+
+       ir_initializer_t *initializer = obstack_alloc(obst, size);
+       initializer->kind                    = IR_INITIALIZER_COMPOUND;
+       initializer->compound.n_initializers = n_entries;
+
+       for(i = 0; i < n_entries; ++i) {
+               initializer->compound.initializers[i] = get_initializer_null();
+       }
+
+       return initializer;
+}
+
+ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
+{
+       assert(initializer->kind == IR_INITIALIZER_CONST);
+       return initializer->consti.value;
+}
+
+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)
+{
+       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)
+{
+       assert(initializer->kind == IR_INITIALIZER_COMPOUND);
+       assert(index < initializer->compound.n_initializers);
+
+       initializer->compound.initializers[index] = value;
+}
+
+ir_initializer_t *get_initializer_compound_value(
+               const ir_initializer_t *initializer, unsigned index)
+{
+       assert(initializer->kind == IR_INITIALIZER_COMPOUND);
+       assert(index < initializer->compound.n_initializers);
+
+       return initializer->compound.initializers[index];
+}
+
+ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
+{
+       return initializer->kind;
+}
+
+static void check_entity_initializer(ir_entity *entity)
+{
+       /* TODO */
+       (void) entity;
+}
+
+void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
+{
+       entity->attr.initializer = initializer;
+       entity->has_initializer  = 1;
+       check_entity_initializer(entity);
+}
+
+ir_initializer_t *get_entity_initializer(const ir_entity *entity)
+{
+       assert(entity->has_initializer);
+       return entity->attr.initializer;
+}
+
 /* Creates a new compound graph path. */
 compound_graph_path *
 new_compound_graph_path(ir_type *tp, int length) {
@@ -986,10 +1101,10 @@ set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
 }  /* set_array_entity_values */
 
 /* Return the overall offset of value at position pos in bytes. */
-int get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
+unsigned get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
        compound_graph_path *path;
        int path_len, i;
-       int offset = 0;
+       unsigned offset = 0;
        ir_type *curr_tp;
 
        assert(get_type_state(get_entity_type(ent)) == layout_fixed);
@@ -1001,16 +1116,14 @@ int get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
        for (i = 0; i < path_len; ++i) {
                if (is_Array_type(curr_tp)) {
                        ir_type *elem_type = get_array_element_type(curr_tp);
-                       int      size      = get_type_size_bits(elem_type);
-                       int      align     = get_type_alignment_bits(elem_type);
+                       unsigned size      = get_type_size_bytes(elem_type);
+                       unsigned align     = get_type_alignment_bytes(elem_type);
                        int      idx;
 
                        assert(size > 0);
                        if(size % align > 0) {
                                size += align - (size % align);
                        }
-                       assert(size % 8 == 0);
-                       size /= 8;
                        idx = get_compound_graph_path_array_index(path, i);
                        assert(idx >= 0);
                        offset += size * idx;
@@ -1026,7 +1139,7 @@ int get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
 }  /* get_compound_ent_value_offset_bytes */
 
 /* Return the offset in bits from the last byte address. */
-int get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
+unsigned get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
        compound_graph_path *path;
        int path_len;
        ir_entity *last_node;
@@ -1217,18 +1330,6 @@ void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
        ent->attr.mtd_attr.vtable_number = vtable_number;
 }  /* set_entity_vtable_number */
 
-/* Returns the section of a method. */
-ir_img_section get_method_img_section(const ir_entity *ent) {
-       assert(is_method_entity((ir_entity *)ent));
-       return ent->attr.mtd_attr.section;
-}  /* get_method_img_section */
-
-/* Sets the section of a method. */
-void set_method_img_section(ir_entity *ent, ir_img_section section) {
-       assert(is_method_entity(ent));
-       ent->attr.mtd_attr.section = section;
-}  /* set_method_img_section */
-
 int
 (is_entity)(const void *thing) {
        return _is_entity(thing);
@@ -1371,5 +1472,6 @@ void firm_init_entity(void)
 
        current_ir_graph      = get_const_code_irg();
        sym.entity_p          = unknown_entity;
-       unknown_entity->value = new_SymConst(sym, symconst_addr_ent);
+       /* TODO: we need two unknown_entities here, one for code and one for data */
+       unknown_entity->value = new_SymConst(mode_P_data, sym, symconst_addr_ent);
 }  /* firm_init_entity */