Switch targets are not inflated in CodeModel if no StackMap.

david32768 at btinternet.com david32768 at btinternet.com
Sat Aug 31 07:18:10 UTC 2024


Method jdk/internal/CodeImpl.inflateJumpTargets should also inflate 
switch instructions.
 
Perhaps BranchInstruction, JsrInstruction, TableSwitchInstruction, 
LookupSwitchInstruction should implement an interface with method 
List<Label> targets().
 
build test method
```
   private static void build(CodeBuilder cob) {
       Label L1 = cob.newLabel();
       Label L2 = cob.newLabel();
       SwitchCase sc1 = SwitchCase.of(0,L2);
       List<SwitchCase> cases = new ArrayList<>();
       cases.add(sc1);
       cob.iload(0)
          .lookupswitch(L1, cases)
          .labelBinding(L2)
          .iconst_1()
          .ireturn()
          .labelBinding(L1)
          .iconst_0()
          .ireturn();
   }
```
print of elements in CodeModel
```
Load[OP=ILOAD_0, slot=0]
LookupSwitch[OP=LOOKUPSWITCH]
UnboundIntrinsicConstantInstruction[op=ICONST_1]
Return[OP=IRETURN]
UnboundIntrinsicConstantInstruction[op=ICONST_0]
Return[OP=IRETURN]
```
test driver class
```
   public static void main(String[] args) throws IOException {
       byte[] bytes = ClassFile.of()
               .build(ClassDesc.of("TestSwitchInflate"),
                     clb -> clb.withFlags(ClassFile.ACC_PUBLIC  | 
ClassFile.ACC_SUPER)
               .withVersion(49,0)
               .withMethodBody("Table", 
MethodTypeDesc.ofDescriptor("(I)I"),
                    ClassFile.ACC_STATIC, SwitchInflate::build));
       ClassFile.of().parse(bytes)
           .methods().stream()
                .flatMap(m -> m.code().stream())
               .flatMap(c -> c.elementStream())
               .forEach(System.out::println);
   }
```
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/classfile-api-dev/attachments/20240831/51d9ab4c/attachment.htm>


More information about the classfile-api-dev mailing list