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>
42 <li><a href="#asm">Asm Table Formats</a>
44 <li><a href="#unwind_tables">Exception Handling Frame</a></li>
45 <li><a href="#exception_tables">Exception Tables</a></li>
47 <li><a href="#todo">ToDo</a></li>
52 <div class="doc_author">
53 <p>Written by <a href="mailto:jlaskey@mac.com">Jim Laskey</a></p>
57 <!-- *********************************************************************** -->
58 <div class="doc_section"><a name="introduction">Introduction</a></div>
59 <!-- *********************************************************************** -->
61 <div class="doc_text">
63 <p>This document is the central repository for all information pertaining to
64 exception handling in LLVM. It describes the format that LLVM exception
65 handling information takes, which is useful for those interested in creating
66 front-ends or dealing directly with the information. Further, this document
67 provides specific examples of what exception handling information is used for
72 <!-- ======================================================================= -->
73 <div class="doc_subsection">
74 <a name="itanium">Itanium ABI Zero-cost Exception Handling</a>
77 <div class="doc_text">
79 <p>Exception handling for most programming languages is designed to recover from
80 conditions that rarely occur during general use of an application. To that
81 end, exception handling should not interfere with the main flow of an
82 application's algorithm by performing checkpointing tasks, such as saving the
83 current pc or register state.</p>
85 <p>The Itanium ABI Exception Handling Specification defines a methodology for
86 providing outlying data in the form of exception tables without inlining
87 speculative exception handling code in the flow of an application's main
88 algorithm. Thus, the specification is said to add "zero-cost" to the normal
89 execution of an application.</p>
91 <p>A more complete description of the Itanium ABI exception handling runtime
92 support of can be found at
93 <a href="http://www.codesourcery.com/cxx-abi/abi-eh.html">Itanium C++ ABI:
94 Exception Handling</a>. A description of the exception frame format can be
96 <a href="http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html">Exception
97 Frames</a>, with details of the DWARF 3 specification at
98 <a href="http://www.eagercon.com/dwarf/dwarf3std.htm">DWARF 3 Standard</a>.
99 A description for the C++ exception table formats can be found at
100 <a href="http://www.codesourcery.com/cxx-abi/exceptions.pdf">Exception Handling
105 <!-- ======================================================================= -->
106 <div class="doc_subsection">
107 <a name="overview">Overview</a>
110 <div class="doc_text">
112 <p>When an exception is thrown in LLVM code, the runtime does its best to find a
113 handler suited to processing the circumstance.</p>
115 <p>The runtime first attempts to find an <i>exception frame</i> corresponding to
116 the function where the exception was thrown. If the programming language
117 (e.g. C++) supports exception handling, the exception frame contains a
118 reference to an exception table describing how to process the exception. If
119 the language (e.g. C) does not support exception handling, or if the
120 exception needs to be forwarded to a prior activation, the exception frame
121 contains information about how to unwind the current activation and restore
122 the state of the prior activation. This process is repeated until the
123 exception is handled. If the exception is not handled and no activations
124 remain, then the application is terminated with an appropriate error
127 <p>Because different programming languages have different behaviors when
128 handling exceptions, the exception handling ABI provides a mechanism for
129 supplying <i>personalities.</i> An exception handling personality is defined
130 by way of a <i>personality function</i> (e.g. <tt>__gxx_personality_v0</tt>
131 in C++), which receives the context of the exception, an <i>exception
132 structure</i> containing the exception object type and value, and a reference
133 to the exception table for the current function. The personality function
134 for the current compile unit is specified in a <i>common exception
137 <p>The organization of an exception table is language dependent. For C++, an
138 exception table is organized as a series of code ranges defining what to do
139 if an exception occurs in that range. Typically, the information associated
140 with a range defines which types of exception objects (using C++ <i>type
141 info</i>) that are handled in that range, and an associated action that
142 should take place. Actions typically pass control to a <i>landing
145 <p>A landing pad corresponds to the code found in the <i>catch</i> portion of
146 a <i>try</i>/<i>catch</i> sequence. When execution resumes at a landing
147 pad, it receives the exception structure and a selector corresponding to
148 the <i>type</i> of exception thrown. The selector is then used to determine
149 which <i>catch</i> should actually process the exception.</p>
153 <!-- ======================================================================= -->
154 <div class="doc_section">
155 <a name="codegen">LLVM Code Generation</a>
158 <div class="doc_text">
160 <p>At the time of this writing, only C++ exception handling support is available
161 in LLVM. So the remainder of this document will be somewhat C++-centric.</p>
163 <p>From the C++ developers perspective, exceptions are defined in terms of the
164 <tt>throw</tt> and <tt>try</tt>/<tt>catch</tt> statements. In this section
165 we will describe the implementation of LLVM exception handling in terms of
170 <!-- ======================================================================= -->
171 <div class="doc_subsection">
172 <a name="throw">Throw</a>
175 <div class="doc_text">
177 <p>Languages that support exception handling typically provide a <tt>throw</tt>
178 operation to initiate the exception process. Internally, a throw operation
179 breaks down into two steps. First, a request is made to allocate exception
180 space for an exception structure. This structure needs to survive beyond the
181 current activation. This structure will contain the type and value of the
182 object being thrown. Second, a call is made to the runtime to raise the
183 exception, passing the exception structure as an argument.</p>
185 <p>In C++, the allocation of the exception structure is done by
186 the <tt>__cxa_allocate_exception</tt> runtime function. The exception
187 raising is handled by <tt>__cxa_throw</tt>. The type of the exception is
188 represented using a C++ RTTI structure.</p>
192 <!-- ======================================================================= -->
193 <div class="doc_subsection">
194 <a name="try_catch">Try/Catch</a>
197 <div class="doc_text">
199 <p>A call within the scope of a <i>try</i> statement can potentially raise an
200 exception. In those circumstances, the LLVM C++ front-end replaces the call
201 with an <tt>invoke</tt> instruction. Unlike a call, the <tt>invoke</tt> has
202 two potential continuation points: where to continue when the call succeeds
203 as per normal; and where to continue if the call raises an exception, either
204 by a throw or the unwinding of a throw.</p>
206 <p>The term used to define a the place where an <tt>invoke</tt> continues after
207 an exception is called a <i>landing pad</i>. LLVM landing pads are
208 conceptually alternative function entry points where an exception structure
209 reference and a type info index are passed in as arguments. The landing pad
210 saves the exception structure reference and then proceeds to select the catch
211 block that corresponds to the type info of the exception object.</p>
213 <p>Two LLVM intrinsic functions are used to convey information about the landing
214 pad to the back end.</p>
217 <li><a href="#llvm_eh_exception"><tt>llvm.eh.exception</tt></a> takes no
218 arguments and returns a pointer to the exception structure. This only
219 returns a sensible value if called after an <tt>invoke</tt> has branched
220 to a landing pad. Due to code generation limitations, it must currently
221 be called in the landing pad itself.</li>
223 <li><a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> takes a minimum
224 of three arguments. The first argument is the reference to the exception
225 structure. The second argument is a reference to the personality function
226 to be used for this <tt>try</tt>/<tt>catch</tt> sequence. Each of the
227 remaining arguments is either a reference to the type info for
228 a <tt>catch</tt> statement, a <a href="#throw_filters">filter</a>
229 expression, or the number zero (<tt>0</tt>) representing
230 a <a href="#cleanups">cleanup</a>. The exception is tested against the
231 arguments sequentially from first to last. The result of
232 the <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> is a
233 positive number if the exception matched a type info, a negative number if
234 it matched a filter, and zero if it matched a cleanup. If nothing is
235 matched, the behaviour of the program
236 is <a href="#restrictions">undefined</a>. This only returns a sensible
237 value if called after an <tt>invoke</tt> has branched to a landing pad.
238 Due to codegen limitations, it must currently be called in the landing pad
239 itself. If a type info matched, then the selector value is the index of
240 the type info in the exception table, which can be obtained using the
241 <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a>
245 <p>Once the landing pad has the type info selector, the code branches to the
246 code for the first catch. The catch then checks the value of the type info
247 selector against the index of type info for that catch. Since the type info
248 index is not known until all the type info have been gathered in the backend,
249 the catch code will call the
250 <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic
251 to determine the index for a given type info. If the catch fails to match
252 the selector then control is passed on to the next catch. Note: Since the
253 landing pad will not be used if there is no match in the list of type info on
254 the call to <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>, then
255 neither the last catch nor <i>catch all</i> need to perform the check
256 against the selector.</p>
258 <p>Finally, the entry and exit of catch code is bracketed with calls
259 to <tt>__cxa_begin_catch</tt> and <tt>__cxa_end_catch</tt>.</p>
262 <li><tt>__cxa_begin_catch</tt> takes a exception structure reference as an
263 argument and returns the value of the exception object.</li>
265 <li><tt>__cxa_end_catch</tt> takes a exception structure reference as an
266 argument. This function clears the exception from the exception space.
267 Note: a rethrow from within the catch may replace this call with
268 a <tt>__cxa_rethrow</tt>.</li>
273 <!-- ======================================================================= -->
274 <div class="doc_subsection">
275 <a name="cleanups">Cleanups</a>
278 <div class="doc_text">
280 <p>To handle destructors and cleanups in <tt>try</tt> code, control may not run
281 directly from a landing pad to the first catch. Control may actually flow
282 from the landing pad to clean up code and then to the first catch. Since the
283 required clean up for each <tt>invoke</tt> in a <tt>try</tt> may be different
284 (e.g. intervening constructor), there may be several landing pads for a given
285 try. If cleanups need to be run, the number zero should be passed as the
286 last <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> argument.
287 However for C++ a <tt>null i8*</tt> <b><a href="#restrictions">must</a></b>
288 be passed instead.</p>
292 <!-- ======================================================================= -->
293 <div class="doc_subsection">
294 <a name="throw_filters">Throw Filters</a>
297 <div class="doc_text">
299 <p>C++ allows the specification of which exception types can be thrown from a
300 function. To represent this a top level landing pad may exist to filter out
301 invalid types. To express this in LLVM code the landing pad will
302 call <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>. The
303 arguments are a reference to the exception structure, a reference to the
304 personality function, the length of the filter expression (the number of type
305 infos plus one), followed by the type infos themselves.
306 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> will return a
307 negative value if the exception does not match any of the type infos. If no
308 match is found then a call to <tt>__cxa_call_unexpected</tt> should be made,
309 otherwise <tt>_Unwind_Resume</tt>. Each of these functions requires a
310 reference to the exception structure. Note that the most general form of an
311 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> call can contain
312 any number of type infos, filter expressions and cleanups (though having more
313 than one cleanup is pointless). The LLVM C++ front-end can generate such
314 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> calls due to
315 inlining creating nested exception handling scopes.</p>
319 <!-- ======================================================================= -->
320 <div class="doc_subsection">
321 <a name="restrictions">Restrictions</a>
324 <div class="doc_text">
326 <p>The semantics of the invoke instruction require that any exception that
327 unwinds through an invoke call should result in a branch to the invoke's
328 unwind label. However such a branch will only happen if the
329 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> matches. Thus in
330 order to ensure correct operation, the front-end must only generate
331 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> calls that are
332 guaranteed to always match whatever exception unwinds through the invoke.
333 For most languages it is enough to pass zero, indicating the presence of
334 a <a href="#cleanups">cleanup</a>, as the
335 last <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> argument.
336 However for C++ this is not sufficient, because the C++ personality function
337 will terminate the program if it detects that unwinding the exception only
338 results in matches with cleanups. For C++ a <tt>null i8*</tt> should be
339 passed as the last <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>
340 argument instead. This is interpreted as a catch-all by the C++ personality
341 function, and will always match.</p>
345 <!-- ======================================================================= -->
346 <div class="doc_section">
347 <a name="format_common_intrinsics">Exception Handling Intrinsics</a>
350 <div class="doc_text">
352 <p>LLVM uses several intrinsic functions (name prefixed with "llvm.eh") to
353 provide exception handling information at various points in generated
358 <!-- ======================================================================= -->
359 <div class="doc_subsubsection">
360 <a name="llvm_eh_exception">llvm.eh.exception</a>
363 <div class="doc_text">
366 i8* %<a href="#llvm_eh_exception">llvm.eh.exception</a>( )
369 <p>This intrinsic returns a pointer to the exception structure.</p>
373 <!-- ======================================================================= -->
374 <div class="doc_subsubsection">
375 <a name="llvm_eh_selector">llvm.eh.selector</a>
378 <div class="doc_text">
381 i32 %<a href="#llvm_eh_selector">llvm.eh.selector.i32</a>(i8*, i8*, i8*, ...)
382 i64 %<a href="#llvm_eh_selector">llvm.eh.selector.i64</a>(i8*, i8*, i8*, ...)
385 <p>This intrinsic is used to compare the exception with the given type infos,
386 filters and cleanups.</p>
388 <p><a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> takes a minimum of
389 three arguments. The first argument is the reference to the exception
390 structure. The second argument is a reference to the personality function to
391 be used for this try catch sequence. Each of the remaining arguments is
392 either a reference to the type info for a catch statement,
393 a <a href="#throw_filters">filter</a> expression, or the number zero
394 representing a <a href="#cleanups">cleanup</a>. The exception is tested
395 against the arguments sequentially from first to last. The result of
396 the <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> is a positive
397 number if the exception matched a type info, a negative number if it matched
398 a filter, and zero if it matched a cleanup. If nothing is matched, the
399 behaviour of the program is <a href="#restrictions">undefined</a>. If a type
400 info matched then the selector value is the index of the type info in the
401 exception table, which can be obtained using the
402 <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic.</p>
406 <!-- ======================================================================= -->
407 <div class="doc_subsubsection">
408 <a name="llvm_eh_typeid_for">llvm.eh.typeid.for</a>
411 <div class="doc_text">
414 i32 %<a href="#llvm_eh_typeid_for">llvm.eh.typeid.for.i32</a>(i8*)
415 i64 %<a href="#llvm_eh_typeid_for">llvm.eh.typeid.for.i64</a>(i8*)
418 <p>This intrinsic returns the type info index in the exception table of the
419 current function. This value can be used to compare against the result
420 of <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>. The single
421 argument is a reference to a type info.</p>
425 <!-- ======================================================================= -->
426 <div class="doc_subsubsection">
427 <a name="llvm_eh_sjlj_setjmp">llvm.eh.sjlj.setjmp</a>
430 <div class="doc_text">
433 i32 %<a href="#llvm_eh_sjlj_setjmp">llvm.eh.sjlj.setjmp</a>(i8*)
436 <p>The SJLJ exception handling uses this intrinsic to force register saving for
437 the current function and to store the address of the following instruction
438 for use as a destination address by <a href="#llvm_eh_sjlj_longjmp">
439 <tt>llvm.eh.sjlj.longjmp</tt></a>. The buffer format and the overall
440 functioning of this intrinsic is compatible with the GCC
441 <tt>__builtin_setjmp</tt> implementation, allowing code built with the
442 two compilers to interoperate.</p>
444 <p>The single parameter is a pointer to a five word buffer in which the calling
445 context is saved. The front end places the frame pointer in the first word,
446 and the target implementation of this intrinsic should place the destination
448 <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a> in the
449 second word. The following three words are available for use in a
450 target-specific manner.</p>
454 <!-- ======================================================================= -->
455 <div class="doc_subsubsection">
456 <a name="llvm_eh_sjlj_lsda">llvm.eh.sjlj.lsda</a>
459 <div class="doc_text">
462 i8* %<a href="#llvm_eh_sjlj_lsda">llvm.eh.sjlj.lsda</a>( )
465 <p>Used for SJLJ based exception handling, the <a href="#llvm_eh_sjlj_lsda">
466 <tt>llvm.eh.sjlj.lsda</tt></a> intrinsic returns the address of the Language
467 Specific Data Area (LSDA) for the current function. The SJLJ front-end code
468 stores this address in the exception handling function context for use by the
473 <!-- ======================================================================= -->
474 <div class="doc_section">
475 <a name="asm">Asm Table Formats</a>
478 <div class="doc_text">
480 <p>There are two tables that are used by the exception handling runtime to
481 determine which actions should take place when an exception is thrown.</p>
485 <!-- ======================================================================= -->
486 <div class="doc_subsection">
487 <a name="unwind_tables">Exception Handling Frame</a>
490 <div class="doc_text">
492 <p>An exception handling frame <tt>eh_frame</tt> is very similar to the unwind
493 frame used by dwarf debug info. The frame contains all the information
494 necessary to tear down the current frame and restore the state of the prior
495 frame. There is an exception handling frame for each function in a compile
496 unit, plus a common exception handling frame that defines information common
497 to all functions in the unit.</p>
499 <p>Todo - Table details here.</p>
503 <!-- ======================================================================= -->
504 <div class="doc_subsection">
505 <a name="exception_tables">Exception Tables</a>
508 <div class="doc_text">
510 <p>An exception table contains information about what actions to take when an
511 exception is thrown in a particular part of a function's code. There is one
512 exception table per function except leaf routines and functions that have
513 only calls to non-throwing functions will not need an exception table.</p>
515 <p>Todo - Table details here.</p>
519 <!-- ======================================================================= -->
520 <div class="doc_section">
521 <a name="todo">ToDo</a>
524 <div class="doc_text">
528 <li>Testing/Testing/Testing.</li>
534 <!-- *********************************************************************** -->
538 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
539 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
540 <a href="http://validator.w3.org/check/referer"><img
541 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
543 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
544 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
545 Last modified: $Date$