Classfile API is missing CodeBuilder hook for various instructions-consuming computation tools

Adam Sotona adam.sotona at oracle.com
Fri Aug 19 06:34:25 UTC 2022


Hi,
I’ve started working on various helper tools that can give user answers during code building.
Answers to questions like:

  *   What is actual instructions count
  *   What is actual/max depts of the stack
  *   Which locals have been used
  *   What kinds or exact types are at locals
  *   What kinds or exact types are on stack
  *   Show me a log of all instructions
  *   …

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/0b1468d6/attachment.htm>


More information about the classfile-api-dev mailing list