[External] : are CodeElements unique?
Adam Sotona
adam.sotona at oracle.com
Mon May 12 09:56:26 UTC 2025
Hi Mark,
Code elements are generally not unique, so they cannot be used as keys in maps.
However, there are several approaches to work around it (based on the use case):
* If you need to iterate over a list of elements multiple times and then transform it – you can use the index of the element as a key.
* Or if you have a complex set of chained and nested transformations and need to mark a code element for later identification, you can bind a label (CodeBuilder::newBoundLabel) to a position before, after, or instead of the element. Labels are unique, and your subsequent transformations will receive a LabelTarget pseudo-instruction at the marked point, and LabelTarget::label will match the label you bound earlier.
Adam
From: classfile-api-dev <classfile-api-dev-retn at openjdk.org> on behalf of Mark Roberts <markro at cs.washington.edu>
Date: Wednesday, 7 May 2025 at 21:06
To: Chen Liang <chen.l.liang at oracle.com>, classfile-api-dev at openjdk.org <classfile-api-dev at openjdk.org>
Subject: RE: [External] : are CodeElements unique?
How about only for the original, immutable elementlist prior to any transforms?
Also, not sure what you meant by a (index, element) pair.
Thanks, Mark
From: Chen Liang <chen.l.liang at oracle.com<mailto:chen.l.liang at oracle.com>>
Sent: Wednesday, May 7, 2025 11:03 AM
To: Mark Roberts <markro at cs.washington.edu<mailto:markro at cs.washington.edu>>; classfile-api-dev at openjdk.org<mailto:classfile-api-dev at openjdk.org>
Subject: Re: [External] : are CodeElements unique?
Hi Mark,
Very good question - and the answer is unfortunately, no. Transforms are allowed to reuse code models, and I think the bound Code attribute expansion also returns duplicate load or store instructions.
To ensure identity, I think tracking a pair of (index, element) is better.
Regards,
Chen Liang
________________________________
From: Mark Roberts <markro at cs.washington.edu<mailto:markro at cs.washington.edu>>
Sent: Wednesday, May 7, 2025 12:43 PM
To: classfile-api-dev at openjdk.org<mailto:classfile-api-dev at openjdk.org> <classfile-api-dev at openjdk.org<mailto:classfile-api-dev at openjdk.org>>
Cc: Chen Liang <chen.l.liang at oracle.com<mailto:chen.l.liang at oracle.com>>
Subject: [External] : are CodeElements unique?
Are the members of a CodeModel.elementList() unique? Specifically, is it safe to use a CodeElement as the key value in a Map?
Thank you,
Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/classfile-api-dev/attachments/20250512/c4ac76d8/attachment-0001.htm>
More information about the classfile-api-dev
mailing list