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