removed pointless bespillremat.h includes
[libfirm] / testprograms / if_example.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/if_example.c
4  * Purpose:     Shows construction of if.
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
17
18 #include <libfirm/firm.h>
19
20 /**
21 *  This file constructs the ir for the following pseudo-program:
22 *
23 *  int main(int a) {
24 *    int b = 2;
25 *    if ( a == b )
26 *      { a := a - 3; }
27 *
28 *    return a;
29 *  }
30 **/
31
32 int
33 main(void)
34 {
35   ir_graph *irg;
36   ir_type *owner;
37   ir_entity *ent;
38   ir_type *proc_main; /* ir_type information for the method main */
39   ir_type *typ;
40   ir_node *x, *r, *t, *f, *a, *cmp;
41   int a_pos, b_pos;
42
43   printf("\nCreating an IR graph: IF_EXAMPLE...\n");
44
45   init_firm(NULL);
46
47 #define CLASSNAME "IF_EXAMPLE"
48 #define METHODNAME "IF_EXAMPLE_main"
49 #define NRARGS 1
50 #define NRES 1
51
52   /** Type information for the procedure **/
53
54   owner = get_glob_type();
55   /* Type information for the procedure */
56   proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
57                               NRARGS, NRES);
58   /* The ir_entity for the procedure */
59   ent = new_entity(owner,
60                    new_id_from_chars(METHODNAME, strlen(METHODNAME)),
61                    proc_main);
62   /* The ir_type int.  This ir_type is necessary to model the result and parameters
63      the procedure. */
64 #define PRIM_NAME "int"
65   typ = new_type_primitive(new_id_from_chars(PRIM_NAME, strlen(PRIM_NAME)), mode_Is);
66   /* The parameter and result types of the procedure. */
67   set_method_param_type(proc_main, 0, typ);
68   set_method_res_type(proc_main, 0, typ);
69
70   /** The code of the procedure **/
71
72   /* Generates start and end blocks and nodes, and a first, initial block */
73 #define NRLOCS 2
74   irg = new_ir_graph(ent, NRLOCS);
75
76   /* The value position used for: */
77   a_pos = 0;
78   b_pos = 1;
79
80   /* Get the procedure parameter and assign it to the parameter variable
81      a. */
82   set_value(a_pos, new_Proj(get_irg_args(irg), mode_Is, 0));
83   /* Generate the constant and assign it to b. The assignment is resovled to a
84      dataflow edge. */
85   set_value(b_pos, new_Const(mode_Is, new_tarval_from_long(2, mode_Is)));
86   /* We know all predecessors of the block and all set_values and set_stores are
87      preformed.   We can mature the block.  */
88   mature_immBlock(get_irg_current_block(irg));
89
90   /* Generate a conditional branch */
91   cmp = new_Cmp(get_value(a_pos, mode_Is), get_value(b_pos, mode_Is));
92   x = new_Cond(new_Proj(cmp, mode_b, pn_Cmp_Eq));
93   f = new_Proj(x, mode_X, pn_Cond_false);
94   t = new_Proj(x, mode_X, pn_Cond_true);
95
96   /* generate and fill the then block */
97   r = new_immBlock();
98   add_immBlock_pred(r, t);
99   {
100     ir_node *b,*c;
101     c = new_Const(mode_Is, new_tarval_from_long(3, mode_Is));
102     b = get_value(a_pos, mode_Is);
103     a = new_Sub(b,
104               c,
105               mode_Is);
106   }
107   set_value(a_pos, a);
108
109   mature_immBlock(r);
110   x = new_Jmp();
111
112   /* generate the fall through block and add all cfg edges */
113   r = new_immBlock();
114   add_immBlock_pred(r, f);
115   add_immBlock_pred(r, x);
116   mature_immBlock(r);
117   /* The Return statement */
118   {
119      ir_node *in[1], *store ;
120      in[0] = get_value(a_pos, mode_Is);
121      store = get_store();
122
123      x = new_Return(store, 1, in);
124   }
125
126   /* finalize the end block generated in new_ir_graph() */
127   add_immBlock_pred(get_irg_end_block(irg), x);
128   mature_immBlock(get_irg_end_block(irg));
129
130   /* verify the graph */
131   irg_vrfy(irg);
132   irg_finalize_cons(irg);
133
134   /* output the vcg file */
135   printf("Done building the graph.  Dumping it.\n");
136   dump_ir_block_graph(irg, 0);
137   printf("Use ycomp to view this graph:\n");
138   printf("ycomp GRAPHNAME\n\n");
139
140   return 0;
141 }