|
| 1 | +/* |
| 2 | + * Copyright (c) 2009 Sun Microsystems, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product |
| 5 | + * that is described in this document. In particular, and without limitation, these intellectual property |
| 6 | + * rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or |
| 7 | + * more additional patents or pending patent applications in the U.S. and in other countries. |
| 8 | + * |
| 9 | + * U.S. Government Rights - Commercial software. Government users are subject to the Sun |
| 10 | + * Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its |
| 11 | + * supplements. |
| 12 | + * |
| 13 | + * Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or |
| 14 | + * registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks |
| 15 | + * are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the |
| 16 | + * U.S. and other countries. |
| 17 | + * |
| 18 | + * UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open |
| 19 | + * Company, Ltd. |
| 20 | + */ |
| 21 | +package com.sun.cri.bytecode; |
| 22 | + |
| 23 | +/** |
| 24 | + * A utility class that makes iterating over bytecodes and reading operands |
| 25 | + * simpler and less error prone. For example, it handles the {@link Bytecodes#WIDE} instruction |
| 26 | + * and wide variants of instructions internally. |
| 27 | + * |
| 28 | + * @author Ben L. Titzer |
| 29 | + */ |
| 30 | +public class BytecodeStream { |
| 31 | + |
| 32 | + final byte[] code; |
| 33 | + int opcode; |
| 34 | + int curBCI; |
| 35 | + int nextBCI; |
| 36 | + |
| 37 | + /** |
| 38 | + * Creates a new {@code BytecodeStream} for the specified bytecode. |
| 39 | + * @param code the array of bytes that contains the bytecode |
| 40 | + */ |
| 41 | + public BytecodeStream(byte[] code) { |
| 42 | + this.code = code; |
| 43 | + setBCI(0); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Advances to the next bytecode. |
| 48 | + */ |
| 49 | + public void next() { |
| 50 | + setBCI(nextBCI); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Gets the next bytecode index (no side-effects). |
| 55 | + * @return the next bytecode index |
| 56 | + */ |
| 57 | + public int nextBCI() { |
| 58 | + return nextBCI; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Gets the current bytecode index. |
| 63 | + * @return the current bytecode index |
| 64 | + */ |
| 65 | + public int currentBCI() { |
| 66 | + return curBCI; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Gets the bytecode index of the end of the code. |
| 71 | + * @return the index of the end of the code |
| 72 | + */ |
| 73 | + public int endBCI() { |
| 74 | + return code.length; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Gets the current opcode. This method will never return the |
| 79 | + * {@link Bytecodes#WIDE WIDE} opcode, but will instead |
| 80 | + * return the opcode that is modified by the {@code WIDE} opcode. |
| 81 | + * @return the current opcode; {@link Bytecodes#END} if at or beyond the end of the code |
| 82 | + */ |
| 83 | + public int currentBC() { |
| 84 | + if (opcode == Bytecodes.WIDE) { |
| 85 | + return Bytes.beU1(code, curBCI + 1); |
| 86 | + } else { |
| 87 | + return opcode; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Reads the index of a local variable for one of the load or store instructions. |
| 93 | + * The WIDE modifier is handled internally. |
| 94 | + * @return the index of the local variable |
| 95 | + */ |
| 96 | + public int readLocalIndex() { |
| 97 | + // read local variable index for load/store |
| 98 | + if (opcode == Bytecodes.WIDE) { |
| 99 | + return Bytes.beU2(code, curBCI + 2); |
| 100 | + } |
| 101 | + return Bytes.beU1(code, curBCI + 1); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Read the delta for an {@link Bytecodes#IINC} bytecode. |
| 106 | + * @return the delta for the {@code IINC} |
| 107 | + */ |
| 108 | + public int readIncrement() { |
| 109 | + // read the delta for the iinc bytecode |
| 110 | + if (opcode == Bytecodes.WIDE) { |
| 111 | + return Bytes.beS2(code, curBCI + 4); |
| 112 | + } |
| 113 | + return Bytes.beS1(code, curBCI + 2); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Read the destination of a {@link Bytecodes#GOTO} or {@code IF} instructions. |
| 118 | + * @return the destination bytecode index |
| 119 | + */ |
| 120 | + public int readBranchDest() { |
| 121 | + // reads the destination for a branch bytecode |
| 122 | + return curBCI + Bytes.beS2(code, curBCI + 1); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Read the destination of a {@link Bytecodes#GOTO_W} or {@link Bytecodes#JSR_W} instructions. |
| 127 | + * @return the destination bytecode index |
| 128 | + */ |
| 129 | + public int readFarBranchDest() { |
| 130 | + // reads the destination for a wide branch bytecode |
| 131 | + return curBCI + Bytes.beS4(code, curBCI + 2); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Read a signed 4-byte integer from the bytecode stream at the specified bytecode index. |
| 136 | + * @param bci the bytecode index |
| 137 | + * @return the integer value |
| 138 | + */ |
| 139 | + public int readInt(int bci) { |
| 140 | + // reads a 4-byte signed value |
| 141 | + return Bytes.beS4(code, bci); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Reads an unsigned, 1-byte value from the bytecode stream at the specified bytecode index. |
| 146 | + * @param bci the bytecode index |
| 147 | + * @return the byte |
| 148 | + */ |
| 149 | + public int readUByte(int bci) { |
| 150 | + return Bytes.beU1(code, bci); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Reads a constant pool index for the current instruction. |
| 155 | + * @return the constant pool index |
| 156 | + */ |
| 157 | + public char readCPI() { |
| 158 | + if (opcode == Bytecodes.LDC) { |
| 159 | + return (char) Bytes.beU1(code, curBCI + 1); |
| 160 | + } |
| 161 | + return (char) Bytes.beU2(code, curBCI + 1); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Reads a signed, 1-byte value for the current instruction (e.g. BIPUSH). |
| 166 | + * @return the byte |
| 167 | + */ |
| 168 | + public byte readByte() { |
| 169 | + return code[curBCI + 1]; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Reads a signed, 2-byte short for the current instruction (e.g. SIPUSH). |
| 174 | + * @return the short value |
| 175 | + */ |
| 176 | + public short readShort() { |
| 177 | + return (short) Bytes.beS2(code, curBCI + 1); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Sets the bytecode index to the specified value. |
| 182 | + * If {@code bci} is beyond the end of the array, {@link #currentBC} will return |
| 183 | + * {@link Bytecodes#END} and other methods may throw {@link ArrayIndexOutOfBoundsException}. |
| 184 | + * @param bci the new bytecode index |
| 185 | + */ |
| 186 | + public void setBCI(int bci) { |
| 187 | + curBCI = bci; |
| 188 | + if (curBCI < code.length) { |
| 189 | + opcode = Bytes.beU1(code, bci); |
| 190 | + nextBCI = bci + Bytecodes.lengthOf(code, bci); |
| 191 | + } else { |
| 192 | + opcode = Bytecodes.END; |
| 193 | + nextBCI = curBCI; |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments