Try/catch/finally builder
Paul Sandoz
paul.sandoz at oracle.com
Wed Aug 24 20:51:19 UTC 2022
Here’s an update:
https://github.com/openjdk/jdk-sandbox/compare/classfile-api-branch...PaulSandoz:jdk-sandbox:try-catch-finally-builder?expand=1
sealed interface CatchFinallyBuilder permits CatchFinallyBuilderImpl {
CatchFinallyBuilder catching(ClassDesc exceptionType, Consumer<BlockCodeBuilder> catchHandler);
default void finally_(Consumer<BlockCodeBuilder> finallyHandler) {
finally_(INLINE_FINALLY_ON_BREAK, finallyHandler);
}
void finally_(BiPredicate<BlockCodeBuilder, BranchInstruction> inlineFinallyTest,
Consumer<BlockCodeBuilder> finallyHandler);
BiPredicate<BlockCodeBuilder, BranchInstruction> INLINE_FINALLY_ON_BREAK =
(b, i) -> b.breakLabel() == i.target();
}
The terminal finally_ builder method accepts an optional predicate.
Paul.
On Aug 24, 2022, at 10:57 AM, Paul Sandoz <paul.sandoz at oracle.com<mailto:paul.sandoz at oracle.com>> wrote:
Or, alternatively, the user can provide an optional BiPredicate<BlockCodeBuilder, BranchInstruction>, which is called for every branch instruction of a try or catch block, and returns true if the branches target is known to exit the block and therefore the finally blocks need to be inlined before it. The default implementation is specified to behave as it does in the prototype, and can be composed.
That seems to give the developer the control they need without explicitly emitting finally blocks, which could get more complex when nesting trying builders.
Paul.
On Aug 23, 2022, at 6:12 PM, Brian Goetz <brian.goetz at oracle.com<mailto:brian.goetz at oracle.com>> wrote:
Maybe the TCB should have an “emit finally” method that causes the finally lambda to be called at that point?
Sent from my MacBook Wheel
On Aug 23, 2022, at 6:39 PM, Paul Sandoz <paul.sandoz at oracle.com<mailto:paul.sandoz at oracle.com>> wrote:
Hi,
Here’s a prototype of a try/catch/finally builder (that should replace the current approach):
https://github.com/openjdk/jdk-sandbox/compare/classfile-api-branch...PaulSandoz:jdk-sandbox:try-catch-finally-builder?expand=1
So far it all seems to work and produces correct code for nested building, generating similar code as the source compiler produces.
I need to do more thorough testing and commit unit tests.
—
One challenge is determining when a block exits. A branching instruction that exits the block should result in inlining of the finally code before the instruction, but it's hard to precisely determine if the branch target is within or outside of the block. At the moment I hard code to checking if the target is the break label of the block, otherwise it is assumed the branch does not exit the block. I don’t think this is generally decidable unless all block instructions are first buffered.
Paul.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/classfile-api-dev/attachments/20220824/5b05cc1d/attachment-0001.htm>
More information about the classfile-api-dev
mailing list