Remove the unused parameter const arch_env_t *env from arch_reg_is_allocatable().
[libfirm] / testprograms / while_example.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/while_example.c
4  * Purpose:     Construct a 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 #include <stdio.h>
13 #include <string.h>
14
15 #include <libfirm/firm.h>
16
17 /**
18 *  This file constructs the ir for the following pseudo-program:
19 *
20 *  main(int a) {        //  pos 0
21 *    int b = 1;         //  pos 1
22 *    int h;             //  pos 2
23 *
24 *    while (0 == 2) loop {
25 *      h = a;
26 *      a = b;
27 *      b = h;
28 *    }
29 *
30 *    return a-b;
31 *  }
32 **/
33
34 int
35 main(void)
36 {
37   const char *suffix = "";
38   ir_type *prim_t_int;
39   ir_graph *irg;
40   ir_type *owner;
41   ir_type *proc_main;
42   ir_entity *ent;
43   ir_node *b, *x, *r, *t, *f;
44
45   printf("\nCreating an IR graph: WHILE_EXAMPLE...\n");
46
47   init_firm(NULL);
48
49   set_optimize(1);
50   set_opt_constant_folding(1);
51   set_opt_cse(1);
52
53   prim_t_int = new_type_primitive(new_id_from_chars("int", 3), mode_Is);
54
55 #define METHODNAME "main_tp"
56 #define NRARGS 1
57 #define NRES 1
58
59   proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
60                               NRARGS, NRES);
61   set_method_param_type(proc_main, 0, prim_t_int);
62   set_method_res_type(proc_main, 0, prim_t_int);
63
64
65   owner = new_type_class(new_id_from_chars("WHILE_EXAMPLE", 13));
66   ent = new_entity(owner, new_id_from_chars("main", strlen("main")), proc_main);
67   get_entity_ld_name(ent); /* force name mangling */
68
69   /* Generates start and end blocks and nodes and a first, initial block */
70   irg = new_ir_graph(ent, 4);
71
72   /* Generate two values */
73   set_value(0, new_Proj(get_irg_args(irg), mode_Is, 0));
74   set_value(1, new_Const(mode_Is, new_tarval_from_long(1, mode_Is)));
75   x = new_Jmp();
76   mature_immBlock(get_irg_current_block(irg));
77
78
79   /* generate a block for the loop header and the conditional branch */
80   r = new_immBlock();
81   add_immBlock_pred(r, x);
82   x = new_Cond(new_Proj(new_Cmp(new_Const(mode_Is, new_tarval_from_long(0, mode_Is)),
83                  get_value(1, mode_Is)),
84                          mode_b, pn_Cmp_Eq));
85   f = new_Proj(x, mode_X, pn_Cond_false);
86   t = new_Proj(x, mode_X, pn_Cond_true);
87
88   /* generate the block for the loop body */
89   b = new_immBlock();
90   add_immBlock_pred(b, t);
91   x = new_Jmp();
92   add_immBlock_pred(r, x);
93
94   /* The code in the loop body,
95      as we are dealing with local variables only the dataflow edges
96      are manipulated. */
97   set_value(2, get_value(0, mode_Is));
98   set_value(0, get_value(1, mode_Is));
99   set_value(1, get_value(2, mode_Is));
100   mature_immBlock(b);
101   mature_immBlock(r);
102
103   /* generate the return block */
104   r = new_immBlock();
105   add_immBlock_pred(r, f);
106   mature_immBlock(r);
107
108   {
109      ir_node *in[1];
110      in[0] = new_Sub(get_value(0, mode_Is), get_value(1, mode_Is), mode_Is);
111
112      x = new_Return(get_store(), 1, in);
113   }
114
115   /* finalize the end block generated in new_ir_graph() */
116   add_immBlock_pred(get_irg_end_block(irg), x);
117   mature_immBlock(get_irg_end_block(irg));
118
119   irg_finalize_cons(irg);
120
121   printf("Optimizing ...\n");
122
123   local_optimize_graph(irg),
124   dead_node_elimination(irg);
125
126   /* verify the graph */
127   irg_vrfy(irg);
128
129   /* output the vcg file */
130   printf("Done building the graph.  Dumping it.\n");
131   turn_off_edge_labels();
132   dump_all_types(suffix);
133   dump_ir_block_graph(irg, suffix);
134   printf("Use ycomp to view this graph:\n");
135   printf("ycomp WHILE_EXAMPLE\n\n");
136
137   return(0);
138 }