1 /*
   2  * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2013, 2015 SAP AG. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #ifndef CC_INTERP
  28 #include "asm/macroAssembler.inline.hpp"
  29 #include "interpreter/bytecodeHistogram.hpp"
  30 #include "interpreter/interpreter.hpp"
  31 #include "interpreter/interpreterGenerator.hpp"
  32 #include "interpreter/interpreterRuntime.hpp"
  33 #include "interpreter/interp_masm.hpp"
  34 #include "interpreter/templateTable.hpp"
  35 #include "oops/arrayOop.hpp"
  36 #include "oops/methodData.hpp"
  37 #include "oops/method.hpp"
  38 #include "oops/oop.inline.hpp"
  39 #include "prims/jvmtiExport.hpp"
  40 #include "prims/jvmtiThreadState.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 #include "utilities/macros.hpp"
  51 
  52 #undef __
  53 #define __ _masm->
  54 
  55 #ifdef PRODUCT
  56 #define BLOCK_COMMENT(str) /* nothing */
  57 #else
  58 #define BLOCK_COMMENT(str) __ block_comment(str)
  59 #endif
  60 
  61 #define BIND(label)        __ bind(label); BLOCK_COMMENT(#label ":")
  62 
  63 //-----------------------------------------------------------------------------
  64 
  65 // Actually we should never reach here since we do stack overflow checks before pushing any frame.
  66 address TemplateInterpreterGenerator::generate_StackOverflowError_handler() {
  67   address entry = __ pc();
  68   __ unimplemented("generate_StackOverflowError_handler");
  69   return entry;
  70 }
  71 
  72 address TemplateInterpreterGenerator::generate_ArrayIndexOutOfBounds_handler(const char* name) {
  73   address entry = __ pc();
  74   __ empty_expression_stack();
  75   __ load_const_optimized(R4_ARG2, (address) name);
  76   // Index is in R17_tos.
  77   __ mr(R5_ARG3, R17_tos);
  78   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_ArrayIndexOutOfBoundsException));
  79   return entry;
  80 }
  81 
  82 #if 0
  83 // Call special ClassCastException constructor taking object to cast
  84 // and target class as arguments.
  85 address TemplateInterpreterGenerator::generate_ClassCastException_verbose_handler() {
  86   address entry = __ pc();
  87 
  88   // Expression stack must be empty before entering the VM if an
  89   // exception happened.
  90   __ empty_expression_stack();
  91 
  92   // Thread will be loaded to R3_ARG1.
  93   // Target class oop is in register R5_ARG3 by convention!
  94   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_ClassCastException_verbose), R17_tos, R5_ARG3);
  95   // Above call must not return here since exception pending.
  96   DEBUG_ONLY(__ should_not_reach_here();)
  97   return entry;
  98 }
  99 #endif
 100 
 101 address TemplateInterpreterGenerator::generate_ClassCastException_handler() {
 102   address entry = __ pc();
 103   // Expression stack must be empty before entering the VM if an
 104   // exception happened.
 105   __ empty_expression_stack();
 106 
 107   // Load exception object.
 108   // Thread will be loaded to R3_ARG1.
 109   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::throw_ClassCastException), R17_tos);
 110 #ifdef ASSERT
 111   // Above call must not return here since exception pending.
 112   __ should_not_reach_here();
 113 #endif
 114   return entry;
 115 }
 116 
 117 address TemplateInterpreterGenerator::generate_exception_handler_common(const char* name, const char* message, bool pass_oop) {
 118   address entry = __ pc();
 119   //__ untested("generate_exception_handler_common");
 120   Register Rexception = R17_tos;
 121 
 122   // Expression stack must be empty before entering the VM if an exception happened.
 123   __ empty_expression_stack();
 124 
 125   __ load_const_optimized(R4_ARG2, (address) name, R11_scratch1);
 126   if (pass_oop) {
 127     __ mr(R5_ARG3, Rexception);
 128     __ call_VM(Rexception, CAST_FROM_FN_PTR(address, InterpreterRuntime::create_klass_exception), false);
 129   } else {
 130     __ load_const_optimized(R5_ARG3, (address) message, R11_scratch1);
 131     __ call_VM(Rexception, CAST_FROM_FN_PTR(address, InterpreterRuntime::create_exception), false);
 132   }
 133 
 134   // Throw exception.
 135   __ mr(R3_ARG1, Rexception);
 136   __ load_const_optimized(R11_scratch1, Interpreter::throw_exception_entry(), R12_scratch2);
 137   __ mtctr(R11_scratch1);
 138   __ bctr();
 139 
 140   return entry;
 141 }
 142 
 143 address TemplateInterpreterGenerator::generate_continuation_for(TosState state) {
 144   address entry = __ pc();
 145   __ unimplemented("generate_continuation_for");
 146   return entry;
 147 }
 148 
 149 // This entry is returned to when a call returns to the interpreter.
 150 // When we arrive here, we expect that the callee stack frame is already popped.
 151 address TemplateInterpreterGenerator::generate_return_entry_for(TosState state, int step, size_t index_size) {
 152   address entry = __ pc();
 153 
 154   // Move the value out of the return register back to the TOS cache of current frame.
 155   switch (state) {
 156     case ltos:
 157     case btos:
 158     case ctos:
 159     case stos:
 160     case atos:
 161     case itos: __ mr(R17_tos, R3_RET); break;   // RET -> TOS cache
 162     case ftos:
 163     case dtos: __ fmr(F15_ftos, F1_RET); break; // TOS cache -> GR_FRET
 164     case vtos: break;                           // Nothing to do, this was a void return.
 165     default  : ShouldNotReachHere();
 166   }
 167 
 168   __ restore_interpreter_state(R11_scratch1); // Sets R11_scratch1 = fp.
 169   __ ld(R12_scratch2, _ijava_state_neg(top_frame_sp), R11_scratch1);
 170   __ resize_frame_absolute(R12_scratch2, R11_scratch1, R0);
 171 
 172   // Compiled code destroys templateTableBase, reload.
 173   __ load_const_optimized(R25_templateTableBase, (address)Interpreter::dispatch_table((TosState)0), R12_scratch2);
 174 
 175   if (state == atos) {
 176     __ profile_return_type(R3_RET, R11_scratch1, R12_scratch2);
 177   }
 178 
 179   const Register cache = R11_scratch1;
 180   const Register size  = R12_scratch2;
 181   __ get_cache_and_index_at_bcp(cache, 1, index_size);
 182 
 183   // Get least significant byte of 64 bit value:
 184 #if defined(VM_LITTLE_ENDIAN)
 185   __ lbz(size, in_bytes(ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::flags_offset()), cache);
 186 #else
 187   __ lbz(size, in_bytes(ConstantPoolCache::base_offset() + ConstantPoolCacheEntry::flags_offset()) + 7, cache);
 188 #endif
 189   __ sldi(size, size, Interpreter::logStackElementSize);
 190   __ add(R15_esp, R15_esp, size);
 191   __ dispatch_next(state, step);
 192   return entry;
 193 }
 194 
 195 address TemplateInterpreterGenerator::generate_deopt_entry_for(TosState state, int step) {
 196   address entry = __ pc();
 197   // If state != vtos, we're returning from a native method, which put it's result
 198   // into the result register. So move the value out of the return register back
 199   // to the TOS cache of current frame.
 200 
 201   switch (state) {
 202     case ltos:
 203     case btos:
 204     case ctos:
 205     case stos:
 206     case atos:
 207     case itos: __ mr(R17_tos, R3_RET); break;   // GR_RET -> TOS cache
 208     case ftos:
 209     case dtos: __ fmr(F15_ftos, F1_RET); break; // TOS cache -> GR_FRET
 210     case vtos: break;                           // Nothing to do, this was a void return.
 211     default  : ShouldNotReachHere();
 212   }
 213 
 214   // Load LcpoolCache @@@ should be already set!
 215   __ get_constant_pool_cache(R27_constPoolCache);
 216 
 217   // Handle a pending exception, fall through if none.
 218   __ check_and_forward_exception(R11_scratch1, R12_scratch2);
 219 
 220   // Start executing bytecodes.
 221   __ dispatch_next(state, step);
 222 
 223   return entry;
 224 }
 225 
 226 // A result handler converts the native result into java format.
 227 // Use the shared code between c++ and template interpreter.
 228 address TemplateInterpreterGenerator::generate_result_handler_for(BasicType type) {
 229   return AbstractInterpreterGenerator::generate_result_handler_for(type);
 230 }
 231 
 232 address TemplateInterpreterGenerator::generate_safept_entry_for(TosState state, address runtime_entry) {
 233   address entry = __ pc();
 234 
 235   __ push(state);
 236   __ call_VM(noreg, runtime_entry);
 237   __ dispatch_via(vtos, Interpreter::_normal_table.table_for(vtos));
 238 
 239   return entry;
 240 }
 241 
 242 // Helpers for commoning out cases in the various type of method entries.
 243 
 244 // Increment invocation count & check for overflow.
 245 //
 246 // Note: checking for negative value instead of overflow
 247 //       so we have a 'sticky' overflow test.
 248 //
 249 void TemplateInterpreterGenerator::generate_counter_incr(Label* overflow, Label* profile_method, Label* profile_method_continue) {
 250   // Note: In tiered we increment either counters in method or in MDO depending if we're profiling or not.
 251   Register Rscratch1   = R11_scratch1;
 252   Register Rscratch2   = R12_scratch2;
 253   Register R3_counters = R3_ARG1;
 254   Label done;
 255 
 256   if (TieredCompilation) {
 257     const int increment = InvocationCounter::count_increment;
 258     const int mask = ((1 << Tier0InvokeNotifyFreqLog) - 1) << InvocationCounter::count_shift;
 259     Label no_mdo;
 260     if (ProfileInterpreter) {
 261       const Register Rmdo = Rscratch1;
 262       // If no method data exists, go to profile_continue.
 263       __ ld(Rmdo, in_bytes(Method::method_data_offset()), R19_method);
 264       __ cmpdi(CCR0, Rmdo, 0);
 265       __ beq(CCR0, no_mdo);
 266 
 267       // Increment backedge counter in the MDO.
 268       const int mdo_bc_offs = in_bytes(MethodData::backedge_counter_offset()) + in_bytes(InvocationCounter::counter_offset());
 269       __ lwz(Rscratch2, mdo_bc_offs, Rmdo);
 270       __ addi(Rscratch2, Rscratch2, increment);
 271       __ stw(Rscratch2, mdo_bc_offs, Rmdo);
 272       __ load_const_optimized(Rscratch1, mask, R0);
 273       __ and_(Rscratch1, Rscratch2, Rscratch1);
 274       __ bne(CCR0, done);
 275       __ b(*overflow);
 276     }
 277 
 278     // Increment counter in MethodCounters*.
 279     const int mo_bc_offs = in_bytes(MethodCounters::backedge_counter_offset()) + in_bytes(InvocationCounter::counter_offset());
 280     __ bind(no_mdo);
 281     __ get_method_counters(R19_method, R3_counters, done);
 282     __ lwz(Rscratch2, mo_bc_offs, R3_counters);
 283     __ addi(Rscratch2, Rscratch2, increment);
 284     __ stw(Rscratch2, mo_bc_offs, R3_counters);
 285     __ load_const_optimized(Rscratch1, mask, R0);
 286     __ and_(Rscratch1, Rscratch2, Rscratch1);
 287     __ beq(CCR0, *overflow);
 288 
 289     __ bind(done);
 290 
 291   } else {
 292 
 293     // Update standard invocation counters.
 294     Register Rsum_ivc_bec = R4_ARG2;
 295     __ get_method_counters(R19_method, R3_counters, done);
 296     __ increment_invocation_counter(R3_counters, Rsum_ivc_bec, R12_scratch2);
 297     // Increment interpreter invocation counter.
 298     if (ProfileInterpreter) {  // %%% Merge this into methodDataOop.
 299       __ lwz(R12_scratch2, in_bytes(MethodCounters::interpreter_invocation_counter_offset()), R3_counters);
 300       __ addi(R12_scratch2, R12_scratch2, 1);
 301       __ stw(R12_scratch2, in_bytes(MethodCounters::interpreter_invocation_counter_offset()), R3_counters);
 302     }
 303     // Check if we must create a method data obj.
 304     if (ProfileInterpreter && profile_method != NULL) {
 305       const Register profile_limit = Rscratch1;
 306       int pl_offs = __ load_const_optimized(profile_limit, &InvocationCounter::InterpreterProfileLimit, R0, true);
 307       __ lwz(profile_limit, pl_offs, profile_limit);
 308       // Test to see if we should create a method data oop.
 309       __ cmpw(CCR0, Rsum_ivc_bec, profile_limit);
 310       __ blt(CCR0, *profile_method_continue);
 311       // If no method data exists, go to profile_method.
 312       __ test_method_data_pointer(*profile_method);
 313     }
 314     // Finally check for counter overflow.
 315     if (overflow) {
 316       const Register invocation_limit = Rscratch1;
 317       int il_offs = __ load_const_optimized(invocation_limit, &InvocationCounter::InterpreterInvocationLimit, R0, true);
 318       __ lwz(invocation_limit, il_offs, invocation_limit);
 319       assert(4 == sizeof(InvocationCounter::InterpreterInvocationLimit), "unexpected field size");
 320       __ cmpw(CCR0, Rsum_ivc_bec, invocation_limit);
 321       __ bge(CCR0, *overflow);
 322     }
 323 
 324     __ bind(done);
 325   }
 326 }
 327 
 328 // Generate code to initiate compilation on invocation counter overflow.
 329 void TemplateInterpreterGenerator::generate_counter_overflow(Label& continue_entry) {
 330   // Generate code to initiate compilation on the counter overflow.
 331 
 332   // InterpreterRuntime::frequency_counter_overflow takes one arguments,
 333   // which indicates if the counter overflow occurs at a backwards branch (NULL bcp)
 334   // We pass zero in.
 335   // The call returns the address of the verified entry point for the method or NULL
 336   // if the compilation did not complete (either went background or bailed out).
 337   //
 338   // Unlike the C++ interpreter above: Check exceptions!
 339   // Assumption: Caller must set the flag "do_not_unlock_if_sychronized" if the monitor of a sync'ed
 340   // method has not yet been created. Thus, no unlocking of a non-existing monitor can occur.
 341 
 342   __ li(R4_ARG2, 0);
 343   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow), R4_ARG2, true);
 344 
 345   // Returns verified_entry_point or NULL.
 346   // We ignore it in any case.
 347   __ b(continue_entry);
 348 }
 349 
 350 void TemplateInterpreterGenerator::generate_stack_overflow_check(Register Rmem_frame_size, Register Rscratch1) {
 351   assert_different_registers(Rmem_frame_size, Rscratch1);
 352   __ generate_stack_overflow_check_with_compare_and_throw(Rmem_frame_size, Rscratch1);
 353 }
 354 
 355 void TemplateInterpreterGenerator::unlock_method(bool check_exceptions) {
 356   __ unlock_object(R26_monitor, check_exceptions);
 357 }
 358 
 359 // Lock the current method, interpreter register window must be set up!
 360 void TemplateInterpreterGenerator::lock_method(Register Rflags, Register Rscratch1, Register Rscratch2, bool flags_preloaded) {
 361   const Register Robj_to_lock = Rscratch2;
 362 
 363   {
 364     if (!flags_preloaded) {
 365       __ lwz(Rflags, method_(access_flags));
 366     }
 367 
 368 #ifdef ASSERT
 369     // Check if methods needs synchronization.
 370     {
 371       Label Lok;
 372       __ testbitdi(CCR0, R0, Rflags, JVM_ACC_SYNCHRONIZED_BIT);
 373       __ btrue(CCR0,Lok);
 374       __ stop("method doesn't need synchronization");
 375       __ bind(Lok);
 376     }
 377 #endif // ASSERT
 378   }
 379 
 380   // Get synchronization object to Rscratch2.
 381   {
 382     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
 383     Label Lstatic;
 384     Label Ldone;
 385 
 386     __ testbitdi(CCR0, R0, Rflags, JVM_ACC_STATIC_BIT);
 387     __ btrue(CCR0, Lstatic);
 388 
 389     // Non-static case: load receiver obj from stack and we're done.
 390     __ ld(Robj_to_lock, R18_locals);
 391     __ b(Ldone);
 392 
 393     __ bind(Lstatic); // Static case: Lock the java mirror
 394     __ ld(Robj_to_lock, in_bytes(Method::const_offset()), R19_method);
 395     __ ld(Robj_to_lock, in_bytes(ConstMethod::constants_offset()), Robj_to_lock);
 396     __ ld(Robj_to_lock, ConstantPool::pool_holder_offset_in_bytes(), Robj_to_lock);
 397     __ ld(Robj_to_lock, mirror_offset, Robj_to_lock);
 398 
 399     __ bind(Ldone);
 400     __ verify_oop(Robj_to_lock);
 401   }
 402 
 403   // Got the oop to lock => execute!
 404   __ add_monitor_to_stack(true, Rscratch1, R0);
 405 
 406   __ std(Robj_to_lock, BasicObjectLock::obj_offset_in_bytes(), R26_monitor);
 407   __ lock_object(R26_monitor, Robj_to_lock);
 408 }
 409 
 410 // Generate a fixed interpreter frame for pure interpreter
 411 // and I2N native transition frames.
 412 //
 413 // Before (stack grows downwards):
 414 //
 415 //         |  ...         |
 416 //         |------------- |
 417 //         |  java arg0   |
 418 //         |  ...         |
 419 //         |  java argn   |
 420 //         |              |   <-   R15_esp
 421 //         |              |
 422 //         |--------------|
 423 //         | abi_112      |
 424 //         |              |   <-   R1_SP
 425 //         |==============|
 426 //
 427 //
 428 // After:
 429 //
 430 //         |  ...         |
 431 //         |  java arg0   |<-   R18_locals
 432 //         |  ...         |
 433 //         |  java argn   |
 434 //         |--------------|
 435 //         |              |
 436 //         |  java locals |
 437 //         |              |
 438 //         |--------------|
 439 //         |  abi_48      |
 440 //         |==============|
 441 //         |              |
 442 //         |   istate     |
 443 //         |              |
 444 //         |--------------|
 445 //         |   monitor    |<-   R26_monitor
 446 //         |--------------|
 447 //         |              |<-   R15_esp
 448 //         | expression   |
 449 //         | stack        |
 450 //         |              |
 451 //         |--------------|
 452 //         |              |
 453 //         | abi_112      |<-   R1_SP
 454 //         |==============|
 455 //
 456 // The top most frame needs an abi space of 112 bytes. This space is needed,
 457 // since we call to c. The c function may spill their arguments to the caller
 458 // frame. When we call to java, we don't need these spill slots. In order to save
 459 // space on the stack, we resize the caller. However, java local reside in
 460 // the caller frame and the frame has to be increased. The frame_size for the
 461 // current frame was calculated based on max_stack as size for the expression
 462 // stack. At the call, just a part of the expression stack might be used.
 463 // We don't want to waste this space and cut the frame back accordingly.
 464 // The resulting amount for resizing is calculated as follows:
 465 // resize =   (number_of_locals - number_of_arguments) * slot_size
 466 //          + (R1_SP - R15_esp) + 48
 467 //
 468 // The size for the callee frame is calculated:
 469 // framesize = 112 + max_stack + monitor + state_size
 470 //
 471 // maxstack:   Max number of slots on the expression stack, loaded from the method.
 472 // monitor:    We statically reserve room for one monitor object.
 473 // state_size: We save the current state of the interpreter to this area.
 474 //
 475 void TemplateInterpreterGenerator::generate_fixed_frame(bool native_call, Register Rsize_of_parameters, Register Rsize_of_locals) {
 476   Register parent_frame_resize = R6_ARG4, // Frame will grow by this number of bytes.
 477            top_frame_size      = R7_ARG5,
 478            Rconst_method       = R8_ARG6;
 479 
 480   assert_different_registers(Rsize_of_parameters, Rsize_of_locals, parent_frame_resize, top_frame_size);
 481 
 482   __ ld(Rconst_method, method_(const));
 483   __ lhz(Rsize_of_parameters /* number of params */,
 484          in_bytes(ConstMethod::size_of_parameters_offset()), Rconst_method);
 485   if (native_call) {
 486     // If we're calling a native method, we reserve space for the worst-case signature
 487     // handler varargs vector, which is max(Argument::n_register_parameters, parameter_count+2).
 488     // We add two slots to the parameter_count, one for the jni
 489     // environment and one for a possible native mirror.
 490     Label skip_native_calculate_max_stack;
 491     __ addi(top_frame_size, Rsize_of_parameters, 2);
 492     __ cmpwi(CCR0, top_frame_size, Argument::n_register_parameters);
 493     __ bge(CCR0, skip_native_calculate_max_stack);
 494     __ li(top_frame_size, Argument::n_register_parameters);
 495     __ bind(skip_native_calculate_max_stack);
 496     __ sldi(Rsize_of_parameters, Rsize_of_parameters, Interpreter::logStackElementSize);
 497     __ sldi(top_frame_size, top_frame_size, Interpreter::logStackElementSize);
 498     __ sub(parent_frame_resize, R1_SP, R15_esp); // <0, off by Interpreter::stackElementSize!
 499     assert(Rsize_of_locals == noreg, "Rsize_of_locals not initialized"); // Only relevant value is Rsize_of_parameters.
 500   } else {
 501     __ lhz(Rsize_of_locals /* number of params */, in_bytes(ConstMethod::size_of_locals_offset()), Rconst_method);
 502     __ sldi(Rsize_of_parameters, Rsize_of_parameters, Interpreter::logStackElementSize);
 503     __ sldi(Rsize_of_locals, Rsize_of_locals, Interpreter::logStackElementSize);
 504     __ lhz(top_frame_size, in_bytes(ConstMethod::max_stack_offset()), Rconst_method);
 505     __ sub(R11_scratch1, Rsize_of_locals, Rsize_of_parameters); // >=0
 506     __ sub(parent_frame_resize, R1_SP, R15_esp); // <0, off by Interpreter::stackElementSize!
 507     __ sldi(top_frame_size, top_frame_size, Interpreter::logStackElementSize);
 508     __ add(parent_frame_resize, parent_frame_resize, R11_scratch1);
 509   }
 510 
 511   // Compute top frame size.
 512   __ addi(top_frame_size, top_frame_size, frame::abi_reg_args_size + frame::ijava_state_size);
 513 
 514   // Cut back area between esp and max_stack.
 515   __ addi(parent_frame_resize, parent_frame_resize, frame::abi_minframe_size - Interpreter::stackElementSize);
 516 
 517   __ round_to(top_frame_size, frame::alignment_in_bytes);
 518   __ round_to(parent_frame_resize, frame::alignment_in_bytes);
 519   // parent_frame_resize = (locals-parameters) - (ESP-SP-ABI48) Rounded to frame alignment size.
 520   // Enlarge by locals-parameters (not in case of native_call), shrink by ESP-SP-ABI48.
 521 
 522   {
 523     // --------------------------------------------------------------------------
 524     // Stack overflow check
 525 
 526     Label cont;
 527     __ add(R11_scratch1, parent_frame_resize, top_frame_size);
 528     generate_stack_overflow_check(R11_scratch1, R12_scratch2);
 529   }
 530 
 531   // Set up interpreter state registers.
 532 
 533   __ add(R18_locals, R15_esp, Rsize_of_parameters);
 534   __ ld(R27_constPoolCache, in_bytes(ConstMethod::constants_offset()), Rconst_method);
 535   __ ld(R27_constPoolCache, ConstantPool::cache_offset_in_bytes(), R27_constPoolCache);
 536 
 537   // Set method data pointer.
 538   if (ProfileInterpreter) {
 539     Label zero_continue;
 540     __ ld(R28_mdx, method_(method_data));
 541     __ cmpdi(CCR0, R28_mdx, 0);
 542     __ beq(CCR0, zero_continue);
 543     __ addi(R28_mdx, R28_mdx, in_bytes(MethodData::data_offset()));
 544     __ bind(zero_continue);
 545   }
 546 
 547   if (native_call) {
 548     __ li(R14_bcp, 0); // Must initialize.
 549   } else {
 550     __ add(R14_bcp, in_bytes(ConstMethod::codes_offset()), Rconst_method);
 551   }
 552 
 553   // Resize parent frame.
 554   __ mflr(R12_scratch2);
 555   __ neg(parent_frame_resize, parent_frame_resize);
 556   __ resize_frame(parent_frame_resize, R11_scratch1);
 557   __ std(R12_scratch2, _abi(lr), R1_SP);
 558 
 559   __ addi(R26_monitor, R1_SP, - frame::ijava_state_size);
 560   __ addi(R15_esp, R26_monitor, - Interpreter::stackElementSize);
 561 
 562   // Store values.
 563   // R15_esp, R14_bcp, R26_monitor, R28_mdx are saved at java calls
 564   // in InterpreterMacroAssembler::call_from_interpreter.
 565   __ std(R19_method, _ijava_state_neg(method), R1_SP);
 566   __ std(R21_sender_SP, _ijava_state_neg(sender_sp), R1_SP);
 567   __ std(R27_constPoolCache, _ijava_state_neg(cpoolCache), R1_SP);
 568   __ std(R18_locals, _ijava_state_neg(locals), R1_SP);
 569 
 570   // Note: esp, bcp, monitor, mdx live in registers. Hence, the correct version can only
 571   // be found in the frame after save_interpreter_state is done. This is always true
 572   // for non-top frames. But when a signal occurs, dumping the top frame can go wrong,
 573   // because e.g. frame::interpreter_frame_bcp() will not access the correct value
 574   // (Enhanced Stack Trace).
 575   // The signal handler does not save the interpreter state into the frame.
 576   __ li(R0, 0);
 577 #ifdef ASSERT
 578   // Fill remaining slots with constants.
 579   __ load_const_optimized(R11_scratch1, 0x5afe);
 580   __ load_const_optimized(R12_scratch2, 0xdead);
 581 #endif
 582   // We have to initialize some frame slots for native calls (accessed by GC).
 583   if (native_call) {
 584     __ std(R26_monitor, _ijava_state_neg(monitors), R1_SP);
 585     __ std(R14_bcp, _ijava_state_neg(bcp), R1_SP);
 586     if (ProfileInterpreter) { __ std(R28_mdx, _ijava_state_neg(mdx), R1_SP); }
 587   }
 588 #ifdef ASSERT
 589   else {
 590     __ std(R12_scratch2, _ijava_state_neg(monitors), R1_SP);
 591     __ std(R12_scratch2, _ijava_state_neg(bcp), R1_SP);
 592     __ std(R12_scratch2, _ijava_state_neg(mdx), R1_SP);
 593   }
 594   __ std(R11_scratch1, _ijava_state_neg(ijava_reserved), R1_SP);
 595   __ std(R12_scratch2, _ijava_state_neg(esp), R1_SP);
 596   __ std(R12_scratch2, _ijava_state_neg(lresult), R1_SP);
 597   __ std(R12_scratch2, _ijava_state_neg(fresult), R1_SP);
 598 #endif
 599   __ subf(R12_scratch2, top_frame_size, R1_SP);
 600   __ std(R0, _ijava_state_neg(oop_tmp), R1_SP);
 601   __ std(R12_scratch2, _ijava_state_neg(top_frame_sp), R1_SP);
 602 
 603   // Push top frame.
 604   __ push_frame(top_frame_size, R11_scratch1);
 605 }
 606 
 607 // End of helpers
 608 
 609 
 610 // Support abs and sqrt like in compiler.
 611 // For others we can use a normal (native) entry.
 612 
 613 inline bool math_entry_available(AbstractInterpreter::MethodKind kind) {
 614   if (!InlineIntrinsics) return false;
 615 
 616   return ((kind==Interpreter::java_lang_math_sqrt && VM_Version::has_fsqrt()) ||
 617           (kind==Interpreter::java_lang_math_abs));
 618 }
 619 
 620 address TemplateInterpreterGenerator::generate_math_entry(AbstractInterpreter::MethodKind kind) {
 621   if (!math_entry_available(kind)) {
 622     NOT_PRODUCT(__ should_not_reach_here();)
 623     return Interpreter::entry_for_kind(Interpreter::zerolocals);
 624   }
 625 
 626   address entry = __ pc();
 627 
 628   __ lfd(F1_RET, Interpreter::stackElementSize, R15_esp);
 629 
 630   // Pop c2i arguments (if any) off when we return.
 631 #ifdef ASSERT
 632   __ ld(R9_ARG7, 0, R1_SP);
 633   __ ld(R10_ARG8, 0, R21_sender_SP);
 634   __ cmpd(CCR0, R9_ARG7, R10_ARG8);
 635   __ asm_assert_eq("backlink", 0x545);
 636 #endif // ASSERT
 637   __ mr(R1_SP, R21_sender_SP); // Cut the stack back to where the caller started.
 638 
 639   if (kind == Interpreter::java_lang_math_sqrt) {
 640     __ fsqrt(F1_RET, F1_RET);
 641   } else if (kind == Interpreter::java_lang_math_abs) {
 642     __ fabs(F1_RET, F1_RET);
 643   } else {
 644     ShouldNotReachHere();
 645   }
 646 
 647   // And we're done.
 648   __ blr();
 649 
 650   __ flush();
 651 
 652   return entry;
 653 }
 654 
 655 // Interpreter stub for calling a native method. (asm interpreter)
 656 // This sets up a somewhat different looking stack for calling the
 657 // native method than the typical interpreter frame setup.
 658 //
 659 // On entry:
 660 //   R19_method    - method
 661 //   R16_thread    - JavaThread*
 662 //   R15_esp       - intptr_t* sender tos
 663 //
 664 //   abstract stack (grows up)
 665 //     [  IJava (caller of JNI callee)  ]  <-- ASP
 666 //        ...
 667 address TemplateInterpreterGenerator::generate_native_entry(bool synchronized) {
 668 
 669   address entry = __ pc();
 670 
 671   const bool inc_counter = UseCompiler || CountCompiledCalls;
 672 
 673   // -----------------------------------------------------------------------------
 674   // Allocate a new frame that represents the native callee (i2n frame).
 675   // This is not a full-blown interpreter frame, but in particular, the
 676   // following registers are valid after this:
 677   // - R19_method
 678   // - R18_local (points to start of argumuments to native function)
 679   //
 680   //   abstract stack (grows up)
 681   //     [  IJava (caller of JNI callee)  ]  <-- ASP
 682   //        ...
 683 
 684   const Register signature_handler_fd = R11_scratch1;
 685   const Register pending_exception    = R0;
 686   const Register result_handler_addr  = R31;
 687   const Register native_method_fd     = R11_scratch1;
 688   const Register access_flags         = R22_tmp2;
 689   const Register active_handles       = R11_scratch1; // R26_monitor saved to state.
 690   const Register sync_state           = R12_scratch2;
 691   const Register sync_state_addr      = sync_state;   // Address is dead after use.
 692   const Register suspend_flags        = R11_scratch1;
 693 
 694   //=============================================================================
 695   // Allocate new frame and initialize interpreter state.
 696 
 697   Label exception_return;
 698   Label exception_return_sync_check;
 699   Label stack_overflow_return;
 700 
 701   // Generate new interpreter state and jump to stack_overflow_return in case of
 702   // a stack overflow.
 703   //generate_compute_interpreter_state(stack_overflow_return);
 704 
 705   Register size_of_parameters = R22_tmp2;
 706 
 707   generate_fixed_frame(true, size_of_parameters, noreg /* unused */);
 708 
 709   //=============================================================================
 710   // Increment invocation counter. On overflow, entry to JNI method
 711   // will be compiled.
 712   Label invocation_counter_overflow, continue_after_compile;
 713   if (inc_counter) {
 714     if (synchronized) {
 715       // Since at this point in the method invocation the exception handler
 716       // would try to exit the monitor of synchronized methods which hasn't
 717       // been entered yet, we set the thread local variable
 718       // _do_not_unlock_if_synchronized to true. If any exception was thrown by
 719       // runtime, exception handling i.e. unlock_if_synchronized_method will
 720       // check this thread local flag.
 721       // This flag has two effects, one is to force an unwind in the topmost
 722       // interpreter frame and not perform an unlock while doing so.
 723       __ li(R0, 1);
 724       __ stb(R0, in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()), R16_thread);
 725     }
 726     generate_counter_incr(&invocation_counter_overflow, NULL, NULL);
 727 
 728     BIND(continue_after_compile);
 729     // Reset the _do_not_unlock_if_synchronized flag.
 730     if (synchronized) {
 731       __ li(R0, 0);
 732       __ stb(R0, in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()), R16_thread);
 733     }
 734   }
 735 
 736   // access_flags = method->access_flags();
 737   // Load access flags.
 738   assert(access_flags->is_nonvolatile(),
 739          "access_flags must be in a non-volatile register");
 740   // Type check.
 741   assert(4 == sizeof(AccessFlags), "unexpected field size");
 742   __ lwz(access_flags, method_(access_flags));
 743 
 744   // We don't want to reload R19_method and access_flags after calls
 745   // to some helper functions.
 746   assert(R19_method->is_nonvolatile(),
 747          "R19_method must be a non-volatile register");
 748 
 749   // Check for synchronized methods. Must happen AFTER invocation counter
 750   // check, so method is not locked if counter overflows.
 751 
 752   if (synchronized) {
 753     lock_method(access_flags, R11_scratch1, R12_scratch2, true);
 754 
 755     // Update monitor in state.
 756     __ ld(R11_scratch1, 0, R1_SP);
 757     __ std(R26_monitor, _ijava_state_neg(monitors), R11_scratch1);
 758   }
 759 
 760   // jvmti/jvmpi support
 761   __ notify_method_entry();
 762 
 763   //=============================================================================
 764   // Get and call the signature handler.
 765 
 766   __ ld(signature_handler_fd, method_(signature_handler));
 767   Label call_signature_handler;
 768 
 769   __ cmpdi(CCR0, signature_handler_fd, 0);
 770   __ bne(CCR0, call_signature_handler);
 771 
 772   // Method has never been called. Either generate a specialized
 773   // handler or point to the slow one.
 774   //
 775   // Pass parameter 'false' to avoid exception check in call_VM.
 776   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), R19_method, false);
 777 
 778   // Check for an exception while looking up the target method. If we
 779   // incurred one, bail.
 780   __ ld(pending_exception, thread_(pending_exception));
 781   __ cmpdi(CCR0, pending_exception, 0);
 782   __ bne(CCR0, exception_return_sync_check); // Has pending exception.
 783 
 784   // Reload signature handler, it may have been created/assigned in the meanwhile.
 785   __ ld(signature_handler_fd, method_(signature_handler));
 786   __ twi_0(signature_handler_fd); // Order wrt. load of klass mirror and entry point (isync is below).
 787 
 788   BIND(call_signature_handler);
 789 
 790   // Before we call the signature handler we push a new frame to
 791   // protect the interpreter frame volatile registers when we return
 792   // from jni but before we can get back to Java.
 793 
 794   // First set the frame anchor while the SP/FP registers are
 795   // convenient and the slow signature handler can use this same frame
 796   // anchor.
 797 
 798   // We have a TOP_IJAVA_FRAME here, which belongs to us.
 799   __ set_top_ijava_frame_at_SP_as_last_Java_frame(R1_SP, R12_scratch2/*tmp*/);
 800 
 801   // Now the interpreter frame (and its call chain) have been
 802   // invalidated and flushed. We are now protected against eager
 803   // being enabled in native code. Even if it goes eager the
 804   // registers will be reloaded as clean and we will invalidate after
 805   // the call so no spurious flush should be possible.
 806 
 807   // Call signature handler and pass locals address.
 808   //
 809   // Our signature handlers copy required arguments to the C stack
 810   // (outgoing C args), R3_ARG1 to R10_ARG8, and FARG1 to FARG13.
 811   __ mr(R3_ARG1, R18_locals);
 812 #if !defined(ABI_ELFv2)
 813   __ ld(signature_handler_fd, 0, signature_handler_fd);
 814 #endif
 815 
 816   __ call_stub(signature_handler_fd);
 817 
 818   // Remove the register parameter varargs slots we allocated in
 819   // compute_interpreter_state. SP+16 ends up pointing to the ABI
 820   // outgoing argument area.
 821   //
 822   // Not needed on PPC64.
 823   //__ add(SP, SP, Argument::n_register_parameters*BytesPerWord);
 824 
 825   assert(result_handler_addr->is_nonvolatile(), "result_handler_addr must be in a non-volatile register");
 826   // Save across call to native method.
 827   __ mr(result_handler_addr, R3_RET);
 828 
 829   __ isync(); // Acquire signature handler before trying to fetch the native entry point and klass mirror.
 830 
 831   // Set up fixed parameters and call the native method.
 832   // If the method is static, get mirror into R4_ARG2.
 833   {
 834     Label method_is_not_static;
 835     // Access_flags is non-volatile and still, no need to restore it.
 836 
 837     // Restore access flags.
 838     __ testbitdi(CCR0, R0, access_flags, JVM_ACC_STATIC_BIT);
 839     __ bfalse(CCR0, method_is_not_static);
 840 
 841     // constants = method->constants();
 842     __ ld(R11_scratch1, in_bytes(Method::const_offset()), R19_method);
 843     __ ld(R11_scratch1, in_bytes(ConstMethod::constants_offset()), R11_scratch1);
 844     // pool_holder = method->constants()->pool_holder();
 845     __ ld(R11_scratch1/*pool_holder*/, ConstantPool::pool_holder_offset_in_bytes(),
 846           R11_scratch1/*constants*/);
 847 
 848     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
 849 
 850     // mirror = pool_holder->klass_part()->java_mirror();
 851     __ ld(R0/*mirror*/, mirror_offset, R11_scratch1/*pool_holder*/);
 852     // state->_native_mirror = mirror;
 853 
 854     __ ld(R11_scratch1, 0, R1_SP);
 855     __ std(R0/*mirror*/, _ijava_state_neg(oop_tmp), R11_scratch1);
 856     // R4_ARG2 = &state->_oop_temp;
 857     __ addi(R4_ARG2, R11_scratch1, _ijava_state_neg(oop_tmp));
 858     BIND(method_is_not_static);
 859   }
 860 
 861   // At this point, arguments have been copied off the stack into
 862   // their JNI positions. Oops are boxed in-place on the stack, with
 863   // handles copied to arguments. The result handler address is in a
 864   // register.
 865 
 866   // Pass JNIEnv address as first parameter.
 867   __ addir(R3_ARG1, thread_(jni_environment));
 868 
 869   // Load the native_method entry before we change the thread state.
 870   __ ld(native_method_fd, method_(native_function));
 871 
 872   //=============================================================================
 873   // Transition from _thread_in_Java to _thread_in_native. As soon as
 874   // we make this change the safepoint code needs to be certain that
 875   // the last Java frame we established is good. The pc in that frame
 876   // just needs to be near here not an actual return address.
 877 
 878   // We use release_store_fence to update values like the thread state, where
 879   // we don't want the current thread to continue until all our prior memory
 880   // accesses (including the new thread state) are visible to other threads.
 881   __ li(R0, _thread_in_native);
 882   __ release();
 883 
 884   // TODO PPC port assert(4 == JavaThread::sz_thread_state(), "unexpected field size");
 885   __ stw(R0, thread_(thread_state));
 886 
 887   if (UseMembar) {
 888     __ fence();
 889   }
 890 
 891   //=============================================================================
 892   // Call the native method. Argument registers must not have been
 893   // overwritten since "__ call_stub(signature_handler);" (except for
 894   // ARG1 and ARG2 for static methods).
 895   __ call_c(native_method_fd);
 896 
 897   __ li(R0, 0);
 898   __ ld(R11_scratch1, 0, R1_SP);
 899   __ std(R3_RET, _ijava_state_neg(lresult), R11_scratch1);
 900   __ stfd(F1_RET, _ijava_state_neg(fresult), R11_scratch1);
 901   __ std(R0/*mirror*/, _ijava_state_neg(oop_tmp), R11_scratch1); // reset
 902 
 903   // Note: C++ interpreter needs the following here:
 904   // The frame_manager_lr field, which we use for setting the last
 905   // java frame, gets overwritten by the signature handler. Restore
 906   // it now.
 907   //__ get_PC_trash_LR(R11_scratch1);
 908   //__ std(R11_scratch1, _top_ijava_frame_abi(frame_manager_lr), R1_SP);
 909 
 910   // Because of GC R19_method may no longer be valid.
 911 
 912   // Block, if necessary, before resuming in _thread_in_Java state.
 913   // In order for GC to work, don't clear the last_Java_sp until after
 914   // blocking.
 915 
 916   //=============================================================================
 917   // Switch thread to "native transition" state before reading the
 918   // synchronization state. This additional state is necessary
 919   // because reading and testing the synchronization state is not
 920   // atomic w.r.t. GC, as this scenario demonstrates: Java thread A,
 921   // in _thread_in_native state, loads _not_synchronized and is
 922   // preempted. VM thread changes sync state to synchronizing and
 923   // suspends threads for GC. Thread A is resumed to finish this
 924   // native method, but doesn't block here since it didn't see any
 925   // synchronization in progress, and escapes.
 926 
 927   // We use release_store_fence to update values like the thread state, where
 928   // we don't want the current thread to continue until all our prior memory
 929   // accesses (including the new thread state) are visible to other threads.
 930   __ li(R0/*thread_state*/, _thread_in_native_trans);
 931   __ release();
 932   __ stw(R0/*thread_state*/, thread_(thread_state));
 933   if (UseMembar) {
 934     __ fence();
 935   }
 936   // Write serialization page so that the VM thread can do a pseudo remote
 937   // membar. We use the current thread pointer to calculate a thread
 938   // specific offset to write to within the page. This minimizes bus
 939   // traffic due to cache line collision.
 940   else {
 941     __ serialize_memory(R16_thread, R11_scratch1, R12_scratch2);
 942   }
 943 
 944   // Now before we return to java we must look for a current safepoint
 945   // (a new safepoint can not start since we entered native_trans).
 946   // We must check here because a current safepoint could be modifying
 947   // the callers registers right this moment.
 948 
 949   // Acquire isn't strictly necessary here because of the fence, but
 950   // sync_state is declared to be volatile, so we do it anyway
 951   // (cmp-br-isync on one path, release (same as acquire on PPC64) on the other path).
 952   int sync_state_offs = __ load_const_optimized(sync_state_addr, SafepointSynchronize::address_of_state(), /*temp*/R0, true);
 953 
 954   // TODO PPC port assert(4 == SafepointSynchronize::sz_state(), "unexpected field size");
 955   __ lwz(sync_state, sync_state_offs, sync_state_addr);
 956 
 957   // TODO PPC port assert(4 == Thread::sz_suspend_flags(), "unexpected field size");
 958   __ lwz(suspend_flags, thread_(suspend_flags));
 959 
 960   Label sync_check_done;
 961   Label do_safepoint;
 962   // No synchronization in progress nor yet synchronized.
 963   __ cmpwi(CCR0, sync_state, SafepointSynchronize::_not_synchronized);
 964   // Not suspended.
 965   __ cmpwi(CCR1, suspend_flags, 0);
 966 
 967   __ bne(CCR0, do_safepoint);
 968   __ beq(CCR1, sync_check_done);
 969   __ bind(do_safepoint);
 970   __ isync();
 971   // Block. We do the call directly and leave the current
 972   // last_Java_frame setup undisturbed. We must save any possible
 973   // native result across the call. No oop is present.
 974 
 975   __ mr(R3_ARG1, R16_thread);
 976 #if defined(ABI_ELFv2)
 977   __ call_c(CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans),
 978             relocInfo::none);
 979 #else
 980   __ call_c(CAST_FROM_FN_PTR(FunctionDescriptor*, JavaThread::check_special_condition_for_native_trans),
 981             relocInfo::none);
 982 #endif
 983 
 984   __ bind(sync_check_done);
 985 
 986   //=============================================================================
 987   // <<<<<< Back in Interpreter Frame >>>>>
 988 
 989   // We are in thread_in_native_trans here and back in the normal
 990   // interpreter frame. We don't have to do anything special about
 991   // safepoints and we can switch to Java mode anytime we are ready.
 992 
 993   // Note: frame::interpreter_frame_result has a dependency on how the
 994   // method result is saved across the call to post_method_exit. For
 995   // native methods it assumes that the non-FPU/non-void result is
 996   // saved in _native_lresult and a FPU result in _native_fresult. If
 997   // this changes then the interpreter_frame_result implementation
 998   // will need to be updated too.
 999 
