src/share/vm/interpreter/bytecodeStream.hpp

Wed, 02 Feb 2011 11:35:26 -0500

author
bobv
date
Wed, 02 Feb 2011 11:35:26 -0500
changeset 2508
b92c45f2bc75
parent 2462
8012aa3ccede
child 4037
da91efe96a93
permissions
-rw-r--r--

7016023: Enable building ARM and PPC from src/closed repository
Reviewed-by: dholmes, bdelsart

duke@435 1 /*
never@2462 2 * Copyright (c) 1997, 2011, 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"
stefank@2314 30 #include "oops/methodOop.hpp"
stefank@2314 31 #ifdef TARGET_ARCH_x86
stefank@2314 32 # include "bytes_x86.hpp"
stefank@2314 33 #endif
stefank@2314 34 #ifdef TARGET_ARCH_sparc
stefank@2314 35 # include "bytes_sparc.hpp"
stefank@2314 36 #endif
stefank@2314 37 #ifdef TARGET_ARCH_zero
stefank@2314 38 # include "bytes_zero.hpp"
stefank@2314 39 #endif
bobv@2508 40 #ifdef TARGET_ARCH_arm
bobv@2508 41 # include "bytes_arm.hpp"
bobv@2508 42 #endif
bobv@2508 43 #ifdef TARGET_ARCH_ppc
bobv@2508 44 # include "bytes_ppc.hpp"
bobv@2508 45 #endif
stefank@2314 46
duke@435 47 // A BytecodeStream is used for fast iteration over the bytecodes
duke@435 48 // of a methodOop.
duke@435 49 //
duke@435 50 // Usage:
duke@435 51 //
duke@435 52 // BytecodeStream s(method);
duke@435 53 // Bytecodes::Code c;
duke@435 54 // while ((c = s.next()) >= 0) {
duke@435 55 // ...
duke@435 56 // }
jrose@1920 57
duke@435 58 // A RawBytecodeStream is a simple version of BytecodeStream.
duke@435 59 // It is used ONLY when we know the bytecodes haven't been rewritten
jrose@1920 60 // yet, such as in the rewriter or the verifier.
duke@435 61
jrose@1920 62 // Here is the common base class for both RawBytecodeStream and BytecodeStream:
jrose@1920 63 class BaseBytecodeStream: StackObj {
duke@435 64 protected:
duke@435 65 // stream buffer
duke@435 66 methodHandle _method; // read from method directly
duke@435 67
duke@435 68 // reading position
duke@435 69 int _bci; // bci if current bytecode
duke@435 70 int _next_bci; // bci of next bytecode
duke@435 71 int _end_bci; // bci after the current iteration interval
duke@435 72
duke@435 73 // last bytecode read
jrose@1920 74 Bytecodes::Code _raw_code;
duke@435 75 bool _is_wide;
jrose@1920 76 bool _is_raw; // false in 'cooked' BytecodeStream
jrose@1920 77
jrose@1920 78 // Construction
jrose@1920 79 BaseBytecodeStream(methodHandle method) : _method(method) {
jrose@1920 80 set_interval(0, _method->code_size());
jrose@1920 81 _is_raw = false;
jrose@1920 82 }
duke@435 83
duke@435 84 public:
duke@435 85 // Iteration control
duke@435 86 void set_interval(int beg_bci, int end_bci) {
duke@435 87 // iterate over the interval [beg_bci, end_bci)
duke@435 88 assert(0 <= beg_bci && beg_bci <= method()->code_size(), "illegal beg_bci");
duke@435 89 assert(0 <= end_bci && end_bci <= method()->code_size(), "illegal end_bci");
duke@435 90 // setup of iteration pointers
duke@435 91 _bci = beg_bci;
duke@435 92 _next_bci = beg_bci;
duke@435 93 _end_bci = end_bci;
duke@435 94 }
duke@435 95 void set_start (int beg_bci) {
duke@435 96 set_interval(beg_bci, _method->code_size());
duke@435 97 }
duke@435 98
jrose@1920 99 bool is_raw() const { return _is_raw; }
jrose@1920 100
jrose@1920 101 // Stream attributes
jrose@1920 102 methodHandle method() const { return _method; }
jrose@1920 103
jrose@1920 104 int bci() const { return _bci; }
jrose@1920 105 int next_bci() const { return _next_bci; }
jrose@1920 106 int end_bci() const { return _end_bci; }
jrose@1920 107
jrose@1920 108 Bytecodes::Code raw_code() const { return _raw_code; }
jrose@1920 109 bool is_wide() const { return _is_wide; }
jrose@1920 110 int instruction_size() const { return (_next_bci - _bci); }
jrose@1920 111 bool is_last_bytecode() const { return _next_bci >= _end_bci; }
jrose@1920 112
jrose@1920 113 address bcp() const { return method()->code_base() + _bci; }
never@2462 114 Bytecode bytecode() const { return Bytecode(_method(), bcp()); }
jrose@1920 115
jrose@1920 116 // State changes
jrose@1920 117 void set_next_bci(int bci) { assert(0 <= bci && bci <= method()->code_size(), "illegal bci"); _next_bci = bci; }
jrose@1920 118
jrose@1920 119 // Bytecode-specific attributes
never@2462 120 int dest() const { return bci() + bytecode().get_offset_s2(raw_code()); }
never@2462 121 int dest_w() const { return bci() + bytecode().get_offset_s4(raw_code()); }
jrose@1920 122
jrose@1920 123 // One-byte indices.
jrose@1920 124 int get_index_u1() const { assert_raw_index_size(1); return *(jubyte*)(bcp()+1); }
jrose@1920 125
jrose@1920 126 protected:
jrose@1920 127 void assert_raw_index_size(int size) const NOT_DEBUG_RETURN;
jrose@1920 128 void assert_raw_stream(bool want_raw) const NOT_DEBUG_RETURN;
jrose@1920 129 };
jrose@1920 130
jrose@1920 131 class RawBytecodeStream: public BaseBytecodeStream {
jrose@1920 132 public:
jrose@1920 133 // Construction
jrose@1920 134 RawBytecodeStream(methodHandle method) : BaseBytecodeStream(method) {
jrose@1920 135 _is_raw = true;
jrose@1920 136 }
jrose@1920 137
jrose@1920 138 public:
duke@435 139 // Iteration
duke@435 140 // Use raw_next() rather than next() for faster method reference
duke@435 141 Bytecodes::Code raw_next() {
duke@435 142 Bytecodes::Code code;
duke@435 143 // set reading position
duke@435 144 _bci = _next_bci;
duke@435 145 assert(!is_last_bytecode(), "caller should check is_last_bytecode()");
duke@435 146
jrose@1920 147 address bcp = this->bcp();
duke@435 148 code = Bytecodes::code_or_bp_at(bcp);
duke@435 149
duke@435 150 // set next bytecode position
duke@435 151 int l = Bytecodes::length_for(code);
duke@435 152 if (l > 0 && (_bci + l) <= _end_bci) {
duke@435 153 assert(code != Bytecodes::_wide && code != Bytecodes::_tableswitch
duke@435 154 && code != Bytecodes::_lookupswitch, "can't be special bytecode");
duke@435 155 _is_wide = false;
duke@435 156 _next_bci += l;
jrose@1920 157 _raw_code = code;
duke@435 158 return code;
duke@435 159 } else {
duke@435 160 return raw_next_special(code);
duke@435 161 }
duke@435 162 }
duke@435 163 Bytecodes::Code raw_next_special(Bytecodes::Code code);
duke@435 164
jrose@1920 165 // Unsigned indices, widening, with no swapping of bytes
jrose@1920 166 int get_index() const { return (is_wide()) ? get_index_u2_raw(bcp() + 2) : get_index_u1(); }
jrose@1920 167 // Get an unsigned 2-byte index, with no swapping of bytes.
jrose@1920 168 int get_index_u2() const { assert(!is_wide(), ""); return get_index_u2_raw(bcp() + 1); }
jrose@1161 169
jrose@1161 170 private:
jrose@1920 171 int get_index_u2_raw(address p) const {
jrose@1920 172 assert_raw_index_size(2); assert_raw_stream(true);
jrose@1920 173 return Bytes::get_Java_u2(p);
jrose@1161 174 }
duke@435 175 };
duke@435 176
duke@435 177 // In BytecodeStream, non-java bytecodes will be translated into the
duke@435 178 // corresponding java bytecodes.
duke@435 179
jrose@1920 180 class BytecodeStream: public BaseBytecodeStream {
jrose@1920 181 Bytecodes::Code _code;
jrose@1920 182
duke@435 183 public:
duke@435 184 // Construction
jrose@1920 185 BytecodeStream(methodHandle method) : BaseBytecodeStream(method) { }
duke@435 186
duke@435 187 // Iteration
duke@435 188 Bytecodes::Code next() {
jrose@1920 189 Bytecodes::Code raw_code, code;
duke@435 190 // set reading position
duke@435 191 _bci = _next_bci;
duke@435 192 if (is_last_bytecode()) {
duke@435 193 // indicate end of bytecode stream
jrose@1920 194 raw_code = code = Bytecodes::_illegal;
duke@435 195 } else {
duke@435 196 // get bytecode
jrose@1920 197 address bcp = this->bcp();
never@2462 198 raw_code = Bytecodes::code_at(_method(), bcp);
jrose@1920 199 code = Bytecodes::java_code(raw_code);
duke@435 200 // set next bytecode position
duke@435 201 //
duke@435 202 // note that we cannot advance before having the
duke@435 203 // tty bytecode otherwise the stepping is wrong!
duke@435 204 // (carefull: length_for(...) must be used first!)
duke@435 205 int l = Bytecodes::length_for(code);
never@2462 206 if (l == 0) l = Bytecodes::length_at(_method(), bcp);
duke@435 207 _next_bci += l;
duke@435 208 assert(_bci < _next_bci, "length must be > 0");
duke@435 209 // set attributes
duke@435 210 _is_wide = false;
duke@435 211 // check for special (uncommon) cases
duke@435 212 if (code == Bytecodes::_wide) {
jrose@1920 213 raw_code = (Bytecodes::Code)bcp[1];
jrose@1920 214 code = raw_code; // wide BCs are always Java-normal
duke@435 215 _is_wide = true;
duke@435 216 }
duke@435 217 assert(Bytecodes::is_java_code(code), "sanity check");
duke@435 218 }
jrose@1920 219 _raw_code = raw_code;
duke@435 220 _code = code;
duke@435 221 return _code;
duke@435 222 }
duke@435 223
duke@435 224 bool is_active_breakpoint() const { return Bytecodes::is_active_breakpoint_at(bcp()); }
jrose@1920 225 Bytecodes::Code code() const { return _code; }
jrose@1920 226
jrose@1920 227 // Unsigned indices, widening
never@2462 228 int get_index() const { return is_wide() ? bytecode().get_index_u2(raw_code(), true) : get_index_u1(); }
jrose@1920 229 // Get an unsigned 2-byte index, swapping the bytes if necessary.
jrose@1920 230 int get_index_u2() const { assert_raw_stream(false);
never@2462 231 return bytecode().get_index_u2(raw_code(), false); }
jrose@1920 232 // Get an unsigned 2-byte index in native order.
jrose@1920 233 int get_index_u2_cpcache() const { assert_raw_stream(false);
never@2462 234 return bytecode().get_index_u2_cpcache(raw_code()); }
jrose@1920 235 int get_index_u4() const { assert_raw_stream(false);
never@2462 236 return bytecode().get_index_u4(raw_code()); }
never@2462 237 bool has_index_u4() const { return bytecode().has_index_u4(raw_code()); }
duke@435 238 };
stefank@2314 239
stefank@2314 240 #endif // SHARE_VM_INTERPRETER_BYTECODESTREAM_HPP

mercurial