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