1 /*
   2  * Copyright (c) 2003, 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 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  56 
  57 #ifdef _WIN64
  58 address AbstractInterpreterGenerator::generate_slow_signature_handler() {
  59   address entry = __ pc();
  60 
  61   // rbx: method
  62   // r14: pointer to locals
  63   // c_rarg3: first stack arg - wordSize
  64   __ mov(c_rarg3, rsp);
  65   // adjust rsp
  66   __ subptr(rsp, 4 * wordSize);
  67   __ call_VM(noreg,
  68              CAST_FROM_FN_PTR(address,
  69                               InterpreterRuntime::slow_signature_handler),
  70              rbx, r14, c_rarg3);
  71 
  72   // rax: result handler
  73 
  74   // Stack layout:
  75   // rsp: 3 integer or float args (if static first is unused)
  76   //      1 float/double identifiers
  77   //        return address
  78   //        stack args
  79   //        garbage
  80   //        expression stack bottom
  81   //        bcp (NULL)
  82   //        ...
  83 
  84   // Do FP first so we can use c_rarg3 as temp
  85   __ movl(c_rarg3, Address(rsp, 3 * wordSize)); // float/double identifiers
  86 
  87   for ( int i= 0; i < Argument::n_int_register_parameters_c-1; i++ ) {
  88     XMMRegister floatreg = as_XMMRegister(i+1);
  89     Label isfloatordouble, isdouble, next;
  90 
  91     __ testl(c_rarg3, 1 << (i*2));      // Float or Double?
  92     __ jcc(Assembler::notZero, isfloatordouble);
  93 
  94     // Do Int register here
  95     switch ( i ) {
  96       case 0:
  97         __ movl(rscratch1, Address(rbx, Method::access_flags_offset()));
  98         __ testl(rscratch1, JVM_ACC_STATIC);
  99         __ cmovptr(Assembler::zero, c_rarg1, Address(rsp, 0));
 100         break;
 101       case 1:
 102         __ movptr(c_rarg2, Address(rsp, wordSize));
 103         break;
 104       case 2:
 105         __ movptr(c_rarg3, Address(rsp, 2 * wordSize));
 106         break;
 107       default:
 108         break;
 109     }
 110 
 111     __ jmp (next);
 112 
 113     __ bind(isfloatordouble);
 114     __ testl(c_rarg3, 1 << ((i*2)+1));     // Double?
 115     __ jcc(Assembler::notZero, isdouble);
 116 
 117 // Do Float Here
 118     __ movflt(floatreg, Address(rsp, i * wordSize));
 119     __ jmp(next);
 120 
 121 // Do Double here
 122     __ bind(isdouble);
 123     __ movdbl(floatreg, Address(rsp, i * wordSize));
 124 
 125     __ bind(next);
 126   }
 127 
 128 
 129   // restore rsp
 130   __ addptr(rsp, 4 * wordSize);
 131 
 132   __ ret(0);
 133 
 134   return entry;
 135 }
 136 #else
 137 address AbstractInterpreterGenerator::generate_slow_signature_handler() {
 138   address entry = __ pc();
 139 
 140   // rbx: method
 141   // r14: pointer to locals
 142   // c_rarg3: first stack arg - wordSize
 143   __ mov(c_rarg3, rsp);
 144   // adjust rsp
 145   __ subptr(rsp, 14 * wordSize);
 146   __ call_VM(noreg,
 147              CAST_FROM_FN_PTR(address,
 148                               InterpreterRuntime::slow_signature_handler),
 149              rbx, r14, c_rarg3);
 150 
 151   // rax: result handler
 152 
 153   // Stack layout:
 154   // rsp: 5 integer args (if static first is unused)
 155   //      1 float/double identifiers
 156   //      8 double args
 157   //        return address
 158   //        stack args
 159   //        garbage
 160   //        expression stack bottom
 161   //        bcp (NULL)
 162   //        ...
 163 
 164   // Do FP first so we can use c_rarg3 as temp
 165   __ movl(c_rarg3, Address(rsp, 5 * wordSize)); // float/double identifiers
 166 
 167   for (int i = 0; i < Argument::n_float_register_parameters_c; i++) {
 168     const XMMRegister r = as_XMMRegister(i);
 169 
 170     Label d, done;
 171 
 172     __ testl(c_rarg3, 1 << i);
 173     __ jcc(Assembler::notZero, d);
 174     __ movflt(r, Address(rsp, (6 + i) * wordSize));
 175     __ jmp(done);
 176     __ bind(d);
 177     __ movdbl(r, Address(rsp, (6 + i) * wordSize));
 178     __ bind(done);
 179   }
 180 
 181   // Now handle integrals.  Only do c_rarg1 if not static.
 182   __ movl(c_rarg3, Address(rbx, Method::access_flags_offset()));
 183   __ testl(c_rarg3, JVM_ACC_STATIC);
 184   __ cmovptr(Assembler::zero, c_rarg1, Address(rsp, 0));
 185 
 186   __ movptr(c_rarg2, Address(rsp, wordSize));
 187   __ movptr(c_rarg3, Address(rsp, 2 * wordSize));
 188   __ movptr(c_rarg4, Address(rsp, 3 * wordSize));
 189   __ movptr(c_rarg5, Address(rsp, 4 * wordSize));
 190 
 191   // restore rsp
 192   __ addptr(rsp, 14 * wordSize);
 193 
 194   __ ret(0);
 195 
 196   return entry;
 197 }
 198 #endif
 199 
 200 
 201 //
 202 // Various method entries
 203 //
 204 
 205 address InterpreterGenerator::generate_math_entry(AbstractInterpreter::MethodKind kind) {
 206 
 207   // rbx,: Method*
 208   // rcx: scratrch
 209   // r13: sender sp
 210 
 211   if (!InlineIntrinsics) return NULL; // Generate a vanilla entry
 212 
 213   address entry_point = __ pc();
 214 
 215   // These don't need a safepoint check because they aren't virtually
 216   // callable. We won't enter these intrinsics from compiled code.
 217   // If in the future we added an intrinsic which was virtually callable
 218   // we'd have to worry about how to safepoint so that this code is used.
 219 
 220   // mathematical functions inlined by compiler
 221   // (interpreter must provide identical implementation
 222   // in order to avoid monotonicity bugs when switching
 223   // from interpreter to compiler in the middle of some
 224   // computation)
 225   //
 226   // stack: [ ret adr ] <-- rsp
 227   //        [ lo(arg) ]
 228   //        [ hi(arg) ]
 229   //
 230 
 231   // Note: For JDK 1.2 StrictMath doesn't exist and Math.sin/cos/sqrt are
 232   //       native methods. Interpreter::method_kind(...) does a check for
 233   //       native methods first before checking for intrinsic methods and
 234   //       thus will never select this entry point. Make sure it is not
 235   //       called accidentally since the SharedRuntime entry points will
 236   //       not work for JDK 1.2.
 237   //
 238   // We no longer need to check for JDK 1.2 since it's EOL'ed.
 239   // The following check existed in pre 1.6 implementation,
 240   //    if (Universe::is_jdk12x_version()) {
 241   //      __ should_not_reach_here();
 242   //    }
 243   // Universe::is_jdk12x_version() always returns false since
 244   // the JDK version is not yet determined when this method is called.
 245   // This method is called during interpreter_init() whereas
 246   // JDK version is only determined when universe2_init() is called.
 247 
 248   // Note: For JDK 1.3 StrictMath exists and Math.sin/cos/sqrt are
 249   //       java methods.  Interpreter::method_kind(...) will select
 250   //       this entry point for the corresponding methods in JDK 1.3.
 251   // get argument
 252 
 253   if (kind == Interpreter::java_lang_math_sqrt) {
 254     __ sqrtsd(xmm0, Address(rsp, wordSize));
 255   } else if (kind == Interpreter::java_lang_math_exp) {
 256     __ movdbl(xmm0, Address(rsp, wordSize));
 257     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, StubRoutines::dexp())));
 258   } else {
 259     __ fld_d(Address(rsp, wordSize));
 260     switch (kind) {
 261       case Interpreter::java_lang_math_sin :
 262           __ trigfunc('s');
 263           break;
 264       case Interpreter::java_lang_math_cos :
 265           __ trigfunc('c');
 266           break;
 267       case Interpreter::java_lang_math_tan :
 268           __ trigfunc('t');
 269           break;
 270       case Interpreter::java_lang_math_abs:
 271           __ fabs();
 272           break;
 273       case Interpreter::java_lang_math_log:
 274           __ flog();
 275           break;
 276       case Interpreter::java_lang_math_log10:
 277           __ flog10();
 278           break;
 279       case Interpreter::java_lang_math_pow:
 280           __ fld_d(Address(rsp, 3*wordSize)); // second argument (one
 281                                               // empty stack slot)
 282           __ pow_with_fallback(0);
 283           break;
 284       default                              :
 285           ShouldNotReachHere();
 286     }
 287 
 288     // return double result in xmm0 for interpreter and compilers.
 289     __ subptr(rsp, 2*wordSize);
 290     // Round to 64bit precision
 291     __ fstp_d(Address(rsp, 0));
 292     __ movdbl(xmm0, Address(rsp, 0));
 293     __ addptr(rsp, 2*wordSize);
 294   }
 295 
 296 
 297   __ pop(rax);
 298   __ mov(rsp, r13);
 299   __ jmp(rax);
 300 
 301   return entry_point;
 302 }
 303 
 304 void Deoptimization::unwind_callee_save_values(frame* f, vframeArray* vframe_array) {
 305 
 306   // This code is sort of the equivalent of C2IAdapter::setup_stack_frame back in
 307   // the days we had adapter frames. When we deoptimize a situation where a
 308   // compiled caller calls a compiled caller will have registers it expects
 309   // to survive the call to the callee. If we deoptimize the callee the only
 310   // way we can restore these registers is to have the oldest interpreter
 311   // frame that we create restore these values. That is what this routine
 312   // will accomplish.
 313 
 314   // At the moment we have modified c2 to not have any callee save registers
 315   // so this problem does not exist and this routine is just a place holder.
 316 
 317   assert(f->is_interpreted_frame(), "must be interpreted");
 318 }