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