Convenient methods to build switch and try/finally in Classfile API
Adam Sotona
adam.sotona at oracle.com
Fri May 19 09:12:42 UTC 2023
Hi,
Classfile API provides convenient methods for if/then/else or try/catch, however similar methods to build switch or try/finally are missing.
Here I would like to propose new CodeBuilder conveniences:
* lookupswitch(Consumer<SwitchBuilder> switchHandler)
* tableswitch(Consumer<SwitchBuilder> switchHandler)
* tryWithFinalizer(Consumer<CodeBuilder> tryHandler, Consumer<CodeBuilder> finalizerHandler, Label... externalLabels)
* tryWithFinalizer(Consumer<CodeBuilder> tryHandler, Consumer<CodeBuilder> finalizerHandler, boolean compactForm, Label... externalLabels)
and new CodeBuilder.SwitchBuilder interface with methods:
* switchCase(int caseValue, Consumer<BlockCodeBuilder> caseHandler)
* switchCase(List<Integer> caseValues, Consumer<BlockCodeBuilder> caseHandler)
* defaultCase(Consumer<BlockCodeBuilder> defaultHandler)
Sample use case (more can be seen in the tests at https://github.com/openjdk/jdk/pull/14056/files):
cob.lookupswitch(swb -> swb
.switchCase(1, b -> b.iinc(0,1).goto_(b.breakLabel()))
.switchCase(2, b -> b.iinc(0,2).goto_(b.breakLabel()))
.switchCase(3, b -> b.iinc(0,3).goto_(b.breakLabel()))
.defaultCase(b -> b.iinc(0,100).goto_(b.breakLabel()))
.switchCase(4, b -> b.iinc(0,4).goto_(b.breakLabel()))
.switchCase(5, b -> b.iinc(0,5).goto_(b.breakLabel()))
.switchCase(6, b -> b.iinc(0,6).goto_(b.breakLabel())))
cob.tryWithFinalizer(
tryb -> tryb.goto_(externalLabel1),
finb -> finb.constantInstruction(42).ireturn(),
externalLabel1);
The implementation work is in progress. I’m now fiddling with precise placing of finalizers in all possible situations. Trying to do not miss any exit from try block, do not produce dead code, do not fall to double finalizer execution and all that with appropriate test coverage.
The implementation of finalizers now offers two possible modes:
· expanded (as javac does now), where the code flow is unaffected and finalizer block is copied before each exit from the try block
· compact, where identical exits are joined to common copy of the finalizer block (for example all returns are merged into one finalizer and return).
Please let me know your comments and suggestions.
Thanks,
Adam
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/classfile-api-dev/attachments/20230519/34bd2735/attachment-0001.htm>
More information about the classfile-api-dev
mailing list