defined function as INLINE to get around gcc warnigs, fix some doxygen comments
[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 # include <string.h>
11
12 # include "irvrfy.h"
13 # include "irdump.h"
14 # include "firm.h"
15
16 /*
17  *  irregular control flow
18  */
19
20 /**
21 *  This file constructs a control flow of following shape:
22 *
23 * StartBlock
24 *     |
25 *    \|/
26 *   Block --->  Block
27 *     |           |
28 *    \|/         \|/
29 *   Block --->  Block
30 *     |           |
31 *    \|/         \|/
32 *   Block --->  Block
33 *                 |
34 *                \|/
35 *              EndBlock
36 *
37 *   This is a program as, e.g.,
38 *
39 *   switch (expr){
40 *     case 1:
41 *     case 2:
42 *       break;
43 *     default:
44 *   }
45 *   return
46 **/
47
48 int main(int argc, char **argv)
49 {
50   ir_graph *irg;        /* this variable contains the irgraph */
51   type     *owner;      /* the class in which this method is defined */
52   type     *proc_main;  /* typeinformation for the method main */
53   entity   *ent;        /* represents this method as entity of owner */
54   ir_node  *expr, *c1, *c2, *cond, *f, *t, *jmp, *x;
55
56   printf("\nCreating an IR graph: IRR_CF...\n");
57
58   /* init library */
59   init_firm ();
60   set_opt_constant_folding(0); /* so that stupid test are not evaluated. */
61
62   /* FIRM was designed for oo languages where all methods belong to a class.
63    * For imperative languages like C we view a file as a large class containing
64    * all functions as methods in this file.
65    * Therefore we define a class "empty" according to the file name
66    * with a method main as an entity.
67    */
68 #define CLASSNAME "IRREGULAR_CF"
69 #define METHODNAME "main"
70 #define NRARGS 0
71 #define NRES 0
72
73   owner = new_type_class (id_from_str (CLASSNAME, strlen(CLASSNAME)));
74   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
75                               NRARGS, NRES);
76   ent = new_entity ((type *)owner,
77                     id_from_str (METHODNAME, strlen(METHODNAME)),
78                     (type *)proc_main);
79   get_entity_ld_name(ent);
80 #define NUM_OF_LOCAL_VARS 0
81
82   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
83
84   /* two make two conditionals that represent a switch */
85   expr = new_Const (mode_Is, tarval_from_long (mode_Is, 0));
86   c1 = new_Const (mode_Is, tarval_from_long (mode_Is, 1));
87   c2 = new_Const (mode_Is, tarval_from_long (mode_Is, 2));
88
89   cond = new_Cond(new_Proj(new_Cmp(expr, c1), mode_b, Eq));
90   f = new_Proj(cond, mode_X, 0);
91   t = new_Proj(cond, mode_X, 1);
92   mature_block(get_irg_current_block(irg));
93
94   new_immBlock();
95   add_in_edge(get_irg_current_block(irg), t);
96   jmp = new_Jmp();
97   mature_block(get_irg_current_block(irg));
98
99   new_immBlock();
100   add_in_edge(get_irg_current_block(irg), f);
101   cond = new_Cond(new_Proj(new_Cmp(expr, c2), mode_b, Eq));
102   f = new_Proj(cond, mode_X, 0);
103   t = new_Proj(cond, mode_X, 1);
104   mature_block(get_irg_current_block(irg));
105
106   new_immBlock();
107   add_in_edge(get_irg_current_block(irg), t);
108   add_in_edge(get_irg_current_block(irg), jmp);
109   jmp = new_Jmp();
110   mature_block(get_irg_current_block(irg));
111
112   new_immBlock();
113   add_in_edge(get_irg_current_block(irg), f);
114   t = new_Jmp();
115   mature_block(get_irg_current_block(irg));
116
117   new_immBlock();
118   add_in_edge(get_irg_current_block(irg), t);
119   add_in_edge(get_irg_current_block(irg), jmp);
120   {
121     ir_node *in[0]; /* this is the array containing the return parameters */
122     x = new_Return (get_store(), 0, in);
123   }
124   mature_block (get_irg_current_block(irg));
125
126   add_in_edge (get_irg_end_block(irg), x);
127   mature_block (get_irg_end_block(irg));
128
129   finalize_cons (irg);
130
131   printf("Optimizing ...\n");
132   dead_node_elimination(irg);
133
134   /* verify the graph */
135   irg_vrfy(irg);
136
137   printf("Dumping the graph and a control flow graph.\n");
138   dump_ir_block_graph (irg);
139   dump_cfg (irg);
140   printf("Use xvcg to view these graphs:\n");
141   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
142
143   return (0);
144 }