Initial revision
[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   entity *ent;
31   ir_node *a, *b, *c, *d, *x;
32
33   printf("creating an IR graph: CONST_EVAL_EXAMPLE...\n");
34
35   init_firm ();
36
37   /* Try both optimizations: */
38   set_opt_constant_folding(1);
39   set_opt_cse(1);
40
41   owner = new_type_class (id_from_str ("CONST_EVAL_EXAMPLE", 18));
42   ent = new_entity ((type *)owner, id_from_str ("main", 4), NULL);
43
44   irg = new_ir_graph (ent, 4);
45
46   a = new_Const (mode_i, tarval_from_long (mode_i, 5));
47   b = new_Const (mode_i, tarval_from_long (mode_i, 7));
48
49   x = new_Jmp ();
50   mature_block (irg->current_block);
51
52   c = new_Add (new_Const (mode_i, tarval_from_long (mode_i, 5)),
53                new_Const (mode_i, tarval_from_long (mode_i, 7)),
54                mode_i);
55   d = new_Add (new_Const (mode_i, tarval_from_long (mode_i, 7)),
56                new_Const (mode_i, tarval_from_long (mode_i, 5)),
57                mode_i);
58
59   {
60      ir_node *in[2];
61      in[0] = c;
62      in[1] = d;
63
64      x = new_Return (get_store (), 2, in);
65   }
66
67   add_in_edge (irg->end_block, x);
68   mature_block (irg->end_block);
69
70   printf("\nDone building the graph.  Dumping it.\n");
71   dump_ir_block_graph (irg);
72
73   printf("use xvcg to view this graph:\n");
74   printf("/ben/trapp/bin/i486/xvcg GRAPHNAME\n");
75
76   return (0);
77 }