SjLj based exception handling unwinding support. This patch is nasty, brutish
[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   <link rel="stylesheet" href="llvm.css" type="text/css">
7 </head>
8 <body>
9
10 <div class="doc_title">Exception Handling in LLVM</div>
11
12 <table class="layout" style="width:100%">
13   <tr class="layout">
14     <td class="left">
15 <ul>
16   <li><a href="#introduction">Introduction</a>
17   <ol>
18     <li><a href="#itanium">Itanium ABI Zero-cost Exception Handling</a></li>
19     <li><a href="#overview">Overview</a></li>
20   </ol></li>
21   <li><a href="#codegen">LLVM Code Generation</a>
22   <ol>
23     <li><a href="#throw">Throw</a></li>
24     <li><a href="#try_catch">Try/Catch</a></li>
25     <li><a href="#cleanups">Cleanups</a></li>
26     <li><a href="#throw_filters">Throw Filters</a></li>
27     <li><a href="#restrictions">Restrictions</a></li>
28   </ol></li>
29   <li><a href="#format_common_intrinsics">Exception Handling Intrinsics</a>
30   <ol>
31         <li><a href="#llvm_eh_exception"><tt>llvm.eh.exception</tt></a></li>
32         <li><a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a></li>
33         <li><a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a></li>
34         <li><a href="#llvm_eh_sjlj_setjmp"><tt>llvm.eh.sjlj.setjmp</tt></a></li>
35         <li><a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a></li>
36         <li><a href="#llvm_eh_sjlj_lsda"><tt>llvm.eh.sjlj.lsda</tt></a></li>
37         <li><a href="#llvm_eh_sjlj_callsite"><tt>llvm.eh.sjlj.callsite</tt></a></li>
38   </ol></li>
39   <li><a href="#asm">Asm Table Formats</a>
40   <ol>
41     <li><a href="#unwind_tables">Exception Handling Frame</a></li>
42     <li><a href="#exception_tables">Exception Tables</a></li>
43   </ol></li>
44   <li><a href="#todo">ToDo</a></li>
45 </ul>
46 </td>
47 </tr></table>
48
49 <div class="doc_author">
50   <p>Written by <a href="mailto:jlaskey@mac.com">Jim Laskey</a></p>
51 </div>
52
53
54 <!-- *********************************************************************** -->
55 <div class="doc_section"><a name="introduction">Introduction</a></div> 
56 <!-- *********************************************************************** -->
57
58 <div class="doc_text">
59
60 <p>This document is the central repository for all information pertaining to
61 exception handling in LLVM.  It describes the format that LLVM exception
62 handling information takes, which is useful for those interested in creating
63 front-ends or dealing directly with the information.  Further, this document
64 provides specific examples of what exception handling information is used for
65 C/C++.</p>
66
67 </div>
68
69 <!-- ======================================================================= -->
70 <div class="doc_subsection">
71   <a name="itanium">Itanium ABI Zero-cost Exception Handling</a>
72 </div>
73
74 <div class="doc_text">
75
76 <p>Exception handling for most programming languages is designed to recover from
77 conditions that rarely occur during general use of an application.  To that end,
78 exception handling should not interfere with the main flow of an
79 application's algorithm by performing checkpointing tasks such as saving
80 the current pc or register state.</p>
81
82 <p>The Itanium ABI Exception Handling Specification defines a methodology for
83 providing outlying data in the form of exception tables without inlining
84 speculative exception handling code in the flow of an application's main
85 algorithm.  Thus, the specification is said to add "zero-cost" to the normal
86 execution of an application.</p>
87
88 <p>A more complete description of the Itanium ABI exception handling runtime
89 support of can be found at <a
90 href="http://www.codesourcery.com/cxx-abi/abi-eh.html">Itanium C++ ABI:
91 Exception Handling.</a> A description of the exception frame format can be found
92 at <a href="http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-
93 Core-generic/ehframechpt.html">Exception Frames</a>, with details of the Dwarf
94 specification at <a href="http://www.eagercon.com/dwarf/dwarf3std.htm">Dwarf 3
95 Standard.</a> A description for the C++ exception table formats can be found at
96 <a href="http://www.codesourcery.com/cxx-abi/exceptions.pdf">Exception Handling
97 Tables.</a></p>
98
99 </div>
100
101 <!-- ======================================================================= -->
102 <div class="doc_subsection">
103   <a name="overview">Overview</a>
104 </div>
105
106 <div class="doc_text">
107
108 <p>When an exception is thrown in llvm code, the runtime does a best effort to
109 find a handler suited to process the circumstance.</p>
110
111 <p>The runtime first attempts to find an <i>exception frame</i> corresponding to
112 the function where the exception was thrown.  If the programming language (ex.
113 C++) supports exception handling, the exception frame contains a reference to an
114 exception table describing how to process the exception.  If the language (ex.
115 C) does not support exception handling or if the exception needs to be forwarded
116 to a prior activation, the exception frame contains information about how to
117 unwind the current activation and restore the state of the prior activation.
118 This process is repeated until the exception is handled.  If the exception is
119 not handled and no activations remain, then the application is terminated with
120 an appropriate error message.</p>
121
122 <p>Since different programming languages have different behaviors when handling
123 exceptions, the exception handling ABI provides a mechanism for supplying
124 <i>personalities.</i> An exception handling personality is defined by way of a
125 <i>personality function</i> (ex. for C++ <tt>__gxx_personality_v0</tt>) which
126 receives the context of the exception, an <i>exception structure</i> containing
127 the exception object type and value, and a reference to the exception table for
128 the current function.  The personality function for the current compile unit is
129 specified in a <i>common exception frame</i>.</p>
130
131 <p>The organization of an exception table is language dependent.  For C++, an
132 exception table is organized as a series of code ranges defining what to do if
133 an exception occurs in that range.  Typically, the information associated with a
134 range defines which types of exception objects (using C++ <i>type info</i>) that
135 are handled in that range, and an associated action that should take place.
136 Actions typically pass control to a <i>landing pad</i>.</p>
137
138 <p>A landing pad corresponds to the code found in the catch portion of a
139 try/catch sequence.  When execution resumes at a landing pad, it receives the
140 exception structure and a selector corresponding to the <i>type</i> of exception
141 thrown.  The selector is then used to determine which catch should actually
142 process the exception.</p>
143
144 </div>
145
146 <!-- ======================================================================= -->
147 <div class="doc_section">
148   <a name="codegen">LLVM Code Generation</a>
149 </div>
150
151 <div class="doc_text">
152
153 <p>At the time of this writing, only C++ exception handling support is available
154 in LLVM.  So the remainder of this document will be somewhat C++-centric.</p>
155
156 <p>From the C++ developers perspective, exceptions are defined in terms of the
157 <tt>throw</tt> and <tt>try/catch</tt> statements.  In this section we will
158 describe the implementation of llvm exception handling in terms of C++
159 examples.</p>
160
161 </div>
162
163 <!-- ======================================================================= -->
164 <div class="doc_subsection">
165   <a name="throw">Throw</a>
166 </div>
167
168 <div class="doc_text">
169
170 <p>Languages that support exception handling typically provide a <tt>throw</tt>
171 operation to initiate the exception process.  Internally, a throw operation
172 breaks down into two steps.  First, a request is made to allocate exception
173 space for an exception structure.  This structure needs to survive beyond the
174 current activation.  This structure will contain the type and value of the
175 object being thrown.  Second, a call is made to the runtime to raise the
176 exception, passing the exception structure as an argument.</p>
177
178 <p>In C++, the allocation of the exception structure is done by the
179 <tt>__cxa_allocate_exception</tt> runtime function.  The exception raising is
180 handled by <tt>__cxa_throw</tt>.  The type of the exception is represented using
181 a C++ RTTI type info structure.</p>
182
183 </div>
184
185 <!-- ======================================================================= -->
186 <div class="doc_subsection">
187   <a name="try_catch">Try/Catch</a>
188 </div>
189
190 <div class="doc_text">
191
192 <p>A call within the scope of a try statement can potentially raise an exception.
193 In those circumstances, the LLVM C++ front-end replaces the call with an
194 <tt>invoke</tt> instruction.  Unlike a call, the invoke has two potential
195 continuation points; where to continue when the call succeeds as per normal, and
196 where to continue if the call raises an exception, either by a throw or the
197 unwinding of a throw.</p>
198
199 <p>The term used to define a the place where an invoke continues after an
200 exception is called a <i>landing pad</i>.  LLVM landing pads are conceptually
201 alternative function entry points where a exception structure reference and a type
202 info index are passed in as arguments.  The landing pad saves the exception
203 structure reference and then proceeds to select the catch block that corresponds
204 to the type info of the exception object.</p>
205
206 <p>Two llvm intrinsic functions are used convey information about the landing
207 pad to the back end.</p>
208
209 <p><a href="#llvm_eh_exception"><tt>llvm.eh.exception</tt></a> takes no
210 arguments and returns a pointer to the exception structure.  This only returns a
211 sensible value if called after an invoke has branched to a landing pad.  Due to
212 codegen limitations, it must currently be called in the landing pad itself.</p>
213
214 <p><a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> takes a minimum of
215 three arguments.  The first argument is the reference to the exception
216 structure. The second argument is a reference to the personality function to be
217 used for this try catch sequence. Each of the remaining arguments is either a
218 reference to the type info for a catch statement,
219 a <a href="#throw_filters">filter</a> expression,
220 or the number zero representing a <a href="#cleanups">cleanup</a>.
221 The exception is tested against the arguments sequentially from first to last.
222 The result of the <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> is a
223 positive number if the exception matched a type info, a negative number if it matched
224 a filter, and zero if it matched a cleanup.  If nothing is matched, the behaviour of
225 the program is <a href="#restrictions">undefined</a>.
226 This only returns a sensible value if called after an invoke has branched to a
227 landing pad.  Due to codegen limitations, it must currently be called in the
228 landing pad itself.
229 If a type info matched then the selector value is the index of the type info in
230 the exception table, which can be obtained using the
231 <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic.</p>
232
233 <p>Once the landing pad has the type info selector, the code branches to the
234 code for the first catch.  The catch then checks the value of the type info
235 selector against the index of type info for that catch.  Since the type info
236 index is not known until all the type info have been gathered in the backend,
237 the catch code will call the <a
238 href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic to
239 determine the index for a given type info.  If the catch fails to match the
240 selector then control is passed on to the next catch. Note: Since the landing
241 pad will not be used if there is no match in the list of type info on the call
242 to <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>, then neither the
243 last catch nor <i>catch all</i> need to perform the the check against the
244 selector.</p>
245
246 <p>Finally, the entry and exit of catch code is bracketed with calls to
247 <tt>__cxa_begin_catch</tt> and <tt>__cxa_end_catch</tt>.
248 <tt>__cxa_begin_catch</tt> takes a exception structure reference as an argument
249 and returns the value of the exception object. <tt>__cxa_end_catch</tt>
250 takes a exception structure reference as an argument. This function clears the
251 exception from the exception space.  Note: a rethrow from within the catch may
252 replace this call with a <tt>__cxa_rethrow</tt>.</p>
253
254 </div>
255
256 <!-- ======================================================================= -->
257 <div class="doc_subsection">
258   <a name="cleanups">Cleanups</a>
259 </div>
260
261 <div class="doc_text">
262
263 <p>To handle destructors and cleanups in try code, control may not run directly
264 from a landing pad to the first catch.  Control may actually flow from the
265 landing pad to clean up code and then to the first catch.  Since the required
266 clean up for each invoke in a try may be different (ex., intervening
267 constructor), there may be several landing pads for a given try.  If cleanups
268 need to be run, the number zero should be passed as the last
269 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> argument.
270 However for C++ a <tt>null i8*</tt> <a href="#restrictions">must</a> be passed
271 instead.
272 </p>
273
274 </div>
275
276 <!-- ======================================================================= -->
277 <div class="doc_subsection">
278   <a name="throw_filters">Throw Filters</a>
279 </div>
280
281 <div class="doc_text">
282
283 <p>C++ allows the specification of which exception types can be thrown from
284 a function.  To represent this a top level landing pad may exist to filter out
285 invalid types.  To express this in LLVM code the landing pad will call <a
286 href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>.  The arguments are a
287 reference to the exception structure, a reference to the personality function,
288 the length of the filter expression (the number of type infos plus one),
289 followed by the type infos themselves.
290 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> will return a negative
291 value if the exception does not match any of the type infos.  If no match is
292 found then a call to <tt>__cxa_call_unexpected</tt> should be made, otherwise
293 <tt>_Unwind_Resume</tt>.  Each of these functions requires a reference to the
294 exception structure.  Note that the most general form of an
295 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> call can contain
296 any number of type infos, filter expressions and cleanups (though having more
297 than one cleanup is pointless).  The LLVM C++ front-end can generate such
298 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> calls due to inlining
299 creating nested exception handling scopes.</p>
300
301 </div>
302
303 <!-- ======================================================================= -->
304 <div class="doc_subsection">
305   <a name="restrictions">Restrictions</a>
306 </div>
307
308 <div class="doc_text">
309
310 <p>The semantics of the invoke instruction require that any exception that
311 unwinds through an invoke call should result in a branch to the invoke's unwind
312 label.  However such a branch will only happen if the
313 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> matches.
314 Thus in order to ensure correct operation, the front-end must only generate
315 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> calls that are
316 guaranteed to always match whatever exception unwinds through the invoke.
317 For most languages it is enough to pass zero, indicating the presence of
318 a <a href="#cleanups">cleanup</a>, as the last
319 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> argument.
320 However for C++ this is not sufficient, because the C++ personality function
321 will terminate the program if it detects that unwinding the exception only
322 results in matches with cleanups.  For C++ a <tt>null i8*</tt> should
323 be passed as the last
324 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> argument instead.
325 This is interpreted as a catch-all by the C++ personality function, and will
326 always match.
327 </p>
328
329 </div>
330
331 <!-- ======================================================================= -->
332 <div class="doc_section">
333   <a name="format_common_intrinsics">Exception Handling Intrinsics</a>
334 </div>
335
336 <div class="doc_text">
337
338 <p>LLVM uses several intrinsic functions (name prefixed with "llvm.eh") to
339 provide exception handling information at various points in generated code.</p>
340
341 </div>
342
343 <!-- ======================================================================= -->
344 <div class="doc_subsubsection">
345   <a name="llvm_eh_exception">llvm.eh.exception</a>
346 </div>
347
348 <div class="doc_text">
349 <pre>
350   i8* %<a href="#llvm_eh_exception">llvm.eh.exception</a>( )
351 </pre>
352
353 <p>This intrinsic returns a pointer to the exception structure.</p>
354
355 </div>
356
357 <!-- ======================================================================= -->
358 <div class="doc_subsubsection">
359   <a name="llvm_eh_selector">llvm.eh.selector</a>
360 </div>
361
362 <div class="doc_text">
363 <pre>
364   i32 %<a href="#llvm_eh_selector">llvm.eh.selector.i32</a>(i8*, i8*, i8*, ...)
365   i64 %<a href="#llvm_eh_selector">llvm.eh.selector.i64</a>(i8*, i8*, i8*, ...)
366 </pre>
367
368 <p>This intrinsic is used to compare the exception with the given type infos,
369 filters and cleanups.</p>
370
371 <p><a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> takes a minimum of
372 three arguments.  The first argument is the reference to the exception
373 structure. The second argument is a reference to the personality function to be
374 used for this try catch sequence. Each of the remaining arguments is either a
375 reference to the type info for a catch statement,
376 a <a href="#throw_filters">filter</a> expression,
377 or the number zero representing a <a href="#cleanups">cleanup</a>.
378 The exception is tested against the arguments sequentially from first to last.
379 The result of the <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> is a
380 positive number if the exception matched a type info, a negative number if it matched
381 a filter, and zero if it matched a cleanup.  If nothing is matched, the behaviour of
382 the program is <a href="#restrictions">undefined</a>.
383 If a type info matched then the selector value is the index of the type info in
384 the exception table, which can be obtained using the
385 <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic.</p>
386
387 </div>
388
389 <!-- ======================================================================= -->
390 <div class="doc_subsubsection">
391   <a name="llvm_eh_typeid_for">llvm.eh.typeid.for</a>
392 </div>
393
394 <div class="doc_text">
395 <pre>
396   i32 %<a href="#llvm_eh_typeid_for">llvm.eh.typeid.for.i32</a>(i8*)
397   i64 %<a href="#llvm_eh_typeid_for">llvm.eh.typeid.for.i64</a>(i8*)
398 </pre>
399
400 <p>This intrinsic returns the type info index in the exception table of the
401 current function.  This value can be used to compare against the result of <a
402 href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>.  The single argument is
403 a reference to a type info.</p>
404
405 </div>
406
407 <!-- ======================================================================= -->
408 <div class="doc_subsubsection">
409   <a name="llvm_eh_sjlj_setjmp">llvm.eh.sjlj.setjmp</a>
410 </div>
411
412 <div class="doc_text">
413 <pre>
414   i32 %<a href="#llvm_eh_sjlj_setjmp">llvm.eh.sjlj.setjmp</a>(i8*)
415 </pre>
416
417 <p>The SJLJ exception handling uses this intrinsic to force register saving
418 for the current function and to store the address of the following instruction
419 for use as a destination address by <a href="#llvm_eh_sjlj_longjmp">
420 <tt>llvm.eh.sjlj.longjmp</tt></a>. The buffer format and the overall functioning
421 of this intrinsic is compatible with the GCC <tt>__builtin_setjmp</tt> 
422 implementation, allowing code built with the two compilers to interoperate.</p>
423
424 <p>The single parameter is a pointer to a five word buffer in which the
425 calling context is saved. The front end places the frame pointer in the
426 first word, and the target implementation of this intrinsic should place the
427 destination address for a <a href="#llvm_eh_sjlj_longjmp"><tt>
428 llvm.eh.sjlj.longjmp</tt></a> in the second word. The following three words
429 are available for use in a target-specific manner.</p>
430
431 </div>
432
433 <!-- ======================================================================= -->
434 <div class="doc_subsubsection">
435   <a name="llvm_eh_sjlj_lsda">llvm.eh.sjlj.lsda</a>
436 </div>
437
438 <div class="doc_text">
439 <pre>
440   i8* %<a href="#llvm_eh_sjlj_lsda">llvm.eh.sjlj.lsda</a>( )
441 </pre>
442
443 <p>Used for SJLJ based exception handling, the <a href="#llvm_eh_sjlj_lsda">
444   <tt>llvm.eh.sjlj.lsda</tt></a> intrinsic returns the address of the Language
445 Specific Data Area (LSDA) for the current function. The SJLJ front-end code
446 stores this address in the exception handling function context for use by
447 the runtime.</p>
448
449 </div>
450
451 <!-- ======================================================================= -->
452 <div class="doc_subsubsection">
453   <a name="llvm_eh_sjlj_callsite">llvm.eh.sjlj.callsite</a>
454 </div>
455
456 <div class="doc_text">
457 <pre>
458   void %<a href="#llvm_eh_sjlj_callsite">llvm.eh.sjlj.callsite</a>(i32)
459 </pre>
460
461 <p>The SJLJ front-end allocates call site indices for invoke instrucitons. 
462 These values are passed to the back-end via the
463 <a href="#llvm_eh_sjlj_callsite"><tt>llvm.eh.sjlj.callsite</tt></a>
464 intrinsic, where they are used to build the LSDA call-site table.</p>
465
466 </div>
467
468 <!-- ======================================================================= -->
469 <div class="doc_section">
470   <a name="asm">Asm Table Formats</a>
471 </div>
472
473 <div class="doc_text">
474
475 <p>There are two tables that are used by the exception handling runtime to
476 determine which actions should take place when an exception is thrown.</p>
477
478 </div>
479
480 <!-- ======================================================================= -->
481 <div class="doc_subsection">
482   <a name="unwind_tables">Exception Handling Frame</a>
483 </div>
484
485 <div class="doc_text">
486
487 <p>An exception handling frame <tt>eh_frame</tt> is very similar to the unwind
488 frame used by dwarf debug info.  The frame contains all the information
489 necessary to tear down the current frame and restore the state of the prior
490 frame.  There is an exception handling frame for each function in a compile
491 unit, plus a common exception handling frame that defines information common to
492 all functions in the unit.</p>
493
494 <p>Todo - Table details here.</p>
495
496 </div>
497
498 <!-- ======================================================================= -->
499 <div class="doc_subsection">
500   <a name="exception_tables">Exception Tables</a>
501 </div>
502
503 <div class="doc_text">
504
505 <p>An exception table contains information about what actions to take when an
506 exception is thrown in a particular part of a function's code.  There is
507 one exception table per function except leaf routines and functions that have
508 only calls to non-throwing functions will not need an exception table.</p>
509
510 <p>Todo - Table details here.</p>
511
512 </div>
513
514 <!-- ======================================================================= -->
515 <div class="doc_section">
516   <a name="todo">ToDo</a>
517 </div>
518
519 <div class="doc_text">
520
521 <ol>
522
523 <li><p>Testing/Testing/Testing.</p></li>
524
525 </ol>
526
527 </div>
528
529 <!-- *********************************************************************** -->
530
531 <hr>
532 <address>
533   <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
534   src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
535   <a href="http://validator.w3.org/check/referer"><img
536   src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
537
538   <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
539   <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
540   Last modified: $Date$
541 </address>
542
543 </body>
544 </html>