1 /*
   2  * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2012, 2024 SAP SE. 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/macroAssembler.inline.hpp"
  28 #include "code/debugInfoRec.hpp"
  29 #include "code/compiledIC.hpp"
  30 #include "code/vtableStubs.hpp"
  31 #include "frame_ppc.hpp"
  32 #include "compiler/oopMap.hpp"
  33 #include "gc/shared/gcLocker.hpp"
  34 #include "interpreter/interpreter.hpp"
  35 #include "interpreter/interp_masm.hpp"
  36 #include "memory/resourceArea.hpp"
  37 #include "oops/klass.inline.hpp"
  38 #include "prims/methodHandles.hpp"
  39 #include "runtime/continuation.hpp"
  40 #include "runtime/continuationEntry.inline.hpp"
  41 #include "runtime/jniHandles.hpp"
  42 #include "runtime/os.inline.hpp"
  43 #include "runtime/safepointMechanism.hpp"
  44 #include "runtime/sharedRuntime.hpp"
  45 #include "runtime/signature.hpp"
  46 #include "runtime/stubRoutines.hpp"
  47 #include "runtime/timerTrace.hpp"
  48 #include "runtime/vframeArray.hpp"
  49 #include "utilities/align.hpp"
  50 #include "utilities/macros.hpp"
  51 #include "vmreg_ppc.inline.hpp"
  52 #ifdef COMPILER1
  53 #include "c1/c1_Runtime1.hpp"
  54 #endif
  55 #ifdef COMPILER2
  56 #include "opto/ad.hpp"
  57 #include "opto/runtime.hpp"
  58 #endif
  59 
  60 #include <alloca.h>
  61 
  62 #define __ masm->
  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 
  73 class RegisterSaver {
  74  // Used for saving volatile registers.
  75  public:
  76 
  77   // Support different return pc locations.
  78   enum ReturnPCLocation {
  79     return_pc_is_lr,
  80     return_pc_is_pre_saved,
  81     return_pc_is_thread_saved_exception_pc
  82   };
  83 
  84   static OopMap* push_frame_reg_args_and_save_live_registers(MacroAssembler* masm,
  85                          int* out_frame_size_in_bytes,
  86                          bool generate_oop_map,
  87                          int return_pc_adjustment,
  88                          ReturnPCLocation return_pc_location,
  89                          bool save_vectors = false);
  90   static void    restore_live_registers_and_pop_frame(MacroAssembler* masm,
  91                          int frame_size_in_bytes,
  92                          bool restore_ctr,
  93                          bool save_vectors = false);
  94 
  95   static void push_frame_and_save_argument_registers(MacroAssembler* masm,
  96                          Register r_temp,
  97                          int frame_size,
  98                          int total_args,
  99                          const VMRegPair *regs, const VMRegPair *regs2 = nullptr);
 100   static void restore_argument_registers_and_pop_frame(MacroAssembler*masm,
 101                          int frame_size,
 102                          int total_args,
 103                          const VMRegPair *regs, const VMRegPair *regs2 = nullptr);
 104 
 105   // During deoptimization only the result registers need to be restored
 106   // all the other values have already been extracted.
 107   static void restore_result_registers(MacroAssembler* masm, int frame_size_in_bytes);
 108 
 109   // Constants and data structures:
 110 
 111   typedef enum {
 112     int_reg,
 113     float_reg,
 114     special_reg,
 115     vs_reg
 116   } RegisterType;
 117 
 118   typedef enum {
 119     reg_size          = 8,
 120     half_reg_size     = reg_size / 2,
 121     vs_reg_size       = 16
 122   } RegisterConstants;
 123 
 124   typedef struct {
 125     RegisterType        reg_type;
 126     int                 reg_num;
 127     VMReg               vmreg;
 128   } LiveRegType;
 129 };
 130 
 131 
 132 #define RegisterSaver_LiveIntReg(regname) \
 133   { RegisterSaver::int_reg,     regname->encoding(), regname->as_VMReg() }
 134 
 135 #define RegisterSaver_LiveFloatReg(regname) \
 136   { RegisterSaver::float_reg,   regname->encoding(), regname->as_VMReg() }
 137 
 138 #define RegisterSaver_LiveSpecialReg(regname) \
 139   { RegisterSaver::special_reg, regname->encoding(), regname->as_VMReg() }
 140 
 141 #define RegisterSaver_LiveVSReg(regname) \
 142   { RegisterSaver::vs_reg,      regname->encoding(), regname->as_VMReg() }
 143 
 144 static const RegisterSaver::LiveRegType RegisterSaver_LiveRegs[] = {
 145   // Live registers which get spilled to the stack. Register
 146   // positions in this array correspond directly to the stack layout.
 147 
 148   //
 149   // live special registers:
 150   //
 151   RegisterSaver_LiveSpecialReg(SR_CTR),
 152   //
 153   // live float registers:
 154   //
 155   RegisterSaver_LiveFloatReg( F0  ),
 156   RegisterSaver_LiveFloatReg( F1  ),
 157   RegisterSaver_LiveFloatReg( F2  ),
 158   RegisterSaver_LiveFloatReg( F3  ),
 159   RegisterSaver_LiveFloatReg( F4  ),
 160   RegisterSaver_LiveFloatReg( F5  ),
 161   RegisterSaver_LiveFloatReg( F6  ),
 162   RegisterSaver_LiveFloatReg( F7  ),
 163   RegisterSaver_LiveFloatReg( F8  ),
 164   RegisterSaver_LiveFloatReg( F9  ),
 165   RegisterSaver_LiveFloatReg( F10 ),
 166   RegisterSaver_LiveFloatReg( F11 ),
 167   RegisterSaver_LiveFloatReg( F12 ),
 168   RegisterSaver_LiveFloatReg( F13 ),
 169   RegisterSaver_LiveFloatReg( F14 ),
 170   RegisterSaver_LiveFloatReg( F15 ),
 171   RegisterSaver_LiveFloatReg( F16 ),
 172   RegisterSaver_LiveFloatReg( F17 ),
 173   RegisterSaver_LiveFloatReg( F18 ),
 174   RegisterSaver_LiveFloatReg( F19 ),
 175   RegisterSaver_LiveFloatReg( F20 ),
 176   RegisterSaver_LiveFloatReg( F21 ),
 177   RegisterSaver_LiveFloatReg( F22 ),
 178   RegisterSaver_LiveFloatReg( F23 ),
 179   RegisterSaver_LiveFloatReg( F24 ),
 180   RegisterSaver_LiveFloatReg( F25 ),
 181   RegisterSaver_LiveFloatReg( F26 ),
 182   RegisterSaver_LiveFloatReg( F27 ),
 183   RegisterSaver_LiveFloatReg( F28 ),
 184   RegisterSaver_LiveFloatReg( F29 ),
 185   RegisterSaver_LiveFloatReg( F30 ),
 186   RegisterSaver_LiveFloatReg( F31 ),
 187   //
 188   // live integer registers:
 189   //
 190   RegisterSaver_LiveIntReg(   R0  ),
 191   //RegisterSaver_LiveIntReg( R1  ), // stack pointer
 192   RegisterSaver_LiveIntReg(   R2  ),
 193   RegisterSaver_LiveIntReg(   R3  ),
 194   RegisterSaver_LiveIntReg(   R4  ),
 195   RegisterSaver_LiveIntReg(   R5  ),
 196   RegisterSaver_LiveIntReg(   R6  ),
 197   RegisterSaver_LiveIntReg(   R7  ),
 198   RegisterSaver_LiveIntReg(   R8  ),
 199   RegisterSaver_LiveIntReg(   R9  ),
 200   RegisterSaver_LiveIntReg(   R10 ),
 201   RegisterSaver_LiveIntReg(   R11 ),
 202   RegisterSaver_LiveIntReg(   R12 ),
 203   //RegisterSaver_LiveIntReg( R13 ), // system thread id
 204   RegisterSaver_LiveIntReg(   R14 ),
 205   RegisterSaver_LiveIntReg(   R15 ),
 206   RegisterSaver_LiveIntReg(   R16 ),
 207   RegisterSaver_LiveIntReg(   R17 ),
 208   RegisterSaver_LiveIntReg(   R18 ),
 209   RegisterSaver_LiveIntReg(   R19 ),
 210   RegisterSaver_LiveIntReg(   R20 ),
 211   RegisterSaver_LiveIntReg(   R21 ),
 212   RegisterSaver_LiveIntReg(   R22 ),
 213   RegisterSaver_LiveIntReg(   R23 ),
 214   RegisterSaver_LiveIntReg(   R24 ),
 215   RegisterSaver_LiveIntReg(   R25 ),
 216   RegisterSaver_LiveIntReg(   R26 ),
 217   RegisterSaver_LiveIntReg(   R27 ),
 218   RegisterSaver_LiveIntReg(   R28 ),
 219   RegisterSaver_LiveIntReg(   R29 ),
 220   RegisterSaver_LiveIntReg(   R30 ),
 221   RegisterSaver_LiveIntReg(   R31 )  // must be the last register (see save/restore functions below)
 222 };
 223 
 224 static const RegisterSaver::LiveRegType RegisterSaver_LiveVSRegs[] = {
 225   //
 226   // live vector scalar registers (optional, only these ones are used by C2):
 227   //
 228   RegisterSaver_LiveVSReg( VSR32 ),
 229   RegisterSaver_LiveVSReg( VSR33 ),
 230   RegisterSaver_LiveVSReg( VSR34 ),
 231   RegisterSaver_LiveVSReg( VSR35 ),
 232   RegisterSaver_LiveVSReg( VSR36 ),
 233   RegisterSaver_LiveVSReg( VSR37 ),
 234   RegisterSaver_LiveVSReg( VSR38 ),
 235   RegisterSaver_LiveVSReg( VSR39 ),
 236   RegisterSaver_LiveVSReg( VSR40 ),
 237   RegisterSaver_LiveVSReg( VSR41 ),
 238   RegisterSaver_LiveVSReg( VSR42 ),
 239   RegisterSaver_LiveVSReg( VSR43 ),
 240   RegisterSaver_LiveVSReg( VSR44 ),
 241   RegisterSaver_LiveVSReg( VSR45 ),
 242   RegisterSaver_LiveVSReg( VSR46 ),
 243   RegisterSaver_LiveVSReg( VSR47 ),
 244   RegisterSaver_LiveVSReg( VSR48 ),
 245   RegisterSaver_LiveVSReg( VSR49 ),
 246   RegisterSaver_LiveVSReg( VSR50 ),
 247   RegisterSaver_LiveVSReg( VSR51 )
 248 };
 249 
 250 
 251 OopMap* RegisterSaver::push_frame_reg_args_and_save_live_registers(MacroAssembler* masm,
 252                          int* out_frame_size_in_bytes,
 253                          bool generate_oop_map,
 254                          int return_pc_adjustment,
 255                          ReturnPCLocation return_pc_location,
 256                          bool save_vectors) {
 257   // Push an abi_reg_args-frame and store all registers which may be live.
 258   // If requested, create an OopMap: Record volatile registers as
 259   // callee-save values in an OopMap so their save locations will be
 260   // propagated to the RegisterMap of the caller frame during
 261   // StackFrameStream construction (needed for deoptimization; see
 262   // compiledVFrame::create_stack_value).
 263   // If return_pc_adjustment != 0 adjust the return pc by return_pc_adjustment.
 264   // Updated return pc is returned in R31 (if not return_pc_is_pre_saved).
 265 
 266   // calculate frame size
 267   const int regstosave_num       = sizeof(RegisterSaver_LiveRegs) /
 268                                    sizeof(RegisterSaver::LiveRegType);
 269   const int vsregstosave_num     = save_vectors ? (sizeof(RegisterSaver_LiveVSRegs) /
 270                                                    sizeof(RegisterSaver::LiveRegType))
 271                                                 : 0;
 272   const int register_save_size   = regstosave_num * reg_size + vsregstosave_num * vs_reg_size;
 273   const int frame_size_in_bytes  = align_up(register_save_size, frame::alignment_in_bytes)
 274                                    + frame::native_abi_reg_args_size;
 275 
 276   *out_frame_size_in_bytes       = frame_size_in_bytes;
 277   const int frame_size_in_slots  = frame_size_in_bytes / sizeof(jint);
 278   const int register_save_offset = frame_size_in_bytes - register_save_size;
 279 
 280   // OopMap frame size is in c2 stack slots (sizeof(jint)) not bytes or words.
 281   OopMap* map = generate_oop_map ? new OopMap(frame_size_in_slots, 0) : nullptr;
 282 
 283   BLOCK_COMMENT("push_frame_reg_args_and_save_live_registers {");
 284 
 285   // push a new frame
 286   __ push_frame(frame_size_in_bytes, noreg);
 287 
 288   // Save some registers in the last (non-vector) slots of the new frame so we
 289   // can use them as scratch regs or to determine the return pc.
 290   __ std(R31, frame_size_in_bytes -   reg_size - vsregstosave_num * vs_reg_size, R1_SP);
 291   __ std(R30, frame_size_in_bytes - 2*reg_size - vsregstosave_num * vs_reg_size, R1_SP);
 292 
 293   // save the flags
 294   // Do the save_LR by hand and adjust the return pc if requested.
 295   switch (return_pc_location) {
 296     case return_pc_is_lr: __ mflr(R31); break;
 297     case return_pc_is_pre_saved: assert(return_pc_adjustment == 0, "unsupported"); break;
 298     case return_pc_is_thread_saved_exception_pc: __ ld(R31, thread_(saved_exception_pc)); break;
 299     default: ShouldNotReachHere();
 300   }
 301   if (return_pc_location != return_pc_is_pre_saved) {
 302     if (return_pc_adjustment != 0) {
 303       __ addi(R31, R31, return_pc_adjustment);
 304     }
 305     __ std(R31, frame_size_in_bytes + _abi0(lr), R1_SP);
 306   }
 307 
 308   // save all registers (ints and floats)
 309   int offset = register_save_offset;
 310 
 311   for (int i = 0; i < regstosave_num; i++) {
 312     int reg_num  = RegisterSaver_LiveRegs[i].reg_num;
 313     int reg_type = RegisterSaver_LiveRegs[i].reg_type;
 314 
 315     switch (reg_type) {
 316       case RegisterSaver::int_reg: {
 317         if (reg_num < 30) { // We spilled R30-31 right at the beginning.
 318           __ std(as_Register(reg_num), offset, R1_SP);
 319         }
 320         break;
 321       }
 322       case RegisterSaver::float_reg: {
 323         __ stfd(as_FloatRegister(reg_num), offset, R1_SP);
 324         break;
 325       }
 326       case RegisterSaver::special_reg: {
 327         if (reg_num == SR_CTR.encoding()) {
 328           __ mfctr(R30);
 329           __ std(R30, offset, R1_SP);
 330         } else {
 331           Unimplemented();
 332         }
 333         break;
 334       }
 335       default:
 336         ShouldNotReachHere();
 337     }
 338 
 339     if (generate_oop_map) {
 340       map->set_callee_saved(VMRegImpl::stack2reg(offset>>2),
 341                             RegisterSaver_LiveRegs[i].vmreg);
 342       map->set_callee_saved(VMRegImpl::stack2reg((offset + half_reg_size)>>2),
 343                             RegisterSaver_LiveRegs[i].vmreg->next());
 344     }
 345     offset += reg_size;
 346   }
 347 
 348   for (int i = 0; i < vsregstosave_num; i++) {
 349     int reg_num  = RegisterSaver_LiveVSRegs[i].reg_num;
 350     int reg_type = RegisterSaver_LiveVSRegs[i].reg_type;
 351 
 352     __ li(R30, offset);
 353     __ stxvd2x(as_VectorSRegister(reg_num), R30, R1_SP);
 354 
 355     if (generate_oop_map) {
 356       map->set_callee_saved(VMRegImpl::stack2reg(offset>>2),
 357                             RegisterSaver_LiveVSRegs[i].vmreg);
 358     }
 359     offset += vs_reg_size;
 360   }
 361 
 362   assert(offset == frame_size_in_bytes, "consistency check");
 363 
 364   BLOCK_COMMENT("} push_frame_reg_args_and_save_live_registers");
 365 
 366   // And we're done.
 367   return map;
 368 }
 369 
 370 
 371 // Pop the current frame and restore all the registers that we
 372 // saved.
 373 void RegisterSaver::restore_live_registers_and_pop_frame(MacroAssembler* masm,
 374                                                          int frame_size_in_bytes,
 375                                                          bool restore_ctr,
 376                                                          bool save_vectors) {
 377   const int regstosave_num       = sizeof(RegisterSaver_LiveRegs) /
 378                                    sizeof(RegisterSaver::LiveRegType);
 379   const int vsregstosave_num     = save_vectors ? (sizeof(RegisterSaver_LiveVSRegs) /
 380                                                    sizeof(RegisterSaver::LiveRegType))
 381                                                 : 0;
 382   const int register_save_size   = regstosave_num * reg_size + vsregstosave_num * vs_reg_size;
 383 
 384   const int register_save_offset = frame_size_in_bytes - register_save_size;
 385 
 386   BLOCK_COMMENT("restore_live_registers_and_pop_frame {");
 387 
 388   // restore all registers (ints and floats)
 389   int offset = register_save_offset;
 390 
 391   for (int i = 0; i < regstosave_num; i++) {
 392     int reg_num  = RegisterSaver_LiveRegs[i].reg_num;
 393     int reg_type = RegisterSaver_LiveRegs[i].reg_type;
 394 
 395     switch (reg_type) {
 396       case RegisterSaver::int_reg: {
 397         if (reg_num != 31) // R31 restored at the end, it's the tmp reg!
 398           __ ld(as_Register(reg_num), offset, R1_SP);
 399         break;
 400       }
 401       case RegisterSaver::float_reg: {
 402         __ lfd(as_FloatRegister(reg_num), offset, R1_SP);
 403         break;
 404       }
 405       case RegisterSaver::special_reg: {
 406         if (reg_num == SR_CTR.encoding()) {
 407           if (restore_ctr) { // Nothing to do here if ctr already contains the next address.
 408             __ ld(R31, offset, R1_SP);
 409             __ mtctr(R31);
 410           }
 411         } else {
 412           Unimplemented();
 413         }
 414         break;
 415       }
 416       default:
 417         ShouldNotReachHere();
 418     }
 419     offset += reg_size;
 420   }
 421 
 422   for (int i = 0; i < vsregstosave_num; i++) {
 423     int reg_num  = RegisterSaver_LiveVSRegs[i].reg_num;
 424     int reg_type = RegisterSaver_LiveVSRegs[i].reg_type;
 425 
 426     __ li(R31, offset);
 427     __ lxvd2x(as_VectorSRegister(reg_num), R31, R1_SP);
 428 
 429     offset += vs_reg_size;
 430   }
 431 
 432   assert(offset == frame_size_in_bytes, "consistency check");
 433 
 434   // restore link and the flags
 435   __ ld(R31, frame_size_in_bytes + _abi0(lr), R1_SP);
 436   __ mtlr(R31);
 437 
 438   // restore scratch register's value
 439   __ ld(R31, frame_size_in_bytes - reg_size - vsregstosave_num * vs_reg_size, R1_SP);
 440 
 441   // pop the frame
 442   __ addi(R1_SP, R1_SP, frame_size_in_bytes);
 443 
 444   BLOCK_COMMENT("} restore_live_registers_and_pop_frame");
 445 }
 446 
 447 void RegisterSaver::push_frame_and_save_argument_registers(MacroAssembler* masm, Register r_temp,
 448                                                            int frame_size,int total_args, const VMRegPair *regs,
 449                                                            const VMRegPair *regs2) {
 450   __ push_frame(frame_size, r_temp);
 451   int st_off = frame_size - wordSize;
 452   for (int i = 0; i < total_args; i++) {
 453     VMReg r_1 = regs[i].first();
 454     VMReg r_2 = regs[i].second();
 455     if (!r_1->is_valid()) {
 456       assert(!r_2->is_valid(), "");
 457       continue;
 458     }
 459     if (r_1->is_Register()) {
 460       Register r = r_1->as_Register();
 461       __ std(r, st_off, R1_SP);
 462       st_off -= wordSize;
 463     } else if (r_1->is_FloatRegister()) {
 464       FloatRegister f = r_1->as_FloatRegister();
 465       __ stfd(f, st_off, R1_SP);
 466       st_off -= wordSize;
 467     }
 468   }
 469   if (regs2 != nullptr) {
 470     for (int i = 0; i < total_args; i++) {
 471       VMReg r_1 = regs2[i].first();
 472       VMReg r_2 = regs2[i].second();
 473       if (!r_1->is_valid()) {
 474         assert(!r_2->is_valid(), "");
 475         continue;
 476       }
 477       if (r_1->is_Register()) {
 478         Register r = r_1->as_Register();
 479         __ std(r, st_off, R1_SP);
 480         st_off -= wordSize;
 481       } else if (r_1->is_FloatRegister()) {
 482         FloatRegister f = r_1->as_FloatRegister();
 483         __ stfd(f, st_off, R1_SP);
 484         st_off -= wordSize;
 485       }
 486     }
 487   }
 488 }
 489 
 490 void RegisterSaver::restore_argument_registers_and_pop_frame(MacroAssembler*masm, int frame_size,
 491                                                              int total_args, const VMRegPair *regs,
 492                                                              const VMRegPair *regs2) {
 493   int st_off = frame_size - wordSize;
 494   for (int i = 0; i < total_args; i++) {
 495     VMReg r_1 = regs[i].first();
 496     VMReg r_2 = regs[i].second();
 497     if (r_1->is_Register()) {
 498       Register r = r_1->as_Register();
 499       __ ld(r, st_off, R1_SP);
 500       st_off -= wordSize;
 501     } else if (r_1->is_FloatRegister()) {
 502       FloatRegister f = r_1->as_FloatRegister();
 503       __ lfd(f, st_off, R1_SP);
 504       st_off -= wordSize;
 505     }
 506   }
 507   if (regs2 != nullptr)
 508     for (int i = 0; i < total_args; i++) {
 509       VMReg r_1 = regs2[i].first();
 510       VMReg r_2 = regs2[i].second();
 511       if (r_1->is_Register()) {
 512         Register r = r_1->as_Register();
 513         __ ld(r, st_off, R1_SP);
 514         st_off -= wordSize;
 515       } else if (r_1->is_FloatRegister()) {
 516         FloatRegister f = r_1->as_FloatRegister();
 517         __ lfd(f, st_off, R1_SP);
 518         st_off -= wordSize;
 519       }
 520     }
 521   __ pop_frame();
 522 }
 523 
 524 // Restore the registers that might be holding a result.
 525 void RegisterSaver::restore_result_registers(MacroAssembler* masm, int frame_size_in_bytes) {
 526   const int regstosave_num       = sizeof(RegisterSaver_LiveRegs) /
 527                                    sizeof(RegisterSaver::LiveRegType);
 528   const int register_save_size   = regstosave_num * reg_size; // VS registers not relevant here.
 529   const int register_save_offset = frame_size_in_bytes - register_save_size;
 530 
 531   // restore all result registers (ints and floats)
 532   int offset = register_save_offset;
 533   for (int i = 0; i < regstosave_num; i++) {
 534     int reg_num  = RegisterSaver_LiveRegs[i].reg_num;
 535     int reg_type = RegisterSaver_LiveRegs[i].reg_type;
 536     switch (reg_type) {
 537       case RegisterSaver::int_reg: {
 538         if (as_Register(reg_num)==R3_RET) // int result_reg
 539           __ ld(as_Register(reg_num), offset, R1_SP);
 540         break;
 541       }
 542       case RegisterSaver::float_reg: {
 543         if (as_FloatRegister(reg_num)==F1_RET) // float result_reg
 544           __ lfd(as_FloatRegister(reg_num), offset, R1_SP);
 545         break;
 546       }
 547       case RegisterSaver::special_reg: {
 548         // Special registers don't hold a result.
 549         break;
 550       }
 551       default:
 552         ShouldNotReachHere();
 553     }
 554     offset += reg_size;
 555   }
 556 
 557   assert(offset == frame_size_in_bytes, "consistency check");
 558 }
 559 
 560 // Is vector's size (in bytes) bigger than a size saved by default?
 561 bool SharedRuntime::is_wide_vector(int size) {
 562   // Note, MaxVectorSize == 8/16 on PPC64.
 563   assert(size <= (SuperwordUseVSX ? 16 : 8), "%d bytes vectors are not supported", size);
 564   return size > 8;
 565 }
 566 
 567 static int reg2slot(VMReg r) {
 568   return r->reg2stack() + SharedRuntime::out_preserve_stack_slots();
 569 }
 570 
 571 static int reg2offset(VMReg r) {
 572   return (r->reg2stack() + SharedRuntime::out_preserve_stack_slots()) * VMRegImpl::stack_slot_size;
 573 }
 574 
 575 // ---------------------------------------------------------------------------
 576 // Read the array of BasicTypes from a signature, and compute where the
 577 // arguments should go. Values in the VMRegPair regs array refer to 4-byte
 578 // quantities. Values less than VMRegImpl::stack0 are registers, those above
 579 // refer to 4-byte stack slots. All stack slots are based off of the stack pointer
 580 // as framesizes are fixed.
 581 // VMRegImpl::stack0 refers to the first slot 0(sp).
 582 // and VMRegImpl::stack0+1 refers to the memory word 4-bytes higher. Register
 583 // up to Register::number_of_registers) are the 64-bit
 584 // integer registers.
 585 
 586 // Note: the INPUTS in sig_bt are in units of Java argument words, which are
 587 // either 32-bit or 64-bit depending on the build. The OUTPUTS are in 32-bit
 588 // units regardless of build. Of course for i486 there is no 64 bit build
 589 
 590 // The Java calling convention is a "shifted" version of the C ABI.
 591 // By skipping the first C ABI register we can call non-static jni methods
 592 // with small numbers of arguments without having to shuffle the arguments
 593 // at all. Since we control the java ABI we ought to at least get some
 594 // advantage out of it.
 595 
 596 const VMReg java_iarg_reg[8] = {
 597   R3->as_VMReg(),
 598   R4->as_VMReg(),
 599   R5->as_VMReg(),
 600   R6->as_VMReg(),
 601   R7->as_VMReg(),
 602   R8->as_VMReg(),
 603   R9->as_VMReg(),
 604   R10->as_VMReg()
 605 };
 606 
 607 const VMReg java_farg_reg[13] = {
 608   F1->as_VMReg(),
 609   F2->as_VMReg(),
 610   F3->as_VMReg(),
 611   F4->as_VMReg(),
 612   F5->as_VMReg(),
 613   F6->as_VMReg(),
 614   F7->as_VMReg(),
 615   F8->as_VMReg(),
 616   F9->as_VMReg(),
 617   F10->as_VMReg(),
 618   F11->as_VMReg(),
 619   F12->as_VMReg(),
 620   F13->as_VMReg()
 621 };
 622 
 623 const int num_java_iarg_registers = sizeof(java_iarg_reg) / sizeof(java_iarg_reg[0]);
 624 const int num_java_farg_registers = sizeof(java_farg_reg) / sizeof(java_farg_reg[0]);
 625 
 626 STATIC_ASSERT(num_java_iarg_registers == Argument::n_int_register_parameters_j);
 627 STATIC_ASSERT(num_java_farg_registers == Argument::n_float_register_parameters_j);
 628 
 629 int SharedRuntime::java_calling_convention(const BasicType *sig_bt,
 630                                            VMRegPair *regs,
 631                                            int total_args_passed) {
 632   // C2c calling conventions for compiled-compiled calls.
 633   // Put 8 ints/longs into registers _AND_ 13 float/doubles into
 634   // registers _AND_ put the rest on the stack.
 635 
 636   const int inc_stk_for_intfloat   = 1; // 1 slots for ints and floats
 637   const int inc_stk_for_longdouble = 2; // 2 slots for longs and doubles
 638 
 639   int i;
 640   VMReg reg;
 641   int stk = 0;
 642   int ireg = 0;
 643   int freg = 0;
 644 
 645   // We put the first 8 arguments into registers and the rest on the
 646   // stack, float arguments are already in their argument registers
 647   // due to c2c calling conventions (see calling_convention).
 648   for (int i = 0; i < total_args_passed; ++i) {
 649     switch(sig_bt[i]) {
 650     case T_BOOLEAN:
 651     case T_CHAR:
 652     case T_BYTE:
 653     case T_SHORT:
 654     case T_INT:
 655       if (ireg < num_java_iarg_registers) {
 656         // Put int/ptr in register
 657         reg = java_iarg_reg[ireg];
 658         ++ireg;
 659       } else {
 660         // Put int/ptr on stack.
 661         reg = VMRegImpl::stack2reg(stk);
 662         stk += inc_stk_for_intfloat;
 663       }
 664       regs[i].set1(reg);
 665       break;
 666     case T_LONG:
 667       assert((i + 1) < total_args_passed && sig_bt[i+1] == T_VOID, "expecting half");
 668       if (ireg < num_java_iarg_registers) {
 669         // Put long in register.
 670         reg = java_iarg_reg[ireg];
 671         ++ireg;
 672       } else {
 673         // Put long on stack. They must be aligned to 2 slots.
 674         if (stk & 0x1) ++stk;
 675         reg = VMRegImpl::stack2reg(stk);
 676         stk += inc_stk_for_longdouble;
 677       }
 678       regs[i].set2(reg);
 679       break;
 680     case T_OBJECT:
 681     case T_ARRAY:
 682     case T_ADDRESS:
 683       if (ireg < num_java_iarg_registers) {
 684         // Put ptr in register.
 685         reg = java_iarg_reg[ireg];
 686         ++ireg;
 687       } else {
 688         // Put ptr on stack. Objects must be aligned to 2 slots too,
 689         // because "64-bit pointers record oop-ishness on 2 aligned
 690         // adjacent registers." (see OopFlow::build_oop_map).
 691         if (stk & 0x1) ++stk;
 692         reg = VMRegImpl::stack2reg(stk);
 693         stk += inc_stk_for_longdouble;
 694       }
 695       regs[i].set2(reg);
 696       break;
 697     case T_FLOAT:
 698       if (freg < num_java_farg_registers) {
 699         // Put float in register.
 700         reg = java_farg_reg[freg];
 701         ++freg;
 702       } else {
 703         // Put float on stack.
 704         reg = VMRegImpl::stack2reg(stk);
 705         stk += inc_stk_for_intfloat;
 706       }
 707       regs[i].set1(reg);
 708       break;
 709     case T_DOUBLE:
 710       assert((i + 1) < total_args_passed && sig_bt[i+1] == T_VOID, "expecting half");
 711       if (freg < num_java_farg_registers) {
 712         // Put double in register.
 713         reg = java_farg_reg[freg];
 714         ++freg;
 715       } else {
 716         // Put double on stack. They must be aligned to 2 slots.
 717         if (stk & 0x1) ++stk;
 718         reg = VMRegImpl::stack2reg(stk);
 719         stk += inc_stk_for_longdouble;
 720       }
 721       regs[i].set2(reg);
 722       break;
 723     case T_VOID:
 724       // Do not count halves.
 725       regs[i].set_bad();
 726       break;
 727     default:
 728       ShouldNotReachHere();
 729     }
 730   }
 731   return stk;
 732 }
 733 
 734 #if defined(COMPILER1) || defined(COMPILER2)
 735 // Calling convention for calling C code.
 736 int SharedRuntime::c_calling_convention(const BasicType *sig_bt,
 737                                         VMRegPair *regs,
 738                                         int total_args_passed) {
 739   // Calling conventions for C runtime calls and calls to JNI native methods.
 740   //
 741   // PPC64 convention: Hoist the first 8 int/ptr/long's in the first 8
 742   // int regs, leaving int regs undefined if the arg is flt/dbl. Hoist
 743   // the first 13 flt/dbl's in the first 13 fp regs but additionally
 744   // copy flt/dbl to the stack if they are beyond the 8th argument.
 745 
 746   const VMReg iarg_reg[8] = {
 747     R3->as_VMReg(),
 748     R4->as_VMReg(),
 749     R5->as_VMReg(),
 750     R6->as_VMReg(),
 751     R7->as_VMReg(),
 752     R8->as_VMReg(),
 753     R9->as_VMReg(),
 754     R10->as_VMReg()
 755   };
 756 
 757   const VMReg farg_reg[13] = {
 758     F1->as_VMReg(),
 759     F2->as_VMReg(),
 760     F3->as_VMReg(),
 761     F4->as_VMReg(),
 762     F5->as_VMReg(),
 763     F6->as_VMReg(),
 764     F7->as_VMReg(),
 765     F8->as_VMReg(),
 766     F9->as_VMReg(),
 767     F10->as_VMReg(),
 768     F11->as_VMReg(),
 769     F12->as_VMReg(),
 770     F13->as_VMReg()
 771   };
 772 
 773   // Check calling conventions consistency.
 774   assert(sizeof(iarg_reg) / sizeof(iarg_reg[0]) == Argument::n_int_register_parameters_c &&
 775          sizeof(farg_reg) / sizeof(farg_reg[0]) == Argument::n_float_register_parameters_c,
 776          "consistency");
 777 
 778   const int additional_frame_header_slots = ((frame::native_abi_minframe_size - frame::jit_out_preserve_size)
 779                                             / VMRegImpl::stack_slot_size);
 780   const int float_offset_in_slots = Argument::float_on_stack_offset_in_bytes_c / VMRegImpl::stack_slot_size;
 781 
 782   VMReg reg;
 783   int arg = 0;
 784   int freg = 0;
 785   bool stack_used = false;
 786 
 787   for (int i = 0; i < total_args_passed; ++i, ++arg) {
 788     // Each argument corresponds to a slot in the Parameter Save Area (if not omitted)
 789     int stk = (arg * 2) + additional_frame_header_slots;
 790 
 791     switch(sig_bt[i]) {
 792     //
 793     // If arguments 0-7 are integers, they are passed in integer registers.
 794     // Argument i is placed in iarg_reg[i].
 795     //
 796     case T_BOOLEAN:
 797     case T_CHAR:
 798     case T_BYTE:
 799     case T_SHORT:
 800     case T_INT:
 801       // We must cast ints to longs and use full 64 bit stack slots
 802       // here.  Thus fall through, handle as long.
 803     case T_LONG:
 804     case T_OBJECT:
 805     case T_ARRAY:
 806     case T_ADDRESS:
 807     case T_METADATA:
 808       // Oops are already boxed if required (JNI).
 809       if (arg < Argument::n_int_register_parameters_c) {
 810         reg = iarg_reg[arg];
 811       } else {
 812         reg = VMRegImpl::stack2reg(stk);
 813         stack_used = true;
 814       }
 815       regs[i].set2(reg);
 816       break;
 817 
 818     //
 819     // Floats are treated differently from int regs:  The first 13 float arguments
 820     // are passed in registers (not the float args among the first 13 args).
 821     // Thus argument i is NOT passed in farg_reg[i] if it is float.  It is passed
 822     // in farg_reg[j] if argument i is the j-th float argument of this call.
 823     //
 824     case T_FLOAT:
 825       if (freg < Argument::n_float_register_parameters_c) {
 826         // Put float in register ...
 827         reg = farg_reg[freg];
 828         ++freg;
 829       } else {
 830         // Put float on stack.
 831         reg = VMRegImpl::stack2reg(stk + float_offset_in_slots);
 832         stack_used = true;
 833       }
 834       regs[i].set1(reg);
 835       break;
 836     case T_DOUBLE:
 837       assert((i + 1) < total_args_passed && sig_bt[i+1] == T_VOID, "expecting half");
 838       if (freg < Argument::n_float_register_parameters_c) {
 839         // Put double in register ...
 840         reg = farg_reg[freg];
 841         ++freg;
 842       } else {
 843         // Put double on stack.
 844         reg = VMRegImpl::stack2reg(stk);
 845         stack_used = true;
 846       }
 847       regs[i].set2(reg);
 848       break;
 849 
 850     case T_VOID:
 851       // Do not count halves.
 852       regs[i].set_bad();
 853       --arg;
 854       break;
 855     default:
 856       ShouldNotReachHere();
 857     }
 858   }
 859 
 860   // Return size of the stack frame excluding the jit_out_preserve part in single-word slots.
 861 #if defined(ABI_ELFv2)
 862   assert(additional_frame_header_slots == 0, "ABIv2 shouldn't use extra slots");
 863   // ABIv2 allows omitting the Parameter Save Area if the callee's prototype
 864   // indicates that all parameters can be passed in registers.
 865   return stack_used ? (arg * 2) : 0;
 866 #else
 867   // The Parameter Save Area needs to be at least 8 double-word slots for ABIv1.
 868   // We have to add extra slots because ABIv1 uses a larger header.
 869   return MAX2(arg, 8) * 2 + additional_frame_header_slots;
 870 #endif
 871 }
 872 #endif // COMPILER2
 873 
 874 int SharedRuntime::vector_calling_convention(VMRegPair *regs,
 875                                              uint num_bits,
 876                                              uint total_args_passed) {
 877   Unimplemented();
 878   return 0;
 879 }
 880 
 881 static address gen_c2i_adapter(MacroAssembler *masm,
 882                             int total_args_passed,
 883                             int comp_args_on_stack,
 884                             const BasicType *sig_bt,
 885                             const VMRegPair *regs,
 886                             Label& call_interpreter,
 887                             const Register& ientry) {
 888 
 889   address c2i_entrypoint;
 890 
 891   const Register sender_SP = R21_sender_SP; // == R21_tmp1
 892   const Register code      = R22_tmp2;
 893   //const Register ientry  = R23_tmp3;
 894   const Register value_regs[] = { R24_tmp4, R25_tmp5, R26_tmp6 };
 895   const int num_value_regs = sizeof(value_regs) / sizeof(Register);
 896   int value_regs_index = 0;
 897 
 898   const Register return_pc = R27_tmp7;
 899   const Register tmp       = R28_tmp8;
 900 
 901   assert_different_registers(sender_SP, code, ientry, return_pc, tmp);
 902 
 903   // Adapter needs TOP_IJAVA_FRAME_ABI.
 904   const int adapter_size = frame::top_ijava_frame_abi_size +
 905                            align_up(total_args_passed * wordSize, frame::alignment_in_bytes);
 906 
 907   // regular (verified) c2i entry point
 908   c2i_entrypoint = __ pc();
 909 
 910   // Does compiled code exists? If yes, patch the caller's callsite.
 911   __ ld(code, method_(code));
 912   __ cmpdi(CCR0, code, 0);
 913   __ ld(ientry, method_(interpreter_entry)); // preloaded
 914   __ beq(CCR0, call_interpreter);
 915 
 916 
 917   // Patch caller's callsite, method_(code) was not null which means that
 918   // compiled code exists.
 919   __ mflr(return_pc);
 920   __ std(return_pc, _abi0(lr), R1_SP);
 921   RegisterSaver::push_frame_and_save_argument_registers(masm, tmp, adapter_size, total_args_passed, regs);
 922 
 923   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::fixup_callers_callsite), R19_method, return_pc);
 924 
 925   RegisterSaver::restore_argument_registers_and_pop_frame(masm, adapter_size, total_args_passed, regs);
 926   __ ld(return_pc, _abi0(lr), R1_SP);
 927   __ ld(ientry, method_(interpreter_entry)); // preloaded
 928   __ mtlr(return_pc);
 929 
 930 
 931   // Call the interpreter.
 932   __ BIND(call_interpreter);
 933   __ mtctr(ientry);
 934 
 935   // Get a copy of the current SP for loading caller's arguments.
 936   __ mr(sender_SP, R1_SP);
 937 
 938   // Add space for the adapter.
 939   __ resize_frame(-adapter_size, R12_scratch2);
 940 
 941   int st_off = adapter_size - wordSize;
 942 
 943   // Write the args into the outgoing interpreter space.
 944   for (int i = 0; i < total_args_passed; i++) {
 945     VMReg r_1 = regs[i].first();
 946     VMReg r_2 = regs[i].second();
 947     if (!r_1->is_valid()) {
 948       assert(!r_2->is_valid(), "");
 949       continue;
 950     }
 951     if (r_1->is_stack()) {
 952       Register tmp_reg = value_regs[value_regs_index];
 953       value_regs_index = (value_regs_index + 1) % num_value_regs;
 954       // The calling convention produces OptoRegs that ignore the out
 955       // preserve area (JIT's ABI). We must account for it here.
 956       int ld_off = (r_1->reg2stack() + SharedRuntime::out_preserve_stack_slots()) * VMRegImpl::stack_slot_size;
 957       if (!r_2->is_valid()) {
 958         __ lwz(tmp_reg, ld_off, sender_SP);
 959       } else {
 960         __ ld(tmp_reg, ld_off, sender_SP);
 961       }
 962       // Pretend stack targets were loaded into tmp_reg.
 963       r_1 = tmp_reg->as_VMReg();
 964     }
 965 
 966     if (r_1->is_Register()) {
 967       Register r = r_1->as_Register();
 968       if (!r_2->is_valid()) {
 969         __ stw(r, st_off, R1_SP);
 970         st_off-=wordSize;
 971       } else {
 972         // Longs are given 2 64-bit slots in the interpreter, but the
 973         // data is passed in only 1 slot.
 974         if (sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) {
 975           DEBUG_ONLY( __ li(tmp, 0); __ std(tmp, st_off, R1_SP); )
 976           st_off-=wordSize;
 977         }
 978         __ std(r, st_off, R1_SP);
 979         st_off-=wordSize;
 980       }
 981     } else {
 982       assert(r_1->is_FloatRegister(), "");
 983       FloatRegister f = r_1->as_FloatRegister();
 984       if (!r_2->is_valid()) {
 985         __ stfs(f, st_off, R1_SP);
 986         st_off-=wordSize;
 987       } else {
 988         // In 64bit, doubles are given 2 64-bit slots in the interpreter, but the
 989         // data is passed in only 1 slot.
 990         // One of these should get known junk...
 991         DEBUG_ONLY( __ li(tmp, 0); __ std(tmp, st_off, R1_SP); )
 992         st_off-=wordSize;
 993         __ stfd(f, st_off, R1_SP);
 994         st_off-=wordSize;
 995       }
 996     }
 997   }
 998 
 999   // Jump to the interpreter just as if interpreter was doing it.
1000 
1001   __ load_const_optimized(R25_templateTableBase, (address)Interpreter::dispatch_table((TosState)0), R11_scratch1);
1002 
1003   // load TOS
1004   __ addi(R15_esp, R1_SP, st_off);
1005 
1006   // Frame_manager expects initial_caller_sp (= SP without resize by c2i) in R21_tmp1.
1007   assert(sender_SP == R21_sender_SP, "passing initial caller's SP in wrong register");
1008   __ bctr();
1009 
1010   return c2i_entrypoint;
1011 }
1012 
1013 void SharedRuntime::gen_i2c_adapter(MacroAssembler *masm,
1014                                     int total_args_passed,
1015                                     int comp_args_on_stack,
1016                                     const BasicType *sig_bt,
1017                                     const VMRegPair *regs) {
1018 
1019   // Load method's entry-point from method.
1020   __ ld(R12_scratch2, in_bytes(Method::from_compiled_offset()), R19_method);
1021   __ mtctr(R12_scratch2);
1022 
1023   // We will only enter here from an interpreted frame and never from after
1024   // passing thru a c2i. Azul allowed this but we do not. If we lose the
1025   // race and use a c2i we will remain interpreted for the race loser(s).
1026   // This removes all sorts of headaches on the x86 side and also eliminates
1027   // the possibility of having c2i -> i2c -> c2i -> ... endless transitions.
1028 
1029   // Note: r13 contains the senderSP on entry. We must preserve it since
1030   // we may do a i2c -> c2i transition if we lose a race where compiled
1031   // code goes non-entrant while we get args ready.
1032   // In addition we use r13 to locate all the interpreter args as
1033   // we must align the stack to 16 bytes on an i2c entry else we
1034   // lose alignment we expect in all compiled code and register
1035   // save code can segv when fxsave instructions find improperly
1036   // aligned stack pointer.
1037 
1038   const Register ld_ptr = R15_esp;
1039   const Register value_regs[] = { R22_tmp2, R23_tmp3, R24_tmp4, R25_tmp5, R26_tmp6 };
1040   const int num_value_regs = sizeof(value_regs) / sizeof(Register);
1041   int value_regs_index = 0;
1042 
1043   int ld_offset = total_args_passed*wordSize;
1044 
1045   // Cut-out for having no stack args. Since up to 2 int/oop args are passed
1046   // in registers, we will occasionally have no stack args.
1047   int comp_words_on_stack = 0;
1048   if (comp_args_on_stack) {
1049     // Sig words on the stack are greater-than VMRegImpl::stack0. Those in
1050     // registers are below. By subtracting stack0, we either get a negative
1051     // number (all values in registers) or the maximum stack slot accessed.
1052 
1053     // Convert 4-byte c2 stack slots to words.
1054     comp_words_on_stack = align_up(comp_args_on_stack*VMRegImpl::stack_slot_size, wordSize)>>LogBytesPerWord;
1055     // Round up to miminum stack alignment, in wordSize.
1056     comp_words_on_stack = align_up(comp_words_on_stack, 2);
1057     __ resize_frame(-comp_words_on_stack * wordSize, R11_scratch1);
1058   }
1059 
1060   // Now generate the shuffle code.  Pick up all register args and move the
1061   // rest through register value=Z_R12.
1062   BLOCK_COMMENT("Shuffle arguments");
1063   for (int i = 0; i < total_args_passed; i++) {
1064     if (sig_bt[i] == T_VOID) {
1065       assert(i > 0 && (sig_bt[i-1] == T_LONG || sig_bt[i-1] == T_DOUBLE), "missing half");
1066       continue;
1067     }
1068 
1069     // Pick up 0, 1 or 2 words from ld_ptr.
1070     assert(!regs[i].second()->is_valid() || regs[i].first()->next() == regs[i].second(),
1071             "scrambled load targets?");
1072     VMReg r_1 = regs[i].first();
1073     VMReg r_2 = regs[i].second();
1074     if (!r_1->is_valid()) {
1075       assert(!r_2->is_valid(), "");
1076       continue;
1077     }
1078     if (r_1->is_FloatRegister()) {
1079       if (!r_2->is_valid()) {
1080         __ lfs(r_1->as_FloatRegister(), ld_offset, ld_ptr);
1081         ld_offset-=wordSize;
1082       } else {
1083         // Skip the unused interpreter slot.
1084         __ lfd(r_1->as_FloatRegister(), ld_offset-wordSize, ld_ptr);
1085         ld_offset-=2*wordSize;
1086       }
1087     } else {
1088       Register r;
1089       if (r_1->is_stack()) {
1090         // Must do a memory to memory move thru "value".
1091         r = value_regs[value_regs_index];
1092         value_regs_index = (value_regs_index + 1) % num_value_regs;
1093       } else {
1094         r = r_1->as_Register();
1095       }
1096       if (!r_2->is_valid()) {
1097         // Not sure we need to do this but it shouldn't hurt.
1098         if (is_reference_type(sig_bt[i]) || sig_bt[i] == T_ADDRESS) {
1099           __ ld(r, ld_offset, ld_ptr);
1100           ld_offset-=wordSize;
1101         } else {
1102           __ lwz(r, ld_offset, ld_ptr);
1103           ld_offset-=wordSize;
1104         }
1105       } else {
1106         // In 64bit, longs are given 2 64-bit slots in the interpreter, but the
1107         // data is passed in only 1 slot.
1108         if (sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) {
1109           ld_offset-=wordSize;
1110         }
1111         __ ld(r, ld_offset, ld_ptr);
1112         ld_offset-=wordSize;
1113       }
1114 
1115       if (r_1->is_stack()) {
1116         // Now store value where the compiler expects it
1117         int st_off = (r_1->reg2stack() + SharedRuntime::out_preserve_stack_slots())*VMRegImpl::stack_slot_size;
1118 
1119         if (sig_bt[i] == T_INT   || sig_bt[i] == T_FLOAT ||sig_bt[i] == T_BOOLEAN ||
1120             sig_bt[i] == T_SHORT || sig_bt[i] == T_CHAR  || sig_bt[i] == T_BYTE) {
1121           __ stw(r, st_off, R1_SP);
1122         } else {
1123           __ std(r, st_off, R1_SP);
1124         }
1125       }
1126     }
1127   }
1128 
1129   __ push_cont_fastpath(); // Set JavaThread::_cont_fastpath to the sp of the oldest interpreted frame we know about
1130 
1131   BLOCK_COMMENT("Store method");
1132   // Store method into thread->callee_target.
1133   // We might end up in handle_wrong_method if the callee is
1134   // deoptimized as we race thru here. If that happens we don't want
1135   // to take a safepoint because the caller frame will look
1136   // interpreted and arguments are now "compiled" so it is much better
1137   // to make this transition invisible to the stack walking
1138   // code. Unfortunately if we try and find the callee by normal means
1139   // a safepoint is possible. So we stash the desired callee in the
1140   // thread and the vm will find there should this case occur.
1141   __ std(R19_method, thread_(callee_target));
1142 
1143   // Jump to the compiled code just as if compiled code was doing it.
1144   __ bctr();
1145 }
1146 
1147 AdapterHandlerEntry* SharedRuntime::generate_i2c2i_adapters(MacroAssembler *masm,
1148                                                             int total_args_passed,
1149                                                             int comp_args_on_stack,
1150                                                             const BasicType *sig_bt,
1151                                                             const VMRegPair *regs,
1152                                                             AdapterFingerPrint* fingerprint) {
1153   address i2c_entry;
1154   address c2i_unverified_entry;
1155   address c2i_entry;
1156 
1157 
1158   // entry: i2c
1159 
1160   __ align(CodeEntryAlignment);
1161   i2c_entry = __ pc();
1162   gen_i2c_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs);
1163 
1164 
1165   // entry: c2i unverified
1166 
1167   __ align(CodeEntryAlignment);
1168   BLOCK_COMMENT("c2i unverified entry");
1169   c2i_unverified_entry = __ pc();
1170 
1171   // inline_cache contains a CompiledICData
1172   const Register ic             = R19_inline_cache_reg;
1173   const Register ic_klass       = R11_scratch1;
1174   const Register receiver_klass = R12_scratch2;
1175   const Register code           = R21_tmp1;
1176   const Register ientry         = R23_tmp3;
1177 
1178   assert_different_registers(ic, ic_klass, receiver_klass, R3_ARG1, code, ientry);
1179   assert(R11_scratch1 == R11, "need prologue scratch register");
1180 
1181   Label call_interpreter;
1182 
1183   __ ic_check(4 /* end_alignment */);
1184   __ ld(R19_method, CompiledICData::speculated_method_offset(), ic);
1185   // Argument is valid and klass is as expected, continue.
1186 
1187   __ ld(code, method_(code));
1188   __ cmpdi(CCR0, code, 0);
1189   __ ld(ientry, method_(interpreter_entry)); // preloaded
1190   __ beq_predict_taken(CCR0, call_interpreter);
1191 
1192   // Branch to ic_miss_stub.
1193   __ b64_patchable((address)SharedRuntime::get_ic_miss_stub(), relocInfo::runtime_call_type);
1194 
1195   // entry: c2i
1196 
1197   c2i_entry = __ pc();
1198 
1199   // Class initialization barrier for static methods
1200   address c2i_no_clinit_check_entry = nullptr;
1201   if (VM_Version::supports_fast_class_init_checks()) {
1202     Label L_skip_barrier;
1203 
1204     { // Bypass the barrier for non-static methods
1205       __ lwz(R0, in_bytes(Method::access_flags_offset()), R19_method);
1206       __ andi_(R0, R0, JVM_ACC_STATIC);
1207       __ beq(CCR0, L_skip_barrier); // non-static
1208     }
1209 
1210     Register klass = R11_scratch1;
1211     __ load_method_holder(klass, R19_method);
1212     __ clinit_barrier(klass, R16_thread, &L_skip_barrier /*L_fast_path*/);
1213 
1214     __ load_const_optimized(klass, SharedRuntime::get_handle_wrong_method_stub(), R0);
1215     __ mtctr(klass);
1216     __ bctr();
1217 
1218     __ bind(L_skip_barrier);
1219     c2i_no_clinit_check_entry = __ pc();
1220   }
1221 
1222   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
1223   bs->c2i_entry_barrier(masm, /* tmp register*/ ic_klass, /* tmp register*/ receiver_klass, /* tmp register*/ code);
1224 
1225   gen_c2i_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs, call_interpreter, ientry);
1226 
1227   return AdapterHandlerLibrary::new_entry(fingerprint, i2c_entry, c2i_entry, c2i_unverified_entry,
1228                                           c2i_no_clinit_check_entry);
1229 }
1230 
1231 // An oop arg. Must pass a handle not the oop itself.
1232 static void object_move(MacroAssembler* masm,
1233                         int frame_size_in_slots,
1234                         OopMap* oop_map, int oop_handle_offset,
1235                         bool is_receiver, int* receiver_offset,
1236                         VMRegPair src, VMRegPair dst,
1237                         Register r_caller_sp, Register r_temp_1, Register r_temp_2) {
1238   assert(!is_receiver || (is_receiver && (*receiver_offset == -1)),
1239          "receiver has already been moved");
1240 
1241   // We must pass a handle. First figure out the location we use as a handle.
1242 
1243   if (src.first()->is_stack()) {
1244     // stack to stack or reg
1245 
1246     const Register r_handle = dst.first()->is_stack() ? r_temp_1 : dst.first()->as_Register();
1247     Label skip;
1248     const int oop_slot_in_callers_frame = reg2slot(src.first());
1249 
1250     guarantee(!is_receiver, "expecting receiver in register");
1251     oop_map->set_oop(VMRegImpl::stack2reg(oop_slot_in_callers_frame + frame_size_in_slots));
1252 
1253     __ addi(r_handle, r_caller_sp, reg2offset(src.first()));
1254     __ ld(  r_temp_2, reg2offset(src.first()), r_caller_sp);
1255     __ cmpdi(CCR0, r_temp_2, 0);
1256     __ bne(CCR0, skip);
1257     // Use a null handle if oop is null.
1258     __ li(r_handle, 0);
1259     __ bind(skip);
1260 
1261     if (dst.first()->is_stack()) {
1262       // stack to stack
1263       __ std(r_handle, reg2offset(dst.first()), R1_SP);
1264     } else {
1265       // stack to reg
1266       // Nothing to do, r_handle is already the dst register.
1267     }
1268   } else {
1269     // reg to stack or reg
1270     const Register r_oop      = src.first()->as_Register();
1271     const Register r_handle   = dst.first()->is_stack() ? r_temp_1 : dst.first()->as_Register();
1272     const int oop_slot        = (r_oop->encoding()-R3_ARG1->encoding()) * VMRegImpl::slots_per_word
1273                                 + oop_handle_offset; // in slots
1274     const int oop_offset = oop_slot * VMRegImpl::stack_slot_size;
1275     Label skip;
1276 
1277     if (is_receiver) {
1278       *receiver_offset = oop_offset;
1279     }
1280     oop_map->set_oop(VMRegImpl::stack2reg(oop_slot));
1281 
1282     __ std( r_oop,    oop_offset, R1_SP);
1283     __ addi(r_handle, R1_SP, oop_offset);
1284 
1285     __ cmpdi(CCR0, r_oop, 0);
1286     __ bne(CCR0, skip);
1287     // Use a null handle if oop is null.
1288     __ li(r_handle, 0);
1289     __ bind(skip);
1290 
1291     if (dst.first()->is_stack()) {
1292       // reg to stack
1293       __ std(r_handle, reg2offset(dst.first()), R1_SP);
1294     } else {
1295       // reg to reg
1296       // Nothing to do, r_handle is already the dst register.
1297     }
1298   }
1299 }
1300 
1301 static void int_move(MacroAssembler*masm,
1302                      VMRegPair src, VMRegPair dst,
1303                      Register r_caller_sp, Register r_temp) {
1304   assert(src.first()->is_valid(), "incoming must be int");
1305   assert(dst.first()->is_valid() && dst.second() == dst.first()->next(), "outgoing must be long");
1306 
1307   if (src.first()->is_stack()) {
1308     if (dst.first()->is_stack()) {
1309       // stack to stack
1310       __ lwa(r_temp, reg2offset(src.first()), r_caller_sp);
1311       __ std(r_temp, reg2offset(dst.first()), R1_SP);
1312     } else {
1313       // stack to reg
1314       __ lwa(dst.first()->as_Register(), reg2offset(src.first()), r_caller_sp);
1315     }
1316   } else if (dst.first()->is_stack()) {
1317     // reg to stack
1318     __ extsw(r_temp, src.first()->as_Register());
1319     __ std(r_temp, reg2offset(dst.first()), R1_SP);
1320   } else {
1321     // reg to reg
1322     __ extsw(dst.first()->as_Register(), src.first()->as_Register());
1323   }
1324 }
1325 
1326 static void long_move(MacroAssembler*masm,
1327                       VMRegPair src, VMRegPair dst,
1328                       Register r_caller_sp, Register r_temp) {
1329   assert(src.first()->is_valid() && src.second() == src.first()->next(), "incoming must be long");
1330   assert(dst.first()->is_valid() && dst.second() == dst.first()->next(), "outgoing must be long");
1331 
1332   if (src.first()->is_stack()) {
1333     if (dst.first()->is_stack()) {
1334       // stack to stack
1335       __ ld( r_temp, reg2offset(src.first()), r_caller_sp);
1336       __ std(r_temp, reg2offset(dst.first()), R1_SP);
1337     } else {
1338       // stack to reg
1339       __ ld(dst.first()->as_Register(), reg2offset(src.first()), r_caller_sp);
1340     }
1341   } else if (dst.first()->is_stack()) {
1342     // reg to stack
1343     __ std(src.first()->as_Register(), reg2offset(dst.first()), R1_SP);
1344   } else {
1345     // reg to reg
1346     if (dst.first()->as_Register() != src.first()->as_Register())
1347       __ mr(dst.first()->as_Register(), src.first()->as_Register());
1348   }
1349 }
1350 
1351 static void float_move(MacroAssembler*masm,
1352                        VMRegPair src, VMRegPair dst,
1353                        Register r_caller_sp, Register r_temp) {
1354   assert(src.first()->is_valid() && !src.second()->is_valid(), "incoming must be float");
1355   assert(dst.first()->is_valid() && !dst.second()->is_valid(), "outgoing must be float");
1356 
1357   if (src.first()->is_stack()) {
1358     if (dst.first()->is_stack()) {
1359       // stack to stack
1360       __ lwz(r_temp, reg2offset(src.first()), r_caller_sp);
1361       __ stw(r_temp, reg2offset(dst.first()), R1_SP);
1362     } else {
1363       // stack to reg
1364       __ lfs(dst.first()->as_FloatRegister(), reg2offset(src.first()), r_caller_sp);
1365     }
1366   } else if (dst.first()->is_stack()) {
1367     // reg to stack
1368     __ stfs(src.first()->as_FloatRegister(), reg2offset(dst.first()), R1_SP);
1369   } else {
1370     // reg to reg
1371     if (dst.first()->as_FloatRegister() != src.first()->as_FloatRegister())
1372       __ fmr(dst.first()->as_FloatRegister(), src.first()->as_FloatRegister());
1373   }
1374 }
1375 
1376 static void double_move(MacroAssembler*masm,
1377                         VMRegPair src, VMRegPair dst,
1378                         Register r_caller_sp, Register r_temp) {
1379   assert(src.first()->is_valid() && src.second() == src.first()->next(), "incoming must be double");
1380   assert(dst.first()->is_valid() && dst.second() == dst.first()->next(), "outgoing must be double");
1381 
1382   if (src.first()->is_stack()) {
1383     if (dst.first()->is_stack()) {
1384       // stack to stack
1385       __ ld( r_temp, reg2offset(src.first()), r_caller_sp);
1386       __ std(r_temp, reg2offset(dst.first()), R1_SP);
1387     } else {
1388       // stack to reg
1389       __ lfd(dst.first()->as_FloatRegister(), reg2offset(src.first()), r_caller_sp);
1390     }
1391   } else if (dst.first()->is_stack()) {
1392     // reg to stack
1393     __ stfd(src.first()->as_FloatRegister(), reg2offset(dst.first()), R1_SP);
1394   } else {
1395     // reg to reg
1396     if (dst.first()->as_FloatRegister() != src.first()->as_FloatRegister())
1397       __ fmr(dst.first()->as_FloatRegister(), src.first()->as_FloatRegister());
1398   }
1399 }
1400 
1401 void SharedRuntime::save_native_result(MacroAssembler *masm, BasicType ret_type, int frame_slots) {
1402   switch (ret_type) {
1403     case T_BOOLEAN:
1404     case T_CHAR:
1405     case T_BYTE:
1406     case T_SHORT:
1407     case T_INT:
1408       __ stw (R3_RET,  frame_slots*VMRegImpl::stack_slot_size, R1_SP);
1409       break;
1410     case T_ARRAY:
1411     case T_OBJECT:
1412     case T_LONG:
1413       __ std (R3_RET,  frame_slots*VMRegImpl::stack_slot_size, R1_SP);
1414       break;
1415     case T_FLOAT:
1416       __ stfs(F1_RET, frame_slots*VMRegImpl::stack_slot_size, R1_SP);
1417       break;
1418     case T_DOUBLE:
1419       __ stfd(F1_RET, frame_slots*VMRegImpl::stack_slot_size, R1_SP);
1420       break;
1421     case T_VOID:
1422       break;
1423     default:
1424       ShouldNotReachHere();
1425       break;
1426   }
1427 }
1428 
1429 void SharedRuntime::restore_native_result(MacroAssembler *masm, BasicType ret_type, int frame_slots) {
1430   switch (ret_type) {
1431     case T_BOOLEAN:
1432     case T_CHAR:
1433     case T_BYTE:
1434     case T_SHORT:
1435     case T_INT:
1436       __ lwz(R3_RET,  frame_slots*VMRegImpl::stack_slot_size, R1_SP);
1437       break;
1438     case T_ARRAY:
1439     case T_OBJECT:
1440     case T_LONG:
1441       __ ld (R3_RET,  frame_slots*VMRegImpl::stack_slot_size, R1_SP);
1442       break;
1443     case T_FLOAT:
1444       __ lfs(F1_RET, frame_slots*VMRegImpl::stack_slot_size, R1_SP);
1445       break;
1446     case T_DOUBLE:
1447       __ lfd(F1_RET, frame_slots*VMRegImpl::stack_slot_size, R1_SP);
1448       break;
1449     case T_VOID:
1450       break;
1451     default:
1452       ShouldNotReachHere();
1453       break;
1454   }
1455 }
1456 
1457 static void verify_oop_args(MacroAssembler* masm,
1458                             const methodHandle& method,
1459                             const BasicType* sig_bt,
1460                             const VMRegPair* regs) {
1461   Register temp_reg = R19_method;  // not part of any compiled calling seq
1462   if (VerifyOops) {
1463     for (int i = 0; i < method->size_of_parameters(); i++) {
1464       if (is_reference_type(sig_bt[i])) {
1465         VMReg r = regs[i].first();
1466         assert(r->is_valid(), "bad oop arg");
1467         if (r->is_stack()) {
1468           __ ld(temp_reg, reg2offset(r), R1_SP);
1469           __ verify_oop(temp_reg, FILE_AND_LINE);
1470         } else {
1471           __ verify_oop(r->as_Register(), FILE_AND_LINE);
1472         }
1473       }
1474     }
1475   }
1476 }
1477 
1478 static void gen_special_dispatch(MacroAssembler* masm,
1479                                  const methodHandle& method,
1480                                  const BasicType* sig_bt,
1481                                  const VMRegPair* regs) {
1482   verify_oop_args(masm, method, sig_bt, regs);
1483   vmIntrinsics::ID iid = method->intrinsic_id();
1484 
1485   // Now write the args into the outgoing interpreter space
1486   bool     has_receiver   = false;
1487   Register receiver_reg   = noreg;
1488   int      member_arg_pos = -1;
1489   Register member_reg     = noreg;
1490   int      ref_kind       = MethodHandles::signature_polymorphic_intrinsic_ref_kind(iid);
1491   if (ref_kind != 0) {
1492     member_arg_pos = method->size_of_parameters() - 1;  // trailing MemberName argument
1493     member_reg = R19_method;  // known to be free at this point
1494     has_receiver = MethodHandles::ref_kind_has_receiver(ref_kind);
1495   } else if (iid == vmIntrinsics::_invokeBasic) {
1496     has_receiver = true;
1497   } else if (iid == vmIntrinsics::_linkToNative) {
1498     member_arg_pos = method->size_of_parameters() - 1;  // trailing NativeEntryPoint argument
1499     member_reg = R19_method;  // known to be free at this point
1500   } else {
1501     fatal("unexpected intrinsic id %d", vmIntrinsics::as_int(iid));
1502   }
1503 
1504   if (member_reg != noreg) {
1505     // Load the member_arg into register, if necessary.
1506     SharedRuntime::check_member_name_argument_is_last_argument(method, sig_bt, regs);
1507     VMReg r = regs[member_arg_pos].first();
1508     if (r->is_stack()) {
1509       __ ld(member_reg, reg2offset(r), R1_SP);
1510     } else {
1511       // no data motion is needed
1512       member_reg = r->as_Register();
1513     }
1514   }
1515 
1516   if (has_receiver) {
1517     // Make sure the receiver is loaded into a register.
1518     assert(method->size_of_parameters() > 0, "oob");
1519     assert(sig_bt[0] == T_OBJECT, "receiver argument must be an object");
1520     VMReg r = regs[0].first();
1521     assert(r->is_valid(), "bad receiver arg");
1522     if (r->is_stack()) {
1523       // Porting note:  This assumes that compiled calling conventions always
1524       // pass the receiver oop in a register.  If this is not true on some
1525       // platform, pick a temp and load the receiver from stack.
1526       fatal("receiver always in a register");
1527       receiver_reg = R11_scratch1;  // TODO (hs24): is R11_scratch1 really free at this point?
1528       __ ld(receiver_reg, reg2offset(r), R1_SP);
1529     } else {
1530       // no data motion is needed
1531       receiver_reg = r->as_Register();
1532     }
1533   }
1534 
1535   // Figure out which address we are really jumping to:
1536   MethodHandles::generate_method_handle_dispatch(masm, iid,
1537                                                  receiver_reg, member_reg, /*for_compiler_entry:*/ true);
1538 }
1539 
1540 //---------------------------- continuation_enter_setup ---------------------------
1541 //
1542 // Frame setup.
1543 //
1544 // Arguments:
1545 //   None.
1546 //
1547 // Results:
1548 //   R1_SP: pointer to blank ContinuationEntry in the pushed frame.
1549 //
1550 // Kills:
1551 //   R0, R20
1552 //
1553 static OopMap* continuation_enter_setup(MacroAssembler* masm, int& framesize_words) {
1554   assert(ContinuationEntry::size() % VMRegImpl::stack_slot_size == 0, "");
1555   assert(in_bytes(ContinuationEntry::cont_offset())  % VMRegImpl::stack_slot_size == 0, "");
1556   assert(in_bytes(ContinuationEntry::chunk_offset()) % VMRegImpl::stack_slot_size == 0, "");
1557 
1558   const int frame_size_in_bytes = (int)ContinuationEntry::size();
1559   assert(is_aligned(frame_size_in_bytes, frame::alignment_in_bytes), "alignment error");
1560 
1561   framesize_words = frame_size_in_bytes / wordSize;
1562 
1563   DEBUG_ONLY(__ block_comment("setup {"));
1564   // Save return pc and push entry frame
1565   const Register return_pc = R20;
1566   __ mflr(return_pc);
1567   __ std(return_pc, _abi0(lr), R1_SP);     // SP->lr = return_pc
1568   __ push_frame(frame_size_in_bytes , R0); // SP -= frame_size_in_bytes
1569 
1570   OopMap* map = new OopMap((int)frame_size_in_bytes / VMRegImpl::stack_slot_size, 0 /* arg_slots*/);
1571 
1572   __ ld_ptr(R0, JavaThread::cont_entry_offset(), R16_thread);
1573   __ st_ptr(R1_SP, JavaThread::cont_entry_offset(), R16_thread);
1574   __ st_ptr(R0, ContinuationEntry::parent_offset(), R1_SP);
1575   DEBUG_ONLY(__ block_comment("} setup"));
1576 
1577   return map;
1578 }
1579 
1580 //---------------------------- fill_continuation_entry ---------------------------
1581 //
1582 // Initialize the new ContinuationEntry.
1583 //
1584 // Arguments:
1585 //   R1_SP: pointer to blank Continuation entry
1586 //   reg_cont_obj: pointer to the continuation
1587 //   reg_flags: flags
1588 //
1589 // Results:
1590 //   R1_SP: pointer to filled out ContinuationEntry
1591 //
1592 // Kills:
1593 //   R8_ARG6, R9_ARG7, R10_ARG8
1594 //
1595 static void fill_continuation_entry(MacroAssembler* masm, Register reg_cont_obj, Register reg_flags) {
1596   assert_different_registers(reg_cont_obj, reg_flags);
1597   Register zero = R8_ARG6;
1598   Register tmp2 = R9_ARG7;
1599   Register tmp3 = R10_ARG8;
1600 
1601   DEBUG_ONLY(__ block_comment("fill {"));
1602 #ifdef ASSERT
1603   __ load_const_optimized(tmp2, ContinuationEntry::cookie_value());
1604   __ stw(tmp2, in_bytes(ContinuationEntry::cookie_offset()), R1_SP);
1605   __ std(tmp2, _abi0(cr), R1_SP);
1606 #endif //ASSERT
1607 
1608   __ li(zero, 0);
1609   __ st_ptr(reg_cont_obj, ContinuationEntry::cont_offset(), R1_SP);
1610   __ stw(reg_flags, in_bytes(ContinuationEntry::flags_offset()), R1_SP);
1611   __ st_ptr(zero, ContinuationEntry::chunk_offset(), R1_SP);
1612   __ stw(zero, in_bytes(ContinuationEntry::argsize_offset()), R1_SP);
1613   __ stw(zero, in_bytes(ContinuationEntry::pin_count_offset()), R1_SP);
1614 
1615   __ ld_ptr(tmp2, JavaThread::cont_fastpath_offset(), R16_thread);
1616   __ ld(tmp3, in_bytes(JavaThread::held_monitor_count_offset()), R16_thread);
1617   __ st_ptr(tmp2, ContinuationEntry::parent_cont_fastpath_offset(), R1_SP);
1618   __ std(tmp3, in_bytes(ContinuationEntry::parent_held_monitor_count_offset()), R1_SP);
1619 
1620   __ st_ptr(zero, JavaThread::cont_fastpath_offset(), R16_thread);
1621   __ std(zero, in_bytes(JavaThread::held_monitor_count_offset()), R16_thread);
1622   DEBUG_ONLY(__ block_comment("} fill"));
1623 }
1624 
1625 //---------------------------- continuation_enter_cleanup ---------------------------
1626 //
1627 // Copy corresponding attributes from the top ContinuationEntry to the JavaThread
1628 // before deleting it.
1629 //
1630 // Arguments:
1631 //   R1_SP: pointer to the ContinuationEntry
1632 //
1633 // Results:
1634 //   None.
1635 //
1636 // Kills:
1637 //   R8_ARG6, R9_ARG7, R10_ARG8, R15_esp
1638 //
1639 static void continuation_enter_cleanup(MacroAssembler* masm) {
1640   Register tmp1 = R8_ARG6;
1641   Register tmp2 = R9_ARG7;
1642   Register tmp3 = R10_ARG8;
1643 
1644 #ifdef ASSERT
1645   __ block_comment("clean {");
1646   __ ld_ptr(tmp1, JavaThread::cont_entry_offset(), R16_thread);
1647   __ cmpd(CCR0, R1_SP, tmp1);
1648   __ asm_assert_eq(FILE_AND_LINE ": incorrect R1_SP");
1649   __ load_const_optimized(tmp1, ContinuationEntry::cookie_value());
1650   __ ld(tmp2, _abi0(cr), R1_SP);
1651   __ cmpd(CCR0, tmp1, tmp2);
1652   __ asm_assert_eq(FILE_AND_LINE ": cookie not found");
1653 #endif
1654 
1655   __ ld_ptr(tmp1, ContinuationEntry::parent_cont_fastpath_offset(), R1_SP);
1656   __ st_ptr(tmp1, JavaThread::cont_fastpath_offset(), R16_thread);
1657 
1658   if (CheckJNICalls) {
1659     // Check if this is a virtual thread continuation
1660     Label L_skip_vthread_code;
1661     __ lwz(R0, in_bytes(ContinuationEntry::flags_offset()), R1_SP);
1662     __ cmpwi(CCR0, R0, 0);
1663     __ beq(CCR0, L_skip_vthread_code);
1664 
1665     // If the held monitor count is > 0 and this vthread is terminating then
1666     // it failed to release a JNI monitor. So we issue the same log message
1667     // that JavaThread::exit does.
1668     __ ld(R0, in_bytes(JavaThread::jni_monitor_count_offset()), R16_thread);
1669     __ cmpdi(CCR0, R0, 0);
1670     __ beq(CCR0, L_skip_vthread_code);
1671 
1672     // Save return value potentially containing the exception oop
1673     Register ex_oop = R15_esp;   // nonvolatile register
1674     __ mr(ex_oop, R3_RET);
1675     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::log_jni_monitor_still_held));
1676     // Restore potental return value
1677     __ mr(R3_RET, ex_oop);
1678 
1679     // For vthreads we have to explicitly zero the JNI monitor count of the carrier
1680     // on termination. The held count is implicitly zeroed below when we restore from
1681     // the parent held count (which has to be zero).
1682     __ li(tmp1, 0);
1683     __ std(tmp1, in_bytes(JavaThread::jni_monitor_count_offset()), R16_thread);
1684 
1685     __ bind(L_skip_vthread_code);
1686   }
1687 #ifdef ASSERT
1688   else {
1689     // Check if this is a virtual thread continuation
1690     Label L_skip_vthread_code;
1691     __ lwz(R0, in_bytes(ContinuationEntry::flags_offset()), R1_SP);
1692     __ cmpwi(CCR0, R0, 0);
1693     __ beq(CCR0, L_skip_vthread_code);
1694 
1695     // See comment just above. If not checking JNI calls the JNI count is only
1696     // needed for assertion checking.
1697     __ li(tmp1, 0);
1698     __ std(tmp1, in_bytes(JavaThread::jni_monitor_count_offset()), R16_thread);
1699 
1700     __ bind(L_skip_vthread_code);
1701   }
1702 #endif
1703 
1704   __ ld(tmp2, in_bytes(ContinuationEntry::parent_held_monitor_count_offset()), R1_SP);
1705   __ ld_ptr(tmp3, ContinuationEntry::parent_offset(), R1_SP);
1706   __ std(tmp2, in_bytes(JavaThread::held_monitor_count_offset()), R16_thread);
1707   __ st_ptr(tmp3, JavaThread::cont_entry_offset(), R16_thread);
1708   DEBUG_ONLY(__ block_comment("} clean"));
1709 }
1710 
1711 static void check_continuation_enter_argument(VMReg actual_vmreg,
1712                                               Register expected_reg,
1713                                               const char* name) {
1714   assert(!actual_vmreg->is_stack(), "%s cannot be on stack", name);
1715   assert(actual_vmreg->as_Register() == expected_reg,
1716          "%s is in unexpected register: %s instead of %s",
1717          name, actual_vmreg->as_Register()->name(), expected_reg->name());
1718 }
1719 
1720 static void gen_continuation_enter(MacroAssembler* masm,
1721                                    const VMRegPair* regs,
1722                                    int& exception_offset,
1723                                    OopMapSet* oop_maps,
1724                                    int& frame_complete,
1725                                    int& framesize_words,
1726                                    int& interpreted_entry_offset,
1727                                    int& compiled_entry_offset) {
1728 
1729   // enterSpecial(Continuation c, boolean isContinue, boolean isVirtualThread)
1730   int pos_cont_obj   = 0;
1731   int pos_is_cont    = 1;
1732   int pos_is_virtual = 2;
1733 
1734   // The platform-specific calling convention may present the arguments in various registers.
1735   // To simplify the rest of the code, we expect the arguments to reside at these known
1736   // registers, and we additionally check the placement here in case calling convention ever
1737   // changes.
1738   Register reg_cont_obj   = R3_ARG1;
1739   Register reg_is_cont    = R4_ARG2;
1740   Register reg_is_virtual = R5_ARG3;
1741 
1742   check_continuation_enter_argument(regs[pos_cont_obj].first(),   reg_cont_obj,   "Continuation object");
1743   check_continuation_enter_argument(regs[pos_is_cont].first(),    reg_is_cont,    "isContinue");
1744   check_continuation_enter_argument(regs[pos_is_virtual].first(), reg_is_virtual, "isVirtualThread");
1745 
1746   address resolve_static_call = SharedRuntime::get_resolve_static_call_stub();
1747 
1748   address start = __ pc();
1749 
1750   Label L_thaw, L_exit;
1751 
1752   // i2i entry used at interp_only_mode only
1753   interpreted_entry_offset = __ pc() - start;
1754   {
1755 #ifdef ASSERT
1756     Label is_interp_only;
1757     __ lwz(R0, in_bytes(JavaThread::interp_only_mode_offset()), R16_thread);
1758     __ cmpwi(CCR0, R0, 0);
1759     __ bne(CCR0, is_interp_only);
1760     __ stop("enterSpecial interpreter entry called when not in interp_only_mode");
1761     __ bind(is_interp_only);
1762 #endif
1763 
1764     // Read interpreter arguments into registers (this is an ad-hoc i2c adapter)
1765     __ ld(reg_cont_obj,    Interpreter::stackElementSize*3, R15_esp);
1766     __ lwz(reg_is_cont,    Interpreter::stackElementSize*2, R15_esp);
1767     __ lwz(reg_is_virtual, Interpreter::stackElementSize*1, R15_esp);
1768 
1769     __ push_cont_fastpath();
1770 
1771     OopMap* map = continuation_enter_setup(masm, framesize_words);
1772 
1773     // The frame is complete here, but we only record it for the compiled entry, so the frame would appear unsafe,
1774     // but that's okay because at the very worst we'll miss an async sample, but we're in interp_only_mode anyway.
1775 
1776     fill_continuation_entry(masm, reg_cont_obj, reg_is_virtual);
1777 
1778     // If isContinue, call to thaw. Otherwise, call Continuation.enter(Continuation c, boolean isContinue)
1779     __ cmpwi(CCR0, reg_is_cont, 0);
1780     __ bne(CCR0, L_thaw);
1781 
1782     // --- call Continuation.enter(Continuation c, boolean isContinue)
1783 
1784     // Emit compiled static call. The call will be always resolved to the c2i
1785     // entry of Continuation.enter(Continuation c, boolean isContinue).
1786     // There are special cases in SharedRuntime::resolve_static_call_C() and
1787     // SharedRuntime::resolve_sub_helper_internal() to achieve this
1788     // See also corresponding call below.
1789     address c2i_call_pc = __ pc();
1790     int start_offset = __ offset();
1791     // Put the entry point as a constant into the constant pool.
1792     const address entry_point_toc_addr   = __ address_constant(resolve_static_call, RelocationHolder::none);
1793     const int     entry_point_toc_offset = __ offset_to_method_toc(entry_point_toc_addr);
1794     guarantee(entry_point_toc_addr != nullptr, "const section overflow");
1795 
1796     // Emit the trampoline stub which will be related to the branch-and-link below.
1797     address stub = __ emit_trampoline_stub(entry_point_toc_offset, start_offset);
1798     guarantee(stub != nullptr, "no space for trampoline stub");
1799 
1800     __ relocate(relocInfo::static_call_type);
1801     // Note: At this point we do not have the address of the trampoline
1802     // stub, and the entry point might be too far away for bl, so __ pc()
1803     // serves as dummy and the bl will be patched later.
1804     __ bl(__ pc());
1805     oop_maps->add_gc_map(__ pc() - start, map);
1806     __ post_call_nop();
1807 
1808     __ b(L_exit);
1809 
1810     // static stub for the call above
1811     stub = CompiledDirectCall::emit_to_interp_stub(masm, c2i_call_pc);
1812     guarantee(stub != nullptr, "no space for static stub");
1813   }
1814 
1815   // compiled entry
1816   __ align(CodeEntryAlignment);
1817   compiled_entry_offset = __ pc() - start;
1818 
1819   OopMap* map = continuation_enter_setup(masm, framesize_words);
1820 
1821   // Frame is now completed as far as size and linkage.
1822   frame_complete =__ pc() - start;
1823 
1824   fill_continuation_entry(masm, reg_cont_obj, reg_is_virtual);
1825 
1826   // If isContinue, call to thaw. Otherwise, call Continuation.enter(Continuation c, boolean isContinue)
1827   __ cmpwi(CCR0, reg_is_cont, 0);
1828   __ bne(CCR0, L_thaw);
1829 
1830   // --- call Continuation.enter(Continuation c, boolean isContinue)
1831 
1832   // Emit compiled static call
1833   // The call needs to be resolved. There's a special case for this in
1834   // SharedRuntime::find_callee_info_helper() which calls
1835   // LinkResolver::resolve_continuation_enter() which resolves the call to
1836   // Continuation.enter(Continuation c, boolean isContinue).
1837   address call_pc = __ pc();
1838   int start_offset = __ offset();
1839   // Put the entry point as a constant into the constant pool.
1840   const address entry_point_toc_addr   = __ address_constant(resolve_static_call, RelocationHolder::none);
1841   const int     entry_point_toc_offset = __ offset_to_method_toc(entry_point_toc_addr);
1842   guarantee(entry_point_toc_addr != nullptr, "const section overflow");
1843 
1844   // Emit the trampoline stub which will be related to the branch-and-link below.
1845   address stub = __ emit_trampoline_stub(entry_point_toc_offset, start_offset);
1846   guarantee(stub != nullptr, "no space for trampoline stub");
1847 
1848   __ relocate(relocInfo::static_call_type);
1849   // Note: At this point we do not have the address of the trampoline
1850   // stub, and the entry point might be too far away for bl, so __ pc()
1851   // serves as dummy and the bl will be patched later.
1852   __ bl(__ pc());
1853   oop_maps->add_gc_map(__ pc() - start, map);
1854   __ post_call_nop();
1855 
1856   __ b(L_exit);
1857 
1858   // --- Thawing path
1859 
1860   __ bind(L_thaw);
1861   ContinuationEntry::_thaw_call_pc_offset = __ pc() - start;
1862   __ add_const_optimized(R0, R29_TOC, MacroAssembler::offset_to_global_toc(StubRoutines::cont_thaw()));
1863   __ mtctr(R0);
1864   __ bctrl();
1865   oop_maps->add_gc_map(__ pc() - start, map->deep_copy());
1866   ContinuationEntry::_return_pc_offset = __ pc() - start;
1867   __ post_call_nop();
1868 
1869   // --- Normal exit (resolve/thawing)
1870 
1871   __ bind(L_exit);
1872   ContinuationEntry::_cleanup_offset = __ pc() - start;
1873   continuation_enter_cleanup(masm);
1874 
1875   // Pop frame and return
1876   DEBUG_ONLY(__ ld_ptr(R0, 0, R1_SP));
1877   __ addi(R1_SP, R1_SP, framesize_words*wordSize);
1878   DEBUG_ONLY(__ cmpd(CCR0, R0, R1_SP));
1879   __ asm_assert_eq(FILE_AND_LINE ": inconsistent frame size");
1880   __ ld(R0, _abi0(lr), R1_SP); // Return pc
1881   __ mtlr(R0);
1882   __ blr();
1883 
1884   // --- Exception handling path
1885 
1886   exception_offset = __ pc() - start;
1887 
1888   continuation_enter_cleanup(masm);
1889   Register ex_pc  = R17_tos;   // nonvolatile register
1890   Register ex_oop = R15_esp;   // nonvolatile register
1891   __ ld(ex_pc, _abi0(callers_sp), R1_SP); // Load caller's return pc
1892   __ ld(ex_pc, _abi0(lr), ex_pc);
1893   __ mr(ex_oop, R3_RET);                  // save return value containing the exception oop
1894   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), R16_thread, ex_pc);
1895   __ mtlr(R3_RET);                        // the exception handler
1896   __ ld(R1_SP, _abi0(callers_sp), R1_SP); // remove enterSpecial frame
1897 
1898   // Continue at exception handler
1899   // See OptoRuntime::generate_exception_blob for register arguments
1900   __ mr(R3_ARG1, ex_oop); // pass exception oop
1901   __ mr(R4_ARG2, ex_pc);  // pass exception pc
1902   __ blr();
1903 
1904   // static stub for the call above
1905   stub = CompiledDirectCall::emit_to_interp_stub(masm, call_pc);
1906   guarantee(stub != nullptr, "no space for static stub");
1907 }
1908 
1909 static void gen_continuation_yield(MacroAssembler* masm,
1910                                    const VMRegPair* regs,
1911                                    OopMapSet* oop_maps,
1912                                    int& frame_complete,
1913                                    int& framesize_words,
1914                                    int& compiled_entry_offset) {
1915   Register tmp = R10_ARG8;
1916 
1917   const int framesize_bytes = (int)align_up((int)frame::native_abi_reg_args_size, frame::alignment_in_bytes);
1918   framesize_words = framesize_bytes / wordSize;
1919 
1920   address start = __ pc();
1921   compiled_entry_offset = __ pc() - start;
1922 
1923   // Save return pc and push entry frame
1924   __ mflr(tmp);
1925   __ std(tmp, _abi0(lr), R1_SP);       // SP->lr = return_pc
1926   __ push_frame(framesize_bytes , R0); // SP -= frame_size_in_bytes
1927 
1928   DEBUG_ONLY(__ block_comment("Frame Complete"));
1929   frame_complete = __ pc() - start;
1930   address last_java_pc = __ pc();
1931 
1932   // This nop must be exactly at the PC we push into the frame info.
1933   // We use this nop for fast CodeBlob lookup, associate the OopMap
1934   // with it right away.
1935   __ post_call_nop();
1936   OopMap* map = new OopMap(framesize_bytes / VMRegImpl::stack_slot_size, 1);
1937   oop_maps->add_gc_map(last_java_pc - start, map);
1938 
1939   __ calculate_address_from_global_toc(tmp, last_java_pc); // will be relocated
1940   __ set_last_Java_frame(R1_SP, tmp);
1941   __ call_VM_leaf(Continuation::freeze_entry(), R16_thread, R1_SP);
1942   __ reset_last_Java_frame();
1943 
1944   Label L_pinned;
1945 
1946   __ cmpwi(CCR0, R3_RET, 0);
1947   __ bne(CCR0, L_pinned);
1948 
1949   // yield succeeded
1950 
1951   // Pop frames of continuation including this stub's frame
1952   __ ld_ptr(R1_SP, JavaThread::cont_entry_offset(), R16_thread);
1953   // The frame pushed by gen_continuation_enter is on top now again
1954   continuation_enter_cleanup(masm);
1955 
1956   // Pop frame and return
1957   Label L_return;
1958   __ bind(L_return);
1959   __ pop_frame();
1960   __ ld(R0, _abi0(lr), R1_SP); // Return pc
1961   __ mtlr(R0);
1962   __ blr();
1963 
1964   // yield failed - continuation is pinned
1965 
1966   __ bind(L_pinned);
1967 
1968   // handle pending exception thrown by freeze
1969   __ ld(tmp, in_bytes(JavaThread::pending_exception_offset()), R16_thread);
1970   __ cmpdi(CCR0, tmp, 0);
1971   __ beq(CCR0, L_return); // return if no exception is pending
1972   __ pop_frame();
1973   __ ld(R0, _abi0(lr), R1_SP); // Return pc
1974   __ mtlr(R0);
1975   __ load_const_optimized(tmp, StubRoutines::forward_exception_entry(), R0);
1976   __ mtctr(tmp);
1977   __ bctr();
1978 }
1979 
1980 void SharedRuntime::continuation_enter_cleanup(MacroAssembler* masm) {
1981   ::continuation_enter_cleanup(masm);
1982 }
1983 
1984 // ---------------------------------------------------------------------------
1985 // Generate a native wrapper for a given method. The method takes arguments
1986 // in the Java compiled code convention, marshals them to the native
1987 // convention (handlizes oops, etc), transitions to native, makes the call,
1988 // returns to java state (possibly blocking), unhandlizes any result and
1989 // returns.
1990 //
1991 // Critical native functions are a shorthand for the use of
1992 // GetPrimtiveArrayCritical and disallow the use of any other JNI
1993 // functions.  The wrapper is expected to unpack the arguments before
1994 // passing them to the callee. Critical native functions leave the state _in_Java,
1995 // since they cannot stop for GC.
1996 // Some other parts of JNI setup are skipped like the tear down of the JNI handle
1997 // block and the check for pending exceptions it's impossible for them
1998 // to be thrown.
1999 //
2000 nmethod *SharedRuntime::generate_native_wrapper(MacroAssembler *masm,
2001                                                 const methodHandle& method,
2002                                                 int compile_id,
2003                                                 BasicType *in_sig_bt,
2004                                                 VMRegPair *in_regs,
2005                                                 BasicType ret_type) {
2006   if (method->is_continuation_native_intrinsic()) {
2007     int exception_offset = -1;
2008     OopMapSet* oop_maps = new OopMapSet();
2009     int frame_complete = -1;
2010     int stack_slots = -1;
2011     int interpreted_entry_offset = -1;
2012     int vep_offset = -1;
2013     if (method->is_continuation_enter_intrinsic()) {
2014       gen_continuation_enter(masm,
2015                              in_regs,
2016                              exception_offset,
2017                              oop_maps,
2018                              frame_complete,
2019                              stack_slots,
2020                              interpreted_entry_offset,
2021                              vep_offset);
2022     } else if (method->is_continuation_yield_intrinsic()) {
2023       gen_continuation_yield(masm,
2024                              in_regs,
2025                              oop_maps,
2026                              frame_complete,
2027                              stack_slots,
2028                              vep_offset);
2029     } else {
2030       guarantee(false, "Unknown Continuation native intrinsic");
2031     }
2032 
2033 #ifdef ASSERT
2034     if (method->is_continuation_enter_intrinsic()) {
2035       assert(interpreted_entry_offset != -1, "Must be set");
2036       assert(exception_offset != -1,         "Must be set");
2037     } else {
2038       assert(interpreted_entry_offset == -1, "Must be unset");
2039       assert(exception_offset == -1,         "Must be unset");
2040     }
2041     assert(frame_complete != -1,    "Must be set");
2042     assert(stack_slots != -1,       "Must be set");
2043     assert(vep_offset != -1,        "Must be set");
2044 #endif
2045 
2046     __ flush();
2047     nmethod* nm = nmethod::new_native_nmethod(method,
2048                                               compile_id,
2049                                               masm->code(),
2050                                               vep_offset,
2051                                               frame_complete,
2052                                               stack_slots,
2053                                               in_ByteSize(-1),
2054                                               in_ByteSize(-1),
2055                                               oop_maps,
2056                                               exception_offset);
2057     if (nm == nullptr) return nm;
2058     if (method->is_continuation_enter_intrinsic()) {
2059       ContinuationEntry::set_enter_code(nm, interpreted_entry_offset);
2060     } else if (method->is_continuation_yield_intrinsic()) {
2061       _cont_doYield_stub = nm;
2062     }
2063     return nm;
2064   }
2065 
2066   if (method->is_method_handle_intrinsic()) {
2067     vmIntrinsics::ID iid = method->intrinsic_id();
2068     intptr_t start = (intptr_t)__ pc();
2069     int vep_offset = ((intptr_t)__ pc()) - start;
2070     gen_special_dispatch(masm,
2071                          method,
2072                          in_sig_bt,
2073                          in_regs);
2074     int frame_complete = ((intptr_t)__ pc()) - start;  // not complete, period
2075     __ flush();
2076     int stack_slots = SharedRuntime::out_preserve_stack_slots();  // no out slots at all, actually
2077     return nmethod::new_native_nmethod(method,
2078                                        compile_id,
2079                                        masm->code(),
2080                                        vep_offset,
2081                                        frame_complete,
2082                                        stack_slots / VMRegImpl::slots_per_word,
2083                                        in_ByteSize(-1),
2084                                        in_ByteSize(-1),
2085                                        (OopMapSet*)nullptr);
2086   }
2087 
2088   address native_func = method->native_function();
2089   assert(native_func != nullptr, "must have function");
2090 
2091   // First, create signature for outgoing C call
2092   // --------------------------------------------------------------------------
2093 
2094   int total_in_args = method->size_of_parameters();
2095   // We have received a description of where all the java args are located
2096   // on entry to the wrapper. We need to convert these args to where
2097   // the jni function will expect them. To figure out where they go
2098   // we convert the java signature to a C signature by inserting
2099   // the hidden arguments as arg[0] and possibly arg[1] (static method)
2100 
2101   // Calculate the total number of C arguments and create arrays for the
2102   // signature and the outgoing registers.
2103   // On ppc64, we have two arrays for the outgoing registers, because
2104   // some floating-point arguments must be passed in registers _and_
2105   // in stack locations.
2106   bool method_is_static = method->is_static();
2107   int  total_c_args     = total_in_args + (method_is_static ? 2 : 1);
2108 
2109   BasicType *out_sig_bt = NEW_RESOURCE_ARRAY(BasicType, total_c_args);
2110   VMRegPair *out_regs   = NEW_RESOURCE_ARRAY(VMRegPair, total_c_args);
2111   BasicType* in_elem_bt = nullptr;
2112 
2113   // Create the signature for the C call:
2114   //   1) add the JNIEnv*
2115   //   2) add the class if the method is static
2116   //   3) copy the rest of the incoming signature (shifted by the number of
2117   //      hidden arguments).
2118 
2119   int argc = 0;
2120   out_sig_bt[argc++] = T_ADDRESS;
2121   if (method->is_static()) {
2122     out_sig_bt[argc++] = T_OBJECT;
2123   }
2124 
2125   for (int i = 0; i < total_in_args ; i++ ) {
2126     out_sig_bt[argc++] = in_sig_bt[i];
2127   }
2128 
2129 
2130   // Compute the wrapper's frame size.
2131   // --------------------------------------------------------------------------
2132 
2133   // Now figure out where the args must be stored and how much stack space
2134   // they require.
2135   //
2136   // Compute framesize for the wrapper. We need to handlize all oops in
2137   // incoming registers.
2138   //
2139   // Calculate the total number of stack slots we will need:
2140   //   1) abi requirements
2141   //   2) outgoing arguments
2142   //   3) space for inbound oop handle area
2143   //   4) space for handlizing a klass if static method
2144   //   5) space for a lock if synchronized method
2145   //   6) workspace for saving return values, int <-> float reg moves, etc.
2146   //   7) alignment
2147   //
2148   // Layout of the native wrapper frame:
2149   // (stack grows upwards, memory grows downwards)
2150   //
2151   // NW     [ABI_REG_ARGS]             <-- 1) R1_SP
2152   //        [outgoing arguments]       <-- 2) R1_SP + out_arg_slot_offset
2153   //        [oopHandle area]           <-- 3) R1_SP + oop_handle_offset
2154   //        klass                      <-- 4) R1_SP + klass_offset
2155   //        lock                       <-- 5) R1_SP + lock_offset
2156   //        [workspace]                <-- 6) R1_SP + workspace_offset
2157   //        [alignment] (optional)     <-- 7)
2158   // caller [JIT_TOP_ABI_48]           <-- r_callers_sp
2159   //
2160   // - *_slot_offset Indicates offset from SP in number of stack slots.
2161   // - *_offset      Indicates offset from SP in bytes.
2162 
2163   int stack_slots = c_calling_convention(out_sig_bt, out_regs, total_c_args) + // 1+2)
2164                     SharedRuntime::out_preserve_stack_slots(); // See c_calling_convention.
2165 
2166   // Now the space for the inbound oop handle area.
2167   int total_save_slots = num_java_iarg_registers * VMRegImpl::slots_per_word;
2168 
2169   int oop_handle_slot_offset = stack_slots;
2170   stack_slots += total_save_slots;                                                // 3)
2171 
2172   int klass_slot_offset = 0;
2173   int klass_offset      = -1;
2174   if (method_is_static) {                                                         // 4)
2175     klass_slot_offset  = stack_slots;
2176     klass_offset       = klass_slot_offset * VMRegImpl::stack_slot_size;
2177     stack_slots       += VMRegImpl::slots_per_word;
2178   }
2179 
2180   int lock_slot_offset = 0;
2181   int lock_offset      = -1;
2182   if (method->is_synchronized()) {                                                // 5)
2183     lock_slot_offset   = stack_slots;
2184     lock_offset        = lock_slot_offset * VMRegImpl::stack_slot_size;
2185     stack_slots       += VMRegImpl::slots_per_word;
2186   }
2187 
2188   int workspace_slot_offset = stack_slots;                                        // 6)
2189   stack_slots         += 2;
2190 
2191   // Now compute actual number of stack words we need.
2192   // Rounding to make stack properly aligned.
2193   stack_slots = align_up(stack_slots,                                             // 7)
2194                          frame::alignment_in_bytes / VMRegImpl::stack_slot_size);
2195   int frame_size_in_bytes = stack_slots * VMRegImpl::stack_slot_size;
2196 
2197 
2198   // Now we can start generating code.
2199   // --------------------------------------------------------------------------
2200 
2201   intptr_t start_pc = (intptr_t)__ pc();
2202   intptr_t vep_start_pc;
2203   intptr_t frame_done_pc;
2204 
2205   Label    handle_pending_exception;
2206   Label    last_java_pc;
2207 
2208   Register r_callers_sp = R21;
2209   Register r_temp_1     = R22;
2210   Register r_temp_2     = R23;
2211   Register r_temp_3     = R24;
2212   Register r_temp_4     = R25;
2213   Register r_temp_5     = R26;
2214   Register r_temp_6     = R27;
2215   Register r_last_java_pc = R28;
2216 
2217   Register r_carg1_jnienv        = noreg;
2218   Register r_carg2_classorobject = noreg;
2219   r_carg1_jnienv        = out_regs[0].first()->as_Register();
2220   r_carg2_classorobject = out_regs[1].first()->as_Register();
2221 
2222 
2223   // Generate the Unverified Entry Point (UEP).
2224   // --------------------------------------------------------------------------
2225   assert(start_pc == (intptr_t)__ pc(), "uep must be at start");
2226 
2227   // Check ic: object class == cached class?
2228   if (!method_is_static) {
2229     __ ic_check(4 /* end_alignment */);
2230   }
2231 
2232   // Generate the Verified Entry Point (VEP).
2233   // --------------------------------------------------------------------------
2234   vep_start_pc = (intptr_t)__ pc();
2235 
2236   if (VM_Version::supports_fast_class_init_checks() && method->needs_clinit_barrier()) {
2237     Label L_skip_barrier;
2238     Register klass = r_temp_1;
2239     // Notify OOP recorder (don't need the relocation)
2240     AddressLiteral md = __ constant_metadata_address(method->method_holder());
2241     __ load_const_optimized(klass, md.value(), R0);
2242     __ clinit_barrier(klass, R16_thread, &L_skip_barrier /*L_fast_path*/);
2243 
2244     __ load_const_optimized(klass, SharedRuntime::get_handle_wrong_method_stub(), R0);
2245     __ mtctr(klass);
2246     __ bctr();
2247 
2248     __ bind(L_skip_barrier);
2249   }
2250 
2251   __ save_LR(r_temp_1);
2252   __ generate_stack_overflow_check(frame_size_in_bytes); // Check before creating frame.
2253   __ mr(r_callers_sp, R1_SP);                            // Remember frame pointer.
2254   __ push_frame(frame_size_in_bytes, r_temp_1);          // Push the c2n adapter's frame.
2255 
2256   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
2257   bs->nmethod_entry_barrier(masm, r_temp_1);
2258 
2259   frame_done_pc = (intptr_t)__ pc();
2260 
2261   // Native nmethod wrappers never take possession of the oop arguments.
2262   // So the caller will gc the arguments.
2263   // The only thing we need an oopMap for is if the call is static.
2264   //
2265   // An OopMap for lock (and class if static), and one for the VM call itself.
2266   OopMapSet *oop_maps = new OopMapSet();
2267   OopMap    *oop_map  = new OopMap(stack_slots * 2, 0 /* arg_slots*/);
2268 
2269   // Move arguments from register/stack to register/stack.
2270   // --------------------------------------------------------------------------
2271   //
2272   // We immediately shuffle the arguments so that for any vm call we have
2273   // to make from here on out (sync slow path, jvmti, etc.) we will have
2274   // captured the oops from our caller and have a valid oopMap for them.
2275   //
2276   // Natives require 1 or 2 extra arguments over the normal ones: the JNIEnv*
2277   // (derived from JavaThread* which is in R16_thread) and, if static,
2278   // the class mirror instead of a receiver. This pretty much guarantees that
2279   // register layout will not match. We ignore these extra arguments during
2280   // the shuffle. The shuffle is described by the two calling convention
2281   // vectors we have in our possession. We simply walk the java vector to
2282   // get the source locations and the c vector to get the destinations.
2283 
2284   // Record sp-based slot for receiver on stack for non-static methods.
2285   int receiver_offset = -1;
2286 
2287   // We move the arguments backward because the floating point registers
2288   // destination will always be to a register with a greater or equal
2289   // register number or the stack.
2290   //   in  is the index of the incoming Java arguments
2291   //   out is the index of the outgoing C arguments
2292 
2293 #ifdef ASSERT
2294   bool reg_destroyed[Register::number_of_registers];
2295   bool freg_destroyed[FloatRegister::number_of_registers];
2296   for (int r = 0 ; r < Register::number_of_registers ; r++) {
2297     reg_destroyed[r] = false;
2298   }
2299   for (int f = 0 ; f < FloatRegister::number_of_registers ; f++) {
2300     freg_destroyed[f] = false;
2301   }
2302 #endif // ASSERT
2303 
2304   for (int in = total_in_args - 1, out = total_c_args - 1; in >= 0 ; in--, out--) {
2305 
2306 #ifdef ASSERT
2307     if (in_regs[in].first()->is_Register()) {
2308       assert(!reg_destroyed[in_regs[in].first()->as_Register()->encoding()], "ack!");
2309     } else if (in_regs[in].first()->is_FloatRegister()) {
2310       assert(!freg_destroyed[in_regs[in].first()->as_FloatRegister()->encoding()], "ack!");
2311     }
2312     if (out_regs[out].first()->is_Register()) {
2313       reg_destroyed[out_regs[out].first()->as_Register()->encoding()] = true;
2314     } else if (out_regs[out].first()->is_FloatRegister()) {
2315       freg_destroyed[out_regs[out].first()->as_FloatRegister()->encoding()] = true;
2316     }
2317 #endif // ASSERT
2318 
2319     switch (in_sig_bt[in]) {
2320       case T_BOOLEAN:
2321       case T_CHAR:
2322       case T_BYTE:
2323       case T_SHORT:
2324       case T_INT:
2325         // Move int and do sign extension.
2326         int_move(masm, in_regs[in], out_regs[out], r_callers_sp, r_temp_1);
2327         break;
2328       case T_LONG:
2329         long_move(masm, in_regs[in], out_regs[out], r_callers_sp, r_temp_1);
2330         break;
2331       case T_ARRAY:
2332       case T_OBJECT:
2333         object_move(masm, stack_slots,
2334                     oop_map, oop_handle_slot_offset,
2335                     ((in == 0) && (!method_is_static)), &receiver_offset,
2336                     in_regs[in], out_regs[out],
2337                     r_callers_sp, r_temp_1, r_temp_2);
2338         break;
2339       case T_VOID:
2340         break;
2341       case T_FLOAT:
2342         float_move(masm, in_regs[in], out_regs[out], r_callers_sp, r_temp_1);
2343         break;
2344       case T_DOUBLE:
2345         double_move(masm, in_regs[in], out_regs[out], r_callers_sp, r_temp_1);
2346         break;
2347       case T_ADDRESS:
2348         fatal("found type (T_ADDRESS) in java args");
2349         break;
2350       default:
2351         ShouldNotReachHere();
2352         break;
2353     }
2354   }
2355 
2356   // Pre-load a static method's oop into ARG2.
2357   // Used both by locking code and the normal JNI call code.
2358   if (method_is_static) {
2359     __ set_oop_constant(JNIHandles::make_local(method->method_holder()->java_mirror()),
2360                         r_carg2_classorobject);
2361 
2362     // Now handlize the static class mirror in carg2. It's known not-null.
2363     __ std(r_carg2_classorobject, klass_offset, R1_SP);
2364     oop_map->set_oop(VMRegImpl::stack2reg(klass_slot_offset));
2365     __ addi(r_carg2_classorobject, R1_SP, klass_offset);
2366   }
2367 
2368   // Get JNIEnv* which is first argument to native.
2369   __ addi(r_carg1_jnienv, R16_thread, in_bytes(JavaThread::jni_environment_offset()));
2370 
2371   // NOTE:
2372   //
2373   // We have all of the arguments setup at this point.
2374   // We MUST NOT touch any outgoing regs from this point on.
2375   // So if we must call out we must push a new frame.
2376 
2377   // The last java pc will also be used as resume pc if this is the wrapper for wait0.
2378   // For this purpose the precise location matters but not for oopmap lookup.
2379   __ calculate_address_from_global_toc(r_last_java_pc, last_java_pc, true, true, true, true);
2380 
2381   // Make sure that thread is non-volatile; it crosses a bunch of VM calls below.
2382   assert(R16_thread->is_nonvolatile(), "thread must be in non-volatile register");
2383 
2384 # if 0
2385   // DTrace method entry
2386 # endif
2387 
2388   // Lock a synchronized method.
2389   // --------------------------------------------------------------------------
2390 
2391   if (method->is_synchronized()) {
2392     Register          r_oop  = r_temp_4;
2393     const Register    r_box  = r_temp_5;
2394     Label             done, locked;
2395 
2396     // Load the oop for the object or class. r_carg2_classorobject contains
2397     // either the handlized oop from the incoming arguments or the handlized
2398     // class mirror (if the method is static).
2399     __ ld(r_oop, 0, r_carg2_classorobject);
2400 
2401     // Get the lock box slot's address.
2402     __ addi(r_box, R1_SP, lock_offset);
2403 
2404     // Try fastpath for locking.
2405     if (LockingMode == LM_LIGHTWEIGHT) {
2406       // fast_lock kills r_temp_1, r_temp_2, r_temp_3.
2407       Register r_temp_3_or_noreg = UseObjectMonitorTable ? r_temp_3 : noreg;
2408       __ compiler_fast_lock_lightweight_object(CCR0, r_oop, r_box, r_temp_1, r_temp_2, r_temp_3_or_noreg);
2409     } else {
2410       // fast_lock kills r_temp_1, r_temp_2, r_temp_3.
2411       __ compiler_fast_lock_object(CCR0, r_oop, r_box, r_temp_1, r_temp_2, r_temp_3);
2412     }
2413     __ beq(CCR0, locked);
2414 
2415     // None of the above fast optimizations worked so we have to get into the
2416     // slow case of monitor enter. Inline a special case of call_VM that
2417     // disallows any pending_exception.
2418 
2419     // Save argument registers and leave room for C-compatible ABI_REG_ARGS.
2420     int frame_size = frame::native_abi_reg_args_size + align_up(total_c_args * wordSize, frame::alignment_in_bytes);
2421     __ mr(R11_scratch1, R1_SP);
2422     RegisterSaver::push_frame_and_save_argument_registers(masm, R12_scratch2, frame_size, total_c_args, out_regs);
2423 
2424     // Do the call.
2425     __ set_last_Java_frame(R11_scratch1, r_last_java_pc);
2426     assert(r_last_java_pc->is_nonvolatile(), "r_last_java_pc needs to be preserved accross complete_monitor_locking_C call");
2427     // The following call will not be preempted.
2428     // push_cont_fastpath forces freeze slow path in case we try to preempt where we will pin the
2429     // vthread to the carrier (see FreezeBase::recurse_freeze_native_frame()).
2430     __ push_cont_fastpath();
2431     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_locking_C), r_oop, r_box, R16_thread);
2432     __ pop_cont_fastpath();
2433     __ reset_last_Java_frame();
2434 
2435     RegisterSaver::restore_argument_registers_and_pop_frame(masm, frame_size, total_c_args, out_regs);
2436 
2437     __ asm_assert_mem8_is_zero(thread_(pending_exception),
2438        "no pending exception allowed on exit from SharedRuntime::complete_monitor_locking_C");
2439 
2440     __ bind(locked);
2441   }
2442 
2443   __ set_last_Java_frame(R1_SP, r_last_java_pc);
2444 
2445   // Publish thread state
2446   // --------------------------------------------------------------------------
2447 
2448   // Transition from _thread_in_Java to _thread_in_native.
2449   __ li(R0, _thread_in_native);
2450   __ release();
2451   // TODO: PPC port assert(4 == JavaThread::sz_thread_state(), "unexpected field size");
2452   __ stw(R0, thread_(thread_state));
2453 
2454 
2455   // The JNI call
2456   // --------------------------------------------------------------------------
2457   __ call_c(native_func, relocInfo::runtime_call_type);
2458 
2459 
2460   // Now, we are back from the native code.
2461 
2462 
2463   // Unpack the native result.
2464   // --------------------------------------------------------------------------
2465 
2466   // For int-types, we do any needed sign-extension required.
2467   // Care must be taken that the return values (R3_RET and F1_RET)
2468   // will survive any VM calls for blocking or unlocking.
2469   // An OOP result (handle) is done specially in the slow-path code.
2470 
2471   switch (ret_type) {
2472     case T_VOID:    break;        // Nothing to do!
2473     case T_FLOAT:   break;        // Got it where we want it (unless slow-path).
2474     case T_DOUBLE:  break;        // Got it where we want it (unless slow-path).
2475     case T_LONG:    break;        // Got it where we want it (unless slow-path).
2476     case T_OBJECT:  break;        // Really a handle.
2477                                   // Cannot de-handlize until after reclaiming jvm_lock.
2478     case T_ARRAY:   break;
2479 
2480     case T_BOOLEAN: {             // 0 -> false(0); !0 -> true(1)
2481       __ normalize_bool(R3_RET);
2482       break;
2483       }
2484     case T_BYTE: {                // sign extension
2485       __ extsb(R3_RET, R3_RET);
2486       break;
2487       }
2488     case T_CHAR: {                // unsigned result
2489       __ andi(R3_RET, R3_RET, 0xffff);
2490       break;
2491       }
2492     case T_SHORT: {               // sign extension
2493       __ extsh(R3_RET, R3_RET);
2494       break;
2495       }
2496     case T_INT:                   // nothing to do
2497       break;
2498     default:
2499       ShouldNotReachHere();
2500       break;
2501   }
2502 
2503   // Publish thread state
2504   // --------------------------------------------------------------------------
2505 
2506   // Switch thread to "native transition" state before reading the
2507   // synchronization state. This additional state is necessary because reading
2508   // and testing the synchronization state is not atomic w.r.t. GC, as this
2509   // scenario demonstrates:
2510   //   - Java thread A, in _thread_in_native state, loads _not_synchronized
2511   //     and is preempted.
2512   //   - VM thread changes sync state to synchronizing and suspends threads
2513   //     for GC.
2514   //   - Thread A is resumed to finish this native method, but doesn't block
2515   //     here since it didn't see any synchronization in progress, and escapes.
2516 
2517   // Transition from _thread_in_native to _thread_in_native_trans.
2518   __ li(R0, _thread_in_native_trans);
2519   __ release();
2520   // TODO: PPC port assert(4 == JavaThread::sz_thread_state(), "unexpected field size");
2521   __ stw(R0, thread_(thread_state));
2522 
2523 
2524   // Must we block?
2525   // --------------------------------------------------------------------------
2526 
2527   // Block, if necessary, before resuming in _thread_in_Java state.
2528   // In order for GC to work, don't clear the last_Java_sp until after blocking.
2529   {
2530     Label no_block, sync;
2531 
2532     // Force this write out before the read below.
2533     if (!UseSystemMemoryBarrier) {
2534       __ fence();
2535     }
2536 
2537     Register sync_state_addr = r_temp_4;
2538     Register sync_state      = r_temp_5;
2539     Register suspend_flags   = r_temp_6;
2540 
2541     // No synchronization in progress nor yet synchronized
2542     // (cmp-br-isync on one path, release (same as acquire on PPC64) on the other path).
2543     __ safepoint_poll(sync, sync_state, true /* at_return */, false /* in_nmethod */);
2544 
2545     // Not suspended.
2546     // TODO: PPC port assert(4 == Thread::sz_suspend_flags(), "unexpected field size");
2547     __ lwz(suspend_flags, thread_(suspend_flags));
2548     __ cmpwi(CCR1, suspend_flags, 0);
2549     __ beq(CCR1, no_block);
2550 
2551     // Block. Save any potential method result value before the operation and
2552     // use a leaf call to leave the last_Java_frame setup undisturbed. Doing this
2553     // lets us share the oopMap we used when we went native rather than create
2554     // a distinct one for this pc.
2555     __ bind(sync);
2556     __ isync();
2557 
2558     address entry_point =
2559       CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans);
2560     save_native_result(masm, ret_type, workspace_slot_offset);
2561     __ call_VM_leaf(entry_point, R16_thread);
2562     restore_native_result(masm, ret_type, workspace_slot_offset);
2563 
2564     __ bind(no_block);
2565 
2566     // Publish thread state.
2567     // --------------------------------------------------------------------------
2568 
2569     // Thread state is thread_in_native_trans. Any safepoint blocking has
2570     // already happened so we can now change state to _thread_in_Java.
2571 
2572     // Transition from _thread_in_native_trans to _thread_in_Java.
2573     __ li(R0, _thread_in_Java);
2574     __ lwsync(); // Acquire safepoint and suspend state, release thread state.
2575     // TODO: PPC port assert(4 == JavaThread::sz_thread_state(), "unexpected field size");
2576     __ stw(R0, thread_(thread_state));
2577 
2578     // Check preemption for Object.wait()
2579     if (LockingMode != LM_LEGACY && method->is_object_wait0()) {
2580       Label not_preempted;
2581       __ ld(R0, in_bytes(JavaThread::preempt_alternate_return_offset()), R16_thread);
2582       __ cmpdi(CCR0, R0, 0);
2583       __ beq(CCR0, not_preempted);
2584       __ mtlr(R0);
2585       __ li(R0, 0);
2586       __ std(R0, in_bytes(JavaThread::preempt_alternate_return_offset()), R16_thread);
2587       __ blr();
2588       __ bind(not_preempted);
2589     }
2590     __ bind(last_java_pc);
2591     // We use the same pc/oopMap repeatedly when we call out above.
2592     intptr_t oopmap_pc = (intptr_t) __ pc();
2593     oop_maps->add_gc_map(oopmap_pc - start_pc, oop_map);
2594   }
2595 
2596   // Reguard any pages if necessary.
2597   // --------------------------------------------------------------------------
2598 
2599   Label no_reguard;
2600   __ lwz(r_temp_1, thread_(stack_guard_state));
2601   __ cmpwi(CCR0, r_temp_1, StackOverflow::stack_guard_yellow_reserved_disabled);
2602   __ bne(CCR0, no_reguard);
2603 
2604   save_native_result(masm, ret_type, workspace_slot_offset);
2605   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages));
2606   restore_native_result(masm, ret_type, workspace_slot_offset);
2607 
2608   __ bind(no_reguard);
2609 
2610 
2611   // Unlock
2612   // --------------------------------------------------------------------------
2613 
2614   if (method->is_synchronized()) {
2615     const Register r_oop       = r_temp_4;
2616     const Register r_box       = r_temp_5;
2617     const Register r_exception = r_temp_6;
2618     Label done;
2619 
2620     // Get oop and address of lock object box.
2621     if (method_is_static) {
2622       assert(klass_offset != -1, "");
2623       __ ld(r_oop, klass_offset, R1_SP);
2624     } else {
2625       assert(receiver_offset != -1, "");
2626       __ ld(r_oop, receiver_offset, R1_SP);
2627     }
2628     __ addi(r_box, R1_SP, lock_offset);
2629 
2630     // Try fastpath for unlocking.
2631     if (LockingMode == LM_LIGHTWEIGHT) {
2632       __ compiler_fast_unlock_lightweight_object(CCR0, r_oop, r_box, r_temp_1, r_temp_2, r_temp_3);
2633     } else {
2634       __ compiler_fast_unlock_object(CCR0, r_oop, r_box, r_temp_1, r_temp_2, r_temp_3);
2635     }
2636     __ beq(CCR0, done);
2637 
2638     // Save and restore any potential method result value around the unlocking operation.
2639     save_native_result(masm, ret_type, workspace_slot_offset);
2640 
2641     // Must save pending exception around the slow-path VM call. Since it's a
2642     // leaf call, the pending exception (if any) can be kept in a register.
2643     __ ld(r_exception, thread_(pending_exception));
2644     assert(r_exception->is_nonvolatile(), "exception register must be non-volatile");
2645     __ li(R0, 0);
2646     __ std(R0, thread_(pending_exception));
2647 
2648     // Slow case of monitor enter.
2649     // Inline a special case of call_VM that disallows any pending_exception.
2650     // Arguments are (oop obj, BasicLock* lock, JavaThread* thread).
2651     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_unlocking_C), r_oop, r_box, R16_thread);
2652 
2653     __ asm_assert_mem8_is_zero(thread_(pending_exception),
2654        "no pending exception allowed on exit from SharedRuntime::complete_monitor_unlocking_C");
2655 
2656     restore_native_result(masm, ret_type, workspace_slot_offset);
2657 
2658     // Check_forward_pending_exception jump to forward_exception if any pending
2659     // exception is set. The forward_exception routine expects to see the
2660     // exception in pending_exception and not in a register. Kind of clumsy,
2661     // since all folks who branch to forward_exception must have tested
2662     // pending_exception first and hence have it in a register already.
2663     __ std(r_exception, thread_(pending_exception));
2664 
2665     __ bind(done);
2666   }
2667 
2668 # if 0
2669   // DTrace method exit
2670 # endif
2671 
2672   // Clear "last Java frame" SP and PC.
2673   // --------------------------------------------------------------------------
2674 
2675   // Last java frame won't be set if we're resuming after preemption
2676   bool maybe_preempted = LockingMode != LM_LEGACY && method->is_object_wait0();
2677   __ reset_last_Java_frame(!maybe_preempted /* check_last_java_sp */);
2678 
2679   // Unbox oop result, e.g. JNIHandles::resolve value.
2680   // --------------------------------------------------------------------------
2681 
2682   if (is_reference_type(ret_type)) {
2683     __ resolve_jobject(R3_RET, r_temp_1, r_temp_2, MacroAssembler::PRESERVATION_NONE);
2684   }
2685 
2686   if (CheckJNICalls) {
2687     // clear_pending_jni_exception_check
2688     __ load_const_optimized(R0, 0L);
2689     __ st_ptr(R0, JavaThread::pending_jni_exception_check_fn_offset(), R16_thread);
2690   }
2691 
2692   // Reset handle block.
2693   // --------------------------------------------------------------------------
2694   __ ld(r_temp_1, thread_(active_handles));
2695   // TODO: PPC port assert(4 == JNIHandleBlock::top_size_in_bytes(), "unexpected field size");
2696   __ li(r_temp_2, 0);
2697   __ stw(r_temp_2, in_bytes(JNIHandleBlock::top_offset()), r_temp_1);
2698 
2699 
2700   // Check for pending exceptions.
2701   // --------------------------------------------------------------------------
2702   __ ld(r_temp_2, thread_(pending_exception));
2703   __ cmpdi(CCR0, r_temp_2, 0);
2704   __ bne(CCR0, handle_pending_exception);
2705 
2706   // Return
2707   // --------------------------------------------------------------------------
2708 
2709   __ pop_frame();
2710   __ restore_LR(R11);
2711   __ blr();
2712 
2713 
2714   // Handler for pending exceptions (out-of-line).
2715   // --------------------------------------------------------------------------
2716   // Since this is a native call, we know the proper exception handler
2717   // is the empty function. We just pop this frame and then jump to
2718   // forward_exception_entry.
2719   __ bind(handle_pending_exception);
2720 
2721   __ pop_frame();
2722   __ restore_LR(R11);
2723   __ b64_patchable((address)StubRoutines::forward_exception_entry(),
2724                        relocInfo::runtime_call_type);
2725 
2726   // Done.
2727   // --------------------------------------------------------------------------
2728 
2729   __ flush();
2730 
2731   nmethod *nm = nmethod::new_native_nmethod(method,
2732                                             compile_id,
2733                                             masm->code(),
2734                                             vep_start_pc-start_pc,
2735                                             frame_done_pc-start_pc,
2736                                             stack_slots / VMRegImpl::slots_per_word,
2737                                             (method_is_static ? in_ByteSize(klass_offset) : in_ByteSize(receiver_offset)),
2738                                             in_ByteSize(lock_offset),
2739                                             oop_maps);
2740 
2741   return nm;
2742 }
2743 
2744 // This function returns the adjust size (in number of words) to a c2i adapter
2745 // activation for use during deoptimization.
2746 int Deoptimization::last_frame_adjust(int callee_parameters, int callee_locals) {
2747   return align_up((callee_locals - callee_parameters) * Interpreter::stackElementWords, frame::frame_alignment_in_words);
2748 }
2749 
2750 uint SharedRuntime::in_preserve_stack_slots() {
2751   return frame::jit_in_preserve_size / VMRegImpl::stack_slot_size;
2752 }
2753 
2754 uint SharedRuntime::out_preserve_stack_slots() {
2755 #if defined(COMPILER1) || defined(COMPILER2)
2756   return frame::jit_out_preserve_size / VMRegImpl::stack_slot_size;
2757 #else
2758   return 0;
2759 #endif
2760 }
2761 
2762 VMReg SharedRuntime::thread_register() {
2763   // On PPC virtual threads don't save the JavaThread* in their context (e.g. C1 stub frames).
2764   ShouldNotCallThis();
2765   return nullptr;
2766 }
2767 
2768 #if defined(COMPILER1) || defined(COMPILER2)
2769 // Frame generation for deopt and uncommon trap blobs.
2770 static void push_skeleton_frame(MacroAssembler* masm, bool deopt,
2771                                 /* Read */
2772                                 Register unroll_block_reg,
2773                                 /* Update */
2774                                 Register frame_sizes_reg,
2775                                 Register number_of_frames_reg,
2776                                 Register pcs_reg,
2777                                 /* Invalidate */
2778                                 Register frame_size_reg,
2779                                 Register pc_reg) {
2780 
2781   __ ld(pc_reg, 0, pcs_reg);
2782   __ ld(frame_size_reg, 0, frame_sizes_reg);
2783   __ std(pc_reg, _abi0(lr), R1_SP);
2784   __ push_frame(frame_size_reg, R0/*tmp*/);
2785   __ std(R1_SP, _ijava_state_neg(sender_sp), R1_SP);
2786   __ addi(number_of_frames_reg, number_of_frames_reg, -1);
2787   __ addi(frame_sizes_reg, frame_sizes_reg, wordSize);
2788   __ addi(pcs_reg, pcs_reg, wordSize);
2789 }
2790 
2791 // Loop through the UnrollBlock info and create new frames.
2792 static void push_skeleton_frames(MacroAssembler* masm, bool deopt,
2793                                  /* read */
2794                                  Register unroll_block_reg,
2795                                  /* invalidate */
2796                                  Register frame_sizes_reg,
2797                                  Register number_of_frames_reg,
2798                                  Register pcs_reg,
2799                                  Register frame_size_reg,
2800                                  Register pc_reg) {
2801   Label loop;
2802 
2803  // _number_of_frames is of type int (deoptimization.hpp)
2804   __ lwa(number_of_frames_reg,
2805              in_bytes(Deoptimization::UnrollBlock::number_of_frames_offset()),
2806              unroll_block_reg);
2807   __ ld(pcs_reg,
2808             in_bytes(Deoptimization::UnrollBlock::frame_pcs_offset()),
2809             unroll_block_reg);
2810   __ ld(frame_sizes_reg,
2811             in_bytes(Deoptimization::UnrollBlock::frame_sizes_offset()),
2812             unroll_block_reg);
2813 
2814   // stack: (caller_of_deoptee, ...).
2815 
2816   // At this point we either have an interpreter frame or a compiled
2817   // frame on top of stack. If it is a compiled frame we push a new c2i
2818   // adapter here
2819 
2820   // Memorize top-frame stack-pointer.
2821   __ mr(frame_size_reg/*old_sp*/, R1_SP);
2822 
2823   // Resize interpreter top frame OR C2I adapter.
2824 
2825   // At this moment, the top frame (which is the caller of the deoptee) is
2826   // an interpreter frame or a newly pushed C2I adapter or an entry frame.
2827   // The top frame has a TOP_IJAVA_FRAME_ABI and the frame contains the
2828   // outgoing arguments.
2829   //
2830   // In order to push the interpreter frame for the deoptee, we need to
2831   // resize the top frame such that we are able to place the deoptee's
2832   // locals in the frame.
2833   // Additionally, we have to turn the top frame's TOP_IJAVA_FRAME_ABI
2834   // into a valid PARENT_IJAVA_FRAME_ABI.
2835 
2836   __ lwa(R11_scratch1,
2837              in_bytes(Deoptimization::UnrollBlock::caller_adjustment_offset()),
2838              unroll_block_reg);
2839   __ neg(R11_scratch1, R11_scratch1);
2840 
2841   // R11_scratch1 contains size of locals for frame resizing.
2842   // R12_scratch2 contains top frame's lr.
2843 
2844   // Resize frame by complete frame size prevents TOC from being
2845   // overwritten by locals. A more stack space saving way would be
2846   // to copy the TOC to its location in the new abi.
2847   __ addi(R11_scratch1, R11_scratch1, - frame::parent_ijava_frame_abi_size);
2848 
2849   // now, resize the frame
2850   __ resize_frame(R11_scratch1, pc_reg/*tmp*/);
2851 
2852   // In the case where we have resized a c2i frame above, the optional
2853   // alignment below the locals has size 32 (why?).
2854   __ std(R12_scratch2, _abi0(lr), R1_SP);
2855 
2856   // Initialize initial_caller_sp.
2857  __ std(frame_size_reg, _ijava_state_neg(sender_sp), R1_SP);
2858 
2859 #ifdef ASSERT
2860   // Make sure that there is at least one entry in the array.
2861   __ cmpdi(CCR0, number_of_frames_reg, 0);
2862   __ asm_assert_ne("array_size must be > 0");
2863 #endif
2864 
2865   // Now push the new interpreter frames.
2866   //
2867   __ bind(loop);
2868   // Allocate a new frame, fill in the pc.
2869   push_skeleton_frame(masm, deopt,
2870                       unroll_block_reg,
2871                       frame_sizes_reg,
2872                       number_of_frames_reg,
2873                       pcs_reg,
2874                       frame_size_reg,
2875                       pc_reg);
2876   __ cmpdi(CCR0, number_of_frames_reg, 0);
2877   __ bne(CCR0, loop);
2878 
2879   // Get the return address pointing into the frame manager.
2880   __ ld(R0, 0, pcs_reg);
2881   // Store it in the top interpreter frame.
2882   __ std(R0, _abi0(lr), R1_SP);
2883   // Initialize frame_manager_lr of interpreter top frame.
2884 }
2885 #endif
2886 
2887 void SharedRuntime::generate_deopt_blob() {
2888   // Allocate space for the code
2889   ResourceMark rm;
2890   // Setup code generation tools
2891   const char* name = SharedRuntime::stub_name(SharedStubId::deopt_id);
2892   CodeBuffer buffer(name, 2048, 1024);
2893   InterpreterMacroAssembler* masm = new InterpreterMacroAssembler(&buffer);
2894   Label exec_mode_initialized;
2895   int frame_size_in_words;
2896   OopMap* map = nullptr;
2897   OopMapSet *oop_maps = new OopMapSet();
2898 
2899   // size of ABI112 plus spill slots for R3_RET and F1_RET.
2900   const int frame_size_in_bytes = frame::native_abi_reg_args_spill_size;
2901   const int frame_size_in_slots = frame_size_in_bytes / sizeof(jint);
2902   int first_frame_size_in_bytes = 0; // frame size of "unpack frame" for call to fetch_unroll_info.
2903 
2904   const Register exec_mode_reg = R21_tmp1;
2905 
2906   const address start = __ pc();
2907 
2908 #if defined(COMPILER1) || defined(COMPILER2)
2909   // --------------------------------------------------------------------------
2910   // Prolog for non exception case!
2911 
2912   // We have been called from the deopt handler of the deoptee.
2913   //
2914   // deoptee:
2915   //                      ...
2916   //                      call X
2917   //                      ...
2918   //  deopt_handler:      call_deopt_stub
2919   //  cur. return pc  --> ...
2920   //
2921   // So currently SR_LR points behind the call in the deopt handler.
2922   // We adjust it such that it points to the start of the deopt handler.
2923   // The return_pc has been stored in the frame of the deoptee and
2924   // will replace the address of the deopt_handler in the call
2925   // to Deoptimization::fetch_unroll_info below.
2926   // We can't grab a free register here, because all registers may
2927   // contain live values, so let the RegisterSaver do the adjustment
2928   // of the return pc.
2929   const int return_pc_adjustment_no_exception = -MacroAssembler::bl64_patchable_size;
2930 
2931   // Push the "unpack frame"
2932   // Save everything in sight.
2933   map = RegisterSaver::push_frame_reg_args_and_save_live_registers(masm,
2934                                                                    &first_frame_size_in_bytes,
2935                                                                    /*generate_oop_map=*/ true,
2936                                                                    return_pc_adjustment_no_exception,
2937                                                                    RegisterSaver::return_pc_is_lr);
2938   assert(map != nullptr, "OopMap must have been created");
2939 
2940   __ li(exec_mode_reg, Deoptimization::Unpack_deopt);
2941   // Save exec mode for unpack_frames.
2942   __ b(exec_mode_initialized);
2943 
2944   // --------------------------------------------------------------------------
2945   // Prolog for exception case
2946 
2947   // An exception is pending.
2948   // We have been called with a return (interpreter) or a jump (exception blob).
2949   //
2950   // - R3_ARG1: exception oop
2951   // - R4_ARG2: exception pc
2952 
2953   int exception_offset = __ pc() - start;
2954 
2955   BLOCK_COMMENT("Prolog for exception case");
2956 
2957   // Store exception oop and pc in thread (location known to GC).
2958   // This is needed since the call to "fetch_unroll_info()" may safepoint.
2959   __ std(R3_ARG1, in_bytes(JavaThread::exception_oop_offset()), R16_thread);
2960   __ std(R4_ARG2, in_bytes(JavaThread::exception_pc_offset()),  R16_thread);
2961   __ std(R4_ARG2, _abi0(lr), R1_SP);
2962 
2963   // Vanilla deoptimization with an exception pending in exception_oop.
2964   int exception_in_tls_offset = __ pc() - start;
2965 
2966   // Push the "unpack frame".
2967   // Save everything in sight.
2968   RegisterSaver::push_frame_reg_args_and_save_live_registers(masm,
2969                                                              &first_frame_size_in_bytes,
2970                                                              /*generate_oop_map=*/ false,
2971                                                              /*return_pc_adjustment_exception=*/ 0,
2972                                                              RegisterSaver::return_pc_is_pre_saved);
2973 
2974   // Deopt during an exception. Save exec mode for unpack_frames.
2975   __ li(exec_mode_reg, Deoptimization::Unpack_exception);
2976 
2977   // fall through
2978 
2979   int reexecute_offset = 0;
2980 #ifdef COMPILER1
2981   __ b(exec_mode_initialized);
2982 
2983   // Reexecute entry, similar to c2 uncommon trap
2984   reexecute_offset = __ pc() - start;
2985 
2986   RegisterSaver::push_frame_reg_args_and_save_live_registers(masm,
2987                                                              &first_frame_size_in_bytes,
2988                                                              /*generate_oop_map=*/ false,
2989                                                              /*return_pc_adjustment_reexecute=*/ 0,
2990                                                              RegisterSaver::return_pc_is_pre_saved);
2991   __ li(exec_mode_reg, Deoptimization::Unpack_reexecute);
2992 #endif
2993 
2994   // --------------------------------------------------------------------------
2995   __ BIND(exec_mode_initialized);
2996 
2997   const Register unroll_block_reg = R22_tmp2;
2998 
2999   // We need to set `last_Java_frame' because `fetch_unroll_info' will
3000   // call `last_Java_frame()'. The value of the pc in the frame is not
3001   // particularly important. It just needs to identify this blob.
3002   __ set_last_Java_frame(R1_SP, noreg);
3003 
3004   // With EscapeAnalysis turned on, this call may safepoint!
3005   __ call_VM_leaf(CAST_FROM_FN_PTR(address, Deoptimization::fetch_unroll_info), R16_thread, exec_mode_reg);
3006   address calls_return_pc = __ last_calls_return_pc();
3007   // Set an oopmap for the call site that describes all our saved registers.
3008   oop_maps->add_gc_map(calls_return_pc - start, map);
3009 
3010   __ reset_last_Java_frame();
3011   // Save the return value.
3012   __ mr(unroll_block_reg, R3_RET);
3013 
3014   // Restore only the result registers that have been saved
3015   // by save_volatile_registers(...).
3016   RegisterSaver::restore_result_registers(masm, first_frame_size_in_bytes);
3017 
3018   // reload the exec mode from the UnrollBlock (it might have changed)
3019   __ lwz(exec_mode_reg, in_bytes(Deoptimization::UnrollBlock::unpack_kind_offset()), unroll_block_reg);
3020   // In excp_deopt_mode, restore and clear exception oop which we
3021   // stored in the thread during exception entry above. The exception
3022   // oop will be the return value of this stub.
3023   Label skip_restore_excp;
3024   __ cmpdi(CCR0, exec_mode_reg, Deoptimization::Unpack_exception);
3025   __ bne(CCR0, skip_restore_excp);
3026   __ ld(R3_RET, in_bytes(JavaThread::exception_oop_offset()), R16_thread);
3027   __ ld(R4_ARG2, in_bytes(JavaThread::exception_pc_offset()), R16_thread);
3028   __ li(R0, 0);
3029   __ std(R0, in_bytes(JavaThread::exception_pc_offset()),  R16_thread);
3030   __ std(R0, in_bytes(JavaThread::exception_oop_offset()), R16_thread);
3031   __ BIND(skip_restore_excp);
3032 
3033   __ pop_frame();
3034 
3035   // stack: (deoptee, optional i2c, caller of deoptee, ...).
3036 
3037   // pop the deoptee's frame
3038   __ pop_frame();
3039 
3040   // stack: (caller_of_deoptee, ...).
3041 
3042   // Freezing continuation frames requires that the caller is trimmed to unextended sp if compiled.
3043   // If not compiled the loaded value is equal to the current SP (see frame::initial_deoptimization_info())
3044   // and the frame is effectively not resized.
3045   Register caller_sp = R23_tmp3;
3046   __ ld_ptr(caller_sp, Deoptimization::UnrollBlock::initial_info_offset(), unroll_block_reg);
3047   __ resize_frame_absolute(caller_sp, R24_tmp4, R25_tmp5);
3048 
3049   // Loop through the `UnrollBlock' info and create interpreter frames.
3050   push_skeleton_frames(masm, true/*deopt*/,
3051                        unroll_block_reg,
3052                        R23_tmp3,
3053                        R24_tmp4,
3054                        R25_tmp5,
3055                        R26_tmp6,
3056                        R27_tmp7);
3057 
3058   // stack: (skeletal interpreter frame, ..., optional skeletal
3059   // interpreter frame, optional c2i, caller of deoptee, ...).
3060 
3061   // push an `unpack_frame' taking care of float / int return values.
3062   __ push_frame(frame_size_in_bytes, R0/*tmp*/);
3063 
3064   // stack: (unpack frame, skeletal interpreter frame, ..., optional
3065   // skeletal interpreter frame, optional c2i, caller of deoptee,
3066   // ...).
3067 
3068   // Spill live volatile registers since we'll do a call.
3069   __ std( R3_RET, _native_abi_reg_args_spill(spill_ret),  R1_SP);
3070   __ stfd(F1_RET, _native_abi_reg_args_spill(spill_fret), R1_SP);
3071 
3072   // Let the unpacker layout information in the skeletal frames just
3073   // allocated.
3074   __ calculate_address_from_global_toc(R3_RET, calls_return_pc, true, true, true, true);
3075   __ set_last_Java_frame(/*sp*/R1_SP, /*pc*/R3_RET);
3076   // This is a call to a LEAF method, so no oop map is required.
3077   __ call_VM_leaf(CAST_FROM_FN_PTR(address, Deoptimization::unpack_frames),
3078                   R16_thread/*thread*/, exec_mode_reg/*exec_mode*/);
3079   __ reset_last_Java_frame();
3080 
3081   // Restore the volatiles saved above.
3082   __ ld( R3_RET, _native_abi_reg_args_spill(spill_ret),  R1_SP);
3083   __ lfd(F1_RET, _native_abi_reg_args_spill(spill_fret), R1_SP);
3084 
3085   // Pop the unpack frame.
3086   __ pop_frame();
3087   __ restore_LR(R0);
3088 
3089   // stack: (top interpreter frame, ..., optional interpreter frame,
3090   // optional c2i, caller of deoptee, ...).
3091 
3092   // Initialize R14_state.
3093   __ restore_interpreter_state(R11_scratch1);
3094   __ load_const_optimized(R25_templateTableBase, (address)Interpreter::dispatch_table((TosState)0), R11_scratch1);
3095 
3096   // Return to the interpreter entry point.
3097   __ blr();
3098   __ flush();
3099 #else // COMPILER2
3100   __ unimplemented("deopt blob needed only with compiler");
3101   int exception_offset = __ pc() - start;
3102 #endif // COMPILER2
3103 
3104   _deopt_blob = DeoptimizationBlob::create(&buffer, oop_maps, 0, exception_offset,
3105                                            reexecute_offset, first_frame_size_in_bytes / wordSize);
3106   _deopt_blob->set_unpack_with_exception_in_tls_offset(exception_in_tls_offset);
3107 }
3108 
3109 #ifdef COMPILER2
3110 void OptoRuntime::generate_uncommon_trap_blob() {
3111   // Allocate space for the code.
3112   ResourceMark rm;
3113   // Setup code generation tools.
3114   CodeBuffer buffer("uncommon_trap_blob", 2048, 1024);
3115   InterpreterMacroAssembler* masm = new InterpreterMacroAssembler(&buffer);
3116   address start = __ pc();
3117 
3118   Register unroll_block_reg = R21_tmp1;
3119   Register klass_index_reg  = R22_tmp2;
3120   Register unc_trap_reg     = R23_tmp3;
3121   Register r_return_pc      = R27_tmp7;
3122 
3123   OopMapSet* oop_maps = new OopMapSet();
3124   int frame_size_in_bytes = frame::native_abi_reg_args_size;
3125   OopMap* map = new OopMap(frame_size_in_bytes / sizeof(jint), 0);
3126 
3127   // stack: (deoptee, optional i2c, caller_of_deoptee, ...).
3128 
3129   // Push a dummy `unpack_frame' and call
3130   // `Deoptimization::uncommon_trap' to pack the compiled frame into a
3131   // vframe array and return the `UnrollBlock' information.
3132 
3133   // Save LR to compiled frame.
3134   __ save_LR(R11_scratch1);
3135 
3136   // Push an "uncommon_trap" frame.
3137   __ push_frame_reg_args(0, R11_scratch1);
3138 
3139   // stack: (unpack frame, deoptee, optional i2c, caller_of_deoptee, ...).
3140 
3141   // Set the `unpack_frame' as last_Java_frame.
3142   // `Deoptimization::uncommon_trap' expects it and considers its
3143   // sender frame as the deoptee frame.
3144   // Remember the offset of the instruction whose address will be
3145   // moved to R11_scratch1.
3146   address gc_map_pc = __ pc();
3147   __ calculate_address_from_global_toc(r_return_pc, gc_map_pc, true, true, true, true);
3148   __ set_last_Java_frame(/*sp*/R1_SP, r_return_pc);
3149 
3150   __ mr(klass_index_reg, R3);
3151   __ li(R5_ARG3, Deoptimization::Unpack_uncommon_trap);
3152   __ call_VM_leaf(CAST_FROM_FN_PTR(address, Deoptimization::uncommon_trap),
3153                   R16_thread, klass_index_reg, R5_ARG3);
3154 
3155   // Set an oopmap for the call site.
3156   oop_maps->add_gc_map(gc_map_pc - start, map);
3157 
3158   __ reset_last_Java_frame();
3159 
3160   // Pop the `unpack frame'.
3161   __ pop_frame();
3162 
3163   // stack: (deoptee, optional i2c, caller_of_deoptee, ...).
3164 
3165   // Save the return value.
3166   __ mr(unroll_block_reg, R3_RET);
3167 
3168   // Pop the uncommon_trap frame.
3169   __ pop_frame();
3170 
3171   // stack: (caller_of_deoptee, ...).
3172 
3173 #ifdef ASSERT
3174   __ lwz(R22_tmp2, in_bytes(Deoptimization::UnrollBlock::unpack_kind_offset()), unroll_block_reg);
3175   __ cmpdi(CCR0, R22_tmp2, (unsigned)Deoptimization::Unpack_uncommon_trap);
3176   __ asm_assert_eq("OptoRuntime::generate_uncommon_trap_blob: expected Unpack_uncommon_trap");
3177 #endif
3178 
3179   // Freezing continuation frames requires that the caller is trimmed to unextended sp if compiled.
3180   // If not compiled the loaded value is equal to the current SP (see frame::initial_deoptimization_info())
3181   // and the frame is effectively not resized.
3182   Register caller_sp = R23_tmp3;
3183   __ ld_ptr(caller_sp, Deoptimization::UnrollBlock::initial_info_offset(), unroll_block_reg);
3184   __ resize_frame_absolute(caller_sp, R24_tmp4, R25_tmp5);
3185 
3186   // Allocate new interpreter frame(s) and possibly a c2i adapter
3187   // frame.
3188   push_skeleton_frames(masm, false/*deopt*/,
3189                        unroll_block_reg,
3190                        R22_tmp2,
3191                        R23_tmp3,
3192                        R24_tmp4,
3193                        R25_tmp5,
3194                        R26_tmp6);
3195 
3196   // stack: (skeletal interpreter frame, ..., optional skeletal
3197   // interpreter frame, optional c2i, caller of deoptee, ...).
3198 
3199   // Push a dummy `unpack_frame' taking care of float return values.
3200   // Call `Deoptimization::unpack_frames' to layout information in the
3201   // interpreter frames just created.
3202 
3203   // Push a simple "unpack frame" here.
3204   __ push_frame_reg_args(0, R11_scratch1);
3205 
3206   // stack: (unpack frame, skeletal interpreter frame, ..., optional
3207   // skeletal interpreter frame, optional c2i, caller of deoptee,
3208   // ...).
3209 
3210   // Set the "unpack_frame" as last_Java_frame.
3211   __ set_last_Java_frame(/*sp*/R1_SP, r_return_pc);
3212 
3213   // Indicate it is the uncommon trap case.
3214   __ li(unc_trap_reg, Deoptimization::Unpack_uncommon_trap);
3215   // Let the unpacker layout information in the skeletal frames just
3216   // allocated.
3217   __ call_VM_leaf(CAST_FROM_FN_PTR(address, Deoptimization::unpack_frames),
3218                   R16_thread, unc_trap_reg);
3219 
3220   __ reset_last_Java_frame();
3221   // Pop the `unpack frame'.
3222   __ pop_frame();
3223   // Restore LR from top interpreter frame.
3224   __ restore_LR(R11_scratch1);
3225 
3226   // stack: (top interpreter frame, ..., optional interpreter frame,
3227   // optional c2i, caller of deoptee, ...).
3228 
3229   __ restore_interpreter_state(R11_scratch1);
3230   __ load_const_optimized(R25_templateTableBase, (address)Interpreter::dispatch_table((TosState)0), R11_scratch1);
3231 
3232   // Return to the interpreter entry point.
3233   __ blr();
3234 
3235   masm->flush();
3236 
3237   _uncommon_trap_blob = UncommonTrapBlob::create(&buffer, oop_maps, frame_size_in_bytes/wordSize);
3238 }
3239 #endif // COMPILER2
3240 
3241 // Generate a special Compile2Runtime blob that saves all registers, and setup oopmap.
3242 SafepointBlob* SharedRuntime::generate_handler_blob(SharedStubId id, address call_ptr) {
3243   assert(StubRoutines::forward_exception_entry() != nullptr,
3244          "must be generated before");
3245   assert(is_polling_page_id(id), "expected a polling page stub id");
3246 
3247   ResourceMark rm;
3248   OopMapSet *oop_maps = new OopMapSet();
3249   OopMap* map;
3250 
3251   // Allocate space for the code. Setup code generation tools.
3252   const char* name = SharedRuntime::stub_name(id);
3253   CodeBuffer buffer(name, 2048, 1024);
3254   MacroAssembler* masm = new MacroAssembler(&buffer);
3255 
3256   address start = __ pc();
3257   int frame_size_in_bytes = 0;
3258 
3259   RegisterSaver::ReturnPCLocation return_pc_location;
3260   bool cause_return = (id == SharedStubId::polling_page_return_handler_id);
3261   if (cause_return) {
3262     // Nothing to do here. The frame has already been popped in MachEpilogNode.
3263     // Register LR already contains the return pc.
3264     return_pc_location = RegisterSaver::return_pc_is_pre_saved;
3265   } else {
3266     // Use thread()->saved_exception_pc() as return pc.
3267     return_pc_location = RegisterSaver::return_pc_is_thread_saved_exception_pc;
3268   }
3269 
3270   bool save_vectors = (id == SharedStubId::polling_page_vectors_safepoint_handler_id);
3271 
3272   // Save registers, fpu state, and flags. Set R31 = return pc.
3273   map = RegisterSaver::push_frame_reg_args_and_save_live_registers(masm,
3274                                                                    &frame_size_in_bytes,
3275                                                                    /*generate_oop_map=*/ true,
3276                                                                    /*return_pc_adjustment=*/0,
3277                                                                    return_pc_location, save_vectors);
3278 
3279   // The following is basically a call_VM. However, we need the precise
3280   // address of the call in order to generate an oopmap. Hence, we do all the
3281   // work ourselves.
3282   __ set_last_Java_frame(/*sp=*/R1_SP, /*pc=*/noreg);
3283 
3284   // The return address must always be correct so that the frame constructor
3285   // never sees an invalid pc.
3286 
3287   // Do the call
3288   __ call_VM_leaf(call_ptr, R16_thread);
3289   address calls_return_pc = __ last_calls_return_pc();
3290 
3291   // Set an oopmap for the call site. This oopmap will map all
3292   // oop-registers and debug-info registers as callee-saved. This
3293   // will allow deoptimization at this safepoint to find all possible
3294   // debug-info recordings, as well as let GC find all oops.
3295   oop_maps->add_gc_map(calls_return_pc - start, map);
3296 
3297   Label noException;
3298 
3299   // Clear the last Java frame.
3300   __ reset_last_Java_frame();
3301 
3302   BLOCK_COMMENT("  Check pending exception.");
3303   const Register pending_exception = R0;
3304   __ ld(pending_exception, thread_(pending_exception));
3305   __ cmpdi(CCR0, pending_exception, 0);
3306   __ beq(CCR0, noException);
3307 
3308   // Exception pending
3309   RegisterSaver::restore_live_registers_and_pop_frame(masm,
3310                                                       frame_size_in_bytes,
3311                                                       /*restore_ctr=*/true, save_vectors);
3312 
3313   BLOCK_COMMENT("  Jump to forward_exception_entry.");
3314   // Jump to forward_exception_entry, with the issuing PC in LR
3315   // so it looks like the original nmethod called forward_exception_entry.
3316   __ b64_patchable(StubRoutines::forward_exception_entry(), relocInfo::runtime_call_type);
3317 
3318   // No exception case.
3319   __ BIND(noException);
3320 
3321   if (!cause_return) {
3322     Label no_adjust;
3323     // If our stashed return pc was modified by the runtime we avoid touching it
3324     __ ld(R0, frame_size_in_bytes + _abi0(lr), R1_SP);
3325     __ cmpd(CCR0, R0, R31);
3326     __ bne(CCR0, no_adjust);
3327 
3328     // Adjust return pc forward to step over the safepoint poll instruction
3329     __ addi(R31, R31, 4);
3330     __ std(R31, frame_size_in_bytes + _abi0(lr), R1_SP);
3331 
3332     __ bind(no_adjust);
3333   }
3334 
3335   // Normal exit, restore registers and exit.
3336   RegisterSaver::restore_live_registers_and_pop_frame(masm,
3337                                                       frame_size_in_bytes,
3338                                                       /*restore_ctr=*/true, save_vectors);
3339 
3340   __ blr();
3341 
3342   // Make sure all code is generated
3343   masm->flush();
3344 
3345   // Fill-out other meta info
3346   // CodeBlob frame size is in words.
3347   return SafepointBlob::create(&buffer, oop_maps, frame_size_in_bytes / wordSize);
3348 }
3349 
3350 // generate_resolve_blob - call resolution (static/virtual/opt-virtual/ic-miss)
3351 //
3352 // Generate a stub that calls into the vm to find out the proper destination
3353 // of a java call. All the argument registers are live at this point
3354 // but since this is generic code we don't know what they are and the caller
3355 // must do any gc of the args.
3356 //
3357 RuntimeStub* SharedRuntime::generate_resolve_blob(SharedStubId id, address destination) {
3358   assert(is_resolve_id(id), "expected a resolve stub id");
3359 
3360   // allocate space for the code
3361   ResourceMark rm;
3362 
3363   const char* name = SharedRuntime::stub_name(id);
3364   CodeBuffer buffer(name, 1000, 512);
3365   MacroAssembler* masm = new MacroAssembler(&buffer);
3366 
3367   int frame_size_in_bytes;
3368 
3369   OopMapSet *oop_maps = new OopMapSet();
3370   OopMap* map = nullptr;
3371 
3372   address start = __ pc();
3373 
3374   map = RegisterSaver::push_frame_reg_args_and_save_live_registers(masm,
3375                                                                    &frame_size_in_bytes,
3376                                                                    /*generate_oop_map*/ true,
3377                                                                    /*return_pc_adjustment*/ 0,
3378                                                                    RegisterSaver::return_pc_is_lr);
3379 
3380   // Use noreg as last_Java_pc, the return pc will be reconstructed
3381   // from the physical frame.
3382   __ set_last_Java_frame(/*sp*/R1_SP, noreg);
3383 
3384   int frame_complete = __ offset();
3385 
3386   // Pass R19_method as 2nd (optional) argument, used by
3387   // counter_overflow_stub.
3388   __ call_VM_leaf(destination, R16_thread, R19_method);
3389   address calls_return_pc = __ last_calls_return_pc();
3390   // Set an oopmap for the call site.
3391   // We need this not only for callee-saved registers, but also for volatile
3392   // registers that the compiler might be keeping live across a safepoint.
3393   // Create the oopmap for the call's return pc.
3394   oop_maps->add_gc_map(calls_return_pc - start, map);
3395 
3396   // R3_RET contains the address we are going to jump to assuming no exception got installed.
3397 
3398   // clear last_Java_sp
3399   __ reset_last_Java_frame();
3400 
3401   // Check for pending exceptions.
3402   BLOCK_COMMENT("Check for pending exceptions.");
3403   Label pending;
3404   __ ld(R11_scratch1, thread_(pending_exception));
3405   __ cmpdi(CCR0, R11_scratch1, 0);
3406   __ bne(CCR0, pending);
3407 
3408   __ mtctr(R3_RET); // Ctr will not be touched by restore_live_registers_and_pop_frame.
3409 
3410   RegisterSaver::restore_live_registers_and_pop_frame(masm, frame_size_in_bytes, /*restore_ctr*/ false);
3411 
3412   // Get the returned method.
3413   __ get_vm_result_2(R19_method);
3414 
3415   __ bctr();
3416 
3417 
3418   // Pending exception after the safepoint.
3419   __ BIND(pending);
3420 
3421   RegisterSaver::restore_live_registers_and_pop_frame(masm, frame_size_in_bytes, /*restore_ctr*/ true);
3422 
3423   // exception pending => remove activation and forward to exception handler
3424 
3425   __ li(R11_scratch1, 0);
3426   __ ld(R3_ARG1, thread_(pending_exception));
3427   __ std(R11_scratch1, in_bytes(JavaThread::vm_result_offset()), R16_thread);
3428   __ b64_patchable(StubRoutines::forward_exception_entry(), relocInfo::runtime_call_type);
3429 
3430   // -------------
3431   // Make sure all code is generated.
3432   masm->flush();
3433 
3434   // return the blob
3435   // frame_size_words or bytes??
3436   return RuntimeStub::new_runtime_stub(name, &buffer, frame_complete, frame_size_in_bytes/wordSize,
3437                                        oop_maps, true);
3438 }
3439 
3440 // Continuation point for throwing of implicit exceptions that are
3441 // not handled in the current activation. Fabricates an exception
3442 // oop and initiates normal exception dispatching in this
3443 // frame. Only callee-saved registers are preserved (through the
3444 // normal register window / RegisterMap handling).  If the compiler
3445 // needs all registers to be preserved between the fault point and
3446 // the exception handler then it must assume responsibility for that
3447 // in AbstractCompiler::continuation_for_implicit_null_exception or
3448 // continuation_for_implicit_division_by_zero_exception. All other
3449 // implicit exceptions (e.g., NullPointerException or
3450 // AbstractMethodError on entry) are either at call sites or
3451 // otherwise assume that stack unwinding will be initiated, so
3452 // caller saved registers were assumed volatile in the compiler.
3453 //
3454 // Note that we generate only this stub into a RuntimeStub, because
3455 // it needs to be properly traversed and ignored during GC, so we
3456 // change the meaning of the "__" macro within this method.
3457 //
3458 // Note: the routine set_pc_not_at_call_for_caller in
3459 // SharedRuntime.cpp requires that this code be generated into a
3460 // RuntimeStub.
3461 RuntimeStub* SharedRuntime::generate_throw_exception(SharedStubId id, address runtime_entry) {
3462   assert(is_throw_id(id), "expected a throw stub id");
3463 
3464   const char* name = SharedRuntime::stub_name(id);
3465 
3466   ResourceMark rm;
3467   const char* timer_msg = "SharedRuntime generate_throw_exception";
3468   TraceTime timer(timer_msg, TRACETIME_LOG(Info, startuptime));
3469 
3470   CodeBuffer code(name, 1024 DEBUG_ONLY(+ 512), 0);
3471   MacroAssembler* masm = new MacroAssembler(&code);
3472 
3473   OopMapSet* oop_maps  = new OopMapSet();
3474   int frame_size_in_bytes = frame::native_abi_reg_args_size;
3475   OopMap* map = new OopMap(frame_size_in_bytes / sizeof(jint), 0);
3476 
3477   address start = __ pc();
3478 
3479   __ save_LR(R11_scratch1);
3480 
3481   // Push a frame.
3482   __ push_frame_reg_args(0, R11_scratch1);
3483 
3484   address frame_complete_pc = __ pc();
3485 
3486   // Note that we always have a runtime stub frame on the top of
3487   // stack by this point. Remember the offset of the instruction
3488   // whose address will be moved to R11_scratch1.
3489   address gc_map_pc = __ get_PC_trash_LR(R11_scratch1);
3490 
3491   __ set_last_Java_frame(/*sp*/R1_SP, /*pc*/R11_scratch1);
3492 
3493   __ mr(R3_ARG1, R16_thread);
3494   __ call_c(runtime_entry);
3495 
3496   // Set an oopmap for the call site.
3497   oop_maps->add_gc_map((int)(gc_map_pc - start), map);
3498 
3499   __ reset_last_Java_frame();
3500 
3501 #ifdef ASSERT
3502   // Make sure that this code is only executed if there is a pending
3503   // exception.
3504   {
3505     Label L;
3506     __ ld(R0,
3507           in_bytes(Thread::pending_exception_offset()),
3508           R16_thread);
3509     __ cmpdi(CCR0, R0, 0);
3510     __ bne(CCR0, L);
3511     __ stop("SharedRuntime::throw_exception: no pending exception");
3512     __ bind(L);
3513   }
3514 #endif
3515 
3516   // Pop frame.
3517   __ pop_frame();
3518 
3519   __ restore_LR(R11_scratch1);
3520 
3521   __ load_const(R11_scratch1, StubRoutines::forward_exception_entry());
3522   __ mtctr(R11_scratch1);
3523   __ bctr();
3524 
3525   // Create runtime stub with OopMap.
3526   RuntimeStub* stub =
3527     RuntimeStub::new_runtime_stub(name, &code,
3528                                   /*frame_complete=*/ (int)(frame_complete_pc - start),
3529                                   frame_size_in_bytes/wordSize,
3530                                   oop_maps,
3531                                   false);
3532   return stub;
3533 }
3534 
3535 //------------------------------Montgomery multiplication------------------------
3536 //
3537 
3538 // Subtract 0:b from carry:a. Return carry.
3539 static unsigned long
3540 sub(unsigned long a[], unsigned long b[], unsigned long carry, long len) {
3541   long i = 0;
3542   unsigned long tmp, tmp2;
3543   __asm__ __volatile__ (
3544     "subfc  %[tmp], %[tmp], %[tmp]   \n" // pre-set CA
3545     "mtctr  %[len]                   \n"
3546     "0:                              \n"
3547     "ldx    %[tmp], %[i], %[a]       \n"
3548     "ldx    %[tmp2], %[i], %[b]      \n"
3549     "subfe  %[tmp], %[tmp2], %[tmp]  \n" // subtract extended
3550     "stdx   %[tmp], %[i], %[a]       \n"
3551     "addi   %[i], %[i], 8            \n"
3552     "bdnz   0b                       \n"
3553     "addme  %[tmp], %[carry]         \n" // carry + CA - 1
3554     : [i]"+b"(i), [tmp]"=&r"(tmp), [tmp2]"=&r"(tmp2)
3555     : [a]"r"(a), [b]"r"(b), [carry]"r"(carry), [len]"r"(len)
3556     : "ctr", "xer", "memory"
3557   );
3558   return tmp;
3559 }
3560 
3561 // Multiply (unsigned) Long A by Long B, accumulating the double-
3562 // length result into the accumulator formed of T0, T1, and T2.
3563 inline void MACC(unsigned long A, unsigned long B, unsigned long &T0, unsigned long &T1, unsigned long &T2) {
3564   unsigned long hi, lo;
3565   __asm__ __volatile__ (
3566     "mulld  %[lo], %[A], %[B]    \n"
3567     "mulhdu %[hi], %[A], %[B]    \n"
3568     "addc   %[T0], %[T0], %[lo]  \n"
3569     "adde   %[T1], %[T1], %[hi]  \n"
3570     "addze  %[T2], %[T2]         \n"
3571     : [hi]"=&r"(hi), [lo]"=&r"(lo), [T0]"+r"(T0), [T1]"+r"(T1), [T2]"+r"(T2)
3572     : [A]"r"(A), [B]"r"(B)
3573     : "xer"
3574   );
3575 }
3576 
3577 // As above, but add twice the double-length result into the
3578 // accumulator.
3579 inline void MACC2(unsigned long A, unsigned long B, unsigned long &T0, unsigned long &T1, unsigned long &T2) {
3580   unsigned long hi, lo;
3581   __asm__ __volatile__ (
3582     "mulld  %[lo], %[A], %[B]    \n"
3583     "mulhdu %[hi], %[A], %[B]    \n"
3584     "addc   %[T0], %[T0], %[lo]  \n"
3585     "adde   %[T1], %[T1], %[hi]  \n"
3586     "addze  %[T2], %[T2]         \n"
3587     "addc   %[T0], %[T0], %[lo]  \n"
3588     "adde   %[T1], %[T1], %[hi]  \n"
3589     "addze  %[T2], %[T2]         \n"
3590     : [hi]"=&r"(hi), [lo]"=&r"(lo), [T0]"+r"(T0), [T1]"+r"(T1), [T2]"+r"(T2)
3591     : [A]"r"(A), [B]"r"(B)
3592     : "xer"
3593   );
3594 }
3595 
3596 // Fast Montgomery multiplication. The derivation of the algorithm is
3597 // in "A Cryptographic Library for the Motorola DSP56000,
3598 // Dusse and Kaliski, Proc. EUROCRYPT 90, pp. 230-237".
3599 static void
3600 montgomery_multiply(unsigned long a[], unsigned long b[], unsigned long n[],
3601                     unsigned long m[], unsigned long inv, int len) {
3602   unsigned long t0 = 0, t1 = 0, t2 = 0; // Triple-precision accumulator
3603   int i;
3604 
3605   assert(inv * n[0] == -1UL, "broken inverse in Montgomery multiply");
3606 
3607   for (i = 0; i < len; i++) {
3608     int j;
3609     for (j = 0; j < i; j++) {
3610       MACC(a[j], b[i-j], t0, t1, t2);
3611       MACC(m[j], n[i-j], t0, t1, t2);
3612     }
3613     MACC(a[i], b[0], t0, t1, t2);
3614     m[i] = t0 * inv;
3615     MACC(m[i], n[0], t0, t1, t2);
3616 
3617     assert(t0 == 0, "broken Montgomery multiply");
3618 
3619     t0 = t1; t1 = t2; t2 = 0;
3620   }
3621 
3622   for (i = len; i < 2*len; i++) {
3623     int j;
3624     for (j = i-len+1; j < len; j++) {
3625       MACC(a[j], b[i-j], t0, t1, t2);
3626       MACC(m[j], n[i-j], t0, t1, t2);
3627     }
3628     m[i-len] = t0;
3629     t0 = t1; t1 = t2; t2 = 0;
3630   }
3631 
3632   while (t0) {
3633     t0 = sub(m, n, t0, len);
3634   }
3635 }
3636 
3637 // Fast Montgomery squaring. This uses asymptotically 25% fewer
3638 // multiplies so it should be up to 25% faster than Montgomery
3639 // multiplication. However, its loop control is more complex and it
3640 // may actually run slower on some machines.
3641 static void
3642 montgomery_square(unsigned long a[], unsigned long n[],
3643                   unsigned long m[], unsigned long inv, int len) {
3644   unsigned long t0 = 0, t1 = 0, t2 = 0; // Triple-precision accumulator
3645   int i;
3646 
3647   assert(inv * n[0] == -1UL, "broken inverse in Montgomery multiply");
3648 
3649   for (i = 0; i < len; i++) {
3650     int j;
3651     int end = (i+1)/2;
3652     for (j = 0; j < end; j++) {
3653       MACC2(a[j], a[i-j], t0, t1, t2);
3654       MACC(m[j], n[i-j], t0, t1, t2);
3655     }
3656     if ((i & 1) == 0) {
3657       MACC(a[j], a[j], t0, t1, t2);
3658     }
3659     for (; j < i; j++) {
3660       MACC(m[j], n[i-j], t0, t1, t2);
3661     }
3662     m[i] = t0 * inv;
3663     MACC(m[i], n[0], t0, t1, t2);
3664 
3665     assert(t0 == 0, "broken Montgomery square");
3666 
3667     t0 = t1; t1 = t2; t2 = 0;
3668   }
3669 
3670   for (i = len; i < 2*len; i++) {
3671     int start = i-len+1;
3672     int end = start + (len - start)/2;
3673     int j;
3674     for (j = start; j < end; j++) {
3675       MACC2(a[j], a[i-j], t0, t1, t2);
3676       MACC(m[j], n[i-j], t0, t1, t2);
3677     }
3678     if ((i & 1) == 0) {
3679       MACC(a[j], a[j], t0, t1, t2);
3680     }
3681     for (; j < len; j++) {
3682       MACC(m[j], n[i-j], t0, t1, t2);
3683     }
3684     m[i-len] = t0;
3685     t0 = t1; t1 = t2; t2 = 0;
3686   }
3687 
3688   while (t0) {
3689     t0 = sub(m, n, t0, len);
3690   }
3691 }
3692 
3693 // The threshold at which squaring is advantageous was determined
3694 // experimentally on an i7-3930K (Ivy Bridge) CPU @ 3.5GHz.
3695 // Doesn't seem to be relevant for Power8 so we use the same value.
3696 #define MONTGOMERY_SQUARING_THRESHOLD 64
3697 
3698 // Copy len longwords from s to d, word-swapping as we go. The
3699 // destination array is reversed.
3700 static void reverse_words(unsigned long *s, unsigned long *d, int len) {
3701   d += len;
3702   while(len-- > 0) {
3703     d--;
3704     unsigned long s_val = *s;
3705     // Swap words in a longword on little endian machines.
3706 #ifdef VM_LITTLE_ENDIAN
3707      s_val = (s_val << 32) | (s_val >> 32);
3708 #endif
3709     *d = s_val;
3710     s++;
3711   }
3712 }
3713 
3714 void SharedRuntime::montgomery_multiply(jint *a_ints, jint *b_ints, jint *n_ints,
3715                                         jint len, jlong inv,
3716                                         jint *m_ints) {
3717   len = len & 0x7fffFFFF; // C2 does not respect int to long conversion for stub calls.
3718   assert(len % 2 == 0, "array length in montgomery_multiply must be even");
3719   int longwords = len/2;
3720 
3721   // Make very sure we don't use so much space that the stack might
3722   // overflow. 512 jints corresponds to an 16384-bit integer and
3723   // will use here a total of 8k bytes of stack space.
3724   int divisor = sizeof(unsigned long) * 4;
3725   guarantee(longwords <= 8192 / divisor, "must be");
3726   int total_allocation = longwords * sizeof (unsigned long) * 4;
3727   unsigned long *scratch = (unsigned long *)alloca(total_allocation);
3728 
3729   // Local scratch arrays
3730   unsigned long
3731     *a = scratch + 0 * longwords,
3732     *b = scratch + 1 * longwords,
3733     *n = scratch + 2 * longwords,
3734     *m = scratch + 3 * longwords;
3735 
3736   reverse_words((unsigned long *)a_ints, a, longwords);
3737   reverse_words((unsigned long *)b_ints, b, longwords);
3738   reverse_words((unsigned long *)n_ints, n, longwords);
3739 
3740   ::montgomery_multiply(a, b, n, m, (unsigned long)inv, longwords);
3741 
3742   reverse_words(m, (unsigned long *)m_ints, longwords);
3743 }
3744 
3745 void SharedRuntime::montgomery_square(jint *a_ints, jint *n_ints,
3746                                       jint len, jlong inv,
3747                                       jint *m_ints) {
3748   len = len & 0x7fffFFFF; // C2 does not respect int to long conversion for stub calls.
3749   assert(len % 2 == 0, "array length in montgomery_square must be even");
3750   int longwords = len/2;
3751 
3752   // Make very sure we don't use so much space that the stack might
3753   // overflow. 512 jints corresponds to an 16384-bit integer and
3754   // will use here a total of 6k bytes of stack space.
3755   int divisor = sizeof(unsigned long) * 3;
3756   guarantee(longwords <= (8192 / divisor), "must be");
3757   int total_allocation = longwords * sizeof (unsigned long) * 3;
3758   unsigned long *scratch = (unsigned long *)alloca(total_allocation);
3759 
3760   // Local scratch arrays
3761   unsigned long
3762     *a = scratch + 0 * longwords,
3763     *n = scratch + 1 * longwords,
3764     *m = scratch + 2 * longwords;
3765 
3766   reverse_words((unsigned long *)a_ints, a, longwords);
3767   reverse_words((unsigned long *)n_ints, n, longwords);
3768 
3769   if (len >= MONTGOMERY_SQUARING_THRESHOLD) {
3770     ::montgomery_square(a, n, m, (unsigned long)inv, longwords);
3771   } else {
3772     ::montgomery_multiply(a, a, n, m, (unsigned long)inv, longwords);
3773   }
3774 
3775   reverse_words(m, (unsigned long *)m_ints, longwords);
3776 }
3777 
3778 #if INCLUDE_JFR
3779 
3780 // For c2: c_rarg0 is junk, call to runtime to write a checkpoint.
3781 // It returns a jobject handle to the event writer.
3782 // The handle is dereferenced and the return value is the event writer oop.
3783 RuntimeStub* SharedRuntime::generate_jfr_write_checkpoint() {
3784   const char* name = SharedRuntime::stub_name(SharedStubId::jfr_write_checkpoint_id);
3785   CodeBuffer code(name, 512, 64);
3786   MacroAssembler* masm = new MacroAssembler(&code);
3787 
3788   Register tmp1 = R10_ARG8;
3789   Register tmp2 = R9_ARG7;
3790 
3791   int framesize = frame::native_abi_reg_args_size / VMRegImpl::stack_slot_size;
3792   address start = __ pc();
3793   __ mflr(tmp1);
3794   __ std(tmp1, _abi0(lr), R1_SP);  // save return pc
3795   __ push_frame_reg_args(0, tmp1);
3796   int frame_complete = __ pc() - start;
3797   __ set_last_Java_frame(R1_SP, noreg);
3798   __ call_VM_leaf(CAST_FROM_FN_PTR(address, JfrIntrinsicSupport::write_checkpoint), R16_thread);
3799   address calls_return_pc = __ last_calls_return_pc();
3800   __ reset_last_Java_frame();
3801   // The handle is dereferenced through a load barrier.
3802   __ resolve_global_jobject(R3_RET, tmp1, tmp2, MacroAssembler::PRESERVATION_NONE);
3803   __ pop_frame();
3804   __ ld(tmp1, _abi0(lr), R1_SP);
3805   __ mtlr(tmp1);
3806   __ blr();
3807 
3808   OopMapSet* oop_maps = new OopMapSet();
3809   OopMap* map = new OopMap(framesize, 0);
3810   oop_maps->add_gc_map(calls_return_pc - start, map);
3811 
3812   RuntimeStub* stub = // codeBlob framesize is in words (not VMRegImpl::slot_size)
3813     RuntimeStub::new_runtime_stub(name, &code, frame_complete,
3814                                   (framesize >> (LogBytesPerWord - LogBytesPerInt)),
3815                                   oop_maps, false);
3816   return stub;
3817 }
3818 
3819 // For c2: call to return a leased buffer.
3820 RuntimeStub* SharedRuntime::generate_jfr_return_lease() {
3821   const char* name = SharedRuntime::stub_name(SharedStubId::jfr_return_lease_id);
3822   CodeBuffer code(name, 512, 64);
3823   MacroAssembler* masm = new MacroAssembler(&code);
3824 
3825   Register tmp1 = R10_ARG8;
3826   Register tmp2 = R9_ARG7;
3827 
3828   int framesize = frame::native_abi_reg_args_size / VMRegImpl::stack_slot_size;
3829   address start = __ pc();
3830   __ mflr(tmp1);
3831   __ std(tmp1, _abi0(lr), R1_SP);  // save return pc
3832   __ push_frame_reg_args(0, tmp1);
3833   int frame_complete = __ pc() - start;
3834   __ set_last_Java_frame(R1_SP, noreg);
3835   __ call_VM_leaf(CAST_FROM_FN_PTR(address, JfrIntrinsicSupport::return_lease), R16_thread);
3836   address calls_return_pc = __ last_calls_return_pc();
3837   __ reset_last_Java_frame();
3838   __ pop_frame();
3839   __ ld(tmp1, _abi0(lr), R1_SP);
3840   __ mtlr(tmp1);
3841   __ blr();
3842 
3843   OopMapSet* oop_maps = new OopMapSet();
3844   OopMap* map = new OopMap(framesize, 0);
3845   oop_maps->add_gc_map(calls_return_pc - start, map);
3846 
3847   RuntimeStub* stub = // codeBlob framesize is in words (not VMRegImpl::slot_size)
3848     RuntimeStub::new_runtime_stub(name, &code, frame_complete,
3849                                   (framesize >> (LogBytesPerWord - LogBytesPerInt)),
3850                                   oop_maps, false);
3851   return stub;
3852 }
3853 
3854 #endif // INCLUDE_JFR