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