Initial revision
[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 ***  main() {
18 ***    int a = 0;
19 ***    int b = 2;
20 ***    if ( a == a )  // a constant condition would be eliminated,
21 ***                   // a realistic condition would no more be a small example
22 ***      { a := a - 2; }
23 ***
24 ***    // return a, bla; // das ist hier kein korrekter graph
25 ***  }
26 **/
27
28 int
29 main(void)
30 {
31   ir_graph *irg;
32   type_class *owner;
33   entity *ent;
34   type_method *proc_main; /* typeinformation for the method main */
35   type_primitive *typ;
36   ir_node *x, *r, *t, *f, *a;
37   int a_pos, b_pos;
38   FILE *outfile;
39
40   printf("creating an IR graph: IF_EXAMPLE...\n");
41
42   init_firm ();
43
44 #define CLASSNAME "IF_EXAMPLE"
45 #define METHODNAME "main"
46 #define NRARGS 0
47 #define NRES 2
48
49   owner = new_type_class (id_from_str (CLASSNAME, strlen(CLASSNAME)));
50   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)), NRARGS, NRES);
51   ent = new_entity ((type *)owner,
52                     id_from_str (METHODNAME, strlen(METHODNAME)),
53                     (type *)proc_main);
54
55 #define RES_NAME "res1"
56   typ = new_type_primitive(ID_FROM_STR(RES_NAME, strlen(RES_NAME)), mode_i);
57   set_method_res_type(proc_main, 0, (type*)typ);
58 #undef RES_NAME
59
60 #define RES_NAME "res2"
61   typ = new_type_primitive(ID_FROM_STR(RES_NAME, strlen(RES_NAME)), mode_i);
62   set_method_res_type(proc_main, 1, (type*)typ);
63 #undef RES_NAME
64
65
66   /* Generates start and end blocks and nodes and a first, initial block */
67   irg = new_ir_graph (ent, 1);
68
69   /* The value position used for a: */
70   a_pos = 0;
71   b_pos = 1;
72
73   /* Generate the constant */
74   set_value (a_pos, new_Const (mode_i, tarval_from_long (mode_i, 0)));
75   set_value (b_pos, new_Const (mode_i, tarval_from_long (mode_i, 2)));
76   mature_block (irg->current_block);
77
78   /* Generate a conditional branch */
79   x = new_Cond (new_Proj(new_Cmp(get_value(a_pos, mode_i),
80                                  get_value(a_pos, mode_i)),
81                          mode_b, Eq));
82   f = new_Proj (x, mode_X, 0);
83   t = new_Proj (x, mode_X, 1);
84
85   /* generate and fill the then block */
86   r = new_Block ();
87   add_in_edge (r, t);
88   // mature_block (r);
89   a = new_Sub(get_value(a_pos, mode_i),
90               new_Const (mode_i, tarval_from_long (mode_i, 2)),
91               mode_i);
92   set_value (a_pos, a);
93
94   mature_block (r);
95   x = new_Jmp ();
96
97   /* generate the fall through block and add all cfg edges */
98   r = new_Block ();
99   add_in_edge (r, f);
100   add_in_edge (r, x);
101   mature_block (r);
102   {
103      ir_node *in[3], *store ;
104      in[0] = get_value (a_pos, mode_i);
105      in[1] = a;
106      in[2] = get_value (b_pos, mode_i);
107      store = get_store();
108
109      x = new_Return (store, 2, in);
110   }
111
112   /* finalize the end block generated in new_ir_graph() */
113   add_in_edge (irg->end_block, x);
114   mature_block (irg->end_block);
115
116   /* output the vcg file */
117   printf("\nDone building the graph.  Dumping it.\n");
118
119   dump_ir_block_graph (irg);
120
121   printf("use xvcg to view this graph:\n");
122   printf("/ben/trapp/bin/i486/xvcg GRAPHNAME\n");
123
124   return (0);
125 }