implement several new section types including comdat for ELF, this potentially breaks...
[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"
30 #include "irflag_t.h"
31 #include "irprintf.h"
32 #include "irgwalk.h"
33 #include "error.h"
34 #include "tv.h"
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 #endif /* #ifndef NDEBUG */
142
143 /**
144  * Check a class
145  */
146 static int check_class(ir_type *tp) {
147         int i, j, k;
148         int found;
149
150         for (i = get_class_n_members(tp) - 1; i >= 0; --i) {
151                 ir_entity *mem = get_class_member(tp, i);
152
153                 ASSERT_AND_RET_DBG(
154                         tp == get_entity_owner(mem),
155                         "class member with wrong owner",
156                         error_ent_wrong_owner,
157                         ir_fprintf(stderr, "Type verification error:\n%+F %+e(owner %+F)\n",tp, mem, get_entity_owner(mem))
158                 );
159                 ASSERT_AND_RET_DBG(
160                         mem,
161                         "NULL members not allowed",
162                         error_null_mem,
163                         ir_fprintf(stderr, "Type verification error:\n%+F member %d is NULL\n", tp, i)
164                 );
165
166                 ASSERT_AND_RET_DBG(
167                         get_entity_n_overwrites(mem) <= get_class_n_supertypes(tp),
168                         "wrong number of entity overwrites",
169                         error_wrong_ent_overwrites,
170                         show_ent_overwrite_cnt(mem)
171                 );
172
173                 for (j = get_entity_n_overwrites(mem) - 1; j >= 0; --j) {
174                         ir_entity *ovw = get_entity_overwrites(mem, j);
175                         /*printf(" overwrites: "); DDME(ovw);*/
176                         /* Check whether ovw is member of one of tp's supertypes. If so,
177                         the representation is correct. */
178                         found = 0;
179                         for (k = get_class_n_supertypes(tp) - 1; k >= 0; --k) {
180                                 if (get_class_member_index(get_class_supertype(tp, k), ovw) >= 0) {
181                                         found = 1;
182                                         break;
183                                 }
184                         }
185                         ASSERT_AND_RET_DBG(
186                                 found,
187                                 "overwrites an entity not contained in direct supertype",
188                                 error_ent_not_cont,
189                                 show_ent_not_supertp(mem, ovw)
190                         );
191                 }
192         }
193         return 0;
194 }
195
196 /**
197  * Check an array.
198  */
199 static int check_array(ir_type *tp) {
200         int i, n_dim = get_array_n_dimensions(tp);
201
202         for (i = 0; i < n_dim; ++i) {
203                 ASSERT_AND_RET_DBG(
204                         has_array_lower_bound(tp, i) || has_array_upper_bound(tp, i),
205                         "array bound missing",
206                         1,
207                         ir_fprintf(stderr, "%+F in dimension %d\n", tp, i)
208                 );
209         }
210         return 0;
211 }
212
213
214 /**
215  * Check a primitive.
216  */
217 static int check_primitive(ir_type *tp) {
218         ASSERT_AND_RET_DBG(
219                 is_mode(get_type_mode(tp)),
220                 "Primitive type without mode",
221                 1,
222                 ir_fprintf(stderr, "%+F\n", tp)
223         );
224         return 0;
225 }
226
227
228 /*
229  * Checks a type.
230  *
231  * return
232  *  0   if no error encountered
233  */
234 int check_type(ir_type *tp) {
235         switch (get_type_tpop_code(tp)) {
236         case tpo_class:
237                 return check_class(tp);
238         case tpo_array:
239                 return check_array(tp);
240         case tpo_primitive:
241                 return check_primitive(tp);
242         default: break;
243         }
244         return 0;
245 }
246
247 /**
248  * checks the visited flag
249  */
250 static int check_visited_flag(ir_graph *irg, ir_node *n) {
251         ASSERT_AND_RET_DBG(
252                 get_irn_visited(n) <= get_irg_visited(irg),
253                 "Visited flag of node is larger than that of corresponding irg.",
254                 0,
255                 ir_fprintf(stderr, "%+F in %+F\n", n, irg)
256         );
257         return 1;
258 }
259
260 /**
261  * helper environment struct for constant_on_wrong_obstack()
262  */
263 struct myenv {
264         int res;
265         ir_graph *irg;
266 };
267
268 /**
269  * called by the walker
270  */
271 static void on_irg_storage(ir_node *n, void *env) {
272         struct myenv *myenv = env;
273
274         /* We also test whether the setting of the visited flag is legal. */
275         myenv->res = node_is_in_irgs_storage(myenv->irg, n) &&
276                      check_visited_flag(myenv->irg, n);
277 }
278
279 /**
280  * checks whether a given constant IR node is NOT on the
281  * constant IR graph.
282  */
283 static int constant_on_wrong_irg(ir_node *n)
284 {
285         struct myenv env;
286
287         env.res = 1;  /* on right obstack */
288         env.irg = get_const_code_irg();
289
290         irg_walk(n, on_irg_storage, NULL, (void *)&env);
291         return ! env.res;
292 }
293
294 static int initializer_constant_on_wrong_irg(ir_initializer_t *initializer)
295 {
296         switch (get_initializer_kind(initializer)) {
297         case IR_INITIALIZER_NULL:
298                 return 0;
299         case IR_INITIALIZER_TARVAL:
300                 return 0;
301         case IR_INITIALIZER_CONST:
302                 return constant_on_wrong_irg(get_initializer_const_value(initializer));
303         case IR_INITIALIZER_COMPOUND: {
304                 int n = get_initializer_compound_n_entries(initializer);
305                 int i;
306                 for (i = 0; i < n; ++i) {
307                         ir_initializer_t *sub
308                                 = get_initializer_compound_value(initializer, i);
309                         if (initializer_constant_on_wrong_irg(sub))
310                                 return 1;
311                 }
312                 return 0;
313         }
314         }
315         panic("invalid initializer in initializer_on_wrong_irg");
316 }
317
318 /**
319  * Check if constants node are NOT on the constant IR graph.
320  *
321  * @return NON-zero if an entity initializer constant is NOT on
322  * the current_ir_graph's obstack.
323  */
324 static int constants_on_wrong_irg(ir_entity *ent)
325 {
326         if (ent->initializer != NULL) {
327                 return initializer_constant_on_wrong_irg(ent->initializer);
328         } else if (entity_has_compound_ent_values(ent)) {
329                 int i;
330                 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
331                         if (constant_on_wrong_irg(get_compound_ent_value(ent, i)))
332                                 return 1;
333                 }
334         }
335         return 0;
336 }
337
338 /*
339  * Check an entity. Currently, we check only if initialized constants
340  * are build on the const irg graph.
341  *
342  * @return
343  *  0   if no error encountered
344  *  != 0    a trvrfy_error_codes code
345  */
346 int check_entity(ir_entity *ent)
347 {
348         ir_type *tp = get_entity_type(ent);
349
350         current_ir_graph =  get_const_code_irg();
351         ASSERT_AND_RET_DBG(
352                 constants_on_wrong_irg(ent) == 0,
353                 "Contants placed on wrong IRG",
354                 error_const_on_wrong_irg,
355                 ir_fprintf(stderr, "%+e not on %+F\n", ent, current_ir_graph)
356         );
357
358         /* Originally, this test assumed, that only method entities have
359            pecularity_inherited.  As I changed this, I have to test for method type
360            before doing the test. */
361         if (get_entity_peculiarity(ent) == peculiarity_existent
362             && is_method_entity(ent)) {
363
364                 ir_entity *impl = get_SymConst_entity(get_atomic_ent_value(ent));
365                 ASSERT_AND_RET_DBG(
366                         impl != NULL,
367                         "inherited method entities must have constant pointing to existent entity.",
368                         error_inherited_ent_without_const,
369                         ir_fprintf(stderr, "%+e points to %+e\n", ent, impl)
370                 );
371         }
372
373         if (is_atomic_entity(ent) && ent->initializer != NULL) {
374                 ir_mode *mode = NULL;
375                 ir_initializer_t *initializer = ent->initializer;
376                 switch (initializer->kind) {
377                 case IR_INITIALIZER_CONST:
378                         mode = get_irn_mode(get_initializer_const_value(initializer));
379                         break;
380                 case IR_INITIALIZER_TARVAL:
381                         mode = get_tarval_mode(get_initializer_tarval_value(initializer));
382                         break;
383                 case IR_INITIALIZER_NULL:
384                 case IR_INITIALIZER_COMPOUND:
385                         break;
386                 }
387                 ASSERT_AND_RET_DBG(
388                         mode == NULL || mode == get_type_mode(tp),
389                         "Mode of constant in entity must match type.",
390                         error_ent_const_mode,
391                         ir_fprintf(stderr, "%+e, type %+F(%+F)\n",
392                         ent, tp, get_type_mode(tp))
393                 );
394         }
395         return no_error;
396 }
397
398 /*
399  * check types and entities
400  */
401 static void check_tore(type_or_ent tore, void *env) {
402         int *res = env;
403         assert(tore.ent);
404         if (is_type(tore.typ)) {
405                 *res = check_type(tore.typ);
406         } else {
407                 assert(is_entity(tore.ent));
408                 *res = check_entity(tore.ent);
409         }
410 }
411
412 /*
413  * Verify types and entities.
414  */
415 int tr_vrfy(void)
416 {
417         int      res = no_error;
418         ir_type *constructors;
419         ir_type *destructors;
420         ir_type *thread_locals;
421         int      i;
422         static ident *empty = NULL;
423
424         if (empty == NULL)
425                 empty = new_id_from_chars("", 0);
426
427         type_walk(check_tore, NULL, &res);
428
429         constructors = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
430         for (i = get_compound_n_members(constructors)-1; i >= 0; --i) {
431                 const ir_entity *entity = get_compound_member(constructors, i);
432                 ASSERT_AND_RET(get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER,
433                                "entity without LINKAGE_HIDDEN_USER in constructors is pointless",
434                                1);
435                 /* Mach-O doesn't like labels in this section */
436                 ASSERT_AND_RET(get_entity_ld_ident(entity),
437                                "entity in constructors should have ld_ident=''", 1);
438         }
439         destructors = get_segment_type(IR_SEGMENT_DESTRUCTORS);
440         for (i = get_compound_n_members(destructors)-1; i >= 0; --i) {
441                 const ir_entity *entity = get_compound_member(destructors, i);
442                 ASSERT_AND_RET(get_entity_linkage(entity) & IR_LINKAGE_HIDDEN_USER,
443                                "entity without LINKAGE_HIDDEN_USER in destructors is pointless",
444                                1);
445                 /* Mach-O doesn't like labels in this section */
446                 ASSERT_AND_RET(get_entity_ld_ident(entity),
447                                "entity in destructors should have ld_ident=''", 1);
448         }
449         thread_locals = get_segment_type(IR_SEGMENT_THREAD_LOCAL);
450         for (i = get_compound_n_members(thread_locals)-1; i >= 0; --i) {
451                 const ir_entity *entity = get_compound_member(thread_locals, i);
452                 /* this is odd and should not be allowed I think */
453                 ASSERT_AND_RET(!is_method_entity(entity),
454                                "method in THREAD_LOCAL segment", 1);
455                 ASSERT_AND_RET(! (get_entity_linkage(entity) & IR_LINKAGE_CONSTANT),
456                                "thread locals must not be constant", 1);
457         }
458
459         return res;
460 }