duke@435: /* never@2462: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_INTERPRETER_BYTECODESTREAM_HPP stefank@2314: #define SHARE_VM_INTERPRETER_BYTECODESTREAM_HPP stefank@2314: stefank@2314: #include "interpreter/bytecode.hpp" stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "oops/methodOop.hpp" stefank@2314: #ifdef TARGET_ARCH_x86 stefank@2314: # include "bytes_x86.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_ARCH_sparc stefank@2314: # include "bytes_sparc.hpp" stefank@2314: #endif stefank@2314: #ifdef TARGET_ARCH_zero stefank@2314: # include "bytes_zero.hpp" stefank@2314: #endif bobv@2508: #ifdef TARGET_ARCH_arm bobv@2508: # include "bytes_arm.hpp" bobv@2508: #endif bobv@2508: #ifdef TARGET_ARCH_ppc bobv@2508: # include "bytes_ppc.hpp" bobv@2508: #endif stefank@2314: duke@435: // A BytecodeStream is used for fast iteration over the bytecodes duke@435: // of a methodOop. duke@435: // duke@435: // Usage: duke@435: // duke@435: // BytecodeStream s(method); duke@435: // Bytecodes::Code c; duke@435: // while ((c = s.next()) >= 0) { duke@435: // ... duke@435: // } jrose@1920: duke@435: // A RawBytecodeStream is a simple version of BytecodeStream. duke@435: // It is used ONLY when we know the bytecodes haven't been rewritten jrose@1920: // yet, such as in the rewriter or the verifier. duke@435: jrose@1920: // Here is the common base class for both RawBytecodeStream and BytecodeStream: jrose@1920: class BaseBytecodeStream: StackObj { duke@435: protected: duke@435: // stream buffer duke@435: methodHandle _method; // read from method directly duke@435: duke@435: // reading position duke@435: int _bci; // bci if current bytecode duke@435: int _next_bci; // bci of next bytecode duke@435: int _end_bci; // bci after the current iteration interval duke@435: duke@435: // last bytecode read jrose@1920: Bytecodes::Code _raw_code; duke@435: bool _is_wide; jrose@1920: bool _is_raw; // false in 'cooked' BytecodeStream jrose@1920: jrose@1920: // Construction jrose@1920: BaseBytecodeStream(methodHandle method) : _method(method) { jrose@1920: set_interval(0, _method->code_size()); jrose@1920: _is_raw = false; jrose@1920: } duke@435: duke@435: public: duke@435: // Iteration control duke@435: void set_interval(int beg_bci, int end_bci) { duke@435: // iterate over the interval [beg_bci, end_bci) duke@435: assert(0 <= beg_bci && beg_bci <= method()->code_size(), "illegal beg_bci"); duke@435: assert(0 <= end_bci && end_bci <= method()->code_size(), "illegal end_bci"); duke@435: // setup of iteration pointers duke@435: _bci = beg_bci; duke@435: _next_bci = beg_bci; duke@435: _end_bci = end_bci; duke@435: } duke@435: void set_start (int beg_bci) { duke@435: set_interval(beg_bci, _method->code_size()); duke@435: } duke@435: jrose@1920: bool is_raw() const { return _is_raw; } jrose@1920: jrose@1920: // Stream attributes jrose@1920: methodHandle method() const { return _method; } jrose@1920: jrose@1920: int bci() const { return _bci; } jrose@1920: int next_bci() const { return _next_bci; } jrose@1920: int end_bci() const { return _end_bci; } jrose@1920: jrose@1920: Bytecodes::Code raw_code() const { return _raw_code; } jrose@1920: bool is_wide() const { return _is_wide; } jrose@1920: int instruction_size() const { return (_next_bci - _bci); } jrose@1920: bool is_last_bytecode() const { return _next_bci >= _end_bci; } jrose@1920: jrose@1920: address bcp() const { return method()->code_base() + _bci; } never@2462: Bytecode bytecode() const { return Bytecode(_method(), bcp()); } jrose@1920: jrose@1920: // State changes jrose@1920: void set_next_bci(int bci) { assert(0 <= bci && bci <= method()->code_size(), "illegal bci"); _next_bci = bci; } jrose@1920: jrose@1920: // Bytecode-specific attributes never@2462: int dest() const { return bci() + bytecode().get_offset_s2(raw_code()); } never@2462: int dest_w() const { return bci() + bytecode().get_offset_s4(raw_code()); } jrose@1920: jrose@1920: // One-byte indices. jrose@1920: int get_index_u1() const { assert_raw_index_size(1); return *(jubyte*)(bcp()+1); } jrose@1920: jrose@1920: protected: jrose@1920: void assert_raw_index_size(int size) const NOT_DEBUG_RETURN; jrose@1920: void assert_raw_stream(bool want_raw) const NOT_DEBUG_RETURN; jrose@1920: }; jrose@1920: jrose@1920: class RawBytecodeStream: public BaseBytecodeStream { jrose@1920: public: jrose@1920: // Construction jrose@1920: RawBytecodeStream(methodHandle method) : BaseBytecodeStream(method) { jrose@1920: _is_raw = true; jrose@1920: } jrose@1920: jrose@1920: public: duke@435: // Iteration duke@435: // Use raw_next() rather than next() for faster method reference duke@435: Bytecodes::Code raw_next() { duke@435: Bytecodes::Code code; duke@435: // set reading position duke@435: _bci = _next_bci; duke@435: assert(!is_last_bytecode(), "caller should check is_last_bytecode()"); duke@435: jrose@1920: address bcp = this->bcp(); duke@435: code = Bytecodes::code_or_bp_at(bcp); duke@435: duke@435: // set next bytecode position duke@435: int l = Bytecodes::length_for(code); duke@435: if (l > 0 && (_bci + l) <= _end_bci) { duke@435: assert(code != Bytecodes::_wide && code != Bytecodes::_tableswitch duke@435: && code != Bytecodes::_lookupswitch, "can't be special bytecode"); duke@435: _is_wide = false; duke@435: _next_bci += l; jrose@1920: _raw_code = code; duke@435: return code; duke@435: } else { duke@435: return raw_next_special(code); duke@435: } duke@435: } duke@435: Bytecodes::Code raw_next_special(Bytecodes::Code code); duke@435: jrose@1920: // Unsigned indices, widening, with no swapping of bytes jrose@1920: int get_index() const { return (is_wide()) ? get_index_u2_raw(bcp() + 2) : get_index_u1(); } jrose@1920: // Get an unsigned 2-byte index, with no swapping of bytes. jrose@1920: int get_index_u2() const { assert(!is_wide(), ""); return get_index_u2_raw(bcp() + 1); } jrose@1161: jrose@1161: private: jrose@1920: int get_index_u2_raw(address p) const { jrose@1920: assert_raw_index_size(2); assert_raw_stream(true); jrose@1920: return Bytes::get_Java_u2(p); jrose@1161: } duke@435: }; duke@435: duke@435: // In BytecodeStream, non-java bytecodes will be translated into the duke@435: // corresponding java bytecodes. duke@435: jrose@1920: class BytecodeStream: public BaseBytecodeStream { jrose@1920: Bytecodes::Code _code; jrose@1920: duke@435: public: duke@435: // Construction jrose@1920: BytecodeStream(methodHandle method) : BaseBytecodeStream(method) { } duke@435: duke@435: // Iteration duke@435: Bytecodes::Code next() { jrose@1920: Bytecodes::Code raw_code, code; duke@435: // set reading position duke@435: _bci = _next_bci; duke@435: if (is_last_bytecode()) { duke@435: // indicate end of bytecode stream jrose@1920: raw_code = code = Bytecodes::_illegal; duke@435: } else { duke@435: // get bytecode jrose@1920: address bcp = this->bcp(); never@2462: raw_code = Bytecodes::code_at(_method(), bcp); jrose@1920: code = Bytecodes::java_code(raw_code); duke@435: // set next bytecode position duke@435: // duke@435: // note that we cannot advance before having the duke@435: // tty bytecode otherwise the stepping is wrong! duke@435: // (carefull: length_for(...) must be used first!) duke@435: int l = Bytecodes::length_for(code); never@2462: if (l == 0) l = Bytecodes::length_at(_method(), bcp); duke@435: _next_bci += l; duke@435: assert(_bci < _next_bci, "length must be > 0"); duke@435: // set attributes duke@435: _is_wide = false; duke@435: // check for special (uncommon) cases duke@435: if (code == Bytecodes::_wide) { jrose@1920: raw_code = (Bytecodes::Code)bcp[1]; jrose@1920: code = raw_code; // wide BCs are always Java-normal duke@435: _is_wide = true; duke@435: } duke@435: assert(Bytecodes::is_java_code(code), "sanity check"); duke@435: } jrose@1920: _raw_code = raw_code; duke@435: _code = code; duke@435: return _code; duke@435: } duke@435: duke@435: bool is_active_breakpoint() const { return Bytecodes::is_active_breakpoint_at(bcp()); } jrose@1920: Bytecodes::Code code() const { return _code; } jrose@1920: jrose@1920: // Unsigned indices, widening never@2462: int get_index() const { return is_wide() ? bytecode().get_index_u2(raw_code(), true) : get_index_u1(); } jrose@1920: // Get an unsigned 2-byte index, swapping the bytes if necessary. jrose@1920: int get_index_u2() const { assert_raw_stream(false); never@2462: return bytecode().get_index_u2(raw_code(), false); } jrose@1920: // Get an unsigned 2-byte index in native order. jrose@1920: int get_index_u2_cpcache() const { assert_raw_stream(false); never@2462: return bytecode().get_index_u2_cpcache(raw_code()); } jrose@1920: int get_index_u4() const { assert_raw_stream(false); never@2462: return bytecode().get_index_u4(raw_code()); } never@2462: bool has_index_u4() const { return bytecode().has_index_u4(raw_code()); } duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_INTERPRETER_BYTECODESTREAM_HPP