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