166fe4cf11d7cb6ef91f73e35ff3a2f7e2953f71
[oota-llvm.git] / docs / ExceptionHandling.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2                       "http://www.w3.org/TR/html4/strict.dtd">
3 <html>
4 <head>
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">
10 </head>
11
12 <body>
13
14 <div class="doc_title">Exception Handling in LLVM</div>
15
16 <table class="layout" style="width:100%">
17   <tr class="layout">
18     <td class="left">
19 <ul>
20   <li><a href="#introduction">Introduction</a>
21   <ol>
22     <li><a href="#itanium">Itanium ABI Zero-cost Exception Handling</a></li>
23     <li><a href="#overview">Overview</a></li>
24   </ol></li>
25   <li><a href="#codegen">LLVM Code Generation</a>
26   <ol>
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>
32   </ol></li>
33   <li><a href="#format_common_intrinsics">Exception Handling Intrinsics</a>
34   <ol>
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>
42   </ol></li>
43   <li><a href="#asm">Asm Table Formats</a>
44   <ol>
45     <li><a href="#unwind_tables">Exception Handling Frame</a></li>
46     <li><a href="#exception_tables">Exception Tables</a></li>
47   </ol></li>
48   <li><a href="#todo">ToDo</a></li>
49 </ul>
50 </td>
51 </tr></table>
52
53 <div class="doc_author">
54   <p>Written by <a href="mailto:jlaskey@mac.com">Jim Laskey</a></p>
55 </div>
56
57
58 <!-- *********************************************************************** -->
59 <div class="doc_section"><a name="introduction">Introduction</a></div> 
60 <!-- *********************************************************************** -->
61
62 <div class="doc_text">
63
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
69    in C/C++.</p>
70
71 </div>
72
73 <!-- ======================================================================= -->
74 <div class="doc_subsection">
75   <a name="itanium">Itanium ABI Zero-cost Exception Handling</a>
76 </div>
77
78 <div class="doc_text">
79
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>
85
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>
91
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
96    found at
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
102    Tables</a>.</p>
103
104 </div>
105
106 <!-- ======================================================================= -->
107 <div class="doc_subsection">
108   <a name="overview">Overview</a>
109 </div>
110
111 <div class="doc_text">
112
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>
115
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
126    message.</p>
127
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
136    frame</i>.</p>
137
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
144    pad</i>.</p>
145
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>
151
152 </div>
153
154 <!-- ======================================================================= -->
155 <div class="doc_section">
156   <a name="codegen">LLVM Code Generation</a>
157 </div>
158
159 <div class="doc_text">
160
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>
163
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
167    C++ examples.</p>
168
169 </div>
170
171 <!-- ======================================================================= -->
172 <div class="doc_subsection">
173   <a name="throw">Throw</a>
174 </div>
175
176 <div class="doc_text">
177
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>
185
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>
190
191 </div>
192
193 <!-- ======================================================================= -->
194 <div class="doc_subsection">
195   <a name="try_catch">Try/Catch</a>
196 </div>
197
198 <div class="doc_text">
199
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>
206
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>
213
214 <p>Two LLVM intrinsic functions are used to convey information about the landing
215    pad to the back end.</p>
216
217 <ol>
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>
223
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>
243       intrinsic.</li>
244 </ol>
245
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>
258
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>
261
262 <ul>
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>
265
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>
270 </ul>
271
272 </div>
273
274 <!-- ======================================================================= -->
275 <div class="doc_subsection">
276   <a name="cleanups">Cleanups</a>
277 </div>
278
279 <div class="doc_text">
280
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>
290
291 </div>
292
293 <!-- ======================================================================= -->
294 <div class="doc_subsection">
295   <a name="throw_filters">Throw Filters</a>
296 </div>
297
298 <div class="doc_text">
299
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>
317
318 </div>
319
320 <!-- ======================================================================= -->
321 <div class="doc_subsection">
322   <a name="restrictions">Restrictions</a>
323 </div>
324
325 <div class="doc_text">
326
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>
343
344 </div>
345
346 <!-- ======================================================================= -->
347 <div class="doc_section">
348   <a name="format_common_intrinsics">Exception Handling Intrinsics</a>
349 </div>
350
351 <div class="doc_text">
352
353 <p>LLVM uses several intrinsic functions (name prefixed with "llvm.eh") to
354    provide exception handling information at various points in generated
355    code.</p>
356
357 </div>
358
359 <!-- ======================================================================= -->
360 <div class="doc_subsubsection">
361   <a name="llvm_eh_exception">llvm.eh.exception</a>
362 </div>
363
364 <div class="doc_text">
365 <pre>
366 i8* %<a href="#llvm_eh_exception">llvm.eh.exception</a>( )
367 </pre>
368 </div>
369
370 <p>This intrinsic returns a pointer to the exception structure.</p>
371
372 </div>
373
374 <!-- ======================================================================= -->
375 <div class="doc_subsubsection">
376   <a name="llvm_eh_selector">llvm.eh.selector</a>
377 </div>
378
379 <div class="doc_text">
380 <pre>
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*, ...)
383 </pre>
384 </div>
385
386 <p>This intrinsic is used to compare the exception with the given type infos,
387    filters and cleanups.</p>
388
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>
404
405 </div>
406
407 <!-- ======================================================================= -->
408 <div class="doc_subsubsection">
409   <a name="llvm_eh_typeid_for">llvm.eh.typeid.for</a>
410 </div>
411
412 <div class="doc_text">
413 <pre>
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*)
416 </pre>
417 </div>
418
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>
423
424 </div>
425
426 <!-- ======================================================================= -->
427 <div class="doc_subsubsection">
428   <a name="llvm_eh_sjlj_setjmp">llvm.eh.sjlj.setjmp</a>
429 </div>
430
431 <div class="doc_text">
432 <pre>
433 i32 %<a href="#llvm_eh_sjlj_setjmp">llvm.eh.sjlj.setjmp</a>(i8*)
434 </pre>
435 </div>
436
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>
444
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
448    address for a
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>
452
453 </div>
454
455 <!-- ======================================================================= -->
456 <div class="doc_subsubsection">
457   <a name="llvm_eh_sjlj_lsda">llvm.eh.sjlj.lsda</a>
458 </div>
459
460 <div class="doc_text">
461 <pre>
462 i8* %<a href="#llvm_eh_sjlj_lsda">llvm.eh.sjlj.lsda</a>( )
463 </pre>
464 </div>
465
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
470    runtime.</p>
471
472 </div>
473
474 <!-- ======================================================================= -->
475 <div class="doc_subsubsection">
476   <a name="llvm_eh_sjlj_callsite">llvm.eh.sjlj.callsite</a>
477 </div>
478
479 <div class="doc_text">
480 <pre>
481 void %<a href="#llvm_eh_sjlj_callsite">llvm.eh.sjlj.callsite</a>(i32)
482 </pre>
483 </div>
484
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>
489
490 </div>
491
492 <!-- ======================================================================= -->
493 <div class="doc_section">
494   <a name="asm">Asm Table Formats</a>
495 </div>
496
497 <div class="doc_text">
498
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>
501
502 </div>
503
504 <!-- ======================================================================= -->
505 <div class="doc_subsection">
506   <a name="unwind_tables">Exception Handling Frame</a>
507 </div>
508
509 <div class="doc_text">
510
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>
517
518 <p>Todo - Table details here.</p>
519
520 </div>
521
522 <!-- ======================================================================= -->
523 <div class="doc_subsection">
524   <a name="exception_tables">Exception Tables</a>
525 </div>
526
527 <div class="doc_text">
528
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>
533
534 <p>Todo - Table details here.</p>
535
536 </div>
537
538 <!-- ======================================================================= -->
539 <div class="doc_section">
540   <a name="todo">ToDo</a>
541 </div>
542
543 <div class="doc_text">
544
545 <ol>
546
547   <li>Testing/Testing/Testing.</li>
548
549 </ol>
550
551 </div>
552
553 <!-- *********************************************************************** -->
554
555 <hr>
556 <address>
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>
561
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$
565 </address>
566
567 </body>
568 </html>