1 /*
   2  * Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2016, 2019 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/icBuffer.hpp"
  30 #include "code/vtableStubs.hpp"
  31 #include "compiler/oopMap.hpp"
  32 #include "gc/shared/gcLocker.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "interpreter/interp_masm.hpp"
  35 #include "memory/resourceArea.hpp"
  36 #include "nativeInst_s390.hpp"
  37 #include "oops/compiledICHolder.hpp"
  38 #include "oops/klass.inline.hpp"
  39 #include "prims/methodHandles.hpp"
  40 #include "registerSaver_s390.hpp"
  41 #include "runtime/jniHandles.hpp"
  42 #include "runtime/safepointMechanism.hpp"
  43 #include "runtime/sharedRuntime.hpp"
  44 #include "runtime/signature.hpp"
  45 #include "runtime/stubRoutines.hpp"
  46 #include "runtime/vframeArray.hpp"
  47 #include "utilities/align.hpp"
  48 #include "utilities/macros.hpp"
  49 #include "vmreg_s390.inline.hpp"
  50 #ifdef COMPILER1
  51 #include "c1/c1_Runtime1.hpp"
  52 #endif
  53 #ifdef COMPILER2
  54 #include "opto/ad.hpp"
  55 #include "opto/runtime.hpp"
  56 #endif
  57 
  58 #ifdef PRODUCT
  59 #define __ masm->
  60 #else
  61 #define __ (Verbose ? (masm->block_comment(FILE_AND_LINE),masm):masm)->
  62 #endif
  63 
  64 #define BLOCK_COMMENT(str) __ block_comment(str)
  65 #define BIND(label)        bind(label); BLOCK_COMMENT(#label ":")
  66 
  67 #define RegisterSaver_LiveIntReg(regname) \
  68   { RegisterSaver::int_reg,   regname->encoding(), regname->as_VMReg() }
  69 
  70 #define RegisterSaver_LiveFloatReg(regname) \
  71   { RegisterSaver::float_reg, regname->encoding(), regname->as_VMReg() }
  72 
  73 // Registers which are not saved/restored, but still they have got a frame slot.
  74 // Used to get same frame size for RegisterSaver_LiveRegs and RegisterSaver_LiveRegsWithoutR2
  75 #define RegisterSaver_ExcludedIntReg(regname) \
  76   { RegisterSaver::excluded_reg, regname->encoding(), regname->as_VMReg() }
  77 
  78 // Registers which are not saved/restored, but still they have got a frame slot.
  79 // Used to get same frame size for RegisterSaver_LiveRegs and RegisterSaver_LiveRegsWithoutR2.
  80 #define RegisterSaver_ExcludedFloatReg(regname) \
  81   { RegisterSaver::excluded_reg, regname->encoding(), regname->as_VMReg() }
  82 
  83 static const RegisterSaver::LiveRegType RegisterSaver_LiveRegs[] = {
  84   // Live registers which get spilled to the stack. Register positions
  85   // in this array correspond directly to the stack layout.
  86   //
  87   // live float registers:
  88   //
  89   RegisterSaver_LiveFloatReg(Z_F0 ),
  90   // RegisterSaver_ExcludedFloatReg(Z_F1 ), // scratch (Z_fscratch_1)
  91   RegisterSaver_LiveFloatReg(Z_F2 ),
  92   RegisterSaver_LiveFloatReg(Z_F3 ),
  93   RegisterSaver_LiveFloatReg(Z_F4 ),
  94   RegisterSaver_LiveFloatReg(Z_F5 ),
  95   RegisterSaver_LiveFloatReg(Z_F6 ),
  96   RegisterSaver_LiveFloatReg(Z_F7 ),
  97   RegisterSaver_LiveFloatReg(Z_F8 ),
  98   RegisterSaver_LiveFloatReg(Z_F9 ),
  99   RegisterSaver_LiveFloatReg(Z_F10),
 100   RegisterSaver_LiveFloatReg(Z_F11),
 101   RegisterSaver_LiveFloatReg(Z_F12),
 102   RegisterSaver_LiveFloatReg(Z_F13),
 103   RegisterSaver_LiveFloatReg(Z_F14),
 104   RegisterSaver_LiveFloatReg(Z_F15),
 105   //
 106   // RegisterSaver_ExcludedIntReg(Z_R0), // scratch
 107   // RegisterSaver_ExcludedIntReg(Z_R1), // scratch
 108   RegisterSaver_LiveIntReg(Z_R2 ),
 109   RegisterSaver_LiveIntReg(Z_R3 ),
 110   RegisterSaver_LiveIntReg(Z_R4 ),
 111   RegisterSaver_LiveIntReg(Z_R5 ),
 112   RegisterSaver_LiveIntReg(Z_R6 ),
 113   RegisterSaver_LiveIntReg(Z_R7 ),
 114   RegisterSaver_LiveIntReg(Z_R8 ),
 115   RegisterSaver_LiveIntReg(Z_R9 ),
 116   RegisterSaver_LiveIntReg(Z_R10),
 117   RegisterSaver_LiveIntReg(Z_R11),
 118   RegisterSaver_LiveIntReg(Z_R12),
 119   RegisterSaver_LiveIntReg(Z_R13),
 120   // RegisterSaver_ExcludedIntReg(Z_R14), // return pc (Saved in caller frame.)
 121   // RegisterSaver_ExcludedIntReg(Z_R15)  // stack pointer
 122 };
 123 
 124 static const RegisterSaver::LiveRegType RegisterSaver_LiveIntRegs[] = {
 125   // Live registers which get spilled to the stack. Register positions
 126   // in this array correspond directly to the stack layout.
 127   //
 128   // live float registers: All excluded, but still they get a stack slot to get same frame size.
 129   //
 130   RegisterSaver_ExcludedFloatReg(Z_F0 ),
 131   // RegisterSaver_ExcludedFloatReg(Z_F1 ), // scratch (Z_fscratch_1)
 132   RegisterSaver_ExcludedFloatReg(Z_F2 ),
 133   RegisterSaver_ExcludedFloatReg(Z_F3 ),
 134   RegisterSaver_ExcludedFloatReg(Z_F4 ),
 135   RegisterSaver_ExcludedFloatReg(Z_F5 ),
 136   RegisterSaver_ExcludedFloatReg(Z_F6 ),
 137   RegisterSaver_ExcludedFloatReg(Z_F7 ),
 138   RegisterSaver_ExcludedFloatReg(Z_F8 ),
 139   RegisterSaver_ExcludedFloatReg(Z_F9 ),
 140   RegisterSaver_ExcludedFloatReg(Z_F10),
 141   RegisterSaver_ExcludedFloatReg(Z_F11),
 142   RegisterSaver_ExcludedFloatReg(Z_F12),
 143   RegisterSaver_ExcludedFloatReg(Z_F13),
 144   RegisterSaver_ExcludedFloatReg(Z_F14),
 145   RegisterSaver_ExcludedFloatReg(Z_F15),
 146   //
 147   // RegisterSaver_ExcludedIntReg(Z_R0), // scratch
 148   // RegisterSaver_ExcludedIntReg(Z_R1), // scratch
 149   RegisterSaver_LiveIntReg(Z_R2 ),
 150   RegisterSaver_LiveIntReg(Z_R3 ),
 151   RegisterSaver_LiveIntReg(Z_R4 ),
 152   RegisterSaver_LiveIntReg(Z_R5 ),
 153   RegisterSaver_LiveIntReg(Z_R6 ),
 154   RegisterSaver_LiveIntReg(Z_R7 ),
 155   RegisterSaver_LiveIntReg(Z_R8 ),
 156   RegisterSaver_LiveIntReg(Z_R9 ),
 157   RegisterSaver_LiveIntReg(Z_R10),
 158   RegisterSaver_LiveIntReg(Z_R11),
 159   RegisterSaver_LiveIntReg(Z_R12),
 160   RegisterSaver_LiveIntReg(Z_R13),
 161   // RegisterSaver_ExcludedIntReg(Z_R14), // return pc (Saved in caller frame.)
 162   // RegisterSaver_ExcludedIntReg(Z_R15)  // stack pointer
 163 };
 164 
 165 static const RegisterSaver::LiveRegType RegisterSaver_LiveRegsWithoutR2[] = {
 166   // Live registers which get spilled to the stack. Register positions
 167   // in this array correspond directly to the stack layout.
 168   //
 169   // live float registers:
 170   //
 171   RegisterSaver_LiveFloatReg(Z_F0 ),
 172   // RegisterSaver_ExcludedFloatReg(Z_F1 ), // scratch (Z_fscratch_1)
 173   RegisterSaver_LiveFloatReg(Z_F2 ),
 174   RegisterSaver_LiveFloatReg(Z_F3 ),
 175   RegisterSaver_LiveFloatReg(Z_F4 ),
 176   RegisterSaver_LiveFloatReg(Z_F5 ),
 177   RegisterSaver_LiveFloatReg(Z_F6 ),
 178   RegisterSaver_LiveFloatReg(Z_F7 ),
 179   RegisterSaver_LiveFloatReg(Z_F8 ),
 180   RegisterSaver_LiveFloatReg(Z_F9 ),
 181   RegisterSaver_LiveFloatReg(Z_F10),
 182   RegisterSaver_LiveFloatReg(Z_F11),
 183   RegisterSaver_LiveFloatReg(Z_F12),
 184   RegisterSaver_LiveFloatReg(Z_F13),
 185   RegisterSaver_LiveFloatReg(Z_F14),
 186   RegisterSaver_LiveFloatReg(Z_F15),
 187   //
 188   // RegisterSaver_ExcludedIntReg(Z_R0), // scratch
 189   // RegisterSaver_ExcludedIntReg(Z_R1), // scratch
 190   RegisterSaver_ExcludedIntReg(Z_R2), // Omit saving R2.
 191   RegisterSaver_LiveIntReg(Z_R3 ),
 192   RegisterSaver_LiveIntReg(Z_R4 ),
 193   RegisterSaver_LiveIntReg(Z_R5 ),
 194   RegisterSaver_LiveIntReg(Z_R6 ),
 195   RegisterSaver_LiveIntReg(Z_R7 ),
 196   RegisterSaver_LiveIntReg(Z_R8 ),
 197   RegisterSaver_LiveIntReg(Z_R9 ),
 198   RegisterSaver_LiveIntReg(Z_R10),
 199   RegisterSaver_LiveIntReg(Z_R11),
 200   RegisterSaver_LiveIntReg(Z_R12),
 201   RegisterSaver_LiveIntReg(Z_R13),
 202   // RegisterSaver_ExcludedIntReg(Z_R14), // return pc (Saved in caller frame.)
 203   // RegisterSaver_ExcludedIntReg(Z_R15)  // stack pointer
 204 };
 205 
 206 // Live argument registers which get spilled to the stack.
 207 static const RegisterSaver::LiveRegType RegisterSaver_LiveArgRegs[] = {
 208   RegisterSaver_LiveFloatReg(Z_FARG1),
 209   RegisterSaver_LiveFloatReg(Z_FARG2),
 210   RegisterSaver_LiveFloatReg(Z_FARG3),
 211   RegisterSaver_LiveFloatReg(Z_FARG4),
 212   RegisterSaver_LiveIntReg(Z_ARG1),
 213   RegisterSaver_LiveIntReg(Z_ARG2),
 214   RegisterSaver_LiveIntReg(Z_ARG3),
 215   RegisterSaver_LiveIntReg(Z_ARG4),
 216   RegisterSaver_LiveIntReg(Z_ARG5)
 217 };
 218 
 219 static const RegisterSaver::LiveRegType RegisterSaver_LiveVolatileRegs[] = {
 220   // Live registers which get spilled to the stack. Register positions
 221   // in this array correspond directly to the stack layout.
 222   //
 223   // live float registers:
 224   //
 225   RegisterSaver_LiveFloatReg(Z_F0 ),
 226   // RegisterSaver_ExcludedFloatReg(Z_F1 ), // scratch (Z_fscratch_1)
 227   RegisterSaver_LiveFloatReg(Z_F2 ),
 228   RegisterSaver_LiveFloatReg(Z_F3 ),
 229   RegisterSaver_LiveFloatReg(Z_F4 ),
 230   RegisterSaver_LiveFloatReg(Z_F5 ),
 231   RegisterSaver_LiveFloatReg(Z_F6 ),
 232   RegisterSaver_LiveFloatReg(Z_F7 ),
 233   // RegisterSaver_LiveFloatReg(Z_F8 ), // non-volatile
 234   // RegisterSaver_LiveFloatReg(Z_F9 ), // non-volatile
 235   // RegisterSaver_LiveFloatReg(Z_F10), // non-volatile
 236   // RegisterSaver_LiveFloatReg(Z_F11), // non-volatile
 237   // RegisterSaver_LiveFloatReg(Z_F12), // non-volatile
 238   // RegisterSaver_LiveFloatReg(Z_F13), // non-volatile
 239   // RegisterSaver_LiveFloatReg(Z_F14), // non-volatile
 240   // RegisterSaver_LiveFloatReg(Z_F15), // non-volatile
 241   //
 242   // RegisterSaver_ExcludedIntReg(Z_R0), // scratch
 243   // RegisterSaver_ExcludedIntReg(Z_R1), // scratch
 244   RegisterSaver_LiveIntReg(Z_R2 ),
 245   RegisterSaver_LiveIntReg(Z_R3 ),
 246   RegisterSaver_LiveIntReg(Z_R4 ),
 247   RegisterSaver_LiveIntReg(Z_R5 ),
 248   // RegisterSaver_LiveIntReg(Z_R6 ), // non-volatile
 249   // RegisterSaver_LiveIntReg(Z_R7 ), // non-volatile
 250   // RegisterSaver_LiveIntReg(Z_R8 ), // non-volatile
 251   // RegisterSaver_LiveIntReg(Z_R9 ), // non-volatile
 252   // RegisterSaver_LiveIntReg(Z_R10), // non-volatile
 253   // RegisterSaver_LiveIntReg(Z_R11), // non-volatile
 254   // RegisterSaver_LiveIntReg(Z_R12), // non-volatile
 255   // RegisterSaver_LiveIntReg(Z_R13), // non-volatile
 256   // RegisterSaver_ExcludedIntReg(Z_R14), // return pc (Saved in caller frame.)
 257   // RegisterSaver_ExcludedIntReg(Z_R15)  // stack pointer
 258 };
 259 
 260 int RegisterSaver::live_reg_save_size(RegisterSet reg_set) {
 261   int reg_space = -1;
 262   switch (reg_set) {
 263     case all_registers:           reg_space = sizeof(RegisterSaver_LiveRegs); break;
 264     case all_registers_except_r2: reg_space = sizeof(RegisterSaver_LiveRegsWithoutR2); break;
 265     case all_integer_registers:   reg_space = sizeof(RegisterSaver_LiveIntRegs); break;
 266     case all_volatile_registers:  reg_space = sizeof(RegisterSaver_LiveVolatileRegs); break;
 267     case arg_registers:           reg_space = sizeof(RegisterSaver_LiveArgRegs); break;
 268     default: ShouldNotReachHere();
 269   }
 270   return (reg_space / sizeof(RegisterSaver::LiveRegType)) * reg_size;
 271 }
 272 
 273 
 274 int RegisterSaver::live_reg_frame_size(RegisterSet reg_set) {
 275   return live_reg_save_size(reg_set) + frame::z_abi_160_size;
 276 }
 277 
 278 
 279 // return_pc: Specify the register that should be stored as the return pc in the current frame.
 280 OopMap* RegisterSaver::save_live_registers(MacroAssembler* masm, RegisterSet reg_set, Register return_pc) {
 281   // Record volatile registers as callee-save values in an OopMap so
 282   // their save locations will be propagated to the caller frame's
 283   // RegisterMap during StackFrameStream construction (needed for
 284   // deoptimization; see compiledVFrame::create_stack_value).
 285 
 286   // Calculate frame size.
 287   const int frame_size_in_bytes  = live_reg_frame_size(reg_set);
 288   const int frame_size_in_slots  = frame_size_in_bytes / sizeof(jint);
 289   const int register_save_offset = frame_size_in_bytes - live_reg_save_size(reg_set);
 290 
 291   // OopMap frame size is in c2 stack slots (sizeof(jint)) not bytes or words.
 292   OopMap* map = new OopMap(frame_size_in_slots, 0);
 293 
 294   int regstosave_num = 0;
 295   const RegisterSaver::LiveRegType* live_regs = NULL;
 296 
 297   switch (reg_set) {
 298     case all_registers:
 299       regstosave_num = sizeof(RegisterSaver_LiveRegs)/sizeof(RegisterSaver::LiveRegType);
 300       live_regs      = RegisterSaver_LiveRegs;
 301       break;
 302     case all_registers_except_r2:
 303       regstosave_num = sizeof(RegisterSaver_LiveRegsWithoutR2)/sizeof(RegisterSaver::LiveRegType);;
 304       live_regs      = RegisterSaver_LiveRegsWithoutR2;
 305       break;
 306     case all_integer_registers:
 307       regstosave_num = sizeof(RegisterSaver_LiveIntRegs)/sizeof(RegisterSaver::LiveRegType);
 308       live_regs      = RegisterSaver_LiveIntRegs;
 309       break;
 310     case all_volatile_registers:
 311       regstosave_num = sizeof(RegisterSaver_LiveVolatileRegs)/sizeof(RegisterSaver::LiveRegType);
 312       live_regs      = RegisterSaver_LiveVolatileRegs;
 313       break;
 314     case arg_registers:
 315       regstosave_num = sizeof(RegisterSaver_LiveArgRegs)/sizeof(RegisterSaver::LiveRegType);;
 316       live_regs      = RegisterSaver_LiveArgRegs;
 317       break;
 318     default: ShouldNotReachHere();
 319   }
 320 
 321   // Save return pc in old frame.
 322   __ save_return_pc(return_pc);
 323 
 324   // Push a new frame (includes stack linkage).
 325   // Use return_pc as scratch for push_frame. Z_R0_scratch (the default) and Z_R1_scratch are
 326   // illegally used to pass parameters by RangeCheckStub::emit_code().
 327   __ push_frame(frame_size_in_bytes, return_pc);
 328   // We have to restore return_pc right away.
 329   // Nobody else will. Furthermore, return_pc isn't necessarily the default (Z_R14).
 330   // Nobody else knows which register we saved.
 331   __ z_lg(return_pc, _z_abi16(return_pc) + frame_size_in_bytes, Z_SP);
 332 
 333   // Register save area in new frame starts above z_abi_160 area.
 334   int offset = register_save_offset;
 335 
 336   Register first = noreg;
 337   Register last  = noreg;
 338   int      first_offset = -1;
 339   bool     float_spilled = false;
 340 
 341   for (int i = 0; i < regstosave_num; i++, offset += reg_size) {
 342     int reg_num  = live_regs[i].reg_num;
 343     int reg_type = live_regs[i].reg_type;
 344 
 345     switch (reg_type) {
 346       case RegisterSaver::int_reg: {
 347         Register reg = as_Register(reg_num);
 348         if (last != reg->predecessor()) {
 349           if (first != noreg) {
 350             __ z_stmg(first, last, first_offset, Z_SP);
 351           }
 352           first = reg;
 353           first_offset = offset;
 354           DEBUG_ONLY(float_spilled = false);
 355         }
 356         last = reg;
 357         assert(last != Z_R0, "r0 would require special treatment");
 358         assert(!float_spilled, "for simplicity, do not mix up ints and floats in RegisterSaver_LiveRegs[]");
 359         break;
 360       }
 361 
 362       case RegisterSaver::excluded_reg: // Not saved/restored, but with dedicated slot.
 363         continue; // Continue with next loop iteration.
 364 
 365       case RegisterSaver::float_reg: {
 366         FloatRegister freg = as_FloatRegister(reg_num);
 367         __ z_std(freg, offset, Z_SP);
 368         DEBUG_ONLY(float_spilled = true);
 369         break;
 370       }
 371 
 372       default:
 373         ShouldNotReachHere();
 374         break;
 375     }
 376 
 377     // Second set_callee_saved is really a waste but we'll keep things as they were for now
 378     map->set_callee_saved(VMRegImpl::stack2reg(offset >> 2), live_regs[i].vmreg);
 379     map->set_callee_saved(VMRegImpl::stack2reg((offset + half_reg_size) >> 2), live_regs[i].vmreg->next());
 380   }
 381   assert(first != noreg, "Should spill at least one int reg.");
 382   __ z_stmg(first, last, first_offset, Z_SP);
 383 
 384   // And we're done.
 385   return map;
 386 }
 387 
 388 
 389 // Generate the OopMap (again, regs where saved before).
 390 OopMap* RegisterSaver::generate_oop_map(MacroAssembler* masm, RegisterSet reg_set) {
 391   // Calculate frame size.
 392   const int frame_size_in_bytes  = live_reg_frame_size(reg_set);
 393   const int frame_size_in_slots  = frame_size_in_bytes / sizeof(jint);
 394   const int register_save_offset = frame_size_in_bytes - live_reg_save_size(reg_set);
 395 
 396   // OopMap frame size is in c2 stack slots (sizeof(jint)) not bytes or words.
 397   OopMap* map = new OopMap(frame_size_in_slots, 0);
 398 
 399   int regstosave_num = 0;
 400   const RegisterSaver::LiveRegType* live_regs = NULL;
 401 
 402   switch (reg_set) {
 403     case all_registers:
 404       regstosave_num = sizeof(RegisterSaver_LiveRegs)/sizeof(RegisterSaver::LiveRegType);
 405       live_regs      = RegisterSaver_LiveRegs;
 406       break;
 407     case all_registers_except_r2:
 408       regstosave_num = sizeof(RegisterSaver_LiveRegsWithoutR2)/sizeof(RegisterSaver::LiveRegType);;
 409       live_regs      = RegisterSaver_LiveRegsWithoutR2;
 410       break;
 411     case all_integer_registers:
 412       regstosave_num = sizeof(RegisterSaver_LiveIntRegs)/sizeof(RegisterSaver::LiveRegType);
 413       live_regs      = RegisterSaver_LiveIntRegs;
 414       break;
 415     case all_volatile_registers:
 416       regstosave_num = sizeof(RegisterSaver_LiveVolatileRegs)/sizeof(RegisterSaver::LiveRegType);
 417       live_regs      = RegisterSaver_LiveVolatileRegs;
 418       break;
 419     case arg_registers:
 420       regstosave_num = sizeof(RegisterSaver_LiveArgRegs)/sizeof(RegisterSaver::LiveRegType);;
 421       live_regs      = RegisterSaver_LiveArgRegs;
 422       break;
 423     default: ShouldNotReachHere();
 424   }
 425 
 426   // Register save area in new frame starts above z_abi_160 area.
 427   int offset = register_save_offset;
 428   for (int i = 0; i < regstosave_num; i++) {
 429     if (live_regs[i].reg_type < RegisterSaver::excluded_reg) {
 430       map->set_callee_saved(VMRegImpl::stack2reg(offset>>2), live_regs[i].vmreg);
 431       map->set_callee_saved(VMRegImpl::stack2reg((offset + half_reg_size)>>2), live_regs[i].vmreg->next());
 432     }
 433     offset += reg_size;
 434   }
 435   return map;
 436 }
 437 
 438 
 439 // Pop the current frame and restore all the registers that we saved.
 440 void RegisterSaver::restore_live_registers(MacroAssembler* masm, RegisterSet reg_set) {
 441   int offset;
 442   const int register_save_offset = live_reg_frame_size(reg_set) - live_reg_save_size(reg_set);
 443 
 444   Register first = noreg;
 445   Register last = noreg;
 446   int      first_offset = -1;
 447   bool     float_spilled = false;
 448 
 449   int regstosave_num = 0;
 450   const RegisterSaver::LiveRegType* live_regs = NULL;
 451 
 452   switch (reg_set) {
 453     case all_registers:
 454       regstosave_num = sizeof(RegisterSaver_LiveRegs)/sizeof(RegisterSaver::LiveRegType);;
 455       live_regs      = RegisterSaver_LiveRegs;
 456       break;
 457     case all_registers_except_r2:
 458       regstosave_num = sizeof(RegisterSaver_LiveRegsWithoutR2)/sizeof(RegisterSaver::LiveRegType);;
 459       live_regs      = RegisterSaver_LiveRegsWithoutR2;
 460       break;
 461     case all_integer_registers:
 462       regstosave_num = sizeof(RegisterSaver_LiveIntRegs)/sizeof(RegisterSaver::LiveRegType);
 463       live_regs      = RegisterSaver_LiveIntRegs;
 464       break;
 465     case all_volatile_registers:
 466       regstosave_num = sizeof(RegisterSaver_LiveVolatileRegs)/sizeof(RegisterSaver::LiveRegType);;
 467       live_regs      = RegisterSaver_LiveVolatileRegs;
 468       break;
 469     case arg_registers:
 470       regstosave_num = sizeof(RegisterSaver_LiveArgRegs)/sizeof(RegisterSaver::LiveRegType);;
 471       live_regs      = RegisterSaver_LiveArgRegs;
 472       break;
 473     default: ShouldNotReachHere();
 474   }
 475 
 476   // Restore all registers (ints and floats).
 477 
 478   // Register save area in new frame starts above z_abi_160 area.
 479   offset = register_save_offset;
 480 
 481   for (int i = 0; i < regstosave_num; i++, offset += reg_size) {
 482     int reg_num  = live_regs[i].reg_num;
 483     int reg_type = live_regs[i].reg_type;
 484 
 485     switch (reg_type) {
 486       case RegisterSaver::excluded_reg:
 487         continue; // Continue with next loop iteration.
 488 
 489       case RegisterSaver::int_reg: {
 490         Register reg = as_Register(reg_num);
 491         if (last != reg->predecessor()) {
 492           if (first != noreg) {
 493             __ z_lmg(first, last, first_offset, Z_SP);
 494           }
 495           first = reg;
 496           first_offset = offset;
 497           DEBUG_ONLY(float_spilled = false);
 498         }
 499         last = reg;
 500         assert(last != Z_R0, "r0 would require special treatment");
 501         assert(!float_spilled, "for simplicity, do not mix up ints and floats in RegisterSaver_LiveRegs[]");
 502         break;
 503       }
 504 
 505       case RegisterSaver::float_reg: {
 506         FloatRegister freg = as_FloatRegister(reg_num);
 507         __ z_ld(freg, offset, Z_SP);
 508         DEBUG_ONLY(float_spilled = true);
 509         break;
 510       }
 511 
 512       default:
 513         ShouldNotReachHere();
 514     }
 515   }
 516   assert(first != noreg, "Should spill at least one int reg.");
 517   __ z_lmg(first, last, first_offset, Z_SP);
 518 
 519   // Pop the frame.
 520   __ pop_frame();
 521 
 522   // Restore the flags.
 523   __ restore_return_pc();
 524 }
 525 
 526 
 527 // Pop the current frame and restore the registers that might be holding a result.
 528 void RegisterSaver::restore_result_registers(MacroAssembler* masm) {
 529   int i;
 530   int offset;
 531   const int regstosave_num       = sizeof(RegisterSaver_LiveRegs) /
 532                                    sizeof(RegisterSaver::LiveRegType);
 533   const int register_save_offset = live_reg_frame_size(all_registers) - live_reg_save_size(all_registers);
 534 
 535   // Restore all result registers (ints and floats).
 536   offset = register_save_offset;
 537   for (int i = 0; i < regstosave_num; i++, offset += reg_size) {
 538     int reg_num = RegisterSaver_LiveRegs[i].reg_num;
 539     int reg_type = RegisterSaver_LiveRegs[i].reg_type;
 540     switch (reg_type) {
 541       case RegisterSaver::excluded_reg:
 542         continue; // Continue with next loop iteration.
 543       case RegisterSaver::int_reg: {
 544         if (as_Register(reg_num) == Z_RET) { // int result_reg
 545           __ z_lg(as_Register(reg_num), offset, Z_SP);
 546         }
 547         break;
 548       }
 549       case RegisterSaver::float_reg: {
 550         if (as_FloatRegister(reg_num) == Z_FRET) { // float result_reg
 551           __ z_ld(as_FloatRegister(reg_num), offset, Z_SP);
 552         }
 553         break;
 554       }
 555       default:
 556         ShouldNotReachHere();
 557     }
 558   }
 559 }
 560 
 561 // ---------------------------------------------------------------------------
 562 void SharedRuntime::save_native_result(MacroAssembler * masm,
 563                                        BasicType ret_type,
 564                                        int frame_slots) {
 565   Address memaddr(Z_SP, frame_slots * VMRegImpl::stack_slot_size);
 566 
 567   switch (ret_type) {
 568     case T_BOOLEAN:  // Save shorter types as int. Do we need sign extension at restore??
 569     case T_BYTE:
 570     case T_CHAR:
 571     case T_SHORT:
 572     case T_INT:
 573       __ reg2mem_opt(Z_RET, memaddr, false);
 574       break;
 575     case T_OBJECT:   // Save pointer types as long.
 576     case T_ARRAY:
 577     case T_ADDRESS:
 578     case T_VOID:
 579     case T_LONG:
 580       __ reg2mem_opt(Z_RET, memaddr);
 581       break;
 582     case T_FLOAT:
 583       __ freg2mem_opt(Z_FRET, memaddr, false);
 584       break;
 585     case T_DOUBLE:
 586       __ freg2mem_opt(Z_FRET, memaddr);
 587       break;
 588     default:
 589       ShouldNotReachHere();
 590       break;
 591   }
 592 }
 593 
 594 void SharedRuntime::restore_native_result(MacroAssembler *masm,
 595                                           BasicType       ret_type,
 596                                           int             frame_slots) {
 597   Address memaddr(Z_SP, frame_slots * VMRegImpl::stack_slot_size);
 598 
 599   switch (ret_type) {
 600     case T_BOOLEAN:  // Restore shorter types as int. Do we need sign extension at restore??
 601     case T_BYTE:
 602     case T_CHAR:
 603     case T_SHORT:
 604     case T_INT:
 605       __ mem2reg_opt(Z_RET, memaddr, false);
 606       break;
 607     case T_OBJECT:   // Restore pointer types as long.
 608     case T_ARRAY:
 609     case T_ADDRESS:
 610     case T_VOID:
 611     case T_LONG:
 612       __ mem2reg_opt(Z_RET, memaddr);
 613       break;
 614     case T_FLOAT:
 615       __ mem2freg_opt(Z_FRET, memaddr, false);
 616       break;
 617     case T_DOUBLE:
 618       __ mem2freg_opt(Z_FRET, memaddr);
 619       break;
 620     default:
 621       ShouldNotReachHere();
 622       break;
 623   }
 624 }
 625 
 626 // ---------------------------------------------------------------------------
 627 // Read the array of BasicTypes from a signature, and compute where the
 628 // arguments should go. Values in the VMRegPair regs array refer to 4-byte
 629 // quantities. Values less than VMRegImpl::stack0 are registers, those above
 630 // refer to 4-byte stack slots. All stack slots are based off of the stack pointer
 631 // as framesizes are fixed.
 632 // VMRegImpl::stack0 refers to the first slot 0(sp).
 633 // VMRegImpl::stack0+1 refers to the memory word 4-byes higher. Registers
 634 // up to RegisterImpl::number_of_registers are the 64-bit integer registers.
 635 
 636 // Note: the INPUTS in sig_bt are in units of Java argument words, which are
 637 // either 32-bit or 64-bit depending on the build. The OUTPUTS are in 32-bit
 638 // units regardless of build.
 639 
 640 // The Java calling convention is a "shifted" version of the C ABI.
 641 // By skipping the first C ABI register we can call non-static jni methods
 642 // with small numbers of arguments without having to shuffle the arguments
 643 // at all. Since we control the java ABI we ought to at least get some
 644 // advantage out of it.
 645 int SharedRuntime::java_calling_convention(const BasicType *sig_bt,
 646                                            VMRegPair *regs,
 647                                            int total_args_passed) {
 648   // c2c calling conventions for compiled-compiled calls.
 649 
 650   // An int/float occupies 1 slot here.
 651   const int inc_stk_for_intfloat   = 1; // 1 slots for ints and floats.
 652   const int inc_stk_for_longdouble = 2; // 2 slots for longs and doubles.
 653 
 654   const VMReg z_iarg_reg[5] = {
 655     Z_R2->as_VMReg(),
 656     Z_R3->as_VMReg(),
 657     Z_R4->as_VMReg(),
 658     Z_R5->as_VMReg(),
 659     Z_R6->as_VMReg()
 660   };
 661   const VMReg z_farg_reg[4] = {
 662     Z_F0->as_VMReg(),
 663     Z_F2->as_VMReg(),
 664     Z_F4->as_VMReg(),
 665     Z_F6->as_VMReg()
 666   };
 667   const int z_num_iarg_registers = sizeof(z_iarg_reg) / sizeof(z_iarg_reg[0]);
 668   const int z_num_farg_registers = sizeof(z_farg_reg) / sizeof(z_farg_reg[0]);
 669 
 670   assert(RegisterImpl::number_of_arg_registers == z_num_iarg_registers, "iarg reg count mismatch");
 671   assert(FloatRegisterImpl::number_of_arg_registers == z_num_farg_registers, "farg reg count mismatch");
 672 
 673   int i;
 674   int stk = 0;
 675   int ireg = 0;
 676   int freg = 0;
 677 
 678   for (int i = 0; i < total_args_passed; ++i) {
 679     switch (sig_bt[i]) {
 680       case T_BOOLEAN:
 681       case T_CHAR:
 682       case T_BYTE:
 683       case T_SHORT:
 684       case T_INT:
 685         if (ireg < z_num_iarg_registers) {
 686           // Put int/ptr in register.
 687           regs[i].set1(z_iarg_reg[ireg]);
 688           ++ireg;
 689         } else {
 690           // Put int/ptr on stack.
 691           regs[i].set1(VMRegImpl::stack2reg(stk));
 692           stk += inc_stk_for_intfloat;
 693         }
 694         break;
 695       case T_LONG:
 696         assert((i + 1) < total_args_passed && sig_bt[i+1] == T_VOID, "expecting half");
 697         if (ireg < z_num_iarg_registers) {
 698           // Put long in register.
 699           regs[i].set2(z_iarg_reg[ireg]);
 700           ++ireg;
 701         } else {
 702           // Put long on stack and align to 2 slots.
 703           if (stk & 0x1) { ++stk; }
 704           regs[i].set2(VMRegImpl::stack2reg(stk));
 705           stk += inc_stk_for_longdouble;
 706         }
 707         break;
 708       case T_OBJECT:
 709       case T_ARRAY:
 710       case T_ADDRESS:
 711         if (ireg < z_num_iarg_registers) {
 712           // Put ptr in register.
 713           regs[i].set2(z_iarg_reg[ireg]);
 714           ++ireg;
 715         } else {
 716           // Put ptr on stack and align to 2 slots, because
 717           // "64-bit pointers record oop-ishness on 2 aligned adjacent
 718           // registers." (see OopFlow::build_oop_map).
 719           if (stk & 0x1) { ++stk; }
 720           regs[i].set2(VMRegImpl::stack2reg(stk));
 721           stk += inc_stk_for_longdouble;
 722         }
 723         break;
 724       case T_FLOAT:
 725         if (freg < z_num_farg_registers) {
 726           // Put float in register.
 727           regs[i].set1(z_farg_reg[freg]);
 728           ++freg;
 729         } else {
 730           // Put float on stack.
 731           regs[i].set1(VMRegImpl::stack2reg(stk));
 732           stk += inc_stk_for_intfloat;
 733         }
 734         break;
 735       case T_DOUBLE:
 736         assert((i + 1) < total_args_passed && sig_bt[i+1] == T_VOID, "expecting half");
 737         if (freg < z_num_farg_registers) {
 738           // Put double in register.
 739           regs[i].set2(z_farg_reg[freg]);
 740           ++freg;
 741         } else {
 742           // Put double on stack and align to 2 slots.
 743           if (stk & 0x1) { ++stk; }
 744           regs[i].set2(VMRegImpl::stack2reg(stk));
 745           stk += inc_stk_for_longdouble;
 746         }
 747         break;
 748       case T_VOID:
 749         assert(i != 0 && (sig_bt[i - 1] == T_LONG || sig_bt[i - 1] == T_DOUBLE), "expecting half");
 750         // Do not count halves.
 751         regs[i].set_bad();
 752         break;
 753       default:
 754         ShouldNotReachHere();
 755     }
 756   }
 757   return align_up(stk, 2);
 758 }
 759 
 760 int SharedRuntime::c_calling_convention(const BasicType *sig_bt,
 761                                         VMRegPair *regs,
 762                                         VMRegPair *regs2,
 763                                         int total_args_passed) {
 764   assert(regs2 == NULL, "second VMRegPair array not used on this platform");
 765 
 766   // Calling conventions for C runtime calls and calls to JNI native methods.
 767   const VMReg z_iarg_reg[5] = {
 768     Z_R2->as_VMReg(),
 769     Z_R3->as_VMReg(),
 770     Z_R4->as_VMReg(),
 771     Z_R5->as_VMReg(),
 772     Z_R6->as_VMReg()
 773   };
 774   const VMReg z_farg_reg[4] = {
 775     Z_F0->as_VMReg(),
 776     Z_F2->as_VMReg(),
 777     Z_F4->as_VMReg(),
 778     Z_F6->as_VMReg()
 779   };
 780   const int z_num_iarg_registers = sizeof(z_iarg_reg) / sizeof(z_iarg_reg[0]);
 781   const int z_num_farg_registers = sizeof(z_farg_reg) / sizeof(z_farg_reg[0]);
 782 
 783   // Check calling conventions consistency.
 784   assert(RegisterImpl::number_of_arg_registers == z_num_iarg_registers, "iarg reg count mismatch");
 785   assert(FloatRegisterImpl::number_of_arg_registers == z_num_farg_registers, "farg reg count mismatch");
 786 
 787   // Avoid passing C arguments in the wrong stack slots.
 788 
 789   // 'Stk' counts stack slots. Due to alignment, 32 bit values occupy
 790   // 2 such slots, like 64 bit values do.
 791   const int inc_stk_for_intfloat   = 2; // 2 slots for ints and floats.
 792   const int inc_stk_for_longdouble = 2; // 2 slots for longs and doubles.
 793 
 794   int i;
 795   // Leave room for C-compatible ABI
 796   int stk = (frame::z_abi_160_size - frame::z_jit_out_preserve_size) / VMRegImpl::stack_slot_size;
 797   int freg = 0;
 798   int ireg = 0;
 799 
 800   // We put the first 5 arguments into registers and the rest on the
 801   // stack. Float arguments are already in their argument registers
 802   // due to c2c calling conventions (see calling_convention).
 803   for (int i = 0; i < total_args_passed; ++i) {
 804     switch (sig_bt[i]) {
 805       case T_BOOLEAN:
 806       case T_CHAR:
 807       case T_BYTE:
 808       case T_SHORT:
 809       case T_INT:
 810         // Fall through, handle as long.
 811       case T_LONG:
 812       case T_OBJECT:
 813       case T_ARRAY:
 814       case T_ADDRESS:
 815       case T_METADATA:
 816         // Oops are already boxed if required (JNI).
 817         if (ireg < z_num_iarg_registers) {
 818           regs[i].set2(z_iarg_reg[ireg]);
 819           ++ireg;
 820         } else {
 821           regs[i].set2(VMRegImpl::stack2reg(stk));
 822           stk += inc_stk_for_longdouble;
 823         }
 824         break;
 825       case T_FLOAT:
 826         if (freg < z_num_farg_registers) {
 827           regs[i].set1(z_farg_reg[freg]);
 828           ++freg;
 829         } else {
 830           regs[i].set1(VMRegImpl::stack2reg(stk+1));
 831           stk +=  inc_stk_for_intfloat;
 832         }
 833         break;
 834       case T_DOUBLE:
 835         assert((i + 1) < total_args_passed && sig_bt[i+1] == T_VOID, "expecting half");
 836         if (freg < z_num_farg_registers) {
 837           regs[i].set2(z_farg_reg[freg]);
 838           ++freg;
 839         } else {
 840           // Put double on stack.
 841           regs[i].set2(VMRegImpl::stack2reg(stk));
 842           stk += inc_stk_for_longdouble;
 843         }
 844         break;
 845       case T_VOID:
 846         // Do not count halves.
 847         regs[i].set_bad();
 848         break;
 849       default:
 850         ShouldNotReachHere();
 851     }
 852   }
 853   return align_up(stk, 2);
 854 }
 855 
 856 int SharedRuntime::vector_calling_convention(VMRegPair *regs,
 857                                              uint num_bits,
 858                                              uint total_args_passed) {
 859   Unimplemented();
 860   return 0;
 861 }
 862 
 863 ////////////////////////////////////////////////////////////////////////
 864 //
 865 //  Argument shufflers
 866 //
 867 ////////////////////////////////////////////////////////////////////////
 868 
 869 //----------------------------------------------------------------------
 870 // The java_calling_convention describes stack locations as ideal slots on
 871 // a frame with no abi restrictions. Since we must observe abi restrictions
 872 // (like the placement of the register window) the slots must be biased by
 873 // the following value.
 874 //----------------------------------------------------------------------
 875 static int reg2slot(VMReg r) {
 876   return r->reg2stack() + SharedRuntime::out_preserve_stack_slots();
 877 }
 878 
 879 static int reg2offset(VMReg r) {
 880   return reg2slot(r) * VMRegImpl::stack_slot_size;
 881 }
 882 
 883 static void verify_oop_args(MacroAssembler *masm,
 884                             int total_args_passed,
 885                             const BasicType *sig_bt,
 886                             const VMRegPair *regs) {
 887   if (!VerifyOops) { return; }
 888 
 889   for (int i = 0; i < total_args_passed; i++) {
 890     if (is_reference_type(sig_bt[i])) {
 891       VMReg r = regs[i].first();
 892       assert(r->is_valid(), "bad oop arg");
 893 
 894       if (r->is_stack()) {
 895         __ z_lg(Z_R0_scratch,
 896                 Address(Z_SP, r->reg2stack() * VMRegImpl::stack_slot_size + wordSize));
 897         __ verify_oop(Z_R0_scratch, FILE_AND_LINE);
 898       } else {
 899         __ verify_oop(r->as_Register(), FILE_AND_LINE);
 900       }
 901     }
 902   }
 903 }
 904 
 905 static void gen_special_dispatch(MacroAssembler *masm,
 906                                  int total_args_passed,
 907                                  vmIntrinsics::ID special_dispatch,
 908                                  const BasicType *sig_bt,
 909                                  const VMRegPair *regs) {
 910   verify_oop_args(masm, total_args_passed, sig_bt, regs);
 911 
 912   // Now write the args into the outgoing interpreter space.
 913   bool     has_receiver   = false;
 914   Register receiver_reg   = noreg;
 915   int      member_arg_pos = -1;
 916   Register member_reg     = noreg;
 917   int      ref_kind       = MethodHandles::signature_polymorphic_intrinsic_ref_kind(special_dispatch);
 918 
 919   if (ref_kind != 0) {
 920     member_arg_pos = total_args_passed - 1;  // trailing MemberName argument
 921     member_reg = Z_R9;                       // Known to be free at this point.
 922     has_receiver = MethodHandles::ref_kind_has_receiver(ref_kind);
 923   } else if (special_dispatch == vmIntrinsics::_linkToNative) {
 924     member_arg_pos = total_args_passed - 1;  // trailing NativeEntryPoint argument
 925     member_reg = Z_R9;  // known to be free at this point
 926   } else {
 927     guarantee(special_dispatch == vmIntrinsics::_invokeBasic,
 928               "special_dispatch=%d", vmIntrinsics::as_int(special_dispatch));
 929     has_receiver = true;
 930   }
 931 
 932   if (member_reg != noreg) {
 933     // Load the member_arg into register, if necessary.
 934     assert(member_arg_pos >= 0 && member_arg_pos < total_args_passed, "oob");
 935     assert(sig_bt[member_arg_pos] == T_OBJECT, "dispatch argument must be an object");
 936 
 937     VMReg r = regs[member_arg_pos].first();
 938     assert(r->is_valid(), "bad member arg");
 939 
 940     if (r->is_stack()) {
 941       __ z_lg(member_reg, Address(Z_SP, reg2offset(r)));
 942     } else {
 943       // No data motion is needed.
 944       member_reg = r->as_Register();
 945     }
 946   }
 947 
 948   if (has_receiver) {
 949     // Make sure the receiver is loaded into a register.
 950     assert(total_args_passed > 0, "oob");
 951     assert(sig_bt[0] == T_OBJECT, "receiver argument must be an object");
 952 
 953     VMReg r = regs[0].first();
 954     assert(r->is_valid(), "bad receiver arg");
 955 
 956     if (r->is_stack()) {
 957       // Porting note: This assumes that compiled calling conventions always
 958       // pass the receiver oop in a register. If this is not true on some
 959       // platform, pick a temp and load the receiver from stack.
 960       assert(false, "receiver always in a register");
 961       receiver_reg = Z_R13;  // Known to be free at this point.
 962       __ z_lg(receiver_reg, Address(Z_SP, reg2offset(r)));
 963     } else {
 964       // No data motion is needed.
 965       receiver_reg = r->as_Register();
 966     }
 967   }
 968 
 969   // Figure out which address we are really jumping to:
 970   MethodHandles::generate_method_handle_dispatch(masm, special_dispatch,
 971                                                  receiver_reg, member_reg,
 972                                                  /*for_compiler_entry:*/ true);
 973 }
 974 
 975 ////////////////////////////////////////////////////////////////////////
 976 //
 977 //  Argument shufflers
 978 //
 979 ////////////////////////////////////////////////////////////////////////
 980 
 981 // Is the size of a vector size (in bytes) bigger than a size saved by default?
 982 // 8 bytes registers are saved by default on z/Architecture.
 983 bool SharedRuntime::is_wide_vector(int size) {
 984   // Note, MaxVectorSize == 8 on this platform.
 985   assert(size <= 8, "%d bytes vectors are not supported", size);
 986   return size > 8;
 987 }
 988 
 989 //----------------------------------------------------------------------
 990 // An oop arg. Must pass a handle not the oop itself
 991 //----------------------------------------------------------------------
 992 static void object_move(MacroAssembler *masm,
 993                         OopMap *map,
 994                         int oop_handle_offset,
 995                         int framesize_in_slots,
 996                         VMRegPair src,
 997                         VMRegPair dst,
 998                         bool is_receiver,
 999                         int *receiver_offset) {
1000   int frame_offset = framesize_in_slots*VMRegImpl::stack_slot_size;
1001 
1002   assert(!is_receiver || (is_receiver && (*receiver_offset == -1)), "only one receiving object per call, please.");
1003 
1004   // Must pass a handle. First figure out the location we use as a handle.
1005 
1006   if (src.first()->is_stack()) {
1007     // Oop is already on the stack, put handle on stack or in register
1008     // If handle will be on the stack, use temp reg to calculate it.
1009     Register rHandle = dst.first()->is_stack() ? Z_R1 : dst.first()->as_Register();
1010     Label    skip;
1011     int      slot_in_older_frame = reg2slot(src.first());
1012 
1013     guarantee(!is_receiver, "expecting receiver in register");
1014     map->set_oop(VMRegImpl::stack2reg(slot_in_older_frame + framesize_in_slots));
1015 
1016     __ add2reg(rHandle, reg2offset(src.first())+frame_offset, Z_SP);
1017     __ load_and_test_long(Z_R0, Address(rHandle));
1018     __ z_brne(skip);
1019     // Use a NULL handle if oop is NULL.
1020     __ clear_reg(rHandle, true, false);
1021     __ bind(skip);
1022 
1023     // Copy handle to the right place (register or stack).
1024     if (dst.first()->is_stack()) {
1025       __ z_stg(rHandle, reg2offset(dst.first()), Z_SP);
1026     } // else
1027       // nothing to do. rHandle uses the correct register
1028   } else {
1029     // Oop is passed in an input register. We must flush it to the stack.
1030     const Register rOop = src.first()->as_Register();
1031     const Register rHandle = dst.first()->is_stack() ? Z_R1 : dst.first()->as_Register();
1032     int            oop_slot = (rOop->encoding()-Z_ARG1->encoding()) * VMRegImpl::slots_per_word + oop_handle_offset;
1033     int            oop_slot_offset = oop_slot*VMRegImpl::stack_slot_size;
1034     NearLabel skip;
1035 
1036     if (is_receiver) {
1037       *receiver_offset = oop_slot_offset;
1038     }
1039     map->set_oop(VMRegImpl::stack2reg(oop_slot));
1040 
1041     // Flush Oop to stack, calculate handle.
1042     __ z_stg(rOop, oop_slot_offset, Z_SP);
1043     __ add2reg(rHandle, oop_slot_offset, Z_SP);
1044 
1045     // If Oop == NULL, use a NULL handle.
1046     __ compare64_and_branch(rOop, (RegisterOrConstant)0L, Assembler::bcondNotEqual, skip);
1047     __ clear_reg(rHandle, true, false);
1048     __ bind(skip);
1049 
1050     // Copy handle to the right place (register or stack).
1051     if (dst.first()->is_stack()) {
1052       __ z_stg(rHandle, reg2offset(dst.first()), Z_SP);
1053     } // else
1054       // nothing to do here, since rHandle = dst.first()->as_Register in this case.
1055   }
1056 }
1057 
1058 //----------------------------------------------------------------------
1059 // A float arg. May have to do float reg to int reg conversion
1060 //----------------------------------------------------------------------
1061 static void float_move(MacroAssembler *masm,
1062                        VMRegPair src,
1063                        VMRegPair dst,
1064                        int framesize_in_slots,
1065                        int workspace_slot_offset) {
1066   int frame_offset = framesize_in_slots * VMRegImpl::stack_slot_size;
1067   int workspace_offset = workspace_slot_offset * VMRegImpl::stack_slot_size;
1068 
1069   // We do not accept an argument in a VMRegPair to be spread over two slots,
1070   // no matter what physical location (reg or stack) the slots may have.
1071   // We just check for the unaccepted slot to be invalid.
1072   assert(!src.second()->is_valid(), "float in arg spread over two slots");
1073   assert(!dst.second()->is_valid(), "float out arg spread over two slots");
1074 
1075   if (src.first()->is_stack()) {
1076     if (dst.first()->is_stack()) {
1077       // stack -> stack. The easiest of the bunch.
1078       __ z_mvc(Address(Z_SP, reg2offset(dst.first())),
1079                Address(Z_SP, reg2offset(src.first()) + frame_offset), sizeof(float));
1080     } else {
1081       // stack to reg
1082       Address memaddr(Z_SP, reg2offset(src.first()) + frame_offset);
1083       if (dst.first()->is_Register()) {
1084         __ mem2reg_opt(dst.first()->as_Register(), memaddr, false);
1085       } else {
1086         __ mem2freg_opt(dst.first()->as_FloatRegister(), memaddr, false);
1087       }
1088     }
1089   } else if (src.first()->is_Register()) {
1090     if (dst.first()->is_stack()) {
1091       // gpr -> stack
1092       __ reg2mem_opt(src.first()->as_Register(),
1093                      Address(Z_SP, reg2offset(dst.first()), false ));
1094     } else {
1095       if (dst.first()->is_Register()) {
1096         // gpr -> gpr
1097         __ move_reg_if_needed(dst.first()->as_Register(), T_INT,
1098                               src.first()->as_Register(), T_INT);
1099       } else {
1100         if (VM_Version::has_FPSupportEnhancements()) {
1101           // gpr -> fpr. Exploit z10 capability of direct transfer.
1102           __ z_ldgr(dst.first()->as_FloatRegister(), src.first()->as_Register());
1103         } else {
1104           // gpr -> fpr. Use work space on stack to transfer data.
1105           Address   stackaddr(Z_SP, workspace_offset);
1106 
1107           __ reg2mem_opt(src.first()->as_Register(), stackaddr, false);
1108           __ mem2freg_opt(dst.first()->as_FloatRegister(), stackaddr, false);
1109         }
1110       }
1111     }
1112   } else {
1113     if (dst.first()->is_stack()) {
1114       // fpr -> stack
1115       __ freg2mem_opt(src.first()->as_FloatRegister(),
1116                       Address(Z_SP, reg2offset(dst.first())), false);
1117     } else {
1118       if (dst.first()->is_Register()) {
1119         if (VM_Version::has_FPSupportEnhancements()) {
1120           // fpr -> gpr.
1121           __ z_lgdr(dst.first()->as_Register(), src.first()->as_FloatRegister());
1122         } else {
1123           // fpr -> gpr. Use work space on stack to transfer data.
1124           Address   stackaddr(Z_SP, workspace_offset);
1125 
1126           __ freg2mem_opt(src.first()->as_FloatRegister(), stackaddr, false);
1127           __ mem2reg_opt(dst.first()->as_Register(), stackaddr, false);
1128         }
1129       } else {
1130         // fpr -> fpr
1131         __ move_freg_if_needed(dst.first()->as_FloatRegister(), T_FLOAT,
1132                                src.first()->as_FloatRegister(), T_FLOAT);
1133       }
1134     }
1135   }
1136 }
1137 
1138 //----------------------------------------------------------------------
1139 // A double arg. May have to do double reg to long reg conversion
1140 //----------------------------------------------------------------------
1141 static void double_move(MacroAssembler *masm,
1142                         VMRegPair src,
1143                         VMRegPair dst,
1144                         int framesize_in_slots,
1145                         int workspace_slot_offset) {
1146   int frame_offset = framesize_in_slots*VMRegImpl::stack_slot_size;
1147   int workspace_offset = workspace_slot_offset*VMRegImpl::stack_slot_size;
1148 
1149   // Since src is always a java calling convention we know that the
1150   // src pair is always either all registers or all stack (and aligned?)
1151 
1152   if (src.first()->is_stack()) {
1153     if (dst.first()->is_stack()) {
1154       // stack -> stack. The easiest of the bunch.
1155       __ z_mvc(Address(Z_SP, reg2offset(dst.first())),
1156                Address(Z_SP, reg2offset(src.first()) + frame_offset), sizeof(double));
1157     } else {
1158       // stack to reg
1159       Address stackaddr(Z_SP, reg2offset(src.first()) + frame_offset);
1160 
1161       if (dst.first()->is_Register()) {
1162         __ mem2reg_opt(dst.first()->as_Register(), stackaddr);
1163       } else {
1164         __ mem2freg_opt(dst.first()->as_FloatRegister(), stackaddr);
1165       }
1166     }
1167   } else if (src.first()->is_Register()) {
1168     if (dst.first()->is_stack()) {
1169       // gpr -> stack
1170       __ reg2mem_opt(src.first()->as_Register(),
1171                      Address(Z_SP, reg2offset(dst.first())));
1172     } else {
1173       if (dst.first()->is_Register()) {
1174         // gpr -> gpr
1175         __ move_reg_if_needed(dst.first()->as_Register(), T_LONG,
1176                               src.first()->as_Register(), T_LONG);
1177       } else {
1178         if (VM_Version::has_FPSupportEnhancements()) {
1179           // gpr -> fpr. Exploit z10 capability of direct transfer.
1180           __ z_ldgr(dst.first()->as_FloatRegister(), src.first()->as_Register());
1181         } else {
1182           // gpr -> fpr. Use work space on stack to transfer data.
1183           Address stackaddr(Z_SP, workspace_offset);
1184           __ reg2mem_opt(src.first()->as_Register(), stackaddr);
1185           __ mem2freg_opt(dst.first()->as_FloatRegister(), stackaddr);
1186         }
1187       }
1188     }
1189   } else {
1190     if (dst.first()->is_stack()) {
1191       // fpr -> stack
1192       __ freg2mem_opt(src.first()->as_FloatRegister(),
1193                       Address(Z_SP, reg2offset(dst.first())));
1194     } else {
1195       if (dst.first()->is_Register()) {
1196         if (VM_Version::has_FPSupportEnhancements()) {
1197           // fpr -> gpr. Exploit z10 capability of direct transfer.
1198           __ z_lgdr(dst.first()->as_Register(), src.first()->as_FloatRegister());
1199         } else {
1200           // fpr -> gpr. Use work space on stack to transfer data.
1201           Address stackaddr(Z_SP, workspace_offset);
1202 
1203           __ freg2mem_opt(src.first()->as_FloatRegister(), stackaddr);
1204           __ mem2reg_opt(dst.first()->as_Register(), stackaddr);
1205         }
1206       } else {
1207         // fpr -> fpr
1208         // In theory these overlap but the ordering is such that this is likely a nop.
1209         __ move_freg_if_needed(dst.first()->as_FloatRegister(), T_DOUBLE,
1210                                src.first()->as_FloatRegister(), T_DOUBLE);
1211       }
1212     }
1213   }
1214 }
1215 
1216 //----------------------------------------------------------------------
1217 // A long arg.
1218 //----------------------------------------------------------------------
1219 static void long_move(MacroAssembler *masm,
1220                       VMRegPair src,
1221                       VMRegPair dst,
1222                       int framesize_in_slots) {
1223   int frame_offset = framesize_in_slots*VMRegImpl::stack_slot_size;
1224 
1225   if (src.first()->is_stack()) {
1226     if (dst.first()->is_stack()) {
1227       // stack -> stack. The easiest of the bunch.
1228       __ z_mvc(Address(Z_SP, reg2offset(dst.first())),
1229                Address(Z_SP, reg2offset(src.first()) + frame_offset), sizeof(long));
1230     } else {
1231       // stack to reg
1232       assert(dst.first()->is_Register(), "long dst value must be in GPR");
1233       __ mem2reg_opt(dst.first()->as_Register(),
1234                       Address(Z_SP, reg2offset(src.first()) + frame_offset));
1235     }
1236   } else {
1237     // reg to reg
1238     assert(src.first()->is_Register(), "long src value must be in GPR");
1239     if (dst.first()->is_stack()) {
1240       // reg -> stack
1241       __ reg2mem_opt(src.first()->as_Register(),
1242                      Address(Z_SP, reg2offset(dst.first())));
1243     } else {
1244       // reg -> reg
1245       assert(dst.first()->is_Register(), "long dst value must be in GPR");
1246       __ move_reg_if_needed(dst.first()->as_Register(),
1247                             T_LONG, src.first()->as_Register(), T_LONG);
1248     }
1249   }
1250 }
1251 
1252 
1253 //----------------------------------------------------------------------
1254 // A int-like arg.
1255 //----------------------------------------------------------------------
1256 // On z/Architecture we will store integer like items to the stack as 64 bit
1257 // items, according to the z/Architecture ABI, even though Java would only store
1258 // 32 bits for a parameter.
1259 // We do sign extension for all base types. That is ok since the only
1260 // unsigned base type is T_CHAR, and T_CHAR uses only 16 bits of an int.
1261 // Sign extension 32->64 bit will thus not affect the value.
1262 //----------------------------------------------------------------------
1263 static void move32_64(MacroAssembler *masm,
1264                       VMRegPair src,
1265                       VMRegPair dst,
1266                       int framesize_in_slots) {
1267   int frame_offset = framesize_in_slots * VMRegImpl::stack_slot_size;
1268 
1269   if (src.first()->is_stack()) {
1270     Address memaddr(Z_SP, reg2offset(src.first()) + frame_offset);
1271     if (dst.first()->is_stack()) {
1272       // stack -> stack. MVC not possible due to sign extension.
1273       Address firstaddr(Z_SP, reg2offset(dst.first()));
1274       __ mem2reg_signed_opt(Z_R0_scratch, memaddr);
1275       __ reg2mem_opt(Z_R0_scratch, firstaddr);
1276     } else {
1277       // stack -> reg, sign extended
1278       __ mem2reg_signed_opt(dst.first()->as_Register(), memaddr);
1279     }
1280   } else {
1281     if (dst.first()->is_stack()) {
1282       // reg -> stack, sign extended
1283       Address firstaddr(Z_SP, reg2offset(dst.first()));
1284       __ z_lgfr(src.first()->as_Register(), src.first()->as_Register());
1285       __ reg2mem_opt(src.first()->as_Register(), firstaddr);
1286     } else {
1287       // reg -> reg, sign extended
1288       __ z_lgfr(dst.first()->as_Register(), src.first()->as_Register());
1289     }
1290   }
1291 }
1292 
1293 //----------------------------------------------------------------------
1294 // Wrap a JNI call.
1295 //----------------------------------------------------------------------
1296 #undef USE_RESIZE_FRAME
1297 nmethod *SharedRuntime::generate_native_wrapper(MacroAssembler *masm,
1298                                                 const methodHandle& method,
1299                                                 int compile_id,
1300                                                 BasicType *in_sig_bt,
1301                                                 VMRegPair *in_regs,
1302                                                 BasicType ret_type) {
1303   int total_in_args = method->size_of_parameters();
1304   if (method->is_method_handle_intrinsic()) {
1305     vmIntrinsics::ID iid = method->intrinsic_id();
1306     intptr_t start = (intptr_t) __ pc();
1307     int vep_offset = ((intptr_t) __ pc()) - start;
1308 
1309     gen_special_dispatch(masm, total_in_args,
1310                          method->intrinsic_id(), in_sig_bt, in_regs);
1311 
1312     int frame_complete = ((intptr_t)__ pc()) - start; // Not complete, period.
1313 
1314     __ flush();
1315 
1316     int stack_slots = SharedRuntime::out_preserve_stack_slots();  // No out slots at all, actually.
1317 
1318     return nmethod::new_native_nmethod(method,
1319                                        compile_id,
1320                                        masm->code(),
1321                                        vep_offset,
1322                                        frame_complete,
1323                                        stack_slots / VMRegImpl::slots_per_word,
1324                                        in_ByteSize(-1),
1325                                        (OopMapSet *) NULL);
1326   }
1327 
1328 
1329   ///////////////////////////////////////////////////////////////////////
1330   //
1331   //  Precalculations before generating any code
1332   //
1333   ///////////////////////////////////////////////////////////////////////
1334 
1335   address native_func = method->native_function();
1336   assert(native_func != NULL, "must have function");
1337 
1338   //---------------------------------------------------------------------
1339   // We have received a description of where all the java args are located
1340   // on entry to the wrapper. We need to convert these args to where
1341   // the jni function will expect them. To figure out where they go
1342   // we convert the java signature to a C signature by inserting
1343   // the hidden arguments as arg[0] and possibly arg[1] (static method).
1344   //
1345   // The first hidden argument arg[0] is a pointer to the JNI environment.
1346   // It is generated for every call.
1347   // The second argument arg[1] to the JNI call, which is hidden for static
1348   // methods, is the boxed lock object. For static calls, the lock object
1349   // is the static method itself. The oop is constructed here. for instance
1350   // calls, the lock is performed on the object itself, the pointer of
1351   // which is passed as the first visible argument.
1352   //---------------------------------------------------------------------
1353 
1354   // Additionally, on z/Architecture we must convert integers
1355   // to longs in the C signature. We do this in advance in order to have
1356   // no trouble with indexes into the bt-arrays.
1357   // So convert the signature and registers now, and adjust the total number
1358   // of in-arguments accordingly.
1359   bool method_is_static = method->is_static();
1360   int  total_c_args     = total_in_args + (method_is_static ? 2 : 1);
1361 
1362   BasicType *out_sig_bt = NEW_RESOURCE_ARRAY(BasicType, total_c_args);
1363   VMRegPair *out_regs   = NEW_RESOURCE_ARRAY(VMRegPair, total_c_args);
1364   BasicType* in_elem_bt = NULL;
1365 
1366   // Create the signature for the C call:
1367   //   1) add the JNIEnv*
1368   //   2) add the class if the method is static
1369   //   3) copy the rest of the incoming signature (shifted by the number of
1370   //      hidden arguments)
1371 
1372   int argc = 0;
1373   out_sig_bt[argc++] = T_ADDRESS;
1374   if (method->is_static()) {
1375     out_sig_bt[argc++] = T_OBJECT;
1376   }
1377 
1378   for (int i = 0; i < total_in_args; i++) {
1379     out_sig_bt[argc++] = in_sig_bt[i];
1380   }
1381 
1382   ///////////////////////////////////////////////////////////////////////
1383   // Now figure out where the args must be stored and how much stack space
1384   // they require (neglecting out_preserve_stack_slots but providing space
1385   // for storing the first five register arguments).
1386   // It's weird, see int_stk_helper.
1387   ///////////////////////////////////////////////////////////////////////
1388 
1389   //---------------------------------------------------------------------
1390   // Compute framesize for the wrapper.
1391   //
1392   // - We need to handlize all oops passed in registers.
1393   // - We must create space for them here that is disjoint from the save area.
1394   // - We always just allocate 5 words for storing down these object.
1395   //   This allows us to simply record the base and use the Ireg number to
1396   //   decide which slot to use.
1397   // - Note that the reg number used to index the stack slot is the inbound
1398   //   number, not the outbound number.
1399   // - We must shuffle args to match the native convention,
1400   //   and to include var-args space.
1401   //---------------------------------------------------------------------
1402 
1403   //---------------------------------------------------------------------
1404   // Calculate the total number of stack slots we will need:
1405   // - 1) abi requirements
1406   // - 2) outgoing args
1407   // - 3) space for inbound oop handle area
1408   // - 4) space for handlizing a klass if static method
1409   // - 5) space for a lock if synchronized method
1410   // - 6) workspace (save rtn value, int<->float reg moves, ...)
1411   // - 7) filler slots for alignment
1412   //---------------------------------------------------------------------
1413   // Here is how the space we have allocated will look like.
1414   // Since we use resize_frame, we do not create a new stack frame,
1415   // but just extend the one we got with our own data area.
1416   //
1417   // If an offset or pointer name points to a separator line, it is
1418   // assumed that addressing with offset 0 selects storage starting
1419   // at the first byte above the separator line.
1420   //
1421   //
1422   //     ...                   ...
1423   //      | caller's frame      |
1424   // FP-> |---------------------|
1425   //      | filler slots, if any|
1426   //     7| #slots == mult of 2 |
1427   //      |---------------------|
1428   //      | work space          |
1429   //     6| 2 slots = 8 bytes   |
1430   //      |---------------------|
1431   //     4| klass (if static)   |
1432   //      |---------------------| <- klass_slot_offset
1433   //     3| oopHandle area      |
1434   //      |                     |
1435   //      |                     |
1436   //      |---------------------| <- oop_handle_offset
1437   //     2| outbound memory     |
1438   //     ...                   ...
1439   //      | based arguments     |
1440   //      |---------------------|
1441   //      | vararg              |
1442   //     ...                   ...
1443   //      | area                |
1444   //      |---------------------| <- out_arg_slot_offset
1445   //     1| out_preserved_slots |
1446   //     ...                   ...
1447   //      | (z_abi spec)        |
1448   // SP-> |---------------------| <- FP_slot_offset (back chain)
1449   //     ...                   ...
1450   //
1451   //---------------------------------------------------------------------
1452 
1453   // *_slot_offset indicates offset from SP in #stack slots
1454   // *_offset      indicates offset from SP in #bytes
1455 
1456   int stack_slots = c_calling_convention(out_sig_bt, out_regs, /*regs2=*/NULL, total_c_args) + // 1+2
1457                     SharedRuntime::out_preserve_stack_slots(); // see c_calling_convention
1458 
1459   // Now the space for the inbound oop handle area.
1460   int total_save_slots = RegisterImpl::number_of_arg_registers * VMRegImpl::slots_per_word;
1461 
1462   int oop_handle_slot_offset = stack_slots;
1463   stack_slots += total_save_slots;                                        // 3)
1464 
1465   int klass_slot_offset = 0;
1466   int klass_offset      = -1;
1467   if (method_is_static) {                                                 // 4)
1468     klass_slot_offset  = stack_slots;
1469     klass_offset       = klass_slot_offset * VMRegImpl::stack_slot_size;
1470     stack_slots       += VMRegImpl::slots_per_word;
1471   }
1472 
1473   int workspace_slot_offset= stack_slots;                                 // 6)
1474   stack_slots         += 2;
1475 
1476   // Now compute actual number of stack words we need.
1477   // Round to align stack properly.
1478   stack_slots = align_up(stack_slots,                                     // 7)
1479                          frame::alignment_in_bytes / VMRegImpl::stack_slot_size);
1480   int frame_size_in_bytes = stack_slots * VMRegImpl::stack_slot_size;
1481 
1482 
1483   ///////////////////////////////////////////////////////////////////////
1484   // Now we can start generating code
1485   ///////////////////////////////////////////////////////////////////////
1486 
1487   unsigned int wrapper_CodeStart  = __ offset();
1488   unsigned int wrapper_UEPStart;
1489   unsigned int wrapper_VEPStart;
1490   unsigned int wrapper_FrameDone;
1491   unsigned int wrapper_CRegsSet;
1492   Label     handle_pending_exception;
1493   Label     ic_miss;
1494 
1495   //---------------------------------------------------------------------
1496   // Unverified entry point (UEP)
1497   //---------------------------------------------------------------------
1498   wrapper_UEPStart = __ offset();
1499 
1500   // check ic: object class <-> cached class
1501   if (!method_is_static) __ nmethod_UEP(ic_miss);
1502   // Fill with nops (alignment of verified entry point).
1503   __ align(CodeEntryAlignment);
1504 
1505   //---------------------------------------------------------------------
1506   // Verified entry point (VEP)
1507   //---------------------------------------------------------------------
1508   wrapper_VEPStart = __ offset();
1509 
1510   if (VM_Version::supports_fast_class_init_checks() && method->needs_clinit_barrier()) {
1511     Label L_skip_barrier;
1512     Register klass = Z_R1_scratch;
1513     // Notify OOP recorder (don't need the relocation)
1514     AddressLiteral md = __ constant_metadata_address(method->method_holder());
1515     __ load_const_optimized(klass, md.value());
1516     __ clinit_barrier(klass, Z_thread, &L_skip_barrier /*L_fast_path*/);
1517 
1518     __ load_const_optimized(klass, SharedRuntime::get_handle_wrong_method_stub());
1519     __ z_br(klass);
1520 
1521     __ bind(L_skip_barrier);
1522   }
1523 
1524   __ save_return_pc();
1525   __ generate_stack_overflow_check(frame_size_in_bytes);  // Check before creating frame.
1526 #ifndef USE_RESIZE_FRAME
1527   __ push_frame(frame_size_in_bytes);                     // Create a new frame for the wrapper.
1528 #else
1529   __ resize_frame(-frame_size_in_bytes, Z_R0_scratch);    // No new frame for the wrapper.
1530                                                           // Just resize the existing one.
1531 #endif
1532 
1533   wrapper_FrameDone = __ offset();
1534 
1535   __ verify_thread();
1536 
1537   // Native nmethod wrappers never take possession of the oop arguments.
1538   // So the caller will gc the arguments.
1539   // The only thing we need an oopMap for is if the call is static.
1540   //
1541   // An OopMap for lock (and class if static), and one for the VM call itself
1542   OopMapSet  *oop_maps        = new OopMapSet();
1543   OopMap     *map             = new OopMap(stack_slots * 2, 0 /* arg_slots*/);
1544 
1545   //////////////////////////////////////////////////////////////////////
1546   //
1547   // The Grand Shuffle
1548   //
1549   //////////////////////////////////////////////////////////////////////
1550   //
1551   // We immediately shuffle the arguments so that for any vm call we have
1552   // to make from here on out (sync slow path, jvmti, etc.) we will have
1553   // captured the oops from our caller and have a valid oopMap for them.
1554   //
1555   //--------------------------------------------------------------------
1556   // Natives require 1 or 2 extra arguments over the normal ones: the JNIEnv*
1557   // (derived from JavaThread* which is in Z_thread) and, if static,
1558   // the class mirror instead of a receiver. This pretty much guarantees that
1559   // register layout will not match. We ignore these extra arguments during
1560   // the shuffle. The shuffle is described by the two calling convention
1561   // vectors we have in our possession. We simply walk the java vector to
1562   // get the source locations and the c vector to get the destinations.
1563   //
1564   // This is a trick. We double the stack slots so we can claim
1565   // the oops in the caller's frame. Since we are sure to have
1566   // more args than the caller doubling is enough to make
1567   // sure we can capture all the incoming oop args from the caller.
1568   //--------------------------------------------------------------------
1569 
1570   // Record sp-based slot for receiver on stack for non-static methods.
1571   int receiver_offset = -1;
1572 
1573   //--------------------------------------------------------------------
1574   // We move the arguments backwards because the floating point registers
1575   // destination will always be to a register with a greater or equal
1576   // register number or the stack.
1577   //   jix is the index of the incoming Java arguments.
1578   //   cix is the index of the outgoing C arguments.
1579   //--------------------------------------------------------------------
1580 
1581 #ifdef ASSERT
1582   bool reg_destroyed[RegisterImpl::number_of_registers];
1583   bool freg_destroyed[FloatRegisterImpl::number_of_registers];
1584   for (int r = 0; r < RegisterImpl::number_of_registers; r++) {
1585     reg_destroyed[r] = false;
1586   }
1587   for (int f = 0; f < FloatRegisterImpl::number_of_registers; f++) {
1588     freg_destroyed[f] = false;
1589   }
1590 #endif // ASSERT
1591 
1592   for (int jix = total_in_args - 1, cix = total_c_args - 1; jix >= 0; jix--, cix--) {
1593 #ifdef ASSERT
1594     if (in_regs[jix].first()->is_Register()) {
1595       assert(!reg_destroyed[in_regs[jix].first()->as_Register()->encoding()], "ack!");
1596     } else {
1597       if (in_regs[jix].first()->is_FloatRegister()) {
1598         assert(!freg_destroyed[in_regs[jix].first()->as_FloatRegister()->encoding()], "ack!");
1599       }
1600     }
1601     if (out_regs[cix].first()->is_Register()) {
1602       reg_destroyed[out_regs[cix].first()->as_Register()->encoding()] = true;
1603     } else {
1604       if (out_regs[cix].first()->is_FloatRegister()) {
1605         freg_destroyed[out_regs[cix].first()->as_FloatRegister()->encoding()] = true;
1606       }
1607     }
1608 #endif // ASSERT
1609 
1610     switch (in_sig_bt[jix]) {
1611       // Due to casting, small integers should only occur in pairs with type T_LONG.
1612       case T_BOOLEAN:
1613       case T_CHAR:
1614       case T_BYTE:
1615       case T_SHORT:
1616       case T_INT:
1617         // Move int and do sign extension.
1618         move32_64(masm, in_regs[jix], out_regs[cix], stack_slots);
1619         break;
1620 
1621       case T_LONG :
1622         long_move(masm, in_regs[jix], out_regs[cix], stack_slots);
1623         break;
1624 
1625       case T_ARRAY:
1626       case T_OBJECT:
1627         object_move(masm, map, oop_handle_slot_offset, stack_slots, in_regs[jix], out_regs[cix],
1628                     ((jix == 0) && (!method_is_static)),
1629                     &receiver_offset);
1630         break;
1631       case T_VOID:
1632         break;
1633 
1634       case T_FLOAT:
1635         float_move(masm, in_regs[jix], out_regs[cix], stack_slots, workspace_slot_offset);
1636         break;
1637 
1638       case T_DOUBLE:
1639         assert(jix+1 <  total_in_args && in_sig_bt[jix+1]  == T_VOID && out_sig_bt[cix+1] == T_VOID, "bad arg list");
1640         double_move(masm, in_regs[jix], out_regs[cix], stack_slots, workspace_slot_offset);
1641         break;
1642 
1643       case T_ADDRESS:
1644         assert(false, "found T_ADDRESS in java args");
1645         break;
1646 
1647       default:
1648         ShouldNotReachHere();
1649     }
1650   }
1651 
1652   //--------------------------------------------------------------------
1653   // Pre-load a static method's oop into ARG2.
1654   // Used both by locking code and the normal JNI call code.
1655   //--------------------------------------------------------------------
1656   if (method_is_static) {
1657     __ set_oop_constant(JNIHandles::make_local(method->method_holder()->java_mirror()), Z_ARG2);
1658 
1659     // Now handlize the static class mirror in ARG2. It's known not-null.
1660     __ z_stg(Z_ARG2, klass_offset, Z_SP);
1661     map->set_oop(VMRegImpl::stack2reg(klass_slot_offset));
1662     __ add2reg(Z_ARG2, klass_offset, Z_SP);
1663   }
1664 
1665   // Get JNIEnv* which is first argument to native.
1666   __ add2reg(Z_ARG1, in_bytes(JavaThread::jni_environment_offset()), Z_thread);
1667 
1668   //////////////////////////////////////////////////////////////////////
1669   // We have all of the arguments setup at this point.
1670   // We MUST NOT touch any outgoing regs from this point on.
1671   // So if we must call out we must push a new frame.
1672   //////////////////////////////////////////////////////////////////////
1673 
1674 
1675   // Calc the current pc into Z_R10 and into wrapper_CRegsSet.
1676   // Both values represent the same position.
1677   __ get_PC(Z_R10);                // PC into register
1678   wrapper_CRegsSet = __ offset();  // and into into variable.
1679 
1680   // Z_R10 now has the pc loaded that we will use when we finally call to native.
1681 
1682   // We use the same pc/oopMap repeatedly when we call out.
1683   oop_maps->add_gc_map((int)(wrapper_CRegsSet-wrapper_CodeStart), map);
1684 
1685   // Lock a synchronized method.
1686 
1687   if (method->is_synchronized()) {
1688 
1689     // ATTENTION: args and Z_R10 must be preserved.
1690     Register r_oop  = Z_R11;
1691     Register r_box  = Z_R12;
1692     Register r_tmp1 = Z_R13;
1693     Register r_tmp2 = Z_R7;
1694     Label done;
1695 
1696     // Load the oop for the object or class. R_carg2_classorobject contains
1697     // either the handlized oop from the incoming arguments or the handlized
1698     // class mirror (if the method is static).
1699     __ z_lg(r_oop, 0, Z_ARG2);
1700 
1701     // Try fastpath for locking.
1702     // Fast_lock kills r_temp_1, r_temp_2. (Don't use R1 as temp, won't work!)
1703     __ compiler_fast_lock_object(r_oop, r_box, r_tmp1, r_tmp2);
1704     __ z_bre(done);
1705 
1706     //-------------------------------------------------------------------------
1707     // None of the above fast optimizations worked so we have to get into the
1708     // slow case of monitor enter. Inline a special case of call_VM that
1709     // disallows any pending_exception.
1710     //-------------------------------------------------------------------------
1711 
1712     Register oldSP = Z_R11;
1713 
1714     __ z_lgr(oldSP, Z_SP);
1715 
1716     RegisterSaver::save_live_registers(masm, RegisterSaver::arg_registers);
1717 
1718     // Prepare arguments for call.
1719     __ z_lg(Z_ARG1, 0, Z_ARG2); // Ynboxed class mirror or unboxed object.
1720     __ z_lgr(Z_ARG3, Z_thread);
1721 
1722     __ set_last_Java_frame(oldSP, Z_R10 /* gc map pc */);
1723 
1724     // Do the call.
1725     __ load_const_optimized(Z_R1_scratch, CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_locking_C));
1726     __ call(Z_R1_scratch);
1727 
1728     __ reset_last_Java_frame();
1729 
1730     RegisterSaver::restore_live_registers(masm, RegisterSaver::arg_registers);
1731 #ifdef ASSERT
1732     { Label L;
1733       __ load_and_test_long(Z_R0, Address(Z_thread, Thread::pending_exception_offset()));
1734       __ z_bre(L);
1735       __ stop("no pending exception allowed on exit from IR::monitorenter");
1736       __ bind(L);
1737     }
1738 #endif
1739     __ bind(done);
1740   } // lock for synchronized methods
1741 
1742 
1743   //////////////////////////////////////////////////////////////////////
1744   // Finally just about ready to make the JNI call.
1745   //////////////////////////////////////////////////////////////////////
1746 
1747   // Use that pc we placed in Z_R10 a while back as the current frame anchor.
1748   __ set_last_Java_frame(Z_SP, Z_R10);
1749 
1750   // Transition from _thread_in_Java to _thread_in_native.
1751   __ set_thread_state(_thread_in_native);
1752 
1753   //////////////////////////////////////////////////////////////////////
1754   // This is the JNI call.
1755   //////////////////////////////////////////////////////////////////////
1756 
1757   __ call_c(native_func);
1758 
1759 
1760   //////////////////////////////////////////////////////////////////////
1761   // We have survived the call once we reach here.
1762   //////////////////////////////////////////////////////////////////////
1763 
1764 
1765   //--------------------------------------------------------------------
1766   // Unpack native results.
1767   //--------------------------------------------------------------------
1768   // For int-types, we do any needed sign-extension required.
1769   // Care must be taken that the return value (in Z_ARG1 = Z_RET = Z_R2
1770   // or in Z_FARG0 = Z_FRET = Z_F0) will survive any VM calls for
1771   // blocking or unlocking.
1772   // An OOP result (handle) is done specially in the slow-path code.
1773   //--------------------------------------------------------------------
1774   switch (ret_type) {
1775     case T_VOID:    break;         // Nothing to do!
1776     case T_FLOAT:   break;         // Got it where we want it (unless slow-path)
1777     case T_DOUBLE:  break;         // Got it where we want it (unless slow-path)
1778     case T_LONG:    break;         // Got it where we want it (unless slow-path)
1779     case T_OBJECT:  break;         // Really a handle.
1780                                    // Cannot de-handlize until after reclaiming jvm_lock.
1781     case T_ARRAY:   break;
1782 
1783     case T_BOOLEAN:                // 0 -> false(0); !0 -> true(1)
1784       __ z_lngfr(Z_RET, Z_RET);    // Force sign bit on except for zero.
1785       __ z_srlg(Z_RET, Z_RET, 63); // Shift sign bit into least significant pos.
1786       break;
1787     case T_BYTE:    __ z_lgbr(Z_RET, Z_RET);  break; // sign extension
1788     case T_CHAR:    __ z_llghr(Z_RET, Z_RET); break; // unsigned result
1789     case T_SHORT:   __ z_lghr(Z_RET, Z_RET);  break; // sign extension
1790     case T_INT:     __ z_lgfr(Z_RET, Z_RET);  break; // sign-extend for beauty.
1791 
1792     default:
1793       ShouldNotReachHere();
1794       break;
1795   }
1796 
1797   Label after_transition;
1798 
1799   // Switch thread to "native transition" state before reading the synchronization state.
1800   // This additional state is necessary because reading and testing the synchronization
1801   // state is not atomic w.r.t. GC, as this scenario demonstrates:
1802   //   - Java thread A, in _thread_in_native state, loads _not_synchronized and is preempted.
1803   //   - VM thread changes sync state to synchronizing and suspends threads for GC.
1804   //   - Thread A is resumed to finish this native method, but doesn't block here since it
1805   //     didn't see any synchronization in progress, and escapes.
1806 
1807   // Transition from _thread_in_native to _thread_in_native_trans.
1808   __ set_thread_state(_thread_in_native_trans);
1809 
1810   // Safepoint synchronization
1811   //--------------------------------------------------------------------
1812   // Must we block?
1813   //--------------------------------------------------------------------
1814   // Block, if necessary, before resuming in _thread_in_Java state.
1815   // In order for GC to work, don't clear the last_Java_sp until after blocking.
1816   //--------------------------------------------------------------------
1817   {
1818     Label no_block, sync;
1819 
1820     save_native_result(masm, ret_type, workspace_slot_offset); // Make Z_R2 available as work reg.
1821 
1822     // Force this write out before the read below.
1823     __ z_fence();
1824 
1825     __ safepoint_poll(sync, Z_R1);
1826 
1827     __ load_and_test_int(Z_R0, Address(Z_thread, JavaThread::suspend_flags_offset()));
1828     __ z_bre(no_block);
1829 
1830     // Block. Save any potential method result value before the operation and
1831     // use a leaf call to leave the last_Java_frame setup undisturbed. Doing this
1832     // lets us share the oopMap we used when we went native rather than create
1833     // a distinct one for this pc.
1834     //
1835     __ bind(sync);
1836     __ z_acquire();
1837 
1838     address entry_point = CAST_FROM_FN_PTR(address, JavaThread::check_special_condition_for_native_trans);
1839 
1840     __ call_VM_leaf(entry_point, Z_thread);
1841 
1842     __ bind(no_block);
1843     restore_native_result(masm, ret_type, workspace_slot_offset);
1844   }
1845 
1846   //--------------------------------------------------------------------
1847   // Thread state is thread_in_native_trans. Any safepoint blocking has
1848   // already happened so we can now change state to _thread_in_Java.
1849   //--------------------------------------------------------------------
1850   // Transition from _thread_in_native_trans to _thread_in_Java.
1851   __ set_thread_state(_thread_in_Java);
1852   __ bind(after_transition);
1853 
1854   //--------------------------------------------------------------------
1855   // Reguard any pages if necessary.
1856   // Protect native result from being destroyed.
1857   //--------------------------------------------------------------------
1858 
1859   Label no_reguard;
1860 
1861   __ z_cli(Address(Z_thread, JavaThread::stack_guard_state_offset() + in_ByteSize(sizeof(StackOverflow::StackGuardState) - 1)),
1862            StackOverflow::stack_guard_yellow_reserved_disabled);
1863 
1864   __ z_bre(no_reguard);
1865 
1866   save_native_result(masm, ret_type, workspace_slot_offset);
1867   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::reguard_yellow_pages), Z_method);
1868   restore_native_result(masm, ret_type, workspace_slot_offset);
1869 
1870   __ bind(no_reguard);
1871 
1872 
1873   // Synchronized methods (slow path only)
1874   // No pending exceptions for now.
1875   //--------------------------------------------------------------------
1876   // Handle possibly pending exception (will unlock if necessary).
1877   // Native result is, if any is live, in Z_FRES or Z_RES.
1878   //--------------------------------------------------------------------
1879   // Unlock
1880   //--------------------------------------------------------------------
1881   if (method->is_synchronized()) {
1882     const Register r_oop        = Z_R11;
1883     const Register r_box        = Z_R12;
1884     const Register r_tmp1       = Z_R13;
1885     const Register r_tmp2       = Z_R7;
1886     Label done;
1887 
1888     // Get unboxed oop of class mirror or object ...
1889     int   offset = method_is_static ? klass_offset : receiver_offset;
1890 
1891     assert(offset != -1, "");
1892     __ z_lg(r_oop, offset, Z_SP);
1893 
1894     // Try fastpath for unlocking.
1895     __ compiler_fast_unlock_object(r_oop, r_box, r_tmp1, r_tmp2); // Don't use R1 as temp.
1896     __ z_bre(done);
1897 
1898     // Slow path for unlocking.
1899     // Save and restore any potential method result value around the unlocking operation.
1900     const Register R_exc = Z_R11;
1901 
1902     save_native_result(masm, ret_type, workspace_slot_offset);
1903 
1904     // Must save pending exception around the slow-path VM call. Since it's a
1905     // leaf call, the pending exception (if any) can be kept in a register.
1906     __ z_lg(R_exc, Address(Z_thread, Thread::pending_exception_offset()));
1907     assert(R_exc->is_nonvolatile(), "exception register must be non-volatile");
1908 
1909     // Must clear pending-exception before re-entering the VM. Since this is
1910     // a leaf call, pending-exception-oop can be safely kept in a register.
1911     __ clear_mem(Address(Z_thread, Thread::pending_exception_offset()), sizeof(intptr_t));
1912 
1913     // Inline a special case of call_VM that disallows any pending_exception.
1914 
1915     // Get locked oop from the handle we passed to jni.
1916     __ z_lg(Z_ARG1, offset, Z_SP);
1917     __ z_lgr(Z_ARG2, Z_thread);
1918 
1919     __ load_const_optimized(Z_R1_scratch, CAST_FROM_FN_PTR(address, SharedRuntime::complete_monitor_unlocking_C));
1920 
1921     __ call(Z_R1_scratch);
1922 
1923 #ifdef ASSERT
1924     {
1925       Label L;
1926       __ load_and_test_long(Z_R0, Address(Z_thread, Thread::pending_exception_offset()));
1927       __ z_bre(L);
1928       __ stop("no pending exception allowed on exit from IR::monitorexit");
1929       __ bind(L);
1930     }
1931 #endif
1932 
1933     // Check_forward_pending_exception jump to forward_exception if any pending
1934     // exception is set. The forward_exception routine expects to see the
1935     // exception in pending_exception and not in a register. Kind of clumsy,
1936     // since all folks who branch to forward_exception must have tested
1937     // pending_exception first and hence have it in a register already.
1938     __ z_stg(R_exc, Address(Z_thread, Thread::pending_exception_offset()));
1939     restore_native_result(masm, ret_type, workspace_slot_offset);
1940     __ z_bru(done);
1941     __ z_illtrap(0x66);
1942 
1943     __ bind(done);
1944   }
1945 
1946 
1947   //--------------------------------------------------------------------
1948   // Clear "last Java frame" SP and PC.
1949   //--------------------------------------------------------------------
1950   __ verify_thread(); // Z_thread must be correct.
1951 
1952   __ reset_last_Java_frame();
1953 
1954   // Unpack oop result, e.g. JNIHandles::resolve result.
1955   if (is_reference_type(ret_type)) {
1956     __ resolve_jobject(Z_RET, /* tmp1 */ Z_R13, /* tmp2 */ Z_R7);
1957   }
1958 
1959   if (CheckJNICalls) {
1960     // clear_pending_jni_exception_check
1961     __ clear_mem(Address(Z_thread, JavaThread::pending_jni_exception_check_fn_offset()), sizeof(oop));
1962   }
1963 
1964   // Reset handle block.
1965   __ z_lg(Z_R1_scratch, Address(Z_thread, JavaThread::active_handles_offset()));
1966   __ clear_mem(Address(Z_R1_scratch, JNIHandleBlock::top_offset_in_bytes()), 4);
1967 
1968   // Check for pending exceptions.
1969   __ load_and_test_long(Z_R0, Address(Z_thread, Thread::pending_exception_offset()));
1970   __ z_brne(handle_pending_exception);
1971 
1972 
1973   //////////////////////////////////////////////////////////////////////
1974   // Return
1975   //////////////////////////////////////////////////////////////////////
1976 
1977 
1978 #ifndef USE_RESIZE_FRAME
1979   __ pop_frame();                     // Pop wrapper frame.
1980 #else
1981   __ resize_frame(frame_size_in_bytes, Z_R0_scratch);  // Revert stack extension.
1982 #endif
1983   __ restore_return_pc();             // This is the way back to the caller.
1984   __ z_br(Z_R14);
1985 
1986 
1987   //////////////////////////////////////////////////////////////////////
1988   // Out-of-line calls to the runtime.
1989   //////////////////////////////////////////////////////////////////////
1990 
1991 
1992   //---------------------------------------------------------------------
1993   // Handler for pending exceptions (out-of-line).
1994   //---------------------------------------------------------------------
1995   // Since this is a native call, we know the proper exception handler
1996   // is the empty function. We just pop this frame and then jump to
1997   // forward_exception_entry. Z_R14 will contain the native caller's
1998   // return PC.
1999   __ bind(handle_pending_exception);
2000   __ pop_frame();
2001   __ load_const_optimized(Z_R1_scratch, StubRoutines::forward_exception_entry());
2002   __ restore_return_pc();
2003   __ z_br(Z_R1_scratch);
2004 
2005   //---------------------------------------------------------------------
2006   // Handler for a cache miss (out-of-line)
2007   //---------------------------------------------------------------------
2008   __ call_ic_miss_handler(ic_miss, 0x77, 0, Z_R1_scratch);
2009   __ flush();
2010 
2011 
2012   //////////////////////////////////////////////////////////////////////
2013   // end of code generation
2014   //////////////////////////////////////////////////////////////////////
2015 
2016 
2017   nmethod *nm = nmethod::new_native_nmethod(method,
2018                                             compile_id,
2019                                             masm->code(),
2020                                             (int)(wrapper_VEPStart-wrapper_CodeStart),
2021                                             (int)(wrapper_FrameDone-wrapper_CodeStart),
2022                                             stack_slots / VMRegImpl::slots_per_word,
2023                                             (method_is_static ? in_ByteSize(klass_offset) : in_ByteSize(receiver_offset)),
2024                                             oop_maps);
2025 
2026   return nm;
2027 }
2028 
2029 static address gen_c2i_adapter(MacroAssembler  *masm,
2030                                int total_args_passed,
2031                                int comp_args_on_stack,
2032                                const BasicType *sig_bt,
2033                                const VMRegPair *regs,
2034                                Label &skip_fixup) {
2035   // Before we get into the guts of the C2I adapter, see if we should be here
2036   // at all. We've come from compiled code and are attempting to jump to the
2037   // interpreter, which means the caller made a static call to get here
2038   // (vcalls always get a compiled target if there is one). Check for a
2039   // compiled target. If there is one, we need to patch the caller's call.
2040 
2041   // These two defs MUST MATCH code in gen_i2c2i_adapter!
2042   const Register ientry = Z_R11;
2043   const Register code   = Z_R11;
2044 
2045   address c2i_entrypoint;
2046   Label   patch_callsite;
2047 
2048   // Regular (verified) c2i entry point.
2049   c2i_entrypoint = __ pc();
2050 
2051   // Call patching needed?
2052   __ load_and_test_long(Z_R0_scratch, method_(code));
2053   __ z_lg(ientry, method_(interpreter_entry));  // Preload interpreter entry (also if patching).
2054   __ z_brne(patch_callsite);                    // Patch required if code != NULL (compiled target exists).
2055 
2056   __ bind(skip_fixup);  // Return point from patch_callsite.
2057 
2058   // Since all args are passed on the stack, total_args_passed*wordSize is the
2059   // space we need. We need ABI scratch area but we use the caller's since
2060   // it has already been allocated.
2061 
2062   const int abi_scratch = frame::z_top_ijava_frame_abi_size;
2063   int       extraspace  = align_up(total_args_passed, 2)*wordSize + abi_scratch;
2064   Register  sender_SP   = Z_R10;
2065   Register  value       = Z_R12;
2066 
2067   // Remember the senderSP so we can pop the interpreter arguments off of the stack.
2068   // In addition, frame manager expects initial_caller_sp in Z_R10.
2069   __ z_lgr(sender_SP, Z_SP);
2070 
2071   // This should always fit in 14 bit immediate.
2072   __ resize_frame(-extraspace, Z_R0_scratch);
2073 
2074   // We use the caller's ABI scratch area (out_preserved_stack_slots) for the initial
2075   // args. This essentially moves the callers ABI scratch area from the top to the
2076   // bottom of the arg area.
2077 
2078   int st_off =  extraspace - wordSize;
2079 
2080   // Now write the args into the outgoing interpreter space.
2081   for (int i = 0; i < total_args_passed; i++) {
2082     VMReg r_1 = regs[i].first();
2083     VMReg r_2 = regs[i].second();
2084     if (!r_1->is_valid()) {
2085       assert(!r_2->is_valid(), "");
2086       continue;
2087     }
2088     if (r_1->is_stack()) {
2089       // The calling convention produces OptoRegs that ignore the preserve area (abi scratch).
2090       // We must account for it here.
2091       int ld_off = (r_1->reg2stack() + SharedRuntime::out_preserve_stack_slots()) * VMRegImpl::stack_slot_size;
2092 
2093       if (!r_2->is_valid()) {
2094         __ z_mvc(Address(Z_SP, st_off), Address(sender_SP, ld_off), sizeof(void*));
2095       } else {
2096         // longs are given 2 64-bit slots in the interpreter,
2097         // but the data is passed in only 1 slot.
2098         if (sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) {
2099 #ifdef ASSERT
2100           __ clear_mem(Address(Z_SP, st_off), sizeof(void *));
2101 #endif
2102           st_off -= wordSize;
2103         }
2104         __ z_mvc(Address(Z_SP, st_off), Address(sender_SP, ld_off), sizeof(void*));
2105       }
2106     } else {
2107       if (r_1->is_Register()) {
2108         if (!r_2->is_valid()) {
2109           __ z_st(r_1->as_Register(), st_off, Z_SP);
2110         } else {
2111           // longs are given 2 64-bit slots in the interpreter, but the
2112           // data is passed in only 1 slot.
2113           if (sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) {
2114 #ifdef ASSERT
2115             __ clear_mem(Address(Z_SP, st_off), sizeof(void *));
2116 #endif
2117             st_off -= wordSize;
2118           }
2119           __ z_stg(r_1->as_Register(), st_off, Z_SP);
2120         }
2121       } else {
2122         assert(r_1->is_FloatRegister(), "");
2123         if (!r_2->is_valid()) {
2124           __ z_ste(r_1->as_FloatRegister(), st_off, Z_SP);
2125         } else {
2126           // In 64bit, doubles are given 2 64-bit slots in the interpreter, but the
2127           // data is passed in only 1 slot.
2128           // One of these should get known junk...
2129 #ifdef ASSERT
2130           __ z_lzdr(Z_F1);
2131           __ z_std(Z_F1, st_off, Z_SP);
2132 #endif
2133           st_off-=wordSize;
2134           __ z_std(r_1->as_FloatRegister(), st_off, Z_SP);
2135         }
2136       }
2137     }
2138     st_off -= wordSize;
2139   }
2140 
2141 
2142   // Jump to the interpreter just as if interpreter was doing it.
2143   __ add2reg(Z_esp, st_off, Z_SP);
2144 
2145   // Frame_manager expects initial_caller_sp (= SP without resize by c2i) in Z_R10.
2146   __ z_br(ientry);
2147 
2148 
2149   // Prevent illegal entry to out-of-line code.
2150   __ z_illtrap(0x22);
2151 
2152   // Generate out-of-line runtime call to patch caller,
2153   // then continue as interpreted.
2154 
2155   // IF you lose the race you go interpreted.
2156   // We don't see any possible endless c2i -> i2c -> c2i ...
2157   // transitions no matter how rare.
2158   __ bind(patch_callsite);
2159 
2160   RegisterSaver::save_live_registers(masm, RegisterSaver::arg_registers);
2161   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::fixup_callers_callsite), Z_method, Z_R14);
2162   RegisterSaver::restore_live_registers(masm, RegisterSaver::arg_registers);
2163   __ z_bru(skip_fixup);
2164 
2165   // end of out-of-line code
2166 
2167   return c2i_entrypoint;
2168 }
2169 
2170 // On entry, the following registers are set
2171 //
2172 //    Z_thread  r8  - JavaThread*
2173 //    Z_method  r9  - callee's method (method to be invoked)
2174 //    Z_esp     r7  - operand (or expression) stack pointer of caller. one slot above last arg.
2175 //    Z_SP      r15 - SP prepared by call stub such that caller's outgoing args are near top
2176 //
2177 void SharedRuntime::gen_i2c_adapter(MacroAssembler *masm,
2178                                     int total_args_passed,
2179                                     int comp_args_on_stack,
2180                                     const BasicType *sig_bt,
2181                                     const VMRegPair *regs) {
2182   const Register value = Z_R12;
2183   const Register ld_ptr= Z_esp;
2184 
2185   int ld_offset = total_args_passed * wordSize;
2186 
2187   // Cut-out for having no stack args.
2188   if (comp_args_on_stack) {
2189     // Sig words on the stack are greater than VMRegImpl::stack0. Those in
2190     // registers are below. By subtracting stack0, we either get a negative
2191     // number (all values in registers) or the maximum stack slot accessed.
2192     // Convert VMRegImpl (4 byte) stack slots to words.
2193     int comp_words_on_stack = align_up(comp_args_on_stack*VMRegImpl::stack_slot_size, wordSize)>>LogBytesPerWord;
2194     // Round up to miminum stack alignment, in wordSize
2195     comp_words_on_stack = align_up(comp_words_on_stack, 2);
2196 
2197     __ resize_frame(-comp_words_on_stack*wordSize, Z_R0_scratch);
2198   }
2199 
2200   // Now generate the shuffle code. Pick up all register args and move the
2201   // rest through register value=Z_R12.
2202   for (int i = 0; i < total_args_passed; i++) {
2203     if (sig_bt[i] == T_VOID) {
2204       assert(i > 0 && (sig_bt[i-1] == T_LONG || sig_bt[i-1] == T_DOUBLE), "missing half");
2205       continue;
2206     }
2207 
2208     // Pick up 0, 1 or 2 words from ld_ptr.
2209     assert(!regs[i].second()->is_valid() || regs[i].first()->next() == regs[i].second(),
2210            "scrambled load targets?");
2211     VMReg r_1 = regs[i].first();
2212     VMReg r_2 = regs[i].second();
2213     if (!r_1->is_valid()) {
2214       assert(!r_2->is_valid(), "");
2215       continue;
2216     }
2217     if (r_1->is_FloatRegister()) {
2218       if (!r_2->is_valid()) {
2219         __ z_le(r_1->as_FloatRegister(), ld_offset, ld_ptr);
2220         ld_offset-=wordSize;
2221       } else {
2222         // Skip the unused interpreter slot.
2223         __ z_ld(r_1->as_FloatRegister(), ld_offset - wordSize, ld_ptr);
2224         ld_offset -= 2 * wordSize;
2225       }
2226     } else {
2227       if (r_1->is_stack()) {
2228         // Must do a memory to memory move.
2229         int st_off = (r_1->reg2stack() + SharedRuntime::out_preserve_stack_slots()) * VMRegImpl::stack_slot_size;
2230 
2231         if (!r_2->is_valid()) {
2232           __ z_mvc(Address(Z_SP, st_off), Address(ld_ptr, ld_offset), sizeof(void*));
2233         } else {
2234           // In 64bit, longs are given 2 64-bit slots in the interpreter, but the
2235           // data is passed in only 1 slot.
2236           if (sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) {
2237             ld_offset -= wordSize;
2238           }
2239           __ z_mvc(Address(Z_SP, st_off), Address(ld_ptr, ld_offset), sizeof(void*));
2240         }
2241       } else {
2242         if (!r_2->is_valid()) {
2243           // Not sure we need to do this but it shouldn't hurt.
2244           if (is_reference_type(sig_bt[i]) || sig_bt[i] == T_ADDRESS) {
2245             __ z_lg(r_1->as_Register(), ld_offset, ld_ptr);
2246           } else {
2247             __ z_l(r_1->as_Register(), ld_offset, ld_ptr);
2248           }
2249         } else {
2250           // In 64bit, longs are given 2 64-bit slots in the interpreter, but the
2251           // data is passed in only 1 slot.
2252           if (sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) {
2253             ld_offset -= wordSize;
2254           }
2255           __ z_lg(r_1->as_Register(), ld_offset, ld_ptr);
2256         }
2257       }
2258       ld_offset -= wordSize;
2259     }
2260   }
2261 
2262   // Jump to the compiled code just as if compiled code was doing it.
2263   // load target address from method:
2264   __ z_lg(Z_R1_scratch, Address(Z_method, Method::from_compiled_offset()));
2265 
2266   // Store method into thread->callee_target.
2267   // 6243940: We might end up in handle_wrong_method if
2268   // the callee is deoptimized as we race thru here. If that
2269   // happens we don't want to take a safepoint because the
2270   // caller frame will look interpreted and arguments are now
2271   // "compiled" so it is much better to make this transition
2272   // invisible to the stack walking code. Unfortunately, if
2273   // we try and find the callee by normal means a safepoint
2274   // is possible. So we stash the desired callee in the thread
2275   // and the vm will find it there should this case occur.
2276   __ z_stg(Z_method, thread_(callee_target));
2277 
2278   __ z_br(Z_R1_scratch);
2279 }
2280 
2281 AdapterHandlerEntry* SharedRuntime::generate_i2c2i_adapters(MacroAssembler *masm,
2282                                                             int total_args_passed,
2283                                                             int comp_args_on_stack,
2284                                                             const BasicType *sig_bt,
2285                                                             const VMRegPair *regs,
2286                                                             AdapterFingerPrint* fingerprint) {
2287   __ align(CodeEntryAlignment);
2288   address i2c_entry = __ pc();
2289   gen_i2c_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs);
2290 
2291   address c2i_unverified_entry;
2292 
2293   Label skip_fixup;
2294   {
2295     Label ic_miss;
2296     const int klass_offset           = oopDesc::klass_offset_in_bytes();
2297     const int holder_klass_offset    = CompiledICHolder::holder_klass_offset();
2298     const int holder_metadata_offset = CompiledICHolder::holder_metadata_offset();
2299 
2300     // Out-of-line call to ic_miss handler.
2301     __ call_ic_miss_handler(ic_miss, 0x11, 0, Z_R1_scratch);
2302 
2303     // Unverified Entry Point UEP
2304     __ align(CodeEntryAlignment);
2305     c2i_unverified_entry = __ pc();
2306 
2307     // Check the pointers.
2308     if (!ImplicitNullChecks || MacroAssembler::needs_explicit_null_check(klass_offset)) {
2309       __ z_ltgr(Z_ARG1, Z_ARG1);
2310       __ z_bre(ic_miss);
2311     }
2312     __ verify_oop(Z_ARG1, FILE_AND_LINE);
2313 
2314     // Check ic: object class <-> cached class
2315     // Compress cached class for comparison. That's more efficient.
2316     if (UseCompressedClassPointers) {
2317       __ z_lg(Z_R11, holder_klass_offset, Z_method);             // Z_R11 is overwritten a few instructions down anyway.
2318       __ compare_klass_ptr(Z_R11, klass_offset, Z_ARG1, false); // Cached class can't be zero.
2319     } else {
2320       __ z_clc(klass_offset, sizeof(void *)-1, Z_ARG1, holder_klass_offset, Z_method);
2321     }
2322     __ z_brne(ic_miss);  // Cache miss: call runtime to handle this.
2323 
2324     // This def MUST MATCH code in gen_c2i_adapter!
2325     const Register code = Z_R11;
2326 
2327     __ z_lg(Z_method, holder_metadata_offset, Z_method);
2328     __ load_and_test_long(Z_R0, method_(code));
2329     __ z_brne(ic_miss);  // Cache miss: call runtime to handle this.
2330 
2331     // Fallthru to VEP. Duplicate LTG, but saved taken branch.
2332   }
2333 
2334   address c2i_entry = __ pc();
2335 
2336   // Class initialization barrier for static methods
2337   address c2i_no_clinit_check_entry = NULL;
2338   if (VM_Version::supports_fast_class_init_checks()) {
2339     Label L_skip_barrier;
2340 
2341     { // Bypass the barrier for non-static methods
2342       __ testbit(Address(Z_method, Method::access_flags_offset()), JVM_ACC_STATIC_BIT);
2343       __ z_bfalse(L_skip_barrier); // non-static
2344     }
2345 
2346     Register klass = Z_R11;
2347     __ load_method_holder(klass, Z_method);
2348     __ clinit_barrier(klass, Z_thread, &L_skip_barrier /*L_fast_path*/);
2349 
2350     __ load_const_optimized(klass, SharedRuntime::get_handle_wrong_method_stub());
2351     __ z_br(klass);
2352 
2353     __ bind(L_skip_barrier);
2354     c2i_no_clinit_check_entry = __ pc();
2355   }
2356 
2357   gen_c2i_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs, skip_fixup);
2358 
2359   return AdapterHandlerLibrary::new_entry(fingerprint, i2c_entry, c2i_entry, c2i_unverified_entry, c2i_no_clinit_check_entry);
2360 }
2361 
2362 // This function returns the adjust size (in number of words) to a c2i adapter
2363 // activation for use during deoptimization.
2364 //
2365 // Actually only compiled frames need to be adjusted, but it
2366 // doesn't harm to adjust entry and interpreter frames, too.
2367 //
2368 int Deoptimization::last_frame_adjust(int callee_parameters, int callee_locals) {
2369   assert(callee_locals >= callee_parameters,
2370           "test and remove; got more parms than locals");
2371   // Handle the abi adjustment here instead of doing it in push_skeleton_frames.
2372   return (callee_locals - callee_parameters) * Interpreter::stackElementWords +
2373          frame::z_parent_ijava_frame_abi_size / BytesPerWord;
2374 }
2375 
2376 uint SharedRuntime::in_preserve_stack_slots() {
2377   return frame::jit_in_preserve_size_in_4_byte_units;
2378 }
2379 
2380 uint SharedRuntime::out_preserve_stack_slots() {
2381   return frame::z_jit_out_preserve_size/VMRegImpl::stack_slot_size;
2382 }
2383 
2384 //
2385 // Frame generation for deopt and uncommon trap blobs.
2386 //
2387 static void push_skeleton_frame(MacroAssembler* masm,
2388                           /* Unchanged */
2389                           Register frame_sizes_reg,
2390                           Register pcs_reg,
2391                           /* Invalidate */
2392                           Register frame_size_reg,
2393                           Register pc_reg) {
2394   BLOCK_COMMENT("  push_skeleton_frame {");
2395    __ z_lg(pc_reg, 0, pcs_reg);
2396    __ z_lg(frame_size_reg, 0, frame_sizes_reg);
2397    __ z_stg(pc_reg, _z_abi(return_pc), Z_SP);
2398    Register fp = pc_reg;
2399    __ push_frame(frame_size_reg, fp);
2400 #ifdef ASSERT
2401    // The magic is required for successful walking skeletal frames.
2402    __ load_const_optimized(frame_size_reg/*tmp*/, frame::z_istate_magic_number);
2403    __ z_stg(frame_size_reg, _z_ijava_state_neg(magic), fp);
2404    // Fill other slots that are supposedly not necessary with eye catchers.
2405    __ load_const_optimized(frame_size_reg/*use as tmp*/, 0xdeadbad1);
2406    __ z_stg(frame_size_reg, _z_ijava_state_neg(top_frame_sp), fp);
2407    // The sender_sp of the bottom frame is set before pushing it.
2408    // The sender_sp of non bottom frames is their caller's top_frame_sp, which
2409    // is unknown here. Luckily it is not needed before filling the frame in
2410    // layout_activation(), we assert this by setting an eye catcher (see
2411    // comments on sender_sp in frame_s390.hpp).
2412    __ z_stg(frame_size_reg, _z_ijava_state_neg(sender_sp), Z_SP);
2413 #endif // ASSERT
2414   BLOCK_COMMENT("  } push_skeleton_frame");
2415 }
2416 
2417 // Loop through the UnrollBlock info and create new frames.
2418 static void push_skeleton_frames(MacroAssembler* masm, bool deopt,
2419                             /* read */
2420                             Register unroll_block_reg,
2421                             /* invalidate */
2422                             Register frame_sizes_reg,
2423                             Register number_of_frames_reg,
2424                             Register pcs_reg,
2425                             Register tmp1,
2426                             Register tmp2) {
2427   BLOCK_COMMENT("push_skeleton_frames {");
2428   // _number_of_frames is of type int (deoptimization.hpp).
2429   __ z_lgf(number_of_frames_reg,
2430            Address(unroll_block_reg, Deoptimization::UnrollBlock::number_of_frames_offset_in_bytes()));
2431   __ z_lg(pcs_reg,
2432           Address(unroll_block_reg, Deoptimization::UnrollBlock::frame_pcs_offset_in_bytes()));
2433   __ z_lg(frame_sizes_reg,
2434           Address(unroll_block_reg, Deoptimization::UnrollBlock::frame_sizes_offset_in_bytes()));
2435 
2436   // stack: (caller_of_deoptee, ...).
2437 
2438   // If caller_of_deoptee is a compiled frame, then we extend it to make
2439   // room for the callee's locals and the frame::z_parent_ijava_frame_abi.
2440   // See also Deoptimization::last_frame_adjust() above.
2441   // Note: entry and interpreted frames are adjusted, too. But this doesn't harm.
2442 
2443   __ z_lgf(Z_R1_scratch,
2444            Address(unroll_block_reg, Deoptimization::UnrollBlock::caller_adjustment_offset_in_bytes()));
2445   __ z_lgr(tmp1, Z_SP);  // Save the sender sp before extending the frame.
2446   __ resize_frame_sub(Z_R1_scratch, tmp2/*tmp*/);
2447   // The oldest skeletal frame requires a valid sender_sp to make it walkable
2448   // (it is required to find the original pc of caller_of_deoptee if it is marked
2449   // for deoptimization - see nmethod::orig_pc_addr()).
2450   __ z_stg(tmp1, _z_ijava_state_neg(sender_sp), Z_SP);
2451 
2452   // Now push the new interpreter frames.
2453   Label loop, loop_entry;
2454 
2455   // Make sure that there is at least one entry in the array.
2456   DEBUG_ONLY(__ z_ltgr(number_of_frames_reg, number_of_frames_reg));
2457   __ asm_assert_ne("array_size must be > 0", 0x205);
2458 
2459   __ z_bru(loop_entry);
2460 
2461   __ bind(loop);
2462 
2463   __ add2reg(frame_sizes_reg, wordSize);
2464   __ add2reg(pcs_reg, wordSize);
2465 
2466   __ bind(loop_entry);
2467 
2468   // Allocate a new frame, fill in the pc.
2469   push_skeleton_frame(masm, frame_sizes_reg, pcs_reg, tmp1, tmp2);
2470 
2471   __ z_aghi(number_of_frames_reg, -1);  // Emit AGHI, because it sets the condition code
2472   __ z_brne(loop);
2473 
2474   // Set the top frame's return pc.
2475   __ add2reg(pcs_reg, wordSize);
2476   __ z_lg(Z_R0_scratch, 0, pcs_reg);
2477   __ z_stg(Z_R0_scratch, _z_abi(return_pc), Z_SP);
2478   BLOCK_COMMENT("} push_skeleton_frames");
2479 }
2480 
2481 //------------------------------generate_deopt_blob----------------------------
2482 void SharedRuntime::generate_deopt_blob() {
2483   // Allocate space for the code.
2484   ResourceMark rm;
2485   // Setup code generation tools.
2486   CodeBuffer buffer("deopt_blob", 2048, 1024);
2487   InterpreterMacroAssembler* masm = new InterpreterMacroAssembler(&buffer);
2488   Label exec_mode_initialized;
2489   OopMap* map = NULL;
2490   OopMapSet *oop_maps = new OopMapSet();
2491 
2492   unsigned int start_off = __ offset();
2493   Label cont;
2494 
2495   // --------------------------------------------------------------------------
2496   // Normal entry (non-exception case)
2497   //
2498   // We have been called from the deopt handler of the deoptee.
2499   // Z_R14 points behind the call in the deopt handler. We adjust
2500   // it such that it points to the start of the deopt handler.
2501   // The return_pc has been stored in the frame of the deoptee and
2502   // will replace the address of the deopt_handler in the call
2503   // to Deoptimization::fetch_unroll_info below.
2504   // The (int) cast is necessary, because -((unsigned int)14)
2505   // is an unsigned int.
2506   __ add2reg(Z_R14, -(int)NativeCall::max_instruction_size());
2507 
2508   const Register   exec_mode_reg = Z_tmp_1;
2509 
2510   // stack: (deoptee, caller of deoptee, ...)
2511 
2512   // pushes an "unpack" frame
2513   // R14 contains the return address pointing into the deoptimized
2514   // nmethod that was valid just before the nmethod was deoptimized.
2515   // save R14 into the deoptee frame.  the `fetch_unroll_info'
2516   // procedure called below will read it from there.
2517   map = RegisterSaver::save_live_registers(masm, RegisterSaver::all_registers);
2518 
2519   // note the entry point.
2520   __ load_const_optimized(exec_mode_reg, Deoptimization::Unpack_deopt);
2521   __ z_bru(exec_mode_initialized);
2522 
2523 #ifndef COMPILER1
2524   int reexecute_offset = 1; // odd offset will produce odd pc, which triggers an hardware trap
2525 #else
2526   // --------------------------------------------------------------------------
2527   // Reexecute entry
2528   // - Z_R14 = Deopt Handler in nmethod
2529 
2530   int reexecute_offset = __ offset() - start_off;
2531 
2532   // No need to update map as each call to save_live_registers will produce identical oopmap
2533   (void) RegisterSaver::save_live_registers(masm, RegisterSaver::all_registers);
2534 
2535   __ load_const_optimized(exec_mode_reg, Deoptimization::Unpack_reexecute);
2536   __ z_bru(exec_mode_initialized);
2537 #endif
2538 
2539 
2540   // --------------------------------------------------------------------------
2541   // Exception entry. We reached here via a branch. Registers on entry:
2542   // - Z_EXC_OOP (Z_ARG1) = exception oop
2543   // - Z_EXC_PC  (Z_ARG2) = the exception pc.
2544 
2545   int exception_offset = __ offset() - start_off;
2546 
2547   // all registers are dead at this entry point, except for Z_EXC_OOP, and
2548   // Z_EXC_PC which contain the exception oop and exception pc
2549   // respectively.  Set them in TLS and fall thru to the
2550   // unpack_with_exception_in_tls entry point.
2551 
2552   // Store exception oop and pc in thread (location known to GC).
2553   // Need this since the call to "fetch_unroll_info()" may safepoint.
2554   __ z_stg(Z_EXC_OOP, Address(Z_thread, JavaThread::exception_oop_offset()));
2555   __ z_stg(Z_EXC_PC,  Address(Z_thread, JavaThread::exception_pc_offset()));
2556 
2557   // fall through
2558 
2559   int exception_in_tls_offset = __ offset() - start_off;
2560 
2561   // new implementation because exception oop is now passed in JavaThread
2562 
2563   // Prolog for exception case
2564   // All registers must be preserved because they might be used by LinearScan
2565   // Exceptiop oop and throwing PC are passed in JavaThread
2566 
2567   // load throwing pc from JavaThread and us it as the return address of the current frame.
2568   __ z_lg(Z_R1_scratch, Address(Z_thread, JavaThread::exception_pc_offset()));
2569 
2570   // Save everything in sight.
2571   (void) RegisterSaver::save_live_registers(masm, RegisterSaver::all_registers, Z_R1_scratch);
2572 
2573   // Now it is safe to overwrite any register
2574 
2575   // Clear the exception pc field in JavaThread
2576   __ clear_mem(Address(Z_thread, JavaThread::exception_pc_offset()), 8);
2577 
2578   // Deopt during an exception.  Save exec mode for unpack_frames.
2579   __ load_const_optimized(exec_mode_reg, Deoptimization::Unpack_exception);
2580 
2581 
2582 #ifdef ASSERT
2583   // verify that there is really an exception oop in JavaThread
2584   __ z_lg(Z_ARG1, Address(Z_thread, JavaThread::exception_oop_offset()));
2585   __ MacroAssembler::verify_oop(Z_ARG1, FILE_AND_LINE);
2586 
2587   // verify that there is no pending exception
2588   __ asm_assert_mem8_is_zero(in_bytes(Thread::pending_exception_offset()), Z_thread,
2589                              "must not have pending exception here", __LINE__);
2590 #endif
2591 
2592   // --------------------------------------------------------------------------
2593   // At this point, the live registers are saved and
2594   // the exec_mode_reg has been set up correctly.
2595   __ bind(exec_mode_initialized);
2596 
2597   // stack: ("unpack" frame, deoptee, caller_of_deoptee, ...).
2598 
2599   {
2600   const Register unroll_block_reg  = Z_tmp_2;
2601 
2602   // we need to set `last_Java_frame' because `fetch_unroll_info' will
2603   // call `last_Java_frame()'.  however we can't block and no gc will
2604   // occur so we don't need an oopmap. the value of the pc in the
2605   // frame is not particularly important.  it just needs to identify the blob.
2606 
2607   // Don't set last_Java_pc anymore here (is implicitly NULL then).
2608   // the correct PC is retrieved in pd_last_frame() in that case.
2609   __ set_last_Java_frame(/*sp*/Z_SP, noreg);
2610   // With EscapeAnalysis turned on, this call may safepoint
2611   // despite it's marked as "leaf call"!
2612   __ call_VM_leaf(CAST_FROM_FN_PTR(address, Deoptimization::fetch_unroll_info), Z_thread, exec_mode_reg);
2613   // Set an oopmap for the call site this describes all our saved volatile registers
2614   int offs = __ offset();
2615   oop_maps->add_gc_map(offs, map);
2616 
2617   __ reset_last_Java_frame();
2618   // save the return value.
2619   __ z_lgr(unroll_block_reg, Z_RET);
2620   // restore the return registers that have been saved
2621   // (among other registers) by save_live_registers(...).
2622   RegisterSaver::restore_result_registers(masm);
2623 
2624   // reload the exec mode from the UnrollBlock (it might have changed)
2625   __ z_llgf(exec_mode_reg, Address(unroll_block_reg, Deoptimization::UnrollBlock::unpack_kind_offset_in_bytes()));
2626 
2627   // In excp_deopt_mode, restore and clear exception oop which we
2628   // stored in the thread during exception entry above. The exception
2629   // oop will be the return value of this stub.
2630   NearLabel skip_restore_excp;
2631   __ compare64_and_branch(exec_mode_reg, Deoptimization::Unpack_exception, Assembler::bcondNotEqual, skip_restore_excp);
2632   __ z_lg(Z_RET, thread_(exception_oop));
2633   __ clear_mem(thread_(exception_oop), 8);
2634   __ bind(skip_restore_excp);
2635 
2636   // remove the "unpack" frame
2637   __ pop_frame();
2638 
2639   // stack: (deoptee, caller of deoptee, ...).
2640 
2641   // pop the deoptee's frame
2642   __ pop_frame();
2643 
2644   // stack: (caller_of_deoptee, ...).
2645 
2646   // loop through the `UnrollBlock' info and create interpreter frames.
2647   push_skeleton_frames(masm, true/*deopt*/,
2648                   unroll_block_reg,
2649                   Z_tmp_3,
2650                   Z_tmp_4,
2651                   Z_ARG5,
2652                   Z_ARG4,
2653                   Z_ARG3);
2654 
2655   // stack: (skeletal interpreter frame, ..., optional skeletal
2656   // interpreter frame, caller of deoptee, ...).
2657   }
2658 
2659   // push an "unpack" frame taking care of float / int return values.
2660   __ push_frame(RegisterSaver::live_reg_frame_size(RegisterSaver::all_registers));
2661 
2662   // stack: (unpack frame, skeletal interpreter frame, ..., optional
2663   // skeletal interpreter frame, caller of deoptee, ...).
2664 
2665   // spill live volatile registers since we'll do a call.
2666   __ z_stg(Z_RET, offset_of(frame::z_abi_160_spill, spill[0]), Z_SP);
2667   __ z_std(Z_FRET, offset_of(frame::z_abi_160_spill, spill[1]), Z_SP);
2668 
2669   // let the unpacker layout information in the skeletal frames just allocated.
2670   __ get_PC(Z_RET);
2671   __ set_last_Java_frame(/*sp*/Z_SP, /*pc*/Z_RET);
2672   __ call_VM_leaf(CAST_FROM_FN_PTR(address, Deoptimization::unpack_frames),
2673                   Z_thread/*thread*/, exec_mode_reg/*exec_mode*/);
2674 
2675   __ reset_last_Java_frame();
2676 
2677   // restore the volatiles saved above.
2678   __ z_lg(Z_RET, offset_of(frame::z_abi_160_spill, spill[0]), Z_SP);
2679   __ z_ld(Z_FRET, offset_of(frame::z_abi_160_spill, spill[1]), Z_SP);
2680 
2681   // pop the "unpack" frame.
2682   __ pop_frame();
2683   __ restore_return_pc();
2684 
2685   // stack: (top interpreter frame, ..., optional interpreter frame,
2686   // caller of deoptee, ...).
2687 
2688   __ z_lg(Z_fp, _z_abi(callers_sp), Z_SP); // restore frame pointer
2689   __ restore_bcp();
2690   __ restore_locals();
2691   __ restore_esp();
2692 
2693   // return to the interpreter entry point.
2694   __ z_br(Z_R14);
2695 
2696   // Make sure all code is generated
2697   masm->flush();
2698 
2699   _deopt_blob = DeoptimizationBlob::create(&buffer, oop_maps, 0, exception_offset, reexecute_offset, RegisterSaver::live_reg_frame_size(RegisterSaver::all_registers)/wordSize);
2700   _deopt_blob->set_unpack_with_exception_in_tls_offset(exception_in_tls_offset);
2701 }
2702 
2703 
2704 #ifdef COMPILER2
2705 //------------------------------generate_uncommon_trap_blob--------------------
2706 void SharedRuntime::generate_uncommon_trap_blob() {
2707   // Allocate space for the code
2708   ResourceMark rm;
2709   // Setup code generation tools
2710   CodeBuffer buffer("uncommon_trap_blob", 2048, 1024);
2711   InterpreterMacroAssembler* masm = new InterpreterMacroAssembler(&buffer);
2712 
2713   Register unroll_block_reg = Z_tmp_1;
2714   Register klass_index_reg  = Z_ARG2;
2715   Register unc_trap_reg     = Z_ARG2;
2716 
2717   // stack: (deoptee, caller_of_deoptee, ...).
2718 
2719   // push a dummy "unpack" frame and call
2720   // `Deoptimization::uncommon_trap' to pack the compiled frame into a
2721   // vframe array and return the `UnrollBlock' information.
2722 
2723   // save R14 to compiled frame.
2724   __ save_return_pc();
2725   // push the "unpack_frame".
2726   __ push_frame_abi160(0);
2727 
2728   // stack: (unpack frame, deoptee, caller_of_deoptee, ...).
2729 
2730   // set the "unpack" frame as last_Java_frame.
2731   // `Deoptimization::uncommon_trap' expects it and considers its
2732   // sender frame as the deoptee frame.
2733   __ get_PC(Z_R1_scratch);
2734   __ set_last_Java_frame(/*sp*/Z_SP, /*pc*/Z_R1_scratch);
2735 
2736   __ z_lgr(klass_index_reg, Z_ARG1);  // passed implicitly as ARG2
2737   __ z_lghi(Z_ARG3, Deoptimization::Unpack_uncommon_trap);  // passed implicitly as ARG3
2738   BLOCK_COMMENT("call Deoptimization::uncommon_trap()");
2739   __ call_VM_leaf(CAST_FROM_FN_PTR(address, Deoptimization::uncommon_trap), Z_thread);
2740 
2741   __ reset_last_Java_frame();
2742 
2743   // pop the "unpack" frame
2744   __ pop_frame();
2745 
2746   // stack: (deoptee, caller_of_deoptee, ...).
2747 
2748   // save the return value.
2749   __ z_lgr(unroll_block_reg, Z_RET);
2750 
2751   // pop the deoptee frame.
2752   __ pop_frame();
2753 
2754   // stack: (caller_of_deoptee, ...).
2755 
2756 #ifdef ASSERT
2757   assert(Immediate::is_uimm8(Deoptimization::Unpack_LIMIT), "Code not fit for larger immediates");
2758   assert(Immediate::is_uimm8(Deoptimization::Unpack_uncommon_trap), "Code not fit for larger immediates");
2759   const int unpack_kind_byte_offset = Deoptimization::UnrollBlock::unpack_kind_offset_in_bytes()
2760 #ifndef VM_LITTLE_ENDIAN
2761   + 3
2762 #endif
2763   ;
2764   if (Displacement::is_shortDisp(unpack_kind_byte_offset)) {
2765     __ z_cli(unpack_kind_byte_offset, unroll_block_reg, Deoptimization::Unpack_uncommon_trap);
2766   } else {
2767     __ z_cliy(unpack_kind_byte_offset, unroll_block_reg, Deoptimization::Unpack_uncommon_trap);
2768   }
2769   __ asm_assert_eq("SharedRuntime::generate_deopt_blob: expected Unpack_uncommon_trap", 0);
2770 #endif
2771 
2772   __ zap_from_to(Z_SP, Z_SP, Z_R0_scratch, Z_R1, 500, -1);
2773 
2774   // allocate new interpreter frame(s) and possibly resize the caller's frame
2775   // (no more adapters !)
2776   push_skeleton_frames(masm, false/*deopt*/,
2777                   unroll_block_reg,
2778                   Z_tmp_2,
2779                   Z_tmp_3,
2780                   Z_tmp_4,
2781                   Z_ARG5,
2782                   Z_ARG4);
2783 
2784   // stack: (skeletal interpreter frame, ..., optional skeletal
2785   // interpreter frame, (resized) caller of deoptee, ...).
2786 
2787   // push a dummy "unpack" frame taking care of float return values.
2788   // call `Deoptimization::unpack_frames' to layout information in the
2789   // interpreter frames just created
2790 
2791   // push the "unpack" frame
2792    const unsigned int framesize_in_bytes = __ push_frame_abi160(0);
2793 
2794   // stack: (unpack frame, skeletal interpreter frame, ..., optional
2795   // skeletal interpreter frame, (resized) caller of deoptee, ...).
2796 
2797   // set the "unpack" frame as last_Java_frame
2798   __ get_PC(Z_R1_scratch);
2799   __ set_last_Java_frame(/*sp*/Z_SP, /*pc*/Z_R1_scratch);
2800 
2801   // indicate it is the uncommon trap case
2802   BLOCK_COMMENT("call Deoptimization::Unpack_uncommon_trap()");
2803   __ load_const_optimized(unc_trap_reg, Deoptimization::Unpack_uncommon_trap);
2804   // let the unpacker layout information in the skeletal frames just allocated.
2805   __ call_VM_leaf(CAST_FROM_FN_PTR(address, Deoptimization::unpack_frames), Z_thread);
2806 
2807   __ reset_last_Java_frame();
2808   // pop the "unpack" frame
2809   __ pop_frame();
2810   // restore LR from top interpreter frame
2811   __ restore_return_pc();
2812 
2813   // stack: (top interpreter frame, ..., optional interpreter frame,
2814   // (resized) caller of deoptee, ...).
2815 
2816   __ z_lg(Z_fp, _z_abi(callers_sp), Z_SP); // restore frame pointer
2817   __ restore_bcp();
2818   __ restore_locals();
2819   __ restore_esp();
2820 
2821   // return to the interpreter entry point
2822   __ z_br(Z_R14);
2823 
2824   masm->flush();
2825   _uncommon_trap_blob = UncommonTrapBlob::create(&buffer, NULL, framesize_in_bytes/wordSize);
2826 }
2827 #endif // COMPILER2
2828 
2829 
2830 //------------------------------generate_handler_blob------
2831 //
2832 // Generate a special Compile2Runtime blob that saves all registers,
2833 // and setup oopmap.
2834 SafepointBlob* SharedRuntime::generate_handler_blob(address call_ptr, int poll_type) {
2835   assert(StubRoutines::forward_exception_entry() != NULL,
2836          "must be generated before");
2837 
2838   ResourceMark rm;
2839   OopMapSet *oop_maps = new OopMapSet();
2840   OopMap* map;
2841 
2842   // Allocate space for the code. Setup code generation tools.
2843   CodeBuffer buffer("handler_blob", 2048, 1024);
2844   MacroAssembler* masm = new MacroAssembler(&buffer);
2845 
2846   unsigned int start_off = __ offset();
2847   address call_pc = NULL;
2848   int frame_size_in_bytes;
2849 
2850   bool cause_return = (poll_type == POLL_AT_RETURN);
2851   // Make room for return address (or push it again)
2852   if (!cause_return) {
2853     __ z_lg(Z_R14, Address(Z_thread, JavaThread::saved_exception_pc_offset()));
2854   }
2855 
2856   // Save registers, fpu state, and flags
2857   map = RegisterSaver::save_live_registers(masm, RegisterSaver::all_registers);
2858 
2859   if (!cause_return) {
2860     // Keep a copy of the return pc to detect if it gets modified.
2861     __ z_lgr(Z_R6, Z_R14);
2862   }
2863 
2864   // The following is basically a call_VM. However, we need the precise
2865   // address of the call in order to generate an oopmap. Hence, we do all the
2866   // work ourselves.
2867   __ set_last_Java_frame(Z_SP, noreg);
2868 
2869   // call into the runtime to handle the safepoint poll
2870   __ call_VM_leaf(call_ptr, Z_thread);
2871 
2872 
2873   // Set an oopmap for the call site. This oopmap will map all
2874   // oop-registers and debug-info registers as callee-saved. This
2875   // will allow deoptimization at this safepoint to find all possible
2876   // debug-info recordings, as well as let GC find all oops.
2877 
2878   oop_maps->add_gc_map((int)(__ offset()-start_off), map);
2879 
2880   Label noException;
2881 
2882   __ reset_last_Java_frame();
2883 
2884   __ load_and_test_long(Z_R1, thread_(pending_exception));
2885   __ z_bre(noException);
2886 
2887   // Pending exception case, used (sporadically) by
2888   // api/java_lang/Thread.State/index#ThreadState et al.
2889   RegisterSaver::restore_live_registers(masm, RegisterSaver::all_registers);
2890 
2891   // Jump to forward_exception_entry, with the issuing PC in Z_R14
2892   // so it looks like the original nmethod called forward_exception_entry.
2893   __ load_const_optimized(Z_R1_scratch, StubRoutines::forward_exception_entry());
2894   __ z_br(Z_R1_scratch);
2895 
2896   // No exception case
2897   __ bind(noException);
2898 
2899   if (!cause_return) {
2900     Label no_adjust;
2901      // If our stashed return pc was modified by the runtime we avoid touching it
2902     const int offset_of_return_pc = _z_abi16(return_pc) + RegisterSaver::live_reg_frame_size(RegisterSaver::all_registers);
2903     __ z_cg(Z_R6, offset_of_return_pc, Z_SP);
2904     __ z_brne(no_adjust);
2905 
2906     // Adjust return pc forward to step over the safepoint poll instruction
2907     __ instr_size(Z_R1_scratch, Z_R6);
2908     __ z_agr(Z_R6, Z_R1_scratch);
2909     __ z_stg(Z_R6, offset_of_return_pc, Z_SP);
2910 
2911     __ bind(no_adjust);
2912   }
2913 
2914   // Normal exit, restore registers and exit.
2915   RegisterSaver::restore_live_registers(masm, RegisterSaver::all_registers);
2916 
2917   __ z_br(Z_R14);
2918 
2919   // Make sure all code is generated
2920   masm->flush();
2921 
2922   // Fill-out other meta info
2923   return SafepointBlob::create(&buffer, oop_maps, RegisterSaver::live_reg_frame_size(RegisterSaver::all_registers)/wordSize);
2924 }
2925 
2926 
2927 //
2928 // generate_resolve_blob - call resolution (static/virtual/opt-virtual/ic-miss
2929 //
2930 // Generate a stub that calls into vm to find out the proper destination
2931 // of a Java call. All the argument registers are live at this point
2932 // but since this is generic code we don't know what they are and the caller
2933 // must do any gc of the args.
2934 //
2935 RuntimeStub* SharedRuntime::generate_resolve_blob(address destination, const char* name) {
2936   assert (StubRoutines::forward_exception_entry() != NULL, "must be generated before");
2937 
2938   // allocate space for the code
2939   ResourceMark rm;
2940 
2941   CodeBuffer buffer(name, 1000, 512);
2942   MacroAssembler* masm                = new MacroAssembler(&buffer);
2943 
2944   OopMapSet *oop_maps = new OopMapSet();
2945   OopMap* map = NULL;
2946 
2947   unsigned int start_off = __ offset();
2948 
2949   map = RegisterSaver::save_live_registers(masm, RegisterSaver::all_registers);
2950 
2951   // We must save a PC from within the stub as return PC
2952   // C code doesn't store the LR where we expect the PC,
2953   // so we would run into trouble upon stack walking.
2954   __ get_PC(Z_R1_scratch);
2955 
2956   unsigned int frame_complete = __ offset();
2957 
2958   __ set_last_Java_frame(/*sp*/Z_SP, Z_R1_scratch);
2959 
2960   __ call_VM_leaf(destination, Z_thread, Z_method);
2961 
2962 
2963   // Set an oopmap for the call site.
2964   // We need this not only for callee-saved registers, but also for volatile
2965   // registers that the compiler might be keeping live across a safepoint.
2966 
2967   oop_maps->add_gc_map((int)(frame_complete-start_off), map);
2968 
2969   // clear last_Java_sp
2970   __ reset_last_Java_frame();
2971 
2972   // check for pending exceptions
2973   Label pending;
2974   __ load_and_test_long(Z_R0, Address(Z_thread, Thread::pending_exception_offset()));
2975   __ z_brne(pending);
2976 
2977   __ z_lgr(Z_R1_scratch, Z_R2); // r1 is neither saved nor restored, r2 contains the continuation.
2978   RegisterSaver::restore_live_registers(masm, RegisterSaver::all_registers);
2979 
2980   // get the returned method
2981   __ get_vm_result_2(Z_method);
2982 
2983   // We are back to the original state on entry and ready to go.
2984   __ z_br(Z_R1_scratch);
2985 
2986   // Pending exception after the safepoint
2987 
2988   __ bind(pending);
2989 
2990   RegisterSaver::restore_live_registers(masm, RegisterSaver::all_registers);
2991 
2992   // exception pending => remove activation and forward to exception handler
2993 
2994   __ z_lgr(Z_R2, Z_R0); // pending_exception
2995   __ clear_mem(Address(Z_thread, JavaThread::vm_result_offset()), sizeof(jlong));
2996   __ load_const_optimized(Z_R1_scratch, StubRoutines::forward_exception_entry());
2997   __ z_br(Z_R1_scratch);
2998 
2999   // -------------
3000   // make sure all code is generated
3001   masm->flush();
3002 
3003   // return the blob
3004   // frame_size_words or bytes??
3005   return RuntimeStub::new_runtime_stub(name, &buffer, frame_complete, RegisterSaver::live_reg_frame_size(RegisterSaver::all_registers)/wordSize,
3006                                        oop_maps, true);
3007 
3008 }
3009 
3010 //------------------------------Montgomery multiplication------------------------
3011 //
3012 
3013 // Subtract 0:b from carry:a. Return carry.
3014 static unsigned long
3015 sub(unsigned long a[], unsigned long b[], unsigned long carry, long len) {
3016   unsigned long i, c = 8 * (unsigned long)(len - 1);
3017   __asm__ __volatile__ (
3018     "SLGR   %[i], %[i]         \n" // initialize to 0 and pre-set carry
3019     "LGHI   0, 8               \n" // index increment (for BRXLG)
3020     "LGR    1, %[c]            \n" // index limit (for BRXLG)
3021     "0:                        \n"
3022     "LG     %[c], 0(%[i],%[a]) \n"
3023     "SLBG   %[c], 0(%[i],%[b]) \n" // subtract with borrow
3024     "STG    %[c], 0(%[i],%[a]) \n"
3025     "BRXLG  %[i], 0, 0b        \n" // while ((i+=8)<limit);
3026     "SLBGR  %[c], %[c]         \n" // save carry - 1
3027     : [i]"=&a"(i), [c]"+r"(c)
3028     : [a]"a"(a), [b]"a"(b)
3029     : "cc", "memory", "r0", "r1"
3030  );
3031   return carry + c;
3032 }
3033 
3034 // Multiply (unsigned) Long A by Long B, accumulating the double-
3035 // length result into the accumulator formed of T0, T1, and T2.
3036 inline void MACC(unsigned long A[], long A_ind,
3037                  unsigned long B[], long B_ind,
3038                  unsigned long &T0, unsigned long &T1, unsigned long &T2) {
3039   long A_si = 8 * A_ind,
3040        B_si = 8 * B_ind;
3041   __asm__ __volatile__ (
3042     "LG     1, 0(%[A_si],%[A]) \n"
3043     "MLG    0, 0(%[B_si],%[B]) \n" // r0r1 = A * B
3044     "ALGR   %[T0], 1           \n"
3045     "LGHI   1, 0               \n" // r1 = 0
3046     "ALCGR  %[T1], 0           \n"
3047     "ALCGR  %[T2], 1           \n"
3048     : [T0]"+r"(T0), [T1]"+r"(T1), [T2]"+r"(T2)
3049     : [A]"r"(A), [A_si]"r"(A_si), [B]"r"(B), [B_si]"r"(B_si)
3050     : "cc", "r0", "r1"
3051  );
3052 }
3053 
3054 // As above, but add twice the double-length result into the
3055 // accumulator.
3056 inline void MACC2(unsigned long A[], long A_ind,
3057                   unsigned long B[], long B_ind,
3058                   unsigned long &T0, unsigned long &T1, unsigned long &T2) {
3059   const unsigned long zero = 0;
3060   long A_si = 8 * A_ind,
3061        B_si = 8 * B_ind;
3062   __asm__ __volatile__ (
3063     "LG     1, 0(%[A_si],%[A]) \n"
3064     "MLG    0, 0(%[B_si],%[B]) \n" // r0r1 = A * B
3065     "ALGR   %[T0], 1           \n"
3066     "ALCGR  %[T1], 0           \n"
3067     "ALCGR  %[T2], %[zero]     \n"
3068     "ALGR   %[T0], 1           \n"
3069     "ALCGR  %[T1], 0           \n"
3070     "ALCGR  %[T2], %[zero]     \n"
3071     : [T0]"+r"(T0), [T1]"+r"(T1), [T2]"+r"(T2)
3072     : [A]"r"(A), [A_si]"r"(A_si), [B]"r"(B), [B_si]"r"(B_si), [zero]"r"(zero)
3073     : "cc", "r0", "r1"
3074  );
3075 }
3076 
3077 // Fast Montgomery multiplication. The derivation of the algorithm is
3078 // in "A Cryptographic Library for the Motorola DSP56000,
3079 // Dusse and Kaliski, Proc. EUROCRYPT 90, pp. 230-237".
3080 static void
3081 montgomery_multiply(unsigned long a[], unsigned long b[], unsigned long n[],
3082                     unsigned long m[], unsigned long inv, int len) {
3083   unsigned long t0 = 0, t1 = 0, t2 = 0; // Triple-precision accumulator
3084   int i;
3085 
3086   assert(inv * n[0] == -1UL, "broken inverse in Montgomery multiply");
3087 
3088   for (i = 0; i < len; i++) {
3089     int j;
3090     for (j = 0; j < i; j++) {
3091       MACC(a, j, b, i-j, t0, t1, t2);
3092       MACC(m, j, n, i-j, t0, t1, t2);
3093     }
3094     MACC(a, i, b, 0, t0, t1, t2);
3095     m[i] = t0 * inv;
3096     MACC(m, i, n, 0, t0, t1, t2);
3097 
3098     assert(t0 == 0, "broken Montgomery multiply");
3099 
3100     t0 = t1; t1 = t2; t2 = 0;
3101   }
3102 
3103   for (i = len; i < 2 * len; i++) {
3104     int j;
3105     for (j = i - len + 1; j < len; j++) {
3106       MACC(a, j, b, i-j, t0, t1, t2);
3107       MACC(m, j, n, i-j, t0, t1, t2);
3108     }
3109     m[i-len] = t0;
3110     t0 = t1; t1 = t2; t2 = 0;
3111   }
3112 
3113   while (t0) {
3114     t0 = sub(m, n, t0, len);
3115   }
3116 }
3117 
3118 // Fast Montgomery squaring. This uses asymptotically 25% fewer
3119 // multiplies so it should be up to 25% faster than Montgomery
3120 // multiplication. However, its loop control is more complex and it
3121 // may actually run slower on some machines.
3122 static void
3123 montgomery_square(unsigned long a[], unsigned long n[],
3124                   unsigned long m[], unsigned long inv, int len) {
3125   unsigned long t0 = 0, t1 = 0, t2 = 0; // Triple-precision accumulator
3126   int i;
3127 
3128   assert(inv * n[0] == -1UL, "broken inverse in Montgomery multiply");
3129 
3130   for (i = 0; i < len; i++) {
3131     int j;
3132     int end = (i+1)/2;
3133     for (j = 0; j < end; j++) {
3134       MACC2(a, j, a, i-j, t0, t1, t2);
3135       MACC(m, j, n, i-j, t0, t1, t2);
3136     }
3137     if ((i & 1) == 0) {
3138       MACC(a, j, a, j, t0, t1, t2);
3139     }
3140     for (; j < i; j++) {
3141       MACC(m, j, n, i-j, t0, t1, t2);
3142     }
3143     m[i] = t0 * inv;
3144     MACC(m, i, n, 0, t0, t1, t2);
3145 
3146     assert(t0 == 0, "broken Montgomery square");
3147 
3148     t0 = t1; t1 = t2; t2 = 0;
3149   }
3150 
3151   for (i = len; i < 2*len; i++) {
3152     int start = i-len+1;
3153     int end = start + (len - start)/2;
3154     int j;
3155     for (j = start; j < end; j++) {
3156       MACC2(a, j, a, i-j, t0, t1, t2);
3157       MACC(m, j, n, i-j, t0, t1, t2);
3158     }
3159     if ((i & 1) == 0) {
3160       MACC(a, j, a, j, t0, t1, t2);
3161     }
3162     for (; j < len; j++) {
3163       MACC(m, j, n, i-j, t0, t1, t2);
3164     }
3165     m[i-len] = t0;
3166     t0 = t1; t1 = t2; t2 = 0;
3167   }
3168 
3169   while (t0) {
3170     t0 = sub(m, n, t0, len);
3171   }
3172 }
3173 
3174 // The threshold at which squaring is advantageous was determined
3175 // experimentally on an i7-3930K (Ivy Bridge) CPU @ 3.5GHz.
3176 // Value seems to be ok for other platforms, too.
3177 #define MONTGOMERY_SQUARING_THRESHOLD 64
3178 
3179 // Copy len longwords from s to d, word-swapping as we go. The
3180 // destination array is reversed.
3181 static void reverse_words(unsigned long *s, unsigned long *d, int len) {
3182   d += len;
3183   while(len-- > 0) {
3184     d--;
3185     unsigned long s_val = *s;
3186     // Swap words in a longword on little endian machines.
3187 #ifdef VM_LITTLE_ENDIAN
3188      Unimplemented();
3189 #endif
3190     *d = s_val;
3191     s++;
3192   }
3193 }
3194 
3195 void SharedRuntime::montgomery_multiply(jint *a_ints, jint *b_ints, jint *n_ints,
3196                                         jint len, jlong inv,
3197                                         jint *m_ints) {
3198   len = len & 0x7fffFFFF; // C2 does not respect int to long conversion for stub calls.
3199   assert(len % 2 == 0, "array length in montgomery_multiply must be even");
3200   int longwords = len/2;
3201 
3202   // Make very sure we don't use so much space that the stack might
3203   // overflow. 512 jints corresponds to an 16384-bit integer and
3204   // will use here a total of 8k bytes of stack space.
3205   int divisor = sizeof(unsigned long) * 4;
3206   guarantee(longwords <= 8192 / divisor, "must be");
3207   int total_allocation = longwords * sizeof (unsigned long) * 4;
3208   unsigned long *scratch = (unsigned long *)alloca(total_allocation);
3209 
3210   // Local scratch arrays
3211   unsigned long
3212     *a = scratch + 0 * longwords,
3213     *b = scratch + 1 * longwords,
3214     *n = scratch + 2 * longwords,
3215     *m = scratch + 3 * longwords;
3216 
3217   reverse_words((unsigned long *)a_ints, a, longwords);
3218   reverse_words((unsigned long *)b_ints, b, longwords);
3219   reverse_words((unsigned long *)n_ints, n, longwords);
3220 
3221   ::montgomery_multiply(a, b, n, m, (unsigned long)inv, longwords);
3222 
3223   reverse_words(m, (unsigned long *)m_ints, longwords);
3224 }
3225 
3226 void SharedRuntime::montgomery_square(jint *a_ints, jint *n_ints,
3227                                       jint len, jlong inv,
3228                                       jint *m_ints) {
3229   len = len & 0x7fffFFFF; // C2 does not respect int to long conversion for stub calls.
3230   assert(len % 2 == 0, "array length in montgomery_square must be even");
3231   int longwords = len/2;
3232 
3233   // Make very sure we don't use so much space that the stack might
3234   // overflow. 512 jints corresponds to an 16384-bit integer and
3235   // will use here a total of 6k bytes of stack space.
3236   int divisor = sizeof(unsigned long) * 3;
3237   guarantee(longwords <= (8192 / divisor), "must be");
3238   int total_allocation = longwords * sizeof (unsigned long) * 3;
3239   unsigned long *scratch = (unsigned long *)alloca(total_allocation);
3240 
3241   // Local scratch arrays
3242   unsigned long
3243     *a = scratch + 0 * longwords,
3244     *n = scratch + 1 * longwords,
3245     *m = scratch + 2 * longwords;
3246 
3247   reverse_words((unsigned long *)a_ints, a, longwords);
3248   reverse_words((unsigned long *)n_ints, n, longwords);
3249 
3250   if (len >= MONTGOMERY_SQUARING_THRESHOLD) {
3251     ::montgomery_square(a, n, m, (unsigned long)inv, longwords);
3252   } else {
3253     ::montgomery_multiply(a, a, n, m, (unsigned long)inv, longwords);
3254   }
3255 
3256   reverse_words(m, (unsigned long *)m_ints, longwords);
3257 }
3258 
3259 extern "C"
3260 int SpinPause() {
3261   return 0;
3262 }