Add access functions to entity visited flag,
[libfirm] / ir / tr / entity.c
1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Authors: Martin Trapp, Christian Schaefer
5 **
6 */
7
8 /* $Id$ */
9
10 #ifdef HAVE_CONFIG_H
11 # include <config.h>
12 #endif
13
14 # include <stdlib.h>
15 # include <stddef.h>
16 # include <string.h>
17
18 # include "entity_t.h"
19 # include "mangle.h"
20 # include "typegmod_t.h"
21 # include "array.h"
22 /* All this is needed to build the constant node for methods: */
23 # include "irprog.h"
24 # include "ircons.h"
25
26 /*******************************************************************/
27 /** general                                                       **/
28 /*******************************************************************/
29
30 unsigned long entity_visited;
31
32 void
33 init_entity (void)
34 {
35 }
36
37 /*******************************************************************/
38 /** ENTITY                                                        **/
39 /*******************************************************************/
40
41 INLINE void insert_entity_in_owner (entity *ent) {
42   type *owner = ent->owner;
43   switch (get_type_tpop_code(owner)) {
44   case tpo_class: {
45     add_class_member (owner, ent);
46   } break;
47   case tpo_struct: {
48     add_struct_member (owner, ent);
49   } break;
50   case tpo_union: {
51     add_union_member (owner, ent);
52   } break;
53   case tpo_array: {
54     set_array_element_entity(owner, ent);
55   } break;
56   default: assert(0);
57   }
58 }
59
60 entity *
61 new_entity (type *owner, ident *name, type *type)
62 {
63   entity *res;
64   ir_graph *rem;
65
66   res = (entity *) malloc (sizeof (entity));
67   res->kind = k_entity;
68   assert_legal_owner_of_ent(owner);
69   res->owner = owner;
70   res->name = name;
71   res->type = type;
72   if (get_type_tpop(type) == type_method)
73     res->allocation = static_allocated;
74   else
75     res->allocation = automatic_allocated;
76   res->visibility = local;
77   res->offset = -1;
78   if (is_method_type(type)) {
79     res->variability = constant;
80     rem = current_ir_graph;
81     current_ir_graph = get_const_code_irg();
82     res->value = new_Const(mode_p, tarval_p_from_entity(res));
83     current_ir_graph = rem;
84   } else {
85     res->variability = uninitialized;
86   }
87   res->peculiarity = existent;
88   res->volatility = non_volatile;
89   res->ld_name = NULL;
90   res->overwrites = NEW_ARR_F(entity *, 1);
91   res->overwrittenby = NEW_ARR_F(entity *, 1);
92
93   res->irg = NULL;
94
95   res->visit = 0;
96
97   /* Remember entity in it's owner. */
98   insert_entity_in_owner (res);
99   return res;
100 }
101
102 INLINE void free_entity_attrs(entity *ent) {
103   assert(ent);
104   if (get_type_tpop(get_entity_owner(ent)) == type_class) {
105     DEL_ARR_F(ent->overwrites);
106     DEL_ARR_F(ent->overwrittenby);
107   }
108 }
109
110 entity *
111 copy_entity_own (entity *old, type *new_owner) {
112   entity *new;
113
114   assert_legal_owner_of_ent(new_owner);
115   if (old->owner == new_owner) return old;
116   new = (entity *) malloc (sizeof (entity));
117   memcpy (new, old, sizeof (entity));
118   new->owner = new_owner;
119   if ((get_type_tpop(get_entity_owner(old)) == type_class) &&
120       (get_type_tpop(new_owner) == type_class)) {
121     new->overwrites = DUP_ARR_F(entity *, old->overwrites);
122     new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
123   } else if ((get_type_tpop(get_entity_owner(old)) != type_class) &&
124              (get_type_tpop(new_owner) == type_class)) {
125     new->overwrites = NEW_ARR_F(entity *, 1);
126     new->overwrittenby = NEW_ARR_F(entity *, 1);
127   }
128
129   insert_entity_in_owner (new);
130
131   return new;
132 }
133
134 entity *
135 copy_entity_name (entity *old, ident *new_name) {
136   entity *new;
137
138   if (old->name == new_name) return old;
139   new = (entity *) malloc (sizeof (entity));
140   memcpy (new, old, sizeof (entity));
141   new->name = new_name;
142   new->ld_name = NULL;
143   new->overwrites = DUP_ARR_F(entity *, old->overwrites);
144   new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
145
146   insert_entity_in_owner (new);
147
148   return new;
149 }
150
151 void
152 free_entity (entity *ent) {
153   free_entity_attrs(ent);
154   free(ent);
155 }
156
157 INLINE const char *
158 get_entity_name (entity *ent) {
159   assert (ent);
160   return id_to_str(get_entity_ident(ent));
161 }
162
163 ident *
164 get_entity_ident    (entity *ent) {
165   assert(ent);
166   return ent->name;
167 }
168
169 /*
170 void   set_entitye_ld_name  (entity *, char *ld_name);
171 void   set_entity_ld_ident (entity *, ident *ld_ident);
172 */
173
174 INLINE type *
175 get_entity_owner (entity *ent) {
176   return ent->owner = skip_tid(ent->owner);
177 }
178
179 INLINE void
180 set_entity_owner (entity *ent, type *owner) {
181   assert_legal_owner_of_ent(owner);
182   ent->owner = owner;
183 }
184
185 INLINE void   /* should this go into type.c? */
186 assert_legal_owner_of_ent(type *owner) {
187   assert (get_type_tpop_code(owner) == tpo_class ||
188           get_type_tpop_code(owner) == tpo_union ||
189           get_type_tpop_code(owner) == tpo_struct ||
190           get_type_tpop_code(owner) == tpo_array);   /* Yes, array has an entity
191                                                         -- to select fields! */
192 }
193
194 INLINE ident *
195 get_entity_ld_ident (entity *ent)
196 {
197   if (ent->ld_name == NULL)
198     ent->ld_name = mangle_entity (ent);
199   return ent->ld_name;
200 }
201
202 INLINE void
203 set_entity_ld_ident (entity *ent, ident *ld_ident) {
204   ent->ld_name = ld_ident;
205 }
206
207 INLINE const char *
208 get_entity_ld_name (entity *ent) {
209   return id_to_str(get_entity_ld_ident(ent));
210 }
211
212 /*
213 char  *get_entity_ld_name  (entity *);
214 void   set_entity_ld_name  (entity *, char *ld_name);
215 */
216
217 INLINE type *
218 get_entity_type (entity *ent) {
219   return ent->type = skip_tid(ent->type);
220 }
221
222 INLINE void
223 set_entity_type (entity *ent, type *type) {
224   ent->type = type;
225 }
226
227
228 INLINE ent_allocation
229 get_entity_allocation (entity *ent) {
230   return ent->allocation;
231 }
232
233 INLINE void
234 set_entity_allocation (entity *ent, ent_allocation al) {
235   ent->allocation = al;
236 }
237
238
239 INLINE ent_visibility
240 get_entity_visibility (entity *ent) {
241   return ent->visibility;
242 }
243
244 INLINE void
245 set_entity_visibility (entity *ent, ent_visibility vis) {
246   if (vis != local) assert(ent->allocation == static_allocated);
247   ent->visibility = vis;
248 }
249
250 INLINE ent_variability
251 get_entity_variability (entity *ent) {
252   return ent->variability;
253 }
254
255 INLINE void
256 set_entity_variability (entity *ent, ent_variability var){
257   if (var == part_constant)
258     assert(is_class_type(ent->type) || is_struct_type(ent->type));
259   if ((is_compound_type(ent->type)) &&
260       (ent->variability == uninitialized) && (var != uninitialized)) {
261     /* Allocate datastructures for constant values */
262     ent->values = NEW_ARR_F(ir_node *, 1);
263     ent->val_ents = NEW_ARR_F(entity *, 1);
264   }
265   if ((is_compound_type(ent->type)) &&
266       (var == uninitialized) && (ent->variability != uninitialized)) {
267     /* Free datastructures for constant values */
268     DEL_ARR_F(ent->values);
269     DEL_ARR_F(ent->val_ents);
270   }
271   ent->variability = var;
272 }
273
274
275 INLINE ent_volatility
276 get_entity_volatility (entity *ent) {
277   assert (ent);
278   return ent->volatility;
279 }
280
281 INLINE void
282 set_entity_volatility (entity *ent, ent_volatility vol) {
283   assert (ent);
284   ent->volatility = vol;
285 }
286
287 INLINE peculiarity
288 get_entity_peculiarity (entity *ent) {
289   assert (ent);
290   return ent->peculiarity;
291 }
292
293 INLINE void
294 set_entity_peculiarity (entity *ent, peculiarity pec) {
295   assert (ent);
296   /* @@@ why peculiarity only for methods? */
297   assert (is_method_type(ent->type));
298   ent->peculiarity = pec;
299 }
300
301 /* Set has no effect for entities of type method. */
302 INLINE ir_node *
303 get_atomic_ent_value(entity *ent) {
304   assert(ent); assert(is_atomic_entity(ent));
305   assert((ent->variability != uninitialized));
306   return ent->value;
307 }
308
309 INLINE void
310 set_atomic_ent_value(entity *ent, ir_node *val) {
311   assert(ent && is_atomic_entity(ent) && (ent->variability != uninitialized));
312   if (is_method_type(ent->type)) return;
313   ent->value = val;
314 }
315
316
317 ir_node *copy_const_value(ir_node *n) {
318   ir_node *nn;
319   ir_mode *m;
320
321   m = get_irn_mode(n);
322   switch(get_irn_opcode(n)) {
323   case iro_Const:
324     nn = new_Const(m, get_Const_tarval(n)); break;
325   case iro_SymConst:
326     nn = new_SymConst(get_SymConst_type_or_id(n), get_SymConst_kind(n)); break;
327   case iro_Add:
328     nn = new_Add(copy_const_value(get_Add_left(n)), copy_const_value(get_Add_right(n)), m); break;
329   default:
330     assert(0 && "opdope invalid or not implemented"); break;
331   }
332   return nn;
333 }
334
335 /* A value of a compound entity is a pair of value and the corresponding member of
336    the compound. */
337 INLINE void
338 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
339   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
340   ARR_APP1 (ir_node *, ent->values, val);
341   ARR_APP1 (entity *, ent->val_ents, member);
342 }
343
344 /* Copies the firm subgraph referenced by val to const_code_irg and adds
345    the node as constant initialization to ent.
346    The subgraph may not contain control flow operations.
347 INLINE void
348 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
349   ir_graph *rem = current_ir_graph;
350
351   assert(get_entity_variability(ent) != uninitialized);
352   current_ir_graph = get_const_code_irg();
353
354   val = copy_const_value(val);
355   add_compound_ent_value(ent, val, member);
356   current_ir_graph = rem;
357   }*/
358
359 INLINE int
360 get_compound_ent_n_values(entity *ent) {
361   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
362   return (ARR_LEN (ent->values))-1;
363 }
364
365 INLINE ir_node  *
366 get_compound_ent_value(entity *ent, int pos) {
367   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
368   return ent->values[pos+1];
369 }
370
371 /* Copies the value i of the entity to current_block in current_ir_graph.
372 ir_node *
373 copy_compound_ent_value(entity *ent, int pos) {
374   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
375   return copy_const_value(ent->values[pos+1]);
376   }*/
377
378 INLINE entity   *
379 get_compound_ent_value_member(entity *ent, int pos) {
380   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
381   return ent->val_ents[pos+1];
382 }
383
384 INLINE void
385 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
386   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
387   ent->values[pos+1] = val;
388   ent->val_ents[pos+1] = member;
389 }
390
391 void
392 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
393   int i;
394   ir_graph *rem = current_ir_graph;
395   type *arrtp = get_entity_type(ent);
396   ir_node *val;
397
398   assert(is_array_type(arrtp));
399   assert(get_array_n_dimensions(arrtp) == 1);
400   /* One bound is sufficient, the nunmber of constant fields makes the
401      size. */
402   assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
403   assert(get_entity_variability(ent) != uninitialized);
404   current_ir_graph = get_const_code_irg();
405
406   for (i = 0; i < num_vals; i++) {
407     val = new_Const(get_tv_mode (values[i]), values[i]);
408     add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
409   }
410   current_ir_graph = rem;
411 }
412
413 INLINE int
414 get_entity_offset (entity *ent) {
415   return ent->offset;
416 }
417
418 INLINE void
419 set_entity_offset (entity *ent, int offset) {
420   ent->offset = offset;
421 }
422
423 INLINE void
424 add_entity_overwrites   (entity *ent, entity *overwritten) {
425   assert(ent);
426   assert(is_class_type(get_entity_owner(ent)));
427   ARR_APP1 (entity *, ent->overwrites, overwritten);
428   ARR_APP1 (entity *, overwritten->overwrittenby, ent);
429 }
430
431 INLINE int
432 get_entity_n_overwrites (entity *ent) {
433   assert(ent);
434   assert(is_class_type(get_entity_owner(ent)));
435   return (ARR_LEN (ent->overwrites))-1;
436 }
437
438 INLINE entity *
439 get_entity_overwrites   (entity *ent, int pos) {
440   assert(ent);
441   assert(is_class_type(get_entity_owner(ent)));
442   assert(pos < get_entity_n_overwrites(ent));
443   return ent->overwrites[pos+1];
444 }
445
446 INLINE void
447 set_entity_overwrites   (entity *ent, int pos, entity *overwritten) {
448   assert(ent);
449   assert(is_class_type(get_entity_owner(ent)));
450   assert(pos < get_entity_n_overwrites(ent));
451   ent->overwrites[pos+1] = overwritten;
452 }
453
454 INLINE void
455 add_entity_overwrittenby   (entity *ent, entity *overwrites) {
456   assert(ent);
457   assert(is_class_type(get_entity_owner(ent)));
458   add_entity_overwrites(overwrites, ent);
459 }
460
461 INLINE int
462 get_entity_n_overwrittenby (entity *ent) {
463   assert(ent);
464   assert(is_class_type(get_entity_owner(ent)));
465   return (ARR_LEN (ent->overwrittenby))-1;
466 }
467
468 INLINE entity *
469 get_entity_overwrittenby   (entity *ent, int pos) {
470   assert(ent);
471   assert(is_class_type(get_entity_owner(ent)));
472   assert(pos < get_entity_n_overwrittenby(ent));
473   return ent->overwrittenby[pos+1];
474 }
475
476 INLINE void
477 set_entity_overwrittenby   (entity *ent, int pos, entity *overwrites) {
478   assert(ent);
479   assert(is_class_type(get_entity_owner(ent)));
480   assert(pos < get_entity_n_overwrittenby(ent));
481   ent->overwrittenby[pos+1] = overwrites;
482 }
483
484 /* A link to store intermediate information */
485 void *
486 get_entity_link(entity *ent) {
487   assert(ent);
488   return ent->link;
489 }
490
491 void
492 set_entity_link(entity *ent, void *l) {
493   assert(ent);
494   ent->link = l;
495 }
496
497 INLINE ir_graph *
498 get_entity_irg(entity *ent) {
499   assert (ent);
500   assert (is_method_type(ent->type));
501   return ent->irg;
502 }
503
504 INLINE void
505 set_entity_irg(entity *ent, ir_graph *irg) {
506   assert (ent && ent->type);
507   /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
508    * Methode selbst nicht mehr aufgerufen werden kann, die Entität
509    * aber erhalten bleiben soll. */
510   /* assert (irg); */
511   assert (is_method_type(ent->type));
512   assert (ent->peculiarity == existent);
513   ent->irg = irg;
514 }
515
516 int is_atomic_entity(entity *ent) {
517   type* t = get_entity_type(ent);
518   return (is_primitive_type(t) || is_pointer_type(t) ||
519           is_enumeration_type(t) || is_method_type(t));
520 }
521
522 int is_compound_entity(entity *ent) {
523   type* t = get_entity_type(ent);
524   return (is_class_type(t) || is_struct_type(t) ||
525           is_array_type(t) || is_union_type(t));
526 }
527
528 bool equal_entity(entity *ent1, entity *ent2) {
529   return true;
530 }
531
532
533 unsigned long get_entity_visited(entity *entity) {
534   assert (entity);
535   return entity->visit;
536 }
537 void        set_entity_visited(entity *entity, unsigned long num) {
538   assert (entity);
539   entity->visit = num;
540 }
541 /* Sets visited field in entity to entity_visited. */
542 void        mark_entity_visited(entity *entity) {
543   assert (entity);
544   entity->visit = entity_visited;
545 }