1 /* Exception Handling interface routines.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by Mike Stump <mrs@cygnus.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
29 /* The type of an exception region. */
32 /* CLEANUP regions implement e.g. destructors run when exiting a block.
33 They can be generated from both GIMPLE_TRY_FINALLY and GIMPLE_TRY_CATCH
34 nodes. It is expected by the runtime that cleanup regions will *not*
35 resume normal program flow, but will continue propagation of the
39 /* TRY regions implement catching an exception. The list of types associated
40 with the attached catch handlers is examined in order by the runtime and
41 control is transfered to the appropriate handler. Note that a NULL type
42 list is a catch-all handler, and that it will catch *all* exceptions
43 including those originating from a different language. */
46 /* ALLOWED_EXCEPTIONS regions implement exception filtering, e.g. the
47 throw(type-list) specification that can be added to C++ functions.
48 The runtime examines the thrown exception vs the type list, and if
49 the exception does not match, transfers control to the handler. The
50 normal handler for C++ calls __cxa_call_unexpected. */
51 ERT_ALLOWED_EXCEPTIONS
,
53 /* MUST_NOT_THROW regions prevent all exceptions from propagating. This
54 region type is used in C++ to surround destructors being run inside a
55 CLEANUP region. This differs from an ALLOWED_EXCEPTIONS region with
56 an empty type list in that the runtime is prepared to terminate the
57 program directly. We only generate code for MUST_NOT_THROW regions
58 along control paths that are already handling an exception within the
64 /* A landing pad for a given exception region. Any transfer of control
65 from the EH runtime to the function happens at a landing pad. */
67 struct GTY(()) eh_landing_pad_d
69 /* The linked list of all landing pads associated with the region. */
70 struct eh_landing_pad_d
*next_lp
;
72 /* The region with which this landing pad is associated. */
73 struct eh_region_d
*region
;
75 /* At the gimple level, the location to which control will be transfered
76 for this landing pad. There can be both EH and normal edges into the
77 block containing the post-landing-pad label. */
78 tree post_landing_pad
;
80 /* At the rtl level, the location to which the runtime will transfer
81 control. This differs from the post-landing-pad in that the target's
82 EXCEPTION_RECEIVER pattern will be expanded here, as well as other
83 bookkeeping specific to exceptions. There must not be normal edges
84 into the block containing the landing-pad label. */
87 /* The index of this landing pad within fun->eh->lp_array. */
91 /* A catch handler associated with an ERT_TRY region. */
93 struct GTY(()) eh_catch_d
95 /* The double-linked list of all catch handlers for the region. */
96 struct eh_catch_d
*next_catch
;
97 struct eh_catch_d
*prev_catch
;
99 /* A TREE_LIST of runtime type objects that this catch handler
100 will catch, or NULL if all exceptions are caught. */
103 /* A TREE_LIST of INTEGER_CSTs that correspond to the type_list entries,
104 having been mapped by assign_filter_values. These integers are to be
105 compared against the __builtin_eh_filter value. */
108 /* The code that should be executed if this catch handler matches the
109 thrown exception. This label is only maintained until
110 pass_lower_eh_dispatch, at which point it is cleared. */
114 /* Describes one exception region. */
116 struct GTY(()) eh_region_d
118 /* The immediately surrounding region. */
119 struct eh_region_d
*outer
;
121 /* The list of immediately contained regions. */
122 struct eh_region_d
*inner
;
123 struct eh_region_d
*next_peer
;
125 /* The index of this region within fun->eh->region_array. */
128 /* Each region does exactly one thing. */
129 enum eh_region_type type
;
131 /* Holds the action to perform based on the preceding type. */
133 struct eh_region_u_try
{
134 /* The double-linked list of all catch handlers for this region. */
135 struct eh_catch_d
*first_catch
;
136 struct eh_catch_d
*last_catch
;
137 } GTY ((tag ("ERT_TRY"))) eh_try
;
139 struct eh_region_u_allowed
{
140 /* A TREE_LIST of runtime type objects allowed to pass. */
142 /* The code that should be executed if the thrown exception does
143 not match the type list. This label is only maintained until
144 pass_lower_eh_dispatch, at which point it is cleared. */
146 /* The integer that will be passed by the runtime to signal that
147 we should execute the code at LABEL. This integer is assigned
148 by assign_filter_values and is to be compared against the
149 __builtin_eh_filter value. */
151 } GTY ((tag ("ERT_ALLOWED_EXCEPTIONS"))) allowed
;
153 struct eh_region_u_must_not_throw
{
154 /* A function decl to be invoked if this region is actually reachable
155 from within the function, rather than implementable from the runtime.
156 The normal way for this to happen is for there to be a CLEANUP region
157 contained within this MUST_NOT_THROW region. Note that if the
158 runtime handles the MUST_NOT_THROW region, we have no control over
159 what termination function is called; it will be decided by the
160 personality function in effect for this CIE. */
162 /* The location assigned to the call of FAILURE_DECL, if expanded. */
163 location_t failure_loc
;
164 } GTY ((tag ("ERT_MUST_NOT_THROW"))) must_not_throw
;
165 } GTY ((desc ("%0.type"))) u
;
167 /* The list of landing pads associated with this region. */
168 struct eh_landing_pad_d
*landing_pads
;
170 /* EXC_PTR and FILTER values copied from the runtime for this region.
171 Each region gets its own psuedos so that if there are nested exceptions
172 we do not overwrite the values of the first exception. */
173 rtx exc_ptr_reg
, filter_reg
;
175 /* True if this region should use __cxa_end_cleanup instead
176 of _Unwind_Resume. */
177 bool use_cxa_end_cleanup
;
180 typedef struct eh_landing_pad_d
*eh_landing_pad
;
181 typedef struct eh_catch_d
*eh_catch
;
182 typedef struct eh_region_d
*eh_region
;
184 DEF_VEC_P(eh_region
);
185 DEF_VEC_ALLOC_P(eh_region
, gc
);
186 DEF_VEC_ALLOC_P(eh_region
, heap
);
188 DEF_VEC_P(eh_landing_pad
);
189 DEF_VEC_ALLOC_P(eh_landing_pad
, gc
);
192 /* The exception status for each function. */
194 struct GTY(()) eh_status
196 /* The tree of all regions for this function. */
197 eh_region region_tree
;
199 /* The same information as an indexable array. */
200 VEC(eh_region
,gc
) *region_array
;
202 /* The landing pads as an indexable array. */
203 VEC(eh_landing_pad
,gc
) *lp_array
;
205 /* At the gimple level, a mapping from gimple statement to landing pad
206 or must-not-throw region. See record_stmt_eh_region. */
207 htab_t
GTY((param_is (struct throw_stmt_node
))) throw_stmt_table
;
209 /* All of the runtime type data used by the function. These objects
210 are emitted to the lang-specific-data-area for the function. */
211 VEC(tree
,gc
) *ttype_data
;
213 /* The table of all action chains. These encode the eh_region tree in
214 a compact form for use by the runtime, and is also emitted to the
215 lang-specific-data-area. Note that the ARM EABI uses a different
216 format for the encoding than all other ports. */
218 VEC(tree
,gc
) * GTY((tag ("1"))) arm_eabi
;
219 VEC(uchar
,gc
) * GTY((tag ("0"))) other
;
220 } GTY ((desc ("targetm.arm_eabi_unwinder"))) ehspec_data
;
224 /* Test: is exception handling turned on? */
225 extern int doing_eh (int);
227 /* Invokes CALLBACK for every exception handler label. Only used by old
228 loop hackery; should not be used by new code. */
229 extern void for_each_eh_label (void (*) (rtx
));
231 extern void init_eh (void);
232 extern void init_eh_for_function (void);
234 extern void remove_eh_landing_pad (eh_landing_pad
);
235 extern void remove_eh_handler (eh_region
);
237 extern bool current_function_has_exception_handlers (void);
238 extern void output_function_exception_table (const char *);
240 extern rtx
expand_builtin_eh_pointer (tree
);
241 extern rtx
expand_builtin_eh_filter (tree
);
242 extern rtx
expand_builtin_eh_copy_values (tree
);
243 extern void expand_builtin_unwind_init (void);
244 extern rtx
expand_builtin_eh_return_data_regno (tree
);
245 extern rtx
expand_builtin_extract_return_addr (tree
);
246 extern void expand_builtin_init_dwarf_reg_sizes (tree
);
247 extern rtx
expand_builtin_frob_return_addr (tree
);
248 extern rtx
expand_builtin_dwarf_sp_column (void);
249 extern void expand_builtin_eh_return (tree
, tree
);
250 extern void expand_eh_return (void);
251 extern rtx
expand_builtin_extend_pointer (tree
);
253 typedef tree (*duplicate_eh_regions_map
) (tree
, void *);
254 extern struct pointer_map_t
*duplicate_eh_regions
255 (struct function
*, eh_region
, int, duplicate_eh_regions_map
, void *);
257 extern void sjlj_emit_function_exit_after (rtx
);
259 extern eh_region
gen_eh_region_cleanup (eh_region
);
260 extern eh_region
gen_eh_region_try (eh_region
);
261 extern eh_region
gen_eh_region_allowed (eh_region
, tree
);
262 extern eh_region
gen_eh_region_must_not_throw (eh_region
);
264 extern eh_catch
gen_eh_region_catch (eh_region
, tree
);
265 extern eh_landing_pad
gen_eh_landing_pad (eh_region
);
267 extern eh_region
get_eh_region_from_number_fn (struct function
*, int);
268 extern eh_region
get_eh_region_from_number (int);
269 extern eh_landing_pad
get_eh_landing_pad_from_number_fn (struct function
*,int);
270 extern eh_landing_pad
get_eh_landing_pad_from_number (int);
271 extern eh_region
get_eh_region_from_lp_number_fn (struct function
*, int);
272 extern eh_region
get_eh_region_from_lp_number (int);
274 extern eh_region
eh_region_outermost (struct function
*, eh_region
, eh_region
);
276 extern void make_reg_eh_region_note (rtx insn
, int ecf_flags
, int lp_nr
);
277 extern void make_reg_eh_region_note_nothrow_nononlocal (rtx
);
279 extern void verify_eh_tree (struct function
*);
280 extern void dump_eh_tree (FILE *, struct function
*);
281 void debug_eh_tree (struct function
*);
282 extern void add_type_for_runtime (tree
);
283 extern tree
lookup_type_for_runtime (tree
);
284 extern void assign_filter_values (void);
286 extern eh_region
get_eh_region_from_rtx (const_rtx
);
287 extern eh_landing_pad
get_eh_landing_pad_from_rtx (const_rtx
);
289 /* If non-NULL, this is a function that returns a function decl to be
290 executed if an unhandled exception is propagated out of a cleanup
291 region. For example, in C++, an exception thrown by a destructor
292 during stack unwinding is required to result in a call to
293 `std::terminate', so the C++ version of this function returns a
294 FUNCTION_DECL for `std::terminate'. */
295 extern tree (*lang_protect_cleanup_actions
) (void);
298 /* Just because the user configured --with-sjlj-exceptions=no doesn't
299 mean that we can use call frame exceptions. Detect that the target
300 has appropriate support. */
302 #ifndef MUST_USE_SJLJ_EXCEPTIONS
303 # if defined (EH_RETURN_DATA_REGNO) \
304 && (defined (TARGET_UNWIND_INFO) \
305 || (DWARF2_UNWIND_INFO \
306 && (defined (EH_RETURN_HANDLER_RTX) \
307 || defined (HAVE_eh_return))))
308 # define MUST_USE_SJLJ_EXCEPTIONS 0
310 # define MUST_USE_SJLJ_EXCEPTIONS 1
314 #ifdef CONFIG_SJLJ_EXCEPTIONS
315 # if CONFIG_SJLJ_EXCEPTIONS == 1
316 # define USING_SJLJ_EXCEPTIONS 1
318 # if CONFIG_SJLJ_EXCEPTIONS == 0
319 # define USING_SJLJ_EXCEPTIONS 0
320 # if !defined(EH_RETURN_DATA_REGNO)
321 #error "EH_RETURN_DATA_REGNO required"
323 # if ! (defined(TARGET_UNWIND_INFO) || DWARF2_UNWIND_INFO)
324 #error "{DWARF2,TARGET}_UNWIND_INFO required"
326 # if !defined(TARGET_UNWIND_INFO) \
327 && !(defined(EH_RETURN_HANDLER_RTX) || defined(HAVE_eh_return))
328 #error "EH_RETURN_HANDLER_RTX or eh_return required"
330 /* Usually the above error checks will have already triggered an
331 error, but backends may set MUST_USE_SJLJ_EXCEPTIONS for their own
333 # if MUST_USE_SJLJ_EXCEPTIONS
334 #error "Must use SJLJ exceptions but configured not to"
338 # define USING_SJLJ_EXCEPTIONS MUST_USE_SJLJ_EXCEPTIONS
341 struct GTY(()) throw_stmt_node
{
346 extern struct htab
*get_eh_throw_stmt_table (struct function
*);
347 extern void set_eh_throw_stmt_table (struct function
*, struct htab
*);
349 enum eh_personality_kind
{
355 extern enum eh_personality_kind
356 function_needs_eh_personality (struct function
*);
358 /* Pre-order iteration within the eh_region tree. */
360 static inline eh_region
361 ehr_next (eh_region r
, eh_region start
)
365 else if (r
->next_peer
&& r
!= start
)
375 while (r
->next_peer
== NULL
);
381 #define FOR_ALL_EH_REGION_AT(R, START) \
382 for ((R) = (START); (R) != NULL; (R) = ehr_next (R, START))
384 #define FOR_ALL_EH_REGION_FN(R, FN) \
385 for ((R) = (FN)->eh->region_tree; (R) != NULL; (R) = ehr_next (R, NULL))
387 #define FOR_ALL_EH_REGION(R) FOR_ALL_EH_REGION_FN (R, cfun)