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