bytecodeStream.hpp revision 1472
0N/A/*
1472N/A * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
0N/A// A BytecodeStream is used for fast iteration over the bytecodes
0N/A// of a methodOop.
0N/A//
0N/A// Usage:
0N/A//
0N/A// BytecodeStream s(method);
0N/A// Bytecodes::Code c;
0N/A// while ((c = s.next()) >= 0) {
0N/A// ...
0N/A// }
0N/A//
0N/A// A RawBytecodeStream is a simple version of BytecodeStream.
0N/A// It is used ONLY when we know the bytecodes haven't been rewritten
0N/A// yet, such as in the rewriter or the verifier. Currently only the
0N/A// verifier uses this class.
0N/A
0N/Aclass RawBytecodeStream: StackObj {
0N/A protected:
0N/A // stream buffer
0N/A methodHandle _method; // read from method directly
0N/A
0N/A // reading position
0N/A int _bci; // bci if current bytecode
0N/A int _next_bci; // bci of next bytecode
0N/A int _end_bci; // bci after the current iteration interval
0N/A
0N/A // last bytecode read
0N/A Bytecodes::Code _code;
0N/A bool _is_wide;
0N/A
0N/A public:
0N/A // Construction
0N/A RawBytecodeStream(methodHandle method) : _method(method) {
0N/A set_interval(0, _method->code_size());
0N/A }
0N/A
0N/A // Iteration control
0N/A void set_interval(int beg_bci, int end_bci) {
0N/A // iterate over the interval [beg_bci, end_bci)
0N/A assert(0 <= beg_bci && beg_bci <= method()->code_size(), "illegal beg_bci");
0N/A assert(0 <= end_bci && end_bci <= method()->code_size(), "illegal end_bci");
0N/A // setup of iteration pointers
0N/A _bci = beg_bci;
0N/A _next_bci = beg_bci;
0N/A _end_bci = end_bci;
0N/A }
0N/A void set_start (int beg_bci) {
0N/A set_interval(beg_bci, _method->code_size());
0N/A }
0N/A
0N/A // Iteration
0N/A // Use raw_next() rather than next() for faster method reference
0N/A Bytecodes::Code raw_next() {
0N/A Bytecodes::Code code;
0N/A // set reading position
0N/A _bci = _next_bci;
0N/A assert(!is_last_bytecode(), "caller should check is_last_bytecode()");
0N/A
0N/A address bcp = RawBytecodeStream::bcp();
0N/A code = Bytecodes::code_or_bp_at(bcp);
0N/A
0N/A // set next bytecode position
0N/A int l = Bytecodes::length_for(code);
0N/A if (l > 0 && (_bci + l) <= _end_bci) {
0N/A assert(code != Bytecodes::_wide && code != Bytecodes::_tableswitch
0N/A && code != Bytecodes::_lookupswitch, "can't be special bytecode");
0N/A _is_wide = false;
0N/A _next_bci += l;
0N/A _code = code;
0N/A return code;
0N/A } else if (code == Bytecodes::_wide && _bci + 1 >= _end_bci) {
0N/A return Bytecodes::_illegal;
0N/A } else {
0N/A return raw_next_special(code);
0N/A }
0N/A }
0N/A Bytecodes::Code raw_next_special(Bytecodes::Code code);
0N/A
0N/A // Stream attributes
0N/A methodHandle method() const { return _method; }
0N/A
0N/A int bci() const { return _bci; }
0N/A int next_bci() const { return _next_bci; }
0N/A int end_bci() const { return _end_bci; }
0N/A
0N/A Bytecodes::Code code() const { return _code; }
0N/A bool is_wide() const { return _is_wide; }
726N/A int instruction_size() const { return (_next_bci - _bci); }
0N/A bool is_last_bytecode() const { return _next_bci >= _end_bci; }
0N/A
0N/A address bcp() const { return method()->code_base() + _bci; }
0N/A address next_bcp() { return method()->code_base() + _next_bci; }
0N/A
0N/A // State changes
0N/A void set_next_bci(int bci) { assert(0 <= bci && bci <= method()->code_size(), "illegal bci"); _next_bci = bci; }
0N/A
0N/A // Bytecode-specific attributes
0N/A int dest() const { return bci() + (short)Bytes::get_Java_u2(bcp() + 1); }
0N/A int dest_w() const { return bci() + (int )Bytes::get_Java_u4(bcp() + 1); }
0N/A
0N/A // Unsigned indices, widening
726N/A int get_index() const { assert_index_size(is_wide() ? 2 : 1);
726N/A return (is_wide()) ? Bytes::get_Java_u2(bcp() + 2) : bcp()[1]; }
726N/A int get_index_big() const { assert_index_size(2);
726N/A return (int)Bytes::get_Java_u2(bcp() + 1); }
726N/A int get_index_int() const { return has_giant_index() ? get_index_giant() : get_index_big(); }
726N/A int get_index_giant() const { assert_index_size(4); return Bytes::get_native_u4(bcp() + 1); }
726N/A int has_giant_index() const { return (code() == Bytecodes::_invokedynamic); }
726N/A
726N/A private:
726N/A void assert_index_size(int required_size) const {
726N/A#ifdef ASSERT
726N/A int isize = instruction_size() - (int)_is_wide - 1;
726N/A if (isize == 2 && code() == Bytecodes::_iinc)
726N/A isize = 1;
726N/A else if (isize <= 2)
726N/A ; // no change
726N/A else if (has_giant_index())
726N/A isize = 4;
726N/A else
726N/A isize = 2;
726N/A assert(isize = required_size, "wrong index size");
726N/A#endif
726N/A }
0N/A};
0N/A
0N/A// In BytecodeStream, non-java bytecodes will be translated into the
0N/A// corresponding java bytecodes.
0N/A
0N/Aclass BytecodeStream: public RawBytecodeStream {
0N/A public:
0N/A // Construction
0N/A BytecodeStream(methodHandle method) : RawBytecodeStream(method) { }
0N/A
0N/A // Iteration
0N/A Bytecodes::Code next() {
0N/A Bytecodes::Code code;
0N/A // set reading position
0N/A _bci = _next_bci;
0N/A if (is_last_bytecode()) {
0N/A // indicate end of bytecode stream
0N/A code = Bytecodes::_illegal;
0N/A } else {
0N/A // get bytecode
0N/A address bcp = BytecodeStream::bcp();
0N/A code = Bytecodes::java_code_at(bcp);
0N/A // set next bytecode position
0N/A //
0N/A // note that we cannot advance before having the
0N/A // tty bytecode otherwise the stepping is wrong!
0N/A // (carefull: length_for(...) must be used first!)
0N/A int l = Bytecodes::length_for(code);
0N/A if (l == 0) l = Bytecodes::length_at(bcp);
0N/A _next_bci += l;
0N/A assert(_bci < _next_bci, "length must be > 0");
0N/A // set attributes
0N/A _is_wide = false;
0N/A // check for special (uncommon) cases
0N/A if (code == Bytecodes::_wide) {
0N/A code = (Bytecodes::Code)bcp[1];
0N/A _is_wide = true;
0N/A }
0N/A assert(Bytecodes::is_java_code(code), "sanity check");
0N/A }
0N/A _code = code;
0N/A return _code;
0N/A }
0N/A
0N/A bool is_active_breakpoint() const { return Bytecodes::is_active_breakpoint_at(bcp()); }
0N/A};