3 * File name: ir/st/exc.c
4 * Purpose: Helper functions for jack exceptions.
5 * Author: Florian Liekweg
9 * Copyright: (c) 2002-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
17 Helper functions for exceptions
24 static char* exc_strings [] = {
25 "Invalid", /* invalid */
26 "Normal", /* normal */
27 "Entry", /* entry to region */
28 "Exit", /* exit of region */
29 "Handler", /* entry to handler */
35 Return true iff (block b is a handler entry AND a dominates b) OR
36 (b has a CFG successor that is both a handler entry AND is dominated
39 static bool has_handler (ir_graph *graph, ir_node *b, ir_node *a)
42 int n = get_irn_n_outs (b);
45 assert (0 && "Wrongly implemented");
46 /* must check for _immediate_ dominance !!! */
48 if (is_handler_entry (graph, b) && dominates (graph, a, b))
51 for (i = 0; i < n; i ++)
53 succ = get_irn_out (b, i);
55 if (has_handler (graph, succ, a))
63 Return true iff the given node represents an exception jump
65 static bool is_exc_jmp (ir_node *node)
67 ir_op *op = get_irn_op (node);
69 /* Proj_X (Load), Proj_X (Sto), Proj_X (Div_Int),
70 Proj_X (Raise), Proj_X (Call), Proj_X (Alloc) */
73 op = get_irn_op (get_Proj_pred (node));
75 assert ((is_fragile_op(get_Proj_pred(node))) &&
76 (op != op_Bad) /*&& (op != op_Unknown)*/ &&
77 (get_irn_mode(node) == mode_X));/* Check for proper Proj attr */
87 Return true iff the given node represents a normal cfg jump
90 static bool is_cfg_jmp (ir_node *node)
92 ir_op *op = get_irn_op (node);
96 op = get_irn_op (get_Proj_pred (node));
98 /* Proj_X (Proj_Cmp (Cond)) */
100 return (true); /* check for op == op_Cmp and op == op_Cond */
108 Return true iff a new exception region must be left upon entry of this block.
110 If all CFG preds of this block are exception jumps, then we must
113 bool is_handler_entry (ir_graph *graph, ir_node *block)
115 bool is_entry = true;
117 int n = get_irn_arity (block);
119 if (exc_invalid == get_Block_exc (block))
121 for (i = 0; (i < n) && (is_entry == true); i ++)
122 if (is_exc_jmp (get_irn_n (block, i)))
127 if (true == is_entry)
128 set_Block_exc (block, exc_handler);
131 return (exc_handler == get_Block_exc (block));
135 Return true iff a new exception region must be started upon entry of this block.
137 If this block immediately dominates a handler entry, we must return true.
139 bool is_region_entry (ir_graph *graph, ir_node *block)
141 assert (0 && "Not implemented");
143 if (exc_invalid == get_Block_exc (block))
146 int n = get_irn_n_outs (block);
149 bool no_handler = true;
151 for (i = 0; (i < n) && (no_handler == true); i ++)
153 succ = get_irn_out (block, i);
155 if (has_handler (graph, succ, block))
159 if (false == no_handler)
160 set_Block_exc (block, exc_region);
163 return (exc_region == get_Block_exc (block));
169 Return true iff this block is part of handler code.
171 If this block is dominated by a block for which {@link
172 is_handler_entry} is true, then this block is part of the handler.
174 bool is_handler_block (ir_graph *graph, ir_node *block)
176 assert (0 && "Not implemented");
178 if (exc_invalid == get_Block_exc (block))
180 bool no_handler = true;
181 dom_env_t *env = get_dom_env (graph, block);
182 int block_index = env->index_a;
183 bs_t block_mask = 0x00000001 << block_index;
184 int n_blocks = env->dt->n_blocks;
187 for (i = 0; (i < n_blocks) && (no_handler == true); i ++)
188 if (0 != (env->dt->masks [i] & block_mask)) /* if dominator */
189 if (is_handler_entry (graph, env->dt->blocks [i])) /* is handler entry */
192 delete_dom_env (env);
194 if (false == no_handler)
195 set_Block_exc (block, exc_handler);
198 return (exc_handler == get_Block_exc (block));
202 Convert a value of type exc_t to a descriptive string.
203 Returns a reference to a statically allocated, constant string.
206 const char *exc_to_string (exc_t exc)
208 int exc_val = (int) exc;
210 assert ((0 <= (int) exc_val) && (exc_val < (int) exc_max));
212 return (exc_strings [exc_val]);