1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Christian Schaefer, Goetz Lindenmaier
15 * a dead block / unreachable code.
19 *** This file constructs a control flow of following shape:
26 *** Block1 scnCondBlock
37 *** This is a program as, e.g.,
40 *** { Jmp label1; } // happens anyways
42 *** { Jmp label1; } // happens anyways
49 int main(int argc, char **argv)
52 ir_graph *irg; /* this variable contains the irgraph */
53 type *owner; /* the class in which this method is defined */
54 type *proc_main; /* type information for the method main */
55 entity *ent; /* represents this method as entity of owner */
56 ir_node *c1, *c2, *cond, *f, *t, *endBlock, *Block1, *jmp,
57 *scndCondBlock, *Block2, *Block3, *x;
64 /*** Make basic type information for primitive type int. ***/
65 prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_i);
67 /* FIRM was designed for oo languages where all methods belong to a class.
68 * For imperative languages like C we view a file as a large class containing
69 * all functions as methods in this file.
70 * This class now is automatically generated.
72 #define METHODNAME "main"
75 printf("\nCreating an IR graph: ...\n");
77 owner = get_glob_type();
78 proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
80 set_method_param_type(proc_main, 0, prim_t_int);
81 set_method_res_type(proc_main, 0, prim_t_int);
83 ent = new_entity (owner,
84 id_from_str (METHODNAME, strlen(METHODNAME)),
87 #define NUM_OF_LOCAL_VARS 2
89 irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
91 /* to make a condition */
92 c1 = new_Const (mode_i, tarval_from_long (mode_i, 1));
93 c2 = new_Proj (get_irg_args(irg), mode_i, 0);
96 cond = new_Cond(new_Proj(new_Cmp(c1, c2), mode_b, Eq));
97 set_value(0, new_Const (mode_i, tarval_from_long (mode_i, 6)));
98 f = new_Proj(cond, mode_X, 0);
99 t = new_Proj(cond, mode_X, 1);
100 mature_block(get_irg_current_block(irg));
102 /* end block to add jmps */
103 endBlock = new_immBlock();
106 Block1 = new_immBlock();
107 add_in_edge(Block1, t);
108 mature_block(Block1);
109 set_value(0, new_Const (mode_i, tarval_from_long (mode_i, 5)));
111 add_in_edge(endBlock, jmp);
114 scndCondBlock = new_immBlock();
115 add_in_edge(scndCondBlock, f);
116 mature_block(scndCondBlock);
117 c1 = new_Const (mode_i, tarval_from_long (mode_i, 3));
118 cond = new_Cond(new_Proj(new_Cmp(c1, get_value(1, mode_i)), mode_b, Eq));
119 f = new_Proj(cond, mode_X, 0);
120 t = new_Proj(cond, mode_X, 1);
121 mature_block(get_irg_current_block(irg));
124 Block2 = new_immBlock();
125 add_in_edge(Block2, f);
126 mature_block(Block2);
128 add_in_edge(endBlock, jmp);
131 Block3 = new_immBlock();
132 add_in_edge(Block3, t);
133 mature_block(Block3);
135 add_in_edge(endBlock, jmp);
137 /* finish the end Block */
138 switch_block(endBlock);
141 in[0] = get_value(0, mode_i);
142 x = new_Return (get_store(), 1, in);
144 mature_block (get_irg_current_block(irg));
146 /* finish the Block with the end node */
147 add_in_edge (get_irg_end_block(irg), x);
148 mature_block (get_irg_end_block(irg));
150 /* verify the graph */
154 printf("Optimizing ...\n");
155 dead_node_elimination(irg);
157 printf("Dumping the graph and a control flow graph.\n");
158 dump_ir_block_graph (irg);
160 printf("Use xvcg to view these graphs:\n");
161 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");