src/share/vm/interpreter/bytecodeStream.hpp

Wed, 31 Jan 2018 19:24:57 -0500

author
dbuck
date
Wed, 31 Jan 2018 19:24:57 -0500
changeset 9289
427b2fb1944f
parent 8498
9edc175ff3e6
child 8604
04d83ba48607
permissions
-rw-r--r--

8189170: Add option to disable stack overflow checking in primordial thread for use with JNI_CreateJavaJVM
Reviewed-by: dcubed

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

mercurial