[LLVMdev] Nested exception handlers

Talin viridia at gmail.com
Wed May 27 23:56:15 PDT 2009

Now that I've got exceptions working, I'm kind of wondering how to 
handle the case of nested "try" blocks. Say I have some code that looks 
like this:

  try {
    try {
      if (test) {
        // throw something
      } else {
    } catch e:Except1 {
    } catch e:Except2 {
    } finally {

    // more code...
  } catch e:Except3 {
  } finally {

Turning this into a list of basic blocks is starting to get rather 
complex. In particular:

    * For the innermost try block, there will be 4 possible selectors,
      corresponding to Except1, Except2, Except3 and "other".
          o The first two selectors jump directly to the catch blocks
            for their respective exceptions.
          o The third selector needs to execute the inner 'finally'
            block first.
          o The fourth selector (0 which is a cleanup handler) needs to
            execute both finally blocks and then re-throw the exception.
    * The return statement also needs to execute both finally blocks and
      then return.
    * Then in the outer try block, there will be another exception
      selector with two entries, Except3 and "other" (0).
    * The "fall through" for the outer try block also needs to execute
      the outer finally block.

In addition, the exception object for Except3 can come from two 
different llvm.eh.selector instructions, which means that it either 
needs to be a phi node or it needs to be a memory variable.

I'm wondering if anyone has dealt with this sort of thing and has or  
has advice on how to approach the problem. It seems like I almost need 
to have my higher-level CFG include a "Local JSR" and "Local Return" 
operator which is lowered into code that sets a state variable and then 
does a switch at the end of the finally block to determine where to jump 
back to.

-- Talin

More information about the llvm-dev mailing list