Initial revision
[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_class *owner;      /* the class in which this method is defined */
50   type_method *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("creating an IR graph: IRR_CF...\n");
55
56   /* init library */
57   init_firm ();
58
59   /* FIRM was designed for oo languages where all methods belong to a class.
60    * For imperative languages like C we view a file as a large class containing
61    * all functions as methods in this file.
62    * Therefore we define a class "empty" according to the file name
63    * with a method main as an entity.
64    */
65 #define CLASSNAME "IRREGULAR_CF"
66 #define METHODNAME "main"
67 #define NRARGS 0
68 #define NRES 0
69
70   owner = new_type_class (id_from_str (CLASSNAME, strlen(CLASSNAME)));
71   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
72                               NRARGS, NRES);
73   ent = new_entity ((type *)owner,
74                     id_from_str (METHODNAME, strlen(METHODNAME)),
75                     (type *)proc_main);
76
77 #define NUM_OF_LOCAL_VARS 0
78
79   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
80
81   /* two make two conditionals that represent a switch */
82   expr = new_Const (mode_i, tarval_from_long (mode_i, 0));
83   c1 = new_Const (mode_i, tarval_from_long (mode_i, 1));
84   c2 = new_Const (mode_i, tarval_from_long (mode_i, 2));
85
86   cond = new_Cond(new_Proj(new_Cmp(expr, c1), mode_b, Eq));
87   f = new_Proj(cond, mode_X, 0);
88   t = new_Proj(cond, mode_X, 1);
89   mature_block(irg->current_block);
90
91   new_Block();
92   add_in_edge(irg->current_block, t);
93   jmp = new_Jmp();
94   mature_block(irg->current_block);
95
96   new_Block();
97   add_in_edge(irg->current_block, f);
98   cond = new_Cond(new_Proj(new_Cmp(expr, c2), mode_b, Eq));
99   f = new_Proj(cond, mode_X, 0);
100   t = new_Proj(cond, mode_X, 1);
101   mature_block(irg->current_block);
102
103   new_Block();
104   add_in_edge(irg->current_block, t);
105   add_in_edge(irg->current_block, jmp);
106   jmp = new_Jmp();
107   mature_block(irg->current_block);
108
109   new_Block();
110   add_in_edge(irg->current_block, f);
111   t = new_Jmp();
112   mature_block(irg->current_block);
113
114   new_Block();
115   add_in_edge(irg->current_block, t);
116   add_in_edge(irg->current_block, jmp);
117   {
118     ir_node *in[0]; /* this is the array containing the return parameters */
119     x = new_Return (get_store(), 0, in);
120   }
121   mature_block (irg->current_block);
122
123   add_in_edge (irg->end_block, x);
124   mature_block (irg->end_block);
125
126   printf("\nDone building the graph.\n");
127   printf("Dumping the graph and a control flow graph.\n");
128   dump_ir_block_graph (irg);
129   dump_cfg (irg);
130
131   printf("use xvcg to view these graphs:\n");
132   printf("/ben/trapp/bin/i486/xvcg GRAPHNAME\n");
133
134   return (0);
135 }