Wed, 27 Apr 2016 01:25:04 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #ifndef SHARE_VM_INTERPRETER_BYTECODESTREAM_HPP |
aoqi@0 | 26 | #define SHARE_VM_INTERPRETER_BYTECODESTREAM_HPP |
aoqi@0 | 27 | |
aoqi@0 | 28 | #include "interpreter/bytecode.hpp" |
aoqi@0 | 29 | #include "memory/allocation.hpp" |
aoqi@0 | 30 | #include "oops/method.hpp" |
aoqi@0 | 31 | #include "runtime/handles.inline.hpp" |
aoqi@0 | 32 | #ifdef TARGET_ARCH_x86 |
aoqi@0 | 33 | # include "bytes_x86.hpp" |
aoqi@0 | 34 | #endif |
aoqi@0 | 35 | #ifdef TARGET_ARCH_sparc |
aoqi@0 | 36 | # include "bytes_sparc.hpp" |
aoqi@0 | 37 | #endif |
aoqi@0 | 38 | #ifdef TARGET_ARCH_zero |
aoqi@0 | 39 | # include "bytes_zero.hpp" |
aoqi@0 | 40 | #endif |
aoqi@0 | 41 | #ifdef TARGET_ARCH_arm |
aoqi@0 | 42 | # include "bytes_arm.hpp" |
aoqi@0 | 43 | #endif |
aoqi@0 | 44 | #ifdef TARGET_ARCH_ppc |
aoqi@0 | 45 | # include "bytes_ppc.hpp" |
aoqi@0 | 46 | #endif |
aoqi@0 | 47 | |
aoqi@0 | 48 | // A BytecodeStream is used for fast iteration over the bytecodes |
aoqi@0 | 49 | // of a Method*. |
aoqi@0 | 50 | // |
aoqi@0 | 51 | // Usage: |
aoqi@0 | 52 | // |
aoqi@0 | 53 | // BytecodeStream s(method); |
aoqi@0 | 54 | // Bytecodes::Code c; |
aoqi@0 | 55 | // while ((c = s.next()) >= 0) { |
aoqi@0 | 56 | // ... |
aoqi@0 | 57 | // } |
aoqi@0 | 58 | |
aoqi@0 | 59 | // A RawBytecodeStream is a simple version of BytecodeStream. |
aoqi@0 | 60 | // It is used ONLY when we know the bytecodes haven't been rewritten |
aoqi@0 | 61 | // yet, such as in the rewriter or the verifier. |
aoqi@0 | 62 | |
aoqi@0 | 63 | // Here is the common base class for both RawBytecodeStream and BytecodeStream: |
aoqi@0 | 64 | class BaseBytecodeStream: StackObj { |
aoqi@0 | 65 | protected: |
aoqi@0 | 66 | // stream buffer |
aoqi@0 | 67 | methodHandle _method; // read from method directly |
aoqi@0 | 68 | |
aoqi@0 | 69 | // reading position |
aoqi@0 | 70 | int _bci; // bci if current bytecode |
aoqi@0 | 71 | int _next_bci; // bci of next bytecode |
aoqi@0 | 72 | int _end_bci; // bci after the current iteration interval |
aoqi@0 | 73 | |
aoqi@0 | 74 | // last bytecode read |
aoqi@0 | 75 | Bytecodes::Code _raw_code; |
aoqi@0 | 76 | bool _is_wide; |
aoqi@0 | 77 | bool _is_raw; // false in 'cooked' BytecodeStream |
aoqi@0 | 78 | |
aoqi@0 | 79 | // Construction |
aoqi@0 | 80 | BaseBytecodeStream(methodHandle method) : _method(method) { |
aoqi@0 | 81 | set_interval(0, _method->code_size()); |
aoqi@0 | 82 | _is_raw = false; |
aoqi@0 | 83 | } |
aoqi@0 | 84 | |
aoqi@0 | 85 | public: |
aoqi@0 | 86 | // Iteration control |
aoqi@0 | 87 | void set_interval(int beg_bci, int end_bci) { |
aoqi@0 | 88 | // iterate over the interval [beg_bci, end_bci) |
aoqi@0 | 89 | assert(0 <= beg_bci && beg_bci <= method()->code_size(), "illegal beg_bci"); |
aoqi@0 | 90 | assert(0 <= end_bci && end_bci <= method()->code_size(), "illegal end_bci"); |
aoqi@0 | 91 | // setup of iteration pointers |
aoqi@0 | 92 | _bci = beg_bci; |
aoqi@0 | 93 | _next_bci = beg_bci; |
aoqi@0 | 94 | _end_bci = end_bci; |
aoqi@0 | 95 | } |
aoqi@0 | 96 | void set_start (int beg_bci) { |
aoqi@0 | 97 | set_interval(beg_bci, _method->code_size()); |
aoqi@0 | 98 | } |
aoqi@0 | 99 | |
aoqi@0 | 100 | bool is_raw() const { return _is_raw; } |
aoqi@0 | 101 | |
aoqi@0 | 102 | // Stream attributes |
aoqi@0 | 103 | methodHandle method() const { return _method; } |
aoqi@0 | 104 | |
aoqi@0 | 105 | int bci() const { return _bci; } |
aoqi@0 | 106 | int next_bci() const { return _next_bci; } |
aoqi@0 | 107 | int end_bci() const { return _end_bci; } |
aoqi@0 | 108 | |
aoqi@0 | 109 | Bytecodes::Code raw_code() const { return _raw_code; } |
aoqi@0 | 110 | bool is_wide() const { return _is_wide; } |
aoqi@0 | 111 | int instruction_size() const { return (_next_bci - _bci); } |
aoqi@0 | 112 | bool is_last_bytecode() const { return _next_bci >= _end_bci; } |
aoqi@0 | 113 | |
aoqi@0 | 114 | address bcp() const { return method()->code_base() + _bci; } |
aoqi@0 | 115 | Bytecode bytecode() const { return Bytecode(_method(), bcp()); } |
aoqi@0 | 116 | |
aoqi@0 | 117 | // State changes |
aoqi@0 | 118 | void set_next_bci(int bci) { assert(0 <= bci && bci <= method()->code_size(), "illegal bci"); _next_bci = bci; } |
aoqi@0 | 119 | |
aoqi@0 | 120 | // Bytecode-specific attributes |
aoqi@0 | 121 | int dest() const { return bci() + bytecode().get_offset_s2(raw_code()); } |
aoqi@0 | 122 | int dest_w() const { return bci() + bytecode().get_offset_s4(raw_code()); } |
aoqi@0 | 123 | |
aoqi@0 | 124 | // One-byte indices. |
aoqi@0 | 125 | int get_index_u1() const { assert_raw_index_size(1); return *(jubyte*)(bcp()+1); } |
aoqi@0 | 126 | |
aoqi@0 | 127 | protected: |
aoqi@0 | 128 | void assert_raw_index_size(int size) const NOT_DEBUG_RETURN; |
aoqi@0 | 129 | void assert_raw_stream(bool want_raw) const NOT_DEBUG_RETURN; |
aoqi@0 | 130 | }; |
aoqi@0 | 131 | |
aoqi@0 | 132 | class RawBytecodeStream: public BaseBytecodeStream { |
aoqi@0 | 133 | public: |
aoqi@0 | 134 | // Construction |
aoqi@0 | 135 | RawBytecodeStream(methodHandle method) : BaseBytecodeStream(method) { |
aoqi@0 | 136 | _is_raw = true; |
aoqi@0 | 137 | } |
aoqi@0 | 138 | |
aoqi@0 | 139 | public: |
aoqi@0 | 140 | // Iteration |
aoqi@0 | 141 | // Use raw_next() rather than next() for faster method reference |
aoqi@0 | 142 | Bytecodes::Code raw_next() { |
aoqi@0 | 143 | Bytecodes::Code code; |
aoqi@0 | 144 | // set reading position |
aoqi@0 | 145 | _bci = _next_bci; |
aoqi@0 | 146 | assert(!is_last_bytecode(), "caller should check is_last_bytecode()"); |
aoqi@0 | 147 | |
aoqi@0 | 148 | address bcp = this->bcp(); |
aoqi@0 | 149 | code = Bytecodes::code_or_bp_at(bcp); |
aoqi@0 | 150 | |
aoqi@0 | 151 | // set next bytecode position |
aoqi@0 | 152 | int l = Bytecodes::length_for(code); |
aoqi@0 | 153 | if (l > 0 && (_bci + l) <= _end_bci) { |
aoqi@0 | 154 | assert(code != Bytecodes::_wide && code != Bytecodes::_tableswitch |
aoqi@0 | 155 | && code != Bytecodes::_lookupswitch, "can't be special bytecode"); |
aoqi@0 | 156 | _is_wide = false; |
aoqi@0 | 157 | _next_bci += l; |
aoqi@0 | 158 | _raw_code = code; |
aoqi@0 | 159 | return code; |
aoqi@0 | 160 | } else { |
aoqi@0 | 161 | return raw_next_special(code); |
aoqi@0 | 162 | } |
aoqi@0 | 163 | } |
aoqi@0 | 164 | Bytecodes::Code raw_next_special(Bytecodes::Code code); |
aoqi@0 | 165 | |
aoqi@0 | 166 | // Unsigned indices, widening, with no swapping of bytes |
aoqi@0 | 167 | int get_index() const { return (is_wide()) ? get_index_u2_raw(bcp() + 2) : get_index_u1(); } |
aoqi@0 | 168 | // Get an unsigned 2-byte index, with no swapping of bytes. |
aoqi@0 | 169 | int get_index_u2() const { assert(!is_wide(), ""); return get_index_u2_raw(bcp() + 1); } |
aoqi@0 | 170 | |
aoqi@0 | 171 | private: |
aoqi@0 | 172 | int get_index_u2_raw(address p) const { |
aoqi@0 | 173 | assert_raw_index_size(2); assert_raw_stream(true); |
aoqi@0 | 174 | return Bytes::get_Java_u2(p); |
aoqi@0 | 175 | } |
aoqi@0 | 176 | }; |
aoqi@0 | 177 | |
aoqi@0 | 178 | // In BytecodeStream, non-java bytecodes will be translated into the |
aoqi@0 | 179 | // corresponding java bytecodes. |
aoqi@0 | 180 | |
aoqi@0 | 181 | class BytecodeStream: public BaseBytecodeStream { |
aoqi@0 | 182 | Bytecodes::Code _code; |
aoqi@0 | 183 | |
aoqi@0 | 184 | public: |
aoqi@0 | 185 | // Construction |
aoqi@0 | 186 | BytecodeStream(methodHandle method) : BaseBytecodeStream(method) { } |
aoqi@0 | 187 | |
aoqi@0 | 188 | // Iteration |
aoqi@0 | 189 | Bytecodes::Code next() { |
aoqi@0 | 190 | Bytecodes::Code raw_code, code; |
aoqi@0 | 191 | // set reading position |
aoqi@0 | 192 | _bci = _next_bci; |
aoqi@0 | 193 | if (is_last_bytecode()) { |
aoqi@0 | 194 | // indicate end of bytecode stream |
aoqi@0 | 195 | raw_code = code = Bytecodes::_illegal; |
aoqi@0 | 196 | } else { |
aoqi@0 | 197 | // get bytecode |
aoqi@0 | 198 | address bcp = this->bcp(); |
aoqi@0 | 199 | raw_code = Bytecodes::code_at(_method(), bcp); |
aoqi@0 | 200 | code = Bytecodes::java_code(raw_code); |
aoqi@0 | 201 | // set next bytecode position |
aoqi@0 | 202 | // |
aoqi@0 | 203 | // note that we cannot advance before having the |
aoqi@0 | 204 | // tty bytecode otherwise the stepping is wrong! |
aoqi@0 | 205 | // (carefull: length_for(...) must be used first!) |
aoqi@0 | 206 | int l = Bytecodes::length_for(code); |
aoqi@0 | 207 | if (l == 0) l = Bytecodes::length_at(_method(), bcp); |
aoqi@0 | 208 | _next_bci += l; |
aoqi@0 | 209 | assert(_bci < _next_bci, "length must be > 0"); |
aoqi@0 | 210 | // set attributes |
aoqi@0 | 211 | _is_wide = false; |
aoqi@0 | 212 | // check for special (uncommon) cases |
aoqi@0 | 213 | if (code == Bytecodes::_wide) { |
aoqi@0 | 214 | raw_code = (Bytecodes::Code)bcp[1]; |
aoqi@0 | 215 | code = raw_code; // wide BCs are always Java-normal |
aoqi@0 | 216 | _is_wide = true; |
aoqi@0 | 217 | } |
aoqi@0 | 218 | assert(Bytecodes::is_java_code(code), "sanity check"); |
aoqi@0 | 219 | } |
aoqi@0 | 220 | _raw_code = raw_code; |
aoqi@0 | 221 | _code = code; |
aoqi@0 | 222 | return _code; |
aoqi@0 | 223 | } |
aoqi@0 | 224 | |
aoqi@0 | 225 | bool is_active_breakpoint() const { return Bytecodes::is_active_breakpoint_at(bcp()); } |
aoqi@0 | 226 | Bytecodes::Code code() const { return _code; } |
aoqi@0 | 227 | |
aoqi@0 | 228 | // Unsigned indices, widening |
aoqi@0 | 229 | int get_index() const { return is_wide() ? bytecode().get_index_u2(raw_code(), true) : get_index_u1(); } |
aoqi@0 | 230 | // Get an unsigned 2-byte index, swapping the bytes if necessary. |
aoqi@0 | 231 | int get_index_u2() const { assert_raw_stream(false); |
aoqi@0 | 232 | return bytecode().get_index_u2(raw_code(), false); } |
aoqi@0 | 233 | // Get an unsigned 2-byte index in native order. |
aoqi@0 | 234 | int get_index_u2_cpcache() const { assert_raw_stream(false); |
aoqi@0 | 235 | return bytecode().get_index_u2_cpcache(raw_code()); } |
aoqi@0 | 236 | int get_index_u4() const { assert_raw_stream(false); |
aoqi@0 | 237 | return bytecode().get_index_u4(raw_code()); } |
aoqi@0 | 238 | bool has_index_u4() const { return bytecode().has_index_u4(raw_code()); } |
aoqi@0 | 239 | }; |
aoqi@0 | 240 | |
aoqi@0 | 241 | #endif // SHARE_VM_INTERPRETER_BYTECODESTREAM_HPP |