Changed implementation of tr module.
[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 ***  Doesn't work for some reason!!!!
16 ***
17 ***  int main(int a) {
18 ***    int b = 2;
19 ***    if ( a == b )
20 ***      { a := a - 2; }
21 ***
22 ***    return a;
23 ***  }
24 **/
25
26 int
27 main(void)
28 {
29   ir_graph *irg;
30   type *owner;
31   entity *ent;
32   type *proc_main; /* type information for the method main */
33   type *typ;
34   ir_node *x, *r, *t, *f, *a, *cmp;
35   int a_pos, b_pos;
36
37   printf("\nCreating an IR graph: IF_EXAMPLE...\n");
38
39   init_firm ();
40
41 #define CLASSNAME "IF_EXAMPLE"
42 #define METHODNAME "main"
43 #define NRARGS 1
44 #define NRES 1
45
46   owner = get_glob_type();
47   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
48                               NRARGS, NRES);
49   ent = new_entity (owner,
50                     id_from_str (METHODNAME, strlen(METHODNAME)),
51                     proc_main);
52
53 #define RES_NAME "int"
54   typ = new_type_primitive(id_from_str(RES_NAME, strlen(RES_NAME)), mode_i);
55   set_method_param_type(proc_main, 0, typ);
56   set_method_res_type(proc_main, 0, typ);
57
58   /* Generates start and end blocks and nodes and a first, initial block */
59   irg = new_ir_graph (ent, 2);
60
61   /* The value position used for a: */
62   a_pos = 0;
63   b_pos = 1;
64
65   /* Generate the constant */
66   set_value (a_pos, new_Proj (get_irg_args(irg), mode_i, 0));
67   /*set_value (a_pos, new_Const (mode_i, tarval_from_long (mode_i, 0)));*/
68   set_value (b_pos, new_Const (mode_i, tarval_from_long (mode_i, 2)));
69   mature_block (get_irg_current_block(irg));
70
71   /* Generate a conditional branch */
72   cmp = new_Cmp(get_value(a_pos, mode_i), get_value(b_pos, mode_i));
73   x = new_Cond (new_Proj(cmp, mode_b, Eq));
74   f = new_Proj (x, mode_X, 0);
75   t = new_Proj (x, mode_X, 1);
76
77   /* generate and fill the then block */
78   r = new_immBlock ();
79   add_in_edge (r, t);
80   a = new_Sub(get_value(a_pos, mode_i),
81               new_Const (mode_i, tarval_from_long (mode_i, 2)),
82               mode_i);
83   set_value (a_pos, a);
84
85   mature_block (r);
86   x = new_Jmp ();
87
88   /* generate the fall through block and add all cfg edges */
89   r = new_immBlock ();
90   add_in_edge (r, f);
91   add_in_edge (r, x);
92   mature_block (r);
93   {
94      ir_node *in[1], *store ;
95      in[0] = get_value (a_pos, mode_i);
96      store = get_store();
97
98      x = new_Return (store, 1, in);
99   }
100
101   /* finalize the end block generated in new_ir_graph() */
102   add_in_edge (get_irg_end_block(irg), x);
103   mature_block (get_irg_end_block(irg));
104
105
106   printf("Optimizing ...\n");
107   local_optimize_graph(irg);
108   dead_node_elimination(irg);
109
110   /* verify the graph */
111   irg_vrfy(irg);
112
113   /* output the vcg file */
114   printf("Done building the graph.  Dumping it.\n");
115   dump_ir_block_graph (irg);
116   printf("use xvcg to view this graph:\n");
117   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
118
119   return (0);
120 }