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