added assertions to get_method_param_type() and get_method_res_type()
[libfirm] / ir / tr / entity.c
index 6b6fc7a..f1923f3 100644 (file)
@@ -1,11 +1,14 @@
-/* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
-* All rights reserved.
-*
-* Authors: Martin Trapp, Christian Schaefer
-*
-*/
-
-/* $Id$ */
+/*
+ * Project:     libFIRM
+ * File name:   ir/tr/entity.c
+ * Purpose:     Representation of all program known entities.
+ * Author:      Martin Trapp, Christian Schaefer
+ * Modified by: Goetz Lindenmaier
+ * Created:
+ * CVS-ID:      $Id$
+ * Copyright:   (c) 1998-2003 Universität Karlsruhe
+ * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
+ */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
@@ -17,7 +20,7 @@
 
 # include "entity_t.h"
 # include "mangle.h"
-# include "typegmod_t.h"
+# include "typegmod.h"
 # include "array.h"
 /* All this is needed to build the constant node for methods: */
 # include "irprog_t.h"
@@ -69,33 +72,35 @@ 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;
   res->name = name;
   res->type = type;
+
   if (get_type_tpop(type) == type_method)
-    res->allocation = static_allocated;
+    res->allocation = allocation_static;
   else
-    res->allocation = automatic_allocated;
-  res->visibility = local;
+    res->allocation = allocation_automatic;
+
+  res->visibility = visibility_local;
   res->offset = -1;
   if (is_method_type(type)) {
-    res->variability = constant;
+    res->variability = 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;
-    res->value = NULL;
+    res->variability = variability_uninitialized;
+    res->value  = NULL;
     res->values = NULL;
   }
-  res->peculiarity existent;
-  res->volatility non_volatile;
-  res->ld_name = NULL;
-  res->overwrites = NEW_ARR_F(entity *, 1);
+  res->peculiarity   = peculiarity_existent;
+  res->volatility    = volatility_non_volatile;
+  res->ld_name       = NULL;
+  res->overwrites    = NEW_ARR_F(entity *, 1);
   res->overwrittenby = NEW_ARR_F(entity *, 1);
 
   res->irg = NULL;
@@ -130,7 +135,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 +167,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;
@@ -184,6 +189,7 @@ void
 free_entity (entity *ent) {
   free_tarval_entity(ent);
   free_entity_attrs(ent);
+  ent->kind = k_BAD;
   free(ent);
 }
 
@@ -201,7 +207,7 @@ get_entity_nr(entity *ent) {
 INLINE const char *
 get_entity_name (entity *ent) {
   assert (ent);
-  return id_to_str(get_entity_ident(ent));
+  return get_id_str(get_entity_ident(ent));
 }
 
 ident *
@@ -250,7 +256,7 @@ set_entity_ld_ident (entity *ent, ident *ld_ident) {
 
 INLINE const char *
 get_entity_ld_name (entity *ent) {
-  return id_to_str(get_entity_ld_ident(ent));
+  return get_id_str(get_entity_ld_ident(ent));
 }
 
 /*
@@ -279,6 +285,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(allocation_automatic);
+    X(allocation_parameter);
+    X(allocation_dynamic);
+    X(allocation_static);
+    default: return "BAD VALUE";
+  }
+#undef X
+}
+
 
 INLINE ent_visibility
 get_entity_visibility (entity *ent) {
@@ -287,38 +307,67 @@ get_entity_visibility (entity *ent) {
 
 INLINE void
 set_entity_visibility (entity *ent, ent_visibility vis) {
-  if (vis != local)
-    assert((ent->allocation == static_allocated) ||
-          (ent->allocation == automatic_allocated));
-  // @@@ Test that the owner type is not local, but how??
-  //       && get_class_visibility(get_entity_owner(ent)) != local));
+  if (vis != visibility_local)
+    assert((ent->allocation == allocation_static) ||
+          (ent->allocation == allocation_automatic));
+  /* @@@ Test that the owner type is not local, but how??
+         && get_class_visibility(get_entity_owner(ent)) != local));*/
   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(visibility_local);
+    X(visibility_external_visible);
+    X(visibility_external_allocated);
+    default: return "BAD VALUE";
+  }
+#undef X
+}
+
 INLINE ent_variability
 get_entity_variability (entity *ent) {
   return ent->variability;
 }
 
 INLINE void
