1 /*
   2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/assembler.hpp"
  27 #include "c1/c1_Defs.hpp"
  28 #include "c1/c1_MacroAssembler.hpp"
  29 #include "c1/c1_Runtime1.hpp"
  30 #include "ci/ciUtilities.hpp"
  31 #include "gc/shared/cardTable.hpp"
  32 #include "gc/shared/cardTableBarrierSet.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "nativeInst_x86.hpp"
  35 #include "oops/compiledICHolder.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "prims/jvmtiExport.hpp"
  38 #include "register_x86.hpp"
  39 #include "runtime/sharedRuntime.hpp"
  40 #include "runtime/signature.hpp"
  41 #include "runtime/vframeArray.hpp"
  42 #include "utilities/macros.hpp"
  43 #include "vmreg_x86.inline.hpp"
  44 
  45 // Implementation of StubAssembler
  46 
  47 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, int args_size) {
  48   // setup registers
  49   const Register thread = NOT_LP64(rdi) LP64_ONLY(r15_thread); // is callee-saved register (Visual C++ calling conventions)
  50   assert(!(oop_result1->is_valid() || metadata_result->is_valid()) || oop_result1 != metadata_result, "registers must be different");
  51   assert(oop_result1 != thread && metadata_result != thread, "registers must be different");
  52   assert(args_size >= 0, "illegal args_size");
  53   bool align_stack = false;
  54 #ifdef _LP64
  55   // At a method handle call, the stack may not be properly aligned
  56   // when returning with an exception.
  57   align_stack = (stub_id() == Runtime1::handle_exception_from_callee_id);
  58 #endif
  59 
  60 #ifdef _LP64
  61   mov(c_rarg0, thread);
  62   set_num_rt_args(0); // Nothing on stack
  63 #else
  64   set_num_rt_args(1 + args_size);
  65 
  66   // push java thread (becomes first argument of C function)
  67   get_thread(thread);
  68   push(thread);
  69 #endif // _LP64
  70 
  71   int call_offset;
  72   if (!align_stack) {
  73     set_last_Java_frame(thread, noreg, rbp, NULL);
  74   } else {
  75     address the_pc = pc();
  76     call_offset = offset();
  77     set_last_Java_frame(thread, noreg, rbp, the_pc);
  78     andptr(rsp, -(StackAlignmentInBytes));    // Align stack
  79   }
  80 
  81   // do the call
  82   call(RuntimeAddress(entry));
  83   if (!align_stack) {
  84     call_offset = offset();
  85   }
  86   // verify callee-saved register
  87 #ifdef ASSERT
  88   guarantee(thread != rax, "change this code");
  89   push(rax);
  90   { Label L;
  91     get_thread(rax);
  92     cmpptr(thread, rax);
  93     jcc(Assembler::equal, L);
  94     int3();
  95     stop("StubAssembler::call_RT: rdi not callee saved?");
  96     bind(L);
  97   }
  98   pop(rax);
  99 #endif
 100   reset_last_Java_frame(thread, true);
 101 
 102   // discard thread and arguments
 103   NOT_LP64(addptr(rsp, num_rt_args()*BytesPerWord));
 104 
 105   // check for pending exceptions
 106   { Label L;
 107     cmpptr(Address(thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 108     jcc(Assembler::equal, L);
 109     // exception pending => remove activation and forward to exception handler
 110     movptr(rax, Address(thread, Thread::pending_exception_offset()));
 111     // make sure that the vm_results are cleared
 112     if (oop_result1->is_valid()) {
 113       movptr(Address(thread, JavaThread::vm_result_offset()), NULL_WORD);
 114     }
 115     if (metadata_result->is_valid()) {
 116       movptr(Address(thread, JavaThread::vm_result_2_offset()), NULL_WORD);
 117     }
 118     if (frame_size() == no_frame_size) {
 119       leave();
 120       jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
 121     } else if (_stub_id == Runtime1::forward_exception_id) {
 122       should_not_reach_here();
 123     } else {
 124       jump(RuntimeAddress(Runtime1::entry_for(Runtime1::forward_exception_id)));
 125     }
 126     bind(L);
 127   }
 128   // get oop results if there are any and reset the values in the thread
 129   if (oop_result1->is_valid()) {
 130     get_vm_result(oop_result1, thread);
 131   }
 132   if (metadata_result->is_valid()) {
 133     get_vm_result_2(metadata_result, thread);
 134   }
 135   return call_offset;
 136 }
 137 
 138 
 139 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1) {
 140 #ifdef _LP64
 141   mov(c_rarg1, arg1);
 142 #else
 143   push(arg1);
 144 #endif // _LP64
 145   return call_RT(oop_result1, metadata_result, entry, 1);
 146 }
 147 
 148 
 149 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1, Register arg2) {
 150 #ifdef _LP64
 151   if (c_rarg1 == arg2) {
 152     if (c_rarg2 == arg1) {
 153       xchgq(arg1, arg2);
 154     } else {
 155       mov(c_rarg2, arg2);
 156       mov(c_rarg1, arg1);
 157     }
 158   } else {
 159     mov(c_rarg1, arg1);
 160     mov(c_rarg2, arg2);
 161   }
 162 #else
 163   push(arg2);
 164   push(arg1);
 165 #endif // _LP64
 166   return call_RT(oop_result1, metadata_result, entry, 2);
 167 }
 168 
 169 
 170 int StubAssembler::call_RT(Register oop_result1, Register metadata_result, address entry, Register arg1, Register arg2, Register arg3) {
 171 #ifdef _LP64
 172   // if there is any conflict use the stack
 173   if (arg1 == c_rarg2 || arg1 == c_rarg3 ||
 174       arg2 == c_rarg1 || arg1 == c_rarg3 ||
 175       arg3 == c_rarg1 || arg1 == c_rarg2) {
 176     push(arg3);
 177     push(arg2);
 178     push(arg1);
 179     pop(c_rarg1);
 180     pop(c_rarg2);
 181     pop(c_rarg3);
 182   } else {
 183     mov(c_rarg1, arg1);
 184     mov(c_rarg2, arg2);
 185     mov(c_rarg3, arg3);
 186   }
 187 #else
 188   push(arg3);
 189   push(arg2);
 190   push(arg1);
 191 #endif // _LP64
 192   return call_RT(oop_result1, metadata_result, entry, 3);
 193 }
 194 
 195 
 196 // Implementation of StubFrame
 197 
 198 class StubFrame: public StackObj {
 199  private:
 200   StubAssembler* _sasm;
 201 
 202  public:
 203   StubFrame(StubAssembler* sasm, const char* name, bool must_gc_arguments);
 204   void load_argument(int offset_in_words, Register reg);
 205 
 206   ~StubFrame();
 207 };
 208 
 209 void StubAssembler::prologue(const char* name, bool must_gc_arguments) {
 210   set_info(name, must_gc_arguments);
 211   enter();
 212 }
 213 
 214 void StubAssembler::epilogue() {
 215   leave();
 216   ret(0);
 217 }
 218 
 219 #define __ _sasm->
 220 
 221 StubFrame::StubFrame(StubAssembler* sasm, const char* name, bool must_gc_arguments) {
 222   _sasm = sasm;
 223   __ prologue(name, must_gc_arguments);
 224 }
 225 
 226 // load parameters that were stored with LIR_Assembler::store_parameter
 227 // Note: offsets for store_parameter and load_argument must match
 228 void StubFrame::load_argument(int offset_in_words, Register reg) {
 229   __ load_parameter(offset_in_words, reg);
 230 }
 231 
 232 
 233 StubFrame::~StubFrame() {
 234   __ epilogue();
 235 }
 236 
 237 #undef __
 238 
 239 
 240 // Implementation of Runtime1
 241 
 242 const int float_regs_as_doubles_size_in_slots = pd_nof_fpu_regs_frame_map * 2;
 243 const int xmm_regs_as_doubles_size_in_slots = FrameMap::nof_xmm_regs * 2;
 244 
 245 // Stack layout for saving/restoring  all the registers needed during a runtime
 246 // call (this includes deoptimization)
 247 // Note: note that users of this frame may well have arguments to some runtime
 248 // while these values are on the stack. These positions neglect those arguments
 249 // but the code in save_live_registers will take the argument count into
 250 // account.
 251 //
 252 #ifdef _LP64
 253   #define SLOT2(x) x,
 254   #define SLOT_PER_WORD 2
 255 #else
 256   #define SLOT2(x)
 257   #define SLOT_PER_WORD 1
 258 #endif // _LP64
 259 
 260 enum reg_save_layout {
 261   // 64bit needs to keep stack 16 byte aligned. So we add some alignment dummies to make that
 262   // happen and will assert if the stack size we create is misaligned
 263 #ifdef _LP64
 264   align_dummy_0, align_dummy_1,
 265 #endif // _LP64
 266 #ifdef _WIN64
 267   // Windows always allocates space for it's argument registers (see
 268   // frame::arg_reg_save_area_bytes).
 269   arg_reg_save_1, arg_reg_save_1H,                                                          // 0, 4
 270   arg_reg_save_2, arg_reg_save_2H,                                                          // 8, 12
 271   arg_reg_save_3, arg_reg_save_3H,                                                          // 16, 20
 272   arg_reg_save_4, arg_reg_save_4H,                                                          // 24, 28
 273 #endif // _WIN64
 274   xmm_regs_as_doubles_off,                                                                  // 32
 275   float_regs_as_doubles_off = xmm_regs_as_doubles_off + xmm_regs_as_doubles_size_in_slots,  // 160
 276   fpu_state_off = float_regs_as_doubles_off + float_regs_as_doubles_size_in_slots,          // 224
 277   // fpu_state_end_off is exclusive
 278   fpu_state_end_off = fpu_state_off + (FPUStateSizeInWords / SLOT_PER_WORD),                // 352
 279   marker = fpu_state_end_off, SLOT2(markerH)                                                // 352, 356
 280   extra_space_offset,                                                                       // 360
 281 #ifdef _LP64
 282   r15_off = extra_space_offset, r15H_off,                                                   // 360, 364
 283   r14_off, r14H_off,                                                                        // 368, 372
 284   r13_off, r13H_off,                                                                        // 376, 380
 285   r12_off, r12H_off,                                                                        // 384, 388
 286   r11_off, r11H_off,                                                                        // 392, 396
 287   r10_off, r10H_off,                                                                        // 400, 404
 288   r9_off, r9H_off,                                                                          // 408, 412
 289   r8_off, r8H_off,                                                                          // 416, 420
 290   rdi_off, rdiH_off,                                                                        // 424, 428
 291 #else
 292   rdi_off = extra_space_offset,
 293 #endif // _LP64
 294   rsi_off, SLOT2(rsiH_off)                                                                  // 432, 436
 295   rbp_off, SLOT2(rbpH_off)                                                                  // 440, 444
 296   rsp_off, SLOT2(rspH_off)                                                                  // 448, 452
 297   rbx_off, SLOT2(rbxH_off)                                                                  // 456, 460
 298   rdx_off, SLOT2(rdxH_off)                                                                  // 464, 468
 299   rcx_off, SLOT2(rcxH_off)                                                                  // 472, 476
 300   rax_off, SLOT2(raxH_off)                                                                  // 480, 484
 301   saved_rbp_off, SLOT2(saved_rbpH_off)                                                      // 488, 492
 302   return_off, SLOT2(returnH_off)                                                            // 496, 500
 303   reg_save_frame_size   // As noted: neglects any parameters to runtime                     // 504
 304 };
 305 
 306 // Save off registers which might be killed by calls into the runtime.
 307 // Tries to smart of about FP registers.  In particular we separate
 308 // saving and describing the FPU registers for deoptimization since we
 309 // have to save the FPU registers twice if we describe them and on P4
 310 // saving FPU registers which don't contain anything appears
 311 // expensive.  The deopt blob is the only thing which needs to
 312 // describe FPU registers.  In all other cases it should be sufficient
 313 // to simply save their current value.
 314 
 315 static OopMap* generate_oop_map(StubAssembler* sasm, int num_rt_args,
 316                                 bool save_fpu_registers = true) {
 317 
 318   // In 64bit all the args are in regs so there are no additional stack slots
 319   LP64_ONLY(num_rt_args = 0);
 320   LP64_ONLY(assert((reg_save_frame_size * VMRegImpl::stack_slot_size) % 16 == 0, "must be 16 byte aligned");)
 321   int frame_size_in_slots = reg_save_frame_size + num_rt_args; // args + thread
 322   sasm->set_frame_size(frame_size_in_slots / VMRegImpl::slots_per_word);
 323 
 324   // record saved value locations in an OopMap
 325   // locations are offsets from sp after runtime call; num_rt_args is number of arguments in call, including thread
 326   OopMap* map = new OopMap(frame_size_in_slots, 0);
 327   map->set_callee_saved(VMRegImpl::stack2reg(rax_off + num_rt_args), rax->as_VMReg());
 328   map->set_callee_saved(VMRegImpl::stack2reg(rcx_off + num_rt_args), rcx->as_VMReg());
 329   map->set_callee_saved(VMRegImpl::stack2reg(rdx_off + num_rt_args), rdx->as_VMReg());
 330   map->set_callee_saved(VMRegImpl::stack2reg(rbx_off + num_rt_args), rbx->as_VMReg());
 331   map->set_callee_saved(VMRegImpl::stack2reg(rsi_off + num_rt_args), rsi->as_VMReg());
 332   map->set_callee_saved(VMRegImpl::stack2reg(rdi_off + num_rt_args), rdi->as_VMReg());
 333 #ifdef _LP64
 334   map->set_callee_saved(VMRegImpl::stack2reg(r8_off + num_rt_args),  r8->as_VMReg());
 335   map->set_callee_saved(VMRegImpl::stack2reg(r9_off + num_rt_args),  r9->as_VMReg());
 336   map->set_callee_saved(VMRegImpl::stack2reg(r10_off + num_rt_args), r10->as_VMReg());
 337   map->set_callee_saved(VMRegImpl::stack2reg(r11_off + num_rt_args), r11->as_VMReg());
 338   map->set_callee_saved(VMRegImpl::stack2reg(r12_off + num_rt_args), r12->as_VMReg());
 339   map->set_callee_saved(VMRegImpl::stack2reg(r13_off + num_rt_args), r13->as_VMReg());
 340   map->set_callee_saved(VMRegImpl::stack2reg(r14_off + num_rt_args), r14->as_VMReg());
 341   map->set_callee_saved(VMRegImpl::stack2reg(r15_off + num_rt_args), r15->as_VMReg());
 342 
 343   // This is stupid but needed.
 344   map->set_callee_saved(VMRegImpl::stack2reg(raxH_off + num_rt_args), rax->as_VMReg()->next());
 345   map->set_callee_saved(VMRegImpl::stack2reg(rcxH_off + num_rt_args), rcx->as_VMReg()->next());
 346   map->set_callee_saved(VMRegImpl::stack2reg(rdxH_off + num_rt_args), rdx->as_VMReg()->next());
 347   map->set_callee_saved(VMRegImpl::stack2reg(rbxH_off + num_rt_args), rbx->as_VMReg()->next());
 348   map->set_callee_saved(VMRegImpl::stack2reg(rsiH_off + num_rt_args), rsi->as_VMReg()->next());
 349   map->set_callee_saved(VMRegImpl::stack2reg(rdiH_off + num_rt_args), rdi->as_VMReg()->next());
 350 
 351   map->set_callee_saved(VMRegImpl::stack2reg(r8H_off + num_rt_args),  r8->as_VMReg()->next());
 352   map->set_callee_saved(VMRegImpl::stack2reg(r9H_off + num_rt_args),  r9->as_VMReg()->next());
 353   map->set_callee_saved(VMRegImpl::stack2reg(r10H_off + num_rt_args), r10->as_VMReg()->next());
 354   map->set_callee_saved(VMRegImpl::stack2reg(r11H_off + num_rt_args), r11->as_VMReg()->next());
 355   map->set_callee_saved(VMRegImpl::stack2reg(r12H_off + num_rt_args), r12->as_VMReg()->next());
 356   map->set_callee_saved(VMRegImpl::stack2reg(r13H_off + num_rt_args), r13->as_VMReg()->next());
 357   map->set_callee_saved(VMRegImpl::stack2reg(r14H_off + num_rt_args), r14->as_VMReg()->next());
 358   map->set_callee_saved(VMRegImpl::stack2reg(r15H_off + num_rt_args), r15->as_VMReg()->next());
 359 #endif // _LP64
 360 
 361   int xmm_bypass_limit = FrameMap::nof_xmm_regs;
 362 #ifdef _LP64
 363   if (UseAVX < 3) {
 364     xmm_bypass_limit = xmm_bypass_limit / 2;
 365   }
 366 #endif
 367 
 368   if (save_fpu_registers) {
 369     if (UseSSE < 2) {
 370       int fpu_off = float_regs_as_doubles_off;
 371       for (int n = 0; n < FrameMap::nof_fpu_regs; n++) {
 372         VMReg fpu_name_0 = FrameMap::fpu_regname(n);
 373         map->set_callee_saved(VMRegImpl::stack2reg(fpu_off +     num_rt_args), fpu_name_0);
 374         // %%% This is really a waste but we'll keep things as they were for now
 375         if (true) {
 376           map->set_callee_saved(VMRegImpl::stack2reg(fpu_off + 1 + num_rt_args), fpu_name_0->next());
 377         }
 378         fpu_off += 2;
 379       }
 380       assert(fpu_off == fpu_state_off, "incorrect number of fpu stack slots");
 381     }
 382 
 383     if (UseSSE >= 2) {
 384       int xmm_off = xmm_regs_as_doubles_off;
 385       for (int n = 0; n < FrameMap::nof_xmm_regs; n++) {
 386         if (n < xmm_bypass_limit) {
 387           VMReg xmm_name_0 = as_XMMRegister(n)->as_VMReg();
 388           map->set_callee_saved(VMRegImpl::stack2reg(xmm_off + num_rt_args), xmm_name_0);
 389           // %%% This is really a waste but we'll keep things as they were for now
 390           if (true) {
 391             map->set_callee_saved(VMRegImpl::stack2reg(xmm_off + 1 + num_rt_args), xmm_name_0->next());
 392           }
 393         }
 394         xmm_off += 2;
 395       }
 396       assert(xmm_off == float_regs_as_doubles_off, "incorrect number of xmm registers");
 397 
 398     } else if (UseSSE == 1) {
 399       int xmm_off = xmm_regs_as_doubles_off;
 400       for (int n = 0; n < FrameMap::nof_fpu_regs; n++) {
 401         VMReg xmm_name_0 = as_XMMRegister(n)->as_VMReg();
 402         map->set_callee_saved(VMRegImpl::stack2reg(xmm_off + num_rt_args), xmm_name_0);
 403         xmm_off += 2;
 404       }
 405       assert(xmm_off == float_regs_as_doubles_off, "incorrect number of xmm registers");
 406     }
 407   }
 408 
 409   return map;
 410 }
 411 
 412 #define __ this->
 413 
 414 void C1_MacroAssembler::save_live_registers_no_oop_map(int num_rt_args, bool save_fpu_registers) {
 415   __ block_comment("save_live_registers");
 416 
 417   __ pusha();         // integer registers
 418 
 419   // assert(float_regs_as_doubles_off % 2 == 0, "misaligned offset");
 420   // assert(xmm_regs_as_doubles_off % 2 == 0, "misaligned offset");
 421 
 422   __ subptr(rsp, extra_space_offset * VMRegImpl::stack_slot_size);
 423 
 424 #ifdef ASSERT
 425   __ movptr(Address(rsp, marker * VMRegImpl::stack_slot_size), (int32_t)0xfeedbeef);
 426 #endif
 427 
 428   if (save_fpu_registers) {
 429     if (UseSSE < 2) {
 430       // save FPU stack
 431       __ fnsave(Address(rsp, fpu_state_off * VMRegImpl::stack_slot_size));
 432       __ fwait();
 433 
 434 #ifdef ASSERT
 435       Label ok;
 436       __ cmpw(Address(rsp, fpu_state_off * VMRegImpl::stack_slot_size), StubRoutines::fpu_cntrl_wrd_std());
 437       __ jccb(Assembler::equal, ok);
 438       __ stop("corrupted control word detected");
 439       __ bind(ok);
 440 #endif
 441 
 442       // Reset the control word to guard against exceptions being unmasked
 443       // since fstp_d can cause FPU stack underflow exceptions.  Write it
 444       // into the on stack copy and then reload that to make sure that the
 445       // current and future values are correct.
 446       __ movw(Address(rsp, fpu_state_off * VMRegImpl::stack_slot_size), StubRoutines::fpu_cntrl_wrd_std());
 447       __ frstor(Address(rsp, fpu_state_off * VMRegImpl::stack_slot_size));
 448 
 449       // Save the FPU registers in de-opt-able form
 450       int offset = 0;
 451       for (int n = 0; n < FrameMap::nof_fpu_regs; n++) {
 452         __ fstp_d(Address(rsp, float_regs_as_doubles_off * VMRegImpl::stack_slot_size + offset));
 453         offset += 8;
 454       }
 455     }
 456 
 457     if (UseSSE >= 2) {
 458       // save XMM registers
 459       // XMM registers can contain float or double values, but this is not known here,
 460       // so always save them as doubles.
 461       // note that float values are _not_ converted automatically, so for float values
 462       // the second word contains only garbage data.
 463       int xmm_bypass_limit = FrameMap::nof_xmm_regs;
 464       int offset = 0;
 465 #ifdef _LP64
 466       if (UseAVX < 3) {
 467         xmm_bypass_limit = xmm_bypass_limit / 2;
 468       }
 469 #endif
 470       for (int n = 0; n < xmm_bypass_limit; n++) {
 471         XMMRegister xmm_name = as_XMMRegister(n);
 472         __ movdbl(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + offset), xmm_name);
 473         offset += 8;
 474       }
 475     } else if (UseSSE == 1) {
 476       // save XMM registers as float because double not supported without SSE2(num MMX == num fpu)
 477       int offset = 0;
 478       for (int n = 0; n < FrameMap::nof_fpu_regs; n++) {
 479         XMMRegister xmm_name = as_XMMRegister(n);
 480         __ movflt(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + offset), xmm_name);
 481         offset += 8;
 482       }
 483     }
 484   }
 485 
 486   // FPU stack must be empty now
 487   __ verify_FPU(0, "save_live_registers");
 488 }
 489 
 490 #undef __
 491 #define __ sasm->
 492 
 493 static void restore_fpu(C1_MacroAssembler* sasm, bool restore_fpu_registers) {
 494   if (restore_fpu_registers) {
 495     if (UseSSE >= 2) {
 496       // restore XMM registers
 497       int xmm_bypass_limit = FrameMap::nof_xmm_regs;
 498 #ifdef _LP64
 499       if (UseAVX < 3) {
 500         xmm_bypass_limit = xmm_bypass_limit / 2;
 501       }
 502 #endif
 503       int offset = 0;
 504       for (int n = 0; n < xmm_bypass_limit; n++) {
 505         XMMRegister xmm_name = as_XMMRegister(n);
 506         __ movdbl(xmm_name, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + offset));
 507         offset += 8;
 508       }
 509     } else if (UseSSE == 1) {
 510       // restore XMM registers(num MMX == num fpu)
 511       int offset = 0;
 512       for (int n = 0; n < FrameMap::nof_fpu_regs; n++) {
 513         XMMRegister xmm_name = as_XMMRegister(n);
 514         __ movflt(xmm_name, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + offset));
 515         offset += 8;
 516       }
 517     }
 518 
 519     if (UseSSE < 2) {
 520       __ frstor(Address(rsp, fpu_state_off * VMRegImpl::stack_slot_size));
 521     } else {
 522       // check that FPU stack is really empty
 523       __ verify_FPU(0, "restore_live_registers");
 524     }
 525 
 526   } else {
 527     // check that FPU stack is really empty
 528     __ verify_FPU(0, "restore_live_registers");
 529   }
 530 
 531 #ifdef ASSERT
 532   {
 533     Label ok;
 534     __ cmpptr(Address(rsp, marker * VMRegImpl::stack_slot_size), (int32_t)0xfeedbeef);
 535     __ jcc(Assembler::equal, ok);
 536     __ stop("bad offsets in frame");
 537     __ bind(ok);
 538   }
 539 #endif // ASSERT
 540 
 541   __ addptr(rsp, extra_space_offset * VMRegImpl::stack_slot_size);
 542 }
 543 
 544 #undef __
 545 #define __ this->
 546 
 547 void C1_MacroAssembler::restore_live_registers(bool restore_fpu_registers) {
 548   __ block_comment("restore_live_registers");
 549 
 550   restore_fpu(this, restore_fpu_registers);
 551   __ popa();
 552 }
 553 
 554 #undef __
 555 #define __ sasm->
 556 
 557 static OopMap* save_live_registers(StubAssembler* sasm, int num_rt_args,
 558                                    bool save_fpu_registers = true) {
 559   sasm->save_live_registers_no_oop_map(num_rt_args, save_fpu_registers);
 560   return generate_oop_map(sasm, num_rt_args, save_fpu_registers);
 561 }
 562 
 563 static void restore_live_registers(StubAssembler* sasm, bool restore_fpu_registers = true) {
 564   sasm->restore_live_registers(restore_fpu_registers);
 565 }
 566 
 567 static void restore_live_registers_except_rax(StubAssembler* sasm, bool restore_fpu_registers = true) {
 568   __ block_comment("restore_live_registers_except_rax");
 569 
 570   restore_fpu(sasm, restore_fpu_registers);
 571 
 572 #ifdef _LP64
 573   __ movptr(r15, Address(rsp, 0));
 574   __ movptr(r14, Address(rsp, wordSize));
 575   __ movptr(r13, Address(rsp, 2 * wordSize));
 576   __ movptr(r12, Address(rsp, 3 * wordSize));
 577   __ movptr(r11, Address(rsp, 4 * wordSize));
 578   __ movptr(r10, Address(rsp, 5 * wordSize));
 579   __ movptr(r9,  Address(rsp, 6 * wordSize));
 580   __ movptr(r8,  Address(rsp, 7 * wordSize));
 581   __ movptr(rdi, Address(rsp, 8 * wordSize));
 582   __ movptr(rsi, Address(rsp, 9 * wordSize));
 583   __ movptr(rbp, Address(rsp, 10 * wordSize));
 584   // skip rsp
 585   __ movptr(rbx, Address(rsp, 12 * wordSize));
 586   __ movptr(rdx, Address(rsp, 13 * wordSize));
 587   __ movptr(rcx, Address(rsp, 14 * wordSize));
 588 
 589   __ addptr(rsp, 16 * wordSize);
 590 #else
 591 
 592   __ pop(rdi);
 593   __ pop(rsi);
 594   __ pop(rbp);
 595   __ pop(rbx); // skip this value
 596   __ pop(rbx);
 597   __ pop(rdx);
 598   __ pop(rcx);
 599   __ addptr(rsp, BytesPerWord);
 600 #endif // _LP64
 601 }
 602 
 603 
 604 void Runtime1::initialize_pd() {
 605   // nothing to do
 606 }
 607 
 608 
 609 // Target: the entry point of the method that creates and posts the exception oop.
 610 // has_argument: true if the exception needs arguments (passed on the stack because
 611 //               registers must be preserved).
 612 OopMapSet* Runtime1::generate_exception_throw(StubAssembler* sasm, address target, bool has_argument) {
 613   // Preserve all registers.
 614   int num_rt_args = has_argument ? (2 + 1) : 1;
 615   OopMap* oop_map = save_live_registers(sasm, num_rt_args);
 616 
 617   // Now all registers are saved and can be used freely.
 618   // Verify that no old value is used accidentally.
 619   __ invalidate_registers(true, true, true, true, true, true);
 620 
 621   // Registers used by this stub.
 622   const Register temp_reg = rbx;
 623 
 624   // Load arguments for exception that are passed as arguments into the stub.
 625   if (has_argument) {
 626 #ifdef _LP64
 627     __ movptr(c_rarg1, Address(rbp, 2*BytesPerWord));
 628     __ movptr(c_rarg2, Address(rbp, 3*BytesPerWord));
 629 #else
 630     __ movptr(temp_reg, Address(rbp, 3*BytesPerWord));
 631     __ push(temp_reg);
 632     __ movptr(temp_reg, Address(rbp, 2*BytesPerWord));
 633     __ push(temp_reg);
 634 #endif // _LP64
 635   }
 636   int call_offset = __ call_RT(noreg, noreg, target, num_rt_args - 1);
 637 
 638   OopMapSet* oop_maps = new OopMapSet();
 639   oop_maps->add_gc_map(call_offset, oop_map);
 640 
 641   __ stop("should not reach here");
 642 
 643   return oop_maps;
 644 }
 645 
 646 
 647 OopMapSet* Runtime1::generate_handle_exception(StubID id, StubAssembler *sasm) {
 648   __ block_comment("generate_handle_exception");
 649 
 650   // incoming parameters
 651   const Register exception_oop = rax;
 652   const Register exception_pc  = rdx;
 653   // other registers used in this stub
 654   const Register thread = NOT_LP64(rdi) LP64_ONLY(r15_thread);
 655 
 656   // Save registers, if required.
 657   OopMapSet* oop_maps = new OopMapSet();
 658   OopMap* oop_map = NULL;
 659   switch (id) {
 660   case forward_exception_id:
 661     // We're handling an exception in the context of a compiled frame.
 662     // The registers have been saved in the standard places.  Perform
 663     // an exception lookup in the caller and dispatch to the handler
 664     // if found.  Otherwise unwind and dispatch to the callers
 665     // exception handler.
 666     oop_map = generate_oop_map(sasm, 1 /*thread*/);
 667 
 668     // load and clear pending exception oop into RAX
 669     __ movptr(exception_oop, Address(thread, Thread::pending_exception_offset()));
 670     __ movptr(Address(thread, Thread::pending_exception_offset()), NULL_WORD);
 671 
 672     // load issuing PC (the return address for this stub) into rdx
 673     __ movptr(exception_pc, Address(rbp, 1*BytesPerWord));
 674 
 675     // make sure that the vm_results are cleared (may be unnecessary)
 676     __ movptr(Address(thread, JavaThread::vm_result_offset()),   NULL_WORD);
 677     __ movptr(Address(thread, JavaThread::vm_result_2_offset()), NULL_WORD);
 678     break;
 679   case handle_exception_nofpu_id:
 680   case handle_exception_id:
 681     // At this point all registers MAY be live.
 682     oop_map = save_live_registers(sasm, 1 /*thread*/, id != handle_exception_nofpu_id);
 683     break;
 684   case handle_exception_from_callee_id: {
 685     // At this point all registers except exception oop (RAX) and
 686     // exception pc (RDX) are dead.
 687     const int frame_size = 2 /*BP, return address*/ NOT_LP64(+ 1 /*thread*/) WIN64_ONLY(+ frame::arg_reg_save_area_bytes / BytesPerWord);
 688     oop_map = new OopMap(frame_size * VMRegImpl::slots_per_word, 0);
 689     sasm->set_frame_size(frame_size);
 690     WIN64_ONLY(__ subq(rsp, frame::arg_reg_save_area_bytes));
 691     break;
 692   }
 693   default:  ShouldNotReachHere();
 694   }
 695 
 696 #ifdef TIERED
 697   // C2 can leave the fpu stack dirty
 698   if (UseSSE < 2) {
 699     __ empty_FPU_stack();
 700   }
 701 #endif // TIERED
 702 
 703   // verify that only rax, and rdx is valid at this time
 704   __ invalidate_registers(false, true, true, false, true, true);
 705   // verify that rax, contains a valid exception
 706   __ verify_not_null_oop(exception_oop);
 707 
 708   // load address of JavaThread object for thread-local data
 709   NOT_LP64(__ get_thread(thread);)
 710 
 711 #ifdef ASSERT
 712   // check that fields in JavaThread for exception oop and issuing pc are
 713   // empty before writing to them
 714   Label oop_empty;
 715   __ cmpptr(Address(thread, JavaThread::exception_oop_offset()), (int32_t) NULL_WORD);
 716   __ jcc(Assembler::equal, oop_empty);
 717   __ stop("exception oop already set");
 718   __ bind(oop_empty);
 719 
 720   Label pc_empty;
 721   __ cmpptr(Address(thread, JavaThread::exception_pc_offset()), 0);
 722   __ jcc(Assembler::equal, pc_empty);
 723   __ stop("exception pc already set");
 724   __ bind(pc_empty);
 725 #endif
 726 
 727   // save exception oop and issuing pc into JavaThread
 728   // (exception handler will load it from here)
 729   __ movptr(Address(thread, JavaThread::exception_oop_offset()), exception_oop);
 730   __ movptr(Address(thread, JavaThread::exception_pc_offset()),  exception_pc);
 731 
 732   // patch throwing pc into return address (has bci & oop map)
 733   __ movptr(Address(rbp, 1*BytesPerWord), exception_pc);
 734 
 735   // compute the exception handler.
 736   // the exception oop and the throwing pc are read from the fields in JavaThread
 737   int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, exception_handler_for_pc));
 738   oop_maps->add_gc_map(call_offset, oop_map);
 739 
 740   // rax: handler address
 741   //      will be the deopt blob if nmethod was deoptimized while we looked up
 742   //      handler regardless of whether handler existed in the nmethod.
 743 
 744   // only rax, is valid at this time, all other registers have been destroyed by the runtime call
 745   __ invalidate_registers(false, true, true, true, true, true);
 746 
 747   // patch the return address, this stub will directly return to the exception handler
 748   __ movptr(Address(rbp, 1*BytesPerWord), rax);
 749 
 750   switch (id) {
 751   case forward_exception_id:
 752   case handle_exception_nofpu_id:
 753   case handle_exception_id:
 754     // Restore the registers that were saved at the beginning.
 755     restore_live_registers(sasm, id != handle_exception_nofpu_id);
 756     break;
 757   case handle_exception_from_callee_id:
 758     // WIN64_ONLY: No need to add frame::arg_reg_save_area_bytes to SP
 759     // since we do a leave anyway.
 760 
 761     // Pop the return address.
 762     __ leave();
 763     __ pop(rcx);
 764     __ jmp(rcx);  // jump to exception handler
 765     break;
 766   default:  ShouldNotReachHere();
 767   }
 768 
 769   return oop_maps;
 770 }
 771 
 772 
 773 void Runtime1::generate_unwind_exception(StubAssembler *sasm) {
 774   // incoming parameters
 775   const Register exception_oop = rax;
 776   // callee-saved copy of exception_oop during runtime call
 777   const Register exception_oop_callee_saved = NOT_LP64(rsi) LP64_ONLY(r14);
 778   // other registers used in this stub
 779   const Register exception_pc = rdx;
 780   const Register handler_addr = rbx;
 781   const Register thread = NOT_LP64(rdi) LP64_ONLY(r15_thread);
 782 
 783   // verify that only rax, is valid at this time
 784   __ invalidate_registers(false, true, true, true, true, true);
 785 
 786 #ifdef ASSERT
 787   // check that fields in JavaThread for exception oop and issuing pc are empty
 788   NOT_LP64(__ get_thread(thread);)
 789   Label oop_empty;
 790   __ cmpptr(Address(thread, JavaThread::exception_oop_offset()), 0);
 791   __ jcc(Assembler::equal, oop_empty);
 792   __ stop("exception oop must be empty");
 793   __ bind(oop_empty);
 794 
 795   Label pc_empty;
 796   __ cmpptr(Address(thread, JavaThread::exception_pc_offset()), 0);
 797   __ jcc(Assembler::equal, pc_empty);
 798   __ stop("exception pc must be empty");
 799   __ bind(pc_empty);
 800 #endif
 801 
 802   // clear the FPU stack in case any FPU results are left behind
 803   __ empty_FPU_stack();
 804 
 805   // save exception_oop in callee-saved register to preserve it during runtime calls
 806   __ verify_not_null_oop(exception_oop);
 807   __ movptr(exception_oop_callee_saved, exception_oop);
 808 
 809   NOT_LP64(__ get_thread(thread);)
 810   // Get return address (is on top of stack after leave).
 811   __ movptr(exception_pc, Address(rsp, 0));
 812 
 813   // search the exception handler address of the caller (using the return address)
 814   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), thread, exception_pc);
 815   // rax: exception handler address of the caller
 816 
 817   // Only RAX and RSI are valid at this time, all other registers have been destroyed by the call.
 818   __ invalidate_registers(false, true, true, true, false, true);
 819 
 820   // move result of call into correct register
 821   __ movptr(handler_addr, rax);
 822 
 823   // Restore exception oop to RAX (required convention of exception handler).
 824   __ movptr(exception_oop, exception_oop_callee_saved);
 825 
 826   // verify that there is really a valid exception in rax
 827   __ verify_not_null_oop(exception_oop);
 828 
 829   // get throwing pc (= return address).
 830   // rdx has been destroyed by the call, so it must be set again
 831   // the pop is also necessary to simulate the effect of a ret(0)
 832   __ pop(exception_pc);
 833 
 834   // continue at exception handler (return address removed)
 835   // note: do *not* remove arguments when unwinding the
 836   //       activation since the caller assumes having
 837   //       all arguments on the stack when entering the
 838   //       runtime to determine the exception handler
 839   //       (GC happens at call site with arguments!)
 840   // rax: exception oop
 841   // rdx: throwing pc
 842   // rbx: exception handler
 843   __ jmp(handler_addr);
 844 }
 845 
 846 
 847 OopMapSet* Runtime1::generate_patching(StubAssembler* sasm, address target) {
 848   // use the maximum number of runtime-arguments here because it is difficult to
 849   // distinguish each RT-Call.
 850   // Note: This number affects also the RT-Call in generate_handle_exception because
 851   //       the oop-map is shared for all calls.
 852   const int num_rt_args = 2;  // thread + dummy
 853 
 854   DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
 855   assert(deopt_blob != NULL, "deoptimization blob must have been created");
 856 
 857   OopMap* oop_map = save_live_registers(sasm, num_rt_args);
 858 
 859 #ifdef _LP64
 860   const Register thread = r15_thread;
 861   // No need to worry about dummy
 862   __ mov(c_rarg0, thread);
 863 #else
 864   __ push(rax); // push dummy
 865 
 866   const Register thread = rdi; // is callee-saved register (Visual C++ calling conventions)
 867   // push java thread (becomes first argument of C function)
 868   __ get_thread(thread);
 869   __ push(thread);
 870 #endif // _LP64
 871   __ set_last_Java_frame(thread, noreg, rbp, NULL);
 872   // do the call
 873   __ call(RuntimeAddress(target));
 874   OopMapSet* oop_maps = new OopMapSet();
 875   oop_maps->add_gc_map(__ offset(), oop_map);
 876   // verify callee-saved register
 877 #ifdef ASSERT
 878   guarantee(thread != rax, "change this code");
 879   __ push(rax);
 880   { Label L;
 881     __ get_thread(rax);
 882     __ cmpptr(thread, rax);
 883     __ jcc(Assembler::equal, L);
 884     __ stop("StubAssembler::call_RT: rdi/r15 not callee saved?");
 885     __ bind(L);
 886   }
 887   __ pop(rax);
 888 #endif
 889   __ reset_last_Java_frame(thread, true);
 890 #ifndef _LP64
 891   __ pop(rcx); // discard thread arg
 892   __ pop(rcx); // discard dummy
 893 #endif // _LP64
 894 
 895   // check for pending exceptions
 896   { Label L;
 897     __ cmpptr(Address(thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 898     __ jcc(Assembler::equal, L);
 899     // exception pending => remove activation and forward to exception handler
 900 
 901     __ testptr(rax, rax);                                   // have we deoptimized?
 902     __ jump_cc(Assembler::equal,
 903                RuntimeAddress(Runtime1::entry_for(Runtime1::forward_exception_id)));
 904 
 905     // the deopt blob expects exceptions in the special fields of
 906     // JavaThread, so copy and clear pending exception.
 907 
 908     // load and clear pending exception
 909     __ movptr(rax, Address(thread, Thread::pending_exception_offset()));
 910     __ movptr(Address(thread, Thread::pending_exception_offset()), NULL_WORD);
 911 
 912     // check that there is really a valid exception
 913     __ verify_not_null_oop(rax);
 914 
 915     // load throwing pc: this is the return address of the stub
 916     __ movptr(rdx, Address(rsp, return_off * VMRegImpl::stack_slot_size));
 917 
 918 #ifdef ASSERT
 919     // check that fields in JavaThread for exception oop and issuing pc are empty
 920     Label oop_empty;
 921     __ cmpptr(Address(thread, JavaThread::exception_oop_offset()), (int32_t)NULL_WORD);
 922     __ jcc(Assembler::equal, oop_empty);
 923     __ stop("exception oop must be empty");
 924     __ bind(oop_empty);
 925 
 926     Label pc_empty;
 927     __ cmpptr(Address(thread, JavaThread::exception_pc_offset()), (int32_t)NULL_WORD);
 928     __ jcc(Assembler::equal, pc_empty);
 929     __ stop("exception pc must be empty");
 930     __ bind(pc_empty);
 931 #endif
 932 
 933     // store exception oop and throwing pc to JavaThread
 934     __ movptr(Address(thread, JavaThread::exception_oop_offset()), rax);
 935     __ movptr(Address(thread, JavaThread::exception_pc_offset()), rdx);
 936 
 937     restore_live_registers(sasm);
 938 
 939     __ leave();
 940     __ addptr(rsp, BytesPerWord);  // remove return address from stack
 941 
 942     // Forward the exception directly to deopt blob. We can blow no
 943     // registers and must leave throwing pc on the stack.  A patch may
 944     // have values live in registers so the entry point with the
 945     // exception in tls.
 946     __ jump(RuntimeAddress(deopt_blob->unpack_with_exception_in_tls()));
 947 
 948     __ bind(L);
 949   }
 950 
 951 
 952   // Runtime will return true if the nmethod has been deoptimized during
 953   // the patching process. In that case we must do a deopt reexecute instead.
 954 
 955   Label reexecuteEntry, cont;
 956 
 957   __ testptr(rax, rax);                                 // have we deoptimized?
 958   __ jcc(Assembler::equal, cont);                       // no
 959 
 960   // Will reexecute. Proper return address is already on the stack we just restore
 961   // registers, pop all of our frame but the return address and jump to the deopt blob
 962   restore_live_registers(sasm);
 963   __ leave();
 964   __ jump(RuntimeAddress(deopt_blob->unpack_with_reexecution()));
 965 
 966   __ bind(cont);
 967   restore_live_registers(sasm);
 968   __ leave();
 969   __ ret(0);
 970 
 971   return oop_maps;
 972 }
 973 
 974 
 975 OopMapSet* Runtime1::generate_code_for(StubID id, StubAssembler* sasm) {
 976 
 977   // for better readability
 978   const bool must_gc_arguments = true;
 979   const bool dont_gc_arguments = false;
 980 
 981   // default value; overwritten for some optimized stubs that are called from methods that do not use the fpu
 982   bool save_fpu_registers = true;
 983 
 984   // stub code & info for the different stubs
 985   OopMapSet* oop_maps = NULL;
 986   switch (id) {
 987     case forward_exception_id:
 988       {
 989         oop_maps = generate_handle_exception(id, sasm);
 990         __ leave();
 991         __ ret(0);
 992       }
 993       break;
 994 
 995     case new_instance_id:
 996     case fast_new_instance_id:
 997     case fast_new_instance_init_check_id:
 998       {
 999         Register klass = rdx; // Incoming
1000         Register obj   = rax; // Result
1001 
1002         if (id == new_instance_id) {
1003           __ set_info("new_instance", dont_gc_arguments);
1004         } else if (id == fast_new_instance_id) {
1005           __ set_info("fast new_instance", dont_gc_arguments);
1006         } else {
1007           assert(id == fast_new_instance_init_check_id, "bad StubID");
1008           __ set_info("fast new_instance init check", dont_gc_arguments);
1009         }
1010 
1011         if ((id == fast_new_instance_id || id == fast_new_instance_init_check_id) && UseTLAB
1012             && Universe::heap()->supports_inline_contig_alloc()) {
1013           Label slow_path;
1014           Register obj_size = rcx;
1015           Register t1       = rbx;
1016           Register t2       = rsi;
1017           assert_different_registers(klass, obj, obj_size, t1, t2);
1018 
1019           __ push(rdi);
1020           __ push(rbx);
1021 
1022           if (id == fast_new_instance_init_check_id) {
1023             // make sure the klass is initialized
1024             __ cmpb(Address(klass, InstanceKlass::init_state_offset()), InstanceKlass::fully_initialized);
1025             __ jcc(Assembler::notEqual, slow_path);
1026           }
1027 
1028 #ifdef ASSERT
1029           // assert object can be fast path allocated
1030           {
1031             Label ok, not_ok;
1032             __ movl(obj_size, Address(klass, Klass::layout_helper_offset()));
1033             __ cmpl(obj_size, 0);  // make sure it's an instance (LH > 0)
1034             __ jcc(Assembler::lessEqual, not_ok);
1035             __ testl(obj_size, Klass::_lh_instance_slow_path_bit);
1036             __ jcc(Assembler::zero, ok);
1037             __ bind(not_ok);
1038             __ stop("assert(can be fast path allocated)");
1039             __ should_not_reach_here();
1040             __ bind(ok);
1041           }
1042 #endif // ASSERT
1043 
1044           // if we got here then the TLAB allocation failed, so try
1045           // refilling the TLAB or allocating directly from eden.
1046           Label retry_tlab, try_eden;
1047           const Register thread = NOT_LP64(rdi) LP64_ONLY(r15_thread);
1048           NOT_LP64(__ get_thread(thread));
1049 
1050           __ bind(try_eden);
1051           // get the instance size (size is postive so movl is fine for 64bit)
1052           __ movl(obj_size, Address(klass, Klass::layout_helper_offset()));
1053 
1054           __ eden_allocate(obj, obj_size, 0, t1, slow_path);
1055           __ incr_allocated_bytes(thread, obj_size, 0);
1056 
1057           __ initialize_object(obj, klass, obj_size, 0, t1, t2, /* is_tlab_allocated */ false);
1058           __ verify_oop(obj);
1059           __ pop(rbx);
1060           __ pop(rdi);
1061           __ ret(0);
1062 
1063           __ bind(slow_path);
1064           __ pop(rbx);
1065           __ pop(rdi);
1066         }
1067 
1068         __ enter();
1069         OopMap* map = save_live_registers(sasm, 2);
1070         int call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_instance), klass);
1071         oop_maps = new OopMapSet();
1072         oop_maps->add_gc_map(call_offset, map);
1073         restore_live_registers_except_rax(sasm);
1074         __ verify_oop(obj);
1075         __ leave();
1076         __ ret(0);
1077 
1078         // rax,: new instance
1079       }
1080 
1081       break;
1082 
1083     case counter_overflow_id:
1084       {
1085         Register bci = rax, method = rbx;
1086         __ enter();
1087         OopMap* map = save_live_registers(sasm, 3);
1088         // Retrieve bci
1089         __ movl(bci, Address(rbp, 2*BytesPerWord));
1090         // And a pointer to the Method*
1091         __ movptr(method, Address(rbp, 3*BytesPerWord));
1092         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, counter_overflow), bci, method);
1093         oop_maps = new OopMapSet();
1094         oop_maps->add_gc_map(call_offset, map);
1095         restore_live_registers(sasm);
1096         __ leave();
1097         __ ret(0);
1098       }
1099       break;
1100 
1101     case new_type_array_id:
1102     case new_object_array_id:
1103       {
1104         Register length   = rbx; // Incoming
1105         Register klass    = rdx; // Incoming
1106         Register obj      = rax; // Result
1107 
1108         if (id == new_type_array_id) {
1109           __ set_info("new_type_array", dont_gc_arguments);
1110         } else {
1111           __ set_info("new_object_array", dont_gc_arguments);
1112         }
1113 
1114 #ifdef ASSERT
1115         // assert object type is really an array of the proper kind
1116         {
1117           Label ok;
1118           Register t0 = obj;
1119           __ movl(t0, Address(klass, Klass::layout_helper_offset()));
1120           __ sarl(t0, Klass::_lh_array_tag_shift);
1121           int tag = ((id == new_type_array_id)
1122                      ? Klass::_lh_array_tag_type_value
1123                      : Klass::_lh_array_tag_obj_value);
1124           __ cmpl(t0, tag);
1125           __ jcc(Assembler::equal, ok);
1126           __ stop("assert(is an array klass)");
1127           __ should_not_reach_here();
1128           __ bind(ok);
1129         }
1130 #endif // ASSERT
1131 
1132         // If we got here, the TLAB allocation failed, so try allocating from
1133         // eden if inline contiguous allocations are supported.
1134         if (UseTLAB && Universe::heap()->supports_inline_contig_alloc()) {
1135           Register arr_size = rsi;
1136           Register t1       = rcx;  // must be rcx for use as shift count
1137           Register t2       = rdi;
1138           Label slow_path;
1139 
1140           // get the allocation size: round_up(hdr + length << (layout_helper & 0x1F))
1141           // since size is positive movl does right thing on 64bit
1142           __ movl(t1, Address(klass, Klass::layout_helper_offset()));
1143           // since size is postive movl does right thing on 64bit
1144           __ movl(arr_size, length);
1145           assert(t1 == rcx, "fixed register usage");
1146           __ shlptr(arr_size /* by t1=rcx, mod 32 */);
1147           __ shrptr(t1, Klass::_lh_header_size_shift);
1148           __ andptr(t1, Klass::_lh_header_size_mask);
1149           __ addptr(arr_size, t1);
1150           __ addptr(arr_size, MinObjAlignmentInBytesMask); // align up
1151           __ andptr(arr_size, ~MinObjAlignmentInBytesMask);
1152 
1153           __ eden_allocate(obj, arr_size, 0, t1, slow_path);  // preserves arr_size
1154 
1155           // Using t2 for non 64-bit.
1156           const Register thread = NOT_LP64(t2) LP64_ONLY(r15_thread);
1157           NOT_LP64(__ get_thread(thread));
1158           __ incr_allocated_bytes(thread, arr_size, 0);
1159 
1160           __ initialize_header(obj, klass, length, t1, t2);
1161           __ movb(t1, Address(klass, in_bytes(Klass::layout_helper_offset()) + (Klass::_lh_header_size_shift / BitsPerByte)));
1162           assert(Klass::_lh_header_size_shift % BitsPerByte == 0, "bytewise");
1163           assert(Klass::_lh_header_size_mask <= 0xFF, "bytewise");
1164           __ andptr(t1, Klass::_lh_header_size_mask);
1165           __ subptr(arr_size, t1);  // body length
1166           __ addptr(t1, obj);       // body start
1167           __ initialize_body(t1, arr_size, 0, t2);
1168           __ verify_oop(obj);
1169           __ ret(0);
1170 
1171           __ bind(slow_path);
1172         }
1173 
1174         __ enter();
1175         OopMap* map = save_live_registers(sasm, 3);
1176         int call_offset;
1177         if (id == new_type_array_id) {
1178           call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_type_array), klass, length);
1179         } else {
1180           call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_object_array), klass, length);
1181         }
1182 
1183         oop_maps = new OopMapSet();
1184         oop_maps->add_gc_map(call_offset, map);
1185         restore_live_registers_except_rax(sasm);
1186 
1187         __ verify_oop(obj);
1188         __ leave();
1189         __ ret(0);
1190 
1191         // rax,: new array
1192       }
1193       break;
1194 
1195     case new_multi_array_id:
1196       { StubFrame f(sasm, "new_multi_array", dont_gc_arguments);
1197         // rax,: klass
1198         // rbx,: rank
1199         // rcx: address of 1st dimension
1200         OopMap* map = save_live_registers(sasm, 4);
1201         int call_offset = __ call_RT(rax, noreg, CAST_FROM_FN_PTR(address, new_multi_array), rax, rbx, rcx);
1202 
1203         oop_maps = new OopMapSet();
1204         oop_maps->add_gc_map(call_offset, map);
1205         restore_live_registers_except_rax(sasm);
1206 
1207         // rax,: new multi array
1208         __ verify_oop(rax);
1209       }
1210       break;
1211 
1212     case register_finalizer_id:
1213       {
1214         __ set_info("register_finalizer", dont_gc_arguments);
1215 
1216         // This is called via call_runtime so the arguments
1217         // will be place in C abi locations
1218 
1219 #ifdef _LP64
1220         __ verify_oop(c_rarg0);
1221         __ mov(rax, c_rarg0);
1222 #else
1223         // The object is passed on the stack and we haven't pushed a
1224         // frame yet so it's one work away from top of stack.
1225         __ movptr(rax, Address(rsp, 1 * BytesPerWord));
1226         __ verify_oop(rax);
1227 #endif // _LP64
1228 
1229         // load the klass and check the has finalizer flag
1230         Label register_finalizer;
1231         Register t = rsi;
1232         __ load_klass(t, rax);
1233         __ movl(t, Address(t, Klass::access_flags_offset()));
1234         __ testl(t, JVM_ACC_HAS_FINALIZER);
1235         __ jcc(Assembler::notZero, register_finalizer);
1236         __ ret(0);
1237 
1238         __ bind(register_finalizer);
1239         __ enter();
1240         OopMap* oop_map = save_live_registers(sasm, 2 /*num_rt_args */);
1241         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, SharedRuntime::register_finalizer), rax);
1242         oop_maps = new OopMapSet();
1243         oop_maps->add_gc_map(call_offset, oop_map);
1244 
1245         // Now restore all the live registers
1246         restore_live_registers(sasm);
1247 
1248         __ leave();
1249         __ ret(0);
1250       }
1251       break;
1252 
1253     case throw_range_check_failed_id:
1254       { StubFrame f(sasm, "range_check_failed", dont_gc_arguments);
1255         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_range_check_exception), true);
1256       }
1257       break;
1258 
1259     case throw_index_exception_id:
1260       { StubFrame f(sasm, "index_range_check_failed", dont_gc_arguments);
1261         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_index_exception), true);
1262       }
1263       break;
1264 
1265     case throw_div0_exception_id:
1266       { StubFrame f(sasm, "throw_div0_exception", dont_gc_arguments);
1267         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_div0_exception), false);
1268       }
1269       break;
1270 
1271     case throw_null_pointer_exception_id:
1272       { StubFrame f(sasm, "throw_null_pointer_exception", dont_gc_arguments);
1273         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_null_pointer_exception), false);
1274       }
1275       break;
1276 
1277     case handle_exception_nofpu_id:
1278     case handle_exception_id:
1279       { StubFrame f(sasm, "handle_exception", dont_gc_arguments);
1280         oop_maps = generate_handle_exception(id, sasm);
1281       }
1282       break;
1283 
1284     case handle_exception_from_callee_id:
1285       { StubFrame f(sasm, "handle_exception_from_callee", dont_gc_arguments);
1286         oop_maps = generate_handle_exception(id, sasm);
1287       }
1288       break;
1289 
1290     case unwind_exception_id:
1291       { __ set_info("unwind_exception", dont_gc_arguments);
1292         // note: no stubframe since we are about to leave the current
1293         //       activation and we are calling a leaf VM function only.
1294         generate_unwind_exception(sasm);
1295       }
1296       break;
1297 
1298     case throw_array_store_exception_id:
1299       { StubFrame f(sasm, "throw_array_store_exception", dont_gc_arguments);
1300         // tos + 0: link
1301         //     + 1: return address
1302         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_array_store_exception), true);
1303       }
1304       break;
1305 
1306     case throw_class_cast_exception_id:
1307       { StubFrame f(sasm, "throw_class_cast_exception", dont_gc_arguments);
1308         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_class_cast_exception), true);
1309       }
1310       break;
1311 
1312     case throw_incompatible_class_change_error_id:
1313       { StubFrame f(sasm, "throw_incompatible_class_cast_exception", dont_gc_arguments);
1314         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_incompatible_class_change_error), false);
1315       }
1316       break;
1317 
1318     case slow_subtype_check_id:
1319       {
1320         // Typical calling sequence:
1321         // __ push(klass_RInfo);  // object klass or other subclass
1322         // __ push(sup_k_RInfo);  // array element klass or other superclass
1323         // __ call(slow_subtype_check);
1324         // Note that the subclass is pushed first, and is therefore deepest.
1325         // Previous versions of this code reversed the names 'sub' and 'super'.
1326         // This was operationally harmless but made the code unreadable.
1327         enum layout {
1328           rax_off, SLOT2(raxH_off)
1329           rcx_off, SLOT2(rcxH_off)
1330           rsi_off, SLOT2(rsiH_off)
1331           rdi_off, SLOT2(rdiH_off)
1332           // saved_rbp_off, SLOT2(saved_rbpH_off)
1333           return_off, SLOT2(returnH_off)
1334           sup_k_off, SLOT2(sup_kH_off)
1335           klass_off, SLOT2(superH_off)
1336           framesize,
1337           result_off = klass_off  // deepest argument is also the return value
1338         };
1339 
1340         __ set_info("slow_subtype_check", dont_gc_arguments);
1341         __ push(rdi);
1342         __ push(rsi);
1343         __ push(rcx);
1344         __ push(rax);
1345 
1346         // This is called by pushing args and not with C abi
1347         __ movptr(rsi, Address(rsp, (klass_off) * VMRegImpl::stack_slot_size)); // subclass
1348         __ movptr(rax, Address(rsp, (sup_k_off) * VMRegImpl::stack_slot_size)); // superclass
1349 
1350         Label miss;
1351         __ check_klass_subtype_slow_path(rsi, rax, rcx, rdi, NULL, &miss);
1352 
1353         // fallthrough on success:
1354         __ movptr(Address(rsp, (result_off) * VMRegImpl::stack_slot_size), 1); // result
1355         __ pop(rax);
1356         __ pop(rcx);
1357         __ pop(rsi);
1358         __ pop(rdi);
1359         __ ret(0);
1360 
1361         __ bind(miss);
1362         __ movptr(Address(rsp, (result_off) * VMRegImpl::stack_slot_size), NULL_WORD); // result
1363         __ pop(rax);
1364         __ pop(rcx);
1365         __ pop(rsi);
1366         __ pop(rdi);
1367         __ ret(0);
1368       }
1369       break;
1370 
1371     case monitorenter_nofpu_id:
1372       save_fpu_registers = false;
1373       // fall through
1374     case monitorenter_id:
1375       {
1376         StubFrame f(sasm, "monitorenter", dont_gc_arguments);
1377         OopMap* map = save_live_registers(sasm, 3, save_fpu_registers);
1378 
1379         // Called with store_parameter and not C abi
1380 
1381         f.load_argument(1, rax); // rax,: object
1382         f.load_argument(0, rbx); // rbx,: lock address
1383 
1384         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorenter), rax, rbx);
1385 
1386         oop_maps = new OopMapSet();
1387         oop_maps->add_gc_map(call_offset, map);
1388         restore_live_registers(sasm, save_fpu_registers);
1389       }
1390       break;
1391 
1392     case monitorexit_nofpu_id:
1393       save_fpu_registers = false;
1394       // fall through
1395     case monitorexit_id:
1396       {
1397         StubFrame f(sasm, "monitorexit", dont_gc_arguments);
1398         OopMap* map = save_live_registers(sasm, 2, save_fpu_registers);
1399 
1400         // Called with store_parameter and not C abi
1401 
1402         f.load_argument(0, rax); // rax,: lock address
1403 
1404         // note: really a leaf routine but must setup last java sp
1405         //       => use call_RT for now (speed can be improved by
1406         //       doing last java sp setup manually)
1407         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorexit), rax);
1408 
1409         oop_maps = new OopMapSet();
1410         oop_maps->add_gc_map(call_offset, map);
1411         restore_live_registers(sasm, save_fpu_registers);
1412       }
1413       break;
1414 
1415     case deoptimize_id:
1416       {
1417         StubFrame f(sasm, "deoptimize", dont_gc_arguments);
1418         const int num_rt_args = 2;  // thread, trap_request
1419         OopMap* oop_map = save_live_registers(sasm, num_rt_args);
1420         f.load_argument(0, rax);
1421         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, deoptimize), rax);
1422         oop_maps = new OopMapSet();
1423         oop_maps->add_gc_map(call_offset, oop_map);
1424         restore_live_registers(sasm);
1425         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
1426         assert(deopt_blob != NULL, "deoptimization blob must have been created");
1427         __ leave();
1428         __ jump(RuntimeAddress(deopt_blob->unpack_with_reexecution()));
1429       }
1430       break;
1431 
1432     case access_field_patching_id:
1433       { StubFrame f(sasm, "access_field_patching", dont_gc_arguments);
1434         // we should set up register map
1435         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, access_field_patching));
1436       }
1437       break;
1438 
1439     case load_klass_patching_id:
1440       { StubFrame f(sasm, "load_klass_patching", dont_gc_arguments);
1441         // we should set up register map
1442         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_klass_patching));
1443       }
1444       break;
1445 
1446     case load_mirror_patching_id:
1447       { StubFrame f(sasm, "load_mirror_patching", dont_gc_arguments);
1448         // we should set up register map
1449         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_mirror_patching));
1450       }
1451       break;
1452 
1453     case load_appendix_patching_id:
1454       { StubFrame f(sasm, "load_appendix_patching", dont_gc_arguments);
1455         // we should set up register map
1456         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, move_appendix_patching));
1457       }
1458       break;
1459 
1460     case dtrace_object_alloc_id:
1461       { // rax,: object
1462         StubFrame f(sasm, "dtrace_object_alloc", dont_gc_arguments);
1463         // we can't gc here so skip the oopmap but make sure that all
1464         // the live registers get saved.
1465         save_live_registers(sasm, 1);
1466 
1467         __ NOT_LP64(push(rax)) LP64_ONLY(mov(c_rarg0, rax));
1468         __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc)));
1469         NOT_LP64(__ pop(rax));
1470 
1471         restore_live_registers(sasm);
1472       }
1473       break;
1474 
1475     case fpu2long_stub_id:
1476       {
1477         // rax, and rdx are destroyed, but should be free since the result is returned there
1478         // preserve rsi,ecx
1479         __ push(rsi);
1480         __ push(rcx);
1481         LP64_ONLY(__ push(rdx);)
1482 
1483         // check for NaN
1484         Label return0, do_return, return_min_jlong, do_convert;
1485 
1486         Address value_high_word(rsp, wordSize + 4);
1487         Address value_low_word(rsp, wordSize);
1488         Address result_high_word(rsp, 3*wordSize + 4);
1489         Address result_low_word(rsp, 3*wordSize);
1490 
1491         __ subptr(rsp, 32);                    // more than enough on 32bit
1492         __ fst_d(value_low_word);
1493         __ movl(rax, value_high_word);
1494         __ andl(rax, 0x7ff00000);
1495         __ cmpl(rax, 0x7ff00000);
1496         __ jcc(Assembler::notEqual, do_convert);
1497         __ movl(rax, value_high_word);
1498         __ andl(rax, 0xfffff);
1499         __ orl(rax, value_low_word);
1500         __ jcc(Assembler::notZero, return0);
1501 
1502         __ bind(do_convert);
1503         __ fnstcw(Address(rsp, 0));
1504         __ movzwl(rax, Address(rsp, 0));
1505         __ orl(rax, 0xc00);
1506         __ movw(Address(rsp, 2), rax);
1507         __ fldcw(Address(rsp, 2));
1508         __ fwait();
1509         __ fistp_d(result_low_word);
1510         __ fldcw(Address(rsp, 0));
1511         __ fwait();
1512         // This gets the entire long in rax on 64bit
1513         __ movptr(rax, result_low_word);
1514         // testing of high bits
1515         __ movl(rdx, result_high_word);
1516         __ mov(rcx, rax);
1517         // What the heck is the point of the next instruction???
1518         __ xorl(rcx, 0x0);
1519         __ movl(rsi, 0x80000000);
1520         __ xorl(rsi, rdx);
1521         __ orl(rcx, rsi);
1522         __ jcc(Assembler::notEqual, do_return);
1523         __ fldz();
1524         __ fcomp_d(value_low_word);
1525         __ fnstsw_ax();
1526 #ifdef _LP64
1527         __ testl(rax, 0x4100);  // ZF & CF == 0
1528         __ jcc(Assembler::equal, return_min_jlong);
1529 #else
1530         __ sahf();
1531         __ jcc(Assembler::above, return_min_jlong);
1532 #endif // _LP64
1533         // return max_jlong
1534 #ifndef _LP64
1535         __ movl(rdx, 0x7fffffff);
1536         __ movl(rax, 0xffffffff);
1537 #else
1538         __ mov64(rax, CONST64(0x7fffffffffffffff));
1539 #endif // _LP64
1540         __ jmp(do_return);
1541 
1542         __ bind(return_min_jlong);
1543 #ifndef _LP64
1544         __ movl(rdx, 0x80000000);
1545         __ xorl(rax, rax);
1546 #else
1547         __ mov64(rax, UCONST64(0x8000000000000000));
1548 #endif // _LP64
1549         __ jmp(do_return);
1550 
1551         __ bind(return0);
1552         __ fpop();
1553 #ifndef _LP64
1554         __ xorptr(rdx,rdx);
1555         __ xorptr(rax,rax);
1556 #else
1557         __ xorptr(rax, rax);
1558 #endif // _LP64
1559 
1560         __ bind(do_return);
1561         __ addptr(rsp, 32);
1562         LP64_ONLY(__ pop(rdx);)
1563         __ pop(rcx);
1564         __ pop(rsi);
1565         __ ret(0);
1566       }
1567       break;
1568 
1569     case predicate_failed_trap_id:
1570       {
1571         StubFrame f(sasm, "predicate_failed_trap", dont_gc_arguments);
1572 
1573         OopMap* map = save_live_registers(sasm, 1);
1574 
1575         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, predicate_failed_trap));
1576         oop_maps = new OopMapSet();
1577         oop_maps->add_gc_map(call_offset, map);
1578         restore_live_registers(sasm);
1579         __ leave();
1580         DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
1581         assert(deopt_blob != NULL, "deoptimization blob must have been created");
1582 
1583         __ jump(RuntimeAddress(deopt_blob->unpack_with_reexecution()));
1584       }
1585       break;
1586 
1587     default:
1588       { StubFrame f(sasm, "unimplemented entry", dont_gc_arguments);
1589         __ movptr(rax, (int)id);
1590         __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, unimplemented_entry), rax);
1591         __ should_not_reach_here();
1592       }
1593       break;
1594   }
1595   return oop_maps;
1596 }
1597 
1598 #undef __
1599 
1600 const char *Runtime1::pd_name_for_address(address entry) {
1601   return "<unknown function>";
1602 }