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