Use more canonical exception-handling link in docs.
[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 Overview
68 --------
69
70 When an exception is thrown in LLVM code, the runtime does its best to find a
71 handler suited to processing the circumstance.
72
73 The runtime first attempts to find an *exception frame* corresponding to the
74 function where the exception was thrown.  If the programming language supports
75 exception handling (e.g. C++), the exception frame contains a reference to an
76 exception table describing how to process the exception.  If the language does
77 not support exception handling (e.g. C), or if the exception needs to be
78 forwarded to a prior activation, the exception frame contains information about
79 how to unwind the current activation and restore the state of the prior
80 activation.  This process is repeated until the exception is handled. If the
81 exception is not handled and no activations remain, then the application is
82 terminated with an appropriate error message.
83
84 Because different programming languages have different behaviors when handling
85 exceptions, the exception handling ABI provides a mechanism for
86 supplying *personalities*. An exception handling personality is defined by
87 way of a *personality function* (e.g. ``__gxx_personality_v0`` in C++),
88 which receives the context of the exception, an *exception structure*
89 containing the exception object type and value, and a reference to the exception
90 table for the current function.  The personality function for the current
91 compile unit is specified in a *common exception frame*.
92
93 The organization of an exception table is language dependent. For C++, an
94 exception table is organized as a series of code ranges defining what to do if
95 an exception occurs in that range. Typically, the information associated with a
96 range defines which types of exception objects (using C++ *type info*) that are
97 handled in that range, and an associated action that should take place. Actions
98 typically pass control to a *landing pad*.
99
100 A landing pad corresponds roughly to the code found in the ``catch`` portion of
101 a ``try``/``catch`` sequence. When execution resumes at a landing pad, it
102 receives an *exception structure* and a *selector value* corresponding to the
103 *type* of exception thrown. The selector is then used to determine which *catch*
104 should actually process the exception.
105
106 LLVM Code Generation
107 ====================
108
109 From a C++ developer's perspective, exceptions are defined in terms of the
110 ``throw`` and ``try``/``catch`` statements. In this section we will describe the
111 implementation of LLVM exception handling in terms of C++ examples.
112
113 Throw
114 -----
115
116 Languages that support exception handling typically provide a ``throw``
117 operation to initiate the exception process. Internally, a ``throw`` operation
118 breaks down into two steps.
119
120 #. A request is made to allocate exception space for an exception structure.
121    This structure needs to survive beyond the current activation. This structure
122    will contain the type and value of the object being thrown.
123
124 #. A call is made to the runtime to raise the exception, passing the exception
125    structure as an argument.
126
127 In C++, the allocation of the exception structure is done by the
128 ``__cxa_allocate_exception`` runtime function. The exception raising is handled
129 by ``__cxa_throw``. The type of the exception is represented using a C++ RTTI
130 structure.
131
132 Try/Catch
133 ---------
134
135 A call within the scope of a *try* statement can potentially raise an
136 exception. In those circumstances, the LLVM C++ front-end replaces the call with
137 an ``invoke`` instruction. Unlike a call, the ``invoke`` has two potential
138 continuation points:
139
140 #. where to continue when the call succeeds as per normal, and
141
142 #. where to continue if the call raises an exception, either by a throw or the
143    unwinding of a throw
144
145 The term used to define a the place where an ``invoke`` continues after an
146 exception is called a *landing pad*. LLVM landing pads are conceptually
147 alternative function entry points where an exception structure reference and a
148 type info index are passed in as arguments. The landing pad saves the exception
149 structure reference and then proceeds to select the catch block that corresponds
150 to the type info of the exception object.
151
152 The LLVM `landingpad instruction <LangRef.html#i_landingpad>`_ is used to convey
153 information about the landing pad to the back end. For C++, the ``landingpad``
154 instruction returns a pointer and integer pair corresponding to the pointer to
155 the *exception structure* and the *selector value* respectively.
156
157 The ``landingpad`` instruction takes a reference to the personality function to
158 be used for this ``try``/``catch`` sequence. The remainder of the instruction is
159 a list of *cleanup*, *catch*, and *filter* clauses. The exception is tested
160 against the clauses sequentially from first to last. The selector value is a
161 positive number if the exception matched a type info, a negative number if it
162 matched a filter, and zero if it matched a cleanup. If nothing is matched, the
163 behavior of the program is `undefined`_. If a type info matched, then the
164 selector value is the index of the type info in the exception table, which can
165 be obtained using the `llvm.eh.typeid.for`_ intrinsic.
166
167 Once the landing pad has the type info selector, the code branches to the code
168 for the first catch. The catch then checks the value of the type info selector
169 against the index of type info for that catch.  Since the type info index is not
170 known until all the type infos have been gathered in the backend, the catch code
171 must call the `llvm.eh.typeid.for`_ intrinsic to determine the index for a given
172 type info. If the catch fails to match the selector then control is passed on to
173 the next catch.
174
175 Finally, the entry and exit of catch code is bracketed with calls to
176 ``__cxa_begin_catch`` and ``__cxa_end_catch``.
177
178 * ``__cxa_begin_catch`` takes an exception structure reference as an argument
179   and returns the value of the exception object.
180
181 * ``__cxa_end_catch`` takes no arguments. This function:
182
183   #. Locates the most recently caught exception and decrements its handler
184      count,
185
186   #. Removes the exception from the *caught* stack if the handler count goes to
187      zero, and
188
189   #. Destroys the exception if the handler count goes to zero and the exception
190      was not re-thrown by throw.
191
192   .. note::
193
194     a rethrow from within the catch may replace this call with a
195     ``__cxa_rethrow``.
196
197 Cleanups
198 --------
199
200 A cleanup is extra code which needs to be run as part of unwinding a scope.  C++
201 destructors are a typical example, but other languages and language extensions
202 provide a variety of different kinds of cleanups. In general, a landing pad may
203 need to run arbitrary amounts of cleanup code before actually entering a catch
204 block. To indicate the presence of cleanups, a `landingpad
205 instruction <LangRef.html#i_landingpad>`_ should have a *cleanup*
206 clause. Otherwise, the unwinder will not stop at the landing pad if there are no
207 catches or filters that require it to.
208
209 .. note::
210
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
217 When all cleanups are finished, if the exception is not handled by the current
218 function, resume unwinding by calling the `resume
219 instruction <LangRef.html#i_resume>`_, passing in the result of the
220 ``landingpad`` instruction for the original landing pad.
221
222 Throw Filters
223 -------------
224
225 C++ allows the specification of which exception types may be thrown from a
226 function. To represent this, a top level landing pad may exist to filter out
227 invalid types. To express this in LLVM code the `landingpad
228 instruction <LangRef.html#i_landingpad>`_ will have a filter clause. The clause
229 consists of an array of type infos.  ``landingpad`` will return a negative value
230 if the exception does not match any of the type infos. If no match is found then
231 a call to ``__cxa_call_unexpected`` should be made, otherwise
232 ``_Unwind_Resume``.  Each of these functions requires a reference to the
233 exception structure.  Note that the most general form of a ``landingpad``
234 instruction can have any number of catch, cleanup, and filter clauses (though
235 having more than one cleanup is pointless). The LLVM C++ front-end can generate
236 such ``landingpad`` instructions due to inlining creating nested exception
237 handling scopes.
238
239 .. _undefined:
240
241 Restrictions
242 ------------
243
244 The unwinder delegates the decision of whether to stop in a call frame to that
245 call frame's language-specific personality function. Not all unwinders guarantee
246 that they will stop to perform cleanups. For example, the GNU C++ unwinder
247 doesn't do so unless the exception is actually caught somewhere further up the
248 stack.
249
250 In order for inlining to behave correctly, landing pads must be prepared to
251 handle selector results that they did not originally advertise. Suppose that a
252 function catches exceptions of type ``A``, and it's inlined into a function that
253 catches exceptions of type ``B``. The inliner will update the ``landingpad``
254 instruction for the inlined landing pad to include the fact that ``B`` is also
255 caught. If that landing pad assumes that it will only be entered to catch an
256 ``A``, it's in for a rude awakening.  Consequently, landing pads must test for
257 the selector results they understand and then resume exception propagation with
258 the `resume instruction <LangRef.html#i_resume>`_ if none of the conditions
259 match.
260
261 Exception Handling Intrinsics
262 =============================
263
264 In addition to the ``landingpad`` and ``resume`` instructions, LLVM uses several
265 intrinsic functions (name prefixed with ``llvm.eh``) to provide exception
266 handling information at various points in generated code.
267
268 .. _llvm.eh.typeid.for:
269
270 llvm.eh.typeid.for
271 ------------------
272
273 .. code-block:: llvm
274
275   i32 @llvm.eh.typeid.for(i8* %type_info)
276
277
278 This intrinsic returns the type info index in the exception table of the current
279 function.  This value can be used to compare against the result of
280 ``landingpad`` instruction.  The single argument is a reference to a type info.
281
282 .. _llvm.eh.sjlj.setjmp:
283
284 llvm.eh.sjlj.setjmp
285 -------------------
286
287 .. code-block:: llvm
288
289   i32 @llvm.eh.sjlj.setjmp(i8* %setjmp_buf)
290
291 For SJLJ based exception handling, this intrinsic forces register saving for the
292 current function and stores the address of the following instruction for use as
293 a destination address by `llvm.eh.sjlj.longjmp`_. The buffer format and the
294 overall functioning of this intrinsic is compatible with the GCC
295 ``__builtin_setjmp`` implementation allowing code built with the clang and GCC
296 to interoperate.
297
298 The single parameter is a pointer to a five word buffer in which the calling
299 context is saved. The front end places the frame pointer in the first word, and
300 the target implementation of this intrinsic should place the destination address
301 for a `llvm.eh.sjlj.longjmp`_ in the second word. The following three words are
302 available for use in a target-specific manner.
303
304 .. _llvm.eh.sjlj.longjmp:
305
306 llvm.eh.sjlj.longjmp
307 --------------------
308
309 .. code-block:: llvm
310
311   void @llvm.eh.sjlj.longjmp(i8* %setjmp_buf)
312
313 For SJLJ based exception handling, the ``llvm.eh.sjlj.longjmp`` intrinsic is
314 used to implement ``__builtin_longjmp()``. The single parameter is a pointer to
315 a buffer populated by `llvm.eh.sjlj.setjmp`_. The frame pointer and stack
316 pointer are restored from the buffer, then control is transferred to the
317 destination address.
318
319 llvm.eh.sjlj.lsda
320 -----------------
321
322 .. code-block:: llvm
323
324   i8* @llvm.eh.sjlj.lsda()
325
326 For SJLJ based exception handling, the ``llvm.eh.sjlj.lsda`` intrinsic returns
327 the address of the Language Specific Data Area (LSDA) for the current
328 function. The SJLJ front-end code stores this address in the exception handling
329 function context for use by the runtime.
330
331 llvm.eh.sjlj.callsite
332 ---------------------
333
334 .. code-block:: llvm
335
336   void @llvm.eh.sjlj.callsite(i32 %call_site_num)
337
338 For SJLJ based exception handling, the ``llvm.eh.sjlj.callsite`` intrinsic
339 identifies the callsite value associated with the following ``invoke``
340 instruction. This is used to ensure that landing pad entries in the LSDA are
341 generated in matching order.
342
343 Asm Table Formats
344 =================
345
346 There are two tables that are used by the exception handling runtime to
347 determine which actions should be taken when an exception is thrown.
348
349 Exception Handling Frame
350 ------------------------
351
352 An exception handling frame ``eh_frame`` is very similar to the unwind frame
353 used by DWARF debug info. The frame contains all the information necessary to
354 tear down the current frame and restore the state of the prior frame. There is
355 an exception handling frame for each function in a compile unit, plus a common
356 exception handling frame that defines information common to all functions in the
357 unit.
358
359 Exception Tables
360 ----------------
361
362 An exception table contains information about what actions to take when an
363 exception is thrown in a particular part of a function's code. There is one
364 exception table per function, except leaf functions and functions that have
365 calls only to non-throwing functions. They do not need an exception table.