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