3 * File name: testprograms/if_example.c
4 * Purpose: Shows construction of if.
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.
18 #include <libfirm/firm.h>
21 * This file constructs the ir for the following pseudo-program:
38 ir_type *proc_main; /* ir_type information for the method main */
40 ir_node *x, *r, *t, *f, *a, *cmp;
43 printf("\nCreating an IR graph: IF_EXAMPLE...\n");
47 #define CLASSNAME "IF_EXAMPLE"
48 #define METHODNAME "IF_EXAMPLE_main"
52 /** Type information for the procedure **/
54 owner = get_glob_type();
55 /* Type information for the procedure */
56 proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
58 /* The ir_entity for the procedure */
59 ent = new_entity(owner,
60 new_id_from_chars(METHODNAME, strlen(METHODNAME)),
62 /* The ir_type int. This ir_type is necessary to model the result and parameters
64 #define PRIM_NAME "int"
65 typ = new_type_primitive(new_id_from_chars(PRIM_NAME, strlen(PRIM_NAME)), mode_Is);
66 /* The parameter and result types of the procedure. */
67 set_method_param_type(proc_main, 0, typ);
68 set_method_res_type(proc_main, 0, typ);
70 /** The code of the procedure **/
72 /* Generates start and end blocks and nodes, and a first, initial block */
74 irg = new_ir_graph(ent, NRLOCS);
76 /* The value position used for: */
80 /* Get the procedure parameter and assign it to the parameter variable
82 set_value(a_pos, new_Proj(get_irg_args(irg), mode_Is, 0));
83 /* Generate the constant and assign it to b. The assignment is resovled to a
85 set_value(b_pos, new_Const(mode_Is, new_tarval_from_long(2, mode_Is)));
86 /* We know all predecessors of the block and all set_values and set_stores are
87 preformed. We can mature the block. */
88 mature_immBlock(get_irg_current_block(irg));
90 /* Generate a conditional branch */
91 cmp = new_Cmp(get_value(a_pos, mode_Is), get_value(b_pos, mode_Is));
92 x = new_Cond(new_Proj(cmp, mode_b, pn_Cmp_Eq));
93 f = new_Proj(x, mode_X, pn_Cond_false);
94 t = new_Proj(x, mode_X, pn_Cond_true);
96 /* generate and fill the then block */
98 add_immBlock_pred(r, t);
101 c = new_Const(mode_Is, new_tarval_from_long(3, mode_Is));
102 b = get_value(a_pos, mode_Is);
112 /* generate the fall through block and add all cfg edges */
114 add_immBlock_pred(r, f);
115 add_immBlock_pred(r, x);
117 /* The Return statement */
119 ir_node *in[1], *store ;
120 in[0] = get_value(a_pos, mode_Is);
123 x = new_Return(store, 1, in);
126 /* finalize the end block generated in new_ir_graph() */
127 add_immBlock_pred(get_irg_end_block(irg), x);
128 mature_immBlock(get_irg_end_block(irg));
130 /* verify the graph */
132 irg_finalize_cons(irg);
134 /* output the vcg file */
135 printf("Done building the graph. Dumping it.\n");
136 dump_ir_block_graph(irg, 0);
137 printf("Use ycomp to view this graph:\n");
138 printf("ycomp GRAPHNAME\n\n");