blob: 42881f5a773d8c7adfdf2bca00c7d54dab640f7f [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 Laskeyd0d39b62007-03-14 19:29:42 +000041 </ol></li>
42 <li><a href="#asm">Asm Table Formats</a>
43 <ol>
44 <li><a href="#unwind_tables">Exception Handling Frame</a></li>
45 <li><a href="#exception_tables">Exception Tables</a></li>
46 </ol></li>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000047</ul>
48</td>
49</tr></table>
50
51<div class="doc_author">
Chris Lattnerf5a3c422011-11-27 21:02:12 +000052 <p>Written by the <a href="http://llvm.org/">LLVM Team</a></p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000053</div>
54
55
56<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000057<h2><a name="introduction">Introduction</a></h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000058<!-- *********************************************************************** -->
59
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000060<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000061
62<p>This document is the central repository for all information pertaining to
Bill Wendlingc4f661e2009-08-15 08:56:09 +000063 exception handling in LLVM. It describes the format that LLVM exception
64 handling information takes, which is useful for those interested in creating
65 front-ends or dealing directly with the information. Further, this document
66 provides specific examples of what exception handling information is used for
Bill Wendling2a6b0732011-09-26 21:06:33 +000067 in C and C++.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000068
Jim Laskeyd0d39b62007-03-14 19:29:42 +000069<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000070<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000071 <a name="itanium">Itanium ABI Zero-cost Exception Handling</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000072</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000073
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000074<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000075
76<p>Exception handling for most programming languages is designed to recover from
Bill Wendlingc4f661e2009-08-15 08:56:09 +000077 conditions that rarely occur during general use of an application. To that
78 end, exception handling should not interfere with the main flow of an
79 application's algorithm by performing checkpointing tasks, such as saving the
80 current pc or register state.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000081
82<p>The Itanium ABI Exception Handling Specification defines a methodology for
Bill Wendlingc4f661e2009-08-15 08:56:09 +000083 providing outlying data in the form of exception tables without inlining
84 speculative exception handling code in the flow of an application's main
85 algorithm. Thus, the specification is said to add "zero-cost" to the normal
86 execution of an application.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000087
88<p>A more complete description of the Itanium ABI exception handling runtime
Bill Wendlingc4f661e2009-08-15 08:56:09 +000089 support of can be found at
90 <a href="http://www.codesourcery.com/cxx-abi/abi-eh.html">Itanium C++ ABI:
91 Exception Handling</a>. A description of the exception frame format can be
92 found at
93 <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 +000094 Frames</a>, with details of the DWARF 4 specification at
95 <a href="http://dwarfstd.org/Dwarf4Std.php">DWARF 4 Standard</a>.
Bill Wendlingc4f661e2009-08-15 08:56:09 +000096 A description for the C++ exception table formats can be found at
97 <a href="http://www.codesourcery.com/cxx-abi/exceptions.pdf">Exception Handling
98 Tables</a>.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000099
100</div>
101
102<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000103<h3>
Jim Grosbach00484d12009-08-22 01:42:39 +0000104 <a name="sjlj">Setjmp/Longjmp Exception Handling</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000105</h3>
Jim Grosbach00484d12009-08-22 01:42:39 +0000106
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000107<div>
Jim Grosbach00484d12009-08-22 01:42:39 +0000108
109<p>Setjmp/Longjmp (SJLJ) based exception handling uses LLVM intrinsics
110 <a href="#llvm_eh_sjlj_setjmp"><tt>llvm.eh.sjlj.setjmp</tt></a> and
111 <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a> to
112 handle control flow for exception handling.</p>
113
Bill Wendling4a065dd2011-09-20 01:08:53 +0000114<p>For each function which does exception processing &mdash; be
115 it <tt>try</tt>/<tt>catch</tt> blocks or cleanups &mdash; that function
116 registers itself on a global frame list. When exceptions are unwinding, the
117 runtime uses this list to identify which functions need processing.<p>
Jim Grosbach00484d12009-08-22 01:42:39 +0000118
119<p>Landing pad selection is encoded in the call site entry of the function
120 context. The runtime returns to the function via
121 <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a>, where
122 a switch table transfers control to the appropriate landing pad based on
123 the index stored in the function context.</p>
124
125<p>In contrast to DWARF exception handling, which encodes exception regions
126 and frame information in out-of-line tables, SJLJ exception handling
127 builds and removes the unwind frame context at runtime. This results in
128 faster exception handling at the expense of slower execution when no
129 exceptions are thrown. As exceptions are, by their nature, intended for
130 uncommon code paths, DWARF exception handling is generally preferred to
131 SJLJ.</p>
Bill Wendling4a065dd2011-09-20 01:08:53 +0000132
Jim Grosbach00484d12009-08-22 01:42:39 +0000133</div>
134
135<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000136<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000137 <a name="overview">Overview</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000138</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000139
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000140<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000141
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000142<p>When an exception is thrown in LLVM code, the runtime does its best to find a
143 handler suited to processing the circumstance.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000144
145<p>The runtime first attempts to find an <i>exception frame</i> corresponding to
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000146 the function where the exception was thrown. If the programming language
Bill Wendling2a6b0732011-09-26 21:06:33 +0000147 supports exception handling (e.g. C++), the exception frame contains a
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000148 reference to an exception table describing how to process the exception. If
Bill Wendling2a6b0732011-09-26 21:06:33 +0000149 the language does not support exception handling (e.g. C), or if the
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000150 exception needs to be forwarded to a prior activation, the exception frame
151 contains information about how to unwind the current activation and restore
152 the state of the prior activation. This process is repeated until the
Bill Wendling2a6b0732011-09-26 21:06:33 +0000153 exception is handled. If the exception is not handled and no activations
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000154 remain, then the application is terminated with an appropriate error
155 message.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000156
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000157<p>Because different programming languages have different behaviors when
158 handling exceptions, the exception handling ABI provides a mechanism for
Bill Wendling2a6b0732011-09-26 21:06:33 +0000159 supplying <i>personalities</i>. An exception handling personality is defined
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000160 by way of a <i>personality function</i> (e.g. <tt>__gxx_personality_v0</tt>
161 in C++), which receives the context of the exception, an <i>exception
162 structure</i> containing the exception object type and value, and a reference
163 to the exception table for the current function. The personality function
164 for the current compile unit is specified in a <i>common exception
165 frame</i>.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000166
Bill Wendling2a6b0732011-09-26 21:06:33 +0000167<p>The organization of an exception table is language dependent. For C++, an
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000168 exception table is organized as a series of code ranges defining what to do
Bill Wendling2a6b0732011-09-26 21:06:33 +0000169 if an exception occurs in that range. Typically, the information associated
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000170 with a range defines which types of exception objects (using C++ <i>type
171 info</i>) that are handled in that range, and an associated action that
Bill Wendling2a6b0732011-09-26 21:06:33 +0000172 should take place. Actions typically pass control to a <i>landing
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000173 pad</i>.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000174
Bill Wendling2a6b0732011-09-26 21:06:33 +0000175<p>A landing pad corresponds roughly to the code found in the <tt>catch</tt>
176 portion of a <tt>try</tt>/<tt>catch</tt> sequence. When execution resumes at
177 a landing pad, it receives an <i>exception structure</i> and a
178 <i>selector value</i> corresponding to the <i>type</i> of exception
179 thrown. The selector is then used to determine which <i>catch</i> should
180 actually process the exception.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000181
182</div>
183
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000184</div>
185
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000186<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000187<h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000188 <a name="codegen">LLVM Code Generation</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000189</h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000190
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000191<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000192
Bill Wendling2a6b0732011-09-26 21:06:33 +0000193<p>From a C++ developer's perspective, exceptions are defined in terms of the
Bill Wendling4a065dd2011-09-20 01:08:53 +0000194 <tt>throw</tt> and <tt>try</tt>/<tt>catch</tt> statements. In this section
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000195 we will describe the implementation of LLVM exception handling in terms of
196 C++ examples.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000197
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000198<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000199<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000200 <a name="throw">Throw</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000201</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000202
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000203<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000204
205<p>Languages that support exception handling typically provide a <tt>throw</tt>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000206 operation to initiate the exception process. Internally, a <tt>throw</tt>
207 operation breaks down into two steps.</p>
208
Bill Wendling4a065dd2011-09-20 01:08:53 +0000209<ol>
210 <li>A request is made to allocate exception space for an exception structure.
211 This structure needs to survive beyond the current activation. This
212 structure will contain the type and value of the object being thrown.</li>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000213
Bill Wendling4a065dd2011-09-20 01:08:53 +0000214 <li>A call is made to the runtime to raise the exception, passing the
215 exception structure as an argument.</li>
216</ol>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000217
Bill Wendling2a6b0732011-09-26 21:06:33 +0000218<p>In C++, the allocation of the exception structure is done by the
Bill Wendling4a065dd2011-09-20 01:08:53 +0000219 <tt>__cxa_allocate_exception</tt> runtime function. The exception raising is
220 handled by <tt>__cxa_throw</tt>. The type of the exception is represented
221 using a C++ RTTI structure.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000222
223</div>
224
225<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000226<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000227 <a name="try_catch">Try/Catch</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000228</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000229
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000230<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000231
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000232<p>A call within the scope of a <i>try</i> statement can potentially raise an
Bill Wendling2a6b0732011-09-26 21:06:33 +0000233 exception. In those circumstances, the LLVM C++ front-end replaces the call
234 with an <tt>invoke</tt> instruction. Unlike a call, the <tt>invoke</tt> has
235 two potential continuation points:</p>
236
237<ol>
238 <li>where to continue when the call succeeds as per normal, and</li>
239
240 <li>where to continue if the call raises an exception, either by a throw or
241 the unwinding of a throw</li>
242</ol>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000243
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000244<p>The term used to define a the place where an <tt>invoke</tt> continues after
Bill Wendling2a6b0732011-09-26 21:06:33 +0000245 an exception is called a <i>landing pad</i>. LLVM landing pads are
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000246 conceptually alternative function entry points where an exception structure
Bill Wendling2a6b0732011-09-26 21:06:33 +0000247 reference and a type info index are passed in as arguments. The landing pad
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000248 saves the exception structure reference and then proceeds to select the catch
249 block that corresponds to the type info of the exception object.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000250
Bill Wendling4a065dd2011-09-20 01:08:53 +0000251<p>The LLVM <a href="LangRef.html#i_landingpad"><tt>landingpad</tt>
252 instruction</a> is used to convey information about the landing pad to the
253 back end. For C++, the <tt>landingpad</tt> instruction returns a pointer and
Bill Wendling2a6b0732011-09-26 21:06:33 +0000254 integer pair corresponding to the pointer to the <i>exception structure</i>
255 and the <i>selector value</i> respectively.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000256
Bill Wendling4a065dd2011-09-20 01:08:53 +0000257<p>The <tt>landingpad</tt> instruction takes a reference to the personality
258 function to be used for this <tt>try</tt>/<tt>catch</tt> sequence. The
Bill Wendling2a6b0732011-09-26 21:06:33 +0000259 remainder of the instruction is a list of <i>cleanup</i>, <i>catch</i>,
260 and <i>filter</i> clauses. The exception is tested against the clauses
261 sequentially from first to last. The selector value is a positive number if
262 the exception matched a type info, a negative number if it matched a filter,
Bill Wendling24771022011-09-27 10:37:28 +0000263 and zero if it matched a cleanup. If nothing is matched, the behavior of
Bill Wendlinga69d9982011-09-26 21:10:31 +0000264 the program is <a href="#restrictions">undefined</a>. If a type info matched,
Bill Wendling2a6b0732011-09-26 21:06:33 +0000265 then the selector value is the index of the type info in the exception table,
266 which can be obtained using the
Bill Wendling4a065dd2011-09-20 01:08:53 +0000267 <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000268
269<p>Once the landing pad has the type info selector, the code branches to the
Bill Wendling4a065dd2011-09-20 01:08:53 +0000270 code for the first catch. The catch then checks the value of the type info
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000271 selector against the index of type info for that catch. Since the type info
Bill Wendling2a6b0732011-09-26 21:06:33 +0000272 index is not known until all the type infos have been gathered in the
273 backend, the catch code must call the
Bill Wendling4a065dd2011-09-20 01:08:53 +0000274 <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic to
275 determine the index for a given type info. If the catch fails to match the
Bill Wendling2a6b0732011-09-26 21:06:33 +0000276 selector then control is passed on to the next catch.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000277
Bill Wendling2a6b0732011-09-26 21:06:33 +0000278<p>Finally, the entry and exit of catch code is bracketed with calls to
279 <tt>__cxa_begin_catch</tt> and <tt>__cxa_end_catch</tt>.</p>
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000280
281<ul>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000282 <li><tt>__cxa_begin_catch</tt> takes an exception structure reference as an
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000283 argument and returns the value of the exception object.</li>
284
Bill Wendling169e1b02009-09-10 22:14:16 +0000285 <li><tt>__cxa_end_catch</tt> takes no arguments. This function:<br><br>
286 <ol>
Bill Wendling808b9ce2009-09-10 22:12:50 +0000287 <li>Locates the most recently caught exception and decrements its handler
288 count,</li>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000289 <li>Removes the exception from the <i>caught</i> stack if the handler
290 count goes to zero, and</li>
291 <li>Destroys the exception if the handler count goes to zero and the
Bill Wendling808b9ce2009-09-10 22:12:50 +0000292 exception was not re-thrown by throw.</li>
Bill Wendling169e1b02009-09-10 22:14:16 +0000293 </ol>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000294 <p><b>Note:</b> a rethrow from within the catch may replace this call with
Bill Wendling808b9ce2009-09-10 22:12:50 +0000295 a <tt>__cxa_rethrow</tt>.</p></li>
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000296</ul>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000297
298</div>
299
300<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000301<h3>
Duncan Sands6590b042007-08-27 15:47:50 +0000302 <a name="cleanups">Cleanups</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000303</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000304
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000305<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000306
Bill Wendling4a065dd2011-09-20 01:08:53 +0000307<p>A cleanup is extra code which needs to be run as part of unwinding a scope.
Bill Wendling2a6b0732011-09-26 21:06:33 +0000308 C++ destructors are a typical example, but other languages and language
309 extensions provide a variety of different kinds of cleanups. In general, a
Bill Wendling4a065dd2011-09-20 01:08:53 +0000310 landing pad may need to run arbitrary amounts of cleanup code before actually
Bill Wendling2a6b0732011-09-26 21:06:33 +0000311 entering a catch block. To indicate the presence of cleanups, a
Bill Wendling4a065dd2011-09-20 01:08:53 +0000312 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a>
313 should have a <i>cleanup</i> clause. Otherwise, the unwinder will not stop at
314 the landing pad if there are no catches or filters that require it to.</p>
John McCalld7c10862011-05-28 07:45:59 +0000315
Bill Wendling2a6b0732011-09-26 21:06:33 +0000316<p><b>Note:</b> Do not allow a new exception to propagate out of the execution
317 of a cleanup. This can corrupt the internal state of the unwinder.
318 Different languages describe different high-level semantics for these
319 situations: for example, C++ requires that the process be terminated, whereas
320 Ada cancels both exceptions and throws a third.</p>
John McCalld7c10862011-05-28 07:45:59 +0000321
Bill Wendling2a6b0732011-09-26 21:06:33 +0000322<p>When all cleanups are finished, if the exception is not handled by the
323 current function, resume unwinding by calling the
Bill Wendling4a065dd2011-09-20 01:08:53 +0000324 <a href="LangRef.html#i_resume"><tt>resume</tt> instruction</a>, passing in
Bill Wendling2a6b0732011-09-26 21:06:33 +0000325 the result of the <tt>landingpad</tt> instruction for the original landing
Bill Wendling4a065dd2011-09-20 01:08:53 +0000326 pad.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000327
328</div>
329
330<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000331<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000332 <a name="throw_filters">Throw Filters</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000333</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000334
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000335<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000336
Bill Wendling2a6b0732011-09-26 21:06:33 +0000337<p>C++ allows the specification of which exception types may be thrown from a
338 function. To represent this, a top level landing pad may exist to filter out
339 invalid types. To express this in LLVM code the
Bill Wendling4a065dd2011-09-20 01:08:53 +0000340 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a> will
341 have a filter clause. The clause consists of an array of type infos.
342 <tt>landingpad</tt> will return a negative value if the exception does not
343 match any of the type infos. If no match is found then a call
344 to <tt>__cxa_call_unexpected</tt> should be made, otherwise
345 <tt>_Unwind_Resume</tt>. Each of these functions requires a reference to the
346 exception structure. Note that the most general form of a
347 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a> can
348 have any number of catch, cleanup, and filter clauses (though having more
349 than one cleanup is pointless). The LLVM C++ front-end can generate such
350 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instructions</a> due
351 to inlining creating nested exception handling scopes.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000352
353</div>
354
355<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000356<h3>
Duncan Sands6590b042007-08-27 15:47:50 +0000357 <a name="restrictions">Restrictions</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000358</h3>
Duncan Sands6590b042007-08-27 15:47:50 +0000359
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000360<div>
Duncan Sands6590b042007-08-27 15:47:50 +0000361
Bill Wendling4a065dd2011-09-20 01:08:53 +0000362<p>The unwinder delegates the decision of whether to stop in a call frame to
Bill Wendling100a3712011-09-27 20:16:57 +0000363 that call frame's language-specific personality function. Not all unwinders
364 guarantee that they will stop to perform cleanups. For example, the GNU C++
365 unwinder doesn't do so unless the exception is actually caught somewhere
366 further up the stack.</p>
John McCalld7c10862011-05-28 07:45:59 +0000367
Bill Wendling4a065dd2011-09-20 01:08:53 +0000368<p>In order for inlining to behave correctly, landing pads must be prepared to
Bill Wendling2a6b0732011-09-26 21:06:33 +0000369 handle selector results that they did not originally advertise. Suppose that
Bill Wendling4a065dd2011-09-20 01:08:53 +0000370 a function catches exceptions of type <tt>A</tt>, and it's inlined into a
Bill Wendling2a6b0732011-09-26 21:06:33 +0000371 function that catches exceptions of type <tt>B</tt>. The inliner will update
Bill Wendling4a065dd2011-09-20 01:08:53 +0000372 the <tt>landingpad</tt> instruction for the inlined landing pad to include
Bill Wendling2a6b0732011-09-26 21:06:33 +0000373 the fact that <tt>B</tt> is also caught. If that landing pad assumes that it
374 will only be entered to catch an <tt>A</tt>, it's in for a rude awakening.
Bill Wendling4a065dd2011-09-20 01:08:53 +0000375 Consequently, landing pads must test for the selector results they understand
376 and then resume exception propagation with the
377 <a href="LangRef.html#i_resume"><tt>resume</tt> instruction</a> if none of
378 the conditions match.</p>
Duncan Sands6590b042007-08-27 15:47:50 +0000379
380</div>
381
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000382</div>
383
Duncan Sands6590b042007-08-27 15:47:50 +0000384<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000385<h2>
Duncan Sands8036ca42007-03-30 12:22:09 +0000386 <a name="format_common_intrinsics">Exception Handling Intrinsics</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000387</h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000388
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000389<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000390
Bill Wendling4a065dd2011-09-20 01:08:53 +0000391<p>In addition to the
392 <a href="LangRef.html#i_landingpad"><tt>landingpad</tt></a> and
393 <a href="LangRef.html#i_resume"><tt>resume</tt></a> instructions, LLVM uses
Bill Wendling2a6b0732011-09-26 21:06:33 +0000394 several intrinsic functions (name prefixed with <i><tt>llvm.eh</tt></i>) to
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000395 provide exception handling information at various points in generated
396 code.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000397
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000398<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000399<h4>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000400 <a name="llvm_eh_typeid_for">llvm.eh.typeid.for</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000401</h4>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000402
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000403<div>
Bill Wendlingbf230bf2009-08-15 20:08:04 +0000404
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000405<pre>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000406 i32 @llvm.eh.typeid.for(i8* %type_info)
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000407</pre>
408
409<p>This intrinsic returns the type info index in the exception table of the
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000410 current function. This value can be used to compare against the result
Bill Wendling3c051962011-09-20 01:14:54 +0000411 of <a href="LangRef.html#i_landingpad"><tt>landingpad</tt> instruction</a>.
412 The single argument is a reference to a type info.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000413
414</div>
415
416<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000417<h4>
Jim Grosbachf9570122009-05-14 00:46:35 +0000418 <a name="llvm_eh_sjlj_setjmp">llvm.eh.sjlj.setjmp</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000419</h4>
Jim Grosbachf9570122009-05-14 00:46:35 +0000420
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000421<div>
Bill Wendling3cf4ffd2009-08-15 20:07:42 +0000422
Jim Grosbachf9570122009-05-14 00:46:35 +0000423<pre>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000424 i32 @llvm.eh.sjlj.setjmp(i8* %setjmp_buf)
Jim Grosbachf9570122009-05-14 00:46:35 +0000425</pre>
426
Bill Wendling2a6b0732011-09-26 21:06:33 +0000427<p>For SJLJ based exception handling, this intrinsic forces register saving for
428 the current function and stores the address of the following instruction for
429 use as a destination address
430 by <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a>. The
431 buffer format and the overall functioning of this intrinsic is compatible
432 with the GCC <tt>__builtin_setjmp</tt> implementation allowing code built
433 with the clang and GCC to interoperate.</p>
Jim Grosbachf9570122009-05-14 00:46:35 +0000434
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000435<p>The single parameter is a pointer to a five word buffer in which the calling
436 context is saved. The front end places the frame pointer in the first word,
437 and the target implementation of this intrinsic should place the destination
438 address for a
439 <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a> in the
440 second word. The following three words are available for use in a
441 target-specific manner.</p>
Jim Grosbachf9570122009-05-14 00:46:35 +0000442
Benjamin Kramere15192b2009-08-05 15:42:44 +0000443</div>
444
Jim Grosbachf9570122009-05-14 00:46:35 +0000445<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000446<h4>
Jim Grosbach90346e22010-05-26 16:21:41 +0000447 <a name="llvm_eh_sjlj_longjmp">llvm.eh.sjlj.longjmp</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000448</h4>
Jim Grosbach90346e22010-05-26 16:21:41 +0000449
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000450<div>
Jim Grosbach90346e22010-05-26 16:21:41 +0000451
452<pre>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000453 void @llvm.eh.sjlj.longjmp(i8* %setjmp_buf)
Jim Grosbach90346e22010-05-26 16:21:41 +0000454</pre>
455
Bill Wendling2a6b0732011-09-26 21:06:33 +0000456<p>For SJLJ based exception handling, the <tt>llvm.eh.sjlj.longjmp</tt>
457 intrinsic is used to implement <tt>__builtin_longjmp()</tt>. The single
458 parameter is a pointer to a buffer populated
459 by <a href="#llvm_eh_sjlj_setjmp"><tt>llvm.eh.sjlj.setjmp</tt></a>. The frame
460 pointer and stack pointer are restored from the buffer, then control is
461 transferred to the destination address.</p>
Jim Grosbach90346e22010-05-26 16:21:41 +0000462
463</div>
464<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000465<h4>
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000466 <a name="llvm_eh_sjlj_lsda">llvm.eh.sjlj.lsda</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000467</h4>
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000468
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000469<div>
Bill Wendling3cf4ffd2009-08-15 20:07:42 +0000470
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000471<pre>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000472 i8* @llvm.eh.sjlj.lsda()
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000473</pre>
474
Bill Wendling2a6b0732011-09-26 21:06:33 +0000475<p>For SJLJ based exception handling, the <tt>llvm.eh.sjlj.lsda</tt> intrinsic
476 returns the address of the Language Specific Data Area (LSDA) for the current
477 function. The SJLJ front-end code stores this address in the exception
478 handling function context for use by the runtime.</p>
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000479
480</div>
481
482<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000483<h4>
Jim Grosbachca752c92010-01-28 01:45:32 +0000484 <a name="llvm_eh_sjlj_callsite">llvm.eh.sjlj.callsite</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000485</h4>
Jim Grosbachca752c92010-01-28 01:45:32 +0000486
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000487<div>
Jim Grosbachca752c92010-01-28 01:45:32 +0000488
489<pre>
Bill Wendling2a6b0732011-09-26 21:06:33 +0000490 void @llvm.eh.sjlj.callsite(i32 %call_site_num)
Jim Grosbachca752c92010-01-28 01:45:32 +0000491</pre>
492
Bill Wendling2a6b0732011-09-26 21:06:33 +0000493<p>For SJLJ based exception handling, the <tt>llvm.eh.sjlj.callsite</tt>
494 intrinsic identifies the callsite value associated with the
495 following <tt>invoke</tt> instruction. This is used to ensure that landing
496 pad entries in the LSDA are generated in matching order.</p>
Jim Grosbachca752c92010-01-28 01:45:32 +0000497
498</div>
499
500<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000501<h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000502 <a name="asm">Asm Table Formats</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000503</h2>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000504
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000505<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000506
507<p>There are two tables that are used by the exception handling runtime to
Bill Wendling2a6b0732011-09-26 21:06:33 +0000508 determine which actions should be taken when an exception is thrown.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000509
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000510<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000511<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000512 <a name="unwind_tables">Exception Handling Frame</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000513</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000514
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000515<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000516
517<p>An exception handling frame <tt>eh_frame</tt> is very similar to the unwind
Bill Wendling2a6b0732011-09-26 21:06:33 +0000518 frame used by DWARF debug info. The frame contains all the information
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000519 necessary to tear down the current frame and restore the state of the prior
Bill Wendling2a6b0732011-09-26 21:06:33 +0000520 frame. There is an exception handling frame for each function in a compile
Bill Wendlingc4f661e2009-08-15 08:56:09 +0000521 unit, plus a common exception handling frame that defines information common
522 to all functions in the unit.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000523
Bill Wendling2a6b0732011-09-26 21:06:33 +0000524<!-- Todo - Table details here. -->
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000525
526</div>
527
528<!-- ======================================================================= -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000529<h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000530 <a name="exception_tables">Exception Tables</a>
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000531</h3>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000532
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000533<div>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000534
535<p>An exception table contains information about what actions to take when an
Bill Wendling2a6b0732011-09-26 21:06:33 +0000536 exception is thrown in a particular part of a function's code. There is one
537 exception table per function, except leaf functions and functions that have
538 calls only to non-throwing functions. They do not need an exception
539 table.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000540
Bill Wendling2a6b0732011-09-26 21:06:33 +0000541<!-- Todo - Table details here. -->
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000542
543</div>
544
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000545</div>
546
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000547<!-- *********************************************************************** -->
548
549<hr>
550<address>
551 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000552 src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000553 <a href="http://validator.w3.org/check/referer"><img
Misha Brukman44408702008-12-11 17:34:48 +0000554 src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000555
NAKAMURA Takumib9a33632011-04-09 02:13:37 +0000556 <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000557 Last modified: $Date$
558</address>
559
560</body>
561</html>