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