-set_entity_variability (entity *ent, ent_variability var){
-  if (var == part_constant)
+set_entity_variability (entity *ent, ent_variability var)
+{
+  if (var == variability_part_constant)
     assert(is_class_type(ent->type) || is_struct_type(ent->type));
+
   if ((is_compound_type(ent->type)) &&
-      (ent->variability == uninitialized) && (var != uninitialized)) {
+      (ent->variability == variability_uninitialized) && (var != variability_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)) {
+      (var == variability_uninitialized) && (ent->variability != 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;
 }
 
+/* return the name of the variablity */
+const char *get_variability_name(ent_variability var)
+{
+#define X(a)   case a: return #a
+  switch (var) {
+    X(variability_uninitialized);
+    X(variability_initialized);
+    X(variability_part_constant);
+    X(variability_constant);
+    default: return "BAD VALUE";
+  }
+#undef X
+}
 
 INLINE ent_volatility
 get_entity_volatility (entity *ent) {
@@ -332,6 +381,18 @@ set_entity_volatility (entity *ent, ent_volatility vol) {
   ent->volatility = vol;
 }
 
+/* return the name of the volatility */
+const char *get_volatility_name(ent_volatility var)
+{
+#define X(a)   case a: return #a
+  switch (var) {
+    X(volatility_non_volatile);
+    X(volatility_is_volatile);
+    default: return "BAD VALUE";
+  }
+#undef X
+}
+
 INLINE peculiarity
 get_entity_peculiarity (entity *ent) {
   assert (ent);
@@ -346,21 +407,62 @@ set_entity_peculiarity (entity *ent, peculiarity pec) {
   ent->peculiarity = pec;
 }
 
-/* Set has no effect for entities of type method. */
+/* return the name of the peculiarity */
+const char *get_peculiarity_name(peculiarity var)
+{
+#define X(a)   case a: return #a
+  switch (var) {
+    X(peculiarity_description);
+    X(peculiarity_inherited);
+    X(peculiarity_existent);
+    default: return "BAD VALUE";
+  }
+#undef X
+}
+
+/* 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));
-  assert((ent->variability != uninitialized));
+get_atomic_ent_value(entity *ent)
+{
+  assert(ent);
+  assert(is_atomic_entity(ent));
+  assert(ent->variability != variability_uninitialized);
   return ent->value;
 }
 
 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;
+  assert(ent && is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
+  if (is_method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
+    return;
   ent->value = val;
 }
 
+/* Returns true if the the node is representable as code on
+ *  const_code_irg. */
+int is_irn_const_expression(ir_node *n) {
+  ir_node *pred;
+  ir_mode *m;
+
+  m = get_irn_mode(n);
+  switch(get_irn_opcode(n)) {
+  case iro_Const:
+  case iro_SymConst:
+  case iro_Unknown:
+    return true; break;
+  case iro_Add:
+    if (is_irn_const_expression(get_Add_left(n)))
+      return is_irn_const_expression(get_Add_right(n));
+  case iro_Conv:
+  case iro_Cast:
+    return is_irn_const_expression(get_irn_n(n, 0));
+  default:
+    return false;
+    break;
+  }
+  return false;
+}
+
 
 ir_node *copy_const_value(ir_node *n) {
   ir_node *nn;
@@ -373,20 +475,146 @@ ir_node *copy_const_value(ir_node *n) {
   case iro_SymConst:
     nn = new_SymConst(get_SymConst_type_or_id(n), get_SymConst_kind(n)); break;
   case iro_Add:
-    nn = new_Add(copy_const_value(get_Add_left(n)), copy_const_value(get_Add_right(n)), m); break;
+    nn = new_Add(copy_const_value(get_Add_left(n)),
+                copy_const_value(get_Add_right(n)), m); break;
+  case iro_Cast:
+    nn = new_Cast(copy_const_value(get_Cast_op(n)), get_Cast_type(n)); break;
+  case iro_Conv:
+    nn = new_Conv(copy_const_value(get_Conv_op(n)), m); break;
+  case iro_Unknown:
+    nn = new_Unknown(); break;
   default:
-    assert(0 && "opdope invalid or not implemented"); break;
+    DDMN(n);
+    assert(0 && "opdope invalid or not implemented");
+    nn = NULL;
+    break;
   }
   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));
+  gr->kind = k_BAD;
+  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) {
-  assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
+add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
+  assert(ent && is_compound_entity(ent) && (ent->variability != 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 != 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 != 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 != 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 != 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 != 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-1] == 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) {
+  compound_graph_path *path;
+  assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
+  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
@@ -396,7 +624,7 @@ INLINE void
 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
   ir_graph *rem = current_ir_graph;
 
-  assert(get_entity_variability(ent) != uninitialized);
+  assert(get_entity_variability(ent) != variability_uninitialized);
   current_ir_graph = get_const_code_irg();
 
   val = copy_const_value(val);
@@ -404,53 +632,29 @@ 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) {
-  assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
+  assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
   return copy_const_value(ent->values[pos+1]);
   }*/
 
 INLINE entity   *
 get_compound_ent_value_member(entity *ent, int pos) {
-  assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
-  return ent->val_ents[pos+1];
+  compound_graph_path *path;
+  assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
+  path = get_compound_ent_value_path(ent, pos);
+
+  return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
 }
 
 INLINE void
 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
-  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;
-    }
-  }
+  compound_graph_path *path;
+  assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
+  path = get_compound_ent_value_path(ent, pos);
+  set_compound_graph_path_node(path, 0, member);
+  set_compound_ent_value_w_path(ent, val, path, pos);
 }
 
 void
@@ -465,7 +669,7 @@ set_array_entity_values(entity *ent, tarval **values, int num_vals) {
   /* One bound is sufficient, the nunmber of constant fields makes the
      size. */
   assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
-  assert(get_entity_variability(ent) != uninitialized);
+  assert(get_entity_variability(ent) != variability_uninitialized);
   current_ir_graph = get_const_code_irg();
 
   for (i = 0; i < num_vals; i++) {
@@ -619,7 +823,7 @@ set_entity_irg(entity *ent, ir_graph *irg) {
    * aber erhalten bleiben soll. */
   /* assert (irg); */
   assert (is_method_type(ent->type));
-  assert (ent->peculiarity == existent);
+  assert (ent->peculiarity == peculiarity_existent);
   ent->irg = irg;
 }
 
@@ -668,6 +872,7 @@ void        mark_entity_visited(entity *ent) {
 INLINE bool entity_visited(entity *ent) {
   return get_entity_visited(ent) >= type_visited;
 }
+
 INLINE bool entity_not_visited(entity *ent) {
   return get_entity_visited(ent) < type_visited;
 }