blob: 247448d2554e9975a901c6d4e8d4f48e99f66415 [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>
Bill Wendlingc4f661e2009-08-15 08:56:09 +00006 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Eric Christopher61f69782009-09-09 01:44:53 +00007 <meta name="description"
Bill Wendlingc4f661e2009-08-15 08:56:09 +00008 content="Exception Handling in LLVM.">
Jim Laskeyd0d39b62007-03-14 19:29:42 +00009 <link rel="stylesheet" href="llvm.css" type="text/css">
10</head>
Bill Wendlingc4f661e2009-08-15 08:56:09 +000011
Jim Laskeyd0d39b62007-03-14 19:29:42 +000012<body>
13
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000014<h1>Exception Handling in LLVM</h1>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000015
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>
Jim Grosbach00484d12009-08-22 01:42:39 +000023 <li><a href="#sjlj">Setjmp/Longjmp Exception Handling</a></li>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000024 <li><a href="#overview">Overview</a></li>
25 </ol></li>
26 <li><a href="#codegen">LLVM Code Generation</a>
27 <ol>
28 <li><a href="#throw">Throw</a></li>
29 <li><a href="#try_catch">Try/Catch</a></li>
Duncan Sands6590b042007-08-27 15:47:50 +000030 <li><a href="#cleanups">Cleanups</a></li>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000031 <li><a href="#throw_filters">Throw Filters</a></li>
Duncan Sands6590b042007-08-27 15:47:50 +000032 <li><a href="#restrictions">Restrictions</a></li>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000033 </ol></li>
Duncan Sands8036ca42007-03-30 12:22:09 +000034 <li><a href="#format_common_intrinsics">Exception Handling Intrinsics</a>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000035 <ol>
36 <li><a href="#llvm_eh_exception"><tt>llvm.eh.exception</tt></a></li>
37 <li><a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a></li>
John McCalld7c10862011-05-28 07:45:59 +000038 <li><a href="#llvm_eh_resume"><tt>llvm.eh.resume</tt></a></li>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000039 <li><a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a></li>
Jim Grosbachf9570122009-05-14 00:46:35 +000040 <li><a href="#llvm_eh_sjlj_setjmp"><tt>llvm.eh.sjlj.setjmp</tt></a></li>
41 <li><a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a></li>
Jim Grosbach1b747ad2009-08-11 00:09:57 +000042 <li><a href="#llvm_eh_sjlj_lsda"><tt>llvm.eh.sjlj.lsda</tt></a></li>
Jim Grosbachca752c92010-01-28 01:45:32 +000043 <li><a href="#llvm_eh_sjlj_callsite"><tt>llvm.eh.sjlj.callsite</tt></a></li>
Jim Grosbache4ad3872010-10-19 23:27:08 +000044 <li><a href="#llvm_eh_sjlj_dispatchsetup"><tt>llvm.eh.sjlj.dispatchsetup</tt></a></li>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000045 </ol></li>
46 <li><a href="#asm">Asm Table Formats</a>
47 <ol>
48 <li><a href="#unwind_tables">Exception Handling Frame</a></li>
49 <li><a href="#exception_tables">Exception Tables</a></li>
50 </ol></li>
51 <li><a href="#todo">ToDo</a></li>
52</ul>
53</td>
54</tr></table>
55
56<div class="doc_author">
57 <p>Written by <a href="mailto:jlaskey@mac.com">Jim Laskey</a></p>
58</div>
59
60
61<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000062<h2><a name="introduction">Introduction</a></h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000063<!-- *********************************************************************** -->
64
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000065<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000066
67<p>This document is the central repository for all information pertaining to
Bill Wendlingc4f661e2009-08-15 08:56:09 +000068 exception handling in LLVM. It describes the format that LLVM exception
69 handling information takes, which is useful for those interested in creating
70 front-ends or dealing directly with the information. Further, this document
71 provides specific examples of what exception handling information is used for
72 in C/C++.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000073
Jim Laskeyd0d39b62007-03-14 19:29:42 +000074<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000075<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000076 <a name="itanium">Itanium ABI Zero-cost Exception Handling</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000077</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000078
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000079<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000080
81<p>Exception handling for most programming languages is designed to recover from
Bill Wendlingc4f661e2009-08-15 08:56:09 +000082 conditions that rarely occur during general use of an application. To that
83 end, exception handling should not interfere with the main flow of an
84 application's algorithm by performing checkpointing tasks, such as saving the
85 current pc or register state.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000086
87<p>The Itanium ABI Exception Handling Specification defines a methodology for
Bill Wendlingc4f661e2009-08-15 08:56:09 +000088 providing outlying data in the form of exception tables without inlining
89 speculative exception handling code in the flow of an application's main
90 algorithm. Thus, the specification is said to add "zero-cost" to the normal
91 execution of an application.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000092
93<p>A more complete description of the Itanium ABI exception handling runtime
Bill Wendlingc4f661e2009-08-15 08:56:09 +000094 support of can be found at
95 <a href="http://www.codesourcery.com/cxx-abi/abi-eh.html">Itanium C++ ABI:
96 Exception Handling</a>. A description of the exception frame format can be
97 found at
98 <a href="http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html">Exception
99 Frames</a>, with details of the DWARF 3 specification at
100 <a href="http://www.eagercon.com/dwarf/dwarf3std.htm">DWARF 3 Standard</a>.
101 A description for the C++ exception table formats can be found at
102 <a href="http://www.codesourcery.com/cxx-abi/exceptions.pdf">Exception Handling
103 Tables</a>.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000104
105</div>
106
107<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000108<h3>
Jim Grosbach00484d12009-08-22 01:42:39 +0000109 <a name="sjlj">Setjmp/Longjmp Exception Handling</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000110</h3>
Jim Grosbach00484d12009-08-22 01:42:39 +0000111
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000112<div>
Jim Grosbach00484d12009-08-22 01:42:39 +0000113
114<p>Setjmp/Longjmp (SJLJ) based exception handling uses LLVM intrinsics
115 <a href="#llvm_eh_sjlj_setjmp"><tt>llvm.eh.sjlj.setjmp</tt></a> and
116 <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a> to
117 handle control flow for exception handling.</p>
118
119<p>For each function which does exception processing, be it try/catch blocks
120 or cleanups, that function registers itself on a global frame list. When
121 exceptions are being unwound, the runtime uses this list to identify which
122 functions need processing.<p>
123
124<p>Landing pad selection is encoded in the call site entry of the function
125 context. The runtime returns to the function via
126 <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a>, where
127 a switch table transfers control to the appropriate landing pad based on
128 the index stored in the function context.</p>
129
130<p>In contrast to DWARF exception handling, which encodes exception regions
131 and frame information in out-of-line tables, SJLJ exception handling
132 builds and removes the unwind frame context at runtime. This results in
133 faster exception handling at the expense of slower execution when no
134 exceptions are thrown. As exceptions are, by their nature, intended for
135 uncommon code paths, DWARF exception handling is generally preferred to
136 SJLJ.</p>
137</div>
138
139<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000140<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000141 <a name="overview">Overview</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000142</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000143
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000144<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000145
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000146<p>When an exception is thrown in LLVM code, the runtime does its best to find a
147 handler suited to processing the circumstance.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000148
149<p>The runtime first attempts to find an <i>exception frame</i> corresponding to
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000150 the function where the exception was thrown. If the programming language
151 (e.g. C++) supports exception handling, the exception frame contains a
152 reference to an exception table describing how to process the exception. If
153 the language (e.g. C) does not support exception handling, or if the
154 exception needs to be forwarded to a prior activation, the exception frame
155 contains information about how to unwind the current activation and restore
156 the state of the prior activation. This process is repeated until the
157 exception is handled. If the exception is not handled and no activations
158 remain, then the application is terminated with an appropriate error
159 message.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000160
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000161<p>Because different programming languages have different behaviors when
162 handling exceptions, the exception handling ABI provides a mechanism for
163 supplying <i>personalities.</i> An exception handling personality is defined
164 by way of a <i>personality function</i> (e.g. <tt>__gxx_personality_v0</tt>
165 in C++), which receives the context of the exception, an <i>exception
166 structure</i> containing the exception object type and value, and a reference
167 to the exception table for the current function. The personality function
168 for the current compile unit is specified in a <i>common exception
169 frame</i>.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000170
171<p>The organization of an exception table is language dependent. For C++, an
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000172 exception table is organized as a series of code ranges defining what to do
173 if an exception occurs in that range. Typically, the information associated
174 with a range defines which types of exception objects (using C++ <i>type
175 info</i>) that are handled in that range, and an associated action that
176 should take place. Actions typically pass control to a <i>landing
177 pad</i>.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000178
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000179<p>A landing pad corresponds to the code found in the <i>catch</i> portion of
180 a <i>try</i>/<i>catch</i> sequence. When execution resumes at a landing
181 pad, it receives the exception structure and a selector corresponding to
182 the <i>type</i> of exception thrown. The selector is then used to determine
183 which <i>catch</i> should actually process the exception.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000184
185</div>
186
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000187</div>
188
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000189<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000190<h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000191 <a name="codegen">LLVM Code Generation</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000192</h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000193
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000194<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000195
196<p>At the time of this writing, only C++ exception handling support is available
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000197 in LLVM. So the remainder of this document will be somewhat C++-centric.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000198
199<p>From the C++ developers perspective, exceptions are defined in terms of the
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000200 <tt>throw</tt> and <tt>try</tt>/<tt>catch</tt> statements. In this section
201 we will describe the implementation of LLVM exception handling in terms of
202 C++ examples.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000203
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000204<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000205<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000206 <a name="throw">Throw</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000207</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000208
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000209<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000210
211<p>Languages that support exception handling typically provide a <tt>throw</tt>
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000212 operation to initiate the exception process. Internally, a throw operation
213 breaks down into two steps. First, a request is made to allocate exception
214 space for an exception structure. This structure needs to survive beyond the
215 current activation. This structure will contain the type and value of the
216 object being thrown. Second, a call is made to the runtime to raise the
217 exception, passing the exception structure as an argument.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000218
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000219<p>In C++, the allocation of the exception structure is done by
220 the <tt>__cxa_allocate_exception</tt> runtime function. The exception
221 raising is handled by <tt>__cxa_throw</tt>. The type of the exception is
222 represented using a C++ RTTI structure.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000223
224</div>
225
226<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000227<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000228 <a name="try_catch">Try/Catch</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000229</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000230
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000231<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000232
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000233<p>A call within the scope of a <i>try</i> statement can potentially raise an
234 exception. In those circumstances, the LLVM C++ front-end replaces the call
235 with an <tt>invoke</tt> instruction. Unlike a call, the <tt>invoke</tt> has
236 two potential continuation points: where to continue when the call succeeds
237 as per normal; and where to continue if the call raises an exception, either
238 by a throw or the unwinding of a throw.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000239
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000240<p>The term used to define a the place where an <tt>invoke</tt> continues after
241 an exception is called a <i>landing pad</i>. LLVM landing pads are
242 conceptually alternative function entry points where an exception structure
243 reference and a type info index are passed in as arguments. The landing pad
244 saves the exception structure reference and then proceeds to select the catch
245 block that corresponds to the type info of the exception object.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000246
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000247<p>Two LLVM intrinsic functions are used to convey information about the landing
248 pad to the back end.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000249
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000250<ol>
251 <li><a href="#llvm_eh_exception"><tt>llvm.eh.exception</tt></a> takes no
252 arguments and returns a pointer to the exception structure. This only
253 returns a sensible value if called after an <tt>invoke</tt> has branched
254 to a landing pad. Due to code generation limitations, it must currently
255 be called in the landing pad itself.</li>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000256
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000257 <li><a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> takes a minimum
258 of three arguments. The first argument is the reference to the exception
259 structure. The second argument is a reference to the personality function
260 to be used for this <tt>try</tt>/<tt>catch</tt> sequence. Each of the
261 remaining arguments is either a reference to the type info for
262 a <tt>catch</tt> statement, a <a href="#throw_filters">filter</a>
263 expression, or the number zero (<tt>0</tt>) representing
264 a <a href="#cleanups">cleanup</a>. The exception is tested against the
265 arguments sequentially from first to last. The result of
266 the <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> is a
267 positive number if the exception matched a type info, a negative number if
268 it matched a filter, and zero if it matched a cleanup. If nothing is
269 matched, the behaviour of the program
270 is <a href="#restrictions">undefined</a>. This only returns a sensible
271 value if called after an <tt>invoke</tt> has branched to a landing pad.
272 Due to codegen limitations, it must currently be called in the landing pad
273 itself. If a type info matched, then the selector value is the index of
274 the type info in the exception table, which can be obtained using the
275 <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a>
276 intrinsic.</li>
277</ol>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000278
279<p>Once the landing pad has the type info selector, the code branches to the
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000280 code for the first catch. The catch then checks the value of the type info
281 selector against the index of type info for that catch. Since the type info
282 index is not known until all the type info have been gathered in the backend,
283 the catch code will call the
284 <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic
285 to determine the index for a given type info. If the catch fails to match
286 the selector then control is passed on to the next catch. Note: Since the
287 landing pad will not be used if there is no match in the list of type info on
288 the call to <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>, then
289 neither the last catch nor <i>catch all</i> need to perform the check
290 against the selector.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000291
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000292<p>Finally, the entry and exit of catch code is bracketed with calls
293 to <tt>__cxa_begin_catch</tt> and <tt>__cxa_end_catch</tt>.</p>
294
295<ul>
296 <li><tt>__cxa_begin_catch</tt> takes a exception structure reference as an
297 argument and returns the value of the exception object.</li>
298
Bill Wendling169e1b02009-09-10 22:14:16 +0000299 <li><tt>__cxa_end_catch</tt> takes no arguments. This function:<br><br>
300 <ol>
Bill Wendling808b9ce2009-09-10 22:12:50 +0000301 <li>Locates the most recently caught exception and decrements its handler
302 count,</li>
303 <li>Removes the exception from the "caught" stack if the handler count
304 goes to zero, and</li>
305 <li>Destroys the exception if the handler count goes to zero, and the
306 exception was not re-thrown by throw.</li>
Bill Wendling169e1b02009-09-10 22:14:16 +0000307 </ol>
Bill Wendling808b9ce2009-09-10 22:12:50 +0000308 <p>Note: a rethrow from within the catch may replace this call with
309 a <tt>__cxa_rethrow</tt>.</p></li>
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000310</ul>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000311
312</div>
313
314<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000315<h3>
Duncan Sands6590b042007-08-27 15:47:50 +0000316 <a name="cleanups">Cleanups</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000317</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000318
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000319<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000320
John McCalld7c10862011-05-28 07:45:59 +0000321<p>A cleanup is extra code which needs to be run as part of unwinding
322 a scope. C++ destructors are a prominent example, but other
323 languages and language extensions provide a variety of different
324 kinds of cleanup. In general, a landing pad may need to run
325 arbitrary amounts of cleanup code before actually entering a catch
326 block. To indicate the presence of cleanups, a landing pad's call
327 to <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> should
328 end with the argument <tt>i32 0</tt>; otherwise, the unwinder will
329 not stop at the landing pad if there are no catches or filters that
330 require it to.</p>
331
332<p>Do not allow a new exception to propagate out of the execution of a
333 cleanup. This can corrupt the internal state of the unwinder.
334 Different languages describe different high-level semantics for
335 these situations: for example, C++ requires that the process be
336 terminated, whereas Ada cancels both exceptions and throws a third.</p>
337
338<p>When all cleanups have completed, if the exception is not handled
339 by the current function, resume unwinding by calling the
340 <a href="#llvm_eh_resume"><tt>llvm.eh.resume</tt></a> intrinsic,
341 passing in the results of <tt>llvm.eh.exception</tt> and
342 <tt>llvm.eh.selector</tt> for the original landing pad.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000343
344</div>
345
346<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000347<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000348 <a name="throw_filters">Throw Filters</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000349</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000350
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000351<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000352
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000353<p>C++ allows the specification of which exception types can be thrown from a
354 function. To represent this a top level landing pad may exist to filter out
355 invalid types. To express this in LLVM code the landing pad will
356 call <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>. The
357 arguments are a reference to the exception structure, a reference to the
358 personality function, the length of the filter expression (the number of type
359 infos plus one), followed by the type infos themselves.
360 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> will return a
361 negative value if the exception does not match any of the type infos. If no
362 match is found then a call to <tt>__cxa_call_unexpected</tt> should be made,
363 otherwise <tt>_Unwind_Resume</tt>. Each of these functions requires a
364 reference to the exception structure. Note that the most general form of an
365 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> call can contain
366 any number of type infos, filter expressions and cleanups (though having more
367 than one cleanup is pointless). The LLVM C++ front-end can generate such
368 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> calls due to
369 inlining creating nested exception handling scopes.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000370
371</div>
372
373<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000374<h3>
Duncan Sands6590b042007-08-27 15:47:50 +0000375 <a name="restrictions">Restrictions</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000376</h3>
Duncan Sands6590b042007-08-27 15:47:50 +0000377
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000378<div>
Duncan Sands6590b042007-08-27 15:47:50 +0000379
John McCalld7c10862011-05-28 07:45:59 +0000380<p>The unwinder delegates the decision of whether to stop in a call
381 frame to that call frame's language-specific personality function.
382 Not all personalities functions guarantee that they will stop to
383 perform cleanups: for example, the GNU C++ personality doesn't do
384 so unless the exception is actually caught somewhere further up the
385 stack. When using this personality to implement EH for a language
386 that guarantees that cleanups will always be run, be sure to
387 indicate a catch-all in the
388 <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> call
389 rather than just cleanups.</p>
390
391<p>In order for inlining to behave correctly, landing pads must be
392 prepared to handle selector results that they did not originally
393 advertise. Suppose that a function catches exceptions of
394 type <tt>A</tt>, and it's inlined into a function that catches
395 exceptions of type <tt>B</tt>. The inliner will update the
396 selector for the inlined landing pad to include the fact
397 that <tt>B</tt> is caught. If that landing pad assumes that it
398 will only be entered to catch an <tt>A</tt>, it's in for a rude
399 surprise. Consequently, landing pads must test for the selector
400 results they understand and then resume exception propagation
401 with the <a href="#llvm_eh_resume"><tt>llvm.eh.resume</tt></a>
402 intrinsic if none of the conditions match.</p>
Duncan Sands6590b042007-08-27 15:47:50 +0000403
404</div>
405
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000406</div>
407
Duncan Sands6590b042007-08-27 15:47:50 +0000408<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000409<h2>
Duncan Sands8036ca42007-03-30 12:22:09 +0000410 <a name="format_common_intrinsics">Exception Handling Intrinsics</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000411</h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000412
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000413<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000414
415<p>LLVM uses several intrinsic functions (name prefixed with "llvm.eh") to
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000416 provide exception handling information at various points in generated
417 code.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000418
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000419<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000420<h4>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000421 <a name="llvm_eh_exception">llvm.eh.exception</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000422</h4>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000423
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000424<div>
Bill Wendling3cf4ffd2009-08-15 20:07:42 +0000425
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000426<pre>
Dan Gohman3dfb3cf2010-05-28 17:07:41 +0000427 i8* %<a href="#llvm_eh_exception">llvm.eh.exception</a>()
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000428</pre>
429
Duncan Sands6531d472008-12-29 15:27:32 +0000430<p>This intrinsic returns a pointer to the exception structure.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000431
432</div>
433
434<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000435<h4>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000436 <a name="llvm_eh_selector">llvm.eh.selector</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000437</h4>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000438
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000439<div>
Bill Wendling3cf4ffd2009-08-15 20:07:42 +0000440
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000441<pre>
Bill Wendling6bfd4f42010-12-09 23:05:48 +0000442 i32 %<a href="#llvm_eh_selector">llvm.eh.selector</a>(i8*, i8*, ...)
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000443</pre>
444
Duncan Sands6531d472008-12-29 15:27:32 +0000445<p>This intrinsic is used to compare the exception with the given type infos,
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000446 filters and cleanups.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000447
John McCalld7c10862011-05-28 07:45:59 +0000448<p><a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> takes a
449 minimum of three arguments. The first argument is the reference to
450 the exception structure. The second argument is a reference to the
451 personality function to be used for this try catch sequence. Each
452 of the remaining arguments is either a reference to the type info
453 for a catch statement, a <a href="#throw_filters">filter</a>
454 expression, or the number zero representing
455 a <a href="#cleanups">cleanup</a>. The exception is tested against
456 the arguments sequentially from first to last. The result of
457 the <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> is a
458 positive number if the exception matched a type info, a negative
459 number if it matched a filter, and zero if it matched a cleanup.
460 If nothing is matched, or if only a cleanup is matched, different
461 personality functions may or may not cause control to stop at the
462 landing pad; see <a href="#restrictions">the restrictions</a> for
463 more information. If a type info matched then the selector value
464 is the index of the type info in the exception table, which can be
465 obtained using the
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000466 <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000467
John McCalld7c10862011-05-28 07:45:59 +0000468<p>If a landing pad containing a call to <tt>llvm.eh.selector</tt> is
469 inlined into an <tt>invoke</tt> instruction, the selector arguments
470 for the outer landing pad are appended to those of the inlined
471 landing pad. Consequently, landing pads must be written to ignore
472 selector values that they did not originally advertise.</p>
473
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000474</div>
475
476<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000477<h4>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000478 <a name="llvm_eh_typeid_for">llvm.eh.typeid.for</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000479</h4>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000480
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000481<div>
Bill Wendlingbf230bf2009-08-15 20:08:04 +0000482
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000483<pre>
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000484 i32 %<a href="#llvm_eh_typeid_for">llvm.eh.typeid.for</a>(i8*)
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000485</pre>
486
487<p>This intrinsic returns the type info index in the exception table of the
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000488 current function. This value can be used to compare against the result
489 of <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>. The single
490 argument is a reference to a type info.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000491
492</div>
493
494<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000495<h4>
John McCalld7c10862011-05-28 07:45:59 +0000496 <a name="llvm_eh_resume">llvm.eh.resume</a>
497</h4>
498
499<div>
500
501<pre>
502 void %<a href="#llvm_eh_resume">llvm.eh.resume</a>(i8*, i32) noreturn
503</pre>
504
505<p>This intrinsic is used to resume propagation of an exception after
506 landing at a landing pad. The first argument should be the result
507 of <a href="#llvm_eh_exception">llvm.eh.exception</a> for that
508 landing pad, and the second argument should be the result of
509 <a href="#llvm_eh_selector">llvm.eh.selector</a>. When a call to
510 this intrinsic is inlined into an invoke, the call is transformed
511 into a branch to the invoke's unwind destination, using its
512 arguments in place of the calls
513 to <a href="#llvm_eh_exception">llvm.eh.exception</a> and
514 <a href="#llvm_eh_selector">llvm.eh.selector</a> there.</p>
515
516<p>This intrinsic is not implicitly <tt>nounwind</tt>; calls to it
517 will always throw. It may not be invoked.</p>
518
519</div>
520
521<!-- ======================================================================= -->
522<h4>
Jim Grosbachf9570122009-05-14 00:46:35 +0000523 <a name="llvm_eh_sjlj_setjmp">llvm.eh.sjlj.setjmp</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000524</h4>
Jim Grosbachf9570122009-05-14 00:46:35 +0000525
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000526<div>
Bill Wendling3cf4ffd2009-08-15 20:07:42 +0000527
Jim Grosbachf9570122009-05-14 00:46:35 +0000528<pre>
Bill Wendling3cf4ffd2009-08-15 20:07:42 +0000529 i32 %<a href="#llvm_eh_sjlj_setjmp">llvm.eh.sjlj.setjmp</a>(i8*)
Jim Grosbachf9570122009-05-14 00:46:35 +0000530</pre>
531
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000532<p>The SJLJ exception handling uses this intrinsic to force register saving for
533 the current function and to store the address of the following instruction
534 for use as a destination address by <a href="#llvm_eh_sjlj_longjmp">
535 <tt>llvm.eh.sjlj.longjmp</tt></a>. The buffer format and the overall
536 functioning of this intrinsic is compatible with the GCC
537 <tt>__builtin_setjmp</tt> implementation, allowing code built with the
538 two compilers to interoperate.</p>
Jim Grosbachf9570122009-05-14 00:46:35 +0000539
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000540<p>The single parameter is a pointer to a five word buffer in which the calling
541 context is saved. The front end places the frame pointer in the first word,
542 and the target implementation of this intrinsic should place the destination
543 address for a
544 <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a> in the
545 second word. The following three words are available for use in a
546 target-specific manner.</p>
Jim Grosbachf9570122009-05-14 00:46:35 +0000547
Benjamin Kramere15192b2009-08-05 15:42:44 +0000548</div>
549
Jim Grosbachf9570122009-05-14 00:46:35 +0000550<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000551<h4>
Jim Grosbach90346e22010-05-26 16:21:41 +0000552 <a name="llvm_eh_sjlj_longjmp">llvm.eh.sjlj.longjmp</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000553</h4>
Jim Grosbach90346e22010-05-26 16:21:41 +0000554
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000555<div>
Jim Grosbach90346e22010-05-26 16:21:41 +0000556
557<pre>
558 void %<a href="#llvm_eh_sjlj_longjmp">llvm.eh.sjlj.setjmp</a>(i8*)
559</pre>
560
561<p>The <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a>
562 intrinsic is used to implement <tt>__builtin_longjmp()</tt> for SJLJ
563 style exception handling. The single parameter is a pointer to a
564 buffer populated by <a href="#llvm_eh_sjlj_setjmp">
565 <tt>llvm.eh.sjlj.setjmp</tt></a>. The frame pointer and stack pointer
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000566 are restored from the buffer, then control is transferred to the
Jim Grosbach90346e22010-05-26 16:21:41 +0000567 destination address.</p>
568
569</div>
570<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000571<h4>
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000572 <a name="llvm_eh_sjlj_lsda">llvm.eh.sjlj.lsda</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000573</h4>
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000574
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000575<div>
Bill Wendling3cf4ffd2009-08-15 20:07:42 +0000576
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000577<pre>
Dan Gohman3dfb3cf2010-05-28 17:07:41 +0000578 i8* %<a href="#llvm_eh_sjlj_lsda">llvm.eh.sjlj.lsda</a>()
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000579</pre>
580
581<p>Used for SJLJ based exception handling, the <a href="#llvm_eh_sjlj_lsda">
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000582 <tt>llvm.eh.sjlj.lsda</tt></a> intrinsic returns the address of the Language
583 Specific Data Area (LSDA) for the current function. The SJLJ front-end code
584 stores this address in the exception handling function context for use by the
585 runtime.</p>
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000586
587</div>
588
589<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000590<h4>
Jim Grosbachca752c92010-01-28 01:45:32 +0000591 <a name="llvm_eh_sjlj_callsite">llvm.eh.sjlj.callsite</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000592</h4>
Jim Grosbachca752c92010-01-28 01:45:32 +0000593
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000594<div>
Jim Grosbachca752c92010-01-28 01:45:32 +0000595
596<pre>
597 void %<a href="#llvm_eh_sjlj_callsite">llvm.eh.sjlj.callsite</a>(i32)
598</pre>
599
600<p>For SJLJ based exception handling, the <a href="#llvm_eh_sjlj_callsite">
601 <tt>llvm.eh.sjlj.callsite</tt></a> intrinsic identifies the callsite value
602 associated with the following invoke instruction. This is used to ensure
603 that landing pad entries in the LSDA are generated in the matching order.</p>
604
605</div>
606
607<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000608<h4>
Jim Grosbache4ad3872010-10-19 23:27:08 +0000609 <a name="llvm_eh_sjlj_dispatchsetup">llvm.eh.sjlj.dispatchsetup</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000610</h4>
Jim Grosbache4ad3872010-10-19 23:27:08 +0000611
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000612<div>
Jim Grosbache4ad3872010-10-19 23:27:08 +0000613
614<pre>
615 void %<a href="#llvm_eh_sjlj_dispatchsetup">llvm.eh.sjlj.dispatchsetup</a>(i32)
616</pre>
617
618<p>For SJLJ based exception handling, the <a href="#llvm_eh_sjlj_dispatchsetup">
619 <tt>llvm.eh.sjlj.dispatchsetup</tt></a> intrinsic is used by targets to do
620 any unwind-edge setup they need. By default, no action is taken. </p>
621
622</div>
623
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000624</div>
625
Jim Grosbache4ad3872010-10-19 23:27:08 +0000626<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000627<h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000628 <a name="asm">Asm Table Formats</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000629</h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000630
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000631<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000632
633<p>There are two tables that are used by the exception handling runtime to
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000634 determine which actions should take place when an exception is thrown.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000635
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000636<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000637<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000638 <a name="unwind_tables">Exception Handling Frame</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000639</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000640
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000641<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000642
643<p>An exception handling frame <tt>eh_frame</tt> is very similar to the unwind
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000644 frame used by dwarf debug info. The frame contains all the information
645 necessary to tear down the current frame and restore the state of the prior
646 frame. There is an exception handling frame for each function in a compile
647 unit, plus a common exception handling frame that defines information common
648 to all functions in the unit.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000649
650<p>Todo - Table details here.</p>
651
652</div>
653
654<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000655<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000656 <a name="exception_tables">Exception Tables</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000657</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000658
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000659<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000660
661<p>An exception table contains information about what actions to take when an
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000662 exception is thrown in a particular part of a function's code. There is one
663 exception table per function except leaf routines and functions that have
664 only calls to non-throwing functions will not need an exception table.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000665
666<p>Todo - Table details here.</p>
667
668</div>
669
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000670</div>
671
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000672<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000673<h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000674 <a name="todo">ToDo</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000675</h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000676
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000677<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000678
679<ol>
680
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000681 <li>Testing/Testing/Testing.</li>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000682
683</ol>
684
685</div>
686
687<!-- *********************************************************************** -->
688
689<hr>
690<address>
691 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000692 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000693 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000694 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000695
696 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
NAKAMURA Takumib9a33632011-04-09 02:13:37 +0000697 <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000698 Last modified: $Date$
699</address>
700
701</body>
702</html>