changed semantics of Unknown: now has a mode.
[libfirm] / ir / tr / entity.c
index 2f263fc..8665d57 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"
@@ -75,27 +78,29 @@ new_entity (type *owner, ident *name, type *type)
   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_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;
@@ -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));
 }
 
 /*
@@ -284,10 +290,10 @@ 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);
+    X(allocation_automatic);
+    X(allocation_parameter);
+    X(allocation_dynamic);
+    X(allocation_static);
     default: return "BAD VALUE";
   }
 #undef X
@@ -301,9 +307,9 @@ 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));
+  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;
@@ -314,9 +320,9 @@ 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);
+    X(visibility_local);
+    X(visibility_external_visible);
+    X(visibility_external_allocated);
     default: return "BAD VALUE";
   }
 #undef X
@@ -328,17 +334,20 @@ get_entity_variability (entity *ent) {
 }
 
 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_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_paths);
@@ -351,10 +360,10 @@ const char *get_variability_name(ent_variability var)
 {
 #define X(a)   case a: return #a
   switch (var) {
-    X(uninitialized);
-    X(initialized);
-    X(part_constant);
-    X(constant);
+    X(variability_uninitialized);
+    X(variability_initialized);
+    X(variability_part_constant);
+    X(variability_constant);
     default: return "BAD VALUE";
   }
 #undef X
@@ -377,8 +386,8 @@ const char *get_volatility_name(ent_volatility var)
 {
 #define X(a)   case a: return #a
   switch (var) {
-    X(non_volatile);
-    X(is_volatile);
+    X(volatility_non_volatile);
+    X(volatility_is_volatile);
     default: return "BAD VALUE";
   }
 #undef X
@@ -403,9 +412,9 @@ const char *get_peculiarity_name(peculiarity var)
 {
 #define X(a)   case a: return #a
   switch (var) {
-    X(description);
-    X(inherited);
-    X(existent);
+    X(peculiarity_description);
+    X(peculiarity_inherited);
+    X(peculiarity_existent);
     default: return "BAD VALUE";
   }
 #undef X
@@ -413,19 +422,46 @@ const char *get_peculiarity_name(peculiarity var)
 
 /* 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)) && (ent->peculiarity==existent)) 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_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;
@@ -438,9 +474,14 @@ 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;
+    nn = new_Unknown(m); break;
   default:
     DDMN(n);
     assert(0 && "opdope invalid or not implemented");
@@ -467,6 +508,7 @@ new_compound_graph_path(type *tp, int length) {
 INLINE void
 free_compound_graph_path (compound_graph_path *gr) {
   assert(gr && is_compound_graph_path(gr));
+  gr->kind = k_BAD;
   free(gr);
 }
 
@@ -517,40 +559,40 @@ set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node) {
    the compound. */
 INLINE void
 add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path) {
-  assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
+  assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
   ARR_APP1 (ir_node *, ent->values, val);
   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));
+  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 != uninitialized));
+  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 != uninitialized));
+  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 != uninitialized));
+  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 != uninitialized));
+  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) {
@@ -567,8 +609,9 @@ remove_compound_ent_value(entity *ent, entity *value_ent) {
 
 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);
+  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);
 }
@@ -580,7 +623,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);
@@ -591,16 +634,15 @@ copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
 /* 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) {
   compound_graph_path *path;
-  assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
+  assert(ent && is_compound_entity(ent) && (ent->variability != variability_uninitialized));
   path = get_compound_ent_value_path(ent, pos);
-  assert(path->len == 1);
 
   return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
 }
@@ -608,7 +650,7 @@ get_compound_ent_value_member(entity *ent, int pos) {
 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));
+  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);
@@ -626,7 +668,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++) {
@@ -780,7 +822,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;
 }
 
@@ -829,6 +871,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;
 }