blob: 24a34844c7b2768791eb5262f007cd4e59d782bc [file] [log] [blame]
Jim Laskeyd0d39b62007-03-14 19:29:42 +00001<!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>
Duncan Sands6590b042007-08-27 15:47:50 +000025 <li><a href="#cleanups">Cleanups</a></li>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000026 <li><a href="#throw_filters">Throw Filters</a></li>
Duncan Sands6590b042007-08-27 15:47:50 +000027 <li><a href="#restrictions">Restrictions</a></li>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000028 </ol></li>
Duncan Sands8036ca42007-03-30 12:22:09 +000029 <li><a href="#format_common_intrinsics">Exception Handling Intrinsics</a>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000030 <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>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000033 <li><a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a></li>
34 </ol></li>
35 <li><a href="#asm">Asm Table Formats</a>
36 <ol>
37 <li><a href="#unwind_tables">Exception Handling Frame</a></li>
38 <li><a href="#exception_tables">Exception Tables</a></li>
39 </ol></li>
40 <li><a href="#todo">ToDo</a></li>
41</ul>
42</td>
43</tr></table>
44
45<div class="doc_author">
46 <p>Written by <a href="mailto:jlaskey@mac.com">Jim Laskey</a></p>
47</div>
48
49
50<!-- *********************************************************************** -->
51<div class="doc_section"><a name="introduction">Introduction</a></div>
52<!-- *********************************************************************** -->
53
54<div class="doc_text">
55
56<p>This document is the central repository for all information pertaining to
57exception handling in LLVM. It describes the format that LLVM exception
58handling information takes, which is useful for those interested in creating
59front-ends or dealing directly with the information. Further, this document
60provides specific examples of what exception handling information is used for
61C/C++.</p>
62
63</div>
64
65<!-- ======================================================================= -->
66<div class="doc_subsection">
67 <a name="itanium">Itanium ABI Zero-cost Exception Handling</a>
68</div>
69
70<div class="doc_text">
71
72<p>Exception handling for most programming languages is designed to recover from
73conditions that rarely occur during general use of an application. To that end,
74exception handling should not interfere with the main flow of an
Bill Wendlingd40bc4a2007-09-22 10:17:08 +000075application's algorithm by performing checkpointing tasks such as saving
Jim Laskeyd0d39b62007-03-14 19:29:42 +000076the current pc or register state.</p>
77
78<p>The Itanium ABI Exception Handling Specification defines a methodology for
79providing outlying data in the form of exception tables without inlining
Bill Wendlingd40bc4a2007-09-22 10:17:08 +000080speculative exception handling code in the flow of an application's main
Jim Laskeyd0d39b62007-03-14 19:29:42 +000081algorithm. Thus, the specification is said to add "zero-cost" to the normal
82execution of an application.</p>
83
84<p>A more complete description of the Itanium ABI exception handling runtime
85support of can be found at <a
86href="http://www.codesourcery.com/cxx-abi/abi-eh.html">Itanium C++ ABI:
Bill Wendlingd40bc4a2007-09-22 10:17:08 +000087Exception Handling.</a> A description of the exception frame format can be found
88at <a href="http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-
Jim Laskeyd0d39b62007-03-14 19:29:42 +000089Core-generic/ehframechpt.html">Exception Frames</a>, with details of the Dwarf
90specification at <a href="http://www.eagercon.com/dwarf/dwarf3std.htm">Dwarf 3
91Standard.</a> A description for the C++ exception table formats can be found at
92<a href="http://www.codesourcery.com/cxx-abi/exceptions.pdf">Exception Handling
93Tables.</a></p>
94
95</div>
96
97<!-- ======================================================================= -->
98<div class="doc_subsection">
99 <a name="overview">Overview</a>
100</div>
101
102<div class="doc_text">
103
104<p>When an exception is thrown in llvm code, the runtime does a best effort to
105find a handler suited to process the circumstance.</p>
106
107<p>The runtime first attempts to find an <i>exception frame</i> corresponding to
108the function where the exception was thrown. If the programming language (ex.
109C++) supports exception handling, the exception frame contains a reference to an
110exception table describing how to process the exception. If the language (ex.
111C) does not support exception handling or if the exception needs to be forwarded
112to a prior activation, the exception frame contains information about how to
113unwind the current activation and restore the state of the prior activation.
114This process is repeated until the exception is handled. If the exception is
115not handled and no activations remain, then the application is terminated with
116an appropriate error message.</p>
117
118<p>Since different programming languages have different behaviors when handling
119exceptions, the exception handling ABI provides a mechanism for supplying
120<i>personalities.</i> An exception handling personality is defined by way of a
121<i>personality function</i> (ex. for C++ <tt>__gxx_personality_v0</tt>) which
122receives the context of the exception, an <i>exception structure</i> containing
Duncan Sandsfb0a64a2007-04-16 13:02:27 +0000123the exception object type and value, and a reference to the exception table for
124the current function. The personality function for the current compile unit is
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000125specified in a <i>common exception frame</i>.</p>
126
127<p>The organization of an exception table is language dependent. For C++, an
128exception table is organized as a series of code ranges defining what to do if
129an exception occurs in that range. Typically, the information associated with a
130range defines which types of exception objects (using C++ <i>type info</i>) that
131are handled in that range, and an associated action that should take place.
132Actions typically pass control to a <i>landing pad</i>.</p>
133
134<p>A landing pad corresponds to the code found in the catch portion of a
135try/catch sequence. When execution resumes at a landing pad, it receives the
136exception structure and a selector corresponding to the <i>type</i> of exception
137thrown. The selector is then used to determine which catch should actually
138process the exception.</p>
139
140</div>
141
142<!-- ======================================================================= -->
143<div class="doc_section">
144 <a name="codegen">LLVM Code Generation</a>
145</div>
146
147<div class="doc_text">
148
149<p>At the time of this writing, only C++ exception handling support is available
150in LLVM. So the remainder of this document will be somewhat C++-centric.</p>
151
152<p>From the C++ developers perspective, exceptions are defined in terms of the
153<tt>throw</tt> and <tt>try/catch</tt> statements. In this section we will
154describe the implementation of llvm exception handling in terms of C++
155examples.</p>
156
157</div>
158
159<!-- ======================================================================= -->
160<div class="doc_subsection">
161 <a name="throw">Throw</a>
162</div>
163
164<div class="doc_text">
165
166<p>Languages that support exception handling typically provide a <tt>throw</tt>
167operation to initiate the exception process. Internally, a throw operation
168breaks down into two steps. First, a request is made to allocate exception
169space for an exception structure. This structure needs to survive beyond the
170current activation. This structure will contain the type and value of the
171object being thrown. Second, a call is made to the runtime to raise the
172exception, passing the exception structure as an argument.</p>
173
174<p>In C++, the allocation of the exception structure is done by the
175<tt>__cxa_allocate_exception</tt> runtime function. The exception raising is
176handled by <tt>__cxa_throw</tt>. The type of the exception is represented using
177a C++ RTTI type info structure.</p>
178
179</div>
180
181<!-- ======================================================================= -->
182<div class="doc_subsection">
183 <a name="try_catch">Try/Catch</a>
184</div>
185
186<div class="doc_text">
187
Duncan Sandsb0a1cbf2007-04-14 12:30:27 +0000188<p>A call within the scope of a try statement can potentially raise an exception.
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000189In those circumstances, the LLVM C++ front-end replaces the call with an
190<tt>invoke</tt> instruction. Unlike a call, the invoke has two potential
191continuation points; where to continue when the call succeeds as per normal, and
192where to continue if the call raises an exception, either by a throw or the
193unwinding of a throw.</p>
194
195<p>The term used to define a the place where an invoke continues after an
196exception is called a <i>landing pad</i>. LLVM landing pads are conceptually
Duncan Sandsfb0a64a2007-04-16 13:02:27 +0000197alternative function entry points where a exception structure reference and a type
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000198info index are passed in as arguments. The landing pad saves the exception
199structure reference and then proceeds to select the catch block that corresponds
200to the type info of the exception object.</p>
201
202<p>Two llvm intrinsic functions are used convey information about the landing
203pad to the back end.</p>
204
205<p><a href="#llvm_eh_exception"><tt>llvm.eh.exception</tt></a> takes no
Duncan Sands6531d472008-12-29 15:27:32 +0000206arguments and returns a pointer to the exception structure. This only returns a
207sensible value if called after an invoke has branched to a landing pad. Due to
208codegen limitations, it must currently be called in the landing pad itself.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000209
210<p><a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> takes a minimum of
211three arguments. The first argument is the reference to the exception
212structure. The second argument is a reference to the personality function to be
Duncan Sandscf26d7c2007-07-04 20:52:51 +0000213used for this try catch sequence. Each of the remaining arguments is either a
Duncan Sands6590b042007-08-27 15:47:50 +0000214reference to the type info for a catch statement,
215a <a href="#throw_filters">filter</a> expression,
216or the number zero representing a <a href="#cleanups">cleanup</a>.
Duncan Sandscf26d7c2007-07-04 20:52:51 +0000217The exception is tested against the arguments sequentially from first to last.
Duncan Sands6590b042007-08-27 15:47:50 +0000218The result of the <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> is a
219positive number if the exception matched a type info, a negative number if it matched
220a filter, and zero if it matched a cleanup. If nothing is matched, the behaviour of
221the program is <a href="#restrictions">undefined</a>.
Duncan Sands6531d472008-12-29 15:27:32 +0000222This only returns a sensible value if called after an invoke has branched to a
223landing pad. Due to codegen limitations, it must currently be called in the
224landing pad itself.
Duncan Sands6590b042007-08-27 15:47:50 +0000225If a type info matched then the selector value is the index of the type info in
226the exception table, which can be obtained using the
227<a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000228
229<p>Once the landing pad has the type info selector, the code branches to the
230code for the first catch. The catch then checks the value of the type info
231selector against the index of type info for that catch. Since the type info
232index is not known until all the type info have been gathered in the backend,
233the catch code will call the <a
234href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic to
235determine the index for a given type info. If the catch fails to match the
236selector then control is passed on to the next catch. Note: Since the landing
237pad will not be used if there is no match in the list of type info on the call
238to <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>, then neither the
239last catch nor <i>catch all</i> need to perform the the check against the
240selector.</p>
241
242<p>Finally, the entry and exit of catch code is bracketed with calls to
243<tt>__cxa_begin_catch</tt> and <tt>__cxa_end_catch</tt>.
244<tt>__cxa_begin_catch</tt> takes a exception structure reference as an argument
Bill Wendlingd40bc4a2007-09-22 10:17:08 +0000245and returns the value of the exception object. <tt>__cxa_end_catch</tt>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000246takes a exception structure reference as an argument. This function clears the
247exception from the exception space. Note: a rethrow from within the catch may
248replace this call with a <tt>__cxa_rethrow</tt>.</p>
249
250</div>
251
252<!-- ======================================================================= -->
253<div class="doc_subsection">
Duncan Sands6590b042007-08-27 15:47:50 +0000254 <a name="cleanups">Cleanups</a>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000255</div>
256
257<div class="doc_text">
258
259<p>To handle destructors and cleanups in try code, control may not run directly
260from a landing pad to the first catch. Control may actually flow from the
261landing pad to clean up code and then to the first catch. Since the required
262clean up for each invoke in a try may be different (ex., intervening
Duncan Sands6590b042007-08-27 15:47:50 +0000263constructor), there may be several landing pads for a given try. If cleanups
264need to be run, the number zero should be passed as the last
265<a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> argument.
266However for C++ a <tt>null i8*</tt> <a href="#restrictions">must</a> be passed
267instead.
268</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000269
270</div>
271
272<!-- ======================================================================= -->
273<div class="doc_subsection">
274 <a name="throw_filters">Throw Filters</a>
275</div>
276
277<div class="doc_text">
278
Duncan Sands6531d472008-12-29 15:27:32 +0000279<p>C++ allows the specification of which exception types can be thrown from
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000280a function. To represent this a top level landing pad may exist to filter out
281invalid types. To express this in LLVM code the landing pad will call <a
Duncan Sands6531d472008-12-29 15:27:32 +0000282href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>. The arguments are a
283reference to the exception structure, a reference to the personality function,
284the length of the filter expression (the number of type infos plus one),
285followed by the type infos themselves.
Duncan Sandscf26d7c2007-07-04 20:52:51 +0000286<a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> will return a negative
287value if the exception does not match any of the type infos. If no match is
288found then a call to <tt>__cxa_call_unexpected</tt> should be made, otherwise
Duncan Sands6531d472008-12-29 15:27:32 +0000289<tt>_Unwind_Resume</tt>. Each of these functions requires a reference to the
290exception structure. Note that the most general form of an
291<a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> call can contain
292any number of type infos, filter expressions and cleanups (though having more
293than one cleanup is pointless). The LLVM C++ front-end can generate such
294<a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> calls due to inlining
295creating nested exception handling scopes.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000296
297</div>
298
299<!-- ======================================================================= -->
Duncan Sands6590b042007-08-27 15:47:50 +0000300<div class="doc_subsection">
301 <a name="restrictions">Restrictions</a>
302</div>
303
304<div class="doc_text">
305
306<p>The semantics of the invoke instruction require that any exception that
307unwinds through an invoke call should result in a branch to the invoke's unwind
308label. However such a branch will only happen if the
309<a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> matches.
310Thus in order to ensure correct operation, the front-end must only generate
311<a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> calls that are
312guaranteed to always match whatever exception unwinds through the invoke.
313For most languages it is enough to pass zero, indicating the presence of
314a <a href="#cleanups">cleanup</a>, as the last
315<a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> argument.
316However for C++ this is not sufficient, because the C++ personality function
317will terminate the program if it detects that unwinding the exception only
318results in matches with cleanups. For C++ a <tt>null i8*</tt> should
319be passed as the last
320<a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> argument instead.
321This is interpreted as a catch-all by the C++ personality function, and will
322always match.
323</p>
324
325</div>
326
327<!-- ======================================================================= -->
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000328<div class="doc_section">
Duncan Sands8036ca42007-03-30 12:22:09 +0000329 <a name="format_common_intrinsics">Exception Handling Intrinsics</a>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000330</div>
331
332<div class="doc_text">
333
334<p>LLVM uses several intrinsic functions (name prefixed with "llvm.eh") to
335provide exception handling information at various points in generated code.</p>
336
337</div>
338
339<!-- ======================================================================= -->
340<div class="doc_subsubsection">
341 <a name="llvm_eh_exception">llvm.eh.exception</a>
342</div>
343
344<div class="doc_text">
345<pre>
346 i8* %<a href="#llvm_eh_exception">llvm.eh.exception</a>( )
347</pre>
348
Duncan Sands6531d472008-12-29 15:27:32 +0000349<p>This intrinsic returns a pointer to the exception structure.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000350
351</div>
352
353<!-- ======================================================================= -->
354<div class="doc_subsubsection">
355 <a name="llvm_eh_selector">llvm.eh.selector</a>
356</div>
357
358<div class="doc_text">
359<pre>
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +0000360 i32 %<a href="#llvm_eh_selector">llvm.eh.selector.i32</a>(i8*, i8*, i8*, ...)
361 i64 %<a href="#llvm_eh_selector">llvm.eh.selector.i64</a>(i8*, i8*, i8*, ...)
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000362</pre>
363
Duncan Sands6531d472008-12-29 15:27:32 +0000364<p>This intrinsic is used to compare the exception with the given type infos,
365filters and cleanups.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000366
367<p><a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> takes a minimum of
368three arguments. The first argument is the reference to the exception
369structure. The second argument is a reference to the personality function to be
Duncan Sands6590b042007-08-27 15:47:50 +0000370used for this try catch sequence. Each of the remaining arguments is either a
371reference to the type info for a catch statement,
372a <a href="#throw_filters">filter</a> expression,
373or the number zero representing a <a href="#cleanups">cleanup</a>.
Duncan Sandscf26d7c2007-07-04 20:52:51 +0000374The exception is tested against the arguments sequentially from first to last.
Duncan Sands6590b042007-08-27 15:47:50 +0000375The result of the <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> is a
376positive number if the exception matched a type info, a negative number if it matched
377a filter, and zero if it matched a cleanup. If nothing is matched, the behaviour of
378the program is <a href="#restrictions">undefined</a>.
379If a type info matched then the selector value is the index of the type info in
380the exception table, which can be obtained using the
381<a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000382
383</div>
384
385<!-- ======================================================================= -->
386<div class="doc_subsubsection">
387 <a name="llvm_eh_typeid_for">llvm.eh.typeid.for</a>
388</div>
389
390<div class="doc_text">
391<pre>
Anton Korobeynikov8806c7b2007-09-07 11:39:35 +0000392 i32 %<a href="#llvm_eh_typeid_for">llvm.eh.typeid.for.i32</a>(i8*)
393 i64 %<a href="#llvm_eh_typeid_for">llvm.eh.typeid.for.i64</a>(i8*)
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000394</pre>
395
396<p>This intrinsic returns the type info index in the exception table of the
397current function. This value can be used to compare against the result of <a
398href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>. The single argument is
399a reference to a type info.</p>
400
401</div>
402
403<!-- ======================================================================= -->
404<div class="doc_section">
405 <a name="asm">Asm Table Formats</a>
406</div>
407
408<div class="doc_text">
409
410<p>There are two tables that are used by the exception handling runtime to
411determine which actions should take place when an exception is thrown.</p>
412
413</div>
414
415<!-- ======================================================================= -->
416<div class="doc_subsection">
417 <a name="unwind_tables">Exception Handling Frame</a>
418</div>
419
420<div class="doc_text">
421
422<p>An exception handling frame <tt>eh_frame</tt> is very similar to the unwind
423frame used by dwarf debug info. The frame contains all the information
424necessary to tear down the current frame and restore the state of the prior
425frame. There is an exception handling frame for each function in a compile
426unit, plus a common exception handling frame that defines information common to
427all functions in the unit.</p>
428
429<p>Todo - Table details here.</p>
430
431</div>
432
433<!-- ======================================================================= -->
434<div class="doc_subsection">
435 <a name="exception_tables">Exception Tables</a>
436</div>
437
438<div class="doc_text">
439
440<p>An exception table contains information about what actions to take when an
Bill Wendlingd40bc4a2007-09-22 10:17:08 +0000441exception is thrown in a particular part of a function's code. There is
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000442one exception table per function except leaf routines and functions that have
443only calls to non-throwing functions will not need an exception table.</p>
444
445<p>Todo - Table details here.</p>
446
447</div>
448
449<!-- ======================================================================= -->
450<div class="doc_section">
451 <a name="todo">ToDo</a>
452</div>
453
454<div class="doc_text">
455
456<ol>
457
Bill Wendlingd40bc4a2007-09-22 10:17:08 +0000458<li><p>Testing/Testing/Testing.</p></li>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000459
460</ol>
461
462</div>
463
464<!-- *********************************************************************** -->
465
466<hr>
467<address>
468 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000469 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000470 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000471 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000472
473 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
474 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
475 Last modified: $Date$
476</address>
477
478</body>
479</html>