New
[libfirm] / testprograms / three_cfpred_example.c
1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Authors: Christian Schaefer, Goetz Lindenmaier
5 **
6 ** testprogram.
7 */
8
9 # include <stdio.h>
10
11 # include "irdump.h"
12 # include "firm.h"
13
14 /*
15  *   a dead block / unreachable code.
16  */
17
18 /**
19 ***  This file constructs a control flow of following shape:
20 ***
21 ***
22 ***       firstCondBlock
23 ***          /     \
24 ***         /       \
25 ***       |/_       _\|
26 ***     Block1    scnCondBlock
27 ***        |       |        |
28 ***        |       |        |
29 ***        |      \ /      \ /
30 ***        |     Block2   Block3
31 ***         \      |       /
32 ***          \     |      /
33 ***          _\|  \ /   |/_
34 ***            nextBlock
35 ***
36 ***
37 ***   This is a program as, e.g.,
38 ***
39 ***   if () then
40 ***     { Jmp label1; } // happens anyways
41 ***   else
42 ***     { Jmp label1; } // happens anyways
43 *** label1:
44 ***   return();
45 ***   Jmp label1;
46 ***
47 **/
48
49 int main(int argc, char **argv)
50 {
51   type     *prim_t_int;
52   ir_graph *irg;          /* this variable contains the irgraph */
53   type *owner;      /* the class in which this method is defined */
54   type *proc_main; /* type information for the method main */
55   entity *ent;            /* represents this method as entity of owner */
56   ir_node *c1, *c2, *cond, *f, *t, *endBlock, *Block1, *jmp,
57           *scndCondBlock, *Block2, *Block3, *x;
58
59   /* init library */
60   init_firm ();
61
62   set_optimize(1);
63
64   /*** Make basic type information for primitive type int. ***/
65   prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_i);
66
67   /* FIRM was designed for oo languages where all methods belong to a class.
68    * For imperative languages like C we view a file as a large class containing
69    * all functions as methods in this file.
70    * This class now is automatically generated.
71    */
72 #define METHODNAME "main"
73 #define NRARGS 1
74 #define NRES 1
75   printf("\nCreating an IR graph: ...\n");
76
77   owner = get_glob_type();
78   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
79                               NRARGS, NRES);
80   set_method_param_type(proc_main, 0, prim_t_int);
81   set_method_res_type(proc_main, 0, prim_t_int);
82
83   ent = new_entity (owner,
84                     id_from_str (METHODNAME, strlen(METHODNAME)),
85                     proc_main);
86
87 #define NUM_OF_LOCAL_VARS 2
88
89   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
90
91   /* to make a condition  */
92   c1 = new_Const (mode_i, tarval_from_long (mode_i, 1));
93   c2 = new_Proj (get_irg_args(irg), mode_i, 0);
94   set_value(1, c2);
95
96   cond = new_Cond(new_Proj(new_Cmp(c1, c2), mode_b, Eq));
97   set_value(0, new_Const (mode_i, tarval_from_long (mode_i, 6)));
98   f = new_Proj(cond, mode_X, 0);
99   t = new_Proj(cond, mode_X, 1);
100   mature_block(get_irg_current_block(irg));
101
102   /* end block to add jmps */
103   endBlock = new_immBlock();
104
105   /* Block 1 */
106   Block1 = new_immBlock();
107   add_in_edge(Block1, t);
108   mature_block(Block1);
109   set_value(0, new_Const (mode_i, tarval_from_long (mode_i, 5)));
110   jmp = new_Jmp();
111   add_in_edge(endBlock, jmp);
112
113   /* scndCondBlock */
114   scndCondBlock = new_immBlock();
115   add_in_edge(scndCondBlock, f);
116   mature_block(scndCondBlock);
117   c1 = new_Const (mode_i, tarval_from_long (mode_i, 3));
118   cond = new_Cond(new_Proj(new_Cmp(c1, get_value(1, mode_i)), mode_b, Eq));
119   f = new_Proj(cond, mode_X, 0);
120   t = new_Proj(cond, mode_X, 1);
121   mature_block(get_irg_current_block(irg));
122
123   /* Block 2 */
124   Block2 = new_immBlock();
125   add_in_edge(Block2, f);
126   mature_block(Block2);
127   jmp = new_Jmp();
128   add_in_edge(endBlock, jmp);
129
130   /* Block 3 */
131   Block3 = new_immBlock();
132   add_in_edge(Block3, t);
133   mature_block(Block3);
134   jmp = new_Jmp();
135   add_in_edge(endBlock, jmp);
136
137   /* finish the end Block */
138   switch_block(endBlock);
139   {
140     ir_node *in[1];
141     in[0] = get_value(0, mode_i);
142     x = new_Return (get_store(), 1, in);
143   }
144   mature_block (get_irg_current_block(irg));
145
146   /* finish the Block with the end node */
147   add_in_edge (get_irg_end_block(irg), x);
148   mature_block (get_irg_end_block(irg));
149
150   /* verify the graph */
151   irg_vrfy(irg);
152   finalize_cons (irg);
153
154   printf("Optimizing ...\n");
155   dead_node_elimination(irg);
156
157   printf("Dumping the graph and a control flow graph.\n");
158   dump_ir_block_graph (irg);
159   dump_cfg (irg);
160   printf("Use xvcg to view these graphs:\n");
161   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
162
163   return (0);
164 }