minor changes to help with making the ajacs-jikes backend
[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 "entity_t.h"
17 # include "mangle.h"
18 # include "typegmod_t.h"
19 # include "array.h"
20 /* All this is needed to build the constant node for methods: */
21 # include "irprog.h"
22 # include "ircons.h"
23
24 /*******************************************************************/
25 /** general                                                       **/
26 /*******************************************************************/
27
28 void
29 init_entity (void)
30 {
31 }
32
33 /*******************************************************************/
34 /** ENTITY                                                        **/
35 /*******************************************************************/
36
37 inline void insert_entity_in_owner (entity *ent) {
38   type *owner = ent->owner;
39   switch (get_type_tpop_code(owner)) {
40   case tpo_class: {
41     add_class_member (owner, ent);
42   } break;
43   case tpo_struct: {
44     add_struct_member (owner, ent);
45   } break;
46   case tpo_union: {
47     add_union_member (owner, ent);
48   } break;
49   case tpo_array: {
50     set_array_element_entity(owner, ent);
51   } break;
52   default: assert(0);
53   }
54 }
55
56 entity *
57 new_entity (type *owner, ident *name, type *type)
58 {
59   entity *res;
60   ir_graph *rem;
61
62   res = (entity *) malloc (sizeof (entity));
63   res->kind = k_entity;
64   assert_legal_owner_of_ent(owner);
65   res->owner = owner;
66   res->name = name;
67   res->type = type;
68   if (get_type_tpop(type) == type_method)
69     res->allocation = static_allocated;
70   else
71     res->allocation = automatic_allocated;
72   res->visibility = local;
73   res->offset = -1;
74   if (is_method_type(type)) {
75     res->variability = constant;
76     rem = current_ir_graph;
77     current_ir_graph = get_const_code_irg();
78     res->value = new_Const(mode_p, tarval_p_from_entity(res));
79     current_ir_graph = rem;
80   } else {
81     res->variability = uninitialized;
82   }
83   res->volatility = non_volatile;
84   res->ld_name = NULL;
85   res->overwrites = NEW_ARR_F(entity *, 1);
86
87   res->visit = 0;
88
89   /* Remember entity in it's owner. */
90   insert_entity_in_owner (res);
91   return res;
92 }
93 inline void free_entity_attrs(entity *ent) {
94   assert(ent);
95   DEL_ARR_F(ent->overwrites);
96 }
97
98 entity *
99 copy_entity_own (entity *old, type *new_owner) {
100   entity *new;
101
102   assert_legal_owner_of_ent(new_owner);
103   if (old->owner == new_owner) return old;
104   new = (entity *) malloc (sizeof (entity));
105   memcpy (new, old, sizeof (entity));
106   new->owner = new_owner;
107   new->overwrites = DUP_ARR_F(entity *, old->overwrites);
108
109   insert_entity_in_owner (new);
110
111   return new;
112 }
113
114 entity *
115 copy_entity_name (entity *old, ident *new_name) {
116   entity *new;
117
118   if (old->name == new_name) return old;
119   new = (entity *) malloc (sizeof (entity));
120   memcpy (new, old, sizeof (entity));
121   new->name = new_name;
122   new->ld_name = NULL;
123   new->overwrites = DUP_ARR_F(entity *, old->overwrites);
124
125   insert_entity_in_owner (new);
126
127   return new;
128 }
129
130 inline const char *
131 get_entity_name (entity *ent) {
132   assert (ent);
133   return id_to_str(get_entity_ident(ent));
134 }
135
136 ident *
137 get_entity_ident    (entity *ent) {
138   assert(ent);
139   return ent->name;
140 }
141
142 /*
143 void   set_entity_ld_name  (entity *, char *ld_name);
144 void   set_entity_ld_ident (entity *, ident *ld_ident);
145 */
146
147 inline type *
148 get_entity_owner (entity *ent) {
149   return ent->owner = skip_tid(ent->owner);
150 }
151
152 inline void
153 set_entity_owner (entity *ent, type *owner) {
154   assert_legal_owner_of_ent(owner);
155   ent->owner = owner;
156 }
157
158 inline void   /* should this go into type.c? */
159 assert_legal_owner_of_ent(type *owner) {
160   assert (get_type_tpop_code(owner) == tpo_class ||
161           get_type_tpop_code(owner) == tpo_union ||
162           get_type_tpop_code(owner) == tpo_struct ||
163           get_type_tpop_code(owner) == tpo_array);   /* Yes, array has an entity
164                                                         -- to select fields! */
165 }
166
167 inline ident *
168 get_entity_ld_ident (entity *ent)
169 {
170   if (ent->ld_name == NULL)
171     ent->ld_name = mangle_entity (ent);
172   return ent->ld_name;
173 }
174
175 inline void
176 set_entity_ld_ident (entity *ent, ident *ld_ident) {
177   ent->ld_name = ld_ident;
178 }
179
180 inline const char *
181 get_entity_ld_name (entity *ent) {
182   return id_to_str(get_entity_ld_ident(ent));
183 }
184
185 /*
186 char  *get_entity_ld_name  (entity *);
187 void   set_entity_ld_name  (entity *, char *ld_name);
188 */
189
190 inline type *
191 get_entity_type (entity *ent) {
192   return ent->type = skip_tid(ent->type);
193 }
194
195 inline void
196 set_entity_type (entity *ent, type *type) {
197   ent->type = type;
198 }
199
200
201 inline ent_allocation
202 get_entity_allocation (entity *ent) {
203   return ent->allocation;
204 }
205
206 inline void
207 set_entity_allocation (entity *ent, ent_allocation al) {
208   ent->allocation = al;
209 }
210
211
212 inline ent_visibility
213 get_entity_visibility (entity *ent) {
214   return ent->visibility;
215 }
216
217 inline void
218 set_entity_visibility (entity *ent, ent_visibility vis) {
219   if (vis != local) assert(ent->allocation == static_allocated);
220   ent->visibility = vis;
221 }
222
223 inline ent_variability
224 get_entity_variability (entity *ent) {
225   return ent->variability;
226 }
227
228 inline void
229 set_entity_variability (entity *ent, ent_variability var){
230   if (var == part_constant)
231     assert(is_class_type(ent->type) || is_struct_type(ent->type));
232   if ((is_compound_type(ent->type)) &&
233       (ent->variability == uninitialized) && (var != uninitialized)) {
234     /* Allocate datastructures for constant values */
235     ent->values = NEW_ARR_F(ir_node *, 1);
236     ent->val_ents = NEW_ARR_F(entity *, 1);
237   }
238   if ((is_compound_type(ent->type)) &&
239       (var == uninitialized) && (ent->variability != uninitialized)) {
240     /* Free datastructures for constant values */
241     DEL_ARR_F(ent->values);
242     DEL_ARR_F(ent->val_ents);
243   }
244   ent->variability = var;
245 }
246
247
248 inline ent_volatility
249 get_entity_volatility (entity *ent) {
250   return ent->volatility;
251 }
252
253 inline void
254 set_entity_volatility (entity *ent, ent_volatility vol) {
255   ent->volatility = vol;
256 }
257
258 /* Set has no effect for entities of type method. */
259 inline ir_node *
260 get_atomic_ent_value(entity *ent) {
261   assert(ent); assert(is_atomic_entity(ent)); assert((ent->variability != uninitialized));
262   return ent->value;
263 }
264
265 inline void
266 set_atomic_ent_value(entity *ent, ir_node *val) {
267   assert(ent && is_atomic_entity(ent) && (ent->variability != uninitialized));
268   if (is_method_type(ent->type)) return;
269   ent->value = val;
270 }
271
272 ir_node *copy_value(ir_node *n) {
273   ir_node *nn;
274   ir_mode *m;
275
276   m = get_irn_mode(n);
277   switch(get_irn_opcode(n)) {
278   case iro_Const:
279     nn = new_Const(m, get_Const_tarval(n)); break;
280   case iro_SymConst:
281     nn = new_SymConst(get_SymConst_type_or_id(n), get_SymConst_kind(n)); break;
282   case iro_Add:
283     nn = new_Add(copy_value(get_Add_left(n)), copy_value(get_Add_right(n)), m); break;
284   default:
285     assert(0 && "opdope invalid or not implemented"); break;
286   }
287   return nn;
288 }
289
290 /* Copies the value represented by the entity to current_block
291    in current_ir_graph. */
292 ir_node *copy_atomic_ent_value(entity *ent) {
293   assert(ent && is_atomic_entity(ent) && (ent->variability != uninitialized));
294   return copy_value(ent->value);
295 }
296
297 /* A value of a compound entity is a pair of value and the corresponding member of
298    the compound. */
299 inline void
300 add_compound_ent_value(entity *ent, ir_node *val, entity *member) {
301   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
302   ARR_APP1 (ir_node *, ent->values, val);
303   ARR_APP1 (entity *, ent->val_ents, member);
304 }
305
306 inline int
307 get_compound_ent_n_values(entity *ent) {
308   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
309   return (ARR_LEN (ent->values))-1;
310 }
311
312 inline ir_node  *
313 get_compound_ent_value(entity *ent, int pos) {
314   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
315   return ent->values[pos+1];
316 }
317
318 /* Copies the value i of the entity to current_block in current_ir_graph. */
319 ir_node *copy_compound_ent_value(entity *ent, int pos) {
320   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
321   return copy_value(ent->values[pos+1]);
322 }
323
324 inline entity   *
325 get_compound_ent_value_member(entity *ent, int pos) {
326   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
327   return ent->val_ents[pos+1];
328 }
329
330 inline void
331 set_compound_ent_value(entity *ent, ir_node *val, entity *member, int pos) {
332   assert(ent && is_compound_entity(ent) && (ent->variability != uninitialized));
333   ent->values[pos+1] = val;
334   ent->val_ents[pos+1] = member;
335 }
336
337 inline int
338 get_entity_offset (entity *ent) {
339   return ent->offset;
340 }
341
342 inline void
343 set_entity_offset (entity *ent, int offset) {
344   ent->offset = offset;
345 }
346
347 inline void
348 add_entity_overwrites   (entity *ent, entity *overwritten) {
349   assert(ent);
350   ARR_APP1 (entity *, ent->overwrites, overwritten);
351 }
352
353 inline int
354 get_entity_n_overwrites (entity *ent){
355   assert(ent);
356   return (ARR_LEN (ent->overwrites))-1;
357 }
358
359 inline entity *
360 get_entity_overwrites   (entity *ent, int pos){
361   assert(ent);
362   return ent->overwrites[pos+1];
363 }
364
365 inline void
366 set_entity_overwrites   (entity *ent, int pos, entity *overwritten) {
367   assert(ent);
368   ent->overwrites[pos+1] = overwritten;
369 }
370
371 /* A link to store intermediate information */
372 void *
373 get_entity_link(entity *ent) {
374   assert(ent);
375   return ent->link;
376 }
377
378 void
379 set_entity_link(entity *ent, void *l) {
380   assert(ent);
381   ent->link = l;
382 }
383
384 inline ir_graph *
385 get_entity_irg(entity *ent) {
386   assert (ent);
387   assert (is_method_type(ent->type));
388   return ent->irg;
389 }
390
391 inline void
392 set_entity_irg(entity *ent, ir_graph *irg) {
393   assert (ent && ent->type);
394   assert (irg);
395   assert (is_method_type(ent->type));
396   ent->irg = irg;
397 }
398
399 int is_atomic_entity(entity *ent) {
400   type* t = get_entity_type(ent);
401   return (is_primitive_type(t) || is_pointer_type(t) ||
402           is_enumeration_type(t) || is_method_type(t));
403 }
404
405 int is_compound_entity(entity *ent) {
406   type* t = get_entity_type(ent);
407   return (is_class_type(t) || is_struct_type(t) ||
408           is_array_type(t) || is_union_type(t));
409 }