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