Move current_ir_graph from ir_graph to ir_cons
[libfirm] / ir / tr / trverify.c
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file    tr_inheritance.c
22  * @brief   Check types and entities for correctness.
23  * @date    29.1.2003
24  * @author  Michael Beck, Goetz Lindenmaier
25  */
26 #include "config.h"
27
28 #include "irgraph_t.h"
29 #include "irflag_t.h"
30 #include "irprintf.h"
31 #include "irgwalk.h"
32 #include "error.h"
33 #include "tv.h"
34 #include "ircons.h"
35
36 #ifdef NDEBUG
37 /*
38  * in RELEASE mode, returns ret if the expression expr evaluates to zero
39  * in ASSERT mode, asserts the expression expr (and the string string).
40  */
41 #define ASSERT_AND_RET(expr, string, ret)       if (!(expr)) return (ret)
42
43 /*
44  * in RELEASE mode, returns ret if the expression expr evaluates to zero
45  * in ASSERT mode, executes blk if the expression expr evaluates to zero and asserts expr
46  */
47 #define ASSERT_AND_RET_DBG(expr, string, ret, blk)      if (!(expr)) return (ret)
48 #else
49 #define ASSERT_AND_RET(expr, string, ret) \
50 do { \
51   if (opt_do_node_verification == FIRM_VERIFICATION_ON) {\
52     assert((expr) && string); } \
53   if (!(expr)) { \
54     if (opt_do_node_verification == FIRM_VERIFICATION_REPORT) \
55       fprintf(stderr, #expr " : " string "\n"); \
56     firm_verify_failure_msg = #expr " && " string; \
57     return (ret); \
58   } \
59 } while (0)
60
61 #define ASSERT_AND_RET_DBG(expr, string, ret, blk) \
62 do { \
63   if (!(expr)) { \
64     firm_verify_failure_msg = #expr " && " string; \
65     if (opt_do_node_verification != FIRM_VERIFICATION_ERROR_ONLY) { blk; } \
66     if (opt_do_node_verification == FIRM_VERIFICATION_REPORT) \
67       fprintf(stderr, #expr " : " string "\n"); \
68     else if (opt_do_node_verification == FIRM_VERIFICATION_ON) { \
69       assert((expr) && string); \
70     } \
71     return (ret); \
72   } \
73 } while (0)
74
75 #endif /* NDEBUG */
76
77 #ifndef NDEBUG
78
79 static const char *firm_verify_failure_msg;
80
81 #if 0
82 /**
83  * Show diagnostic if an entity overwrites another one not
84  * in direct superclasses.
85  */
86 static void show_ent_not_supertp(ir_entity *ent, ir_entity *ovw)
87 {
88         ir_type *owner = get_entity_owner(ent);
89         ir_type *ov_own = get_entity_owner(ovw);
90         size_t   i;
91
92         fprintf(stderr, "Type verification error:\n");
93         ir_fprintf(stderr, "Entity %+F::%+e owerwrites ", owner, ent);
94         ir_fprintf(stderr, "Entity %+F::%+e\n", ov_own, ovw);
95
96         ir_fprintf(stderr, "Supertypes of %+F:\n", owner);
97         for (i = 0; i < get_class_n_supertypes(owner); ++i) {
98                 ir_type *super = get_class_supertype(owner, i);
99                 ir_fprintf(stderr, " %+F:\n", super);
100         }
101 }
102 #endif
103
104 /**
105  * Show diagnostic if an entity overwrites a wrong number of things.
106  */
107 static void show_ent_overwrite_cnt(ir_entity *ent)
108 {
109         ir_type *owner = get_entity_owner(ent);
110         size_t i;
111         size_t j;
112         size_t k;
113         bool   found;
114         bool   show_stp = false;
115
116         fprintf(stderr, "Type verification error:\n");
117         ir_fprintf(stderr, "Entity %t::%e owerwrites\n", owner, ent);
118         for (i = 0; i < get_entity_n_overwrites(ent); ++i) {
119                 ir_entity *ovw = get_entity_overwrites(ent, i);
120                 ir_type *ov_own = get_entity_owner(ovw);
121                 size_t n_supertypes = get_class_n_supertypes(owner);
122
123                 ir_fprintf(stderr, "  %t::%e\n", ov_own, ovw);
124                 for (k = 0; k < i; ++k) {
125                         if (ovw == get_entity_overwrites(ent, k)) {
126                                 ir_fprintf(stderr, "  ->%t::%e entered more than once\n", ov_own, ovw);
127                                 break;
128                         }
129                 }
130
131                 found = false;
132                 for (j = 0; j < n_supertypes; ++j) {
133                         if (ov_own == get_class_supertype(owner, j)) {
134                                 show_stp = found = true;
135                                 break;
136                         }
137                 }
138                 if (! found)
139                         ir_fprintf(stderr, "  ->%t not in super types of %t\n", ov_own, owner);
140         }
141
142         if (show_stp) {
143                 ir_fprintf(stderr, "Supertypes of %t:\n", owner);
144                 for (i = 0; i < get_class_n_supertypes(owner); ++i) {
145                         ir_type *super = get_class_supertype(owner, i);
146                         ir_fprintf(stderr, " %t:\n", super);
147                 }
148         }
149 }
150
151 #endif /* #ifndef NDEBUG */
152
153 /**
154  * Check a class
155  */
156 static int check_class(ir_type *tp)
157 {
158         size_t i, n;
159
160         for (i = 0, n = get_class_n_members(tp); i < n; ++i) {
161                 ir_entity *mem = get_class_member(tp, i);
162
163                 ASSERT_AND_RET_DBG(
164                         tp == get_entity_owner(mem),
165                         "class member with wrong owner",
166                         error_ent_wrong_owner,
167                         ir_fprintf(stderr, "Type verification error:\n%+F %+e(owner %+F)\n",tp, mem, get_entity_owner(mem))
168                 );
169                 ASSERT_AND_RET_DBG(
170                         mem,
171                         "NULL members not allowed",
172                         error_null_mem,
173                         ir_fprintf(stderr, "Type verification error:\n%+F member %zu is NULL\n", tp, i)
174                 );
175
176                 ASSERT_AND_RET_DBG(
177                         get_entity_n_overwrites(mem) <= get_class_n_supertypes(tp),
178                         "wrong number of entity overwrites",
179                         error_wrong_ent_overwrites,
180                         show_ent_overwrite_cnt(mem)
181                 );
182
183 #if 0
184                 {
185                         size_t j, m;
186                         /* check if the overwrite relation is flat, i.e. every overwrite
187                          * is visible in every direct superclass. */
188                         for (j = 0, m = get_entity_n_overwrites(mem); j < m; ++j) {
189                                 ir_entity *ovw = get_entity_overwrites(mem, j);
190                                 size_t    k, n_super;
191
192                                 /* Check whether ovw is member of one of tp's supertypes. If so,
193                                    the representation is correct. */
194                                 for (k = 0, n_super = get_class_n_supertypes(tp); k < n_super; ++k) {
195                                         if (get_class_member_index(get_class_supertype(tp, k), ovw) != INVALID_MEMBER_INDEX) {
196                                                 ASSERT_AND_RET_DBG(
197                                                         0,
198                                                         "overwrites an entity not contained in direct supertype",
199                                                         error_ent_not_cont,
200                                                         show_ent_not_supertp(mem, ovw)
201                                                 );
202                                                 break;
203                                         }
204                                 }
205                         }
206                 }
207 #endif
208         }
209         return 0;
210 }
211
212 /**
213  * Check an array.
214  */
215 static int check_array(const ir_type *tp)
216 {
217         size_t i, n_dim = get_array_n_dimensions(tp);
218
219         for (i = 0; i < n_dim; ++i) {
220                 ASSERT_AND_RET_DBG(
221                         has_array_lower_bound(tp, i) || has_array_upper_bound(tp, i),
222                         "array bound missing",
223                         1,
224                         ir_fprintf(stderr, "%+F in dimension %zu\n", tp, i)
225                 );
226         }
227         return 0;
228 }
229
230
231 /**
232  * Check a primitive.
233  */
234 static int check_primitive(ir_type *tp)
235 {
236         ASSERT_AND_RET_DBG(
237                 is_mode(get_type_mode(tp)),
238                 "Primitive type without mode",
239                 1,
240                 ir_fprintf(stderr, "%+F\n", tp)
241         );
242         return 0;
243 }
244
245 int check_type(ir_type *tp)
246 {
247         switch (get_type_tpop_code(tp)) {
248         case tpo_class:
249                 return check_class(tp);
250         case tpo_array:
251                 return check_array(tp);
252         case tpo_primitive:
253                 return check_primitive(tp);
254         default: break;
255         }
256         return 0;
257 }
258
259 /**
260  * checks the visited flag
261  */
262 static int check_visited_flag(ir_graph *irg, ir_node *n)
263 {
264         ASSERT_AND_RET_DBG(
265                 get_irn_visited(n) <= get_irg_visited(irg),
266                 "Visited flag of node is larger than that of corresponding irg.",
267                 0,
268                 ir_fprintf(stderr, "%+F in %+F\n", n, irg)
269         );
270         return 1;
271 }
272
273 /**
274  * helper environment struct for constant_on_wrong_obstack()
275  */
276 typedef struct myenv {
277         int res;
278         ir_graph *irg;
279 } myenv;
280
281 /**
282  * called by the walker
283  */
284 static void on_irg_storage(ir_node *n, void *data)
285 {
286         myenv *env = (myenv*)data;
287
288         /* We also test whether the setting of the visited flag is legal. */
289         env->res = node_is_in_irgs_storage(env->irg, n) &&
290                    check_visited_flag(env->irg, n);
291 }
292
293 /**
294  * checks whether a given constant IR node is NOT on the
295  * constant IR graph.
296  */
297 static int constant_on_wrong_irg(ir_node *n)
298 {
299         myenv env;
300
301         env.res = 1;  /* on right obstack */
302         env.irg = get_const_code_irg();
303
304         irg_walk(n, on_irg_storage, NULL, (void *)&env);
305         return ! env.res;
306 }
307
308 static int initializer_constant_on_wrong_irg(ir_initializer_t *initializer)
309 {
310         switch (get_initializer_kind(initializer)) {
311         case IR_INITIALIZER_NULL:
312                 return 0;
313         case IR_INITIALIZER_TARVAL:
314                 return 0;
315         case IR_INITIALIZER_CONST:
316                 return constant_on_wrong_irg(get_initializer_const_value(initializer));
317         case IR_INITIALIZER_COMPOUND: {
318                 size_t i, n = get_initializer_compound_n_entries(initializer);
319                 for (i = 0; i < n; ++i) {
320                         ir_initializer_t *sub
321                                 = get_initializer_compound_value(initializer, i);
322                         if (initializer_constant_on_wrong_irg(sub))
323                                 return 1;
324                 }
325                 return 0;
326         }
327         }
328         panic("invalid initializer in initializer_on_wrong_irg");
329 }
330
331 /**
332  * Check if constants node are NOT on the constant IR graph.
333  *
334  * @return NON-zero if an entity initializer constant is NOT on
335  * the current_ir_graph's obstack.
336  */
337 static int constants_on_wrong_irg(ir_entity *ent)
338 {
339         if (ent->initializer != NULL) {
340                 return initializer_constant_on_wrong_irg(ent->initializer);
341         } else if (entity_has_compound_ent_values(ent)) {
342                 size_t i, n;
343                 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
344                         if (constant_on_wrong_irg(get_compound_ent_value(ent, i)))
345                                 return 1;
346                 }
347         }
348         return 0;
349 }
350
351 int check_entity(ir_entity *ent)
352 {
353         ir_type *tp = get_entity_type(ent);
354
355         current_ir_graph =  get_const_code_irg();
356         ASSERT_AND_RET_DBG(
357                 constants_on_wrong_irg(ent) == 0,
358                 "Contants placed on wrong IRG",
359                 error_const_on_wrong_irg,
360                 ir_fprintf(stderr, "%+e not on %+F\n", ent, current_ir_graph)
361         );
362
363         /* Originally, this test assumed, that only method entities have
364            pecularity_inherited.  As I changed this, I have to test for method type
365            before doing the test. */
366         if (get_entity_peculiarity(ent) == peculiarity_existent
367             && is_method_entity(ent)) {
368
369                 ir_entity *impl = get_SymConst_entity(get_atomic_ent_value(ent));
370                 ASSERT_AND_RET_DBG(
371                         impl != NULL,
372                         "inherited method entities must have constant pointing to existent entity.",
373                         error_inherited_ent_without_const,
374                         ir_fprintf(stderr, "%+e points to %+e\n", ent, impl)
375                 );
376         }
377
378         if (is_atomic_entity(ent) && ent->initializer != NULL) {
379                 ir_mode *mode = NULL;
380                 ir_initializer_t *initializer = ent->initializer;
381                 switch (initializer->kind) {
382                 case IR_INITIALIZER_CONST:
383                         mode = get_irn_mode(get_initializer_const_value(initializer));
384                         break;
385                 case IR_INITIALIZER_TARVAL:
386                         mode = get_tarval_mode(get_initializer_tarval_value(initializer));
387                         break;
388                 case IR_INITIALIZER_NULL:
389                 case IR_INITIALIZER_COMPOUND:
390                         break;
391                 }
392                 ASSERT_AND_RET_DBG(
393                         mode == NULL || mode == get_type_mode(tp),
394                         "Mode of constant in entity must match type.",
395                         error_ent_const_mode,
396                         ir_fprintf(stderr, "%+e, type %+F(%+F)\n",
397                         ent, tp, get_type_mode(tp))
398                 );
399         }
400         return no_error;
401 }
402
403 /*
404  * check types and entities
405  */
406 static void check_tore(type_or_ent tore, void *env)
407 {
408         int *res = (int*)env;
409         assert(tore.ent);
410         if (is_type(tore.typ)) {
411                 *res = check_type(tore.typ);
412         } else {
413                 assert(is_entity(tore.ent));
414                 *res = check_entity(tore.ent);
415         }
416 }
417
418 int tr_verify(void)
419 {
420         static ident *empty = NULL;
421         int           res = no_error;
422         ir_type      *constructors;
423         ir_type      *destructors;
424         ir_type      *thread_locals;
425         size_t        i, n;
426         ir_segment_t  s;
427
428         if (empty == NULL)
429                 empty = new_id_from_chars("", 0);
430
431         type_walk(check_tore, NULL, &res);
432
433         for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
434                 const ir_type *type = get_segment_type(s);
435                 size_t         e;
436                 for (e = 0; e < get_compound_n_members(type); ++e) {
437                         ir_entity *entity = get_compound_member(type, e);
438                         ASSERT_AND_RET(get_entity_ld_ident(entity) != NULL ||
439                                         get_entity_visibility(entity) == ir_visibility_private,
440                                         "segment members must have a name or visibility_private",
441                                         1);
442                 }
443         }
444
445         constructors = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
446         for (i = 0, n = get_compound_n_members(constructors); i < n; ++i) {
447                 const ir_entity *entity = get_compound_member(constructors, i);
448                 ASSERT_AND_RET(get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER,
449                                "entity without LINKAGE_HIDDEN_USER in constructors is pointless",
450                                1);
451                 /* Mach-O doesn't like labels in this section */
452                 ASSERT_AND_RET(get_entity_ld_ident(entity),
453                                "entity in constructors should have ld_ident=''", 1);
454         }
455         destructors = get_segment_type(IR_SEGMENT_DESTRUCTORS);
456         for (i = 0, n = get_compound_n_members(destructors); i < n; ++i) {
457                 const ir_entity *entity = get_compound_member(destructors, i);
458                 ASSERT_AND_RET(get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER,
459                                "entity without LINKAGE_HIDDEN_USER in destructors is pointless",
460                                1);
461                 /* Mach-O doesn't like labels in this section */
462                 ASSERT_AND_RET(get_entity_ld_ident(entity),
463                                "entity in destructors should have ld_ident=''", 1);
464         }
465         thread_locals = get_segment_type(IR_SEGMENT_THREAD_LOCAL);
466         for (i = 0, n = get_compound_n_members(thread_locals); i < n; ++i) {
467                 const ir_entity *entity = get_compound_member(thread_locals, i);
468                 /* this is odd and should not be allowed I think */
469                 ASSERT_AND_RET(!is_method_entity(entity),
470                                "method in THREAD_LOCAL segment", 1);
471                 ASSERT_AND_RET(! (get_entity_linkage(entity) & IR_LINKAGE_CONSTANT),
472                                "thread locals must not be constant", 1);
473         }
474
475         return res;
476 }