set_ia32_op_type() want ia32_AddrModeS, not ia32_am_Source (though they happen to...
[libfirm] / testprograms / irr_cf_example.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/irr_cf_example.c
4  * Purpose:     Test Phi construction with irregular control flow.
5  * Author:      Christian Schaefer, Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1999-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #include <stdio.h>
14 #include <string.h>
15
16 #include <libfirm/firm.h>
17
18 /**
19 *  This file constructs a control flow of following shape:
20 *
21 * StartBlock
22 *     |
23 *    \|/
24 *   Block --->  Block
25 *     |           |
26 *    \|/         \|/
27 *   Block --->  Block
28 *     |           |
29 *    \|/         \|/
30 *   Block --->  Block
31 *                 |
32 *                \|/
33 *              EndBlock
34 *
35 *   This is a program as, e.g.,
36 *
37 *   switch (expr){
38 *     case 1:
39 *     case 2:
40 *       break;
41 *     default:
42 *   }
43 *   return
44 **/
45
46 int main(void)
47 {
48   char *dump_file_suffix = "";
49   ir_graph *irg;        /* this variable contains the irgraph */
50   ir_type     *owner;      /* the class in which this method is defined */
51   ir_type     *proc_main;  /* typeinformation for the method main */
52   ir_entity   *ent;        /* represents this method as ir_entity of owner */
53   ir_node  *expr, *c1, *c2, *cond, *f, *t, *jmp, *x;
54
55   printf("\nCreating an IR graph: IRR_CF...\n");
56
57   /* init library */
58   init_firm(NULL);
59   set_opt_constant_folding(0); /* so that stupid test are not evaluated. */
60
61   /* FIRM was designed for oo languages where all methods belong to a class.
62    * For imperative languages like C we view a file as a large class containing
63    * all functions as methods in this file.
64    * Therefore we define a class "empty" according to the file name
65    * with a method main as an ir_entity.
66    */
67 #define CLASSNAME "IRREGULAR_CF"
68 #define METHODNAME "main"
69 #define NRARGS 0
70 #define NRES 0
71
72   owner = new_type_class(new_id_from_chars(CLASSNAME, strlen(CLASSNAME)));
73   proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
74                               NRARGS, NRES);
75   ent = new_entity(owner,
76                    new_id_from_chars(METHODNAME, strlen(METHODNAME)),
77                    proc_main);
78   get_entity_ld_name(ent);
79 #define NUM_OF_LOCAL_VARS 0
80
81   irg = new_ir_graph(ent, NUM_OF_LOCAL_VARS);
82
83   /* two make two conditionals that represent a switch */
84   expr = new_Const(mode_Is, new_tarval_from_long(0, mode_Is));
85   c1 = new_Const(mode_Is, new_tarval_from_long(1, mode_Is));
86   c2 = new_Const(mode_Is, new_tarval_from_long(2, mode_Is));
87
88   cond = new_Cond(new_Proj(new_Cmp(expr, c1), mode_b, pn_Cmp_Eq));
89   f = new_Proj(cond, mode_X, pn_Cond_false);
90   t = new_Proj(cond, mode_X, pn_Cond_true);
91   mature_immBlock(get_irg_current_block(irg));
92
93   new_immBlock();
94   add_immBlock_pred(get_irg_current_block(irg), t);
95   jmp = new_Jmp();
96   mature_immBlock(get_irg_current_block(irg));
97
98   new_immBlock();
99   add_immBlock_pred(get_irg_current_block(irg), f);
100   cond = new_Cond(new_Proj(new_Cmp(expr, c2), mode_b, pn_Cmp_Eq));
101   f = new_Proj(cond, mode_X, pn_Cond_false);
102   t = new_Proj(cond, mode_X, pn_Cond_true);
103   mature_immBlock(get_irg_current_block(irg));
104
105   new_immBlock();
106   add_immBlock_pred(get_irg_current_block(irg), t);
107   add_immBlock_pred(get_irg_current_block(irg), jmp);
108   jmp = new_Jmp();
109   mature_immBlock(get_irg_current_block(irg));
110
111   new_immBlock();
112   add_immBlock_pred(get_irg_current_block(irg), f);
113   t = new_Jmp();
114   mature_immBlock(get_irg_current_block(irg));
115
116   new_immBlock();
117   add_immBlock_pred(get_irg_current_block(irg), t);
118   add_immBlock_pred(get_irg_current_block(irg), jmp);
119
120   x = new_Return(get_store(), 0, NULL);
121
122   mature_immBlock(get_irg_current_block(irg));
123
124   add_immBlock_pred(get_irg_end_block(irg), x);
125   mature_immBlock(get_irg_end_block(irg));
126
127   irg_finalize_cons(irg);
128
129   printf("Optimizing ...\n");
130   dead_node_elimination(irg);
131
132   /* verify the graph */
133   irg_vrfy(irg);
134
135   printf("Dumping the graph and a control flow graph.\n");
136   dump_ir_block_graph(irg, dump_file_suffix);
137   dump_cfg(irg, dump_file_suffix);
138   printf("Use ycomp to view these graphs:\n");
139   printf("ycomp IRREGULAR_CF_main.vcg\n\n");
140   printf("ycomp IRREGULAR_CF_main-cfg.vcg\n\n");
141
142   return 0;
143 }