View Javadoc
1   package mockit.asm.jvmConstants;
2   
3   /**
4    * Constant data about all <a href="https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-6.html">JVM bytecode
5    * instructions</a>: {@link #SIZE}, {@link #TYPE}.
6    */
7   public final class JVMInstruction {
8       /**
9        * The stack size variation corresponding to each JVM instruction. Said variation equals the size of the values
10       * produced by an instruction, minus the size of the values consumed by the instruction.
11       */
12      public static final int[] SIZE;
13      static {
14          String s = "EFFFFFFFFGGFFFGGFFFEEFGFGFEEEEEEEEEEEEEEEEEEEEDEDEDDDDD"
15                  + "CDCDEEEEEEEEEEEEEEEEEEEEBABABBBBDCFFFGGGEDCDCDCDCDCDCDCDCD"
16                  + "CDCEEEEDDDDDDDCDCDCEFEFDDEEFFDEDEEEBDDBBDDDDDDCCCCCCCCEFED" + "DDCDCDEEEEEEEEEEFEEEEEEDDEEDDEE";
17          int n = s.length();
18          int[] sizes = new int[n];
19  
20          for (int i = 0; i < n; i++) {
21              // noinspection CharUsedInArithmeticContext
22              sizes[i] = s.charAt(i) - 'E';
23          }
24  
25          SIZE = sizes;
26      }
27  
28      /**
29       * Constants that subdivide the 220 {@linkplain Opcodes instruction opcodes} in 18 types of instructions. Such types
30       * vary in the number and size of arguments the instruction takes (no argument, a signed byte, a signed short), on
31       * whether it takes a local variable index, a jump target label, etc. Some types contain a single instruction, such
32       * as LDC and IINC.
33       */
34      public interface InstructionType {
35          int NOARG = 0; // instructions without any argument
36          int SBYTE = 1; // instructions with a signed byte argument
37          int SHORT = 2; // instructions with a signed short argument
38          int VAR = 3; // instructions with a local variable index argument
39          int IMPLVAR = 4; // instructions with an implicit local variable index argument
40          int TYPE_INSN = 5; // instructions with a type descriptor argument
41          int FIELDORMETH = 6; // field and method invocations instructions
42          int ITFMETH = 7; // INVOKEINTERFACE/INVOKEDYNAMIC instruction
43          int INDYMETH = 8; // INVOKEDYNAMIC instruction
44          int LABEL = 9; // instructions with a 2 bytes bytecode offset label
45          int LABELW = 10; // instructions with a 4 bytes bytecode offset label
46          int LDC_INSN = 11; // the LDC instruction
47          int LDCW_INSN = 12; // the LDC_W and LDC2_W instructions
48          int IINC_INSN = 13; // the IINC instruction
49          int TABL_INSN = 14; // the TABLESWITCH instruction
50          int LOOK_INSN = 15; // the LOOKUPSWITCH instruction
51          int MANA_INSN = 16; // the MULTIANEWARRAY instruction
52          int WIDE_INSN = 17; // the WIDE instruction
53      }
54  
55      /**
56       * The {@linkplain InstructionType instruction types} of all JVM opcodes, one value for each instruction opcode.
57       */
58      public static final byte[] TYPE;
59      static {
60          String s = "AAAAAAAAAAAAAAAABCLMMDDDDDEEEEEEEEEEEEEEEEEEEEAAAAAAAADD"
61                  + "DDDEEEEEEEEEEEEEEEEEEEEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
62                  + "AAAAAAAAAAAAAAAAANAAAAAAAAAAAAAAAAAAAAJJJJJJJJJJJJJJJJDOPAA"
63                  + "AAAAGGGGGGGHIFBFAAFFAARQJJKKJJJJJJJJJJJJJJJJJJ";
64          int n = s.length();
65          byte[] types = new byte[n];
66  
67          for (int i = 0; i < n; i++) {
68              // noinspection NumericCastThatLosesPrecision,CharUsedInArithmeticContext
69              types[i] = (byte) (s.charAt(i) - 'A');
70          }
71  
72          TYPE = types;
73      }
74  
75      private JVMInstruction() {
76      }
77  }