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