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 * @brief Representation of all program known entities.
23 * @author Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
34 #include "firm_common_t.h"
43 /* All this is needed to build the constant node for methods: */
47 #include "irdump.h" /* for output if errors occur. */
49 #include "callgraph.h" /* for dumping debug output */
51 /*******************************************************************/
53 /*******************************************************************/
55 ir_entity *unknown_entity = NULL;
57 ir_entity *get_unknown_entity(void) { return unknown_entity; }
59 #define UNKNOWN_ENTITY_NAME "unknown_entity"
61 /*-----------------------------------------------------------------*/
63 /*-----------------------------------------------------------------*/
66 * Add an entity to it's already set owner type.
68 static INLINE void insert_entity_in_owner(ir_entity *ent) {
69 ir_type *owner = ent->owner;
70 switch (get_type_tpop_code(owner)) {
72 add_class_member(owner, ent);
75 add_struct_member(owner, ent);
78 add_union_member(owner, ent);
81 set_array_element_entity(owner, ent);
85 } /* insert_entity_in_owner */
88 * Creates a new entity. This entity is NOT inserted in the owner type.
90 * @param db debug info for this entity
91 * @param owner the owner type of the new entity
92 * @param name the name of the new entity
93 * @param type the type of the new entity
95 * @return the new created entity
97 static INLINE ir_entity *
98 new_rd_entity(dbg_info *db, ir_type *owner, ident *name, ir_type *type)
103 assert(!id_contains_char(name, ' ') && "entity name should not contain spaces");
105 res = xmalloc(sizeof(*res));
106 memset(res, 0, sizeof(*res));
108 res->kind = k_entity;
114 res->allocation = allocation_automatic;
115 res->visibility = visibility_local;
116 res->volatility = volatility_non_volatile;
117 res->stickyness = stickyness_unsticky;
118 res->peculiarity = peculiarity_existent;
119 res->address_taken = ir_address_taken_unknown;
121 res->compiler_gen = 0;
122 res->backend_marked = 0;
124 res->offset_bit_remainder = 0;
126 res->repr_class = NULL;
128 if (is_Method_type(type)) {
131 rem = current_ir_graph;
132 current_ir_graph = get_const_code_irg();
133 res->value = new_SymConst(sym, symconst_addr_ent);
134 current_ir_graph = rem;
135 res->allocation = allocation_static;
136 res->variability = variability_constant;
137 res->attr.mtd_attr.irg_add_properties = mtp_property_inherited;
138 res->attr.mtd_attr.vtable_number = VTABLE_NUM_NOT_SET;
139 res->attr.mtd_attr.param_access = NULL;
140 res->attr.mtd_attr.param_weight = NULL;
141 res->attr.mtd_attr.irg = NULL;
142 res->attr.mtd_attr.section = section_text;
143 } else if (is_compound_type(type)) {
144 res->variability = variability_uninitialized;
146 res->attr.cmpd_attr.values = NULL;
147 res->attr.cmpd_attr.val_paths = NULL;
149 res->variability = variability_uninitialized;
153 if (is_Class_type(owner)) {
154 res->overwrites = NEW_ARR_F(ir_entity *, 0);
155 res->overwrittenby = NEW_ARR_F(ir_entity *, 0);
157 res->overwrites = NULL;
158 res->overwrittenby = NULL;
162 res->nr = get_irp_new_node_nr();
163 #endif /* DEBUG_libfirm */
166 set_entity_dbg_info(res, db);
169 } /* new_rd_entity */
172 new_d_entity(ir_type *owner, ident *name, ir_type *type, dbg_info *db) {
175 assert(is_compound_type(owner));
176 res = new_rd_entity(db, owner, name, type);
177 /* Remember entity in it's owner. */
178 insert_entity_in_owner(res);
180 hook_new_entity(res);
185 new_entity(ir_type *owner, ident *name, ir_type *type) {
186 return new_d_entity(owner, name, type, NULL);
190 * Free entity attributes.
192 * @param ent the entity
194 static void free_entity_attrs(ir_entity *ent) {
196 if (get_type_tpop(get_entity_owner(ent)) == type_class) {
197 DEL_ARR_F(ent->overwrites); ent->overwrites = NULL;
198 DEL_ARR_F(ent->overwrittenby); ent->overwrittenby = NULL;
200 assert(ent->overwrites == NULL);
201 assert(ent->overwrittenby == NULL);
203 if (is_compound_entity(ent)) {
204 if (ent->attr.cmpd_attr.val_paths) {
205 for (i = 0; i < get_compound_ent_n_values(ent); i++)
206 if (ent->attr.cmpd_attr.val_paths[i]) {
207 /* free_compound_graph_path(ent->attr.cmpd_attr.val_paths[i]) ; * @@@ warum nich? */
208 /* Geht nich: wird mehrfach verwendet!!! ==> mehrfach frei gegeben. */
209 /* DEL_ARR_F(ent->attr.cmpd_attr.val_paths); */
211 ent->attr.cmpd_attr.val_paths = NULL;
213 /* if (ent->attr.cmpd_attr.values) DEL_ARR_F(ent->attr.cmpd_attr.values); *//* @@@ warum nich? */
214 ent->attr.cmpd_attr.values = NULL;
215 } else if (is_method_entity(ent)) {
216 if (ent->attr.mtd_attr.param_access) {
217 DEL_ARR_F(ent->attr.mtd_attr.param_access);
218 ent->attr.mtd_attr.param_access = NULL;
220 if (ent->attr.mtd_attr.param_weight) {
221 DEL_ARR_F(ent->attr.mtd_attr.param_weight);
222 ent->attr.mtd_attr.param_weight = NULL;
225 } /* free_entity_attrs */
228 copy_entity_own(ir_entity *old, ir_type *new_owner) {
230 assert(is_entity(old));
231 assert(is_compound_type(new_owner));
233 if (old->owner == new_owner) return old;
234 newe = xmalloc(sizeof(*newe));
235 memcpy(newe, old, sizeof(*newe));
236 newe->owner = new_owner;
237 if (is_Class_type(new_owner)) {
238 newe->overwrites = NEW_ARR_F(ir_entity *, 0);
239 newe->overwrittenby = NEW_ARR_F(ir_entity *, 0);
242 newe->nr = get_irp_new_node_nr();
245 insert_entity_in_owner(newe);
248 } /* copy_entity_own */
251 copy_entity_name(ir_entity *old, ident *new_name) {
253 assert(old && old->kind == k_entity);
255 if (old->name == new_name) return old;
256 newe = xmalloc(sizeof(*newe));
257 memcpy(newe, old, sizeof(*newe));
258 newe->name = new_name;
259 newe->ld_name = NULL;
260 if (is_Class_type(newe->owner)) {
261 newe->overwrites = DUP_ARR_F(ir_entity *, old->overwrites);
262 newe->overwrittenby = DUP_ARR_F(ir_entity *, old->overwrittenby);
265 newe->nr = get_irp_new_node_nr();
268 insert_entity_in_owner(newe);
271 } /* copy_entity_name */
275 free_entity(ir_entity *ent) {
276 assert(ent && ent->kind == k_entity);
277 free_entity_attrs(ent);
282 /* Outputs a unique number for this node */
284 get_entity_nr(const ir_entity *ent) {
285 assert(ent && ent->kind == k_entity);
289 return (long)PTR_TO_INT(ent);
291 } /* get_entity_nr */
294 (get_entity_name)(const ir_entity *ent) {
295 return _get_entity_name(ent);
296 } /* get_entity_name */
299 (get_entity_ident)(const ir_entity *ent) {
300 return _get_entity_ident(ent);
301 } /* get_entity_ident */
304 (set_entity_ident)(ir_entity *ent, ident *id) {
305 _set_entity_ident(ent, id);
306 } /* set_entity_ident */
309 (get_entity_owner)(ir_entity *ent) {
310 return _get_entity_owner(ent);
311 } /* get_entity_owner */
314 set_entity_owner(ir_entity *ent, ir_type *owner) {
315 assert(is_entity(ent));
316 assert(is_compound_type(owner));
318 } /* set_entity_owner */
321 (get_entity_ld_ident)(ir_entity *ent) {
322 return _get_entity_ld_ident(ent);
323 } /* get_entity_ld_ident */
326 (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident) {
327 _set_entity_ld_ident(ent, ld_ident);
328 } /* set_entity_ld_ident */
331 (get_entity_ld_name)(ir_entity *ent) {
332 return _get_entity_ld_name(ent);
333 } /* get_entity_ld_name */
336 (get_entity_type)(ir_entity *ent) {
337 return _get_entity_type(ent);
338 } /* get_entity_type */
341 (set_entity_type)(ir_entity *ent, ir_type *type) {
342 _set_entity_type(ent, type);
343 } /* set_entity_type */
346 (get_entity_allocation)(const ir_entity *ent) {
347 return _get_entity_allocation(ent);
348 } /* get_entity_allocation */
351 (set_entity_allocation)(ir_entity *ent, ir_allocation al) {
352 _set_entity_allocation(ent, al);
353 } /* set_entity_allocation */
355 /* return the name of the visibility */
356 const char *get_allocation_name(ir_allocation all)
358 #define X(a) case a: return #a
360 X(allocation_automatic);
361 X(allocation_parameter);
362 X(allocation_dynamic);
363 X(allocation_static);
364 default: return "BAD VALUE";
367 } /* get_allocation_name */
370 (get_entity_visibility)(const ir_entity *ent) {
371 return _get_entity_visibility(ent);
372 } /* get_entity_visibility */
375 set_entity_visibility(ir_entity *ent, ir_visibility vis) {
376 assert(ent && ent->kind == k_entity);
377 if (vis != visibility_local)
378 assert((ent->allocation == allocation_static) ||
379 (ent->allocation == allocation_automatic));
380 /* @@@ Test that the owner type is not local, but how??
381 && get_class_visibility(get_entity_owner(ent)) != local));*/
382 ent->visibility = vis;
383 } /* set_entity_visibility */
385 /* return the name of the visibility */
386 const char *get_visibility_name(ir_visibility vis)
388 #define X(a) case a: return #a
391 X(visibility_external_visible);
392 X(visibility_external_allocated);
393 default: return "BAD VALUE";
396 } /* get_visibility_name */
399 (get_entity_variability)(const ir_entity *ent) {
400 return _get_entity_variability(ent);
401 } /* get_entity_variability */
404 set_entity_variability(ir_entity *ent, ir_variability var)
406 assert(ent && ent->kind == k_entity);
407 if (var == variability_part_constant)
408 assert(is_Class_type(ent->type) || is_Struct_type(ent->type));
410 if ((is_compound_type(ent->type)) &&
411 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
412 /* Allocate data structures for constant values */
413 ent->attr.cmpd_attr.values = NEW_ARR_F(ir_node *, 0);
414 ent->attr.cmpd_attr.val_paths = NEW_ARR_F(compound_graph_path *, 0);
416 if ((is_atomic_type(ent->type)) &&
417 (ent->variability == variability_uninitialized) && (var != variability_uninitialized)) {
418 /* Set default constant value. */
419 ent->value = new_rd_Unknown(get_const_code_irg(), get_type_mode(ent->type));
422 if ((is_compound_type(ent->type)) &&
423 (var == variability_uninitialized) && (ent->variability != variability_uninitialized)) {
424 /* Free data structures for constant values */
425 DEL_ARR_F(ent->attr.cmpd_attr.values); ent->attr.cmpd_attr.values = NULL;
426 DEL_ARR_F(ent->attr.cmpd_attr.val_paths); ent->attr.cmpd_attr.val_paths = NULL;
428 ent->variability = var;
429 } /* set_entity_variability */
431 /* return the name of the variability */
432 const char *get_variability_name(ir_variability var)
434 #define X(a) case a: return #a
436 X(variability_uninitialized);
437 X(variability_initialized);
438 X(variability_part_constant);
439 X(variability_constant);
440 default: return "BAD VALUE";
443 } /* get_variability_name */
446 (get_entity_volatility)(const ir_entity *ent) {
447 return _get_entity_volatility(ent);
448 } /* get_entity_volatility */
451 (set_entity_volatility)(ir_entity *ent, ir_volatility vol) {
452 _set_entity_volatility(ent, vol);
453 } /* set_entity_volatility */
455 /* return the name of the volatility */
456 const char *get_volatility_name(ir_volatility var)
458 #define X(a) case a: return #a
460 X(volatility_non_volatile);
461 X(volatility_is_volatile);
462 default: return "BAD VALUE";
465 } /* get_volatility_name */
468 (get_entity_peculiarity)(const ir_entity *ent) {
469 return _get_entity_peculiarity(ent);
470 } /* get_entity_peculiarity */
473 (set_entity_peculiarity)(ir_entity *ent, ir_peculiarity pec) {
474 _set_entity_peculiarity(ent, pec);
475 } /* set_entity_peculiarity */
477 /* Checks if an entity cannot be overridden anymore. */
478 int (is_entity_final)(const ir_entity *ent) {
479 return _is_entity_final(ent);
480 } /* is_entity_final */
482 /* Sets/resets the final flag of an entity. */
483 void (set_entity_final)(ir_entity *ent, int final) {
484 _set_entity_final(ent, final);
485 } /* set_entity_final */
487 /* Checks if an entity is compiler generated */
488 int (is_entity_compiler_generated)(const ir_entity *ent) {
489 return _is_entity_compiler_generated(ent);
490 } /* is_entity_compiler_generated */
492 /* Sets/resets the compiler generated flag */
493 void (set_entity_compiler_generated)(ir_entity *ent, int flag) {
494 _set_entity_compiler_generated(ent, flag);
495 } /* set_entity_compiler_generated */
497 /* Checks if an entity is marked by the backend */
498 int (is_entity_backend_marked)(const ir_entity *ent) {
499 return _is_entity_backend_marked(ent);
500 } /* is_entity_backend_marked */
502 /* Sets/resets the compiler generated flag */
503 void (set_entity_backend_marked)(ir_entity *ent, int flag) {
504 _set_entity_backend_marked(ent, flag);
505 } /* set_entity_backend_marked */
507 /* Checks if the address of an entity was taken. */
508 ir_address_taken_state (get_entity_address_taken)(const ir_entity *ent) {
509 return _get_entity_address_taken(ent);
510 } /* is_entity_address_taken */
512 /* Sets/resets the address taken flag. */
513 void (set_entity_address_taken)(ir_entity *ent, ir_address_taken_state flag) {
514 _set_entity_address_taken(ent, flag);
515 } /* set_entity_address_taken */
517 /* Return the name of the address_taken state. */
518 const char *get_address_taken_state_name(ir_address_taken_state state) {
519 #define X(a) case a: return #a
521 X(ir_address_not_taken);
522 X(ir_address_taken_unknown);
524 default: return "BAD VALUE";
527 } /* get_address_taken_state_name */
529 /* Get the entity's stickyness */
531 (get_entity_stickyness)(const ir_entity *ent) {
532 return _get_entity_stickyness(ent);
533 } /* get_entity_stickyness */
535 /* Set the entity's stickyness */
537 (set_entity_stickyness)(ir_entity *ent, ir_stickyness stickyness) {
538 _set_entity_stickyness(ent, stickyness);
539 } /* set_entity_stickyness */
541 /* Set has no effect for existent entities of type method. */
543 get_atomic_ent_value(ir_entity *ent)
545 assert(ent && is_atomic_entity(ent));
546 assert(ent->variability != variability_uninitialized);
547 return skip_Id(ent->value);
548 } /* get_atomic_ent_value */
551 set_atomic_ent_value(ir_entity *ent, ir_node *val) {
552 assert(is_atomic_entity(ent) && (ent->variability != variability_uninitialized));
553 if (is_Method_type(ent->type) && (ent->peculiarity == peculiarity_existent))
556 } /* set_atomic_ent_value */
558 /* Returns true if the the node is representable as code on
560 int is_irn_const_expression(ir_node *n) {
563 /* we are in danger iff an exception will arise. TODO: be more precisely,
564 * for instance Div. will NOT rise if divisor != 0
566 if (is_binop(n) && !is_fragile_op(n))
567 return is_irn_const_expression(get_binop_left(n)) && is_irn_const_expression(get_binop_right(n));
570 switch (get_irn_opcode(n)) {
577 return is_irn_const_expression(get_irn_n(n, 0));
582 } /* is_irn_const_expression */
585 * Copies a firm subgraph that complies to the restrictions for
586 * constant expressions to current_block in current_ir_graph.
588 ir_node *copy_const_value(dbg_info *dbg, ir_node *n) {
592 /* @@@ GL I think we should implement this using the routines from irgopt for
593 dead node elimination/inlineing. */
596 switch (get_irn_opcode(n)) {
598 nn = new_d_Const_type(dbg, m, get_Const_tarval(n), get_Const_type(n));
601 nn = new_d_SymConst_type(dbg, get_SymConst_symbol(n), get_SymConst_kind(n),
602 get_SymConst_value_type(n));
605 nn = new_d_Add(dbg, copy_const_value(dbg, get_Add_left(n)),
606 copy_const_value(dbg, get_Add_right(n)), m); break;
608 nn = new_d_Sub(dbg, copy_const_value(dbg, get_Sub_left(n)),
609 copy_const_value(dbg, get_Sub_right(n)), m); break;
611 nn = new_d_Mul(dbg, copy_const_value(dbg, get_Mul_left(n)),
612 copy_const_value(dbg, get_Mul_right(n)), m); break;
614 nn = new_d_And(dbg, copy_const_value(dbg, get_And_left(n)),
615 copy_const_value(dbg, get_And_right(n)), m); break;
617 nn = new_d_Or(dbg, copy_const_value(dbg, get_Or_left(n)),
618 copy_const_value(dbg, get_Or_right(n)), m); break;
620 nn = new_d_Eor(dbg, copy_const_value(dbg, get_Eor_left(n)),
621 copy_const_value(dbg, get_Eor_right(n)), m); break;
623 nn = new_d_Cast(dbg, copy_const_value(dbg, get_Cast_op(n)), get_Cast_type(n)); break;
625 nn = new_d_Conv(dbg, copy_const_value(dbg, get_Conv_op(n)), m); break;
627 nn = new_d_Unknown(m); break;
629 assert(0 && "opcode invalid or not implemented");
634 } /* copy_const_value */
636 /* Creates a new compound graph path. */
637 compound_graph_path *
638 new_compound_graph_path(ir_type *tp, int length) {
639 compound_graph_path *res;
641 assert(is_compound_type(tp));
644 res = xmalloc(sizeof(*res) + (length-1) * sizeof(res->list[0]));
645 memset(res, 0, sizeof(*res) + (length-1) * sizeof(res->list[0]));
646 res->kind = k_ir_compound_graph_path;
651 } /* new_compound_graph_path */
653 /* Frees an graph path object */
654 void free_compound_graph_path (compound_graph_path *gr) {
655 assert(gr && is_compound_graph_path(gr));
658 } /* free_compound_graph_path */
660 /* Returns non-zero if an object is a compound graph path */
661 int is_compound_graph_path(const void *thing) {
662 return (get_kind(thing) == k_ir_compound_graph_path);
663 } /* is_compound_graph_path */
665 /* Checks whether the path up to pos is correct. If the path contains a NULL,
666 * assumes the path is not complete and returns 'true'. */
667 int is_proper_compound_graph_path(compound_graph_path *gr, int pos) {
670 ir_type *owner = gr->tp;
672 for (i = 0; i <= pos; i++) {
673 node = get_compound_graph_path_node(gr, i);
675 /* Path not yet complete. */
677 if (get_entity_owner(node) != owner)
679 owner = get_entity_type(node);
681 if (pos == get_compound_graph_path_length(gr))
682 if (!is_atomic_type(owner))
685 } /* is_proper_compound_graph_path */
687 /* Returns the length of a graph path */
688 int get_compound_graph_path_length(const compound_graph_path *gr) {
689 assert(gr && is_compound_graph_path(gr));
691 } /* get_compound_graph_path_length */
694 get_compound_graph_path_node(const compound_graph_path *gr, int pos) {
695 assert(gr && is_compound_graph_path(gr));
696 assert(pos >= 0 && pos < gr->len);
697 return gr->list[pos].node;
698 } /* get_compound_graph_path_node */
701 set_compound_graph_path_node(compound_graph_path *gr, int pos, ir_entity *node) {
702 assert(gr && is_compound_graph_path(gr));
703 assert(pos >= 0 && pos < gr->len);
704 assert(is_entity(node));
705 gr->list[pos].node = node;
706 assert(is_proper_compound_graph_path(gr, pos));
707 } /* set_compound_graph_path_node */
710 get_compound_graph_path_array_index(const compound_graph_path *gr, int pos) {
711 assert(gr && is_compound_graph_path(gr));
712 assert(pos >= 0 && pos < gr->len);
713 return gr->list[pos].index;
714 } /* get_compound_graph_path_array_index */
717 set_compound_graph_path_array_index(compound_graph_path *gr, int pos, int index) {
718 assert(gr && is_compound_graph_path(gr));
719 assert(pos >= 0 && pos < gr->len);
720 gr->list[pos].index = index;
721 } /* set_compound_graph_path_array_index */
723 /* A value of a compound entity is a pair of value and the corresponding path to a member of
726 add_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path) {
727 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
728 assert(is_compound_graph_path(path));
729 ARR_APP1(ir_node *, ent->attr.cmpd_attr.values, val);
730 ARR_APP1(compound_graph_path *, ent->attr.cmpd_attr.val_paths, path);
731 } /* add_compound_ent_value_w_path */
734 set_compound_ent_value_w_path(ir_entity *ent, ir_node *val, compound_graph_path *path, int pos) {
735 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
736 assert(is_compound_graph_path(path));
737 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
738 ent->attr.cmpd_attr.values[pos] = val;
739 ent->attr.cmpd_attr.val_paths[pos] = path;
740 } /* set_compound_ent_value_w_path */
743 get_compound_ent_n_values(ir_entity *ent) {
744 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
745 return ARR_LEN(ent->attr.cmpd_attr.values);
746 } /* get_compound_ent_n_values */
749 get_compound_ent_value(ir_entity *ent, int pos) {
750 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
751 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.values));
752 return ent->attr.cmpd_attr.values[pos];
753 } /* get_compound_ent_value */
755 compound_graph_path *
756 get_compound_ent_value_path(ir_entity *ent, int pos) {
757 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
758 assert(0 <= pos && pos < ARR_LEN(ent->attr.cmpd_attr.val_paths));
759 return ent->attr.cmpd_attr.val_paths[pos];
760 } /* get_compound_ent_value_path */
763 * Returns non-zero, if two compound_graph_pathes are equal
765 * @param path1 the first path
766 * @param visited_indices
767 * @param path2 the second path
769 static int equal_paths(compound_graph_path *path1, int *visited_indices, compound_graph_path *path2) {
771 int len1 = get_compound_graph_path_length(path1);
772 int len2 = get_compound_graph_path_length(path2);
774 if (len2 != len1) return 0;
776 for (i = 0; i < len1; i++) {
778 ir_entity *node1 = get_compound_graph_path_node(path1, i);
779 ir_entity *node2 = get_compound_graph_path_node(path2, i);
781 if (node1 != node2) return 0;
783 /* FIXME: Strange code. What is it good for? */
784 tp = get_entity_owner(node1);
785 if (is_Array_type(tp)) {
788 /* Compute the index of this node. */
789 assert(get_array_n_dimensions(tp) == 1 && "multidim not implemented");
791 low = get_array_lower_bound_int(tp, 0);
792 if (low + visited_indices[i] < get_compound_graph_path_array_index(path2, i)) {
793 visited_indices[i]++;
796 assert(low + visited_indices[i] == get_compound_graph_path_array_index(path2, i));
803 * Returns the position of a value with the given path.
804 * The path must contain array indices for all array element entities.
806 * @todo This implementation is very low and should be replaced when the new tree oriented
807 * value representation is finally implemented.
809 static int get_compound_ent_pos_by_path(ir_entity *ent, compound_graph_path *path) {
810 int i, n_paths = get_compound_ent_n_values(ent);
811 int *visited_indices;
812 int path_len = get_compound_graph_path_length(path);
814 NEW_ARR_A(int *, visited_indices, path_len);
815 memset(visited_indices, 0, sizeof(*visited_indices) * path_len);
816 for (i = 0; i < n_paths; i ++) {
817 if (equal_paths(get_compound_ent_value_path(ent, i), visited_indices, path))
824 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
825 printf("Entity %s : ", get_entity_name(ent));
826 for (j = 0; j < get_compound_graph_path_length(path); ++j) {
827 ir_entity *node = get_compound_graph_path_node(path, j);
828 printf("%s", get_entity_name(node));
829 if (is_Array_type(get_entity_owner(node)))
830 printf("[%d]", get_compound_graph_path_array_index(path, j));
832 printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
836 assert(0 && "path not found");
838 } /* get_compound_ent_pos_by_path */
840 /* Returns a constant value given the access path.
841 * The path must contain array indices for all array element entities. */
842 ir_node *get_compound_ent_value_by_path(ir_entity *ent, compound_graph_path *path) {
843 return get_compound_ent_value(ent, get_compound_ent_pos_by_path(ent, path));
844 } /* get_compound_ent_value_by_path */
848 remove_compound_ent_value(ir_entity *ent, ir_entity *value_ent) {
850 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
851 for (i = 0; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths)); ++i) {
852 compound_graph_path *path = ent->attr.cmpd_attr.val_paths[i];
853 if (path->list[path->len-1].node == value_ent) {
854 for (; i < (ARR_LEN(ent->attr.cmpd_attr.val_paths))-1; ++i) {
855 ent->attr.cmpd_attr.val_paths[i] = ent->attr.cmpd_attr.val_paths[i+1];
856 ent->attr.cmpd_attr.values[i] = ent->attr.cmpd_attr.values[i+1];
858 ARR_SETLEN(ir_entity*, ent->attr.cmpd_attr.val_paths, ARR_LEN(ent->attr.cmpd_attr.val_paths) - 1);
859 ARR_SETLEN(ir_node*, ent->attr.cmpd_attr.values, ARR_LEN(ent->attr.cmpd_attr.values) - 1);
863 } /* remove_compound_ent_value */
866 add_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member) {
867 compound_graph_path *path;
868 ir_type *owner_tp = get_entity_owner(member);
869 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
870 path = new_compound_graph_path(get_entity_type(ent), 1);
871 path->list[0].node = member;
872 if (is_Array_type(owner_tp)) {
876 assert(get_array_n_dimensions(owner_tp) == 1 && has_array_lower_bound(owner_tp, 0));
877 max = get_array_lower_bound_int(owner_tp, 0) -1;
878 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
879 int index = get_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0);
884 path->list[0].index = max + 1;
886 add_compound_ent_value_w_path(ent, val, path);
887 } /* add_compound_ent_value */
891 get_compound_ent_value_member(ir_entity *ent, int pos) {
892 compound_graph_path *path;
893 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
894 path = get_compound_ent_value_path(ent, pos);
896 return get_compound_graph_path_node(path, get_compound_graph_path_length(path)-1);
897 } /* get_compound_ent_value_member */
900 set_compound_ent_value(ir_entity *ent, ir_node *val, ir_entity *member, int pos) {
901 compound_graph_path *path;
902 assert(is_compound_entity(ent) && (ent->variability != variability_uninitialized));
903 path = get_compound_ent_value_path(ent, pos);
904 set_compound_graph_path_node(path, 0, member);
905 set_compound_ent_value_w_path(ent, val, path, pos);
906 } /* set_compound_ent_value */
909 set_array_entity_values(ir_entity *ent, tarval **values, int num_vals) {
911 ir_graph *rem = current_ir_graph;
912 ir_type *arrtp = get_entity_type(ent);
914 ir_type *elttp = get_array_element_type(arrtp);
916 assert(is_Array_type(arrtp));
917 assert(get_array_n_dimensions(arrtp) == 1);
918 /* One bound is sufficient, the number of constant fields makes the
920 assert(get_array_lower_bound (arrtp, 0) || get_array_upper_bound (arrtp, 0));
921 assert(get_entity_variability(ent) != variability_uninitialized);
922 current_ir_graph = get_const_code_irg();
924 for (i = 0; i < num_vals; i++) {
925 val = new_Const_type(values[i], elttp);
926 add_compound_ent_value(ent, val, get_array_element_entity(arrtp));
927 set_compound_graph_path_array_index(get_compound_ent_value_path(ent, i), 0, i);
929 current_ir_graph = rem;
930 } /* set_array_entity_values */
932 /* Return the overall offset of value at position pos in bytes. */
933 int get_compound_ent_value_offset_bytes(ir_entity *ent, int pos) {
934 compound_graph_path *path;
939 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
941 path = get_compound_ent_value_path(ent, pos);
942 path_len = get_compound_graph_path_length(path);
945 for (i = 0; i < path_len; ++i) {
946 ir_entity *node = get_compound_graph_path_node(path, i);
947 ir_type *node_tp = get_entity_type(node);
949 if (is_Array_type(curr_tp)) {
950 int size = get_type_size_bits(node_tp);
951 int align = get_type_alignment_bits(node_tp);
955 if(size % align > 0) {
956 size += align - (size % align);
958 assert(size % 8 == 0);
960 idx = get_compound_graph_path_array_index(path, i);
962 offset += size * idx;
964 offset += get_entity_offset(node);
970 } /* get_compound_ent_value_offset_bytes */
972 /* Return the offset in bits from the last byte address. */
973 int get_compound_ent_value_offset_bit_remainder(ir_entity *ent, int pos) {
974 compound_graph_path *path;
976 ir_entity *last_node;
978 assert(get_type_state(get_entity_type(ent)) == layout_fixed);
980 path = get_compound_ent_value_path(ent, pos);
981 path_len = get_compound_graph_path_length(path);
982 last_node = get_compound_graph_path_node(path, path_len - 1);
984 return get_entity_offset_bits_remainder(last_node);
985 } /* get_compound_ent_value_offset_bit_remainder */
988 (get_entity_offset)(const ir_entity *ent) {
989 return _get_entity_offset(ent);
990 } /* get_entity_offset */
993 (set_entity_offset)(ir_entity *ent, int offset) {
994 _set_entity_offset(ent, offset);
995 } /* set_entity_offset */
998 (get_entity_offset_bits_remainder)(const ir_entity *ent) {
999 return _get_entity_offset_bits_remainder(ent);
1000 } /* get_entity_offset_bits_remainder */
1003 (set_entity_offset_bits_remainder)(ir_entity *ent, unsigned char offset) {
1004 _set_entity_offset_bits_remainder(ent, offset);
1005 } /* set_entity_offset_bits_remainder */
1008 add_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1010 ir_type *owner = get_entity_owner(ent);
1011 ir_type *ovw_ovner = get_entity_owner(overwritten);
1012 assert(is_Class_type(owner));
1013 assert(is_Class_type(ovw_ovner));
1014 assert(! is_class_final(ovw_ovner));
1016 ARR_APP1(ir_entity *, ent->overwrites, overwritten);
1017 ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
1018 } /* add_entity_overwrites */
1021 get_entity_n_overwrites(ir_entity *ent) {
1022 assert(is_Class_type(get_entity_owner(ent)));
1023 return (ARR_LEN(ent->overwrites));
1024 } /* get_entity_n_overwrites */
1027 get_entity_overwrites_index(ir_entity *ent, ir_entity *overwritten) {
1029 assert(is_Class_type(get_entity_owner(ent)));
1030 for (i = 0; i < get_entity_n_overwrites(ent); i++)
1031 if (get_entity_overwrites(ent, i) == overwritten)
1034 } /* get_entity_overwrites_index */
1037 get_entity_overwrites(ir_entity *ent, int pos) {
1038 assert(is_Class_type(get_entity_owner(ent)));
1039 assert(pos < get_entity_n_overwrites(ent));
1040 return ent->overwrites[pos];
1041 } /* get_entity_overwrites */
1044 set_entity_overwrites(ir_entity *ent, int pos, ir_entity *overwritten) {
1045 assert(is_Class_type(get_entity_owner(ent)));
1046 assert(pos < get_entity_n_overwrites(ent));
1047 ent->overwrites[pos] = overwritten;
1048 } /* set_entity_overwrites */
1051 remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten) {
1053 assert(is_Class_type(get_entity_owner(ent)));
1054 for (i = 0; i < (ARR_LEN (ent->overwrites)); i++)
1055 if (ent->overwrites[i] == overwritten) {
1056 for(; i < (ARR_LEN (ent->overwrites))-1; i++)
1057 ent->overwrites[i] = ent->overwrites[i+1];
1058 ARR_SETLEN(ir_entity*, ent->overwrites, ARR_LEN(ent->overwrites) - 1);
1061 } /* remove_entity_overwrites */
1064 add_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1065 add_entity_overwrites(overwrites, ent);
1066 } /* add_entity_overwrittenby */
1069 get_entity_n_overwrittenby(ir_entity *ent) {
1070 assert(is_Class_type(get_entity_owner(ent)));
1071 return (ARR_LEN (ent->overwrittenby));
1072 } /* get_entity_n_overwrittenby */
1075 get_entity_overwrittenby_index(ir_entity *ent, ir_entity *overwrites) {
1077 assert(is_Class_type(get_entity_owner(ent)));
1078 for (i = 0; i < get_entity_n_overwrittenby(ent); i++)
1079 if (get_entity_overwrittenby(ent, i) == overwrites)
1082 } /* get_entity_overwrittenby_index */
1085 get_entity_overwrittenby(ir_entity *ent, int pos) {
1086 assert(is_Class_type(get_entity_owner(ent)));
1087 assert(pos < get_entity_n_overwrittenby(ent));
1088 return ent->overwrittenby[pos];
1089 } /* get_entity_overwrittenby */
1092 set_entity_overwrittenby(ir_entity *ent, int pos, ir_entity *overwrites) {
1093 assert(is_Class_type(get_entity_owner(ent)));
1094 assert(pos < get_entity_n_overwrittenby(ent));
1095 ent->overwrittenby[pos] = overwrites;
1096 } /* set_entity_overwrittenby */
1098 void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites) {
1100 assert(is_Class_type(get_entity_owner(ent)));
1101 for (i = 0; i < (ARR_LEN (ent->overwrittenby)); i++)
1102 if (ent->overwrittenby[i] == overwrites) {
1103 for(; i < (ARR_LEN (ent->overwrittenby))-1; i++)
1104 ent->overwrittenby[i] = ent->overwrittenby[i+1];
1105 ARR_SETLEN(ir_entity*, ent->overwrittenby, ARR_LEN(ent->overwrittenby) - 1);
1108 } /* remove_entity_overwrittenby */
1110 /* A link to store intermediate information */
1112 (get_entity_link)(const ir_entity *ent) {
1113 return _get_entity_link(ent);
1114 } /* get_entity_link */
1117 (set_entity_link)(ir_entity *ent, void *l) {
1118 _set_entity_link(ent, l);
1119 } /* set_entity_link */
1122 (get_entity_irg)(const ir_entity *ent) {
1123 return _get_entity_irg(ent);
1124 } /* get_entity_irg */
1127 set_entity_irg(ir_entity *ent, ir_graph *irg) {
1128 assert(is_method_entity(ent));
1129 /* Wie kann man die Referenz auf einen IRG löschen, z.B. wenn die
1130 * Methode selbst nicht mehr aufgerufen werden kann, die Entität
1131 * aber erhalten bleiben soll? Wandle die Entitaet in description oder
1134 assert((irg && ent->peculiarity == peculiarity_existent) ||
1135 (!irg && (ent->peculiarity == peculiarity_existent)
1136 && (ent -> visibility == visibility_external_allocated)) ||
1137 (!irg && ent->peculiarity == peculiarity_description) ||
1138 (!irg && ent->peculiarity == peculiarity_inherited));
1139 ent->attr.mtd_attr.irg = irg;
1140 } /* set_entity_irg */
1142 unsigned get_entity_vtable_number(const ir_entity *ent) {
1143 assert(is_method_entity((ir_entity *)ent));
1144 return ent->attr.mtd_attr.vtable_number;
1145 } /* get_entity_vtable_number */
1147 void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number) {
1148 assert(is_method_entity(ent));
1149 ent->attr.mtd_attr.vtable_number = vtable_number;
1150 } /* set_entity_vtable_number */
1152 /* Returns the section of a method. */
1153 ir_img_section get_method_img_section(const ir_entity *ent) {
1154 assert(is_method_entity((ir_entity *)ent));
1155 return ent->attr.mtd_attr.section;
1156 } /* get_method_img_section */
1158 /* Sets the section of a method. */
1159 void set_method_img_section(ir_entity *ent, ir_img_section section) {
1160 assert(is_method_entity(ent));
1161 ent->attr.mtd_attr.section = section;
1162 } /* set_method_img_section */
1165 (is_entity)(const void *thing) {
1166 return _is_entity(thing);
1169 int is_atomic_entity(ir_entity *ent) {
1170 ir_type *t = get_entity_type(ent);
1171 const tp_op *op = get_type_tpop(t);
1172 return (op == type_primitive || op == type_pointer ||
1173 op == type_enumeration || op == type_method);
1174 } /* is_atomic_entity */
1176 int is_compound_entity(ir_entity *ent) {
1177 ir_type *t = get_entity_type(ent);
1178 const tp_op *op = get_type_tpop(t);
1179 return (op == type_class || op == type_struct ||
1180 op == type_array || op == type_union);
1181 } /* is_compound_entity */
1183 int is_method_entity(ir_entity *ent) {
1184 ir_type *t = get_entity_type(ent);
1185 return is_Method_type(t);
1186 } /* is_method_entity */
1189 * @todo not implemented!!! */
1190 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 */