1 /*
   2  * Copyright (c) 2013, Red Hat Inc.
   3  * Copyright (c) 2003, 2011, Oracle and/or its affiliates.
   4  * All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  *
  25  */
  26 
  27 #include "precompiled.hpp"
  28 #include "asm/macroAssembler.hpp"
  29 #include "interpreter/bytecodeHistogram.hpp"
  30 #include "interpreter/interpreter.hpp"
  31 #include "interpreter/interpreterGenerator.hpp"
  32 #include "interpreter/interpreterRuntime.hpp"
  33 #include "interpreter/templateTable.hpp"
  34 #include "oops/arrayOop.hpp"
  35 #include "oops/methodData.hpp"
  36 #include "oops/method.hpp"
  37 #include "oops/oop.inline.hpp"
  38 #include "prims/jvmtiExport.hpp"
  39 #include "prims/jvmtiThreadState.hpp"
  40 #include "prims/methodHandles.hpp"
  41 #include "runtime/arguments.hpp"
  42 #include "runtime/deoptimization.hpp"
  43 #include "runtime/frame.inline.hpp"
  44 #include "runtime/sharedRuntime.hpp"
  45 #include "runtime/stubRoutines.hpp"
  46 #include "runtime/synchronizer.hpp"
  47 #include "runtime/timer.hpp"
  48 #include "runtime/vframeArray.hpp"
  49 #include "utilities/debug.hpp"
  50 #ifdef COMPILER1
  51 #include "c1/c1_Runtime1.hpp"
  52 #endif
  53 
  54 #define __ _masm->
  55 
  56 
  57 address AbstractInterpreterGenerator::generate_slow_signature_handler() {
  58   address entry = __ pc();
  59 
  60   __ andr(esp, esp, -16);
  61   __ mov(c_rarg3, esp);
  62   // rmethod
  63   // rlocals
  64   // c_rarg3: first stack arg - wordSize
  65 
  66   // adjust sp
  67   __ sub(sp, c_rarg3, 18 * wordSize);
  68   __ str(lr, Address(__ pre(sp, -2 * wordSize)));
  69   __ call_VM(noreg,
  70              CAST_FROM_FN_PTR(address,
  71                               InterpreterRuntime::slow_signature_handler),
  72              rmethod, rlocals, c_rarg3);
  73 
  74   // r0: result handler
  75 
  76   // Stack layout:
  77   // rsp: return address           <- sp
  78   //      1 garbage
  79   //      8 integer args (if static first is unused)
  80   //      1 float/double identifiers
  81   //      8 double args
  82   //        stack args              <- esp
  83   //        garbage
  84   //        expression stack bottom
  85   //        bcp (NULL)
  86   //        ...
  87 
  88   // Restore LR
  89   __ ldr(lr, Address(__ post(sp, 2 * wordSize)));
  90 
  91   // Do FP first so we can use c_rarg3 as temp
  92   __ ldrw(c_rarg3, Address(sp, 9 * wordSize)); // float/double identifiers
  93 
  94   for (int i = 0; i < Argument::n_float_register_parameters_c; i++) {
  95     const FloatRegister r = as_FloatRegister(i);
  96 
  97     Label d, done;
  98 
  99     __ tbnz(c_rarg3, i, d);
 100     __ ldrs(r, Address(sp, (10 + i) * wordSize));
 101     __ b(done);
 102     __ bind(d);
 103     __ ldrd(r, Address(sp, (10 + i) * wordSize));
 104     __ bind(done);
 105   }
 106 
 107   // c_rarg0 contains the result from the call of
 108   // InterpreterRuntime::slow_signature_handler so we don't touch it
 109   // here.  It will be loaded with the JNIEnv* later.
 110   __ ldr(c_rarg1, Address(sp, 1 * wordSize));
 111   for (int i = c_rarg2->encoding(); i <= c_rarg7->encoding(); i += 2) {
 112     Register rm = as_Register(i), rn = as_Register(i+1);
 113     __ ldp(rm, rn, Address(sp, i * wordSize));
 114   }
 115 
 116   __ add(sp, sp, 18 * wordSize);
 117   __ ret(lr);
 118 
 119   return entry;
 120 }
 121 
 122 
 123 //
 124 // Various method entries
 125 //
 126 
 127 address InterpreterGenerator::generate_math_entry(AbstractInterpreter::MethodKind kind) {
 128   // rmethod: Method*
 129   // r13: sender sp
 130   // esp: args
 131 
 132   if (!InlineIntrinsics) return NULL; // Generate a vanilla entry
 133 
 134   // These don't need a safepoint check because they aren't virtually
 135   // callable. We won't enter these intrinsics from compiled code.
 136   // If in the future we added an intrinsic which was virtually callable
 137   // we'd have to worry about how to safepoint so that this code is used.
 138 
 139   // mathematical functions inlined by compiler
 140   // (interpreter must provide identical implementation
 141   // in order to avoid monotonicity bugs when switching
 142   // from interpreter to compiler in the middle of some
 143   // computation)
 144   //
 145   // stack:
 146   //        [ arg ] <-- esp
 147   //        [ arg ]
 148   // retaddr in lr
 149 
 150   address entry_point = NULL;
 151   Register continuation = lr;
 152   switch (kind) {
 153   case Interpreter::java_lang_math_abs:
 154     entry_point = __ pc();
 155     __ ldrd(v0, Address(esp));
 156     __ fabsd(v0, v0);
 157     __ mov(sp, r13); // Restore caller's SP
 158     break;
 159   case Interpreter::java_lang_math_sqrt:
 160     entry_point = __ pc();
 161     __ ldrd(v0, Address(esp));
 162     __ fsqrtd(v0, v0);
 163     __ mov(sp, r13);
 164     break;
 165   case Interpreter::java_lang_math_sin :
 166   case Interpreter::java_lang_math_cos :
 167   case Interpreter::java_lang_math_tan :
 168   case Interpreter::java_lang_math_log :
 169   case Interpreter::java_lang_math_log10 :
 170   case Interpreter::java_lang_math_exp :
 171     entry_point = __ pc();
 172     __ ldrd(v0, Address(esp));
 173     __ mov(sp, r13);
 174     __ mov(r19, lr);
 175     continuation = r19;  // The first callee-saved register
 176     generate_transcendental_entry(kind, 1);
 177     break;
 178   case Interpreter::java_lang_math_pow :
 179     entry_point = __ pc();
 180     __ mov(r19, lr);
 181     continuation = r19;
 182     __ ldrd(v0, Address(esp, 2 * Interpreter::stackElementSize));
 183     __ ldrd(v1, Address(esp));
 184     __ mov(sp, r13);
 185     generate_transcendental_entry(kind, 2);
 186     break;
 187   default:
 188     ;
 189   }
 190   if (entry_point) {
 191     __ br(continuation);
 192   }
 193 
 194   return entry_point;
 195 }
 196 
 197   // double trigonometrics and transcendentals
 198   // static jdouble dsin(jdouble x);
 199   // static jdouble dcos(jdouble x);
 200   // static jdouble dtan(jdouble x);
 201   // static jdouble dlog(jdouble x);
 202   // static jdouble dlog10(jdouble x);
 203   // static jdouble dexp(jdouble x);
 204   // static jdouble dpow(jdouble x, jdouble y);
 205 
 206 void InterpreterGenerator::generate_transcendental_entry(AbstractInterpreter::MethodKind kind, int fpargs) {
 207   address fn;
 208   switch (kind) {
 209   case Interpreter::java_lang_math_sin :
 210     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dsin);
 211     break;
 212   case Interpreter::java_lang_math_cos :
 213     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dcos);
 214     break;
 215   case Interpreter::java_lang_math_tan :
 216     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dtan);
 217     break;
 218   case Interpreter::java_lang_math_log :
 219     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dlog);
 220     break;
 221   case Interpreter::java_lang_math_log10 :
 222     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dlog10);
 223     break;
 224   case Interpreter::java_lang_math_exp :
 225     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dexp);
 226     break;
 227   case Interpreter::java_lang_math_pow :
 228     fn = CAST_FROM_FN_PTR(address, SharedRuntime::dpow);
 229     break;
 230   default:
 231     ShouldNotReachHere();
 232   }
 233   __ mov(rscratch1, fn);
 234   __ blr(rscratch1);
 235 }
 236 
 237 // Abstract method entry
 238 // Attempt to execute abstract method. Throw exception
 239 address InterpreterGenerator::generate_abstract_entry(void) {
 240   // rmethod: Method*
 241   // r13: sender SP
 242 
 243   address entry_point = __ pc();
 244 
 245   // abstract method entry
 246 
 247   //  pop return address, reset last_sp to NULL
 248   __ empty_expression_stack();
 249   __ restore_bcp();      // bcp must be correct for exception handler   (was destroyed)
 250   __ restore_locals();   // make sure locals pointer is correct as well (was destroyed)
 251 
 252   // throw exception
 253   __ call_VM(noreg, CAST_FROM_FN_PTR(address,
 254                              InterpreterRuntime::throw_AbstractMethodError));
 255   // the call_VM checks for exception, so we should never return here.
 256   __ should_not_reach_here();
 257 
 258   return entry_point;
 259 }
 260 
 261 
 262 // Empty method, generate a very fast return.
 263 
 264 address InterpreterGenerator::generate_empty_entry(void) {
 265   // rmethod: Method*
 266   // r13: sender sp must set sp to this value on return
 267 
 268   if (!UseFastEmptyMethods) {
 269     return NULL;
 270   }
 271 
 272   address entry_point = __ pc();
 273 
 274   // If we need a safepoint check, generate full interpreter entry.
 275   Label slow_path;
 276   {
 277     unsigned long offset;
 278     assert(SafepointSynchronize::_not_synchronized == 0,
 279            "SafepointSynchronize::_not_synchronized");
 280     __ adrp(rscratch2, SafepointSynchronize::address_of_state(), offset);
 281     __ ldrw(rscratch2, Address(rscratch2, offset));
 282     __ cbnz(rscratch2, slow_path);
 283   }
 284 
 285   // do nothing for empty methods (do not even increment invocation counter)
 286   // Code: _return
 287   // _return
 288   // return w/o popping parameters
 289   __ mov(sp, r13); // Restore caller's SP
 290   __ br(lr);
 291 
 292   __ bind(slow_path);
 293   (void) generate_normal_entry(false);
 294   return entry_point;
 295 
 296 }
 297 
 298 void Deoptimization::unwind_callee_save_values(frame* f, vframeArray* vframe_array) {
 299 
 300   // This code is sort of the equivalent of C2IAdapter::setup_stack_frame back in
 301   // the days we had adapter frames. When we deoptimize a situation where a
 302   // compiled caller calls a compiled caller will have registers it expects
 303   // to survive the call to the callee. If we deoptimize the callee the only
 304   // way we can restore these registers is to have the oldest interpreter
 305   // frame that we create restore these values. That is what this routine
 306   // will accomplish.
 307 
 308   // At the moment we have modified c2 to not have any callee save registers
 309   // so this problem does not exist and this routine is just a place holder.
 310 
 311   assert(f->is_interpreted_frame(), "must be interpreted");
 312 }