blob: d49a285ce847c3df5549029615e622f29c7cdaea [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>
6 <link rel="stylesheet" href="llvm.css" type="text/css">
7</head>
8<body>
9
10<div class="doc_title">Exception Handling in LLVM</div>
11
12<table class="layout" style="width:100%">
13 <tr class="layout">
14 <td class="left">
15<ul>
16 <li><a href="#introduction">Introduction</a>
17 <ol>
18 <li><a href="#itanium">Itanium ABI Zero-cost Exception Handling</a></li>
19 <li><a href="#overview">Overview</a></li>
20 </ol></li>
21 <li><a href="#codegen">LLVM Code Generation</a>
22 <ol>
23 <li><a href="#throw">Throw</a></li>
24 <li><a href="#try_catch">Try/Catch</a></li>
Duncan Sands8036ca42007-03-30 12:22:09 +000025 <li><a href="#finallys">Finallys</a></li>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000026 <li><a href="#throw_filters">Throw Filters</a></li>
27 </ol></li>
Duncan Sands8036ca42007-03-30 12:22:09 +000028 <li><a href="#format_common_intrinsics">Exception Handling Intrinsics</a>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000029 <ol>
30 <li><a href="#llvm_eh_exception"><tt>llvm.eh.exception</tt></a></li>
31 <li><a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a></li>
Jim Laskeyd0d39b62007-03-14 19:29:42 +000032 <li><a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a></li>
33 </ol></li>
34 <li><a href="#asm">Asm Table Formats</a>
35 <ol>
36 <li><a href="#unwind_tables">Exception Handling Frame</a></li>
37 <li><a href="#exception_tables">Exception Tables</a></li>
38 </ol></li>
39 <li><a href="#todo">ToDo</a></li>
40</ul>
41</td>
42</tr></table>
43
44<div class="doc_author">
45 <p>Written by <a href="mailto:jlaskey@mac.com">Jim Laskey</a></p>
46</div>
47
48
49<!-- *********************************************************************** -->
50<div class="doc_section"><a name="introduction">Introduction</a></div>
51<!-- *********************************************************************** -->
52
53<div class="doc_text">
54
55<p>This document is the central repository for all information pertaining to
56exception handling in LLVM. It describes the format that LLVM exception
57handling information takes, which is useful for those interested in creating
58front-ends or dealing directly with the information. Further, this document
59provides specific examples of what exception handling information is used for
60C/C++.</p>
61
62</div>
63
64<!-- ======================================================================= -->
65<div class="doc_subsection">
66 <a name="itanium">Itanium ABI Zero-cost Exception Handling</a>
67</div>
68
69<div class="doc_text">
70
71<p>Exception handling for most programming languages is designed to recover from
72conditions that rarely occur during general use of an application. To that end,
73exception handling should not interfere with the main flow of an
74application&apos;s algorithm by performing checkpointing tasks such as saving
75the current pc or register state.</p>
76
77<p>The Itanium ABI Exception Handling Specification defines a methodology for
78providing outlying data in the form of exception tables without inlining
79speculative exception handling code in the flow of an application&apos;s main
80algorithm. Thus, the specification is said to add "zero-cost" to the normal
81execution of an application.</p>
82
83<p>A more complete description of the Itanium ABI exception handling runtime
84support of can be found at <a
85href="http://www.codesourcery.com/cxx-abi/abi-eh.html">Itanium C++ ABI:
86Exception Handling.</a> A description of the exception frame format can be
87found at <a
88href="http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-
89Core-generic/ehframechpt.html">Exception Frames</a>, with details of the Dwarf
90specification at <a href="http://www.eagercon.com/dwarf/dwarf3std.htm">Dwarf 3
91Standard.</a> A description for the C++ exception table formats can be found at
92<a href="http://www.codesourcery.com/cxx-abi/exceptions.pdf">Exception Handling
93Tables.</a></p>
94
95</div>
96
97<!-- ======================================================================= -->
98<div class="doc_subsection">
99 <a name="overview">Overview</a>
100</div>
101
102<div class="doc_text">
103
104<p>When an exception is thrown in llvm code, the runtime does a best effort to
105find a handler suited to process the circumstance.</p>
106
107<p>The runtime first attempts to find an <i>exception frame</i> corresponding to
108the function where the exception was thrown. If the programming language (ex.
109C++) supports exception handling, the exception frame contains a reference to an
110exception table describing how to process the exception. If the language (ex.
111C) does not support exception handling or if the exception needs to be forwarded
112to a prior activation, the exception frame contains information about how to
113unwind the current activation and restore the state of the prior activation.
114This process is repeated until the exception is handled. If the exception is
115not handled and no activations remain, then the application is terminated with
116an appropriate error message.</p>
117
118<p>Since different programming languages have different behaviors when handling
119exceptions, the exception handling ABI provides a mechanism for supplying
120<i>personalities.</i> An exception handling personality is defined by way of a
121<i>personality function</i> (ex. for C++ <tt>__gxx_personality_v0</tt>) which
122receives the context of the exception, an <i>exception structure</i> containing
Duncan Sandsfb0a64a2007-04-16 13:02:27 +0000123the exception object type and value, and a reference to the exception table for
124the current function. The personality function for the current compile unit is
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000125specified in a <i>common exception frame</i>.</p>
126
127<p>The organization of an exception table is language dependent. For C++, an
128exception table is organized as a series of code ranges defining what to do if
129an exception occurs in that range. Typically, the information associated with a
130range defines which types of exception objects (using C++ <i>type info</i>) that
131are handled in that range, and an associated action that should take place.
132Actions typically pass control to a <i>landing pad</i>.</p>
133
134<p>A landing pad corresponds to the code found in the catch portion of a
135try/catch sequence. When execution resumes at a landing pad, it receives the
136exception structure and a selector corresponding to the <i>type</i> of exception
137thrown. The selector is then used to determine which catch should actually
138process the exception.</p>
139
140</div>
141
142<!-- ======================================================================= -->
143<div class="doc_section">
144 <a name="codegen">LLVM Code Generation</a>
145</div>
146
147<div class="doc_text">
148
149<p>At the time of this writing, only C++ exception handling support is available
150in LLVM. So the remainder of this document will be somewhat C++-centric.</p>
151
152<p>From the C++ developers perspective, exceptions are defined in terms of the
153<tt>throw</tt> and <tt>try/catch</tt> statements. In this section we will
154describe the implementation of llvm exception handling in terms of C++
155examples.</p>
156
157</div>
158
159<!-- ======================================================================= -->
160<div class="doc_subsection">
161 <a name="throw">Throw</a>
162</div>
163
164<div class="doc_text">
165
166<p>Languages that support exception handling typically provide a <tt>throw</tt>
167operation to initiate the exception process. Internally, a throw operation
168breaks down into two steps. First, a request is made to allocate exception
169space for an exception structure. This structure needs to survive beyond the
170current activation. This structure will contain the type and value of the
171object being thrown. Second, a call is made to the runtime to raise the
172exception, passing the exception structure as an argument.</p>
173
174<p>In C++, the allocation of the exception structure is done by the
175<tt>__cxa_allocate_exception</tt> runtime function. The exception raising is
176handled by <tt>__cxa_throw</tt>. The type of the exception is represented using
177a C++ RTTI type info structure.</p>
178
179</div>
180
181<!-- ======================================================================= -->
182<div class="doc_subsection">
183 <a name="try_catch">Try/Catch</a>
184</div>
185
186<div class="doc_text">
187
Duncan Sandsb0a1cbf2007-04-14 12:30:27 +0000188<p>A call within the scope of a try statement can potentially raise an exception.
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000189In those circumstances, the LLVM C++ front-end replaces the call with an
190<tt>invoke</tt> instruction. Unlike a call, the invoke has two potential
191continuation points; where to continue when the call succeeds as per normal, and
192where to continue if the call raises an exception, either by a throw or the
193unwinding of a throw.</p>
194
195<p>The term used to define a the place where an invoke continues after an
196exception is called a <i>landing pad</i>. LLVM landing pads are conceptually
Duncan Sandsfb0a64a2007-04-16 13:02:27 +0000197alternative function entry points where a exception structure reference and a type
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000198info index are passed in as arguments. The landing pad saves the exception
199structure reference and then proceeds to select the catch block that corresponds
200to the type info of the exception object.</p>
201
202<p>Two llvm intrinsic functions are used convey information about the landing
203pad to the back end.</p>
204
205<p><a href="#llvm_eh_exception"><tt>llvm.eh.exception</tt></a> takes no
206arguments and returns the exception structure reference. The backend replaces
207this intrinsic with the code that accesses the first argument of a call. The
208LLVM C++ front end generates code to save this value in an alloca location for
209further use in the landing pad and catch code.</p>
210
211<p><a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> takes a minimum of
212three arguments. The first argument is the reference to the exception
213structure. The second argument is a reference to the personality function to be
Duncan Sandscf26d7c2007-07-04 20:52:51 +0000214used for this try catch sequence. Each of the remaining arguments is either a
215reference to the type info for a catch statement, or a non-negative integer
216followed by that many type info references, representing a
217<a href="#throw_filters">filter</a>.
218The exception is tested against the arguments sequentially from first to last.
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000219The <i>catch all</i> (...) is represented with a <tt>null i8*</tt>. The result
Duncan Sandscf26d7c2007-07-04 20:52:51 +0000220of the <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> is a positive
221number if the exception matched a type info, a negative number if it matched a
222filter, and zero if it didn't match anything. If a type info matched then the
223returned value is the index of the type info in the exception table.
224The LLVM C++ front end generates code to save this value in an alloca location
225for further use in the landing pad and catch code.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000226
227<p>Once the landing pad has the type info selector, the code branches to the
228code for the first catch. The catch then checks the value of the type info
229selector against the index of type info for that catch. Since the type info
230index is not known until all the type info have been gathered in the backend,
231the catch code will call the <a
232href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic to
233determine the index for a given type info. If the catch fails to match the
234selector then control is passed on to the next catch. Note: Since the landing
235pad will not be used if there is no match in the list of type info on the call
236to <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>, then neither the
237last catch nor <i>catch all</i> need to perform the the check against the
238selector.</p>
239
240<p>Finally, the entry and exit of catch code is bracketed with calls to
241<tt>__cxa_begin_catch</tt> and <tt>__cxa_end_catch</tt>.
242<tt>__cxa_begin_catch</tt> takes a exception structure reference as an argument
243and returns the value of the exception object.</tt> <tt>__cxa_end_catch</tt>
244takes a exception structure reference as an argument. This function clears the
245exception from the exception space. Note: a rethrow from within the catch may
246replace this call with a <tt>__cxa_rethrow</tt>.</p>
247
248</div>
249
250<!-- ======================================================================= -->
251<div class="doc_subsection">
252 <a name="finallys">Finallys</a>
253</div>
254
255<div class="doc_text">
256
257<p>To handle destructors and cleanups in try code, control may not run directly
258from a landing pad to the first catch. Control may actually flow from the
259landing pad to clean up code and then to the first catch. Since the required
260clean up for each invoke in a try may be different (ex., intervening
261constructor), there may be several landing pads for a given try.</p>
262
263</div>
264
265<!-- ======================================================================= -->
266<div class="doc_subsection">
267 <a name="throw_filters">Throw Filters</a>
268</div>
269
270<div class="doc_text">
271
272<p>C++ allows the specification of which exception types that can be thrown from
273a function. To represent this a top level landing pad may exist to filter out
274invalid types. To express this in LLVM code the landing pad will call <a
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000275href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>. The arguments are the
Duncan Sandscf26d7c2007-07-04 20:52:51 +0000276number of different type infos the function may throw, followed by the type
277infos themselves.
278<a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> will return a negative
279value if the exception does not match any of the type infos. If no match is
280found then a call to <tt>__cxa_call_unexpected</tt> should be made, otherwise
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000281<tt>_Unwind_Resume</tt>. Each of these functions require a reference to the
282exception structure.</p>
283
284</div>
285
286<!-- ======================================================================= -->
287<div class="doc_section">
Duncan Sands8036ca42007-03-30 12:22:09 +0000288 <a name="format_common_intrinsics">Exception Handling Intrinsics</a>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000289</div>
290
291<div class="doc_text">
292
293<p>LLVM uses several intrinsic functions (name prefixed with "llvm.eh") to
294provide exception handling information at various points in generated code.</p>
295
296</div>
297
298<!-- ======================================================================= -->
299<div class="doc_subsubsection">
300 <a name="llvm_eh_exception">llvm.eh.exception</a>
301</div>
302
303<div class="doc_text">
304<pre>
305 i8* %<a href="#llvm_eh_exception">llvm.eh.exception</a>( )
306</pre>
307
308<p>This intrinsic indicates that the exception structure is available at this
309point in the code. The backend will replace this intrinsic with code to fetch
310the first argument of a call. The effect is that the intrinsic result is the
311exception structure reference.</p>
312
313</div>
314
315<!-- ======================================================================= -->
316<div class="doc_subsubsection">
317 <a name="llvm_eh_selector">llvm.eh.selector</a>
318</div>
319
320<div class="doc_text">
321<pre>
322 i32 %<a href="#llvm_eh_selector">llvm.eh.selector</a>(i8*, i8*, i8*, ...)
323</pre>
324
325<p>This intrinsic indicates that the exception selector is available at this
326point in the code. The backend will replace this intrinsic with code to fetch
327the second argument of a call. The effect is that the intrinsic result is the
328exception selector.</p>
329
330<p><a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> takes a minimum of
331three arguments. The first argument is the reference to the exception
332structure. The second argument is a reference to the personality function to be
Duncan Sandscf26d7c2007-07-04 20:52:51 +0000333used for this try catch sequence. Each of the remaining arguments is either a
334reference to the type info for a catch statement, or a non-negative integer
335followed by that many type info references, representing a
336<a href="#throw_filters">filter</a>.
337The exception is tested against the arguments sequentially from first to last.
338The <i>catch all</i> (...) is represented with a <tt>null i8*</tt>. The result
339of the <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> is a positive
340number if the exception matched a type info, a negative number if it matched a
341filter, and zero if it didn't match anything. If a type info matched then the
342returned value is the index of the type info in the exception table.</p>
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000343
344</div>
345
346<!-- ======================================================================= -->
347<div class="doc_subsubsection">
348 <a name="llvm_eh_typeid_for">llvm.eh.typeid.for</a>
349</div>
350
351<div class="doc_text">
352<pre>
353 i32 %<a href="#llvm_eh_typeid_for">llvm.eh.typeid.for</a>(i8*)
354</pre>
355
356<p>This intrinsic returns the type info index in the exception table of the
357current function. This value can be used to compare against the result of <a
358href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>. The single argument is
359a reference to a type info.</p>
360
361</div>
362
363<!-- ======================================================================= -->
364<div class="doc_section">
365 <a name="asm">Asm Table Formats</a>
366</div>
367
368<div class="doc_text">
369
370<p>There are two tables that are used by the exception handling runtime to
371determine which actions should take place when an exception is thrown.</p>
372
373</div>
374
375<!-- ======================================================================= -->
376<div class="doc_subsection">
377 <a name="unwind_tables">Exception Handling Frame</a>
378</div>
379
380<div class="doc_text">
381
382<p>An exception handling frame <tt>eh_frame</tt> is very similar to the unwind
383frame used by dwarf debug info. The frame contains all the information
384necessary to tear down the current frame and restore the state of the prior
385frame. There is an exception handling frame for each function in a compile
386unit, plus a common exception handling frame that defines information common to
387all functions in the unit.</p>
388
389<p>Todo - Table details here.</p>
390
391</div>
392
393<!-- ======================================================================= -->
394<div class="doc_subsection">
395 <a name="exception_tables">Exception Tables</a>
396</div>
397
398<div class="doc_text">
399
400<p>An exception table contains information about what actions to take when an
401exception is thrown in a particular part of a function&apos;s code. There is
402one exception table per function except leaf routines and functions that have
403only calls to non-throwing functions will not need an exception table.</p>
404
405<p>Todo - Table details here.</p>
406
407</div>
408
409<!-- ======================================================================= -->
410<div class="doc_section">
411 <a name="todo">ToDo</a>
412</div>
413
414<div class="doc_text">
415
416<ol>
417
Jim Laskeyd0d39b62007-03-14 19:29:42 +0000418<li><p>Testing/Testing/Testing.</li></p>
419
420</ol>
421
422</div>
423
424<!-- *********************************************************************** -->
425
426<hr>
427<address>
428 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
429 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
430 <a href="http://validator.w3.org/check/referer"><img
431 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
432
433 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
434 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
435 Last modified: $Date$
436</address>
437
438</body>
439</html>