Convert comments to doxygen
[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   get_entity_ld_name(ent);
57
58   irg = new_ir_graph (ent, 4);
59
60   a = new_Const (mode_Is, tarval_from_long (mode_Is, 7));
61   b = new_Const (mode_Is, tarval_from_long (mode_Is, 5));
62
63   x = new_Jmp ();
64   mature_block (get_irg_current_block(irg));
65
66   /*  To test const eval on DivMod
67   c = new_DivMod(get_store(), a, b);
68   set_store(new_Proj(c, mode_M, 0));
69   d = new_Proj(c, mode_Is, 3);
70   c = new_Proj(c, mode_Is, 2);
71   */
72
73   c = new_Add (new_Const (mode_Is, tarval_from_long (mode_Is, 5)),
74                new_Const (mode_Is, tarval_from_long (mode_Is, 7)),
75                mode_Is);
76   d = new_Add (new_Const (mode_Is, tarval_from_long (mode_Is, 7)),
77                new_Const (mode_Is, tarval_from_long (mode_Is, 5)),
78                mode_Is);
79
80   {
81      ir_node *in[2];
82      in[0] = c;
83      in[1] = d;
84
85      x = new_Return (get_store (), 2, in);
86   }
87
88   add_in_edge (get_irg_end_block(irg), x);
89   mature_block (get_irg_end_block(irg));
90
91   finalize_cons (irg);
92
93   printf("Optimizing ...\n");
94   dead_node_elimination(irg);
95
96   /* verify the graph */
97   irg_vrfy(irg);
98
99   printf("Done building the graph.  Dumping it.\n");
100   dump_ir_block_graph (irg);
101
102   printf("use xvcg to view this graph:\n");
103   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
104
105   return (0);
106 }