added a generic function pointer to an opcode
[libfirm] / ir / tr / entity.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/tr/entity.h
4  * Purpose:     Representation of all program known entities.
5  * Author:      Martin Trapp, Christian Schaefer
6  * Modified by: Goetz Lindenmaier
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14 *  @file entity.h
15 *
16 *  Entities represent all program known objects.
17 *
18 *  @author Martin Trapp, Christian Schaefer
19 *  @author Goetz Lindenmaier
20 *
21 *  An entity is the representation of program known objects in Firm.
22 *  The primary concept of entities is to represent members of complex
23 *  types, i.e., fields and methods of classes.  As not all programming
24 *  language model all variables and methods as members of some class,
25 *  the concept of entities is extended to cover also local and global
26 *  variables, and arbitrary procedures.
27 *
28 *  An entity always specifies the type of the object it represents and
29 *  the type of the object it is a part of, the owner of the entity.
30 *  Originally this is the type of the class of which the entity is a
31 *  member.
32 *  The owner of local variables is the procedure they are defined in.
33 *  The owner of global variables and procedures visible in the whole
34 *  program is a universally defined class type "GlobalType".  The owner
35 *  of procedures defined in the scope of an other procedure is the
36 *  enclosing procedure.
37 *
38 *  In detail the datastructure entity has the following fields:
39 *
40 *  - ident *name:    Name of this entity as specified in the source code.
41 *                    Only unequivocal in conjuction with scope.
42 *  - ident *ld_name: Unique name of this entity, i.e., the mangled
43 *                    name.  E.g., for a class `A' with field `a' this
44 *                    is the ident for `A_a'.
45 *  - type *type:     The type of this entity, e.g., a method type, a
46 *                    basic type of the language or a class itself.
47 *  - type *owner:    The class this entity belongs to.  In case of local
48 *                    variables the method they are defined in.
49 *  - int offset:     Offset in bits for this entity.  Fixed when layout
50 *                    of owner is determined.
51 *  - ir_graph *irg:  If (type == method_type) this is the corresponding irg.
52 *                    The ir_graph constructor automatically sets this field.
53 *                    If (type != method_type) access of this field will cause
54 *                    an assertion.
55 *  - unsigned irg_add_properties:
56 *                    If (type == method_type) this mirrors the additional flags
57 *                    of the corresponding irg if set or is an own set for
58 *                    this entity. This construction allows to specify these
59 *                    flags even if no graph is available.
60 *                    If (type != method_type) access of this field will cause
61 *                    an assertion.
62 */
63
64 # ifndef _ENTITY_H_
65 # define _ENTITY_H_
66
67 # include "ident.h"
68 # include "type.h"
69 # include "dbginfo.h"
70 # include "irgraph.h"
71
72 # include "tr_inheritance.h"
73
74 /*-----------------------------------------------------------------*/
75 /* general                                                         */
76 /*-----------------------------------------------------------------*/
77
78 /** Initalize entity module. */
79 void init_entity (void);
80
81 /*-----------------------------------------------------------------*/
82 /* ENTITY                                                          */
83 /*-----------------------------------------------------------------*/
84
85 /* to resolve recursion between entity.h and irgraph.h */
86 #ifndef _IR_GRAPH_TYPEDEF_
87 #define _IR_GRAPH_TYPEDEF_
88 typedef struct ir_graph ir_graph;
89 #endif
90
91 /**
92  *
93  *   An abstract data type to represent program entities.
94  *
95  *   @param owner      A compound type this entity is a part of.
96  *   @param type       The type of this entity.
97  *   @param name       The string that represents this entity in the source program.
98  *   @param allocation A flag saying whether the entity is dynamically or statically
99  *              allocated (values: dynamic_allocated,  static_allocated,
100  *              automatic_allocated).
101  *   @param visibility A flag indicating the visibility of this entity (values: local,
102  *              external_visible,  external_allocated)
103  *   @param variability A flag indicating the variability of this entity (values:
104  *              uninitialized, initialized, part_constant, constant)
105  *   @param volatility @@@
106  *   @param offset     The offset of the entity within the compound object in bits.  Only set
107  *              if the owner in the state "layout_fixed".
108  *   @param overwrites A list of entities overwritten by this entity.  This list is only
109  *              existent if the owner of this entity is a class.  The members in
110  *              this list must be entities of super classes.
111  *   @param overwrittenby A list of entities that overwrite this entity.  This list is only
112  *              existent if the owner of this entity is a class.  The members in
113  *              this list must be entities of sub classes.
114  *   @param link       A void* to associate some additional information with the entity.
115  *   @param irg        If the entity is a method this is the ir graph that represents the
116  *              code of the method.
117  *   @param peculiarity The peculiarity of the entity.  If the entity is a method this
118  *              indicates whether the entity represents
119  *              a real method or whether it only exists to describe an interface.
120  *              In that case there nowhere exists code for this entity and this entity
121  *              is never dynamically used in the code.
122  *              Values: description, existent.  Default: existent.
123  *   @param visited   visited flag.  Master flag is type_visited.
124  *
125  *  @param These fields can only be accessed via access functions.
126  *
127  * @see  type
128  */
129
130 /* to resolve recursion between entity.h and type.h */
131 /** the type of an entity */
132 #ifndef _ENTITY_TYPEDEF_
133 #define _ENTITY_TYPEDEF_
134 typedef struct entity entity;
135 #endif
136
137 /**
138  * Creates a new entity.
139  *
140  * Automatically inserts the entity as a member of owner.
141  * Entity is automatic_allocated and uninitialized except if the type
142  * is type_method, then it is static_allocated and constant.  The constant
143  * value is a pointer to the method.
144  * Visibility is local, offset -1, and it is not volatile.
145  */
146 entity     *new_entity (type *owner, ident *name, type *tp);
147
148 /**
149  * Creates a new entity.
150  *
151  * Automatically inserts the entity as a member of owner.
152  * The entity is automatic allocated and uninitialized except if the type
153  * is type_method, then it is static allocated and constant.  The constant
154  * value is a pointer to the method.
155  * Visibility is local, offset -1, and it is not volatile.
156  */
157 entity     *new_d_entity (type *owner, ident *name, type *tp, dbg_info *db);
158
159 /**
160  * Copies the entity if the new_owner is different from the
161  * owner of the old entity,  else returns the old entity.
162  *
163  * Automatically inserts the new entity as a member of owner.
164  * Resets the overwrites/overwritten_by fields.
165  * Keeps the old atomic value.
166  *   @@@ Maybe we should change this.  If peculiarity of a method
167  *       is existent, we should add a new SymConst that points to
168  *       itself and not to the origin.  Right now we have to change
169  *       the peculiarity and then set a new atomic value by hand.
170  */
171 entity     *copy_entity_own (entity *old, type *new_owner);
172
173 /**
174  * Copies the entity if the new_name is different from the
175  * name of the old entity, else returns the old entity.
176  *
177  * Automatically inserts the new entity as a member of owner.
178  * The mangled name ld_name is set to NULL.
179  * Overwrites relation is copied from old.
180  */
181 entity     *copy_entity_name (entity *old, ident *new_name);
182
183 /**
184  * Frees the entity.
185  *
186  * The owner will still contain the pointer to this
187  * entity, as well as all other references!
188  */
189 void        free_entity (entity *ent);
190
191 /** Returns the name of an entity. */
192 const char *get_entity_name     (const entity *ent);
193
194 /** Returns the ident of an entity. */
195 ident      *get_entity_ident    (const entity *ent);
196
197 /** Returns the mangled name of the entity.
198  *
199  * If the mangled name is set it returns the existing name.
200  * Else it generates a name with mangle_entity()
201  * and remembers this new name internally.
202  */
203 ident      *get_entity_ld_ident (entity *ent);
204
205 /** Sets the mangled name of the entity. */
206 void        set_entity_ld_ident (entity *ent, ident *ld_ident);
207
208 /** Returns the mangled name of the entity as a string. */
209 const char *get_entity_ld_name (entity *ent);
210
211 /** Returns the owner of the entity. */
212 type       *get_entity_owner (entity *ent);
213
214 /** Sets the owner field in entity to owner.  Don't forget to add
215    ent to owner!! */
216 void        set_entity_owner (entity *ent, type *owner);
217
218 /** Asserts if the type owner is either a compound type or an array */
219 void assert_legal_owner_of_ent(type *owner);
220
221 /** Returns the type of an entity. */
222 type     *get_entity_type (entity *ent);
223
224 /** Sets the type of an entity. */
225 void      set_entity_type (entity *ent, type *tp);
226
227 /** The allocation type. */
228 typedef enum {
229   allocation_automatic, /**< The entity is allocated during runtime, implicitly
230                  as component of a compound type.   This is the default. */
231   allocation_parameter, /**< The entity is a parameter.  It is also automatic allocated.
232                  We distinguish the allocation of parameters from the allocation
233                  of local variables as their placement depends on the calling
234                  conventions. */
235   allocation_dynamic,   /**< The entity is allocated during runtime, explicitly
236                  by an Alloc node. */
237   allocation_static     /**< The entity is allocated statically.  We can use a
238                              Const as address of the entity. */
239 } ent_allocation;
240
241 /** Returns the allocation type of an entity. */
242 ent_allocation get_entity_allocation (const entity *ent);
243
244 /** Sets the allocation type of an entity. */
245 void           set_entity_allocation (entity *ent, ent_allocation al);
246
247 /** Return the name of the allocation type. */
248 const char *get_allocation_name(ent_allocation vis);
249
250 /** Returns the visibility of an entity. */
251 visibility get_entity_visibility (const entity *ent);
252
253 /** Sets the visibility of an entity. */
254 void       set_entity_visibility (entity *ent, visibility vis);
255
256 /** Return the name of the visibility */
257 const char *get_visibility_name(visibility vis);
258
259 /** This enumeration flags the variability of entities. */
260 typedef enum {
261   variability_uninitialized,    /**< The content of the entity is completely unknown. Default. */
262   variability_initialized,      /**< After allocation the entity is initialized with the
263                              value given somewhere in the entity. */
264   variability_part_constant,    /**< For entities of compound types.
265                                      The members of the entity are mixed constant,
266                                      initialized or uninitialized. */
267   variability_constant          /**< The entity is constant. */
268 } ent_variability;
269
270 /** Returns the variability of an entity. */
271 ent_variability get_entity_variability (const entity *ent);
272
273 /** Sets the variability of an entity. */
274 void            set_entity_variability (entity *ent, ent_variability var);
275
276 /** Return the name of the variability. */
277 const char *get_variability_name(ent_variability var);
278
279 /** This enumeration flags the volatility of entities. */
280 typedef enum {
281   volatility_non_volatile,    /**< The entity is not volatile. Default. */
282   volatility_is_volatile      /**< The entity is volatile */
283 } ent_volatility;
284
285 /** Returns the volatility of an entity. */
286 ent_volatility get_entity_volatility (const entity *ent);
287
288 /** Sets the volatility of an entity. */
289 void           set_entity_volatility (entity *ent, ent_volatility vol);
290
291 /** Return the name of the volatility. */
292 const char *get_volatility_name(ent_volatility var);
293
294 /** This enumeration flags the stickyness of an entity. */
295 typedef enum {
296   stickyness_unsticky,          /**< The entity can be removed from
297                                    the program, unless contraindicated
298                                    by other attributes. Default. */
299   stickyness_sticky             /**< The entity must remain in the
300                                    program in any case. */
301 } ent_stickyness;
302
303 /** Get the entity's stickyness */
304 ent_stickyness get_entity_stickyness(const entity *ent);
305
306 /** Set the entity's stickyness */
307 void      set_entity_stickyness(entity *ent, ent_stickyness stickyness);
308
309 /** Returns the offset of an entity (in a compound) in bytes. Only set if layout = fixed. */
310 int       get_entity_offset_bytes(const entity *ent);
311
312 /** Returns the offset of an entity (in a compound) in bits. Only set if layout = fixed. */
313 int       get_entity_offset_bits(const entity *ent);
314
315 /** Sets the offset of an entity (in a compound) in bytes. */
316 void      set_entity_offset_bytes(entity *ent, int offset);
317
318 /** Sets the offset of an entity (in a compound) in bits. */
319 void      set_entity_offset_bits(entity *ent, int offset);
320
321 /** Returns the stored intermediate information. */
322 void*   get_entity_link(const entity *ent);
323
324 /** Stores new intermediate information. */
325 void    set_entity_link(entity *ent, void *l);
326
327 /* -- Fields of method entities -- */
328 /** The entity knows the corresponding irg if the entity is a method.
329    This allows to get from a Call to the called irg.
330    Only entities of peculiarity "existent" can have a corresponding irg,
331    else the field is fixed to NULL.  (Get returns NULL, set asserts.) */
332 ir_graph *get_entity_irg(const entity *ent);
333 void      set_entity_irg(entity *ent, ir_graph *irg);
334
335 /** Return the peculiarity of an entity. */
336 peculiarity get_entity_peculiarity (const entity *ent);
337
338 /** Sets the peculiarity of an entity. */
339 void        set_entity_peculiarity (entity *ent, peculiarity pec);
340
341 /** Return the name of the peculiarity. */
342 const char *get_peculiarity_name(peculiarity var);
343
344 /* -- Representation of constant values of entities -- */
345 /** Returns true if the the node is representable as code on
346  *  const_code_irg. */
347 int      is_irn_const_expression(ir_node *n);
348 /* Set current_ir_graph to get_const_code_irg() to generate a constant
349    expression. */
350
351 /**
352  * Copies a firm subgraph that complies to the restrictions for
353  * constant expressions to current_block in current_ir_graph.
354  */
355 ir_node *copy_const_value(ir_node *n);
356
357 /* Set has no effect for existent entities of type method. */
358 ir_node *get_atomic_ent_value(entity *ent);
359 void     set_atomic_ent_value(entity *ent, ir_node *val);
360
361 /**
362  * The following type describes a path to a leave in the compound graph.
363  * Node 0 in the path must be an entity of type tp given in the constructor.  If
364  * the type of this element is compound, the path node 1 is an element of the type
365  * of node 0 an so forth, until an entity of atomic type is reached.
366  */
367 #ifndef _COMPOUND_GRAPH_PATH_TYPEDEF_
368 #define _COMPOUND_GRAPH_PATH_TYPEDEF_
369 typedef struct compound_graph_path compound_graph_path;
370 #endif /* _COMPOUND_GRAPH_PATH_TYPEDEF_ */
371
372 /** Creates a new compound graph path. */
373 compound_graph_path *new_compound_graph_path(type *tp, int length);
374
375 /** Returns non-zero if an object is a compound graph path */
376 int     is_compound_graph_path(void *thing);
377
378 /** Frees a graph path object */
379 void    free_compound_graph_path (compound_graph_path *gr);
380
381 /** Returns the length of a graph path */
382 int     get_compound_graph_path_length(compound_graph_path *gr);
383
384 entity *get_compound_graph_path_node(compound_graph_path *gr, int pos);
385 void    set_compound_graph_path_node(compound_graph_path *gr, int pos, entity *node);
386 int     get_compound_graph_path_array_index(compound_graph_path *gr, int pos);
387 void    set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index);
388
389 /** Checks whether the path up to pos is correct. If the path contains a NULL,
390  *  assumes the path is not complete and returns 'true'. */
391 int is_proper_compound_graph_path(compound_graph_path *gr, int pos);
392
393 /* A value of a compound entity is a pair of a value and the description of the
394    corresponding access path to the member of the compound.  */
395 void     add_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path);
396 void     set_compound_ent_value_w_path(entity *ent, ir_node *val, compound_graph_path *path, int pos);
397 /** Returns the number of constant values needed to initialize the entity.
398  *
399  *  Asserts if the entity has variability_uninitialized.
400  * */
401 int      get_compound_ent_n_values(entity *ent);
402 /** Returns a constant value given the position. */
403 ir_node *get_compound_ent_value(entity *ent, int pos);
404 /** Returns the access path for value at position pos. */
405 compound_graph_path *get_compound_ent_value_path(entity *ent, int pos);
406 /** Returns the position of a value with the given path.
407  *  The path must contain array indicees for all array element entities. */
408 int get_compound_ent_pos_by_path(entity *ent, compound_graph_path *path);
409 /** Returns a constant value given the access path.
410  *  The path must contain array indicees for all array element entities. */
411 ir_node *get_compound_ent_value_by_path(entity *ent, compound_graph_path *path);
412
413 /** Removes all constant entries where the path ends at value_ent. Does not
414    free the memory of the paths.  (The same path might be used for several
415    constant entities. */
416 void     remove_compound_ent_value(entity *ent, entity *value_ent);
417
418 /* Some languages support only trivial access paths, i.e., the member is a
419    direct, atomic member of the constant entities type. In this case the
420    corresponding entity can be accessed directly.  The following functions
421    allow direct access. */
422
423 /** generates a Path with length 1 */
424 void     add_compound_ent_value(entity *ent, ir_node *val, entity *member);
425
426 /** Returns the last member in the path */
427 entity  *get_compound_ent_value_member(entity *ent, int pos);
428
429 /** Sets the path at pos 0 */
430 void     set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos);
431
432 /** Initializes the entity ent which must be of a one dimensional
433    array type with the values given in the values array.
434    The array must have a lower and an upper bound.  Keeps the
435    order of values. Does not test whether the number of values
436    fits into the given array size.  Does not test whether the
437    values have the proper mode for the array. */
438 void set_array_entity_values(entity *ent, tarval **values, int num_vals);
439
440 /** Return the overall offset of value at position pos in bits.
441  *
442  * This requires that the layout of all concerned types is fixed.
443  *
444  * @param ent Any entity of compound type with at least pos initialization values.
445  * @param pos The position of the value for which the offset is requested.
446  */
447 int  get_compound_ent_value_offset_bits(entity *ent, int pos);
448
449 /** Return the overall offset of value at position pos in bytes.
450  *
451  * This requires that the layout of all concerned types is fixed.
452  * Asserts if bit offset is not byte aligned.
453  *
454  * @param ent Any entity of compound type with at least pos initialization values.
455  * @param pos The position of the value for which the offset is requested.
456  */
457 int  get_compound_ent_value_offset_bytes(entity *ent, int pos);
458
459 /** Compute the array indicees in compound graph paths of initialized entities.
460  *
461  * All arrays must have fixed lower and upper bounds.  One array can
462  * have an open upper bound.  If there are several open bounds, we do
463  * nothing.  There must be initializer elements for all array
464  * elements.  Uses the link field in the array element entities.  The
465  * array bounds must be representable as ints.
466  *
467  * @param ent Any entity.
468  */
469 void compute_compound_ent_array_indicees(entity *ent);
470
471 /** Sort the values of the compound entity by their overall offset.
472  *
473  * This requires that the layout of all concerned types is fixed.
474  * If the entity has no initialization information the method just
475  * returns.  This is needed to dump the entity in a backend.
476  *
477  * @param ent Any entity.
478  */
479 void sort_compound_ent_values(entity *ent);
480
481
482 /* --- Fields of entities with a class type as owner --- */
483 /* Overwrites is a field that specifies that an access to the overwritten
484    entity in the supertype must use this entity.  It's a list as with
485    multiple inheritance several entities can be overwritten.  This field
486    is mostly useful for method entities.
487    If a Sel node selects an entity that is overwritten by other entities it
488    must return a pointer to the entity of the dynamic type of the pointer
489    that is passed to it.  Lowering of the Sel node must assure this.
490    Overwrittenby is the inverse of overwrites.  Both add routines add
491    both relations, they only differ in the order of arguments. */
492 void    add_entity_overwrites   (entity *ent, entity *overwritten);
493 int     get_entity_n_overwrites (entity *ent);
494 int     get_entity_overwrites_index(entity *ent, entity *overwritten);
495 entity *get_entity_overwrites   (entity *ent, int pos);
496 void    set_entity_overwrites   (entity *ent, int pos, entity *overwritten);
497 void    remove_entity_overwrites(entity *ent, entity *overwritten);
498
499 void    add_entity_overwrittenby   (entity *ent, entity *overwrites);
500 int     get_entity_n_overwrittenby (entity *ent);
501 int     get_entity_overwrittenby_index(entity *ent, entity *overwrites);
502 entity *get_entity_overwrittenby   (entity *ent, int pos);
503 void    set_entity_overwrittenby   (entity *ent, int pos, entity *overwrites);
504 void    remove_entity_overwrittenby(entity *ent, entity *overwrites);
505
506 /**
507  *   Checks whether a pointer points to an entity.
508  *
509  *   @param thing     an arbitrary pointer
510  *
511  *   @return
512  *       true if the thing is an entity, else false
513  */
514 int is_entity (const void *thing);
515
516 /** Returns true if the type of the entity is a primitive, pointer
517    enumeration or method type. */
518 int is_atomic_entity(entity *ent);
519 /** Returns true if the type of the entity is a class, structure,
520    array or union type. */
521 int is_compound_entity(entity *ent);
522
523 /** Returns true if ent1 and ent2 have are equal except for their owner.
524    Two entities are equal if
525     - they have the same type (the same C-struct)
526     - ...?
527 */
528 bool equal_entity(entity *ent1, entity *ent2);
529
530 /** Outputs a unique number for this entity if libfirm is compiled for
531    debugging, (configure with --enable-debug) else returns 0. */
532 long get_entity_nr(entity *ent);
533
534 /** Returns the entities visited count. */
535 unsigned long get_entity_visited(entity *ent);
536
537 /** Sets the entities visited count. */
538 void        set_entity_visited(entity *ent, unsigned long num);
539
540 /** Sets visited field in entity to entity_visited. */
541 void        mark_entity_visited(entity *ent);
542
543 /** Returns true if this entity was visited. */
544 int        entity_visited(entity *ent);
545
546 /** Returns true if this entity was not visited. */
547 int        entity_not_visited(entity *ent);
548
549 /** Returns the mask of the additional graph properties. */
550 unsigned get_entity_additional_properties(const entity *ent);
551
552 /** Sets the mask of the additional graph properties. */
553 void set_entity_additional_properties(entity *ent, unsigned property_mask);
554
555 /** Sets one additional graph property. */
556 void set_entity_additional_property(entity *ent, unsigned flag);
557
558 /**
559  * @page unknown_entity
560  *
561  *  This entity is an auxiliary entity dedicated to support analyses.
562  *
563  *  The unknown entity represents that there could be an entity, but it is not
564  *  known.  This entity can be used to initialize fields before an analysis (not known
565  *  yet) or to represent the top of a lattice (could not be determined).  There exists
566  *  exactly one entity unknown. This entity has as owner and as type the unknown type. It is
567  *  allocated when initializing the entity module.
568  *
569  *  The entity can take the role of any entity, also methods.  It returns default
570  *  values in these cases.
571  *
572  *  The following values are set:
573  *    name          = "unknown_entity"
574  *    ld_name       = "unknown_entity"
575  *    owner         = unknown_type
576  *    type          = unknown_type
577  *    allocation    = allocation_automatic
578  *    visibility    = visibility_external_allocated
579  *    offset        = -1
580  *    variability   = variability_uninitialized
581  *    value         = SymConst(unknown_entity)
582  *    values        = NULL
583  *    val_paths     = NULL
584  *    peculiarity   = peculiarity_existent
585  *    volatility    = volatility_non_volatile
586  *    stickyness    = stickyness_unsticky
587  *    ld_name       = NULL
588  *    overwrites    = NULL
589  *    overwrittenby = NULL
590  *    irg           = NULL
591  *    link          = NULL
592  *
593  */
594 /* A variable that contains the only unknown entity. */
595 extern entity *unknown_entity;
596
597 /** Returns the unknown entity */
598 entity *get_unknown_entity(void);
599
600 # endif /* _ENTITY_H_ */