1 /*
   2  * Copyright (c) 1997, 2014, 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 "interpreter/bytecodeHistogram.hpp"
  28 #include "interpreter/interpreter.hpp"
  29 #include "interpreter/interpreterGenerator.hpp"
  30 #include "interpreter/interpreterRuntime.hpp"
  31 #include "interpreter/interp_masm.hpp"
  32 #include "interpreter/templateTable.hpp"
  33 #include "oops/arrayOop.hpp"
  34 #include "oops/methodData.hpp"
  35 #include "oops/method.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "prims/jvmtiExport.hpp"
  38 #include "prims/jvmtiThreadState.hpp"
  39 #include "prims/methodHandles.hpp"
  40 #include "runtime/arguments.hpp"
  41 #include "runtime/deoptimization.hpp"
  42 #include "runtime/frame.inline.hpp"
  43 #include "runtime/sharedRuntime.hpp"
  44 #include "runtime/stubRoutines.hpp"
  45 #include "runtime/synchronizer.hpp"
  46 #include "runtime/timer.hpp"
  47 #include "runtime/vframeArray.hpp"
  48 #include "utilities/debug.hpp"
  49 #ifdef COMPILER1
  50 #include "c1/c1_Runtime1.hpp"
  51 #endif
  52 
  53 #define __ _masm->
  54 
  55 //------------------------------------------------------------------------------------------------------------------------
  56 
  57 address AbstractInterpreterGenerator::generate_slow_signature_handler() {
  58   address entry = __ pc();
  59   // rbx,: method
  60   // rcx: temporary
  61   // rdi: pointer to locals
  62   // rsp: end of copied parameters area
  63   __ mov(rcx, rsp);
  64   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::slow_signature_handler), rbx, rdi, rcx);
  65   __ ret(0);
  66   return entry;
  67 }
  68 
  69 
  70 address InterpreterGenerator::generate_math_entry(AbstractInterpreter::MethodKind kind) {
  71 
  72   // rbx,: Method*
  73   // rcx: scratrch
  74   // rsi: sender sp
  75 
  76   if (!InlineIntrinsics) return NULL; // Generate a vanilla entry
  77 
  78   address entry_point = __ pc();
  79 
  80   // These don't need a safepoint check because they aren't virtually
  81   // callable. We won't enter these intrinsics from compiled code.
  82   // If in the future we added an intrinsic which was virtually callable
  83   // we'd have to worry about how to safepoint so that this code is used.
  84 
  85   // mathematical functions inlined by compiler
  86   // (interpreter must provide identical implementation
  87   // in order to avoid monotonicity bugs when switching
  88   // from interpreter to compiler in the middle of some
  89   // computation)
  90   //
  91   // stack: [ ret adr ] <-- rsp
  92   //        [ lo(arg) ]
  93   //        [ hi(arg) ]
  94   //
  95 
  96   // Note: For JDK 1.2 StrictMath doesn't exist and Math.sin/cos/sqrt are
  97   //       native methods. Interpreter::method_kind(...) does a check for
  98   //       native methods first before checking for intrinsic methods and
  99   //       thus will never select this entry point. Make sure it is not
 100   //       called accidentally since the SharedRuntime entry points will
 101   //       not work for JDK 1.2.
 102   //
 103   // We no longer need to check for JDK 1.2 since it's EOL'ed.
 104   // The following check existed in pre 1.6 implementation,
 105   //    if (Universe::is_jdk12x_version()) {
 106   //      __ should_not_reach_here();
 107   //    }
 108   // Universe::is_jdk12x_version() always returns false since
 109   // the JDK version is not yet determined when this method is called.
 110   // This method is called during interpreter_init() whereas
 111   // JDK version is only determined when universe2_init() is called.
 112 
 113   // Note: For JDK 1.3 StrictMath exists and Math.sin/cos/sqrt are
 114   //       java methods.  Interpreter::method_kind(...) will select
 115   //       this entry point for the corresponding methods in JDK 1.3.
 116   // get argument
 117   __ fld_d(Address(rsp, 1*wordSize));
 118   switch (kind) {
 119     case Interpreter::java_lang_math_sin :
 120         __ trigfunc('s');
 121         break;
 122     case Interpreter::java_lang_math_cos :
 123         __ trigfunc('c');
 124         break;
 125     case Interpreter::java_lang_math_tan :
 126         __ trigfunc('t');
 127         break;
 128     case Interpreter::java_lang_math_sqrt:
 129         __ fsqrt();
 130         break;
 131     case Interpreter::java_lang_math_abs:
 132         __ fabs();
 133         break;
 134     case Interpreter::java_lang_math_log:
 135         __ subptr(rsp, 2 * wordSize);
 136         __ fstp_d(Address(rsp, 0));
 137         if (VM_Version::supports_sse2()) {
 138           __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, StubRoutines::dlog())));
 139         }
 140         else {
 141           __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::dlog)));
 142         }
 143         __ addptr(rsp, 2 * wordSize);
 144         break;
 145     case Interpreter::java_lang_math_log10:
 146         __ flog10();
 147         // Store to stack to convert 80bit precision back to 64bits
 148         __ push_fTOS();
 149         __ pop_fTOS();
 150         break;
 151     case Interpreter::java_lang_math_pow:
 152       __ fld_d(Address(rsp, 3*wordSize)); // second argument
 153       __ pow_with_fallback(0);
 154       // Store to stack to convert 80bit precision back to 64bits
 155       __ push_fTOS();
 156       __ pop_fTOS();
 157       break;
 158     case Interpreter::java_lang_math_exp:
 159       __ subptr(rsp, 2*wordSize);
 160       __ fstp_d(Address(rsp, 0));
 161       if (VM_Version::supports_sse2()) {
 162         __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, StubRoutines::dexp())));
 163       } else {
 164         __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::dexp)));
 165       }
 166       __ addptr(rsp, 2*wordSize);
 167     break;
 168     default                              :
 169         ShouldNotReachHere();
 170   }
 171 
 172   // return double result in xmm0 for interpreter and compilers.
 173   if (UseSSE >= 2) {
 174     __ subptr(rsp, 2*wordSize);
 175     __ fstp_d(Address(rsp, 0));
 176     __ movdbl(xmm0, Address(rsp, 0));
 177     __ addptr(rsp, 2*wordSize);
 178   }
 179 
 180   // done, result in FPU ST(0) or XMM0
 181   __ pop(rdi);                               // get return address
 182   __ mov(rsp, rsi);                          // set sp to sender sp
 183   __ jmp(rdi);
 184 
 185   return entry_point;
 186 }
 187 
 188 
 189 void Deoptimization::unwind_callee_save_values(frame* f, vframeArray* vframe_array) {
 190 
 191   // This code is sort of the equivalent of C2IAdapter::setup_stack_frame back in
 192   // the days we had adapter frames. When we deoptimize a situation where a
 193   // compiled caller calls a compiled caller will have registers it expects
 194   // to survive the call to the callee. If we deoptimize the callee the only
 195   // way we can restore these registers is to have the oldest interpreter
 196   // frame that we create restore these values. That is what this routine
 197   // will accomplish.
 198 
 199   // At the moment we have modified c2 to not have any callee save registers
 200   // so this problem does not exist and this routine is just a place holder.
 201 
 202   assert(f->is_interpreted_frame(), "must be interpreted");
 203 }