2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * File name: ir/tr/entity.c
23 * Purpose: Representation of all program known entities.
24 * Author: Martin Trapp, Christian Schaefer
25 * Modified by: Goetz Lindenmaier, Michael Beck
28 * Copyright: (c) 1998-2007 Universität Karlsruhe
44 #include "firm_common_t.h"
55 /* All this is needed to build the constant node for methods: */
59 #include "irdump.h" /* for output if errors occur. */
61 #include "callgraph.h" /* for dumping debug output */
63 /*******************************************************************/
65 /*******************************************************************/
67 ir_entity *unknown_entity = NULL;
69 ir_entity *get_unknown_entity(void) { return unknown_entity; }
71 #define UNKNOWN_ENTITY_NAME "unknown_entity"
73 /*-----------------------------------------------------------------*/
75 /*-----------------------------------------------------------------*/
78 * Add an entity to it's already set owner type.
80 static INLINE void insert_entity_in_owner(ir_entity *ent) {
81 ir_type *owner = ent->owner;
82 switch (get_type_tpop_code(owner)) {
84 add_class_member(owner, ent);
87 add_struct_member(owner, ent);
90 add_union_member(owner, ent);
93 set_array_element_entity(owner, ent);
97 } /* insert_entity_in_owner */
100 * Creates a new entity. This entity is NOT inserted in the owner type.
102 * @param db debug info for this entity
103 * @param owner the owner type of the new entity
104 * @param name the name of the new entity
105 * @param type the type of the new entity
107 * @return the new created entity
109 static INLINE ir_entity *
110 new_rd_entity(dbg_info *db, ir_type *owner, ident *name, ir_type *type)
115 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
117 res = xmalloc(sizeof(*res));
118 memset(res, 0, sizeof(*res));
120 res->kind = k_entity;
126 res->allocation = allocation_automatic;
127 res->visibility = visibility_local;
128 res->volatility = volatility_non_volatile;
129 res->stickyness = stickyness_unsticky;
130 res->peculiarity = peculiarity_existent;
131 res->address_taken = ir_address_taken_unknown;
133 res->compiler_gen = 0;
135 res->offset_bit_remainder = 0;
137 res->repr_class = NULL;
139 if (is_Method_type(type)) {
142 rem = current_ir_graph;
143 current_ir_graph = get_const_code_irg();
144 res->value = new_SymConst(sym, symconst_addr_ent);
145 current_ir_graph = rem;
146 res->allocation = allocation_static;
147 res->variability = variability_constant;
148 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
149 res->attr.mtd_attr.vtable_number = VTABLE_NUM_NOT_SET;
150 res->attr.mtd_attr.param_access = NULL;
151 res->attr.mtd_attr.param_weight = NULL;
152 res->attr.mtd_attr.irg = NULL;
153 res->attr.mtd_attr.section = section_text;
154 } else if (is_compound_type(type)) {
155 res->variability = variability_uninitialized;
157 res->attr.cmpd_attr.values = NULL;
158 res->attr.cmpd_attr.val_paths = NULL;
160 res->variability = variability_uninitialized;
164 if (is_Class_type(owner)) {
165 res->overwrites = NEW_ARR_F(ir_entity *, 0);
166 res->overwrittenby = NEW_ARR_F(ir_entity *, 0);
168 res->overwrites = NULL;
169 res->overwrittenby = NULL;
173 res->nr = get_irp_new_node_nr();
174 #endif /* DEBUG_libfirm */
177 set_entity_dbg_info(res, db);
180 } /* new_rd_entity */
183 new_d_entity(ir_type *owner, ident *name, ir_type *type, dbg_info *db) {
186 assert(is_compound_type(owner));
187 res = new_rd_entity(db, owner, name, type);
188 /* Remember entity in it's owner. */
189 insert_entity_in_owner(res);
191 hook_new_entity(res);
196 new_entity(ir_type *owner, ident *name, ir_type *type) {
197 return new_d_entity(owner, name, type, NULL);
201 * Free entity attributes.
203 * @param ent the entity
205 static void free_entity_attrs(ir_entity *ent) {
207 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
208 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
209 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
211 assert(ent->overwrites == NULL);
212 assert(ent->overwrittenby == NULL);
214 if (is_compound_entity(ent)) {
215 if (ent->attr.cmpd_attr.val_paths) {
216 for (i = 0; i < get_compound_ent_n_values(ent); i++)
217 if (ent->attr.cmpd_attr.val_paths[i]) {
218 /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ; * @@@ warum nich? */
219 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
220 /* DEL_ARR_F(ent->attr.cmpd_attr.val_paths); */
222 ent->attr.cmpd_attr.val_paths = NULL;
224 /* if (ent->attr.cmpd_attr.values) DEL_ARR_F(ent->attr.cmpd_attr.values); *//* @@@ warum nich? */
225 ent->attr.cmpd_attr.values = NULL;
226 } else if (is_method_entity(ent)) {
227 if (ent->attr.mtd_attr.param_access) {
228 DEL_ARR_F(ent->attr.mtd_attr.param_access);
229 ent->attr.mtd_attr.param_access = NULL;
231 if (ent->attr.mtd_attr.param_weight) {
232 DEL_ARR_F(ent->attr.mtd_attr.param_weight);
233 ent->attr.mtd_attr.param_weight = NULL;
236 } /* free_entity_attrs */
239 copy_entity_own(ir_entity *old, ir_type *new_owner) {
241 assert(is_entity(old));
242 assert(is_compound_type(new_owner));
244 if (old->owner == new_owner) return old;
245 newe = xmalloc(sizeof(*newe));
246 memcpy(newe, old, sizeof(*newe));
247 newe->owner = new_owner;
248 if (is_Class_type(new_owner)) {
249 newe->overwrites = NEW_ARR_F(ir_entity *, 0);
250 newe->overwrittenby = NEW_ARR_F(ir_entity *, 0);
253 newe->nr = get_irp_new_node_nr();
256 insert_entity_in_owner(newe);
259 } /* copy_entity_own */
262 copy_entity_name(ir_entity *old, ident *new_name) {
264 assert(old && old->kind == k_entity);
266 if (old->name == new_name) return old;
267 newe = xmalloc(sizeof(*newe));
268 memcpy(newe, old, sizeof(*newe));
269 newe->name = new_name;
270 newe->ld_name = NULL;
271 if (is_Class_type(newe->owner)) {
272 newe->overwrites = DUP_ARR_F(ir_entity *, old->overwrites);
273 newe->overwrittenby = DUP_ARR_F(ir_entity *, old->overwrittenby);
276 newe->nr = get_irp_new_node_nr();
279 insert_entity_in_owner(newe);
282 } /* copy_entity_name */
286 free_entity(ir_entity *ent) {
287 assert(ent && ent->kind == k_entity);
288 free_entity_attrs(ent);
293 /* Outputs a unique number for this node */
295 get_entity_nr(const ir_entity *ent) {
296 assert(ent && ent->kind == k_entity);
300 return (long)PTR_TO_INT(ent);
302 } /* get_entity_nr */
305 (get_entity_name)(const ir_entity *ent) {
306 return _get_entity_name(ent);
307 } /* get_entity_name */
310 (get_entity_ident)(const ir_entity *ent) {
311 return _get_entity_ident(ent);
312 } /* get_entity_ident */
315 (set_entity_ident)(ir_entity *ent, ident *id) {
316 _set_entity_ident(ent, id);
317 } /* set_entity_ident */
320 (get_entity_owner)(ir_entity *ent) {
321 return _get_entity_owner(ent);
322 } /* get_entity_owner */
325 set_entity_owner(ir_entity *ent, ir_type *owner) {
326 assert(is_entity(ent));
327 assert(is_compound_type(owner));
329 } /* set_entity_owner */
332 (get_entity_ld_ident)(ir_entity *ent) {
333 return _get_entity_ld_ident(ent);
334 } /* get_entity_ld_ident */
337 (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident) {
338 _set_entity_ld_ident(ent, ld_ident);
339 } /* set_entity_ld_ident */
342 (get_entity_ld_name)(ir_entity *ent) {
343 return _get_entity_ld_name(ent);
344 } /* get_entity_ld_name */
347 (get_entity_type)(ir_entity *ent) {
348 return _get_entity_type(ent);
349 } /* get_entity_type */
352 (set_entity_type)(ir_entity *ent, ir_type *type) {
353 _set_entity_type(ent, type);
354 } /* set_entity_type */
357 (get_entity_allocation)(const ir_entity *ent) {
358 return _get_entity_allocation(ent);
359 } /* get_entity_allocation */
362 (set_entity_allocation)(ir_entity *ent, ir_allocation al) {
363 _set_entity_allocation(ent, al);
364 } /* set_entity_allocation */
366 /* return the name of the visibility */
367 const char *get_allocation_name(ir_allocation all)
369 #define X(a) case a: return #a
371 X(allocation_automatic);
372 X(allocation_parameter);
373 X(allocation_dynamic);
374 X(allocation_static);
375 default: return "BAD VALUE";
378 } /* get_allocation_name */
381 (get_entity_visibility)(const ir_entity *ent) {
382 return _get_entity_visibility(ent);
383 } /* get_entity_visibility */
386 set_entity_visibility(ir_entity *ent, ir_visibility vis) {
387 assert(ent && ent->kind == k_entity);
388 if (vis != visibility_local)
389 assert((ent->allocation == allocation_static) ||
390 (ent->allocation == allocation_automatic));
391 /* @@@ Test that the owner type is not local, but how??
392 && get_class_visibility(get_entity_owner(ent)) != local));*/
393 ent->visibility = vis;
394 } /* set_entity_visibility */
396 /* return the name of the visibility */
397 const char *get_visibility_name(ir_visibility vis)
399 #define X(a) case a: return #a
402 X(visibility_external_visible);
403 X(visibility_external_allocated);
404 default: return "BAD VALUE";
407 } /* get_visibility_name */
410 (get_entity_variability)(const ir_entity *ent) {
411 return _get_entity_variability(ent);
412 } /* get_entity_variability */
415 set_entity_variability(ir_entity *ent, ir_variability var)
417 assert(ent && ent->kind == k_entity);
418 if (var == variability_part_constant)
419 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
421 if ((is_compound_type(ent->type)) &&
422 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
423 /* Allocate data structures for constant values */
424 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node *, 0);
425 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
427 if ((is_atomic_type(ent->type)) &&
428 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
429 /* Set default constant value. */
430 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
433 if ((is_compound_type(ent->type)) &&
434 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
435 /* Free data structures for constant values */
436 DEL_ARR_F(ent->attr.cmpd_attr.values); ent->attr.cmpd_attr.values = NULL;
437 DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
439 ent->variability = var;
440 } /* set_entity_variability */
442 /* return the name of the variability */
443 const char *get_variability_name(ir_variability var)
445 #define X(a) case a: return #a
447 X(variability_uninitialized);
448 X(variability_initialized);
449 X(variability_part_constant);
450 X(variability_constant);
451 default: return "BAD VALUE";
454 } /* get_variability_name */
457 (get_entity_volatility)(const ir_entity *ent) {
458 return _get_entity_volatility(ent);
459 } /* get_entity_volatility */
462 (set_entity_volatility)(ir_entity *ent, ir_volatility vol) {
463 _set_entity_volatility(ent, vol);
464 } /* set_entity_volatility */
466 /* return the name of the volatility */
467 const char *get_volatility_name(ir_volatility var)
469 #define X(a) case a: return #a
471 X(volatility_non_volatile);
472 X(volatility_is_volatile);
473 default: return "BAD VALUE";
476 } /* get_volatility_name */
479 (get_entity_peculiarity)(const ir_entity *ent) {
480 return _get_entity_peculiarity(ent);
481 } /* get_entity_peculiarity */
484 (set_entity_peculiarity)(ir_entity *ent, ir_peculiarity pec) {
485 _set_entity_peculiarity(ent, pec);
486 } /* set_entity_peculiarity */
488 /* Checks if an entity cannot be overridden anymore. */
489 int (is_entity_final)(const ir_entity *ent) {
490 return _is_entity_final(ent);
491 } /* is_entity_final */
493 /* Sets/resets the final flag of an entity. */
494 void (set_entity_final)(ir_entity *ent, int final) {
495 _set_entity_final(ent, final);
496 } /* set_entity_final */
498 /* Checks if an entity is compiler generated */
499 int (is_entity_compiler_generated)(const ir_entity *ent) {
500 return _is_entity_compiler_generated(ent);
501 } /* is_entity_compiler_generated */
503 /* Sets/resets the compiler generated flag */
504 void (set_entity_compiler_generated)(ir_entity *ent, int flag) {
505 _set_entity_compiler_generated(ent, flag);
506 } /* set_entity_compiler_generated */
508 /* Checks if the address of an entity was taken. */
509 ir_address_taken_state (get_entity_address_taken)(const ir_entity *ent) {
510 return _get_entity_address_taken(ent);
511 } /* is_entity_address_taken */
513 /* Sets/resets the address taken flag. */
514 void (set_entity_address_taken)(ir_entity *ent, ir_address_taken_state flag) {
515 _set_entity_address_taken(ent, flag);
516 } /* set_entity_address_taken */
518 /* Return the name of the address_taken state. */
519 const char *get_address_taken_state_name(ir_address_taken_state state) {
520 #define X(a) case a: return #a
522 X(ir_address_not_taken);
523 X(ir_address_taken_unknown);
525 default: return "BAD VALUE";
528 } /* get_address_taken_state_name */
530 /* Get the entity's stickyness */
532 (get_entity_stickyness)(const ir_entity *ent) {
533 return _get_entity_stickyness(ent);
534 } /* get_entity_stickyness */
536 /* Set the entity's stickyness */
538 (set_entity_stickyness)(ir_entity *ent, ir_stickyness stickyness) {
539 _set_entity_stickyness(ent, stickyness);
540 } /* set_entity_stickyness */
542 /* Set has no effect for existent entities of type method. */
544 get_atomic_ent_value(ir_entity *ent)
546 assert(ent && is_atomic_entity(ent));
547 assert(ent->variability != variability_uninitialized);
548 return skip_Id(ent->value);
549 } /* get_atomic_ent_value */
552 set_atomic_ent_value(ir_entity *ent, ir_node *val) {
553 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
554 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
557 } /* set_atomic_ent_value */
559 /* Returns true if the the node is representable as code on
561 int is_irn_const_expression(ir_node *n) {
564 /* we are in danger iff an exception will arise. TODO: be more precisely,
565 * for instance Div. will NOT rise if divisor != 0
567 if (is_binop(n) && !is_fragile_op(n))
568 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
571 switch (get_irn_opcode(n)) {
578 return is_irn_const_expression(get_irn_n(n, 0));
583 } /* is_irn_const_expression */
586 * Copies a firm subgraph that complies to the restrictions for
587 * constant expressions to current_block in current_ir_graph.
589 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
593 /* @@@ GL I think we should implement this using the routines from irgopt for
594 dead node elimination/inlineing. */
597 switch (get_irn_opcode(n)) {
599 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
602 nn = new_d_SymConst_type(dbg, get_SymConst_symbol(n), get_SymConst_kind(n),
603 get_SymConst_value_type(n));
606 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
607 copy_const_value(dbg, get_Add_right(n)), m); break;
609 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
610 copy_const_value(dbg, get_Sub_right(n)), m); break;
612 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
613 copy_const_value(dbg, get_Mul_right(n)), m); break;
615 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
616 copy_const_value(dbg, get_And_right(n)), m); break;
618 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
619 copy_const_value(dbg, get_Or_right(n)), m); break;
621 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
622 copy_const_value(dbg, get_Eor_right(n)), m); break;
624 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
626 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
628 nn = new_d_Unknown(m); break;
631 assert(0 && "opcode invalid or not implemented");
636 } /* copy_const_value */
638 /* Creates a new compound graph path. */
639 compound_graph_path *
640 new_compound_graph_path(ir_type *tp, int length) {
641 compound_graph_path *res;
643 assert(is_compound_type(tp));
646 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
647 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
648 res->kind = k_ir_compound_graph_path;
653 } /* new_compound_graph_path */
655 /* Frees an graph path object */
656 void free_compound_graph_path (compound_graph_path *gr) {
657 assert(gr && is_compound_graph_path(gr));
660 } /* free_compound_graph_path */
662 /* Returns non-zero if an object is a compound graph path */
663 int is_compound_graph_path(const void *thing) {
664 return (get_kind(thing) == k_ir_compound_graph_path);
665 } /* is_compound_graph_path */
667 /* Checks whether the path up to pos is correct. If the path contains a NULL,
668 * assumes the path is not complete and returns 'true'. */
669 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
672 ir_type *owner = gr->tp;
674 for (i = 0; i <= pos; i++) {
675 node = get_compound_graph_path_node(gr, i);
677 /* Path not yet complete. */
679 if (get_entity_owner(node) != owner)
681 owner = get_entity_type(node);
683 if (pos == get_compound_graph_path_length(gr))
684 if (!is_atomic_type(owner))
687 } /* is_proper_compound_graph_path */
689 /* Returns the length of a graph path */
690 int get_compound_graph_path_length(const compound_graph_path *gr) {
691 assert(gr && is_compound_graph_path(gr));
693 } /* get_compound_graph_path_length */
696 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
697 assert(gr && is_compound_graph_path(gr));
698 assert(pos >= 0 && pos < gr->len);
699 return gr->list[pos].node;
700 } /* get_compound_graph_path_node */
703 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
704 assert(gr && is_compound_graph_path(gr));
705 assert(pos >= 0 && pos < gr->len);
706 assert(is_entity(node));
707 gr->list[pos].node = node;
708 assert(is_proper_compound_graph_path(gr, pos));
709 } /* set_compound_graph_path_node */
712 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
713 assert(gr && is_compound_graph_path(gr));
714 assert(pos >= 0 && pos < gr->len);
715 return gr->list[pos].index;
716 } /* get_compound_graph_path_array_index */
719 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
720 assert(gr && is_compound_graph_path(gr));
721 assert(pos >= 0 && pos < gr->len);
722 gr->list[pos].index = index;
723 } /* set_compound_graph_path_array_index */
725 /* A value of a compound entity is a pair of value and the corresponding path to a member of
728 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
729 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
730 assert(is_compound_graph_path(path));
731 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
732 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
733 } /* add_compound_ent_value_w_path */
736 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
737 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
738 assert(is_compound_graph_path(path));
739 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
740 ent->attr.cmpd_attr.values[pos] = val;
741 ent->attr.cmpd_attr.val_paths[pos] = path;
742 } /* set_compound_ent_value_w_path */
745 get_compound_ent_n_values(ir_entity *ent) {
746 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
747 return ARR_LEN(ent->attr.cmpd_attr.values);
748 } /* get_compound_ent_n_values */
751 get_compound_ent_value(ir_entity *ent, int pos) {
752 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
753 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
754 return ent->attr.cmpd_attr.values[pos];
755 } /* get_compound_ent_value */
757 compound_graph_path *
758 get_compound_ent_value_path(ir_entity *ent, int pos) {
759 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
760 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
761 return ent->attr.cmpd_attr.val_paths[pos];
762 } /* get_compound_ent_value_path */
765 * Returns non-zero, if two compound_graph_pathes are equal
767 * @param path1 the first path
768 * @param visited_indices
769 * @param path2 the second path
771 static int equal_paths(compound_graph_path *path1, int *visited_indices, compound_graph_path *path2) {
773 int len1 = get_compound_graph_path_length(path1);
774 int len2 = get_compound_graph_path_length(path2);
776 if (len2 != len1) return 0;
778 for (i = 0; i < len1; i++) {
780 ir_entity *node1 = get_compound_graph_path_node(path1, i);
781 ir_entity *node2 = get_compound_graph_path_node(path2, i);
783 if (node1 != node2) return 0;
785 /* FIXME: Strange code. What is it good for? */
786 tp = get_entity_owner(node1);
787 if (is_Array_type(tp)) {
790 /* Compute the index of this node. */
791 assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
793 low = get_array_lower_bound_int(tp, 0);
794 if (low + visited_indices[i] < get_compound_graph_path_array_index(path2, i)) {
795 visited_indices[i]++;
798 assert(low + visited_indices[i] == get_compound_graph_path_array_index(path2, i));
805 * Returns the position of a value with the given path.
806 * The path must contain array indices for all array element entities.
808 * @todo This implementation is very low and should be replaced when the new tree oriented
809 * value representation is finally implemented.
811 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
812 int i, n_paths = get_compound_ent_n_values(ent);
813 int *visited_indices;
814 int path_len = get_compound_graph_path_length(path);
816 NEW_ARR_A(int *, visited_indices, path_len);
817 memset(visited_indices, 0, sizeof(*visited_indices) * path_len);
818 for (i = 0; i < n_paths; i ++) {
819 if (equal_paths(get_compound_ent_value_path(ent, i), visited_indices, path))
826 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
827 printf("Entity %s : ", get_entity_name(ent));
828 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
829 ir_entity *node = get_compound_graph_path_node(path, j);
830 printf("%s", get_entity_name(node));
831 if (is_Array_type(get_entity_owner(node)))
832 printf("[%d]", get_compound_graph_path_array_index(path, j));
834 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
838 assert(0 && "path not found");
840 } /* get_compound_ent_pos_by_path */
842 /* Returns a constant value given the access path.
843 * The path must contain array indices for all array element entities. */
844 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
845 return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
846 } /* get_compound_ent_value_by_path */
850 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
852 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
853 for (i = 0; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths)); ++i) {
854 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
855 if (path->list[path->len-1].node == value_ent) {
856 for (; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths))-1; ++i) {
857 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
858 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
860 ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, ARR_LEN(ent->attr.cmpd_attr.val_paths) - 1);
861 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, ARR_LEN(ent->attr.cmpd_attr.values) - 1);
865 } /* remove_compound_ent_value */
868 add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member) {
869 compound_graph_path *path;
870 ir_type *owner_tp = get_entity_owner(member);
871 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
872 path = new_compound_graph_path(get_entity_type(ent), 1);
873 path->list[0].node = member;
874 if (is_Array_type(owner_tp)) {
878 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
879 max = get_array_lower_bound_int(owner_tp, 0) -1;
880 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
881 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
886 path->list[0].index = max + 1;
888 add_compound_ent_value_w_path(ent, val, path);
889 } /* add_compound_ent_value */
893 get_compound_ent_value_member(ir_entity *ent, int pos) {
894 compound_graph_path *path;
895 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
896 path = get_compound_ent_value_path(ent, pos);
898 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
899 } /* get_compound_ent_value_member */
902 set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos) {
903 compound_graph_path *path;
904 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
905 path = get_compound_ent_value_path(ent, pos);
906 set_compound_graph_path_node(path, 0, member);
907 set_compound_ent_value_w_path(ent, val, path, pos);
908 } /* set_compound_ent_value */
911 set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
913 ir_graph *rem = current_ir_graph;
914 ir_type *arrtp = get_entity_type(ent);
916 ir_type *elttp = get_array_element_type(arrtp);
918 assert(is_Array_type(arrtp));
919 assert(get_array_n_dimensions(arrtp) == 1);
920 /* One bound is sufficient, the number of constant fields makes the
922 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
923 assert(get_entity_variability(ent) != variability_uninitialized);
924 current_ir_graph = get_const_code_irg();
926 for (i = 0; i < num_vals; i++) {
927 val = new_Const_type(values[i], elttp);
928 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
929 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
931 current_ir_graph = rem;
932 } /* set_array_entity_values */
934 /* Return the overall offset of value at position pos in bytes. */
935 int get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
936 compound_graph_path *path;
941 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
943 path = get_compound_ent_value_path(ent, pos);
944 path_len = get_compound_graph_path_length(path);
947 for (i = 0; i < path_len; ++i) {
948 ir_entity *node = get_compound_graph_path_node(path, i);
949 ir_type *node_tp = get_entity_type(node);
951 if (is_Array_type(curr_tp)) {
952 int size = get_type_size_bits(node_tp);
953 int align = get_type_alignment_bits(node_tp);
957 if(size % align > 0) {
958 size += align - (size % align);
960 assert(size % 8 == 0);
962 idx = get_compound_graph_path_array_index(path, i);
964 offset += size * idx;
966 offset += get_entity_offset(node);
972 } /* get_compound_ent_value_offset_bytes */
974 /* Return the offset in bits from the last byte address. */
975 int get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
976 compound_graph_path *path;
978 ir_entity *last_node;
980 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
982 path = get_compound_ent_value_path(ent, pos);
983 path_len = get_compound_graph_path_length(path);
984 last_node = get_compound_graph_path_node(path, path_len - 1);
986 return get_entity_offset_bits_remainder(last_node);
987 } /* get_compound_ent_value_offset_bit_remainder */
990 (get_entity_offset)(const ir_entity *ent) {
991 return _get_entity_offset(ent);
992 } /* get_entity_offset */
995 (set_entity_offset)(ir_entity *ent, int offset) {
996 _set_entity_offset(ent, offset);
997 } /* set_entity_offset */
1000 (get_entity_offset_bits_remainder)(const ir_entity *ent) {
1001 return _get_entity_offset_bits_remainder(ent);
1002 } /* get_entity_offset_bits_remainder */
1005 (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
1006 _set_entity_offset_bits_remainder(ent, offset);
1007 } /* set_entity_offset_bits_remainder */
1010 add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1012 ir_type *owner = get_entity_owner(ent);
1013 ir_type *ovw_ovner = get_entity_owner(overwritten);
1014 assert(is_Class_type(owner));
1015 assert(is_Class_type(ovw_ovner));
1016 assert(! is_class_final(ovw_ovner));
1018 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
1019 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
1020 } /* add_entity_overwrites */
1023 get_entity_n_overwrites(ir_entity *ent) {
1024 assert(is_Class_type(get_entity_owner(ent)));
1025 return (ARR_LEN(ent->overwrites));
1026 } /* get_entity_n_overwrites */
1029 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
1031 assert(is_Class_type(get_entity_owner(ent)));
1032 for (i = 0; i < get_entity_n_overwrites(ent); i++)
1033 if (get_entity_overwrites(ent, i) == overwritten)
1036 } /* get_entity_overwrites_index */
1039 get_entity_overwrites(ir_entity *ent, int pos) {
1040 assert(is_Class_type(get_entity_owner(ent)));
1041 assert(pos < get_entity_n_overwrites(ent));
1042 return ent->overwrites[pos];
1043 } /* get_entity_overwrites */
1046 set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
1047 assert(is_Class_type(get_entity_owner(ent)));
1048 assert(pos < get_entity_n_overwrites(ent));
1049 ent->overwrites[pos] = overwritten;
1050 } /* set_entity_overwrites */
1053 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1055 assert(is_Class_type(get_entity_owner(ent)));
1056 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1057 if (ent->overwrites[i] == overwritten) {
1058 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1059 ent->overwrites[i] = ent->overwrites[i+1];
1060 ARR_SETLEN(ir_entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1063 } /* remove_entity_overwrites */
1066 add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1067 add_entity_overwrites(overwrites, ent);
1068 } /* add_entity_overwrittenby */
1071 get_entity_n_overwrittenby(ir_entity *ent) {
1072 assert(is_Class_type(get_entity_owner(ent)));
1073 return (ARR_LEN (ent->overwrittenby));
1074 } /* get_entity_n_overwrittenby */
1077 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
1079 assert(is_Class_type(get_entity_owner(ent)));
1080 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1081 if (get_entity_overwrittenby(ent, i) == overwrites)
1084 } /* get_entity_overwrittenby_index */
1087 get_entity_overwrittenby(ir_entity *ent, int pos) {
1088 assert(is_Class_type(get_entity_owner(ent)));
1089 assert(pos < get_entity_n_overwrittenby(ent));
1090 return ent->overwrittenby[pos];
1091 } /* get_entity_overwrittenby */
1094 set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
1095 assert(is_Class_type(get_entity_owner(ent)));
1096 assert(pos < get_entity_n_overwrittenby(ent));
1097 ent->overwrittenby[pos] = overwrites;
1098 } /* set_entity_overwrittenby */
1100 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1102 assert(is_Class_type(get_entity_owner(ent)));
1103 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1104 if (ent->overwrittenby[i] == overwrites) {
1105 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1106 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1107 ARR_SETLEN(ir_entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1110 } /* remove_entity_overwrittenby */
1112 /* A link to store intermediate information */
1114 (get_entity_link)(const ir_entity *ent) {
1115 return _get_entity_link(ent);
1116 } /* get_entity_link */
1119 (set_entity_link)(ir_entity *ent, void *l) {
1120 _set_entity_link(ent, l);
1121 } /* set_entity_link */
1124 (get_entity_irg)(const ir_entity *ent) {
1125 return _get_entity_irg(ent);
1126 } /* get_entity_irg */
1129 set_entity_irg(ir_entity *ent, ir_graph *irg) {
1130 assert(is_method_entity(ent));
1131 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1132 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1133 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1136 assert((irg && ent->peculiarity == peculiarity_existent) ||
1137 (!irg && (ent->peculiarity == peculiarity_existent)
1138 && (ent -> visibility == visibility_external_allocated)) ||
1139 (!irg && ent->peculiarity == peculiarity_description) ||
1140 (!irg && ent->peculiarity == peculiarity_inherited));
1141 ent->attr.mtd_attr.irg = irg;
1142 } /* set_entity_irg */
1144 unsigned get_entity_vtable_number(const ir_entity *ent) {
1145 assert(is_method_entity((ir_entity *)ent));
1146 return ent->attr.mtd_attr.vtable_number;
1147 } /* get_entity_vtable_number */
1149 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
1150 assert(is_method_entity(ent));
1151 ent->attr.mtd_attr.vtable_number = vtable_number;
1152 } /* set_entity_vtable_number */
1154 /* Returns the section of a method. */
1155 ir_img_section get_method_img_section(const ir_entity *ent) {
1156 assert(is_method_entity((ir_entity *)ent));
1157 return ent->attr.mtd_attr.section;
1158 } /* get_method_img_section */
1160 /* Sets the section of a method. */
1161 void set_method_img_section(ir_entity *ent, ir_img_section section) {
1162 assert(is_method_entity(ent));
1163 ent->attr.mtd_attr.section = section;
1164 } /* set_method_img_section */
1167 (is_entity)(const void *thing) {
1168 return _is_entity(thing);
1171 int is_atomic_entity(ir_entity *ent) {
1172 ir_type *t = get_entity_type(ent);
1173 const tp_op *op = get_type_tpop(t);
1174 return (op == type_primitive || op == type_pointer ||
1175 op == type_enumeration || op == type_method);
1176 } /* is_atomic_entity */
1178 int is_compound_entity(ir_entity *ent) {
1179 ir_type *t = get_entity_type(ent);
1180 const tp_op *op = get_type_tpop(t);
1181 return (op == type_class || op == type_struct ||
1182 op == type_array || op == type_union);
1183 } /* is_compound_entity */
1185 int is_method_entity(ir_entity *ent) {
1186 ir_type *t = get_entity_type(ent);
1187 return is_Method_type(t);
1188 } /* is_method_entity */
1191 * @todo not implemented!!! */
1192 int equal_entity(ir_entity *ent1, ir_entity *ent2) {
1193 fprintf(stderr, " calling unimplemented equal entity!!! \n");
1195 } /* equal_entity */
1198 unsigned long (get_entity_visited)(ir_entity *ent) {
1199 return _get_entity_visited(ent);
1200 } /* get_entity_visited */
1202 void (set_entity_visited)(ir_entity *ent, unsigned long num) {
1203 _set_entity_visited(ent, num);
1204 } /* set_entity_visited */
1206 /* Sets visited field in ir_entity to entity_visited. */
1207 void (mark_entity_visited)(ir_entity *ent) {
1208 _mark_entity_visited(ent);
1209 } /* mark_entity_visited */
1211 int (entity_visited)(ir_entity *ent) {
1212 return _entity_visited(ent);
1213 } /* entity_visited */
1215 int (entity_not_visited)(ir_entity *ent) {
1216 return _entity_not_visited(ent);
1217 } /* entity_not_visited */
1219 /* Returns the mask of the additional entity properties. */
1220 unsigned get_entity_additional_properties(ir_entity *ent) {
1223 assert(is_method_entity(ent));
1225 /* first check, if the graph has additional properties */
1226 irg = get_entity_irg(ent);
1229 return get_irg_additional_properties(irg);
1231 if (ent->attr.mtd_attr.irg_add_properties & mtp_property_inherited)
1232 return get_method_additional_properties(get_entity_type(ent));
1234 return ent->attr.mtd_attr.irg_add_properties;
1235 } /* get_entity_additional_properties */
1237 /* Sets the mask of the additional graph properties. */
1238 void set_entity_additional_properties(ir_entity *ent, unsigned property_mask)
1242 assert(is_method_entity(ent));
1244 /* first check, if the graph exists */
1245 irg = get_entity_irg(ent);
1247 set_irg_additional_properties(irg, property_mask);
1249 /* do not allow to set the mtp_property_inherited flag or
1250 * the automatic inheritance of flags will not work */
1251 ent->attr.mtd_attr.irg_add_properties = property_mask & ~mtp_property_inherited;
1253 } /* set_entity_additional_properties */
1255 /* Sets one additional graph property. */
1256 void set_entity_additional_property(ir_entity *ent, mtp_additional_property flag)
1260 assert(is_method_entity(ent));
1262 /* first check, if the graph exists */
1263 irg = get_entity_irg(ent);
1265 set_irg_additional_property(irg, flag);
1267 unsigned mask = ent->attr.mtd_attr.irg_add_properties;
1269 if (mask & mtp_property_inherited)
1270 mask = get_method_additional_properties(get_entity_type(ent));
1272 /* do not allow to set the mtp_property_inherited flag or
1273 * the automatic inheritance of flags will not work */
1274 ent->attr.mtd_attr.irg_add_properties = mask | (flag & ~mtp_property_inherited);
1276 } /* set_entity_additional_property */
1278 /* Returns the class type that this type info entity represents or NULL
1279 if ent is no type info entity. */
1280 ir_type *(get_entity_repr_class)(const ir_entity *ent) {
1281 return _get_entity_repr_class(ent);
1282 } /* get_entity_repr_class */
1284 /* Initialize entity module. */
1285 void firm_init_entity(void)
1287 symconst_symbol sym;
1289 assert(firm_unknown_type && "Call init_type() before firm_init_entity()!");
1290 assert(!unknown_entity && "Call firm_init_entity() only once!");
1292 unknown_entity = new_rd_entity(NULL, firm_unknown_type, new_id_from_str(UNKNOWN_ENTITY_NAME), firm_unknown_type);
1293 set_entity_visibility(unknown_entity, visibility_external_allocated);
1294 set_entity_ld_ident(unknown_entity, get_entity_ident(unknown_entity));
1296 current_ir_graph = get_const_code_irg();
1297 sym.entity_p = unknown_entity;
1298 unknown_entity->value = new_SymConst(sym, symconst_addr_ent);
1299 } /* firm_init_entity */