Changed implementation of tr module.
[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   ir_graph *irg;          /* this variable contains the irgraph */
52   type *owner;      /* the class in which this method is defined */
53   type *proc_main; /* type information for the method main */
54   entity *ent;            /* represents this method as entity of owner */
55   ir_node *c1, *c2, *cond, *f, *t, *endBlock, *Block1, *jmp,
56           *scndCondBlock, *Block2, *Block3, *x;
57
58   /* init library */
59   init_firm ();
60
61   set_optimize(1);
62
63   /* FIRM was designed for oo languages where all methods belong to a class.
64    * For imperative languages like C we view a file as a large class containing
65    * all functions as methods in this file.
66    * This class now is automatically generated.
67    */
68 #define METHODNAME "main"
69 #define NRARGS 1
70 #define NRES 1
71   printf("\nCreating an IR graph: ...\n");
72
73   owner = get_glob_type();
74   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
75                               NRARGS, NRES);
76   /** @@@ setting of arg/res types misses **/
77   ent = new_entity (owner,
78                     id_from_str (METHODNAME, strlen(METHODNAME)),
79                     proc_main);
80
81 #define NUM_OF_LOCAL_VARS 2
82
83   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
84
85   /* to make a condition  */
86   c1 = new_Const (mode_i, tarval_from_long (mode_i, 1));
87   c2 = new_Proj (get_irg_args(irg), mode_i, 0);
88   set_value(1, c2);
89
90   cond = new_Cond(new_Proj(new_Cmp(c1, c2), mode_b, Eq));
91   set_value(0, new_Const (mode_i, tarval_from_long (mode_i, 6)));
92   f = new_Proj(cond, mode_X, 0);
93   t = new_Proj(cond, mode_X, 1);
94   mature_block(get_irg_current_block(irg));
95
96   /* end block to add jmps */
97   endBlock = new_immBlock();
98
99   /* Block 1 */
100   Block1 = new_immBlock();
101   add_in_edge(Block1, t);
102   mature_block(Block1);
103   set_value(0, new_Const (mode_i, tarval_from_long (mode_i, 5)));
104   jmp = new_Jmp();
105   add_in_edge(endBlock, jmp);
106
107   /* scndCondBlock */
108   scndCondBlock = new_immBlock();
109   add_in_edge(scndCondBlock, f);
110   mature_block(scndCondBlock);
111   c1 = new_Const (mode_i, tarval_from_long (mode_i, 3));
112   cond = new_Cond(new_Proj(new_Cmp(c1, get_value(1, mode_i)), mode_b, Eq));
113   f = new_Proj(cond, mode_X, 0);
114   t = new_Proj(cond, mode_X, 1);
115   mature_block(get_irg_current_block(irg));
116
117   /* Block 2 */
118   Block2 = new_immBlock();
119   add_in_edge(Block2, f);
120   mature_block(Block2);
121   jmp = new_Jmp();
122   add_in_edge(endBlock, jmp);
123
124   /* Block 3 */
125   Block3 = new_immBlock();
126   add_in_edge(Block3, t);
127   mature_block(Block3);
128   jmp = new_Jmp();
129   add_in_edge(endBlock, jmp);
130
131   /* finish the end Block */
132   switch_block(endBlock);
133   {
134     ir_node *in[1];
135     in[0] = get_value(0, mode_i);
136     x = new_Return (get_store(), 1, in);
137   }
138   mature_block (get_irg_current_block(irg));
139
140   /* finish the Block with the end node */
141   add_in_edge (get_irg_end_block(irg), x);
142   mature_block (get_irg_end_block(irg));
143
144   printf("Optimizing ...\n");
145   dead_node_elimination(irg);
146
147   /* verify the graph */
148   irg_vrfy(irg);
149
150   printf("Dumping the graph and a control flow graph.\n");
151   dump_ir_block_graph (irg);
152   dump_cfg (irg);
153   printf("Use xvcg to view these graphs:\n");
154   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
155
156   return (0);
157 }