some fixes for xml dumper / still buggy.
[libfirm] / ir / tr / entity.c
index 8bc7955..8391547 100644 (file)
@@ -69,7 +69,7 @@ new_entity (type *owner, ident *name, type *type)
 
   assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
 
-  res = (entity *) malloc (sizeof (entity));
+  res = (entity *) xmalloc (sizeof (entity));
   res->kind = k_entity;
   assert_legal_owner_of_ent(owner);
   res->owner = owner;
@@ -85,7 +85,7 @@ new_entity (type *owner, ident *name, type *type)
     res->variability = constant;
     rem = current_ir_graph;
     current_ir_graph = get_const_code_irg();
-    res->value = new_Const(mode_P, new_tarval_from_entity(res, mode_P));
+    res->value = new_Const(mode_P_mach, new_tarval_from_entity(res, mode_P_mach));
     current_ir_graph = rem;
   } else {
     res->variability = uninitialized;
@@ -130,7 +130,7 @@ copy_entity_own (entity *old, type *new_owner) {
 
   assert_legal_owner_of_ent(new_owner);
   if (old->owner == new_owner) return old;
-  new = (entity *) malloc (sizeof (entity));
+  new = (entity *) xmalloc (sizeof (entity));
   memcpy (new, old, sizeof (entity));
   new->owner = new_owner;
   /*
@@ -162,7 +162,7 @@ copy_entity_name (entity *old, ident *new_name) {
   entity *new;
 
   if (old->name == new_name) return old;
-  new = (entity *) malloc (sizeof (entity));
+  new = (entity *) xmalloc (sizeof (entity));
   memcpy (new, old, sizeof (entity));
   new->name = new_name;
   new->ld_name = NULL;
@@ -279,6 +279,20 @@ set_entity_allocation (entity *ent, ent_allocation al) {
   ent->allocation = al;
 }
 
+/* return the name of the visibility */
+const char *get_allocation_name(ent_allocation all)
+{
+#define X(a)   case a: return #a
+  switch (all) {
+    X(automatic_allocated);
+    X(parameter_allocated);
+    X(dynamic_allocated);
+    X(static_allocated);
+    default: return "BAD VALUE";
+  }
+#undef X
+}
+
 
 INLINE ent_visibility
 get_entity_visibility (entity *ent) {
@@ -295,6 +309,19 @@ set_entity_visibility (entity *ent, ent_visibility vis) {
   ent->visibility = vis;
 }
 
+/* return the name of the visibility */
+const char *get_visibility_name(ent_visibility vis)
+{
+#define X(a)   case a: return #a
+  switch (vis) {
+    X(local);
+    X(external_visible);
+    X(external_allocated);
+    default: return "BAD VALUE";
+  }
+#undef X
+}
+
 INLINE ent_variability
 get_entity_variability (entity *ent) {
   return ent->variability;
@@ -308,13 +335,13 @@ set_entity_variability (entity *ent, ent_variability var){
       (ent->variability == uninitialized) && (var != uninitialized)) {
     /* Allocate datastructures for constant values */
     ent->values = NEW_ARR_F(ir_node *, 1);
-    ent->val_ents = NEW_ARR_F(entity *, 1);
+    ent->val_paths = NEW_ARR_F(compound_graph_path *, 1);
   }
   if ((is_compound_type(ent->type)) &&
       (var == uninitialized) && (ent->variability != uninitialized)) {
     /* Free datastructures for constant values */
     DEL_ARR_F(ent->values);
-    DEL_ARR_F(ent->val_ents);
+    DEL_ARR_F(ent->val_paths);
   }
   ent->variability = var;
 }
@@ -384,7 +411,7 @@ const char *get_peculiarity_name(peculiarity var)
 #undef X
 }
 
-/* Set has no effect for entities of type method. */
+/* Set has no effect for existent entities of type method. */
 INLINE ir_node *
 get_atomic_ent_value(entity *ent) {
   assert(ent); assert(is_atomic_entity(ent));
@@ -395,7 +422,7 @@ get_atomic_ent_value(entity *ent) {
 INLINE void
 set_atomic_ent_value(entity *ent, ir_node *val) {
   assert(ent && is_atomic_entity(ent) && (ent->variability != uninitialized));
-  if (is_method_type(ent->type)) return;
+  if ((is_method_type(ent->type)) && (ent->peculiarity==existent)) return;
   ent->value = val;
 }
 
@@ -420,13 +447,127 @@ ir_node *copy_const_value(ir_node *n) {
   return nn;
 }
 
-/* A value of a compound entity is a pair of value and the corresponding member of
+compound_graph_path *
+new_compound_graph_path(type *tp, int length) {
+  compound_graph_path *res;
+  assert(is_type(tp) && is_compound_type(tp));
+  assert(length > 0);
+
+  res = (compound_graph_path *) malloc (sizeof(compound_graph_path) + (length-1) * sizeof(entity *));
+  res->kind = k_ir_compound_graph_path;
+  res->tp = tp;
+  res->len = length;
+  memset(res->nodes, 0, sizeof(entity *) * length);
+  return res;
+}
+
+INLINE void
+free_compound_graph_path (compound_graph_path *gr) {
+  assert(gr && is_compound_graph_path(gr));
+  free(gr);
+}
+
+INLINE int
+is_compound_graph_path(void *thing) {
+  return (get_kind(thing) == k_ir_compound_graph_path);
+}
+
+/* checks whether nodes 0..pos are correct (all lie on a path.) */
+/* @@@ not implemented */
+INLINE int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
+  int i;
+  entity *node;
+  type *owner = gr->tp;
+  for (i = 0; i <= pos; i++) {
+    node = get_compound_graph_path_node(gr, i);
+    if (get_entity_owner(node) != owner) return false;
+    owner = get_entity_type(node);
+  }
+  if (pos == get_compound_graph_path_length(gr) -1)
+    if (!is_atomic_type(owner)) return false;
+  return true;
+}
+
+INLINE int
+get_compound_graph_path_length(compound_graph_path *gr) {
+  assert(gr && is_compound_graph_path(gr));
+  return gr->len;
+}
+
+INLINE entity *
+get_compound_graph_path_node(compound_graph_path *gr, int pos) {
+  assert(gr && is_compound_graph_path(gr));
+  assert(pos >= 0 && pos < gr->len);
+  return gr->nodes[pos];
+}
+
+INLINE void
+set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
+  assert(gr && is_compound_graph_path(gr));
+  assert(pos >= 0 && pos < gr->len);
+  assert(is_entity(node));
+  gr->nodes[pos] = node;
+  assert(is_proper_compound_graph_path(gr, pos));
+}
+
+/* A value of a compound entity is a pair of value and the corresponding path to a member of
    the compound. */
 INLINE void
-add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
+add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
   ARR_APP1 (ir_node *, ent->values, val);
-  ARR_APP1 (entity *, ent->val_ents, member);
+  ARR_APP1 (compound_graph_path *, ent->val_paths, path);
+}
+
+INLINE void
+set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos) {
+  assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
+  ent->values[pos+1] = val;
+  ent->val_paths[pos+1] = path;
+}
+
+INLINE int
+get_compound_ent_n_values(entity *ent) {
+  assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
+  return (ARR_LEN (ent->values))-1;
+}
+
+INLINE ir_node  *
+get_compound_ent_value(entity *ent, int pos) {
+  assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
+  return ent->values[pos+1];
+}
+
+INLINE compound_graph_path *
+get_compound_ent_value_path(entity *ent, int pos) {
+  assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
+  return ent->val_paths[pos+1];
+}
+
+void
+remove_compound_ent_value(entity *ent, entity *value_ent) {
+  int i;
+  assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
+  for (i = 1; i < (ARR_LEN (ent->val_paths)); i++) {
+    compound_graph_path *path = ent->val_paths[i];
+    if (path->nodes[path->len] == value_ent) {
+      for(; i < (ARR_LEN (ent->val_paths))-1; i++) {
+       ent->val_paths[i] = ent->val_paths[i+1];
+       ent->values[i]   = ent->values[i+1];
+      }
+      ARR_SETLEN(entity*,  ent->val_paths, ARR_LEN(ent->val_paths) - 1);
+      ARR_SETLEN(ir_node*, ent->values,   ARR_LEN(ent->values) - 1);
+      break;
+    }
+  }
+}
+
+INLINE void
+add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
+  assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
+  compound_graph_path *path = new_compound_graph_path(get_entity_owner(ent), 1);
+  path->nodes[0] = member;
+  add_compound_ent_value_w_path(ent, val, path);
 }
 
 /* Copies the firm subgraph referenced by val to const_code_irg and adds
@@ -444,18 +585,6 @@ copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
   current_ir_graph = rem;
   }*/
 
