1*e4b17023SJohn Marino /* Exception Handling interface routines.
2*e4b17023SJohn Marino Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3*e4b17023SJohn Marino 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4*e4b17023SJohn Marino Contributed by Mike Stump <mrs@cygnus.com>.
5*e4b17023SJohn Marino
6*e4b17023SJohn Marino This file is part of GCC.
7*e4b17023SJohn Marino
8*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
9*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
10*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
11*e4b17023SJohn Marino version.
12*e4b17023SJohn Marino
13*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
15*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16*e4b17023SJohn Marino for more details.
17*e4b17023SJohn Marino
18*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
19*e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see
20*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
21*e4b17023SJohn Marino
22*e4b17023SJohn Marino /* No include guards here, but define an include file marker anyway, so
23*e4b17023SJohn Marino that the compiler can keep track of where this file is included. This
24*e4b17023SJohn Marino is e.g. used to avoid including this file in front-end specific files. */
25*e4b17023SJohn Marino #ifndef GCC_EXCEPT_H
26*e4b17023SJohn Marino # define GCC_EXCEPT_H
27*e4b17023SJohn Marino #endif
28*e4b17023SJohn Marino
29*e4b17023SJohn Marino #include "hashtab.h"
30*e4b17023SJohn Marino #include "vecprim.h"
31*e4b17023SJohn Marino #include "vecir.h"
32*e4b17023SJohn Marino
33*e4b17023SJohn Marino struct function;
34*e4b17023SJohn Marino struct eh_region_d;
35*e4b17023SJohn Marino struct pointer_map_t;
36*e4b17023SJohn Marino
37*e4b17023SJohn Marino /* The type of an exception region. */
38*e4b17023SJohn Marino enum eh_region_type
39*e4b17023SJohn Marino {
40*e4b17023SJohn Marino /* CLEANUP regions implement e.g. destructors run when exiting a block.
41*e4b17023SJohn Marino They can be generated from both GIMPLE_TRY_FINALLY and GIMPLE_TRY_CATCH
42*e4b17023SJohn Marino nodes. It is expected by the runtime that cleanup regions will *not*
43*e4b17023SJohn Marino resume normal program flow, but will continue propagation of the
44*e4b17023SJohn Marino exception. */
45*e4b17023SJohn Marino ERT_CLEANUP,
46*e4b17023SJohn Marino
47*e4b17023SJohn Marino /* TRY regions implement catching an exception. The list of types associated
48*e4b17023SJohn Marino with the attached catch handlers is examined in order by the runtime and
49*e4b17023SJohn Marino control is transfered to the appropriate handler. Note that a NULL type
50*e4b17023SJohn Marino list is a catch-all handler, and that it will catch *all* exceptions
51*e4b17023SJohn Marino including those originating from a different language. */
52*e4b17023SJohn Marino ERT_TRY,
53*e4b17023SJohn Marino
54*e4b17023SJohn Marino /* ALLOWED_EXCEPTIONS regions implement exception filtering, e.g. the
55*e4b17023SJohn Marino throw(type-list) specification that can be added to C++ functions.
56*e4b17023SJohn Marino The runtime examines the thrown exception vs the type list, and if
57*e4b17023SJohn Marino the exception does not match, transfers control to the handler. The
58*e4b17023SJohn Marino normal handler for C++ calls __cxa_call_unexpected. */
59*e4b17023SJohn Marino ERT_ALLOWED_EXCEPTIONS,
60*e4b17023SJohn Marino
61*e4b17023SJohn Marino /* MUST_NOT_THROW regions prevent all exceptions from propagating. This
62*e4b17023SJohn Marino region type is used in C++ to surround destructors being run inside a
63*e4b17023SJohn Marino CLEANUP region. This differs from an ALLOWED_EXCEPTIONS region with
64*e4b17023SJohn Marino an empty type list in that the runtime is prepared to terminate the
65*e4b17023SJohn Marino program directly. We only generate code for MUST_NOT_THROW regions
66*e4b17023SJohn Marino along control paths that are already handling an exception within the
67*e4b17023SJohn Marino current function. */
68*e4b17023SJohn Marino ERT_MUST_NOT_THROW
69*e4b17023SJohn Marino };
70*e4b17023SJohn Marino
71*e4b17023SJohn Marino
72*e4b17023SJohn Marino /* A landing pad for a given exception region. Any transfer of control
73*e4b17023SJohn Marino from the EH runtime to the function happens at a landing pad. */
74*e4b17023SJohn Marino
75*e4b17023SJohn Marino struct GTY(()) eh_landing_pad_d
76*e4b17023SJohn Marino {
77*e4b17023SJohn Marino /* The linked list of all landing pads associated with the region. */
78*e4b17023SJohn Marino struct eh_landing_pad_d *next_lp;
79*e4b17023SJohn Marino
80*e4b17023SJohn Marino /* The region with which this landing pad is associated. */
81*e4b17023SJohn Marino struct eh_region_d *region;
82*e4b17023SJohn Marino
83*e4b17023SJohn Marino /* At the gimple level, the location to which control will be transfered
84*e4b17023SJohn Marino for this landing pad. There can be both EH and normal edges into the
85*e4b17023SJohn Marino block containing the post-landing-pad label. */
86*e4b17023SJohn Marino tree post_landing_pad;
87*e4b17023SJohn Marino
88*e4b17023SJohn Marino /* At the rtl level, the location to which the runtime will transfer
89*e4b17023SJohn Marino control. This differs from the post-landing-pad in that the target's
90*e4b17023SJohn Marino EXCEPTION_RECEIVER pattern will be expanded here, as well as other
91*e4b17023SJohn Marino bookkeeping specific to exceptions. There must not be normal edges
92*e4b17023SJohn Marino into the block containing the landing-pad label. */
93*e4b17023SJohn Marino rtx landing_pad;
94*e4b17023SJohn Marino
95*e4b17023SJohn Marino /* The index of this landing pad within fun->eh->lp_array. */
96*e4b17023SJohn Marino int index;
97*e4b17023SJohn Marino };
98*e4b17023SJohn Marino
99*e4b17023SJohn Marino /* A catch handler associated with an ERT_TRY region. */
100*e4b17023SJohn Marino
101*e4b17023SJohn Marino struct GTY(()) eh_catch_d
102*e4b17023SJohn Marino {
103*e4b17023SJohn Marino /* The double-linked list of all catch handlers for the region. */
104*e4b17023SJohn Marino struct eh_catch_d *next_catch;
105*e4b17023SJohn Marino struct eh_catch_d *prev_catch;
106*e4b17023SJohn Marino
107*e4b17023SJohn Marino /* A TREE_LIST of runtime type objects that this catch handler
108*e4b17023SJohn Marino will catch, or NULL if all exceptions are caught. */
109*e4b17023SJohn Marino tree type_list;
110*e4b17023SJohn Marino
111*e4b17023SJohn Marino /* A TREE_LIST of INTEGER_CSTs that correspond to the type_list entries,
112*e4b17023SJohn Marino having been mapped by assign_filter_values. These integers are to be
113*e4b17023SJohn Marino compared against the __builtin_eh_filter value. */
114*e4b17023SJohn Marino tree filter_list;
115*e4b17023SJohn Marino
116*e4b17023SJohn Marino /* The code that should be executed if this catch handler matches the
117*e4b17023SJohn Marino thrown exception. This label is only maintained until
118*e4b17023SJohn Marino pass_lower_eh_dispatch, at which point it is cleared. */
119*e4b17023SJohn Marino tree label;
120*e4b17023SJohn Marino };
121*e4b17023SJohn Marino
122*e4b17023SJohn Marino /* Describes one exception region. */
123*e4b17023SJohn Marino
124*e4b17023SJohn Marino struct GTY(()) eh_region_d
125*e4b17023SJohn Marino {
126*e4b17023SJohn Marino /* The immediately surrounding region. */
127*e4b17023SJohn Marino struct eh_region_d *outer;
128*e4b17023SJohn Marino
129*e4b17023SJohn Marino /* The list of immediately contained regions. */
130*e4b17023SJohn Marino struct eh_region_d *inner;
131*e4b17023SJohn Marino struct eh_region_d *next_peer;
132*e4b17023SJohn Marino
133*e4b17023SJohn Marino /* The index of this region within fun->eh->region_array. */
134*e4b17023SJohn Marino int index;
135*e4b17023SJohn Marino
136*e4b17023SJohn Marino /* Each region does exactly one thing. */
137*e4b17023SJohn Marino enum eh_region_type type;
138*e4b17023SJohn Marino
139*e4b17023SJohn Marino /* Holds the action to perform based on the preceding type. */
140*e4b17023SJohn Marino union eh_region_u {
141*e4b17023SJohn Marino struct eh_region_u_try {
142*e4b17023SJohn Marino /* The double-linked list of all catch handlers for this region. */
143*e4b17023SJohn Marino struct eh_catch_d *first_catch;
144*e4b17023SJohn Marino struct eh_catch_d *last_catch;
145*e4b17023SJohn Marino } GTY ((tag ("ERT_TRY"))) eh_try;
146*e4b17023SJohn Marino
147*e4b17023SJohn Marino struct eh_region_u_allowed {
148*e4b17023SJohn Marino /* A TREE_LIST of runtime type objects allowed to pass. */
149*e4b17023SJohn Marino tree type_list;
150*e4b17023SJohn Marino /* The code that should be executed if the thrown exception does
151*e4b17023SJohn Marino not match the type list. This label is only maintained until
152*e4b17023SJohn Marino pass_lower_eh_dispatch, at which point it is cleared. */
153*e4b17023SJohn Marino tree label;
154*e4b17023SJohn Marino /* The integer that will be passed by the runtime to signal that
155*e4b17023SJohn Marino we should execute the code at LABEL. This integer is assigned
156*e4b17023SJohn Marino by assign_filter_values and is to be compared against the
157*e4b17023SJohn Marino __builtin_eh_filter value. */
158*e4b17023SJohn Marino int filter;
159*e4b17023SJohn Marino } GTY ((tag ("ERT_ALLOWED_EXCEPTIONS"))) allowed;
160*e4b17023SJohn Marino
161*e4b17023SJohn Marino struct eh_region_u_must_not_throw {
162*e4b17023SJohn Marino /* A function decl to be invoked if this region is actually reachable
163*e4b17023SJohn Marino from within the function, rather than implementable from the runtime.
164*e4b17023SJohn Marino The normal way for this to happen is for there to be a CLEANUP region
165*e4b17023SJohn Marino contained within this MUST_NOT_THROW region. Note that if the
166*e4b17023SJohn Marino runtime handles the MUST_NOT_THROW region, we have no control over
167*e4b17023SJohn Marino what termination function is called; it will be decided by the
168*e4b17023SJohn Marino personality function in effect for this CIE. */
169*e4b17023SJohn Marino tree failure_decl;
170*e4b17023SJohn Marino /* The location assigned to the call of FAILURE_DECL, if expanded. */
171*e4b17023SJohn Marino location_t failure_loc;
172*e4b17023SJohn Marino } GTY ((tag ("ERT_MUST_NOT_THROW"))) must_not_throw;
173*e4b17023SJohn Marino } GTY ((desc ("%0.type"))) u;
174*e4b17023SJohn Marino
175*e4b17023SJohn Marino /* The list of landing pads associated with this region. */
176*e4b17023SJohn Marino struct eh_landing_pad_d *landing_pads;
177*e4b17023SJohn Marino
178*e4b17023SJohn Marino /* EXC_PTR and FILTER values copied from the runtime for this region.
179*e4b17023SJohn Marino Each region gets its own psuedos so that if there are nested exceptions
180*e4b17023SJohn Marino we do not overwrite the values of the first exception. */
181*e4b17023SJohn Marino rtx exc_ptr_reg, filter_reg;
182*e4b17023SJohn Marino
183*e4b17023SJohn Marino /* True if this region should use __cxa_end_cleanup instead
184*e4b17023SJohn Marino of _Unwind_Resume. */
185*e4b17023SJohn Marino bool use_cxa_end_cleanup;
186*e4b17023SJohn Marino };
187*e4b17023SJohn Marino
188*e4b17023SJohn Marino typedef struct eh_landing_pad_d *eh_landing_pad;
189*e4b17023SJohn Marino typedef struct eh_catch_d *eh_catch;
190*e4b17023SJohn Marino typedef struct eh_region_d *eh_region;
191*e4b17023SJohn Marino
192*e4b17023SJohn Marino DEF_VEC_P(eh_region);
193*e4b17023SJohn Marino DEF_VEC_ALLOC_P(eh_region, gc);
194*e4b17023SJohn Marino DEF_VEC_ALLOC_P(eh_region, heap);
195*e4b17023SJohn Marino
196*e4b17023SJohn Marino DEF_VEC_P(eh_landing_pad);
197*e4b17023SJohn Marino DEF_VEC_ALLOC_P(eh_landing_pad, gc);
198*e4b17023SJohn Marino
199*e4b17023SJohn Marino
200*e4b17023SJohn Marino /* The exception status for each function. */
201*e4b17023SJohn Marino
202*e4b17023SJohn Marino struct GTY(()) eh_status
203*e4b17023SJohn Marino {
204*e4b17023SJohn Marino /* The tree of all regions for this function. */
205*e4b17023SJohn Marino eh_region region_tree;
206*e4b17023SJohn Marino
207*e4b17023SJohn Marino /* The same information as an indexable array. */
208*e4b17023SJohn Marino VEC(eh_region,gc) *region_array;
209*e4b17023SJohn Marino
210*e4b17023SJohn Marino /* The landing pads as an indexable array. */
211*e4b17023SJohn Marino VEC(eh_landing_pad,gc) *lp_array;
212*e4b17023SJohn Marino
213*e4b17023SJohn Marino /* At the gimple level, a mapping from gimple statement to landing pad
214*e4b17023SJohn Marino or must-not-throw region. See record_stmt_eh_region. */
215*e4b17023SJohn Marino htab_t GTY((param_is (struct throw_stmt_node))) throw_stmt_table;
216*e4b17023SJohn Marino
217*e4b17023SJohn Marino /* All of the runtime type data used by the function. These objects
218*e4b17023SJohn Marino are emitted to the lang-specific-data-area for the function. */
219*e4b17023SJohn Marino VEC(tree,gc) *ttype_data;
220*e4b17023SJohn Marino
221*e4b17023SJohn Marino /* The table of all action chains. These encode the eh_region tree in
222*e4b17023SJohn Marino a compact form for use by the runtime, and is also emitted to the
223*e4b17023SJohn Marino lang-specific-data-area. Note that the ARM EABI uses a different
224*e4b17023SJohn Marino format for the encoding than all other ports. */
225*e4b17023SJohn Marino union eh_status_u {
226*e4b17023SJohn Marino VEC(tree,gc) * GTY((tag ("1"))) arm_eabi;
227*e4b17023SJohn Marino VEC(uchar,gc) * GTY((tag ("0"))) other;
228*e4b17023SJohn Marino } GTY ((desc ("targetm.arm_eabi_unwinder"))) ehspec_data;
229*e4b17023SJohn Marino };
230*e4b17023SJohn Marino
231*e4b17023SJohn Marino
232*e4b17023SJohn Marino /* Invokes CALLBACK for every exception handler label. Only used by old
233*e4b17023SJohn Marino loop hackery; should not be used by new code. */
234*e4b17023SJohn Marino extern void for_each_eh_label (void (*) (rtx));
235*e4b17023SJohn Marino
236*e4b17023SJohn Marino extern void init_eh_for_function (void);
237*e4b17023SJohn Marino
238*e4b17023SJohn Marino extern void remove_eh_landing_pad (eh_landing_pad);
239*e4b17023SJohn Marino extern void remove_eh_handler (eh_region);
240*e4b17023SJohn Marino
241*e4b17023SJohn Marino extern bool current_function_has_exception_handlers (void);
242*e4b17023SJohn Marino extern void output_function_exception_table (const char *);
243*e4b17023SJohn Marino
244*e4b17023SJohn Marino extern rtx expand_builtin_eh_pointer (tree);
245*e4b17023SJohn Marino extern rtx expand_builtin_eh_filter (tree);
246*e4b17023SJohn Marino extern rtx expand_builtin_eh_copy_values (tree);
247*e4b17023SJohn Marino extern void expand_builtin_unwind_init (void);
248*e4b17023SJohn Marino extern rtx expand_builtin_eh_return_data_regno (tree);
249*e4b17023SJohn Marino extern rtx expand_builtin_extract_return_addr (tree);
250*e4b17023SJohn Marino extern void expand_builtin_init_dwarf_reg_sizes (tree);
251*e4b17023SJohn Marino extern rtx expand_builtin_frob_return_addr (tree);
252*e4b17023SJohn Marino extern rtx expand_builtin_dwarf_sp_column (void);
253*e4b17023SJohn Marino extern void expand_builtin_eh_return (tree, tree);
254*e4b17023SJohn Marino extern void expand_eh_return (void);
255*e4b17023SJohn Marino extern rtx expand_builtin_extend_pointer (tree);
256*e4b17023SJohn Marino extern void expand_dw2_landing_pad_for_region (eh_region);
257*e4b17023SJohn Marino
258*e4b17023SJohn Marino typedef tree (*duplicate_eh_regions_map) (tree, void *);
259*e4b17023SJohn Marino extern struct pointer_map_t *duplicate_eh_regions
260*e4b17023SJohn Marino (struct function *, eh_region, int, duplicate_eh_regions_map, void *);
261*e4b17023SJohn Marino
262*e4b17023SJohn Marino extern void sjlj_emit_function_exit_after (rtx);
263*e4b17023SJohn Marino
264*e4b17023SJohn Marino extern eh_region gen_eh_region_cleanup (eh_region);
265*e4b17023SJohn Marino extern eh_region gen_eh_region_try (eh_region);
266*e4b17023SJohn Marino extern eh_region gen_eh_region_allowed (eh_region, tree);
267*e4b17023SJohn Marino extern eh_region gen_eh_region_must_not_throw (eh_region);
268*e4b17023SJohn Marino
269*e4b17023SJohn Marino extern eh_catch gen_eh_region_catch (eh_region, tree);
270*e4b17023SJohn Marino extern eh_landing_pad gen_eh_landing_pad (eh_region);
271*e4b17023SJohn Marino
272*e4b17023SJohn Marino extern eh_region get_eh_region_from_number_fn (struct function *, int);
273*e4b17023SJohn Marino extern eh_region get_eh_region_from_number (int);
274*e4b17023SJohn Marino extern eh_landing_pad get_eh_landing_pad_from_number_fn (struct function*,int);
275*e4b17023SJohn Marino extern eh_landing_pad get_eh_landing_pad_from_number (int);
276*e4b17023SJohn Marino extern eh_region get_eh_region_from_lp_number_fn (struct function *, int);
277*e4b17023SJohn Marino extern eh_region get_eh_region_from_lp_number (int);
278*e4b17023SJohn Marino
279*e4b17023SJohn Marino extern eh_region eh_region_outermost (struct function *, eh_region, eh_region);
280*e4b17023SJohn Marino
281*e4b17023SJohn Marino extern void make_reg_eh_region_note (rtx insn, int ecf_flags, int lp_nr);
282*e4b17023SJohn Marino extern void make_reg_eh_region_note_nothrow_nononlocal (rtx);
283*e4b17023SJohn Marino
284*e4b17023SJohn Marino extern void verify_eh_tree (struct function *);
285*e4b17023SJohn Marino extern void dump_eh_tree (FILE *, struct function *);
286*e4b17023SJohn Marino void debug_eh_tree (struct function *);
287*e4b17023SJohn Marino extern void add_type_for_runtime (tree);
288*e4b17023SJohn Marino extern tree lookup_type_for_runtime (tree);
289*e4b17023SJohn Marino extern void assign_filter_values (void);
290*e4b17023SJohn Marino
291*e4b17023SJohn Marino extern eh_region get_eh_region_from_rtx (const_rtx);
292*e4b17023SJohn Marino extern eh_landing_pad get_eh_landing_pad_from_rtx (const_rtx);
293*e4b17023SJohn Marino
294*e4b17023SJohn Marino struct GTY(()) throw_stmt_node {
295*e4b17023SJohn Marino gimple stmt;
296*e4b17023SJohn Marino int lp_nr;
297*e4b17023SJohn Marino };
298*e4b17023SJohn Marino
299*e4b17023SJohn Marino extern struct htab *get_eh_throw_stmt_table (struct function *);
300*e4b17023SJohn Marino extern void set_eh_throw_stmt_table (struct function *, struct htab *);
301*e4b17023SJohn Marino
302*e4b17023SJohn Marino enum eh_personality_kind {
303*e4b17023SJohn Marino eh_personality_none,
304*e4b17023SJohn Marino eh_personality_any,
305*e4b17023SJohn Marino eh_personality_lang
306*e4b17023SJohn Marino };
307*e4b17023SJohn Marino
308*e4b17023SJohn Marino extern enum eh_personality_kind
309*e4b17023SJohn Marino function_needs_eh_personality (struct function *);
310*e4b17023SJohn Marino
311*e4b17023SJohn Marino /* Pre-order iteration within the eh_region tree. */
312*e4b17023SJohn Marino
313*e4b17023SJohn Marino static inline eh_region
ehr_next(eh_region r,eh_region start)314*e4b17023SJohn Marino ehr_next (eh_region r, eh_region start)
315*e4b17023SJohn Marino {
316*e4b17023SJohn Marino if (r->inner)
317*e4b17023SJohn Marino r = r->inner;
318*e4b17023SJohn Marino else if (r->next_peer && r != start)
319*e4b17023SJohn Marino r = r->next_peer;
320*e4b17023SJohn Marino else
321*e4b17023SJohn Marino {
322*e4b17023SJohn Marino do
323*e4b17023SJohn Marino {
324*e4b17023SJohn Marino r = r->outer;
325*e4b17023SJohn Marino if (r == start)
326*e4b17023SJohn Marino return NULL;
327*e4b17023SJohn Marino }
328*e4b17023SJohn Marino while (r->next_peer == NULL);
329*e4b17023SJohn Marino r = r->next_peer;
330*e4b17023SJohn Marino }
331*e4b17023SJohn Marino return r;
332*e4b17023SJohn Marino }
333*e4b17023SJohn Marino
334*e4b17023SJohn Marino #define FOR_ALL_EH_REGION_AT(R, START) \
335*e4b17023SJohn Marino for ((R) = (START); (R) != NULL; (R) = ehr_next (R, START))
336*e4b17023SJohn Marino
337*e4b17023SJohn Marino #define FOR_ALL_EH_REGION_FN(R, FN) \
338*e4b17023SJohn Marino for ((R) = (FN)->eh->region_tree; (R) != NULL; (R) = ehr_next (R, NULL))
339*e4b17023SJohn Marino
340*e4b17023SJohn Marino #define FOR_ALL_EH_REGION(R) FOR_ALL_EH_REGION_FN (R, cfun)
341