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