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