1000   // On PPC64, we have stored the result directly after the native call.
1001 
1002   //=============================================================================
1003   // Back in Java
1004 
1005   // We use release_store_fence to update values like the thread state, where
1006   // we don't want the current thread to continue until all our prior memory
1007   // accesses (including the new thread state) are visible to other threads.
1008   __ li(R0/*thread_state*/, _thread_in_Java);
1009   __ release();
1010   __ stw(R0/*thread_state*/, thread_(thread_state));
1011   if (UseMembar) {
1012     __ fence();
1013   }
1014 
1015   __ reset_last_Java_frame();
1016 
1017   // Jvmdi/jvmpi support. Whether we've got an exception pending or
1018   // not, and whether unlocking throws an exception or not, we notify
1019   // on native method exit. If we do have an exception, we'll end up
1020   // in the caller's context to handle it, so if we don't do the
1021   // notify here, we'll drop it on the floor.
1022   __ notify_method_exit(true/*native method*/,
1023                         ilgl /*illegal state (not used for native methods)*/,
1024                         InterpreterMacroAssembler::NotifyJVMTI,
1025                         false /*check_exceptions*/);
1026 
1027   //=============================================================================
1028   // Handle exceptions
1029 
1030   if (synchronized) {
1031     // Don't check for exceptions since we're still in the i2n frame. Do that
1032     // manually afterwards.
1033     unlock_method(false);
1034   }
1035 
1036   // Reset active handles after returning from native.
1037   // thread->active_handles()->clear();
1038   __ ld(active_handles, thread_(active_handles));
1039   // TODO PPC port assert(4 == JNIHandleBlock::top_size_in_bytes(), "unexpected field size");
1040   __ li(R0, 0);
1041   __ stw(R0, JNIHandleBlock::top_offset_in_bytes(), active_handles);
1042 
1043   Label exception_return_sync_check_already_unlocked;
1044   __ ld(R0/*pending_exception*/, thread_(pending_exception));
1045   __ cmpdi(CCR0, R0/*pending_exception*/, 0);
1046   __ bne(CCR0, exception_return_sync_check_already_unlocked);
1047 
1048   //-----------------------------------------------------------------------------
1049   // No exception pending.
1050 
1051   // Move native method result back into proper registers and return.
1052   // Invoke result handler (may unbox/promote).
1053   __ ld(R11_scratch1, 0, R1_SP);
1054   __ ld(R3_RET, _ijava_state_neg(lresult), R11_scratch1);
1055   __ lfd(F1_RET, _ijava_state_neg(fresult), R11_scratch1);
1056   __ call_stub(result_handler_addr);
1057 
1058   __ merge_frames(/*top_frame_sp*/ R21_sender_SP, /*return_pc*/ R0, R11_scratch1, R12_scratch2);
1059 
1060   // Must use the return pc which was loaded from the caller's frame
1061   // as the VM uses return-pc-patching for deoptimization.
1062   __ mtlr(R0);
1063   __ blr();
1064 
1065   //-----------------------------------------------------------------------------
1066   // An exception is pending. We call into the runtime only if the
1067   // caller was not interpreted. If it was interpreted the
1068   // interpreter will do the correct thing. If it isn't interpreted
1069   // (call stub/compiled code) we will change our return and continue.
1070 
1071   BIND(exception_return_sync_check);
1072 
1073   if (synchronized) {
1074     // Don't check for exceptions since we're still in the i2n frame. Do that
1075     // manually afterwards.
1076     unlock_method(false);
1077   }
1078   BIND(exception_return_sync_check_already_unlocked);
1079 
1080   const Register return_pc = R31;
1081 
1082   __ ld(return_pc, 0, R1_SP);
1083   __ ld(return_pc, _abi(lr), return_pc);
1084 
1085   // Get the address of the exception handler.
1086   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address),
1087                   R16_thread,
1088                   return_pc /* return pc */);
1089   __ merge_frames(/*top_frame_sp*/ R21_sender_SP, noreg, R11_scratch1, R12_scratch2);
1090 
1091   // Load the PC of the the exception handler into LR.
1092   __ mtlr(R3_RET);
1093 
1094   // Load exception into R3_ARG1 and clear pending exception in thread.
1095   __ ld(R3_ARG1/*exception*/, thread_(pending_exception));
1096   __ li(R4_ARG2, 0);
1097   __ std(R4_ARG2, thread_(pending_exception));
1098 
1099   // Load the original return pc into R4_ARG2.
1100   __ mr(R4_ARG2/*issuing_pc*/, return_pc);
1101 
1102   // Return to exception handler.
1103   __ blr();
1104 
1105   //=============================================================================
1106   // Counter overflow.
1107 
1108   if (inc_counter) {
1109     // Handle invocation counter overflow.
1110     __ bind(invocation_counter_overflow);
1111 
1112     generate_counter_overflow(continue_after_compile);
1113   }
1114 
1115   return entry;
1116 }
1117 
1118 // Generic interpreted method entry to (asm) interpreter.
1119 //
1120 address TemplateInterpreterGenerator::generate_normal_entry(bool synchronized) {
1121   bool inc_counter = UseCompiler || CountCompiledCalls;
1122   address entry = __ pc();
1123   // Generate the code to allocate the interpreter stack frame.
1124   Register Rsize_of_parameters = R4_ARG2, // Written by generate_fixed_frame.
1125            Rsize_of_locals     = R5_ARG3; // Written by generate_fixed_frame.
1126 
1127   generate_fixed_frame(false, Rsize_of_parameters, Rsize_of_locals);
1128 
1129 #ifdef FAST_DISPATCH
1130   __ unimplemented("Fast dispatch in generate_normal_entry");
1131 #if 0
1132   __ set((intptr_t)Interpreter::dispatch_table(), IdispatchTables);
1133   // Set bytecode dispatch table base.
1134 #endif
1135 #endif
1136 
1137   // --------------------------------------------------------------------------
1138   // Zero out non-parameter locals.
1139   // Note: *Always* zero out non-parameter locals as Sparc does. It's not
1140   // worth to ask the flag, just do it.
1141   Register Rslot_addr = R6_ARG4,
1142            Rnum       = R7_ARG5;
1143   Label Lno_locals, Lzero_loop;
1144 
1145   // Set up the zeroing loop.
1146   __ subf(Rnum, Rsize_of_parameters, Rsize_of_locals);
1147   __ subf(Rslot_addr, Rsize_of_parameters, R18_locals);
1148   __ srdi_(Rnum, Rnum, Interpreter::logStackElementSize);
1149   __ beq(CCR0, Lno_locals);
1150   __ li(R0, 0);
1151   __ mtctr(Rnum);
1152 
1153   // The zero locals loop.
1154   __ bind(Lzero_loop);
1155   __ std(R0, 0, Rslot_addr);
1156   __ addi(Rslot_addr, Rslot_addr, -Interpreter::stackElementSize);
1157   __ bdnz(Lzero_loop);
1158 
1159   __ bind(Lno_locals);
1160 
1161   // --------------------------------------------------------------------------
1162   // Counter increment and overflow check.
1163   Label invocation_counter_overflow,
1164         profile_method,
1165         profile_method_continue;
1166   if (inc_counter || ProfileInterpreter) {
1167 
1168     Register Rdo_not_unlock_if_synchronized_addr = R11_scratch1;
1169     if (synchronized) {
1170       // Since at this point in the method invocation the exception handler
1171       // would try to exit the monitor of synchronized methods which hasn't
1172       // been entered yet, we set the thread local variable
1173       // _do_not_unlock_if_synchronized to true. If any exception was thrown by
1174       // runtime, exception handling i.e. unlock_if_synchronized_method will
1175       // check this thread local flag.
1176       // This flag has two effects, one is to force an unwind in the topmost
1177       // interpreter frame and not perform an unlock while doing so.
1178       __ li(R0, 1);
1179       __ stb(R0, in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()), R16_thread);
1180     }
1181 
1182     // Argument and return type profiling.
1183     __ profile_parameters_type(R3_ARG1, R4_ARG2, R5_ARG3, R6_ARG4);
1184 
1185     // Increment invocation counter and check for overflow.
1186     if (inc_counter) {
1187       generate_counter_incr(&invocation_counter_overflow, &profile_method, &profile_method_continue);
1188     }
1189 
1190     __ bind(profile_method_continue);
1191 
1192     // Reset the _do_not_unlock_if_synchronized flag.
1193     if (synchronized) {
1194       __ li(R0, 0);
1195       __ stb(R0, in_bytes(JavaThread::do_not_unlock_if_synchronized_offset()), R16_thread);
1196     }
1197   }
1198 
1199   // --------------------------------------------------------------------------
1200   // Locking of synchronized methods. Must happen AFTER invocation_counter
1201   // check and stack overflow check, so method is not locked if overflows.
1202   if (synchronized) {
1203     lock_method(R3_ARG1, R4_ARG2, R5_ARG3);
1204   }
1205 #ifdef ASSERT
1206   else {
1207     Label Lok;
1208     __ lwz(R0, in_bytes(Method::access_flags_offset()), R19_method);
1209     __ andi_(R0, R0, JVM_ACC_SYNCHRONIZED);
1210     __ asm_assert_eq("method needs synchronization", 0x8521);
1211     __ bind(Lok);
1212   }
1213 #endif // ASSERT
1214 
1215   __ verify_thread();
1216 
1217   // --------------------------------------------------------------------------
1218   // JVMTI support
1219   __ notify_method_entry();
1220 
1221   // --------------------------------------------------------------------------
1222   // Start executing instructions.
1223   __ dispatch_next(vtos);
1224 
1225   // --------------------------------------------------------------------------
1226   // Out of line counter overflow and MDO creation code.
1227   if (ProfileInterpreter) {
1228     // We have decided to profile this method in the interpreter.
1229     __ bind(profile_method);
1230     __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::profile_method));
1231     __ set_method_data_pointer_for_bcp();
1232     __ b(profile_method_continue);
1233   }
1234 
1235   if (inc_counter) {
1236     // Handle invocation counter overflow.
1237     __ bind(invocation_counter_overflow);
1238     generate_counter_overflow(profile_method_continue);
1239   }
1240   return entry;
1241 }
1242 // CRC32 Intrinsics.
1243 //
1244 // Contract on scratch and work registers.
1245 // =======================================
1246 //
1247 // On ppc, the register set {R2..R12} is available in the interpreter as scratch/work registers.
1248 // You should, however, keep in mind that {R3_ARG1..R10_ARG8} is the C-ABI argument register set.
1249 // You can't rely on these registers across calls.
1250 //
1251 // The generators for CRC32_update and for CRC32_updateBytes use the
1252 // scratch/work register set internally, passing the work registers
1253 // as arguments to the MacroAssembler emitters as required.
1254 //
1255 // R3_ARG1..R6_ARG4 are preset to hold the incoming java arguments.
1256 // Their contents is not constant but may change according to the requirements
1257 // of the emitted code.
1258 //
1259 // All other registers from the scratch/work register set are used "internally"
1260 // and contain garbage (i.e. unpredictable values) once blr() is reached.
1261 // Basically, only R3_RET contains a defined value which is the function result.
1262 //
1263 /**
1264  * Method entry for static native methods:
1265  *   int java.util.zip.CRC32.update(int crc, int b)
1266  */
1267 address InterpreterGenerator::generate_CRC32_update_entry() {
1268   address start = __ function_entry();  // Remember stub start address (is rtn value).
1269 
1270   if (UseCRC32Intrinsics) {
1271     Label slow_path;
1272 
1273     // Safepoint check
1274     const Register sync_state = R11_scratch1;
1275     int sync_state_offs = __ load_const_optimized(sync_state, SafepointSynchronize::address_of_state(), /*temp*/R0, true);
1276     __ lwz(sync_state, sync_state_offs, sync_state);
1277     __ cmpwi(CCR0, sync_state, SafepointSynchronize::_not_synchronized);
1278     __ bne(CCR0, slow_path);
1279 
1280     // We don't generate local frame and don't align stack because
1281     // we not even call stub code (we generate the code inline)
1282     // and there is no safepoint on this path.
1283 
1284     // Load java parameters.
1285     // R15_esp is callers operand stack pointer, i.e. it points to the parameters.
1286     const Register argP    = R15_esp;
1287     const Register crc     = R3_ARG1;  // crc value
1288     const Register data    = R4_ARG2;  // address of java byte value (kernel_crc32 needs address)
1289     const Register dataLen = R5_ARG3;  // source data len (1 byte). Not used because calling the single-byte emitter.
1290     const Register table   = R6_ARG4;  // address of crc32 table
1291     const Register tmp     = dataLen;  // Reuse unused len register to show we don't actually need a separate tmp here.
1292 
1293     BLOCK_COMMENT("CRC32_update {");
1294 
1295     // Arguments are reversed on java expression stack
1296 #ifdef VM_LITTLE_ENDIAN
1297     __ addi(data, argP, 0+1*wordSize); // (stack) address of byte value. Emitter expects address, not value.
1298                                        // Being passed as an int, the single byte is at offset +0.
1299 #else
1300     __ addi(data, argP, 3+1*wordSize); // (stack) address of byte value. Emitter expects address, not value.
1301                                        // Being passed from java as an int, the single byte is at offset +3.
1302 #endif
1303     __ lwz(crc,  2*wordSize, argP);    // Current crc state, zero extend to 64 bit to have a clean register.
1304 
1305     StubRoutines::ppc64::generate_load_crc_table_addr(_masm, table);
1306     __ kernel_crc32_singleByte(crc, data, dataLen, table, tmp);
1307 
1308     // Restore caller sp for c2i case and return.
1309     __ mr(R1_SP, R21_sender_SP); // Cut the stack back to where the caller started.
1310     __ blr();
1311 
1312     // Generate a vanilla native entry as the slow path.
1313     BLOCK_COMMENT("} CRC32_update");
1314     BIND(slow_path);
1315   }
1316 
1317   (void) generate_native_entry(false);
1318 
1319 #if defined(ABI_ELFv2)
1320   return start;
1321 #else
1322   return *(address*)start;
1323 #endif
1324 }
1325 
1326 // CRC32 Intrinsics.
1327 /**
1328  * Method entry for static native methods:
1329  *   int java.util.zip.CRC32.updateBytes(     int crc, byte[] b,  int off, int len)
1330  *   int java.util.zip.CRC32.updateByteBuffer(int crc, long* buf, int off, int len)
1331  */
1332 address InterpreterGenerator::generate_CRC32_updateBytes_entry(AbstractInterpreter::MethodKind kind) {
1333   address start = __ function_entry();  // Remember stub start address (is rtn value).
1334 
1335   if (UseCRC32Intrinsics) {
1336     Label slow_path;
1337 
1338     // Safepoint check
1339     const Register sync_state = R11_scratch1;
1340     int sync_state_offs = __ load_const_optimized(sync_state, SafepointSynchronize::address_of_state(), /*temp*/R0, true);
1341     __ lwz(sync_state, sync_state_offs, sync_state);
1342     __ cmpwi(CCR0, sync_state, SafepointSynchronize::_not_synchronized);
1343     __ bne(CCR0, slow_path);
1344 
1345     // We don't generate local frame and don't align stack because
1346     // we not even call stub code (we generate the code inline)
1347     // and there is no safepoint on this path.
1348 
1349     // Load parameters.
1350     // Z_esp is callers operand stack pointer, i.e. it points to the parameters.
1351     const Register argP    = R15_esp;
1352     const Register crc     = R3_ARG1;  // crc value
1353     const Register data    = R4_ARG2;  // address of java byte array
1354     const Register dataLen = R5_ARG3;  // source data len
1355     const Register table   = R6_ARG4;  // address of crc32 table
1356 
1357     const Register t0      = R9;       // scratch registers for crc calculation
1358     const Register t1      = R10;
1359     const Register t2      = R11;
1360     const Register t3      = R12;
1361 
1362     const Register tc0     = R2;       // registers to hold pre-calculated column addresses
1363     const Register tc1     = R7;
1364     const Register tc2     = R8;
1365     const Register tc3     = table;    // table address is reconstructed at the end of kernel_crc32_* emitters
1366 
1367     const Register tmp     = t0;       // Only used very locally to calculate byte buffer address.
1368 
1369     // Arguments are reversed on java expression stack.
1370     // Calculate address of start element.
1371     if (kind == Interpreter::java_util_zip_CRC32_updateByteBuffer) { // Used for "updateByteBuffer direct".
1372       BLOCK_COMMENT("CRC32_updateByteBuffer {");
1373       // crc     @ (SP + 5W) (32bit)
1374       // buf     @ (SP + 3W) (64bit ptr to long array)
1375       // off     @ (SP + 2W) (32bit)
1376       // dataLen @ (SP + 1W) (32bit)
1377       // data = buf + off
1378       __ ld(  data,    3*wordSize, argP);  // start of byte buffer
1379       __ lwa( tmp,     2*wordSize, argP);  // byte buffer offset
1380       __ lwa( dataLen, 1*wordSize, argP);  // #bytes to process
1381       __ lwz( crc,     5*wordSize, argP);  // current crc state
1382       __ add( data, data, tmp);            // Add byte buffer offset.
1383     } else {                                                         // Used for "updateBytes update".
1384       BLOCK_COMMENT("CRC32_updateBytes {");
1385       // crc     @ (SP + 4W) (32bit)
1386       // buf     @ (SP + 3W) (64bit ptr to byte array)
1387       // off     @ (SP + 2W) (32bit)
1388       // dataLen @ (SP + 1W) (32bit)
1389       // data = buf + off + base_offset
1390       __ ld(  data,    3*wordSize, argP);  // start of byte buffer
1391       __ lwa( tmp,     2*wordSize, argP);  // byte buffer offset
1392       __ lwa( dataLen, 1*wordSize, argP);  // #bytes to process
1393       __ add( data, data, tmp);            // add byte buffer offset
1394       __ lwz( crc,     4*wordSize, argP);  // current crc state
1395       __ addi(data, data, arrayOopDesc::base_offset_in_bytes(T_BYTE));
1396     }
1397 
1398     StubRoutines::ppc64::generate_load_crc_table_addr(_masm, table);
1399 
1400 #if 1
1401     // Performance measurements show the 1word and 2word variants to be almost equivalent,
1402     // with very light advantages for the 1word variant. We chose the 1word variant for
1403     // code compactness.
1404     __ kernel_crc32_1word(crc, data, dataLen, table, t0, t1, t2, t3, tc0, tc1, tc2, tc3);
1405 #else
1406     if (UseNewCode) {
1407       __ kernel_crc32_1word(crc, data, dataLen, table, t0, t1, t2, t3, tc0, tc1, tc2, tc3);
1408     } else if (UseNewCode2) {
1409       __ kernel_crc32_2word(crc, data, dataLen, table, t0, t1, t2, t3, tc0, tc1, tc2, tc3);
1410     } else if (UseNewCode3) {
1411       __ kernel_crc32_1byte(crc, data, dataLen, table, t0, t1, t2, t3);
1412     } else {
1413       __ kernel_crc32_1word(crc, data, dataLen, table, t0, t1, t2, t3, tc0, tc1, tc2, tc3);
1414     }
1415 #endif
1416 
1417     // Restore caller sp for c2i case and return.
1418     __ mr(R1_SP, R21_sender_SP); // Cut the stack back to where the caller started.
1419     __ blr();
1420 
1421     // Generate a vanilla native entry as the slow path.
1422     BLOCK_COMMENT("} CRC32_updateBytes(Buffer)");
1423     BIND(slow_path);
1424   }
1425 
1426   (void) generate_native_entry(false);
1427 
1428 #if defined(ABI_ELFv2)
1429   return start;
1430 #else
1431   return *(address*)start;
1432 #endif
1433 }
1434 
1435 // These should never be compiled since the interpreter will prefer
1436 // the compiled version to the intrinsic version.
1437 bool AbstractInterpreter::can_be_compiled(methodHandle m) {
1438   return !math_entry_available(method_kind(m));
1439 }
1440 
1441 // How much stack a method activation needs in stack slots.
1442 // We must calc this exactly like in generate_fixed_frame.
1443 // Note: This returns the conservative size assuming maximum alignment.
1444 int AbstractInterpreter::size_top_interpreter_activation(Method* method) {
1445   const int max_alignment_size = 2;
1446   const int abi_scratch = frame::abi_reg_args_size;
1447   return method->max_locals() + method->max_stack() +
1448          frame::interpreter_frame_monitor_size() + max_alignment_size + abi_scratch;
1449 }
1450 
1451 // Returns number of stackElementWords needed for the interpreter frame with the
1452 // given sections.
1453 // This overestimates the stack by one slot in case of alignments.
1454 int AbstractInterpreter::size_activation(int max_stack,
1455                                          int temps,
1456                                          int extra_args,
1457                                          int monitors,
1458                                          int callee_params,
1459                                          int callee_locals,
1460                                          bool is_top_frame) {
1461   // Note: This calculation must exactly parallel the frame setup
1462   // in InterpreterGenerator::generate_fixed_frame.
1463   assert(Interpreter::stackElementWords == 1, "sanity");
1464   const int max_alignment_space = StackAlignmentInBytes / Interpreter::stackElementSize;
1465   const int abi_scratch = is_top_frame ? (frame::abi_reg_args_size / Interpreter::stackElementSize) :
1466                                          (frame::abi_minframe_size / Interpreter::stackElementSize);
1467   const int size =
1468     max_stack                                                +
1469     (callee_locals - callee_params)                          +
1470     monitors * frame::interpreter_frame_monitor_size()       +
1471     max_alignment_space                                      +
1472     abi_scratch                                              +
1473     frame::ijava_state_size / Interpreter::stackElementSize;
1474 
1475   // Fixed size of an interpreter frame, align to 16-byte.
1476   return (size & -2);
1477 }
1478 
1479 // Fills a sceletal interpreter frame generated during deoptimizations.
1480 //
1481 // Parameters:
1482 //
1483 // interpreter_frame != NULL:
1484 //   set up the method, locals, and monitors.
1485 //   The frame interpreter_frame, if not NULL, is guaranteed to be the
1486 //   right size, as determined by a previous call to this method.
1487 //   It is also guaranteed to be walkable even though it is in a skeletal state
1488 //
1489 // is_top_frame == true:
1490 //   We're processing the *oldest* interpreter frame!
1491 //
1492 // pop_frame_extra_args:
1493 //   If this is != 0 we are returning to a deoptimized frame by popping
1494 //   off the callee frame. We want to re-execute the call that called the
1495 //   callee interpreted, but since the return to the interpreter would pop
1496 //   the arguments off advance the esp by dummy popframe_extra_args slots.
1497 //   Popping off those will establish the stack layout as it was before the call.
1498 //
1499 void AbstractInterpreter::layout_activation(Method* method,
1500                                             int tempcount,
1501                                             int popframe_extra_args,
1502                                             int moncount,
1503                                             int caller_actual_parameters,
1504                                             int callee_param_count,
1505                                             int callee_locals_count,
1506                                             frame* caller,
1507                                             frame* interpreter_frame,
1508                                             bool is_top_frame,
1509                                             bool is_bottom_frame) {
1510 
1511   const int abi_scratch = is_top_frame ? (frame::abi_reg_args_size / Interpreter::stackElementSize) :
1512                                          (frame::abi_minframe_size / Interpreter::stackElementSize);
1513 
1514   intptr_t* locals_base  = (caller->is_interpreted_frame()) ?
1515     caller->interpreter_frame_esp() + caller_actual_parameters :
1516     caller->sp() + method->max_locals() - 1 + (frame::abi_minframe_size / Interpreter::stackElementSize) ;
1517 
1518   intptr_t* monitor_base = caller->sp() - frame::ijava_state_size / Interpreter::stackElementSize ;
1519   intptr_t* monitor      = monitor_base - (moncount * frame::interpreter_frame_monitor_size());
1520   intptr_t* esp_base     = monitor - 1;
1521   intptr_t* esp          = esp_base - tempcount - popframe_extra_args;
1522   intptr_t* sp           = (intptr_t *) (((intptr_t) (esp_base - callee_locals_count + callee_param_count - method->max_stack()- abi_scratch)) & -StackAlignmentInBytes);
1523   intptr_t* sender_sp    = caller->sp() + (frame::abi_minframe_size - frame::abi_reg_args_size) / Interpreter::stackElementSize;
1524   intptr_t* top_frame_sp = is_top_frame ? sp : sp + (frame::abi_minframe_size - frame::abi_reg_args_size) / Interpreter::stackElementSize;
1525 
1526   interpreter_frame->interpreter_frame_set_method(method);
1527   interpreter_frame->interpreter_frame_set_locals(locals_base);
1528   interpreter_frame->interpreter_frame_set_cpcache(method->constants()->cache());
1529   interpreter_frame->interpreter_frame_set_esp(esp);
1530   interpreter_frame->interpreter_frame_set_monitor_end((BasicObjectLock *)monitor);
1531   interpreter_frame->interpreter_frame_set_top_frame_sp(top_frame_sp);
1532   if (!is_bottom_frame) {
1533     interpreter_frame->interpreter_frame_set_sender_sp(sender_sp);
1534   }
1535 }
1536 
1537 // =============================================================================
1538 // Exceptions
1539 
1540 void TemplateInterpreterGenerator::generate_throw_exception() {
1541   Register Rexception    = R17_tos,
1542            Rcontinuation = R3_RET;
1543 
1544   // --------------------------------------------------------------------------
1545   // Entry point if an method returns with a pending exception (rethrow).
1546   Interpreter::_rethrow_exception_entry = __ pc();
1547   {
1548     __ restore_interpreter_state(R11_scratch1); // Sets R11_scratch1 = fp.
1549     __ ld(R12_scratch2, _ijava_state_neg(top_frame_sp), R11_scratch1);
1550     __ resize_frame_absolute(R12_scratch2, R11_scratch1, R0);
1551 
1552     // Compiled code destroys templateTableBase, reload.
1553     __ load_const_optimized(R25_templateTableBase, (address)Interpreter::dispatch_table((TosState)0), R11_scratch1);
1554   }
1555 
1556   // Entry point if a interpreted method throws an exception (throw).
1557   Interpreter::_throw_exception_entry = __ pc();
1558   {
1559     __ mr(Rexception, R3_RET);
1560 
1561     __ verify_thread();
1562     __ verify_oop(Rexception);
1563 
1564     // Expression stack must be empty before entering the VM in case of an exception.
1565     __ empty_expression_stack();
1566     // Find exception handler address and preserve exception oop.
1567     // Call C routine to find handler and jump to it.
1568     __ call_VM(Rexception, CAST_FROM_FN_PTR(address, InterpreterRuntime::exception_handler_for_exception), Rexception);
1569     __ mtctr(Rcontinuation);
1570     // Push exception for exception handler bytecodes.
1571     __ push_ptr(Rexception);
1572 
1573     // Jump to exception handler (may be remove activation entry!).
1574     __ bctr();
1575   }
1576 
1577   // If the exception is not handled in the current frame the frame is
1578   // removed and the exception is rethrown (i.e. exception
1579   // continuation is _rethrow_exception).
1580   //
1581   // Note: At this point the bci is still the bxi for the instruction
1582   // which caused the exception and the expression stack is
1583   // empty. Thus, for any VM calls at this point, GC will find a legal
1584   // oop map (with empty expression stack).
1585 
1586   // In current activation
1587   // tos: exception
1588   // bcp: exception bcp
1589 
1590   // --------------------------------------------------------------------------
1591   // JVMTI PopFrame support
1592 
1593   Interpreter::_remove_activation_preserving_args_entry = __ pc();
1594   {
1595     // Set the popframe_processing bit in popframe_condition indicating that we are
1596     // currently handling popframe, so that call_VMs that may happen later do not
1597     // trigger new popframe handling cycles.
1598     __ lwz(R11_scratch1, in_bytes(JavaThread::popframe_condition_offset()), R16_thread);
1599     __ ori(R11_scratch1, R11_scratch1, JavaThread::popframe_processing_bit);
1600     __ stw(R11_scratch1, in_bytes(JavaThread::popframe_condition_offset()), R16_thread);
1601 
1602     // Empty the expression stack, as in normal exception handling.
1603     __ empty_expression_stack();
1604     __ unlock_if_synchronized_method(vtos, /* throw_monitor_exception */ false, /* install_monitor_exception */ false);
1605 
1606     // Check to see whether we are returning to a deoptimized frame.
1607     // (The PopFrame call ensures that the caller of the popped frame is
1608     // either interpreted or compiled and deoptimizes it if compiled.)
1609     // Note that we don't compare the return PC against the
1610     // deoptimization blob's unpack entry because of the presence of
1611     // adapter frames in C2.
1612     Label Lcaller_not_deoptimized;
1613     Register return_pc = R3_ARG1;
1614     __ ld(return_pc, 0, R1_SP);
1615     __ ld(return_pc, _abi(lr), return_pc);
1616     __ call_VM_leaf(CAST_FROM_FN_PTR(address, InterpreterRuntime::interpreter_contains), return_pc);
1617     __ cmpdi(CCR0, R3_RET, 0);
1618     __ bne(CCR0, Lcaller_not_deoptimized);
1619 
1620     // The deoptimized case.
1621     // In this case, we can't call dispatch_next() after the frame is
1622     // popped, but instead must save the incoming arguments and restore
1623     // them after deoptimization has occurred.
1624     __ ld(R4_ARG2, in_bytes(Method::const_offset()), R19_method);
1625     __ lhz(R4_ARG2 /* number of params */, in_bytes(ConstMethod::size_of_parameters_offset()), R4_ARG2);
1626     __ slwi(R4_ARG2, R4_ARG2, Interpreter::logStackElementSize);
1627     __ addi(R5_ARG3, R18_locals, Interpreter::stackElementSize);
1628     __ subf(R5_ARG3, R4_ARG2, R5_ARG3);
1629     // Save these arguments.
1630     __ call_VM_leaf(CAST_FROM_FN_PTR(address, Deoptimization::popframe_preserve_args), R16_thread, R4_ARG2, R5_ARG3);
1631 
1632     // Inform deoptimization that it is responsible for restoring these arguments.
1633     __ load_const_optimized(R11_scratch1, JavaThread::popframe_force_deopt_reexecution_bit);
1634     __ stw(R11_scratch1, in_bytes(JavaThread::popframe_condition_offset()), R16_thread);
1635 
1636     // Return from the current method into the deoptimization blob. Will eventually
1637     // end up in the deopt interpeter entry, deoptimization prepared everything that
1638     // we will reexecute the call that called us.
1639     __ merge_frames(/*top_frame_sp*/ R21_sender_SP, /*reload return_pc*/ return_pc, R11_scratch1, R12_scratch2);
1640     __ mtlr(return_pc);
1641     __ blr();
1642 
1643     // The non-deoptimized case.
1644     __ bind(Lcaller_not_deoptimized);
1645 
1646     // Clear the popframe condition flag.
1647     __ li(R0, 0);
1648     __ stw(R0, in_bytes(JavaThread::popframe_condition_offset()), R16_thread);
1649 
1650     // Get out of the current method and re-execute the call that called us.
1651     __ merge_frames(/*top_frame_sp*/ R21_sender_SP, /*return_pc*/ noreg, R11_scratch1, R12_scratch2);
1652     __ restore_interpreter_state(R11_scratch1);
1653     __ ld(R12_scratch2, _ijava_state_neg(top_frame_sp), R11_scratch1);
1654     __ resize_frame_absolute(R12_scratch2, R11_scratch1, R0);
1655     if (ProfileInterpreter) {
1656       __ set_method_data_pointer_for_bcp();
1657       __ ld(R11_scratch1, 0, R1_SP);
1658       __ std(R28_mdx, _ijava_state_neg(mdx), R11_scratch1);
1659     }
1660 #if INCLUDE_JVMTI
1661     Label L_done;
1662 
1663     __ lbz(R11_scratch1, 0, R14_bcp);
1664     __ cmpwi(CCR0, R11_scratch1, Bytecodes::_invokestatic);
1665     __ bne(CCR0, L_done);
1666 
1667     // The member name argument must be restored if _invokestatic is re-executed after a PopFrame call.
1668     // Detect such a case in the InterpreterRuntime function and return the member name argument, or NULL.
1669     __ ld(R4_ARG2, 0, R18_locals);
1670     __ MacroAssembler::call_VM(R4_ARG2, CAST_FROM_FN_PTR(address, InterpreterRuntime::member_name_arg_or_null), R4_ARG2, R19_method, R14_bcp, false);
1671     __ restore_interpreter_state(R11_scratch1, /*bcp_and_mdx_only*/ true);
1672     __ cmpdi(CCR0, R4_ARG2, 0);
1673     __ beq(CCR0, L_done);
1674     __ std(R4_ARG2, wordSize, R15_esp);
1675     __ bind(L_done);
1676 #endif // INCLUDE_JVMTI
1677     __ dispatch_next(vtos);
1678   }
1679   // end of JVMTI PopFrame support
1680 
1681   // --------------------------------------------------------------------------
1682   // Remove activation exception entry.
1683   // This is jumped to if an interpreted method can't handle an exception itself
1684   // (we come from the throw/rethrow exception entry above). We're going to call
1685   // into the VM to find the exception handler in the caller, pop the current
1686   // frame and return the handler we calculated.
1687   Interpreter::_remove_activation_entry = __ pc();
1688   {
1689     __ pop_ptr(Rexception);
1690     __ verify_thread();
1691     __ verify_oop(Rexception);
1692     __ std(Rexception, in_bytes(JavaThread::vm_result_offset()), R16_thread);
1693 
1694     __ unlock_if_synchronized_method(vtos, /* throw_monitor_exception */ false, true);
1695     __ notify_method_exit(false, vtos, InterpreterMacroAssembler::SkipNotifyJVMTI, false);
1696 
1697     __ get_vm_result(Rexception);
1698 
1699     // We are done with this activation frame; find out where to go next.
1700     // The continuation point will be an exception handler, which expects
1701     // the following registers set up:
1702     //
1703     // RET:  exception oop
1704     // ARG2: Issuing PC (see generate_exception_blob()), only used if the caller is compiled.
1705 
1706     Register return_pc = R31; // Needs to survive the runtime call.
1707     __ ld(return_pc, 0, R1_SP);
1708     __ ld(return_pc, _abi(lr), return_pc);
1709     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), R16_thread, return_pc);
1710 
1711     // Remove the current activation.
1712     __ merge_frames(/*top_frame_sp*/ R21_sender_SP, /*return_pc*/ noreg, R11_scratch1, R12_scratch2);
1713 
1714     __ mr(R4_ARG2, return_pc);
1715     __ mtlr(R3_RET);
1716     __ mr(R3_RET, Rexception);
1717     __ blr();
1718   }
1719 }
1720 
1721 // JVMTI ForceEarlyReturn support.
1722 // Returns "in the middle" of a method with a "fake" return value.
1723 address TemplateInterpreterGenerator::generate_earlyret_entry_for(TosState state) {
1724 
1725   Register Rscratch1 = R11_scratch1,
1726            Rscratch2 = R12_scratch2;
1727 
1728   address entry = __ pc();
1729   __ empty_expression_stack();
1730 
1731   __ load_earlyret_value(state, Rscratch1);
1732 
1733   __ ld(Rscratch1, in_bytes(JavaThread::jvmti_thread_state_offset()), R16_thread);
1734   // Clear the earlyret state.
1735   __ li(R0, 0);
1736   __ stw(R0, in_bytes(JvmtiThreadState::earlyret_state_offset()), Rscratch1);
1737 
1738   __ remove_activation(state, false, false);
1739   // Copied from TemplateTable::_return.
1740   // Restoration of lr done by remove_activation.
1741   switch (state) {
1742     case ltos:
1743     case btos:
1744     case ctos:
1745     case stos:
1746     case atos:
1747     case itos: __ mr(R3_RET, R17_tos); break;
1748     case ftos:
1749     case dtos: __ fmr(F1_RET, F15_ftos); break;
1750     case vtos: // This might be a constructor. Final fields (and volatile fields on PPC64) need
1751                // to get visible before the reference to the object gets stored anywhere.
1752                __ membar(Assembler::StoreStore); break;
1753     default  : ShouldNotReachHere();
1754   }
1755   __ blr();
1756 
1757   return entry;
1758 } // end of ForceEarlyReturn support
1759 
1760 //-----------------------------------------------------------------------------
1761 // Helper for vtos entry point generation
1762 
1763 void TemplateInterpreterGenerator::set_vtos_entry_points(Template* t,
1764                                                          address& bep,
1765                                                          address& cep,
1766                                                          address& sep,
1767                                                          address& aep,
1768                                                          address& iep,
1769                                                          address& lep,
1770                                                          address& fep,
1771                                                          address& dep,
1772                                                          address& vep) {
1773   assert(t->is_valid() && t->tos_in() == vtos, "illegal template");
1774   Label L;
1775 
1776   aep = __ pc();  __ push_ptr();  __ b(L);
1777   fep = __ pc();  __ push_f();    __ b(L);
1778   dep = __ pc();  __ push_d();    __ b(L);
1779   lep = __ pc();  __ push_l();    __ b(L);
1780   __ align(32, 12, 24); // align L
1781   bep = cep = sep =
1782   iep = __ pc();  __ push_i();
1783   vep = __ pc();
1784   __ bind(L);
1785   generate_and_dispatch(t);
1786 }
1787 
1788 //-----------------------------------------------------------------------------
1789 // Generation of individual instructions
1790 
1791 // helpers for generate_and_dispatch
1792 
1793 InterpreterGenerator::InterpreterGenerator(StubQueue* code)
1794   : TemplateInterpreterGenerator(code) {
1795   generate_all(); // Down here so it can be "virtual".
1796 }
1797 
1798 //-----------------------------------------------------------------------------
1799 
1800 // Non-product code
1801 #ifndef PRODUCT
1802 address TemplateInterpreterGenerator::generate_trace_code(TosState state) {
1803   //__ flush_bundle();
1804   address entry = __ pc();
1805 
1806   const char *bname = NULL;
1807   uint tsize = 0;
1808   switch(state) {
1809   case ftos:
1810     bname = "trace_code_ftos {";
1811     tsize = 2;
1812     break;
1813   case btos:
1814     bname = "trace_code_btos {";
1815     tsize = 2;
1816     break;
1817   case ctos:
1818     bname = "trace_code_ctos {";
1819     tsize = 2;
1820     break;
1821   case stos:
1822     bname = "trace_code_stos {";
1823     tsize = 2;
1824     break;
1825   case itos:
1826     bname = "trace_code_itos {";
1827     tsize = 2;
1828     break;
1829   case ltos:
1830     bname = "trace_code_ltos {";
1831     tsize = 3;
1832     break;
1833   case atos:
1834     bname = "trace_code_atos {";
1835     tsize = 2;
1836     break;
1837   case vtos:
1838     // Note: In case of vtos, the topmost of stack value could be a int or doubl
1839     // In case of a double (2 slots) we won't see the 2nd stack value.
1840     // Maybe we simply should print the topmost 3 stack slots to cope with the problem.
1841     bname = "trace_code_vtos {";
1842     tsize = 2;
1843 
1844     break;
1845   case dtos:
1846     bname = "trace_code_dtos {";
1847     tsize = 3;
1848     break;
1849   default:
1850     ShouldNotReachHere();
1851   }
1852   BLOCK_COMMENT(bname);
1853 
1854   // Support short-cut for TraceBytecodesAt.
1855   // Don't call into the VM if we don't want to trace to speed up things.
1856   Label Lskip_vm_call;
1857   if (TraceBytecodesAt > 0 && TraceBytecodesAt < max_intx) {
1858     int offs1 = __ load_const_optimized(R11_scratch1, (address) &TraceBytecodesAt, R0, true);
1859     int offs2 = __ load_const_optimized(R12_scratch2, (address) &BytecodeCounter::_counter_value, R0, true);
1860     __ ld(R11_scratch1, offs1, R11_scratch1);
1861     __ lwa(R12_scratch2, offs2, R12_scratch2);
1862     __ cmpd(CCR0, R12_scratch2, R11_scratch1);
1863     __ blt(CCR0, Lskip_vm_call);
1864   }
1865 
1866   __ push(state);
1867   // Load 2 topmost expression stack values.
1868   __ ld(R6_ARG4, tsize*Interpreter::stackElementSize, R15_esp);
1869   __ ld(R5_ARG3, Interpreter::stackElementSize, R15_esp);
1870   __ mflr(R31);
1871   __ call_VM(noreg, CAST_FROM_FN_PTR(address, SharedRuntime::trace_bytecode), /* unused */ R4_ARG2, R5_ARG3, R6_ARG4, false);
1872   __ mtlr(R31);
1873   __ pop(state);
1874 
1875   if (TraceBytecodesAt > 0 && TraceBytecodesAt < max_intx) {
1876     __ bind(Lskip_vm_call);
1877   }
1878   __ blr();
1879   BLOCK_COMMENT("} trace_code");
1880   return entry;
1881 }
1882 
1883 void TemplateInterpreterGenerator::count_bytecode() {
1884   int offs = __ load_const_optimized(R11_scratch1, (address) &BytecodeCounter::_counter_value, R12_scratch2, true);
1885   __ lwz(R12_scratch2, offs, R11_scratch1);
1886   __ addi(R12_scratch2, R12_scratch2, 1);
1887   __ stw(R12_scratch2, offs, R11_scratch1);
1888 }
1889 
1890 void TemplateInterpreterGenerator::histogram_bytecode(Template* t) {
1891   int offs = __ load_const_optimized(R11_scratch1, (address) &BytecodeHistogram::_counters[t->bytecode()], R12_scratch2, true);
1892   __ lwz(R12_scratch2, offs, R11_scratch1);
1893   __ addi(R12_scratch2, R12_scratch2, 1);
1894   __ stw(R12_scratch2, offs, R11_scratch1);
1895 }
1896 
1897 void TemplateInterpreterGenerator::histogram_bytecode_pair(Template* t) {
1898   const Register addr = R11_scratch1,
1899                  tmp  = R12_scratch2;
1900   // Get index, shift out old bytecode, bring in new bytecode, and store it.
1901   // _index = (_index >> log2_number_of_codes) |
1902   //          (bytecode << log2_number_of_codes);
1903   int offs1 = __ load_const_optimized(addr, (address)&BytecodePairHistogram::_index, tmp, true);
1904   __ lwz(tmp, offs1, addr);
1905   __ srwi(tmp, tmp, BytecodePairHistogram::log2_number_of_codes);
1906   __ ori(tmp, tmp, ((int) t->bytecode()) << BytecodePairHistogram::log2_number_of_codes);
1907   __ stw(tmp, offs1, addr);
1908 
1909   // Bump bucket contents.
1910   // _counters[_index] ++;
1911   int offs2 = __ load_const_optimized(addr, (address)&BytecodePairHistogram::_counters, R0, true);
1912   __ sldi(tmp, tmp, LogBytesPerInt);
1913   __ add(addr, tmp, addr);
1914   __ lwz(tmp, offs2, addr);
1915   __ addi(tmp, tmp, 1);
1916   __ stw(tmp, offs2, addr);
1917 }
1918 
1919 void TemplateInterpreterGenerator::trace_bytecode(Template* t) {
1920   // Call a little run-time stub to avoid blow-up for each bytecode.
1921   // The run-time runtime saves the right registers, depending on
1922   // the tosca in-state for the given template.
1923 
1924   assert(Interpreter::trace_code(t->tos_in()) != NULL,
1925          "entry must have been generated");
1926 
1927   // Note: we destroy LR here.
1928   __ bl(Interpreter::trace_code(t->tos_in()));
1929 }
1930 
1931 void TemplateInterpreterGenerator::stop_interpreter_at() {
1932   Label L;
1933   int offs1 = __ load_const_optimized(R11_scratch1, (address) &StopInterpreterAt, R0, true);
1934   int offs2 = __ load_const_optimized(R12_scratch2, (address) &BytecodeCounter::_counter_value, R0, true);
1935   __ ld(R11_scratch1, offs1, R11_scratch1);
1936   __ lwa(R12_scratch2, offs2, R12_scratch2);
1937   __ cmpd(CCR0, R12_scratch2, R11_scratch1);
1938   __ bne(CCR0, L);
1939   __ illtrap();
1940   __ bind(L);
1941 }
1942 
1943 #endif // !PRODUCT
1944 #endif // !CC_INTERP