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