blob: 2cd5f406f2cebcfe49717d40dab7024a236d743d [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>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000036 <li><a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a></li>
Jim Grosbachf9570122009-05-14 00:46:35 +000037 <li><a href="#llvm_eh_sjlj_setjmp"><tt>llvm.eh.sjlj.setjmp</tt></a></li>
38 <li><a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a></li>
Jim Grosbach1b747ad2009-08-11 00:09:57 +000039 <li><a href="#llvm_eh_sjlj_lsda"><tt>llvm.eh.sjlj.lsda</tt></a></li>
Jim Grosbachca752c92010-01-28 01:45:32 +000040 <li><a href="#llvm_eh_sjlj_callsite"><tt>llvm.eh.sjlj.callsite</tt></a></li>
Jim Grosbache4ad3872010-10-19 23:27:08 +000041 <li><a href="#llvm_eh_sjlj_dispatchsetup"><tt>llvm.eh.sjlj.dispatchsetup</tt></a></li>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000042 </ol></li>
43 <li><a href="#asm">Asm Table Formats</a>
44 <ol>
45 <li><a href="#unwind_tables">Exception Handling Frame</a></li>
46 <li><a href="#exception_tables">Exception Tables</a></li>
47 </ol></li>
48 <li><a href="#todo">ToDo</a></li>
49</ul>
50</td>
51</tr></table>
52
53<div class="doc_author">
54 <p>Written by <a href="mailto:jlaskey@mac.com">Jim Laskey</a></p>
55</div>
56
57
58<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000059<h2><a name="introduction">Introduction</a></h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000060<!-- *********************************************************************** -->
61
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000062<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000063
64<p>This document is the central repository for all information pertaining to
Bill Wendlingc4f661e2009-08-15 08:56:09 +000065 exception handling in LLVM. It describes the format that LLVM exception
66 handling information takes, which is useful for those interested in creating
67 front-ends or dealing directly with the information. Further, this document
68 provides specific examples of what exception handling information is used for
Bill Wendling2a6b0732011-09-26 21:06:33 +000069 in C and C++.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000070
Jim Laskeyd0d39b62007-03-14 19:29:42 +000071<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000072<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000073 <a name="itanium">Itanium ABI Zero-cost Exception Handling</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000074</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000075
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000076<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000077
78<p>Exception handling for most programming languages is designed to recover from
Bill Wendlingc4f661e2009-08-15 08:56:09 +000079 conditions that rarely occur during general use of an application. To that
80 end, exception handling should not interfere with the main flow of an
81 application's algorithm by performing checkpointing tasks, such as saving the
82 current pc or register state.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000083
84<p>The Itanium ABI Exception Handling Specification defines a methodology for
Bill Wendlingc4f661e2009-08-15 08:56:09 +000085 providing outlying data in the form of exception tables without inlining
86 speculative exception handling code in the flow of an application's main
87 algorithm. Thus, the specification is said to add "zero-cost" to the normal
88 execution of an application.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000089
90<p>A more complete description of the Itanium ABI exception handling runtime
Bill Wendlingc4f661e2009-08-15 08:56:09 +000091 support of can be found at
92 <a href="http://www.codesourcery.com/cxx-abi/abi-eh.html">Itanium C++ ABI:
93 Exception Handling</a>. A description of the exception frame format can be
94 found at
95 <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 +000096 Frames</a>, with details of the DWARF 4 specification at
97 <a href="http://dwarfstd.org/Dwarf4Std.php">DWARF 4 Standard</a>.
Bill Wendlingc4f661e2009-08-15 08:56:09 +000098 A description for the C++ exception table formats can be found at
99 <a href="http://www.codesourcery.com/cxx-abi/exceptions.pdf">Exception Handling
100 Tables</a>.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000101
102</div>
103
104<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000105<h3>
Jim Grosbach00484d12009-08-22 01:42:39 +0000106 <a name="sjlj">Setjmp/Longjmp Exception Handling</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000107</h3>
Jim Grosbach00484d12009-08-22 01:42:39 +0000108
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000109<div>
Jim Grosbach00484d12009-08-22 01:42:39 +0000110
111<p>Setjmp/Longjmp (SJLJ) based exception handling uses LLVM intrinsics
112 <a href="#llvm_eh_sjlj_setjmp"><tt>llvm.eh.sjlj.setjmp</tt></a> and
113 <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a> to
114 handle control flow for exception handling.</p>
115
Bill Wendling4a065dd2011-09-20 01:08:53 +0000116<p>For each function which does exception processing &mdash; be
117 it <tt>try</tt>/<tt>catch</tt> blocks or cleanups &mdash; that function
118 registers itself on a global frame list. When exceptions are unwinding, the
119 runtime uses this list to identify which functions need processing.<p>
Jim Grosbach00484d12009-08-22 01:42:39 +0000120
121<p>Landing pad selection is encoded in the call site entry of the function
122 context. The runtime returns to the function via
123 <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a>, where
124 a switch table transfers control to the appropriate landing pad based on
125 the index stored in the function context.</p>
126
127<p>In contrast to DWARF exception handling, which encodes exception regions
128 and frame information in out-of-line tables, SJLJ exception handling
129 builds and removes the unwind frame context at runtime. This results in
130 faster exception handling at the expense of slower execution when no
131 exceptions are thrown. As exceptions are, by their nature, intended for
132 uncommon code paths, DWARF exception handling is generally preferred to
133 SJLJ.</p>
Bill Wendling4a065dd2011-09-20 01:08:53 +0000134
Jim Grosbach00484d12009-08-22 01:42:39 +0000135</div>
136
137<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000138<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000139 <a name="overview">Overview</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000140</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000141
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000142<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000143
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000144<p>When an exception is thrown in LLVM code, the runtime does its best to find a
145 handler suited to processing the circumstance.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000146
147<p>The runtime first attempts to find an <i>exception frame</i> corresponding to
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000148 the function where the exception was thrown. If the programming language
Bill Wendling2a6b0732011-09-26 21:06:33 +0000149 supports exception handling (e.g. C++), the exception frame contains a
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000150 reference to an exception table describing how to process the exception. If
Bill Wendling2a6b0732011-09-26 21:06:33 +0000151 the language does not support exception handling (e.g. C), or if the
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000152 exception needs to be forwarded to a prior activation, the exception frame
153 contains information about how to unwind the current activation and restore
154 the state of the prior activation. This process is repeated until the
Bill Wendling2a6b0732011-09-26 21:06:33 +0000155 exception is handled. If the exception is not handled and no activations
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000156 remain, then the application is terminated with an appropriate error
157 message.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000158
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000159<p>Because different programming languages have different behaviors when
160 handling exceptions, the exception handling ABI provides a mechanism for
Bill Wendling2a6b0732011-09-26 21:06:33 +0000161 supplying <i>personalities</i>. An exception handling personality is defined
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000162 by way of a <i>personality function</i> (e.g. <tt>__gxx_personality_v0</tt>
163 in C++), which receives the context of the exception, an <i>exception
164 structure</i> containing the exception object type and value, and a reference
165 to the exception table for the current function. The personality function
166 for the current compile unit is specified in a <i>common exception
167 frame</i>.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000168
Bill Wendling2a6b0732011-09-26 21:06:33 +0000169<p>The organization of an exception table is language dependent. For C++, an
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000170 exception table is organized as a series of code ranges defining what to do
Bill Wendling2a6b0732011-09-26 21:06:33 +0000171 if an exception occurs in that range. Typically, the information associated
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000172 with a range defines which types of exception objects (using C++ <i>type
173 info</i>) that are handled in that range, and an associated action that
Bill Wendling2a6b0732011-09-26 21:06:33 +0000174 should take place. Actions typically pass control to a <i>landing
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000175 pad</i>.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000176
Bill Wendling2a6b0732011-09-26 21:06:33 +0000177<p>A landing pad corresponds roughly to the code found in the <tt>catch</tt>
178 portion of a <tt>try</tt>/<tt>catch</tt> sequence. When execution resumes at
179 a landing pad, it receives an <i>exception structure</i> and a
180 <i>selector value</i> corresponding to the <i>type</i> of exception
181 thrown. The selector is then used to determine which <i>catch</i> should
182 actually process the exception.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000183
184</div>
185
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000186</div>
187
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000188<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000189<h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000190 <a name="codegen">LLVM Code Generation</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000191</h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000192
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000193<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000194
Bill Wendling2a6b0732011-09-26 21:06:33 +0000195<p>From a C++ developer's perspective, exceptions are defined in terms of the
Bill Wendling4a065dd2011-09-20 01:08:53 +0000196 <tt>throw</tt> and <tt>try</tt>/<tt>catch</tt> statements. In this section
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000197 we will describe the implementation of LLVM exception handling in terms of
198 C++ examples.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000199
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000200<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000201<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000202 <a name="throw">Throw</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000203</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000204
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000205<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000206
207<p>Languages that support exception handling typically provide a <tt>throw</tt>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000208 operation to initiate the exception process. Internally, a <tt>throw</tt>
209 operation breaks down into two steps.</p>
210
Bill Wendling4a065dd2011-09-20 01:08:53 +0000211<ol>
212 <li>A request is made to allocate exception space for an exception structure.
213 This structure needs to survive beyond the current activation. This
214 structure will contain the type and value of the object being thrown.</li>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000215
Bill Wendling4a065dd2011-09-20 01:08:53 +0000216 <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 Wendling2a6b0732011-09-26 21:06:33 +0000220<p>In C++, the allocation of the exception structure is done by the
Bill Wendling4a065dd2011-09-20 01:08:53 +0000221 <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
Bill Wendling2a6b0732011-09-26 21:06:33 +0000235 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:</p>
238
239<ol>
240 <li>where to continue when the call succeeds as per normal, and</li>
241
242 <li>where to continue if the call raises an exception, either by a throw or
243 the unwinding of a throw</li>
244</ol>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000245
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000246<p>The term used to define a the place where an <tt>invoke</tt> continues after
Bill Wendling2a6b0732011-09-26 21:06:33 +0000247 an exception is called a <i>landing pad</i>. LLVM landing pads are
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000248 conceptually alternative function entry points where an exception structure
Bill Wendling2a6b0732011-09-26 21:06:33 +0000249 reference and a type info index are passed in as arguments. The landing pad
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000250 saves the exception structure reference and then proceeds to select the catch
251 block that corresponds to the type info of the exception object.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000252
Bill Wendling4a065dd2011-09-20 01:08:53 +0000253<p>The LLVM <a href="LangRef.html#i_landingpad"><tt>landingpad</tt>
254 instruction</a> is used to convey information about the landing pad to the
255 back end. For C++, the <tt>landingpad</tt> instruction returns a pointer and
Bill Wendling2a6b0732011-09-26 21:06:33 +0000256 integer pair corresponding to the pointer to the <i>exception structure</i>
257 and the <i>selector value</i> respectively.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000258
Bill Wendling4a065dd2011-09-20 01:08:53 +0000259<p>The <tt>landingpad</tt> instruction takes a reference to the personality
260 function to be used for this <tt>try</tt>/<tt>catch</tt> sequence. The
Bill Wendling2a6b0732011-09-26 21:06:33 +0000261 remainder of the instruction is a list of <i>cleanup</i>, <i>catch</i>,
262 and <i>filter</i> clauses. The exception is tested against the clauses
263 sequentially from first to last. The selector value is a positive number if
264 the exception matched a type info, a negative number if it matched a filter,
265 and zero if it matched a cleanup. If nothing is matched, the behavior of the
266 program is <a href="#restrictions">undefined</a>. If a type info matched,
267 then the selector value is the index of the type info in the exception table,
268 which can be obtained using the
Bill Wendling4a065dd2011-09-20 01:08:53 +0000269 <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000270
271<p>Once the landing pad has the type info selector, the code branches to the
Bill Wendling4a065dd2011-09-20 01:08:53 +0000272 code for the first catch. The catch then checks the value of the type info
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000273 selector against the index of type info for that catch. Since the type info
Bill Wendling2a6b0732011-09-26 21:06:33 +0000274 index is not known until all the type infos have been gathered in the
275 backend, the catch code must call the
Bill Wendling4a065dd2011-09-20 01:08:53 +0000276 <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic to
277 determine the index for a given type info. If the catch fails to match the
Bill Wendling2a6b0732011-09-26 21:06:33 +0000278 selector then control is passed on to the next catch.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000279
Bill Wendling2a6b0732011-09-26 21:06:33 +0000280<p><b>Note:</b> Since the landing pad will not be used if there is no match in
281 the list of type info on the call to the <tt>landingpad</tt> instruction,
282 then neither the last catch nor <i>catch all</i> need to perform the check
283 against the selector.</p>
284
285<p>Finally, the entry and exit of catch code is bracketed with calls to
286 <tt>__cxa_begin_catch</tt> and <tt>__cxa_end_catch</tt>.</p>
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000287
288<ul>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000289 <li><tt>__cxa_begin_catch</tt> takes an exception structure reference as an
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000290 argument and returns the value of the exception object.</li>
291
Bill Wendling169e1b02009-09-10 22:14:16 +0000292 <li><tt>__cxa_end_catch</tt> takes no arguments. This function:<br><br>
293 <ol>
Bill Wendling808b9ce2009-09-10 22:12:50 +0000294 <li>Locates the most recently caught exception and decrements its handler
295 count,</li>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000296 <li>Removes the exception from the <i>caught</i> stack if the handler
297 count goes to zero, and</li>
298 <li>Destroys the exception if the handler count goes to zero and the
Bill Wendling808b9ce2009-09-10 22:12:50 +0000299 exception was not re-thrown by throw.</li>
Bill Wendling169e1b02009-09-10 22:14:16 +0000300 </ol>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000301 <p><b>Note:</b> a rethrow from within the catch may replace this call with
Bill Wendling808b9ce2009-09-10 22:12:50 +0000302 a <tt>__cxa_rethrow</tt>.</p></li>
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000303</ul>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000304
305</div>
306
307<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000308<h3>
Duncan Sands6590b042007-08-27 15:47:50 +0000309 <a name="cleanups">Cleanups</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000310</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000311
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000312<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000313
Bill Wendling4a065dd2011-09-20 01:08:53 +0000314<p>A cleanup is extra code which needs to be run as part of unwinding a scope.
Bill Wendling2a6b0732011-09-26 21:06:33 +0000315 C++ destructors are a typical example, but other languages and language
316 extensions provide a variety of different kinds of cleanups. In general, a
Bill Wendling4a065dd2011-09-20 01:08:53 +0000317 landing pad may need to run arbitrary amounts of cleanup code before actually
Bill Wendling2a6b0732011-09-26 21:06:33 +0000318 entering a catch block. To indicate the presence of cleanups, a
Bill Wendling4a065dd2011-09-20 01:08:53 +0000319 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a>
320 should have a <i>cleanup</i> clause. Otherwise, the unwinder will not stop at
321 the landing pad if there are no catches or filters that require it to.</p>
John McCalld7c10862011-05-28 07:45:59 +0000322
Bill Wendling2a6b0732011-09-26 21:06:33 +0000323<p><b>Note:</b> Do not allow a new exception to propagate out of the execution
324 of a cleanup. This can corrupt the internal state of the unwinder.
325 Different languages describe different high-level semantics for these
326 situations: for example, C++ requires that the process be terminated, whereas
327 Ada cancels both exceptions and throws a third.</p>
John McCalld7c10862011-05-28 07:45:59 +0000328
Bill Wendling2a6b0732011-09-26 21:06:33 +0000329<p>When all cleanups are finished, if the exception is not handled by the
330 current function, resume unwinding by calling the
Bill Wendling4a065dd2011-09-20 01:08:53 +0000331 <a href="LangRef.html#i_resume"><tt>resume</tt> instruction</a>, passing in
Bill Wendling2a6b0732011-09-26 21:06:33 +0000332 the result of the <tt>landingpad</tt> instruction for the original landing
Bill Wendling4a065dd2011-09-20 01:08:53 +0000333 pad.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000334
335</div>
336
337<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000338<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000339 <a name="throw_filters">Throw Filters</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000340</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000341
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000342<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000343
Bill Wendling2a6b0732011-09-26 21:06:33 +0000344<p>C++ allows the specification of which exception types may be thrown from a
345 function. To represent this, a top level landing pad may exist to filter out
346 invalid types. To express this in LLVM code the
Bill Wendling4a065dd2011-09-20 01:08:53 +0000347 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a> will
348 have a filter clause. The clause consists of an array of type infos.
349 <tt>landingpad</tt> will return a negative value if the exception does not
350 match any of the type infos. If no match is found then a call
351 to <tt>__cxa_call_unexpected</tt> should be made, otherwise
352 <tt>_Unwind_Resume</tt>. Each of these functions requires a reference to the
353 exception structure. Note that the most general form of a
354 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a> can
355 have any number of catch, cleanup, and filter clauses (though having more
356 than one cleanup is pointless). The LLVM C++ front-end can generate such
357 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instructions</a> due
358 to inlining creating nested exception handling scopes.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000359
360</div>
361
362<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000363<h3>
Duncan Sands6590b042007-08-27 15:47:50 +0000364 <a name="restrictions">Restrictions</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000365</h3>
Duncan Sands6590b042007-08-27 15:47:50 +0000366
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000367<div>
Duncan Sands6590b042007-08-27 15:47:50 +0000368
Bill Wendling4a065dd2011-09-20 01:08:53 +0000369<p>The unwinder delegates the decision of whether to stop in a call frame to
Bill Wendling2a6b0732011-09-26 21:06:33 +0000370 that call frame's language-specific personality function. Not all personality
371 functions guarantee that they will stop to perform cleanups. For example, the
372 GNU C++ personality function doesn't do so unless the exception is actually
373 caught somewhere further up the stack. When using this personality to
374 implement EH for a language that guarantees that cleanups will always be run
375 (e.g. Ada), be sure to indicate a catch-all in the
Bill Wendling4a065dd2011-09-20 01:08:53 +0000376 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a>
John McCalld7c10862011-05-28 07:45:59 +0000377 rather than just cleanups.</p>
378
Bill Wendling4a065dd2011-09-20 01:08:53 +0000379<p>In order for inlining to behave correctly, landing pads must be prepared to
Bill Wendling2a6b0732011-09-26 21:06:33 +0000380 handle selector results that they did not originally advertise. Suppose that
Bill Wendling4a065dd2011-09-20 01:08:53 +0000381 a function catches exceptions of type <tt>A</tt>, and it's inlined into a
Bill Wendling2a6b0732011-09-26 21:06:33 +0000382 function that catches exceptions of type <tt>B</tt>. The inliner will update
Bill Wendling4a065dd2011-09-20 01:08:53 +0000383 the <tt>landingpad</tt> instruction for the inlined landing pad to include
Bill Wendling2a6b0732011-09-26 21:06:33 +0000384 the fact that <tt>B</tt> is also caught. If that landing pad assumes that it
385 will only be entered to catch an <tt>A</tt>, it's in for a rude awakening.
Bill Wendling4a065dd2011-09-20 01:08:53 +0000386 Consequently, landing pads must test for the selector results they understand
387 and then resume exception propagation with the
388 <a href="LangRef.html#i_resume"><tt>resume</tt> instruction</a> if none of
389 the conditions match.</p>
Duncan Sands6590b042007-08-27 15:47:50 +0000390
391</div>
392
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000393</div>
394
Duncan Sands6590b042007-08-27 15:47:50 +0000395<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000396<h2>
Duncan Sands8036ca42007-03-30 12:22:09 +0000397 <a name="format_common_intrinsics">Exception Handling Intrinsics</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000398</h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000399
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000400<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000401
Bill Wendling4a065dd2011-09-20 01:08:53 +0000402<p>In addition to the
403 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt></a> and
404 <a href="LangRef.html#i_resume"><tt>resume</tt></a> instructions, LLVM uses
Bill Wendling2a6b0732011-09-26 21:06:33 +0000405 several intrinsic functions (name prefixed with <i><tt>llvm.eh</tt></i>) to
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000406 provide exception handling information at various points in generated
407 code.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000408
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000409<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000410<h4>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000411 <a name="llvm_eh_typeid_for">llvm.eh.typeid.for</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000412</h4>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000413
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000414<div>
Bill Wendlingbf230bf2009-08-15 20:08:04 +0000415
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000416<pre>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000417 i32 @llvm.eh.typeid.for(i8* %type_info)
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000418</pre>
419
420<p>This intrinsic returns the type info index in the exception table of the
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000421 current function. This value can be used to compare against the result
Bill Wendling3c051962011-09-20 01:14:54 +0000422 of <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a>.
423 The single argument is a reference to a type info.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000424
425</div>
426
427<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000428<h4>
Jim Grosbachf9570122009-05-14 00:46:35 +0000429 <a name="llvm_eh_sjlj_setjmp">llvm.eh.sjlj.setjmp</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000430</h4>
Jim Grosbachf9570122009-05-14 00:46:35 +0000431
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000432<div>
Bill Wendling3cf4ffd2009-08-15 20:07:42 +0000433
Jim Grosbachf9570122009-05-14 00:46:35 +0000434<pre>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000435 i32 @llvm.eh.sjlj.setjmp(i8* %setjmp_buf)
Jim Grosbachf9570122009-05-14 00:46:35 +0000436</pre>
437
Bill Wendling2a6b0732011-09-26 21:06:33 +0000438<p>For SJLJ based exception handling, this intrinsic forces register saving for
439 the current function and stores the address of the following instruction for
440 use as a destination address
441 by <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a>. The
442 buffer format and the overall functioning of this intrinsic is compatible
443 with the GCC <tt>__builtin_setjmp</tt> implementation allowing code built
444 with the clang and GCC to interoperate.</p>
Jim Grosbachf9570122009-05-14 00:46:35 +0000445
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000446<p>The single parameter is a pointer to a five word buffer in which the calling
447 context is saved. The front end places the frame pointer in the first word,
448 and the target implementation of this intrinsic should place the destination
449 address for a
450 <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a> in the
451 second word. The following three words are available for use in a
452 target-specific manner.</p>
Jim Grosbachf9570122009-05-14 00:46:35 +0000453
Benjamin Kramere15192b2009-08-05 15:42:44 +0000454</div>
455
Jim Grosbachf9570122009-05-14 00:46:35 +0000456<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000457<h4>
Jim Grosbach90346e22010-05-26 16:21:41 +0000458 <a name="llvm_eh_sjlj_longjmp">llvm.eh.sjlj.longjmp</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000459</h4>
Jim Grosbach90346e22010-05-26 16:21:41 +0000460
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000461<div>
Jim Grosbach90346e22010-05-26 16:21:41 +0000462
463<pre>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000464 void @llvm.eh.sjlj.longjmp(i8* %setjmp_buf)
Jim Grosbach90346e22010-05-26 16:21:41 +0000465</pre>
466
Bill Wendling2a6b0732011-09-26 21:06:33 +0000467<p>For SJLJ based exception handling, the <tt>llvm.eh.sjlj.longjmp</tt>
468 intrinsic is used to implement <tt>__builtin_longjmp()</tt>. The single
469 parameter is a pointer to a buffer populated
470 by <a href="#llvm_eh_sjlj_setjmp"><tt>llvm.eh.sjlj.setjmp</tt></a>. The frame
471 pointer and stack pointer are restored from the buffer, then control is
472 transferred to the destination address.</p>
Jim Grosbach90346e22010-05-26 16:21:41 +0000473
474</div>
475<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000476<h4>
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000477 <a name="llvm_eh_sjlj_lsda">llvm.eh.sjlj.lsda</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000478</h4>
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000479
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000480<div>
Bill Wendling3cf4ffd2009-08-15 20:07:42 +0000481
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000482<pre>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000483 i8* @llvm.eh.sjlj.lsda()
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000484</pre>
485
Bill Wendling2a6b0732011-09-26 21:06:33 +0000486<p>For SJLJ based exception handling, the <tt>llvm.eh.sjlj.lsda</tt> intrinsic
487 returns the address of the Language Specific Data Area (LSDA) for the current
488 function. The SJLJ front-end code stores this address in the exception
489 handling function context for use by the runtime.</p>
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000490
491</div>
492
493<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000494<h4>
Jim Grosbachca752c92010-01-28 01:45:32 +0000495 <a name="llvm_eh_sjlj_callsite">llvm.eh.sjlj.callsite</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000496</h4>
Jim Grosbachca752c92010-01-28 01:45:32 +0000497
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000498<div>
Jim Grosbachca752c92010-01-28 01:45:32 +0000499
500<pre>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000501 void @llvm.eh.sjlj.callsite(i32 %call_site_num)
Jim Grosbachca752c92010-01-28 01:45:32 +0000502</pre>
503
Bill Wendling2a6b0732011-09-26 21:06:33 +0000504<p>For SJLJ based exception handling, the <tt>llvm.eh.sjlj.callsite</tt>
505 intrinsic identifies the callsite value associated with the
506 following <tt>invoke</tt> instruction. This is used to ensure that landing
507 pad entries in the LSDA are generated in matching order.</p>
Jim Grosbachca752c92010-01-28 01:45:32 +0000508
509</div>
510
511<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000512<h4>
Jim Grosbache4ad3872010-10-19 23:27:08 +0000513 <a name="llvm_eh_sjlj_dispatchsetup">llvm.eh.sjlj.dispatchsetup</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000514</h4>
Jim Grosbache4ad3872010-10-19 23:27:08 +0000515
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000516<div>
Jim Grosbache4ad3872010-10-19 23:27:08 +0000517
518<pre>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000519 void @llvm.eh.sjlj.dispatchsetup(i32 %dispatch_value)
Jim Grosbache4ad3872010-10-19 23:27:08 +0000520</pre>
521
Bill Wendling2a6b0732011-09-26 21:06:33 +0000522<p>For SJLJ based exception handling, the <tt>llvm.eh.sjlj.dispatchsetup</tt>
523 intrinsic is used by targets to do any unwind edge setup they need. By
524 default, no action is taken.</p>
Jim Grosbache4ad3872010-10-19 23:27:08 +0000525
526</div>
527
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000528</div>
529
Jim Grosbache4ad3872010-10-19 23:27:08 +0000530<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000531<h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000532 <a name="asm">Asm Table Formats</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000533</h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000534
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000535<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000536
537<p>There are two tables that are used by the exception handling runtime to
Bill Wendling2a6b0732011-09-26 21:06:33 +0000538 determine which actions should be taken when an exception is thrown.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000539
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000540<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000541<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000542 <a name="unwind_tables">Exception Handling Frame</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000543</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000544
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000545<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000546
547<p>An exception handling frame <tt>eh_frame</tt> is very similar to the unwind
Bill Wendling2a6b0732011-09-26 21:06:33 +0000548 frame used by DWARF debug info. The frame contains all the information
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000549 necessary to tear down the current frame and restore the state of the prior
Bill Wendling2a6b0732011-09-26 21:06:33 +0000550 frame. There is an exception handling frame for each function in a compile
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000551 unit, plus a common exception handling frame that defines information common
552 to all functions in the unit.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000553
Bill Wendling2a6b0732011-09-26 21:06:33 +0000554<!-- Todo - Table details here. -->
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000555
556</div>
557
558<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000559<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000560 <a name="exception_tables">Exception Tables</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000561</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000562
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000563<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000564
565<p>An exception table contains information about what actions to take when an
Bill Wendling2a6b0732011-09-26 21:06:33 +0000566 exception is thrown in a particular part of a function's code. There is one
567 exception table per function, except leaf functions and functions that have
568 calls only to non-throwing functions. They do not need an exception
569 table.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000570
Bill Wendling2a6b0732011-09-26 21:06:33 +0000571<!-- Todo - Table details here. -->
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000572
573</div>
574
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000575</div>
576
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000577<!-- *********************************************************************** -->
578
579<hr>
580<address>
581 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000582 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000583 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000584 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000585
586 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
NAKAMURA Takumib9a33632011-04-09 02:13:37 +0000587 <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000588 Last modified: $Date$
589</address>
590
591</body>
592</html>