3b8b9211aa90c7bfb2793046ca3d1646a3c9d18e
[libfirm] / testprograms / irr_cf_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  *  irregular control flow
16  */
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(int argc, char **argv)
47 {
48   ir_graph *irg;        /* this variable contains the irgraph */
49   type     *owner;      /* the class in which this method is defined */
50   type     *proc_main;  /* typeinformation for the method main */
51   entity   *ent;        /* represents this method as entity of owner */
52   ir_node  *expr, *c1, *c2, *cond, *f, *t, *jmp, *x;
53
54   printf("\nCreating an IR graph: IRR_CF...\n");
55
56   /* init library */
57   init_firm ();
58   set_opt_constant_folding(0); /* so that stupid test are not evaluated. */
59
60   /* FIRM was designed for oo languages where all methods belong to a class.
61    * For imperative languages like C we view a file as a large class containing
62    * all functions as methods in this file.
63    * Therefore we define a class "empty" according to the file name
64    * with a method main as an entity.
65    */
66 #define CLASSNAME "IRREGULAR_CF"
67 #define METHODNAME "main"
68 #define NRARGS 0
69 #define NRES 0
70
71   owner = new_type_class (id_from_str (CLASSNAME, strlen(CLASSNAME)));
72   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
73                               NRARGS, NRES);
74   ent = new_entity ((type *)owner,
75                     id_from_str (METHODNAME, strlen(METHODNAME)),
76                     (type *)proc_main);
77
78 #define NUM_OF_LOCAL_VARS 0
79
80   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
81
82   /* two make two conditionals that represent a switch */
83   expr = new_Const (mode_i, tarval_from_long (mode_i, 0));
84   c1 = new_Const (mode_i, tarval_from_long (mode_i, 1));
85   c2 = new_Const (mode_i, tarval_from_long (mode_i, 2));
86
87   cond = new_Cond(new_Proj(new_Cmp(expr, c1), mode_b, Eq));
88   f = new_Proj(cond, mode_X, 0);
89   t = new_Proj(cond, mode_X, 1);
90   mature_block(get_irg_current_block(irg));
91
92   new_immBlock();
93   add_in_edge(get_irg_current_block(irg), t);
94   jmp = new_Jmp();
95   mature_block(get_irg_current_block(irg));
96
97   new_immBlock();
98   add_in_edge(get_irg_current_block(irg), f);
99   cond = new_Cond(new_Proj(new_Cmp(expr, c2), mode_b, Eq));
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   new_immBlock();
105   add_in_edge(get_irg_current_block(irg), t);
106   add_in_edge(get_irg_current_block(irg), jmp);
107   jmp = new_Jmp();
108   mature_block(get_irg_current_block(irg));
109
110   new_immBlock();
111   add_in_edge(get_irg_current_block(irg), f);
112   t = new_Jmp();
113   mature_block(get_irg_current_block(irg));
114
115   new_immBlock();
116   add_in_edge(get_irg_current_block(irg), t);
117   add_in_edge(get_irg_current_block(irg), jmp);
118   {
119     ir_node *in[0]; /* this is the array containing the return parameters */
120     x = new_Return (get_store(), 0, in);
121   }
122   mature_block (get_irg_current_block(irg));
123
124   add_in_edge (get_irg_end_block(irg), x);
125   mature_block (get_irg_end_block(irg));
126
127   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);
137   dump_cfg (irg);
138   printf("Use xvcg to view these graphs:\n");
139   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
140
141   return (0);
142 }