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