0N/A/*
3261N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.classfile;
0N/A
0N/A/**
0N/A * See JVMS, chapter 6.
0N/A *
0N/A * <p><b>This is NOT part of any supported API.
0N/A * If you write code that depends on this, you do so at your own risk.
0N/A * This code and its internal interfaces are subject to change or
0N/A * deletion without notice.</b>
0N/A *
0N/A * @see Code_attribute#getInstructions
0N/A */
0N/Apublic class Instruction {
1999N/A /** The kind of an instruction, as determined by the position, size and
0N/A * types of its operands. */
0N/A public static enum Kind {
0N/A /** Opcode is not followed by any operands. */
0N/A NO_OPERANDS(1),
0N/A /** Opcode is followed by a byte indicating a type. */
0N/A ATYPE(2),
0N/A /** Opcode is followed by a 2-byte branch offset. */
0N/A BRANCH(3),
0N/A /** Opcode is followed by a 4-byte branch offset. */
0N/A BRANCH_W(5),
0N/A /** Opcode is followed by a signed byte value. */
1999N/A BYTE(2),
0N/A /** Opcode is followed by a 1-byte index into the constant pool. */
1999N/A CPREF(2),
1999N/A /** Opcode is followed by a 2-byte index into the constant pool. */
0N/A CPREF_W(3),
0N/A /** Opcode is followed by a 2-byte index into the constant pool,
0N/A * an unsigned byte value. */
0N/A CPREF_W_UBYTE(4),
0N/A /** Opcode is followed by a 2-byte index into the constant pool.,
0N/A * an unsigned byte value, and a zero byte. */
1999N/A CPREF_W_UBYTE_ZERO(5),
0N/A /** Opcode is followed by variable number of operands, depending
0N/A * on the instruction.*/
0N/A DYNAMIC(-1),
0N/A /** Opcode is followed by a 1-byte reference to a local variable. */
0N/A LOCAL(2),
0N/A /** Opcode is followed by a 1-byte reference to a local variable,
0N/A * and a signed byte value. */
0N/A LOCAL_BYTE(3),
0N/A /** Opcode is followed by a signed short value. */
0N/A SHORT(3),
0N/A /** Wide opcode is not followed by any operands. */
0N/A WIDE_NO_OPERANDS(2),
0N/A /** Wide opcode is followed by a 2-byte index into the constant pool. */
0N/A WIDE_CPREF_W(4),
0N/A /** Wide opcode is followed by a 2-byte index into the constant pool,
0N/A * and a signed short value. */
0N/A WIDE_CPREF_W_SHORT(6),
0N/A /** Opcode was not recognized. */
0N/A UNKNOWN(1);
0N/A
0N/A Kind(int length) {
0N/A this.length = length;
0N/A }
0N/A
0N/A /** The length, in bytes, of this kind of instruction, or -1 is the
0N/A * length depends on the specific instruction. */
0N/A public final int length;
0N/A };
0N/A
0N/A /** A utility visitor to help decode the operands of an instruction.
0N/A * @see Instruction#accept */
0N/A public interface KindVisitor<R,P> {
0N/A /** See {@link Kind#NO_OPERANDS}, {@link Kind#WIDE_NO_OPERANDS}. */
0N/A R visitNoOperands(Instruction instr, P p);
0N/A /** See {@link Kind#ATYPE}. */
0N/A R visitArrayType(Instruction instr, TypeKind kind, P p);
0N/A /** See {@link Kind#BRANCH}, {@link Kind#BRANCH_W}. */
0N/A R visitBranch(Instruction instr, int offset, P p);
0N/A /** See {@link Kind#CPREF}, {@link Kind#CPREF_W}, {@link Kind#WIDE_CPREF_W}. */
0N/A R visitConstantPoolRef(Instruction instr, int index, P p);
0N/A /** See {@link Kind#CPREF_W_UBYTE}, {@link Kind#CPREF_W_UBYTE_ZERO}, {@link Kind#WIDE_CPREF_W_SHORT}. */
0N/A R visitConstantPoolRefAndValue(Instruction instr, int index, int value, P p);
0N/A /** See {@link Kind#LOCAL}. */
0N/A R visitLocal(Instruction instr, int index, P p);
0N/A /** See {@link Kind#LOCAL_UBYTE}. */
0N/A R visitLocalAndValue(Instruction instr, int index, int value, P p);
0N/A /** See {@link Kind#DYNAMIC}. */
0N/A R visitLookupSwitch(Instruction instr, int default_, int npairs, int[] matches, int[] offsets, P p);
0N/A /** See {@link Kind#DYNAMIC}. */
0N/A R visitTableSwitch(Instruction instr, int default_, int low, int high, int[] offsets, P p);
0N/A /** See {@link Kind#BYTE}, {@link Kind#SHORT}. */
0N/A R visitValue(Instruction instr, int value, P p);
0N/A /** Instruction is unrecognized. */
0N/A R visitUnknown(Instruction instr, P p);
0N/A
0N/A }
0N/A
0N/A /** The kind of primitive array type to create.
0N/A * See JVMS chapter 6, newarray. */
0N/A public static enum TypeKind {
0N/A T_BOOLEAN(4, "boolean"),
0N/A T_CHAR(5, "char"),
0N/A T_FLOAT(6, "float"),
0N/A T_DOUBLE(7, "double"),
0N/A T_BYTE(8, "byte"),
0N/A T_SHORT(9, "short"),
0N/A T_INT (10, "int"),
0N/A T_LONG (11, "long");
0N/A TypeKind(int value, String name) {
0N/A this.value = value;
0N/A this.name = name;
0N/A }
0N/A
0N/A public static TypeKind get(int value) {
0N/A switch (value) {
0N/A case 4: return T_BOOLEAN;
0N/A case 5: return T_CHAR;
0N/A case 6: return T_FLOAT;
0N/A case 7: return T_DOUBLE;
0N/A case 8: return T_BYTE;
0N/A case 9: return T_SHORT;
0N/A case 10: return T_INT;
0N/A case 11: return T_LONG;
0N/A default: return null;
0N/A }
0N/A }
0N/A
0N/A public final int value;
1999N/A public final String name;
1999N/A }
1999N/A
1999N/A /** An instruction is defined by its position in a bytecode array. */
0N/A public Instruction(byte[] bytes, int pc) {
1999N/A this.bytes = bytes;
0N/A this.pc = pc;
0N/A }
0N/A
0N/A /** Get the position of the instruction within the bytecode array. */
0N/A public int getPC() {
0N/A return pc;
0N/A }
1999N/A
2146N/A /** Get a byte value, relative to the start of this instruction. */
2146N/A public int getByte(int offset) {
2146N/A return bytes[pc + offset];
2146N/A }
2146N/A
2146N/A /** Get an unsigned byte value, relative to the start of this instruction. */
2146N/A public int getUnsignedByte(int offset) {
2146N/A return getByte(offset) & 0xff;
2146N/A }
2146N/A
1999N/A /** Get a 2-byte value, relative to the start of this instruction. */
1999N/A public int getShort(int offset) {
0N/A return (getByte(offset) << 8) | getUnsignedByte(offset + 1);
0N/A }
0N/A
0N/A /** Get a unsigned 2-byte value, relative to the start of this instruction. */
0N/A public int getUnsignedShort(int offset) {
0N/A return getShort(offset) & 0xFFFF;
0N/A }
0N/A
0N/A /** Get a 4-byte value, relative to the start of this instruction. */
0N/A public int getInt(int offset) {
1999N/A return (getShort(offset) << 16) | (getUnsignedShort(offset + 2));
1999N/A }
1999N/A
1999N/A /** Get the Opcode for this instruction, or null if the instruction is
1999N/A * unrecognized. */
0N/A public Opcode getOpcode() {
1999N/A int b = getUnsignedByte(0);
1999N/A switch (b) {
2146N/A case Opcode.NONPRIV:
2146N/A case Opcode.PRIV:
0N/A case Opcode.WIDE:
0N/A return Opcode.get(b, getUnsignedByte(1));
0N/A }
0N/A return Opcode.get(b);
0N/A }
0N/A
0N/A /** Get the mnemonic for this instruction, or a default string if the
0N/A * instruction is unrecognized. */
0N/A public String getMnemonic() {
0N/A Opcode opcode = getOpcode();
1999N/A if (opcode == null)
1999N/A return "bytecode " + getUnsignedByte(0);
1999N/A else
1999N/A return opcode.toString().toLowerCase();
0N/A }
0N/A
0N/A /** Get the length, in bytes, of this instruction, including the opcode
0N/A * and all its operands. */
0N/A public int length() {
1999N/A Opcode opcode = getOpcode();
1999N/A if (opcode == null)
1999N/A return 1;
1999N/A
1999N/A switch (opcode) {
0N/A case TABLESWITCH: {
0N/A int pad = align(pc + 1) - pc;
0N/A int low = getInt(pad + 4);
0N/A int high = getInt(pad + 8);
0N/A return pad + 12 + 4 * (high - low + 1);
0N/A }
0N/A case LOOKUPSWITCH: {
0N/A int pad = align(pc + 1) - pc;
0N/A int npairs = getInt(pad + 4);
0N/A return pad + 8 + 8 * npairs;
0N/A
0N/A }
0N/A default:
0N/A return opcode.kind.length;
0N/A }
1999N/A }
0N/A
0N/A /** Get the {@link Kind} of this instruction. */
0N/A public Kind getKind() {
0N/A Opcode opcode = getOpcode();
0N/A return (opcode != null ? opcode.kind : Kind.UNKNOWN);
0N/A }
0N/A
1999N/A /** Invoke a method on the visitor according to the kind of this
1999N/A * instruction, passing in the decoded operands for the instruction. */
1999N/A public <R,P> R accept(KindVisitor<R,P> visitor, P p) {
1999N/A switch (getKind()) {
0N/A case NO_OPERANDS:
0N/A return visitor.visitNoOperands(this, p);
0N/A
614N/A case ATYPE:
1999N/A return visitor.visitArrayType(
0N/A this, TypeKind.get(getUnsignedByte(1)), p);
0N/A
1999N/A case BRANCH:
1999N/A return visitor.visitBranch(this, getShort(1), p);
1999N/A
1999N/A case BRANCH_W:
0N/A return visitor.visitBranch(this, getInt(1), p);
0N/A
0N/A case BYTE:
0N/A return visitor.visitValue(this, getByte(1), p);
1999N/A
0N/A case CPREF:
0N/A return visitor.visitConstantPoolRef(this, getUnsignedByte(1), p);
0N/A
0N/A case CPREF_W:
0N/A return visitor.visitConstantPoolRef(this, getUnsignedShort(1), p);
0N/A
0N/A case CPREF_W_UBYTE:
1999N/A case CPREF_W_UBYTE_ZERO:
1999N/A return visitor.visitConstantPoolRefAndValue(
1999N/A this, getUnsignedShort(1), getUnsignedByte(3), p);
1999N/A
1999N/A case DYNAMIC: {
1999N/A switch (getOpcode()) {
1999N/A case TABLESWITCH: {
1999N/A int pad = align(pc + 1) - pc;
0N/A int default_ = getInt(pad);
0N/A int low = getInt(pad + 4);
int high = getInt(pad + 8);
int[] values = new int[high - low + 1];
for (int i = 0; i < values.length; i++)
values[i] = getInt(pad + 12 + 4 * i);
return visitor.visitTableSwitch(
this, default_, low, high, values, p);
}
case LOOKUPSWITCH: {
int pad = align(pc + 1) - pc;
int default_ = getInt(pad);
int npairs = getInt(pad + 4);
int[] matches = new int[npairs];
int[] offsets = new int[npairs];
for (int i = 0; i < npairs; i++) {
matches[i] = getInt(pad + 8 + i * 8);
offsets[i] = getInt(pad + 12 + i * 8);
}
return visitor.visitLookupSwitch(
this, default_, npairs, matches, offsets, p);
}
default:
throw new IllegalStateException();
}
}
case LOCAL:
return visitor.visitLocal(this, getUnsignedByte(1), p);
case LOCAL_BYTE:
return visitor.visitLocalAndValue(
this, getUnsignedByte(1), getByte(2), p);
case SHORT:
return visitor.visitValue(this, getShort(1), p);
case WIDE_NO_OPERANDS:
return visitor.visitNoOperands(this, p);
case WIDE_CPREF_W:
return visitor.visitConstantPoolRef(this, getUnsignedShort(2), p);
case WIDE_CPREF_W_SHORT:
return visitor.visitConstantPoolRefAndValue(
this, getUnsignedShort(2), getUnsignedByte(4), p);
case UNKNOWN:
return visitor.visitUnknown(this, p);
default:
throw new IllegalStateException();
}
}
private static int align(int n) {
return (n + 3) & ~3;
}
private byte[] bytes;
private int pc;
}