- simplified code
[libfirm] / testprograms / endless_loop.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/endless_loop.c
4  * Purpose:     Representation of an endless loop.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1999-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #include <stdio.h>
14 #include <string.h>
15
16 #include <libfirm/firm.h>
17
18 /**
19 *  This file constructs the ir for the following pseudo-program:
20 *
21 *  VAR_A is some extern variable.
22 *
23 *  main(int a) {        //  pos 0
24 *    int b = 1;         //  pos 1
25 *    int h;             //  pos 2
26 *
27 *    while (0 == 0) loop {
28 *      h = a;
29 *      a = b;
30 *      b = h;
31 *      VAR_A = b;
32 *    }
33 *
34 *    return a-b;
35 *  }
36 **/
37
38 int
39 main(void)
40 {
41   char *dump_file_suffix = "";
42   ir_type *prim_t_int;
43   ir_graph *irg;
44   ir_type *owner;
45   ir_type *proc_main;
46   ir_entity *ent;
47   ir_node *b, *x, *r, *t, *f;
48   union symconst_symbol symbol;
49
50   printf("\nCreating an IR graph: ENDLESS_LOOP_EXAMPLE...\n");
51
52   init_firm(NULL);
53
54   set_optimize(1);
55   set_opt_constant_folding(1);
56   set_opt_cse(1);
57   set_opt_global_cse(0);
58
59   prim_t_int = new_type_primitive(new_id_from_chars("int", 3), mode_Is);
60
61 #define METHODNAME "main_tp"
62 #define NRARGS 1
63 #define NRES 1
64
65   proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
66                               NRARGS, NRES);
67   set_method_param_type(proc_main, 0, prim_t_int);
68   set_method_res_type(proc_main, 0, prim_t_int);
69
70
71   owner = new_type_class(new_id_from_chars("ENDLESS_LOOP_EXAMPLE", 20));
72   ent = new_entity(owner, new_id_from_chars("main", strlen("main")), proc_main);
73   get_entity_ld_name(ent); /* force name mangling */
74
75   /* Generates start and end blocks and nodes and a first, initial block */
76   irg = new_ir_graph(ent, 4);
77
78   /* Generate two values */
79   set_value(0, new_Proj(get_irg_args(irg), mode_Is, 0));
80   set_value(1, new_Const(mode_Is, new_tarval_from_long(1, mode_Is)));
81
82   x = new_Jmp();
83   mature_immBlock(get_irg_current_block(irg));
84
85   /* generate a block for the loop header and the conditional branch */
86   r = new_immBlock();
87   add_immBlock_pred(r, x);
88   x = new_Cond(new_Proj(new_Cmp(new_Const(mode_Is, new_tarval_from_long(0, mode_Is)),
89                  new_Const(mode_Is, new_tarval_from_long(0, mode_Is))),
90              mode_b, pn_Cmp_Eq));
91   f = new_Proj(x, mode_X, pn_Cond_false);
92   t = new_Proj(x, mode_X, pn_Cond_true);
93
94   /* generate the block for the loop body */
95   b = new_immBlock();
96   add_immBlock_pred(b, t);
97   x = new_Jmp();
98   add_immBlock_pred(r, x);
99
100   /* The code in the loop body,
101      as we are dealing with local variables only the dataflow edges
102      are manipulated. */
103   set_value(2, get_value(0, mode_Is));
104   set_value(0, get_value(1, mode_Is));
105   set_value(1, get_value(2, mode_Is));
106
107   /* set VAR_A to constant value */
108   symbol.entity_p = new_entity(get_glob_type(),new_id_from_chars("VAR_A",6),prim_t_int);
109   set_store(new_Proj(new_Store(get_store(),
110                                   new_SymConst(mode_P, symbol, symconst_addr_ent),
111                                   get_value(1, mode_Is)),
112                           mode_M, pn_Store_M));
113
114   mature_immBlock(b);
115   mature_immBlock(r);
116
117   /* generate the return block */
118   r = new_immBlock();
119   add_immBlock_pred(r, f);
120   mature_immBlock(r);
121
122   {
123      ir_node *in[1];
124      in[0] = new_Sub(get_value(0, mode_Is), get_value(1, mode_Is), mode_Is);
125
126      x = new_Return(get_store(), 1, in);
127   }
128
129   /* finalize the end block generated in new_ir_graph() */
130   add_immBlock_pred(get_irg_end_block(irg), x);
131   mature_immBlock(get_irg_end_block(irg));
132
133   irg_finalize_cons(irg);
134
135   printf("Optimizing ...\n");
136
137   dead_node_elimination(irg);
138   local_optimize_graph(irg);
139
140   /* verify the graph */
141   irg_vrfy(irg);
142
143   /* output the vcg file */
144   printf("Done building the graph.  Dumping it.\n");
145   /* turn_of_edge_labels(); */
146   dump_keepalive_edges(1);
147   dump_all_types(dump_file_suffix);
148   dump_ir_block_graph(irg, dump_file_suffix);
149   printf("Use ycomp to view this graph:\n");
150   printf("ycomp GRAPHNAME\n\n");
151
152   return 0;
153 }