Shows the block for floating nodes
[libfirm] / ir / ir / irvrfy.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irvrfy.c
4  * Purpose:     Check irnodes for correctness.
5  * Author:      Christian Schaefer
6  * Modified by: Goetz Lindenmaier. Till Riedel
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
16
17 # include "irprog.h"
18 # include "irgraph_t.h"
19 # include "irvrfy.h"
20 # include "irgwalk.h"
21 # include "irdump.h"
22
23 #ifdef NDEBUG
24 /*
25  * in RELEASE mode, returns ret if the expression expr evaluates to zero
26  * in ASSERT mode, asserts the expression expr (and the string string).
27  */
28 #define ASSERT_AND_RET(expr, string, ret)       if (!(expr)) return (ret)
29
30 /*
31  * in RELEASE mode, returns ret if the expression expr evaluates to zero
32  * in ASSERT mode, executes blk if the expression expr evaluates to zero and asserts expr
33  */
34 #define ASSERT_AND_RET_DBG(expr, string, ret, blk)      if (!(expr)) return (ret)
35 #else
36 #define ASSERT_AND_RET(expr, string, ret) \
37 do { \
38   if (opt_do_node_verification == NODE_VERIFICATION_ON) {\
39     if (!(expr)) dump_ir_block_graph(current_ir_graph, "-assert"); \
40     assert((expr) && string); } \
41   if (!(expr)) { \
42     if (opt_do_node_verification == NODE_VERIFICATION_REPORT) \
43       fprintf(stderr, #expr " : " string "\n"); \
44     bad_msg = #expr " && " string; \
45     return (ret); \
46   } \
47 } while(0)
48
49 #define ASSERT_AND_RET_DBG(expr, string, ret, blk) \
50 do { \
51   if (!(expr)) { \
52     bad_msg = #expr " && " string; \
53     if (opt_do_node_verification != NODE_VERIFICATION_ERROR_ONLY) { blk; } \
54     if (opt_do_node_verification == NODE_VERIFICATION_REPORT) \
55       fprintf(stderr, #expr " : " string "\n"); \
56     else if (opt_do_node_verification == NODE_VERIFICATION_ON) \
57       assert((expr) && string); \
58     return (ret); \
59   } \
60 } while(0)
61
62 #endif
63
64 /* @@@ replace use of array "in" by access functions. */
65 ir_node **get_irn_in(ir_node *node);
66
67 static node_verification_t opt_do_node_verification = NODE_VERIFICATION_ON;
68 static const char *bad_msg;
69
70 /**
71  * little helper for NULL modes
72  */
73 static const char *get_mode_name_ex(ir_mode *mode)
74 {
75   if (! mode)
76     return "<no mode>";
77   return get_mode_name(mode);
78 }
79
80 void do_node_verification(node_verification_t mode)
81 {
82   opt_do_node_verification = mode;
83 }
84
85 /**
86  * Prints a failure for a Node
87  */
88 static void show_node_failure(ir_node *n)
89 {
90   fprintf(stderr, "\nFIRM: irn_vrfy_irg() of node %ld %s%s\n" ,
91     get_irn_node_nr(n),
92     get_irn_opname(n), get_irn_modename(n)
93   );
94 }
95
96 /**
97  * Prints a failure message for a binop
98  */
99 static void show_binop_failure(ir_node *n, const char *text)
100 {
101   ir_node *left  = get_binop_left(n);
102   ir_node *right = get_binop_right(n);
103
104   fprintf(stderr, "\nFIRM: irn_vrfy_irg() of node %ld %s%s(%s%s, %s%s) did not match (%s)\n",
105       get_irn_node_nr(n),
106       get_irn_opname(n), get_irn_modename(n),
107       get_irn_opname(left), get_irn_modename(left),
108       get_irn_opname(right), get_irn_modename(right),
109       text);
110 }
111
112 /**
113  * Prints a failure message for an unop
114  */
115 static void show_unop_failure(ir_node *n, const char *text)
116 {
117   ir_node *op  = get_unop_op(n);
118
119   fprintf(stderr, "\nFIRM: irn_vrfy_irg() of node %ld %s%s(%s%s) did not match (%s)\n",
120       get_irn_node_nr(n),
121       get_irn_opname(n), get_irn_modename(n),
122       get_irn_opname(op), get_irn_modename(op),
123       text);
124 }
125
126 /**
127  * Prints a failure message for a proj
128  */
129 static void show_proj_failure(ir_node *n)
130 {
131   ir_node *op  = get_Proj_pred(n);
132   int proj     = get_Proj_proj(n);
133
134   fprintf(stderr, "\nFIRM: irn_vrfy_irg() of node %ld %s%s %d(%s%s) failed\n" ,
135       get_irn_node_nr(n),
136       get_irn_opname(n), get_irn_modename(n), proj,
137       get_irn_opname(op), get_irn_modename(op));
138 }
139
140 /**
141  * Prints a failure message for a proj
142  */
143 static void show_proj_failure_ent(ir_node *n, entity *ent)
144 {
145   ir_node *op  = get_Proj_pred(n);
146   int proj     = get_Proj_proj(n);
147   ir_mode *m   = get_type_mode(get_entity_type(ent));
148
149   fprintf(stderr, "\nFIRM: irn_vrfy_irg() of node %ld %s%s %d(%s%s) entity %s(type %s mode %s)failed\n" ,
150       get_irn_node_nr(n),
151       get_irn_opname(n), get_irn_modename(n), proj,
152       get_irn_opname(op), get_irn_modename(op),
153       get_entity_name(ent), get_type_name(get_entity_type(ent)),
154       get_mode_name_ex(m));
155 }
156
157 /**
158  * Show a node and a graph
159  */
160 static void show_node_on_graph(ir_graph *irg, ir_node *n)
161 {
162   entity *ent = get_irg_entity(irg);
163
164   if (ent)
165     fprintf(stderr, "\nFIRM: irn_vrfy_irg() of entity %s, node %ld %s%s\n",
166       get_entity_name(ent),
167       get_irn_node_nr(n), get_irn_opname(n), get_irn_modename(n));
168   else
169     fprintf(stderr, "\nFIRM: irn_vrfy_irg() of graph %p, node %ld %s%s\n",
170       (void *)irg,
171       get_irn_node_nr(n), get_irn_opname(n), get_irn_modename(n));
172 }
173
174 /**
175  * Show call params
176  */
177 static void show_call_param(ir_node *n, type *mt)
178 {
179   int i;
180
181   fprintf(stderr, "\nFIRM: irn_vrfy_irg() Call type-check failed: %s(", get_type_name(mt));
182   for (i = 0; i < get_method_n_params(mt); ++i) {
183     fprintf(stderr, "%s ", get_mode_name_ex(get_type_mode(get_method_param_type(mt, i))));
184   }
185   fprintf(stderr, ") != CALL(");
186
187   for (i = 0; i < get_Call_n_params(n); ++i) {
188     fprintf(stderr, "%s ", get_mode_name_ex(get_irn_mode(get_Call_param(n, i))));
189   }
190   fprintf(stderr, ")\n");
191
192 }
193
194 /**
195  * Show return modes
196  */
197 static void show_return_modes(ir_graph *irg, ir_node *n, type *mt, int i)
198 {
199   entity *ent = get_irg_entity(irg);
200
201   fprintf(stderr, "\nFIRM: irn_vrfy_irg() Return node %ld in entity \"%s\" mode %s different from type mode %s\n",
202     get_irn_node_nr(n), get_entity_name(ent),
203     get_mode_name_ex(get_irn_mode(get_Return_res(n, i))),
204     get_mode_name_ex(get_type_mode(get_method_res_type(mt, i)))
205   );
206 }
207
208 /**
209  * Show return number of results
210  */
211 static void show_return_nres(ir_graph *irg, ir_node *n, type *mt)
212 {
213   entity *ent = get_irg_entity(irg);
214
215   fprintf(stderr, "\nFIRM: irn_vrfy_irg() Return node %ld in entity \"%s\" has %d results different from type %d\n",
216     get_irn_node_nr(n), get_entity_name(ent),
217     get_Return_n_ress(n), get_method_n_ress(mt));
218 }
219
220 /**
221  * Show Phi input
222  */
223 static void show_phi_failure(ir_node *phi, ir_node *pred, int pos)
224 {
225   fprintf(stderr, "\nFIRM: irn_vrfy_irg() Phi node %ld has mode %s different from predeccessor node %ld mode %s\n",
226     get_irn_node_nr(phi), get_mode_name_ex(get_irn_mode(phi)),
227     get_irn_node_nr(pred), get_mode_name_ex(get_irn_mode(pred)));
228 }
229
230 /**
231  * Show Phi inputs
232  */
233 static void show_phi_inputs(ir_node *phi, ir_node *block)
234 {
235   fprintf(stderr, "\nFIRM: irn_vrfy_irg() Phi node %ld has %d inputs, its Block %ld has %d\n",
236     get_irn_node_nr(phi),   get_irn_arity(phi),
237     get_irn_node_nr(block), get_irn_arity(block));
238 }
239
240 /* If the address is Sel or SymConst, return the entity. */
241 static entity *get_ptr_entity(ir_node *ptr) {
242   if (get_irn_op(ptr) == op_Sel) {
243     return get_Sel_entity(ptr);
244   } else if ((get_irn_op(ptr) == op_SymConst) && (get_SymConst_kind(ptr) == symconst_addr_ent)) {
245     return get_SymConst_entity(ptr);
246   }
247   return NULL;
248 }
249
250 /**
251  * verify the Proj number
252  */
253 static int
254 vrfy_Proj_proj(ir_node *p, ir_graph *irg) {
255   ir_node *pred;
256   ir_mode *mode;
257   int proj;
258
259   pred = skip_Id(get_Proj_pred(p));
260   ASSERT_AND_RET(get_irn_mode(pred) == mode_T, "mode of a 'projed' node is not Tuple", 0);
261   mode = get_irn_mode(p);
262   proj = get_Proj_proj(p);
263
264   switch (get_irn_opcode(pred)) {
265     case iro_Start:
266       ASSERT_AND_RET_DBG(
267           (
268            (proj == pn_Start_X_initial_exec && mode == mode_X) ||
269            (proj == pn_Start_M         && mode == mode_M) ||
270            (proj == pn_Start_P_frame_base && mode_is_reference(mode)) ||
271            (proj == pn_Start_P_globals && mode_is_reference(mode)) ||
272            (proj == pn_Start_T_args    && mode == mode_T) ||
273            (proj == pn_Start_P_value_arg_base && mode_is_reference(mode)) ||
274            (proj == pn_Start_P_value_arg_base && mode == mode_T)    /* FIXME: only one of those */
275           ),
276           "wrong Proj from Start", 0,
277       show_proj_failure(p);
278       );
279       break;
280
281     case iro_Cond:
282       ASSERT_AND_RET_DBG(
283         (
284           (proj >= 0 && mode == mode_X && get_irn_mode(get_Cond_selector(pred)) == mode_b) ||   /* compare */
285           (mode == mode_X && mode_is_int(get_irn_mode(get_Cond_selector(pred))))                /* switch */
286         ),
287         "wrong Proj from Cond", 0,
288         show_proj_failure(p);
289       );
290       break;
291
292     case iro_Raise:
293       ASSERT_AND_RET_DBG(
294         ((proj == pn_Raise_X && mode == mode_X) || (proj == pn_Raise_M && mode == mode_M)),
295         "wrong Proj from Raise", 0,
296     show_proj_failure(p);
297       );
298       break;
299
300     case iro_InstOf:
301       ASSERT_AND_RET_DBG(
302     (proj >= 0 && mode == mode_X),
303     "wrong Proj from InstOf", 0,
304     show_proj_failure(p);
305       );
306       break;
307
308     case iro_Call:
309       ASSERT_AND_RET_DBG(
310         ((proj == pn_Call_M_regular        && mode == mode_M) ||
311          (proj == pn_Call_X_except         && mode == mode_X) ||
312          (proj == pn_Call_T_result         && mode == mode_T) ||
313          (proj == pn_Call_M_except         && mode == mode_M) ||
314      (proj == pn_Call_P_value_res_base && mode == mode_P)),
315         "wrong Proj from Call", 0,
316         show_proj_failure(p);
317       );
318       break;
319
320     case iro_FuncCall:
321       ASSERT_AND_RET_DBG(
322         ((proj == pn_Call_M_regular        && mode == mode_M) ||
323          (proj == pn_Call_X_except         && mode == mode_X) ||
324          (proj == pn_Call_T_result         && mode == mode_T) ||
325          (proj == pn_Call_M_except         && mode == mode_M) ||
326      (proj == pn_Call_P_value_res_base && mode == mode_P)),
327         "wrong Proj from FuncCall", 0,
328         show_proj_failure(p);
329       );
330       break;
331
332     case iro_Quot:
333       ASSERT_AND_RET_DBG(
334         ((proj == pn_Quot_M        && mode == mode_M) ||
335          (proj == pn_Quot_X_except && mode == mode_X) ||
336          (proj == pn_Quot_res      && mode_is_float(mode))),
337         "wrong Proj from Quot", 0,
338     show_proj_failure(p);
339       );
340       break;
341
342     case iro_DivMod:
343       ASSERT_AND_RET_DBG(
344         ((proj == pn_DivMod_M        && mode == mode_M) ||
345          (proj == pn_DivMod_X_except && mode == mode_X) ||
346          (proj == pn_DivMod_res_div  && mode_is_int(mode)) ||
347          (proj == pn_DivMod_res_mod  && mode_is_int(mode))),
348         "wrong Proj from DivMod", 0,
349     show_proj_failure(p);
350       );
351       break;
352
353     case iro_Div:
354       ASSERT_AND_RET_DBG(
355         ((proj == pn_Div_M        && mode == mode_M) ||
356          (proj == pn_Div_X_except && mode == mode_X) ||
357          (proj == pn_Div_res      && mode_is_int(mode))),
358         "wrong Proj from Div or Mod", 0,
359     show_proj_failure(p);
360       );
361       break;
362
363     case iro_Mod:
364       ASSERT_AND_RET_DBG(
365         ((proj == pn_Mod_M        && mode == mode_M) ||
366          (proj == pn_Mod_X_except && mode == mode_X) ||
367          (proj == pn_Mod_res      && mode_is_int(mode))),
368         "wrong Proj from Div or Mod", 0,
369     show_proj_failure(p);
370       );
371       break;
372
373     case iro_Cmp:
374       ASSERT_AND_RET_DBG(
375         (proj >= 0 && proj <= 15 && mode == mode_b),
376         "wrong Proj from Cmp", 0,
377     show_proj_failure(p);
378       );
379       break;
380
381     case iro_Load:
382       if (proj == pn_Load_res) {
383         ir_node *ptr = get_Load_ptr(pred);
384         entity *ent = get_ptr_entity(ptr);
385         if (ent) {
386           ASSERT_AND_RET_DBG(
387                              (mode == get_type_mode(get_entity_type(ent))),
388                              "wrong data Proj from Load, entity type_mode failed", 0,
389                              show_proj_failure_ent(p, ent);
390                              );
391         }
392         else {
393           ASSERT_AND_RET_DBG(
394                              mode_is_data(mode),
395                              "wrong data Proj from Load", 0,
396                              show_proj_failure(p);
397                              );
398         }
399       } else {
400         ASSERT_AND_RET_DBG(
401                            ((proj == pn_Load_M        && mode == mode_M) ||
402                             (proj == pn_Load_X_except && mode == mode_X)),
403                            "wrong Proj from Load", 0,
404                            show_proj_failure(p);
405                            );
406       }
407       break;
408
409     case iro_Store:
410       ASSERT_AND_RET_DBG(
411         ((proj == pn_Store_M        && mode == mode_M) ||
412          (proj == pn_Store_X_except && mode == mode_X)),
413         "wrong Proj from Store", 0,
414     show_proj_failure(p);
415       );
416       break;
417
418     case iro_Alloc:
419       ASSERT_AND_RET_DBG(
420         (
421          (proj == pn_Alloc_M        && mode == mode_M) ||
422          (proj == pn_Alloc_X_except /* && mode == mode_X*/) ||
423          (proj == pn_Alloc_res      && mode_is_reference(mode))
424         ),
425         "wrong Proj from Alloc", 0,
426         show_proj_failure(p);
427       );
428       break;
429
430     case iro_Proj:
431       {
432         type *mt; /* A method type */
433         long nr = get_Proj_proj(pred);
434
435         pred = skip_Id(get_Proj_pred(pred));
436         ASSERT_AND_RET((get_irn_mode(pred) == mode_T), "Proj from something not a tuple", 0);
437         switch (get_irn_opcode(pred))
438         {
439           case iro_Start:
440             mt = get_entity_type(get_irg_entity(irg));
441
442         if (nr == pn_Start_T_args) {
443               ASSERT_AND_RET(
444                   (proj >= 0 && mode_is_data(mode)),
445                   "wrong Proj from Proj from Start", 0);
446               ASSERT_AND_RET(
447                 (proj < get_method_n_params(mt)),
448                 "More Projs for args than args in type", 0
449               );
450               if ((mode_is_reference(mode)) && is_compound_type(get_method_param_type(mt, proj)))
451                 /* value argument */ break;
452
453               ASSERT_AND_RET(
454                   (mode == get_type_mode(get_method_param_type(mt, proj))),
455                   "Mode of Proj from Start doesn't match mode of param type.", 0);
456             }
457         else if (nr == pn_Start_P_value_arg_base) {
458           ASSERT_AND_RET(
459                   (proj >= 0 && mode_is_reference(mode)),
460                   "wrong Proj from Proj from Start", 0
461               );
462               ASSERT_AND_RET(
463                 (proj < get_method_n_params(mt)),
464                 "More Projs for args than args in type", 0
465               );
466         }
467             break;
468
469           case iro_Call:
470             {
471               ASSERT_AND_RET(
472                   (proj >= 0 && mode_is_data(mode)),
473                   "wrong Proj from Proj from Call", 0);
474               mt = get_Call_type(pred);
475               ASSERT_AND_RET(
476                   (proj < get_method_n_ress(mt)),
477                   "More Projs for results than results in type.", 0);
478               if ((mode_is_reference(mode)) && is_compound_type(get_method_res_type(mt, proj)))
479                 /* value result */ break;
480
481               ASSERT_AND_RET(
482                   (mode == get_type_mode(get_method_res_type(mt, proj))),
483                   "Mode of Proj from Call doesn't match mode of result type.", 0);
484             }
485             break;
486
487           case iro_FuncCall:
488             {
489               ASSERT_AND_RET(
490                   (proj >= 0 && mode_is_data(mode)),
491                   "wrong Proj from Proj from FuncCall", 0);
492               mt = get_FuncCall_type(pred);
493               ASSERT_AND_RET(
494                   (proj < get_method_n_ress(mt)),
495                   "More Projs for results than results in type.", 0);
496               if ((mode_is_reference(mode)) && is_compound_type(get_method_res_type(mt, proj)))
497                 /* value result */ break;
498
499               ASSERT_AND_RET(
500                   (mode == get_type_mode(get_method_res_type(mt, proj))),
501                   "Mode of Proj from FuncCall doesn't match mode of result type.", 0);
502             }
503             break;
504
505           case iro_Tuple:
506             /* We don't test */
507             break;
508
509           default:
510             ASSERT_AND_RET(0, "Unknown opcode", 0);
511         }
512         break;
513
514       }
515     case iro_Tuple:
516       /* We don't test */
517       break;
518
519     case iro_CallBegin:
520       break;
521
522     case iro_EndReg:
523       break;
524
525     case iro_EndExcept:
526       break;
527
528     default:
529       ASSERT_AND_RET(0, "Unknown opcode", 0);
530   }
531
532   /* all went ok */
533   return 1;
534 }
535
536 int irn_vrfy_irg(ir_node *n, ir_graph *irg)
537 {
538   int i;
539   int opcode, opcode1;
540   ir_mode *mymode, *op1mode = NULL, *op2mode, *op3mode;
541   int op_is_symmetric = 1;  /*  0: asymmetric
542                                 1: operands have identical modes
543                                 2: modes of operands == mode of this node */
544   type *mt;                 /* A method type */
545   entity *ent;
546
547   ir_node **in;
548
549   if (!opt_do_node_verification) return 1;
550
551   if (! interprocedural_view) {
552     /*
553      * do NOT check placement in interprocedural view, as we don't always know
554      * the "right" graph ...
555      */
556     ASSERT_AND_RET_DBG(
557                        node_is_in_irgs_storage(irg, n),
558                        "Node is not stored on proper IR graph!", 0,
559                        show_node_on_graph(irg, n);
560                        );
561   }
562
563   opcode = get_irn_opcode(n);
564
565   /* We don't want to test nodes whose predecessors are Bad,
566      as we would have to special case that for each operation. */
567   if (opcode != iro_Phi && opcode != iro_Block)
568     for (i = 0; i < get_irn_arity(n); i++) {
569       opcode1 = get_irn_opcode(get_irn_n(n, i));
570       if (opcode1 == iro_Bad)
571         return 1;
572     }
573
574   mymode = get_irn_mode(n);
575   in = get_irn_in(n);
576
577   switch (opcode) {
578
579     case iro_Block:
580       for (i = 0; i < get_Block_n_cfgpreds(n); ++i) {
581         ir_node *pred =  get_Block_cfgpred(n, i);
582         ASSERT_AND_RET(
583                        (is_Bad(pred)     ||
584                         is_Unknown(pred) ||
585                         (get_irn_mode(pred) == mode_X)
586                         ), "Block node", 0);
587       }
588       /*  End block may only have Return, Raise or fragile ops as preds. */
589       if (n == get_irg_end_block(irg))
590         for (i = 0; i < get_Block_n_cfgpreds(n); ++i) {
591           ir_node *pred =  skip_Proj(get_Block_cfgpred(n, i));
592           if (is_Proj(pred) || get_irn_op(pred) == op_Tuple)
593             break;   /*  We can not test properly.  How many tuples are there? */
594           ASSERT_AND_RET(((get_irn_op(pred) == op_Return) ||
595                           is_Bad(pred)                    ||
596                           (get_irn_op(pred) == op_Raise)  ||
597                           is_fragile_op(pred)               ),
598                          "End Block node", 0);
599         }
600       /*  irg attr must == graph we are in. */
601       if (! interprocedural_view) {
602         ASSERT_AND_RET(((get_irn_irg(n) && get_irn_irg(n) == irg)), "Block node has wrong irg attribute", 0);
603       }
604
605       break;
606
607     case iro_Start:
608       ASSERT_AND_RET(
609                      /* Start: BB --> X x M x ref x data1 x ... x datan x ref */
610                      mymode == mode_T, "Start node", 0
611                      );
612       break;
613
614     case iro_Jmp:
615       ASSERT_AND_RET(
616                      /* Jmp: BB --> X */
617                      mymode == mode_X, "Jmp node", 0
618                      );
619       break;
620
621     case iro_Break:
622       ASSERT_AND_RET(
623                      /* Jmp: BB --> X */
624                      mymode == mode_X, "Jmp node", 0
625                      );
626       break;
627
628     case iro_Cond:
629       op1mode = get_irn_mode(in[1]);
630       ASSERT_AND_RET(
631                      /* Cond: BB x b --> X x X */
632                      (op1mode == mode_b ||
633                       /* Cond: BB x int --> X^n */
634                       mode_is_int(op1mode) ),  "Cond node", 0
635                      );
636       ASSERT_AND_RET(mymode == mode_T, "Cond mode is not a tuple", 0);
637       break;
638
639     case iro_Return:
640       op1mode = get_irn_mode(in[1]);
641       /* Return: BB x M x data1 x ... x datan --> X */
642       /* printf("mode: %s, code %s\n", ID_TO_STR(n->mode->name), ID_TO_STR(n->op->name));*/
643       ASSERT_AND_RET( op1mode == mode_M, "Return node", 0 );  /* operand M */
644       for (i = 2; i < get_irn_arity(n); i++) {
645         ASSERT_AND_RET( mode_is_data(get_irn_mode(in[i])), "Return node", 0 );  /* operand datai */
646       };
647       ASSERT_AND_RET( mymode == mode_X, "Result X", 0 );   /* result X */
648       /* Compare returned results with result types of method type */
649       mt = get_entity_type(get_irg_entity(irg));
650       ASSERT_AND_RET_DBG( get_Return_n_ress(n) == get_method_n_ress(mt),
651         "Number of results for Return doesn't match number of results in type.", 0,
652       show_return_nres(irg, n, mt););
653       for (i = 0; i < get_Return_n_ress(n); i++) {
654     type *res_type = get_method_res_type(mt, i);
655
656         if (is_atomic_type(res_type)) {
657       ASSERT_AND_RET_DBG(
658         get_irn_mode(get_Return_res(n, i)) == get_type_mode(res_type),
659         "Mode of result for Return doesn't match mode of result type.", 0,
660         show_return_modes(irg, n, mt, i);
661       );
662     }
663     else {
664       ASSERT_AND_RET_DBG(
665         mode_is_reference(get_irn_mode(get_Return_res(n, i))),
666         "Mode of result for Return doesn't match mode of result type.", 0,
667         show_return_modes(irg, n, mt, i);
668       );
669     }
670       }
671       break;
672
673     case iro_Raise:
674       op1mode = get_irn_mode(in[1]);
675       op2mode = get_irn_mode(in[2]);
676       ASSERT_AND_RET(
677                      /* Sel: BB x M x ref --> X x M */
678                      op1mode == mode_M && mode_is_reference(op2mode) &&
679                      mymode == mode_T, "Raise node", 0
680                      );
681       break;
682
683     case iro_Const: {
684       ASSERT_AND_RET(
685                      /* Const: BB --> data */
686                      (mode_is_data (mymode) ||
687                       mymode == mode_b)      /* we want boolean constants for static evaluation */
688                      ,"Const node", 0        /* of Cmp. */
689                      );
690       } break;
691     case iro_SymConst:
692       if (get_SymConst_kind(n) == symconst_addr_ent) {
693         entity *ent = get_SymConst_entity(n);
694         if (is_method_type(get_entity_type(ent)) &&
695             get_irn_irg(n) != get_const_code_irg()) {
696 #if 1
697           ASSERT_AND_RET((get_entity_peculiarity(ent) != peculiarity_description),
698                          "A constant must address an existing method.", 0);
699 #endif
700         }
701       }
702       ASSERT_AND_RET(
703                      /* SymConst: BB --> int*/
704                      (mode_is_int(mymode) ||
705                       /* SymConst: BB --> ref */
706                       mode_is_reference(mymode))
707                      ,"SymConst node", 0);
708       break;
709
710     case iro_Sel:
711       op1mode = get_irn_mode(in[1]);
712       op2mode = get_irn_mode(in[2]);
713       ASSERT_AND_RET_DBG(
714                          /* Sel: BB x M x ref x int^n --> ref */
715                          (op1mode == mode_M && op2mode == mymode && mode_is_reference(mymode)),
716                          "Sel node", 0, show_node_failure(n)
717                          );
718       for (i=3; i < get_irn_arity(n); i++)
719         {
720           ASSERT_AND_RET_DBG(mode_is_int(get_irn_mode(in[i])), "Sel node", 0, show_node_failure(n));
721         }
722       ent = get_Sel_entity(n);
723       ASSERT_AND_RET_DBG(ent, "Sel node with empty entity", 0, show_node_failure(n));
724       break;
725
726     case iro_InstOf:
727       ASSERT_AND_RET(mode_T == mymode, "mode of Instof is not a tuple", 0);
728       ASSERT_AND_RET(mode_is_data(op1mode), "Instof not on data", 0);
729       break;
730
731     case iro_Call:
732       op1mode = get_irn_mode(in[1]);
733       op2mode = get_irn_mode(in[2]);
734       /* Call: BB x M x ref x data1 x ... x datan
735          --> M x datan+1 x ... x data n+m */
736       ASSERT_AND_RET( op1mode == mode_M && mode_is_reference(op2mode), "Call node", 0 );  /* operand M x ref */
737       for (i=3; i < get_irn_arity(n); i++) {
738         ASSERT_AND_RET( mode_is_data(get_irn_mode(in[i])), "Call node", 0 );  /* operand datai */
739       };
740       ASSERT_AND_RET( mymode == mode_T, "Call result not a tuple", 0 );   /* result T */
741       /* Compare arguments of node with those of type */
742       mt = get_Call_type(n);
743
744       if (get_method_variadicity(mt) == variadicity_variadic) {
745         ASSERT_AND_RET_DBG(
746                            get_Call_n_params(n) >= get_method_n_params(mt),
747                            "Number of args for Call doesn't match number of args in variadic type.",
748                            0,
749                            fprintf(stderr, "Call has %d params, method %s type %d\n",
750                                    get_Call_n_params(n), get_type_name(mt), get_method_n_params(mt));
751                            );
752       }
753       else {
754         ASSERT_AND_RET(
755                        get_Call_n_params(n) == get_method_n_params(mt),
756                        "Number of args for Call doesn't match number of args in non variadic type.",
757                        0);
758       }
759
760       for (i = 0; i < get_method_n_params(mt); i++) {
761     type *t = get_method_param_type(mt, i);
762
763     if (is_atomic_type(t)) {
764       ASSERT_AND_RET_DBG(
765           get_irn_mode(get_Call_param(n, i)) == get_type_mode(t),
766           "Mode of arg for Call doesn't match mode of arg type.", 0,
767       show_call_param(n, mt);
768       );
769     }
770     else {
771       /* call with a compound type, mode must be reference */
772       ASSERT_AND_RET_DBG(
773           mode_is_reference(get_irn_mode(get_Call_param(n, i))),
774           "Mode of arg for Call doesn't match mode of arg type.", 0,
775       show_call_param(n, mt);
776       );
777     }
778       }
779       break;
780
781     case iro_Add:
782       op1mode = get_irn_mode(in[1]);
783       op2mode = get_irn_mode(in[2]);
784       ASSERT_AND_RET_DBG(
785                          (
786                           /* common Add: BB x numP x numP --> numP */
787                           (op1mode == mymode && op2mode == op1mode && mode_is_numP(mymode)) ||
788                           /* Pointer Add: BB x ref x int --> ref */
789                           (mode_is_reference(op1mode) && mode_is_int(op2mode) && op1mode == mymode) ||
790                           /* Pointer Add: BB x int x ref --> ref */
791                           (mode_is_int(op1mode) && op2mode == mymode && mode_is_reference(mymode))
792                           ),
793                          "Add node", 0,
794                          show_binop_failure(n, "/* common Add: BB x numP x numP --> numP */ |\n"
795                                             "/* Pointer Add: BB x ref x int --> ref */   |\n"
796                                             "/* Pointer Add: BB x int x ref --> ref */");
797                          );
798       if (mode_is_reference(op1mode) != mode_is_reference(op2mode)) {
799         /* BB x ref x int --> ref or BB x int x ref --> ref */
800         op_is_symmetric = 0;
801       } else {
802         /* BB x num x num --> num or BB x ref x ref */
803         op_is_symmetric = 2;
804       }
805       break;
806
807     case iro_Sub:
808       op1mode = get_irn_mode(in[1]);
809       op2mode = get_irn_mode(in[2]);
810       ASSERT_AND_RET_DBG(
811                          /* common Sub: BB x numP x numP --> numP */
812                          ((mymode ==op1mode && mymode == op2mode && mode_is_numP(op1mode)) ||
813                           /* Pointer Sub: BB x ref x int --> ref */
814                           (op1mode == mymode && mode_is_int(op2mode) && mode_is_reference(mymode)) ||
815                           /* Pointer Sub: BB x int x ref --> ref */
816                           (mode_is_int(op1mode) && op2mode == mymode && mode_is_reference(mymode)) ||
817                           /* Pointer Sub: BB x ref x ref --> int */
818                           (op1mode == op2mode && mode_is_reference(op2mode) && mode_is_int(mymode))),
819                          "Sub node", 0,
820                          show_binop_failure(n, "/* common Sub: BB x numP x numP --> numP */ |\n"
821                                             "/* Pointer Sub: BB x ref x int --> ref */   |\n"
822                                             "/* Pointer Sub: BB x int x ref --> ref */   |\n"
823                                             "/* Pointer Sub: BB x ref x ref --> int */" );
824                          );
825       if (mode_is_reference(op1mode) != mode_is_reference(op2mode)) {
826         op_is_symmetric = 0;
827       } else {
828         op_is_symmetric = 2;
829       }
830       break;
831
832     case iro_Minus:
833       op1mode = get_irn_mode(in[1]);
834       ASSERT_AND_RET_DBG(
835                          /* Minus: BB x float --> float */
836                          op1mode == mymode && mode_is_float(op1mode), "Minus node", 0,
837                          show_unop_failure(n , "/* Minus: BB x float --> float */");
838                          );
839       op_is_symmetric = 2;
840       break;
841
842     case iro_Mul:
843       op1mode = get_irn_mode(in[1]);
844       op2mode = get_irn_mode(in[2]);
845       ASSERT_AND_RET_DBG(
846                          /* Mul: BB x int1 x int1 --> int2 */
847                          ((mode_is_int(op1mode)   && op2mode == op1mode && mode_is_int(mymode)) ||
848                          /* Mul: BB x float x float --> float */
849                           (mode_is_float(op1mode) && op2mode == op1mode && mymode == op1mode)),
850                          "Mul node",0,
851                          show_binop_failure(n, "/* Mul: BB x int1 x int1 --> int2 */ |\n"
852                                                "/* Mul: BB x float x float --> float */");
853                          );
854       op_is_symmetric = 2;
855       break;
856
857     case iro_Quot:
858       op1mode = get_irn_mode(in[1]);
859       op2mode = get_irn_mode(in[2]);
860       op3mode = get_irn_mode(in[3]);
861       ASSERT_AND_RET_DBG(
862                          /* Quot: BB x M x float x float --> M x X x float */
863                          op1mode == mode_M && op2mode == op3mode &&
864                          get_mode_sort(op2mode) == irms_float_number &&
865                          mymode == mode_T,
866                          "Quot node",0,
867                          show_binop_failure(n, "/* Quot: BB x M x float x float --> M x X x float */");
868                          );
869       op_is_symmetric = 2;
870       break;
871
872     case iro_DivMod:
873       op1mode = get_irn_mode(in[1]);
874       op2mode = get_irn_mode(in[2]);
875       op3mode = get_irn_mode(in[3]);
876       ASSERT_AND_RET(
877                      /* DivMod: BB x M x int x int --> M x X x int x int */
878                      op1mode == mode_M &&
879                      mode_is_int(op2mode) &&
880                      op3mode == op2mode &&
881                      mymode == mode_T,
882                      "DivMod node", 0
883                      );
884       op_is_symmetric = 1;
885       break;
886
887     case iro_Div:
888     case iro_Mod:
889       op1mode = get_irn_mode(in[1]);
890       op2mode = get_irn_mode(in[2]);
891       op3mode = get_irn_mode(in[3]);
892       ASSERT_AND_RET(
893                      /* Div or Mod: BB x M x int x int --> M x X x int */
894                      op1mode == mode_M &&
895                      op2mode == op3mode &&
896                      mode_is_int(op2mode) &&
897                      mymode == mode_T,
898                      "Div or Mod node", 0
899                      );
900       op_is_symmetric = 1;
901       break;
902
903     case iro_Abs:
904       op1mode = get_irn_mode(in[1]);
905       ASSERT_AND_RET_DBG(
906                          /* Abs: BB x num --> num */
907                          op1mode == mymode &&
908                          mode_is_num (op1mode),
909                          "Abs node", 0,
910                          show_unop_failure(n, "/* Abs: BB x num --> num */");
911                          );
912       op_is_symmetric = 2;
913       break;
914
915     case iro_And:
916     case iro_Or:
917     case iro_Eor:
918       op1mode = get_irn_mode(in[1]);
919       op2mode = get_irn_mode(in[2]);
920       ASSERT_AND_RET_DBG(
921                          /* And or Or or Eor: BB x int x int --> int */
922                          mode_is_int(mymode) &&
923                          op2mode == op1mode &&
924                          mymode == op2mode,
925                          "And, Or or Eor node", 0,
926                          show_binop_failure(n, "/* And or Or or Eor: BB x int x int --> int */");
927                          );
928       op_is_symmetric = 2;
929       break;
930
931     case iro_Not:
932       op1mode = get_irn_mode(in[1]);
933       ASSERT_AND_RET_DBG(
934                          /* Not: BB x int --> int */
935                          mode_is_int(mymode) &&
936                          mymode == op1mode,
937                          "Not node", 0,
938                          show_unop_failure(n, "/* Not: BB x int --> int */");
939                          );
940       op_is_symmetric = 2;
941       break;
942
943
944     case iro_Cmp:
945       op1mode = get_irn_mode(in[1]);
946       op2mode = get_irn_mode(in[2]);
947       ASSERT_AND_RET_DBG(
948                          /* Cmp: BB x datab x datab --> b16 */
949                          mode_is_data (op1mode) &&
950                          op2mode == op1mode &&
951                          mymode == mode_T,
952                          "Cmp node", 0,
953                          show_binop_failure(n, "/* Cmp: BB x datab x datab --> b16 */");
954                          );
955       break;
956
957     case iro_Shl:
958     case iro_Shr:
959     case iro_Shrs:
960       op1mode = get_irn_mode(in[1]);
961       op2mode = get_irn_mode(in[2]);
962       ASSERT_AND_RET_DBG(
963                          /* Shl, Shr or Shrs: BB x int x int_u --> int */
964                          mode_is_int(op1mode) &&
965                          mode_is_int(op2mode) &&
966                          !mode_is_signed(op2mode) &&
967                          mymode == op1mode,
968                          "Shl, Shr, Shr or Rot node", 0,
969                          show_binop_failure(n, "/* Shl, Shr or Shrs: BB x int x int_u --> int */");
970                          );
971       break;
972
973     case iro_Rot:
974       op1mode = get_irn_mode(in[1]);
975       op2mode = get_irn_mode(in[2]);
976       ASSERT_AND_RET_DBG(
977                          /* Rot: BB x int x int --> int */
978                          mode_is_int(op1mode) &&
979                          mode_is_int(op2mode) &&
980                          mymode == op1mode,
981                          "Rot node", 0,
982                          show_binop_failure(n, "/* Rot: BB x int x int --> int */");
983                          );
984       break;
985
986     case iro_Conv:
987       op1mode = get_irn_mode(in[1]);
988       ASSERT_AND_RET_DBG(
989                          /* Conv: BB x datab1 --> datab2 */
990                          mode_is_datab(op1mode) && mode_is_data(mymode),
991                          "Conv node", 0,
992                          show_unop_failure(n, "/* Conv: BB x datab1 --> datab2 */");
993                          );
994       break;
995
996     case iro_Cast:
997       op1mode = get_irn_mode(in[1]);
998       ASSERT_AND_RET_DBG(
999                          /* Conv: BB x datab1 --> datab2 */
1000                          mode_is_data(op1mode) && op1mode == mymode,
1001                          "Cast node", 0,
1002                          show_unop_failure(n, "/* Conv: BB x datab1 --> datab2 */");
1003                          );
1004       break;
1005
1006     case iro_Phi:
1007     {
1008       ir_node *block = get_nodes_block(n);
1009
1010       if (! is_Bad(block) && get_irg_phase_state(get_irn_irg(n)) != phase_building) {
1011         /* a Phi node MUST have the same number of inputs as its block */
1012         ASSERT_AND_RET_DBG(
1013           get_irn_arity(n) == get_irn_arity(block),
1014           "wrong number of inputs in Phi node", 0,
1015           show_phi_inputs(n, block);
1016         );
1017       }
1018
1019       /* Phi: BB x dataM^n --> dataM */
1020       for (i = 1; i < get_irn_arity(n); i++) {
1021         if (!is_Bad(in[i]) && (get_irn_op(in[i]) != op_Unknown))
1022           ASSERT_AND_RET_DBG(
1023                              get_irn_mode(in[i]) == mymode,
1024                              "Phi node", 0,
1025                              show_phi_failure(n, in[i], i);
1026                              );
1027       };
1028       ASSERT_AND_RET( mode_is_dataM(mymode), "Phi node", 0 );
1029       break;
1030     }
1031     case iro_Load:
1032       op1mode = get_irn_mode(in[1]);
1033       op2mode = get_irn_mode(in[2]);
1034       ASSERT_AND_RET(
1035                      /* Load: BB x M x ref --> M x X x data */
1036                      op1mode == mode_M && mode_is_reference(op2mode),
1037                      "Load node", 0
1038                      );
1039       ASSERT_AND_RET( mymode == mode_T, "Load node", 0 );
1040
1041       /*
1042        * jack's gen_add_firm_code:simpleSel seems to build Load (Load
1043        * (Proj (Proj))) sometimes ...
1044
1045        * interprete.c:ai_eval seems to assume that this happens, too
1046
1047        * obset.c:get_abstval_any can't deal with this if the load has
1048        * mode_T
1049        *
1050       {
1051         entity *ent = hunt_for_entity (get_Load_ptr (n), n);
1052         assert ((NULL != ent) || (mymode != mode_T));
1053       }
1054       */
1055
1056       break;
1057
1058     case iro_Store:
1059       op1mode = get_irn_mode(in[1]);
1060       op2mode = get_irn_mode(in[2]);
1061       op3mode = get_irn_mode(in[3]);
1062       ASSERT_AND_RET(
1063                      /* Load: BB x M x ref data --> M x X */
1064                      op1mode == mode_M && mode_is_reference(op2mode) && mode_is_data(op3mode),
1065                      "Store node", 0
1066                      );
1067       ASSERT_AND_RET(mymode == mode_T, "Store node", 0);
1068
1069       entity *target = get_ptr_entity(in[2]);
1070       if (target) {
1071         ASSERT_AND_RET(op3mode == get_type_mode(get_entity_type(target)), "Store node", 0);
1072       }
1073
1074       break;
1075
1076     case iro_Alloc:
1077       op1mode = get_irn_mode(in[1]);
1078       op2mode = get_irn_mode(in[2]);
1079       ASSERT_AND_RET_DBG(
1080                          /* Alloc: BB x M x int_u --> M x X x ref */
1081                          op1mode == mode_M &&
1082                          mode_is_int(op2mode) &&
1083                          !mode_is_signed(op2mode) &&
1084                          mymode == mode_T,
1085                          "Alloc node", 0,
1086                          show_binop_failure(n, "/* Alloc: BB x M x int_u --> M x X x ref */");
1087                          );
1088       break;
1089
1090     case iro_Free:
1091       op1mode = get_irn_mode(in[1]);
1092       op2mode = get_irn_mode(in[2]);
1093       ASSERT_AND_RET_DBG(
1094                          /* Free: BB x M x ref --> M */
1095                          op1mode == mode_M && mode_is_reference(op2mode) &&
1096                          mymode == mode_M,
1097                          "Free node", 0,
1098                          show_binop_failure(n, "/* Free: BB x M x ref --> M */");
1099                          );
1100       break;
1101
1102     case iro_Sync:
1103       /* Sync: BB x M^n --> M */
1104       for (i=1; i < get_irn_arity(n); i++) {
1105         ASSERT_AND_RET( get_irn_mode(in[i]) == mode_M, "Sync node", 0 );
1106       };
1107       ASSERT_AND_RET( mymode == mode_M, "Sync node", 0 );
1108       break;
1109
1110     case iro_Proj:
1111       return vrfy_Proj_proj(n, irg);
1112       break;
1113
1114     case iro_Confirm:
1115       op1mode = get_irn_mode(in[1]);
1116       op2mode = get_irn_mode(in[2]);
1117       ASSERT_AND_RET_DBG(
1118                          /* Confirm: BB x T x T --> T */
1119                          op1mode == mymode &&
1120                          op2mode == mymode,
1121                          "Confirm node", 0,
1122                          show_binop_failure(n, "/* Confirm: BB x T x T --> T */");
1123                          );
1124       break;
1125
1126     default:
1127       break;
1128     }
1129
1130   /* All went ok */
1131   return 1;
1132 }
1133
1134 int irn_vrfy(ir_node *n)
1135 {
1136   int res = 1;
1137 #ifdef DEBUG_libfirm
1138   res = irn_vrfy_irg(n, current_ir_graph);
1139 #endif
1140   return res;
1141 }
1142
1143 /*-----------------------------------------------------------------*/
1144 /* Verify the whole graph.                                         */
1145 /*-----------------------------------------------------------------*/
1146
1147 /* This *is* used, except gcc doesn't notice that */
1148 static void vrfy_wrap(ir_node *node, void *env)
1149 {
1150   int *res = env;
1151
1152   *res = irn_vrfy(node);
1153 }
1154
1155 int irg_vrfy(ir_graph *irg)
1156 {
1157   int res = 1;
1158 #ifdef DEBUG_libfirm
1159   ir_graph *rem;
1160
1161   rem = current_ir_graph;
1162   current_ir_graph = irg;
1163
1164   assert(get_irg_pinned(irg) == op_pin_state_pinned);
1165
1166   irg_walk(irg->end, vrfy_wrap, NULL, &res);
1167
1168   current_ir_graph = rem;
1169
1170   if (opt_do_node_verification == NODE_VERIFICATION_REPORT && ! res) {
1171     entity *ent = get_irg_entity(current_ir_graph);
1172
1173     if (ent)
1174       fprintf(stderr, "irg_verify: Verifying graph %s failed\n", get_entity_name(ent));
1175     else
1176       fprintf(stderr, "irg_verify: Verifying graph %p failed\n", (void *)current_ir_graph);
1177   }
1178
1179 #endif
1180   return res;
1181 }
1182
1183 int irn_vrfy_irg_dump(ir_node *n, ir_graph *irg, const char **bad_string)
1184 {
1185   int res;
1186   node_verification_t old = opt_do_node_verification;
1187
1188   bad_msg = NULL;
1189   opt_do_node_verification = NODE_VERIFICATION_ERROR_ONLY;
1190   res = irn_vrfy_irg(n, irg);
1191   opt_do_node_verification = old;
1192   *bad_string = bad_msg;
1193
1194   return res;
1195 }