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