src/share/classes/com/sun/tools/classfile/Instruction.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 2413
fe033d997ddf
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial