New
[libfirm] / testprograms / const_eval_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 ***  main() {
16 ***    int c, d;
17 ***
18 ***    c = 5 + 7;
19 ***    d = 7 + 5;
20 ***
21 ***    return (c, d);
22 ***  }
23 **/
24
25 int
26 main(void)
27 {
28   type     *prim_t_int;
29   ir_graph *irg;
30   type *owner;
31   type *method;    /* the type of this method */
32   entity *ent;
33   ir_node *a, *b, *c, *d, *x;
34
35   printf("\nCreating an IR graph: CONST_EVAL_EXAMPLE...\n");
36
37   init_firm ();
38
39   /*** Make basic type information for primitive type int. ***/
40   prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_i);
41
42   /* Try both optimizations: */
43   set_opt_constant_folding(1);
44   set_opt_cse(1);
45   set_opt_dead_node_elimination (1);
46
47   owner = new_type_class (id_from_str ("CONST_EVAL_EXAMPLE", 18));
48   method = new_type_method (id_from_str("main", 4), 0, 2);
49   set_method_res_type(method, 0, prim_t_int);
50   set_method_res_type(method, 1, prim_t_int);
51   ent = new_entity (owner, id_from_str ("main", 4), method);
52
53   irg = new_ir_graph (ent, 4);
54
55   a = new_Const (mode_i, tarval_from_long (mode_i, 7));
56   b = new_Const (mode_i, tarval_from_long (mode_i, 5));
57
58   x = new_Jmp ();
59   mature_block (get_irg_current_block(irg));
60
61   /*  To test const eval on DivMod
62   c = new_DivMod(get_store(), a, b);
63   set_store(new_Proj(c, mode_M, 0));
64   d = new_Proj(c, mode_i, 3);
65   c = new_Proj(c, mode_i, 2);
66   */
67
68   c = new_Add (new_Const (mode_i, tarval_from_long (mode_i, 5)),
69                new_Const (mode_i, tarval_from_long (mode_i, 7)),
70                mode_i);
71   d = new_Add (new_Const (mode_i, tarval_from_long (mode_i, 7)),
72                new_Const (mode_i, tarval_from_long (mode_i, 5)),
73                mode_i);
74
75   {
76      ir_node *in[2];
77      in[0] = c;
78      in[1] = d;
79
80      x = new_Return (get_store (), 2, in);
81   }
82
83   add_in_edge (get_irg_end_block(irg), x);
84   mature_block (get_irg_end_block(irg));
85
86   finalize_cons (irg);
87
88   printf("Optimizing ...\n");
89   dead_node_elimination(irg);
90
91   /* verify the graph */
92   irg_vrfy(irg);
93
94   printf("Done building the graph.  Dumping it.\n");
95   dump_ir_block_graph (irg);
96
97   printf("use xvcg to view this graph:\n");
98   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
99
100   return (0);
101 }