1 /* 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_INTERPRETER_BYTECODE_HPP 26 #define SHARE_VM_INTERPRETER_BYTECODE_HPP 27 28 #include "interpreter/bytecodes.hpp" 29 #include "memory/allocation.hpp" 30 #include "oops/methodOop.hpp" 31 #ifdef TARGET_ARCH_x86 32 # include "bytes_x86.hpp" 33 #endif 34 #ifdef TARGET_ARCH_sparc 35 # include "bytes_sparc.hpp" 36 #endif 37 #ifdef TARGET_ARCH_zero 38 # include "bytes_zero.hpp" 39 #endif 40 41 // Base class for different kinds of abstractions working 42 // relative to an objects 'this' pointer. 43 44 class ThisRelativeObj VALUE_OBJ_CLASS_SPEC { 45 public: 46 // Address computation 47 address addr_at (int offset) const { return (address)this + offset; } 48 int byte_at (int offset) const { return *(addr_at(offset)); } 49 address aligned_addr_at (int offset) const { return (address)round_to((intptr_t)addr_at(offset), jintSize); } 50 int aligned_offset (int offset) const { return aligned_addr_at(offset) - addr_at(0); } 51 52 // Word access: 53 int get_Java_u2_at (int offset) const { return Bytes::get_Java_u2(addr_at(offset)); } 54 int get_Java_u4_at (int offset) const { return Bytes::get_Java_u4(addr_at(offset)); } 55 int get_native_u2_at (int offset) const { return Bytes::get_native_u2(addr_at(offset)); } 56 int get_native_u4_at (int offset) const { return Bytes::get_native_u4(addr_at(offset)); } 57 }; 58 59 60 // The base class for different kinds of bytecode abstractions. 61 // Provides the primitive operations to manipulate code relative 62 // to an objects 'this' pointer. 63 // FIXME: Make this a ResourceObj, include the enclosing methodOop, and cache the opcode. 64 65 class Bytecode: public ThisRelativeObj { 66 protected: 67 u_char byte_at(int offset) const { return *addr_at(offset); } 68 bool check_must_rewrite(Bytecodes::Code bc) const; 69 70 public: 71 // Attributes 72 address bcp() const { return addr_at(0); } 73 int instruction_size() const { return Bytecodes::length_at(bcp()); } 74 75 // Warning: Use code() with caution on live bytecode streams. 4926272 76 Bytecodes::Code code() const { return Bytecodes::code_at(addr_at(0)); } 77 Bytecodes::Code java_code() const { return Bytecodes::java_code(code()); } 78 bool must_rewrite(Bytecodes::Code code) const { return Bytecodes::can_rewrite(code) && check_must_rewrite(code); } 79 80 // Creation 81 inline friend Bytecode* Bytecode_at(address bcp); 82 83 // Static functions for parsing bytecodes in place. 84 int get_index_u1(Bytecodes::Code bc) const { 85 assert_same_format_as(bc); assert_index_size(1, bc); 86 return *(jubyte*)addr_at(1); 87 } 88 int get_index_u2(Bytecodes::Code bc, bool is_wide = false) const { 89 assert_same_format_as(bc, is_wide); assert_index_size(2, bc, is_wide); 90 address p = addr_at(is_wide ? 2 : 1); 91 if (can_use_native_byte_order(bc, is_wide)) 92 return Bytes::get_native_u2(p); 93 else return Bytes::get_Java_u2(p); 94 } 95 int get_index_u1_cpcache(Bytecodes::Code bc) const { 96 assert_same_format_as(bc); assert_index_size(1, bc); 97 return *(jubyte*)addr_at(1) + constantPoolOopDesc::CPCACHE_INDEX_TAG; 98 } 99 int get_index_u2_cpcache(Bytecodes::Code bc) const { 100 assert_same_format_as(bc); assert_index_size(2, bc); assert_native_index(bc); 101 return Bytes::get_native_u2(addr_at(1)) + constantPoolOopDesc::CPCACHE_INDEX_TAG; 102 } 103 int get_index_u4(Bytecodes::Code bc) const { 104 assert_same_format_as(bc); assert_index_size(4, bc); 105 assert(can_use_native_byte_order(bc), ""); 106 return Bytes::get_native_u4(addr_at(1)); 107 } 108 bool has_index_u4(Bytecodes::Code bc) const { 109 return bc == Bytecodes::_invokedynamic; 110 } 111 112 int get_offset_s2(Bytecodes::Code bc) const { 113 assert_same_format_as(bc); assert_offset_size(2, bc); 114 return (jshort) Bytes::get_Java_u2(addr_at(1)); 115 } 116 int get_offset_s4(Bytecodes::Code bc) const { 117 assert_same_format_as(bc); assert_offset_size(4, bc); 118 return (jint) Bytes::get_Java_u4(addr_at(1)); 119 } 120 121 int get_constant_u1(int offset, Bytecodes::Code bc) const { 122 assert_same_format_as(bc); assert_constant_size(1, offset, bc); 123 return *(jbyte*)addr_at(offset); 124 } 125 int get_constant_u2(int offset, Bytecodes::Code bc, bool is_wide = false) const { 126 assert_same_format_as(bc, is_wide); assert_constant_size(2, offset, bc, is_wide); 127 return (jshort) Bytes::get_Java_u2(addr_at(offset)); 128 } 129 130 // These are used locally and also from bytecode streams. 131 void assert_same_format_as(Bytecodes::Code testbc, bool is_wide = false) const NOT_DEBUG_RETURN; 132 static void assert_index_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN; 133 static void assert_offset_size(int required_size, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN; 134 static void assert_constant_size(int required_size, int where, Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN; 135 static void assert_native_index(Bytecodes::Code bc, bool is_wide = false) NOT_DEBUG_RETURN; 136 static bool can_use_native_byte_order(Bytecodes::Code bc, bool is_wide = false) { 137 return (!Bytes::is_Java_byte_ordering_different() || Bytecodes::native_byte_order(bc /*, is_wide*/)); 138 } 139 }; 140 141 inline Bytecode* Bytecode_at(address bcp) { 142 // Warning: Use with caution on live bytecode streams. 4926272 143 return (Bytecode*)bcp; 144 } 145 146 147 // Abstractions for lookupswitch bytecode 148 149 class LookupswitchPair: ThisRelativeObj { 150 private: 151 int _match; 152 int _offset; 153 154 public: 155 int match() const { return get_Java_u4_at(0 * jintSize); } 156 int offset() const { return get_Java_u4_at(1 * jintSize); } 157 }; 158 159 160 class Bytecode_lookupswitch: public Bytecode { 161 public: 162 void verify() const PRODUCT_RETURN; 163 164 // Attributes 165 int default_offset() const { return get_Java_u4_at(aligned_offset(1 + 0*jintSize)); } 166 int number_of_pairs() const { return get_Java_u4_at(aligned_offset(1 + 1*jintSize)); } 167 LookupswitchPair* pair_at(int i) const { assert(0 <= i && i < number_of_pairs(), "pair index out of bounds"); 168 return (LookupswitchPair*)aligned_addr_at(1 + (1 + i)*2*jintSize); } 169 // Creation 170 inline friend Bytecode_lookupswitch* Bytecode_lookupswitch_at(address bcp); 171 }; 172 173 inline Bytecode_lookupswitch* Bytecode_lookupswitch_at(address bcp) { 174 Bytecode_lookupswitch* b = (Bytecode_lookupswitch*)bcp; 175 DEBUG_ONLY(b->verify()); 176 return b; 177 } 178 179 180 class Bytecode_tableswitch: public Bytecode { 181 public: 182 void verify() const PRODUCT_RETURN; 183 184 // Attributes 185 int default_offset() const { return get_Java_u4_at(aligned_offset(1 + 0*jintSize)); } 186 int low_key() const { return get_Java_u4_at(aligned_offset(1 + 1*jintSize)); } 187 int high_key() const { return get_Java_u4_at(aligned_offset(1 + 2*jintSize)); } 188 int dest_offset_at(int i) const; 189 int length() { return high_key()-low_key()+1; } 190 191 // Creation 192 inline friend Bytecode_tableswitch* Bytecode_tableswitch_at(address bcp); 193 }; 194 195 inline Bytecode_tableswitch* Bytecode_tableswitch_at(address bcp) { 196 Bytecode_tableswitch* b = (Bytecode_tableswitch*)bcp; 197 DEBUG_ONLY(b->verify()); 198 return b; 199 } 200 201 202 // Common code for decoding invokes and field references. 203 204 class Bytecode_member_ref: public ResourceObj { 205 protected: 206 methodHandle _method; // method containing the bytecode 207 int _bci; // position of the bytecode 208 209 Bytecode_member_ref(methodHandle method, int bci) : _method(method), _bci(bci) {} 210 211 public: 212 // Attributes 213 methodHandle method() const { return _method; } 214 int bci() const { return _bci; } 215 address bcp() const { return _method->bcp_from(bci()); } 216 Bytecode* bytecode() const { return Bytecode_at(bcp()); } 217 218 int index() const; // cache index (loaded from instruction) 219 int pool_index() const; // constant pool index 220 symbolOop name() const; // returns the name of the method or field 221 symbolOop signature() const; // returns the signature of the method or field 222 223 BasicType result_type(Thread* thread) const; // returns the result type of the getfield or invoke 224 225 Bytecodes::Code code() const { return Bytecodes::code_at(bcp(), _method()); } 226 Bytecodes::Code java_code() const { return Bytecodes::java_code(code()); } 227 }; 228 229 // Abstraction for invoke_{virtual, static, interface, special} 230 231 class Bytecode_invoke: public Bytecode_member_ref { 232 protected: 233 Bytecode_invoke(methodHandle method, int bci) : Bytecode_member_ref(method, bci) {} 234 235 public: 236 void verify() const; 237 238 // Attributes 239 methodHandle static_target(TRAPS); // "specified" method (from constant pool) 240 241 // Testers 242 bool is_invokeinterface() const { return java_code() == Bytecodes::_invokeinterface; } 243 bool is_invokevirtual() const { return java_code() == Bytecodes::_invokevirtual; } 244 bool is_invokestatic() const { return java_code() == Bytecodes::_invokestatic; } 245 bool is_invokespecial() const { return java_code() == Bytecodes::_invokespecial; } 246 bool is_invokedynamic() const { return java_code() == Bytecodes::_invokedynamic; } 247 248 bool has_receiver() const { return !is_invokestatic() && !is_invokedynamic(); } 249 250 bool is_valid() const { return is_invokeinterface() || 251 is_invokevirtual() || 252 is_invokestatic() || 253 is_invokespecial() || 254 is_invokedynamic(); } 255 256 // Creation 257 inline friend Bytecode_invoke* Bytecode_invoke_at(methodHandle method, int bci); 258 259 // Like Bytecode_invoke_at. Instead it returns NULL if the bci is not at an invoke. 260 inline friend Bytecode_invoke* Bytecode_invoke_at_check(methodHandle method, int bci); 261 }; 262 263 inline Bytecode_invoke* Bytecode_invoke_at(methodHandle method, int bci) { 264 Bytecode_invoke* b = new Bytecode_invoke(method, bci); 265 DEBUG_ONLY(b->verify()); 266 return b; 267 } 268 269 inline Bytecode_invoke* Bytecode_invoke_at_check(methodHandle method, int bci) { 270 Bytecode_invoke* b = new Bytecode_invoke(method, bci); 271 return b->is_valid() ? b : NULL; 272 } 273 274 275 // Abstraction for all field accesses (put/get field/static) 276 class Bytecode_field: public Bytecode_member_ref { 277 protected: 278 Bytecode_field(methodHandle method, int bci) : Bytecode_member_ref(method, bci) {} 279 280 public: 281 // Testers 282 bool is_getfield() const { return java_code() == Bytecodes::_getfield; } 283 bool is_putfield() const { return java_code() == Bytecodes::_putfield; } 284 bool is_getstatic() const { return java_code() == Bytecodes::_getstatic; } 285 bool is_putstatic() const { return java_code() == Bytecodes::_putstatic; } 286 287 bool is_getter() const { return is_getfield() || is_getstatic(); } 288 bool is_static() const { return is_getstatic() || is_putstatic(); } 289 290 bool is_valid() const { return is_getfield() || 291 is_putfield() || 292 is_getstatic() || 293 is_putstatic(); } 294 void verify() const; 295 296 // Creation 297 inline friend Bytecode_field* Bytecode_field_at(methodHandle method, int bci); 298 }; 299 300 inline Bytecode_field* Bytecode_field_at(methodHandle method, int bci) { 301 Bytecode_field* b = new Bytecode_field(method, bci); 302 DEBUG_ONLY(b->verify()); 303 return b; 304 } 305 306 307 // Abstraction for checkcast 308 309 class Bytecode_checkcast: public Bytecode { 310 public: 311 void verify() const { assert(Bytecodes::java_code(code()) == Bytecodes::_checkcast, "check checkcast"); } 312 313 // Returns index 314 long index() const { return get_index_u2(Bytecodes::_checkcast); }; 315 316 // Creation 317 inline friend Bytecode_checkcast* Bytecode_checkcast_at(address bcp); 318 }; 319 320 inline Bytecode_checkcast* Bytecode_checkcast_at(address bcp) { 321 Bytecode_checkcast* b = (Bytecode_checkcast*)bcp; 322 DEBUG_ONLY(b->verify()); 323 return b; 324 } 325 326 327 // Abstraction for instanceof 328 329 class Bytecode_instanceof: public Bytecode { 330 public: 331 void verify() const { assert(code() == Bytecodes::_instanceof, "check instanceof"); } 332 333 // Returns index 334 long index() const { return get_index_u2(Bytecodes::_instanceof); }; 335 336 // Creation 337 inline friend Bytecode_instanceof* Bytecode_instanceof_at(address bcp); 338 }; 339 340 inline Bytecode_instanceof* Bytecode_instanceof_at(address bcp) { 341 Bytecode_instanceof* b = (Bytecode_instanceof*)bcp; 342 DEBUG_ONLY(b->verify()); 343 return b; 344 } 345 346 347 class Bytecode_new: public Bytecode { 348 public: 349 void verify() const { assert(java_code() == Bytecodes::_new, "check new"); } 350 351 // Returns index 352 long index() const { return get_index_u2(Bytecodes::_new); }; 353 354 // Creation 355 inline friend Bytecode_new* Bytecode_new_at(address bcp); 356 }; 357 358 inline Bytecode_new* Bytecode_new_at(address bcp) { 359 Bytecode_new* b = (Bytecode_new*)bcp; 360 DEBUG_ONLY(b->verify()); 361 return b; 362 } 363 364 365 class Bytecode_multianewarray: public Bytecode { 366 public: 367 void verify() const { assert(java_code() == Bytecodes::_multianewarray, "check new"); } 368 369 // Returns index 370 long index() const { return get_index_u2(Bytecodes::_multianewarray); }; 371 372 // Creation 373 inline friend Bytecode_multianewarray* Bytecode_multianewarray_at(address bcp); 374 }; 375 376 inline Bytecode_multianewarray* Bytecode_multianewarray_at(address bcp) { 377 Bytecode_multianewarray* b = (Bytecode_multianewarray*)bcp; 378 DEBUG_ONLY(b->verify()); 379 return b; 380 } 381 382 383 class Bytecode_anewarray: public Bytecode { 384 public: 385 void verify() const { assert(java_code() == Bytecodes::_anewarray, "check anewarray"); } 386 387 // Returns index 388 long index() const { return get_index_u2(Bytecodes::_anewarray); }; 389 390 // Creation 391 inline friend Bytecode_anewarray* Bytecode_anewarray_at(address bcp); 392 }; 393 394 inline Bytecode_anewarray* Bytecode_anewarray_at(address bcp) { 395 Bytecode_anewarray* b = (Bytecode_anewarray*)bcp; 396 DEBUG_ONLY(b->verify()); 397 return b; 398 } 399 400 401 // Abstraction for ldc, ldc_w and ldc2_w 402 403 class Bytecode_loadconstant: public ResourceObj { 404 private: 405 int _bci; 406 methodHandle _method; 407 408 Bytecodes::Code code() const { return bytecode()->code(); } 409 410 int raw_index() const; 411 412 Bytecode_loadconstant(methodHandle method, int bci) : _method(method), _bci(bci) {} 413 414 public: 415 // Attributes 416 methodHandle method() const { return _method; } 417 int bci() const { return _bci; } 418 address bcp() const { return _method->bcp_from(bci()); } 419 Bytecode* bytecode() const { return Bytecode_at(bcp()); } 420 421 void verify() const { 422 assert(_method.not_null(), "must supply method"); 423 Bytecodes::Code stdc = Bytecodes::java_code(code()); 424 assert(stdc == Bytecodes::_ldc || 425 stdc == Bytecodes::_ldc_w || 426 stdc == Bytecodes::_ldc2_w, "load constant"); 427 } 428 429 // Only non-standard bytecodes (fast_aldc) have CP cache indexes. 430 bool has_cache_index() const { return code() >= Bytecodes::number_of_java_codes; } 431 432 int pool_index() const; // index into constant pool 433 int cache_index() const { // index into CP cache (or -1 if none) 434 return has_cache_index() ? raw_index() : -1; 435 } 436 437 BasicType result_type() const; // returns the result type of the ldc 438 439 oop resolve_constant(TRAPS) const; 440 441 // Creation 442 inline friend Bytecode_loadconstant* Bytecode_loadconstant_at(methodHandle method, int bci); 443 }; 444 445 inline Bytecode_loadconstant* Bytecode_loadconstant_at(methodHandle method, int bci) { 446 Bytecode_loadconstant* b = new Bytecode_loadconstant(method, bci); 447 DEBUG_ONLY(b->verify()); 448 return b; 449 } 450 451 #endif // SHARE_VM_INTERPRETER_BYTECODE_HPP