fixed the fix of the fix (that was fix)
[libfirm] / ir / tr / trvrfy.c
1 /*
2  * Copyright (C) 1995-2007 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 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include "irgraph_t.h"  /* for checking whether constant code is allocated
32                            on proper obstack */
33 #include "irflag_t.h"
34 #include "irprintf.h"
35 #include "irgwalk.h"
36
37 static const char *firm_vrfy_failure_msg;
38
39 #ifdef NDEBUG
40 /*
41  * in RELEASE mode, returns ret if the expression expr evaluates to zero
42  * in ASSERT mode, asserts the expression expr (and the string string).
43  */
44 #define ASSERT_AND_RET(expr, string, ret)       if (!(expr)) return (ret)
45
46 /*
47  * in RELEASE mode, returns ret if the expression expr evaluates to zero
48  * in ASSERT mode, executes blk if the expression expr evaluates to zero and asserts expr
49  */
50 #define ASSERT_AND_RET_DBG(expr, string, ret, blk)      if (!(expr)) return (ret)
51 #else
52 #define ASSERT_AND_RET(expr, string, ret) \
53 do { \
54   if (opt_do_node_verification == FIRM_VERIFICATION_ON) {\
55     assert((expr) && string); } \
56   if (!(expr)) { \
57     if (opt_do_node_verification == FIRM_VERIFICATION_REPORT) \
58       fprintf(stderr, #expr " : " string "\n"); \
59     firm_vrfy_failure_msg = #expr " && " string; \
60     return (ret); \
61   } \
62 } while(0)
63
64 #define ASSERT_AND_RET_DBG(expr, string, ret, blk) \
65 do { \
66   if (!(expr)) { \
67     firm_vrfy_failure_msg = #expr " && " string; \
68     if (opt_do_node_verification != FIRM_VERIFICATION_ERROR_ONLY) { blk; } \
69     if (opt_do_node_verification == FIRM_VERIFICATION_REPORT) \
70       fprintf(stderr, #expr " : " string "\n"); \
71     else if (opt_do_node_verification == FIRM_VERIFICATION_ON) { \
72       assert((expr) && string); \
73     } \
74     return (ret); \
75   } \
76 } while(0)
77
78 #endif /* NDEBUG */
79
80 /**
81  * Show diagnostic if an entity overwrites another one not
82  * in direct superclasses.
83  */
84 static void show_ent_not_supertp(ir_entity *ent, ir_entity *ovw) {
85         ir_type *owner = get_entity_owner(ent);
86         ir_type *ov_own = get_entity_owner(ovw);
87         int i;
88
89         fprintf(stderr, "Type verification error:\n");
90         ir_fprintf(stderr, "Entity %+F::%+e owerwrites ", owner, ent);
91         ir_fprintf(stderr, "Entity %+F::%+e\n", ov_own, ovw);
92
93         ir_fprintf(stderr, "Supertypes of %+F:\n", owner);
94         for (i = 0; i < get_class_n_supertypes(owner); ++i) {
95                 ir_type *super = get_class_supertype(owner, i);
96                 ir_fprintf(stderr, " %+F:\n", super);
97         }
98 }
99
100 /**
101  * Show diagnostic if an entity overwrites a wrong number of things.
102  */
103 static void show_ent_overwrite_cnt(ir_entity *ent) {
104         ir_type *owner = get_entity_owner(ent);
105         int i, j, k, found, show_stp = 0;
106
107         fprintf(stderr, "Type verification error:\n");
108         ir_fprintf(stderr, "Entity %t::%e owerwrites\n", owner, ent);
109         for (i = 0; i < get_entity_n_overwrites(ent); ++i) {
110                 ir_entity *ovw = get_entity_overwrites(ent, i);
111                 ir_type *ov_own = get_entity_owner(ovw);
112
113                 ir_fprintf(stderr, "  %t::%e\n", ov_own, ovw);
114                 for (k = 0; k < i; ++k)
115                         if (ovw == get_entity_overwrites(ent, k)) {
116                                 ir_fprintf(stderr, "  ->%t::%e entered more than once\n", ov_own, ovw);
117                                 break;
118                         }
119
120                 found = 0;
121                 for (j = get_class_n_supertypes(owner) - 1; j >= 0; --j) {
122                         if (ov_own == get_class_supertype(owner, j)) {
123                                 show_stp = found = 1;
124                                 break;
125                         }
126                 }
127                 if (! found)
128                         ir_fprintf(stderr, "  ->%t not in super types of %t\n", ov_own, owner);
129         }
130
131         if (show_stp) {
132                 ir_fprintf(stderr, "Supertypes of %t:\n", owner);
133                 for (i = 0; i < get_class_n_supertypes(owner); ++i) {
134                         ir_type *super = get_class_supertype(owner, i);
135                         ir_fprintf(stderr, " %t:\n", super);
136                 }
137         }
138 }
139
140 /**
141  * Check a class
142  */
143 static int check_class(ir_type *tp) {
144         int i, j, k;
145         int found;
146
147         for (i = get_class_n_members(tp) - 1; i >= 0; --i) {
148                 ir_entity *mem = get_class_member(tp, i);
149
150                 ASSERT_AND_RET_DBG(
151                         tp == get_entity_owner(mem),
152                         "class member with wrong owner",
153                         error_ent_wrong_owner,
154                         ir_fprintf(stderr, "Type verification error:\n%+F %+e(owner %+F)\n",tp, mem, get_entity_owner(mem))
155                 );
156                 ASSERT_AND_RET_DBG(
157                         mem,
158                         "NULL members not allowed",
159                         error_null_mem,
160                         ir_fprintf(stderr, "Type verification error:\n%+F member %d is NULL\n", tp, i)
161                 );
162
163                 ASSERT_AND_RET_DBG(
164                         get_entity_n_overwrites(mem) <= get_class_n_supertypes(tp),
165                         "wrong number of entity overwrites",
166                         error_wrong_ent_overwrites,
167                         show_ent_overwrite_cnt(mem)
168                 );
169
170                 for (j = get_entity_n_overwrites(mem) - 1; j >= 0; --j) {
171                         ir_entity *ovw = get_entity_overwrites(mem, j);
172                         /*printf(" overwrites: "); DDME(ovw);*/
173                         /* Check whether ovw is member of one of tp's supertypes. If so,
174                         the representation is correct. */
175                         found = 0;
176                         for (k = get_class_n_supertypes(tp) - 1; k >= 0; --k) {
177                                 if (get_class_member_index(get_class_supertype(tp, k), ovw) >= 0) {
178                                         found = 1;
179                                         break;
180                                 }
181                         }
182                         ASSERT_AND_RET_DBG(
183                                 found,
184                                 "overwrites an entity not contained in direct supertype",
185                                 error_ent_not_cont,
186                                 show_ent_not_supertp(mem, ovw)
187                         );
188                 }
189         }
190         return 0;
191 }
192
193 /**
194  * Check an array.
195  */
196 static int check_array(ir_type *tp) {
197         int i, n_dim = get_array_n_dimensions(tp);
198
199         for (i = 0; i < n_dim; ++i) {
200                 ASSERT_AND_RET_DBG(
201                         has_array_lower_bound(tp, i) || has_array_upper_bound(tp, i),
202                         "array bound missing",
203                         1,
204                         ir_fprintf(stderr, "%+F in dimension %d\n", tp, i)
205                 );
206         }
207         return 0;
208 }
209
210
211 /**
212  * Check a primitive.
213  */
214 static int check_primitive(ir_type *tp) {
215         ASSERT_AND_RET_DBG(
216                 is_mode(get_type_mode(tp)),
217                 "Primitive type without mode",
218                 1,
219                 ir_fprintf(stderr, "%+F\n", tp)
220         );
221         return 0;
222 }
223
224
225 /*
226  * Checks a type.
227  *
228  * return
229  *  0   if no error encountered
230  */
231 int check_type(ir_type *tp) {
232         switch (get_type_tpop_code(tp)) {
233         case tpo_class:
234                 return check_class(tp);
235         case tpo_array:
236                 return check_array(tp);
237         case tpo_primitive:
238                 return check_primitive(tp);
239         default: break;
240         }
241         return 0;
242 }
243
244 /**
245  * checks the visited flag
246  */
247 static int check_visited_flag(ir_graph *irg, ir_node *n) {
248         ASSERT_AND_RET_DBG(
249                 get_irn_visited(n) <= get_irg_visited(irg),
250                 "Visited flag of node is larger than that of corresponding irg.",
251                 0,
252                 ir_fprintf(stderr, "%+F in %+F\n", n, irg)
253         );
254         return 1;
255 }
256
257 /**
258  * helper environment struct for constant_on_wrong_obstack()
259  */
260 struct myenv {
261         int res;
262         ir_graph *irg;
263 };
264
265 /**
266  * called by the walker
267  */
268 static void on_irg_storage(ir_node *n, void *env) {
269         struct myenv *myenv = env;
270
271         /* We also test whether the setting of the visited flag is legal. */
272         myenv->res = node_is_in_irgs_storage(myenv->irg, n) &&
273                      check_visited_flag(myenv->irg, n);
274 }
275
276 /**
277  * checks whether a given constant IR node is NOT on the
278  * constant IR graph.
279  */
280 static int constant_on_wrong_irg(ir_node *n) {
281         struct myenv env;
282
283         env.res = 1;  /* on right obstack */
284         env.irg = get_const_code_irg();
285
286         irg_walk(n, on_irg_storage, NULL, (void *)&env);
287         return ! env.res;
288 }
289
290 /**
291  * Check if constants node are NOT on the constant IR graph.
292  *
293  * @return NON-zero if an entity initializer constant is NOT on
294  * the current_ir_graph's obstack.
295  */
296 static int constants_on_wrong_irg(ir_entity *ent) {
297         if (get_entity_variability(ent) == variability_uninitialized) return 0;
298
299         if (is_compound_entity(ent)) {
300                 int i;
301                 for (i = 0; i < get_compound_ent_n_values(ent); i++) {
302                         if (constant_on_wrong_irg(get_compound_ent_value(ent, i)))
303                                 return 1;
304                 }
305         } else {
306                 /* Might not be set if entity belongs to a description or is external allocated. */
307                 if (get_atomic_ent_value(ent))
308                         return constant_on_wrong_irg(get_atomic_ent_value(ent));
309                 else if (get_entity_visibility(ent) != visibility_external_allocated) {
310                         ASSERT_AND_RET_DBG(
311                                 is_Class_type(get_entity_owner(ent)) &&
312                                 get_class_peculiarity(get_entity_owner(ent)) == peculiarity_description,
313                                 "Value in constant atomic entity not set.",
314                                 0,
315                                 ir_fprintf(stderr, "%+e, owner %+F\n", ent, get_entity_owner(ent))
316                         );
317                 }
318         }
319         return 0;
320 }
321
322 /**
323  * Shows a wrong entity allocation
324  */
325 static void show_ent_alloc_error(ir_entity *ent) {
326         ir_fprintf(stderr, "%+e owner %t has allocation %s\n",
327                 ent, get_entity_type(ent),
328                 get_allocation_name(get_entity_allocation(ent)));
329 }
330
331 /*
332  * Check an entity. Currently, we check only if initialized constants
333  * are build on the const irg graph.
334  *
335  * @return
336  *  0   if no error encountered
337  *  != 0    a trvrfy_error_codes code
338  */
339 int check_entity(ir_entity *ent) {
340         int rem_vpi;
341         ir_type *tp = get_entity_type(ent);
342         ir_type *owner = get_entity_owner(ent);
343
344         current_ir_graph =  get_const_code_irg();
345         ASSERT_AND_RET_DBG(
346                 constants_on_wrong_irg(ent) == 0,
347                 "Contants placed on wrong IRG",
348                 error_const_on_wrong_irg,
349                 ir_fprintf(stderr, "%+e not on %+F\n", ent, current_ir_graph)
350         );
351
352         rem_vpi = get_visit_pseudo_irgs();
353         set_visit_pseudo_irgs(1);
354         if ((get_entity_peculiarity(ent) == peculiarity_existent)         &&
355             (get_entity_visibility(ent) != visibility_external_allocated) &&
356             (is_Method_type(get_entity_type(ent)))                        &&
357             (!get_entity_irg(ent) || !(is_ir_graph(get_entity_irg(ent))))) {
358                 ASSERT_AND_RET_DBG(
359                         0,
360                         "Method ents with pec_exist must have an irg",
361                         error_existent_entity_without_irg,
362                         ir_fprintf(stderr, "%+e\n", ent)
363                 );
364         }
365         set_visit_pseudo_irgs(rem_vpi);
366
367         /* Originally, this test assumed, that only method entities have
368            pecularity_inherited.  As I changed this, I have to test for method type before
369            doing the test. */
370         if (get_entity_peculiarity(ent) == peculiarity_inherited) {
371                 if (is_Method_type(get_entity_type(ent))) {
372                         ir_entity *impl = get_SymConst_entity(get_atomic_ent_value(ent));
373                         ASSERT_AND_RET_DBG(
374                                 get_entity_peculiarity(impl) == peculiarity_existent,
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
382         /* Entities in global type are not dynamic or automatic allocated. */
383         if (owner == get_glob_type()) {
384                 ASSERT_AND_RET_DBG(
385                         get_entity_allocation(ent) != allocation_dynamic &&
386                         get_entity_allocation(ent) != allocation_automatic,
387                         "Entities in global type are not allowed to by dynamic or automatic allocated",
388                         error_glob_ent_allocation,
389                         show_ent_alloc_error(ent)
390                 );
391         }
392
393         if (get_entity_variability(ent) != variability_uninitialized) {
394                 if (is_atomic_type(tp)) {
395                         ir_node *val = get_atomic_ent_value(ent);
396                         if (val) {
397                                 ASSERT_AND_RET_DBG(
398                                         get_irn_mode(val) == get_type_mode(tp),
399                                         "Mode of constant in entity must match type.",
400                                         error_ent_const_mode,
401                                         ir_fprintf(stderr, "%+e const %+F, type %+F(%+F)\n",
402                                         ent, val, tp, get_type_mode(tp))
403                                 );
404                         }
405                 }
406         }
407         return no_error;
408 }
409
410 /*
411  * check types and entities
412  */
413 static void check_tore(type_or_ent *tore, void *env) {
414         int *res = env;
415         assert(tore);
416         if (is_type(tore)) {
417                 *res = check_type((ir_type *)tore);
418         } else {
419                 assert(is_entity(tore));
420                 *res = check_entity((ir_entity *)tore);
421         }
422 }
423
424 /*
425  * Verify types and entities.
426  */
427 int tr_vrfy(void) {
428         int res;
429
430         type_walk(check_tore, NULL, &res);
431         return res;
432 }