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 6This file is part of GCC. 7 8GCC is free software; you can redistribute it and/or modify it under 9the terms of the GNU General Public License as published by the Free 10Software Foundation; either version 3, or (at your option) any later 11version. 12 13GCC is distributed in the hope that it will be useful, but WITHOUT ANY 14WARRANTY; without even the implied warranty of MERCHANTABILITY or 15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16for more details. 17 18You should have received a copy of the GNU General Public License 19along 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 25struct function; 26struct eh_region_d; 27struct pointer_map_t; 28 29/* The type of an exception region. */ 30enum 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 67struct 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 93struct 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 116struct 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 180typedef struct eh_landing_pad_d *eh_landing_pad; 181typedef struct eh_catch_d *eh_catch; 182typedef struct eh_region_d *eh_region; 183 184DEF_VEC_P(eh_region); 185DEF_VEC_ALLOC_P(eh_region, gc); 186DEF_VEC_ALLOC_P(eh_region, heap); 187 188DEF_VEC_P(eh_landing_pad); 189DEF_VEC_ALLOC_P(eh_landing_pad, gc); 190 191 192/* The exception status for each function. */ 193 194struct 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? */ 225extern 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. */ 229extern void for_each_eh_label (void (*) (rtx)); 230 231extern void init_eh (void); 232extern void init_eh_for_function (void); 233 234extern void remove_eh_landing_pad (eh_landing_pad); 235extern void remove_eh_handler (eh_region); 236 237extern bool current_function_has_exception_handlers (void); 238extern void output_function_exception_table (const char *); 239 240extern rtx expand_builtin_eh_pointer (tree); 241extern rtx expand_builtin_eh_filter (tree); 242extern rtx expand_builtin_eh_copy_values (tree); 243extern void expand_builtin_unwind_init (void); 244extern rtx expand_builtin_eh_return_data_regno (tree); 245extern rtx expand_builtin_extract_return_addr (tree); 246extern void expand_builtin_init_dwarf_reg_sizes (tree); 247extern rtx expand_builtin_frob_return_addr (tree); 248extern rtx expand_builtin_dwarf_sp_column (void); 249extern void expand_builtin_eh_return (tree, tree); 250extern void expand_eh_return (void); 251extern rtx expand_builtin_extend_pointer (tree); 252 253typedef tree (*duplicate_eh_regions_map) (tree, void *); 254extern struct pointer_map_t *duplicate_eh_regions 255 (struct function *, eh_region, int, duplicate_eh_regions_map, void *); 256 257extern void sjlj_emit_function_exit_after (rtx); 258 259extern eh_region gen_eh_region_cleanup (eh_region); 260extern eh_region gen_eh_region_try (eh_region); 261extern eh_region gen_eh_region_allowed (eh_region, tree); 262extern eh_region gen_eh_region_must_not_throw (eh_region); 263 264extern eh_catch gen_eh_region_catch (eh_region, tree); 265extern eh_landing_pad gen_eh_landing_pad (eh_region); 266 267extern eh_region get_eh_region_from_number_fn (struct function *, int); 268extern eh_region get_eh_region_from_number (int); 269extern eh_landing_pad get_eh_landing_pad_from_number_fn (struct function*,int); 270extern eh_landing_pad get_eh_landing_pad_from_number (int); 271extern eh_region get_eh_region_from_lp_number_fn (struct function *, int); 272extern eh_region get_eh_region_from_lp_number (int); 273 274extern eh_region eh_region_outermost (struct function *, eh_region, eh_region); 275 276extern void make_reg_eh_region_note (rtx insn, int ecf_flags, int lp_nr); 277extern void make_reg_eh_region_note_nothrow_nononlocal (rtx); 278 279extern void verify_eh_tree (struct function *); 280extern void dump_eh_tree (FILE *, struct function *); 281void debug_eh_tree (struct function *); 282extern void add_type_for_runtime (tree); 283extern tree lookup_type_for_runtime (tree); 284extern void assign_filter_values (void); 285 286extern eh_region get_eh_region_from_rtx (const_rtx); 287extern 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'. */ 295extern tree (*lang_protect_cleanup_actions) (void); 296 297/* Return true if type A catches type B. */ 298extern int (*lang_eh_type_covers) (tree a, tree b); 299 300 301/* Just because the user configured --with-sjlj-exceptions=no doesn't 302 mean that we can use call frame exceptions. Detect that the target 303 has appropriate support. */ 304 305#ifndef MUST_USE_SJLJ_EXCEPTIONS 306# if defined (EH_RETURN_DATA_REGNO) \ 307 && (defined (TARGET_UNWIND_INFO) \ 308 || (DWARF2_UNWIND_INFO \ 309 && (defined (EH_RETURN_HANDLER_RTX) \ 310 || defined (HAVE_eh_return)))) 311# define MUST_USE_SJLJ_EXCEPTIONS 0 312# else 313# define MUST_USE_SJLJ_EXCEPTIONS 1 314# endif 315#endif 316 317#ifdef CONFIG_SJLJ_EXCEPTIONS 318# if CONFIG_SJLJ_EXCEPTIONS == 1 319# define USING_SJLJ_EXCEPTIONS 1 320# endif 321# if CONFIG_SJLJ_EXCEPTIONS == 0 322# define USING_SJLJ_EXCEPTIONS 0 323# if !defined(EH_RETURN_DATA_REGNO) 324 #error "EH_RETURN_DATA_REGNO required" 325# endif 326# if ! (defined(TARGET_UNWIND_INFO) || DWARF2_UNWIND_INFO) 327 #error "{DWARF2,TARGET}_UNWIND_INFO required" 328# endif 329# if !defined(TARGET_UNWIND_INFO) \ 330 && !(defined(EH_RETURN_HANDLER_RTX) || defined(HAVE_eh_return)) 331 #error "EH_RETURN_HANDLER_RTX or eh_return required" 332# endif 333/* Usually the above error checks will have already triggered an 334 error, but backends may set MUST_USE_SJLJ_EXCEPTIONS for their own 335 reasons. */ 336# if MUST_USE_SJLJ_EXCEPTIONS 337 #error "Must use SJLJ exceptions but configured not to" 338# endif 339# endif 340#else 341# define USING_SJLJ_EXCEPTIONS MUST_USE_SJLJ_EXCEPTIONS 342#endif 343 344struct GTY(()) throw_stmt_node { 345 gimple stmt; 346 int lp_nr; 347}; 348 349extern struct htab *get_eh_throw_stmt_table (struct function *); 350extern void set_eh_throw_stmt_table (struct function *, struct htab *); 351 352enum eh_personality_kind { 353 eh_personality_none, 354 eh_personality_any, 355 eh_personality_lang 356}; 357 358extern enum eh_personality_kind 359function_needs_eh_personality (struct function *); 360 361/* Pre-order iteration within the eh_region tree. */ 362 363static inline eh_region 364ehr_next (eh_region r, eh_region start) 365{ 366 if (r->inner) 367 r = r->inner; 368 else if (r->next_peer && r != start) 369 r = r->next_peer; 370 else 371 { 372 do 373 { 374 r = r->outer; 375 if (r == start) 376 return NULL; 377 } 378 while (r->next_peer == NULL); 379 r = r->next_peer; 380 } 381 return r; 382} 383 384#define FOR_ALL_EH_REGION_AT(R, START) \ 385 for ((R) = (START); (R) != NULL; (R) = ehr_next (R, START)) 386 387#define FOR_ALL_EH_REGION_FN(R, FN) \ 388 for ((R) = (FN)->eh->region_tree; (R) != NULL; (R) = ehr_next (R, NULL)) 389 390#define FOR_ALL_EH_REGION(R) FOR_ALL_EH_REGION_FN (R, cfun) 391