removed pointless bespillremat.h includes
[libfirm] / testprograms / exception_example.c
1 /*
2  * Project:     libFIRM
3  * File name:   testprograms/exception.c
4  * Purpose:     Shows construction of exceptions.
5  *              Tests Phi construction.
6  * Author:      Goetz Lindenmaier
7  * Modified by:
8  * Created:
9  * CVS-ID:      $Id$
10  * Copyright:   (c) 2004 Universität Karlsruhe
11  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
12  */
13
14 #include <stdio.h>
15 #include <string.h>
16
17
18 #include <libfirm/firm.h>
19
20
21
22 /**
23 *  This file constructs the ir for the following pseudo-program:
24 *
25 *  main() {
26 *    int a = 5;  // val 0
27 *    int b = 6;  // val 1
28 *    int c = 7;  // val 2
29 *    int d;      // val 3
30 *
31 *    try {
32 *      d = a/0;
33 *      c = 8;
34 *      d = b/0
35 *    } catch () { return c }
36 *
37 *    return d;
38 **/
39
40 int main(void)
41 {
42   ir_type     *prim_t_int;
43   ir_graph *irg;       /* this variable contains the irgraph */
44   ir_type     *owner;     /* the class in which this method is defined */
45   ir_type     *method;    /* the ir_type of this method */
46   ir_entity   *ent;       /* represents this method as ir_entity of owner */
47   ir_node  *x, *catch_block, *block, *zero, *a, *b, *c, *d;
48
49   printf("\nCreating an IR graph: EXCEPTION...\n");
50
51   /* init library */
52   init_firm (NULL);
53
54   /*** Make basic ir_type information for primitive ir_type int. ***/
55   prim_t_int = new_type_primitive(new_id_from_str ("int"), mode_Is);
56
57   /* FIRM was designed for oo languages where all methods belong to a class.
58    * For imperative languages like C we view a file as a large class containing
59    * all functions as methods in this file.
60    * Therefore we define a class "IF_ELSE_EXAMPLE" with a method main as an
61    * ir_entity.
62    */
63 #define ENTITYNAME "EXCEPTION_main"
64
65   owner = get_glob_type();
66   method = new_type_method (new_id_from_str(ENTITYNAME), 0, 1);
67   set_method_res_type(method, 0, prim_t_int);
68
69   ent = new_entity (owner, new_id_from_str (ENTITYNAME), method);
70
71   /* Generates the basic graph for the method represented by ir_entity ent, that
72    * is, generates start and end blocks and nodes and a first, initial block.
73    * The constructor needs to know how many local variables the method has.
74    */
75 #define NUM_OF_LOCAL_VARS 4
76
77   irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
78
79   /* Initialize a, b, c. */
80   zero = new_Const (mode_Is, new_tarval_from_long (0, mode_Is));
81   a = new_Const (mode_Is, new_tarval_from_long (5, mode_Is));
82   b = new_Const (mode_Is, new_tarval_from_long (6, mode_Is));
83   c = new_Const (mode_Is, new_tarval_from_long (7, mode_Is));
84
85   /* set a and b to constants */
86   set_value (0, a);  /* this (0) is variable a */
87   set_value (1, b);  /* this (1) is variable b */
88   set_value (2, c);  /* this (2) is variable c */
89
90   block = get_cur_block();
91   catch_block = new_immBlock();
92   set_cur_block(block);
93
94   /* d = a / 0 */
95   d = new_Div(get_store(), get_value(0, mode_Is), zero, mode_Is, op_pin_state_pinned);
96   set_store(new_Proj(d, mode_M, pn_Div_M));
97   x = new_Proj(d, mode_X, pn_Div_X_except);
98   add_immBlock_pred(catch_block, x);
99   d = new_Proj(d, mode_Is, pn_Div_res);
100   set_value(3, d);  /* this (3) is variable d */
101
102   /* c = 8 */
103   c = new_Const (mode_Is, new_tarval_from_long (8, mode_Is));
104   set_value (2, c);  /* this (2) is variable c */
105
106   /* d = b / 0 */
107   d = new_Div(get_store(), get_value(1, mode_Is), zero, mode_Is, op_pin_state_pinned);
108   set_store(new_Proj(d, mode_M, pn_Div_M));
109   x = new_Proj(d, mode_X, pn_Div_X_except);
110   add_immBlock_pred(catch_block, x);
111   d = new_Proj(d, mode_Is, pn_Div_res);
112   set_value(3, d);  /* this (3) is variable d */
113
114   /* return d */
115   d = get_value(3, mode_Is);
116   x = new_Return (get_store(), 1, &d);
117   mature_immBlock(get_cur_block());
118   add_immBlock_pred(get_irg_end_block(current_ir_graph), x);
119
120   /* return c */
121   set_cur_block(catch_block);
122   c = get_value(2, mode_Is);
123   x = new_Return (get_store(), 1, &c);
124   mature_immBlock(get_cur_block());
125   add_immBlock_pred(get_irg_end_block(current_ir_graph), x);
126
127   /* Now we can mature the end block as all it's predecessors are known. */
128   mature_immBlock (get_irg_end_block(irg));
129
130   irg_finalize_cons (irg);
131
132   /* verify the graph */
133   irg_vrfy(irg);
134
135   printf("Done building the graph.  Dumping it.\n");
136   dump_ir_block_graph (irg, "");
137   dump_ir_graph (irg, "");
138   printf("Use ycomp to view this graph:\n");
139   printf("ycomp GRAPHNAME\n\n");
140
141   return (0);
142 }