adapt to timer changes
[libfirm] / ir / tr / trvrfy.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"  /* for checking whether constant code is allocated
30                            on proper obstack */
31 #include "irflag_t.h"
32 #include "irprintf.h"
33 #include "irgwalk.h"
34
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_vrfy_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_vrfy_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_vrfy_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         ir_type *owner = get_entity_owner(ent);
87         ir_type *ov_own = get_entity_owner(ovw);
88         int i;
89
90         fprintf(stderr, "Type verification error:\n");
91         ir_fprintf(stderr, "Entity %+F::%+e owerwrites ", owner, ent);
92         ir_fprintf(stderr, "Entity %+F::%+e\n", ov_own, ovw);
93
94         ir_fprintf(stderr, "Supertypes of %+F:\n", owner);
95         for (i = 0; i < get_class_n_supertypes(owner); ++i) {
96                 ir_type *super = get_class_supertype(owner, i);
97                 ir_fprintf(stderr, " %+F:\n", super);
98         }
99 }
100
101 /**
102  * Show diagnostic if an entity overwrites a wrong number of things.
103  */
104 static void show_ent_overwrite_cnt(ir_entity *ent) {
105         ir_type *owner = get_entity_owner(ent);
106         int i, j, k, found, show_stp = 0;
107
108         fprintf(stderr, "Type verification error:\n");
109         ir_fprintf(stderr, "Entity %t::%e owerwrites\n", owner, ent);
110         for (i = 0; i < get_entity_n_overwrites(ent); ++i) {
111                 ir_entity *ovw = get_entity_overwrites(ent, i);
112                 ir_type *ov_own = get_entity_owner(ovw);
113
114                 ir_fprintf(stderr, "  %t::%e\n", ov_own, ovw);
115                 for (k = 0; k < i; ++k)
116                         if (ovw == get_entity_overwrites(ent, k)) {
117                                 ir_fprintf(stderr, "  ->%t::%e entered more than once\n", ov_own, ovw);
118                                 break;
119                         }
120
121                 found = 0;
122                 for (j = get_class_n_supertypes(owner) - 1; j >= 0; --j) {
123                         if (ov_own == get_class_supertype(owner, j)) {
124                                 show_stp = found = 1;
125                                 break;
126                         }
127                 }
128                 if (! found)
129                         ir_fprintf(stderr, "  ->%t not in super types of %t\n", ov_own, owner);
130         }
131
132         if (show_stp) {
133                 ir_fprintf(stderr, "Supertypes of %t:\n", owner);
134                 for (i = 0; i < get_class_n_supertypes(owner); ++i) {
135                         ir_type *super = get_class_supertype(owner, i);
136                         ir_fprintf(stderr, " %t:\n", super);
137                 }
138         }
139 }
140
141 /**
142  * Shows a wrong entity allocation
143  */
144 static void show_ent_alloc_error(ir_entity *ent) {
145         ir_fprintf(stderr, "%+e owner %t has allocation %s\n",
146                 ent, get_entity_type(ent),
147                 get_allocation_name(get_entity_allocation(ent)));
148 }
149
150 #endif /* #ifndef NDEBUG */
151
152 /**
153  * Check a class
154  */
155 static int check_class(ir_type *tp) {
156         int i, j, k;
157         int found;
158
159         for (i = get_class_n_members(tp) - 1; i >= 0; --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 %d 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                 for (j = get_entity_n_overwrites(mem) - 1; j >= 0; --j) {
183                         ir_entity *ovw = get_entity_overwrites(mem, j);
184                         /*printf(" overwrites: "); DDME(ovw);*/
185                         /* Check whether ovw is member of one of tp's supertypes. If so,
186                         the representation is correct. */
187                         found = 0;
188                         for (k = get_class_n_supertypes(tp) - 1; k >= 0; --k) {
189                                 if (get_class_member_index(get_class_supertype(tp, k), ovw) >= 0) {
190                                         found = 1;
191                                         break;
192                                 }
193                         }
194                         ASSERT_AND_RET_DBG(
195                                 found,
196                                 "overwrites an entity not contained in direct supertype",
197                                 error_ent_not_cont,
198                                 show_ent_not_supertp(mem, ovw)
199                         );
200                 }
201         }
202         return 0;
203 }
204
205 /**
206  * Check an array.
207  */
208 static int check_array(ir_type *tp) {
209         int i, n_dim = get_array_n_dimensions(tp);
210
211         for (i = 0; i < n_dim; ++i) {
212                 ASSERT_AND_RET_DBG(
213                         has_array_lower_bound(tp, i) || has_array_upper_bound(tp, i),
214                         "array bound missing",
215                         1,
216                         ir_fprintf(stderr, "%+F in dimension %d\n", tp, i)
217                 );
218         }
219         return 0;
220 }
221
222
223 /**
224  * Check a primitive.
225  */
226 static int check_primitive(ir_type *tp) {
227         ASSERT_AND_RET_DBG(
228                 is_mode(get_type_mode(tp)),
229                 "Primitive type without mode",
230                 1,
231                 ir_fprintf(stderr, "%+F\n", tp)
232         );
233         return 0;
234 }
235
236
237 /*
238  * Checks a type.
239  *
240  * return
241  *  0   if no error encountered
242  */
243 int check_type(ir_type *tp) {
244         switch (get_type_tpop_code(tp)) {
245         case tpo_class:
246                 return check_class(tp);
247         case tpo_array:
248                 return check_array(tp);
249         case tpo_primitive:
250                 return check_primitive(tp);
251         default: break;
252         }
253         return 0;
254 }
255
256 /**
257  * checks the visited flag
258  */
259 static int check_visited_flag(ir_graph *irg, ir_node *n) {
260         ASSERT_AND_RET_DBG(
261                 get_irn_visited(n) <= get_irg_visited(irg),
262                 "Visited flag of node is larger than that of corresponding irg.",
263                 0,
264                 ir_fprintf(stderr, "%+F in %+F\n", n, irg)
265         );
266         return 1;
267 }
268
269 /**
270  * helper environment struct for constant_on_wrong_obstack()
271  */
272 struct myenv {
273         int res;
274         ir_graph *irg;
275 };
276
277 /**
278  * called by the walker
279  */
280 static void on_irg_storage(ir_node *n, void *env) {
281         struct myenv *myenv = env;
282
283         /* We also test whether the setting of the visited flag is legal. */
284         myenv->res = node_is_in_irgs_storage(myenv->irg, n) &&
285                      check_visited_flag(myenv->irg, n);
286 }
287
288 /**
289  * checks whether a given constant IR node is NOT on the
290  * constant IR graph.
291  */
292 static int constant_on_wrong_irg(ir_node *n) {
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 /**
303  * Check if constants node are NOT on the constant IR graph.
304  *
305  * @return NON-zero if an entity initializer constant is NOT on
306  * the current_ir_graph's obstack.
307  */
308 static int constants_on_wrong_irg(ir_entity *ent) {
309         if (get_entity_variability(ent) == variability_uninitialized) return 0;
310
311         if (is_compound_entity(ent)) {
312                 if(!ent->has_initializer) {
313                         int i;
314                         for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
315                                 if (constant_on_wrong_irg(get_compound_ent_value(ent, i)))
316                                         return 1;
317                         }
318                 }
319         } else {
320                 /* Might not be set if entity belongs to a description or is external allocated. */
321                 if (get_atomic_ent_value(ent))
322                         return constant_on_wrong_irg(get_atomic_ent_value(ent));
323                 else if (get_entity_visibility(ent) != visibility_external_allocated) {
324                         ASSERT_AND_RET_DBG(
325                                 is_Class_type(get_entity_owner(ent)) &&
326                                 get_class_peculiarity(get_entity_owner(ent)) == peculiarity_description,
327                                 "Value in constant atomic entity not set.",
328                                 0,
329                                 ir_fprintf(stderr, "%+e, owner %+F\n", ent, get_entity_owner(ent))
330                         );
331                 }
332         }
333         return 0;
334 }
335
336 /*
337  * Check an entity. Currently, we check only if initialized constants
338  * are build on the const irg graph.
339  *
340  * @return
341  *  0   if no error encountered
342  *  != 0    a trvrfy_error_codes code
343  */
344 int check_entity(ir_entity *ent) {
345         int rem_vpi;
346         ir_type *tp = get_entity_type(ent);
347         ir_type *owner = get_entity_owner(ent);
348
349         current_ir_graph =  get_const_code_irg();
350         ASSERT_AND_RET_DBG(
351                 constants_on_wrong_irg(ent) == 0,
352                 "Contants placed on wrong IRG",
353                 error_const_on_wrong_irg,
354                 ir_fprintf(stderr, "%+e not on %+F\n", ent, current_ir_graph)
355         );
356
357         rem_vpi = get_visit_pseudo_irgs();
358         set_visit_pseudo_irgs(1);
359         if ((get_entity_peculiarity(ent) == peculiarity_existent)         &&
360             (get_entity_visibility(ent) != visibility_external_allocated) &&
361             (is_Method_type(get_entity_type(ent)))                        &&
362             (!get_entity_irg(ent) || !(is_ir_graph(get_entity_irg(ent))))) {
363                 ASSERT_AND_RET_DBG(
364                         0,
365                         "Method ents with pec_exist must have an irg",
366                         error_existent_entity_without_irg,
367                         ir_fprintf(stderr, "%+e\n", ent)
368                 );
369         }
370         set_visit_pseudo_irgs(rem_vpi);
371
372         /* Originally, this test assumed, that only method entities have
373            pecularity_inherited.  As I changed this, I have to test for method type before
374            doing the test. */
375         if (get_entity_peculiarity(ent) == peculiarity_inherited) {
376                 if (is_Method_type(get_entity_type(ent))) {
377                         ir_entity *impl = get_SymConst_entity(get_atomic_ent_value(ent));
378                         ASSERT_AND_RET_DBG(
379                                 get_entity_peculiarity(impl) == peculiarity_existent,
380                                 "inherited method entities must have constant pointing to existent entity.",
381                                 error_inherited_ent_without_const,
382                                 ir_fprintf(stderr, "%+e points to %+e\n", ent, impl)
383                         );
384                 }
385         }
386
387         /* Entities in global type are not dynamic or automatic allocated. */
388         if (owner == get_glob_type()) {
389                 ASSERT_AND_RET_DBG(
390                         get_entity_allocation(ent) != allocation_dynamic &&
391                         get_entity_allocation(ent) != allocation_automatic,
392                         "Entities in global type are not allowed to by dynamic or automatic allocated",
393                         error_glob_ent_allocation,
394                         show_ent_alloc_error(ent)
395                 );
396         }
397
398         if (get_entity_variability(ent) != variability_uninitialized) {
399                 if (is_atomic_type(tp)) {
400                         ir_node *val = get_atomic_ent_value(ent);
401                         if (val) {
402                                 ASSERT_AND_RET_DBG(
403                                         get_irn_mode(val) == get_type_mode(tp),
404                                         "Mode of constant in entity must match type.",
405                                         error_ent_const_mode,
406                                         ir_fprintf(stderr, "%+e const %+F, type %+F(%+F)\n",
407                                         ent, val, tp, get_type_mode(tp))
408                                 );
409                         }
410                 }
411         }
412         return no_error;
413 }
414
415 /*
416  * check types and entities
417  */
418 static void check_tore(type_or_ent tore, void *env) {
419         int *res = 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_vrfy(void) {
433         int res;
434
435         type_walk(check_tore, NULL, &res);
436         return res;
437 }