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.StackMapGenerator.*;
 43 import static jdk.internal.classfile.impl.RawBytecodeHelper.*;
 44 
 45 public final class CodeImpl
 46         extends BoundAttribute.BoundCodeAttribute
 47         implements LabelContext {
 48 
 49     static final Instruction[] SINGLETON_INSTRUCTIONS = new Instruction[256];
 50 
 51     static {
 52         for (var o : Opcode.values()) {
 53             if (o.sizeIfFixed() == 1) {
 54                 SINGLETON_INSTRUCTIONS[o.bytecode()] = switch (o.kind()) {
 55                     case ARRAY_LOAD -> ArrayLoadInstruction.of(o);
 56                     case ARRAY_STORE -> ArrayStoreInstruction.of(o);
 57                     case CONSTANT -> ConstantInstruction.ofIntrinsic(o);
 58                     case CONVERT -> ConvertInstruction.of(o);
 59                     case LOAD -> new AbstractInstruction.UnboundLoadInstruction(o, BytecodeHelpers.intrinsicLoadSlot(o));
 60                     case MONITOR -> MonitorInstruction.of(o);
 61                     case NOP -> NopInstruction.of();
 62                     case OPERATOR -> OperatorInstruction.of(o);
 63                     case RETURN -> ReturnInstruction.of(o);
 64                     case STACK -> StackInstruction.of(o);
 65                     case STORE -> new AbstractInstruction.UnboundStoreInstruction(o, BytecodeHelpers.intrinsicStoreSlot(o));
 66                     case THROW_EXCEPTION -> ThrowInstruction.of();
 67                     default -> throw new AssertionError("invalid opcode: " + o);
 68                 };
 69             }
 70         }
 71     }
 72 
 73     List<ExceptionCatch> exceptionTable;
 74     List<Attribute<?>> attributes;
 75 
 76     // Inflated for iteration
 77     LabelImpl[] labels;
 78     int[] lineNumbers;
 79     boolean inflated;
 80 
 81     public CodeImpl(AttributedElement enclosing,
 82                     ClassReader reader,
 83                     AttributeMapper<CodeAttribute> mapper,
 84                     int payloadStart) {
 85         super(enclosing, reader, mapper, payloadStart);
 86     }
 87 
 88     // LabelContext
 89 
 90     @Override
 91     public Label newLabel() {
 92         throw new UnsupportedOperationException("CodeAttribute only supports fixed labels");
 93     }
 94 
 95     @Override
 96     public void setLabelTarget(Label label, int bci) {
 97         throw new UnsupportedOperationException("CodeAttribute only supports fixed labels");
 98     }
 99 
100     @Override
101     public Label getLabel(int bci) {
102         if (bci < 0 || bci > codeLength)
103             throw new IllegalArgumentException(String.format("Bytecode offset out of range; bci=%d, codeLength=%d",
104                                                              bci, codeLength));
105         if (labels == null)
106             labels = new LabelImpl[codeLength + 1];
107         LabelImpl l = labels[bci];
108         if (l == null)
109             l = labels[bci] = new LabelImpl(this, bci);
110         return l;
111     }
112 
113     @Override
114     public int labelToBci(Label label) {
115         LabelImpl lab = (LabelImpl) label;
116         if (lab.labelContext() != this)
117             throw new IllegalArgumentException(String.format("Illegal label reuse; context=%s, label=%s",
118                                                              this, lab.labelContext()));
119         return lab.getBCI();
120     }
121 
122     private void inflateMetadata() {
123         if (!inflated) {
124             if (labels == null)
125                 labels = new LabelImpl[codeLength + 1];
126             if (classReader.context().passLineNumbers())
127                 inflateLineNumbers();
128             inflateJumpTargets();
129             inflateTypeAnnotations();
130             inflated = true;
131         }
132     }
133 
134     // CodeAttribute
135 
136     @Override
137     public List<Attribute<?>> attributes() {
138         if (attributes == null) {
139             attributes = BoundAttribute.readAttributes(this, classReader, attributePos, classReader.customAttributes());
140         }
141         return attributes;
142     }
143 
144     @Override
145     public void writeTo(BufWriterImpl buf) {
146         if (buf.canWriteDirect(classReader)) {
147             super.writeTo(buf);
148         }
149         else {
150             DirectCodeBuilder.build((MethodInfo) enclosingMethod,
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;
296             if (frameType <= SAME_FRAME_END) {
297                 offsetDelta = frameType;
298                 ++p;
299             }
300             else if (frameType <= SAME_LOCALS_1_STACK_ITEM_FRAME_END) {
301                 offsetDelta = frameType & 0x3f;
302                 p = adjustForObjectOrUninitialized(p + 1);
303             }
304             else
305                 switch (frameType) {
306                     case SAME_LOCALS_1_STACK_ITEM_EXTENDED -> {
307                         offsetDelta = classReader.readU2(p + 1);
308                         p = adjustForObjectOrUninitialized(p + 3);
309                     }
310                     case CHOP_FRAME_START, CHOP_FRAME_START + 1, CHOP_FRAME_END, SAME_FRAME_EXTENDED -> {
311                         offsetDelta = classReader.readU2(p + 1);
312                         p += 3;
313                     }
314                     case APPEND_FRAME_START, APPEND_FRAME_START + 1, APPEND_FRAME_END -> {
315                         offsetDelta = classReader.readU2(p + 1);
316                         int k = frameType - APPEND_FRAME_START + 1;
317                         p += 3;
318                         for (int c = 0; c < k; ++c) {
319                             p = adjustForObjectOrUninitialized(p);
320                         }
321                     }
322                     case FULL_FRAME -> {
323                         offsetDelta = classReader.readU2(p + 1);
324                         p += 3;
325                         int k = classReader.readU2(p);
326                         p += 2;
327                         for (int c = 0; c < k; ++c) {
328                             p = adjustForObjectOrUninitialized(p);
329                         }
330                         k = classReader.readU2(p);
331                         p += 2;
332                         for (int c = 0; c < k; ++c) {
333                             p = adjustForObjectOrUninitialized(p);
334                         }
335                     }
336                     default -> throw new IllegalArgumentException("Bad frame type: " + frameType);
337                 }
338             bci += offsetDelta + 1;
339             inflateLabel(bci);
340         }
341     }
342 
343     private void inflateTypeAnnotations() {
344         findAttribute(Attributes.runtimeVisibleTypeAnnotations()).ifPresent(RuntimeVisibleTypeAnnotationsAttribute::annotations);
345         findAttribute(Attributes.runtimeInvisibleTypeAnnotations()).ifPresent(RuntimeInvisibleTypeAnnotationsAttribute::annotations);
346     }
347 
348     private void generateCatchTargets(Consumer<? super CodeElement> consumer) {
349         // We attach all catch targets to bci zero, because trying to attach them
350         // to their range could subtly affect the order of exception processing
351         iterateExceptionHandlers(new ExceptionHandlerAction() {
352             @Override
353             public void accept(int s, int e, int h, int c) {
354                 ClassEntry catchType = c == 0
355                                                     ? null
356                                                     : classReader.entryByIndex(c, ClassEntry.class);
357                 consumer.accept(new AbstractPseudoInstruction.ExceptionCatchImpl(getLabel(h), getLabel(s), getLabel(e), catchType));
358             }
359         });
360     }
361 
362     private void generateDebugElements(Consumer<? super CodeElement> consumer) {
363         for (Attribute<?> a : attributes()) {
364             if (a.attributeMapper() == Attributes.characterRangeTable()) {
365                 var attr = (BoundCharacterRangeTableAttribute) a;
366                 int cnt = classReader.readU2(attr.payloadStart);
367                 int p = attr.payloadStart + 2;
368                 int pEnd = p + (cnt * 14);
369                 for (; p < pEnd; p += 14) {
370                     var instruction = new BoundCharacterRange(this, p);
371                     inflateLabel(instruction.startPc());
372                     inflateLabel(instruction.endPc() + 1);
373                     consumer.accept(instruction);
374                 }
375             }
376             else if (a.attributeMapper() == Attributes.localVariableTable()) {
377                 var attr = (BoundLocalVariableTableAttribute) a;
378                 int cnt = classReader.readU2(attr.payloadStart);
379                 int p = attr.payloadStart + 2;
380                 int pEnd = p + (cnt * 10);
381                 for (; p < pEnd; p += 10) {
382                     BoundLocalVariable instruction = new BoundLocalVariable(this, p);
383                     inflateLabel(instruction.startPc());
384                     inflateLabel(instruction.startPc() + instruction.length());
385                     consumer.accept(instruction);
386                 }
387             }
388             else if (a.attributeMapper() == Attributes.localVariableTypeTable()) {
389                 var attr = (BoundLocalVariableTypeTableAttribute) a;
390                 int cnt = classReader.readU2(attr.payloadStart);
391                 int p = attr.payloadStart + 2;
392                 int pEnd = p + (cnt * 10);
393                 for (; p < pEnd; p += 10) {
394                     BoundLocalVariableType instruction = new BoundLocalVariableType(this, p);
395                     inflateLabel(instruction.startPc());
396                     inflateLabel(instruction.startPc() + instruction.length());
397                     consumer.accept(instruction);
398                 }
399             }
400             else if (a.attributeMapper() == Attributes.runtimeVisibleTypeAnnotations()) {
401                 consumer.accept((BoundRuntimeVisibleTypeAnnotationsAttribute) a);
402             }
403             else if (a.attributeMapper() == Attributes.runtimeInvisibleTypeAnnotations()) {
404                 consumer.accept((BoundRuntimeInvisibleTypeAnnotationsAttribute) a);
405             }
406         }
407     }
408 
409     public interface ExceptionHandlerAction {
410         void accept(int start, int end, int handler, int catchTypeIndex);
411     }
412 
413     public void iterateExceptionHandlers(ExceptionHandlerAction a) {
414         int p = exceptionHandlerPos + 2;
415         for (int i = 0; i < exceptionHandlerCnt; ++i) {
416             a.accept(classReader.readU2(p), classReader.readU2(p + 2), classReader.readU2(p + 4), classReader.readU2(p + 6));
417             p += 8;
418         }
419     }
420 
421     private Instruction bcToInstruction(int bc, int pos) {
422         return switch (bc) {
423             case BIPUSH -> new AbstractInstruction.BoundArgumentConstantInstruction(Opcode.BIPUSH, CodeImpl.this, pos);
424             case SIPUSH -> new AbstractInstruction.BoundArgumentConstantInstruction(Opcode.SIPUSH, CodeImpl.this, pos);
425             case LDC -> new AbstractInstruction.BoundLoadConstantInstruction(Opcode.LDC, CodeImpl.this, pos);
426             case LDC_W -> new AbstractInstruction.BoundLoadConstantInstruction(Opcode.LDC_W, CodeImpl.this, pos);
427             case LDC2_W -> new AbstractInstruction.BoundLoadConstantInstruction(Opcode.LDC2_W, CodeImpl.this, pos);
428             case ILOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.ILOAD, CodeImpl.this, pos);
429             case LLOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.LLOAD, CodeImpl.this, pos);
430             case FLOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.FLOAD, CodeImpl.this, pos);
431             case DLOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.DLOAD, CodeImpl.this, pos);
432             case ALOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.ALOAD, CodeImpl.this, pos);
433             case ISTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.ISTORE, CodeImpl.this, pos);
434             case LSTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.LSTORE, CodeImpl.this, pos);
435             case FSTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.FSTORE, CodeImpl.this, pos);
436             case DSTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.DSTORE, CodeImpl.this, pos);
437             case ASTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.ASTORE, CodeImpl.this, pos);
438             case IINC -> new AbstractInstruction.BoundIncrementInstruction(Opcode.IINC, CodeImpl.this, pos);
439             case IFEQ -> new AbstractInstruction.BoundBranchInstruction(Opcode.IFEQ, CodeImpl.this, pos);
440             case IFNE -> new AbstractInstruction.BoundBranchInstruction(Opcode.IFNE, CodeImpl.this, pos);
441             case IFLT -> new AbstractInstruction.BoundBranchInstruction(Opcode.IFLT, CodeImpl.this, pos);
442             case IFGE -> new AbstractInstruction.BoundBranchInstruction(Opcode.IFGE, CodeImpl.this, pos);
443             case IFGT -> new AbstractInstruction.BoundBranchInstruction(Opcode.IFGT, CodeImpl.this, pos);
444             case IFLE -> new AbstractInstruction.BoundBranchInstruction(Opcode.IFLE, CodeImpl.this, pos);
445             case IF_ICMPEQ -> new AbstractInstruction.BoundBranchInstruction(Opcode.IF_ICMPEQ, CodeImpl.this, pos);
446             case IF_ICMPNE -> new AbstractInstruction.BoundBranchInstruction(Opcode.IF_ICMPNE, CodeImpl.this, pos);
447             case IF_ICMPLT -> new AbstractInstruction.BoundBranchInstruction(Opcode.IF_ICMPLT, CodeImpl.this, pos);
448             case IF_ICMPGE -> new AbstractInstruction.BoundBranchInstruction(Opcode.IF_ICMPGE, CodeImpl.this, pos);
449             case IF_ICMPGT -> new AbstractInstruction.BoundBranchInstruction(Opcode.IF_ICMPGT, CodeImpl.this, pos);
450             case IF_ICMPLE -> new AbstractInstruction.BoundBranchInstruction(Opcode.IF_ICMPLE, CodeImpl.this, pos);
451             case IF_ACMPEQ -> new AbstractInstruction.BoundBranchInstruction(Opcode.IF_ACMPEQ, CodeImpl.this, pos);
452             case IF_ACMPNE -> new AbstractInstruction.BoundBranchInstruction(Opcode.IF_ACMPNE, CodeImpl.this, pos);
453             case GOTO -> new AbstractInstruction.BoundBranchInstruction(Opcode.GOTO, CodeImpl.this, pos);
454             case TABLESWITCH -> new AbstractInstruction.BoundTableSwitchInstruction(Opcode.TABLESWITCH, CodeImpl.this, pos);
455             case LOOKUPSWITCH -> new AbstractInstruction.BoundLookupSwitchInstruction(Opcode.LOOKUPSWITCH, CodeImpl.this, pos);
456             case GETSTATIC -> new AbstractInstruction.BoundFieldInstruction(Opcode.GETSTATIC, CodeImpl.this, pos);
457             case PUTSTATIC -> new AbstractInstruction.BoundFieldInstruction(Opcode.PUTSTATIC, CodeImpl.this, pos);
458             case GETFIELD -> new AbstractInstruction.BoundFieldInstruction(Opcode.GETFIELD, CodeImpl.this, pos);
459             case PUTFIELD -> new AbstractInstruction.BoundFieldInstruction(Opcode.PUTFIELD, CodeImpl.this, pos);
460             case INVOKEVIRTUAL -> new AbstractInstruction.BoundInvokeInstruction(Opcode.INVOKEVIRTUAL, CodeImpl.this, pos);
461             case INVOKESPECIAL -> new AbstractInstruction.BoundInvokeInstruction(Opcode.INVOKESPECIAL, CodeImpl.this, pos);
462             case INVOKESTATIC -> new AbstractInstruction.BoundInvokeInstruction(Opcode.INVOKESTATIC, CodeImpl.this, pos);
463             case INVOKEINTERFACE -> new AbstractInstruction.BoundInvokeInterfaceInstruction(Opcode.INVOKEINTERFACE, CodeImpl.this, pos);
464             case INVOKEDYNAMIC -> new AbstractInstruction.BoundInvokeDynamicInstruction(Opcode.INVOKEDYNAMIC, CodeImpl.this, pos);
465             case NEW -> new AbstractInstruction.BoundNewObjectInstruction(CodeImpl.this, pos);
466             case NEWARRAY -> new AbstractInstruction.BoundNewPrimitiveArrayInstruction(Opcode.NEWARRAY, CodeImpl.this, pos);
467             case ANEWARRAY -> new AbstractInstruction.BoundNewReferenceArrayInstruction(Opcode.ANEWARRAY, CodeImpl.this, pos);
468             case CHECKCAST -> new AbstractInstruction.BoundTypeCheckInstruction(Opcode.CHECKCAST, CodeImpl.this, pos);
469             case INSTANCEOF -> new AbstractInstruction.BoundTypeCheckInstruction(Opcode.INSTANCEOF, CodeImpl.this, pos);
470 
471             case WIDE -> {
472                 int bclow = classReader.readU1(pos + 1);
473                 yield switch (bclow) {
474                     case ILOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.ILOAD_W, this, pos);
475                     case LLOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.LLOAD_W, this, pos);
476                     case FLOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.FLOAD_W, this, pos);
477                     case DLOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.DLOAD_W, this, pos);
478                     case ALOAD -> new AbstractInstruction.BoundLoadInstruction(Opcode.ALOAD_W, this, pos);
479                     case ISTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.ISTORE_W, this, pos);
480                     case LSTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.LSTORE_W, this, pos);
481                     case FSTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.FSTORE_W, this, pos);
482                     case DSTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.DSTORE_W, this, pos);
483                     case ASTORE -> new AbstractInstruction.BoundStoreInstruction(Opcode.ASTORE_W, this, pos);
484                     case IINC -> new AbstractInstruction.BoundIncrementInstruction(Opcode.IINC_W, this, pos);
485                     case RET ->  new AbstractInstruction.BoundRetInstruction(Opcode.RET_W, this, pos);
486                     default -> throw new IllegalArgumentException("unknown wide instruction: " + bclow);
487                 };
488             }
489 
490             case MULTIANEWARRAY -> new AbstractInstruction.BoundNewMultidimensionalArrayInstruction(Opcode.MULTIANEWARRAY, CodeImpl.this, pos);
491             case IFNULL -> new AbstractInstruction.BoundBranchInstruction(Opcode.IFNULL, CodeImpl.this, pos);
492             case IFNONNULL -> new AbstractInstruction.BoundBranchInstruction(Opcode.IFNONNULL, CodeImpl.this, pos);
493             case GOTO_W -> new AbstractInstruction.BoundBranchInstruction(Opcode.GOTO_W, CodeImpl.this, pos);
494 
495             case JSR -> new AbstractInstruction.BoundJsrInstruction(Opcode.JSR, CodeImpl.this, pos);
496             case RET ->  new AbstractInstruction.BoundRetInstruction(Opcode.RET, this, pos);
497             case JSR_W -> new AbstractInstruction.BoundJsrInstruction(Opcode.JSR_W, CodeImpl.this, pos);
498             default -> {
499                 Instruction instr = SINGLETON_INSTRUCTIONS[bc];
500                 if (instr == null)
501                     throw new IllegalArgumentException("unknown instruction: " + bc);
502                 yield instr;
503             }
504         };
505     }
506 
507     @Override
508     public String toString() {
509         return String.format("CodeModel[id=%d]", System.identityHashCode(this));
510     }
511 }