add missing \n at end
[libfirm] / ir / tr / entity.c
index f0b35d9..bd17601 100644 (file)
@@ -1,40 +1,40 @@
 /*
- * Project:     libFIRM
- * File name:   ir/tr/entity.c
- * Purpose:     Representation of all program known entities.
- * Author:      Martin Trapp, Christian Schaefer
- * Modified by: Goetz Lindenmaier, Michael Beck
- * Created:
- * CVS-ID:      $Id$
- * Copyright:   (c) 1998-2006 Universität Karlsruhe
- * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
+ * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ *
+ * This file is part of libFirm.
+ *
+ * This file may be distributed and/or modified under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation and appearing in the file LICENSE.GPL included in the
+ * packaging of this file.
+ *
+ * Licensees holding valid libFirm Professional Edition licenses may use
+ * this file in accordance with the libFirm Commercial License.
+ * Agreement provided with the Software.
+ *
+ * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+ * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
+ */
+
+/**
+ * @file
+ * @brief   Representation of all program known entities.
+ * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
+ * @version $Id$
  */
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 
-#ifdef HAVE_STRING_H
-# include <string.h>
-#endif
-#ifdef HAVE_STDLIB_H
-# include <stdlib.h>
-#endif
-#ifdef HAVE_STDDEF_H
-# include <stddef.h>
-#endif
-#ifdef HAVE_MALLOC_H
-# include <malloc.h>
-#endif
-#ifdef HAVE_ALLOCA_H
-# include <alloca.h>
-#endif
+#include <string.h>
+#include <stdlib.h>
+#include <stddef.h>
 
 #include "firm_common_t.h"
 
 #include "xmalloc.h"
 #include "entity_t.h"
-#include "mangle.h"
-#include "typegmod.h"
 #include "array.h"
 #include "irtools.h"
 #include "irhooks.h"
 
 #include "callgraph.h"  /* for dumping debug output */
 
-/*******************************************************************/
+/**
+ * An interval initializer.
+ */
+typedef struct interval_initializer interval_initializer;
+
+/**
+ * A value initializer.
+ */
+typedef struct value_initializer value_initializer;
+
+struct interval_initializer {
+       int                  first_index; /**< The first index of the initialized interval. */
+       int                  last_index;  /**< The last index of the initialized interval. */
+       interval_initializer *next;       /**< Points to the next interval initializer. */
+};
+
+struct value_initializer {
+       ir_entity *ent;           /**< The initialized entity. */
+       value_initializer *next;  /**< Points to the next value initializer. */
+};
+
+typedef union initializer {
+       ir_node              *value;     /**< The value of the initializer. */
+       ir_node              **values;   /**< The values of an interval. */
+       value_initializer    *val_init;  /**< Points the the head of the next value initializers. */
+       interval_initializer *int_init;  /**< Points to the head of the next value initializers. */
+} initializer;
+
+/*-----------------------------------------------------------------*/
 /** 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"
 
 /*-----------------------------------------------------------------*/
@@ -114,10 +143,13 @@ new_rd_entity(dbg_info *db, ir_type *owner, ident *name, ir_type *type)
        res->allocation           = allocation_automatic;
        res->visibility           = visibility_local;
        res->volatility           = volatility_non_volatile;
+       res->align                = align_is_aligned;
        res->stickyness           = stickyness_unsticky;
        res->peculiarity          = peculiarity_existent;
+       res->address_taken        = ir_address_taken_unknown;
        res->final                = 0;
        res->compiler_gen         = 0;
+       res->backend_marked       = 0;
        res->offset               = -1;
        res->offset_bit_remainder = 0;
        res->link                 = NULL;
