1 /* 2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2015-2018, Azul Systems, Inc. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "precompiled.hpp" 27 #include "asm/macroAssembler.hpp" 28 #include "asm/macroAssembler.inline.hpp" 29 #include "compiler/disassembler.hpp" 30 #include "interpreter/bytecodeHistogram.hpp" 31 #include "interpreter/bytecodeInterpreter.hpp" 32 #include "interpreter/interpreter.hpp" 33 #include "interpreter/interpreterRuntime.hpp" 34 #include "interpreter/interp_masm.hpp" 35 #include "interpreter/templateTable.hpp" 36 #include "memory/allocation.inline.hpp" 37 #include "memory/metaspaceShared.hpp" 38 #include "memory/resourceArea.hpp" 39 #include "oops/arrayOop.hpp" 40 #include "oops/methodData.hpp" 41 #include "oops/method.hpp" 42 #include "oops/oop.inline.hpp" 43 #include "prims/forte.hpp" 44 #include "prims/jvmtiExport.hpp" 45 #include "prims/methodHandles.hpp" 46 #include "runtime/handles.inline.hpp" 47 #include "runtime/sharedRuntime.hpp" 48 #include "runtime/stubRoutines.hpp" 49 #include "runtime/timer.hpp" 50 51 # define __ _masm-> 52 53 //------------------------------------------------------------------------------------------------------------------------ 54 // Implementation of platform independent aspects of Interpreter 55 56 void AbstractInterpreter::initialize() { 57 if (_code != NULL) return; 58 59 // make sure 'imported' classes are initialized 60 if (CountBytecodes || TraceBytecodes || StopInterpreterAt) BytecodeCounter::reset(); 61 if (PrintBytecodeHistogram) BytecodeHistogram::reset(); 62 if (PrintBytecodePairHistogram) BytecodePairHistogram::reset(); 63 64 InvocationCounter::reinitialize(DelayCompilationDuringStartup); 65 66 } 67 68 void AbstractInterpreter::print() { 69 tty->cr(); 70 tty->print_cr("----------------------------------------------------------------------"); 71 tty->print_cr("Interpreter"); 72 tty->cr(); 73 tty->print_cr("code size = %6dK bytes", (int)_code->used_space()/1024); 74 tty->print_cr("total space = %6dK bytes", (int)_code->total_space()/1024); 75 tty->print_cr("wasted space = %6dK bytes", (int)_code->available_space()/1024); 76 tty->cr(); 77 tty->print_cr("# of codelets = %6d" , _code->number_of_stubs()); 78 if (_code->number_of_stubs() != 0) { 79 tty->print_cr("avg codelet size = %6d bytes", _code->used_space() / _code->number_of_stubs()); 80 tty->cr(); 81 } 82 _code->print(); 83 tty->print_cr("----------------------------------------------------------------------"); 84 tty->cr(); 85 } 86 87 88 //------------------------------------------------------------------------------------------------------------------------ 89 // Implementation of interpreter 90 91 StubQueue* AbstractInterpreter::_code = NULL; 92 bool AbstractInterpreter::_notice_safepoints = false; 93 address AbstractInterpreter::_rethrow_exception_entry = NULL; 94 95 address AbstractInterpreter::_native_entry_begin = NULL; 96 address AbstractInterpreter::_native_entry_end = NULL; 97 address AbstractInterpreter::_slow_signature_handler; 98 address AbstractInterpreter::_entry_table [AbstractInterpreter::number_of_method_entries]; 99 address AbstractInterpreter::_cds_entry_table [AbstractInterpreter::number_of_method_entries]; 100 address AbstractInterpreter::_native_abi_to_tosca [AbstractInterpreter::number_of_result_handlers]; 101 102 //------------------------------------------------------------------------------------------------------------------------ 103 // Generation of complete interpreter 104 105 AbstractInterpreterGenerator::AbstractInterpreterGenerator(StubQueue* _code) { 106 _masm = NULL; 107 } 108 109 110 //------------------------------------------------------------------------------------------------------------------------ 111 // Entry points 112 113 AbstractInterpreter::MethodKind AbstractInterpreter::method_kind(const methodHandle& m) { 114 // Abstract method? 115 if (m->is_abstract()) return abstract; 116 117 // Method handle primitive? 118 if (m->is_method_handle_intrinsic()) { 119 vmIntrinsics::ID id = m->intrinsic_id(); 120 assert(MethodHandles::is_signature_polymorphic(id), "must match an intrinsic"); 121 MethodKind kind = (MethodKind)( method_handle_invoke_FIRST + 122 ((int)id - vmIntrinsics::FIRST_MH_SIG_POLY) ); 123 assert(kind <= method_handle_invoke_LAST, "parallel enum ranges"); 124 return kind; 125 } 126 127 #ifndef CC_INTERP 128 switch (m->intrinsic_id()) { 129 // Use optimized stub code for CRC32 native methods. 130 case vmIntrinsics::_updateCRC32 : return java_util_zip_CRC32_update; 131 case vmIntrinsics::_updateBytesCRC32 : return java_util_zip_CRC32_updateBytes; 132 case vmIntrinsics::_updateByteBufferCRC32 : return java_util_zip_CRC32_updateByteBuffer; 133 // Use optimized stub code for CRC32C methods. 134 case vmIntrinsics::_updateBytesCRC32C : return java_util_zip_CRC32C_updateBytes; 135 case vmIntrinsics::_updateDirectByteBufferCRC32C : return java_util_zip_CRC32C_updateDirectByteBuffer; 136 case vmIntrinsics::_intBitsToFloat: return java_lang_Float_intBitsToFloat; 137 case vmIntrinsics::_floatToRawIntBits: return java_lang_Float_floatToRawIntBits; 138 case vmIntrinsics::_longBitsToDouble: return java_lang_Double_longBitsToDouble; 139 case vmIntrinsics::_doubleToRawLongBits: return java_lang_Double_doubleToRawLongBits; 140 default: break; 141 } 142 #endif // CC_INTERP 143 144 // Native method? 145 // Note: This test must come _before_ the test for intrinsic 146 // methods. See also comments below. 147 if (m->is_native()) { 148 assert(!m->is_method_handle_intrinsic(), "overlapping bits here, watch out"); 149 return m->is_synchronized() ? native_synchronized : native; 150 } 151 152 // Synchronized? 153 if (m->is_synchronized()) { 154 return zerolocals_synchronized; 155 } 156 157 if (RegisterFinalizersAtInit && m->code_size() == 1 && 158 m->intrinsic_id() == vmIntrinsics::_Object_init) { 159 // We need to execute the special return bytecode to check for 160 // finalizer registration so create a normal frame. 161 return zerolocals; 162 } 163 164 // Empty method? 165 if (m->is_empty_method()) { 166 return empty; 167 } 168 169 // Special intrinsic method? 170 // Note: This test must come _after_ the test for native methods, 171 // otherwise we will run into problems with JDK 1.2, see also 172 // TemplateInterpreterGenerator::generate_method_entry() for 173 // for details. 174 switch (m->intrinsic_id()) { 175 case vmIntrinsics::_dsin : return java_lang_math_sin ; 176 case vmIntrinsics::_dcos : return java_lang_math_cos ; 177 case vmIntrinsics::_dtan : return java_lang_math_tan ; 178 case vmIntrinsics::_dabs : return java_lang_math_abs ; 179 case vmIntrinsics::_dsqrt : return java_lang_math_sqrt ; 180 case vmIntrinsics::_dlog : return java_lang_math_log ; 181 case vmIntrinsics::_dlog10: return java_lang_math_log10; 182 case vmIntrinsics::_dpow : return java_lang_math_pow ; 183 case vmIntrinsics::_dexp : return java_lang_math_exp ; 184 case vmIntrinsics::_fmaD : return java_lang_math_fmaD ; 185 case vmIntrinsics::_fmaF : return java_lang_math_fmaF ; 186 187 case vmIntrinsics::_Reference_get 188 : return java_lang_ref_reference_get; 189 default : break; 190 } 191 192 #ifdef AARCH32 193 if (UseAESIntrinsics) { 194 // Use optimized stub code for AES native methods. 195 switch (m->intrinsic_id()) { 196 case vmIntrinsics::_aescrypt_encryptBlock : return com_sun_crypto_provider_AESCrypt_encryptBlock; 197 case vmIntrinsics::_aescrypt_decryptBlock : return com_sun_crypto_provider_AESCrypt_decryptBlock; 198 } 199 // Use optimized stub code for AES CBC native methods. 200 if (StubRoutines::cipherBlockChaining_encryptAESCrypt_special() && 201 m->intrinsic_id() == vmIntrinsics::_cipherBlockChaining_encryptAESCrypt) 202 return com_sun_crypto_provider_CipherBlockChaining_encrypt; 203 204 if (StubRoutines::cipherBlockChaining_decryptAESCrypt_special() && 205 m->intrinsic_id() == vmIntrinsics::_cipherBlockChaining_decryptAESCrypt) 206 return com_sun_crypto_provider_CipherBlockChaining_decrypt; 207 } 208 209 // Use optimized stub code for SHA256/512 native methods. 210 switch (m->intrinsic_id()) { 211 case vmIntrinsics::_sha_implCompress : 212 if (UseSHA1Intrinsics) return sun_security_provider_SHA_implCompress; 213 break; 214 case vmIntrinsics::_sha2_implCompress : 215 if (UseSHA256Intrinsics) return sun_security_provider_SHA2_implCompress; 216 break; 217 case vmIntrinsics::_sha5_implCompress : 218 if (UseSHA512Intrinsics) return sun_security_provider_SHA5_implCompress; 219 break; 220 } 221 #endif 222 223 // Accessor method? 224 if (m->is_getter()) { 225 // TODO: We should have used ::is_accessor above, but fast accessors in Zero expect only getters. 226 // See CppInterpreter::accessor_entry in cppInterpreter_zero.cpp. This should be fixed in Zero, 227 // then the call above updated to ::is_accessor 228 assert(m->size_of_parameters() == 1, "fast code for accessors assumes parameter size = 1"); 229 return accessor; 230 } 231 232 // Note: for now: zero locals for all non-empty methods 233 return zerolocals; 234 } 235 236 #if INCLUDE_CDS 237 238 address AbstractInterpreter::get_trampoline_code_buffer(AbstractInterpreter::MethodKind kind) { 239 const size_t trampoline_size = SharedRuntime::trampoline_size(); 240 address addr = MetaspaceShared::cds_i2i_entry_code_buffers((size_t)(AbstractInterpreter::number_of_method_entries) * trampoline_size); 241 addr += (size_t)(kind) * trampoline_size; 242 243 return addr; 244 } 245 246 void AbstractInterpreter::update_cds_entry_table(AbstractInterpreter::MethodKind kind) { 247 if (DumpSharedSpaces || UseSharedSpaces) { 248 address trampoline = get_trampoline_code_buffer(kind); 249 _cds_entry_table[kind] = trampoline; 250 251 CodeBuffer buffer(trampoline, (int)(SharedRuntime::trampoline_size())); 252 MacroAssembler _masm(&buffer); 253 SharedRuntime::generate_trampoline(&_masm, _entry_table[kind]); 254 255 if (PrintInterpreter) { 256 Disassembler::decode(buffer.insts_begin(), buffer.insts_end()); 257 } 258 } 259 } 260 261 #endif 262 263 void AbstractInterpreter::set_entry_for_kind(AbstractInterpreter::MethodKind kind, address entry) { 264 assert(kind >= method_handle_invoke_FIRST && 265 kind <= method_handle_invoke_LAST, "late initialization only for MH entry points"); 266 assert(_entry_table[kind] == _entry_table[abstract], "previous value must be AME entry"); 267 _entry_table[kind] = entry; 268 269 update_cds_entry_table(kind); 270 } 271 272 // Return true if the interpreter can prove that the given bytecode has 273 // not yet been executed (in Java semantics, not in actual operation). 274 bool AbstractInterpreter::is_not_reached(const methodHandle& method, int bci) { 275 Bytecodes::Code code = method()->code_at(bci); 276 277 if (!Bytecodes::must_rewrite(code)) { 278 // might have been reached 279 return false; 280 } 281 282 // the bytecode might not be rewritten if the method is an accessor, etc. 283 address ientry = method->interpreter_entry(); 284 if (ientry != entry_for_kind(AbstractInterpreter::zerolocals) && 285 ientry != entry_for_kind(AbstractInterpreter::zerolocals_synchronized)) 286 return false; // interpreter does not run this method! 287 288 // otherwise, we can be sure this bytecode has never been executed 289 return true; 290 } 291 292 293 #ifndef PRODUCT 294 void AbstractInterpreter::print_method_kind(MethodKind kind) { 295 switch (kind) { 296 case zerolocals : tty->print("zerolocals" ); break; 297 case zerolocals_synchronized: tty->print("zerolocals_synchronized"); break; 298 case native : tty->print("native" ); break; 299 case native_synchronized : tty->print("native_synchronized" ); break; 300 case empty : tty->print("empty" ); break; 301 case accessor : tty->print("accessor" ); break; 302 case abstract : tty->print("abstract" ); break; 303 case java_lang_math_sin : tty->print("java_lang_math_sin" ); break; 304 case java_lang_math_cos : tty->print("java_lang_math_cos" ); break; 305 case java_lang_math_tan : tty->print("java_lang_math_tan" ); break; 306 case java_lang_math_abs : tty->print("java_lang_math_abs" ); break; 307 case java_lang_math_sqrt : tty->print("java_lang_math_sqrt" ); break; 308 case java_lang_math_log : tty->print("java_lang_math_log" ); break; 309 case java_lang_math_log10 : tty->print("java_lang_math_log10" ); break; 310 case java_lang_math_fmaD : tty->print("java_lang_math_fmaD" ); break; 311 case java_lang_math_fmaF : tty->print("java_lang_math_fmaF" ); break; 312 case java_util_zip_CRC32_update : tty->print("java_util_zip_CRC32_update"); break; 313 case java_util_zip_CRC32_updateBytes : tty->print("java_util_zip_CRC32_updateBytes"); break; 314 case java_util_zip_CRC32_updateByteBuffer : tty->print("java_util_zip_CRC32_updateByteBuffer"); break; 315 case java_util_zip_CRC32C_updateBytes : tty->print("java_util_zip_CRC32C_updateBytes"); break; 316 case java_util_zip_CRC32C_updateDirectByteBuffer: tty->print("java_util_zip_CRC32C_updateDirectByteByffer"); break; 317 #ifdef AARCH32 318 case com_sun_crypto_provider_AESCrypt_encryptBlock : tty->print("com_sun_crypto_provider_AESCrypt_encryptBlock"); break; 319 case com_sun_crypto_provider_AESCrypt_decryptBlock : tty->print("com_sun_crypto_provider_AESCrypt_decryptBlock"); break; 320 case com_sun_crypto_provider_CipherBlockChaining_encrypt : tty->print("com_sun_crypto_provider_CipherBlockChaining_encrypt"); break; 321 case com_sun_crypto_provider_CipherBlockChaining_decrypt : tty->print("com_sun_crypto_provider_CipherBlockChaining_decrypt"); break; 322 case sun_security_provider_SHA_implCompress : tty->print("sun_security_provider_SHA_implCompress"); break; 323 case sun_security_provider_SHA2_implCompress : tty->print("sun_security_provider_SHA2_implCompress"); break; 324 case sun_security_provider_SHA5_implCompress : tty->print("sun_security_provider_SHA5_implCompress"); break; 325 #endif 326 default: 327 if (kind >= method_handle_invoke_FIRST && 328 kind <= method_handle_invoke_LAST) { 329 const char* kind_name = vmIntrinsics::name_at(method_handle_intrinsic(kind)); 330 if (kind_name[0] == '_') kind_name = &kind_name[1]; // '_invokeExact' => 'invokeExact' 331 tty->print("method_handle_%s", kind_name); 332 break; 333 } 334 ShouldNotReachHere(); 335 break; 336 } 337 } 338 #endif // PRODUCT 339 340 341 //------------------------------------------------------------------------------------------------------------------------ 342 // Deoptimization support 343 344 /** 345 * If a deoptimization happens, this function returns the point of next bytecode to continue execution. 346 */ 347 address AbstractInterpreter::deopt_continue_after_entry(Method* method, address bcp, int callee_parameters, bool is_top_frame) { 348 assert(method->contains(bcp), "just checkin'"); 349 350 // Get the original and rewritten bytecode. 351 Bytecodes::Code code = Bytecodes::java_code_at(method, bcp); 352 assert(!Interpreter::bytecode_should_reexecute(code), "should not reexecute"); 353 354 const int bci = method->bci_from(bcp); 355 356 // compute continuation length 357 const int length = Bytecodes::length_at(method, bcp); 358 359 // compute result type 360 BasicType type = T_ILLEGAL; 361 362 switch (code) { 363 case Bytecodes::_invokevirtual : 364 case Bytecodes::_invokespecial : 365 case Bytecodes::_invokestatic : 366 case Bytecodes::_invokeinterface: { 367 Thread *thread = Thread::current(); 368 ResourceMark rm(thread); 369 methodHandle mh(thread, method); 370 type = Bytecode_invoke(mh, bci).result_type(); 371 // since the cache entry might not be initialized: 372 // (NOT needed for the old calling convension) 373 if (!is_top_frame) { 374 int index = Bytes::get_native_u2(bcp+1); 375 method->constants()->cache()->entry_at(index)->set_parameter_size(callee_parameters); 376 } 377 break; 378 } 379 380 case Bytecodes::_invokedynamic: { 381 Thread *thread = Thread::current(); 382 ResourceMark rm(thread); 383 methodHandle mh(thread, method); 384 type = Bytecode_invoke(mh, bci).result_type(); 385 // since the cache entry might not be initialized: 386 // (NOT needed for the old calling convension) 387 if (!is_top_frame) { 388 int index = Bytes::get_native_u4(bcp+1); 389 method->constants()->invokedynamic_cp_cache_entry_at(index)->set_parameter_size(callee_parameters); 390 } 391 break; 392 } 393 394 case Bytecodes::_ldc : 395 case Bytecodes::_ldc_w : // fall through 396 case Bytecodes::_ldc2_w: 397 { 398 Thread *thread = Thread::current(); 399 ResourceMark rm(thread); 400 methodHandle mh(thread, method); 401 type = Bytecode_loadconstant(mh, bci).result_type(); 402 break; 403 } 404 405 default: 406 type = Bytecodes::result_type(code); 407 break; 408 } 409 410 // return entry point for computed continuation state & bytecode length 411 return 412 is_top_frame 413 ? Interpreter::deopt_entry (as_TosState(type), length) 414 : Interpreter::return_entry(as_TosState(type), length, code); 415 } 416 417 // If deoptimization happens, this function returns the point where the interpreter reexecutes 418 // the bytecode. 419 // Note: Bytecodes::_athrow is a special case in that it does not return 420 // Interpreter::deopt_entry(vtos, 0) like others 421 address AbstractInterpreter::deopt_reexecute_entry(Method* method, address bcp) { 422 assert(method->contains(bcp), "just checkin'"); 423 Bytecodes::Code code = Bytecodes::java_code_at(method, bcp); 424 #if defined(COMPILER1) || INCLUDE_JVMCI 425 if(code == Bytecodes::_athrow ) { 426 return Interpreter::rethrow_exception_entry(); 427 } 428 #endif /* COMPILER1 || INCLUDE_JVMCI */ 429 return Interpreter::deopt_entry(vtos, 0); 430 } 431 432 // If deoptimization happens, the interpreter should reexecute these bytecodes. 433 // This function mainly helps the compilers to set up the reexecute bit. 434 bool AbstractInterpreter::bytecode_should_reexecute(Bytecodes::Code code) { 435 switch (code) { 436 case Bytecodes::_lookupswitch: 437 case Bytecodes::_tableswitch: 438 case Bytecodes::_fast_binaryswitch: 439 case Bytecodes::_fast_linearswitch: 440 // recompute condtional expression folded into _if<cond> 441 case Bytecodes::_lcmp : 442 case Bytecodes::_fcmpl : 443 case Bytecodes::_fcmpg : 444 case Bytecodes::_dcmpl : 445 case Bytecodes::_dcmpg : 446 case Bytecodes::_ifnull : 447 case Bytecodes::_ifnonnull : 448 case Bytecodes::_goto : 449 case Bytecodes::_goto_w : 450 case Bytecodes::_ifeq : 451 case Bytecodes::_ifne : 452 case Bytecodes::_iflt : 453 case Bytecodes::_ifge : 454 case Bytecodes::_ifgt : 455 case Bytecodes::_ifle : 456 case Bytecodes::_if_icmpeq : 457 case Bytecodes::_if_icmpne : 458 case Bytecodes::_if_icmplt : 459 case Bytecodes::_if_icmpge : 460 case Bytecodes::_if_icmpgt : 461 case Bytecodes::_if_icmple : 462 case Bytecodes::_if_acmpeq : 463 case Bytecodes::_if_acmpne : 464 // special cases 465 case Bytecodes::_getfield : 466 case Bytecodes::_putfield : 467 case Bytecodes::_getstatic : 468 case Bytecodes::_putstatic : 469 case Bytecodes::_aastore : 470 #ifdef COMPILER1 471 //special case of reexecution 472 case Bytecodes::_athrow : 473 #endif 474 return true; 475 476 default: 477 return false; 478 } 479 } 480 481 void AbstractInterpreter::initialize_method_handle_entries() { 482 // method handle entry kinds are generated later in MethodHandlesAdapterGenerator::generate: 483 for (int i = method_handle_invoke_FIRST; i <= method_handle_invoke_LAST; i++) { 484 MethodKind kind = (MethodKind) i; 485 _entry_table[kind] = _entry_table[Interpreter::abstract]; 486 Interpreter::update_cds_entry_table(kind); 487 } 488 }