src/share/vm/interpreter/bytecode.hpp

Tue, 12 Mar 2013 18:22:40 -0700

author
lana
date
Tue, 12 Mar 2013 18:22:40 -0700
changeset 4706
11d5942ef9c7
parent 4037
da91efe96a93
child 6876
710a3c8b516e
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_BYTECODE_HPP
stefank@2314 26 #define SHARE_VM_INTERPRETER_BYTECODE_HPP
stefank@2314 27
stefank@2314 28 #include "interpreter/bytecodes.hpp"
stefank@2314 29 #include "memory/allocation.hpp"
coleenp@4037 30 #include "oops/method.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
never@2462 47 class ciBytecodeStream;
duke@435 48
never@2462 49 // The base class for different kinds of bytecode abstractions.
never@2462 50 // Provides the primitive operations to manipulate code relative
never@2462 51 // to the bcp.
never@2462 52
never@2462 53 class Bytecode: public StackObj {
never@2462 54 protected:
never@2462 55 const address _bcp;
never@2462 56 const Bytecodes::Code _code;
never@2462 57
duke@435 58 // Address computation
never@2462 59 address addr_at (int offset) const { return (address)_bcp + offset; }
never@2462 60 u_char byte_at(int offset) const { return *addr_at(offset); }
duke@435 61 address aligned_addr_at (int offset) const { return (address)round_to((intptr_t)addr_at(offset), jintSize); }
duke@435 62 int aligned_offset (int offset) const { return aligned_addr_at(offset) - addr_at(0); }
duke@435 63
jrose@1920 64 // Word access:
jrose@1920 65 int get_Java_u2_at (int offset) const { return Bytes::get_Java_u2(addr_at(offset)); }
jrose@1920 66 int get_Java_u4_at (int offset) const { return Bytes::get_Java_u4(addr_at(offset)); }
jrose@1920 67 int get_native_u2_at (int offset) const { return Bytes::get_native_u2(addr_at(offset)); }
jrose@1920 68 int get_native_u4_at (int offset) const { return Bytes::get_native_u4(addr_at(offset)); }
duke@435 69
duke@435 70 public:
coleenp@4037 71 Bytecode(Method* method, address bcp): _bcp(bcp), _code(Bytecodes::code_at(method, addr_at(0))) {
coleenp@4037 72 assert(method != NULL, "this form requires a valid Method*");
never@2462 73 }
never@2462 74 // Defined in ciStreams.hpp
never@2462 75 inline Bytecode(const ciBytecodeStream* stream, address bcp = NULL);
never@2462 76
duke@435 77 // Attributes
never@2462 78 address bcp() const { return _bcp; }
never@2462 79 int instruction_size() const { return Bytecodes::length_for_code_at(_code, bcp()); }
duke@435 80
never@2462 81 Bytecodes::Code code() const { return _code; }
duke@435 82 Bytecodes::Code java_code() const { return Bytecodes::java_code(code()); }
twisti@3969 83 Bytecodes::Code invoke_code() const { return (code() == Bytecodes::_invokehandle) ? code() : java_code(); }
jrose@1161 84
jrose@1920 85 // Static functions for parsing bytecodes in place.
jrose@1920 86 int get_index_u1(Bytecodes::Code bc) const {
jrose@1920 87 assert_same_format_as(bc); assert_index_size(1, bc);
jrose@1920 88 return *(jubyte*)addr_at(1);
jrose@1920 89 }
jrose@1920 90 int get_index_u2(Bytecodes::Code bc, bool is_wide = false) const {
jrose@1920 91 assert_same_format_as(bc, is_wide); assert_index_size(2, bc, is_wide);
jrose@1920 92 address p = addr_at(is_wide ? 2 : 1);
jrose@1920 93 if (can_use_native_byte_order(bc, is_wide))
never@2462 94 return Bytes::get_native_u2(p);
jrose@1920 95 else return Bytes::get_Java_u2(p);
jrose@1920 96 }
jrose@1957 97 int get_index_u1_cpcache(Bytecodes::Code bc) const {
jrose@1957 98 assert_same_format_as(bc); assert_index_size(1, bc);
coleenp@4037 99 return *(jubyte*)addr_at(1) + ConstantPool::CPCACHE_INDEX_TAG;
jrose@1957 100 }
jrose@1920 101 int get_index_u2_cpcache(Bytecodes::Code bc) const {
jrose@1920 102 assert_same_format_as(bc); assert_index_size(2, bc); assert_native_index(bc);
coleenp@4037 103 return Bytes::get_native_u2(addr_at(1)) + ConstantPool::CPCACHE_INDEX_TAG;
jrose@1920 104 }
jrose@1920 105 int get_index_u4(Bytecodes::Code bc) const {
jrose@1920 106 assert_same_format_as(bc); assert_index_size(4, bc);
jrose@1920 107 assert(can_use_native_byte_order(bc), "");
jrose@1920 108 return Bytes::get_native_u4(addr_at(1));
jrose@1920 109 }
jrose@1920 110 bool has_index_u4(Bytecodes::Code bc) const {
jrose@1920 111 return bc == Bytecodes::_invokedynamic;
jrose@1920 112 }
jrose@1920 113
jrose@1920 114 int get_offset_s2(Bytecodes::Code bc) const {
jrose@1920 115 assert_same_format_as(bc); assert_offset_size(2, bc);
jrose@1920 116 return (jshort) Bytes::get_Java_u2(addr_at(1));
jrose@1920 117 }
jrose@1920 118 int get_offset_s4(Bytecodes::Code bc) const {
jrose@1920 119 assert_same_format_as(bc); assert_offset_size(4, bc);
jrose@1920 120 return (jint) Bytes::get_Java_u4(addr_at(1));
jrose@1920 121 }
jrose@1920 122
jrose@1920 123 int get_constant_u1(int offset, Bytecodes::Code bc) const {
jrose@1920 124 assert_same_format_as(bc); assert_constant_size(1, offset, bc);
jrose@1920 125 return *(jbyte*)addr_at(offset);
jrose@1920 126 }
jrose@1920 127 int get_constant_u2(int offset, Bytecodes::Code bc, bool is_wide = false) const {
jrose@1920 128 assert_same_format_as(bc, is_wide); assert_constant_size(2, offset, bc, is_wide);
jrose@1920 129 return (jshort) Bytes::get_Java_u2(addr_at(offset));
jrose@1920 130 }
jrose@1920 131
jrose@1920 132 // These are used locally and also from bytecode streams.
jrose@1920 133 void assert_same_format_as(Bytecodes::Code testbc, bool is_wide = false) const NOT_DEBUG_RETURN;
jrose@1920 134 static void assert_index_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
jrose@1920 135 static void assert_offset_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
jrose@1920 136 static void assert_constant_size(int required_size, int where, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
jrose@1920 137 static void assert_native_index(Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN;
jrose@1920 138 static bool can_use_native_byte_order(Bytecodes::Code bc, bool is_wide = false) {
jrose@1920 139 return (!Bytes::is_Java_byte_ordering_different() || Bytecodes::native_byte_order(bc /*, is_wide*/));
jrose@1161 140 }
duke@435 141 };
duke@435 142
duke@435 143
duke@435 144 // Abstractions for lookupswitch bytecode
never@2462 145 class LookupswitchPair VALUE_OBJ_CLASS_SPEC {
never@2462 146 private:
never@2462 147 const address _bcp;
duke@435 148
never@2462 149 address addr_at (int offset) const { return _bcp + offset; }
never@2462 150 int get_Java_u4_at (int offset) const { return Bytes::get_Java_u4(addr_at(offset)); }
duke@435 151
duke@435 152 public:
never@2462 153 LookupswitchPair(address bcp): _bcp(bcp) {}
jrose@1920 154 int match() const { return get_Java_u4_at(0 * jintSize); }
jrose@1920 155 int offset() const { return get_Java_u4_at(1 * jintSize); }
duke@435 156 };
duke@435 157
duke@435 158
duke@435 159 class Bytecode_lookupswitch: public Bytecode {
duke@435 160 public:
coleenp@4037 161 Bytecode_lookupswitch(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
never@2462 162 // Defined in ciStreams.hpp
never@2462 163 inline Bytecode_lookupswitch(const ciBytecodeStream* stream);
duke@435 164 void verify() const PRODUCT_RETURN;
duke@435 165
duke@435 166 // Attributes
jrose@1920 167 int default_offset() const { return get_Java_u4_at(aligned_offset(1 + 0*jintSize)); }
jrose@1920 168 int number_of_pairs() const { return get_Java_u4_at(aligned_offset(1 + 1*jintSize)); }
never@2462 169 LookupswitchPair pair_at(int i) const {
never@2462 170 assert(0 <= i && i < number_of_pairs(), "pair index out of bounds");
never@2462 171 return LookupswitchPair(aligned_addr_at(1 + (1 + i)*2*jintSize));
never@2462 172 }
duke@435 173 };
duke@435 174
duke@435 175 class Bytecode_tableswitch: public Bytecode {
duke@435 176 public:
coleenp@4037 177 Bytecode_tableswitch(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
never@2462 178 // Defined in ciStreams.hpp
never@2462 179 inline Bytecode_tableswitch(const ciBytecodeStream* stream);
duke@435 180 void verify() const PRODUCT_RETURN;
duke@435 181
duke@435 182 // Attributes
jrose@1920 183 int default_offset() const { return get_Java_u4_at(aligned_offset(1 + 0*jintSize)); }
jrose@1920 184 int low_key() const { return get_Java_u4_at(aligned_offset(1 + 1*jintSize)); }
jrose@1920 185 int high_key() const { return get_Java_u4_at(aligned_offset(1 + 2*jintSize)); }
duke@435 186 int dest_offset_at(int i) const;
duke@435 187 int length() { return high_key()-low_key()+1; }
duke@435 188 };
duke@435 189
jrose@1957 190 // Common code for decoding invokes and field references.
duke@435 191
never@2462 192 class Bytecode_member_ref: public Bytecode {
duke@435 193 protected:
never@2462 194 const methodHandle _method; // method containing the bytecode
duke@435 195
never@2462 196 Bytecode_member_ref(methodHandle method, int bci) : Bytecode(method(), method()->bcp_from(bci)), _method(method) {}
never@2462 197
never@2462 198 methodHandle method() const { return _method; }
coleenp@4037 199 ConstantPool* constants() const { return _method->constants(); }
coleenp@4037 200 ConstantPoolCache* cpcache() const { return _method->constants()->cache(); }
twisti@3969 201 ConstantPoolCacheEntry* cpcache_entry() const;
jrose@1957 202
jrose@1957 203 public:
jrose@1957 204 int index() const; // cache index (loaded from instruction)
jrose@1957 205 int pool_index() const; // constant pool index
twisti@3969 206 Symbol* klass() const; // returns the klass of the method or field
coleenp@2497 207 Symbol* name() const; // returns the name of the method or field
coleenp@2497 208 Symbol* signature() const; // returns the signature of the method or field
jrose@1957 209
coleenp@2497 210 BasicType result_type() const; // returns the result type of the getfield or invoke
jrose@1957 211 };
jrose@1957 212
jrose@1957 213 // Abstraction for invoke_{virtual, static, interface, special}
jrose@1957 214
jrose@1957 215 class Bytecode_invoke: public Bytecode_member_ref {
jrose@1957 216 protected:
never@2462 217 // Constructor that skips verification
never@2462 218 Bytecode_invoke(methodHandle method, int bci, bool unused) : Bytecode_member_ref(method, bci) {}
duke@435 219
duke@435 220 public:
never@2462 221 Bytecode_invoke(methodHandle method, int bci) : Bytecode_member_ref(method, bci) { verify(); }
duke@435 222 void verify() const;
duke@435 223
duke@435 224 // Attributes
duke@435 225 methodHandle static_target(TRAPS); // "specified" method (from constant pool)
twisti@3969 226 Handle appendix(TRAPS); // if CPCE::has_appendix (from constant pool)
duke@435 227
duke@435 228 // Testers
twisti@3969 229 bool is_invokeinterface() const { return invoke_code() == Bytecodes::_invokeinterface; }
twisti@3969 230 bool is_invokevirtual() const { return invoke_code() == Bytecodes::_invokevirtual; }
twisti@3969 231 bool is_invokestatic() const { return invoke_code() == Bytecodes::_invokestatic; }
twisti@3969 232 bool is_invokespecial() const { return invoke_code() == Bytecodes::_invokespecial; }
twisti@3969 233 bool is_invokedynamic() const { return invoke_code() == Bytecodes::_invokedynamic; }
twisti@3969 234 bool is_invokehandle() const { return invoke_code() == Bytecodes::_invokehandle; }
jrose@1161 235
twisti@1573 236 bool has_receiver() const { return !is_invokestatic() && !is_invokedynamic(); }
duke@435 237
duke@435 238 bool is_valid() const { return is_invokeinterface() ||
duke@435 239 is_invokevirtual() ||
duke@435 240 is_invokestatic() ||
twisti@1570 241 is_invokespecial() ||
twisti@3969 242 is_invokedynamic() ||
twisti@3969 243 is_invokehandle(); }
duke@435 244
twisti@3969 245 bool has_appendix() { return cpcache_entry()->has_appendix(); }
twisti@3251 246
twisti@3969 247 private:
never@2462 248 // Helper to skip verification. Used is_valid() to check if the result is really an invoke
never@2462 249 inline friend Bytecode_invoke Bytecode_invoke_check(methodHandle method, int bci);
duke@435 250 };
duke@435 251
never@2462 252 inline Bytecode_invoke Bytecode_invoke_check(methodHandle method, int bci) {
never@2462 253 return Bytecode_invoke(method, bci, false);
duke@435 254 }
duke@435 255
duke@435 256
jrose@1957 257 // Abstraction for all field accesses (put/get field/static)
jrose@1957 258 class Bytecode_field: public Bytecode_member_ref {
never@2462 259 public:
never@2462 260 Bytecode_field(methodHandle method, int bci) : Bytecode_member_ref(method, bci) { verify(); }
jrose@1957 261
jrose@1957 262 // Testers
jrose@1957 263 bool is_getfield() const { return java_code() == Bytecodes::_getfield; }
jrose@1957 264 bool is_putfield() const { return java_code() == Bytecodes::_putfield; }
jrose@1957 265 bool is_getstatic() const { return java_code() == Bytecodes::_getstatic; }
jrose@1957 266 bool is_putstatic() const { return java_code() == Bytecodes::_putstatic; }
jrose@1957 267
jrose@1957 268 bool is_getter() const { return is_getfield() || is_getstatic(); }
jrose@1957 269 bool is_static() const { return is_getstatic() || is_putstatic(); }
jrose@1957 270
jrose@1957 271 bool is_valid() const { return is_getfield() ||
jrose@1957 272 is_putfield() ||
jrose@1957 273 is_getstatic() ||
jrose@1957 274 is_putstatic(); }
duke@435 275 void verify() const;
duke@435 276 };
duke@435 277
duke@435 278 // Abstraction for checkcast
duke@435 279 class Bytecode_checkcast: public Bytecode {
duke@435 280 public:
coleenp@4037 281 Bytecode_checkcast(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
duke@435 282 void verify() const { assert(Bytecodes::java_code(code()) == Bytecodes::_checkcast, "check checkcast"); }
duke@435 283
duke@435 284 // Returns index
jrose@1920 285 long index() const { return get_index_u2(Bytecodes::_checkcast); };
duke@435 286 };
duke@435 287
duke@435 288 // Abstraction for instanceof
duke@435 289 class Bytecode_instanceof: public Bytecode {
duke@435 290 public:
coleenp@4037 291 Bytecode_instanceof(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
duke@435 292 void verify() const { assert(code() == Bytecodes::_instanceof, "check instanceof"); }
duke@435 293
duke@435 294 // Returns index
jrose@1920 295 long index() const { return get_index_u2(Bytecodes::_instanceof); };
duke@435 296 };
duke@435 297
duke@435 298 class Bytecode_new: public Bytecode {
duke@435 299 public:
coleenp@4037 300 Bytecode_new(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
duke@435 301 void verify() const { assert(java_code() == Bytecodes::_new, "check new"); }
duke@435 302
duke@435 303 // Returns index
jrose@1920 304 long index() const { return get_index_u2(Bytecodes::_new); };
duke@435 305 };
duke@435 306
duke@435 307 class Bytecode_multianewarray: public Bytecode {
duke@435 308 public:
coleenp@4037 309 Bytecode_multianewarray(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
duke@435 310 void verify() const { assert(java_code() == Bytecodes::_multianewarray, "check new"); }
duke@435 311
duke@435 312 // Returns index
jrose@1920 313 long index() const { return get_index_u2(Bytecodes::_multianewarray); };
duke@435 314 };
duke@435 315
duke@435 316 class Bytecode_anewarray: public Bytecode {
duke@435 317 public:
coleenp@4037 318 Bytecode_anewarray(Method* method, address bcp): Bytecode(method, bcp) { verify(); }
duke@435 319 void verify() const { assert(java_code() == Bytecodes::_anewarray, "check anewarray"); }
duke@435 320
duke@435 321 // Returns index
jrose@1920 322 long index() const { return get_index_u2(Bytecodes::_anewarray); };
duke@435 323 };
duke@435 324
duke@435 325 // Abstraction for ldc, ldc_w and ldc2_w
never@2462 326 class Bytecode_loadconstant: public Bytecode {
jrose@1957 327 private:
never@2462 328 const methodHandle _method;
jrose@1957 329
jrose@1957 330 int raw_index() const;
jrose@1957 331
duke@435 332 public:
never@2462 333 Bytecode_loadconstant(methodHandle method, int bci): Bytecode(method(), method->bcp_from(bci)), _method(method) { verify(); }
jrose@1957 334
duke@435 335 void verify() const {
jrose@1957 336 assert(_method.not_null(), "must supply method");
duke@435 337 Bytecodes::Code stdc = Bytecodes::java_code(code());
duke@435 338 assert(stdc == Bytecodes::_ldc ||
duke@435 339 stdc == Bytecodes::_ldc_w ||
duke@435 340 stdc == Bytecodes::_ldc2_w, "load constant");
duke@435 341 }
duke@435 342
coleenp@4037 343 // Only non-standard bytecodes (fast_aldc) have reference cache indexes.
jrose@1957 344 bool has_cache_index() const { return code() >= Bytecodes::number_of_java_codes; }
duke@435 345
jrose@1957 346 int pool_index() const; // index into constant pool
coleenp@4037 347 int cache_index() const { // index into reference cache (or -1 if none)
jrose@1957 348 return has_cache_index() ? raw_index() : -1;
jrose@1957 349 }
jrose@1957 350
jrose@1957 351 BasicType result_type() const; // returns the result type of the ldc
jrose@1957 352
jrose@1957 353 oop resolve_constant(TRAPS) const;
duke@435 354 };
duke@435 355
stefank@2314 356 #endif // SHARE_VM_INTERPRETER_BYTECODE_HPP

mercurial