Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 1 | <!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 Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 6 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
Eric Christopher | 61f6978 | 2009-09-09 01:44:53 +0000 | [diff] [blame] | 7 | <meta name="description" |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 8 | content="Exception Handling in LLVM."> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 9 | <link rel="stylesheet" href="llvm.css" type="text/css"> |
| 10 | </head> |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 11 | |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 12 | <body> |
| 13 | |
| 14 | <div class="doc_title">Exception Handling in LLVM</div> |
| 15 | |
| 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 Grosbach | 00484d1 | 2009-08-22 01:42:39 +0000 | [diff] [blame] | 23 | <li><a href="#sjlj">Setjmp/Longjmp Exception Handling</a></li> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 24 | <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 Sands | 6590b04 | 2007-08-27 15:47:50 +0000 | [diff] [blame] | 30 | <li><a href="#cleanups">Cleanups</a></li> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 31 | <li><a href="#throw_filters">Throw Filters</a></li> |
Duncan Sands | 6590b04 | 2007-08-27 15:47:50 +0000 | [diff] [blame] | 32 | <li><a href="#restrictions">Restrictions</a></li> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 33 | </ol></li> |
Duncan Sands | 8036ca4 | 2007-03-30 12:22:09 +0000 | [diff] [blame] | 34 | <li><a href="#format_common_intrinsics">Exception Handling Intrinsics</a> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 35 | <ol> |
| 36 | <li><a href="#llvm_eh_exception"><tt>llvm.eh.exception</tt></a></li> |
| 37 | <li><a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a></li> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 38 | <li><a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a></li> |
Jim Grosbach | f957012 | 2009-05-14 00:46:35 +0000 | [diff] [blame] | 39 | <li><a href="#llvm_eh_sjlj_setjmp"><tt>llvm.eh.sjlj.setjmp</tt></a></li> |
| 40 | <li><a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a></li> |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 41 | <li><a href="#llvm_eh_sjlj_lsda"><tt>llvm.eh.sjlj.lsda</tt></a></li> |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 42 | <li><a href="#llvm_eh_sjlj_callsite"><tt>llvm.eh.sjlj.callsite</tt></a></li> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 43 | </ol></li> |
| 44 | <li><a href="#asm">Asm Table Formats</a> |
| 45 | <ol> |
| 46 | <li><a href="#unwind_tables">Exception Handling Frame</a></li> |
| 47 | <li><a href="#exception_tables">Exception Tables</a></li> |
| 48 | </ol></li> |
| 49 | <li><a href="#todo">ToDo</a></li> |
| 50 | </ul> |
| 51 | </td> |
| 52 | </tr></table> |
| 53 | |
| 54 | <div class="doc_author"> |
| 55 | <p>Written by <a href="mailto:jlaskey@mac.com">Jim Laskey</a></p> |
| 56 | </div> |
| 57 | |
| 58 | |
| 59 | <!-- *********************************************************************** --> |
Eric Christopher | 61f6978 | 2009-09-09 01:44:53 +0000 | [diff] [blame] | 60 | <div class="doc_section"><a name="introduction">Introduction</a></div> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 61 | <!-- *********************************************************************** --> |
| 62 | |
| 63 | <div class="doc_text"> |
| 64 | |
| 65 | <p>This document is the central repository for all information pertaining to |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 66 | exception handling in LLVM. It describes the format that LLVM exception |
| 67 | handling information takes, which is useful for those interested in creating |
| 68 | front-ends or dealing directly with the information. Further, this document |
| 69 | provides specific examples of what exception handling information is used for |
| 70 | in C/C++.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 71 | |
| 72 | </div> |
| 73 | |
| 74 | <!-- ======================================================================= --> |
| 75 | <div class="doc_subsection"> |
| 76 | <a name="itanium">Itanium ABI Zero-cost Exception Handling</a> |
| 77 | </div> |
| 78 | |
| 79 | <div class="doc_text"> |
| 80 | |
| 81 | <p>Exception handling for most programming languages is designed to recover from |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 82 | conditions that rarely occur during general use of an application. To that |
| 83 | end, exception handling should not interfere with the main flow of an |
| 84 | application's algorithm by performing checkpointing tasks, such as saving the |
| 85 | current pc or register state.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 86 | |
| 87 | <p>The Itanium ABI Exception Handling Specification defines a methodology for |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 88 | providing outlying data in the form of exception tables without inlining |
| 89 | speculative exception handling code in the flow of an application's main |
| 90 | algorithm. Thus, the specification is said to add "zero-cost" to the normal |
| 91 | execution of an application.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 92 | |
| 93 | <p>A more complete description of the Itanium ABI exception handling runtime |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 94 | support of can be found at |
| 95 | <a href="http://www.codesourcery.com/cxx-abi/abi-eh.html">Itanium C++ ABI: |
| 96 | Exception Handling</a>. A description of the exception frame format can be |
| 97 | found at |
| 98 | <a href="http://refspecs.freestandards.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html">Exception |
| 99 | Frames</a>, with details of the DWARF 3 specification at |
| 100 | <a href="http://www.eagercon.com/dwarf/dwarf3std.htm">DWARF 3 Standard</a>. |
| 101 | A description for the C++ exception table formats can be found at |
| 102 | <a href="http://www.codesourcery.com/cxx-abi/exceptions.pdf">Exception Handling |
| 103 | Tables</a>.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 104 | |
| 105 | </div> |
| 106 | |
| 107 | <!-- ======================================================================= --> |
| 108 | <div class="doc_subsection"> |
Jim Grosbach | 00484d1 | 2009-08-22 01:42:39 +0000 | [diff] [blame] | 109 | <a name="sjlj">Setjmp/Longjmp Exception Handling</a> |
| 110 | </div> |
| 111 | |
| 112 | <div class="doc_text"> |
| 113 | |
| 114 | <p>Setjmp/Longjmp (SJLJ) based exception handling uses LLVM intrinsics |
| 115 | <a href="#llvm_eh_sjlj_setjmp"><tt>llvm.eh.sjlj.setjmp</tt></a> and |
| 116 | <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a> to |
| 117 | handle control flow for exception handling.</p> |
| 118 | |
| 119 | <p>For each function which does exception processing, be it try/catch blocks |
| 120 | or cleanups, that function registers itself on a global frame list. When |
| 121 | exceptions are being unwound, the runtime uses this list to identify which |
| 122 | functions need processing.<p> |
| 123 | |
| 124 | <p>Landing pad selection is encoded in the call site entry of the function |
| 125 | context. The runtime returns to the function via |
| 126 | <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a>, where |
| 127 | a switch table transfers control to the appropriate landing pad based on |
| 128 | the index stored in the function context.</p> |
| 129 | |
| 130 | <p>In contrast to DWARF exception handling, which encodes exception regions |
| 131 | and frame information in out-of-line tables, SJLJ exception handling |
| 132 | builds and removes the unwind frame context at runtime. This results in |
| 133 | faster exception handling at the expense of slower execution when no |
| 134 | exceptions are thrown. As exceptions are, by their nature, intended for |
| 135 | uncommon code paths, DWARF exception handling is generally preferred to |
| 136 | SJLJ.</p> |
| 137 | </div> |
| 138 | |
| 139 | <!-- ======================================================================= --> |
| 140 | <div class="doc_subsection"> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 141 | <a name="overview">Overview</a> |
| 142 | </div> |
| 143 | |
| 144 | <div class="doc_text"> |
| 145 | |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 146 | <p>When an exception is thrown in LLVM code, the runtime does its best to find a |
| 147 | handler suited to processing the circumstance.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 148 | |
| 149 | <p>The runtime first attempts to find an <i>exception frame</i> corresponding to |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 150 | the function where the exception was thrown. If the programming language |
| 151 | (e.g. C++) supports exception handling, the exception frame contains a |
| 152 | reference to an exception table describing how to process the exception. If |
| 153 | the language (e.g. C) does not support exception handling, or if the |
| 154 | exception needs to be forwarded to a prior activation, the exception frame |
| 155 | contains information about how to unwind the current activation and restore |
| 156 | the state of the prior activation. This process is repeated until the |
| 157 | exception is handled. If the exception is not handled and no activations |
| 158 | remain, then the application is terminated with an appropriate error |
| 159 | message.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 160 | |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 161 | <p>Because different programming languages have different behaviors when |
| 162 | handling exceptions, the exception handling ABI provides a mechanism for |
| 163 | supplying <i>personalities.</i> An exception handling personality is defined |
| 164 | by way of a <i>personality function</i> (e.g. <tt>__gxx_personality_v0</tt> |
| 165 | in C++), which receives the context of the exception, an <i>exception |
| 166 | structure</i> containing the exception object type and value, and a reference |
| 167 | to the exception table for the current function. The personality function |
| 168 | for the current compile unit is specified in a <i>common exception |
| 169 | frame</i>.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 170 | |
| 171 | <p>The organization of an exception table is language dependent. For C++, an |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 172 | exception table is organized as a series of code ranges defining what to do |
| 173 | if an exception occurs in that range. Typically, the information associated |
| 174 | with a range defines which types of exception objects (using C++ <i>type |
| 175 | info</i>) that are handled in that range, and an associated action that |
| 176 | should take place. Actions typically pass control to a <i>landing |
| 177 | pad</i>.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 178 | |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 179 | <p>A landing pad corresponds to the code found in the <i>catch</i> portion of |
| 180 | a <i>try</i>/<i>catch</i> sequence. When execution resumes at a landing |
| 181 | pad, it receives the exception structure and a selector corresponding to |
| 182 | the <i>type</i> of exception thrown. The selector is then used to determine |
| 183 | which <i>catch</i> should actually process the exception.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 184 | |
| 185 | </div> |
| 186 | |
| 187 | <!-- ======================================================================= --> |
| 188 | <div class="doc_section"> |
| 189 | <a name="codegen">LLVM Code Generation</a> |
| 190 | </div> |
| 191 | |
| 192 | <div class="doc_text"> |
| 193 | |
| 194 | <p>At the time of this writing, only C++ exception handling support is available |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 195 | in LLVM. So the remainder of this document will be somewhat C++-centric.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 196 | |
| 197 | <p>From the C++ developers perspective, exceptions are defined in terms of the |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 198 | <tt>throw</tt> and <tt>try</tt>/<tt>catch</tt> statements. In this section |
| 199 | we will describe the implementation of LLVM exception handling in terms of |
| 200 | C++ examples.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 201 | |
| 202 | </div> |
| 203 | |
| 204 | <!-- ======================================================================= --> |
| 205 | <div class="doc_subsection"> |
| 206 | <a name="throw">Throw</a> |
| 207 | </div> |
| 208 | |
| 209 | <div class="doc_text"> |
| 210 | |
| 211 | <p>Languages that support exception handling typically provide a <tt>throw</tt> |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 212 | operation to initiate the exception process. Internally, a throw operation |
| 213 | breaks down into two steps. First, a request is made to allocate exception |
| 214 | space for an exception structure. This structure needs to survive beyond the |
| 215 | current activation. This structure will contain the type and value of the |
| 216 | object being thrown. Second, a call is made to the runtime to raise the |
| 217 | exception, passing the exception structure as an argument.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 218 | |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 219 | <p>In C++, the allocation of the exception structure is done by |
| 220 | the <tt>__cxa_allocate_exception</tt> runtime function. The exception |
| 221 | raising is handled by <tt>__cxa_throw</tt>. The type of the exception is |
| 222 | represented using a C++ RTTI structure.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 223 | |
| 224 | </div> |
| 225 | |
| 226 | <!-- ======================================================================= --> |
| 227 | <div class="doc_subsection"> |
| 228 | <a name="try_catch">Try/Catch</a> |
| 229 | </div> |
| 230 | |
| 231 | <div class="doc_text"> |
| 232 | |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 233 | <p>A call within the scope of a <i>try</i> statement can potentially raise an |
| 234 | 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: where to continue when the call succeeds |
| 237 | as per normal; and where to continue if the call raises an exception, either |
| 238 | by a throw or the unwinding of a throw.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 239 | |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 240 | <p>The term used to define a the place where an <tt>invoke</tt> continues after |
| 241 | an exception is called a <i>landing pad</i>. LLVM landing pads are |
| 242 | conceptually alternative function entry points where an exception structure |
| 243 | reference and a type info index are passed in as arguments. The landing pad |
| 244 | saves the exception structure reference and then proceeds to select the catch |
| 245 | block that corresponds to the type info of the exception object.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 246 | |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 247 | <p>Two LLVM intrinsic functions are used to convey information about the landing |
| 248 | pad to the back end.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 249 | |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 250 | <ol> |
| 251 | <li><a href="#llvm_eh_exception"><tt>llvm.eh.exception</tt></a> takes no |
| 252 | arguments and returns a pointer to the exception structure. This only |
| 253 | returns a sensible value if called after an <tt>invoke</tt> has branched |
| 254 | to a landing pad. Due to code generation limitations, it must currently |
| 255 | be called in the landing pad itself.</li> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 256 | |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 257 | <li><a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> takes a minimum |
| 258 | of three arguments. The first argument is the reference to the exception |
| 259 | structure. The second argument is a reference to the personality function |
| 260 | to be used for this <tt>try</tt>/<tt>catch</tt> sequence. Each of the |
| 261 | remaining arguments is either a reference to the type info for |
| 262 | a <tt>catch</tt> statement, a <a href="#throw_filters">filter</a> |
| 263 | expression, or the number zero (<tt>0</tt>) representing |
| 264 | a <a href="#cleanups">cleanup</a>. The exception is tested against the |
| 265 | arguments sequentially from first to last. The result of |
| 266 | the <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> is a |
| 267 | positive number if the exception matched a type info, a negative number if |
| 268 | it matched a filter, and zero if it matched a cleanup. If nothing is |
| 269 | matched, the behaviour of the program |
| 270 | is <a href="#restrictions">undefined</a>. This only returns a sensible |
| 271 | value if called after an <tt>invoke</tt> has branched to a landing pad. |
| 272 | Due to codegen limitations, it must currently be called in the landing pad |
| 273 | itself. If a type info matched, then the selector value is the index of |
| 274 | the type info in the exception table, which can be obtained using the |
| 275 | <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> |
| 276 | intrinsic.</li> |
| 277 | </ol> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 278 | |
| 279 | <p>Once the landing pad has the type info selector, the code branches to the |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 280 | code for the first catch. The catch then checks the value of the type info |
| 281 | selector against the index of type info for that catch. Since the type info |
| 282 | index is not known until all the type info have been gathered in the backend, |
| 283 | the catch code will call the |
| 284 | <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic |
| 285 | to determine the index for a given type info. If the catch fails to match |
| 286 | the selector then control is passed on to the next catch. Note: Since the |
| 287 | landing pad will not be used if there is no match in the list of type info on |
| 288 | the call to <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>, then |
| 289 | neither the last catch nor <i>catch all</i> need to perform the check |
| 290 | against the selector.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 291 | |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 292 | <p>Finally, the entry and exit of catch code is bracketed with calls |
| 293 | to <tt>__cxa_begin_catch</tt> and <tt>__cxa_end_catch</tt>.</p> |
| 294 | |
| 295 | <ul> |
| 296 | <li><tt>__cxa_begin_catch</tt> takes a exception structure reference as an |
| 297 | argument and returns the value of the exception object.</li> |
| 298 | |
Bill Wendling | 169e1b0 | 2009-09-10 22:14:16 +0000 | [diff] [blame] | 299 | <li><tt>__cxa_end_catch</tt> takes no arguments. This function:<br><br> |
| 300 | <ol> |
Bill Wendling | 808b9ce | 2009-09-10 22:12:50 +0000 | [diff] [blame] | 301 | <li>Locates the most recently caught exception and decrements its handler |
| 302 | count,</li> |
| 303 | <li>Removes the exception from the "caught" stack if the handler count |
| 304 | goes to zero, and</li> |
| 305 | <li>Destroys the exception if the handler count goes to zero, and the |
| 306 | exception was not re-thrown by throw.</li> |
Bill Wendling | 169e1b0 | 2009-09-10 22:14:16 +0000 | [diff] [blame] | 307 | </ol> |
Bill Wendling | 808b9ce | 2009-09-10 22:12:50 +0000 | [diff] [blame] | 308 | <p>Note: a rethrow from within the catch may replace this call with |
| 309 | a <tt>__cxa_rethrow</tt>.</p></li> |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 310 | </ul> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 311 | |
| 312 | </div> |
| 313 | |
| 314 | <!-- ======================================================================= --> |
| 315 | <div class="doc_subsection"> |
Duncan Sands | 6590b04 | 2007-08-27 15:47:50 +0000 | [diff] [blame] | 316 | <a name="cleanups">Cleanups</a> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 317 | </div> |
| 318 | |
| 319 | <div class="doc_text"> |
| 320 | |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 321 | <p>To handle destructors and cleanups in <tt>try</tt> code, control may not run |
| 322 | directly from a landing pad to the first catch. Control may actually flow |
| 323 | from the landing pad to clean up code and then to the first catch. Since the |
| 324 | required clean up for each <tt>invoke</tt> in a <tt>try</tt> may be different |
| 325 | (e.g. intervening constructor), there may be several landing pads for a given |
Jim Grosbach | 00484d1 | 2009-08-22 01:42:39 +0000 | [diff] [blame] | 326 | try. If cleanups need to be run, an <tt>i32 0</tt> should be passed as the |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 327 | last <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> argument. |
Jim Grosbach | 00484d1 | 2009-08-22 01:42:39 +0000 | [diff] [blame] | 328 | However, when using DWARF exception handling with C++, a <tt>i8* null</tt> |
| 329 | <a href="#restrictions">must</a> be passed instead.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 330 | |
| 331 | </div> |
| 332 | |
| 333 | <!-- ======================================================================= --> |
| 334 | <div class="doc_subsection"> |
| 335 | <a name="throw_filters">Throw Filters</a> |
| 336 | </div> |
| 337 | |
| 338 | <div class="doc_text"> |
| 339 | |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 340 | <p>C++ allows the specification of which exception types can be thrown from a |
| 341 | function. To represent this a top level landing pad may exist to filter out |
| 342 | invalid types. To express this in LLVM code the landing pad will |
| 343 | call <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>. The |
| 344 | arguments are a reference to the exception structure, a reference to the |
| 345 | personality function, the length of the filter expression (the number of type |
| 346 | infos plus one), followed by the type infos themselves. |
| 347 | <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> will return a |
| 348 | negative value if the exception does not match any of the type infos. If no |
| 349 | match is found then a call to <tt>__cxa_call_unexpected</tt> should be made, |
| 350 | otherwise <tt>_Unwind_Resume</tt>. Each of these functions requires a |
| 351 | reference to the exception structure. Note that the most general form of an |
| 352 | <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> call can contain |
| 353 | any number of type infos, filter expressions and cleanups (though having more |
| 354 | than one cleanup is pointless). The LLVM C++ front-end can generate such |
| 355 | <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> calls due to |
| 356 | inlining creating nested exception handling scopes.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 357 | |
| 358 | </div> |
| 359 | |
| 360 | <!-- ======================================================================= --> |
Duncan Sands | 6590b04 | 2007-08-27 15:47:50 +0000 | [diff] [blame] | 361 | <div class="doc_subsection"> |
| 362 | <a name="restrictions">Restrictions</a> |
| 363 | </div> |
| 364 | |
| 365 | <div class="doc_text"> |
| 366 | |
| 367 | <p>The semantics of the invoke instruction require that any exception that |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 368 | unwinds through an invoke call should result in a branch to the invoke's |
| 369 | unwind label. However such a branch will only happen if the |
| 370 | <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> matches. Thus in |
| 371 | order to ensure correct operation, the front-end must only generate |
| 372 | <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> calls that are |
| 373 | guaranteed to always match whatever exception unwinds through the invoke. |
| 374 | For most languages it is enough to pass zero, indicating the presence of |
| 375 | a <a href="#cleanups">cleanup</a>, as the |
| 376 | last <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> argument. |
| 377 | However for C++ this is not sufficient, because the C++ personality function |
| 378 | will terminate the program if it detects that unwinding the exception only |
| 379 | results in matches with cleanups. For C++ a <tt>null i8*</tt> should be |
| 380 | passed as the last <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> |
| 381 | argument instead. This is interpreted as a catch-all by the C++ personality |
| 382 | function, and will always match.</p> |
Duncan Sands | 6590b04 | 2007-08-27 15:47:50 +0000 | [diff] [blame] | 383 | |
| 384 | </div> |
| 385 | |
| 386 | <!-- ======================================================================= --> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 387 | <div class="doc_section"> |
Duncan Sands | 8036ca4 | 2007-03-30 12:22:09 +0000 | [diff] [blame] | 388 | <a name="format_common_intrinsics">Exception Handling Intrinsics</a> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 389 | </div> |
| 390 | |
| 391 | <div class="doc_text"> |
| 392 | |
| 393 | <p>LLVM uses several intrinsic functions (name prefixed with "llvm.eh") to |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 394 | provide exception handling information at various points in generated |
| 395 | code.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 396 | |
| 397 | </div> |
| 398 | |
| 399 | <!-- ======================================================================= --> |
| 400 | <div class="doc_subsubsection"> |
| 401 | <a name="llvm_eh_exception">llvm.eh.exception</a> |
| 402 | </div> |
| 403 | |
| 404 | <div class="doc_text"> |
Bill Wendling | 3cf4ffd | 2009-08-15 20:07:42 +0000 | [diff] [blame] | 405 | |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 406 | <pre> |
Bill Wendling | 3cf4ffd | 2009-08-15 20:07:42 +0000 | [diff] [blame] | 407 | i8* %<a href="#llvm_eh_exception">llvm.eh.exception</a>( ) |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 408 | </pre> |
| 409 | |
Duncan Sands | 6531d47 | 2008-12-29 15:27:32 +0000 | [diff] [blame] | 410 | <p>This intrinsic returns a pointer to the exception structure.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 411 | |
| 412 | </div> |
| 413 | |
| 414 | <!-- ======================================================================= --> |
| 415 | <div class="doc_subsubsection"> |
| 416 | <a name="llvm_eh_selector">llvm.eh.selector</a> |
| 417 | </div> |
| 418 | |
| 419 | <div class="doc_text"> |
Bill Wendling | 3cf4ffd | 2009-08-15 20:07:42 +0000 | [diff] [blame] | 420 | |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 421 | <pre> |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 422 | i32 %<a href="#llvm_eh_selector">llvm.eh.selector</a>(i8*, i8*, i8*, ...) |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 423 | </pre> |
| 424 | |
Duncan Sands | 6531d47 | 2008-12-29 15:27:32 +0000 | [diff] [blame] | 425 | <p>This intrinsic is used to compare the exception with the given type infos, |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 426 | filters and cleanups.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 427 | |
| 428 | <p><a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> takes a minimum of |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 429 | three arguments. The first argument is the reference to the exception |
| 430 | structure. The second argument is a reference to the personality function to |
| 431 | be used for this try catch sequence. Each of the remaining arguments is |
| 432 | either a reference to the type info for a catch statement, |
| 433 | a <a href="#throw_filters">filter</a> expression, or the number zero |
| 434 | representing a <a href="#cleanups">cleanup</a>. The exception is tested |
| 435 | against the arguments sequentially from first to last. The result of |
| 436 | the <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a> is a positive |
| 437 | number if the exception matched a type info, a negative number if it matched |
| 438 | a filter, and zero if it matched a cleanup. If nothing is matched, the |
| 439 | behaviour of the program is <a href="#restrictions">undefined</a>. If a type |
| 440 | info matched then the selector value is the index of the type info in the |
| 441 | exception table, which can be obtained using the |
| 442 | <a href="#llvm_eh_typeid_for"><tt>llvm.eh.typeid.for</tt></a> intrinsic.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 443 | |
| 444 | </div> |
| 445 | |
| 446 | <!-- ======================================================================= --> |
| 447 | <div class="doc_subsubsection"> |
| 448 | <a name="llvm_eh_typeid_for">llvm.eh.typeid.for</a> |
| 449 | </div> |
| 450 | |
| 451 | <div class="doc_text"> |
Bill Wendling | bf230bf | 2009-08-15 20:08:04 +0000 | [diff] [blame] | 452 | |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 453 | <pre> |
Duncan Sands | b01bbdc | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 454 | i32 %<a href="#llvm_eh_typeid_for">llvm.eh.typeid.for</a>(i8*) |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 455 | </pre> |
| 456 | |
| 457 | <p>This intrinsic returns the type info index in the exception table of the |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 458 | current function. This value can be used to compare against the result |
| 459 | of <a href="#llvm_eh_selector"><tt>llvm.eh.selector</tt></a>. The single |
| 460 | argument is a reference to a type info.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 461 | |
| 462 | </div> |
| 463 | |
| 464 | <!-- ======================================================================= --> |
Jim Grosbach | f957012 | 2009-05-14 00:46:35 +0000 | [diff] [blame] | 465 | <div class="doc_subsubsection"> |
| 466 | <a name="llvm_eh_sjlj_setjmp">llvm.eh.sjlj.setjmp</a> |
| 467 | </div> |
| 468 | |
| 469 | <div class="doc_text"> |
Bill Wendling | 3cf4ffd | 2009-08-15 20:07:42 +0000 | [diff] [blame] | 470 | |
Jim Grosbach | f957012 | 2009-05-14 00:46:35 +0000 | [diff] [blame] | 471 | <pre> |
Bill Wendling | 3cf4ffd | 2009-08-15 20:07:42 +0000 | [diff] [blame] | 472 | i32 %<a href="#llvm_eh_sjlj_setjmp">llvm.eh.sjlj.setjmp</a>(i8*) |
Jim Grosbach | f957012 | 2009-05-14 00:46:35 +0000 | [diff] [blame] | 473 | </pre> |
| 474 | |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 475 | <p>The SJLJ exception handling uses this intrinsic to force register saving for |
| 476 | the current function and to store the address of the following instruction |
| 477 | for use as a destination address by <a href="#llvm_eh_sjlj_longjmp"> |
| 478 | <tt>llvm.eh.sjlj.longjmp</tt></a>. The buffer format and the overall |
| 479 | functioning of this intrinsic is compatible with the GCC |
| 480 | <tt>__builtin_setjmp</tt> implementation, allowing code built with the |
| 481 | two compilers to interoperate.</p> |
Jim Grosbach | f957012 | 2009-05-14 00:46:35 +0000 | [diff] [blame] | 482 | |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 483 | <p>The single parameter is a pointer to a five word buffer in which the calling |
| 484 | context is saved. The front end places the frame pointer in the first word, |
| 485 | and the target implementation of this intrinsic should place the destination |
| 486 | address for a |
| 487 | <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a> in the |
| 488 | second word. The following three words are available for use in a |
| 489 | target-specific manner.</p> |
Jim Grosbach | f957012 | 2009-05-14 00:46:35 +0000 | [diff] [blame] | 490 | |
Benjamin Kramer | e15192b | 2009-08-05 15:42:44 +0000 | [diff] [blame] | 491 | </div> |
| 492 | |
Jim Grosbach | f957012 | 2009-05-14 00:46:35 +0000 | [diff] [blame] | 493 | <!-- ======================================================================= --> |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 494 | <div class="doc_subsubsection"> |
Jim Grosbach | 90346e2 | 2010-05-26 16:21:41 +0000 | [diff] [blame^] | 495 | <a name="llvm_eh_sjlj_longjmp">llvm.eh.sjlj.longjmp</a> |
| 496 | </div> |
| 497 | |
| 498 | <div class="doc_text"> |
| 499 | |
| 500 | <pre> |
| 501 | void %<a href="#llvm_eh_sjlj_longjmp">llvm.eh.sjlj.setjmp</a>(i8*) |
| 502 | </pre> |
| 503 | |
| 504 | <p>The <a href="#llvm_eh_sjlj_longjmp"><tt>llvm.eh.sjlj.longjmp</tt></a> |
| 505 | intrinsic is used to implement <tt>__builtin_longjmp()</tt> for SJLJ |
| 506 | style exception handling. The single parameter is a pointer to a |
| 507 | buffer populated by <a href="#llvm_eh_sjlj_setjmp"> |
| 508 | <tt>llvm.eh.sjlj.setjmp</tt></a>. The frame pointer and stack pointer |
| 509 | are restored from the buffer, then control is transfered to the |
| 510 | destination address.</p> |
| 511 | |
| 512 | </div> |
| 513 | <!-- ======================================================================= --> |
| 514 | <div class="doc_subsubsection"> |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 515 | <a name="llvm_eh_sjlj_lsda">llvm.eh.sjlj.lsda</a> |
| 516 | </div> |
| 517 | |
| 518 | <div class="doc_text"> |
Bill Wendling | 3cf4ffd | 2009-08-15 20:07:42 +0000 | [diff] [blame] | 519 | |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 520 | <pre> |
Bill Wendling | 3cf4ffd | 2009-08-15 20:07:42 +0000 | [diff] [blame] | 521 | i8* %<a href="#llvm_eh_sjlj_lsda">llvm.eh.sjlj.lsda</a>( ) |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 522 | </pre> |
| 523 | |
| 524 | <p>Used for SJLJ based exception handling, the <a href="#llvm_eh_sjlj_lsda"> |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 525 | <tt>llvm.eh.sjlj.lsda</tt></a> intrinsic returns the address of the Language |
| 526 | Specific Data Area (LSDA) for the current function. The SJLJ front-end code |
| 527 | stores this address in the exception handling function context for use by the |
| 528 | runtime.</p> |
Jim Grosbach | 1b747ad | 2009-08-11 00:09:57 +0000 | [diff] [blame] | 529 | |
| 530 | </div> |
| 531 | |
| 532 | <!-- ======================================================================= --> |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 533 | <div class="doc_subsubsection"> |
| 534 | <a name="llvm_eh_sjlj_callsite">llvm.eh.sjlj.callsite</a> |
| 535 | </div> |
| 536 | |
| 537 | <div class="doc_text"> |
| 538 | |
| 539 | <pre> |
| 540 | void %<a href="#llvm_eh_sjlj_callsite">llvm.eh.sjlj.callsite</a>(i32) |
| 541 | </pre> |
| 542 | |
| 543 | <p>For SJLJ based exception handling, the <a href="#llvm_eh_sjlj_callsite"> |
| 544 | <tt>llvm.eh.sjlj.callsite</tt></a> intrinsic identifies the callsite value |
| 545 | associated with the following invoke instruction. This is used to ensure |
| 546 | that landing pad entries in the LSDA are generated in the matching order.</p> |
| 547 | |
| 548 | </div> |
| 549 | |
| 550 | <!-- ======================================================================= --> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 551 | <div class="doc_section"> |
| 552 | <a name="asm">Asm Table Formats</a> |
| 553 | </div> |
| 554 | |
| 555 | <div class="doc_text"> |
| 556 | |
| 557 | <p>There are two tables that are used by the exception handling runtime to |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 558 | determine which actions should take place when an exception is thrown.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 559 | |
| 560 | </div> |
| 561 | |
| 562 | <!-- ======================================================================= --> |
| 563 | <div class="doc_subsection"> |
| 564 | <a name="unwind_tables">Exception Handling Frame</a> |
| 565 | </div> |
| 566 | |
| 567 | <div class="doc_text"> |
| 568 | |
| 569 | <p>An exception handling frame <tt>eh_frame</tt> is very similar to the unwind |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 570 | frame used by dwarf debug info. The frame contains all the information |
| 571 | necessary to tear down the current frame and restore the state of the prior |
| 572 | frame. There is an exception handling frame for each function in a compile |
| 573 | unit, plus a common exception handling frame that defines information common |
| 574 | to all functions in the unit.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 575 | |
| 576 | <p>Todo - Table details here.</p> |
| 577 | |
| 578 | </div> |
| 579 | |
| 580 | <!-- ======================================================================= --> |
| 581 | <div class="doc_subsection"> |
| 582 | <a name="exception_tables">Exception Tables</a> |
| 583 | </div> |
| 584 | |
| 585 | <div class="doc_text"> |
| 586 | |
| 587 | <p>An exception table contains information about what actions to take when an |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 588 | exception is thrown in a particular part of a function's code. There is one |
| 589 | exception table per function except leaf routines and functions that have |
| 590 | only calls to non-throwing functions will not need an exception table.</p> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 591 | |
| 592 | <p>Todo - Table details here.</p> |
| 593 | |
| 594 | </div> |
| 595 | |
| 596 | <!-- ======================================================================= --> |
| 597 | <div class="doc_section"> |
| 598 | <a name="todo">ToDo</a> |
| 599 | </div> |
| 600 | |
| 601 | <div class="doc_text"> |
| 602 | |
| 603 | <ol> |
| 604 | |
Bill Wendling | c4f661e | 2009-08-15 08:56:09 +0000 | [diff] [blame] | 605 | <li>Testing/Testing/Testing.</li> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 606 | |
| 607 | </ol> |
| 608 | |
| 609 | </div> |
| 610 | |
| 611 | <!-- *********************************************************************** --> |
| 612 | |
| 613 | <hr> |
| 614 | <address> |
| 615 | <a href="http://jigsaw.w3.org/css-validator/check/referer"><img |
Misha Brukman | 4440870 | 2008-12-11 17:34:48 +0000 | [diff] [blame] | 616 | src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 617 | <a href="http://validator.w3.org/check/referer"><img |
Misha Brukman | 4440870 | 2008-12-11 17:34:48 +0000 | [diff] [blame] | 618 | src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a> |
Jim Laskey | d0d39b6 | 2007-03-14 19:29:42 +0000 | [diff] [blame] | 619 | |
| 620 | <a href="mailto:sabre@nondot.org">Chris Lattner</a><br> |
| 621 | <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br> |
| 622 | Last modified: $Date$ |
| 623 | </address> |
| 624 | |
| 625 | </body> |
| 626 | </html> |