1c30d61bbbbff2c23a1b1816b8e75c9147acf0aa
[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 void
31 init_entity (void)
32 {
33 }
34
35 /*******************************************************************/
36 /** ENTITY                                                        **/
37 /*******************************************************************/
38
39 INLINE type *get_entity_owner (entity *ent);
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 entity *
102 new_d_entity (type *owner, ident *name, type *type, dbg_info *db) {
103   entity *res = new_entity(owner, name, type);
104   set_entity_dbg_info(res, db);
105   return res;
106 }
107 INLINE void free_entity_attrs(entity *ent) {
108   assert(ent);
109   if (get_type_tpop(get_entity_owner(ent)) == type_class) {
110     DEL_ARR_F(ent->overwrites);
111     DEL_ARR_F(ent->overwrittenby);
112   }
113 }
114
115 entity *
116 copy_entity_own (entity *old, type *new_owner) {
117   entity *new;
118
119   assert_legal_owner_of_ent(new_owner);
120   if (old->owner == new_owner) return old;
121   new = (entity *) malloc (sizeof (entity));
122   memcpy (new, old, sizeof (entity));
123   new->owner = new_owner;
124   if ((get_type_tpop(get_entity_owner(old)) == type_class) &&
125       (get_type_tpop(new_owner) == type_class)) {
126     new->overwrites = DUP_ARR_F(entity *, old->overwrites);
127     new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
128   } else if ((get_type_tpop(get_entity_owner(old)) != type_class) &&
129              (get_type_tpop(new_owner) == type_class)) {
130     new->overwrites = NEW_ARR_F(entity *, 1);
131     new->overwrittenby = NEW_ARR_F(entity *, 1);
132   }
133
134   insert_entity_in_owner (new);
135
136   return new;
137 }
138
139 entity *
140 copy_entity_name (entity *old, ident *new_name) {
141   entity *new;
142
143   if (old->name == new_name) return old;
144   new = (entity *) malloc (sizeof (entity));
145   memcpy (new, old, sizeof (entity));
146   new->name = new_name;
147   new->ld_name = NULL;
148   new->overwrites = DUP_ARR_F(entity *, old->overwrites);
149   new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
150
151   insert_entity_in_owner (new);
152
153   return new;
154 }
155
156 void
157 free_entity (entity *ent) {
158   free_entity_attrs(ent);
159   free(ent);
160 }
161
162 INLINE const char *
163 get_entity_name (entity *ent) {
164   assert (ent);
165   return id_to_str(get_entity_ident(ent));
166 }
167
168 ident *
169 get_entity_ident    (entity *ent) {
170   assert(ent);
171   return ent->name;
172 }
173
174 /*
175 void   set_entitye_ld_name  (entity *, char *ld_name);
176 void   set_entity_ld_ident (entity *, ident *ld_ident);
177 */
178
179 INLINE type *
180 get_entity_owner (entity *ent) {
181   return ent->owner = skip_tid(ent->owner);
182 }
183
184 INLINE void
185 set_entity_owner (entity *ent, type *owner) {
186   assert_legal_owner_of_ent(owner);
187   ent->owner = owner;
188 }
189
190 INLINE void   /* should this go into type.c? */
191 assert_legal_owner_of_ent(type *owner) {
192   assert (get_type_tpop_code(owner) == tpo_class ||
193           get_type_tpop_code(owner) == tpo_union ||
194           get_type_tpop_code(owner) == tpo_struct ||
195           get_type_tpop_code(owner) == tpo_array);   /* Yes, array has an entity
196                                                         -- to select fields! */
197 }
198
199 INLINE ident *
200 get_entity_ld_ident (entity *ent)
201 {
202   if (ent->ld_name == NULL)
203     ent->ld_name = mangle_entity (ent);
204   return ent->ld_name;
205 }
206
207 INLINE void
208 set_entity_ld_ident (entity *ent, ident *ld_ident) {
209   ent->ld_name = ld_ident;
210 }
211
212 INLINE const char *
213 get_entity_ld_name (entity *ent) {
214   return id_to_str(get_entity_ld_ident(ent));
215 }
216
217 /*
218 char  *get_entity_ld_name  (entity *);
219 void   set_entity_ld_name  (entity *, char *ld_name);
220 */
221
222 INLINE type *
223 get_entity_type (entity *ent) {
224   return ent->type = skip_tid(ent->type);
225 }
226
227 INLINE void
228 set_entity_type (entity *ent, type *type) {
229   ent->type = type;
230 }
231
232
233 INLINE ent_allocation
234 get_entity_allocation (entity *ent) {
235   return ent->allocation;
236 }
237
238 INLINE void
239 set_entity_allocation (entity *ent, ent_allocation al) {
240   ent->allocation = al;
241 }
242
243
244 INLINE ent_visibility
245 get_entity_visibility (entity *ent) {
246   return ent->visibility;
247 }
248
249 INLINE void
250 set_entity_visibility (entity *ent, ent_visibility vis) {
251   if (vis != local)
252     assert((ent->allocation == static_allocated) ||
253            (ent->allocation == automatic_allocated));
254   // @@@ Test that the owner type is not local, but how??
255   //       && get_class_visibility(get_entity_owner(ent)) != local));
256   ent->visibility = vis;
257 }
258
259 INLINE ent_variability
260 get_entity_variability (entity *ent) {
261   return ent->variability;
262 }
263
264 INLINE void
265 set_entity_variability (entity *ent, ent_variability var){
266   if (var == part_constant)
267     assert(is_class_type(ent->type) || is_struct_type(ent->type));
268   if ((is_compound_type(ent->type)) &&
269       (ent->variability == uninitialized) && (var != uninitialized)) {
270     /* Allocate datastructures for constant values */
271     ent->values = NEW_ARR_F(ir_node *, 1);
272     ent->val_ents = NEW_ARR_F(entity *, 1);
273   }
274   if ((is_compound_type(ent->type)) &&
275       (var == uninitialized) && (ent->variability != uninitialized)) {
276     /* Free datastructures for constant values */
277     DEL_ARR_F(ent->values);
278     DEL_ARR_F(ent->val_ents);
279   }
280   ent->variability = var;
281 }
282
283
284 INLINE ent_volatility
285 get_entity_volatility (entity *ent) {
286   assert (ent);
287   return ent->volatility;
288 }
289
290 INLINE void
291 set_entity_volatility (entity *ent, ent_volatility vol) {
292   assert (ent);
293   ent->volatility = vol;
294 }
295
296 INLINE peculiarity
297 get_entity_peculiarity (entity *ent) {
298   assert (ent);
299   return ent->peculiarity;
300 }
301
302 INLINE void
303 set_entity_peculiarity (entity *ent, peculiarity pec) {
304   assert (ent);
305   /* @@@ why peculiarity only for methods? */
306   assert (is_method_type(ent->type));
307   ent->peculiarity = pec;
308 }
309
310 /* Set has no effect for entities of type method. */
311 INLINE ir_node *
312 get_atomic_ent_value(entity *ent) {
313   assert(ent); assert(is_atomic_entity(ent));
314   assert((ent->variability != uninitialized));
315   return ent->value;
316 }
317
318 INLINE void
319 set_atomic_ent_value(entity *ent, ir_node *val) {
320   assert(ent && is_atomic_entity(ent) && (ent->variability != uninitialized));
321   if (is_method_type(ent->type)) return;
322   ent->value = val;
323 }
324
325
326 ir_node *copy_const_value(ir_node *n) {
327   ir_node *nn;
328   ir_mode *m;
329
330   m = get_irn_mode(n);
331   switch(get_irn_opcode(n)) {
332   case iro_Const:
333     nn = new_Const(m, get_Const_tarval(n)); break;
334   case iro_SymConst:
335     nn = new_SymConst(get_SymConst_type_or_id(n), get_SymConst_kind(n)); break;
336   case iro_Add:
337     nn = new_Add(copy_const_value(get_Add_left(n)), copy_const_value(get_Add_right(n)), m); break;
338   default:
339     assert(0 && "opdope invalid or not implemented"); break;
340   }
341   return nn;
342 }
343
344 /* A value of a compound entity is a pair of value and the corresponding member of
345    the compound. */
346 INLINE void
347 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
348   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
349   ARR_APP1 (ir_node *, ent->values, val);
350   ARR_APP1 (entity *, ent->val_ents, member);
351 }
352
353 /* Copies the firm subgraph referenced by val to const_code_irg and adds
354    the node as constant initialization to ent.
355    The subgraph may not contain control flow operations.
356 INLINE void
357 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
358   ir_graph *rem = current_ir_graph;
359
360   assert(get_entity_variability(ent) != uninitialized);
361   current_ir_graph = get_const_code_irg();
362
363   val = copy_const_value(val);
364   add_compound_ent_value(ent, val, member);
365   current_ir_graph = rem;
366   }*/
367
368 INLINE int
369 get_compound_ent_n_values(entity *ent) {
370   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
371   return (ARR_LEN (ent->values))-1;
372 }
373
374 INLINE ir_node  *
375 get_compound_ent_value(entity *ent, int pos) {
376   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
377   return ent->values[pos+1];
378 }
379
380 /* Copies the value i of the entity to current_block in current_ir_graph.
381 ir_node *
382 copy_compound_ent_value(entity *ent, int pos) {
383   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
384   return copy_const_value(ent->values[pos+1]);
385   }*/
386
387 INLINE entity   *
388 get_compound_ent_value_member(entity *ent, int pos) {
389   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
390   return ent->val_ents[pos+1];
391 }
392
393 INLINE void
394 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
395   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
396   ent->values[pos+1] = val;
397   ent->val_ents[pos+1] = member;
398 }
399
400 void
401 remove_compound_ent_value(entity *ent, entity *value_ent) {
402   int i;
403   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
404   for (i = 1; i < (ARR_LEN (ent->val_ents)); i++) {
405     if (ent->val_ents[i] == value_ent) {
406       for(; i < (ARR_LEN (ent->val_ents))-1; i++) {
407         ent->val_ents[i] = ent->val_ents[i+1];
408         ent->values[i]   = ent->values[i+1];
409       }
410       ARR_SETLEN(entity*,  ent->val_ents, ARR_LEN(ent->val_ents) - 1);
411       ARR_SETLEN(ir_node*, ent->values,   ARR_LEN(ent->values) - 1);
412       break;
413     }
414   }
415 }
416
417 void
418 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
419   int i;
420   ir_graph *rem = current_ir_graph;
421   type *arrtp = get_entity_type(ent);
422   ir_node *val;
423
424   assert(is_array_type(arrtp));
425   assert(get_array_n_dimensions(arrtp) == 1);
426   /* One bound is sufficient, the nunmber of constant fields makes the
427      size. */
428   assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
429   assert(get_entity_variability(ent) != uninitialized);
430   current_ir_graph = get_const_code_irg();
431
432   for (i = 0; i < num_vals; i++) {
433     val = new_Const(get_tv_mode (values[i]), values[i]);
434     add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
435   }
436   current_ir_graph = rem;
437 }
438
439 INLINE int
440 get_entity_offset (entity *ent) {
441   return ent->offset;
442 }
443
444 INLINE void
445 set_entity_offset (entity *ent, int offset) {
446   ent->offset = offset;
447 }
448
449 INLINE void
450 add_entity_overwrites   (entity *ent, entity *overwritten) {
451   assert(ent);
452   assert(is_class_type(get_entity_owner(ent)));
453   ARR_APP1 (entity *, ent->overwrites, overwritten);
454   ARR_APP1 (entity *, overwritten->overwrittenby, ent);
455 }
456
457 INLINE int
458 get_entity_n_overwrites (entity *ent) {
459   assert(ent);
460   assert(is_class_type(get_entity_owner(ent)));
461   return (ARR_LEN (ent->overwrites))-1;
462 }
463
464 INLINE entity *
465 get_entity_overwrites   (entity *ent, int pos) {
466   assert(ent);
467   assert(is_class_type(get_entity_owner(ent)));
468   assert(pos < get_entity_n_overwrites(ent));
469   return ent->overwrites[pos+1];
470 }
471
472 INLINE void
473 set_entity_overwrites   (entity *ent, int pos, entity *overwritten) {
474   assert(ent);
475   assert(is_class_type(get_entity_owner(ent)));
476   assert(pos < get_entity_n_overwrites(ent));
477   ent->overwrites[pos+1] = overwritten;
478 }
479
480 INLINE void
481 add_entity_overwrittenby   (entity *ent, entity *overwrites) {
482   assert(ent);
483   assert(is_class_type(get_entity_owner(ent)));
484   add_entity_overwrites(overwrites, ent);
485 }
486
487 INLINE int
488 get_entity_n_overwrittenby (entity *ent) {
489   assert(ent);
490   assert(is_class_type(get_entity_owner(ent)));
491   return (ARR_LEN (ent->overwrittenby))-1;
492 }
493
494 INLINE entity *
495 get_entity_overwrittenby   (entity *ent, int pos) {
496   assert(ent);
497   assert(is_class_type(get_entity_owner(ent)));
498   assert(pos < get_entity_n_overwrittenby(ent));
499   return ent->overwrittenby[pos+1];
500 }
501
502 INLINE void
503 set_entity_overwrittenby   (entity *ent, int pos, entity *overwrites) {
504   assert(ent);
505   assert(is_class_type(get_entity_owner(ent)));
506   assert(pos < get_entity_n_overwrittenby(ent));
507   ent->overwrittenby[pos+1] = overwrites;
508 }
509
510 /* A link to store intermediate information */
511 void *
512 get_entity_link(entity *ent) {
513   assert(ent);
514   return ent->link;
515 }
516
517 void
518 set_entity_link(entity *ent, void *l) {
519   assert(ent);
520   ent->link = l;
521 }
522
523 INLINE ir_graph *
524 get_entity_irg(entity *ent) {
525   assert (ent);
526   assert (is_method_type(ent->type));
527   return ent->irg;
528 }
529
530 INLINE void
531 set_entity_irg(entity *ent, ir_graph *irg) {
532   assert (ent && ent->type);
533   /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
534    * Methode selbst nicht mehr aufgerufen werden kann, die Entität
535    * aber erhalten bleiben soll. */
536   /* assert (irg); */
537   assert (is_method_type(ent->type));
538   assert (ent->peculiarity == existent);
539   ent->irg = irg;
540 }
541
542 int is_atomic_entity(entity *ent) {
543   type* t = get_entity_type(ent);
544   return (is_primitive_type(t) || is_pointer_type(t) ||
545           is_enumeration_type(t) || is_method_type(t));
546 }
547
548 int is_compound_entity(entity *ent) {
549   type* t = get_entity_type(ent);
550   return (is_class_type(t) || is_struct_type(t) ||
551           is_array_type(t) || is_union_type(t));
552 }
553
554 /* @@@ not implemnted!!! */
555 bool equal_entity(entity *ent1, entity *ent2) {
556   printf(" calling unimplemented equal entity!!! \n");
557   return true;
558 }
559
560
561 unsigned long get_entity_visited(entity *ent) {
562   assert (ent);
563   return ent->visit;
564 }
565 void        set_entity_visited(entity *ent, unsigned long num) {
566   assert (ent);
567   ent->visit = num;
568 }
569 /* Sets visited field in entity to entity_visited. */
570 void        mark_entity_visited(entity *ent) {
571   assert (ent);
572   ent->visit = type_visited;
573 }