Classfile API is missing CodeBuilder hook for various instructions-consuming computation tools
Adam Sotona
adam.sotona at oracle.com
Fri Aug 19 14:30:02 UTC 2022
These tools are helpers for specific use cases and they do not represent transformations, but rather they collect and provide information necessary for building code.
For example I need to know actual stack when I plan to use stack-to-locals (and back) function in my generated code.
Another example (which we may embed) is to count max stack when the generator is off.
Optional custom logging of the exact instructions dropped to CodeBuilder is another requested use case.
All of the above are expensive operations, so they should be applied on user request only.
This API does not have much value for transformations, where user can easily listen to each transformed CodeElement and compute whatever is necessary. However when building code using various “magic” (like blocks, constants auto-detection, and other conveniences…) it is important to see what instructions it really builds into.
From: Brian Goetz <brian.goetz at oracle.com>
Date: Friday, 19 August 2022 15:39
To: Adam Sotona <adam.sotona at oracle.com>, classfile-api-dev at openjdk.org <classfile-api-dev at openjdk.org>
Subject: Re: Classfile API is missing CodeBuilder hook for various instructions-consuming computation tools
Before we start to do architectural surgery, let's back up a bit and try to distill some requirements and goals. What problems led you here? Are these tools we expect people to run full-time, or mostly for debugging when something goes wrong? Can they be applied to ordinary classfiles, or do they have to work during building? Could this be framed as a kind of transform, instead of a new concept?
On 8/19/2022 2:34 AM, Adam Sotona wrote:
Hi,
I’ve started working on various helper tools that can give user answers during code building.
Answers to questions like:
1. What is actual instructions count
2. What is actual/max depts of the stack
3. Which locals have been used
4. What kinds or exact types are at locals
5. What kinds or exact types are on stack
6. Show me a log of all instructions
7. …
Each of this question has specific use case and calculation of each answer scarifies different amount of resources.
Unfortunately implementation of tool answering any of the above questions requires:
- either heavy use of BufferedCodeBuilder (and complicate existing building or transformation blocks a lot)
- or hacking into CodeBuilder(s) implementation
I would like to propose a similar approach as Linux “tee” tool does, so user can attach another consumer of the CodeElements passed to the CodeBuilder.
It might be just a single method in CodeBuilder:
default void tee(Consumer<CodeElement> secondListener, Consumer<CodeBuilder> handler) {
handler.accept(new TeeCodeBuilder(this, secondListener));
}
Use case may then look for example like this:
var stackCounter = ...//custom computation tool
codeBuilder.tee(stackCounter, cb -> {
... //block of instructions to be counted
});
stackCounter.actual();
stackCounter.max();
What do you think about this CodeBuilder hook for various instructions-consuming computation tools?
Thanks,
Adam
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/classfile-api-dev/attachments/20220819/fe88fa8e/attachment.htm>
More information about the classfile-api-dev
mailing list