aoqi@0: /* aoqi@0: * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.classfile; aoqi@0: aoqi@0: import java.util.Locale; aoqi@0: aoqi@0: /** aoqi@0: * See JVMS, chapter 6. aoqi@0: * aoqi@0: *

This is NOT part of any supported API. aoqi@0: * If you write code that depends on this, you do so at your own risk. aoqi@0: * This code and its internal interfaces are subject to change or aoqi@0: * deletion without notice. aoqi@0: * aoqi@0: * @see Code_attribute#getInstructions aoqi@0: */ aoqi@0: public class Instruction { aoqi@0: /** The kind of an instruction, as determined by the position, size and aoqi@0: * types of its operands. */ aoqi@0: public static enum Kind { aoqi@0: /** Opcode is not followed by any operands. */ aoqi@0: NO_OPERANDS(1), aoqi@0: /** Opcode is followed by a byte indicating a type. */ aoqi@0: ATYPE(2), aoqi@0: /** Opcode is followed by a 2-byte branch offset. */ aoqi@0: BRANCH(3), aoqi@0: /** Opcode is followed by a 4-byte branch offset. */ aoqi@0: BRANCH_W(5), aoqi@0: /** Opcode is followed by a signed byte value. */ aoqi@0: BYTE(2), aoqi@0: /** Opcode is followed by a 1-byte index into the constant pool. */ aoqi@0: CPREF(2), aoqi@0: /** Opcode is followed by a 2-byte index into the constant pool. */ aoqi@0: CPREF_W(3), aoqi@0: /** Opcode is followed by a 2-byte index into the constant pool, aoqi@0: * an unsigned byte value. */ aoqi@0: CPREF_W_UBYTE(4), aoqi@0: /** Opcode is followed by a 2-byte index into the constant pool., aoqi@0: * an unsigned byte value, and a zero byte. */ aoqi@0: CPREF_W_UBYTE_ZERO(5), aoqi@0: /** Opcode is followed by variable number of operands, depending aoqi@0: * on the instruction.*/ aoqi@0: DYNAMIC(-1), aoqi@0: /** Opcode is followed by a 1-byte reference to a local variable. */ aoqi@0: LOCAL(2), aoqi@0: /** Opcode is followed by a 1-byte reference to a local variable, aoqi@0: * and a signed byte value. */ aoqi@0: LOCAL_BYTE(3), aoqi@0: /** Opcode is followed by a signed short value. */ aoqi@0: SHORT(3), aoqi@0: /** Wide opcode is not followed by any operands. */ aoqi@0: WIDE_NO_OPERANDS(2), aoqi@0: /** Wide opcode is followed by a 2-byte index into the local variables array. */ aoqi@0: WIDE_LOCAL(4), aoqi@0: /** Wide opcode is followed by a 2-byte index into the constant pool. */ aoqi@0: WIDE_CPREF_W(4), aoqi@0: /** Wide opcode is followed by a 2-byte index into the constant pool, aoqi@0: * and a signed short value. */ aoqi@0: WIDE_CPREF_W_SHORT(6), aoqi@0: /** Wide opcode is followed by a 2-byte reference to a local variable, aoqi@0: * and a signed short value. */ aoqi@0: WIDE_LOCAL_SHORT(6), aoqi@0: /** Opcode was not recognized. */ aoqi@0: UNKNOWN(1); aoqi@0: aoqi@0: Kind(int length) { aoqi@0: this.length = length; aoqi@0: } aoqi@0: aoqi@0: /** The length, in bytes, of this kind of instruction, or -1 is the aoqi@0: * length depends on the specific instruction. */ aoqi@0: public final int length; aoqi@0: }; aoqi@0: aoqi@0: /** A utility visitor to help decode the operands of an instruction. aoqi@0: * @see Instruction#accept */ aoqi@0: public interface KindVisitor { aoqi@0: /** See {@link Kind#NO_OPERANDS}, {@link Kind#WIDE_NO_OPERANDS}. */ aoqi@0: R visitNoOperands(Instruction instr, P p); aoqi@0: /** See {@link Kind#ATYPE}. */ aoqi@0: R visitArrayType(Instruction instr, TypeKind kind, P p); aoqi@0: /** See {@link Kind#BRANCH}, {@link Kind#BRANCH_W}. */ aoqi@0: R visitBranch(Instruction instr, int offset, P p); aoqi@0: /** See {@link Kind#CPREF}, {@link Kind#CPREF_W}, {@link Kind#WIDE_CPREF_W}. */ aoqi@0: R visitConstantPoolRef(Instruction instr, int index, P p); aoqi@0: /** See {@link Kind#CPREF_W_UBYTE}, {@link Kind#CPREF_W_UBYTE_ZERO}, {@link Kind#WIDE_CPREF_W_SHORT}. */ aoqi@0: R visitConstantPoolRefAndValue(Instruction instr, int index, int value, P p); aoqi@0: /** See {@link Kind#LOCAL}, {@link Kind#WIDE_LOCAL}. */ aoqi@0: R visitLocal(Instruction instr, int index, P p); aoqi@0: /** See {@link Kind#LOCAL_BYTE}. */ aoqi@0: R visitLocalAndValue(Instruction instr, int index, int value, P p); aoqi@0: /** See {@link Kind#DYNAMIC}. */ aoqi@0: R visitLookupSwitch(Instruction instr, int default_, int npairs, int[] matches, int[] offsets, P p); aoqi@0: /** See {@link Kind#DYNAMIC}. */ aoqi@0: R visitTableSwitch(Instruction instr, int default_, int low, int high, int[] offsets, P p); aoqi@0: /** See {@link Kind#BYTE}, {@link Kind#SHORT}. */ aoqi@0: R visitValue(Instruction instr, int value, P p); aoqi@0: /** Instruction is unrecognized. */ aoqi@0: R visitUnknown(Instruction instr, P p); aoqi@0: aoqi@0: } aoqi@0: aoqi@0: /** The kind of primitive array type to create. aoqi@0: * See JVMS chapter 6, newarray. */ aoqi@0: public static enum TypeKind { aoqi@0: T_BOOLEAN(4, "boolean"), aoqi@0: T_CHAR(5, "char"), aoqi@0: T_FLOAT(6, "float"), aoqi@0: T_DOUBLE(7, "double"), aoqi@0: T_BYTE(8, "byte"), aoqi@0: T_SHORT(9, "short"), aoqi@0: T_INT (10, "int"), aoqi@0: T_LONG (11, "long"); aoqi@0: TypeKind(int value, String name) { aoqi@0: this.value = value; aoqi@0: this.name = name; aoqi@0: } aoqi@0: aoqi@0: public static TypeKind get(int value) { aoqi@0: switch (value) { aoqi@0: case 4: return T_BOOLEAN; aoqi@0: case 5: return T_CHAR; aoqi@0: case 6: return T_FLOAT; aoqi@0: case 7: return T_DOUBLE; aoqi@0: case 8: return T_BYTE; aoqi@0: case 9: return T_SHORT; aoqi@0: case 10: return T_INT; aoqi@0: case 11: return T_LONG; aoqi@0: default: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public final int value; aoqi@0: public final String name; aoqi@0: } aoqi@0: aoqi@0: /** An instruction is defined by its position in a bytecode array. */ aoqi@0: public Instruction(byte[] bytes, int pc) { aoqi@0: this.bytes = bytes; aoqi@0: this.pc = pc; aoqi@0: } aoqi@0: aoqi@0: /** Get the position of the instruction within the bytecode array. */ aoqi@0: public int getPC() { aoqi@0: return pc; aoqi@0: } aoqi@0: aoqi@0: /** Get a byte value, relative to the start of this instruction. */ aoqi@0: public int getByte(int offset) { aoqi@0: return bytes[pc + offset]; aoqi@0: } aoqi@0: aoqi@0: /** Get an unsigned byte value, relative to the start of this instruction. */ aoqi@0: public int getUnsignedByte(int offset) { aoqi@0: return getByte(offset) & 0xff; aoqi@0: } aoqi@0: aoqi@0: /** Get a 2-byte value, relative to the start of this instruction. */ aoqi@0: public int getShort(int offset) { aoqi@0: return (getByte(offset) << 8) | getUnsignedByte(offset + 1); aoqi@0: } aoqi@0: aoqi@0: /** Get a unsigned 2-byte value, relative to the start of this instruction. */ aoqi@0: public int getUnsignedShort(int offset) { aoqi@0: return getShort(offset) & 0xFFFF; aoqi@0: } aoqi@0: aoqi@0: /** Get a 4-byte value, relative to the start of this instruction. */ aoqi@0: public int getInt(int offset) { aoqi@0: return (getShort(offset) << 16) | (getUnsignedShort(offset + 2)); aoqi@0: } aoqi@0: aoqi@0: /** Get the Opcode for this instruction, or null if the instruction is aoqi@0: * unrecognized. */ aoqi@0: public Opcode getOpcode() { aoqi@0: int b = getUnsignedByte(0); aoqi@0: switch (b) { aoqi@0: case Opcode.NONPRIV: aoqi@0: case Opcode.PRIV: aoqi@0: case Opcode.WIDE: aoqi@0: return Opcode.get(b, getUnsignedByte(1)); aoqi@0: } aoqi@0: return Opcode.get(b); aoqi@0: } aoqi@0: aoqi@0: /** Get the mnemonic for this instruction, or a default string if the aoqi@0: * instruction is unrecognized. */ aoqi@0: public String getMnemonic() { aoqi@0: Opcode opcode = getOpcode(); aoqi@0: if (opcode == null) aoqi@0: return "bytecode " + getUnsignedByte(0); aoqi@0: else aoqi@0: return opcode.toString().toLowerCase(Locale.US); aoqi@0: } aoqi@0: aoqi@0: /** Get the length, in bytes, of this instruction, including the opcode aoqi@0: * and all its operands. */ aoqi@0: public int length() { aoqi@0: Opcode opcode = getOpcode(); aoqi@0: if (opcode == null) aoqi@0: return 1; aoqi@0: aoqi@0: switch (opcode) { aoqi@0: case TABLESWITCH: { aoqi@0: int pad = align(pc + 1) - pc; aoqi@0: int low = getInt(pad + 4); aoqi@0: int high = getInt(pad + 8); aoqi@0: return pad + 12 + 4 * (high - low + 1); aoqi@0: } aoqi@0: case LOOKUPSWITCH: { aoqi@0: int pad = align(pc + 1) - pc; aoqi@0: int npairs = getInt(pad + 4); aoqi@0: return pad + 8 + 8 * npairs; aoqi@0: aoqi@0: } aoqi@0: default: aoqi@0: return opcode.kind.length; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** Get the {@link Kind} of this instruction. */ aoqi@0: public Kind getKind() { aoqi@0: Opcode opcode = getOpcode(); aoqi@0: return (opcode != null ? opcode.kind : Kind.UNKNOWN); aoqi@0: } aoqi@0: aoqi@0: /** Invoke a method on the visitor according to the kind of this aoqi@0: * instruction, passing in the decoded operands for the instruction. */ aoqi@0: public R accept(KindVisitor visitor, P p) { aoqi@0: switch (getKind()) { aoqi@0: case NO_OPERANDS: aoqi@0: return visitor.visitNoOperands(this, p); aoqi@0: aoqi@0: case ATYPE: aoqi@0: return visitor.visitArrayType( aoqi@0: this, TypeKind.get(getUnsignedByte(1)), p); aoqi@0: aoqi@0: case BRANCH: aoqi@0: return visitor.visitBranch(this, getShort(1), p); aoqi@0: aoqi@0: case BRANCH_W: aoqi@0: return visitor.visitBranch(this, getInt(1), p); aoqi@0: aoqi@0: case BYTE: aoqi@0: return visitor.visitValue(this, getByte(1), p); aoqi@0: aoqi@0: case CPREF: aoqi@0: return visitor.visitConstantPoolRef(this, getUnsignedByte(1), p); aoqi@0: aoqi@0: case CPREF_W: aoqi@0: return visitor.visitConstantPoolRef(this, getUnsignedShort(1), p); aoqi@0: aoqi@0: case CPREF_W_UBYTE: aoqi@0: case CPREF_W_UBYTE_ZERO: aoqi@0: return visitor.visitConstantPoolRefAndValue( aoqi@0: this, getUnsignedShort(1), getUnsignedByte(3), p); aoqi@0: aoqi@0: case DYNAMIC: { aoqi@0: switch (getOpcode()) { aoqi@0: case TABLESWITCH: { aoqi@0: int pad = align(pc + 1) - pc; aoqi@0: int default_ = getInt(pad); aoqi@0: int low = getInt(pad + 4); aoqi@0: int high = getInt(pad + 8); aoqi@0: int[] values = new int[high - low + 1]; aoqi@0: for (int i = 0; i < values.length; i++) aoqi@0: values[i] = getInt(pad + 12 + 4 * i); aoqi@0: return visitor.visitTableSwitch( aoqi@0: this, default_, low, high, values, p); aoqi@0: } aoqi@0: case LOOKUPSWITCH: { aoqi@0: int pad = align(pc + 1) - pc; aoqi@0: int default_ = getInt(pad); aoqi@0: int npairs = getInt(pad + 4); aoqi@0: int[] matches = new int[npairs]; aoqi@0: int[] offsets = new int[npairs]; aoqi@0: for (int i = 0; i < npairs; i++) { aoqi@0: matches[i] = getInt(pad + 8 + i * 8); aoqi@0: offsets[i] = getInt(pad + 12 + i * 8); aoqi@0: } aoqi@0: return visitor.visitLookupSwitch( aoqi@0: this, default_, npairs, matches, offsets, p); aoqi@0: } aoqi@0: default: aoqi@0: throw new IllegalStateException(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: case LOCAL: aoqi@0: return visitor.visitLocal(this, getUnsignedByte(1), p); aoqi@0: aoqi@0: case LOCAL_BYTE: aoqi@0: return visitor.visitLocalAndValue( aoqi@0: this, getUnsignedByte(1), getByte(2), p); aoqi@0: aoqi@0: case SHORT: aoqi@0: return visitor.visitValue(this, getShort(1), p); aoqi@0: aoqi@0: case WIDE_NO_OPERANDS: aoqi@0: return visitor.visitNoOperands(this, p); aoqi@0: aoqi@0: case WIDE_LOCAL: aoqi@0: return visitor.visitLocal(this, getUnsignedShort(2), p); aoqi@0: aoqi@0: case WIDE_CPREF_W: aoqi@0: return visitor.visitConstantPoolRef(this, getUnsignedShort(2), p); aoqi@0: aoqi@0: case WIDE_CPREF_W_SHORT: aoqi@0: return visitor.visitConstantPoolRefAndValue( aoqi@0: this, getUnsignedShort(2), getUnsignedByte(4), p); aoqi@0: aoqi@0: case WIDE_LOCAL_SHORT: aoqi@0: return visitor.visitLocalAndValue( aoqi@0: this, getUnsignedShort(2), getShort(4), p); aoqi@0: aoqi@0: case UNKNOWN: aoqi@0: return visitor.visitUnknown(this, p); aoqi@0: aoqi@0: default: aoqi@0: throw new IllegalStateException(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private static int align(int n) { aoqi@0: return (n + 3) & ~3; aoqi@0: } aoqi@0: aoqi@0: private byte[] bytes; aoqi@0: private int pc; aoqi@0: }