langtools/src/share/classes/com/sun/tools/classfile/Instruction.java
changeset 2512 70eb5f17c5f8
child 4411 84c397e2ee67
equal deleted inserted replaced
2511:1ccb94d04005 2512:70eb5f17c5f8
       
     1 /*
       
     2  * Copyright 2009 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package com.sun.tools.classfile;
       
    27 
       
    28 /**
       
    29  * See JVMS3, chapter 6.
       
    30  *
       
    31  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
       
    32  *  you write code that depends on this, you do so at your own risk.
       
    33  *  This code and its internal interfaces are subject to change or
       
    34  *  deletion without notice.</b>
       
    35  *
       
    36  * @see Code_attribute#getInstructions
       
    37  */
       
    38 public class Instruction {
       
    39     /** The kind of an instruction, as determined by the position, size and
       
    40      *  types of its operands. */
       
    41     public static enum Kind {
       
    42         /** Opcode is not followed by any operands. */
       
    43         NO_OPERANDS(1),
       
    44         /** Opcode is followed by a byte indicating a type. */
       
    45         ATYPE(2),
       
    46         /** Opcode is followed by a 2-byte branch offset. */
       
    47         BRANCH(3),
       
    48         /** Opcode is followed by a 4-byte branch offset. */
       
    49         BRANCH_W(5),
       
    50         /** Opcode is followed by a signed byte value. */
       
    51         BYTE(2),
       
    52         /** Opcode is followed by a 1-byte index into the constant pool. */
       
    53         CPREF(2),
       
    54         /** Opcode is followed by a 2-byte index into the constant pool. */
       
    55         CPREF_W(3),
       
    56         /** Opcode is followed by a 2-byte index into the constant pool,
       
    57          *  an unsigned byte value. */
       
    58         CPREF_W_UBYTE(4),
       
    59         /** Opcode is followed by a 2-byte index into the constant pool.,
       
    60          *  an unsigned byte value, and a zero byte. */
       
    61         CPREF_W_UBYTE_ZERO(5),
       
    62         /** Opcode is followed by variable number of operands, depending
       
    63          * on the instruction.*/
       
    64         DYNAMIC(-1),
       
    65         /** Opcode is followed by a 1-byte reference to a local variable. */
       
    66         LOCAL(2),
       
    67         /** Opcode is followed by a 1-byte reference to a local variable,
       
    68          *  and a signed byte value. */
       
    69         LOCAL_BYTE(3),
       
    70         /** Opcode is followed by a signed short value. */
       
    71         SHORT(3),
       
    72         /** Wide opcode is not followed by any operands. */
       
    73         WIDE_NO_OPERANDS(2),
       
    74         /** Wide opcode is followed by a 2-byte index into the constant pool. */
       
    75         WIDE_CPREF_W(4),
       
    76         /** Wide opcode is followed by a 2-byte index into the constant pool,
       
    77          *  and a signed short value. */
       
    78         WIDE_CPREF_W_SHORT(6),
       
    79         /** Opcode was not recognized. */
       
    80         UNKNOWN(1);
       
    81 
       
    82         Kind(int length) {
       
    83             this.length = length;
       
    84         }
       
    85 
       
    86         /** The length, in bytes, of this kind of instruction, or -1 is the
       
    87          *  length depends on the specific instruction. */
       
    88         public final int length;
       
    89     };
       
    90 
       
    91     /** A utility visitor to help decode the operands of an instruction.
       
    92      *  @see Instruction#accept */
       
    93     public interface KindVisitor<R,P> {
       
    94         /** See {@link Kind#NO_OPERANDS}, {@link Kind#WIDE_NO_OPERANDS}. */
       
    95         R visitNoOperands(Instruction instr, P p);
       
    96         /** See {@link Kind#ATYPE}. */
       
    97         R visitArrayType(Instruction instr, TypeKind kind, P p);
       
    98         /** See {@link Kind#BRANCH}, {@link Kind#BRANCH_W}. */
       
    99         R visitBranch(Instruction instr, int offset, P p);
       
   100         /** See {@link Kind#CPREF}, {@link Kind#CPREF_W}, {@link Kind#WIDE_CPREF_W}. */
       
   101         R visitConstantPoolRef(Instruction instr, int index, P p);
       
   102         /** See {@link Kind#CPREF_W_UBYTE}, {@link Kind#CPREF_W_UBYTE_ZERO}, {@link Kind#WIDE_CPREF_W_SHORT}. */
       
   103         R visitConstantPoolRefAndValue(Instruction instr, int index, int value, P p);
       
   104         /** See {@link Kind#LOCAL}. */
       
   105         R visitLocal(Instruction instr, int index, P p);
       
   106         /** See {@link Kind#LOCAL_UBYTE}. */
       
   107         R visitLocalAndValue(Instruction instr, int index, int value, P p);
       
   108         /** See {@link Kind#DYNAMIC}. */
       
   109         R visitLookupSwitch(Instruction instr, int default_, int npairs, int[] matches, int[] offsets);
       
   110         /** See {@link Kind#DYNAMIC}. */
       
   111         R visitTableSwitch(Instruction instr, int default_, int low, int high, int[] offsets);
       
   112         /** See {@link Kind#BYTE}, {@link Kind#SHORT}. */
       
   113         R visitValue(Instruction instr, int value, P p);
       
   114         /** Instruction is unrecognized. */
       
   115         R visitUnknown(Instruction instr, P p);
       
   116 
       
   117     }
       
   118 
       
   119     /** The kind of primitive array type to create.
       
   120      *  See JVMS chapter 6, newarray. */
       
   121     public static enum TypeKind {
       
   122         T_BOOLEAN(4, "boolean"),
       
   123         T_CHAR(5, "char"),
       
   124         T_FLOAT(6, "float"),
       
   125         T_DOUBLE(7, "double"),
       
   126         T_BYTE(8, "byte"),
       
   127         T_SHORT(9, "short"),
       
   128         T_INT (10, "int"),
       
   129         T_LONG (11, "long");
       
   130         TypeKind(int value, String name) {
       
   131             this.value = value;
       
   132             this.name = name;
       
   133         }
       
   134 
       
   135         public static TypeKind get(int value) {
       
   136             switch (value) {
       
   137                 case  4: return T_BOOLEAN;
       
   138                 case  5: return T_CHAR;
       
   139                 case  6: return T_FLOAT;
       
   140                 case  7: return T_DOUBLE;
       
   141                 case  8: return T_BYTE;
       
   142                 case  9: return T_SHORT;
       
   143                 case  10: return T_INT;
       
   144                 case  11: return T_LONG;
       
   145                 default: return null;
       
   146             }
       
   147         }
       
   148 
       
   149         public final int value;
       
   150         public final String name;
       
   151     }
       
   152 
       
   153     /** An instruction is defined by its position in a bytecode array. */
       
   154     public Instruction(byte[] bytes, int pc) {
       
   155         this.bytes = bytes;
       
   156         this.pc = pc;
       
   157     }
       
   158 
       
   159     /** Get the position of the instruction within the bytecode array. */
       
   160     public int getPC() {
       
   161         return pc;
       
   162     }
       
   163 
       
   164     /** Get a byte value, relative to the start of this instruction. */
       
   165     public int getByte(int offset) {
       
   166         return bytes[pc + offset];
       
   167     }
       
   168 
       
   169     /** Get an unsigned byte value, relative to the start of this instruction. */
       
   170     public int getUnsignedByte(int offset) {
       
   171         return getByte(offset) & 0xff;
       
   172     }
       
   173 
       
   174     /** Get a 2-byte value, relative to the start of this instruction. */
       
   175     public int getShort(int offset) {
       
   176         return (getByte(offset) << 8) | getUnsignedByte(offset + 1);
       
   177     }
       
   178 
       
   179     /** Get a unsigned 2-byte value, relative to the start of this instruction. */
       
   180     public int getUnsignedShort(int offset) {
       
   181         return getShort(offset) & 0xFFFF;
       
   182     }
       
   183 
       
   184     /** Get a 4-byte value, relative to the start of this instruction. */
       
   185     public int getInt(int offset) {
       
   186         return (getShort(offset) << 16) | (getUnsignedShort(offset + 2));
       
   187     }
       
   188 
       
   189     /** Get the Opcode for this instruction, or null if the instruction is
       
   190      * unrecognized. */
       
   191     public Opcode getOpcode() {
       
   192         int b = getUnsignedByte(0);
       
   193         switch (b) {
       
   194             case Opcode.NONPRIV:
       
   195             case Opcode.PRIV:
       
   196             case Opcode.WIDE:
       
   197                 return Opcode.get(b, getUnsignedByte(1));
       
   198         }
       
   199         return Opcode.get(b);
       
   200     }
       
   201 
       
   202     /** Get the mnemonic for this instruction, or a default string if the
       
   203      * instruction is unrecognized. */
       
   204     public String getMnemonic() {
       
   205         Opcode opcode = getOpcode();
       
   206         if (opcode == null)
       
   207             return "bytecode " + getUnsignedByte(0);
       
   208         else
       
   209             return opcode.toString().toLowerCase();
       
   210     }
       
   211 
       
   212     /** Get the length, in bytes, of this instruction, including the opcode
       
   213      * and all its operands. */
       
   214     public int length() {
       
   215         Opcode opcode = getOpcode();
       
   216         if (opcode == null)
       
   217             return 1;
       
   218 
       
   219         switch (opcode) {
       
   220             case TABLESWITCH: {
       
   221                 int pad = align(pc + 1) - pc;
       
   222                 int low = getInt(pad + 4);
       
   223                 int high = getInt(pad + 8);
       
   224                 return pad + 12 + 4 * (high - low + 1);
       
   225             }
       
   226             case LOOKUPSWITCH: {
       
   227                 int pad = align(pc + 1) - pc;
       
   228                 int npairs = getInt(pad + 4);
       
   229                 return pad + 8 + 8 * npairs;
       
   230 
       
   231             }
       
   232             default:
       
   233                 return opcode.kind.length;
       
   234         }
       
   235     }
       
   236 
       
   237     /** Get the {@link Kind} of this instruction. */
       
   238     public Kind getKind() {
       
   239         Opcode opcode = getOpcode();
       
   240         return (opcode != null ? opcode.kind : Kind.UNKNOWN);
       
   241     }
       
   242 
       
   243     /** Invoke a method on the visitor according to the kind of this
       
   244      * instruction, passing in the decoded operands for the instruction. */
       
   245     public <R,P> R accept(KindVisitor<R,P> visitor, P p) {
       
   246         switch (getKind()) {
       
   247             case NO_OPERANDS:
       
   248                 return visitor.visitNoOperands(this, p);
       
   249 
       
   250             case ATYPE:
       
   251                 return visitor.visitArrayType(
       
   252                         this, TypeKind.get(getUnsignedByte(1)), p);
       
   253 
       
   254             case BRANCH:
       
   255                 return visitor.visitBranch(this, getShort(1), p);
       
   256 
       
   257             case BRANCH_W:
       
   258                 return visitor.visitBranch(this, getInt(1), p);
       
   259 
       
   260             case BYTE:
       
   261                 return visitor.visitValue(this, getByte(1), p);
       
   262 
       
   263             case CPREF:
       
   264                 return visitor.visitConstantPoolRef(this, getUnsignedByte(1), p);
       
   265 
       
   266             case CPREF_W:
       
   267                 return visitor.visitConstantPoolRef(this, getUnsignedShort(1), p);
       
   268 
       
   269             case CPREF_W_UBYTE:
       
   270             case CPREF_W_UBYTE_ZERO:
       
   271                 return visitor.visitConstantPoolRefAndValue(
       
   272                         this, getUnsignedShort(1), getUnsignedByte(3), p);
       
   273 
       
   274             case DYNAMIC: {
       
   275                 switch (getOpcode()) {
       
   276                     case TABLESWITCH: {
       
   277                         int pad = align(pc + 1) - pc;
       
   278                         int default_ = getInt(pad);
       
   279                         int low = getInt(pad + 4);
       
   280                         int high = getInt(pad + 8);
       
   281                         int[] values = new int[high - low + 1];
       
   282                         for (int i = 0; i < values.length; i++)
       
   283                             values[i] = getInt(pad + 12 + 4 * i);
       
   284                         return visitor.visitTableSwitch(
       
   285                                 this, default_, low, high, values);
       
   286                     }
       
   287                     case LOOKUPSWITCH: {
       
   288                         int pad = align(pc + 1) - pc;
       
   289                         int default_ = getInt(pad);
       
   290                         int npairs = getInt(pad + 4);
       
   291                         int[] matches = new int[npairs];
       
   292                         int[] offsets = new int[npairs];
       
   293                         for (int i = 0; i < npairs; i++) {
       
   294                             matches[i] = getInt(pad +  8 + i * 8);
       
   295                             offsets[i] = getInt(pad + 12 + i * 8);
       
   296                         }
       
   297                         return visitor.visitLookupSwitch(
       
   298                                 this, default_, npairs, matches, offsets);
       
   299                     }
       
   300                     default:
       
   301                         throw new IllegalStateException();
       
   302                 }
       
   303             }
       
   304 
       
   305             case LOCAL:
       
   306                 return visitor.visitLocal(this, getUnsignedByte(1), p);
       
   307 
       
   308             case LOCAL_BYTE:
       
   309                 return visitor.visitLocalAndValue(
       
   310                         this, getUnsignedByte(1), getByte(2), p);
       
   311 
       
   312             case SHORT:
       
   313                 return visitor.visitValue(this, getShort(1), p);
       
   314 
       
   315             case WIDE_NO_OPERANDS:
       
   316                 return visitor.visitNoOperands(this, p);
       
   317 
       
   318             case WIDE_CPREF_W:
       
   319                 return visitor.visitConstantPoolRef(this, getUnsignedShort(2), p);
       
   320 
       
   321             case WIDE_CPREF_W_SHORT:
       
   322                 return visitor.visitConstantPoolRefAndValue(
       
   323                         this, getUnsignedShort(2), getUnsignedByte(4), p);
       
   324 
       
   325             case UNKNOWN:
       
   326                 return visitor.visitUnknown(this, p);
       
   327 
       
   328             default:
       
   329                 throw new IllegalStateException();
       
   330         }
       
   331     }
       
   332 
       
   333     private static int align(int n) {
       
   334         return (n + 3) & ~3;
       
   335     }
       
   336 
       
   337     private byte[] bytes;
       
   338     private int pc;
       
   339 }