1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2012, 2013 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 #include "asm/assembler.hpp"
  28 #include "asm/macroAssembler.inline.hpp"
  29 #include "interpreter/bytecodeHistogram.hpp"
  30 #include "interpreter/cppInterpreter.hpp"
  31 #include "interpreter/interpreter.hpp"
  32 #include "interpreter/interpreterGenerator.hpp"
  33 #include "interpreter/interpreterRuntime.hpp"
  34 #include "oops/arrayOop.hpp"
  35 #include "oops/methodData.hpp"
  36 #include "oops/method.hpp"
  37 #include "oops/oop.inline.hpp"
  38 #include "prims/jvmtiExport.hpp"
  39 #include "prims/jvmtiThreadState.hpp"
  40 #include "runtime/arguments.hpp"
  41 #include "runtime/deoptimization.hpp"
  42 #include "runtime/frame.inline.hpp"
  43 #include "runtime/interfaceSupport.hpp"
  44 #include "runtime/sharedRuntime.hpp"
  45 #include "runtime/stubRoutines.hpp"
  46 #include "runtime/synchronizer.hpp"
  47 #include "runtime/timer.hpp"
  48 #include "runtime/vframeArray.hpp"
  49 #include "utilities/debug.hpp"
  50 #ifdef SHARK
  51 #include "shark/shark_globals.hpp"
  52 #endif
  53 
  54 #ifdef CC_INTERP
  55 
  56 #define __ _masm->
  57 
  58 // Contains is used for identifying interpreter frames during a stack-walk.
  59 // A frame with a PC in InterpretMethod must be identified as a normal C frame.
  60 bool CppInterpreter::contains(address pc) {
  61   return _code->contains(pc);
  62 }
  63 
  64 #ifdef PRODUCT
  65 #define BLOCK_COMMENT(str) // nothing
  66 #else
  67 #define BLOCK_COMMENT(str) __ block_comment(str)
  68 #endif
  69 
  70 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
  71 
  72 static address interpreter_frame_manager        = NULL;
  73 static address frame_manager_specialized_return = NULL;
  74 static address native_entry                     = NULL;
  75 
  76 static address interpreter_return_address       = NULL;
  77 
  78 static address unctrap_frame_manager_entry      = NULL;
  79 
  80 static address deopt_frame_manager_return_atos  = NULL;
  81 static address deopt_frame_manager_return_btos  = NULL;
  82 static address deopt_frame_manager_return_itos  = NULL;
  83 static address deopt_frame_manager_return_ltos  = NULL;
  84 static address deopt_frame_manager_return_ftos  = NULL;
  85 static address deopt_frame_manager_return_dtos  = NULL;
  86 static address deopt_frame_manager_return_vtos  = NULL;
  87 
  88 // A result handler converts/unboxes a native call result into
  89 // a java interpreter/compiler result. The current frame is an
  90 // interpreter frame.
  91 address CppInterpreterGenerator::generate_result_handler_for(BasicType type) {
  92   return AbstractInterpreterGenerator::generate_result_handler_for(type);
  93 }
  94 
  95 // tosca based result to c++ interpreter stack based result.
  96 address CppInterpreterGenerator::generate_tosca_to_stack_converter(BasicType type) {
  97   //
  98   // A result is in the native abi result register from a native
  99   // method call. We need to return this result to the interpreter by
 100   // pushing the result on the interpreter's stack.
 101   //
 102   // Registers alive:
 103   //   R3_ARG1(R3_RET)/F1_ARG1(F1_RET) - result to move
 104   //   R4_ARG2                         - address of tos
 105   //   LR
 106   //
 107   // Registers updated:
 108   //   R3_RET(R3_ARG1)   - address of new tos (== R17_tos for T_VOID)
 109   //
 110 
 111   int number_of_used_slots = 1;
 112 
 113   const Register tos = R4_ARG2;
 114   Label done;
 115   Label is_false;
 116 
 117   address entry = __ pc();
 118 
 119   switch (type) {
 120   case T_BOOLEAN:
 121     __ cmpwi(CCR0, R3_RET, 0);
 122     __ beq(CCR0, is_false);
 123     __ li(R3_RET, 1);
 124     __ stw(R3_RET, 0, tos);
 125     __ b(done);
 126     __ bind(is_false);
 127     __ li(R3_RET, 0);
 128     __ stw(R3_RET, 0, tos);
 129     break;
 130   case T_BYTE:
 131   case T_CHAR:
 132   case T_SHORT:
 133   case T_INT:
 134     __ stw(R3_RET, 0, tos);
 135     break;
 136   case T_LONG:
 137     number_of_used_slots = 2;
 138     // mark unused slot for debugging
 139     // long goes to topmost slot
 140     __ std(R3_RET, -BytesPerWord, tos);
 141     __ li(R3_RET, 0);
 142     __ std(R3_RET, 0, tos);
 143     break;
 144   case T_OBJECT:
 145     __ verify_oop(R3_RET);
 146     __ std(R3_RET, 0, tos);
 147     break;
 148   case T_FLOAT:
 149     __ stfs(F1_RET, 0, tos);
 150     break;
 151   case T_DOUBLE:
 152     number_of_used_slots = 2;
 153     // mark unused slot for debugging
 154     __ li(R3_RET, 0);
 155     __ std(R3_RET, 0, tos);
 156     // double goes to topmost slot
 157     __ stfd(F1_RET, -BytesPerWord, tos);
 158     break;
 159   case T_VOID:
 160     number_of_used_slots = 0;
 161     break;
 162   default:
 163     ShouldNotReachHere();
 164   }
 165 
 166   __ BIND(done);
 167 
 168   // new expression stack top
 169   __ addi(R3_RET, tos, -BytesPerWord * number_of_used_slots);
 170 
 171   __ blr();
 172 
 173   return entry;
 174 }
 175 
 176 address CppInterpreterGenerator::generate_stack_to_stack_converter(BasicType type) {
 177   //
 178   // Copy the result from the callee's stack to the caller's stack,
 179   // caller and callee both being interpreted.
 180   //
 181   // Registers alive
 182   //   R3_ARG1        - address of callee's tos + BytesPerWord
 183   //   R4_ARG2        - address of caller's tos [i.e. free location]
 184   //   LR
 185   //
 186   //   stack grows upwards, memory grows downwards.
 187   //
 188   //   [      free         ]  <-- callee's tos
 189   //   [  optional result  ]  <-- R3_ARG1
 190   //   [  optional dummy   ]
 191   //          ...
 192   //   [      free         ]  <-- caller's tos, R4_ARG2
 193   //          ...
 194   // Registers updated
 195   //   R3_RET(R3_ARG1) - address of caller's new tos
 196   //
 197   //   stack grows upwards, memory grows downwards.
 198   //
 199   //   [      free         ]  <-- current tos, R3_RET
 200   //   [  optional result  ]
 201   //   [  optional dummy   ]
 202   //          ...
 203   //
 204 
 205   const Register from = R3_ARG1;
 206   const Register ret  = R3_ARG1;
 207   const Register tos  = R4_ARG2;
 208   const Register tmp1 = R21_tmp1;
 209   const Register tmp2 = R22_tmp2;
 210 
 211   address entry = __ pc();
 212 
 213   switch (type) {
 214   case T_BOOLEAN:
 215   case T_BYTE:
 216   case T_CHAR:
 217   case T_SHORT:
 218   case T_INT:
 219   case T_FLOAT:
 220     __ lwz(tmp1, 0, from);
 221     __ stw(tmp1, 0, tos);
 222     // New expression stack top.
 223     __ addi(ret, tos, - BytesPerWord);
 224     break;
 225   case T_LONG:
 226   case T_DOUBLE:
 227     // Move both entries for debug purposes even though only one is live.
 228     __ ld(tmp1, BytesPerWord, from);
 229     __ ld(tmp2, 0, from);
 230     __ std(tmp1, 0, tos);
 231     __ std(tmp2, -BytesPerWord, tos);
 232     // New expression stack top.
 233     __ addi(ret, tos, - 2 * BytesPerWord); // two slots
 234     break;
 235   case T_OBJECT:
 236     __ ld(tmp1, 0, from);
 237     __ verify_oop(tmp1);
 238     __ std(tmp1, 0, tos);
 239     // New expression stack top.
 240     __ addi(ret, tos, - BytesPerWord);
 241     break;
 242   case T_VOID:
 243     // New expression stack top.
 244     __ mr(ret, tos);
 245     break;
 246   default:
 247     ShouldNotReachHere();
 248   }
 249 
 250   __ blr();
 251 
 252   return entry;
 253 }
 254 
 255 address CppInterpreterGenerator::generate_stack_to_native_abi_converter(BasicType type) {
 256   //
 257   // Load a result from the callee's stack into the caller's expecting
 258   // return register, callee being interpreted, caller being call stub
 259   // or jit code.
 260   //
 261   // Registers alive
 262   //   R3_ARG1   - callee expression tos + BytesPerWord
 263   //   LR
 264   //
 265   //   stack grows upwards, memory grows downwards.
 266   //
 267   //   [      free         ]  <-- callee's tos
 268   //   [  optional result  ]  <-- R3_ARG1
 269   //   [  optional dummy   ]
 270   //          ...
 271   //
 272   // Registers updated
 273   //   R3_RET(R3_ARG1)/F1_RET - result
 274   //
 275 
 276   const Register from = R3_ARG1;
 277   const Register ret = R3_ARG1;
 278   const FloatRegister fret = F1_ARG1;
 279 
 280   address entry = __ pc();
 281 
 282   // Implemented uniformly for both kinds of endianness. The interpreter
 283   // implements boolean, byte, char, and short as jint (4 bytes).
 284   switch (type) {
 285   case T_BOOLEAN:
 286   case T_CHAR:
 287     // zero extension
 288     __ lwz(ret, 0, from);
 289     break;
 290   case T_BYTE:
 291   case T_SHORT:
 292   case T_INT:
 293     // sign extension
 294     __ lwa(ret, 0, from);
 295     break;
 296   case T_LONG:
 297     __ ld(ret, 0, from);
 298     break;
 299   case T_OBJECT:
 300     __ ld(ret, 0, from);
 301     __ verify_oop(ret);
 302     break;
 303   case T_FLOAT:
 304     __ lfs(fret, 0, from);
 305     break;
 306   case T_DOUBLE:
 307     __ lfd(fret, 0, from);
 308     break;
 309   case T_VOID:
 310     break;
 311   default:
 312     ShouldNotReachHere();
 313   }
 314 
 315   __ blr();
 316 
 317   return entry;
 318 }
 319 
 320 address CppInterpreter::return_entry(TosState state, int length, Bytecodes::Code code) {
 321   assert(interpreter_return_address != NULL, "Not initialized");
 322   return interpreter_return_address;
 323 }
 324 
 325 address CppInterpreter::deopt_entry(TosState state, int length) {
 326   address ret = NULL;
 327   if (length != 0) {
 328     switch (state) {
 329       case atos: ret = deopt_frame_manager_return_atos; break;
 330       case btos: ret = deopt_frame_manager_return_itos; break;
 331       case ctos:
 332       case stos:
 333       case itos: ret = deopt_frame_manager_return_itos; break;
 334       case ltos: ret = deopt_frame_manager_return_ltos; break;
 335       case ftos: ret = deopt_frame_manager_return_ftos; break;
 336       case dtos: ret = deopt_frame_manager_return_dtos; break;
 337       case vtos: ret = deopt_frame_manager_return_vtos; break;
 338       default: ShouldNotReachHere();
 339     }
 340   } else {
 341     ret = unctrap_frame_manager_entry;  // re-execute the bytecode (e.g. uncommon trap, popframe)
 342   }
 343   assert(ret != NULL, "Not initialized");
 344   return ret;
 345 }
 346 
 347 //
 348 // Helpers for commoning out cases in the various type of method entries.
 349 //
 350 
 351 //
 352 // Registers alive
 353 //   R16_thread      - JavaThread*
 354 //   R1_SP           - old stack pointer
 355 //   R19_method      - callee's Method
 356 //   R17_tos         - address of caller's tos (prepushed)
 357 //   R15_prev_state  - address of caller's BytecodeInterpreter or 0
 358 //   return_pc in R21_tmp15 (only when called within generate_native_entry)
 359 //
 360 // Registers updated
 361 //   R14_state       - address of callee's interpreter state
 362 //   R1_SP           - new stack pointer
 363 //   CCR4_is_synced  - current method is synchronized
 364 //
 365 void CppInterpreterGenerator::generate_compute_interpreter_state(Label& stack_overflow_return) {
 366   //
 367   // Stack layout at this point:
 368   //
 369   //   F1      [TOP_IJAVA_FRAME_ABI]              <-- R1_SP
 370   //           alignment (optional)
 371   //           [F1's outgoing Java arguments]     <-- R17_tos
 372   //           ...
 373   //   F2      [PARENT_IJAVA_FRAME_ABI]
 374   //            ...
 375 
 376   //=============================================================================
 377   // Allocate space for locals other than the parameters, the
 378   // interpreter state, monitors, and the expression stack.
 379 
 380   const Register local_count        = R21_tmp1;
 381   const Register parameter_count    = R22_tmp2;
 382   const Register max_stack          = R23_tmp3;
 383   // Must not be overwritten within this method!
 384   // const Register return_pc         = R29_tmp9;
 385 
 386   const ConditionRegister is_synced = CCR4_is_synced;
 387   const ConditionRegister is_native = CCR6;
 388   const ConditionRegister is_static = CCR7;
 389 
 390   assert(is_synced != is_native, "condition code registers must be distinct");
 391   assert(is_synced != is_static, "condition code registers must be distinct");
 392   assert(is_native != is_static, "condition code registers must be distinct");
 393 
 394   {
 395 
 396   // Local registers
 397   const Register top_frame_size     = R24_tmp4;
 398   const Register access_flags       = R25_tmp5;
 399   const Register state_offset       = R26_tmp6;
 400   Register mem_stack_limit          = R27_tmp7;
 401   const Register page_size          = R28_tmp8;
 402 
 403   BLOCK_COMMENT("compute_interpreter_state {");
 404 
 405   // access_flags = method->access_flags();
 406   // TODO: PPC port: assert(4 == methodOopDesc::sz_access_flags(), "unexpected field size");
 407   __ lwa(access_flags, method_(access_flags));
 408 
 409   // parameter_count = method->constMethod->size_of_parameters();
 410   // TODO: PPC port: assert(2 == ConstMethod::sz_size_of_parameters(), "unexpected field size");
 411   __ ld(max_stack, in_bytes(Method::const_offset()), R19_method);   // Max_stack holds constMethod for a while.
 412   __ lhz(parameter_count, in_bytes(ConstMethod::size_of_parameters_offset()), max_stack);
 413 
 414   // local_count = method->constMethod()->max_locals();
 415   // TODO: PPC port: assert(2 == ConstMethod::sz_max_locals(), "unexpected field size");
 416   __ lhz(local_count, in_bytes(ConstMethod::size_of_locals_offset()), max_stack);
 417 
 418   // max_stack = method->constMethod()->max_stack();
 419   // TODO: PPC port: assert(2 == ConstMethod::sz_max_stack(), "unexpected field size");
 420   __ lhz(max_stack, in_bytes(ConstMethod::max_stack_offset()), max_stack);
 421 
 422   if (EnableInvokeDynamic) {
 423     // Take into account 'extra_stack_entries' needed by method handles (see method.hpp).
 424     __ addi(max_stack, max_stack, Method::extra_stack_entries());
 425   }
 426 
 427   // mem_stack_limit = thread->stack_limit();
 428   __ ld(mem_stack_limit, thread_(stack_overflow_limit));
 429 
 430   // Point locals at the first argument. Method's locals are the
 431   // parameters on top of caller's expression stack.
 432 
 433   // tos points past last Java argument
 434   __ sldi(R18_locals, parameter_count, Interpreter::logStackElementSize);
 435   __ add(R18_locals, R17_tos, R18_locals);
 436 
 437   // R18_locals - i*BytesPerWord points to i-th Java local (i starts at 0)
 438 
 439   // Set is_native, is_synced, is_static - will be used later.
 440   __ testbitdi(is_native, R0, access_flags, JVM_ACC_NATIVE_BIT);
 441   __ testbitdi(is_synced, R0, access_flags, JVM_ACC_SYNCHRONIZED_BIT);
 442   assert(is_synced->is_nonvolatile(), "is_synced must be non-volatile");
 443   __ testbitdi(is_static, R0, access_flags, JVM_ACC_STATIC_BIT);
 444 
 445   // PARENT_IJAVA_FRAME_ABI
 446   //
 447   // frame_size =
 448   //   round_to((local_count - parameter_count)*BytesPerWord +
 449   //              2*BytesPerWord +
 450   //              alignment +
 451   //              frame::interpreter_frame_cinterpreterstate_size_in_bytes()
 452   //              sizeof(PARENT_IJAVA_FRAME_ABI)
 453   //              method->is_synchronized() ? sizeof(BasicObjectLock) : 0 +
 454   //              max_stack*BytesPerWord,
 455   //            16)
 456   //
 457   // Note that this calculation is exactly mirrored by
 458   // AbstractInterpreter::layout_activation_impl() [ and
 459   // AbstractInterpreter::size_activation() ]. Which is used by
 460   // deoptimization so that it can allocate the proper sized
 461   // frame. This only happens for interpreted frames so the extra
 462   // notes below about max_stack below are not important. The other
 463   // thing to note is that for interpreter frames other than the
 464   // current activation the size of the stack is the size of the live
 465   // portion of the stack at the particular bcp and NOT the maximum
 466   // stack that the method might use.
 467   //
 468   // If we're calling a native method, we replace max_stack (which is
 469   // zero) with space for the worst-case signature handler varargs
 470   // vector, which is:
 471   //
 472   //   max_stack = max(Argument::n_register_parameters, parameter_count+2);
 473   //
 474   // We add two slots to the parameter_count, one for the jni
 475   // environment and one for a possible native mirror.  We allocate
 476   // space for at least the number of ABI registers, even though
 477   // InterpreterRuntime::slow_signature_handler won't write more than
 478   // parameter_count+2 words when it creates the varargs vector at the
 479   // top of the stack.  The generated slow signature handler will just
 480   // load trash into registers beyond the necessary number.  We're
 481   // still going to cut the stack back by the ABI register parameter
 482   // count so as to get SP+16 pointing at the ABI outgoing parameter
 483   // area, so we need to allocate at least that much even though we're
 484   // going to throw it away.
 485   //
 486 
 487   // Adjust max_stack for native methods:
 488   Label skip_native_calculate_max_stack;
 489   __ bfalse(is_native, skip_native_calculate_max_stack);
 490   // if (is_native) {
 491   //  max_stack = max(Argument::n_register_parameters, parameter_count+2);
 492   __ addi(max_stack, parameter_count, 2*Interpreter::stackElementWords);
 493   __ cmpwi(CCR0, max_stack, Argument::n_register_parameters);
 494   __ bge(CCR0, skip_native_calculate_max_stack);
 495   __ li(max_stack,  Argument::n_register_parameters);
 496   // }
 497   __ bind(skip_native_calculate_max_stack);
 498   // max_stack is now in bytes
 499   __ slwi(max_stack, max_stack, Interpreter::logStackElementSize);
 500 
 501   // Calculate number of non-parameter locals (in slots):
 502   Label not_java;
 503   __ btrue(is_native, not_java);
 504   // if (!is_native) {
 505   //   local_count = non-parameter local count
 506   __ sub(local_count, local_count, parameter_count);
 507   // } else {
 508   //   // nothing to do: method->max_locals() == 0 for native methods
 509   // }
 510   __ bind(not_java);
 511 
 512 
 513   // Calculate top_frame_size and parent_frame_resize.
 514   {
 515   const Register parent_frame_resize = R12_scratch2;
 516 
 517   BLOCK_COMMENT("Compute top_frame_size.");
 518   // top_frame_size = TOP_IJAVA_FRAME_ABI
 519   //                  + size of interpreter state
 520   __ li(top_frame_size, frame::top_ijava_frame_abi_size
 521                         + frame::interpreter_frame_cinterpreterstate_size_in_bytes());
 522   //                  + max_stack
 523   __ add(top_frame_size, top_frame_size, max_stack);
 524   //                  + stack slots for a BasicObjectLock for synchronized methods
 525   {
 526     Label not_synced;
 527     __ bfalse(is_synced, not_synced);
 528     __ addi(top_frame_size, top_frame_size, frame::interpreter_frame_monitor_size_in_bytes());
 529     __ bind(not_synced);
 530   }
 531   // align
 532   __ round_to(top_frame_size, frame::alignment_in_bytes);
 533 
 534 
 535   BLOCK_COMMENT("Compute parent_frame_resize.");
 536   // parent_frame_resize = R1_SP - R17_tos
 537   __ sub(parent_frame_resize, R1_SP, R17_tos);
 538   //__ li(parent_frame_resize, 0);
 539   //                       + PARENT_IJAVA_FRAME_ABI
 540   //                       + extra two slots for the no-parameter/no-locals
 541   //                         method result
 542   __ addi(parent_frame_resize, parent_frame_resize,
 543                                       frame::parent_ijava_frame_abi_size
 544                                     + 2*Interpreter::stackElementSize);
 545   //                       + (locals_count - params_count)
 546   __ sldi(R0, local_count, Interpreter::logStackElementSize);
 547   __ add(parent_frame_resize, parent_frame_resize, R0);
 548   // align
 549   __ round_to(parent_frame_resize, frame::alignment_in_bytes);
 550 
 551   //
 552   // Stack layout at this point:
 553   //
 554   // The new frame F0 hasn't yet been pushed, F1 is still the top frame.
 555   //
 556   //   F0      [TOP_IJAVA_FRAME_ABI]
 557   //           alignment (optional)
 558   //           [F0's full operand stack]
 559   //           [F0's monitors] (optional)
 560   //           [F0's BytecodeInterpreter object]
 561   //   F1      [PARENT_IJAVA_FRAME_ABI]
 562   //           alignment (optional)
 563   //           [F0's Java result]
 564   //           [F0's non-arg Java locals]
 565   //           [F1's outgoing Java arguments]     <-- R17_tos
 566   //           ...
 567   //   F2      [PARENT_IJAVA_FRAME_ABI]
 568   //            ...
 569 
 570 
 571   // Calculate new R14_state
 572   // and
 573   // test that the new memory stack pointer is above the limit,
 574   // throw a StackOverflowError otherwise.
 575   __ sub(R11_scratch1/*F1's SP*/,  R1_SP, parent_frame_resize);
 576   __ addi(R14_state, R11_scratch1/*F1's SP*/,
 577               -frame::interpreter_frame_cinterpreterstate_size_in_bytes());
 578   __ sub(R11_scratch1/*F0's SP*/,
 579              R11_scratch1/*F1's SP*/, top_frame_size);
 580 
 581   BLOCK_COMMENT("Test for stack overflow:");
 582   __ cmpld(CCR0/*is_stack_overflow*/, R11_scratch1, mem_stack_limit);
 583   __ blt(CCR0/*is_stack_overflow*/, stack_overflow_return);
 584 
 585 
 586   //=============================================================================
 587   // Frame_size doesn't overflow the stack. Allocate new frame and
 588   // initialize interpreter state.
 589 
 590   // Register state
 591   //
 592   //   R15            - local_count
 593   //   R16            - parameter_count
 594   //   R17            - max_stack
 595   //
 596   //   R18            - frame_size
 597   //   R19            - access_flags
 598   //   CCR4_is_synced - is_synced
 599   //
 600   //   GR_Lstate      - pointer to the uninitialized new BytecodeInterpreter.
 601 
 602   // _last_Java_pc just needs to be close enough that we can identify
 603   // the frame as an interpreted frame. It does not need to be the
 604   // exact return address from either calling
 605   // BytecodeInterpreter::InterpretMethod or the call to a jni native method.
 606   // So we can initialize it here with a value of a bundle in this
 607   // code fragment. We only do this initialization for java frames
 608   // where InterpretMethod needs a a way to get a good pc value to
 609   // store in the thread state. For interpreter frames used to call
 610   // jni native code we just zero the value in the state and move an
 611   // ip as needed in the native entry code.
 612   //
 613   // const Register last_Java_pc_addr     = GR24_SCRATCH;  // QQQ 27
 614   // const Register last_Java_pc          = GR26_SCRATCH;
 615 
 616   // Must reference stack before setting new SP since Windows
 617   // will not be able to deliver the exception on a bad SP.
 618   // Windows also insists that we bang each page one at a time in order
 619   // for the OS to map in the reserved pages. If we bang only
 620   // the final page, Windows stops delivering exceptions to our
 621   // VectoredExceptionHandler and terminates our program.
 622   // Linux only requires a single bang but it's rare to have
 623   // to bang more than 1 page so the code is enabled for both OS's.
 624 
 625   // BANG THE STACK
 626   //
 627   // Nothing to do for PPC, because updating the SP will automatically
 628   // bang the page.
 629 
 630   // Up to here we have calculated the delta for the new C-frame and
 631   // checked for a stack-overflow. Now we can savely update SP and
 632   // resize the C-frame.
 633 
 634   // R14_state has already been calculated.
 635   __ push_interpreter_frame(top_frame_size, parent_frame_resize,
 636                             R25_tmp5, R26_tmp6, R27_tmp7, R28_tmp8);
 637 
 638   }
 639 
 640   //
 641   // Stack layout at this point:
 642   //
 643   //   F0 has been been pushed!
 644   //
 645   //   F0      [TOP_IJAVA_FRAME_ABI]              <-- R1_SP
 646   //           alignment (optional)               (now it's here, if required)
 647   //           [F0's full operand stack]
 648   //           [F0's monitors] (optional)
 649   //           [F0's BytecodeInterpreter object]
 650   //   F1      [PARENT_IJAVA_FRAME_ABI]
 651   //           alignment (optional)               (now it's here, if required)
 652   //           [F0's Java result]
 653   //           [F0's non-arg Java locals]
 654   //           [F1's outgoing Java arguments]
 655   //           ...
 656   //   F2      [PARENT_IJAVA_FRAME_ABI]
 657   //           ...
 658   //
 659   // R14_state points to F0's BytecodeInterpreter object.
 660   //
 661 
 662   }
 663 
 664   //=============================================================================
 665   // new BytecodeInterpreter-object is save, let's initialize it:
 666   BLOCK_COMMENT("New BytecodeInterpreter-object is save.");
 667 
 668   {
 669   // Locals
 670   const Register bytecode_addr = R24_tmp4;
 671   const Register constants     = R25_tmp5;
 672   const Register tos           = R26_tmp6;
 673   const Register stack_base    = R27_tmp7;
 674   const Register local_addr    = R28_tmp8;
 675   {
 676     Label L;
 677     __ btrue(is_native, L);
 678     // if (!is_native) {
 679       // bytecode_addr = constMethod->codes();
 680       __ ld(bytecode_addr, method_(const));
 681       __ addi(bytecode_addr, bytecode_addr, in_bytes(ConstMethod::codes_offset()));
 682     // }
 683     __ bind(L);
 684   }
 685 
 686   __ ld(constants, in_bytes(Method::const_offset()), R19_method);
 687   __ ld(constants, in_bytes(ConstMethod::constants_offset()), constants);
 688 
 689   // state->_prev_link = prev_state;
 690   __ std(R15_prev_state, state_(_prev_link));
 691 
 692   // For assertions only.
 693   // TODO: not needed anyway because it coincides with `_monitor_base'. remove!
 694   // state->_self_link = state;
 695   DEBUG_ONLY(__ std(R14_state, state_(_self_link));)
 696 
 697   // state->_thread = thread;
 698   __ std(R16_thread, state_(_thread));
 699 
 700   // state->_method = method;
 701   __ std(R19_method, state_(_method));
 702 
 703   // state->_locals = locals;
 704   __ std(R18_locals, state_(_locals));
 705 
 706   // state->_oop_temp = NULL;
 707   __ li(R0, 0);
 708   __ std(R0, state_(_oop_temp));
 709 
 710   // state->_last_Java_fp = *R1_SP // Use *R1_SP as fp
 711   __ ld(R0, _abi(callers_sp), R1_SP);
 712   __ std(R0, state_(_last_Java_fp));
 713 
 714   BLOCK_COMMENT("load Stack base:");
 715   {
 716     // Stack_base.
 717     // if (!method->synchronized()) {
 718     //   stack_base = state;
 719     // } else {
 720     //   stack_base = (uintptr_t)state - sizeof(BasicObjectLock);
 721     // }
 722     Label L;
 723     __ mr(stack_base, R14_state);
 724     __ bfalse(is_synced, L);
 725     __ addi(stack_base, stack_base, -frame::interpreter_frame_monitor_size_in_bytes());
 726     __ bind(L);
 727   }
 728 
 729   // state->_mdx = NULL;
 730   __ li(R0, 0);
 731   __ std(R0, state_(_mdx));
 732 
 733   {
 734     // if (method->is_native()) state->_bcp = NULL;
 735     // else state->_bcp = bytecode_addr;
 736     Label label1, label2;
 737     __ bfalse(is_native, label1);
 738     __ std(R0, state_(_bcp));
 739     __ b(label2);
 740     __ bind(label1);
 741     __ std(bytecode_addr, state_(_bcp));
 742     __ bind(label2);
 743   }
 744 
 745 
 746   // state->_result._to_call._callee = NULL;
 747   __ std(R0, state_(_result._to_call._callee));
 748 
 749   // state->_monitor_base = state;
 750   __ std(R14_state, state_(_monitor_base));
 751 
 752   // state->_msg = BytecodeInterpreter::method_entry;
 753   __ li(R0, BytecodeInterpreter::method_entry);
 754   __ stw(R0, state_(_msg));
 755 
 756   // state->_last_Java_sp = R1_SP;
 757   __ std(R1_SP, state_(_last_Java_sp));
 758 
 759   // state->_stack_base = stack_base;
 760   __ std(stack_base, state_(_stack_base));
 761 
 762   // tos = stack_base - 1 slot (prepushed);
 763   // state->_stack.Tos(tos);
 764   __ addi(tos, stack_base, - Interpreter::stackElementSize);
 765   __ std(tos,  state_(_stack));
 766 
 767 
 768   {
 769     BLOCK_COMMENT("get last_Java_pc:");
 770     // if (!is_native) state->_last_Java_pc = <some_ip_in_this_code_buffer>;
 771     // else state->_last_Java_pc = NULL; (just for neatness)
 772     Label label1, label2;
 773     __ btrue(is_native, label1);
 774     __ get_PC_trash_LR(R0);
 775     __ std(R0, state_(_last_Java_pc));
 776     __ b(label2);
 777     __ bind(label1);
 778     __ li(R0, 0);
 779     __ std(R0, state_(_last_Java_pc));
 780     __ bind(label2);
 781   }
 782 
 783 
 784   // stack_limit = tos - max_stack;
 785   __ sub(R0, tos, max_stack);
 786   // state->_stack_limit = stack_limit;
 787   __ std(R0, state_(_stack_limit));
 788 
 789 
 790   // cache = method->constants()->cache();
 791    __ ld(R0, ConstantPool::cache_offset_in_bytes(), constants);
 792   // state->_constants = method->constants()->cache();
 793   __ std(R0, state_(_constants));
 794 
 795 
 796 
 797   //=============================================================================
 798   // synchronized method, allocate and initialize method object lock.
 799   // if (!method->is_synchronized()) goto fill_locals_with_0x0s;
 800   Label fill_locals_with_0x0s;
 801   __ bfalse(is_synced, fill_locals_with_0x0s);
 802 
 803   //   pool_holder = method->constants()->pool_holder();
 804   const int mirror_offset = in_bytes(Klass::java_mirror_offset());
 805   {
 806     Label label1, label2;
 807     // lockee = NULL; for java methods, correct value will be inserted in BytecodeInterpretMethod.hpp
 808     __ li(R0,0);
 809     __ bfalse(is_native, label2);
 810 
 811     __ bfalse(is_static, label1);
 812     // if (method->is_static()) lockee =
 813     // pool_holder->klass_part()->java_mirror();
 814     __ ld(R11_scratch1/*pool_holder*/, ConstantPool::pool_holder_offset_in_bytes(), constants);
 815     __ ld(R0/*lockee*/, mirror_offset, R11_scratch1/*pool_holder*/);
 816     __ b(label2);
 817 
 818     __ bind(label1);
 819     // else lockee = *(oop*)locals;
 820     __ ld(R0/*lockee*/, 0, R18_locals);
 821     __ bind(label2);
 822 
 823     // monitor->set_obj(lockee);
 824     __ std(R0/*lockee*/, BasicObjectLock::obj_offset_in_bytes(), stack_base);
 825   }
 826 
 827   // See if we need to zero the locals
 828   __ BIND(fill_locals_with_0x0s);
 829 
 830 
 831   //=============================================================================
 832   // fill locals with 0x0s
 833   Label locals_zeroed;
 834   __ btrue(is_native, locals_zeroed);
 835 
 836   if (true /* zerolocals */ || ClearInterpreterLocals) {
 837     // local_count is already num_locals_slots - num_param_slots
 838     __ sldi(R0, parameter_count, Interpreter::logStackElementSize);
 839     __ sub(local_addr, R18_locals, R0);
 840     __ cmpdi(CCR0, local_count, 0);
 841     __ ble(CCR0, locals_zeroed);
 842 
 843     __ mtctr(local_count);
 844     //__ ld_const_addr(R0, (address) 0xcafe0000babe);
 845     __ li(R0, 0);
 846 
 847     Label zero_slot;
 848     __ bind(zero_slot);
 849 
 850     // first local is at local_addr
 851     __ std(R0, 0, local_addr);
 852     __ addi(local_addr, local_addr, -BytesPerWord);
 853     __ bdnz(zero_slot);
 854   }
 855 
 856    __ BIND(locals_zeroed);
 857 
 858   }
 859   BLOCK_COMMENT("} compute_interpreter_state");
 860 }
 861 
 862 // Generate code to initiate compilation on invocation counter overflow.
 863 void CppInterpreterGenerator::generate_counter_overflow(Label& continue_entry) {
 864   // Registers alive
 865   //   R14_state
 866   //   R16_thread
 867   //
 868   // Registers updated
 869   //   R14_state
 870   //   R3_ARG1 (=R3_RET)
 871   //   R4_ARG2
 872 
 873   // After entering the vm we remove the activation and retry the
 874   // entry point in case the compilation is complete.
 875 
 876   // InterpreterRuntime::frequency_counter_overflow takes one argument
 877   // that indicates if the counter overflow occurs at a backwards
 878   // branch (NULL bcp). We pass zero. The call returns the address
 879   // of the verified entry point for the method or NULL if the
 880   // compilation did not complete (either went background or bailed
 881   // out).
 882   __ li(R4_ARG2, 0);
 883 
 884   // Pass false to call_VM so it doesn't check for pending exceptions,
 885   // since at this point in the method invocation the exception
 886   // handler would try to exit the monitor of synchronized methods
 887   // which haven't been entered yet.
 888   //
 889   // Returns verified_entry_point or NULL, we don't care which.
 890   //
 891   // Do not use the variant `frequency_counter_overflow' that returns
 892   // a structure, because this will change the argument list by a
 893   // hidden parameter (gcc 4.1).
 894 
 895   __ call_VM(noreg,
 896              CAST_FROM_FN_PTR(address, InterpreterRuntime::frequency_counter_overflow),
 897              R4_ARG2,
 898              false);
 899   // Returns verified_entry_point or NULL, we don't care which as we ignore it
 900   // and run interpreted.
 901 
 902   // Reload method, it may have moved.
 903   __ ld(R19_method, state_(_method));
 904 
 905   // We jump now to the label "continue_after_compile".
 906   __ b(continue_entry);
 907 }
 908 
 909 // Increment invocation count and check for overflow.
 910 //
 911 // R19_method must contain Method* of method to profile.
 912 void CppInterpreterGenerator::generate_counter_incr(Label& overflow) {
 913   Label done;
 914   const Register Rcounters             = R12_scratch2;
 915   const Register iv_be_count           = R11_scratch1;
 916   const Register invocation_limit      = R12_scratch2;
 917   const Register invocation_limit_addr = invocation_limit;
 918 
 919   // Load and ev. allocate MethodCounters object.
 920   __ get_method_counters(R19_method, Rcounters, done);
 921 
 922   // Update standard invocation counters.
 923   __ increment_invocation_counter(Rcounters, iv_be_count, R0);
 924 
 925   // Compare against limit.
 926   BLOCK_COMMENT("Compare counter against limit:");
 927   assert(4 == sizeof(InvocationCounter::InterpreterInvocationLimit),
 928          "must be 4 bytes");
 929   __ load_const(invocation_limit_addr, (address)&InvocationCounter::InterpreterInvocationLimit);
 930   __ lwa(invocation_limit, 0, invocation_limit_addr);
 931   __ cmpw(CCR0, iv_be_count, invocation_limit);
 932   __ bge(CCR0, overflow);
 933   __ bind(done);
 934 }
 935 
 936 //
 937 // Call a JNI method.
 938 //
 939 // Interpreter stub for calling a native method. (C++ interpreter)
 940 // This sets up a somewhat different looking stack for calling the native method
 941 // than the typical interpreter frame setup.
 942 //
 943 address CppInterpreterGenerator::generate_native_entry(void) {
 944   if (native_entry != NULL) return native_entry;
 945   address entry = __ pc();
 946 
 947   // Read
 948   //   R16_thread
 949   //   R15_prev_state  - address of caller's BytecodeInterpreter, if this snippet
 950   //                     gets called by the frame manager.
 951   //   R19_method      - callee's Method
 952   //   R17_tos         - address of caller's tos
 953   //   R1_SP           - caller's stack pointer
 954   //   R21_sender_SP   - initial caller sp
 955   //
 956   // Update
 957   //   R14_state       - address of caller's BytecodeInterpreter
 958   //   R3_RET          - integer result, if any.
 959   //   F1_RET          - float result, if any.
 960   //
 961   //
 962   // Stack layout at this point:
 963   //
 964   //    0       [TOP_IJAVA_FRAME_ABI]         <-- R1_SP
 965   //            alignment (optional)
 966   //            [outgoing Java arguments]     <-- R17_tos
 967   //            ...
 968   //    PARENT  [PARENT_IJAVA_FRAME_ABI]
 969   //            ...
 970   //
 971 
 972   const bool inc_counter = UseCompiler || CountCompiledCalls;
 973 
 974   const Register signature_handler_fd   = R21_tmp1;
 975   const Register pending_exception      = R22_tmp2;
 976   const Register result_handler_addr    = R23_tmp3;
 977   const Register native_method_fd       = R24_tmp4;
 978   const Register access_flags           = R25_tmp5;
 979   const Register active_handles         = R26_tmp6;
 980   const Register sync_state             = R27_tmp7;
 981   const Register sync_state_addr        = sync_state;     // Address is dead after use.
 982   const Register suspend_flags          = R24_tmp4;
 983 
 984   const Register return_pc              = R28_tmp8;       // Register will be locked for some time.
 985 
 986   const ConditionRegister is_synced     = CCR4_is_synced; // Live-on-exit from compute_interpreter_state.
 987 
 988 
 989   // R1_SP still points to caller's SP at this point.
 990 
 991   // Save initial_caller_sp to caller's abi. The caller frame must be
 992   // resized before returning to get rid of the c2i arguments (if
 993   // any).
 994   // Override the saved SP with the senderSP so we can pop c2i
 995   // arguments (if any) off when we return
 996   __ std(R21_sender_SP, _top_ijava_frame_abi(initial_caller_sp), R1_SP);
 997 
 998   // Save LR to caller's frame. We don't use _abi(lr) here, because it is not safe.
 999   __ mflr(return_pc);
1000   __ std(return_pc, _top_ijava_frame_abi(frame_manager_lr), R1_SP);
1001 
1002   assert(return_pc->is_nonvolatile(), "return_pc must be a non-volatile register");
1003 
1004   __ verify_method_ptr(R19_method);
1005 
1006   //=============================================================================
1007 
1008   // If this snippet gets called by the frame manager (at label
1009   // `call_special'), then R15_prev_state is valid. If this snippet
1010   // is not called by the frame manager, but e.g. by the call stub or
1011   // by compiled code, then R15_prev_state is invalid.
1012   {
1013     // Set R15_prev_state to 0 if we don't return to the frame
1014     // manager; we will return to the call_stub or to compiled code
1015     // instead. If R15_prev_state is 0 there will be only one
1016     // interpreter frame (we will set this up later) in this C frame!
1017     // So we must take care about retrieving prev_state_(_prev_link)
1018     // and restoring R1_SP when popping that interpreter.
1019     Label prev_state_is_valid;
1020 
1021     __ load_const(R11_scratch1/*frame_manager_returnpc_addr*/, (address)&frame_manager_specialized_return);
1022     __ ld(R12_scratch2/*frame_manager_returnpc*/, 0, R11_scratch1/*frame_manager_returnpc_addr*/);
1023     __ cmpd(CCR0, return_pc, R12_scratch2/*frame_manager_returnpc*/);
1024     __ beq(CCR0, prev_state_is_valid);
1025 
1026     __ li(R15_prev_state, 0);
1027 
1028     __ BIND(prev_state_is_valid);
1029   }
1030 
1031   //=============================================================================
1032   // Allocate new frame and initialize interpreter state.
1033 
1034   Label exception_return;
1035   Label exception_return_sync_check;
1036   Label stack_overflow_return;
1037 
1038   // Generate new interpreter state and jump to stack_overflow_return in case of
1039   // a stack overflow.
1040   generate_compute_interpreter_state(stack_overflow_return);
1041 
1042   //=============================================================================
1043   // Increment invocation counter. On overflow, entry to JNI method
1044   // will be compiled.
1045   Label invocation_counter_overflow;
1046   if (inc_counter) {
1047     generate_counter_incr(invocation_counter_overflow);
1048   }
1049 
1050   Label continue_after_compile;
1051   __ BIND(continue_after_compile);
1052 
1053   // access_flags = method->access_flags();
1054   // Load access flags.
1055   assert(access_flags->is_nonvolatile(),
1056          "access_flags must be in a non-volatile register");
1057   // Type check.
1058   // TODO: PPC port: assert(4 == methodOopDesc::sz_access_flags(), "unexpected field size");
1059   __ lwz(access_flags, method_(access_flags));
1060 
1061   // We don't want to reload R19_method and access_flags after calls
1062   // to some helper functions.
1063   assert(R19_method->is_nonvolatile(), "R19_method must be a non-volatile register");
1064 
1065   // Check for synchronized methods. Must happen AFTER invocation counter
1066   // check, so method is not locked if counter overflows.
1067 
1068   {
1069     Label method_is_not_synced;
1070     // Is_synced is still alive.
1071     assert(is_synced->is_nonvolatile(), "is_synced must be non-volatile");
1072     __ bfalse(is_synced, method_is_not_synced);
1073 
1074     lock_method();
1075     // Reload method, it may have moved.
1076     __ ld(R19_method, state_(_method));
1077 
1078     __ BIND(method_is_not_synced);
1079   }
1080 
1081   // jvmti/jvmpi support
1082   __ notify_method_entry();
1083 
1084   // Reload method, it may have moved.
1085   __ ld(R19_method, state_(_method));
1086 
1087   //=============================================================================
1088   // Get and call the signature handler
1089 
1090   __ ld(signature_handler_fd, method_(signature_handler));
1091   Label call_signature_handler;
1092 
1093   __ cmpdi(CCR0, signature_handler_fd, 0);
1094   __ bne(CCR0, call_signature_handler);
1095 
1096   // Method has never been called. Either generate a specialized
1097   // handler or point to the slow one.
1098   //
1099   // Pass parameter 'false' to avoid exception check in call_VM.
1100   __ call_VM(noreg, CAST_FROM_FN_PTR(address, InterpreterRuntime::prepare_native_call), R19_method, false);
1101 
1102   // Check for an exception while looking up the target method. If we
1103   // incurred one, bail.
1104   __ ld(pending_exception, thread_(pending_exception));
1105   __ cmpdi(CCR0, pending_exception, 0);
1106   __ bne(CCR0, exception_return_sync_check); // has pending exception
1107 
1108   // reload method
1109   __ ld(R19_method, state_(_method));
1110 
1111   // Reload signature handler, it may have been created/assigned in the meanwhile
1112   __ ld(signature_handler_fd, method_(signature_handler));
1113 
1114   __ BIND(call_signature_handler);
1115 
1116   // Before we call the signature handler we push a new frame to
1117   // protect the interpreter frame volatile registers when we return
1118   // from jni but before we can get back to Java.
1119 
1120   // First set the frame anchor while the SP/FP registers are
1121   // convenient and the slow signature handler can use this same frame
1122   // anchor.
1123 
1124   // We have a TOP_IJAVA_FRAME here, which belongs to us.
1125   __ set_top_ijava_frame_at_SP_as_last_Java_frame(R1_SP, R12_scratch2/*tmp*/);
1126 
1127   // Now the interpreter frame (and its call chain) have been
1128   // invalidated and flushed. We are now protected against eager
1129   // being enabled in native code. Even if it goes eager the
1130   // registers will be reloaded as clean and we will invalidate after
1131   // the call so no spurious flush should be possible.
1132 
1133   // Call signature handler and pass locals address.
1134   //
1135   // Our signature handlers copy required arguments to the C stack
1136   // (outgoing C args), R3_ARG1 to R10_ARG8, and F1_ARG1 to
1137   // F13_ARG13.
1138   __ mr(R3_ARG1, R18_locals);
1139   __ ld(signature_handler_fd, 0, signature_handler_fd);
1140   __ call_stub(signature_handler_fd);
1141   // reload method
1142   __ ld(R19_method, state_(_method));
1143 
1144   // Remove the register parameter varargs slots we allocated in
1145   // compute_interpreter_state. SP+16 ends up pointing to the ABI
1146   // outgoing argument area.
1147   //
1148   // Not needed on PPC64.
1149   //__ add(SP, SP, Argument::n_register_parameters*BytesPerWord);
1150 
1151   assert(result_handler_addr->is_nonvolatile(), "result_handler_addr must be in a non-volatile register");
1152   // Save across call to native method.
1153   __ mr(result_handler_addr, R3_RET);
1154 
1155   // Set up fixed parameters and call the native method.
1156   // If the method is static, get mirror into R4_ARG2.
1157 
1158   {
1159     Label method_is_not_static;
1160     // access_flags is non-volatile and still, no need to restore it
1161 
1162     // restore access flags
1163     __ testbitdi(CCR0, R0, access_flags, JVM_ACC_STATIC_BIT);
1164     __ bfalse(CCR0, method_is_not_static);
1165 
1166     // constants = method->constants();
1167     __ ld(R11_scratch1, in_bytes(Method::const_offset()), R19_method);
1168     __ ld(R11_scratch1/*constants*/, in_bytes(ConstMethod::constants_offset()), R11_scratch1);
1169     // pool_holder = method->constants()->pool_holder();
1170     __ ld(R11_scratch1/*pool_holder*/, ConstantPool::pool_holder_offset_in_bytes(),
1171           R11_scratch1/*constants*/);
1172 
1173     const int mirror_offset = in_bytes(Klass::java_mirror_offset());
1174 
1175     // mirror = pool_holder->klass_part()->java_mirror();
1176     __ ld(R0/*mirror*/, mirror_offset, R11_scratch1/*pool_holder*/);
1177     // state->_native_mirror = mirror;
1178     __ std(R0/*mirror*/, state_(_oop_temp));
1179     // R4_ARG2 = &state->_oop_temp;
1180     __ addir(R4_ARG2, state_(_oop_temp));
1181 
1182     __ BIND(method_is_not_static);
1183   }
1184 
1185   // At this point, arguments have been copied off the stack into
1186   // their JNI positions. Oops are boxed in-place on the stack, with
1187   // handles copied to arguments. The result handler address is in a
1188   // register.
1189 
1190   // pass JNIEnv address as first parameter
1191   __ addir(R3_ARG1, thread_(jni_environment));
1192 
1193   // Load the native_method entry before we change the thread state.
1194   __ ld(native_method_fd, method_(native_function));
1195 
1196   //=============================================================================
1197   // Transition from _thread_in_Java to _thread_in_native. As soon as
1198   // we make this change the safepoint code needs to be certain that
1199   // the last Java frame we established is good. The pc in that frame
1200   // just needs to be near here not an actual return address.
1201 
1202   // We use release_store_fence to update values like the thread state, where
1203   // we don't want the current thread to continue until all our prior memory
1204   // accesses (including the new thread state) are visible to other threads.
1205   __ li(R0, _thread_in_native);
1206   __ release();
1207 
1208   // TODO: PPC port: assert(4 == JavaThread::sz_thread_state(), "unexpected field size");
1209   __ stw(R0, thread_(thread_state));
1210 
1211   if (UseMembar) {
1212     __ fence();
1213   }
1214 
1215   //=============================================================================
1216   // Call the native method. Argument registers must not have been
1217   // overwritten since "__ call_stub(signature_handler);" (except for
1218   // ARG1 and ARG2 for static methods)
1219   __ call_c(native_method_fd);
1220 
1221   __ std(R3_RET, state_(_native_lresult));
1222   __ stfd(F1_RET, state_(_native_fresult));
1223 
1224   // The frame_manager_lr field, which we use for setting the last
1225   // java frame, gets overwritten by the signature handler. Restore
1226   // it now.
1227   __ get_PC_trash_LR(R11_scratch1);
1228   __ std(R11_scratch1, _top_ijava_frame_abi(frame_manager_lr), R1_SP);
1229 
1230   // Because of GC R19_method may no longer be valid.
1231 
1232   // Block, if necessary, before resuming in _thread_in_Java state.
1233   // In order for GC to work, don't clear the last_Java_sp until after
1234   // blocking.
1235 
1236 
1237 
1238   //=============================================================================
1239   // Switch thread to "native transition" state before reading the
1240   // synchronization state.  This additional state is necessary
1241   // because reading and testing the synchronization state is not
1242   // atomic w.r.t. GC, as this scenario demonstrates: Java thread A,
1243   // in _thread_in_native state, loads _not_synchronized and is
1244   // preempted.  VM thread changes sync state to synchronizing and
1245   // suspends threads for GC. Thread A is resumed to finish this
1246   // native method, but doesn't block here since it didn't see any
1247   // synchronization in progress, and escapes.
1248 
1249   // We use release_store_fence to update values like the thread state, where
1250   // we don't want the current thread to continue until all our prior memory
1251   // accesses (including the new thread state) are visible to other threads.
1252   __ li(R0/*thread_state*/, _thread_in_native_trans);
1253   __ release();
1254   __ stw(R0/*thread_state*/, thread_(thread_state));
1255   if (UseMembar) {
1256     __ fence();
1257   }
1258   // Write serialization page so that the VM thread can do a pseudo remote
1259   // membar. We use the current thread pointer to calculate a thread
1260   // specific offset to write to within the page. This minimizes bus
1261   // traffic due to cache line collision.
1262   else {
1263     __ serialize_memory(R16_thread, R11_scratch1, R12_scratch2);
1264   }
1265 
1266   // Now before we return to java we must look for a current safepoint
1267   // (a new safepoint can not start since we entered native_trans).
1268   // We must check here because a current safepoint could be modifying
1269   // the callers registers right this moment.
1270 
1271   // Acquire isn't strictly necessary here because of the fence, but
1272   // sync_state is declared to be volatile, so we do it anyway.
1273   __ load_const(sync_state_addr, SafepointSynchronize::address_of_state());
1274 
1275   // TODO: PPC port: assert(4 == SafepointSynchronize::sz_state(), "unexpected field size");
1276   __ lwz(sync_state, 0, sync_state_addr);
1277 
1278   // TODO: PPC port: assert(4 == Thread::sz_suspend_flags(), "unexpected field size");
1279   __ lwz(suspend_flags, thread_(suspend_flags));
1280 
1281   __ acquire();
1282 
1283   Label sync_check_done;
1284   Label do_safepoint;
1285   // No synchronization in progress nor yet synchronized
1286   __ cmpwi(CCR0, sync_state, SafepointSynchronize::_not_synchronized);
1287   // not suspended
1288   __ cmpwi(CCR1, suspend_flags, 0);
1289 
1290   __ bne(CCR0, do_safepoint);
1291   __ beq(CCR1, sync_check_done);
1292   __ bind(do_safepoint);
1293   // Block.  We do the call directly and leave the current
1294   // last_Java_frame setup undisturbed.  We must save any possible
1295   // native result acrosss the call. No oop is present
1296 
1297   __ mr(R3_ARG1, R16_thread);
1298   __ call_c(CAST_FROM_FN_PTR(FunctionDescriptor*, JavaThread::check_special_condition_for_native_trans),
1299             relocInfo::none);
1300   __ bind(sync_check_done);
1301 
1302   //=============================================================================
1303   // <<<<<< Back in Interpreter Frame >>>>>
1304 
1305   // We are in thread_in_native_trans here and back in the normal
1306   // interpreter frame. We don't have to do anything special about
1307   // safepoints and we can switch to Java mode anytime we are ready.
1308 
1309   // Note: frame::interpreter_frame_result has a dependency on how the
1310   // method result is saved across the call to post_method_exit. For
1311   // native methods it assumes that the non-FPU/non-void result is
1312   // saved in _native_lresult and a FPU result in _native_fresult. If
1313   // this changes then the interpreter_frame_result implementation
1314   // will need to be updated too.
1315 
1316   // On PPC64, we have stored the result directly after the native call.
1317 
1318   //=============================================================================
1319   // back in Java
1320 
1321   // We use release_store_fence to update values like the thread state, where
1322   // we don't want the current thread to continue until all our prior memory
1323   // accesses (including the new thread state) are visible to other threads.
1324   __ li(R0/*thread_state*/, _thread_in_Java);
1325   __ release();
1326   __ stw(R0/*thread_state*/, thread_(thread_state));
1327   if (UseMembar) {
1328     __ fence();
1329   }
1330 
1331   __ reset_last_Java_frame();
1332 
1333   // Reload GR27_method, call killed it. We can't look at
1334   // state->_method until we're back in java state because in java
1335   // state gc can't happen until we get to a safepoint.
1336   //
1337   // We've set thread_state to _thread_in_Java already, so restoring
1338   // R19_method from R14_state works; R19_method is invalid, because
1339   // GC may have happened.
1340   __ ld(R19_method, state_(_method)); // reload method, may have moved
1341 
1342   // jvmdi/jvmpi support. Whether we've got an exception pending or
1343   // not, and whether unlocking throws an exception or not, we notify
1344   // on native method exit. If we do have an exception, we'll end up
1345   // in the caller's context to handle it, so if we don't do the
1346   // notify here, we'll drop it on the floor.
1347 
1348   __ notify_method_exit(true/*native method*/,
1349                         ilgl /*illegal state (not used for native methods)*/);
1350 
1351 
1352 
1353   //=============================================================================
1354   // Handle exceptions
1355 
1356   // See if we must unlock.
1357   //
1358   {
1359     Label method_is_not_synced;
1360     // is_synced is still alive
1361     assert(is_synced->is_nonvolatile(), "is_synced must be non-volatile");
1362     __ bfalse(is_synced, method_is_not_synced);
1363 
1364     unlock_method();
1365 
1366     __ bind(method_is_not_synced);
1367   }
1368 
1369   // Reset active handles after returning from native.
1370   // thread->active_handles()->clear();
1371   __ ld(active_handles, thread_(active_handles));
1372   // JNIHandleBlock::_top is an int.
1373   // TODO:  PPC port: assert(4 == JNIHandleBlock::top_size_in_bytes(), "unexpected field size");
1374   __ li(R0, 0);
1375   __ stw(R0, JNIHandleBlock::top_offset_in_bytes(), active_handles);
1376 
1377   Label no_pending_exception_from_native_method;
1378   __ ld(R0/*pending_exception*/, thread_(pending_exception));
1379   __ cmpdi(CCR0, R0/*pending_exception*/, 0);
1380   __ beq(CCR0, no_pending_exception_from_native_method);
1381 
1382 
1383   //-----------------------------------------------------------------------------
1384   // An exception is pending. We call into the runtime only if the
1385   // caller was not interpreted. If it was interpreted the
1386   // interpreter will do the correct thing. If it isn't interpreted
1387   // (call stub/compiled code) we will change our return and continue.
1388   __ BIND(exception_return);
1389 
1390   Label return_to_initial_caller_with_pending_exception;
1391   __ cmpdi(CCR0, R15_prev_state, 0);
1392   __ beq(CCR0, return_to_initial_caller_with_pending_exception);
1393 
1394   // We are returning to an interpreter activation, just pop the state,
1395   // pop our frame, leave the exception pending, and return.
1396   __ pop_interpreter_state(/*prev_state_may_be_0=*/false);
1397   __ pop_interpreter_frame(R11_scratch1, R12_scratch2, R21_tmp1 /* set to return pc */, R22_tmp2);
1398   __ mtlr(R21_tmp1);
1399   __ blr();
1400 
1401   __ BIND(exception_return_sync_check);
1402 
1403   assert(is_synced->is_nonvolatile(), "is_synced must be non-volatile");
1404   __ bfalse(is_synced, exception_return);
1405   unlock_method();
1406   __ b(exception_return);
1407 
1408 
1409   __ BIND(return_to_initial_caller_with_pending_exception);
1410   // We are returning to a c2i-adapter / call-stub, get the address of the
1411   // exception handler, pop the frame and return to the handler.
1412 
1413   // First, pop to caller's frame.
1414   __ pop_interpreter_frame(R11_scratch1, R12_scratch2, R21_tmp1  /* set to return pc */, R22_tmp2);
1415 
1416   __ push_frame_abi112(0, R11_scratch1);
1417   // Get the address of the exception handler.
1418   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address),
1419                   R16_thread,
1420                   R21_tmp1 /* return pc */);
1421   __ pop_frame();
1422 
1423   // Load the PC of the the exception handler into LR.
1424   __ mtlr(R3_RET);
1425 
1426   // Load exception into R3_ARG1 and clear pending exception in thread.
1427   __ ld(R3_ARG1/*exception*/, thread_(pending_exception));
1428   __ li(R4_ARG2, 0);
1429   __ std(R4_ARG2, thread_(pending_exception));
1430 
1431   // Load the original return pc into R4_ARG2.
1432   __ mr(R4_ARG2/*issuing_pc*/, R21_tmp1);
1433 
1434   // Resize frame to get rid of a potential extension.
1435   __ resize_frame_to_initial_caller(R11_scratch1, R12_scratch2);
1436 
1437   // Return to exception handler.
1438   __ blr();
1439 
1440 
1441   //-----------------------------------------------------------------------------
1442   // No exception pending.
1443   __ BIND(no_pending_exception_from_native_method);
1444 
1445   // Move native method result back into proper registers and return.
1446   // Invoke result handler (may unbox/promote).
1447   __ ld(R3_RET, state_(_native_lresult));
1448   __ lfd(F1_RET, state_(_native_fresult));
1449   __ call_stub(result_handler_addr);
1450 
1451   // We have created a new BytecodeInterpreter object, now we must destroy it.
1452   //
1453   // Restore previous R14_state and caller's SP.  R15_prev_state may
1454   // be 0 here, because our caller may be the call_stub or compiled
1455   // code.
1456   __ pop_interpreter_state(/*prev_state_may_be_0=*/true);
1457   __ pop_interpreter_frame(R11_scratch1, R12_scratch2, R21_tmp1 /* set to return pc */, R22_tmp2);
1458   // Resize frame to get rid of a potential extension.
1459   __ resize_frame_to_initial_caller(R11_scratch1, R12_scratch2);
1460 
1461   // Must use the return pc which was loaded from the caller's frame
1462   // as the VM uses return-pc-patching for deoptimization.
1463   __ mtlr(R21_tmp1);
1464   __ blr();
1465 
1466 
1467 
1468   //=============================================================================
1469   // We encountered an exception while computing the interpreter
1470   // state, so R14_state isn't valid. Act as if we just returned from
1471   // the callee method with a pending exception.
1472   __ BIND(stack_overflow_return);
1473 
1474   //
1475   // Register state:
1476   //   R14_state         invalid; trashed by compute_interpreter_state
1477   //   R15_prev_state    valid, but may be 0
1478   //
1479   //   R1_SP             valid, points to caller's SP; wasn't yet updated by
1480   //                     compute_interpreter_state
1481   //
1482 
1483   // Create exception oop and make it pending.
1484 
1485   // Throw the exception via RuntimeStub "throw_StackOverflowError_entry".
1486   //
1487   // Previously, we called C-Code directly. As a consequence, a
1488   // possible GC tried to process the argument oops of the top frame
1489   // (see RegisterMap::clear, which sets the corresponding flag to
1490   // true). This lead to crashes because:
1491   //   1. The top register map did not contain locations for the argument registers
1492   //   2. The arguments are dead anyway, could be already overwritten in the worst case
1493   // Solution: Call via special runtime stub that pushes it's own
1494   // frame. This runtime stub has the flag "CodeBlob::caller_must_gc_arguments()"
1495   // set to "false", what prevents the dead arguments getting GC'd.
1496   //
1497   // 2 cases exist:
1498   // 1. We were called by the c2i adapter / call stub
1499   // 2. We were called by the frame manager
1500   //
1501   // Both cases are handled by this code:
1502   // 1. - initial_caller_sp was saved in both cases on entry, so it's safe to load it back even if it was not changed.
1503   //    - control flow will be:
1504   //      throw_stackoverflow_stub->VM->throw_stackoverflow_stub->forward_excep->excp_blob of caller method
1505   // 2. - control flow will be:
1506   //      throw_stackoverflow_stub->VM->throw_stackoverflow_stub->forward_excep->rethrow_excp_entry of frame manager->resume_method
1507   //      Since we restored the caller SP above, the rethrow_excp_entry can restore the original interpreter state
1508   //      registers using the stack and resume the calling method with a pending excp.
1509 
1510   // Pop any c2i extension from the stack, restore LR just to be sure
1511   __ ld(R0, _top_ijava_frame_abi(frame_manager_lr), R1_SP);
1512   __ mtlr(R0);
1513   // Resize frame to get rid of a potential extension.
1514   __ resize_frame_to_initial_caller(R11_scratch1, R12_scratch2);
1515 
1516   assert(StubRoutines::throw_StackOverflowError_entry() != NULL, "generated in wrong order");
1517   // Load target address of the runtime stub.
1518   __ load_const(R12_scratch2, (StubRoutines::throw_StackOverflowError_entry()));
1519   __ mtctr(R12_scratch2);
1520   __ bctr();
1521 
1522 
1523   //=============================================================================
1524   // Counter overflow.
1525 
1526   if (inc_counter) {
1527     // Handle invocation counter overflow
1528     __ bind(invocation_counter_overflow);
1529 
1530     generate_counter_overflow(continue_after_compile);
1531   }
1532 
1533   native_entry = entry;
1534   return entry;
1535 }
1536 
1537 bool AbstractInterpreter::can_be_compiled(methodHandle m) {
1538   // No special entry points that preclude compilation.
1539   return true;
1540 }
1541 
1542 // Unlock the current method.
1543 //
1544 void CppInterpreterGenerator::unlock_method(void) {
1545   // Find preallocated monitor and unlock method. Method monitor is
1546   // the first one.
1547 
1548   // Registers alive
1549   //   R14_state
1550   //
1551   // Registers updated
1552   //   volatiles
1553   //
1554   const Register monitor = R4_ARG2;
1555 
1556   // Pass address of initial monitor we allocated.
1557   //
1558   // First monitor.
1559   __ addi(monitor, R14_state, -frame::interpreter_frame_monitor_size_in_bytes());
1560 
1561   // Unlock method
1562   __ unlock_object(monitor);
1563 }
1564 
1565 // Lock the current method.
1566 //
1567 void CppInterpreterGenerator::lock_method(void) {
1568   // Find preallocated monitor and lock method. Method monitor is the
1569   // first one.
1570 
1571   //
1572   // Registers alive
1573   //   R14_state
1574   //
1575   // Registers updated
1576   //   volatiles
1577   //
1578 
1579   const Register monitor = R4_ARG2;
1580   const Register object  = R5_ARG3;
1581 
1582   // Pass address of initial monitor we allocated.
1583   __ addi(monitor, R14_state, -frame::interpreter_frame_monitor_size_in_bytes());
1584 
1585   // Pass object address.
1586   __ ld(object, BasicObjectLock::obj_offset_in_bytes(), monitor);
1587 
1588   // Lock method.
1589   __ lock_object(monitor, object);
1590 }
1591 
1592 // Generate code for handling resuming a deopted method.
1593 void CppInterpreterGenerator::generate_deopt_handling(Register result_index) {
1594 
1595   //=============================================================================
1596   // Returning from a compiled method into a deopted method. The
1597   // bytecode at the bcp has completed. The result of the bytecode is
1598   // in the native abi (the tosca for the template based
1599   // interpreter). Any stack space that was used by the bytecode that
1600   // has completed has been removed (e.g. parameters for an invoke) so
1601   // all that we have to do is place any pending result on the
1602   // expression stack and resume execution on the next bytecode.
1603 
1604   Label return_from_deopt_common;
1605 
1606   // R3_RET and F1_RET are live here! Load the array index of the
1607   // required result stub address and continue at return_from_deopt_common.
1608 
1609   // Deopt needs to jump to here to enter the interpreter (return a result).
1610   deopt_frame_manager_return_atos = __ pc();
1611   __ li(result_index, AbstractInterpreter::BasicType_as_index(T_OBJECT));
1612   __ b(return_from_deopt_common);
1613 
1614   deopt_frame_manager_return_btos = __ pc();
1615   __ li(result_index, AbstractInterpreter::BasicType_as_index(T_BOOLEAN));
1616   __ b(return_from_deopt_common);
1617 
1618   deopt_frame_manager_return_itos = __ pc();
1619   __ li(result_index, AbstractInterpreter::BasicType_as_index(T_INT));
1620   __ b(return_from_deopt_common);
1621 
1622   deopt_frame_manager_return_ltos = __ pc();
1623   __ li(result_index, AbstractInterpreter::BasicType_as_index(T_LONG));
1624   __ b(return_from_deopt_common);
1625 
1626   deopt_frame_manager_return_ftos = __ pc();
1627   __ li(result_index, AbstractInterpreter::BasicType_as_index(T_FLOAT));
1628   __ b(return_from_deopt_common);
1629 
1630   deopt_frame_manager_return_dtos = __ pc();
1631   __ li(result_index, AbstractInterpreter::BasicType_as_index(T_DOUBLE));
1632   __ b(return_from_deopt_common);
1633 
1634   deopt_frame_manager_return_vtos = __ pc();
1635   __ li(result_index, AbstractInterpreter::BasicType_as_index(T_VOID));
1636   // Last one, fall-through to return_from_deopt_common.
1637 
1638   // Deopt return common. An index is present that lets us move any
1639   // possible result being return to the interpreter's stack.
1640   //
1641   __ BIND(return_from_deopt_common);
1642 
1643 }
1644 
1645 // Generate the code to handle a more_monitors message from the c++ interpreter.
1646 void CppInterpreterGenerator::generate_more_monitors() {
1647 
1648   //
1649   // Registers alive
1650   //   R16_thread      - JavaThread*
1651   //   R15_prev_state  - previous BytecodeInterpreter or 0
1652   //   R14_state       - BytecodeInterpreter* address of receiver's interpreter state
1653   //   R1_SP           - old stack pointer
1654   //
1655   // Registers updated
1656   //   R1_SP          - new stack pointer
1657   //
1658 
1659   // Very-local scratch registers.
1660   const Register old_tos         = R21_tmp1;
1661   const Register new_tos         = R22_tmp2;
1662   const Register stack_base      = R23_tmp3;
1663   const Register stack_limit     = R24_tmp4;
1664   const Register slot            = R25_tmp5;
1665   const Register n_slots         = R25_tmp5;
1666 
1667   // Interpreter state fields.
1668   const Register msg             = R24_tmp4;
1669 
1670   // Load up relevant interpreter state.
1671 
1672   __ ld(stack_base, state_(_stack_base));                // Old stack_base
1673   __ ld(old_tos, state_(_stack));                        // Old tos
1674   __ ld(stack_limit, state_(_stack_limit));              // Old stack_limit
1675 
1676   // extracted monitor_size
1677   int monitor_size = frame::interpreter_frame_monitor_size_in_bytes();
1678   assert(Assembler::is_aligned((unsigned int)monitor_size,
1679                                (unsigned int)frame::alignment_in_bytes),
1680          "size of a monitor must respect alignment of SP");
1681 
1682   // Save and restore top LR
1683   __ ld(R12_scratch2, _top_ijava_frame_abi(frame_manager_lr), R1_SP);
1684   __ resize_frame(-monitor_size, R11_scratch1);// Allocate space for new monitor
1685   __ std(R12_scratch2, _top_ijava_frame_abi(frame_manager_lr), R1_SP);
1686     // Initial_caller_sp is used as unextended_sp for non initial callers.
1687   __ std(R1_SP, _top_ijava_frame_abi(initial_caller_sp), R1_SP);
1688   __ addi(stack_base, stack_base, -monitor_size);        // New stack_base
1689   __ addi(new_tos, old_tos, -monitor_size);              // New tos
1690   __ addi(stack_limit, stack_limit, -monitor_size);      // New stack_limit
1691 
1692   __ std(R1_SP, state_(_last_Java_sp));                  // Update frame_bottom
1693 
1694   __ std(stack_base, state_(_stack_base));               // Update stack_base
1695   __ std(new_tos, state_(_stack));                       // Update tos
1696   __ std(stack_limit, state_(_stack_limit));             // Update stack_limit
1697 
1698   __ li(msg, BytecodeInterpreter::got_monitors);         // Tell interpreter we allocated the lock
1699   __ stw(msg, state_(_msg));
1700 
1701   // Shuffle expression stack down. Recall that stack_base points
1702   // just above the new expression stack bottom. Old_tos and new_tos
1703   // are used to scan thru the old and new expression stacks.
1704 
1705   Label copy_slot, copy_slot_finished;
1706   __ sub(n_slots, stack_base, new_tos);
1707   __ srdi_(n_slots, n_slots, LogBytesPerWord);           // compute number of slots to copy
1708   assert(LogBytesPerWord == 3, "conflicts assembler instructions");
1709   __ beq(CCR0, copy_slot_finished);                       // nothing to copy
1710 
1711   __ mtctr(n_slots);
1712 
1713   // loop
1714   __ bind(copy_slot);
1715   __ ldu(slot, BytesPerWord, old_tos);                   // slot = *++old_tos;
1716   __ stdu(slot, BytesPerWord, new_tos);                  // *++new_tos = slot;
1717   __ bdnz(copy_slot);
1718 
1719   __ bind(copy_slot_finished);
1720 
1721   // Restart interpreter
1722   __ li(R0, 0);
1723   __ std(R0, BasicObjectLock::obj_offset_in_bytes(), stack_base);  // Mark lock as unused
1724 }
1725 
1726 address CppInterpreterGenerator::generate_normal_entry(void) {
1727   if (interpreter_frame_manager != NULL) return interpreter_frame_manager;
1728 
1729   address entry = __ pc();
1730 
1731   address return_from_native_pc = (address) NULL;
1732 
1733   // Initial entry to frame manager (from call_stub or c2i_adapter)
1734 
1735   //
1736   // Registers alive
1737   //   R16_thread               - JavaThread*
1738   //   R19_method               - callee's Method (method to be invoked)
1739   //   R17_tos                  - address of sender tos (prepushed)
1740   //   R1_SP                    - SP prepared by call stub such that caller's outgoing args are near top
1741   //   LR                       - return address to caller (call_stub or c2i_adapter)
1742   //   R21_sender_SP            - initial caller sp
1743   //
1744   // Registers updated
1745   //   R15_prev_state           - 0
1746   //
1747   // Stack layout at this point:
1748   //
1749   //   0       [TOP_IJAVA_FRAME_ABI]         <-- R1_SP
1750   //           alignment (optional)
1751   //           [outgoing Java arguments]     <-- R17_tos
1752   //           ...
1753   //   PARENT  [PARENT_IJAVA_FRAME_ABI]
1754   //           ...
1755   //
1756 
1757   // Save initial_caller_sp to caller's abi.
1758   // The caller frame must be resized before returning to get rid of
1759   // the c2i part on top of the calling compiled frame (if any).
1760   // R21_tmp1 must match sender_sp in gen_c2i_adapter.
1761   // Now override the saved SP with the senderSP so we can pop c2i
1762   // arguments (if any) off when we return.
1763   __ std(R21_sender_SP, _top_ijava_frame_abi(initial_caller_sp), R1_SP);
1764 
1765   // Save LR to caller's frame. We don't use _abi(lr) here,
1766   // because it is not safe.
1767   __ mflr(R0);
1768   __ std(R0, _top_ijava_frame_abi(frame_manager_lr), R1_SP);
1769 
1770   // If we come here, it is the first invocation of the frame manager.
1771   // So there is no previous interpreter state.
1772   __ li(R15_prev_state, 0);
1773 
1774 
1775   // Fall through to where "recursive" invocations go.
1776 
1777   //=============================================================================
1778   // Dispatch an instance of the interpreter. Recursive activations
1779   // come here.
1780 
1781   Label re_dispatch;
1782   __ BIND(re_dispatch);
1783 
1784   //
1785   // Registers alive
1786   //    R16_thread        - JavaThread*
1787   //    R19_method        - callee's Method
1788   //    R17_tos           - address of caller's tos (prepushed)
1789   //    R15_prev_state    - address of caller's BytecodeInterpreter or 0
1790   //    R1_SP             - caller's SP trimmed such that caller's outgoing args are near top.
1791   //
1792   // Stack layout at this point:
1793   //
1794   //   0       [TOP_IJAVA_FRAME_ABI]
1795   //           alignment (optional)
1796   //           [outgoing Java arguments]
1797   //           ...
1798   //   PARENT  [PARENT_IJAVA_FRAME_ABI]
1799   //           ...
1800 
1801   // fall through to interpreted execution
1802 
1803   //=============================================================================
1804   // Allocate a new Java frame and initialize the new interpreter state.
1805 
1806   Label stack_overflow_return;
1807 
1808   // Create a suitable new Java frame plus a new BytecodeInterpreter instance
1809   // in the current (frame manager's) C frame.
1810   generate_compute_interpreter_state(stack_overflow_return);
1811 
1812   // fall through
1813 
1814   //=============================================================================
1815   // Interpreter dispatch.
1816 
1817   Label call_interpreter;
1818   __ BIND(call_interpreter);
1819 
1820   //
1821   // Registers alive
1822   //   R16_thread       - JavaThread*
1823   //   R15_prev_state   - previous BytecodeInterpreter or 0
1824   //   R14_state        - address of receiver's BytecodeInterpreter
1825   //   R1_SP            - receiver's stack pointer
1826   //
1827 
1828   // Thread fields.
1829   const Register pending_exception = R21_tmp1;
1830 
1831   // Interpreter state fields.
1832   const Register msg               = R24_tmp4;
1833 
1834   // MethodOop fields.
1835   const Register parameter_count   = R25_tmp5;
1836   const Register result_index      = R26_tmp6;
1837 
1838   const Register dummy             = R28_tmp8;
1839 
1840   // Address of various interpreter stubs.
1841   // R29_tmp9 is reserved.
1842   const Register stub_addr         = R27_tmp7;
1843 
1844   // Uncommon trap needs to jump to here to enter the interpreter
1845   // (re-execute current bytecode).
1846   unctrap_frame_manager_entry  = __ pc();
1847 
1848   // If we are profiling, store our fp (BSP) in the thread so we can
1849   // find it during a tick.
1850   if (Arguments::has_profile()) {
1851     // On PPC64 we store the pointer to the current BytecodeInterpreter,
1852     // instead of the bsp of ia64. This should suffice to be able to
1853     // find all interesting information.
1854     __ std(R14_state, thread_(last_interpreter_fp));
1855   }
1856 
1857   // R16_thread, R14_state and R15_prev_state are nonvolatile
1858   // registers. There is no need to save these. If we needed to save
1859   // some state in the current Java frame, this could be a place to do
1860   // so.
1861 
1862   // Call Java bytecode dispatcher passing "BytecodeInterpreter* istate".
1863   __ call_VM_leaf(CAST_FROM_FN_PTR(address,
1864                                    JvmtiExport::can_post_interpreter_events()
1865                                    ? BytecodeInterpreter::runWithChecks
1866                                    : BytecodeInterpreter::run),
1867                   R14_state);
1868 
1869   interpreter_return_address  = __ last_calls_return_pc();
1870 
1871   // R16_thread, R14_state and R15_prev_state have their values preserved.
1872 
1873   // If we are profiling, clear the fp in the thread to tell
1874   // the profiler that we are no longer in the interpreter.
1875   if (Arguments::has_profile()) {
1876     __ li(R11_scratch1, 0);
1877     __ std(R11_scratch1, thread_(last_interpreter_fp));
1878   }
1879 
1880   // Load message from bytecode dispatcher.
1881   // TODO: PPC port: guarantee(4 == BytecodeInterpreter::sz_msg(), "unexpected field size");
1882   __ lwz(msg, state_(_msg));
1883 
1884 
1885   Label more_monitors;
1886   Label return_from_native;
1887   Label return_from_native_common;
1888   Label return_from_native_no_exception;
1889   Label return_from_interpreted_method;
1890   Label return_from_recursive_activation;
1891   Label unwind_recursive_activation;
1892   Label resume_interpreter;
1893   Label return_to_initial_caller;
1894   Label unwind_initial_activation;
1895   Label unwind_initial_activation_pending_exception;
1896   Label call_method;
1897   Label call_special;
1898   Label retry_method;
1899   Label retry_method_osr;
1900   Label popping_frame;
1901   Label throwing_exception;
1902 
1903   // Branch according to the received message
1904 
1905   __ cmpwi(CCR1, msg, BytecodeInterpreter::call_method);
1906   __ cmpwi(CCR2, msg, BytecodeInterpreter::return_from_method);
1907 
1908   __ beq(CCR1, call_method);
1909   __ beq(CCR2, return_from_interpreted_method);
1910 
1911   __ cmpwi(CCR3, msg, BytecodeInterpreter::more_monitors);
1912   __ cmpwi(CCR4, msg, BytecodeInterpreter::throwing_exception);
1913 
1914   __ beq(CCR3, more_monitors);
1915   __ beq(CCR4, throwing_exception);
1916 
1917   __ cmpwi(CCR5, msg, BytecodeInterpreter::popping_frame);
1918   __ cmpwi(CCR6, msg, BytecodeInterpreter::do_osr);
1919 
1920   __ beq(CCR5, popping_frame);
1921   __ beq(CCR6, retry_method_osr);
1922 
1923   __ stop("bad message from interpreter");
1924 
1925 
1926   //=============================================================================
1927   // Add a monitor just below the existing one(s). State->_stack_base
1928   // points to the lowest existing one, so we insert the new one just
1929   // below it and shuffle the expression stack down. Ref. the above
1930   // stack layout picture, we must update _stack_base, _stack, _stack_limit
1931   // and _last_Java_sp in the interpreter state.
1932 
1933   __ BIND(more_monitors);
1934 
1935   generate_more_monitors();
1936   __ b(call_interpreter);
1937 
1938   generate_deopt_handling(result_index);
1939 
1940   // Restoring the R14_state is already done by the deopt_blob.
1941 
1942   // Current tos includes no parameter slots.
1943   __ ld(R17_tos, state_(_stack));
1944   __ li(msg, BytecodeInterpreter::deopt_resume);
1945   __ b(return_from_native_common);
1946 
1947   // We are sent here when we are unwinding from a native method or
1948   // adapter with an exception pending. We need to notify the interpreter
1949   // that there is an exception to process.
1950   // We arrive here also if the frame manager called an (interpreted) target
1951   // which returns with a StackOverflow exception.
1952   // The control flow is in this case is:
1953   // frame_manager->throw_excp_stub->forward_excp->rethrow_excp_entry
1954 
1955   AbstractInterpreter::_rethrow_exception_entry = __ pc();
1956 
1957   // Restore R14_state.
1958   __ ld(R14_state, 0, R1_SP);
1959   __ addi(R14_state, R14_state,
1960               -frame::interpreter_frame_cinterpreterstate_size_in_bytes());
1961 
1962   // Store exception oop into thread object.
1963   __ std(R3_RET, thread_(pending_exception));
1964   __ li(msg, BytecodeInterpreter::method_resume /*rethrow_exception*/);
1965   //
1966   // NOTE: the interpreter frame as setup be deopt does NOT include
1967   // any parameter slots (good thing since we have no callee here
1968   // and couldn't remove them) so we don't have to do any calculations
1969   // here to figure it out.
1970   //
1971   __ ld(R17_tos, state_(_stack));
1972   __ b(return_from_native_common);
1973 
1974 
1975   //=============================================================================
1976   // Returning from a native method.  Result is in the native abi
1977   // location so we must move it to the java expression stack.
1978 
1979   __ BIND(return_from_native);
1980   guarantee(return_from_native_pc == (address) NULL, "precondition");
1981   return_from_native_pc = __ pc();
1982 
1983   // Restore R14_state.
1984   __ ld(R14_state, 0, R1_SP);
1985   __ addi(R14_state, R14_state, -frame::interpreter_frame_cinterpreterstate_size_in_bytes());
1986 
1987   //
1988   // Registers alive
1989   //   R16_thread
1990   //   R14_state    - address of caller's BytecodeInterpreter.
1991   //   R3_RET       - integer result, if any.
1992   //   F1_RET       - float result, if any.
1993   //
1994   // Registers updated
1995   //   R19_method   - callee's Method
1996   //   R17_tos      - caller's tos, with outgoing args popped
1997   //   result_index - index of result handler.
1998   //   msg          - message for resuming interpreter.
1999   //
2000 
2001   // Very-local scratch registers.
2002 
2003   const ConditionRegister have_pending_exception = CCR0;
2004 
2005   // Load callee Method, gc may have moved it.
2006   __ ld(R19_method, state_(_result._to_call._callee));
2007 
2008   // Load address of caller's tos. includes parameter slots.
2009   __ ld(R17_tos, state_(_stack));
2010 
2011   // Pop callee's parameters.
2012 
2013   __ ld(parameter_count, in_bytes(Method::const_offset()), R19_method);
2014   __ lhz(parameter_count, in_bytes(ConstMethod::size_of_parameters_offset()), parameter_count);
2015   __ sldi(parameter_count, parameter_count, Interpreter::logStackElementSize);
2016   __ add(R17_tos, R17_tos, parameter_count);
2017 
2018   // Result stub address array index
2019   // TODO: PPC port: assert(4 == methodOopDesc::sz_result_index(), "unexpected field size");
2020   __ lwa(result_index, method_(result_index));
2021 
2022   __ li(msg, BytecodeInterpreter::method_resume);
2023 
2024   //
2025   // Registers alive
2026   //   R16_thread
2027   //   R14_state    - address of caller's BytecodeInterpreter.
2028   //   R17_tos      - address of caller's tos with outgoing args already popped
2029   //   R3_RET       - integer return value, if any.
2030   //   F1_RET       - float return value, if any.
2031   //   result_index - index of result handler.
2032   //   msg          - message for resuming interpreter.
2033   //
2034   // Registers updated
2035   //   R3_RET       - new address of caller's tos, including result, if any
2036   //
2037 
2038   __ BIND(return_from_native_common);
2039 
2040   // Check for pending exception
2041   __ ld(pending_exception, thread_(pending_exception));
2042   __ cmpdi(CCR0, pending_exception, 0);
2043   __ beq(CCR0, return_from_native_no_exception);
2044 
2045   // If there's a pending exception, we really have no result, so
2046   // R3_RET is dead. Resume_interpreter assumes the new tos is in
2047   // R3_RET.
2048   __ mr(R3_RET, R17_tos);
2049   // `resume_interpreter' expects R15_prev_state to be alive.
2050   __ ld(R15_prev_state, state_(_prev_link));
2051   __ b(resume_interpreter);
2052 
2053   __ BIND(return_from_native_no_exception);
2054 
2055   // No pending exception, copy method result from native ABI register
2056   // to tos.
2057 
2058   // Address of stub descriptor address array.
2059   __ load_const(stub_addr, CppInterpreter::tosca_result_to_stack());
2060 
2061   // Pass address of tos to stub.
2062   __ mr(R4_ARG2, R17_tos);
2063 
2064   // Address of stub descriptor address.
2065   __ sldi(result_index, result_index, LogBytesPerWord);
2066   __ add(stub_addr, stub_addr, result_index);
2067 
2068   // Stub descriptor address.
2069   __ ld(stub_addr, 0, stub_addr);
2070 
2071   // TODO: don't do this via a call, do it in place!
2072   //
2073   // call stub via descriptor
2074   // in R3_ARG1/F1_ARG1: result value (R3_RET or F1_RET)
2075   __ call_stub(stub_addr);
2076 
2077   // new tos = result of call in R3_RET
2078 
2079   // `resume_interpreter' expects R15_prev_state to be alive.
2080   __ ld(R15_prev_state, state_(_prev_link));
2081   __ b(resume_interpreter);
2082 
2083   //=============================================================================
2084   // We encountered an exception while computing the interpreter
2085   // state, so R14_state isn't valid. Act as if we just returned from
2086   // the callee method with a pending exception.
2087   __ BIND(stack_overflow_return);
2088 
2089   //
2090   // Registers alive
2091   //   R16_thread        - JavaThread*
2092   //   R1_SP             - old stack pointer
2093   //   R19_method        - callee's Method
2094   //   R17_tos           - address of caller's tos (prepushed)
2095   //   R15_prev_state    - address of caller's BytecodeInterpreter or 0
2096   //   R18_locals        - address of callee's locals array
2097   //
2098   // Registers updated
2099   //   R3_RET           - address of resuming tos, if recursive unwind
2100 
2101   Label Lskip_unextend_SP;
2102 
2103   {
2104   const ConditionRegister is_initial_call = CCR0;
2105   const Register tos_save = R21_tmp1;
2106   const Register tmp = R22_tmp2;
2107 
2108   assert(tos_save->is_nonvolatile(), "need a nonvolatile");
2109 
2110   // Is the exception thrown in the initial Java frame of this frame
2111   // manager frame?
2112   __ cmpdi(is_initial_call, R15_prev_state, 0);
2113   __ bne(is_initial_call, Lskip_unextend_SP);
2114 
2115   // Pop any c2i extension from the stack. This is necessary in the
2116   // non-recursive case (that is we were called by the c2i adapter,
2117   // meaning we have to prev state). In this case we entered the frame
2118   // manager through a special entry which pushes the orignal
2119   // unextended SP to the stack. Here we load it back.
2120   __ ld(R0, _top_ijava_frame_abi(frame_manager_lr), R1_SP);
2121   __ mtlr(R0);
2122   // Resize frame to get rid of a potential extension.
2123   __ resize_frame_to_initial_caller(R11_scratch1, R12_scratch2);
2124 
2125   // Fall through
2126 
2127   __ bind(Lskip_unextend_SP);
2128 
2129   // Throw the exception via RuntimeStub "throw_StackOverflowError_entry".
2130   //
2131   // Previously, we called C-Code directly. As a consequence, a
2132   // possible GC tried to process the argument oops of the top frame
2133   // (see RegisterMap::clear, which sets the corresponding flag to
2134   // true). This lead to crashes because:
2135   // 1. The top register map did not contain locations for the argument registers
2136   // 2. The arguments are dead anyway, could be already overwritten in the worst case
2137   // Solution: Call via special runtime stub that pushes it's own frame. This runtime stub has the flag
2138   // "CodeBlob::caller_must_gc_arguments()" set to "false", what prevents the dead arguments getting GC'd.
2139   //
2140   // 2 cases exist:
2141   // 1. We were called by the c2i adapter / call stub
2142   // 2. We were called by the frame manager
2143   //
2144   // Both cases are handled by this code:
2145   // 1. - initial_caller_sp was saved on stack => Load it back and we're ok
2146   //    - control flow will be:
2147   //      throw_stackoverflow_stub->VM->throw_stackoverflow_stub->forward_excep->excp_blob of calling method
2148   // 2. - control flow will be:
2149   //      throw_stackoverflow_stub->VM->throw_stackoverflow_stub->forward_excep->
2150   //        ->rethrow_excp_entry of frame manager->resume_method
2151   //      Since we restored the caller SP above, the rethrow_excp_entry can restore the original interpreter state
2152   //      registers using the stack and resume the calling method with a pending excp.
2153 
2154   assert(StubRoutines::throw_StackOverflowError_entry() != NULL, "generated in wrong order");
2155   __ load_const(R3_ARG1, (StubRoutines::throw_StackOverflowError_entry()));
2156   __ mtctr(R3_ARG1);
2157   __ bctr();
2158   }
2159   //=============================================================================
2160   // We have popped a frame from an interpreted call. We are assured
2161   // of returning to an interpreted call by the popframe abi. We have
2162   // no return value all we have to do is pop the current frame and
2163   // then make sure that the top of stack (of the caller) gets set to
2164   // where it was when we entered the callee (i.e. the args are still
2165   // in place).  Or we are returning to the interpreter. In the first
2166   // case we must extract result (if any) from the java expression
2167   // stack and store it in the location the native abi would expect
2168   // for a call returning this type. In the second case we must simply
2169   // do a stack to stack move as we unwind.
2170 
2171   __ BIND(popping_frame);
2172 
2173   // Registers alive
2174   //   R14_state
2175   //   R15_prev_state
2176   //   R17_tos
2177   //
2178   // Registers updated
2179   //   R19_method
2180   //   R3_RET
2181   //   msg
2182   {
2183     Label L;
2184 
2185     // Reload callee method, gc may have moved it.
2186     __ ld(R19_method, state_(_method));
2187 
2188     // We may be returning to a deoptimized frame in which case the
2189     // usual assumption of a recursive return is not true.
2190 
2191     // not equal = is recursive call
2192     __ cmpdi(CCR0, R15_prev_state, 0);
2193 
2194     __ bne(CCR0, L);
2195 
2196     // Pop_frame capability.
2197     // The pop_frame api says that the underlying frame is a Java frame, in this case
2198     // (prev_state==null) it must be a compiled frame:
2199     //
2200     // Stack at this point: I, C2I + C, ...
2201     //
2202     // The outgoing arguments of the call have just been copied (popframe_preserve_args).
2203     // By the pop_frame api, we must end up in an interpreted frame. So the compiled frame
2204     // will be deoptimized. Deoptimization will restore the outgoing arguments from
2205     // popframe_preserve_args, adjust the tos such that it includes the popframe_preserve_args,
2206     // and adjust the bci such that the call will be executed again.
2207     // We have no results, just pop the interpreter frame, resize the compiled frame to get rid
2208     // of the c2i extension and return to the deopt_handler.
2209     __ b(unwind_initial_activation);
2210 
2211     // is recursive call
2212     __ bind(L);
2213 
2214     // Resume_interpreter expects the original tos in R3_RET.
2215     __ ld(R3_RET, prev_state_(_stack));
2216 
2217     // We're done.
2218     __ li(msg, BytecodeInterpreter::popping_frame);
2219 
2220     __ b(unwind_recursive_activation);
2221   }
2222 
2223 
2224   //=============================================================================
2225 
2226   // We have finished an interpreted call. We are either returning to
2227   // native (call_stub/c2) or we are returning to the interpreter.
2228   // When returning to native, we must extract the result (if any)
2229   // from the java expression stack and store it in the location the
2230   // native abi expects. When returning to the interpreter we must
2231   // simply do a stack to stack move as we unwind.
2232 
2233   __ BIND(return_from_interpreted_method);
2234 
2235   //
2236   // Registers alive
2237   //   R16_thread     - JavaThread*
2238   //   R15_prev_state - address of caller's BytecodeInterpreter or 0
2239   //   R14_state      - address of callee's interpreter state
2240   //   R1_SP          - callee's stack pointer
2241   //
2242   // Registers updated
2243   //   R19_method     - callee's method
2244   //   R3_RET         - address of result (new caller's tos),
2245   //
2246   // if returning to interpreted
2247   //   msg  - message for interpreter,
2248   // if returning to interpreted
2249   //
2250 
2251   // Check if this is the initial invocation of the frame manager.
2252   // If so, R15_prev_state will be null.
2253   __ cmpdi(CCR0, R15_prev_state, 0);
2254 
2255   // Reload callee method, gc may have moved it.
2256   __ ld(R19_method, state_(_method));
2257 
2258   // Load the method's result type.
2259   __ lwz(result_index, method_(result_index));
2260 
2261   // Go to return_to_initial_caller if R15_prev_state is null.
2262   __ beq(CCR0, return_to_initial_caller);
2263 
2264   // Copy callee's result to caller's expression stack via inline stack-to-stack
2265   // converters.
2266   {
2267     Register new_tos   = R3_RET;
2268     Register from_temp = R4_ARG2;
2269     Register from      = R5_ARG3;
2270     Register tos       = R6_ARG4;
2271     Register tmp1      = R7_ARG5;
2272     Register tmp2      = R8_ARG6;
2273 
2274     ConditionRegister result_type_is_void   = CCR1;
2275     ConditionRegister result_type_is_long   = CCR2;
2276     ConditionRegister result_type_is_double = CCR3;
2277 
2278     Label stack_to_stack_void;
2279     Label stack_to_stack_double_slot; // T_LONG, T_DOUBLE
2280     Label stack_to_stack_single_slot; // T_BOOLEAN, T_BYTE, T_CHAR, T_SHORT, T_INT, T_FLOAT, T_OBJECT
2281     Label stack_to_stack_done;
2282 
2283     // Pass callee's address of tos + BytesPerWord
2284     __ ld(from_temp, state_(_stack));
2285 
2286     // result type: void
2287     __ cmpwi(result_type_is_void, result_index, AbstractInterpreter::BasicType_as_index(T_VOID));
2288 
2289     // Pass caller's tos == callee's locals address
2290     __ ld(tos, state_(_locals));
2291 
2292     // result type: long
2293     __ cmpwi(result_type_is_long, result_index, AbstractInterpreter::BasicType_as_index(T_LONG));
2294 
2295     __ addi(from, from_temp, Interpreter::stackElementSize);
2296 
2297     // !! don't branch above this line !!
2298 
2299     // handle void
2300     __ beq(result_type_is_void,   stack_to_stack_void);
2301 
2302     // result type: double
2303     __ cmpwi(result_type_is_double, result_index, AbstractInterpreter::BasicType_as_index(T_DOUBLE));
2304 
2305     // handle long or double
2306     __ beq(result_type_is_long, stack_to_stack_double_slot);
2307     __ beq(result_type_is_double, stack_to_stack_double_slot);
2308 
2309     // fall through to single slot types (incl. object)
2310 
2311     {
2312       __ BIND(stack_to_stack_single_slot);
2313       // T_BOOLEAN, T_BYTE, T_CHAR, T_SHORT, T_INT, T_FLOAT, T_OBJECT
2314 
2315       __ ld(tmp1, 0, from);
2316       __ std(tmp1, 0, tos);
2317       // New expression stack top
2318       __ addi(new_tos, tos, - BytesPerWord);
2319 
2320       __ b(stack_to_stack_done);
2321     }
2322 
2323     {
2324       __ BIND(stack_to_stack_double_slot);
2325       // T_LONG, T_DOUBLE
2326 
2327       // Move both entries for debug purposes even though only one is live
2328       __ ld(tmp1, BytesPerWord, from);
2329       __ ld(tmp2, 0, from);
2330       __ std(tmp1, 0, tos);
2331       __ std(tmp2, -BytesPerWord, tos);
2332 
2333       // new expression stack top
2334       __ addi(new_tos, tos, - 2 * BytesPerWord); // two slots
2335       __ b(stack_to_stack_done);
2336     }
2337 
2338     {
2339       __ BIND(stack_to_stack_void);
2340       // T_VOID
2341 
2342       // new expression stack top
2343       __ mr(new_tos, tos);
2344       // fall through to stack_to_stack_done
2345     }
2346 
2347     __ BIND(stack_to_stack_done);
2348   }
2349 
2350   // new tos = R3_RET
2351 
2352   // Get the message for the interpreter
2353   __ li(msg, BytecodeInterpreter::method_resume);
2354 
2355   // And fall thru
2356 
2357 
2358   //=============================================================================
2359   // Restore caller's interpreter state and pass pointer to caller's
2360   // new tos to caller.
2361 
2362   __ BIND(unwind_recursive_activation);
2363 
2364   //
2365   // Registers alive
2366   //   R15_prev_state   - address of caller's BytecodeInterpreter
2367   //   R3_RET           - address of caller's tos
2368   //   msg              - message for caller's BytecodeInterpreter
2369   //   R1_SP            - callee's stack pointer
2370   //
2371   // Registers updated
2372   //   R14_state        - address of caller's BytecodeInterpreter
2373   //   R15_prev_state   - address of its parent or 0
2374   //
2375 
2376   // Pop callee's interpreter and set R14_state to caller's interpreter.
2377   __ pop_interpreter_state(/*prev_state_may_be_0=*/false);
2378 
2379   // And fall thru
2380 
2381 
2382   //=============================================================================
2383   // Resume the (calling) interpreter after a call.
2384 
2385   __ BIND(resume_interpreter);
2386 
2387   //
2388   // Registers alive
2389   //   R14_state        - address of resuming BytecodeInterpreter
2390   //   R15_prev_state   - address of its parent or 0
2391   //   R3_RET           - address of resuming tos
2392   //   msg              - message for resuming interpreter
2393   //   R1_SP            - callee's stack pointer
2394   //
2395   // Registers updated
2396   //   R1_SP            - caller's stack pointer
2397   //
2398 
2399   // Restore C stack pointer of caller (resuming interpreter),
2400   // R14_state already points to the resuming BytecodeInterpreter.
2401   __ pop_interpreter_frame_to_state(R14_state, R21_tmp1, R11_scratch1, R12_scratch2);
2402 
2403   // Store new address of tos (holding return value) in interpreter state.
2404   __ std(R3_RET, state_(_stack));
2405 
2406   // Store message for interpreter.
2407   __ stw(msg, state_(_msg));
2408 
2409   __ b(call_interpreter);
2410 
2411   //=============================================================================
2412   // Interpreter returning to native code (call_stub/c1/c2) from
2413   // initial activation. Convert stack result and unwind activation.
2414 
2415   __ BIND(return_to_initial_caller);
2416 
2417   //
2418   // Registers alive
2419   //   R19_method       - callee's Method
2420   //   R14_state        - address of callee's interpreter state
2421   //   R16_thread       - JavaThread
2422   //   R1_SP            - callee's stack pointer
2423   //
2424   // Registers updated
2425   //   R3_RET/F1_RET - result in expected output register
2426   //
2427 
2428   // If we have an exception pending we have no result and we
2429   // must figure out where to really return to.
2430   //
2431   __ ld(pending_exception, thread_(pending_exception));
2432   __ cmpdi(CCR0, pending_exception, 0);
2433   __ bne(CCR0, unwind_initial_activation_pending_exception);
2434 
2435   __ lwa(result_index, method_(result_index));
2436 
2437   // Address of stub descriptor address array.
2438   __ load_const(stub_addr, CppInterpreter::stack_result_to_native());
2439 
2440   // Pass address of callee's tos + BytesPerWord.
2441   // Will then point directly to result.
2442   __ ld(R3_ARG1, state_(_stack));
2443   __ addi(R3_ARG1, R3_ARG1, Interpreter::stackElementSize);
2444 
2445   // Address of stub descriptor address
2446   __ sldi(result_index, result_index, LogBytesPerWord);
2447   __ add(stub_addr, stub_addr, result_index);
2448 
2449   // Stub descriptor address
2450   __ ld(stub_addr, 0, stub_addr);
2451 
2452   // TODO: don't do this via a call, do it in place!
2453   //
2454   // call stub via descriptor
2455   __ call_stub(stub_addr);
2456 
2457   __ BIND(unwind_initial_activation);
2458 
2459   // Unwind from initial activation. No exception is pending.
2460 
2461   //
2462   // Stack layout at this point:
2463   //
2464   //    0       [TOP_IJAVA_FRAME_ABI]         <-- R1_SP
2465   //            ...
2466   //    CALLER  [PARENT_IJAVA_FRAME_ABI]
2467   //            ...
2468   //    CALLER  [unextended ABI]
2469   //            ...
2470   //
2471   //  The CALLER frame has a C2I adapter or is an entry-frame.
2472   //
2473 
2474   // An interpreter frame exists, we may pop the TOP_IJAVA_FRAME and
2475   // turn the caller's PARENT_IJAVA_FRAME back into a TOP_IJAVA_FRAME.
2476   // But, we simply restore the return pc from the caller's frame and
2477   // use the caller's initial_caller_sp as the new SP which pops the
2478   // interpreter frame and "resizes" the caller's frame to its "unextended"
2479   // size.
2480 
2481   // get rid of top frame
2482   __ pop_frame();
2483 
2484   // Load return PC from parent frame.
2485   __ ld(R21_tmp1, _parent_ijava_frame_abi(lr), R1_SP);
2486 
2487   // Resize frame to get rid of a potential extension.
2488   __ resize_frame_to_initial_caller(R11_scratch1, R12_scratch2);
2489 
2490   // update LR
2491   __ mtlr(R21_tmp1);
2492 
2493   // return
2494   __ blr();
2495 
2496   //=============================================================================
2497   // Unwind from initial activation. An exception is pending
2498 
2499   __ BIND(unwind_initial_activation_pending_exception);
2500 
2501   //
2502   // Stack layout at this point:
2503   //
2504   //   0       [TOP_IJAVA_FRAME_ABI]         <-- R1_SP
2505   //           ...
2506   //   CALLER  [PARENT_IJAVA_FRAME_ABI]
2507   //           ...
2508   //   CALLER  [unextended ABI]
2509   //           ...
2510   //
2511   // The CALLER frame has a C2I adapter or is an entry-frame.
2512   //
2513 
2514   // An interpreter frame exists, we may pop the TOP_IJAVA_FRAME and
2515   // turn the caller's PARENT_IJAVA_FRAME back into a TOP_IJAVA_FRAME.
2516   // But, we just pop the current TOP_IJAVA_FRAME and fall through
2517 
2518   __ pop_frame();
2519   __ ld(R3_ARG1, _top_ijava_frame_abi(lr), R1_SP);
2520 
2521   //
2522   // Stack layout at this point:
2523   //
2524   //   CALLER  [PARENT_IJAVA_FRAME_ABI]      <-- R1_SP
2525   //           ...
2526   //   CALLER  [unextended ABI]
2527   //           ...
2528   //
2529   // The CALLER frame has a C2I adapter or is an entry-frame.
2530   //
2531   // Registers alive
2532   //   R16_thread
2533   //   R3_ARG1 - return address to caller
2534   //
2535   // Registers updated
2536   //   R3_ARG1 - address of pending exception
2537   //   R4_ARG2 - issuing pc = return address to caller
2538   //   LR      - address of exception handler stub
2539   //
2540 
2541   // Resize frame to get rid of a potential extension.
2542   __ resize_frame_to_initial_caller(R11_scratch1, R12_scratch2);
2543 
2544   __ mr(R14, R3_ARG1);   // R14 := ARG1
2545   __ mr(R4_ARG2, R3_ARG1);  // ARG2 := ARG1
2546 
2547   // Find the address of the "catch_exception" stub.
2548   __ push_frame_abi112(0, R11_scratch1);
2549   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address),
2550                   R16_thread,
2551                   R4_ARG2);
2552   __ pop_frame();
2553 
2554   // Load continuation address into LR.
2555   __ mtlr(R3_RET);
2556 
2557   // Load address of pending exception and clear it in thread object.
2558   __ ld(R3_ARG1/*R3_RET*/, thread_(pending_exception));
2559   __ li(R4_ARG2, 0);
2560   __ std(R4_ARG2, thread_(pending_exception));
2561 
2562   // re-load issuing pc
2563   __ mr(R4_ARG2, R14);
2564 
2565   // Branch to found exception handler.
2566   __ blr();
2567 
2568   //=============================================================================
2569   // Call a new method. Compute new args and trim the expression stack
2570   // to only what we are currently using and then recurse.
2571 
2572   __ BIND(call_method);
2573 
2574   //
2575   //  Registers alive
2576   //    R16_thread
2577   //    R14_state      - address of caller's BytecodeInterpreter
2578   //    R1_SP          - caller's stack pointer
2579   //
2580   //  Registers updated
2581   //    R15_prev_state - address of caller's BytecodeInterpreter
2582   //    R17_tos        - address of caller's tos
2583   //    R19_method     - callee's Method
2584   //    R1_SP          - trimmed back
2585   //
2586 
2587   // Very-local scratch registers.
2588 
2589   const Register offset = R21_tmp1;
2590   const Register tmp    = R22_tmp2;
2591   const Register self_entry  = R23_tmp3;
2592   const Register stub_entry  = R24_tmp4;
2593 
2594   const ConditionRegister cr = CCR0;
2595 
2596   // Load the address of the frame manager.
2597   __ load_const(self_entry, &interpreter_frame_manager);
2598   __ ld(self_entry, 0, self_entry);
2599 
2600   // Load BytecodeInterpreter._result._to_call._callee (callee's Method).
2601   __ ld(R19_method, state_(_result._to_call._callee));
2602   // Load BytecodeInterpreter._stack (outgoing tos).
2603   __ ld(R17_tos, state_(_stack));
2604 
2605   // Save address of caller's BytecodeInterpreter.
2606   __ mr(R15_prev_state, R14_state);
2607 
2608   // Load the callee's entry point.
2609   // Load BytecodeInterpreter._result._to_call._callee_entry_point.
2610   __ ld(stub_entry, state_(_result._to_call._callee_entry_point));
2611 
2612   // Check whether stub_entry is equal to self_entry.
2613   __ cmpd(cr, self_entry, stub_entry);
2614   // if (self_entry == stub_entry)
2615   //   do a re-dispatch
2616   __ beq(cr, re_dispatch);
2617   // else
2618   //   call the specialized entry (adapter for jni or compiled code)
2619   __ BIND(call_special);
2620 
2621   //
2622   // Call the entry generated by `InterpreterGenerator::generate_native_entry'.
2623   //
2624   // Registers alive
2625   //   R16_thread
2626   //   R15_prev_state    - address of caller's BytecodeInterpreter
2627   //   R19_method        - callee's Method
2628   //   R17_tos           - address of caller's tos
2629   //   R1_SP             - caller's stack pointer
2630   //
2631 
2632   // Mark return from specialized entry for generate_native_entry.
2633   guarantee(return_from_native_pc != (address) NULL, "precondition");
2634   frame_manager_specialized_return = return_from_native_pc;
2635 
2636   // Set sender_SP in case we call interpreter native wrapper which
2637   // will expect it. Compiled code should not care.
2638   __ mr(R21_sender_SP, R1_SP);
2639 
2640   // Do a tail call here, and let the link register point to
2641   // frame_manager_specialized_return which is return_from_native_pc.
2642   __ load_const(tmp, frame_manager_specialized_return);
2643   __ call_stub_and_return_to(stub_entry,  tmp /* return_pc=tmp */);
2644 
2645 
2646   //=============================================================================
2647   //
2648   // InterpretMethod triggered OSR compilation of some Java method M
2649   // and now asks to run the compiled code.  We call this code the
2650   // `callee'.
2651   //
2652   // This is our current idea on how OSR should look like on PPC64:
2653   //
2654   // While interpreting a Java method M the stack is:
2655   //
2656   //  (InterpretMethod (M), IJAVA_FRAME (M), ANY_FRAME, ...).
2657   //
2658   // After having OSR compiled M, `InterpretMethod' returns to the
2659   // frame manager, sending the message `retry_method_osr'.  The stack
2660   // is:
2661   //
2662   //  (IJAVA_FRAME (M), ANY_FRAME, ...).
2663   //
2664   // The compiler will have generated an `nmethod' suitable for
2665   // continuing execution of M at the bytecode index at which OSR took
2666   // place.  So now the frame manager calls the OSR entry.  The OSR
2667   // entry sets up a JIT_FRAME for M and continues execution of M with
2668   // initial state determined by the IJAVA_FRAME.
2669   //
2670   //  (JIT_FRAME (M), IJAVA_FRAME (M), ANY_FRAME, ...).
2671   //
2672 
2673   __ BIND(retry_method_osr);
2674   {
2675   //
2676   // Registers alive
2677   //   R16_thread
2678   //   R15_prev_state     - address of caller's BytecodeInterpreter
2679   //   R14_state          - address of callee's BytecodeInterpreter
2680   //   R1_SP              - callee's SP before call to InterpretMethod
2681   //
2682   // Registers updated
2683   //   R17                - pointer to callee's locals array
2684   //                       (declared via `interpreter_arg_ptr_reg' in the AD file)
2685   //   R19_method         - callee's Method
2686   //   R1_SP              - callee's SP (will become SP of OSR adapter frame)
2687   //
2688 
2689   // Provide a debugger breakpoint in the frame manager if breakpoints
2690   // in osr'd methods are requested.
2691 #ifdef COMPILER2
2692   NOT_PRODUCT( if (OptoBreakpointOSR) { __ illtrap(); } )
2693 #endif
2694 
2695   // Load callee's pointer to locals array from callee's state.
2696   //  __ ld(R17, state_(_locals));
2697 
2698   // Load osr entry.
2699   __ ld(R12_scratch2, state_(_result._osr._osr_entry));
2700 
2701   // Load address of temporary osr buffer to arg1.
2702   __ ld(R3_ARG1, state_(_result._osr._osr_buf));
2703   __ mtctr(R12_scratch2);
2704 
2705   // Load method oop, gc may move it during execution of osr'd method.
2706   __ ld(R22_tmp2, state_(_method));
2707   // Load message 'call_method'.
2708   __ li(R23_tmp3, BytecodeInterpreter::call_method);
2709 
2710   {
2711     // Pop the IJAVA frame of the method which we are going to call osr'd.
2712     Label no_state, skip_no_state;
2713     __ pop_interpreter_state(/*prev_state_may_be_0=*/true);
2714     __ cmpdi(CCR0, R14_state,0);
2715     __ beq(CCR0, no_state);
2716     // return to interpreter
2717     __ pop_interpreter_frame_to_state(R14_state, R11_scratch1, R12_scratch2, R21_tmp1);
2718 
2719     // Init _result._to_call._callee and tell gc that it contains a valid oop
2720     // by setting _msg to 'call_method'.
2721     __ std(R22_tmp2, state_(_result._to_call._callee));
2722     // TODO: PPC port: assert(4 == BytecodeInterpreter::sz_msg(), "unexpected field size");
2723     __ stw(R23_tmp3, state_(_msg));
2724 
2725     __ load_const(R21_tmp1, frame_manager_specialized_return);
2726     __ b(skip_no_state);
2727     __ bind(no_state);
2728 
2729     // Return to initial caller.
2730 
2731     // Get rid of top frame.
2732     __ pop_frame();
2733 
2734     // Load return PC from parent frame.
2735     __ ld(R21_tmp1, _parent_ijava_frame_abi(lr), R1_SP);
2736 
2737     // Resize frame to get rid of a potential extension.
2738     __ resize_frame_to_initial_caller(R11_scratch1, R12_scratch2);
2739 
2740     __ bind(skip_no_state);
2741 
2742     // Update LR with return pc.
2743     __ mtlr(R21_tmp1);
2744   }
2745   // Jump to the osr entry point.
2746   __ bctr();
2747 
2748   }
2749 
2750   //=============================================================================
2751   // Interpreted method "returned" with an exception, pass it on.
2752   // Pass no result, unwind activation and continue/return to
2753   // interpreter/call_stub/c2.
2754 
2755   __ BIND(throwing_exception);
2756 
2757   // Check if this is the initial invocation of the frame manager.  If
2758   // so, previous interpreter state in R15_prev_state will be null.
2759 
2760   // New tos of caller is callee's first parameter address, that is
2761   // callee's incoming arguments are popped.
2762   __ ld(R3_RET, state_(_locals));
2763 
2764   // Check whether this is an initial call.
2765   __ cmpdi(CCR0, R15_prev_state, 0);
2766   // Yes, called from the call stub or from generated code via a c2i frame.
2767   __ beq(CCR0, unwind_initial_activation_pending_exception);
2768 
2769   // Send resume message, interpreter will see the exception first.
2770 
2771   __ li(msg, BytecodeInterpreter::method_resume);
2772   __ b(unwind_recursive_activation);
2773 
2774 
2775   //=============================================================================
2776   // Push the last instruction out to the code buffer.
2777 
2778   {
2779     __ unimplemented("end of InterpreterGenerator::generate_normal_entry", 128);
2780   }
2781 
2782   interpreter_frame_manager = entry;
2783   return interpreter_frame_manager;
2784 }
2785 
2786 // Generate code for various sorts of method entries
2787 //
2788 address AbstractInterpreterGenerator::generate_method_entry(AbstractInterpreter::MethodKind kind) {
2789   address entry_point = NULL;
2790 
2791   switch (kind) {
2792     case Interpreter::zerolocals                 :                                                                              break;
2793     case Interpreter::zerolocals_synchronized    :                                                                              break;
2794     case Interpreter::native                     : // Fall thru
2795     case Interpreter::native_synchronized        : entry_point = ((CppInterpreterGenerator*)this)->generate_native_entry();     break;
2796     case Interpreter::empty                      :                                                                              break;
2797     case Interpreter::accessor                   : entry_point = ((InterpreterGenerator*)this)->generate_accessor_entry();      break;
2798     case Interpreter::abstract                   : entry_point = ((InterpreterGenerator*)this)->generate_abstract_entry();      break;
2799     // These are special interpreter intrinsics which we don't support so far.
2800     case Interpreter::java_lang_math_sin         :                                                                              break;
2801     case Interpreter::java_lang_math_cos         :                                                                              break;
2802     case Interpreter::java_lang_math_tan         :                                                                              break;
2803     case Interpreter::java_lang_math_abs         :                                                                              break;
2804     case Interpreter::java_lang_math_log         :                                                                              break;
2805     case Interpreter::java_lang_math_log10       :                                                                              break;
2806     case Interpreter::java_lang_math_sqrt        :                                                                              break;
2807     case Interpreter::java_lang_math_pow         :                                                                              break;
2808     case Interpreter::java_lang_math_exp         :                                                                              break;
2809     case Interpreter::java_lang_ref_reference_get: entry_point = ((InterpreterGenerator*)this)->generate_Reference_get_entry(); break;
2810     default                                      : ShouldNotReachHere();                                                        break;
2811   }
2812 
2813   if (entry_point) {
2814     return entry_point;
2815   }
2816   return ((InterpreterGenerator*)this)->generate_normal_entry();
2817 }
2818 
2819 InterpreterGenerator::InterpreterGenerator(StubQueue* code)
2820  : CppInterpreterGenerator(code) {
2821    generate_all(); // down here so it can be "virtual"
2822 }
2823 
2824 // How much stack a topmost interpreter method activation needs in words.
2825 int AbstractInterpreter::size_top_interpreter_activation(Method* method) {
2826   // Computation is in bytes not words to match layout_activation_impl
2827   // below, but the return is in words.
2828 
2829   //
2830   //  0       [TOP_IJAVA_FRAME_ABI]                                                    \
2831   //          alignment (optional)                                             \       |
2832   //          [operand stack / Java parameters] > stack                        |       |
2833   //          [monitors] (optional)             > monitors                     |       |
2834   //          [PARENT_IJAVA_FRAME_ABI]                                \        |       |
2835   //          [BytecodeInterpreter object]      > interpreter \       |        |       |
2836   //          alignment (optional)                            | round | parent | round | top
2837   //          [Java result] (2 slots)           > result      |       |        |       |
2838   //          [Java non-arg locals]             \ locals      |       |        |       |
2839   //          [arg locals]                      /             /       /        /       /
2840   //
2841 
2842   int locals = method->max_locals() * BytesPerWord;
2843   int interpreter = frame::interpreter_frame_cinterpreterstate_size_in_bytes();
2844   int result = 2 * BytesPerWord;
2845 
2846   int parent = round_to(interpreter + result + locals, 16) + frame::parent_ijava_frame_abi_size;
2847 
2848   int stack = method->max_stack() * BytesPerWord;
2849   int monitors = method->is_synchronized() ? frame::interpreter_frame_monitor_size_in_bytes() : 0;
2850   int top = round_to(parent + monitors + stack, 16) + frame::top_ijava_frame_abi_size;
2851 
2852   return (top / BytesPerWord);
2853 }
2854 
2855 void BytecodeInterpreter::layout_interpreterState(interpreterState to_fill,
2856                                                   frame* caller,
2857                                                   frame* current,
2858                                                   Method* method,
2859                                                   intptr_t* locals,
2860                                                   intptr_t* stack,
2861                                                   intptr_t* stack_base,
2862                                                   intptr_t* monitor_base,
2863                                                   intptr_t* frame_sp,
2864                                                   bool is_top_frame) {
2865   // What about any vtable?
2866   //
2867   to_fill->_thread = JavaThread::current();
2868   // This gets filled in later but make it something recognizable for now.
2869   to_fill->_bcp = method->code_base();
2870   to_fill->_locals = locals;
2871   to_fill->_constants = method->constants()->cache();
2872   to_fill->_method = method;
2873   to_fill->_mdx = NULL;
2874   to_fill->_stack = stack;
2875 
2876   if (is_top_frame && JavaThread::current()->popframe_forcing_deopt_reexecution()) {
2877     to_fill->_msg = deopt_resume2;
2878   } else {
2879     to_fill->_msg = method_resume;
2880   }
2881   to_fill->_result._to_call._bcp_advance = 0;
2882   to_fill->_result._to_call._callee_entry_point = NULL; // doesn't matter to anyone
2883   to_fill->_result._to_call._callee = NULL; // doesn't matter to anyone
2884   to_fill->_prev_link = NULL;
2885 
2886   if (caller->is_interpreted_frame()) {
2887     interpreterState prev  = caller->get_interpreterState();
2888 
2889     // Support MH calls. Make sure the interpreter will return the right address:
2890     // 1. Caller did ordinary interpreted->compiled call call: Set a prev_state
2891     //    which makes the CPP interpreter return to frame manager "return_from_interpreted_method"
2892     //    entry after finishing execution.
2893     // 2. Caller did a MH call: If the caller has a MethodHandleInvoke in it's
2894     //    state (invariant: must be the caller of the bottom vframe) we used the
2895     //    "call_special" entry to do the call, meaning the arguments have not been
2896     //    popped from the stack. Therefore, don't enter a prev state in this case
2897     //    in order to return to "return_from_native" frame manager entry which takes
2898     //    care of popping arguments. Also, don't overwrite the MH.invoke Method in
2899     //    the prev_state in order to be able to figure out the number of arguments to
2900     //     pop.
2901     // The parameter method can represent MethodHandle.invokeExact(...).
2902     // The MethodHandleCompiler generates these synthetic Methods,
2903     // including bytecodes, if an invokedynamic call gets inlined. In
2904     // this case we want to return like from any other interpreted
2905     // Java call, so we set _prev_link.
2906     to_fill->_prev_link = prev;
2907 
2908     if (*prev->_bcp == Bytecodes::_invokeinterface || *prev->_bcp == Bytecodes::_invokedynamic) {
2909       prev->_result._to_call._bcp_advance = 5;
2910     } else {
2911       prev->_result._to_call._bcp_advance = 3;
2912     }
2913   }
2914   to_fill->_oop_temp = NULL;
2915   to_fill->_stack_base = stack_base;
2916   // Need +1 here because stack_base points to the word just above the
2917   // first expr stack entry and stack_limit is supposed to point to
2918   // the word just below the last expr stack entry. See
2919   // generate_compute_interpreter_state.
2920   to_fill->_stack_limit = stack_base - (method->max_stack() + 1);
2921   to_fill->_monitor_base = (BasicObjectLock*) monitor_base;
2922 
2923   to_fill->_frame_bottom = frame_sp;
2924 
2925   // PPC64 specific
2926   to_fill->_last_Java_pc = NULL;
2927   to_fill->_last_Java_fp = NULL;
2928   to_fill->_last_Java_sp = frame_sp;
2929 #ifdef ASSERT
2930   to_fill->_self_link = to_fill;
2931   to_fill->_native_fresult = 123456.789;
2932   to_fill->_native_lresult = CONST64(0xdeafcafedeadc0de);
2933 #endif
2934 }
2935 
2936 void BytecodeInterpreter::pd_layout_interpreterState(interpreterState istate,
2937                                                      address last_Java_pc,
2938                                                      intptr_t* last_Java_fp) {
2939   istate->_last_Java_pc = last_Java_pc;
2940   istate->_last_Java_fp = last_Java_fp;
2941 }
2942 
2943 int AbstractInterpreter::layout_activation(Method* method,
2944                                            int temps,        // Number of slots on java expression stack in use.
2945                                            int popframe_args,
2946                                            int monitors,     // Number of active monitors.
2947                                            int caller_actual_parameters,
2948                                            int callee_params,// Number of slots for callee parameters.
2949                                            int callee_locals,// Number of slots for locals.
2950                                            frame* caller,
2951                                            frame* interpreter_frame,
2952                                            bool is_top_frame,
2953                                            bool is_bottom_frame) {
2954 
2955   // NOTE this code must exactly mimic what
2956   // InterpreterGenerator::generate_compute_interpreter_state() does
2957   // as far as allocating an interpreter frame. However there is an
2958   // exception. With the C++ based interpreter only the top most frame
2959   // has a full sized expression stack.  The 16 byte slop factor is
2960   // both the abi scratch area and a place to hold a result from a
2961   // callee on its way to the callers stack.
2962 
2963   int monitor_size = frame::interpreter_frame_monitor_size_in_bytes() * monitors;
2964   int frame_size;
2965   int top_frame_size = round_to(frame::interpreter_frame_cinterpreterstate_size_in_bytes()
2966                                 + monitor_size
2967                                 + (method->max_stack() *Interpreter::stackElementWords * BytesPerWord)
2968                                 + 2*BytesPerWord,
2969                                 frame::alignment_in_bytes)
2970                       + frame::top_ijava_frame_abi_size;
2971   if (is_top_frame) {
2972     frame_size = top_frame_size;
2973   } else {
2974     frame_size = round_to(frame::interpreter_frame_cinterpreterstate_size_in_bytes()
2975                           + monitor_size
2976                           + ((temps - callee_params + callee_locals) *
2977                              Interpreter::stackElementWords * BytesPerWord)
2978                           + 2*BytesPerWord,
2979                           frame::alignment_in_bytes)
2980                  + frame::parent_ijava_frame_abi_size;
2981     assert(popframe_args==0, "non-zero for top_frame only");
2982   }
2983 
2984   // If we actually have a frame to layout we must now fill in all the pieces.
2985   if (interpreter_frame != NULL) {
2986 
2987     intptr_t sp = (intptr_t)interpreter_frame->sp();
2988     intptr_t fp = *(intptr_t *)sp;
2989     assert(fp == (intptr_t)caller->sp(), "fp must match");
2990     interpreterState cur_state =
2991       (interpreterState)(fp - frame::interpreter_frame_cinterpreterstate_size_in_bytes());
2992 
2993     // Now fill in the interpreterState object.
2994 
2995     intptr_t* locals;
2996     if (caller->is_interpreted_frame()) {
2997       // Locals must agree with the caller because it will be used to set the
2998       // caller's tos when we return.
2999       interpreterState prev  = caller->get_interpreterState();
3000       // Calculate start of "locals" for MH calls.  For MH calls, the
3001       // current method() (= MH target) and prev->callee() (=
3002       // MH.invoke*()) are different and especially have different
3003       // signatures. To pop the argumentsof the caller, we must use
3004       // the prev->callee()->size_of_arguments() because that's what
3005       // the caller actually pushed.  Currently, for synthetic MH
3006       // calls (deoptimized from inlined MH calls), detected by
3007       // is_method_handle_invoke(), we use the callee's arguments
3008       // because here, the caller's and callee's signature match.
3009       if (true /*!caller->is_at_mh_callsite()*/) {
3010         locals = prev->stack() + method->size_of_parameters();
3011       } else {
3012         // Normal MH call.
3013         locals = prev->stack() + prev->callee()->size_of_parameters();
3014       }
3015     } else {
3016       bool is_deopted;
3017       locals = (intptr_t*) (fp + ((method->max_locals() - 1) * BytesPerWord) +
3018                             frame::parent_ijava_frame_abi_size);
3019     }
3020 
3021     intptr_t* monitor_base = (intptr_t*) cur_state;
3022     intptr_t* stack_base   = (intptr_t*) ((intptr_t) monitor_base - monitor_size);
3023 
3024     // Provide pop_frame capability on PPC64, add popframe_args.
3025     // +1 because stack is always prepushed.
3026     intptr_t* stack = (intptr_t*) ((intptr_t) stack_base - (temps + popframe_args + 1) * BytesPerWord);
3027 
3028     BytecodeInterpreter::layout_interpreterState(cur_state,
3029                                                  caller,
3030                                                  interpreter_frame,
3031                                                  method,
3032                                                  locals,
3033                                                  stack,
3034                                                  stack_base,
3035                                                  monitor_base,
3036                                                  (intptr_t*)(((intptr_t)fp)-top_frame_size),
3037                                                  is_top_frame);
3038 
3039     BytecodeInterpreter::pd_layout_interpreterState(cur_state, interpreter_return_address,
3040                                                     interpreter_frame->fp());
3041   }
3042   return frame_size/BytesPerWord;
3043 }
3044 
3045 #endif // CC_INTERP