1 /*
   2  * Copyright (c) 2009, 2011, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.bytecode;
  24 
  25 import static org.graalvm.compiler.bytecode.Bytecodes.Flags.ASSOCIATIVE;
  26 import static org.graalvm.compiler.bytecode.Bytecodes.Flags.BRANCH;
  27 import static org.graalvm.compiler.bytecode.Bytecodes.Flags.COMMUTATIVE;
  28 import static org.graalvm.compiler.bytecode.Bytecodes.Flags.FALL_THROUGH;
  29 import static org.graalvm.compiler.bytecode.Bytecodes.Flags.FIELD_READ;
  30 import static org.graalvm.compiler.bytecode.Bytecodes.Flags.FIELD_WRITE;
  31 import static org.graalvm.compiler.bytecode.Bytecodes.Flags.INVOKE;
  32 import static org.graalvm.compiler.bytecode.Bytecodes.Flags.LOAD;
  33 import static org.graalvm.compiler.bytecode.Bytecodes.Flags.STOP;
  34 import static org.graalvm.compiler.bytecode.Bytecodes.Flags.STORE;
  35 import static org.graalvm.compiler.bytecode.Bytecodes.Flags.TRAP;
  36 
  37 import java.lang.reflect.Field;
  38 import java.lang.reflect.Modifier;
  39 
  40 /**
  41  * Definitions of the standard Java bytecodes defined by
  42  * <a href= "http://java.sun.com/docs/books/jvms/second_edition/html/VMSpecTOC.doc.html"> Java
  43  * Virtual Machine Specification</a>.
  44  */
  45 public class Bytecodes {
  46 
  47     // @formatter:off
  48     public static final int NOP                  =   0; // 0x00
  49     public static final int ACONST_NULL          =   1; // 0x01
  50     public static final int ICONST_M1            =   2; // 0x02
  51     public static final int ICONST_0             =   3; // 0x03
  52     public static final int ICONST_1             =   4; // 0x04
  53     public static final int ICONST_2             =   5; // 0x05
  54     public static final int ICONST_3             =   6; // 0x06
  55     public static final int ICONST_4             =   7; // 0x07
  56     public static final int ICONST_5             =   8; // 0x08
  57     public static final int LCONST_0             =   9; // 0x09
  58     public static final int LCONST_1             =  10; // 0x0A
  59     public static final int FCONST_0             =  11; // 0x0B
  60     public static final int FCONST_1             =  12; // 0x0C
  61     public static final int FCONST_2             =  13; // 0x0D
  62     public static final int DCONST_0             =  14; // 0x0E
  63     public static final int DCONST_1             =  15; // 0x0F
  64     public static final int BIPUSH               =  16; // 0x10
  65     public static final int SIPUSH               =  17; // 0x11
  66     public static final int LDC                  =  18; // 0x12
  67     public static final int LDC_W                =  19; // 0x13
  68     public static final int LDC2_W               =  20; // 0x14
  69     public static final int ILOAD                =  21; // 0x15
  70     public static final int LLOAD                =  22; // 0x16
  71     public static final int FLOAD                =  23; // 0x17
  72     public static final int DLOAD                =  24; // 0x18
  73     public static final int ALOAD                =  25; // 0x19
  74     public static final int ILOAD_0              =  26; // 0x1A
  75     public static final int ILOAD_1              =  27; // 0x1B
  76     public static final int ILOAD_2              =  28; // 0x1C
  77     public static final int ILOAD_3              =  29; // 0x1D
  78     public static final int LLOAD_0              =  30; // 0x1E
  79     public static final int LLOAD_1              =  31; // 0x1F
  80     public static final int LLOAD_2              =  32; // 0x20
  81     public static final int LLOAD_3              =  33; // 0x21
  82     public static final int FLOAD_0              =  34; // 0x22
  83     public static final int FLOAD_1              =  35; // 0x23
  84     public static final int FLOAD_2              =  36; // 0x24
  85     public static final int FLOAD_3              =  37; // 0x25
  86     public static final int DLOAD_0              =  38; // 0x26
  87     public static final int DLOAD_1              =  39; // 0x27
  88     public static final int DLOAD_2              =  40; // 0x28
  89     public static final int DLOAD_3              =  41; // 0x29
  90     public static final int ALOAD_0              =  42; // 0x2A
  91     public static final int ALOAD_1              =  43; // 0x2B
  92     public static final int ALOAD_2              =  44; // 0x2C
  93     public static final int ALOAD_3              =  45; // 0x2D
  94     public static final int IALOAD               =  46; // 0x2E
  95     public static final int LALOAD               =  47; // 0x2F
  96     public static final int FALOAD               =  48; // 0x30
  97     public static final int DALOAD               =  49; // 0x31
  98     public static final int AALOAD               =  50; // 0x32
  99     public static final int BALOAD               =  51; // 0x33
 100     public static final int CALOAD               =  52; // 0x34
 101     public static final int SALOAD               =  53; // 0x35
 102     public static final int ISTORE               =  54; // 0x36
 103     public static final int LSTORE               =  55; // 0x37
 104     public static final int FSTORE               =  56; // 0x38
 105     public static final int DSTORE               =  57; // 0x39
 106     public static final int ASTORE               =  58; // 0x3A
 107     public static final int ISTORE_0             =  59; // 0x3B
 108     public static final int ISTORE_1             =  60; // 0x3C
 109     public static final int ISTORE_2             =  61; // 0x3D
 110     public static final int ISTORE_3             =  62; // 0x3E
 111     public static final int LSTORE_0             =  63; // 0x3F
 112     public static final int LSTORE_1             =  64; // 0x40
 113     public static final int LSTORE_2             =  65; // 0x41
 114     public static final int LSTORE_3             =  66; // 0x42
 115     public static final int FSTORE_0             =  67; // 0x43
 116     public static final int FSTORE_1             =  68; // 0x44
 117     public static final int FSTORE_2             =  69; // 0x45
 118     public static final int FSTORE_3             =  70; // 0x46
 119     public static final int DSTORE_0             =  71; // 0x47
 120     public static final int DSTORE_1             =  72; // 0x48
 121     public static final int DSTORE_2             =  73; // 0x49
 122     public static final int DSTORE_3             =  74; // 0x4A
 123     public static final int ASTORE_0             =  75; // 0x4B
 124     public static final int ASTORE_1             =  76; // 0x4C
 125     public static final int ASTORE_2             =  77; // 0x4D
 126     public static final int ASTORE_3             =  78; // 0x4E
 127     public static final int IASTORE              =  79; // 0x4F
 128     public static final int LASTORE              =  80; // 0x50
 129     public static final int FASTORE              =  81; // 0x51
 130     public static final int DASTORE              =  82; // 0x52
 131     public static final int AASTORE              =  83; // 0x53
 132     public static final int BASTORE              =  84; // 0x54
 133     public static final int CASTORE              =  85; // 0x55
 134     public static final int SASTORE              =  86; // 0x56
 135     public static final int POP                  =  87; // 0x57
 136     public static final int POP2                 =  88; // 0x58
 137     public static final int DUP                  =  89; // 0x59
 138     public static final int DUP_X1               =  90; // 0x5A
 139     public static final int DUP_X2               =  91; // 0x5B
 140     public static final int DUP2                 =  92; // 0x5C
 141     public static final int DUP2_X1              =  93; // 0x5D
 142     public static final int DUP2_X2              =  94; // 0x5E
 143     public static final int SWAP                 =  95; // 0x5F
 144     public static final int IADD                 =  96; // 0x60
 145     public static final int LADD                 =  97; // 0x61
 146     public static final int FADD                 =  98; // 0x62
 147     public static final int DADD                 =  99; // 0x63
 148     public static final int ISUB                 = 100; // 0x64
 149     public static final int LSUB                 = 101; // 0x65
 150     public static final int FSUB                 = 102; // 0x66
 151     public static final int DSUB                 = 103; // 0x67
 152     public static final int IMUL                 = 104; // 0x68
 153     public static final int LMUL                 = 105; // 0x69
 154     public static final int FMUL                 = 106; // 0x6A
 155     public static final int DMUL                 = 107; // 0x6B
 156     public static final int IDIV                 = 108; // 0x6C
 157     public static final int LDIV                 = 109; // 0x6D
 158     public static final int FDIV                 = 110; // 0x6E
 159     public static final int DDIV                 = 111; // 0x6F
 160     public static final int IREM                 = 112; // 0x70
 161     public static final int LREM                 = 113; // 0x71
 162     public static final int FREM                 = 114; // 0x72
 163     public static final int DREM                 = 115; // 0x73
 164     public static final int INEG                 = 116; // 0x74
 165     public static final int LNEG                 = 117; // 0x75
 166     public static final int FNEG                 = 118; // 0x76
 167     public static final int DNEG                 = 119; // 0x77
 168     public static final int ISHL                 = 120; // 0x78
 169     public static final int LSHL                 = 121; // 0x79
 170     public static final int ISHR                 = 122; // 0x7A
 171     public static final int LSHR                 = 123; // 0x7B
 172     public static final int IUSHR                = 124; // 0x7C
 173     public static final int LUSHR                = 125; // 0x7D
 174     public static final int IAND                 = 126; // 0x7E
 175     public static final int LAND                 = 127; // 0x7F
 176     public static final int IOR                  = 128; // 0x80
 177     public static final int LOR                  = 129; // 0x81
 178     public static final int IXOR                 = 130; // 0x82
 179     public static final int LXOR                 = 131; // 0x83
 180     public static final int IINC                 = 132; // 0x84
 181     public static final int I2L                  = 133; // 0x85
 182     public static final int I2F                  = 134; // 0x86
 183     public static final int I2D                  = 135; // 0x87
 184     public static final int L2I                  = 136; // 0x88
 185     public static final int L2F                  = 137; // 0x89
 186     public static final int L2D                  = 138; // 0x8A
 187     public static final int F2I                  = 139; // 0x8B
 188     public static final int F2L                  = 140; // 0x8C
 189     public static final int F2D                  = 141; // 0x8D
 190     public static final int D2I                  = 142; // 0x8E
 191     public static final int D2L                  = 143; // 0x8F
 192     public static final int D2F                  = 144; // 0x90
 193     public static final int I2B                  = 145; // 0x91
 194     public static final int I2C                  = 146; // 0x92
 195     public static final int I2S                  = 147; // 0x93
 196     public static final int LCMP                 = 148; // 0x94
 197     public static final int FCMPL                = 149; // 0x95
 198     public static final int FCMPG                = 150; // 0x96
 199     public static final int DCMPL                = 151; // 0x97
 200     public static final int DCMPG                = 152; // 0x98
 201     public static final int IFEQ                 = 153; // 0x99
 202     public static final int IFNE                 = 154; // 0x9A
 203     public static final int IFLT                 = 155; // 0x9B
 204     public static final int IFGE                 = 156; // 0x9C
 205     public static final int IFGT                 = 157; // 0x9D
 206     public static final int IFLE                 = 158; // 0x9E
 207     public static final int IF_ICMPEQ            = 159; // 0x9F
 208     public static final int IF_ICMPNE            = 160; // 0xA0
 209     public static final int IF_ICMPLT            = 161; // 0xA1
 210     public static final int IF_ICMPGE            = 162; // 0xA2
 211     public static final int IF_ICMPGT            = 163; // 0xA3
 212     public static final int IF_ICMPLE            = 164; // 0xA4
 213     public static final int IF_ACMPEQ            = 165; // 0xA5
 214     public static final int IF_ACMPNE            = 166; // 0xA6
 215     public static final int GOTO                 = 167; // 0xA7
 216     public static final int JSR                  = 168; // 0xA8
 217     public static final int RET                  = 169; // 0xA9
 218     public static final int TABLESWITCH          = 170; // 0xAA
 219     public static final int LOOKUPSWITCH         = 171; // 0xAB
 220     public static final int IRETURN              = 172; // 0xAC
 221     public static final int LRETURN              = 173; // 0xAD
 222     public static final int FRETURN              = 174; // 0xAE
 223     public static final int DRETURN              = 175; // 0xAF
 224     public static final int ARETURN              = 176; // 0xB0
 225     public static final int RETURN               = 177; // 0xB1
 226     public static final int GETSTATIC            = 178; // 0xB2
 227     public static final int PUTSTATIC            = 179; // 0xB3
 228     public static final int GETFIELD             = 180; // 0xB4
 229     public static final int PUTFIELD             = 181; // 0xB5
 230     public static final int INVOKEVIRTUAL        = 182; // 0xB6
 231     public static final int INVOKESPECIAL        = 183; // 0xB7
 232     public static final int INVOKESTATIC         = 184; // 0xB8
 233     public static final int INVOKEINTERFACE      = 185; // 0xB9
 234     public static final int INVOKEDYNAMIC        = 186; // 0xBA
 235     public static final int NEW                  = 187; // 0xBB
 236     public static final int NEWARRAY             = 188; // 0xBC
 237     public static final int ANEWARRAY            = 189; // 0xBD
 238     public static final int ARRAYLENGTH          = 190; // 0xBE
 239     public static final int ATHROW               = 191; // 0xBF
 240     public static final int CHECKCAST            = 192; // 0xC0
 241     public static final int INSTANCEOF           = 193; // 0xC1
 242     public static final int MONITORENTER         = 194; // 0xC2
 243     public static final int MONITOREXIT          = 195; // 0xC3
 244     public static final int WIDE                 = 196; // 0xC4
 245     public static final int MULTIANEWARRAY       = 197; // 0xC5
 246     public static final int IFNULL               = 198; // 0xC6
 247     public static final int IFNONNULL            = 199; // 0xC7
 248     public static final int GOTO_W               = 200; // 0xC8
 249     public static final int JSR_W                = 201; // 0xC9
 250     public static final int BREAKPOINT           = 202; // 0xCA
 251 
 252     public static final int ILLEGAL = 255;
 253     public static final int END = 256;
 254     // @formatter:on
 255 
 256     /**
 257      * The last opcode defined by the JVM specification. To iterate over all JVM bytecodes:
 258      *
 259      * <pre>
 260      * for (int opcode = 0; opcode &lt;= Bytecodes.LAST_JVM_OPCODE; ++opcode) {
 261      *     //
 262      * }
 263      * </pre>
 264      */
 265     public static final int LAST_JVM_OPCODE = JSR_W;
 266 
 267     /**
 268      * A collection of flags describing various bytecode attributes.
 269      */
 270     static class Flags {
 271 
 272         /**
 273          * Denotes an instruction that ends a basic block and does not let control flow fall through
 274          * to its lexical successor.
 275          */
 276         static final int STOP = 0x00000001;
 277 
 278         /**
 279          * Denotes an instruction that ends a basic block and may let control flow fall through to
 280          * its lexical successor. In practice this means it is a conditional branch.
 281          */
 282         static final int FALL_THROUGH = 0x00000002;
 283 
 284         /**
 285          * Denotes an instruction that has a 2 or 4 byte operand that is an offset to another
 286          * instruction in the same method. This does not include the {@link Bytecodes#TABLESWITCH}
 287          * or {@link Bytecodes#LOOKUPSWITCH} instructions.
 288          */
 289         static final int BRANCH = 0x00000004;
 290 
 291         /**
 292          * Denotes an instruction that reads the value of a static or instance field.
 293          */
 294         static final int FIELD_READ = 0x00000008;
 295 
 296         /**
 297          * Denotes an instruction that writes the value of a static or instance field.
 298          */
 299         static final int FIELD_WRITE = 0x00000010;
 300 
 301         /**
 302          * Denotes an instruction that can cause a trap.
 303          */
 304         static final int TRAP = 0x00000080;
 305         /**
 306          * Denotes an instruction that is commutative.
 307          */
 308         static final int COMMUTATIVE = 0x00000100;
 309         /**
 310          * Denotes an instruction that is associative.
 311          */
 312         static final int ASSOCIATIVE = 0x00000200;
 313         /**
 314          * Denotes an instruction that loads an operand.
 315          */
 316         static final int LOAD = 0x00000400;
 317         /**
 318          * Denotes an instruction that stores an operand.
 319          */
 320         static final int STORE = 0x00000800;
 321         /**
 322          * Denotes the 4 INVOKE* instructions.
 323          */
 324         static final int INVOKE = 0x00001000;
 325     }
 326 
 327     // Performs a sanity check that none of the flags overlap.
 328     static {
 329         int allFlags = 0;
 330         try {
 331             for (Field field : Flags.class.getDeclaredFields()) {
 332                 int flagsFilter = Modifier.FINAL | Modifier.STATIC;
 333                 if ((field.getModifiers() & flagsFilter) == flagsFilter && !field.isSynthetic()) {
 334                     assert field.getType() == int.class : "Field is not int : " + field;
 335                     final int flag = field.getInt(null);
 336                     assert flag != 0;
 337                     assert (flag & allFlags) == 0 : field.getName() + " has a value conflicting with another flag";
 338                     allFlags |= flag;
 339                 }
 340             }
 341         } catch (Exception e) {
 342             throw new InternalError(e.toString());
 343         }
 344     }
 345 
 346     /**
 347      * An array that maps from a bytecode value to a {@link String} for the corresponding
 348      * instruction mnemonic.
 349      */
 350     private static final String[] nameArray = new String[256];
 351 
 352     /**
 353      * An array that maps from a bytecode value to the set of {@link Flags} for the corresponding
 354      * instruction.
 355      */
 356     private static final int[] flagsArray = new int[256];
 357 
 358     /**
 359      * An array that maps from a bytecode value to the length in bytes for the corresponding
 360      * instruction.
 361      */
 362     private static final int[] lengthArray = new int[256];
 363 
 364     /**
 365      * An array that maps from a bytecode value to the number of slots pushed on the stack by the
 366      * corresponding instruction.
 367      */
 368     private static final int[] stackEffectArray = new int[256];
 369 
 370     // Checkstyle: stop
 371     // @formatter:off
 372     static {
 373         def(NOP                 , "nop"             , "b"    ,  0);
 374         def(ACONST_NULL         , "aconst_null"     , "b"    ,  1);
 375         def(ICONST_M1           , "iconst_m1"       , "b"    ,  1);
 376         def(ICONST_0            , "iconst_0"        , "b"    ,  1);
 377         def(ICONST_1            , "iconst_1"        , "b"    ,  1);
 378         def(ICONST_2            , "iconst_2"        , "b"    ,  1);
 379         def(ICONST_3            , "iconst_3"        , "b"    ,  1);
 380         def(ICONST_4            , "iconst_4"        , "b"    ,  1);
 381         def(ICONST_5            , "iconst_5"        , "b"    ,  1);
 382         def(LCONST_0            , "lconst_0"        , "b"    ,  2);
 383         def(LCONST_1            , "lconst_1"        , "b"    ,  2);
 384         def(FCONST_0            , "fconst_0"        , "b"    ,  1);
 385         def(FCONST_1            , "fconst_1"        , "b"    ,  1);
 386         def(FCONST_2            , "fconst_2"        , "b"    ,  1);
 387         def(DCONST_0            , "dconst_0"        , "b"    ,  2);
 388         def(DCONST_1            , "dconst_1"        , "b"    ,  2);
 389         def(BIPUSH              , "bipush"          , "bc"   ,  1);
 390         def(SIPUSH              , "sipush"          , "bcc"  ,  1);
 391         def(LDC                 , "ldc"             , "bi"   ,  1, TRAP);
 392         def(LDC_W               , "ldc_w"           , "bii"  ,  1, TRAP);
 393         def(LDC2_W              , "ldc2_w"          , "bii"  ,  2, TRAP);
 394         def(ILOAD               , "iload"           , "bi"   ,  1, LOAD);
 395         def(LLOAD               , "lload"           , "bi"   ,  2, LOAD);
 396         def(FLOAD               , "fload"           , "bi"   ,  1, LOAD);
 397         def(DLOAD               , "dload"           , "bi"   ,  2, LOAD);
 398         def(ALOAD               , "aload"           , "bi"   ,  1, LOAD);
 399         def(ILOAD_0             , "iload_0"         , "b"    ,  1, LOAD);
 400         def(ILOAD_1             , "iload_1"         , "b"    ,  1, LOAD);
 401         def(ILOAD_2             , "iload_2"         , "b"    ,  1, LOAD);
 402         def(ILOAD_3             , "iload_3"         , "b"    ,  1, LOAD);
 403         def(LLOAD_0             , "lload_0"         , "b"    ,  2, LOAD);
 404         def(LLOAD_1             , "lload_1"         , "b"    ,  2, LOAD);
 405         def(LLOAD_2             , "lload_2"         , "b"    ,  2, LOAD);
 406         def(LLOAD_3             , "lload_3"         , "b"    ,  2, LOAD);
 407         def(FLOAD_0             , "fload_0"         , "b"    ,  1, LOAD);
 408         def(FLOAD_1             , "fload_1"         , "b"    ,  1, LOAD);
 409         def(FLOAD_2             , "fload_2"         , "b"    ,  1, LOAD);
 410         def(FLOAD_3             , "fload_3"         , "b"    ,  1, LOAD);
 411         def(DLOAD_0             , "dload_0"         , "b"    ,  2, LOAD);
 412         def(DLOAD_1             , "dload_1"         , "b"    ,  2, LOAD);
 413         def(DLOAD_2             , "dload_2"         , "b"    ,  2, LOAD);
 414         def(DLOAD_3             , "dload_3"         , "b"    ,  2, LOAD);
 415         def(ALOAD_0             , "aload_0"         , "b"    ,  1, LOAD);
 416         def(ALOAD_1             , "aload_1"         , "b"    ,  1, LOAD);
 417         def(ALOAD_2             , "aload_2"         , "b"    ,  1, LOAD);
 418         def(ALOAD_3             , "aload_3"         , "b"    ,  1, LOAD);
 419         def(IALOAD              , "iaload"          , "b"    , -1, TRAP);
 420         def(LALOAD              , "laload"          , "b"    ,  0, TRAP);
 421         def(FALOAD              , "faload"          , "b"    , -1, TRAP);
 422         def(DALOAD              , "daload"          , "b"    ,  0, TRAP);
 423         def(AALOAD              , "aaload"          , "b"    , -1, TRAP);
 424         def(BALOAD              , "baload"          , "b"    , -1, TRAP);
 425         def(CALOAD              , "caload"          , "b"    , -1, TRAP);
 426         def(SALOAD              , "saload"          , "b"    , -1, TRAP);
 427         def(ISTORE              , "istore"          , "bi"   , -1, STORE);
 428         def(LSTORE              , "lstore"          , "bi"   , -2, STORE);
 429         def(FSTORE              , "fstore"          , "bi"   , -1, STORE);
 430         def(DSTORE              , "dstore"          , "bi"   , -2, STORE);
 431         def(ASTORE              , "astore"          , "bi"   , -1, STORE);
 432         def(ISTORE_0            , "istore_0"        , "b"    , -1, STORE);
 433         def(ISTORE_1            , "istore_1"        , "b"    , -1, STORE);
 434         def(ISTORE_2            , "istore_2"        , "b"    , -1, STORE);
 435         def(ISTORE_3            , "istore_3"        , "b"    , -1, STORE);
 436         def(LSTORE_0            , "lstore_0"        , "b"    , -2, STORE);
 437         def(LSTORE_1            , "lstore_1"        , "b"    , -2, STORE);
 438         def(LSTORE_2            , "lstore_2"        , "b"    , -2, STORE);
 439         def(LSTORE_3            , "lstore_3"        , "b"    , -2, STORE);
 440         def(FSTORE_0            , "fstore_0"        , "b"    , -1, STORE);
 441         def(FSTORE_1            , "fstore_1"        , "b"    , -1, STORE);
 442         def(FSTORE_2            , "fstore_2"        , "b"    , -1, STORE);
 443         def(FSTORE_3            , "fstore_3"        , "b"    , -1, STORE);
 444         def(DSTORE_0            , "dstore_0"        , "b"    , -2, STORE);
 445         def(DSTORE_1            , "dstore_1"        , "b"    , -2, STORE);
 446         def(DSTORE_2            , "dstore_2"        , "b"    , -2, STORE);
 447         def(DSTORE_3            , "dstore_3"        , "b"    , -2, STORE);
 448         def(ASTORE_0            , "astore_0"        , "b"    , -1, STORE);
 449         def(ASTORE_1            , "astore_1"        , "b"    , -1, STORE);
 450         def(ASTORE_2            , "astore_2"        , "b"    , -1, STORE);
 451         def(ASTORE_3            , "astore_3"        , "b"    , -1, STORE);
 452         def(IASTORE             , "iastore"         , "b"    , -3, TRAP);
 453         def(LASTORE             , "lastore"         , "b"    , -4, TRAP);
 454         def(FASTORE             , "fastore"         , "b"    , -3, TRAP);
 455         def(DASTORE             , "dastore"         , "b"    , -4, TRAP);
 456         def(AASTORE             , "aastore"         , "b"    , -3, TRAP);
 457         def(BASTORE             , "bastore"         , "b"    , -3, TRAP);
 458         def(CASTORE             , "castore"         , "b"    , -3, TRAP);
 459         def(SASTORE             , "sastore"         , "b"    , -3, TRAP);
 460         def(POP                 , "pop"             , "b"    , -1);
 461         def(POP2                , "pop2"            , "b"    , -2);
 462         def(DUP                 , "dup"             , "b"    ,  1);
 463         def(DUP_X1              , "dup_x1"          , "b"    ,  1);
 464         def(DUP_X2              , "dup_x2"          , "b"    ,  1);
 465         def(DUP2                , "dup2"            , "b"    ,  2);
 466         def(DUP2_X1             , "dup2_x1"         , "b"    ,  2);
 467         def(DUP2_X2             , "dup2_x2"         , "b"    ,  2);
 468         def(SWAP                , "swap"            , "b"    ,  0);
 469         def(IADD                , "iadd"            , "b"    , -1, COMMUTATIVE | ASSOCIATIVE);
 470         def(LADD                , "ladd"            , "b"    , -2, COMMUTATIVE | ASSOCIATIVE);
 471         def(FADD                , "fadd"            , "b"    , -1, COMMUTATIVE | ASSOCIATIVE);
 472         def(DADD                , "dadd"            , "b"    , -2, COMMUTATIVE | ASSOCIATIVE);
 473         def(ISUB                , "isub"            , "b"    , -1);
 474         def(LSUB                , "lsub"            , "b"    , -2);
 475         def(FSUB                , "fsub"            , "b"    , -1);
 476         def(DSUB                , "dsub"            , "b"    , -2);
 477         def(IMUL                , "imul"            , "b"    , -1, COMMUTATIVE | ASSOCIATIVE);
 478         def(LMUL                , "lmul"            , "b"    , -2, COMMUTATIVE | ASSOCIATIVE);
 479         def(FMUL                , "fmul"            , "b"    , -1, COMMUTATIVE | ASSOCIATIVE);
 480         def(DMUL                , "dmul"            , "b"    , -2, COMMUTATIVE | ASSOCIATIVE);
 481         def(IDIV                , "idiv"            , "b"    , -1, TRAP);
 482         def(LDIV                , "ldiv"            , "b"    , -2, TRAP);
 483         def(FDIV                , "fdiv"            , "b"    , -1);
 484         def(DDIV                , "ddiv"            , "b"    , -2);
 485         def(IREM                , "irem"            , "b"    , -1, TRAP);
 486         def(LREM                , "lrem"            , "b"    , -2, TRAP);
 487         def(FREM                , "frem"            , "b"    , -1);
 488         def(DREM                , "drem"            , "b"    , -2);
 489         def(INEG                , "ineg"            , "b"    ,  0);
 490         def(LNEG                , "lneg"            , "b"    ,  0);
 491         def(FNEG                , "fneg"            , "b"    ,  0);
 492         def(DNEG                , "dneg"            , "b"    ,  0);
 493         def(ISHL                , "ishl"            , "b"    , -1);
 494         def(LSHL                , "lshl"            , "b"    , -1);
 495         def(ISHR                , "ishr"            , "b"    , -1);
 496         def(LSHR                , "lshr"            , "b"    , -1);
 497         def(IUSHR               , "iushr"           , "b"    , -1);
 498         def(LUSHR               , "lushr"           , "b"    , -1);
 499         def(IAND                , "iand"            , "b"    , -1, COMMUTATIVE | ASSOCIATIVE);
 500         def(LAND                , "land"            , "b"    , -2, COMMUTATIVE | ASSOCIATIVE);
 501         def(IOR                 , "ior"             , "b"    , -1, COMMUTATIVE | ASSOCIATIVE);
 502         def(LOR                 , "lor"             , "b"    , -2, COMMUTATIVE | ASSOCIATIVE);
 503         def(IXOR                , "ixor"            , "b"    , -1, COMMUTATIVE | ASSOCIATIVE);
 504         def(LXOR                , "lxor"            , "b"    , -2, COMMUTATIVE | ASSOCIATIVE);
 505         def(IINC                , "iinc"            , "bic"  ,  0, LOAD | STORE);
 506         def(I2L                 , "i2l"             , "b"    ,  1);
 507         def(I2F                 , "i2f"             , "b"    ,  0);
 508         def(I2D                 , "i2d"             , "b"    ,  1);
 509         def(L2I                 , "l2i"             , "b"    , -1);
 510         def(L2F                 , "l2f"             , "b"    , -1);
 511         def(L2D                 , "l2d"             , "b"    ,  0);
 512         def(F2I                 , "f2i"             , "b"    ,  0);
 513         def(F2L                 , "f2l"             , "b"    ,  1);
 514         def(F2D                 , "f2d"             , "b"    ,  1);
 515         def(D2I                 , "d2i"             , "b"    , -1);
 516         def(D2L                 , "d2l"             , "b"    ,  0);
 517         def(D2F                 , "d2f"             , "b"    , -1);
 518         def(I2B                 , "i2b"             , "b"    ,  0);
 519         def(I2C                 , "i2c"             , "b"    ,  0);
 520         def(I2S                 , "i2s"             , "b"    ,  0);
 521         def(LCMP                , "lcmp"            , "b"    , -3);
 522         def(FCMPL               , "fcmpl"           , "b"    , -1);
 523         def(FCMPG               , "fcmpg"           , "b"    , -1);
 524         def(DCMPL               , "dcmpl"           , "b"    , -3);
 525         def(DCMPG               , "dcmpg"           , "b"    , -3);
 526         def(IFEQ                , "ifeq"            , "boo"  , -1, FALL_THROUGH | BRANCH);
 527         def(IFNE                , "ifne"            , "boo"  , -1, FALL_THROUGH | BRANCH);
 528         def(IFLT                , "iflt"            , "boo"  , -1, FALL_THROUGH | BRANCH);
 529         def(IFGE                , "ifge"            , "boo"  , -1, FALL_THROUGH | BRANCH);
 530         def(IFGT                , "ifgt"            , "boo"  , -1, FALL_THROUGH | BRANCH);
 531         def(IFLE                , "ifle"            , "boo"  , -1, FALL_THROUGH | BRANCH);
 532         def(IF_ICMPEQ           , "if_icmpeq"       , "boo"  , -2, COMMUTATIVE | FALL_THROUGH | BRANCH);
 533         def(IF_ICMPNE           , "if_icmpne"       , "boo"  , -2, COMMUTATIVE | FALL_THROUGH | BRANCH);
 534         def(IF_ICMPLT           , "if_icmplt"       , "boo"  , -2, FALL_THROUGH | BRANCH);
 535         def(IF_ICMPGE           , "if_icmpge"       , "boo"  , -2, FALL_THROUGH | BRANCH);
 536         def(IF_ICMPGT           , "if_icmpgt"       , "boo"  , -2, FALL_THROUGH | BRANCH);
 537         def(IF_ICMPLE           , "if_icmple"       , "boo"  , -2, FALL_THROUGH | BRANCH);
 538         def(IF_ACMPEQ           , "if_acmpeq"       , "boo"  , -2, COMMUTATIVE | FALL_THROUGH | BRANCH);
 539         def(IF_ACMPNE           , "if_acmpne"       , "boo"  , -2, COMMUTATIVE | FALL_THROUGH | BRANCH);
 540         def(GOTO                , "goto"            , "boo"  ,  0, STOP | BRANCH);
 541         def(JSR                 , "jsr"             , "boo"  ,  0, STOP | BRANCH);
 542         def(RET                 , "ret"             , "bi"   ,  0, STOP);
 543         def(TABLESWITCH         , "tableswitch"     , ""     , -1, STOP);
 544         def(LOOKUPSWITCH        , "lookupswitch"    , ""     , -1, STOP);
 545         def(IRETURN             , "ireturn"         , "b"    , -1, TRAP | STOP);
 546         def(LRETURN             , "lreturn"         , "b"    , -2, TRAP | STOP);
 547         def(FRETURN             , "freturn"         , "b"    , -1, TRAP | STOP);
 548         def(DRETURN             , "dreturn"         , "b"    , -2, TRAP | STOP);
 549         def(ARETURN             , "areturn"         , "b"    , -1, TRAP | STOP);
 550         def(RETURN              , "return"          , "b"    ,  0, TRAP | STOP);
 551         def(GETSTATIC           , "getstatic"       , "bjj"  ,  1, TRAP | FIELD_READ);
 552         def(PUTSTATIC           , "putstatic"       , "bjj"  , -1, TRAP | FIELD_WRITE);
 553         def(GETFIELD            , "getfield"        , "bjj"  ,  0, TRAP | FIELD_READ);
 554         def(PUTFIELD            , "putfield"        , "bjj"  , -2, TRAP | FIELD_WRITE);
 555         def(INVOKEVIRTUAL       , "invokevirtual"   , "bjj"  , -1, TRAP | INVOKE);
 556         def(INVOKESPECIAL       , "invokespecial"   , "bjj"  , -1, TRAP | INVOKE);
 557         def(INVOKESTATIC        , "invokestatic"    , "bjj"  ,  0, TRAP | INVOKE);
 558         def(INVOKEINTERFACE     , "invokeinterface" , "bjja_", -1, TRAP | INVOKE);
 559         def(INVOKEDYNAMIC       , "invokedynamic"   , "bjjjj",  0, TRAP | INVOKE);
 560         def(NEW                 , "new"             , "bii"  ,  1, TRAP);
 561         def(NEWARRAY            , "newarray"        , "bc"   ,  0, TRAP);
 562         def(ANEWARRAY           , "anewarray"       , "bii"  ,  0, TRAP);
 563         def(ARRAYLENGTH         , "arraylength"     , "b"    ,  0, TRAP);
 564         def(ATHROW              , "athrow"          , "b"    , -1, TRAP | STOP);
 565         def(CHECKCAST           , "checkcast"       , "bii"  ,  0, TRAP);
 566         def(INSTANCEOF          , "instanceof"      , "bii"  ,  0, TRAP);
 567         def(MONITORENTER        , "monitorenter"    , "b"    , -1, TRAP);
 568         def(MONITOREXIT         , "monitorexit"     , "b"    , -1, TRAP);
 569         def(WIDE                , "wide"            , ""     ,  0);
 570         def(MULTIANEWARRAY      , "multianewarray"  , "biic" ,  1, TRAP);
 571         def(IFNULL              , "ifnull"          , "boo"  , -1, FALL_THROUGH | BRANCH);
 572         def(IFNONNULL           , "ifnonnull"       , "boo"  , -1, FALL_THROUGH | BRANCH);
 573         def(GOTO_W              , "goto_w"          , "boooo",  0, STOP | BRANCH);
 574         def(JSR_W               , "jsr_w"           , "boooo",  0, STOP | BRANCH);
 575         def(BREAKPOINT          , "breakpoint"      , "b"    ,  0, TRAP);
 576     }
 577     // @formatter:on
 578     // Checkstyle: resume
 579 
 580     /**
 581      * Determines if an opcode is commutative.
 582      *
 583      * @param opcode the opcode to check
 584      * @return {@code true} iff commutative
 585      */
 586     public static boolean isCommutative(int opcode) {
 587         return (flagsArray[opcode & 0xff] & COMMUTATIVE) != 0;
 588     }
 589 
 590     /**
 591      * Gets the length of an instruction denoted by a given opcode.
 592      *
 593      * @param opcode an instruction opcode
 594      * @return the length of the instruction denoted by {@code opcode}. If {@code opcode} is an
 595      *         illegal instruction or denotes a variable length instruction (e.g.
 596      *         {@link #TABLESWITCH}), then 0 is returned.
 597      */
 598     public static int lengthOf(int opcode) {
 599         return lengthArray[opcode & 0xff];
 600     }
 601 
 602     /**
 603      * Gets the effect on the depth of the expression stack of an instruction denoted by a given
 604      * opcode.
 605      *
 606      * @param opcode an instruction opcode
 607      * @return the change in the stack caused by the instruction denoted by {@code opcode}. If
 608      *         {@code opcode} is an illegal instruction then 0 is returned. Note that invoke
 609      *         instructions may pop more arguments so this value is a minimum stack effect.
 610      */
 611     public static int stackEffectOf(int opcode) {
 612         return stackEffectArray[opcode & 0xff];
 613     }
 614 
 615     /**
 616      * Gets the lower-case mnemonic for a given opcode.
 617      *
 618      * @param opcode an opcode
 619      * @return the mnemonic for {@code opcode} or {@code "<illegal opcode: " + opcode + ">"} if
 620      *         {@code opcode} is not a legal opcode
 621      */
 622     public static String nameOf(int opcode) throws IllegalArgumentException {
 623         String name = nameArray[opcode & 0xff];
 624         if (name == null) {
 625             return "<illegal opcode: " + opcode + ">";
 626         }
 627         return name;
 628     }
 629 
 630     /**
 631      * Allocation-free version of {@linkplain #nameOf(int)}.
 632      *
 633      * @param opcode an opcode.
 634      * @return the mnemonic for {@code opcode} or {@code "<illegal opcode>"} if {@code opcode} is
 635      *         not a legal opcode.
 636      */
 637     public static String baseNameOf(int opcode) {
 638         String name = nameArray[opcode & 0xff];
 639         if (name == null) {
 640             return "<illegal opcode>";
 641         }
 642         return name;
 643     }
 644 
 645     /**
 646      * Gets the opcode corresponding to a given mnemonic.
 647      *
 648      * @param name an opcode mnemonic
 649      * @return the opcode corresponding to {@code mnemonic}
 650      * @throws IllegalArgumentException if {@code name} does not denote a valid opcode
 651      */
 652     public static int valueOf(String name) {
 653         for (int opcode = 0; opcode < nameArray.length; ++opcode) {
 654             if (name.equalsIgnoreCase(nameArray[opcode])) {
 655                 return opcode;
 656             }
 657         }
 658         throw new IllegalArgumentException("No opcode for " + name);
 659     }
 660 
 661     /**
 662      * Determines if a given opcode denotes an instruction that can cause an implicit exception.
 663      *
 664      * @param opcode an opcode to test
 665      * @return {@code true} iff {@code opcode} can cause an implicit exception, {@code false}
 666      *         otherwise
 667      */
 668     public static boolean canTrap(int opcode) {
 669         return (flagsArray[opcode & 0xff] & TRAP) != 0;
 670     }
 671 
 672     /**
 673      * Determines if a given opcode denotes an instruction that loads a local variable to the
 674      * operand stack.
 675      *
 676      * @param opcode an opcode to test
 677      * @return {@code true} iff {@code opcode} loads a local variable to the operand stack,
 678      *         {@code false} otherwise
 679      */
 680     public static boolean isLoad(int opcode) {
 681         return (flagsArray[opcode & 0xff] & LOAD) != 0;
 682     }
 683 
 684     /**
 685      * Determines if a given opcode denotes an instruction that ends a basic block and does not let
 686      * control flow fall through to its lexical successor.
 687      *
 688      * @param opcode an opcode to test
 689      * @return {@code true} iff {@code opcode} properly ends a basic block
 690      */
 691     public static boolean isStop(int opcode) {
 692         return (flagsArray[opcode & 0xff] & STOP) != 0;
 693     }
 694 
 695     /**
 696      * Determines if a given opcode denotes an instruction that stores a value to a local variable
 697      * after popping it from the operand stack.
 698      *
 699      * @param opcode an opcode to test
 700      * @return {@code true} iff {@code opcode} stores a value to a local variable, {@code false}
 701      *         otherwise
 702      */
 703     public static boolean isInvoke(int opcode) {
 704         return (flagsArray[opcode & 0xff] & INVOKE) != 0;
 705     }
 706 
 707     /**
 708      * Determines if a given opcode denotes an instruction that stores a value to a local variable
 709      * after popping it from the operand stack.
 710      *
 711      * @param opcode an opcode to test
 712      * @return {@code true} iff {@code opcode} stores a value to a local variable, {@code false}
 713      *         otherwise
 714      */
 715     public static boolean isStore(int opcode) {
 716         return (flagsArray[opcode & 0xff] & STORE) != 0;
 717     }
 718 
 719     /**
 720      * Determines if a given opcode is an instruction that delimits a basic block.
 721      *
 722      * @param opcode an opcode to test
 723      * @return {@code true} iff {@code opcode} delimits a basic block
 724      */
 725     public static boolean isBlockEnd(int opcode) {
 726         return (flagsArray[opcode & 0xff] & (STOP | FALL_THROUGH)) != 0;
 727     }
 728 
 729     /**
 730      * Determines if a given opcode is an instruction that has a 2 or 4 byte operand that is an
 731      * offset to another instruction in the same method. This does not include the
 732      * {@linkplain #TABLESWITCH switch} instructions.
 733      *
 734      * @param opcode an opcode to test
 735      * @return {@code true} iff {@code opcode} is a branch instruction with a single operand
 736      */
 737     public static boolean isBranch(int opcode) {
 738         return (flagsArray[opcode & 0xff] & BRANCH) != 0;
 739     }
 740 
 741     /**
 742      * Determines if a given opcode denotes a conditional branch.
 743      *
 744      * @param opcode
 745      * @return {@code true} iff {@code opcode} is a conditional branch
 746      */
 747     public static boolean isConditionalBranch(int opcode) {
 748         return (flagsArray[opcode & 0xff] & FALL_THROUGH) != 0;
 749     }
 750 
 751     /**
 752      * Gets the arithmetic operator name for a given opcode. If {@code opcode} does not denote an
 753      * arithmetic instruction, then the {@linkplain #nameOf(int) name} of the opcode is returned
 754      * instead.
 755      *
 756      * @param op an opcode
 757      * @return the arithmetic operator name
 758      */
 759     public static String operator(int op) {
 760         // Checkstyle: stop
 761         switch (op) {
 762             // arithmetic ops
 763             case IADD: // fall through
 764             case LADD: // fall through
 765             case FADD: // fall through
 766             case DADD:
 767                 return "+";
 768             case ISUB: // fall through
 769             case LSUB: // fall through
 770             case FSUB: // fall through
 771             case DSUB:
 772                 return "-";
 773             case IMUL: // fall through
 774             case LMUL: // fall through
 775             case FMUL: // fall through
 776             case DMUL:
 777                 return "*";
 778             case IDIV: // fall through
 779             case LDIV: // fall through
 780             case FDIV: // fall through
 781             case DDIV:
 782                 return "/";
 783             case IREM: // fall through
 784             case LREM: // fall through
 785             case FREM: // fall through
 786             case DREM:
 787                 return "%";
 788             // shift ops
 789             case ISHL: // fall through
 790             case LSHL:
 791                 return "<<";
 792             case ISHR: // fall through
 793             case LSHR:
 794                 return ">>";
 795             case IUSHR: // fall through
 796             case LUSHR:
 797                 return ">>>";
 798             // logic ops
 799             case IAND: // fall through
 800             case LAND:
 801                 return "&";
 802             case IOR: // fall through
 803             case LOR:
 804                 return "|";
 805             case IXOR: // fall through
 806             case LXOR:
 807                 return "^";
 808         }
 809         // Checkstyle: resume
 810         return nameOf(op);
 811     }
 812 
 813     /**
 814      * Defines a bytecode by entering it into the arrays that record its name, length and flags.
 815      *
 816      * @param name instruction name (should be lower case)
 817      * @param format encodes the length of the instruction
 818      */
 819     private static void def(int opcode, String name, String format, int stackEffect) {
 820         def(opcode, name, format, stackEffect, 0);
 821     }
 822 
 823     /**
 824      * Defines a bytecode by entering it into the arrays that record its name, length and flags.
 825      *
 826      * @param name instruction name (lower case)
 827      * @param format encodes the length of the instruction
 828      * @param flags the set of {@link Flags} associated with the instruction
 829      */
 830     private static void def(int opcode, String name, String format, int stackEffect, int flags) {
 831         assert nameArray[opcode] == null : "opcode " + opcode + " is already bound to name " + nameArray[opcode];
 832         nameArray[opcode] = name;
 833         int instructionLength = format.length();
 834         lengthArray[opcode] = instructionLength;
 835         stackEffectArray[opcode] = stackEffect;
 836         Bytecodes.flagsArray[opcode] = flags;
 837 
 838         assert !isConditionalBranch(opcode) || isBranch(opcode) : "a conditional branch must also be a branch";
 839     }
 840 }