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