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