@@ -200,7 +232,7 @@ static void free_entity_attrs(ir_entity *ent) {
        }
        if (is_compound_entity(ent)) {
                if (ent->attr.cmpd_attr.val_paths) {
-                       for (i = 0; i < get_compound_ent_n_values(ent); i++)
+                       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. */
@@ -364,7 +396,6 @@ const char *get_allocation_name(ir_allocation all)
 #undef X
 }  /* get_allocation_name */
 
-
 ir_visibility
 (get_entity_visibility)(const ir_entity *ent) {
        return _get_entity_visibility(ent);
@@ -451,7 +482,7 @@ void
        _set_entity_volatility(ent, vol);
 }  /* set_entity_volatility */
 
-/* return the name of the volatility */
+/* Return the name of the volatility. */
 const char *get_volatility_name(ir_volatility var)
 {
 #define X(a)    case a: return #a
@@ -463,6 +494,28 @@ const char *get_volatility_name(ir_volatility var)
 #undef X
 }  /* get_volatility_name */
 
+ir_align
+(get_entity_align)(const ir_entity *ent) {
+       return _get_entity_align(ent);
+}  /* get_entity_align */
+
+void
+(set_entity_align)(ir_entity *ent, ir_align a) {
+       _set_entity_align(ent, a);
+}  /* set_entity_align */
+
+/* Return the name of the alignment. */
+const char *get_align_name(ir_align a)
+{
+#define X(a)    case a: return #a
+       switch (a) {
+       X(align_non_aligned);
+       X(align_is_aligned);
+       default: return "BAD VALUE";
+       }
+#undef X
+}  /* get_align_name */
+
 ir_peculiarity
 (get_entity_peculiarity)(const ir_entity *ent) {
        return _get_entity_peculiarity(ent);
@@ -474,27 +527,57 @@ void
 }  /* set_entity_peculiarity */
 
 /* Checks if an entity cannot be overridden anymore. */
-int (get_entity_final)(const ir_entity *ent) {
-       return _get_entity_final(ent);
-}  /* get_entity_final */
+int (is_entity_final)(const ir_entity *ent) {
+       return _is_entity_final(ent);
+}  /* is_entity_final */
 
 /* Sets/resets the final flag of an entity. */
 void (set_entity_final)(ir_entity *ent, int final) {
-  _set_entity_final(ent, final);
-}
+       _set_entity_final(ent, final);
+}  /* set_entity_final */
 
 /* Checks if an entity is compiler generated */
-int is_entity_compiler_generated(const ir_entity *ent) {
-       assert(is_entity(ent));
-       return ent->compiler_gen;
+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) {
-       assert(is_entity(ent));
-       ent->compiler_gen = flag ? 1 : 0;
+void (set_entity_compiler_generated)(ir_entity *ent, int flag) {
+       _set_entity_compiler_generated(ent, flag);
 }  /* set_entity_compiler_generated */
 
+/* Checks if an entity is marked by the backend */
+int (is_entity_backend_marked)(const ir_entity *ent) {
+       return _is_entity_backend_marked(ent);
+}  /* is_entity_backend_marked */
+
+/* Sets/resets the compiler generated flag */
+void (set_entity_backend_marked)(ir_entity *ent, int flag) {
+       _set_entity_backend_marked(ent, flag);
+}  /* set_entity_backend_marked */
+
+/* Checks if the address of an entity was taken. */
+ir_address_taken_state (get_entity_address_taken)(const ir_entity *ent) {
+       return _get_entity_address_taken(ent);
+}  /* is_entity_address_taken */
+
+/* Sets/resets the address taken flag. */
+void (set_entity_address_taken)(ir_entity *ent, ir_address_taken_state flag) {
+       _set_entity_address_taken(ent, flag);
+}  /* set_entity_address_taken */
+
+/* Return the name of the address_taken state. */
+const char *get_address_taken_state_name(ir_address_taken_state state) {
+#define X(a)    case a: return #a
+       switch (state) {
+       X(ir_address_not_taken);
+       X(ir_address_taken_unknown);
+       X(ir_address_taken);
+    default: return "BAD VALUE";
+       }
+#undef X
+}  /* get_address_taken_state_name */
+
 /* Get the entity's stickyness */
 ir_stickyness
 (get_entity_stickyness)(const ir_entity *ent) {
@@ -595,7 +678,6 @@ ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
        case iro_Unknown:
                nn = new_d_Unknown(m); break;
        default:
-               DDMN(n);
                assert(0 && "opcode invalid or not implemented");
                nn = NULL;
                break;
@@ -773,7 +855,8 @@ static int equal_paths(compound_graph_path *path1, int *visited_indices, compoun
  * Returns the position of a value with the given path.
  * The path must contain array indices for all array element entities.
  *
- * @todo  This implementation is very low and should be replaced when the new tree oriented
+ * @todo  This implementation is very slow (O(number of initializers^2) and should
+ *        be replaced when the new tree oriented
  *        value representation is finally implemented.
  */
 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
@@ -816,17 +899,19 @@ ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *pat
 
 void
 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
-       int i;
+       int i, n;
        assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
-       for (i = 0; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths)); ++i) {
+
+       n = ARR_LEN(ent->attr.cmpd_attr.val_paths);
+       for (i = 0; i < n; ++i) {
                compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
                if (path->list[path->len-1].node == value_ent) {
-                       for (; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths))-1; ++i) {
+                       for (; i < n - 1; ++i) {
                                ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
                                ent->attr.cmpd_attr.values[i]    = ent->attr.cmpd_attr.values[i+1];
                        }
-                       ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, ARR_LEN(ent->attr.cmpd_attr.val_paths) - 1);
-                       ARR_SETLEN(ir_node*,   ent->attr.cmpd_attr.values,    ARR_LEN(ent->attr.cmpd_attr.values)    - 1);
+                       ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, n - 1);
+                       ARR_SETLEN(ir_node*,   ent->attr.cmpd_attr.values,    n - 1);
                        break;
                }
        }
