Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/interpreter/BytecodeStream.java
41161 views
/*1* Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot.interpreter;2526import sun.jvm.hotspot.oops.*;27import sun.jvm.hotspot.runtime.*;28import sun.jvm.hotspot.utilities.*;2930public class BytecodeStream {31private Method _method;3233// reading position34private int _bci; // bci if current bytecode35private int _next_bci; // bci of next bytecode36private int _end_bci; // bci after the current iteration interval3738// last bytecode read39private int _code;40private boolean _is_wide;4142// Construction43public BytecodeStream(Method method) {44_method = method;45setInterval(0, (int) method.getCodeSize());46}4748// Iteration control49public void setInterval(int beg_bci, int end_bci) {50if (Assert.ASSERTS_ENABLED) {51Assert.that(0 <= beg_bci && beg_bci <= _method.getCodeSize(), "illegal beg_bci");52Assert.that(0 <= end_bci && end_bci <= _method.getCodeSize(), "illegal end_bci");53}54// setup of iteration pointers55_bci = beg_bci;56_next_bci = beg_bci;57_end_bci = end_bci;58}5960public void setStart(int beg_bci) {61setInterval(beg_bci, (int) _method.getCodeSize());62}6364// Iteration65public int next() {66int code;67// set reading position68_bci = _next_bci;69if (isLastBytecode()) {70// indicate end of bytecode stream71code = Bytecodes._illegal;72} else {73// get bytecode74int rawCode = Bytecodes.codeAt(_method, _bci);75code = 0; // Make javac happy76try {77code = Bytecodes.javaCode(rawCode);78} catch (AssertionFailure e) {79e.printStackTrace();80Assert.that(false, "Failure occurred at bci " + _bci + " in method " + _method.externalNameAndSignature());81}8283// set next bytecode position84//85int l = Bytecodes.lengthFor(code);86if (l == 0) l = Bytecodes.lengthAt(_method, _bci);87_next_bci += l;88if (Assert.ASSERTS_ENABLED) {89Assert.that(_bci < _next_bci, "length must be > 0");90}91// set attributes92_is_wide = false;93// check for special (uncommon) cases94if (code == Bytecodes._wide) {95code = _method.getBytecodeOrBPAt(_bci + 1);96_is_wide = true;97}98if (Assert.ASSERTS_ENABLED) {99Assert.that(Bytecodes.isJavaCode(code), "sanity check");100}101}102_code = code;103return _code;104}105106// Stream attributes107public Method method() { return _method; }108public int bci() { return _bci; }109public int nextBCI() { return _next_bci; }110public int endBCI() { return _end_bci; }111public int code() { return _code; }112public boolean isWide() { return _is_wide; }113public boolean isActiveBreakpoint() { return Bytecodes.isActiveBreakpointAt(_method, _bci); }114public boolean isLastBytecode() { return _next_bci >= _end_bci; }115116// State changes117public void setNextBCI(int bci) {118if (Assert.ASSERTS_ENABLED) {119Assert.that(0 <= bci && bci <= _method.getCodeSize(), "illegal bci");120}121_next_bci = bci;122}123124// Bytecode-specific attributes125public int dest() { return bci() + _method.getBytecodeShortArg(bci() + 1); }126public int dest_w() { return bci() + _method.getBytecodeIntArg(bci() + 1); }127128// Unsigned indices, widening129public int getIndex() { return (isWide())130? (_method.getBytecodeShortArg(bci() + 2) & 0xFFFF)131: (_method.getBytecodeOrBPAt(bci() + 1) & 0xFF); }132public int getIndexU1() { return _method.getBytecodeOrBPAt(bci() + 1) & 0xFF; }133public int getIndexU2() { return _method.getBytecodeShortArg(bci() + 1) & 0xFFFF; }134public int getIndexU4() { return _method.getNativeIntArg(bci() + 1); }135public boolean hasIndexU4() { return code() == Bytecodes._invokedynamic; }136137public int getIndexU1Cpcache() { return _method.getBytecodeOrBPAt(bci() + 1) & 0xFF; }138public int getIndexU2Cpcache() { return _method.getNativeShortArg(bci() + 1) & 0xFFFF; }139140// Fetch at absolute BCI (for manual parsing of certain bytecodes)141public int codeAt(int bci) {142return _method.getBytecodeOrBPAt(bci);143}144}145146147