remove a bunch of comments marking end of blocks
[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  * @version $Id$
26  */
27 #include "config.h"
28
29 #include "irgraph_t.h"
30 #include "irflag_t.h"
31 #include "irprintf.h"
32 #include "irgwalk.h"
33 #include "error.h"
34 #include "tv.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
246 /*
247  * Checks a type.
248  *
249  * return
250  *  0   if no error encountered
251  */
252 int check_type(ir_type *tp)
253 {
254         switch (get_type_tpop_code(tp)) {
255         case tpo_class:
256                 return check_class(tp);
257         case tpo_array:
258                 return check_array(tp);
259         case tpo_primitive:
260                 return check_primitive(tp);
261         default: break;
262         }
263         return 0;
264 }
265
266 /**
267  * checks the visited flag
268  */
269 static int check_visited_flag(ir_graph *irg, ir_node *n)
270 {
271         ASSERT_AND_RET_DBG(
272                 get_irn_visited(n) <= get_irg_visited(irg),
273                 "Visited flag of node is larger than that of corresponding irg.",
274                 0,
275                 ir_fprintf(stderr, "%+F in %+F\n", n, irg)
276         );
277         return 1;
278 }
279
280 /**
281  * helper environment struct for constant_on_wrong_obstack()
282  */
283 typedef struct myenv {
284         int res;
285         ir_graph *irg;
286 } myenv;
287
288 /**
289  * called by the walker
290  */
291 static void on_irg_storage(ir_node *n, void *data)
292 {
293         myenv *env = (myenv*)data;
294
295         /* We also test whether the setting of the visited flag is legal. */
296         env->res = node_is_in_irgs_storage(env->irg, n) &&
297                    check_visited_flag(env->irg, n);
298 }
299
300 /**
301  * checks whether a given constant IR node is NOT on the
302  * constant IR graph.
303  */
304 static int constant_on_wrong_irg(ir_node *n)
305 {
306         myenv env;
307
308         env.res = 1;  /* on right obstack */
309         env.irg = get_const_code_irg();
310
311         irg_walk(n, on_irg_storage, NULL, (void *)&env);
312         return ! env.res;
313 }
314
315 static int initializer_constant_on_wrong_irg(ir_initializer_t *initializer)
316 {
317         switch (get_initializer_kind(initializer)) {
318         case IR_INITIALIZER_NULL:
319                 return 0;
320         case IR_INITIALIZER_TARVAL:
321                 return 0;
322         case IR_INITIALIZER_CONST:
323                 return constant_on_wrong_irg(get_initializer_const_value(initializer));
324         case IR_INITIALIZER_COMPOUND: {
325                 size_t i, n = get_initializer_compound_n_entries(initializer);
326                 for (i = 0; i < n; ++i) {
327                         ir_initializer_t *sub
328                                 = get_initializer_compound_value(initializer, i);
329                         if (initializer_constant_on_wrong_irg(sub))
330                                 return 1;
331                 }
332                 return 0;
333         }
334         }
335         panic("invalid initializer in initializer_on_wrong_irg");
336 }
337
338 /**
339  * Check if constants node are NOT on the constant IR graph.
340  *
341  * @return NON-zero if an entity initializer constant is NOT on
342  * the current_ir_graph's obstack.
343  */
344 static int constants_on_wrong_irg(ir_entity *ent)
345 {
346         if (ent->initializer != NULL) {
347                 return initializer_constant_on_wrong_irg(ent->initializer);
348         } else if (entity_has_compound_ent_values(ent)) {
349                 size_t i, n;
350                 for (i = 0, n = get_compound_ent_n_values(ent); i < n; ++i) {
351                         if (constant_on_wrong_irg(get_compound_ent_value(ent, i)))
352                                 return 1;
353                 }
354         }
355         return 0;
356 }
357
358 /*
359  * Check an entity. Currently, we check only if initialized constants
360  * are build on the const irg graph.
361  *
362  * @return
363  *  0   if no error encountered
364  *  != 0    a trverify_error_codes code
365  */
366 int check_entity(ir_entity *ent)
367 {
368         ir_type *tp = get_entity_type(ent);
369
370         current_ir_graph =  get_const_code_irg();
371         ASSERT_AND_RET_DBG(
372                 constants_on_wrong_irg(ent) == 0,
373                 "Contants placed on wrong IRG",
374                 error_const_on_wrong_irg,
375                 ir_fprintf(stderr, "%+e not on %+F\n", ent, current_ir_graph)
376         );
377
378         /* Originally, this test assumed, that only method entities have
379            pecularity_inherited.  As I changed this, I have to test for method type
380            before doing the test. */
381         if (get_entity_peculiarity(ent) == peculiarity_existent
382             && is_method_entity(ent)) {
383
384                 ir_entity *impl = get_SymConst_entity(get_atomic_ent_value(ent));
385                 ASSERT_AND_RET_DBG(
386                         impl != NULL,
387                         "inherited method entities must have constant pointing to existent entity.",
388                         error_inherited_ent_without_const,
389                         ir_fprintf(stderr, "%+e points to %+e\n", ent, impl)
390                 );
391         }
392
393         if (is_atomic_entity(ent) && ent->initializer != NULL) {
394                 ir_mode *mode = NULL;
395                 ir_initializer_t *initializer = ent->initializer;
396                 switch (initializer->kind) {
397                 case IR_INITIALIZER_CONST:
398                         mode = get_irn_mode(get_initializer_const_value(initializer));
399                         break;
400                 case IR_INITIALIZER_TARVAL:
401                         mode = get_tarval_mode(get_initializer_tarval_value(initializer));
402                         break;
403                 case IR_INITIALIZER_NULL:
404                 case IR_INITIALIZER_COMPOUND:
405                         break;
406                 }
407                 ASSERT_AND_RET_DBG(
408                         mode == NULL || mode == get_type_mode(tp),
409                         "Mode of constant in entity must match type.",
410                         error_ent_const_mode,
411                         ir_fprintf(stderr, "%+e, type %+F(%+F)\n",
412                         ent, tp, get_type_mode(tp))
413                 );
414         }
415         return no_error;
416 }
417
418 /*
419  * check types and entities
420  */
421 static void check_tore(type_or_ent tore, void *env)
422 {
423         int *res = (int*)env;
424         assert(tore.ent);
425         if (is_type(tore.typ)) {
426                 *res = check_type(tore.typ);
427         } else {
428                 assert(is_entity(tore.ent));
429                 *res = check_entity(tore.ent);
430         }
431 }
432
433 /*
434  * Verify types and entities.
435  */
436 int tr_verify(void)
437 {
438         static ident *empty = NULL;
439         int           res = no_error;
440         ir_type      *constructors;
441         ir_type      *destructors;
442         ir_type      *thread_locals;
443         size_t        i, n;
444         ir_segment_t  s;
445
446         if (empty == NULL)
447                 empty = new_id_from_chars("", 0);
448
449         type_walk(check_tore, NULL, &res);
450
451         for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
452                 const ir_type *type = get_segment_type(s);
453                 size_t         e;
454                 for (e = 0; e < get_compound_n_members(type); ++e) {
455                         ir_entity *entity = get_compound_member(type, e);
456                         ASSERT_AND_RET(get_entity_ld_ident(entity) != NULL ||
457                                         get_entity_visibility(entity) == ir_visibility_private,
458                                         "segment members must have a name or visibility_private",
459                                         1);
460                 }
461         }
462
463         constructors = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
464         for (i = 0, n = get_compound_n_members(constructors); i < n; ++i) {
465                 const ir_entity *entity = get_compound_member(constructors, i);
466                 ASSERT_AND_RET(get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER,
467                                "entity without LINKAGE_HIDDEN_USER in constructors is pointless",
468                                1);
469                 /* Mach-O doesn't like labels in this section */
470                 ASSERT_AND_RET(get_entity_ld_ident(entity),
471                                "entity in constructors should have ld_ident=''", 1);
472         }
473         destructors = get_segment_type(IR_SEGMENT_DESTRUCTORS);
474         for (i = 0, n = get_compound_n_members(destructors); i < n; ++i) {
475                 const ir_entity *entity = get_compound_member(destructors, i);
476                 ASSERT_AND_RET(get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER,
477                                "entity without LINKAGE_HIDDEN_USER in destructors is pointless",
478                                1);
479                 /* Mach-O doesn't like labels in this section */
480                 ASSERT_AND_RET(get_entity_ld_ident(entity),
481                                "entity in destructors should have ld_ident=''", 1);
482         }
483         thread_locals = get_segment_type(IR_SEGMENT_THREAD_LOCAL);
484         for (i = 0, n = get_compound_n_members(thread_locals); i < n; ++i) {
485                 const ir_entity *entity = get_compound_member(thread_locals, i);
486                 /* this is odd and should not be allowed I think */
487                 ASSERT_AND_RET(!is_method_entity(entity),
488                                "method in THREAD_LOCAL segment", 1);
489                 ASSERT_AND_RET(! (get_entity_linkage(entity) & IR_LINKAGE_CONSTANT),
490                                "thread locals must not be constant", 1);
491         }
492
493         return res;
494 }