3 * File name: testprograms/exception.c
4 * Purpose: Shows construction of exceptions.
5 * Tests Phi construction.
6 * Author: Goetz Lindenmaier
10 * Copyright: (c) 2004 Universität Karlsruhe
11 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
23 * This file constructs the ir for the following pseudo-program:
35 * } catch () { return c }
40 int main(int argc, char **argv)
43 ir_graph *irg; /* this variable contains the irgraph */
44 type *owner; /* the class in which this method is defined */
45 type *method; /* the type of this method */
46 entity *ent; /* represents this method as entity of owner */
47 ir_node *x, *catch_block, *block, *zero, *a, *b, *c, *d;
49 printf("\nCreating an IR graph: EXCEPTION...\n");
54 /*** Make basic type information for primitive type int. ***/
55 prim_t_int = new_type_primitive(new_id_from_str ("int"), mode_Is);
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
63 #define ENTITYNAME "EXCEPTION_main"
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);
69 ent = new_entity (owner, new_id_from_str (ENTITYNAME), method);
71 /* Generates the basic graph for the method represented by 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.
75 #define NUM_OF_LOCAL_VARS 4
77 irg = new_ir_graph (ent, NUM_OF_LOCAL_VARS);
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));
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 */
90 block = get_cur_block();
91 catch_block = new_immBlock();
95 d = new_Div(get_store(), get_value(0, mode_Is), zero);
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 */
103 c = new_Const (mode_Is, new_tarval_from_long (8, mode_Is));
104 set_value (2, c); /* this (2) is variable c */
107 d = new_Div(get_store(), get_value(1, mode_Is), zero);
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 */
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);
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);
127 /* Now we can mature the end block as all it's predecessors are known. */
128 mature_immBlock (get_irg_end_block(irg));
130 irg_finalize_cons (irg);
132 /* verify the graph */
135 printf("Done building the graph. Dumping it.\n");
136 dump_ir_block_graph (irg, "");
137 dump_ir_graph (irg, "");
138 printf("use xvcg to view this graph:\n");
139 printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");