obstack access funcion fixed
[libfirm] / ir / ir / irvrfy.c
1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 * All rights reserved.
3 *
4 * Authors: Christian Schaefer
5 *
6 *
7 */
8
9 /* $Id$ */
10
11 #ifdef HAVE_CONFIG_H
12 # include <config.h>
13 #endif
14
15 # include "irgraph_t.h"
16 # include "irvrfy.h"
17 # include "irgwalk.h"
18
19 /* @@@ replace use of array "in" by access functions. */
20 ir_node **get_irn_in (ir_node *node);
21
22 INLINE static void
23 vrfy_Proj_proj(ir_node *p) {
24   ir_node *pred;
25   ir_mode *mode;
26   int proj;
27
28   pred = skip_nop(get_Proj_pred(p));
29   assert(get_irn_mode(pred) == mode_T);
30   mode = get_irn_mode(p);
31   proj = get_Proj_proj(p);
32
33   switch (get_irn_opcode(pred)) {
34   case iro_Start:
35     assert (((proj == 0 && mode == mode_X) ||
36              (proj == 1 && mode == mode_M) ||
37              (proj == 2 && mode == mode_P) ||
38              (proj == 3 && mode == mode_P) ||
39              (proj == 4 && mode == mode_T))   &&
40             "wrong Proj from Start"  );
41     break;
42   case iro_Cond:
43     assert ((proj >= 0 && mode == mode_X) &&
44             "wrong Proj from Cond");
45     break;
46   case iro_Raise:
47     assert (((proj == 0 && mode == mode_X) ||
48              (proj == 1 && mode == mode_M))   &&
49             "wrong Proj from Raise" );
50     break;
51   case iro_InstOf:
52         assert (((proj >= 0 && mode == mode_X) &&
53                          "wrong Proj from InstOf"));
54         break;
55   case iro_Call:
56     assert (((proj == 0 && mode == mode_M) ||
57              (proj == 1 && mode == mode_X) ||
58              (proj == 2 && mode == mode_T) ||
59              (proj == 3 && mode == mode_M))   &&
60             "wrong Proj from Call" );
61     break;
62   case iro_Quot:
63     assert (((proj == 0 && mode == mode_M) ||
64              (proj == 1 && mode == mode_X) ||
65              (proj == 2 && mode_is_float(mode)))  &&
66             "wrong Proj from Quot");
67     break;
68   case iro_DivMod:
69     assert (((proj == 0 && mode == mode_M) ||
70              (proj == 1 && mode == mode_X) ||
71              (proj == 2 && mode == mode_Is) ||
72              (proj == 3 && mode == mode_Is))   &&
73             "wrong Proj from DivMod" );
74     break;
75   case iro_Div:
76   case iro_Mod:
77     assert (((proj == 0 && mode == mode_M) ||
78              (proj == 1 && mode == mode_X) ||
79              (proj == 2 && mode == mode_Is))   &&
80              "wrong Proj from Div or Mod" );
81     break;
82   case iro_Cmp:
83     assert ((proj >= 0 && proj <= 15 && mode == mode_b) &&
84             "wrong Proj from Cmp");
85     break;
86   case iro_Load:
87     assert (((proj == 0 && mode == mode_M) ||
88              (proj == 1 && mode == mode_X) ||
89              (proj == 2 && mode_is_data(mode)))  &&
90             "wrong Proj from Load");
91     break;
92   case iro_Store:
93     assert (((proj == 0 && mode == mode_M) ||
94              (proj == 1 && mode == mode_X))    &&
95             "wrong Proj from Store");
96     break;
97   case iro_Alloc:
98     assert (((proj == 0 && mode == mode_M) ||
99              (proj == 1 /* && mode == mode_X*/) ||
100              (proj == 2 && mode == mode_P))      &&
101             "wrong Proj from Alloc");
102     break;
103   case iro_Proj: {
104     type *mt; /* A method type */
105     pred = skip_nop(get_Proj_pred(pred));
106     assert(get_irn_mode(pred) == mode_T);
107     switch (get_irn_opcode(pred)) {
108     case iro_Start: {
109       assert (proj >= 0 && mode_is_data(mode) &&
110               "wrong Proj from Proj from Start");
111       mt = get_entity_type(get_irg_ent(current_ir_graph));
112       assert(proj < get_method_n_params(mt) &&
113              "More Projs for args than args in type");
114       if ((mode == mode_P) && is_compound_type(get_method_param_type(mt, proj)))
115         /* value argument */ break;
116       assert(mode == get_type_mode(get_method_param_type(mt, proj)) &&
117       "Mode of Proj from Start doesn't match mode of param type.");
118     } break;
119     case iro_Call: {
120       assert (proj >= 0 && mode_is_data(mode) &&
121               "wrong Proj from Proj from Call");
122       mt = get_Call_type(pred);
123       assert(proj < get_method_n_ress(mt) &&
124              "More Projs for results than results in type.");
125       if ((mode == mode_P) && is_compound_type(get_method_res_type(mt, proj)))
126         /* value result */ break;
127       assert(mode == get_type_mode(get_method_res_type(mt, proj)) &&
128       "Mode of Proj from Call doesn't match mode of result type.");
129     } break;
130     case iro_Tuple: ;
131       /* We don't test */
132       break;
133     default: assert(0);
134     } break;
135   }
136   case iro_Tuple:
137     /* We don't test */
138     break;
139   case iro_CallBegin:
140     break;
141   case iro_EndReg:
142     break;
143   case iro_EndExcept:
144     break;
145   default: assert(0);
146   }
147 }
148
149 void
150 irn_vrfy (ir_node *n)
151 {
152   int i;
153   int opcode;
154   ir_mode *mymode, *op1mode = NULL, *op2mode, *op3mode;
155   int op_is_symmetric = 1;      /* 0: asymmetric
156                                    1: operands have identical modes
157                                    2: modes of operands == mode of this node */
158   type *mt; /* A method type */
159
160   ir_node **in;
161
162   opcode = get_irn_opcode (n);
163
164   if (opcode != iro_Phi && opcode != iro_Block)
165     for (i = 0; i < get_irn_arity(n); i++)
166       if (get_irn_opcode(get_irn_n(n, i)) == iro_Bad
167           || get_irn_opcode(get_irn_n(n, i)) == iro_Unknown)
168         return;
169
170   mymode = get_irn_mode (n);
171   in = get_irn_in (n);
172
173   switch (opcode) {
174
175   case iro_Start:
176     assert (
177             /* Start: BB --> X x M x P x data1 x ... x datan */
178             mymode == mode_T && "Start node"
179            );
180     break;
181   case iro_Jmp:
182     assert (
183             /* Jmp: BB --> X */
184             mymode == mode_X && "Jmp node"
185            );
186     break;
187   case iro_Break:
188     assert (
189             /* Jmp: BB --> X */
190             mymode == mode_X && "Jmp node"
191            );
192     break;
193   case iro_Cond:
194     op1mode = get_irn_mode(in[1]);
195     assert (
196             /* Cond: BB x b --> X x X */
197             (op1mode == mode_b
198             /* Cond: BB x Iu --> X^n */
199             || op1mode == mode_Iu) && "Cond node"
200            );
201     assert (mymode == mode_T);
202     break;
203   case iro_Return:
204     op1mode = get_irn_mode(in[1]);
205     /* Return: BB x M x data1 x ... x datan --> X */
206     /* printf("mode: %s, code %s\n", ID_TO_STR(n->mode->name), ID_TO_STR(n->op->name));*/
207     assert ( op1mode == mode_M  && "Return node" );  /* operand M */
208     for (i=2; i < get_irn_arity(n); i++) {
209       assert ( mode_is_data(get_irn_mode(in[i]))  && "Return node");  /* operand datai */
210     };
211     assert ( mymode == mode_X );   /* result X */
212     /* Compare returned results with result types of method type */
213     mt = get_entity_type(get_irg_ent(current_ir_graph));
214     assert(get_Return_n_ress(n) == get_method_n_ress(mt) &&
215              "Number of results for Return doesn't match number of results in type.");
216     for (i = 0; i < get_Return_n_ress(n); i++)
217       assert((get_irn_mode(get_Return_res(n, i))
218               == get_type_mode(get_method_res_type(mt, i))) &&
219              "Mode of result for Return doesn't match mode of result type.");
220
221     break;
222   case iro_Raise:
223     op1mode = get_irn_mode(in[1]);
224     op2mode = get_irn_mode(in[2]);
225     assert (
226             /* Sel: BB x M x P --> X x M */
227             op1mode == mode_M && op2mode == mode_P
228             && mymode == mode_T && "Raise node"
229            );
230     break;
231   case iro_Const:
232     assert (
233             /* Const: BB --> data */
234             (mode_is_data (mymode) ||
235              mymode == mode_b)      /* we want boolean constants for static evaluation */
236              && "Const node"        /* of Cmp. */
237            );
238     break;
239   case iro_SymConst:
240     assert (
241             /* SymConst: BB --> Iu or
242                          BB --> P */
243             ((mymode == mode_Iu) || (mymode == mode_P))  && "SymConst node"
244            );
245     break;
246   case iro_Sel:
247     op1mode = get_irn_mode(in[1]);
248     op2mode = get_irn_mode(in[2]);
249     assert (
250             /* Sel: BB x M x P x Iu^n --> P */
251             op1mode == mode_M && op2mode == mode_P
252             && mymode == mode_P && "Sel node"
253            );
254     for (i=3; i < get_irn_arity(n); i++) {
255             assert (get_irn_mode(in[i]) == mode_Iu && "Sel node"); }
256     break;
257   case iro_InstOf:
258         assert (mode_T == mymode);
259         assert (mode_is_data (op1mode));
260         break;
261   case iro_Call:
262     op1mode = get_irn_mode(in[1]);
263     op2mode = get_irn_mode(in[2]);
264       /* Call: BB x M x P x data1 x ... x datan
265                  --> M x datan+1 x ... x data n+m */
266     assert ( op1mode == mode_M && op2mode == mode_P  && "Call node");  /* operand M x P */
267     for (i=3; i < get_irn_arity(n); i++) {
268       assert ( mode_is_data(get_irn_mode(in[i])) && "Call node");  /* operand datai */
269     };
270     assert ( mymode == mode_T );   /* result T */
271     /* Compare arguments of node with those of type */
272     mt = get_Call_type(n);
273
274     if(get_method_variadicity(mt) == variadic)
275       {
276         assert(get_Call_n_params(n) >= get_method_n_params(mt) &&
277                "Number of args for Call doesn't match number of args in " \
278                " variadic type.");
279       }
280     else
281       {
282         assert(get_Call_n_params(n) == get_method_n_params(mt) &&
283                "Number of args for Call doesn't match number of args in " \
284                "non variadic type.");
285       }
286
287     for (i = 0; i < get_method_n_params(mt); i++)
288       assert((get_irn_mode(get_Call_param(n, i))
289               == get_type_mode(get_method_param_type(mt, i))) &&
290              "Mode of arg for Call doesn't match mode of arg type.");
291     break;
292   case iro_Add:
293     op1mode = get_irn_mode(in[1]);
294     op2mode = get_irn_mode(in[2]);
295     assert (
296             /* common Add: BB x num x num --> num */
297             ((mymode == op1mode && mymode == op2mode
298               && (mode_is_num(mymode) || mymode == mode_P))
299              ||  /* Pointer Add: BB x P x Is --> P */
300              (op1mode == mode_P && op2mode == mode_Is && mymode == mode_P)
301              ||  /* Pointer Add: BB x Is x P --> P */
302              (op1mode == mode_Is && op2mode == mode_P && mymode == mode_P))
303             && "Add node"
304            );
305       if (op1mode == mode_P || op2mode == mode_P) {
306         /* BB x P x Is --> P or BB x Is x P --> P */
307         op_is_symmetric = 0; /* ArmRoq */
308       } else {
309         /* BB x num x num --> num */
310         op_is_symmetric = 2;
311       }
312     break;
313   case iro_Sub:
314     op1mode = get_irn_mode(in[1]);
315     op2mode = get_irn_mode(in[2]);
316     assert (
317             /* common Sub: BB x num x num --> num */
318             ((mymode ==op1mode && mymode == op2mode
319               && mode_is_num(op1mode))
320              ||  /* Pointer Sub: BB x P x Is --> P */
321              (op1mode == mode_P && op2mode == mode_Is && mymode == mode_P)
322              ||  /* Pointer Sub: BB x Is x P --> P */
323              (op1mode == mode_Is && op2mode == mode_P && mymode == mode_P)
324              ||  /* Pointer Sub: BB x P x P --> Is */
325              (op1mode == mode_P && op2mode == mode_P && mymode == mode_Is))
326             && "Sub node"
327            );
328       if (op1mode == mode_P && op2mode == mode_P) {
329         op_is_symmetric = 1; /* ArmRoq */
330       } else if (op1mode == mode_P || op2mode == mode_P) {
331         op_is_symmetric = 0; /* ArmRoq */
332       } else {
333         op_is_symmetric = 2;
334       }
335     break;
336   case iro_Minus:
337     op1mode = get_irn_mode(in[1]);
338     assert (
339             /* Minus: BB x float --> float */
340             op1mode == mymode && mode_is_float (op1mode)  && "Minus node"
341            );
342     op_is_symmetric = 2;
343     break;
344   case iro_Mul:
345     op1mode = get_irn_mode(in[1]);
346     op2mode = get_irn_mode(in[2]);
347     assert (
348             /* Mul: BB x num x num --> num */
349             mymode == op1mode && mymode == op2mode
350             && mode_is_num (op1mode) && "Mul node"
351            );
352     op_is_symmetric = 2;
353     break;
354   case iro_Quot:
355     op1mode = get_irn_mode(in[1]);
356     op2mode = get_irn_mode(in[2]);
357     op3mode = get_irn_mode(in[3]);
358     assert (
359             /* Quot: BB x M x float x float --> M x X x float */
360             op1mode == mode_M && op2mode == op3mode
361             && mode_is_float(op2mode) && mymode == mode_T && "Quot node"
362            );
363     op_is_symmetric = 2;
364     break;
365   case iro_DivMod:;
366     op1mode = get_irn_mode(in[1]);
367     op2mode = get_irn_mode(in[2]);
368     op3mode = get_irn_mode(in[3]);
369     assert (
370             /* DivMod: BB x M x num x num --> M x X x Is x Is */
371             op1mode == mode_M && op2mode == op3mode
372             && mode_is_num (op2mode) && mymode == mode_T && "DivMod node"
373            );
374     op_is_symmetric = 1;
375     break;
376   case iro_Div:
377   case iro_Mod:
378     op1mode = get_irn_mode(in[1]);
379     op2mode = get_irn_mode(in[2]);
380     op3mode = get_irn_mode(in[3]);
381     assert (
382             /* Div or Mod: BB x M x num x num --> M x X x Is */
383             op1mode == mode_M && op2mode == op3mode &&
384             mode_is_num (op2mode) && mymode == mode_T && "Div or Mod node"
385            );
386     op_is_symmetric = 1;
387     break;
388   case iro_Abs:
389     op1mode = get_irn_mode(in[1]);
390     assert (
391             /* Abs: BB x num --> num */
392             op1mode == mymode && mode_is_num (op1mode) && "Abs node"
393            );
394     op_is_symmetric = 2;
395     break;
396   case iro_And:
397   case iro_Or:
398   case iro_Eor:
399     op1mode = get_irn_mode(in[1]);
400     op2mode = get_irn_mode(in[2]);
401     assert(
402            /* And or Or or Eor: BB x int x int --> int */
403            mymode == op1mode && mymode == op2mode
404            && mode_is_int (mymode) && "And, Or or Eor node"
405           );
406     op_is_symmetric = 2;
407     break;
408   case iro_Not:
409     op1mode = get_irn_mode(in[1]);
410     assert(
411            /* Not: BB x int --> int */
412            mymode == op1mode
413            && mode_is_int (mymode) && "Not node"
414           );
415     op_is_symmetric = 2;
416     break;
417
418   case iro_Cmp:
419     op1mode = get_irn_mode(in[1]);
420     op2mode = get_irn_mode(in[2]);
421     assert(
422            /* Cmp: BB x datab x datab --> b16 */
423            op1mode == op2mode && mode_is_data (op1mode)
424            && mymode == mode_T && "Cmp node"
425           );
426     break;
427   case iro_Shl:
428   case iro_Shr:
429   case iro_Shrs:
430   case iro_Rot:
431     op1mode = get_irn_mode(in[1]);
432     op2mode = get_irn_mode(in[2]);
433     assert(
434            /* Shl, Shr, Shrs or Rot: BB x int x Iu --> int */
435            mode_is_int (op1mode) && op2mode == mode_Iu
436            && op1mode == mymode && "Shl, Shr, Shr or Rot node"
437           );
438     break;
439   case iro_Conv:
440     op1mode = get_irn_mode(in[1]);
441     assert(
442            /* Conv: BB x datab1 --> datab2 */
443            mode_is_datab (op1mode)
444            && mode_is_data (mymode) && "Conv node"
445           );
446     break;
447   case iro_Phi:
448     /* Phi: BB x dataM^n --> dataM */
449     /* for some reason "<=" aborts. Is there a problem with get_store? */
450     for (i=1; i < get_irn_arity(n); i++) {
451       if (!is_Bad(in[i]) && (get_irn_op(in[i]) != op_Unknown))
452         assert ( get_irn_mode(in[i]) == mymode  && "Phi node");
453     };
454     assert ( mode_is_dataM(mymode)  && "Phi node");
455     break;
456   case iro_Load:
457     op1mode = get_irn_mode(in[1]);
458     op2mode = get_irn_mode(in[2]);
459     assert(
460            /* Load: BB x M x P --> M x X x data */
461            op1mode == mode_M && op2mode == mode_P  && "Load node"
462           );
463     assert ( mymode == mode_T  && "Load node");
464     break;
465   case iro_Store:
466     op1mode = get_irn_mode(in[1]);
467     op2mode = get_irn_mode(in[2]);
468     op3mode = get_irn_mode(in[3]);
469     assert(
470            /* Load: BB x M x P x data --> M x X */
471            op1mode == mode_M && op2mode == mode_P
472            && mode_is_data (op3mode) && "Store node"
473           );
474     assert(mymode == mode_T && "Store node");
475     break;
476   case iro_Alloc:
477     op1mode = get_irn_mode(in[1]);
478     op2mode = get_irn_mode(in[2]);
479     assert(
480            /* Alloc: BB x M x Iu --> M x X x P */
481            op1mode == mode_M && op2mode == mode_Iu
482            && mymode == mode_T && "Alloc node"
483           );
484     break;
485   case iro_Free:
486     op1mode = get_irn_mode(in[1]);
487     op2mode = get_irn_mode(in[2]);
488     op3mode = get_irn_mode(in[3]);
489     assert(
490            /* Free: BB x M x P x Iu --> M */
491            op1mode == mode_M && op2mode == mode_P && op3mode == mode_Iu
492            && mymode == mode_M && "Free node"
493           );
494     break;
495   case iro_Sync:
496            /* Sync: BB x M^n --> M */
497     for (i=1; i < get_irn_arity(n); i++) {
498       assert ( get_irn_mode(in[i]) == mode_M  && "Sync node");
499     };
500     assert ( mymode == mode_M  && "Sync node");
501     break;
502   case iro_Proj:
503     vrfy_Proj_proj(n);
504     break;
505   default: ;
506   }
507 }
508
509
510 /*******************************************************************/
511 /* Verify the whole graph.                                         */
512 /*******************************************************************/
513
514 static void
515 vrfy_wrap (ir_node *node, void *env) {
516   irn_vrfy(node);
517 }
518
519 void irg_vrfy (ir_graph *irg)
520 {
521   ir_graph *rem;
522   rem = current_ir_graph;
523   current_ir_graph = irg;
524
525   assert(get_irg_pinned(irg) == pinned);
526
527   irg_walk(irg->end, vrfy_wrap, NULL, NULL);
528
529   current_ir_graph = rem;
530 }