except.h 12 KB

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