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