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