- implemented get_irg_value_param_type()
[libfirm] / testprograms / const_eval_example.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/const_eval_example.c
4  * Purpose:     Test constant evaluation.
5  * Author:      Christian Schaefer, Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1999-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #include <stdio.h>
14 #include <string.h>
15
16 #include <libfirm/firm.h>
17
18 /**
19 *  This file constructs the ir for the following pseudo-program:
20 *
21 *  main() {
22 *    int c, d;
23 *
24 *    c = 5 + 7;
25 *    d = 7 + 5;
26 *
27 *    return (c, d);
28 *  }
29 **/
30
31 int
32 main(void)
33 {
34   ir_type     *prim_t_int;
35   ir_graph *irg;
36   ir_type *owner;
37   ir_type *method;    /* the ir_type of this method */
38   ir_entity *ent;
39   ir_node *a, *b, *c, *d, *x;
40
41   printf("\nCreating an IR graph: CONST_EVAL_EXAMPLE...\n");
42
43   init_firm(NULL);
44
45   /*** Make basic ir_type information for primitive ir_type int. ***/
46   prim_t_int = new_type_primitive(new_id_from_chars("int", 3), mode_Is);
47
48   /* Try both optimizations: */
49   set_opt_constant_folding(1);
50   set_opt_cse(1);
51
52   owner = new_type_class(new_id_from_chars("CONST_EVAL_EXAMPLE", 18));
53   method = new_type_method(new_id_from_chars("main", 4), 0, 2);
54   set_method_res_type(method, 0, prim_t_int);
55   set_method_res_type(method, 1, prim_t_int);
56   ent = new_entity(owner, new_id_from_chars("main", 4), method);
57   get_entity_ld_name(ent);
58
59   irg = new_ir_graph(ent, 4);
60
61   a = new_Const(mode_Is, new_tarval_from_long(7, mode_Is));
62   b = new_Const(mode_Is, new_tarval_from_long(5, mode_Is));
63
64   x = new_Jmp();
65   mature_immBlock(get_irg_current_block(irg));
66
67   /*  To test const eval on DivMod
68   c = new_DivMod(get_store(), a, b);
69   set_store(new_Proj(c, mode_M, pn_DivMod_M));
70   d = new_Proj(c, mode_Is, pn_DivMod_res_mod);
71   c = new_Proj(c, mode_Is, pn_DivMod_res_div);
72   */
73
74   c = new_Add(new_Const(mode_Is, new_tarval_from_long(5, mode_Is)),
75                new_Const(mode_Is, new_tarval_from_long(7, mode_Is)),
76                mode_Is);
77   d = new_Add(new_Const(mode_Is, new_tarval_from_long(7, mode_Is)),
78                new_Const(mode_Is, new_tarval_from_long(5, mode_Is)),
79                mode_Is);
80
81   {
82      ir_node *in[2];
83      in[0] = c;
84      in[1] = d;
85
86      x = new_Return(get_store(), 2, in);
87   }
88
89   add_immBlock_pred(get_irg_end_block(irg), x);
90   mature_immBlock(get_irg_end_block(irg));
91
92   irg_finalize_cons(irg);
93
94   printf("Optimizing ...\n");
95   dead_node_elimination(irg);
96
97   /* verify the graph */
98   irg_vrfy(irg);
99
100   printf("Done building the graph.  Dumping it.\n");
101   dump_ir_block_graph(irg, 0);
102
103   printf("Use ycomp to view this graph:\n");
104   printf("ycomp GRAPHNAME\n\n");
105
106   return 0;
107 }