blob: 425b8b7a70e2e1bda6f150c661b306f16fcb4662 [file] [log] [blame]
Jim Laskey69567732007-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 Wendling365ab602009-08-15 08:56:09 +00006 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Eric Christopher98e0a592009-09-09 01:44:53 +00007 <meta name="description"
Bill Wendling365ab602009-08-15 08:56:09 +00008 content="Exception Handling in LLVM.">
Jim Laskey69567732007-03-14 19:29:42 +00009 <link rel="stylesheet" href="llvm.css" type="text/css">
10</head>
Bill Wendling365ab602009-08-15 08:56:09 +000011
Jim Laskey69567732007-03-14 19:29:42 +000012<body>
13
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +000014<h1>Exception Handling in LLVM</h1>
Jim Laskey69567732007-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 Grosbach8a2cfcb2009-08-22 01:42:39 +000023 <li><a href="#sjlj">Setjmp/Longjmp Exception Handling</a></li>
Jim Laskey69567732007-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 Sandsef5a6542007-08-27 15:47:50 +000030 <li><a href="#cleanups">Cleanups</a></li>
Jim Laskey69567732007-03-14 19:29:42 +000031 <li><a href="#throw_filters">Throw Filters</a></li>
Duncan Sandsef5a6542007-08-27 15:47:50 +000032 <li><a href="#restrictions">Restrictions</a></li>
Jim Laskey69567732007-03-14 19:29:42 +000033 </ol></li>
Duncan Sands16f122e2007-03-30 12:22:09 +000034 <li><a href="#format_common_intrinsics">Exception Handling Intrinsics</a>
Jim Laskey69567732007-03-14 19:29:42 +000035 <ol>
Jim Laskey69567732007-03-14 19:29:42 +000036 <li><a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a></li>
Jim Grosbach06928192009-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 Grosbach693e36a2009-08-11 00:09:57 +000039 <li><a href="#llvm_eh_sjlj_lsda"><tt>llvm.eh.sjlj.lsda</tt></a></li>
Jim Grosbach54c05302010-01-28 01:45:32 +000040 <li><a href="#llvm_eh_sjlj_callsite"><tt>llvm.eh.sjlj.callsite</tt></a></li>
Jim Grosbachbbdc5d22010-10-19 23:27:08 +000041 <li><a href="#llvm_eh_sjlj_dispatchsetup"><tt>llvm.eh.sjlj.dispatchsetup</tt></a></li>
Jim Laskey69567732007-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>
Jim Laskey69567732007-03-14 19:29:42 +000048</ul>
49</td>
50</tr></table>
51
52<div class="doc_author">
53 <p>Written by <a href="mailto:jlaskey@mac.com">Jim Laskey</a></p>
54</div>
55
56
57<!-- *********************************************************************** -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +000058<h2><a name="introduction">Introduction</a></h2>
Jim Laskey69567732007-03-14 19:29:42 +000059<!-- *********************************************************************** -->
60
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +000061<div>
Jim Laskey69567732007-03-14 19:29:42 +000062
63<p>This document is the central repository for all information pertaining to
Bill Wendling365ab602009-08-15 08:56:09 +000064 exception handling in LLVM. It describes the format that LLVM exception
65 handling information takes, which is useful for those interested in creating
66 front-ends or dealing directly with the information. Further, this document
67 provides specific examples of what exception handling information is used for
Bill Wendling58c80f82011-09-26 21:06:33 +000068 in C and C++.</p>
Jim Laskey69567732007-03-14 19:29:42 +000069
Jim Laskey69567732007-03-14 19:29:42 +000070<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +000071<h3>
Jim Laskey69567732007-03-14 19:29:42 +000072 <a name="itanium">Itanium ABI Zero-cost Exception Handling</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +000073</h3>
Jim Laskey69567732007-03-14 19:29:42 +000074
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +000075<div>
Jim Laskey69567732007-03-14 19:29:42 +000076
77<p>Exception handling for most programming languages is designed to recover from
Bill Wendling365ab602009-08-15 08:56:09 +000078 conditions that rarely occur during general use of an application. To that
79 end, exception handling should not interfere with the main flow of an
80 application's algorithm by performing checkpointing tasks, such as saving the
81 current pc or register state.</p>
Jim Laskey69567732007-03-14 19:29:42 +000082
83<p>The Itanium ABI Exception Handling Specification defines a methodology for
Bill Wendling365ab602009-08-15 08:56:09 +000084 providing outlying data in the form of exception tables without inlining
85 speculative exception handling code in the flow of an application's main
86 algorithm. Thus, the specification is said to add "zero-cost" to the normal
87 execution of an application.</p>
Jim Laskey69567732007-03-14 19:29:42 +000088
89<p>A more complete description of the Itanium ABI exception handling runtime
Bill Wendling365ab602009-08-15 08:56:09 +000090 support of can be found at
91 <a href="http://www.codesourcery.com/cxx-abi/abi-eh.html">Itanium C++ ABI:
92 Exception Handling</a>. A description of the exception frame format can be
93 found at
94 <a href="http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html">Exception
Bill Wendling01161842011-09-20 01:08:53 +000095 Frames</a>, with details of the DWARF 4 specification at
96 <a href="http://dwarfstd.org/Dwarf4Std.php">DWARF 4 Standard</a>.
Bill Wendling365ab602009-08-15 08:56:09 +000097 A description for the C++ exception table formats can be found at
98 <a href="http://www.codesourcery.com/cxx-abi/exceptions.pdf">Exception Handling
99 Tables</a>.</p>
Jim Laskey69567732007-03-14 19:29:42 +0000100
101</div>
102
103<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000104<h3>
Jim Grosbach8a2cfcb2009-08-22 01:42:39 +0000105 <a name="sjlj">Setjmp/Longjmp Exception Handling</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000106</h3>
Jim Grosbach8a2cfcb2009-08-22 01:42:39 +0000107
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000108<div>
Jim Grosbach8a2cfcb2009-08-22 01:42:39 +0000109
110<p>Setjmp/Longjmp (SJLJ) based exception handling uses LLVM intrinsics
111 <a href="#llvm_eh_sjlj_setjmp"><tt>llvm.eh.sjlj.setjmp</tt></a> and
112 <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a> to
113 handle control flow for exception handling.</p>
114
Bill Wendling01161842011-09-20 01:08:53 +0000115<p>For each function which does exception processing &mdash; be
116 it <tt>try</tt>/<tt>catch</tt> blocks or cleanups &mdash; that function
117 registers itself on a global frame list. When exceptions are unwinding, the
118 runtime uses this list to identify which functions need processing.<p>
Jim Grosbach8a2cfcb2009-08-22 01:42:39 +0000119
120<p>Landing pad selection is encoded in the call site entry of the function
121 context. The runtime returns to the function via
122 <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a>, where
123 a switch table transfers control to the appropriate landing pad based on
124 the index stored in the function context.</p>
125
126<p>In contrast to DWARF exception handling, which encodes exception regions
127 and frame information in out-of-line tables, SJLJ exception handling
128 builds and removes the unwind frame context at runtime. This results in
129 faster exception handling at the expense of slower execution when no
130 exceptions are thrown. As exceptions are, by their nature, intended for
131 uncommon code paths, DWARF exception handling is generally preferred to
132 SJLJ.</p>
Bill Wendling01161842011-09-20 01:08:53 +0000133
Jim Grosbach8a2cfcb2009-08-22 01:42:39 +0000134</div>
135
136<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000137<h3>
Jim Laskey69567732007-03-14 19:29:42 +0000138 <a name="overview">Overview</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000139</h3>
Jim Laskey69567732007-03-14 19:29:42 +0000140
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000141<div>
Jim Laskey69567732007-03-14 19:29:42 +0000142
Bill Wendling365ab602009-08-15 08:56:09 +0000143<p>When an exception is thrown in LLVM code, the runtime does its best to find a
144 handler suited to processing the circumstance.</p>
Jim Laskey69567732007-03-14 19:29:42 +0000145
146<p>The runtime first attempts to find an <i>exception frame</i> corresponding to
Bill Wendling365ab602009-08-15 08:56:09 +0000147 the function where the exception was thrown. If the programming language
Bill Wendling58c80f82011-09-26 21:06:33 +0000148 supports exception handling (e.g. C++), the exception frame contains a
Bill Wendling365ab602009-08-15 08:56:09 +0000149 reference to an exception table describing how to process the exception. If
Bill Wendling58c80f82011-09-26 21:06:33 +0000150 the language does not support exception handling (e.g. C), or if the
Bill Wendling365ab602009-08-15 08:56:09 +0000151 exception needs to be forwarded to a prior activation, the exception frame
152 contains information about how to unwind the current activation and restore
153 the state of the prior activation. This process is repeated until the
Bill Wendling58c80f82011-09-26 21:06:33 +0000154 exception is handled. If the exception is not handled and no activations
Bill Wendling365ab602009-08-15 08:56:09 +0000155 remain, then the application is terminated with an appropriate error
156 message.</p>
Jim Laskey69567732007-03-14 19:29:42 +0000157
Bill Wendling365ab602009-08-15 08:56:09 +0000158<p>Because different programming languages have different behaviors when
159 handling exceptions, the exception handling ABI provides a mechanism for
Bill Wendling58c80f82011-09-26 21:06:33 +0000160 supplying <i>personalities</i>. An exception handling personality is defined
Bill Wendling365ab602009-08-15 08:56:09 +0000161 by way of a <i>personality function</i> (e.g. <tt>__gxx_personality_v0</tt>
162 in C++), which receives the context of the exception, an <i>exception
163 structure</i> containing the exception object type and value, and a reference
164 to the exception table for the current function. The personality function
165 for the current compile unit is specified in a <i>common exception
166 frame</i>.</p>
Jim Laskey69567732007-03-14 19:29:42 +0000167
Bill Wendling58c80f82011-09-26 21:06:33 +0000168<p>The organization of an exception table is language dependent. For C++, an
Bill Wendling365ab602009-08-15 08:56:09 +0000169 exception table is organized as a series of code ranges defining what to do
Bill Wendling58c80f82011-09-26 21:06:33 +0000170 if an exception occurs in that range. Typically, the information associated
Bill Wendling365ab602009-08-15 08:56:09 +0000171 with a range defines which types of exception objects (using C++ <i>type
172 info</i>) that are handled in that range, and an associated action that
Bill Wendling58c80f82011-09-26 21:06:33 +0000173 should take place. Actions typically pass control to a <i>landing
Bill Wendling365ab602009-08-15 08:56:09 +0000174 pad</i>.</p>
Jim Laskey69567732007-03-14 19:29:42 +0000175
Bill Wendling58c80f82011-09-26 21:06:33 +0000176<p>A landing pad corresponds roughly to the code found in the <tt>catch</tt>
177 portion of a <tt>try</tt>/<tt>catch</tt> sequence. When execution resumes at
178 a landing pad, it receives an <i>exception structure</i> and a
179 <i>selector value</i> corresponding to the <i>type</i> of exception
180 thrown. The selector is then used to determine which <i>catch</i> should
181 actually process the exception.</p>
Jim Laskey69567732007-03-14 19:29:42 +0000182
183</div>
184
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000185</div>
186
Jim Laskey69567732007-03-14 19:29:42 +0000187<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000188<h2>
Jim Laskey69567732007-03-14 19:29:42 +0000189 <a name="codegen">LLVM Code Generation</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000190</h2>
Jim Laskey69567732007-03-14 19:29:42 +0000191
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000192<div>
Jim Laskey69567732007-03-14 19:29:42 +0000193
Bill Wendling58c80f82011-09-26 21:06:33 +0000194<p>From a C++ developer's perspective, exceptions are defined in terms of the
Bill Wendling01161842011-09-20 01:08:53 +0000195 <tt>throw</tt> and <tt>try</tt>/<tt>catch</tt> statements. In this section
Bill Wendling365ab602009-08-15 08:56:09 +0000196 we will describe the implementation of LLVM exception handling in terms of
197 C++ examples.</p>
Jim Laskey69567732007-03-14 19:29:42 +0000198
Jim Laskey69567732007-03-14 19:29:42 +0000199<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000200<h3>
Jim Laskey69567732007-03-14 19:29:42 +0000201 <a name="throw">Throw</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000202</h3>
Jim Laskey69567732007-03-14 19:29:42 +0000203
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000204<div>
Jim Laskey69567732007-03-14 19:29:42 +0000205
206<p>Languages that support exception handling typically provide a <tt>throw</tt>
Bill Wendling58c80f82011-09-26 21:06:33 +0000207 operation to initiate the exception process. Internally, a <tt>throw</tt>
208 operation breaks down into two steps.</p>
209
Bill Wendling01161842011-09-20 01:08:53 +0000210<ol>
211 <li>A request is made to allocate exception space for an exception structure.
212 This structure needs to survive beyond the current activation. This
213 structure will contain the type and value of the object being thrown.</li>
Bill Wendling58c80f82011-09-26 21:06:33 +0000214
Bill Wendling01161842011-09-20 01:08:53 +0000215 <li>A call is made to the runtime to raise the exception, passing the
216 exception structure as an argument.</li>
217</ol>
Jim Laskey69567732007-03-14 19:29:42 +0000218
Bill Wendling58c80f82011-09-26 21:06:33 +0000219<p>In C++, the allocation of the exception structure is done by the
Bill Wendling01161842011-09-20 01:08:53 +0000220 <tt>__cxa_allocate_exception</tt> runtime function. The exception raising is
221 handled by <tt>__cxa_throw</tt>. The type of the exception is represented
222 using a C++ RTTI structure.</p>
Jim Laskey69567732007-03-14 19:29:42 +0000223
224</div>
225
226<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000227<h3>
Jim Laskey69567732007-03-14 19:29:42 +0000228 <a name="try_catch">Try/Catch</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000229</h3>
Jim Laskey69567732007-03-14 19:29:42 +0000230
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000231<div>
Jim Laskey69567732007-03-14 19:29:42 +0000232
Bill Wendling365ab602009-08-15 08:56:09 +0000233<p>A call within the scope of a <i>try</i> statement can potentially raise an
Bill Wendling58c80f82011-09-26 21:06:33 +0000234 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:</p>
237
238<ol>
239 <li>where to continue when the call succeeds as per normal, and</li>
240
241 <li>where to continue if the call raises an exception, either by a throw or
242 the unwinding of a throw</li>
243</ol>
Jim Laskey69567732007-03-14 19:29:42 +0000244
Bill Wendling365ab602009-08-15 08:56:09 +0000245<p>The term used to define a the place where an <tt>invoke</tt> continues after
Bill Wendling58c80f82011-09-26 21:06:33 +0000246 an exception is called a <i>landing pad</i>. LLVM landing pads are
Bill Wendling365ab602009-08-15 08:56:09 +0000247 conceptually alternative function entry points where an exception structure
Bill Wendling58c80f82011-09-26 21:06:33 +0000248 reference and a type info index are passed in as arguments. The landing pad
Bill Wendling365ab602009-08-15 08:56:09 +0000249 saves the exception structure reference and then proceeds to select the catch
250 block that corresponds to the type info of the exception object.</p>
Jim Laskey69567732007-03-14 19:29:42 +0000251
Bill Wendling01161842011-09-20 01:08:53 +0000252<p>The LLVM <a href="LangRef.html#i_landingpad"><tt>landingpad</tt>
253 instruction</a> is used to convey information about the landing pad to the
254 back end. For C++, the <tt>landingpad</tt> instruction returns a pointer and
Bill Wendling58c80f82011-09-26 21:06:33 +0000255 integer pair corresponding to the pointer to the <i>exception structure</i>
256 and the <i>selector value</i> respectively.</p>
Jim Laskey69567732007-03-14 19:29:42 +0000257
Bill Wendling01161842011-09-20 01:08:53 +0000258<p>The <tt>landingpad</tt> instruction takes a reference to the personality
259 function to be used for this <tt>try</tt>/<tt>catch</tt> sequence. The
Bill Wendling58c80f82011-09-26 21:06:33 +0000260 remainder of the instruction is a list of <i>cleanup</i>, <i>catch</i>,
261 and <i>filter</i> clauses. The exception is tested against the clauses
262 sequentially from first to last. The selector value is a positive number if
263 the exception matched a type info, a negative number if it matched a filter,
264 and zero if it matched a cleanup. If nothing is matched, the behavior of the
265 program is <a href="#restrictions">undefined</a>. If a type info matched,
266 then the selector value is the index of the type info in the exception table,
267 which can be obtained using the
Bill Wendling01161842011-09-20 01:08:53 +0000268 <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic.</p>
Jim Laskey69567732007-03-14 19:29:42 +0000269
270<p>Once the landing pad has the type info selector, the code branches to the
Bill Wendling01161842011-09-20 01:08:53 +0000271 code for the first catch. The catch then checks the value of the type info
Bill Wendling365ab602009-08-15 08:56:09 +0000272 selector against the index of type info for that catch. Since the type info
Bill Wendling58c80f82011-09-26 21:06:33 +0000273 index is not known until all the type infos have been gathered in the
274 backend, the catch code must call the
Bill Wendling01161842011-09-20 01:08:53 +0000275 <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic to
276 determine the index for a given type info. If the catch fails to match the
Bill Wendling58c80f82011-09-26 21:06:33 +0000277 selector then control is passed on to the next catch.</p>
Jim Laskey69567732007-03-14 19:29:42 +0000278
Bill Wendling58c80f82011-09-26 21:06:33 +0000279<p><b>Note:</b> Since the landing pad will not be used if there is no match in
280 the list of type info on the call to the <tt>landingpad</tt> instruction,
281 then neither the last catch nor <i>catch all</i> need to perform the check
282 against the selector.</p>
283
284<p>Finally, the entry and exit of catch code is bracketed with calls to
285 <tt>__cxa_begin_catch</tt> and <tt>__cxa_end_catch</tt>.</p>
Bill Wendling365ab602009-08-15 08:56:09 +0000286
287<ul>
Bill Wendling58c80f82011-09-26 21:06:33 +0000288 <li><tt>__cxa_begin_catch</tt> takes an exception structure reference as an
Bill Wendling365ab602009-08-15 08:56:09 +0000289 argument and returns the value of the exception object.</li>
290
Bill Wendlingfd272012009-09-10 22:14:16 +0000291 <li><tt>__cxa_end_catch</tt> takes no arguments. This function:<br><br>
292 <ol>
Bill Wendling33b693f2009-09-10 22:12:50 +0000293 <li>Locates the most recently caught exception and decrements its handler
294 count,</li>
Bill Wendling58c80f82011-09-26 21:06:33 +0000295 <li>Removes the exception from the <i>caught</i> stack if the handler
296 count goes to zero, and</li>
297 <li>Destroys the exception if the handler count goes to zero and the
Bill Wendling33b693f2009-09-10 22:12:50 +0000298 exception was not re-thrown by throw.</li>
Bill Wendlingfd272012009-09-10 22:14:16 +0000299 </ol>
Bill Wendling58c80f82011-09-26 21:06:33 +0000300 <p><b>Note:</b> a rethrow from within the catch may replace this call with
Bill Wendling33b693f2009-09-10 22:12:50 +0000301 a <tt>__cxa_rethrow</tt>.</p></li>
Bill Wendling365ab602009-08-15 08:56:09 +0000302</ul>
Jim Laskey69567732007-03-14 19:29:42 +0000303
304</div>
305
306<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000307<h3>
Duncan Sandsef5a6542007-08-27 15:47:50 +0000308 <a name="cleanups">Cleanups</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000309</h3>
Jim Laskey69567732007-03-14 19:29:42 +0000310
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000311<div>
Jim Laskey69567732007-03-14 19:29:42 +0000312
Bill Wendling01161842011-09-20 01:08:53 +0000313<p>A cleanup is extra code which needs to be run as part of unwinding a scope.
Bill Wendling58c80f82011-09-26 21:06:33 +0000314 C++ destructors are a typical example, but other languages and language
315 extensions provide a variety of different kinds of cleanups. In general, a
Bill Wendling01161842011-09-20 01:08:53 +0000316 landing pad may need to run arbitrary amounts of cleanup code before actually
Bill Wendling58c80f82011-09-26 21:06:33 +0000317 entering a catch block. To indicate the presence of cleanups, a
Bill Wendling01161842011-09-20 01:08:53 +0000318 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a>
319 should have a <i>cleanup</i> clause. Otherwise, the unwinder will not stop at
320 the landing pad if there are no catches or filters that require it to.</p>
John McCall046c47e2011-05-28 07:45:59 +0000321
Bill Wendling58c80f82011-09-26 21:06:33 +0000322<p><b>Note:</b> Do not allow a new exception to propagate out of the execution
323 of a cleanup. This can corrupt the internal state of the unwinder.
324 Different languages describe different high-level semantics for these
325 situations: for example, C++ requires that the process be terminated, whereas
326 Ada cancels both exceptions and throws a third.</p>
John McCall046c47e2011-05-28 07:45:59 +0000327
Bill Wendling58c80f82011-09-26 21:06:33 +0000328<p>When all cleanups are finished, if the exception is not handled by the
329 current function, resume unwinding by calling the
Bill Wendling01161842011-09-20 01:08:53 +0000330 <a href="LangRef.html#i_resume"><tt>resume</tt> instruction</a>, passing in
Bill Wendling58c80f82011-09-26 21:06:33 +0000331 the result of the <tt>landingpad</tt> instruction for the original landing
Bill Wendling01161842011-09-20 01:08:53 +0000332 pad.</p>
Jim Laskey69567732007-03-14 19:29:42 +0000333
334</div>
335
336<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000337<h3>
Jim Laskey69567732007-03-14 19:29:42 +0000338 <a name="throw_filters">Throw Filters</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000339</h3>
Jim Laskey69567732007-03-14 19:29:42 +0000340
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000341<div>
Jim Laskey69567732007-03-14 19:29:42 +0000342
Bill Wendling58c80f82011-09-26 21:06:33 +0000343<p>C++ allows the specification of which exception types may be thrown from a
344 function. To represent this, a top level landing pad may exist to filter out
345 invalid types. To express this in LLVM code the
Bill Wendling01161842011-09-20 01:08:53 +0000346 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a> will
347 have a filter clause. The clause consists of an array of type infos.
348 <tt>landingpad</tt> will return a negative value if the exception does not
349 match any of the type infos. If no match is found then a call
350 to <tt>__cxa_call_unexpected</tt> should be made, otherwise
351 <tt>_Unwind_Resume</tt>. Each of these functions requires a reference to the
352 exception structure. Note that the most general form of a
353 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a> can
354 have any number of catch, cleanup, and filter clauses (though having more
355 than one cleanup is pointless). The LLVM C++ front-end can generate such
356 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instructions</a> due
357 to inlining creating nested exception handling scopes.</p>
Jim Laskey69567732007-03-14 19:29:42 +0000358
359</div>
360
361<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000362<h3>
Duncan Sandsef5a6542007-08-27 15:47:50 +0000363 <a name="restrictions">Restrictions</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000364</h3>
Duncan Sandsef5a6542007-08-27 15:47:50 +0000365
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000366<div>
Duncan Sandsef5a6542007-08-27 15:47:50 +0000367
Bill Wendling01161842011-09-20 01:08:53 +0000368<p>The unwinder delegates the decision of whether to stop in a call frame to
Bill Wendling58c80f82011-09-26 21:06:33 +0000369 that call frame's language-specific personality function. Not all personality
370 functions guarantee that they will stop to perform cleanups. For example, the
371 GNU C++ personality function doesn't do so unless the exception is actually
372 caught somewhere further up the stack. When using this personality to
373 implement EH for a language that guarantees that cleanups will always be run
374 (e.g. Ada), be sure to indicate a catch-all in the
Bill Wendling01161842011-09-20 01:08:53 +0000375 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a>
John McCall046c47e2011-05-28 07:45:59 +0000376 rather than just cleanups.</p>
377
Bill Wendling01161842011-09-20 01:08:53 +0000378<p>In order for inlining to behave correctly, landing pads must be prepared to
Bill Wendling58c80f82011-09-26 21:06:33 +0000379 handle selector results that they did not originally advertise. Suppose that
Bill Wendling01161842011-09-20 01:08:53 +0000380 a function catches exceptions of type <tt>A</tt>, and it's inlined into a
Bill Wendling58c80f82011-09-26 21:06:33 +0000381 function that catches exceptions of type <tt>B</tt>. The inliner will update
Bill Wendling01161842011-09-20 01:08:53 +0000382 the <tt>landingpad</tt> instruction for the inlined landing pad to include
Bill Wendling58c80f82011-09-26 21:06:33 +0000383 the fact that <tt>B</tt> is also caught. If that landing pad assumes that it
384 will only be entered to catch an <tt>A</tt>, it's in for a rude awakening.
Bill Wendling01161842011-09-20 01:08:53 +0000385 Consequently, landing pads must test for the selector results they understand
386 and then resume exception propagation with the
387 <a href="LangRef.html#i_resume"><tt>resume</tt> instruction</a> if none of
388 the conditions match.</p>
Duncan Sandsef5a6542007-08-27 15:47:50 +0000389
390</div>
391
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000392</div>
393
Duncan Sandsef5a6542007-08-27 15:47:50 +0000394<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000395<h2>
Duncan Sands16f122e2007-03-30 12:22:09 +0000396 <a name="format_common_intrinsics">Exception Handling Intrinsics</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000397</h2>
Jim Laskey69567732007-03-14 19:29:42 +0000398
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000399<div>
Jim Laskey69567732007-03-14 19:29:42 +0000400
Bill Wendling01161842011-09-20 01:08:53 +0000401<p>In addition to the
402 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt></a> and
403 <a href="LangRef.html#i_resume"><tt>resume</tt></a> instructions, LLVM uses
Bill Wendling58c80f82011-09-26 21:06:33 +0000404 several intrinsic functions (name prefixed with <i><tt>llvm.eh</tt></i>) to
Bill Wendling365ab602009-08-15 08:56:09 +0000405 provide exception handling information at various points in generated
406 code.</p>
Jim Laskey69567732007-03-14 19:29:42 +0000407
Jim Laskey69567732007-03-14 19:29:42 +0000408<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000409<h4>
Jim Laskey69567732007-03-14 19:29:42 +0000410 <a name="llvm_eh_typeid_for">llvm.eh.typeid.for</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000411</h4>
Jim Laskey69567732007-03-14 19:29:42 +0000412
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000413<div>
Bill Wendling4c1b9b02009-08-15 20:08:04 +0000414
Jim Laskey69567732007-03-14 19:29:42 +0000415<pre>
Bill Wendling58c80f82011-09-26 21:06:33 +0000416 i32 @llvm.eh.typeid.for(i8* %type_info)
Jim Laskey69567732007-03-14 19:29:42 +0000417</pre>
418
419<p>This intrinsic returns the type info index in the exception table of the
Bill Wendling365ab602009-08-15 08:56:09 +0000420 current function. This value can be used to compare against the result
Bill Wendling12e58f92011-09-20 01:14:54 +0000421 of <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a>.
422 The single argument is a reference to a type info.</p>
Jim Laskey69567732007-03-14 19:29:42 +0000423
424</div>
425
426<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000427<h4>
Jim Grosbach06928192009-05-14 00:46:35 +0000428 <a name="llvm_eh_sjlj_setjmp">llvm.eh.sjlj.setjmp</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000429</h4>
Jim Grosbach06928192009-05-14 00:46:35 +0000430
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000431<div>
Bill Wendling527241b2009-08-15 20:07:42 +0000432
Jim Grosbach06928192009-05-14 00:46:35 +0000433<pre>
Bill Wendling58c80f82011-09-26 21:06:33 +0000434 i32 @llvm.eh.sjlj.setjmp(i8* %setjmp_buf)
Jim Grosbach06928192009-05-14 00:46:35 +0000435</pre>
436
Bill Wendling58c80f82011-09-26 21:06:33 +0000437<p>For SJLJ based exception handling, this intrinsic forces register saving for
438 the current function and stores the address of the following instruction for
439 use as a destination address
440 by <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a>. The
441 buffer format and the overall functioning of this intrinsic is compatible
442 with the GCC <tt>__builtin_setjmp</tt> implementation allowing code built
443 with the clang and GCC to interoperate.</p>
Jim Grosbach06928192009-05-14 00:46:35 +0000444
Bill Wendling365ab602009-08-15 08:56:09 +0000445<p>The single parameter is a pointer to a five word buffer in which the calling
446 context is saved. The front end places the frame pointer in the first word,
447 and the target implementation of this intrinsic should place the destination
448 address for a
449 <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a> in the
450 second word. The following three words are available for use in a
451 target-specific manner.</p>
Jim Grosbach06928192009-05-14 00:46:35 +0000452
Benjamin Kramereaccdd32009-08-05 15:42:44 +0000453</div>
454
Jim Grosbach06928192009-05-14 00:46:35 +0000455<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000456<h4>
Jim Grosbach172aaad2010-05-26 16:21:41 +0000457 <a name="llvm_eh_sjlj_longjmp">llvm.eh.sjlj.longjmp</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000458</h4>
Jim Grosbach172aaad2010-05-26 16:21:41 +0000459
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000460<div>
Jim Grosbach172aaad2010-05-26 16:21:41 +0000461
462<pre>
Bill Wendling58c80f82011-09-26 21:06:33 +0000463 void @llvm.eh.sjlj.longjmp(i8* %setjmp_buf)
Jim Grosbach172aaad2010-05-26 16:21:41 +0000464</pre>
465
Bill Wendling58c80f82011-09-26 21:06:33 +0000466<p>For SJLJ based exception handling, the <tt>llvm.eh.sjlj.longjmp</tt>
467 intrinsic is used to implement <tt>__builtin_longjmp()</tt>. The single
468 parameter is a pointer to a buffer populated
469 by <a href="#llvm_eh_sjlj_setjmp"><tt>llvm.eh.sjlj.setjmp</tt></a>. The frame
470 pointer and stack pointer are restored from the buffer, then control is
471 transferred to the destination address.</p>
Jim Grosbach172aaad2010-05-26 16:21:41 +0000472
473</div>
474<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000475<h4>
Jim Grosbach693e36a2009-08-11 00:09:57 +0000476 <a name="llvm_eh_sjlj_lsda">llvm.eh.sjlj.lsda</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000477</h4>
Jim Grosbach693e36a2009-08-11 00:09:57 +0000478
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000479<div>
Bill Wendling527241b2009-08-15 20:07:42 +0000480
Jim Grosbach693e36a2009-08-11 00:09:57 +0000481<pre>
Bill Wendling58c80f82011-09-26 21:06:33 +0000482 i8* @llvm.eh.sjlj.lsda()
Jim Grosbach693e36a2009-08-11 00:09:57 +0000483</pre>
484
Bill Wendling58c80f82011-09-26 21:06:33 +0000485<p>For SJLJ based exception handling, the <tt>llvm.eh.sjlj.lsda</tt> intrinsic
486 returns the address of the Language Specific Data Area (LSDA) for the current
487 function. The SJLJ front-end code stores this address in the exception
488 handling function context for use by the runtime.</p>
Jim Grosbach693e36a2009-08-11 00:09:57 +0000489
490</div>
491
492<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000493<h4>
Jim Grosbach54c05302010-01-28 01:45:32 +0000494 <a name="llvm_eh_sjlj_callsite">llvm.eh.sjlj.callsite</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000495</h4>
Jim Grosbach54c05302010-01-28 01:45:32 +0000496
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000497<div>
Jim Grosbach54c05302010-01-28 01:45:32 +0000498
499<pre>
Bill Wendling58c80f82011-09-26 21:06:33 +0000500 void @llvm.eh.sjlj.callsite(i32 %call_site_num)
Jim Grosbach54c05302010-01-28 01:45:32 +0000501</pre>
502
Bill Wendling58c80f82011-09-26 21:06:33 +0000503<p>For SJLJ based exception handling, the <tt>llvm.eh.sjlj.callsite</tt>
504 intrinsic identifies the callsite value associated with the
505 following <tt>invoke</tt> instruction. This is used to ensure that landing
506 pad entries in the LSDA are generated in matching order.</p>
Jim Grosbach54c05302010-01-28 01:45:32 +0000507
508</div>
509
510<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000511<h4>
Jim Grosbachbbdc5d22010-10-19 23:27:08 +0000512 <a name="llvm_eh_sjlj_dispatchsetup">llvm.eh.sjlj.dispatchsetup</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000513</h4>
Jim Grosbachbbdc5d22010-10-19 23:27:08 +0000514
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000515<div>
Jim Grosbachbbdc5d22010-10-19 23:27:08 +0000516
517<pre>
Bill Wendling58c80f82011-09-26 21:06:33 +0000518 void @llvm.eh.sjlj.dispatchsetup(i32 %dispatch_value)
Jim Grosbachbbdc5d22010-10-19 23:27:08 +0000519</pre>
520
Bill Wendling58c80f82011-09-26 21:06:33 +0000521<p>For SJLJ based exception handling, the <tt>llvm.eh.sjlj.dispatchsetup</tt>
522 intrinsic is used by targets to do any unwind edge setup they need. By
523 default, no action is taken.</p>
Jim Grosbachbbdc5d22010-10-19 23:27:08 +0000524
525</div>
526
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000527</div>
528
Jim Grosbachbbdc5d22010-10-19 23:27:08 +0000529<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000530<h2>
Jim Laskey69567732007-03-14 19:29:42 +0000531 <a name="asm">Asm Table Formats</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000532</h2>
Jim Laskey69567732007-03-14 19:29:42 +0000533
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000534<div>
Jim Laskey69567732007-03-14 19:29:42 +0000535
536<p>There are two tables that are used by the exception handling runtime to
Bill Wendling58c80f82011-09-26 21:06:33 +0000537 determine which actions should be taken when an exception is thrown.</p>
Jim Laskey69567732007-03-14 19:29:42 +0000538
Jim Laskey69567732007-03-14 19:29:42 +0000539<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000540<h3>
Jim Laskey69567732007-03-14 19:29:42 +0000541 <a name="unwind_tables">Exception Handling Frame</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000542</h3>
Jim Laskey69567732007-03-14 19:29:42 +0000543
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000544<div>
Jim Laskey69567732007-03-14 19:29:42 +0000545
546<p>An exception handling frame <tt>eh_frame</tt> is very similar to the unwind
Bill Wendling58c80f82011-09-26 21:06:33 +0000547 frame used by DWARF debug info. The frame contains all the information
Bill Wendling365ab602009-08-15 08:56:09 +0000548 necessary to tear down the current frame and restore the state of the prior
Bill Wendling58c80f82011-09-26 21:06:33 +0000549 frame. There is an exception handling frame for each function in a compile
Bill Wendling365ab602009-08-15 08:56:09 +0000550 unit, plus a common exception handling frame that defines information common
551 to all functions in the unit.</p>
Jim Laskey69567732007-03-14 19:29:42 +0000552
Bill Wendling58c80f82011-09-26 21:06:33 +0000553<!-- Todo - Table details here. -->
Jim Laskey69567732007-03-14 19:29:42 +0000554
555</div>
556
557<!-- ======================================================================= -->
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000558<h3>
Jim Laskey69567732007-03-14 19:29:42 +0000559 <a name="exception_tables">Exception Tables</a>
NAKAMURA Takumifc8d9302011-04-18 23:59:50 +0000560</h3>
Jim Laskey69567732007-03-14 19:29:42 +0000561
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000562<div>
Jim Laskey69567732007-03-14 19:29:42 +0000563
564<p>An exception table contains information about what actions to take when an
Bill Wendling58c80f82011-09-26 21:06:33 +0000565 exception is thrown in a particular part of a function's code. There is one
566 exception table per function, except leaf functions and functions that have
567 calls only to non-throwing functions. They do not need an exception
568 table.</p>
Jim Laskey69567732007-03-14 19:29:42 +0000569
Bill Wendling58c80f82011-09-26 21:06:33 +0000570<!-- Todo - Table details here. -->
Jim Laskey69567732007-03-14 19:29:42 +0000571
572</div>
573
NAKAMURA Takumiaa3d6242011-04-23 00:30:22 +0000574</div>
575
Jim Laskey69567732007-03-14 19:29:42 +0000576<!-- *********************************************************************** -->
577
578<hr>
579<address>
580 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman86242e12008-12-11 17:34:48 +0000581 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Jim Laskey69567732007-03-14 19:29:42 +0000582 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman86242e12008-12-11 17:34:48 +0000583 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Jim Laskey69567732007-03-14 19:29:42 +0000584
585 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
NAKAMURA Takumica46f5a2011-04-09 02:13:37 +0000586 <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br>
Jim Laskey69567732007-03-14 19:29:42 +0000587 Last modified: $Date$
588</address>
589
590</body>
591</html>