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