local variable creation and manipulation

Chen Liang chen.l.liang at oracle.com
Tue Nov 12 23:59:53 UTC 2024


Hello Mark,
In bytecode generation with ASM or ClassFile API, we usually do not use a fully-expanded "local variable."  We use a much simpler form, a reference to a local variable slot.

Such a slot is usually allocated with `CodeBuilder::allocateLocal(TypeKind)`.  If you want to create scope-local variables, first create a scope handler with `CodeBuilder::block(Consumer<BlockCodeBuilder>)`, and the slots allocated in the block will not exist outside of the scope.

If you want to create a debug-friendly local variable (type) table entry, use the `localVariable` (used when none of your local variables have generic types) and the `localVariableType` methods.  You can pass in the allocated slot and the start and end labels of your blocks (CodeBuilder::startLabel/endLabel).  This debug information is also the class-file representation of javac-generated local variables.

For stack maps: modern bytecode generation almost always come with a stack map generator; same with the ClassFile API.  You don't have to worry about stack maps as the API already takes care of it for you.

Regards,
Chen Liang
________________________________
From: classfile-api-dev <classfile-api-dev-retn at openjdk.org> on behalf of Mark Roberts <markro at cs.washington.edu>
Sent: Tuesday, November 12, 2024 5:45 PM
To: classfile-api-dev at openjdk.org <classfile-api-dev at openjdk.org>
Subject: local variable creation and manipulation


My name is Mark Roberts and I work in the University of Washington PLSE department.  We have several tools the currently use BCEL to read and modify class files. I would love to replace that with the new java.lang.classfile package – and throw away hundreds of line of code to deal with updating and maintaining StackMaps!  I have only recently started to study the package so I may be missing somethings completely and please let me know if that is the case.



My concern is that I don’t see a high level interface for creating and then using new local variables.  I would like to be able to create a method scoped or a blocked scoped local variable and then be able to refer to it symbolically in the various byte code builders.



Thank you,

Mark


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/classfile-api-dev/attachments/20241112/ba89d89c/attachment.htm>


More information about the classfile-api-dev mailing list