1 /*
   2  * Copyright (c) 1997, 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 
  53 
  54 // Generation of Interpreter
  55 //
  56 // The InterpreterGenerator generates the interpreter into Interpreter::_code.
  57 
  58 
  59 #define __ _masm->
  60 
  61 
  62 //----------------------------------------------------------------------------------------------------
  63 
  64 
  65 
  66 
  67 int AbstractInterpreter::BasicType_as_index(BasicType type) {
  68   int i = 0;
  69   switch (type) {
  70     case T_BOOLEAN: i = 0; break;
  71     case T_CHAR   : i = 1; break;
  72     case T_BYTE   : i = 2; break;
  73     case T_SHORT  : i = 3; break;
  74     case T_INT    : i = 4; break;
  75     case T_LONG   : i = 5; break;
  76     case T_VOID   : i = 6; break;
  77     case T_FLOAT  : i = 7; break;
  78     case T_DOUBLE : i = 8; break;
  79     case T_OBJECT : i = 9; break;
  80     case T_ARRAY  : i = 9; break;
  81     default       : ShouldNotReachHere();
  82   }
  83   assert(0 <= i && i < AbstractInterpreter::number_of_result_handlers, "index out of bounds");
  84   return i;
  85 }
  86 
  87 
  88 #ifndef _LP64
  89 address AbstractInterpreterGenerator::generate_slow_signature_handler() {
  90   address entry = __ pc();
  91   Argument argv(0, true);
  92 
  93   // We are in the jni transition frame. Save the last_java_frame corresponding to the
  94   // outer interpreter frame
  95   //
  96   __ set_last_Java_frame(FP, noreg);
  97   // make sure the interpreter frame we've pushed has a valid return pc
  98   __ mov(O7, I7);
  99   __ mov(Lmethod, G3_scratch);
 100   __ mov(Llocals, G4_scratch);
 101   __ save_frame(0);
 102   __ mov(G2_thread, L7_thread_cache);
 103   __ add(argv.address_in_frame(), O3);
 104   __ mov(G2_thread, O0);
 105   __ mov(G3_scratch, O1);
 106   __ call(CAST_FROM_FN_PTR(address, InterpreterRuntime::slow_signature_handler), relocInfo::runtime_call_type);
 107   __ delayed()->mov(G4_scratch, O2);
 108   __ mov(L7_thread_cache, G2_thread);
 109   __ reset_last_Java_frame();
 110 
 111   // load the register arguments (the C code packed them as varargs)
 112   for (Argument ldarg = argv.successor(); ldarg.is_register(); ldarg = ldarg.successor()) {
 113       __ ld_ptr(ldarg.address_in_frame(), ldarg.as_register());
 114   }
 115   __ ret();
 116   __ delayed()->
 117      restore(O0, 0, Lscratch);  // caller's Lscratch gets the result handler
 118   return entry;
 119 }
 120 
 121 
 122 #else
 123 // LP64 passes floating point arguments in F1, F3, F5, etc. instead of
 124 // O0, O1, O2 etc..
 125 // Doubles are passed in D0, D2, D4
 126 // We store the signature of the first 16 arguments in the first argument
 127 // slot because it will be overwritten prior to calling the native
 128 // function, with the pointer to the JNIEnv.
 129 // If LP64 there can be up to 16 floating point arguments in registers
 130 // or 6 integer registers.
 131 address AbstractInterpreterGenerator::generate_slow_signature_handler() {
 132 
 133   enum {
 134     non_float  = 0,
 135     float_sig  = 1,
 136     double_sig = 2,
 137     sig_mask   = 3
 138   };
 139 
 140   address entry = __ pc();
 141   Argument argv(0, true);
 142 
 143   // We are in the jni transition frame. Save the last_java_frame corresponding to the
 144   // outer interpreter frame
 145   //
 146   __ set_last_Java_frame(FP, noreg);
 147   // make sure the interpreter frame we've pushed has a valid return pc
 148   __ mov(O7, I7);
 149   __ mov(Lmethod, G3_scratch);
 150   __ mov(Llocals, G4_scratch);
 151   __ save_frame(0);
 152   __ mov(G2_thread, L7_thread_cache);
 153   __ add(argv.address_in_frame(), O3);
 154   __ mov(G2_thread, O0);
 155   __ mov(G3_scratch, O1);
 156   __ call(CAST_FROM_FN_PTR(address, InterpreterRuntime::slow_signature_handler), relocInfo::runtime_call_type);
 157   __ delayed()->mov(G4_scratch, O2);
 158   __ mov(L7_thread_cache, G2_thread);
 159   __ reset_last_Java_frame();
 160 
 161 
 162   // load the register arguments (the C code packed them as varargs)
 163   Address Sig = argv.address_in_frame();        // Argument 0 holds the signature
 164   __ ld_ptr( Sig, G3_scratch );                   // Get register argument signature word into G3_scratch
 165   __ mov( G3_scratch, G4_scratch);
 166   __ srl( G4_scratch, 2, G4_scratch);             // Skip Arg 0
 167   Label done;
 168   for (Argument ldarg = argv.successor(); ldarg.is_float_register(); ldarg = ldarg.successor()) {
 169     Label NonFloatArg;
 170     Label LoadFloatArg;
 171     Label LoadDoubleArg;
 172     Label NextArg;
 173     Address a = ldarg.address_in_frame();
 174     __ andcc(G4_scratch, sig_mask, G3_scratch);
 175     __ br(Assembler::zero, false, Assembler::pt, NonFloatArg);
 176     __ delayed()->nop();
 177 
 178     __ cmp(G3_scratch, float_sig );
 179     __ br(Assembler::equal, false, Assembler::pt, LoadFloatArg);
 180     __ delayed()->nop();
 181 
 182     __ cmp(G3_scratch, double_sig );
 183     __ br(Assembler::equal, false, Assembler::pt, LoadDoubleArg);
 184     __ delayed()->nop();
 185 
 186     __ bind(NonFloatArg);
 187     // There are only 6 integer register arguments!
 188     if ( ldarg.is_register() )
 189       __ ld_ptr(ldarg.address_in_frame(), ldarg.as_register());
 190     else {
 191     // Optimization, see if there are any more args and get out prior to checking
 192     // all 16 float registers.  My guess is that this is rare.
 193     // If is_register is false, then we are done the first six integer args.
 194       __ br_null_short(G4_scratch, Assembler::pt, done);
 195     }
 196     __ ba(NextArg);
 197     __ delayed()->srl( G4_scratch, 2, G4_scratch );
 198 
 199     __ bind(LoadFloatArg);
 200     __ ldf( FloatRegisterImpl::S, a, ldarg.as_float_register(), 4);
 201     __ ba(NextArg);
 202     __ delayed()->srl( G4_scratch, 2, G4_scratch );
 203 
 204     __ bind(LoadDoubleArg);
 205     __ ldf( FloatRegisterImpl::D, a, ldarg.as_double_register() );
 206     __ ba(NextArg);
 207     __ delayed()->srl( G4_scratch, 2, G4_scratch );
 208 
 209     __ bind(NextArg);
 210 
 211   }
 212 
 213   __ bind(done);
 214   __ ret();
 215   __ delayed()->
 216      restore(O0, 0, Lscratch);  // caller's Lscratch gets the result handler
 217   return entry;
 218 }
 219 #endif
 220 
 221 void InterpreterGenerator::generate_counter_overflow(Label& Lcontinue) {
 222 
 223   // Generate code to initiate compilation on the counter overflow.
 224 
 225   // InterpreterRuntime::frequency_counter_overflow takes two arguments,
 226   // the first indicates if the counter overflow occurs at a backwards branch (NULL bcp)
 227   // and the second is only used when the first is true.  We pass zero for both.
 228   // The call returns the address of the verified entry point for the method or NULL
 229   // if the compilation did not complete (either went background or bailed out).
 230   __ set((int)false, O2);
 231   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow), O2, O2, true);
 232   // returns verified_entry_point or NULL
 233   // we ignore it in any case
 234   __ ba_short(Lcontinue);
 235 
 236 }
 237 
 238 
 239 // End of helpers
 240 
 241 // Various method entries
 242 
 243 // Abstract method entry
 244 // Attempt to execute abstract method. Throw exception
 245 //
 246 address InterpreterGenerator::generate_abstract_entry(void) {
 247   address entry = __ pc();
 248   // abstract method entry
 249   // throw exception
 250   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_AbstractMethodError));
 251   // the call_VM checks for exception, so we should never return here.
 252   __ should_not_reach_here();
 253   return entry;
 254 
 255 }
 256 
 257 
 258 //----------------------------------------------------------------------------------------------------
 259 // Entry points & stack frame layout
 260 //
 261 // Here we generate the various kind of entries into the interpreter.
 262 // The two main entry type are generic bytecode methods and native call method.
 263 // These both come in synchronized and non-synchronized versions but the
 264 // frame layout they create is very similar. The other method entry
 265 // types are really just special purpose entries that are really entry
 266 // and interpretation all in one. These are for trivial methods like
 267 // accessor, empty, or special math methods.
 268 //
 269 // When control flow reaches any of the entry types for the interpreter
 270 // the following holds ->
 271 //
 272 // C2 Calling Conventions:
 273 //
 274 // The entry code below assumes that the following registers are set
 275 // when coming in:
 276 //    G5_method: holds the Method* of the method to call
 277 //    Lesp:    points to the TOS of the callers expression stack
 278 //             after having pushed all the parameters
 279 //
 280 // The entry code does the following to setup an interpreter frame
 281 //   pop parameters from the callers stack by adjusting Lesp
 282 //   set O0 to Lesp
 283 //   compute X = (max_locals - num_parameters)
 284 //   bump SP up by X to accomadate the extra locals
 285 //   compute X = max_expression_stack
 286 //               + vm_local_words
 287 //               + 16 words of register save area
 288 //   save frame doing a save sp, -X, sp growing towards lower addresses
 289 //   set Lbcp, Lmethod, LcpoolCache
 290 //   set Llocals to i0
 291 //   set Lmonitors to FP - rounded_vm_local_words
 292 //   set Lesp to Lmonitors - 4
 293 //
 294 //  The frame has now been setup to do the rest of the entry code
 295 
 296 // Try this optimization:  Most method entries could live in a
 297 // "one size fits all" stack frame without all the dynamic size
 298 // calculations.  It might be profitable to do all this calculation
 299 // statically and approximately for "small enough" methods.
 300 
 301 //-----------------------------------------------------------------------------------------------
 302 
 303 // C1 Calling conventions
 304 //
 305 // Upon method entry, the following registers are setup:
 306 //
 307 // g2 G2_thread: current thread
 308 // g5 G5_method: method to activate
 309 // g4 Gargs  : pointer to last argument
 310 //
 311 //
 312 // Stack:
 313 //
 314 // +---------------+ <--- sp
 315 // |               |
 316 // : reg save area :
 317 // |               |
 318 // +---------------+ <--- sp + 0x40
 319 // |               |
 320 // : extra 7 slots :      note: these slots are not really needed for the interpreter (fix later)
 321 // |               |
 322 // +---------------+ <--- sp + 0x5c
 323 // |               |
 324 // :     free      :
 325 // |               |
 326 // +---------------+ <--- Gargs
 327 // |               |
 328 // :   arguments   :
 329 // |               |
 330 // +---------------+
 331 // |               |
 332 //
 333 //
 334 //
 335 // AFTER FRAME HAS BEEN SETUP for method interpretation the stack looks like:
 336 //
 337 // +---------------+ <--- sp
 338 // |               |
 339 // : reg save area :
 340 // |               |
 341 // +---------------+ <--- sp + 0x40
 342 // |               |
 343 // : extra 7 slots :      note: these slots are not really needed for the interpreter (fix later)
 344 // |               |
 345 // +---------------+ <--- sp + 0x5c
 346 // |               |
 347 // :               :
 348 // |               | <--- Lesp
 349 // +---------------+ <--- Lmonitors (fp - 0x18)
 350 // |   VM locals   |
 351 // +---------------+ <--- fp
 352 // |               |
 353 // : reg save area :
 354 // |               |
 355 // +---------------+ <--- fp + 0x40
 356 // |               |
 357 // : extra 7 slots :      note: these slots are not really needed for the interpreter (fix later)
 358 // |               |
 359 // +---------------+ <--- fp + 0x5c
 360 // |               |
 361 // :     free      :
 362 // |               |
 363 // +---------------+
 364 // |               |
 365 // : nonarg locals :
 366 // |               |
 367 // +---------------+
 368 // |               |
 369 // :   arguments   :
 370 // |               | <--- Llocals
 371 // +---------------+ <--- Gargs
 372 // |               |
 373 
 374 address AbstractInterpreterGenerator::generate_method_entry(AbstractInterpreter::MethodKind kind) {
 375   // determine code generation flags
 376   bool synchronized = false;
 377   address entry_point = NULL;
 378 
 379   switch (kind) {
 380     case Interpreter::zerolocals             :                                                                             break;
 381     case Interpreter::zerolocals_synchronized: synchronized = true;                                                        break;
 382     case Interpreter::native                 : entry_point = ((InterpreterGenerator*)this)->generate_native_entry(false);  break;
 383     case Interpreter::native_synchronized    : entry_point = ((InterpreterGenerator*)this)->generate_native_entry(true);   break;
 384     case Interpreter::empty                  : entry_point = ((InterpreterGenerator*)this)->generate_empty_entry();        break;
 385     case Interpreter::accessor               : entry_point = ((InterpreterGenerator*)this)->generate_accessor_entry();     break;
 386     case Interpreter::abstract               : entry_point = ((InterpreterGenerator*)this)->generate_abstract_entry();     break;
 387 
 388     case Interpreter::java_lang_math_sin     :                                                                             break;
 389     case Interpreter::java_lang_math_cos     :                                                                             break;
 390     case Interpreter::java_lang_math_tan     :                                                                             break;
 391     case Interpreter::java_lang_math_sqrt    :                                                                             break;
 392     case Interpreter::java_lang_math_abs     :                                                                             break;
 393     case Interpreter::java_lang_math_log     :                                                                             break;
 394     case Interpreter::java_lang_math_log10   :                                                                             break;
 395     case Interpreter::java_lang_math_pow     :                                                                             break;
 396     case Interpreter::java_lang_math_exp     :                                                                             break;
 397     case Interpreter::java_lang_ref_reference_get
 398                                              : entry_point = ((InterpreterGenerator*)this)->generate_Reference_get_entry(); break;
 399     default:
 400       fatal(err_msg("unexpected method kind: %d", kind));
 401       break;
 402   }
 403 
 404   if (entry_point) return entry_point;
 405 
 406   return ((InterpreterGenerator*)this)->generate_normal_entry(synchronized);
 407 }
 408 
 409 
 410 bool AbstractInterpreter::can_be_compiled(methodHandle m) {
 411   // No special entry points that preclude compilation
 412   return true;
 413 }
 414 
 415 void Deoptimization::unwind_callee_save_values(frame* f, vframeArray* vframe_array) {
 416 
 417   // This code is sort of the equivalent of C2IAdapter::setup_stack_frame back in
 418   // the days we had adapter frames. When we deoptimize a situation where a
 419   // compiled caller calls a compiled caller will have registers it expects
 420   // to survive the call to the callee. If we deoptimize the callee the only
 421   // way we can restore these registers is to have the oldest interpreter
 422   // frame that we create restore these values. That is what this routine
 423   // will accomplish.
 424 
 425   // At the moment we have modified c2 to not have any callee save registers
 426   // so this problem does not exist and this routine is just a place holder.
 427 
 428   assert(f->is_interpreted_frame(), "must be interpreted");
 429 }
 430 
 431 
 432 //----------------------------------------------------------------------------------------------------
 433 // Exceptions