Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/interpreter/bytecodeStream.hpp
32285 views
/*1* Copyright (c) 1997, 2016, 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*/2324#ifndef SHARE_VM_INTERPRETER_BYTECODESTREAM_HPP25#define SHARE_VM_INTERPRETER_BYTECODESTREAM_HPP2627#include "interpreter/bytecode.hpp"28#include "memory/allocation.hpp"29#include "oops/method.hpp"30#include "runtime/handles.inline.hpp"31#ifdef TARGET_ARCH_x8632# include "bytes_x86.hpp"33#endif34#ifdef TARGET_ARCH_aarch3235# include "bytes_aarch32.hpp"36#endif37#ifdef TARGET_ARCH_aarch6438# include "bytes_aarch64.hpp"39#endif40#ifdef TARGET_ARCH_sparc41# include "bytes_sparc.hpp"42#endif43#ifdef TARGET_ARCH_zero44# include "bytes_zero.hpp"45#endif46#ifdef TARGET_ARCH_arm47# include "bytes_arm.hpp"48#endif49#ifdef TARGET_ARCH_ppc50# include "bytes_ppc.hpp"51#endif5253// A BytecodeStream is used for fast iteration over the bytecodes54// of a Method*.55//56// Usage:57//58// BytecodeStream s(method);59// Bytecodes::Code c;60// while ((c = s.next()) >= 0) {61// ...62// }6364// A RawBytecodeStream is a simple version of BytecodeStream.65// It is used ONLY when we know the bytecodes haven't been rewritten66// yet, such as in the rewriter or the verifier.6768// Here is the common base class for both RawBytecodeStream and BytecodeStream:69class BaseBytecodeStream: StackObj {70protected:71// stream buffer72methodHandle _method; // read from method directly7374// reading position75int _bci; // bci if current bytecode76int _next_bci; // bci of next bytecode77int _end_bci; // bci after the current iteration interval7879// last bytecode read80Bytecodes::Code _raw_code;81bool _is_wide;82bool _is_raw; // false in 'cooked' BytecodeStream8384// Construction85BaseBytecodeStream(methodHandle method) : _method(method) {86set_interval(0, _method->code_size());87_is_raw = false;88}8990public:91// Iteration control92void set_interval(int beg_bci, int end_bci) {93// iterate over the interval [beg_bci, end_bci)94assert(0 <= beg_bci && beg_bci <= method()->code_size(), "illegal beg_bci");95assert(0 <= end_bci && end_bci <= method()->code_size(), "illegal end_bci");96// setup of iteration pointers97_bci = beg_bci;98_next_bci = beg_bci;99_end_bci = end_bci;100}101void set_start (int beg_bci) {102set_interval(beg_bci, _method->code_size());103}104105bool is_raw() const { return _is_raw; }106107// Stream attributes108methodHandle method() const { return _method; }109110int bci() const { return _bci; }111int next_bci() const { return _next_bci; }112int end_bci() const { return _end_bci; }113114Bytecodes::Code raw_code() const { return _raw_code; }115bool is_wide() const { return _is_wide; }116int instruction_size() const { return (_next_bci - _bci); }117bool is_last_bytecode() const { return _next_bci >= _end_bci; }118119address bcp() const { return method()->code_base() + _bci; }120Bytecode bytecode() const { return Bytecode(_method(), bcp()); }121122// State changes123void set_next_bci(int bci) { assert(0 <= bci && bci <= method()->code_size(), "illegal bci"); _next_bci = bci; }124125// Bytecode-specific attributes126int dest() const { return bci() + bytecode().get_offset_s2(raw_code()); }127int dest_w() const { return bci() + bytecode().get_offset_s4(raw_code()); }128129// One-byte indices.130int get_index_u1() const { assert_raw_index_size(1); return *(jubyte*)(bcp()+1); }131132protected:133void assert_raw_index_size(int size) const NOT_DEBUG_RETURN;134void assert_raw_stream(bool want_raw) const NOT_DEBUG_RETURN;135};136137class RawBytecodeStream: public BaseBytecodeStream {138public:139// Construction140RawBytecodeStream(methodHandle method) : BaseBytecodeStream(method) {141_is_raw = true;142}143144public:145// Iteration146// Use raw_next() rather than next() for faster method reference147Bytecodes::Code raw_next() {148Bytecodes::Code code;149// set reading position150_bci = _next_bci;151assert(!is_last_bytecode(), "caller should check is_last_bytecode()");152153address bcp = this->bcp();154code = Bytecodes::code_or_bp_at(bcp);155156// set next bytecode position157int len = Bytecodes::length_for(code);158if (len > 0 && (_bci <= _end_bci - len)) {159assert(code != Bytecodes::_wide && code != Bytecodes::_tableswitch160&& code != Bytecodes::_lookupswitch, "can't be special bytecode");161_is_wide = false;162_next_bci += len;163if (_next_bci <= _bci) { // Check for integer overflow164code = Bytecodes::_illegal;165}166_raw_code = code;167return code;168} else {169return raw_next_special(code);170}171}172Bytecodes::Code raw_next_special(Bytecodes::Code code);173174// Unsigned indices, widening, with no swapping of bytes175int get_index() const { return (is_wide()) ? get_index_u2_raw(bcp() + 2) : get_index_u1(); }176// Get an unsigned 2-byte index, with no swapping of bytes.177int get_index_u2() const { assert(!is_wide(), ""); return get_index_u2_raw(bcp() + 1); }178179private:180int get_index_u2_raw(address p) const {181assert_raw_index_size(2); assert_raw_stream(true);182return Bytes::get_Java_u2(p);183}184};185186// In BytecodeStream, non-java bytecodes will be translated into the187// corresponding java bytecodes.188189class BytecodeStream: public BaseBytecodeStream {190Bytecodes::Code _code;191192public:193// Construction194BytecodeStream(methodHandle method) : BaseBytecodeStream(method) { }195196// Iteration197Bytecodes::Code next() {198Bytecodes::Code raw_code, code;199// set reading position200_bci = _next_bci;201if (is_last_bytecode()) {202// indicate end of bytecode stream203raw_code = code = Bytecodes::_illegal;204} else {205// get bytecode206address bcp = this->bcp();207raw_code = Bytecodes::code_at(_method(), bcp);208code = Bytecodes::java_code(raw_code);209// set next bytecode position210//211// note that we cannot advance before having the212// tty bytecode otherwise the stepping is wrong!213// (carefull: length_for(...) must be used first!)214int len = Bytecodes::length_for(code);215if (len == 0) len = Bytecodes::length_at(_method(), bcp);216if (len <= 0 || (_bci > _end_bci - len) || (_bci - len >= _next_bci)) {217raw_code = code = Bytecodes::_illegal;218} else {219_next_bci += len;220assert(_bci < _next_bci, "length must be > 0");221// set attributes222_is_wide = false;223// check for special (uncommon) cases224if (code == Bytecodes::_wide) {225raw_code = (Bytecodes::Code)bcp[1];226code = raw_code; // wide BCs are always Java-normal227_is_wide = true;228}229assert(Bytecodes::is_java_code(code), "sanity check");230}231}232_raw_code = raw_code;233_code = code;234return _code;235}236237bool is_active_breakpoint() const { return Bytecodes::is_active_breakpoint_at(bcp()); }238Bytecodes::Code code() const { return _code; }239240// Unsigned indices, widening241int get_index() const { return is_wide() ? bytecode().get_index_u2(raw_code(), true) : get_index_u1(); }242// Get an unsigned 2-byte index, swapping the bytes if necessary.243int get_index_u2() const { assert_raw_stream(false);244return bytecode().get_index_u2(raw_code(), false); }245// Get an unsigned 2-byte index in native order.246int get_index_u2_cpcache() const { assert_raw_stream(false);247return bytecode().get_index_u2_cpcache(raw_code()); }248int get_index_u4() const { assert_raw_stream(false);249return bytecode().get_index_u4(raw_code()); }250bool has_index_u4() const { return bytecode().has_index_u4(raw_code()); }251};252253#endif // SHARE_VM_INTERPRETER_BYTECODESTREAM_HPP254255256