OSDN Git Service

PR c++/43856
[pf3gnuchains/gcc-fork.git] / gcc / except.h
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>.
5
6 This file is part of GCC.
7
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
11 version.
12
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
16 for more details.
17
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/>.  */
21
22 #include "sbitmap.h"
23 #include "vecprim.h"
24
25 struct function;
26 struct eh_region_d;
27 struct pointer_map_t;
28
29 /* The type of an exception region.  */
30 enum eh_region_type
31 {
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
36      exception.  */
37   ERT_CLEANUP,
38
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.  */
44   ERT_TRY,
45
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,
52
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
59      current function.  */
60   ERT_MUST_NOT_THROW
61 };
62
63
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.  */
66
67 struct GTY(()) eh_landing_pad_d
68 {
69   /* The linked list of all landing pads associated with the region.  */
70   struct eh_landing_pad_d *next_lp;
71
72   /* The region with which this landing pad is associated.  */
73   struct eh_region_d *region;
74
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;
79
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.  */
85   rtx landing_pad;
86
87   /* The index of this landing pad within fun->eh->lp_array.  */
88   int index;
89 };
90
91 /* A catch handler associated with an ERT_TRY region.  */
92
93 struct GTY(()) eh_catch_d
94 {
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;
98
99   /* A TREE_LIST of runtime type objects that this catch handler
100      will catch, or NULL if all exceptions are caught.  */
101   tree type_list;
102
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.  */
106   tree filter_list;
107
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.  */
111   tree label;
112 };
113
114 /* Describes one exception region.  */
115
116 struct GTY(()) eh_region_d
117 {
118   /* The immediately surrounding region.  */
119   struct eh_region_d *outer;
120
121   /* The list of immediately contained regions.  */
122   struct eh_region_d *inner;
123   struct eh_region_d *next_peer;
124
125   /* The index of this region within fun->eh->region_array.  */
126   int index;
127
128   /* Each region does exactly one thing.  */
129   enum eh_region_type type;
130
131   /* Holds the action to perform based on the preceding type.  */
132   union eh_region_u {
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;
138
139     struct eh_region_u_allowed {
140       /* A TREE_LIST of runtime type objects allowed to pass.  */
141       tree type_list;
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.  */
145       tree label;
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.  */
150       int filter;
151     } GTY ((tag ("ERT_ALLOWED_EXCEPTIONS"))) allowed;
152
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.  */
161       tree failure_decl;
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;
166
167   /* The list of landing pads associated with this region.  */
168   struct eh_landing_pad_d *landing_pads;
169
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;
174
175   /* True if this region should use __cxa_end_cleanup instead
176      of _Unwind_Resume.  */
177   bool use_cxa_end_cleanup;
178 };
179
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;
183
184 DEF_VEC_P(eh_region);
185 DEF_VEC_ALLOC_P(eh_region, gc);
186 DEF_VEC_ALLOC_P(eh_region, heap);
187
188 DEF_VEC_P(eh_landing_pad);
189 DEF_VEC_ALLOC_P(eh_landing_pad, gc);
190
191
192 /* The exception status for each function.  */
193
194 struct GTY(()) eh_status
195 {
196   /* The tree of all regions for this function.  */
197   eh_region region_tree;
198
199   /* The same information as an indexable array.  */
200   VEC(eh_region,gc) *region_array;
201
202   /* The landing pads as an indexable array.  */
203   VEC(eh_landing_pad,gc) *lp_array;
204
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;
208
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;
212
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.  */
217   union eh_status_u {
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;
221 };
222
223
224 /* Test: is exception handling turned on?  */
225 extern int doing_eh (int);
226
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));
230
231 extern void init_eh (void);
232 extern void init_eh_for_function (void);
233
234 extern void remove_eh_landing_pad (eh_landing_pad);
235 extern void remove_eh_handler (eh_region);
236
237 extern bool current_function_has_exception_handlers (void);
238 extern void output_function_exception_table (const char *);
239
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);
252
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 *);
256
257 extern void sjlj_emit_function_exit_after (rtx);
258
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);
263
264 extern eh_catch gen_eh_region_catch (eh_region, tree);
265 extern eh_landing_pad gen_eh_landing_pad (eh_region);
266
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);
273
274 extern eh_region eh_region_outermost (struct function *, eh_region, eh_region);
275
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);
278
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);
285
286 extern eh_region get_eh_region_from_rtx (const_rtx);
287 extern eh_landing_pad get_eh_landing_pad_from_rtx (const_rtx);
288
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);
296
297
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.  */
301
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
309 # else
310 #  define MUST_USE_SJLJ_EXCEPTIONS      1
311 # endif
312 #endif
313
314 #ifdef CONFIG_SJLJ_EXCEPTIONS
315 # if CONFIG_SJLJ_EXCEPTIONS == 1
316 #  define USING_SJLJ_EXCEPTIONS         1
317 # endif
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"
322 #  endif
323 #  if ! (defined(TARGET_UNWIND_INFO) || DWARF2_UNWIND_INFO)
324     #error "{DWARF2,TARGET}_UNWIND_INFO required"
325 #  endif
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"
329 #  endif
330 /* Usually the above error checks will have already triggered an
331    error, but backends may set MUST_USE_SJLJ_EXCEPTIONS for their own
332    reasons.  */
333 #  if MUST_USE_SJLJ_EXCEPTIONS
334     #error "Must use SJLJ exceptions but configured not to"
335 #  endif
336 # endif
337 #else
338 # define USING_SJLJ_EXCEPTIONS          MUST_USE_SJLJ_EXCEPTIONS
339 #endif
340
341 struct GTY(()) throw_stmt_node {
342   gimple stmt;
343   int lp_nr;
344 };
345
346 extern struct htab *get_eh_throw_stmt_table (struct function *);
347 extern void set_eh_throw_stmt_table (struct function *, struct htab *);
348
349 enum eh_personality_kind {
350   eh_personality_none,
351   eh_personality_any,
352   eh_personality_lang
353 };
354
355 extern enum eh_personality_kind
356 function_needs_eh_personality (struct function *);
357
358 /* Pre-order iteration within the eh_region tree.  */
359
360 static inline eh_region
361 ehr_next (eh_region r, eh_region start)
362 {
363   if (r->inner)
364     r = r->inner;
365   else if (r->next_peer && r != start)
366     r = r->next_peer;
367   else
368     {
369       do
370         {
371           r = r->outer;
372           if (r == start)
373             return NULL;
374         }
375       while (r->next_peer == NULL);
376       r = r->next_peer;
377     }
378   return r;
379 }
380
381 #define FOR_ALL_EH_REGION_AT(R, START) \
382   for ((R) = (START); (R) != NULL; (R) = ehr_next (R, START))
383
384 #define FOR_ALL_EH_REGION_FN(R, FN) \
385   for ((R) = (FN)->eh->region_tree; (R) != NULL; (R) = ehr_next (R, NULL))
386
387 #define FOR_ALL_EH_REGION(R) FOR_ALL_EH_REGION_FN (R, cfun)