3 * File name: testprograms/const_eval_example.c
4 * Purpose: Test constant evaluation.
5 * Author: Christian Schaefer, Goetz Lindenmaier
9 * Copyright: (c) 1999-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 #include <libfirm/firm.h>
19 * This file constructs the ir for the following pseudo-program:
37 ir_type *method; /* the ir_type of this method */
39 ir_node *a, *b, *c, *d, *x;
41 printf("\nCreating an IR graph: CONST_EVAL_EXAMPLE...\n");
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);
48 /* Try both optimizations: */
49 set_opt_constant_folding(1);
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);
59 irg = new_ir_graph(ent, 4);
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));
65 mature_immBlock(get_irg_current_block(irg));
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);
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)),
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)),
86 x = new_Return(get_store(), 2, in);
89 add_immBlock_pred(get_irg_end_block(irg), x);
90 mature_immBlock(get_irg_end_block(irg));
92 irg_finalize_cons(irg);
94 printf("Optimizing ...\n");
95 dead_node_elimination(irg);
97 /* verify the graph */
100 printf("Done building the graph. Dumping it.\n");
101 dump_ir_block_graph(irg, 0);
103 printf("Use ycomp to view this graph:\n");
104 printf("ycomp GRAPHNAME\n\n");