Add dump_file_suffix to All_types.vcg filename
[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   set_opt_cse(0);  /* there is a bug: first and start block are cse!! @@@ */
61
62   /*** Make basic type information for primitive type int. ***/
63   prim_t_int = new_type_primitive(id_from_str ("int", 3), mode_i);
64
65   /* FIRM was designed for oo languages where all methods belong to a class.
66    * For imperative languages like C we view a file as a large class containing
67    * all functions as methods in this file.
68    * Therefore we define a class "empty" according to the file name
69    * with a method main as an entity.
70    */
71 #define CLASSNAME "DEAD_BLOCK"
72 #define METHODNAME "main"
73 #define NRARGS 0
74 #define NRES 1
75   printf("\nCreating an IR graph: %s...\n", CLASSNAME);
76
77   owner = new_type_class (id_from_str (CLASSNAME, strlen(CLASSNAME)));
78   proc_main = new_type_method(id_from_str(METHODNAME, strlen(METHODNAME)),
79                               NRARGS, NRES);
80   set_method_res_type(proc_main, 0, prim_t_int);
81   ent = new_entity (owner,
82                     id_from_str (METHODNAME, strlen(METHODNAME)),
83                     proc_main);
84
85 #define NUM_OF_LOCAL_VARS 1
86
87   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
88
89   /* to make a condition  */
90   c1 = new_Const (mode_i, tarval_from_long (mode_i, 1));
91   c2 = new_Const (mode_i, tarval_from_long (mode_i, 2));
92   set_value(0, c2);
93
94   cond = new_Cond(new_Proj(new_Cmp(c1, c2), mode_b, Eq));
95   f = new_Proj(cond, mode_X, 0);
96   t = new_Proj(cond, mode_X, 1);
97   mature_block(get_irg_current_block(irg));
98
99   /* end block to add jmps */
100   endBlock = new_immBlock();
101
102   /* Block 1 */
103   Block1 = new_immBlock();
104   add_in_edge(Block1, t);
105   mature_block(Block1);
106   jmp = new_Jmp();
107   add_in_edge(endBlock, jmp);
108
109   /* Block 2 */
110   Block2 = new_immBlock();
111   add_in_edge(Block2, f);
112   mature_block(Block2);
113   jmp = new_Jmp();
114   add_in_edge(endBlock, jmp);
115
116   /* dead Block */
117   deadBlock = new_immBlock();
118   mature_block(deadBlock);
119   jmp = new_Jmp();
120   add_in_edge(endBlock, jmp);
121
122   /* finish end block */
123   switch_block(endBlock);
124   {
125     ir_node *in[1];
126     in[0] = get_value(0, mode_i);
127     get_store();
128     x = new_Return (get_store(), 1, in);
129   }
130   mature_block (get_irg_current_block(irg));
131
132   add_in_edge (get_irg_end_block(irg), x);
133   mature_block (get_irg_end_block(irg));
134
135   finalize_cons (irg);
136
137   printf("Optimizing ...\n");
138   local_optimize_graph (irg);
139   dead_node_elimination (irg);
140
141   /* verify the graph */
142   irg_vrfy(irg);
143
144   printf("Dumping the graph and a control flow graph.\n");
145   dump_ir_block_graph (irg);
146   dump_cfg (irg);
147   printf("Use xvcg to view these graphs:\n");
148   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
149
150   return (0);
151 }