0N/A/*
2027N/A * Copyright (c) 1997, 2011, 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
1879N/A#ifndef SHARE_VM_INTERPRETER_BYTECODESTREAM_HPP
1879N/A#define SHARE_VM_INTERPRETER_BYTECODESTREAM_HPP
1879N/A
1879N/A#include "interpreter/bytecode.hpp"
1879N/A#include "memory/allocation.hpp"
1879N/A#include "oops/methodOop.hpp"
1879N/A#ifdef TARGET_ARCH_x86
1879N/A# include "bytes_x86.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_ARCH_sparc
1879N/A# include "bytes_sparc.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_ARCH_zero
1879N/A# include "bytes_zero.hpp"
1879N/A#endif
2073N/A#ifdef TARGET_ARCH_arm
2073N/A# include "bytes_arm.hpp"
2073N/A#endif
2073N/A#ifdef TARGET_ARCH_ppc
2073N/A# include "bytes_ppc.hpp"
2073N/A#endif
1879N/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// }
1485N/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
1485N/A// yet, such as in the rewriter or the verifier.
0N/A
1485N/A// Here is the common base class for both RawBytecodeStream and BytecodeStream:
1485N/Aclass BaseBytecodeStream: 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
1485N/A Bytecodes::Code _raw_code;
0N/A bool _is_wide;
1485N/A bool _is_raw; // false in 'cooked' BytecodeStream
1485N/A
1485N/A // Construction
1485N/A BaseBytecodeStream(methodHandle method) : _method(method) {
1485N/A set_interval(0, _method->code_size());
1485N/A _is_raw = false;
1485N/A }
0N/A
0N/A public:
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
1485N/A bool is_raw() const { return _is_raw; }
1485N/A
1485N/A // Stream attributes
1485N/A methodHandle method() const { return _method; }
1485N/A
1485N/A int bci() const { return _bci; }
1485N/A int next_bci() const { return _next_bci; }
1485N/A int end_bci() const { return _end_bci; }
1485N/A
1485N/A Bytecodes::Code raw_code() const { return _raw_code; }
1485N/A bool is_wide() const { return _is_wide; }
1485N/A int instruction_size() const { return (_next_bci - _bci); }
1485N/A bool is_last_bytecode() const { return _next_bci >= _end_bci; }
1485N/A
1485N/A address bcp() const { return method()->code_base() + _bci; }
2027N/A Bytecode bytecode() const { return Bytecode(_method(), bcp()); }
1485N/A
1485N/A // State changes
1485N/A void set_next_bci(int bci) { assert(0 <= bci && bci <= method()->code_size(), "illegal bci"); _next_bci = bci; }
1485N/A
1485N/A // Bytecode-specific attributes
2027N/A int dest() const { return bci() + bytecode().get_offset_s2(raw_code()); }
2027N/A int dest_w() const { return bci() + bytecode().get_offset_s4(raw_code()); }
1485N/A
1485N/A // One-byte indices.
1485N/A int get_index_u1() const { assert_raw_index_size(1); return *(jubyte*)(bcp()+1); }
1485N/A
1485N/A protected:
1485N/A void assert_raw_index_size(int size) const NOT_DEBUG_RETURN;
1485N/A void assert_raw_stream(bool want_raw) const NOT_DEBUG_RETURN;
1485N/A};
1485N/A
1485N/Aclass RawBytecodeStream: public BaseBytecodeStream {
1485N/A public:
1485N/A // Construction
1485N/A RawBytecodeStream(methodHandle method) : BaseBytecodeStream(method) {
1485N/A _is_raw = true;
1485N/A }
1485N/A
1485N/A public:
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
1485N/A address bcp = this->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;
1485N/A _raw_code = code;
0N/A return code;
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
1485N/A // Unsigned indices, widening, with no swapping of bytes
1485N/A int get_index() const { return (is_wide()) ? get_index_u2_raw(bcp() + 2) : get_index_u1(); }
1485N/A // Get an unsigned 2-byte index, with no swapping of bytes.
1485N/A int get_index_u2() const { assert(!is_wide(), ""); return get_index_u2_raw(bcp() + 1); }
726N/A
726N/A private:
1485N/A int get_index_u2_raw(address p) const {
1485N/A assert_raw_index_size(2); assert_raw_stream(true);
1485N/A return Bytes::get_Java_u2(p);
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
1485N/Aclass BytecodeStream: public BaseBytecodeStream {
1485N/A Bytecodes::Code _code;
1485N/A
0N/A public:
0N/A // Construction
1485N/A BytecodeStream(methodHandle method) : BaseBytecodeStream(method) { }
0N/A
0N/A // Iteration
0N/A Bytecodes::Code next() {
1485N/A Bytecodes::Code raw_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
1485N/A raw_code = code = Bytecodes::_illegal;
0N/A } else {
0N/A // get bytecode
1485N/A address bcp = this->bcp();
2027N/A raw_code = Bytecodes::code_at(_method(), bcp);
1485N/A code = Bytecodes::java_code(raw_code);
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);
2027N/A if (l == 0) l = Bytecodes::length_at(_method(), 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) {
1485N/A raw_code = (Bytecodes::Code)bcp[1];
1485N/A code = raw_code; // wide BCs are always Java-normal
0N/A _is_wide = true;
0N/A }
0N/A assert(Bytecodes::is_java_code(code), "sanity check");
0N/A }
1485N/A _raw_code = raw_code;
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()); }
1485N/A Bytecodes::Code code() const { return _code; }
1485N/A
1485N/A // Unsigned indices, widening
2027N/A int get_index() const { return is_wide() ? bytecode().get_index_u2(raw_code(), true) : get_index_u1(); }
1485N/A // Get an unsigned 2-byte index, swapping the bytes if necessary.
1485N/A int get_index_u2() const { assert_raw_stream(false);
2027N/A return bytecode().get_index_u2(raw_code(), false); }
1485N/A // Get an unsigned 2-byte index in native order.
1485N/A int get_index_u2_cpcache() const { assert_raw_stream(false);
2027N/A return bytecode().get_index_u2_cpcache(raw_code()); }
1485N/A int get_index_u4() const { assert_raw_stream(false);
2027N/A return bytecode().get_index_u4(raw_code()); }
2027N/A bool has_index_u4() const { return bytecode().has_index_u4(raw_code()); }
0N/A};
1879N/A
1879N/A#endif // SHARE_VM_INTERPRETER_BYTECODESTREAM_HPP