ExceptionHandling.rst 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. ==========================
  2. Exception Handling in LLVM
  3. ==========================
  4. .. contents::
  5. :local:
  6. Introduction
  7. ============
  8. This document is the central repository for all information pertaining to
  9. exception handling in LLVM. It describes the format that LLVM exception
  10. handling information takes, which is useful for those interested in creating
  11. front-ends or dealing directly with the information. Further, this document
  12. provides specific examples of what exception handling information is used for in
  13. C and C++.
  14. Itanium ABI Zero-cost Exception Handling
  15. ----------------------------------------
  16. Exception handling for most programming languages is designed to recover from
  17. conditions that rarely occur during general use of an application. To that end,
  18. exception handling should not interfere with the main flow of an application's
  19. algorithm by performing checkpointing tasks, such as saving the current pc or
  20. register state.
  21. The Itanium ABI Exception Handling Specification defines a methodology for
  22. providing outlying data in the form of exception tables without inlining
  23. speculative exception handling code in the flow of an application's main
  24. algorithm. Thus, the specification is said to add "zero-cost" to the normal
  25. execution of an application.
  26. A more complete description of the Itanium ABI exception handling runtime
  27. support of can be found at `Itanium C++ ABI: Exception Handling
  28. <http://mentorembedded.github.com/cxx-abi/abi-eh.html>`_. A description of the
  29. exception frame format can be found at `Exception Frames
  30. <http://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html>`_,
  31. with details of the DWARF 4 specification at `DWARF 4 Standard
  32. <http://dwarfstd.org/Dwarf4Std.php>`_. A description for the C++ exception
  33. table formats can be found at `Exception Handling Tables
  34. <http://mentorembedded.github.com/cxx-abi/exceptions.pdf>`_.
  35. Setjmp/Longjmp Exception Handling
  36. ---------------------------------
  37. Setjmp/Longjmp (SJLJ) based exception handling uses LLVM intrinsics
  38. `llvm.eh.sjlj.setjmp`_ and `llvm.eh.sjlj.longjmp`_ to handle control flow for
  39. exception handling.
  40. For each function which does exception processing --- be it ``try``/``catch``
  41. blocks or cleanups --- that function registers itself on a global frame
  42. list. When exceptions are unwinding, the runtime uses this list to identify
  43. which functions need processing.
  44. Landing pad selection is encoded in the call site entry of the function
  45. context. The runtime returns to the function via `llvm.eh.sjlj.longjmp`_, where
  46. a switch table transfers control to the appropriate landing pad based on the
  47. index stored in the function context.
  48. In contrast to DWARF exception handling, which encodes exception regions and
  49. frame information in out-of-line tables, SJLJ exception handling builds and
  50. removes the unwind frame context at runtime. This results in faster exception
  51. handling at the expense of slower execution when no exceptions are thrown. As
  52. exceptions are, by their nature, intended for uncommon code paths, DWARF
  53. exception handling is generally preferred to SJLJ.
  54. Windows Runtime Exception Handling
  55. -----------------------------------
  56. Windows runtime based exception handling uses the same basic IR structure as
  57. Itanium ABI based exception handling, but it relies on the personality
  58. functions provided by the native Windows runtime library, ``__CxxFrameHandler3``
  59. for C++ exceptions: ``__C_specific_handler`` for 64-bit SEH or
  60. ``_frame_handler3/4`` for 32-bit SEH. This results in a very different
  61. execution model and requires some minor modifications to the initial IR
  62. representation and a significant restructuring just before code generation.
  63. General information about the Windows x64 exception handling mechanism can be
  64. found at `MSDN Exception Handling (x64)
  65. <https://msdn.microsoft.com/en-us/library/1eyas8tf(v=vs.80).aspx>`_.
  66. Overview
  67. --------
  68. When an exception is thrown in LLVM code, the runtime does its best to find a
  69. handler suited to processing the circumstance.
  70. The runtime first attempts to find an *exception frame* corresponding to the
  71. function where the exception was thrown. If the programming language supports
  72. exception handling (e.g. C++), the exception frame contains a reference to an
  73. exception table describing how to process the exception. If the language does
  74. not support exception handling (e.g. C), or if the exception needs to be
  75. forwarded to a prior activation, the exception frame contains information about
  76. how to unwind the current activation and restore the state of the prior
  77. activation. This process is repeated until the exception is handled. If the
  78. exception is not handled and no activations remain, then the application is
  79. terminated with an appropriate error message.
  80. Because different programming languages have different behaviors when handling
  81. exceptions, the exception handling ABI provides a mechanism for
  82. supplying *personalities*. An exception handling personality is defined by
  83. way of a *personality function* (e.g. ``__gxx_personality_v0`` in C++),
  84. which receives the context of the exception, an *exception structure*
  85. containing the exception object type and value, and a reference to the exception
  86. table for the current function. The personality function for the current
  87. compile unit is specified in a *common exception frame*.
  88. The organization of an exception table is language dependent. For C++, an
  89. exception table is organized as a series of code ranges defining what to do if
  90. an exception occurs in that range. Typically, the information associated with a
  91. range defines which types of exception objects (using C++ *type info*) that are
  92. handled in that range, and an associated action that should take place. Actions
  93. typically pass control to a *landing pad*.
  94. A landing pad corresponds roughly to the code found in the ``catch`` portion of
  95. a ``try``/``catch`` sequence. When execution resumes at a landing pad, it
  96. receives an *exception structure* and a *selector value* corresponding to the
  97. *type* of exception thrown. The selector is then used to determine which *catch*
  98. should actually process the exception.
  99. LLVM Code Generation
  100. ====================
  101. From a C++ developer's perspective, exceptions are defined in terms of the
  102. ``throw`` and ``try``/``catch`` statements. In this section we will describe the
  103. implementation of LLVM exception handling in terms of C++ examples.
  104. Throw
  105. -----
  106. Languages that support exception handling typically provide a ``throw``
  107. operation to initiate the exception process. Internally, a ``throw`` operation
  108. breaks down into two steps.
  109. #. A request is made to allocate exception space for an exception structure.
  110. This structure needs to survive beyond the current activation. This structure
  111. will contain the type and value of the object being thrown.
  112. #. A call is made to the runtime to raise the exception, passing the exception
  113. structure as an argument.
  114. In C++, the allocation of the exception structure is done by the
  115. ``__cxa_allocate_exception`` runtime function. The exception raising is handled
  116. by ``__cxa_throw``. The type of the exception is represented using a C++ RTTI
  117. structure.
  118. Try/Catch
  119. ---------
  120. A call within the scope of a *try* statement can potentially raise an
  121. exception. In those circumstances, the LLVM C++ front-end replaces the call with
  122. an ``invoke`` instruction. Unlike a call, the ``invoke`` has two potential
  123. continuation points:
  124. #. where to continue when the call succeeds as per normal, and
  125. #. where to continue if the call raises an exception, either by a throw or the
  126. unwinding of a throw
  127. The term used to define the place where an ``invoke`` continues after an
  128. exception is called a *landing pad*. LLVM landing pads are conceptually
  129. alternative function entry points where an exception structure reference and a
  130. type info index are passed in as arguments. The landing pad saves the exception
  131. structure reference and then proceeds to select the catch block that corresponds
  132. to the type info of the exception object.
  133. The LLVM :ref:`i_landingpad` is used to convey information about the landing
  134. pad to the back end. For C++, the ``landingpad`` instruction returns a pointer
  135. and integer pair corresponding to the pointer to the *exception structure* and
  136. the *selector value* respectively.
  137. The ``landingpad`` instruction takes a reference to the personality function to
  138. be used for this ``try``/``catch`` sequence. The remainder of the instruction is
  139. a list of *cleanup*, *catch*, and *filter* clauses. The exception is tested
  140. against the clauses sequentially from first to last. The clauses have the
  141. following meanings:
  142. - ``catch <type> @ExcType``
  143. - This clause means that the landingpad block should be entered if the
  144. exception being thrown is of type ``@ExcType`` or a subtype of
  145. ``@ExcType``. For C++, ``@ExcType`` is a pointer to the ``std::type_info``
  146. object (an RTTI object) representing the C++ exception type.
  147. - If ``@ExcType`` is ``null``, any exception matches, so the landingpad
  148. should always be entered. This is used for C++ catch-all blocks ("``catch
  149. (...)``").
  150. - When this clause is matched, the selector value will be equal to the value
  151. returned by "``@llvm.eh.typeid.for(i8* @ExcType)``". This will always be a
  152. positive value.
  153. - ``filter <type> [<type> @ExcType1, ..., <type> @ExcTypeN]``
  154. - This clause means that the landingpad should be entered if the exception
  155. being thrown does *not* match any of the types in the list (which, for C++,
  156. are again specified as ``std::type_info`` pointers).
  157. - C++ front-ends use this to implement C++ exception specifications, such as
  158. "``void foo() throw (ExcType1, ..., ExcTypeN) { ... }``".
  159. - When this clause is matched, the selector value will be negative.
  160. - The array argument to ``filter`` may be empty; for example, "``[0 x i8**]
  161. undef``". This means that the landingpad should always be entered. (Note
  162. that such a ``filter`` would not be equivalent to "``catch i8* null``",
  163. because ``filter`` and ``catch`` produce negative and positive selector
  164. values respectively.)
  165. - ``cleanup``
  166. - This clause means that the landingpad should always be entered.
  167. - C++ front-ends use this for calling objects' destructors.
  168. - When this clause is matched, the selector value will be zero.
  169. - The runtime may treat "``cleanup``" differently from "``catch <type>
  170. null``".
  171. In C++, if an unhandled exception occurs, the language runtime will call
  172. ``std::terminate()``, but it is implementation-defined whether the runtime
  173. unwinds the stack and calls object destructors first. For example, the GNU
  174. C++ unwinder does not call object destructors when an unhandled exception
  175. occurs. The reason for this is to improve debuggability: it ensures that
  176. ``std::terminate()`` is called from the context of the ``throw``, so that
  177. this context is not lost by unwinding the stack. A runtime will typically
  178. implement this by searching for a matching non-``cleanup`` clause, and
  179. aborting if it does not find one, before entering any landingpad blocks.
  180. Once the landing pad has the type info selector, the code branches to the code
  181. for the first catch. The catch then checks the value of the type info selector
  182. against the index of type info for that catch. Since the type info index is not
  183. known until all the type infos have been gathered in the backend, the catch code
  184. must call the `llvm.eh.typeid.for`_ intrinsic to determine the index for a given
  185. type info. If the catch fails to match the selector then control is passed on to
  186. the next catch.
  187. Finally, the entry and exit of catch code is bracketed with calls to
  188. ``__cxa_begin_catch`` and ``__cxa_end_catch``.
  189. * ``__cxa_begin_catch`` takes an exception structure reference as an argument
  190. and returns the value of the exception object.
  191. * ``__cxa_end_catch`` takes no arguments. This function:
  192. #. Locates the most recently caught exception and decrements its handler
  193. count,
  194. #. Removes the exception from the *caught* stack if the handler count goes to
  195. zero, and
  196. #. Destroys the exception if the handler count goes to zero and the exception
  197. was not re-thrown by throw.
  198. .. note::
  199. a rethrow from within the catch may replace this call with a
  200. ``__cxa_rethrow``.
  201. Cleanups
  202. --------
  203. A cleanup is extra code which needs to be run as part of unwinding a scope. C++
  204. destructors are a typical example, but other languages and language extensions
  205. provide a variety of different kinds of cleanups. In general, a landing pad may
  206. need to run arbitrary amounts of cleanup code before actually entering a catch
  207. block. To indicate the presence of cleanups, a :ref:`i_landingpad` should have
  208. a *cleanup* clause. Otherwise, the unwinder will not stop at the landing pad if
  209. there are no catches or filters that require it to.
  210. .. note::
  211. Do not allow a new exception to propagate out of the execution of a
  212. cleanup. This can corrupt the internal state of the unwinder. Different
  213. languages describe different high-level semantics for these situations: for
  214. example, C++ requires that the process be terminated, whereas Ada cancels both
  215. exceptions and throws a third.
  216. When all cleanups are finished, if the exception is not handled by the current
  217. function, resume unwinding by calling the :ref:`resume instruction <i_resume>`,
  218. passing in the result of the ``landingpad`` instruction for the original
  219. landing pad.
  220. Throw Filters
  221. -------------
  222. C++ allows the specification of which exception types may be thrown from a
  223. function. To represent this, a top level landing pad may exist to filter out
  224. invalid types. To express this in LLVM code the :ref:`i_landingpad` will have a
  225. filter clause. The clause consists of an array of type infos.
  226. ``landingpad`` will return a negative value
  227. if the exception does not match any of the type infos. If no match is found then
  228. a call to ``__cxa_call_unexpected`` should be made, otherwise
  229. ``_Unwind_Resume``. Each of these functions requires a reference to the
  230. exception structure. Note that the most general form of a ``landingpad``
  231. instruction can have any number of catch, cleanup, and filter clauses (though
  232. having more than one cleanup is pointless). The LLVM C++ front-end can generate
  233. such ``landingpad`` instructions due to inlining creating nested exception
  234. handling scopes.
  235. .. _undefined:
  236. Restrictions
  237. ------------
  238. The unwinder delegates the decision of whether to stop in a call frame to that
  239. call frame's language-specific personality function. Not all unwinders guarantee
  240. that they will stop to perform cleanups. For example, the GNU C++ unwinder
  241. doesn't do so unless the exception is actually caught somewhere further up the
  242. stack.
  243. In order for inlining to behave correctly, landing pads must be prepared to
  244. handle selector results that they did not originally advertise. Suppose that a
  245. function catches exceptions of type ``A``, and it's inlined into a function that
  246. catches exceptions of type ``B``. The inliner will update the ``landingpad``
  247. instruction for the inlined landing pad to include the fact that ``B`` is also
  248. caught. If that landing pad assumes that it will only be entered to catch an
  249. ``A``, it's in for a rude awakening. Consequently, landing pads must test for
  250. the selector results they understand and then resume exception propagation with
  251. the `resume instruction <LangRef.html#i_resume>`_ if none of the conditions
  252. match.
  253. C++ Exception Handling using the Windows Runtime
  254. =================================================
  255. (Note: Windows C++ exception handling support is a work in progress and is
  256. not yet fully implemented. The text below describes how it will work
  257. when completed.)
  258. The Windows runtime function for C++ exception handling uses a multi-phase
  259. approach. When an exception occurs it searches the current callstack for a
  260. frame that has a handler for the exception. If a handler is found, it then
  261. calls the cleanup handler for each frame above the handler which has a
  262. cleanup handler before calling the catch handler. These calls are all made
  263. from a stack context different from the original frame in which the handler
  264. is defined. Therefore, it is necessary to outline these handlers from their
  265. original context before code generation.
  266. Catch handlers are called with a pointer to the handler itself as the first
  267. argument and a pointer to the parent function's stack frame as the second
  268. argument. The catch handler uses the `llvm.localrecover
  269. <LangRef.html#llvm-localescape-and-llvm-localrecover-intrinsics>`_ to get a
  270. pointer to a frame allocation block that is created in the parent frame using
  271. the `llvm.localescape
  272. <LangRef.html#llvm-localescape-and-llvm-localrecover-intrinsics>`_ intrinsic.
  273. The ``WinEHPrepare`` pass will have created a structure definition for the
  274. contents of this block. The first two members of the structure will always be
  275. (1) a 32-bit integer that the runtime uses to track the exception state of the
  276. parent frame for the purposes of handling chained exceptions and (2) a pointer
  277. to the object associated with the exception (roughly, the parameter of the
  278. catch clause). These two members will be followed by any frame variables from
  279. the parent function which must be accessed in any of the functions unwind or
  280. catch handlers. The catch handler returns the address at which execution
  281. should continue.
  282. Cleanup handlers perform any cleanup necessary as the frame goes out of scope,
  283. such as calling object destructors. The runtime handles the actual unwinding
  284. of the stack. If an exception occurs in a cleanup handler the runtime manages
  285. termination of the process. Cleanup handlers are called with the same arguments
  286. as catch handlers (a pointer to the handler and a pointer to the parent stack
  287. frame) and use the same mechanism described above to access frame variables
  288. in the parent function. Cleanup handlers do not return a value.
  289. The IR generated for Windows runtime based C++ exception handling is initially
  290. very similar to the ``landingpad`` mechanism described above. Calls to
  291. libc++abi functions (such as ``__cxa_begin_catch``/``__cxa_end_catch`` and
  292. ``__cxa_throw_exception`` are replaced with calls to intrinsics or Windows
  293. runtime functions (such as ``llvm.eh.begincatch``/``llvm.eh.endcatch`` and
  294. ``__CxxThrowException``).
  295. During the WinEHPrepare pass, the handler functions are outlined into handler
  296. functions and the original landing pad code is replaced with a call to the
  297. ``llvm.eh.actions`` intrinsic that describes the order in which handlers will
  298. be processed from the logical location of the landing pad and an indirect
  299. branch to the return value of the ``llvm.eh.actions`` intrinsic. The
  300. ``llvm.eh.actions`` intrinsic is defined as returning the address at which
  301. execution will continue. This is a temporary construct which will be removed
  302. before code generation, but it allows for the accurate tracking of control
  303. flow until then.
  304. A typical landing pad will look like this after outlining:
  305. .. code-block:: llvm
  306. lpad:
  307. %vals = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
  308. cleanup
  309. catch i8* bitcast (i8** @_ZTIi to i8*)
  310. catch i8* bitcast (i8** @_ZTIf to i8*)
  311. %recover = call i8* (...)* @llvm.eh.actions(
  312. i32 3, i8* bitcast (i8** @_ZTIi to i8*), i8* (i8*, i8*)* @_Z4testb.catch.1)
  313. i32 2, i8* null, void (i8*, i8*)* @_Z4testb.cleanup.1)
  314. i32 1, i8* bitcast (i8** @_ZTIf to i8*), i8* (i8*, i8*)* @_Z4testb.catch.0)
  315. i32 0, i8* null, void (i8*, i8*)* @_Z4testb.cleanup.0)
  316. indirectbr i8* %recover, [label %try.cont1, label %try.cont2]
  317. In this example, the landing pad represents an exception handling context with
  318. two catch handlers and a cleanup handler that have been outlined. If an
  319. exception is thrown with a type that matches ``_ZTIi``, the ``_Z4testb.catch.1``
  320. handler will be called an no clean-up is needed. If an exception is thrown
  321. with a type that matches ``_ZTIf``, first the ``_Z4testb.cleanup.1`` handler
  322. will be called to perform unwind-related cleanup, then the ``_Z4testb.catch.1``
  323. handler will be called. If an exception is throw which does not match either
  324. of these types and the exception is handled by another frame further up the
  325. call stack, first the ``_Z4testb.cleanup.1`` handler will be called, then the
  326. ``_Z4testb.cleanup.0`` handler (which corresponds to a different scope) will be
  327. called, and exception handling will continue at the next frame in the call
  328. stack will be called. One of the catch handlers will return the address of
  329. ``%try.cont1`` in the parent function and the other will return the address of
  330. ``%try.cont2``, meaning that execution continues at one of those blocks after
  331. an exception is caught.
  332. Exception Handling Intrinsics
  333. =============================
  334. In addition to the ``landingpad`` and ``resume`` instructions, LLVM uses several
  335. intrinsic functions (name prefixed with ``llvm.eh``) to provide exception
  336. handling information at various points in generated code.
  337. .. _llvm.eh.typeid.for:
  338. ``llvm.eh.typeid.for``
  339. ----------------------
  340. .. code-block:: llvm
  341. i32 @llvm.eh.typeid.for(i8* %type_info)
  342. This intrinsic returns the type info index in the exception table of the current
  343. function. This value can be used to compare against the result of
  344. ``landingpad`` instruction. The single argument is a reference to a type info.
  345. Uses of this intrinsic are generated by the C++ front-end.
  346. .. _llvm.eh.begincatch:
  347. ``llvm.eh.begincatch``
  348. ----------------------
  349. .. code-block:: llvm
  350. void @llvm.eh.begincatch(i8* %ehptr, i8* %ehobj)
  351. This intrinsic marks the beginning of catch handling code within the blocks
  352. following a ``landingpad`` instruction. The exact behavior of this function
  353. depends on the compilation target and the personality function associated
  354. with the ``landingpad`` instruction.
  355. The first argument to this intrinsic is a pointer that was previously extracted
  356. from the aggregate return value of the ``landingpad`` instruction. The second
  357. argument to the intrinsic is a pointer to stack space where the exception object
  358. should be stored. The runtime handles the details of copying the exception
  359. object into the slot. If the second parameter is null, no copy occurs.
  360. Uses of this intrinsic are generated by the C++ front-end. Many targets will
  361. use implementation-specific functions (such as ``__cxa_begin_catch``) instead
  362. of this intrinsic. The intrinsic is provided for targets that require a more
  363. abstract interface.
  364. When used in the native Windows C++ exception handling implementation, this
  365. intrinsic serves as a placeholder to delimit code before a catch handler is
  366. outlined. When the handler is is outlined, this intrinsic will be replaced
  367. by instructions that retrieve the exception object pointer from the frame
  368. allocation block.
  369. .. _llvm.eh.endcatch:
  370. ``llvm.eh.endcatch``
  371. ----------------------
  372. .. code-block:: llvm
  373. void @llvm.eh.endcatch()
  374. This intrinsic marks the end of catch handling code within the current block,
  375. which will be a successor of a block which called ``llvm.eh.begincatch''.
  376. The exact behavior of this function depends on the compilation target and the
  377. personality function associated with the corresponding ``landingpad``
  378. instruction.
  379. There may be more than one call to ``llvm.eh.endcatch`` for any given call to
  380. ``llvm.eh.begincatch`` with each ``llvm.eh.endcatch`` call corresponding to the
  381. end of a different control path. All control paths following a call to
  382. ``llvm.eh.begincatch`` must reach a call to ``llvm.eh.endcatch``.
  383. Uses of this intrinsic are generated by the C++ front-end. Many targets will
  384. use implementation-specific functions (such as ``__cxa_begin_catch``) instead
  385. of this intrinsic. The intrinsic is provided for targets that require a more
  386. abstract interface.
  387. When used in the native Windows C++ exception handling implementation, this
  388. intrinsic serves as a placeholder to delimit code before a catch handler is
  389. outlined. After the handler is outlined, this intrinsic is simply removed.
  390. .. _llvm.eh.actions:
  391. ``llvm.eh.actions``
  392. ----------------------
  393. .. code-block:: llvm
  394. void @llvm.eh.actions()
  395. This intrinsic represents the list of actions to take when an exception is
  396. thrown. It is typically used by Windows exception handling schemes where cleanup
  397. outlining is required by the runtime. The arguments are a sequence of ``i32``
  398. sentinels indicating the action type followed by some pre-determined number of
  399. arguments required to implement that action.
  400. A code of ``i32 0`` indicates a cleanup action, which expects one additional
  401. argument. The argument is a pointer to a function that implements the cleanup
  402. action.
  403. A code of ``i32 1`` indicates a catch action, which expects three additional
  404. arguments. Different EH schemes give different meanings to the three arguments,
  405. but the first argument indicates whether the catch should fire, the second is
  406. the localescape index of the exception object, and the third is the code to run
  407. to catch the exception.
  408. For Windows C++ exception handling, the first argument for a catch handler is a
  409. pointer to the RTTI type descriptor for the object to catch. The second
  410. argument is an index into the argument list of the ``llvm.localescape`` call in
  411. the main function. The exception object will be copied into the provided stack
  412. object. If the exception object is not required, this argument should be -1.
  413. The third argument is a pointer to a function implementing the catch. This
  414. function returns the address of the basic block where execution should resume
  415. after handling the exception.
  416. For Windows SEH, the first argument is a pointer to the filter function, which
  417. indicates if the exception should be caught or not. The second argument is
  418. typically negative one. The third argument is the address of a basic block
  419. where the exception will be handled. In other words, catch handlers are not
  420. outlined in SEH. After running cleanups, execution immediately resumes at this
  421. PC.
  422. In order to preserve the structure of the CFG, a call to '``llvm.eh.actions``'
  423. must be followed by an ':ref:`indirectbr <i_indirectbr>`' instruction that
  424. jumps to the result of the intrinsic call.
  425. SJLJ Intrinsics
  426. ---------------
  427. The ``llvm.eh.sjlj`` intrinsics are used internally within LLVM's
  428. backend. Uses of them are generated by the backend's
  429. ``SjLjEHPrepare`` pass.
  430. .. _llvm.eh.sjlj.setjmp:
  431. ``llvm.eh.sjlj.setjmp``
  432. ~~~~~~~~~~~~~~~~~~~~~~~
  433. .. code-block:: llvm
  434. i32 @llvm.eh.sjlj.setjmp(i8* %setjmp_buf)
  435. For SJLJ based exception handling, this intrinsic forces register saving for the
  436. current function and stores the address of the following instruction for use as
  437. a destination address by `llvm.eh.sjlj.longjmp`_. The buffer format and the
  438. overall functioning of this intrinsic is compatible with the GCC
  439. ``__builtin_setjmp`` implementation allowing code built with the clang and GCC
  440. to interoperate.
  441. The single parameter is a pointer to a five word buffer in which the calling
  442. context is saved. The front end places the frame pointer in the first word, and
  443. the target implementation of this intrinsic should place the destination address
  444. for a `llvm.eh.sjlj.longjmp`_ in the second word. The following three words are
  445. available for use in a target-specific manner.
  446. .. _llvm.eh.sjlj.longjmp:
  447. ``llvm.eh.sjlj.longjmp``
  448. ~~~~~~~~~~~~~~~~~~~~~~~~
  449. .. code-block:: llvm
  450. void @llvm.eh.sjlj.longjmp(i8* %setjmp_buf)
  451. For SJLJ based exception handling, the ``llvm.eh.sjlj.longjmp`` intrinsic is
  452. used to implement ``__builtin_longjmp()``. The single parameter is a pointer to
  453. a buffer populated by `llvm.eh.sjlj.setjmp`_. The frame pointer and stack
  454. pointer are restored from the buffer, then control is transferred to the
  455. destination address.
  456. ``llvm.eh.sjlj.lsda``
  457. ~~~~~~~~~~~~~~~~~~~~~
  458. .. code-block:: llvm
  459. i8* @llvm.eh.sjlj.lsda()
  460. For SJLJ based exception handling, the ``llvm.eh.sjlj.lsda`` intrinsic returns
  461. the address of the Language Specific Data Area (LSDA) for the current
  462. function. The SJLJ front-end code stores this address in the exception handling
  463. function context for use by the runtime.
  464. ``llvm.eh.sjlj.callsite``
  465. ~~~~~~~~~~~~~~~~~~~~~~~~~
  466. .. code-block:: llvm
  467. void @llvm.eh.sjlj.callsite(i32 %call_site_num)
  468. For SJLJ based exception handling, the ``llvm.eh.sjlj.callsite`` intrinsic
  469. identifies the callsite value associated with the following ``invoke``
  470. instruction. This is used to ensure that landing pad entries in the LSDA are
  471. generated in matching order.
  472. Asm Table Formats
  473. =================
  474. There are two tables that are used by the exception handling runtime to
  475. determine which actions should be taken when an exception is thrown.
  476. Exception Handling Frame
  477. ------------------------
  478. An exception handling frame ``eh_frame`` is very similar to the unwind frame
  479. used by DWARF debug info. The frame contains all the information necessary to
  480. tear down the current frame and restore the state of the prior frame. There is
  481. an exception handling frame for each function in a compile unit, plus a common
  482. exception handling frame that defines information common to all functions in the
  483. unit.
  484. Exception Tables
  485. ----------------
  486. An exception table contains information about what actions to take when an
  487. exception is thrown in a particular part of a function's code. There is one
  488. exception table per function, except leaf functions and functions that have
  489. calls only to non-throwing functions. They do not need an exception table.