blob: ca4a453bdb06a6647a4957f27eaa6d5116736e3b [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
Bill Wendling4a065dd2011-09-20 01:08:53 +000099 Frames</a>, with details of the DWARF 4 specification at
100 <a href="http://dwarfstd.org/Dwarf4Std.php">DWARF 4 Standard</a>.
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000101 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
Bill Wendling4a065dd2011-09-20 01:08:53 +0000119<p>For each function which does exception processing &mdash; be
120 it <tt>try</tt>/<tt>catch</tt> blocks or cleanups &mdash; that function
121 registers itself on a global frame list. When exceptions are unwinding, the
122 runtime uses this list to identify which functions need processing.<p>
Jim Grosbach00484d12009-08-22 01:42:39 +0000123
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>
Bill Wendling4a065dd2011-09-20 01:08:53 +0000137
Jim Grosbach00484d12009-08-22 01:42:39 +0000138</div>
139
140<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000141<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000142 <a name="overview">Overview</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000143</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000144
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000145<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000146
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000147<p>When an exception is thrown in LLVM code, the runtime does its best to find a
148 handler suited to processing the circumstance.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000149
150<p>The runtime first attempts to find an <i>exception frame</i> corresponding to
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000151 the function where the exception was thrown. If the programming language
152 (e.g. C++) supports exception handling, the exception frame contains a
153 reference to an exception table describing how to process the exception. If
154 the language (e.g. C) does not support exception handling, or if the
155 exception needs to be forwarded to a prior activation, the exception frame
156 contains information about how to unwind the current activation and restore
157 the state of the prior activation. This process is repeated until the
158 exception is handled. If the exception is not handled and no activations
159 remain, then the application is terminated with an appropriate error
160 message.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000161
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000162<p>Because different programming languages have different behaviors when
163 handling exceptions, the exception handling ABI provides a mechanism for
164 supplying <i>personalities.</i> An exception handling personality is defined
165 by way of a <i>personality function</i> (e.g. <tt>__gxx_personality_v0</tt>
166 in C++), which receives the context of the exception, an <i>exception
167 structure</i> containing the exception object type and value, and a reference
168 to the exception table for the current function. The personality function
169 for the current compile unit is specified in a <i>common exception
170 frame</i>.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000171
172<p>The organization of an exception table is language dependent. For C++, an
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000173 exception table is organized as a series of code ranges defining what to do
174 if an exception occurs in that range. Typically, the information associated
175 with a range defines which types of exception objects (using C++ <i>type
176 info</i>) that are handled in that range, and an associated action that
177 should take place. Actions typically pass control to a <i>landing
178 pad</i>.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000179
Bill Wendling4a065dd2011-09-20 01:08:53 +0000180<p>A landing pad corresponds to the code found in the <tt>catch</tt> portion of
181 a <tt>try</tt>/<tt>catch</tt> sequence. When execution resumes at a landing
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000182 pad, it receives the exception structure and a selector corresponding to
183 the <i>type</i> of exception thrown. The selector is then used to determine
184 which <i>catch</i> should actually process the exception.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000185
186</div>
187
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000188</div>
189
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000190<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000191<h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000192 <a name="codegen">LLVM Code Generation</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000193</h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000194
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000195<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000196
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000197<p>From the C++ developers perspective, exceptions are defined in terms of the
Bill Wendling4a065dd2011-09-20 01:08:53 +0000198 <tt>throw</tt> and <tt>try</tt>/<tt>catch</tt> statements. In this section
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000199 we will describe the implementation of LLVM exception handling in terms of
200 C++ examples.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000201
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000202<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000203<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000204 <a name="throw">Throw</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000205</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000206
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000207<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000208
209<p>Languages that support exception handling typically provide a <tt>throw</tt>
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000210 operation to initiate the exception process. Internally, a throw operation
Bill Wendling4a065dd2011-09-20 01:08:53 +0000211 breaks down into two steps.</p>
212<ol>
213 <li>A request is made to allocate exception space for an exception structure.
214 This structure needs to survive beyond the current activation. This
215 structure will contain the type and value of the object being thrown.</li>
216 <li>A call is made to the runtime to raise the exception, passing the
217 exception structure as an argument.</li>
218</ol>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000219
Bill Wendling4a065dd2011-09-20 01:08:53 +0000220<p>In C++, the allocation of the exception structure is done by then
221 <tt>__cxa_allocate_exception</tt> runtime function. The exception raising is
222 handled by <tt>__cxa_throw</tt>. The type of the exception is represented
223 using a C++ RTTI structure.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000224
225</div>
226
227<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000228<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000229 <a name="try_catch">Try/Catch</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000230</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000231
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000232<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000233
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000234<p>A call within the scope of a <i>try</i> statement can potentially raise an
235 exception. In those circumstances, the LLVM C++ front-end replaces the call
236 with an <tt>invoke</tt> instruction. Unlike a call, the <tt>invoke</tt> has
237 two potential continuation points: where to continue when the call succeeds
238 as per normal; and where to continue if the call raises an exception, either
239 by a throw or the unwinding of a throw.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000240
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000241<p>The term used to define a the place where an <tt>invoke</tt> continues after
242 an exception is called a <i>landing pad</i>. LLVM landing pads are
243 conceptually alternative function entry points where an exception structure
244 reference and a type info index are passed in as arguments. The landing pad
245 saves the exception structure reference and then proceeds to select the catch
246 block that corresponds to the type info of the exception object.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000247
Bill Wendling4a065dd2011-09-20 01:08:53 +0000248<p>The LLVM <a href="LangRef.html#i_landingpad"><tt>landingpad</tt>
249 instruction</a> is used to convey information about the landing pad to the
250 back end. For C++, the <tt>landingpad</tt> instruction returns a pointer and
251 integer pair corresponding to the pointer to the exception structure and the
252 "selector value" respectively.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000253
Bill Wendling4a065dd2011-09-20 01:08:53 +0000254<p>The <tt>landingpad</tt> instruction takes a reference to the personality
255 function to be used for this <tt>try</tt>/<tt>catch</tt> sequence. The
256 remainder of the instruction is a list of <i>catch</i> and <i>filter</i>
257 clauses. The exception is tested against the clauses sequentially from first
258 to last. The selector value is a positive number if the exception matched a
259 type info, a negative number if it matched a filter, and zero if it matched a
260 cleanup. If nothing is matched, the behaviour of the program
261 is <a href="#restrictions">undefined</a>. If a type info matched, then the
262 selector value is the index of the type info in the exception table, which
263 can be obtained using the
264 <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000265
266<p>Once the landing pad has the type info selector, the code branches to the
Bill Wendling4a065dd2011-09-20 01:08:53 +0000267 code for the first catch. The catch then checks the value of the type info
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000268 selector against the index of type info for that catch. Since the type info
269 index is not known until all the type info have been gathered in the backend,
270 the catch code will call the
Bill Wendling4a065dd2011-09-20 01:08:53 +0000271 <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic to
272 determine the index for a given type info. If the catch fails to match the
273 selector then control is passed on to the next catch. Note: Since the landing
274 pad will not be used if there is no match in the list of type info on the
275 call to the <a href="LangRef.html#i_landingpad"><tt>landingpad</tt>
276 instruction</a>, then neither the last catch nor <i>catch all</i> need to
277 perform the check against the selector.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000278
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000279<p>Finally, the entry and exit of catch code is bracketed with calls
280 to <tt>__cxa_begin_catch</tt> and <tt>__cxa_end_catch</tt>.</p>
281
282<ul>
283 <li><tt>__cxa_begin_catch</tt> takes a exception structure reference as an
284 argument and returns the value of the exception object.</li>
285
Bill Wendling169e1b02009-09-10 22:14:16 +0000286 <li><tt>__cxa_end_catch</tt> takes no arguments. This function:<br><br>
287 <ol>
Bill Wendling808b9ce2009-09-10 22:12:50 +0000288 <li>Locates the most recently caught exception and decrements its handler
289 count,</li>
290 <li>Removes the exception from the "caught" stack if the handler count
291 goes to zero, and</li>
292 <li>Destroys the exception if the handler count goes to zero, and the
293 exception was not re-thrown by throw.</li>
Bill Wendling169e1b02009-09-10 22:14:16 +0000294 </ol>
Bill Wendling808b9ce2009-09-10 22:12:50 +0000295 <p>Note: a rethrow from within the catch may replace this call with
296 a <tt>__cxa_rethrow</tt>.</p></li>
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000297</ul>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000298
299</div>
300
301<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000302<h3>
Duncan Sands6590b042007-08-27 15:47:50 +0000303 <a name="cleanups">Cleanups</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000304</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000305
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000306<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000307
Bill Wendling4a065dd2011-09-20 01:08:53 +0000308<p>A cleanup is extra code which needs to be run as part of unwinding a scope.
309 C++ destructors are a prominent example, but other languages and language
310 extensions provide a variety of different kinds of cleanup. In general, a
311 landing pad may need to run arbitrary amounts of cleanup code before actually
312 entering a catch block. To indicate the presence of cleanups, a
313 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a>
314 should have a <i>cleanup</i> clause. Otherwise, the unwinder will not stop at
315 the landing pad if there are no catches or filters that require it to.</p>
John McCalld7c10862011-05-28 07:45:59 +0000316
317<p>Do not allow a new exception to propagate out of the execution of a
318 cleanup. This can corrupt the internal state of the unwinder.
319 Different languages describe different high-level semantics for
320 these situations: for example, C++ requires that the process be
321 terminated, whereas Ada cancels both exceptions and throws a third.</p>
322
323<p>When all cleanups have completed, if the exception is not handled
324 by the current function, resume unwinding by calling the
Bill Wendling4a065dd2011-09-20 01:08:53 +0000325 <a href="LangRef.html#i_resume"><tt>resume</tt> instruction</a>, passing in
326 the results of the <tt>landingpad</tt> instruction for the original landing
327 pad.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000328
329</div>
330
331<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000332<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000333 <a name="throw_filters">Throw Filters</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000334</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000335
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000336<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000337
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000338<p>C++ allows the specification of which exception types can be thrown from a
339 function. To represent this a top level landing pad may exist to filter out
Bill Wendling4a065dd2011-09-20 01:08:53 +0000340 invalid types. To express this in LLVM code the
341 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a> will
342 have a filter clause. The clause consists of an array of type infos.
343 <tt>landingpad</tt> will return a negative value if the exception does not
344 match any of the type infos. If no match is found then a call
345 to <tt>__cxa_call_unexpected</tt> should be made, otherwise
346 <tt>_Unwind_Resume</tt>. Each of these functions requires a reference to the
347 exception structure. Note that the most general form of a
348 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a> can
349 have any number of catch, cleanup, and filter clauses (though having more
350 than one cleanup is pointless). The LLVM C++ front-end can generate such
351 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instructions</a> due
352 to inlining creating nested exception handling scopes.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000353
354</div>
355
356<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000357<h3>
Duncan Sands6590b042007-08-27 15:47:50 +0000358 <a name="restrictions">Restrictions</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000359</h3>
Duncan Sands6590b042007-08-27 15:47:50 +0000360
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000361<div>
Duncan Sands6590b042007-08-27 15:47:50 +0000362
Bill Wendling4a065dd2011-09-20 01:08:53 +0000363<p>The unwinder delegates the decision of whether to stop in a call frame to
364 that call frame's language-specific personality function. Not all
365 personalities functions guarantee that they will stop to perform
366 cleanups. For example, the GNU C++ personality doesn't do so unless the
367 exception is actually caught somewhere further up the stack. When using this
368 personality to implement EH for a language that guarantees that cleanups will
369 always be run, be sure to indicate a catch-all in the
370 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a>
John McCalld7c10862011-05-28 07:45:59 +0000371 rather than just cleanups.</p>
372
Bill Wendling4a065dd2011-09-20 01:08:53 +0000373<p>In order for inlining to behave correctly, landing pads must be prepared to
374 handle selector results that they did not originally advertise. Suppose that
375 a function catches exceptions of type <tt>A</tt>, and it's inlined into a
376 function that catches exceptions of type <tt>B</tt>. The inliner will update
377 the <tt>landingpad</tt> instruction for the inlined landing pad to include
378 the fact that <tt>B</tt> is caught. If that landing pad assumes that it will
379 only be entered to catch an <tt>A</tt>, it's in for a rude surprise.
380 Consequently, landing pads must test for the selector results they understand
381 and then resume exception propagation with the
382 <a href="LangRef.html#i_resume"><tt>resume</tt> instruction</a> if none of
383 the conditions match.</p>
Duncan Sands6590b042007-08-27 15:47:50 +0000384
385</div>
386
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000387</div>
388
Duncan Sands6590b042007-08-27 15:47:50 +0000389<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000390<h2>
Duncan Sands8036ca42007-03-30 12:22:09 +0000391 <a name="format_common_intrinsics">Exception Handling Intrinsics</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000392</h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000393
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000394<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000395
Bill Wendling4a065dd2011-09-20 01:08:53 +0000396<p>In addition to the
397 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt></a> and
398 <a href="LangRef.html#i_resume"><tt>resume</tt></a> instructions, LLVM uses
399 several intrinsic functions (name prefixed with "<tt>llvm.eh</tt>") to
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000400 provide exception handling information at various points in generated
401 code.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000402
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000403<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000404<h4>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000405 <a name="llvm_eh_typeid_for">llvm.eh.typeid.for</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000406</h4>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000407
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000408<div>
Bill Wendlingbf230bf2009-08-15 20:08:04 +0000409
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000410<pre>
Duncan Sandsb01bbdc2009-10-14 16:11:37 +0000411 i32 %<a href="#llvm_eh_typeid_for">llvm.eh.typeid.for</a>(i8*)
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000412</pre>
413
414<p>This intrinsic returns the type info index in the exception table of the
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000415 current function. This value can be used to compare against the result
416 of <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>. The single
417 argument is a reference to a type info.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000418
419</div>
420
421<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000422<h4>
Jim Grosbachf9570122009-05-14 00:46:35 +0000423 <a name="llvm_eh_sjlj_setjmp">llvm.eh.sjlj.setjmp</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000424</h4>
Jim Grosbachf9570122009-05-14 00:46:35 +0000425
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000426<div>
Bill Wendling3cf4ffd2009-08-15 20:07:42 +0000427
Jim Grosbachf9570122009-05-14 00:46:35 +0000428<pre>
Bill Wendling3cf4ffd2009-08-15 20:07:42 +0000429 i32 %<a href="#llvm_eh_sjlj_setjmp">llvm.eh.sjlj.setjmp</a>(i8*)
Jim Grosbachf9570122009-05-14 00:46:35 +0000430</pre>
431
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000432<p>The SJLJ exception handling uses this intrinsic to force register saving for
433 the current function and to store the address of the following instruction
434 for use as a destination address by <a href="#llvm_eh_sjlj_longjmp">
435 <tt>llvm.eh.sjlj.longjmp</tt></a>. The buffer format and the overall
436 functioning of this intrinsic is compatible with the GCC
437 <tt>__builtin_setjmp</tt> implementation, allowing code built with the
438 two compilers to interoperate.</p>
Jim Grosbachf9570122009-05-14 00:46:35 +0000439
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000440<p>The single parameter is a pointer to a five word buffer in which the calling
441 context is saved. The front end places the frame pointer in the first word,
442 and the target implementation of this intrinsic should place the destination
443 address for a
444 <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a> in the
445 second word. The following three words are available for use in a
446 target-specific manner.</p>
Jim Grosbachf9570122009-05-14 00:46:35 +0000447
Benjamin Kramere15192b2009-08-05 15:42:44 +0000448</div>
449
Jim Grosbachf9570122009-05-14 00:46:35 +0000450<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000451<h4>
Jim Grosbach90346e22010-05-26 16:21:41 +0000452 <a name="llvm_eh_sjlj_longjmp">llvm.eh.sjlj.longjmp</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000453</h4>
Jim Grosbach90346e22010-05-26 16:21:41 +0000454
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000455<div>
Jim Grosbach90346e22010-05-26 16:21:41 +0000456
457<pre>
458 void %<a href="#llvm_eh_sjlj_longjmp">llvm.eh.sjlj.setjmp</a>(i8*)
459</pre>
460
461<p>The <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a>
462 intrinsic is used to implement <tt>__builtin_longjmp()</tt> for SJLJ
463 style exception handling. The single parameter is a pointer to a
464 buffer populated by <a href="#llvm_eh_sjlj_setjmp">
465 <tt>llvm.eh.sjlj.setjmp</tt></a>. The frame pointer and stack pointer
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000466 are restored from the buffer, then control is transferred to the
Jim Grosbach90346e22010-05-26 16:21:41 +0000467 destination address.</p>
468
469</div>
470<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000471<h4>
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000472 <a name="llvm_eh_sjlj_lsda">llvm.eh.sjlj.lsda</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000473</h4>
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000474
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000475<div>
Bill Wendling3cf4ffd2009-08-15 20:07:42 +0000476
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000477<pre>
Dan Gohman3dfb3cf2010-05-28 17:07:41 +0000478 i8* %<a href="#llvm_eh_sjlj_lsda">llvm.eh.sjlj.lsda</a>()
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000479</pre>
480
481<p>Used for SJLJ based exception handling, the <a href="#llvm_eh_sjlj_lsda">
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000482 <tt>llvm.eh.sjlj.lsda</tt></a> intrinsic returns the address of the Language
483 Specific Data Area (LSDA) for the current function. The SJLJ front-end code
484 stores this address in the exception handling function context for use by the
485 runtime.</p>
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000486
487</div>
488
489<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000490<h4>
Jim Grosbachca752c92010-01-28 01:45:32 +0000491 <a name="llvm_eh_sjlj_callsite">llvm.eh.sjlj.callsite</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000492</h4>
Jim Grosbachca752c92010-01-28 01:45:32 +0000493
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000494<div>
Jim Grosbachca752c92010-01-28 01:45:32 +0000495
496<pre>
497 void %<a href="#llvm_eh_sjlj_callsite">llvm.eh.sjlj.callsite</a>(i32)
498</pre>
499
500<p>For SJLJ based exception handling, the <a href="#llvm_eh_sjlj_callsite">
501 <tt>llvm.eh.sjlj.callsite</tt></a> intrinsic identifies the callsite value
502 associated with the following invoke instruction. This is used to ensure
503 that landing pad entries in the LSDA are generated in the matching order.</p>
504
505</div>
506
507<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000508<h4>
Jim Grosbache4ad3872010-10-19 23:27:08 +0000509 <a name="llvm_eh_sjlj_dispatchsetup">llvm.eh.sjlj.dispatchsetup</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000510</h4>
Jim Grosbache4ad3872010-10-19 23:27:08 +0000511
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000512<div>
Jim Grosbache4ad3872010-10-19 23:27:08 +0000513
514<pre>
515 void %<a href="#llvm_eh_sjlj_dispatchsetup">llvm.eh.sjlj.dispatchsetup</a>(i32)
516</pre>
517
518<p>For SJLJ based exception handling, the <a href="#llvm_eh_sjlj_dispatchsetup">
519 <tt>llvm.eh.sjlj.dispatchsetup</tt></a> intrinsic is used by targets to do
520 any unwind-edge setup they need. By default, no action is taken. </p>
521
522</div>
523
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000524</div>
525
Jim Grosbache4ad3872010-10-19 23:27:08 +0000526<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000527<h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000528 <a name="asm">Asm Table Formats</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000529</h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000530
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000531<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000532
533<p>There are two tables that are used by the exception handling runtime to
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000534 determine which actions should take place when an exception is thrown.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000535
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000536<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000537<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000538 <a name="unwind_tables">Exception Handling Frame</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000539</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000540
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000541<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000542
543<p>An exception handling frame <tt>eh_frame</tt> is very similar to the unwind
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000544 frame used by dwarf debug info. The frame contains all the information
545 necessary to tear down the current frame and restore the state of the prior
546 frame. There is an exception handling frame for each function in a compile
547 unit, plus a common exception handling frame that defines information common
548 to all functions in the unit.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000549
550<p>Todo - Table details here.</p>
551
552</div>
553
554<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000555<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000556 <a name="exception_tables">Exception Tables</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000557</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000558
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000559<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000560
561<p>An exception table contains information about what actions to take when an
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000562 exception is thrown in a particular part of a function's code. There is one
563 exception table per function except leaf routines and functions that have
564 only calls to non-throwing functions will not need an exception table.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000565
566<p>Todo - Table details here.</p>
567
568</div>
569
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000570</div>
571
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000572<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000573<h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000574 <a name="todo">ToDo</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000575</h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000576
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000577<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000578
579<ol>
580
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000581 <li>Testing/Testing/Testing.</li>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000582
583</ol>
584
585</div>
586
587<!-- *********************************************************************** -->
588
589<hr>
590<address>
591 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000592 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000593 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000594 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000595
596 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
NAKAMURA Takumib9a33632011-04-09 02:13:37 +0000597 <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000598 Last modified: $Date$
599</address>
600
601</body>
602</html>