3 * File name: ir/tr/trvrfy.c
4 * Purpose: Check types and entities for correctness.
5 * Author: Michael Beck, Goetz Lindenmaier
9 * Copyright: (c) 2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
17 #include "irgraph_t.h" /* for checking whether constant code is allocated
24 static const char *firm_vrfy_failure_msg;
28 * in RELEASE mode, returns ret if the expression expr evaluates to zero
29 * in ASSERT mode, asserts the expression expr (and the string string).
31 #define ASSERT_AND_RET(expr, string, ret) if (!(expr)) return (ret)
34 * in RELEASE mode, returns ret if the expression expr evaluates to zero
35 * in ASSERT mode, executes blk if the expression expr evaluates to zero and asserts expr
37 #define ASSERT_AND_RET_DBG(expr, string, ret, blk) if (!(expr)) return (ret)
39 #define ASSERT_AND_RET(expr, string, ret) \
41 if (opt_do_node_verification == FIRM_VERIFICATION_ON) {\
42 assert((expr) && string); } \
44 if (opt_do_node_verification == FIRM_VERIFICATION_REPORT) \
45 fprintf(stderr, #expr " : " string "\n"); \
46 firm_vrfy_failure_msg = #expr " && " string; \
51 #define ASSERT_AND_RET_DBG(expr, string, ret, blk) \
54 firm_vrfy_failure_msg = #expr " && " string; \
55 if (opt_do_node_verification != FIRM_VERIFICATION_ERROR_ONLY) { blk; } \
56 if (opt_do_node_verification == FIRM_VERIFICATION_REPORT) \
57 fprintf(stderr, #expr " : " string "\n"); \
58 else if (opt_do_node_verification == FIRM_VERIFICATION_ON) { \
59 assert((expr) && string); \
68 * Show diagnostic if an entity overwrites another one not
69 * in direct superclasses.
71 static void show_ent_not_supertp(ir_entity *ent, ir_entity *ovw)
73 ir_type *owner = get_entity_owner(ent);
74 ir_type *ov_own = get_entity_owner(ovw);
77 fprintf(stderr, "Type verification error:\n");
78 ir_fprintf(stderr, "Entity %+F::%+e owerwrites ", owner, ent);
79 ir_fprintf(stderr, "Entity %+F::%+e\n", ov_own, ovw);
81 ir_fprintf(stderr, "Supertypes of %+F:\n", owner);
82 for (i = 0; i < get_class_n_supertypes(owner); ++i) {
83 ir_type *super = get_class_supertype(owner, i);
84 ir_fprintf(stderr, " %+F:\n", super);
89 * Show diagnostic if an entity overwrites a wrong number of things.
91 static void show_ent_overwrite_cnt(ir_entity *ent)
93 ir_type *owner = get_entity_owner(ent);
94 int i, j, k, found, show_stp = 0;
96 fprintf(stderr, "Type verification error:\n");
97 ir_fprintf(stderr, "Entity %t::%e owerwrites\n", owner, ent);
98 for (i = 0; i < get_entity_n_overwrites(ent); ++i) {
99 ir_entity *ovw = get_entity_overwrites(ent, i);
100 ir_type *ov_own = get_entity_owner(ovw);
102 ir_fprintf(stderr, " %t::%e\n", ov_own, ovw);
103 for (k = 0; k < i; ++k)
104 if (ovw == get_entity_overwrites(ent, k)) {
105 ir_fprintf(stderr, " ->%t::%e entered more than once\n", ov_own, ovw);
110 for (j = get_class_n_supertypes(owner) - 1; j >= 0; --j) {
111 if (ov_own == get_class_supertype(owner, j)) {
112 show_stp = found = 1;
117 ir_fprintf(stderr, " ->%t not in super types of %t\n", ov_own, owner);
121 ir_fprintf(stderr, "Supertypes of %t:\n", owner);
122 for (i = 0; i < get_class_n_supertypes(owner); ++i) {
123 ir_type *super = get_class_supertype(owner, i);
124 ir_fprintf(stderr, " %t:\n", super);
132 static int check_class(ir_type *tp) {
136 /*printf("\n"); DDMT(tp);*/
138 for (i = get_class_n_members(tp) - 1; i >= 0; --i) {
139 ir_entity *mem = get_class_member(tp, i);
142 tp == get_entity_owner(mem),
143 "class member with wrong owner",
144 error_ent_wrong_owner,
145 ir_fprintf(stderr, "Type verification error:\n%+F %+e(owner %+F)\n",tp, mem, get_entity_owner(mem))
149 "NULL members not allowed",
151 ir_fprintf(stderr, "Type verification error:\n%+F member %d is NULL\n", tp, i)
155 get_entity_n_overwrites(mem) <= get_class_n_supertypes(tp),
156 "wrong number of entity overwrites",
157 error_wrong_ent_overwrites,
158 show_ent_overwrite_cnt(mem)
161 for (j = get_entity_n_overwrites(mem) - 1; j >= 0; --j) {
162 ir_entity *ovw = get_entity_overwrites(mem, j);
163 /*printf(" overwrites: "); DDME(ovw);*/
164 /* Check whether ovw is member of one of tp's supertypes. If so,
165 the representation is correct. */
167 for (k = get_class_n_supertypes(tp) - 1; k >= 0; --k) {
168 if (get_class_member_index(get_class_supertype(tp, k), ovw) >= 0) {
175 "overwrites an entity not contained in direct supertype",
177 show_ent_not_supertp(mem, ovw)
187 static int check_array(ir_type *tp) {
188 int i, n_dim = get_array_n_dimensions(tp);
189 for (i = 0; i < n_dim; ++i) {
191 has_array_lower_bound(tp, i) || has_array_upper_bound(tp, i),
192 "array bound missing",
194 ir_fprintf(stderr, "%+F in dimension %d\n", tp, i)
204 static int check_primitive(ir_type *tp) {
206 is_mode(get_type_mode(tp)),
207 "Primitive type without mode",
209 ir_fprintf(stderr, "%+F\n", tp)
219 * 0 if no error encountered
221 int check_type(ir_type *tp) {
222 switch (get_type_tpop_code(tp)) {
224 return check_class(tp);
226 return check_array(tp);
228 return check_primitive(tp);
235 * checks the visited flag
237 static int check_visited_flag(ir_graph *irg, ir_node *n) {
239 get_irn_visited(n) <= get_irg_visited(irg),
240 "Visited flag of node is larger than that of corresponding irg.",
242 ir_fprintf(stderr, "%+F in %+F\n", n, irg)
248 * helper environment struct for constant_on_wrong_obstack()
256 * called by the walker
258 static void on_irg_storage(ir_node *n, void *env) {
259 struct myenv *myenv = env;
261 /* We also test whether the setting of the visited flag is legal. */
262 myenv->res = node_is_in_irgs_storage(myenv->irg, n) &&
263 check_visited_flag(myenv->irg, n);
267 * checks whether a given constant IR node is NOT on the
270 static int constant_on_wrong_irg(ir_node *n) {
273 env.res = 1; /* on right obstack */
274 env.irg = get_const_code_irg();
276 irg_walk(n, on_irg_storage, NULL, (void *)&env);
281 * Check if constants node are NOT on the constant IR graph.
283 * @return NON-zero if an entity initializer constant is NOT on
284 * the current_ir_graph's obstack.
286 static int constants_on_wrong_irg(ir_entity *ent) {
287 if (get_entity_variability(ent) == variability_uninitialized) return 0;
289 if (is_compound_entity(ent)) {
291 for (i = 0; i < get_compound_ent_n_values(ent); i++) {
292 if (constant_on_wrong_irg(get_compound_ent_value(ent, i)))
296 /* Might not be set if entity belongs to a description or is external allocated. */
297 if (get_atomic_ent_value(ent))
298 return constant_on_wrong_irg(get_atomic_ent_value(ent));
299 else if (get_entity_visibility(ent) != visibility_external_allocated) {
301 is_Class_type(get_entity_owner(ent)) &&
302 get_class_peculiarity(get_entity_owner(ent)) == peculiarity_description,
303 "Value in constant atomic entity not set.",
305 ir_fprintf(stderr, "%+e, owner %+F\n", ent, get_entity_owner(ent))
313 * Shows a wrong entity allocation
315 static void show_ent_alloc_error(ir_entity *ent)
317 ir_fprintf(stderr, "%+e owner %t has allocation %s\n",
318 ent, get_entity_type(ent),
319 get_allocation_name(get_entity_allocation(ent)));
323 * Check an entity. Currently, we check only if initialized constants
324 * are build on the const irg graph.
327 * 0 if no error encountered
328 * != 0 a trvrfy_error_codes code
330 int check_entity(ir_entity *ent) {
332 ir_type *tp = get_entity_type(ent);
333 ir_type *owner = get_entity_owner(ent);
335 current_ir_graph = get_const_code_irg();
337 constants_on_wrong_irg(ent) == 0,
338 "Contants placed on wrong IRG",
339 error_const_on_wrong_irg,
340 ir_fprintf(stderr, "%+e not on %+F\n", ent, current_ir_graph));
342 rem_vpi = get_visit_pseudo_irgs();
343 set_visit_pseudo_irgs(1);
344 if ((get_entity_peculiarity(ent) == peculiarity_existent) &&
345 (get_entity_visibility(ent) != visibility_external_allocated) &&
346 (is_Method_type(get_entity_type(ent))) &&
347 (!get_entity_irg(ent) || !(is_ir_graph(get_entity_irg(ent))))) {
350 "Method ents with pec_exist must have an irg",
351 error_existent_entity_without_irg,
352 ir_fprintf(stderr, "%+e\n", ent)
355 set_visit_pseudo_irgs(rem_vpi);
357 /* Originally, this test assumed, that only method entities have
358 pecularity_inherited. As I changed this, I have to test for method type before
360 if (get_entity_peculiarity(ent) == peculiarity_inherited) {
361 if (is_Method_type(get_entity_type(ent))) {
362 ir_entity *impl = get_SymConst_entity(get_atomic_ent_value(ent));
364 get_entity_peculiarity(impl) == peculiarity_existent,
365 "inherited method entities must have constant pointing to existent entity.",
366 error_inherited_ent_without_const,
367 ir_fprintf(stderr, "%+e points to %+e\n", ent, impl)
372 /* Entities in global type are not dynamic or automatic allocated. */
373 if (owner == get_glob_type()) {
375 get_entity_allocation(ent) != allocation_dynamic &&
376 get_entity_allocation(ent) != allocation_automatic,
377 "Entities in global type are not allowed to by dynamic or automatic allocated",
378 error_glob_ent_allocation,
379 show_ent_alloc_error(ent)
383 if (get_entity_variability(ent) != variability_uninitialized) {
384 if (is_atomic_type(tp)) {
385 ir_node *val = get_atomic_ent_value(ent);
388 get_irn_mode(val) == get_type_mode(tp),
389 "Mode of constant in entity must match type.",
390 error_ent_const_mode,
391 ir_fprintf(stderr, "%+e const %+F, type %+F(%+F)\n",
392 ent, val, tp, get_type_mode(tp))
400 * check types and entities
402 static void check_tore(type_or_ent *tore, void *env) {
406 *res = check_type((ir_type *)tore);
408 assert(is_entity(tore));
409 *res = check_entity((ir_entity *)tore);
414 * Verify types and entities.
419 type_walk(check_tore, NULL, &res);