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