1 /*
  2  * Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package jdk.internal.classfile.impl;
 26 
 27 import java.lang.classfile.*;
 28 import java.lang.classfile.attribute.CodeAttribute;
 29 import java.lang.classfile.attribute.RuntimeInvisibleTypeAnnotationsAttribute;
 30 import java.lang.classfile.attribute.RuntimeVisibleTypeAnnotationsAttribute;
 31 import java.lang.classfile.attribute.StackMapTableAttribute;
 32 import java.lang.classfile.attribute.UnknownAttribute;
 33 import java.lang.classfile.constantpool.ClassEntry;
 34 import java.lang.classfile.instruction.*;
 35 import java.util.ArrayList;
 36 import java.util.Collections;
 37 import java.util.List;
 38 import java.util.Objects;
 39 import java.util.Optional;
 40 import java.util.function.Consumer;
 41 
 42 import static jdk.internal.classfile.impl.RawBytecodeHelper.*;
 43 
 44 public final class CodeImpl
 45         extends BoundAttribute.BoundCodeAttribute
 46         implements LabelContext {
 47 
 48     static final Instruction[] SINGLETON_INSTRUCTIONS = new Instruction[256];
 49 
 50     static {
 51         for (var o : Opcode.values()) {
 52             if (o.sizeIfFixed() == 1) {
 53                 SINGLETON_INSTRUCTIONS[o.bytecode()] = switch (o.kind()) {
 54                     case ARRAY_LOAD -> ArrayLoadInstruction.of(o);
 55                     case ARRAY_STORE -> ArrayStoreInstruction.of(o);
 56                     case CONSTANT -> ConstantInstruction.ofIntrinsic(o);
 57                     case CONVERT -> ConvertInstruction.of(o);
 58                     case LOAD -> new AbstractInstruction.UnboundLoadInstruction(o, BytecodeHelpers.intrinsicLoadSlot(o));
 59                     case MONITOR -> MonitorInstruction.of(o);
 60                     case NOP -> NopInstruction.of();
 61                     case OPERATOR -> OperatorInstruction.of(o);
 62                     case RETURN -> ReturnInstruction.of(o);
 63                     case STACK -> StackInstruction.of(o);
 64                     case STORE -> new AbstractInstruction.UnboundStoreInstruction(o, BytecodeHelpers.intrinsicStoreSlot(o));
 65                     case THROW_EXCEPTION -> ThrowInstruction.of();
 66                     default -> throw new AssertionError("invalid opcode: " + o);
 67                 };
 68             }
 69         }
 70     }
 71 
 72     List<ExceptionCatch> exceptionTable;
 73     List<Attribute<?>> attributes;
 74 
 75     // Inflated for iteration
 76     LabelImpl[] labels;
 77     int[] lineNumbers;
 78     boolean inflated;
 79 
 80     public CodeImpl(AttributedElement enclosing,
 81                     ClassReader reader,
 82                     AttributeMapper<CodeAttribute> mapper,
 83                     int payloadStart) {
 84         super(enclosing, reader, mapper, payloadStart);
 85     }
 86 
 87     // LabelContext
 88 
 89     @Override
 90     public Label newLabel() {
 91         throw new UnsupportedOperationException("CodeAttribute only supports fixed labels");
 92     }
 93 
 94     @Override
 95     public void setLabelTarget(Label label, int bci) {
 96         throw new UnsupportedOperationException("CodeAttribute only supports fixed labels");
 97     }
 98 
 99     @Override
100     public Label getLabel(int bci) {
101         if (bci < 0 || bci > codeLength)
102             throw new IllegalArgumentException(String.format("Bytecode offset out of range; bci=%d, codeLength=%d",
103                                                              bci, codeLength));
104         if (labels == null)
105             labels = new LabelImpl[codeLength + 1];
106         LabelImpl l = labels[bci];
107         if (l == null)
108             l = labels[bci] = new LabelImpl(this, bci);
109         return l;
110     }
111 
112     @Override
113     public int labelToBci(Label label) {
114         LabelImpl lab = (LabelImpl) label;
115         if (lab.labelContext() != this)
116             throw new IllegalArgumentException(String.format("Illegal label reuse; context=%s, label=%s",
117                                                              this, lab.labelContext()));
118         return lab.getBCI();
119     }
120 
121     private void inflateMetadata() {
122         if (!inflated) {
123             if (labels == null)
124                 labels = new LabelImpl[codeLength + 1];
125             if (classReader.context().passLineNumbers())
126                 inflateLineNumbers();
127             inflateJumpTargets();
128             inflateTypeAnnotations();
129             inflated = true;
130         }
131     }
132 
133     // CodeAttribute
134 
135     @Override
136     public List<Attribute<?>> attributes() {
137         if (attributes == null) {
138             attributes = BoundAttribute.readAttributes(this, classReader, attributePos, classReader.customAttributes());
139         }
140         return attributes;
141     }
142 
143     @Override
144     public void writeTo(BufWriterImpl buf) {
145         var methodInfo = (MethodInfo) enclosingMethod;
146         if (Util.canSkipMethodInflation(classReader, methodInfo, buf)) {
147             super.writeTo(buf);
148         }
149         else {
150             DirectCodeBuilder.build(methodInfo,
151                                     Util.writingAll(this),
152                                     (SplitConstantPool)buf.constantPool(),
153                                     buf.context(),
154                                     null).writeTo(buf);
155         }
156     }
157 
158     // CodeModel
159 
160     @Override
161     public Optional<MethodModel> parent() {
162         return Optional.of(enclosingMethod);
163     }
164 
165     @Override
166     public void forEach(Consumer<? super CodeElement> consumer) {
167         Objects.requireNonNull(consumer);
168         inflateMetadata();
169         boolean doLineNumbers = (lineNumbers != null);
170         generateCatchTargets(consumer);
171         if (classReader.context().passDebugElements())
172             generateDebugElements(consumer);
173         generateUserAttributes(consumer);
174         for (int pos=codeStart; pos<codeEnd; ) {
175             if (labels[pos - codeStart] != null)
176                 consumer.accept(labels[pos - codeStart]);
177             if (doLineNumbers && lineNumbers[pos - codeStart] != 0)
178                 consumer.accept(LineNumberImpl.of(lineNumbers[pos - codeStart]));
179             int bc = classReader.readU1(pos);
180             Instruction instr = bcToInstruction(bc, pos);
181             consumer.accept(instr);
182             pos += instr.sizeInBytes();
183         }
184         // There might be labels pointing to the bci at codeEnd
185         if (labels[codeEnd-codeStart] != null)
186             consumer.accept(labels[codeEnd - codeStart]);
187         if (doLineNumbers && lineNumbers[codeEnd - codeStart] != 0)
188             consumer.accept(LineNumberImpl.of(lineNumbers[codeEnd - codeStart]));
189     }
190 
191     @Override
192     public List<ExceptionCatch> exceptionHandlers() {
193         if (exceptionTable == null) {
194             inflateMetadata();
195             exceptionTable = new ArrayList<>(exceptionHandlerCnt);
196             iterateExceptionHandlers(new ExceptionHandlerAction() {
197                 @Override
198                 public void accept(int s, int e, int h, int c) {
199                     ClassEntry catchTypeEntry = c == 0
200                                                              ? null
201                                                              : constantPool().entryByIndex(c, ClassEntry.class);
202                     exceptionTable.add(new AbstractPseudoInstruction.ExceptionCatchImpl(getLabel(h), getLabel(s), getLabel(e), catchTypeEntry));
203                 }
204             });
205             exceptionTable = Collections.unmodifiableList(exceptionTable);
206         }
207         return exceptionTable;
208     }
209 
210     private void generateUserAttributes(Consumer<? super CodeElement> consumer) {
211         for (var attr : attributes) {
212             if (attr instanceof CustomAttribute || attr instanceof UnknownAttribute) {
213                 consumer.accept((CodeElement) attr);
214             }
215         }
216     }
217 
218     public boolean compareCodeBytes(BufWriterImpl buf, int offset, int len) {
219         return codeLength == len
220                && classReader.compare(buf, offset, codeStart, codeLength);
221     }
222 
223     private int adjustForObjectOrUninitialized(int bci) {
224         int vt = classReader.readU1(bci);
225         //inflate newTarget labels from Uninitialized VTIs
226         if (vt == 8) inflateLabel(classReader.readU2(bci + 1));
227         return (vt == 7 || vt == 8) ? bci + 3 : bci + 1;
228     }
229 
230     private void inflateLabel(int bci) {
231         if (bci < 0 || bci > codeLength)
232             throw new IllegalArgumentException(String.format("Bytecode offset out of range; bci=%d, codeLength=%d",
233                                                              bci, codeLength));
234         if (labels[bci] == null)
235             labels[bci] = new LabelImpl(this, bci);
236     }
237 
238     private void inflateLineNumbers() {
239         for (Attribute<?> a : attributes()) {
240             if (a.attributeMapper() == Attributes.lineNumberTable()) {
241                 BoundLineNumberTableAttribute attr = (BoundLineNumberTableAttribute) a;
242                 if (lineNumbers == null)
243                     lineNumbers = new int[codeLength + 1];
244 
245                 int nLn = classReader.readU2(attr.payloadStart);
246                 int p = attr.payloadStart + 2;
247                 int pEnd = p + (nLn * 4);
248                 for (; p < pEnd; p += 4) {
249                     int startPc = classReader.readU2(p);
250                     if (startPc > codeLength) {
251                         throw new IllegalArgumentException(String.format(
252                                 "Line number start_pc out of range; start_pc=%d, codeLength=%d", startPc, codeLength));
253                     }
254                     int lineNumber = classReader.readU2(p + 2);
255                     lineNumbers[startPc] = lineNumber;
256                 }
257             }
258         }
259     }
260 
261     private void inflateJumpTargets() {
262         Optional<StackMapTableAttribute> a = findAttribute(Attributes.stackMapTable());
263         if (a.isEmpty()) {
264             if (classReader.readU2(6) <= ClassFile.JAVA_6_VERSION) {
265                 //fallback to jump targets inflation without StackMapTableAttribute
266                 for (int pos=codeStart; pos<codeEnd; ) {
267                     var i = bcToInstruction(classReader.readU1(pos), pos);
268                     switch (i.opcode().kind()) {
269                         case BRANCH -> ((BranchInstruction) i).target();
270                         case DISCONTINUED_JSR -> ((DiscontinuedInstruction.JsrInstruction) i).target();
271                         case LOOKUP_SWITCH -> {
272                             var ls = (LookupSwitchInstruction) i;
273                             ls.defaultTarget();
274                             ls.cases();
275                         }
276                         case TABLE_SWITCH -> {
277                             var ts = (TableSwitchInstruction) i;
278                             ts.defaultTarget();
279                             ts.cases();
280                         }
281                         default -> {}
282                     }
283                     pos += i.sizeInBytes();
284                 }
285             }
286             return;
287         }
288         int stackMapPos = ((BoundAttribute<StackMapTableAttribute>) a.get()).payloadStart;
289 
290         int bci = -1; //compensate for offsetDelta + 1
291         int nEntries = classReader.readU2(stackMapPos);
292         int p = stackMapPos + 2;
293         for (int i = 0; i < nEntries; ++i) {
294             int frameType = classReader.readU1(p);
295             int offsetDelta = -1;
296             if (frameType < 64) {
297                 offsetDelta = frameType;
298                 ++p;
299             }
300             else if (frameType < 128) {
301                 offsetDelta = frameType & 0x3f;
302                 p = adjustForObjectOrUninitialized(p + 1);
303             }
304             else {
305                 switch (frameType) {
306                     case StackMapDecoder.EARLY_LARVAL -> {
307                         int numberOfUnsetFields = classReader.readU2(p + 1);
308                         p += 3;
309                         p += 2 * numberOfUnsetFields;
310                         i--; // one more enclosed frame
311                         continue;
312                     }
313                     case 247 -> {
314                         offsetDelta = classReader.readU2(p + 1);
315                         p = adjustForObjectOrUninitialized(p + 3);
316                     }
317                     case 248, 249, 250, 251 -> {
318                         offsetDelta = classReader.readU2(p + 1);
319                         p += 3;
320                     }
321                     case 252, 253, 254 -> {
322                         offsetDelta = classReader.readU2(p + 1);
323                         int k = frameType - 251;
324                         p += 3;
325                         for (int c = 0; c < k; ++c) {
326                             p = adjustForObjectOrUninitialized(p);
327                         }
328                     }
329                     case 255 -> {
330                         offsetDelta = classReader.readU2(p + 1);
331                         p += 3;
332                         int k = classReader.readU2(p);
333                         p += 2;
334                         for (int c = 0; c < k; ++c) {
335                             p = adjustForObjectOrUninitialized(p);
336                         }
337                         k = classReader.readU2(p);
338                         p += 2;
339                         for (int c = 0; c < k; ++c) {
340                             p = adjustForObjectOrUninitialized(p);
341                         }
342                     }
343                     default -> throw new IllegalArgumentException("Bad frame type: " + frameType);
344                 }
345             }
346             bci += offsetDelta + 1;
347             inflateLabel(bci);
348         }
349     }
350 
351     private void inflateTypeAnnotations() {
352         findAttribute(Attributes.runtimeVisibleTypeAnnotations()).ifPresent(RuntimeVisibleTypeAnnotationsAttribute::annotations);
353         findAttribute(Attributes.runtimeInvisibleTypeAnnotations()).ifPresent(RuntimeInvisibleTypeAnnotationsAttribute::annotations);
354     }
355 
356     private void generateCatchTargets(Consumer<? super CodeElement> consumer) {
357         // We attach all catch targets to bci zero, because trying to attach them
358         // to their range could subtly affect the order of exception processing
359         iterateExceptionHandlers(new ExceptionHandlerAction() {
360             @Override
361             public void accept(int s, int e, int h, int c) {
362                 ClassEntry catchType = c == 0
363                                                     ? null
364                                                     : classReader.entryByIndex(c, ClassEntry.class);
365                 consumer.accept(new AbstractPseudoInstruction.ExceptionCatchImpl(getLabel(h), getLabel(s), getLabel(e), catchType));
366             }
367         });
368     }
369 
370     private void generateDebugElements(Consumer<? super CodeElement> consumer) {
371         for (Attribute<?> a : attributes()) {
372             if (a.attributeMapper() == Attributes.characterRangeTable()) {
373                 var attr = (BoundCharacterRangeTableAttribute) a;
374                 int cnt = classReader.readU2(attr.payloadStart);
375                 int p = attr.payloadStart + 2;
376                 int pEnd = p + (cnt * 14);
377                 for (; p < pEnd; p += 14) {
378                     var instruction = new BoundCharacterRange(this, p);
379                     inflateLabel(instruction.startPc());
380                     inflateLabel(instruction.endPc() + 1);
381                     consumer.accept(instruction);
382                 }
383             }
384             else if (a.attributeMapper() == Attributes.localVariableTable()) {
385                 var attr = (BoundLocalVariableTableAttribute) a;
386                 int cnt = classReader.readU2(attr.payloadStart);
387                 int p = attr.payloadStart + 2;
388                 int pEnd = p + (cnt * 10);
389                 for (; p < pEnd; p += 10) {
390                     BoundLocalVariable instruction = new BoundLocalVariable(this, p);
391                     inflateLabel(instruction.startPc());
392                     inflateLabel(instruction.startPc() + instruction.length());
393                     consumer.accept(instruction);
394                 }
395             }
396             else if (a.attributeMapper() == Attributes.localVariableTypeTable()) {
397                 var attr = (BoundLocalVariableTypeTableAttribute) a;
398                 int cnt = classReader.readU2(attr.payloadStart);
399                 int p = attr.payloadStart + 2;
400                 int pEnd = p + (cnt * 10);
401                 for (; p < pEnd; p += 10) {
402                     BoundLocalVariableType instruction = new BoundLocalVariableType(this, p);
403                     inflateLabel(instruction.startPc());
404                     inflateLabel(instruction.startPc() + instruction.length());
405                     consumer.accept(instruction);
406                 }
407             }
408             else if (a.attributeMapper() == Attributes.runtimeVisibleTypeAnnotations()) {
409                 consumer.accept((BoundRuntimeVisibleTypeAnnotationsAttribute) a);
410             }
411             else if (a.attributeMapper() == Attributes.runtimeInvisibleTypeAnnotations()) {
412                 consumer.accept((BoundRuntimeInvisibleTypeAnnotationsAttribute) a);
413             }
414         }
415     }
416 
417     public interface ExceptionHandlerAction {
418         void accept(int start, int end, int handler, int catchTypeIndex);
419     }
420 
421     public void iterateExceptionHandlers(ExceptionHandlerAction a) {
422         int p = exceptionHandlerPos + 2;
423         for (int i = 0; i < exceptionHandlerCnt; ++i) {
424             a.accept(classReader.readU2(p), classReader.readU2(p + 2), classReader.readU2(p + 4), classReader.readU2(p + 6));
425             p += 8;
426         }
427     }
428 
429     private Instruction bcToInstruction(int bc, int pos) {
430         return switch (bc) {
431             case BIPUSH -> new AbstractInstruction.BoundArgumentConstantInstruction(Opcode.BIPUSH, CodeImpl.this, pos);
432             case SIPUSH -> new AbstractInstruction.BoundArgumentConstantInstruction(Opcode.SIPUSH, CodeImpl.this, pos);
433             case LDC -> new AbstractInstruction.BoundLoadConstantInstruction(Opcode.LDC, CodeImpl.this, pos);
434             case LDC_W -> new AbstractInstruction.BoundLoadConstantInstruction(Opcode.LDC_W, CodeImpl.this, pos);
435             case LDC2_W -> new AbstractInstruction.BoundLoadConstantInstruction(Opcode.LDC2_W, CodeImpl.this, pos);
436             case ILOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.ILOAD, CodeImpl.this, pos);
437             case LLOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.LLOAD, CodeImpl.this, pos);
438             case FLOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.FLOAD, CodeImpl.this, pos);
439             case DLOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.DLOAD, CodeImpl.this, pos);
440             case ALOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.ALOAD, CodeImpl.this, pos);
441             case ISTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.ISTORE, CodeImpl.this, pos);
442             case LSTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.LSTORE, CodeImpl.this, pos);
443             case FSTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.FSTORE, CodeImpl.this, pos);
444             case DSTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.DSTORE, CodeImpl.this, pos);
445             case ASTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.ASTORE, CodeImpl.this, pos);
446             case IINC -> new AbstractInstruction.BoundIncrementInstruction(Opcode.IINC, CodeImpl.this, pos);
447             case IFEQ -> new AbstractInstruction.BoundBranchInstruction(Opcode.IFEQ, CodeImpl.this, pos);
448             case IFNE -> new AbstractInstruction.BoundBranchInstruction(Opcode.IFNE, CodeImpl.this, pos);
449             case IFLT -> new AbstractInstruction.BoundBranchInstruction(Opcode.IFLT, CodeImpl.this, pos);
450             case IFGE -> new AbstractInstruction.BoundBranchInstruction(Opcode.IFGE, CodeImpl.this, pos);
451             case IFGT -> new AbstractInstruction.BoundBranchInstruction(Opcode.IFGT, CodeImpl.this, pos);
452             case IFLE -> new AbstractInstruction.BoundBranchInstruction(Opcode.IFLE, CodeImpl.this, pos);
453             case IF_ICMPEQ -> new AbstractInstruction.BoundBranchInstruction(Opcode.IF_ICMPEQ, CodeImpl.this, pos);
454             case IF_ICMPNE -> new AbstractInstruction.BoundBranchInstruction(Opcode.IF_ICMPNE, CodeImpl.this, pos);
455             case IF_ICMPLT -> new AbstractInstruction.BoundBranchInstruction(Opcode.IF_ICMPLT, CodeImpl.this, pos);
456             case IF_ICMPGE -> new AbstractInstruction.BoundBranchInstruction(Opcode.IF_ICMPGE, CodeImpl.this, pos);
457             case IF_ICMPGT -> new AbstractInstruction.BoundBranchInstruction(Opcode.IF_ICMPGT, CodeImpl.this, pos);
458             case IF_ICMPLE -> new AbstractInstruction.BoundBranchInstruction(Opcode.IF_ICMPLE, CodeImpl.this, pos);
459             case IF_ACMPEQ -> new AbstractInstruction.BoundBranchInstruction(Opcode.IF_ACMPEQ, CodeImpl.this, pos);
460             case IF_ACMPNE -> new AbstractInstruction.BoundBranchInstruction(Opcode.IF_ACMPNE, CodeImpl.this, pos);
461             case GOTO -> new AbstractInstruction.BoundBranchInstruction(Opcode.GOTO, CodeImpl.this, pos);
462             case TABLESWITCH -> new AbstractInstruction.BoundTableSwitchInstruction(Opcode.TABLESWITCH, CodeImpl.this, pos);
463             case LOOKUPSWITCH -> new AbstractInstruction.BoundLookupSwitchInstruction(Opcode.LOOKUPSWITCH, CodeImpl.this, pos);
464             case GETSTATIC -> new AbstractInstruction.BoundFieldInstruction(Opcode.GETSTATIC, CodeImpl.this, pos);
465             case PUTSTATIC -> new AbstractInstruction.BoundFieldInstruction(Opcode.PUTSTATIC, CodeImpl.this, pos);
466             case GETFIELD -> new AbstractInstruction.BoundFieldInstruction(Opcode.GETFIELD, CodeImpl.this, pos);
467             case PUTFIELD -> new AbstractInstruction.BoundFieldInstruction(Opcode.PUTFIELD, CodeImpl.this, pos);
468             case INVOKEVIRTUAL -> new AbstractInstruction.BoundInvokeInstruction(Opcode.INVOKEVIRTUAL, CodeImpl.this, pos);
469             case INVOKESPECIAL -> new AbstractInstruction.BoundInvokeInstruction(Opcode.INVOKESPECIAL, CodeImpl.this, pos);
470             case INVOKESTATIC -> new AbstractInstruction.BoundInvokeInstruction(Opcode.INVOKESTATIC, CodeImpl.this, pos);
471             case INVOKEINTERFACE -> new AbstractInstruction.BoundInvokeInterfaceInstruction(Opcode.INVOKEINTERFACE, CodeImpl.this, pos);
472             case INVOKEDYNAMIC -> new AbstractInstruction.BoundInvokeDynamicInstruction(Opcode.INVOKEDYNAMIC, CodeImpl.this, pos);
473             case NEW -> new AbstractInstruction.BoundNewObjectInstruction(CodeImpl.this, pos);
474             case NEWARRAY -> new AbstractInstruction.BoundNewPrimitiveArrayInstruction(Opcode.NEWARRAY, CodeImpl.this, pos);
475             case ANEWARRAY -> new AbstractInstruction.BoundNewReferenceArrayInstruction(Opcode.ANEWARRAY, CodeImpl.this, pos);
476             case CHECKCAST -> new AbstractInstruction.BoundTypeCheckInstruction(Opcode.CHECKCAST, CodeImpl.this, pos);
477             case INSTANCEOF -> new AbstractInstruction.BoundTypeCheckInstruction(Opcode.INSTANCEOF, CodeImpl.this, pos);
478 
479             case WIDE -> {
480                 int bclow = classReader.readU1(pos + 1);
481                 yield switch (bclow) {
482                     case ILOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.ILOAD_W, this, pos);
483                     case LLOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.LLOAD_W, this, pos);
484                     case FLOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.FLOAD_W, this, pos);
485                     case DLOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.DLOAD_W, this, pos);
486                     case ALOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.ALOAD_W, this, pos);
487                     case ISTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.ISTORE_W, this, pos);
488                     case LSTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.LSTORE_W, this, pos);
489                     case FSTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.FSTORE_W, this, pos);
490                     case DSTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.DSTORE_W, this, pos);
491                     case ASTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.ASTORE_W, this, pos);
492                     case IINC -> new AbstractInstruction.BoundIncrementInstruction(Opcode.IINC_W, this, pos);
493                     case RET ->  new AbstractInstruction.BoundRetInstruction(Opcode.RET_W, this, pos);
494                     default -> throw new IllegalArgumentException("unknown wide instruction: " + bclow);
495                 };
496             }
497 
498             case MULTIANEWARRAY -> new AbstractInstruction.BoundNewMultidimensionalArrayInstruction(Opcode.MULTIANEWARRAY, CodeImpl.this, pos);
499             case IFNULL -> new AbstractInstruction.BoundBranchInstruction(Opcode.IFNULL, CodeImpl.this, pos);
500             case IFNONNULL -> new AbstractInstruction.BoundBranchInstruction(Opcode.IFNONNULL, CodeImpl.this, pos);
501             case GOTO_W -> new AbstractInstruction.BoundBranchInstruction(Opcode.GOTO_W, CodeImpl.this, pos);
502 
503             case JSR -> new AbstractInstruction.BoundJsrInstruction(Opcode.JSR, CodeImpl.this, pos);
504             case RET ->  new AbstractInstruction.BoundRetInstruction(Opcode.RET, this, pos);
505             case JSR_W -> new AbstractInstruction.BoundJsrInstruction(Opcode.JSR_W, CodeImpl.this, pos);
506             default -> {
507                 Instruction instr = SINGLETON_INSTRUCTIONS[bc];
508                 if (instr == null)
509                     throw new IllegalArgumentException("unknown instruction: " + bc);
510                 yield instr;
511             }
512         };
513     }
514 
515     @Override
516     public String toString() {
517         return String.format("CodeModel[id=%d]", System.identityHashCode(this));
518     }
519 }