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