@@ -904,29 +989,36 @@ int get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
        compound_graph_path *path;
        int path_len, i;
        int offset = 0;
+       ir_type *curr_tp;
 
        assert(get_type_state(get_entity_type(ent)) == layout_fixed);
 
        path = get_compound_ent_value_path(ent, pos);
        path_len = get_compound_graph_path_length(path);
+       curr_tp = path->tp;
 
        for (i = 0; i < path_len; ++i) {
                ir_entity *node = get_compound_graph_path_node(path, i);
                ir_type *node_tp = get_entity_type(node);
-               ir_type *owner_tp = get_entity_owner(node);
 
-               if (is_Array_type(owner_tp)) {
+               if (is_Array_type(curr_tp)) {
                        int size  = get_type_size_bits(node_tp);
                        int align = get_type_alignment_bits(node_tp);
+                       int idx;
+
+                       assert(size > 0);
                        if(size % align > 0) {
                                size += align - (size % align);
                        }
                        assert(size % 8 == 0);
                        size /= 8;
-                       offset += size * get_compound_graph_path_array_index(path, i);
+                       idx = get_compound_graph_path_array_index(path, i);
+                       assert(idx >= 0);
+                       offset += size * idx;
                } else {
                        offset += get_entity_offset(node);
                }
+               curr_tp = node_tp;
        }
 
        return offset;
@@ -947,127 +1039,6 @@ int get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
        return get_entity_offset_bits_remainder(last_node);
 }  /* get_compound_ent_value_offset_bit_remainder */
 
-typedef struct {
-       int n_elems;      /**< number of elements the array can hold */
-       int current_elem; /**< current array index */
-       ir_entity *ent;
-} array_info;
-
-/* Compute the array indices in compound graph paths of initialized entities.
- *
- *  All arrays must have fixed lower and upper bounds.  One array can
- *  have an open bound.  If there are several open bounds, we do
- *  nothing.  There must be initializer elements for all array
- *  elements.  Uses the link field in the array element entities.  The
- *  array bounds must be representable as ints.
- *
- * WARNING: it is impossible to get this 100% right with the current
- *          design... (in array of structs you cant know when a struct is
- *          really finished and the next array element starts)
- *
- *  (If the bounds are not representable as ints we have to represent
- *  the indices as firm nodes.  But still we must be able to
- *  evaluate the index against the upper bound.)
- */
-int compute_compound_ent_array_indices(ir_entity *ent) {
-       ir_type *tp = get_entity_type(ent);
-       int i, n_vals;
-       int max_len = 0;
-       array_info *array_infos;
-
-       assert(is_compound_type(tp));
-
-       if (!is_compound_type(tp) ||
-               (ent->variability == variability_uninitialized))
-               return 1;
-
-       n_vals = get_compound_ent_n_values(ent);
-       for(i = 0; i < n_vals; ++i) {
-               compound_graph_path *path = get_compound_ent_value_path(ent, i);
-               int len = get_compound_graph_path_length(path);
-               if(len > max_len)
-                       max_len = len;
-       }
-
-       array_infos = alloca(max_len * sizeof(array_infos[0]));
-       memset(array_infos, 0, max_len * sizeof(array_infos[0]));
-
-       for(i = 0; i < n_vals; ++i) {
-               compound_graph_path *path = get_compound_ent_value_path(ent, i);
-               int path_len = get_compound_graph_path_length(path);
-               int j;
-               int needadd = 0;
-               ir_entity *prev_node = NULL;
-
-               for(j = path_len-1; j >= 0; --j) {
-                       int dim, dims;
-                       int n_elems;
-                       ir_entity *node = get_compound_graph_path_node(path, j);
-                       const ir_type *node_type = get_entity_type(node);
-                       array_info *info = &array_infos[j];
-
-                       if(is_atomic_entity(node)) {
-                               needadd = 1;
-                               set_compound_graph_path_array_index(path, j, -1);
-                               prev_node = node;
-                               continue;
-                       } else if(is_compound_type(node_type) && !is_Array_type(node_type)) {
-                               int n_members = get_compound_n_members(node_type);
-                               ir_entity *last = get_compound_member(node_type, n_members - 1);
-                               if(needadd && last == prev_node) {
-                                       needadd = 1;
-                               } else {
-                                       needadd = 0;
-                               }
-                               set_compound_graph_path_array_index(path, j, -1);
-                               prev_node = node;
-                               continue;
-                       }
-
-                       if(info->ent != node) {
-                               n_elems = 1;
-                               dims = get_array_n_dimensions(node_type);
-                               for(dim = 0; dim < dims; ++dim) {
-                                       long lower_bound = 0;
-                                       long upper_bound = -1;
-
-                                       if(has_array_lower_bound(node_type, 0)) {
-                                               lower_bound = get_array_lower_bound_int(node_type, 0);
-                                       }
-                                       if(has_array_upper_bound(node_type, 0)) {
-                                               upper_bound = get_array_upper_bound_int(node_type, 0);
-                                               assert(upper_bound >= lower_bound);
-                                               n_elems *= (upper_bound - lower_bound);
-                                       } else {
-                                               assert(dim == dims-1);
-                                               n_elems = -1;
-                                       }
-                               }
-
-                               info->ent = node;
-                               info->n_elems = n_elems;
-                               info->current_elem = 0;
-                       }
-
-                       set_compound_graph_path_array_index(path, j, info->current_elem);
-
-                       if(needadd) {
-                               info->current_elem++;
-                               if(info->current_elem >= info->n_elems) {
-                                       needadd = 1;
-                                       info->current_elem = 0;
-                               } else {
-                                       needadd = 0;
-                               }
-                       }
-
-                       prev_node = node;
-               }
-       }
-
-       return 1;
-}  /* compute_compound_ent_array_indices */
-
 int
 (get_entity_offset)(const ir_entity *ent) {
        return _get_entity_offset(ent);
@@ -1109,11 +1080,13 @@ get_entity_n_overwrites(ir_entity *ent) {
 
 int
 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
-       int i;
+       int i, n;
        assert(is_Class_type(get_entity_owner(ent)));
-       for (i = 0; i < get_entity_n_overwrites(ent); i++)
+       n = get_entity_n_overwrites(ent);
+       for (i = 0; i < n; ++i) {
                if (get_entity_overwrites(ent, i) == overwritten)
                        return i;
+       }
        return -1;
 }  /* get_entity_overwrites_index */
 
@@ -1133,15 +1106,17 @@ set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
 
 void
 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
-       int i;
+       int i, n;
        assert(is_Class_type(get_entity_owner(ent)));
-       for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
+       n = ARR_LEN(ent->overwrites);
+       for (i = 0; i < n; ++i) {
                if (ent->overwrites[i] == overwritten) {
-                       for(; i < (ARR_LEN (ent->overwrites))-1; i++)
+                       for (; i < n - 1; i++)
                                ent->overwrites[i] = ent->overwrites[i+1];
-                       ARR_SETLEN(ir_entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
+                       ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
                        break;
                }
+       }
 }  /* remove_entity_overwrites */
 
 void
@@ -1152,16 +1127,18 @@ add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
 int
 get_entity_n_overwrittenby(ir_entity *ent) {
        assert(is_Class_type(get_entity_owner(ent)));
-       return (ARR_LEN (ent->overwrittenby));
+       return ARR_LEN(ent->overwrittenby);
 }  /* get_entity_n_overwrittenby */
 
 int
 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
-       int i;
+       int i, n;
        assert(is_Class_type(get_entity_owner(ent)));
-       for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
+       n = get_entity_n_overwrittenby(ent);
+       for (i = 0; i < n; ++i) {
                if (get_entity_overwrittenby(ent, i) == overwrites)
                        return i;
+       }
        return -1;
 }  /* get_entity_overwrittenby_index */
 
