1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Martin Trapp, Christian Schaefer
18 # include "entity_t.h"
20 # include "typegmod_t.h"
22 /* All this is needed to build the constant node for methods: */
26 /*******************************************************************/
28 /*******************************************************************/
35 /*******************************************************************/
37 /*******************************************************************/
39 INLINE void insert_entity_in_owner (entity *ent) {
40 type *owner = ent->owner;
41 switch (get_type_tpop_code(owner)) {
43 add_class_member (owner, ent);
46 add_struct_member (owner, ent);
49 add_union_member (owner, ent);
52 set_array_element_entity(owner, ent);
59 new_entity (type *owner, ident *name, type *type)
64 res = (entity *) malloc (sizeof (entity));
66 assert_legal_owner_of_ent(owner);
70 if (get_type_tpop(type) == type_method)
71 res->allocation = static_allocated;
73 res->allocation = automatic_allocated;
74 res->visibility = local;
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;
83 res->variability = uninitialized;
85 res->peculiarity = existent;
86 res->volatility = non_volatile;
88 res->overwrites = NEW_ARR_F(entity *, 1);
89 res->overwrittenby = NEW_ARR_F(entity *, 1);
95 /* Remember entity in it's owner. */
96 insert_entity_in_owner (res);
99 INLINE void free_entity_attrs(entity *ent) {
101 DEL_ARR_F(ent->overwrites);
102 DEL_ARR_F(ent->overwrittenby);
106 copy_entity_own (entity *old, type *new_owner) {
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);
117 insert_entity_in_owner (new);
123 copy_entity_name (entity *old, ident *new_name) {
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;
131 new->overwrites = DUP_ARR_F(entity *, old->overwrites);
132 new->overwrittenby = DUP_ARR_F(entity *, old->overwrittenby);
134 insert_entity_in_owner (new);
140 get_entity_name (entity *ent) {
142 return id_to_str(get_entity_ident(ent));
146 get_entity_ident (entity *ent) {
152 void set_entitye_ld_name (entity *, char *ld_name);
153 void set_entity_ld_ident (entity *, ident *ld_ident);
157 get_entity_owner (entity *ent) {
158 return ent->owner = skip_tid(ent->owner);
162 set_entity_owner (entity *ent, type *owner) {
163 assert_legal_owner_of_ent(owner);
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! */
177 get_entity_ld_ident (entity *ent)
179 if (ent->ld_name == NULL)
180 ent->ld_name = mangle_entity (ent);
185 set_entity_ld_ident (entity *ent, ident *ld_ident) {
186 ent->ld_name = ld_ident;
190 get_entity_ld_name (entity *ent) {
191 return id_to_str(get_entity_ld_ident(ent));
195 char *get_entity_ld_name (entity *);
196 void set_entity_ld_name (entity *, char *ld_name);
200 get_entity_type (entity *ent) {
201 return ent->type = skip_tid(ent->type);
205 set_entity_type (entity *ent, type *type) {
210 INLINE ent_allocation
211 get_entity_allocation (entity *ent) {
212 return ent->allocation;
216 set_entity_allocation (entity *ent, ent_allocation al) {
217 ent->allocation = al;
221 INLINE ent_visibility
222 get_entity_visibility (entity *ent) {
223 return ent->visibility;
227 set_entity_visibility (entity *ent, ent_visibility vis) {
228 if (vis != local) assert(ent->allocation == static_allocated);
229 ent->visibility = vis;
232 INLINE ent_variability
233 get_entity_variability (entity *ent) {
234 return ent->variability;
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);
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);
253 ent->variability = var;
257 INLINE ent_volatility
258 get_entity_volatility (entity *ent) {
260 return ent->volatility;
264 set_entity_volatility (entity *ent, ent_volatility vol) {
266 ent->volatility = vol;
270 get_entity_peculiarity (entity *ent) {
272 return ent->peculiarity;
276 set_entity_peculiarity (entity *ent, peculiarity pec) {
278 /* @@@ why peculiarity only for methods? */
279 assert (is_method_type(ent->type));
280 ent->peculiarity = pec;
283 /* Set has no effect for entities of type method. */
285 get_atomic_ent_value(entity *ent) {
286 assert(ent); assert(is_atomic_entity(ent));
287 assert((ent->variability != uninitialized));
292 set_atomic_ent_value(entity *ent, ir_node *val) {
293 assert(ent && is_atomic_entity(ent) && (ent->variability != uninitialized));
294 if (is_method_type(ent->type)) return;
299 ir_node *copy_const_value(ir_node *n) {
304 switch(get_irn_opcode(n)) {
306 nn = new_Const(m, get_Const_tarval(n)); break;
308 nn = new_SymConst(get_SymConst_type_or_id(n), get_SymConst_kind(n)); break;
310 nn = new_Add(copy_const_value(get_Add_left(n)), copy_const_value(get_Add_right(n)), m); break;
312 assert(0 && "opdope invalid or not implemented"); break;
317 /* A value of a compound entity is a pair of value and the corresponding member of
320 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
321 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
322 ARR_APP1 (ir_node *, ent->values, val);
323 ARR_APP1 (entity *, ent->val_ents, member);
326 /* Copies the firm subgraph referenced by val to const_code_irg and adds
327 the node as constant initialization to ent.
328 The subgraph may not contain control flow operations.
330 copy_and_add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
331 ir_graph *rem = current_ir_graph;
333 assert(get_entity_variability(ent) != uninitialized);
334 current_ir_graph = get_const_code_irg();
336 val = copy_const_value(val);
337 add_compound_ent_value(ent, val, member);
338 current_ir_graph = rem;
342 get_compound_ent_n_values(entity *ent) {
343 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
344 return (ARR_LEN (ent->values))-1;
348 get_compound_ent_value(entity *ent, int pos) {
349 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
350 return ent->values[pos+1];
353 /* Copies the value i of the entity to current_block in current_ir_graph.
355 copy_compound_ent_value(entity *ent, int pos) {
356 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
357 return copy_const_value(ent->values[pos+1]);
361 get_compound_ent_value_member(entity *ent, int pos) {
362 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
363 return ent->val_ents[pos+1];
367 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
368 assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
369 ent->values[pos+1] = val;
370 ent->val_ents[pos+1] = member;
374 set_array_entity_values(entity *ent, tarval **values, int num_vals) {
376 ir_graph *rem = current_ir_graph;
377 type *arrtp = get_entity_type(ent);
380 assert(is_array_type(arrtp));
381 assert(get_array_n_dimensions(arrtp) == 1);
382 /* One bound is sufficient, the nunmber of constant fields makes the
384 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
385 assert(get_entity_variability(ent) != uninitialized);
386 current_ir_graph = get_const_code_irg();
388 for (i = 0; i < num_vals; i++) {
389 val = new_Const(get_tv_mode (values[i]), values[i]);
390 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
392 current_ir_graph = rem;
396 get_entity_offset (entity *ent) {
401 set_entity_offset (entity *ent, int offset) {
402 ent->offset = offset;
406 add_entity_overwrites (entity *ent, entity *overwritten) {
408 assert(is_class_type(get_entity_owner(ent)));
409 ARR_APP1 (entity *, ent->overwrites, overwritten);
410 ARR_APP1 (entity *, overwritten->overwrittenby, ent);
414 get_entity_n_overwrites (entity *ent) {
416 assert(is_class_type(get_entity_owner(ent)));
417 return (ARR_LEN (ent->overwrites))-1;
421 get_entity_overwrites (entity *ent, int pos) {
423 assert(is_class_type(get_entity_owner(ent)));
424 assert(pos < get_entity_n_overwrites(ent));
425 return ent->overwrites[pos+1];
429 set_entity_overwrites (entity *ent, int pos, entity *overwritten) {
431 assert(is_class_type(get_entity_owner(ent)));
432 assert(pos < get_entity_n_overwrites(ent));
433 ent->overwrites[pos+1] = overwritten;
437 add_entity_overwrittenby (entity *ent, entity *overwrites) {
439 assert(is_class_type(get_entity_owner(ent)));
440 add_entity_overwrites(overwrites, ent);
444 get_entity_n_overwrittenby (entity *ent) {
446 assert(is_class_type(get_entity_owner(ent)));
447 return (ARR_LEN (ent->overwrittenby))-1;
451 get_entity_overwrittenby (entity *ent, int pos) {
453 assert(is_class_type(get_entity_owner(ent)));
454 assert(pos < get_entity_n_overwrittenby(ent));
455 return ent->overwrittenby[pos+1];
459 set_entity_overwrittenby (entity *ent, int pos, entity *overwrites) {
461 assert(is_class_type(get_entity_owner(ent)));
462 assert(pos < get_entity_n_overwrittenby(ent));
463 ent->overwrittenby[pos+1] = overwrites;
466 /* A link to store intermediate information */
468 get_entity_link(entity *ent) {
474 set_entity_link(entity *ent, void *l) {
480 get_entity_irg(entity *ent) {
482 assert (is_method_type(ent->type));
487 set_entity_irg(entity *ent, ir_graph *irg) {
488 assert (ent && ent->type);
489 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
490 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
491 * aber erhalten bleiben soll. */
493 assert (is_method_type(ent->type));
494 assert (ent->peculiarity == existent);
498 int is_atomic_entity(entity *ent) {
499 type* t = get_entity_type(ent);
500 return (is_primitive_type(t) || is_pointer_type(t) ||
501 is_enumeration_type(t) || is_method_type(t));
504 int is_compound_entity(entity *ent) {
505 type* t = get_entity_type(ent);
506 return (is_class_type(t) || is_struct_type(t) ||
507 is_array_type(t) || is_union_type(t));
510 bool equal_entity(entity *ent1, entity *ent2) {