From 6590b0457c5e28a7336ae31de194953d8127217a Mon Sep 17 00:00:00 2001 From: Duncan Sands Date: Mon, 27 Aug 2007 15:47:50 +0000 Subject: There is an impedance matching problem between LLVM and gcc exception handling: if an exception unwinds through an invoke, then execution must branch to the invoke's unwind target. We previously tried to enforce this by appending a cleanup action to every selector, however this does not always work correctly due to an optimization in the C++ unwinding runtime: if only cleanups would be run while unwinding an exception, then the program just terminates without actually executing the cleanups, as invoke semantics would require. I was hoping this wouldn't be a problem, but in fact it turns out to be the cause of all the remaining failures in the LLVM testsuite (these also fail with -enable-correct-eh-support, so turning on -enable-eh didn't make things worse!). Instead we need to append a full-blown catch-all to the end of each selector. The correct way of doing this depends on the personality function, i.e. it is language dependent, so can only be done by gcc. Thus this patch which generalizes the eh.selector intrinsic so that it can handle all possible kinds of action table entries (before it didn't accomodate cleanups): now 0 indicates a cleanup, and filters have to be specified using the number of type infos plus one rather than the number of type infos. Related gcc patches will cause Ada to pass a cleanup (0) to force the selector to always fire, while C++ will use a C++ catch-all (null). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41484 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/ExceptionHandling.html | 86 ++++++++++++++++++++++++++++++++------------- 1 file changed, 62 insertions(+), 24 deletions(-) (limited to 'docs') diff --git a/docs/ExceptionHandling.html b/docs/ExceptionHandling.html index d49a285..eb3c5a2 100644 --- a/docs/ExceptionHandling.html +++ b/docs/ExceptionHandling.html @@ -22,8 +22,9 @@
  1. Throw
  2. Try/Catch
  3. -
  4. Finallys
  5. +
  6. Cleanups
  7. Throw Filters
  8. +
  9. Restrictions
  • Exception Handling Intrinsics
      @@ -212,17 +213,19 @@ further use in the landing pad and catch code.

      three arguments. The first argument is the reference to the exception structure. The second argument is a reference to the personality function to be used for this try catch sequence. Each of the remaining arguments is either a -reference to the type info for a catch statement, or a non-negative integer -followed by that many type info references, representing a -filter. +reference to the type info for a catch statement, +a filter expression, +or the number zero representing a cleanup. The exception is tested against the arguments sequentially from first to last. -The catch all (...) is represented with a null i8*. The result -of the llvm.eh.selector is a positive -number if the exception matched a type info, a negative number if it matched a -filter, and zero if it didn't match anything. If a type info matched then the -returned value is the index of the type info in the exception table. -The LLVM C++ front end generates code to save this value in an alloca location -for further use in the landing pad and catch code.

      +The result of the llvm.eh.selector is a +positive number if the exception matched a type info, a negative number if it matched +a filter, and zero if it matched a cleanup. If nothing is matched, the behaviour of +the program is undefined. +The LLVM C++ front end generates code to save the selector value in an alloca +location for further use in the landing pad and catch code. +If a type info matched then the selector value is the index of the type info in +the exception table, which can be obtained using the +llvm.eh.typeid.for intrinsic.

      Once the landing pad has the type info selector, the code branches to the code for the first catch. The catch then checks the value of the type info @@ -249,7 +252,7 @@ replace this call with a __cxa_rethrow.

      @@ -258,7 +261,12 @@ replace this call with a __cxa_rethrow.

      from a landing pad to the first catch. Control may actually flow from the landing pad to clean up code and then to the first catch. Since the required clean up for each invoke in a try may be different (ex., intervening -constructor), there may be several landing pads for a given try.

      +constructor), there may be several landing pads for a given try. If cleanups +need to be run, the number zero should be passed as the last +llvm.eh.selector argument. +However for C++ a null i8* must be passed +instead. +

      @@ -273,8 +281,8 @@ constructor), there may be several landing pads for a given try.

      a function. To represent this a top level landing pad may exist to filter out invalid types. To express this in LLVM code the landing pad will call llvm.eh.selector. The arguments are the -number of different type infos the function may throw, followed by the type -infos themselves. +length of the filter expression (the number of type infos plus one), followed by +the type infos themselves. llvm.eh.selector will return a negative value if the exception does not match any of the type infos. If no match is found then a call to __cxa_call_unexpected should be made, otherwise @@ -284,6 +292,34 @@ exception structure.

      + + +
      + +

      The semantics of the invoke instruction require that any exception that +unwinds through an invoke call should result in a branch to the invoke's unwind +label. However such a branch will only happen if the +llvm.eh.selector matches. +Thus in order to ensure correct operation, the front-end must only generate +llvm.eh.selector calls that are +guaranteed to always match whatever exception unwinds through the invoke. +For most languages it is enough to pass zero, indicating the presence of +a cleanup, as the last +llvm.eh.selector argument. +However for C++ this is not sufficient, because the C++ personality function +will terminate the program if it detects that unwinding the exception only +results in matches with cleanups. For C++ a null i8* should +be passed as the last +llvm.eh.selector argument instead. +This is interpreted as a catch-all by the C++ personality function, and will +always match. +

      + +
      + + @@ -330,16 +366,18 @@ exception selector.

      llvm.eh.selector takes a minimum of three arguments. The first argument is the reference to the exception structure. The second argument is a reference to the personality function to be -used for this try catch sequence. Each of the remaining arguments is either a -reference to the type info for a catch statement, or a non-negative integer -followed by that many type info references, representing a -filter. +used for this try catch sequence. Each of the remaining arguments is either a +reference to the type info for a catch statement, +a filter expression, +or the number zero representing a cleanup. The exception is tested against the arguments sequentially from first to last. -The catch all (...) is represented with a null i8*. The result -of the llvm.eh.selector is a positive -number if the exception matched a type info, a negative number if it matched a -filter, and zero if it didn't match anything. If a type info matched then the -returned value is the index of the type info in the exception table.

      +The result of the llvm.eh.selector is a +positive number if the exception matched a type info, a negative number if it matched +a filter, and zero if it matched a cleanup. If nothing is matched, the behaviour of +the program is undefined. +If a type info matched then the selector value is the index of the type info in +the exception table, which can be obtained using the +llvm.eh.typeid.for intrinsic.

      -- cgit v1.1