src/share/vm/interpreter/bytecodeStream.hpp

Wed, 22 Jan 2014 17:42:23 -0800

author
kvn
date
Wed, 22 Jan 2014 17:42:23 -0800
changeset 6503
a9becfeecd1b
parent 4037
da91efe96a93
child 6876
710a3c8b516e
child 8498
9edc175ff3e6
permissions
-rw-r--r--

Merge

duke@435 1 /*
coleenp@4037 2 * Copyright (c) 1997, 2012, 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
duke@435 152 int l = Bytecodes::length_for(code);
duke@435 153 if (l > 0 && (_bci + l) <= _end_bci) {
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;
duke@435 157 _next_bci += l;
jrose@1920 158 _raw_code = code;
duke@435 159 return code;
duke@435 160 } else {
duke@435 161 return raw_next_special(code);
duke@435 162 }
duke@435 163 }
duke@435 164 Bytecodes::Code raw_next_special(Bytecodes::Code code);
duke@435 165
jrose@1920 166 // Unsigned indices, widening, with no swapping of bytes
jrose@1920 167 int get_index() const { return (is_wide()) ? get_index_u2_raw(bcp() + 2) : get_index_u1(); }
jrose@1920 168 // Get an unsigned 2-byte index, with no swapping of bytes.
jrose@1920 169 int get_index_u2() const { assert(!is_wide(), ""); return get_index_u2_raw(bcp() + 1); }
jrose@1161 170
jrose@1161 171 private:
jrose@1920 172 int get_index_u2_raw(address p) const {
jrose@1920 173 assert_raw_index_size(2); assert_raw_stream(true);
jrose@1920 174 return Bytes::get_Java_u2(p);
jrose@1161 175 }
duke@435 176 };
duke@435 177
duke@435 178 // In BytecodeStream, non-java bytecodes will be translated into the
duke@435 179 // corresponding java bytecodes.
duke@435 180
jrose@1920 181 class BytecodeStream: public BaseBytecodeStream {
jrose@1920 182 Bytecodes::Code _code;
jrose@1920 183
duke@435 184 public:
duke@435 185 // Construction
jrose@1920 186 BytecodeStream(methodHandle method) : BaseBytecodeStream(method) { }
duke@435 187
duke@435 188 // Iteration
duke@435 189 Bytecodes::Code next() {
jrose@1920 190 Bytecodes::Code raw_code, code;
duke@435 191 // set reading position
duke@435 192 _bci = _next_bci;
duke@435 193 if (is_last_bytecode()) {
duke@435 194 // indicate end of bytecode stream
jrose@1920 195 raw_code = code = Bytecodes::_illegal;
duke@435 196 } else {
duke@435 197 // get bytecode
jrose@1920 198 address bcp = this->bcp();
never@2462 199 raw_code = Bytecodes::code_at(_method(), bcp);
jrose@1920 200 code = Bytecodes::java_code(raw_code);
duke@435 201 // set next bytecode position
duke@435 202 //
duke@435 203 // note that we cannot advance before having the
duke@435 204 // tty bytecode otherwise the stepping is wrong!
duke@435 205 // (carefull: length_for(...) must be used first!)
duke@435 206 int l = Bytecodes::length_for(code);
never@2462 207 if (l == 0) l = Bytecodes::length_at(_method(), bcp);
duke@435 208 _next_bci += l;
duke@435 209 assert(_bci < _next_bci, "length must be > 0");
duke@435 210 // set attributes
duke@435 211 _is_wide = false;
duke@435 212 // check for special (uncommon) cases
duke@435 213 if (code == Bytecodes::_wide) {
jrose@1920 214 raw_code = (Bytecodes::Code)bcp[1];
jrose@1920 215 code = raw_code; // wide BCs are always Java-normal
duke@435 216 _is_wide = true;
duke@435 217 }
duke@435 218 assert(Bytecodes::is_java_code(code), "sanity check");
duke@435 219 }
jrose@1920 220 _raw_code = raw_code;
duke@435 221 _code = code;
duke@435 222 return _code;
duke@435 223 }
duke@435 224
duke@435 225 bool is_active_breakpoint() const { return Bytecodes::is_active_breakpoint_at(bcp()); }
jrose@1920 226 Bytecodes::Code code() const { return _code; }
jrose@1920 227
jrose@1920 228 // Unsigned indices, widening
never@2462 229 int get_index() const { return is_wide() ? bytecode().get_index_u2(raw_code(), true) : get_index_u1(); }
jrose@1920 230 // Get an unsigned 2-byte index, swapping the bytes if necessary.
jrose@1920 231 int get_index_u2() const { assert_raw_stream(false);
never@2462 232 return bytecode().get_index_u2(raw_code(), false); }
jrose@1920 233 // Get an unsigned 2-byte index in native order.
jrose@1920 234 int get_index_u2_cpcache() const { assert_raw_stream(false);
never@2462 235 return bytecode().get_index_u2_cpcache(raw_code()); }
jrose@1920 236 int get_index_u4() const { assert_raw_stream(false);
never@2462 237 return bytecode().get_index_u4(raw_code()); }
never@2462 238 bool has_index_u4() const { return bytecode().has_index_u4(raw_code()); }
duke@435 239 };
stefank@2314 240
stefank@2314 241 #endif // SHARE_VM_INTERPRETER_BYTECODESTREAM_HPP

mercurial