@@ -1179,16 +1156,19 @@ set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
        ent->overwrittenby[pos] = overwrites;
 }  /* set_entity_overwrittenby */
 
-void    remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
-       int i;
+void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
+       int i, n;
        assert(is_Class_type(get_entity_owner(ent)));
-       for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
+
+       n = ARR_LEN(ent->overwrittenby);
+       for (i = 0; i < n; ++i) {
                if (ent->overwrittenby[i] == overwrites) {
-                       for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
+                       for(; i < n - 1; ++i)
                                ent->overwrittenby[i] = ent->overwrittenby[i+1];
-                       ARR_SETLEN(ir_entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
+                       ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
                        break;
                }
+       }
 }  /* remove_entity_overwrittenby */
 
 /* A link to store intermediate information */
@@ -1272,6 +1252,8 @@ int is_method_entity(ir_entity *ent) {
 /**
  * @todo not implemented!!! */
 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
+       (void) ent1;
+       (void) ent2;
        fprintf(stderr, " calling unimplemented equal entity!!! \n");
        return 1;
 }  /* equal_entity */
@@ -1363,6 +1345,14 @@ ir_type *(get_entity_repr_class)(const ir_entity *ent) {
        return _get_entity_repr_class(ent);
 }  /* get_entity_repr_class */
 
+dbg_info *(get_entity_dbg_info)(const ir_entity *ent) {
+       return _get_entity_dbg_info(ent);
+}  /* get_entity_dbg_info */
+
+void (set_entity_dbg_info)(ir_entity *ent, dbg_info *db) {
+       _set_entity_dbg_info(ent, db);
+}  /* set_entity_dbg_info */
+
 /* Initialize entity module. */
 void firm_init_entity(void)
 {