Do not emit cld in the CopyB prologue. The ABI mandates that DF is cleared, so do...
[libfirm] / testprograms / three_cfpred_example.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/three_cfpred_example.c
4  * Purpose:     Construct a block with more than two predecessors.
5  * Author:      Christian Schaefer, Goetz Lindenmaier
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1999-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #include <stdio.h>
14 #include <string.h>
15
16 #include <libfirm/firm.h>
17
18 /**
19  *  This file constructs a control flow of following shape:
20  *
21  *
22  *       firstCondBlock
23  *          /     \
24  *         /       \
25  *       |/_       _\|
26  *     Block1    scnCondBlock
27  *        |       |        |
28  *        |       |        |
29  *    |      \ /      \ /
30  *        |     Block2   Block3
31  *         \      |       /
32  *          \     |      /
33  *          _\|  \ /   |/_
34  *            nextBlock
35  *
36  *
37  *   This is a program as, e.g.,
38  *
39  *   if () then
40  *     { Jmp label1; } //  happens anyways
41  *   else
42  *     { Jmp label1; } //  happens anyways
43  * label1:
44  *   return();
45  *   Jmp label1;
46  *
47  **/
48
49 int main(void)
50 {
51   const char *suffix = "";
52   ir_type     *prim_t_int;
53   ir_graph *irg;          /* this variable contains the irgraph */
54   ir_type *owner;      /* the class in which this method is defined */
55   ir_type *proc_main; /* ir_type information for the method main */
56   ir_entity *ent;            /* represents this method as ir_entity of owner */
57   ir_node *c1, *c2, *cond, *f, *t, *endBlock, *Block1, *jmp,
58           *scndCondBlock, *Block2, *Block3, *x;
59
60   /* init library */
61   init_firm(NULL);
62
63   set_optimize(1);
64
65   /*** Make basic ir_type information for primitive ir_type int. ***/
66   prim_t_int = new_type_primitive(new_id_from_chars("int", 3), mode_Is);
67
68   /* FIRM was designed for oo languages where all methods belong to a class.
69    * For imperative languages like C we view a file as a large class containing
70    * all functions as methods in this file.
71    * This class now is automatically generated.
72    */
73 #define METHODNAME "THREE_CFPRED_EXAMPLE_main"
74 #define NRARGS 1
75 #define NRES 1
76   printf("\nCreating an IR graph: THREE_CFPRED_EXAMPLE ...\n");
77
78   owner = get_glob_type();
79   proc_main = new_type_method(new_id_from_chars(METHODNAME, strlen(METHODNAME)),
80                   NRARGS, NRES);
81   set_method_param_type(proc_main, 0, prim_t_int);
82   set_method_res_type(proc_main, 0, prim_t_int);
83
84   ent = new_entity(owner,
85                    new_id_from_chars(METHODNAME, strlen(METHODNAME)),
86                    proc_main);
87
88 #define NUM_OF_LOCAL_VARS 2
89
90   irg = new_ir_graph(ent, NUM_OF_LOCAL_VARS);
91
92   /* to make a condition  */
93   c1 = new_Const(mode_Is, new_tarval_from_long(1, mode_Is));
94   c2 = new_Proj(get_irg_args(irg), mode_Is, 0);
95   set_value(1, c2);
96
97   cond = new_Cond(new_Proj(new_Cmp(c1, c2), mode_b, pn_Cmp_Eq));
98   set_value(0, new_Const(mode_Is, new_tarval_from_long(6, mode_Is)));
99   f = new_Proj(cond, mode_X, pn_Cond_false);
100   t = new_Proj(cond, mode_X, pn_Cond_true);
101   mature_immBlock(get_irg_current_block(irg));
102
103   /* end block to add jmps */
104   endBlock = new_immBlock();
105
106   /* Block 1 */
107   Block1 = new_immBlock();
108   add_immBlock_pred(Block1, t);
109   mature_immBlock(Block1);
110   set_value(0, new_Const(mode_Is, new_tarval_from_long(5, mode_Is)));
111   jmp = new_Jmp();
112   add_immBlock_pred(endBlock, jmp);
113
114   /* scndCondBlock */
115   scndCondBlock = new_immBlock();
116   add_immBlock_pred(scndCondBlock, f);
117   mature_immBlock(scndCondBlock);
118   c1 = new_Const(mode_Is, new_tarval_from_long(3, mode_Is));
119   cond = new_Cond(new_Proj(new_Cmp(c1, get_value(1, mode_Is)), mode_b, pn_Cmp_Eq));
120   f = new_Proj(cond, mode_X, pn_Cond_false);
121   t = new_Proj(cond, mode_X, pn_Cond_true);
122   mature_immBlock(get_irg_current_block(irg));
123
124   /* Block 2 */
125   Block2 = new_immBlock();
126   add_immBlock_pred(Block2, f);
127   mature_immBlock(Block2);
128   jmp = new_Jmp();
129   add_immBlock_pred(endBlock, jmp);
130
131   /* Block 3 */
132   Block3 = new_immBlock();
133   add_immBlock_pred(Block3, t);
134   mature_immBlock(Block3);
135   jmp = new_Jmp();
136   add_immBlock_pred(endBlock, jmp);
137
138   /* finish the end Block */
139   set_cur_block(endBlock);
140   {
141     ir_node *in[1];
142     in[0] = get_value(0, mode_Is);
143     x = new_Return(get_store(), 1, in);
144   }
145   mature_immBlock(get_irg_current_block(irg));
146
147   /* finish the Block with the end node */
148   add_immBlock_pred(get_irg_end_block(irg), x);
149   mature_immBlock(get_irg_end_block(irg));
150
151   /* verify the graph */
152   irg_vrfy(irg);
153   irg_finalize_cons(irg);
154
155   printf("Optimizing ...\n");
156   dead_node_elimination(irg);
157
158   printf("Dumping the graph and a control flow graph.\n");
159   dump_ir_block_graph(irg, suffix);
160   dump_cfg(irg, suffix);
161   printf("Use ycomp to view these graphs:\n");
162   printf("ycomp THREE_CFPRED_EXAMPLE_main.vcg\n\n");
163   printf("ycomp THREE_CFPRED_EXAMPLE_main-cfg.vcg\n\n");
164
165   return(0);
166 }