-INLINE int
-get_compound_ent_n_values(entity *ent) {
-  assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
-  return (ARR_LEN (ent->values))-1;
-}
-
-INLINE ir_node  *
-get_compound_ent_value(entity *ent, int pos) {
-  assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
-  return ent->values[pos+1];
-}
-
 /* Copies the value i of the entity to current_block in current_ir_graph.
 ir_node *
 copy_compound_ent_value(entity *ent, int pos) {
@@ -465,32 +594,21 @@ copy_compound_ent_value(entity *ent, int pos) {
 
 INLINE entity   *
 get_compound_ent_value_member(entity *ent, int pos) {
+  compound_graph_path *path;
   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
-  return ent->val_ents[pos+1];
+  path = get_compound_ent_value_path(ent, pos);
+  assert(path->len == 1);
+
+  return get_compound_graph_path_node(path, 0);
 }
 
 INLINE void
 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
+  compound_graph_path *path;
   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
-  ent->values[pos+1] = val;
-  ent->val_ents[pos+1] = member;
-}
-
-void
-remove_compound_ent_value(entity *ent, entity *value_ent) {
-  int i;
-  assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
-  for (i = 1; i < (ARR_LEN (ent->val_ents)); i++) {
-    if (ent->val_ents[i] == value_ent) {
-      for(; i < (ARR_LEN (ent->val_ents))-1; i++) {
-       ent->val_ents[i] = ent->val_ents[i+1];
-       ent->values[i]   = ent->values[i+1];
-      }
-      ARR_SETLEN(entity*,  ent->val_ents, ARR_LEN(ent->val_ents) - 1);
-      ARR_SETLEN(ir_node*, ent->values,   ARR_LEN(ent->values) - 1);
-      break;
-    }
-  }
+  path = new_compound_graph_path(get_entity_owner(ent), 1);
+  set_compound_graph_path_node(path, 0, member);
+  set_compound_ent_value_w_path(ent, val, path, pos);
 }
 
 void