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