ExceptionRegion modeling issues and proposed improvements

Adam Sotona adam.sotona at oracle.com
Thu Oct 10 15:39:27 UTC 2024



From: Paul Sandoz <paul.sandoz at oracle.com>

> In your modeling approach (ignoring multi-catch for now) I think it may be possible to refer directly to the catch blocks as successors for both exit and enter, thereby avoiding a level of indirection.

I think it would be a good start with significant effect. However, the position of each handler in the exception region stack would be very unclear and I would implicitly position them out of any stack. It is much simple to calculate which exception regions should each handler re-enter, rather than which regions to exit. It will also make much simpler modeling of variables modified in the try blocks. Exception regions exit and immediate re-enter after the variable modification fixes the flow graph.


> It would be interesting to expand on your example to include finally

Finally blocks are just complex compositions of try/catch blocks (with duplications of the finally block code to all exit points).
When lifting from bytecode I do not recognize try/finally, it would require further analysis to identify specific try/catch structures and lift it to try/finally.

The example code:
    static void nestedCatch(int i) {
        PrintStream out = System.out;
        while (true) {
            try {
                try {
                    if (i == 0) {
                        break;
                    } else if (i == 1) {
                        continue;
                    }
                } catch (NullPointerException npe) {
                    out.println("CATCH NPE");
                } finally {
                    out.println("FINALLY INNER");
                }
                out.println("AFTER TRY INNER");
            } catch (RuntimeException re) {
                out.println("CATCH RE");
            } finally {
                out.println("FINALLY OUTER");
            }
        }
    }

Can be desugar the try/finally as:
    static void nestedCatch(int i) {
        PrintStream out = System.out;
        while (true) {
            try {
                try {
                    try {
                        try {
                            if (i == 0) {
                                out.println("FINALLY INNER");
                                out.println("FINALLY OUTER");
                                break;
                            } else if (i == 1) {
                                out.println("FINALLY INNER");
                                out.println("FINALLY OUTER");
                                continue;
                            }
                        } catch (NullPointerException npe) {
                            out.println("CATCH NPE");
                        }
                    } catch (Throwable t)  {
                        out.println("FINALLY INNER");
                        throw t;
                    }
                    out.println("FINALLY INNER");
                    out.println("AFTER TRY INNER");
                } catch (RuntimeException re) {
                    out.println("CATCH RE");
                }
            } catch (Throwable t)  {
                out.println("FINALLY OUTER");
                throw t;
            }
            out.println("FINALLY OUTER");
        }
    }

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/babylon-dev/attachments/20241010/e986d07e/attachment-0001.htm>


More information about the babylon-dev mailing list