1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
5 <title>Exception Handling in LLVM</title>
6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
7 <meta name="description"
8 content="Exception Handling in LLVM.">
9 <link rel="stylesheet" href="llvm.css" type="text/css">
14 <div class="doc_title">Exception Handling in LLVM</div>
16 <table class="layout" style="width:100%">
20 <li><a href="#introduction">Introduction</a>
22 <li><a href="#itanium">Itanium ABI Zero-cost Exception Handling</a></li>
23 <li><a href="#overview">Overview</a></li>
25 <li><a href="#codegen">LLVM Code Generation</a>
27 <li><a href="#throw">Throw</a></li>
28 <li><a href="#try_catch">Try/Catch</a></li>
29 <li><a href="#cleanups">Cleanups</a></li>
30 <li><a href="#throw_filters">Throw Filters</a></li>
31 <li><a href="#restrictions">Restrictions</a></li>
33 <li><a href="#format_common_intrinsics">Exception Handling Intrinsics</a>
35 <li><a href="#llvm_eh_exception"><tt>llvm.eh.exception</tt></a></li>
36 <li><a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a></li>
37 <li><a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a></li>
38 <li><a href="#llvm_eh_sjlj_setjmp"><tt>llvm.eh.sjlj.setjmp</tt></a></li>
39 <li><a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a></li>
40 <li><a href="#llvm_eh_sjlj_lsda"><tt>llvm.eh.sjlj.lsda</tt></a></li>
41 <li><a href="#llvm_eh_sjlj_callsite"><tt>llvm.eh.sjlj.callsite</tt></a></li>
43 <li><a href="#asm">Asm Table Formats</a>
45 <li><a href="#unwind_tables">Exception Handling Frame</a></li>
46 <li><a href="#exception_tables">Exception Tables</a></li>
48 <li><a href="#todo">ToDo</a></li>
53 <div class="doc_author">
54 <p>Written by <a href="mailto:jlaskey@mac.com">Jim Laskey</a></p>
58 <!-- *********************************************************************** -->
59 <div class="doc_section"><a name="introduction">Introduction</a></div>
60 <!-- *********************************************************************** -->
62 <div class="doc_text">
64 <p>This document is the central repository for all information pertaining to
65 exception handling in LLVM. It describes the format that LLVM exception
66 handling information takes, which is useful for those interested in creating
67 front-ends or dealing directly with the information. Further, this document
68 provides specific examples of what exception handling information is used for
73 <!-- ======================================================================= -->
74 <div class="doc_subsection">
75 <a name="itanium">Itanium ABI Zero-cost Exception Handling</a>
78 <div class="doc_text">
80 <p>Exception handling for most programming languages is designed to recover from
81 conditions that rarely occur during general use of an application. To that
82 end, exception handling should not interfere with the main flow of an
83 application's algorithm by performing checkpointing tasks, such as saving the
84 current pc or register state.</p>
86 <p>The Itanium ABI Exception Handling Specification defines a methodology for
87 providing outlying data in the form of exception tables without inlining
88 speculative exception handling code in the flow of an application's main
89 algorithm. Thus, the specification is said to add "zero-cost" to the normal
90 execution of an application.</p>
92 <p>A more complete description of the Itanium ABI exception handling runtime
93 support of can be found at
94 <a href="http://www.codesourcery.com/cxx-abi/abi-eh.html">Itanium C++ ABI:
95 Exception Handling</a>. A description of the exception frame format can be
97 <a href="http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html">Exception
98 Frames</a>, with details of the DWARF 3 specification at
99 <a href="http://www.eagercon.com/dwarf/dwarf3std.htm">DWARF 3 Standard</a>.
100 A description for the C++ exception table formats can be found at
101 <a href="http://www.codesourcery.com/cxx-abi/exceptions.pdf">Exception Handling
106 <!-- ======================================================================= -->
107 <div class="doc_subsection">
108 <a name="overview">Overview</a>
111 <div class="doc_text">
113 <p>When an exception is thrown in LLVM code, the runtime does its best to find a
114 handler suited to processing the circumstance.</p>
116 <p>The runtime first attempts to find an <i>exception frame</i> corresponding to
117 the function where the exception was thrown. If the programming language
118 (e.g. C++) supports exception handling, the exception frame contains a
119 reference to an exception table describing how to process the exception. If
120 the language (e.g. C) does not support exception handling, or if the
121 exception needs to be forwarded to a prior activation, the exception frame
122 contains information about how to unwind the current activation and restore
123 the state of the prior activation. This process is repeated until the
124 exception is handled. If the exception is not handled and no activations
125 remain, then the application is terminated with an appropriate error
128 <p>Because different programming languages have different behaviors when
129 handling exceptions, the exception handling ABI provides a mechanism for
130 supplying <i>personalities.</i> An exception handling personality is defined
131 by way of a <i>personality function</i> (e.g. <tt>__gxx_personality_v0</tt>
132 in C++), which receives the context of the exception, an <i>exception
133 structure</i> containing the exception object type and value, and a reference
134 to the exception table for the current function. The personality function
135 for the current compile unit is specified in a <i>common exception
138 <p>The organization of an exception table is language dependent. For C++, an
139 exception table is organized as a series of code ranges defining what to do
140 if an exception occurs in that range. Typically, the information associated
141 with a range defines which types of exception objects (using C++ <i>type
142 info</i>) that are handled in that range, and an associated action that
143 should take place. Actions typically pass control to a <i>landing
146 <p>A landing pad corresponds to the code found in the <i>catch</i> portion of
147 a <i>try</i>/<i>catch</i> sequence. When execution resumes at a landing
148 pad, it receives the exception structure and a selector corresponding to
149 the <i>type</i> of exception thrown. The selector is then used to determine
150 which <i>catch</i> should actually process the exception.</p>
154 <!-- ======================================================================= -->
155 <div class="doc_section">
156 <a name="codegen">LLVM Code Generation</a>
159 <div class="doc_text">
161 <p>At the time of this writing, only C++ exception handling support is available
162 in LLVM. So the remainder of this document will be somewhat C++-centric.</p>
164 <p>From the C++ developers perspective, exceptions are defined in terms of the
165 <tt>throw</tt> and <tt>try</tt>/<tt>catch</tt> statements. In this section
166 we will describe the implementation of LLVM exception handling in terms of
171 <!-- ======================================================================= -->
172 <div class="doc_subsection">
173 <a name="throw">Throw</a>
176 <div class="doc_text">
178 <p>Languages that support exception handling typically provide a <tt>throw</tt>
179 operation to initiate the exception process. Internally, a throw operation
180 breaks down into two steps. First, a request is made to allocate exception
181 space for an exception structure. This structure needs to survive beyond the
182 current activation. This structure will contain the type and value of the
183 object being thrown. Second, a call is made to the runtime to raise the
184 exception, passing the exception structure as an argument.</p>
186 <p>In C++, the allocation of the exception structure is done by
187 the <tt>__cxa_allocate_exception</tt> runtime function. The exception
188 raising is handled by <tt>__cxa_throw</tt>. The type of the exception is
189 represented using a C++ RTTI structure.</p>
193 <!-- ======================================================================= -->
194 <div class="doc_subsection">
195 <a name="try_catch">Try/Catch</a>
198 <div class="doc_text">
200 <p>A call within the scope of a <i>try</i> statement can potentially raise an
201 exception. In those circumstances, the LLVM C++ front-end replaces the call
202 with an <tt>invoke</tt> instruction. Unlike a call, the <tt>invoke</tt> has
203 two potential continuation points: where to continue when the call succeeds
204 as per normal; and where to continue if the call raises an exception, either
205 by a throw or the unwinding of a throw.</p>
207 <p>The term used to define a the place where an <tt>invoke</tt> continues after
208 an exception is called a <i>landing pad</i>. LLVM landing pads are
209 conceptually alternative function entry points where an exception structure
210 reference and a type info index are passed in as arguments. The landing pad
211 saves the exception structure reference and then proceeds to select the catch
212 block that corresponds to the type info of the exception object.</p>
214 <p>Two LLVM intrinsic functions are used to convey information about the landing
215 pad to the back end.</p>
218 <li><a href="#llvm_eh_exception"><tt>llvm.eh.exception</tt></a> takes no
219 arguments and returns a pointer to the exception structure. This only
220 returns a sensible value if called after an <tt>invoke</tt> has branched
221 to a landing pad. Due to code generation limitations, it must currently
222 be called in the landing pad itself.</li>
224 <li><a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> takes a minimum
225 of three arguments. The first argument is the reference to the exception
226 structure. The second argument is a reference to the personality function
227 to be used for this <tt>try</tt>/<tt>catch</tt> sequence. Each of the
228 remaining arguments is either a reference to the type info for
229 a <tt>catch</tt> statement, a <a href="#throw_filters">filter</a>
230 expression, or the number zero (<tt>0</tt>) representing
231 a <a href="#cleanups">cleanup</a>. The exception is tested against the
232 arguments sequentially from first to last. The result of
233 the <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> is a
234 positive number if the exception matched a type info, a negative number if
235 it matched a filter, and zero if it matched a cleanup. If nothing is
236 matched, the behaviour of the program
237 is <a href="#restrictions">undefined</a>. This only returns a sensible
238 value if called after an <tt>invoke</tt> has branched to a landing pad.
239 Due to codegen limitations, it must currently be called in the landing pad
240 itself. If a type info matched, then the selector value is the index of
241 the type info in the exception table, which can be obtained using the
242 <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a>
246 <p>Once the landing pad has the type info selector, the code branches to the
247 code for the first catch. The catch then checks the value of the type info
248 selector against the index of type info for that catch. Since the type info
249 index is not known until all the type info have been gathered in the backend,
250 the catch code will call the
251 <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic
252 to determine the index for a given type info. If the catch fails to match
253 the selector then control is passed on to the next catch. Note: Since the
254 landing pad will not be used if there is no match in the list of type info on
255 the call to <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>, then
256 neither the last catch nor <i>catch all</i> need to perform the check
257 against the selector.</p>
259 <p>Finally, the entry and exit of catch code is bracketed with calls
260 to <tt>__cxa_begin_catch</tt> and <tt>__cxa_end_catch</tt>.</p>
263 <li><tt>__cxa_begin_catch</tt> takes a exception structure reference as an
264 argument and returns the value of the exception object.</li>
266 <li><tt>__cxa_end_catch</tt> takes a exception structure reference as an
267 argument. This function clears the exception from the exception space.
268 Note: a rethrow from within the catch may replace this call with
269 a <tt>__cxa_rethrow</tt>.</li>
274 <!-- ======================================================================= -->
275 <div class="doc_subsection">
276 <a name="cleanups">Cleanups</a>
279 <div class="doc_text">
281 <p>To handle destructors and cleanups in <tt>try</tt> code, control may not run
282 directly from a landing pad to the first catch. Control may actually flow
283 from the landing pad to clean up code and then to the first catch. Since the
284 required clean up for each <tt>invoke</tt> in a <tt>try</tt> may be different
285 (e.g. intervening constructor), there may be several landing pads for a given
286 try. If cleanups need to be run, the number zero should be passed as the
287 last <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> argument.
288 However for C++ a <tt>null i8*</tt> <b><a href="#restrictions">must</a></b>
289 be passed instead.</p>
293 <!-- ======================================================================= -->
294 <div class="doc_subsection">
295 <a name="throw_filters">Throw Filters</a>
298 <div class="doc_text">
300 <p>C++ allows the specification of which exception types can be thrown from a
301 function. To represent this a top level landing pad may exist to filter out
302 invalid types. To express this in LLVM code the landing pad will
303 call <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>. The
304 arguments are a reference to the exception structure, a reference to the
305 personality function, the length of the filter expression (the number of type
306 infos plus one), followed by the type infos themselves.
307 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> will return a
308 negative value if the exception does not match any of the type infos. If no
309 match is found then a call to <tt>__cxa_call_unexpected</tt> should be made,
310 otherwise <tt>_Unwind_Resume</tt>. Each of these functions requires a
311 reference to the exception structure. Note that the most general form of an
312 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> call can contain
313 any number of type infos, filter expressions and cleanups (though having more
314 than one cleanup is pointless). The LLVM C++ front-end can generate such
315 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> calls due to
316 inlining creating nested exception handling scopes.</p>
320 <!-- ======================================================================= -->
321 <div class="doc_subsection">
322 <a name="restrictions">Restrictions</a>
325 <div class="doc_text">
327 <p>The semantics of the invoke instruction require that any exception that
328 unwinds through an invoke call should result in a branch to the invoke's
329 unwind label. However such a branch will only happen if the
330 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> matches. Thus in
331 order to ensure correct operation, the front-end must only generate
332 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> calls that are
333 guaranteed to always match whatever exception unwinds through the invoke.
334 For most languages it is enough to pass zero, indicating the presence of
335 a <a href="#cleanups">cleanup</a>, as the
336 last <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> argument.
337 However for C++ this is not sufficient, because the C++ personality function
338 will terminate the program if it detects that unwinding the exception only
339 results in matches with cleanups. For C++ a <tt>null i8*</tt> should be
340 passed as the last <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>
341 argument instead. This is interpreted as a catch-all by the C++ personality
342 function, and will always match.</p>
346 <!-- ======================================================================= -->
347 <div class="doc_section">
348 <a name="format_common_intrinsics">Exception Handling Intrinsics</a>
351 <div class="doc_text">
353 <p>LLVM uses several intrinsic functions (name prefixed with "llvm.eh") to
354 provide exception handling information at various points in generated
359 <!-- ======================================================================= -->
360 <div class="doc_subsubsection">
361 <a name="llvm_eh_exception">llvm.eh.exception</a>
364 <div class="doc_text">
367 i8* %<a href="#llvm_eh_exception">llvm.eh.exception</a>( )
370 <p>This intrinsic returns a pointer to the exception structure.</p>
374 <!-- ======================================================================= -->
375 <div class="doc_subsubsection">
376 <a name="llvm_eh_selector">llvm.eh.selector</a>
379 <div class="doc_text">
382 i32 %<a href="#llvm_eh_selector">llvm.eh.selector.i32</a>(i8*, i8*, i8*, ...)
383 i64 %<a href="#llvm_eh_selector">llvm.eh.selector.i64</a>(i8*, i8*, i8*, ...)
386 <p>This intrinsic is used to compare the exception with the given type infos,
387 filters and cleanups.</p>
389 <p><a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> takes a minimum of
390 three arguments. The first argument is the reference to the exception
391 structure. The second argument is a reference to the personality function to
392 be used for this try catch sequence. Each of the remaining arguments is
393 either a reference to the type info for a catch statement,
394 a <a href="#throw_filters">filter</a> expression, or the number zero
395 representing a <a href="#cleanups">cleanup</a>. The exception is tested
396 against the arguments sequentially from first to last. The result of
397 the <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> is a positive
398 number if the exception matched a type info, a negative number if it matched
399 a filter, and zero if it matched a cleanup. If nothing is matched, the
400 behaviour of the program is <a href="#restrictions">undefined</a>. If a type
401 info matched then the selector value is the index of the type info in the
402 exception table, which can be obtained using the
403 <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic.</p>
407 <!-- ======================================================================= -->
408 <div class="doc_subsubsection">
409 <a name="llvm_eh_typeid_for">llvm.eh.typeid.for</a>
412 <div class="doc_text">
415 i32 %<a href="#llvm_eh_typeid_for">llvm.eh.typeid.for.i32</a>(i8*)
416 i64 %<a href="#llvm_eh_typeid_for">llvm.eh.typeid.for.i64</a>(i8*)
419 <p>This intrinsic returns the type info index in the exception table of the
420 current function. This value can be used to compare against the result
421 of <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>. The single
422 argument is a reference to a type info.</p>
426 <!-- ======================================================================= -->
427 <div class="doc_subsubsection">
428 <a name="llvm_eh_sjlj_setjmp">llvm.eh.sjlj.setjmp</a>
431 <div class="doc_text">
434 i32 %<a href="#llvm_eh_sjlj_setjmp">llvm.eh.sjlj.setjmp</a>(i8*)
437 <p>The SJLJ exception handling uses this intrinsic to force register saving for
438 the current function and to store the address of the following instruction
439 for use as a destination address by <a href="#llvm_eh_sjlj_longjmp">
440 <tt>llvm.eh.sjlj.longjmp</tt></a>. The buffer format and the overall
441 functioning of this intrinsic is compatible with the GCC
442 <tt>__builtin_setjmp</tt> implementation, allowing code built with the
443 two compilers to interoperate.</p>
445 <p>The single parameter is a pointer to a five word buffer in which the calling
446 context is saved. The front end places the frame pointer in the first word,
447 and the target implementation of this intrinsic should place the destination
449 <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a> in the
450 second word. The following three words are available for use in a
451 target-specific manner.</p>
455 <!-- ======================================================================= -->
456 <div class="doc_subsubsection">
457 <a name="llvm_eh_sjlj_lsda">llvm.eh.sjlj.lsda</a>
460 <div class="doc_text">
463 i8* %<a href="#llvm_eh_sjlj_lsda">llvm.eh.sjlj.lsda</a>( )
466 <p>Used for SJLJ based exception handling, the <a href="#llvm_eh_sjlj_lsda">
467 <tt>llvm.eh.sjlj.lsda</tt></a> intrinsic returns the address of the Language
468 Specific Data Area (LSDA) for the current function. The SJLJ front-end code
469 stores this address in the exception handling function context for use by the
474 <!-- ======================================================================= -->
475 <div class="doc_subsubsection">
476 <a name="llvm_eh_sjlj_callsite">llvm.eh.sjlj.callsite</a>
479 <div class="doc_text">
482 void %<a href="#llvm_eh_sjlj_callsite">llvm.eh.sjlj.callsite</a>(i32)
485 <p>The SJLJ front-end allocates call site indices for invoke instrucitons.
486 These values are passed to the back-end via the
487 <a href="#llvm_eh_sjlj_callsite"><tt>llvm.eh.sjlj.callsite</tt></a>
488 intrinsic, where they are used to build the LSDA call-site table.</p>
492 <!-- ======================================================================= -->
493 <div class="doc_section">
494 <a name="asm">Asm Table Formats</a>
497 <div class="doc_text">
499 <p>There are two tables that are used by the exception handling runtime to
500 determine which actions should take place when an exception is thrown.</p>
504 <!-- ======================================================================= -->
505 <div class="doc_subsection">
506 <a name="unwind_tables">Exception Handling Frame</a>
509 <div class="doc_text">
511 <p>An exception handling frame <tt>eh_frame</tt> is very similar to the unwind
512 frame used by dwarf debug info. The frame contains all the information
513 necessary to tear down the current frame and restore the state of the prior
514 frame. There is an exception handling frame for each function in a compile
515 unit, plus a common exception handling frame that defines information common
516 to all functions in the unit.</p>
518 <p>Todo - Table details here.</p>
522 <!-- ======================================================================= -->
523 <div class="doc_subsection">
524 <a name="exception_tables">Exception Tables</a>
527 <div class="doc_text">
529 <p>An exception table contains information about what actions to take when an
530 exception is thrown in a particular part of a function's code. There is one
531 exception table per function except leaf routines and functions that have
532 only calls to non-throwing functions will not need an exception table.</p>
534 <p>Todo - Table details here.</p>
538 <!-- ======================================================================= -->
539 <div class="doc_section">
540 <a name="todo">ToDo</a>
543 <div class="doc_text">
547 <li>Testing/Testing/Testing.</li>
553 <!-- *********************************************************************** -->
557 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
558 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
559 <a href="http://validator.w3.org/check/referer"><img
560 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
562 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
563 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
564 Last modified: $Date$