fixed warnings
[libfirm] / testprograms / irr_cf_example.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/irr_cf_example.c
4  * Purpose:     Test Phi construction with irregular control flow.
5  * Author:      Christian Schaefer, Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1999-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 # include <stdio.h>
14 # include <string.h>
15
16 # include "irvrfy.h"
17 # include "irdump.h"
18 # include "firm.h"
19
20
21 /**
22 *  This file constructs a control flow of following shape:
23 *
24 * StartBlock
25 *     |
26 *    \|/
27 *   Block --->  Block
28 *     |           |
29 *    \|/         \|/
30 *   Block --->  Block
31 *     |           |
32 *    \|/         \|/
33 *   Block --->  Block
34 *                 |
35 *                \|/
36 *              EndBlock
37 *
38 *   This is a program as, e.g.,
39 *
40 *   switch (expr){
41 *     case 1:
42 *     case 2:
43 *       break;
44 *     default:
45 *   }
46 *   return
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;  /* typeinformation for the method main */
54   entity   *ent;        /* represents this method as entity of owner */
55   ir_node  *expr, *c1, *c2, *cond, *f, *t, *jmp, *x;
56
57   printf("\nCreating an IR graph: IRR_CF...\n");
58
59   /* init library */
60   init_firm (NULL);
61   set_opt_constant_folding(0); /* so that stupid test are not evaluated. */
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    * Therefore we define a class "empty" according to the file name
67    * with a method main as an entity.
68    */
69 #define CLASSNAME "IRREGULAR_CF"
70 #define METHODNAME "main"
71 #define NRARGS 0
72 #define NRES 0
73
74   owner = new_type_class (new_id_from_chars (CLASSNAME, strlen(CLASSNAME)));
75   proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
76                               NRARGS, NRES);
77   ent = new_entity ((type *)owner,
78                     new_id_from_chars (METHODNAME, strlen(METHODNAME)),
79                     (type *)proc_main);
80   get_entity_ld_name(ent);
81 #define NUM_OF_LOCAL_VARS 0
82
83   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
84
85   /* two make two conditionals that represent a switch */
86   expr = new_Const (mode_Is, new_tarval_from_long (0, mode_Is));
87   c1 = new_Const (mode_Is, new_tarval_from_long (1, mode_Is));
88   c2 = new_Const (mode_Is, new_tarval_from_long (2, mode_Is));
89
90   cond = new_Cond(new_Proj(new_Cmp(expr, c1), mode_b, pn_Cmp_Eq));
91   f = new_Proj(cond, mode_X, 0);
92   t = new_Proj(cond, mode_X, 1);
93   mature_immBlock(get_irg_current_block(irg));
94
95   new_immBlock();
96   add_immBlock_pred(get_irg_current_block(irg), t);
97   jmp = new_Jmp();
98   mature_immBlock(get_irg_current_block(irg));
99
100   new_immBlock();
101   add_immBlock_pred(get_irg_current_block(irg), f);
102   cond = new_Cond(new_Proj(new_Cmp(expr, c2), mode_b, pn_Cmp_Eq));
103   f = new_Proj(cond, mode_X, 0);
104   t = new_Proj(cond, mode_X, 1);
105   mature_immBlock(get_irg_current_block(irg));
106
107   new_immBlock();
108   add_immBlock_pred(get_irg_current_block(irg), t);
109   add_immBlock_pred(get_irg_current_block(irg), jmp);
110   jmp = new_Jmp();
111   mature_immBlock(get_irg_current_block(irg));
112
113   new_immBlock();
114   add_immBlock_pred(get_irg_current_block(irg), f);
115   t = new_Jmp();
116   mature_immBlock(get_irg_current_block(irg));
117
118   new_immBlock();
119   add_immBlock_pred(get_irg_current_block(irg), t);
120   add_immBlock_pred(get_irg_current_block(irg), jmp);
121
122   x = new_Return (get_store(), 0, NULL);
123
124   mature_immBlock (get_irg_current_block(irg));
125
126   add_immBlock_pred (get_irg_end_block(irg), x);
127   mature_immBlock (get_irg_end_block(irg));
128
129   irg_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   char *dump_file_suffix = "";
139   dump_ir_block_graph (irg, dump_file_suffix);
140   dump_cfg (irg, dump_file_suffix);
141   printf("Use xvcg to view these graphs:\n");
142   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
143
144   return (0);
145 }