added doxygen comments
[libfirm] / testprograms / dead_block_example.c
1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 * All rights reserved.
3 *
4 * Authors: Christian Schaefer, Goetz Lindenmaier
5 *
6 * testprogram.
7 */
8
9 # include <stdio.h>
10 # include <string.h>
11
12 # include "irvrfy.h"
13 # include "irdump.h"
14 # include "firm.h"
15
16 /*
17  *   a dead block / unreachable code.
18  */
19
20 /**
21 *  This file constructs a control flow of following shape:
22 *
23 *
24 *         firstBlock
25 *          /   \
26 *         /     \
27 *       |/_     _\|
28 *     Block1    Block2   deadBlock
29 *        \       |       /
30 *           \      |      /
31 *           _\|   \ /   |/_
32 *            nextBlock
33 *
34 *
35 *   This is a program as, e.g.,
36 *
37 *   if () then
38 *     { Jmp label1; } // happens anyways
39 *   else
40 *     { Jmp label1; } // happens anyways
41 * label1:
42 *   return();
43 *   Jmp label1;
44 *
45 **/
46
47 int main(int argc, char **argv)
48 {
49   ir_graph *irg;          /* this variable contains the irgraph */
50   type *owner;      /* the class in which this method is defined */
51   type *proc_main; /* type information for the method main */
52   type     *prim_t_int;
53   entity *ent;            /* represents this method as entity of owner */
54   ir_node *c1, *c2, *cond, *f, *t, *endBlock, *Block1, *jmp, *Block2,
55           *deadBlock, *x;
56
57   /* init library */
58   init_firm ();
59
60   /*** Make basic type information for primitive type int. ***/
61   prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_Is);
62
63   /* FIRM was designed for oo languages where all methods belong to a class.
64    * For imperative languages like C we view a file as a large class containing
65    * all functions as methods in this file.
66    * Therefore we define a class "empty" according to the file name
67    * with a method main as an entity.
68    */
69 #define CLASSNAME "DEAD_BLOCK"
70 #define METHODNAME "main"
71 #define NRARGS 0
72 #define NRES 1
73   printf("\nCreating an IR graph: %s...\n", CLASSNAME);
74
75   owner = new_type_class (id_from_str (CLASSNAME, strlen(CLASSNAME)));
76   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
77                               NRARGS, NRES);
78   set_method_res_type(proc_main, 0, prim_t_int);
79   ent = new_entity (owner,
80                     id_from_str (METHODNAME, strlen(METHODNAME)),
81                     proc_main);
82
83 #define NUM_OF_LOCAL_VARS 1
84
85   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
86
87   /* to make a condition  */
88   c1 = new_Const (mode_Is, tarval_from_long (mode_Is, 1));
89   c2 = new_Const (mode_Is, tarval_from_long (mode_Is, 2));
90   set_value(0, c2);
91
92   cond = new_Cond(new_Proj(new_Cmp(c1, c2), mode_b, Eq));
93   f = new_Proj(cond, mode_X, 0);
94   t = new_Proj(cond, mode_X, 1);
95   mature_block(get_irg_current_block(irg));
96
97   /* end block to add jmps */
98   endBlock = new_immBlock();
99
100   /* Block 1 */
101   Block1 = new_immBlock();
102   add_in_edge(Block1, t);
103   mature_block(Block1);
104   jmp = new_Jmp();
105   add_in_edge(endBlock, jmp);
106
107   /* Block 2 */
108   Block2 = new_immBlock();
109   add_in_edge(Block2, f);
110   mature_block(Block2);
111   jmp = new_Jmp();
112   add_in_edge(endBlock, jmp);
113
114   /* dead Block */
115   deadBlock = new_immBlock();
116   mature_block(deadBlock);
117   jmp = new_Jmp();
118   add_in_edge(endBlock, jmp);
119
120   /* finish end block */
121   switch_block(endBlock);
122   {
123     ir_node *in[1];
124     in[0] = get_value(0, mode_Is);
125     get_store();
126     x = new_Return (get_store(), 1, in);
127   }
128   mature_block (get_irg_current_block(irg));
129
130   add_in_edge (get_irg_end_block(irg), x);
131   mature_block (get_irg_end_block(irg));
132
133   finalize_cons (irg);
134
135   printf("Optimizing ...\n");
136   local_optimize_graph (irg);
137   dead_node_elimination (irg);
138
139   /* verify the graph */
140   irg_vrfy(irg);
141
142   printf("Dumping the graph and a control flow graph.\n");
143   dump_ir_block_graph (irg);
144   dump_cfg (irg);
145   printf("Use xvcg to view these graphs:\n");
146   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
147
148   return (0);
149 }