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         __ flog();
 136         // Store to stack to convert 80bit precision back to 64bits
 137         __ push_fTOS();
 138         __ pop_fTOS();
 139         break;
 140     case Interpreter::java_lang_math_log10:
 141         __ flog10();
 142         // Store to stack to convert 80bit precision back to 64bits
 143         __ push_fTOS();
 144         __ pop_fTOS();
 145         break;
 146     case Interpreter::java_lang_math_pow:
 147       __ fld_d(Address(rsp, 3*wordSize)); // second argument
 148       __ pow_with_fallback(0);
 149       // Store to stack to convert 80bit precision back to 64bits
 150       __ push_fTOS();
 151       __ pop_fTOS();
 152       break;
 153     case Interpreter::java_lang_math_exp:
 154       __ exp_with_fallback(0);
 155       // Store to stack to convert 80bit precision back to 64bits
 156       __ push_fTOS();
 157       __ pop_fTOS();
 158       break;
 159     default                              :
 160         ShouldNotReachHere();
 161   }
 162 
 163   // return double result in xmm0 for interpreter and compilers.
 164   if (UseSSE >= 2) {
 165     __ subptr(rsp, 2*wordSize);
 166     __ fstp_d(Address(rsp, 0));
 167     __ movdbl(xmm0, Address(rsp, 0));
 168     __ addptr(rsp, 2*wordSize);
 169   }
 170 
 171   // done, result in FPU ST(0) or XMM0
 172   __ pop(rdi);                               // get return address
 173   __ mov(rsp, rsi);                          // set sp to sender sp
 174   __ jmp(rdi);
 175 
 176   return entry_point;
 177 }
 178 
 179 
 180 void Deoptimization::unwind_callee_save_values(frame* f, vframeArray* vframe_array) {
 181 
 182   // This code is sort of the equivalent of C2IAdapter::setup_stack_frame back in
 183   // the days we had adapter frames. When we deoptimize a situation where a
 184   // compiled caller calls a compiled caller will have registers it expects
 185   // to survive the call to the callee. If we deoptimize the callee the only
 186   // way we can restore these registers is to have the oldest interpreter
 187   // frame that we create restore these values. That is what this routine
 188   // will accomplish.
 189 
 190   // At the moment we have modified c2 to not have any callee save registers
 191   // so this problem does not exist and this routine is just a place holder.
 192 
 193   assert(f->is_interpreted_frame(), "must be interpreted");
 194 }