except.h 12 KB

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