1 /*
   2  * Copyright (c) 1997, 2011, 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/assembler.hpp"
  27 #include "interpreter/bytecodeHistogram.hpp"
  28 #include "interpreter/interpreter.hpp"
  29 #include "interpreter/interpreterGenerator.hpp"
  30 #include "interpreter/interpreterRuntime.hpp"
  31 #include "interpreter/templateTable.hpp"
  32 #include "oops/arrayOop.hpp"
  33 #include "oops/methodDataOop.hpp"
  34 #include "oops/methodOop.hpp"
  35 #include "oops/oop.inline.hpp"
  36 #include "prims/jvmtiExport.hpp"
  37 #include "prims/jvmtiThreadState.hpp"
  38 #include "prims/methodHandles.hpp"
  39 #include "runtime/arguments.hpp"
  40 #include "runtime/deoptimization.hpp"
  41 #include "runtime/frame.inline.hpp"
  42 #include "runtime/sharedRuntime.hpp"
  43 #include "runtime/stubRoutines.hpp"
  44 #include "runtime/synchronizer.hpp"
  45 #include "runtime/timer.hpp"
  46 #include "runtime/vframeArray.hpp"
  47 #include "utilities/debug.hpp"
  48 #ifdef COMPILER1
  49 #include "c1/c1_Runtime1.hpp"
  50 #endif
  51 
  52 #define __ _masm->
  53 
  54 //------------------------------------------------------------------------------------------------------------------------
  55 
  56 address AbstractInterpreterGenerator::generate_slow_signature_handler() {
  57   address entry = __ pc();
  58   // rbx,: method
  59   // rcx: temporary
  60   // rdi: pointer to locals
  61   // rsp: end of copied parameters area
  62   __ mov(rcx, rsp);
  63   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::slow_signature_handler), rbx, rdi, rcx);
  64   __ ret(0);
  65   return entry;
  66 }
  67 
  68 
  69 //
  70 // Various method entries (that c++ and asm interpreter agree upon)
  71 //------------------------------------------------------------------------------------------------------------------------
  72 //
  73 //
  74 
  75 // Empty method, generate a very fast return.
  76 
  77 address InterpreterGenerator::generate_empty_entry(void) {
  78 
  79   // rbx,: methodOop
  80   // rcx: receiver (unused)
  81   // rsi: previous interpreter state (C++ interpreter) must preserve
  82   // rsi: sender sp must set sp to this value on return
  83 
  84   if (!UseFastEmptyMethods) return NULL;
  85 
  86   address entry_point = __ pc();
  87 
  88   // If we need a safepoint check, generate full interpreter entry.
  89   Label slow_path;
  90   ExternalAddress state(SafepointSynchronize::address_of_state());
  91   __ cmp32(ExternalAddress(SafepointSynchronize::address_of_state()),
  92            SafepointSynchronize::_not_synchronized);
  93   __ jcc(Assembler::notEqual, slow_path);
  94 
  95   // do nothing for empty methods (do not even increment invocation counter)
  96   // Code: _return
  97   // _return
  98   // return w/o popping parameters
  99   __ pop(rax);
 100   __ mov(rsp, rsi);
 101   __ jmp(rax);
 102 
 103   __ bind(slow_path);
 104   (void) generate_normal_entry(false);
 105   return entry_point;
 106 }
 107 
 108 address InterpreterGenerator::generate_math_entry(AbstractInterpreter::MethodKind kind) {
 109 
 110   // rbx,: methodOop
 111   // rcx: scratrch
 112   // rsi: sender sp
 113 
 114   if (!InlineIntrinsics) return NULL; // Generate a vanilla entry
 115 
 116   address entry_point = __ pc();
 117 
 118   // These don't need a safepoint check because they aren't virtually
 119   // callable. We won't enter these intrinsics from compiled code.
 120   // If in the future we added an intrinsic which was virtually callable
 121   // we'd have to worry about how to safepoint so that this code is used.
 122 
 123   // mathematical functions inlined by compiler
 124   // (interpreter must provide identical implementation
 125   // in order to avoid monotonicity bugs when switching
 126   // from interpreter to compiler in the middle of some
 127   // computation)
 128   //
 129   // stack: [ ret adr ] <-- rsp
 130   //        [ lo(arg) ]
 131   //        [ hi(arg) ]
 132   //
 133 
 134   // Note: For JDK 1.2 StrictMath doesn't exist and Math.sin/cos/sqrt are
 135   //       native methods. Interpreter::method_kind(...) does a check for
 136   //       native methods first before checking for intrinsic methods and
 137   //       thus will never select this entry point. Make sure it is not
 138   //       called accidentally since the SharedRuntime entry points will
 139   //       not work for JDK 1.2.
 140   //
 141   // We no longer need to check for JDK 1.2 since it's EOL'ed.
 142   // The following check existed in pre 1.6 implementation,
 143   //    if (Universe::is_jdk12x_version()) {
 144   //      __ should_not_reach_here();
 145   //    }
 146   // Universe::is_jdk12x_version() always returns false since
 147   // the JDK version is not yet determined when this method is called.
 148   // This method is called during interpreter_init() whereas
 149   // JDK version is only determined when universe2_init() is called.
 150 
 151   // Note: For JDK 1.3 StrictMath exists and Math.sin/cos/sqrt are
 152   //       java methods.  Interpreter::method_kind(...) will select
 153   //       this entry point for the corresponding methods in JDK 1.3.
 154   // get argument
 155   __ fld_d(Address(rsp, 1*wordSize));
 156   switch (kind) {
 157     case Interpreter::java_lang_math_sin :
 158         __ trigfunc('s');
 159         break;
 160     case Interpreter::java_lang_math_cos :
 161         __ trigfunc('c');
 162         break;
 163     case Interpreter::java_lang_math_tan :
 164         __ trigfunc('t');
 165         break;
 166     case Interpreter::java_lang_math_sqrt:
 167         __ fsqrt();
 168         break;
 169     case Interpreter::java_lang_math_abs:
 170         __ fabs();
 171         break;
 172     case Interpreter::java_lang_math_log:
 173         __ flog();
 174         // Store to stack to convert 80bit precision back to 64bits
 175         __ push_fTOS();
 176         __ pop_fTOS();
 177         break;
 178     case Interpreter::java_lang_math_log10:
 179         __ flog10();
 180         // Store to stack to convert 80bit precision back to 64bits
 181         __ push_fTOS();
 182         __ pop_fTOS();
 183         break;
 184     default                              :
 185         ShouldNotReachHere();
 186   }
 187 
 188   // return double result in xmm0 for interpreter and compilers.
 189   if (UseSSE >= 2) {
 190     __ subptr(rsp, 2*wordSize);
 191     __ fstp_d(Address(rsp, 0));
 192     __ movdbl(xmm0, Address(rsp, 0));
 193     __ addptr(rsp, 2*wordSize);
 194   }
 195 
 196   // done, result in FPU ST(0) or XMM0
 197   __ pop(rdi);                               // get return address
 198   __ mov(rsp, rsi);                          // set sp to sender sp
 199   __ jmp(rdi);
 200 
 201   return entry_point;
 202 }
 203 
 204 
 205 // Abstract method entry
 206 // Attempt to execute abstract method. Throw exception
 207 address InterpreterGenerator::generate_abstract_entry(void) {
 208 
 209   // rbx,: methodOop
 210   // rcx: receiver (unused)
 211   // rsi: previous interpreter state (C++ interpreter) must preserve
 212 
 213   // rsi: sender SP
 214 
 215   address entry_point = __ pc();
 216 
 217   // abstract method entry
 218 
 219   //  pop return address, reset last_sp to NULL
 220   __ empty_expression_stack();
 221   __ restore_bcp();      // rsi must be correct for exception handler   (was destroyed)
 222   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
 223 
 224   // throw exception
 225   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodError));
 226   // the call_VM checks for exception, so we should never return here.
 227   __ should_not_reach_here();
 228 
 229   return entry_point;
 230 }
 231 
 232 
 233 // Method handle invoker
 234 // Dispatch a method of the form java.lang.invoke.MethodHandles::invoke(...)
 235 address InterpreterGenerator::generate_method_handle_entry(void) {
 236   if (!EnableMethodHandles) {
 237     return generate_abstract_entry();
 238   }
 239 
 240   address entry_point = MethodHandles::generate_method_handle_interpreter_entry(_masm);
 241 
 242   return entry_point;
 243 }
 244 
 245 
 246 // This method tells the deoptimizer how big an interpreted frame must be:
 247 int AbstractInterpreter::size_activation(methodOop method,
 248                                          int tempcount,
 249                                          int popframe_extra_args,
 250                                          int moncount,
 251                                          int callee_param_count,
 252                                          int callee_locals,
 253                                          bool is_top_frame) {
 254   return layout_activation(method,
 255                            tempcount,
 256                            popframe_extra_args,
 257                            moncount,
 258                            callee_param_count,
 259                            callee_locals,
 260                            (frame*) NULL,
 261                            (frame*) NULL,
 262                            is_top_frame);
 263 }
 264 
 265 void Deoptimization::unwind_callee_save_values(frame* f, vframeArray* vframe_array) {
 266 
 267   // This code is sort of the equivalent of C2IAdapter::setup_stack_frame back in
 268   // the days we had adapter frames. When we deoptimize a situation where a
 269   // compiled caller calls a compiled caller will have registers it expects
 270   // to survive the call to the callee. If we deoptimize the callee the only
 271   // way we can restore these registers is to have the oldest interpreter
 272   // frame that we create restore these values. That is what this routine
 273   // will accomplish.
 274 
 275   // At the moment we have modified c2 to not have any callee save registers
 276   // so this problem does not exist and this routine is just a place holder.
 277 
 278   assert(f->is_interpreted_frame(), "must be interpreted");
 279 }