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