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