3 * File name: testprograms/three_cfpred_example.c
4 * Purpose: Construct a block with more than two predecessors.
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.
22 * This file constructs a control flow of following shape:
40 * This is a program as, e.g.,
43 * { Jmp label1; } // happens anyways
45 * { Jmp label1; } // happens anyways
52 int main(int argc, char **argv)
55 ir_graph *irg; /* this variable contains the irgraph */
56 type *owner; /* the class in which this method is defined */
57 type *proc_main; /* type information for the method main */
58 entity *ent; /* represents this method as entity of owner */
59 ir_node *c1, *c2, *cond, *f, *t, *endBlock, *Block1, *jmp,
60 *scndCondBlock, *Block2, *Block3, *x;
67 /*** Make basic type information for primitive type int. ***/
68 prim_t_int = new_type_primitive(new_id_from_chars ("int", 3), mode_Is);
70 /* FIRM was designed for oo languages where all methods belong to a class.
71 * For imperative languages like C we view a file as a large class containing
72 * all functions as methods in this file.
73 * This class now is automatically generated.
75 #define METHODNAME "THREE_CFPRED_EXAMPLE_main"
78 printf("\nCreating an IR graph: THREE_CFPRED_EXAMPLE ...\n");
80 owner = get_glob_type();
81 proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
83 set_method_param_type(proc_main, 0, prim_t_int);
84 set_method_res_type(proc_main, 0, prim_t_int);
86 ent = new_entity (owner,
87 new_id_from_chars (METHODNAME, strlen(METHODNAME)),
90 #define NUM_OF_LOCAL_VARS 2
92 irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
94 /* to make a condition */
95 c1 = new_Const (mode_Is, new_tarval_from_long (1, mode_Is));
96 c2 = new_Proj (get_irg_args(irg), mode_Is, 0);
99 cond = new_Cond(new_Proj(new_Cmp(c1, c2), mode_b, pn_Cmp_Eq));
100 set_value(0, new_Const (mode_Is, new_tarval_from_long (6, mode_Is)));
101 f = new_Proj(cond, mode_X, 0);
102 t = new_Proj(cond, mode_X, 1);
103 mature_immBlock(get_irg_current_block(irg));
105 /* end block to add jmps */
106 endBlock = new_immBlock();
109 Block1 = new_immBlock();
110 add_immBlock_pred(Block1, t);
111 mature_immBlock(Block1);
112 set_value(0, new_Const (mode_Is, new_tarval_from_long (5, mode_Is)));
114 add_immBlock_pred(endBlock, jmp);
117 scndCondBlock = new_immBlock();
118 add_immBlock_pred(scndCondBlock, f);
119 mature_immBlock(scndCondBlock);
120 c1 = new_Const (mode_Is, new_tarval_from_long (3, mode_Is));
121 cond = new_Cond(new_Proj(new_Cmp(c1, get_value(1, mode_Is)), mode_b, pn_Cmp_Eq));
122 f = new_Proj(cond, mode_X, 0);
123 t = new_Proj(cond, mode_X, 1);
124 mature_immBlock(get_irg_current_block(irg));
127 Block2 = new_immBlock();
128 add_immBlock_pred(Block2, f);
129 mature_immBlock(Block2);
131 add_immBlock_pred(endBlock, jmp);
134 Block3 = new_immBlock();
135 add_immBlock_pred(Block3, t);
136 mature_immBlock(Block3);
138 add_immBlock_pred(endBlock, jmp);
140 /* finish the end Block */
141 set_cur_block(endBlock);
144 in[0] = get_value(0, mode_Is);
145 x = new_Return (get_store(), 1, in);
147 mature_immBlock (get_irg_current_block(irg));
149 /* finish the Block with the end node */
150 add_immBlock_pred (get_irg_end_block(irg), x);
151 mature_immBlock (get_irg_end_block(irg));
153 /* verify the graph */
155 irg_finalize_cons (irg);
157 printf("Optimizing ...\n");
158 dead_node_elimination(irg);
160 printf("Dumping the graph and a control flow graph.\n");
162 dump_ir_block_graph (irg, suffix);
163 dump_cfg (irg, suffix);
164 printf("Use xvcg to view these graphs:\n");
165 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");