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

Wed, 10 Jun 2015 14:22:04 -0700

author
mfang
date
Wed, 10 Jun 2015 14:22:04 -0700
changeset 2815
d4051d4f5daf
parent 2413
fe033d997ddf
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8083601: jdk8u60 l10n resource file translation update 2
Reviewed-by: ksrini, yhuang

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

mercurial