hotspot/src/cpu/x86/vm/c1_Runtime1_x86.cpp

Print this page
rev 611 : Merge
   1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)c1_Runtime1_x86.cpp  1.197 07/09/17 09:25:58 JVM"
   3 #endif
   4 /*
   5  * Copyright 1999-2007 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 #include "incls/_precompiled.incl"
  29 #include "incls/_c1_Runtime1_x86.cpp.incl"
  30 
  31 
  32 // Implementation of StubAssembler
  33 
  34 int StubAssembler::call_RT(Register oop_result1, Register oop_result2, address entry, int args_size) {
  35   // setup registers
  36   const Register thread = rdi; // is callee-saved register (Visual C++ calling conventions)
  37   assert(!(oop_result1->is_valid() || oop_result2->is_valid()) || oop_result1 != oop_result2, "registers must be different");
  38   assert(oop_result1 != thread && oop_result2 != thread, "registers must be different");
  39   assert(args_size >= 0, "illegal args_size");
  40 




  41   set_num_rt_args(1 + args_size);
  42 
  43   // push java thread (becomes first argument of C function)
  44   get_thread(thread);
  45   pushl(thread);

  46 
  47   set_last_Java_frame(thread, noreg, rbp, NULL);

  48   // do the call
  49   call(RuntimeAddress(entry));
  50   int call_offset = offset();
  51   // verify callee-saved register
  52 #ifdef ASSERT
  53   guarantee(thread != rax, "change this code");
  54   pushl(rax);
  55   { Label L;
  56     get_thread(rax);
  57     cmpl(thread, rax);
  58     jcc(Assembler::equal, L);
  59     int3();
  60     stop("StubAssembler::call_RT: rdi not callee saved?");
  61     bind(L);
  62   }
  63   popl(rax);
  64 #endif
  65   reset_last_Java_frame(thread, true, false);
  66 
  67   // discard thread and arguments
  68   addl(rsp, (1 + args_size)*BytesPerWord);
  69 
  70   // check for pending exceptions
  71   { Label L;
  72     cmpl(Address(thread, Thread::pending_exception_offset()), NULL_WORD);
  73     jcc(Assembler::equal, L);
  74     // exception pending => remove activation and forward to exception handler
  75     movl(rax, Address(thread, Thread::pending_exception_offset()));
  76     // make sure that the vm_results are cleared
  77     if (oop_result1->is_valid()) {
  78       movl(Address(thread, JavaThread::vm_result_offset()), NULL_WORD);
  79     }
  80     if (oop_result2->is_valid()) {
  81       movl(Address(thread, JavaThread::vm_result_2_offset()), NULL_WORD);
  82     }
  83     if (frame_size() == no_frame_size) {
  84       leave();
  85       jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
  86     } else if (_stub_id == Runtime1::forward_exception_id) {
  87       should_not_reach_here();
  88     } else {
  89       jump(RuntimeAddress(Runtime1::entry_for(Runtime1::forward_exception_id)));
  90     }
  91     bind(L);
  92   }
  93   // get oop results if there are any and reset the values in the thread
  94   if (oop_result1->is_valid()) {
  95     movl(oop_result1, Address(thread, JavaThread::vm_result_offset()));
  96     movl(Address(thread, JavaThread::vm_result_offset()), NULL_WORD);
  97     verify_oop(oop_result1);
  98   }
  99   if (oop_result2->is_valid()) {
 100     movl(oop_result2, Address(thread, JavaThread::vm_result_2_offset()));
 101     movl(Address(thread, JavaThread::vm_result_2_offset()), NULL_WORD);
 102     verify_oop(oop_result2);
 103   }
 104   return call_offset;
 105 }
 106 
 107 
 108 int StubAssembler::call_RT(Register oop_result1, Register oop_result2, address entry, Register arg1) {
 109   pushl(arg1);




 110   return call_RT(oop_result1, oop_result2, entry, 1);
 111 }
 112 
 113 
 114 int StubAssembler::call_RT(Register oop_result1, Register oop_result2, address entry, Register arg1, Register arg2) {
 115   pushl(arg2);
 116   pushl(arg1);














 117   return call_RT(oop_result1, oop_result2, entry, 2);
 118 }
 119 
 120 
 121 int StubAssembler::call_RT(Register oop_result1, Register oop_result2, address entry, Register arg1, Register arg2, Register arg3) {
 122   pushl(arg3);
 123   pushl(arg2);
 124   pushl(arg1);


















 125   return call_RT(oop_result1, oop_result2, entry, 3);
 126 }
 127 
 128 
 129 // Implementation of StubFrame
 130 
 131 class StubFrame: public StackObj {
 132  private:
 133   StubAssembler* _sasm;
 134 
 135  public:
 136   StubFrame(StubAssembler* sasm, const char* name, bool must_gc_arguments);
 137   void load_argument(int offset_in_words, Register reg);
 138 
 139   ~StubFrame();
 140 };
 141 
 142 
 143 #define __ _sasm->
 144 
 145 StubFrame::StubFrame(StubAssembler* sasm, const char* name, bool must_gc_arguments) {
 146   _sasm = sasm;
 147   __ set_info(name, must_gc_arguments);
 148   __ enter();
 149 }
 150 
 151 // load parameters that were stored with LIR_Assembler::store_parameter
 152 // Note: offsets for store_parameter and load_argument must match
 153 void StubFrame::load_argument(int offset_in_words, Register reg) {
 154   // rbp, + 0: link
 155   //     + 1: return address
 156   //     + 2: argument with offset 0
 157   //     + 3: argument with offset 1
 158   //     + 4: ...
 159 
 160   __ movl(reg, Address(rbp, (offset_in_words + 2) * BytesPerWord));
 161 }
 162 
 163 
 164 StubFrame::~StubFrame() {
 165   __ leave();
 166   __ ret(0);
 167 }
 168 
 169 #undef __
 170 
 171 
 172 // Implementation of Runtime1
 173 
 174 #define __ sasm->
 175 
 176 const int float_regs_as_doubles_size_in_words = 16;
 177 const int xmm_regs_as_doubles_size_in_words = 16;
 178 
 179 // Stack layout for saving/restoring  all the registers needed during a runtime
 180 // call (this includes deoptimization)
 181 // Note: note that users of this frame may well have arguments to some runtime
 182 // while these values are on the stack. These positions neglect those arguments
 183 // but the code in save_live_registers will take the argument count into
 184 // account.
 185 //








 186 enum reg_save_layout {
 187   dummy1,
 188   dummy2,





 189   // Two temps to be used as needed by users of save/restore callee registers
 190   temp_2_off,
 191   temp_1_off,
 192   xmm_regs_as_doubles_off,
 193   float_regs_as_doubles_off = xmm_regs_as_doubles_off + xmm_regs_as_doubles_size_in_words,
 194   fpu_state_off = float_regs_as_doubles_off + float_regs_as_doubles_size_in_words,
 195   fpu_state_end_off = fpu_state_off + FPUStateSizeInWords,
 196   marker = fpu_state_end_off,
 197   extra_space_offset,












 198   rdi_off = extra_space_offset,
 199   rsi_off,
 200   rbp_off,
 201   rsp_off,
 202   rbx_off,
 203   rdx_off,
 204   rcx_off,
 205   rax_off,      
 206   saved_rbp_off,
 207   return_off,
 208   reg_save_frame_size,  // As noted: neglects any parameters to runtime







 209 
 210   // equates
 211 
 212   // illegal instruction handler
 213   continue_dest_off = temp_1_off,
 214 
 215   // deoptimization equates
 216   fp0_off = float_regs_as_doubles_off, // slot for java float/double return value
 217   xmm0_off = xmm_regs_as_doubles_off,  // slot for java float/double return value
 218   deopt_type = temp_2_off,             // slot for type of deopt in progress
 219   ret_type = temp_1_off                // slot for return type
 220 };
 221 
 222 
 223 
 224 // Save off registers which might be killed by calls into the runtime.
 225 // Tries to smart of about FP registers.  In particular we separate
 226 // saving and describing the FPU registers for deoptimization since we
 227 // have to save the FPU registers twice if we describe them and on P4
 228 // saving FPU registers which don't contain anything appears
 229 // expensive.  The deopt blob is the only thing which needs to
 230 // describe FPU registers.  In all other cases it should be sufficient
 231 // to simply save their current value.
 232 
 233 static OopMap* generate_oop_map(StubAssembler* sasm, int num_rt_args,
 234                                 bool save_fpu_registers = true) {
 235   int frame_size = reg_save_frame_size + num_rt_args; // args + thread
 236   sasm->set_frame_size(frame_size);




 237 
 238   // record saved value locations in an OopMap
 239   // locations are offsets from sp after runtime call; num_rt_args is number of arguments in call, including thread
 240   OopMap* map = new OopMap(frame_size, 0);
 241   map->set_callee_saved(VMRegImpl::stack2reg(rax_off + num_rt_args), rax->as_VMReg());
 242   map->set_callee_saved(VMRegImpl::stack2reg(rcx_off + num_rt_args), rcx->as_VMReg());
 243   map->set_callee_saved(VMRegImpl::stack2reg(rdx_off + num_rt_args), rdx->as_VMReg());
 244   map->set_callee_saved(VMRegImpl::stack2reg(rbx_off + num_rt_args), rbx->as_VMReg());
 245   map->set_callee_saved(VMRegImpl::stack2reg(rsi_off + num_rt_args), rsi->as_VMReg());
 246   map->set_callee_saved(VMRegImpl::stack2reg(rdi_off + num_rt_args), rdi->as_VMReg());



























 247 
 248   if (save_fpu_registers) {
 249     if (UseSSE < 2) {
 250       int fpu_off = float_regs_as_doubles_off;
 251       for (int n = 0; n < FrameMap::nof_fpu_regs; n++) {
 252         VMReg fpu_name_0 = FrameMap::fpu_regname(n);
 253         map->set_callee_saved(VMRegImpl::stack2reg(fpu_off +     num_rt_args), fpu_name_0);
 254         // %%% This is really a waste but we'll keep things as they were for now
 255         if (true) {
 256           map->set_callee_saved(VMRegImpl::stack2reg(fpu_off + 1 + num_rt_args), fpu_name_0->next());
 257         }
 258         fpu_off += 2;
 259       }
 260       assert(fpu_off == fpu_state_off, "incorrect number of fpu stack slots");
 261     }
 262 
 263     if (UseSSE >= 2) {
 264       int xmm_off = xmm_regs_as_doubles_off;
 265       for (int n = 0; n < FrameMap::nof_xmm_regs; n++) {
 266         VMReg xmm_name_0 = as_XMMRegister(n)->as_VMReg();


 274       assert(xmm_off == float_regs_as_doubles_off, "incorrect number of xmm registers");
 275 
 276     } else if (UseSSE == 1) {
 277       int xmm_off = xmm_regs_as_doubles_off;
 278       for (int n = 0; n < FrameMap::nof_xmm_regs; n++) {
 279         VMReg xmm_name_0 = as_XMMRegister(n)->as_VMReg();
 280         map->set_callee_saved(VMRegImpl::stack2reg(xmm_off +     num_rt_args), xmm_name_0);
 281         xmm_off += 2;
 282       }
 283       assert(xmm_off == float_regs_as_doubles_off, "incorrect number of xmm registers");
 284     }
 285   }
 286 
 287   return map;
 288 }
 289 
 290 static OopMap* save_live_registers(StubAssembler* sasm, int num_rt_args,
 291                                    bool save_fpu_registers = true) {
 292   __ block_comment("save_live_registers");
 293 
 294   int frame_size = reg_save_frame_size + num_rt_args; // args + thread

 295   // frame_size = round_to(frame_size, 4);
 296   sasm->set_frame_size(frame_size);
 297 
 298   __ pushad();         // integer registers
 299 
 300   // assert(float_regs_as_doubles_off % 2 == 0, "misaligned offset");
 301   // assert(xmm_regs_as_doubles_off % 2 == 0, "misaligned offset");
 302 
 303   __ subl(rsp, extra_space_offset * wordSize);
 304 
 305 #ifdef ASSERT
 306   __ movl(Address(rsp, marker * wordSize), 0xfeedbeef);
 307 #endif
 308 
 309   if (save_fpu_registers) {
 310     if (UseSSE < 2) {
 311       // save FPU stack
 312       __ fnsave(Address(rsp, fpu_state_off * wordSize));
 313       __ fwait();
 314 
 315 #ifdef ASSERT
 316       Label ok;
 317       __ cmpw(Address(rsp, fpu_state_off * wordSize), StubRoutines::fpu_cntrl_wrd_std());
 318       __ jccb(Assembler::equal, ok);
 319       __ stop("corrupted control word detected");
 320       __ bind(ok);
 321 #endif
 322 
 323       // Reset the control word to guard against exceptions being unmasked
 324       // since fstp_d can cause FPU stack underflow exceptions.  Write it
 325       // into the on stack copy and then reload that to make sure that the
 326       // current and future values are correct.
 327       __ movw(Address(rsp, fpu_state_off * wordSize), StubRoutines::fpu_cntrl_wrd_std());
 328       __ frstor(Address(rsp, fpu_state_off * wordSize));
 329 
 330       // Save the FPU registers in de-opt-able form 
 331       __ fstp_d(Address(rsp, float_regs_as_doubles_off * BytesPerWord +  0));
 332       __ fstp_d(Address(rsp, float_regs_as_doubles_off * BytesPerWord +  8));
 333       __ fstp_d(Address(rsp, float_regs_as_doubles_off * BytesPerWord + 16));
 334       __ fstp_d(Address(rsp, float_regs_as_doubles_off * BytesPerWord + 24));
 335       __ fstp_d(Address(rsp, float_regs_as_doubles_off * BytesPerWord + 32));
 336       __ fstp_d(Address(rsp, float_regs_as_doubles_off * BytesPerWord + 40));
 337       __ fstp_d(Address(rsp, float_regs_as_doubles_off * BytesPerWord + 48));
 338       __ fstp_d(Address(rsp, float_regs_as_doubles_off * BytesPerWord + 56));
 339     }
 340 
 341     if (UseSSE >= 2) {
 342       // save XMM registers
 343       // XMM registers can contain float or double values, but this is not known here,
 344       // so always save them as doubles.
 345       // note that float values are _not_ converted automatically, so for float values 
 346       // the second word contains only garbage data.
 347       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * wordSize +  0), xmm0);
 348       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * wordSize +  8), xmm1);
 349       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * wordSize + 16), xmm2);
 350       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * wordSize + 24), xmm3);
 351       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * wordSize + 32), xmm4);
 352       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * wordSize + 40), xmm5);
 353       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * wordSize + 48), xmm6);
 354       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * wordSize + 56), xmm7);










 355     } else if (UseSSE == 1) {
 356       // save XMM registers as float because double not supported without SSE2
 357       __ movflt(Address(rsp, xmm_regs_as_doubles_off * wordSize +  0), xmm0);
 358       __ movflt(Address(rsp, xmm_regs_as_doubles_off * wordSize +  8), xmm1);
 359       __ movflt(Address(rsp, xmm_regs_as_doubles_off * wordSize + 16), xmm2);
 360       __ movflt(Address(rsp, xmm_regs_as_doubles_off * wordSize + 24), xmm3);
 361       __ movflt(Address(rsp, xmm_regs_as_doubles_off * wordSize + 32), xmm4);
 362       __ movflt(Address(rsp, xmm_regs_as_doubles_off * wordSize + 40), xmm5);
 363       __ movflt(Address(rsp, xmm_regs_as_doubles_off * wordSize + 48), xmm6);
 364       __ movflt(Address(rsp, xmm_regs_as_doubles_off * wordSize + 56), xmm7);
 365     }
 366   }
 367 
 368   // FPU stack must be empty now
 369   __ verify_FPU(0, "save_live_registers"); 
 370 
 371   return generate_oop_map(sasm, num_rt_args, save_fpu_registers);
 372 }
 373 
 374 
 375 static void restore_fpu(StubAssembler* sasm, bool restore_fpu_registers = true) {
 376   if (restore_fpu_registers) {
 377     if (UseSSE >= 2) {
 378       // restore XMM registers
 379       __ movdbl(xmm0, Address(rsp, xmm_regs_as_doubles_off * wordSize +  0));
 380       __ movdbl(xmm1, Address(rsp, xmm_regs_as_doubles_off * wordSize +  8));
 381       __ movdbl(xmm2, Address(rsp, xmm_regs_as_doubles_off * wordSize + 16));
 382       __ movdbl(xmm3, Address(rsp, xmm_regs_as_doubles_off * wordSize + 24));
 383       __ movdbl(xmm4, Address(rsp, xmm_regs_as_doubles_off * wordSize + 32));
 384       __ movdbl(xmm5, Address(rsp, xmm_regs_as_doubles_off * wordSize + 40));
 385       __ movdbl(xmm6, Address(rsp, xmm_regs_as_doubles_off * wordSize + 48));
 386       __ movdbl(xmm7, Address(rsp, xmm_regs_as_doubles_off * wordSize + 56));










 387     } else if (UseSSE == 1) {
 388       // restore XMM registers
 389       __ movflt(xmm0, Address(rsp, xmm_regs_as_doubles_off * wordSize +  0));
 390       __ movflt(xmm1, Address(rsp, xmm_regs_as_doubles_off * wordSize +  8));
 391       __ movflt(xmm2, Address(rsp, xmm_regs_as_doubles_off * wordSize + 16));
 392       __ movflt(xmm3, Address(rsp, xmm_regs_as_doubles_off * wordSize + 24));
 393       __ movflt(xmm4, Address(rsp, xmm_regs_as_doubles_off * wordSize + 32));
 394       __ movflt(xmm5, Address(rsp, xmm_regs_as_doubles_off * wordSize + 40));
 395       __ movflt(xmm6, Address(rsp, xmm_regs_as_doubles_off * wordSize + 48));
 396       __ movflt(xmm7, Address(rsp, xmm_regs_as_doubles_off * wordSize + 56));
 397     }
 398 
 399     if (UseSSE < 2) {
 400       __ frstor(Address(rsp, fpu_state_off * wordSize));
 401     } else {
 402       // check that FPU stack is really empty
 403       __ verify_FPU(0, "restore_live_registers"); 
 404     }
 405 
 406   } else {
 407     // check that FPU stack is really empty
 408     __ verify_FPU(0, "restore_live_registers"); 
 409   }
 410 
 411 #ifdef ASSERT
 412   {
 413     Label ok;
 414     __ cmpl(Address(rsp, marker * wordSize), 0xfeedbeef);
 415     __ jcc(Assembler::equal, ok);
 416     __ stop("bad offsets in frame");
 417     __ bind(ok);
 418   }
 419 #endif
 420 
 421   __ addl(rsp, extra_space_offset * wordSize);
 422 }
 423 
 424 
 425 static void restore_live_registers(StubAssembler* sasm, bool restore_fpu_registers = true) {
 426   __ block_comment("restore_live_registers");
 427 
 428   restore_fpu(sasm, restore_fpu_registers);
 429   __ popad();
 430 }
 431 
 432 
 433 static void restore_live_registers_except_rax(StubAssembler* sasm, bool restore_fpu_registers = true) {
 434   __ block_comment("restore_live_registers_except_rax");
 435 
 436   restore_fpu(sasm, restore_fpu_registers);
 437 
 438   __ popl(rdi);
 439   __ popl(rsi);
 440   __ popl(rbp);
 441   __ popl(rbx); // skip this value
 442   __ popl(rbx);
 443   __ popl(rdx);
 444   __ popl(rcx);
 445   __ addl(rsp, 4);





















 446 }
 447 
 448 
 449 void Runtime1::initialize_pd() {
 450   // nothing to do
 451 }
 452 
 453 
 454 // target: the entry point of the method that creates and posts the exception oop
 455 // has_argument: true if the exception needs an argument (passed on stack because registers must be preserved)
 456 
 457 OopMapSet* Runtime1::generate_exception_throw(StubAssembler* sasm, address target, bool has_argument) {
 458   // preserve all registers
 459   int num_rt_args = has_argument ? 2 : 1;
 460   OopMap* oop_map = save_live_registers(sasm, num_rt_args);
 461 
 462   // now all registers are saved and can be used freely
 463   // verify that no old value is used accidentally
 464   __ invalidate_registers(true, true, true, true, true, true);
 465 
 466   // registers used by this stub
 467   const Register temp_reg = rbx;
 468 
 469   // load argument for exception that is passed as an argument into the stub
 470   if (has_argument) {
 471     __ movl(temp_reg, Address(rbp, 2*BytesPerWord));
 472     __ pushl(temp_reg);




 473   }
 474 
 475   int call_offset = __ call_RT(noreg, noreg, target, num_rt_args - 1);
 476 
 477   OopMapSet* oop_maps = new OopMapSet();
 478   oop_maps->add_gc_map(call_offset, oop_map);
 479 
 480   __ stop("should not reach here");
 481 
 482   return oop_maps;
 483 }
 484 
 485 
 486 void Runtime1::generate_handle_exception(StubAssembler *sasm, OopMapSet* oop_maps, OopMap* oop_map, bool save_fpu_registers) {
 487   // incoming parameters
 488   const Register exception_oop = rax;
 489   const Register exception_pc = rdx;
 490   // other registers used in this stub
 491   const Register real_return_addr = rbx;
 492   const Register thread = rdi;
 493 
 494   __ block_comment("generate_handle_exception");
 495 
 496 #ifdef TIERED
 497   // C2 can leave the fpu stack dirty
 498   if (UseSSE < 2 ) {
 499     __ empty_FPU_stack();
 500   }
 501 #endif // TIERED
 502 
 503   // verify that only rax, and rdx is valid at this time
 504   __ invalidate_registers(false, true, true, false, true, true);
 505   // verify that rax, contains a valid exception
 506   __ verify_not_null_oop(exception_oop);
 507 
 508   // load address of JavaThread object for thread-local data
 509   __ get_thread(thread);
 510 
 511 #ifdef ASSERT
 512   // check that fields in JavaThread for exception oop and issuing pc are 
 513   // empty before writing to them
 514   Label oop_empty;
 515   __ cmpl(Address(thread, JavaThread::exception_oop_offset()), 0);
 516   __ jcc(Assembler::equal, oop_empty);
 517   __ stop("exception oop already set");
 518   __ bind(oop_empty);
 519 
 520   Label pc_empty;
 521   __ cmpl(Address(thread, JavaThread::exception_pc_offset()), 0);
 522   __ jcc(Assembler::equal, pc_empty);
 523   __ stop("exception pc already set");
 524   __ bind(pc_empty);
 525 #endif
 526 
 527   // save exception oop and issuing pc into JavaThread
 528   // (exception handler will load it from here)
 529   __ movl(Address(thread, JavaThread::exception_oop_offset()), exception_oop);
 530   __ movl(Address(thread, JavaThread::exception_pc_offset()), exception_pc);
 531 
 532   // save real return address (pc that called this stub)
 533   __ movl(real_return_addr, Address(rbp, 1*BytesPerWord));   
 534   __ movl(Address(rsp, temp_1_off * BytesPerWord), real_return_addr);
 535 
 536   // patch throwing pc into return address (has bci & oop map)
 537   __ movl(Address(rbp, 1*BytesPerWord), exception_pc);       
 538 
 539   // compute the exception handler. 
 540   // the exception oop and the throwing pc are read from the fields in JavaThread
 541   int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, exception_handler_for_pc));
 542   oop_maps->add_gc_map(call_offset, oop_map);
 543 
 544   // rax,: handler address or NULL if no handler exists
 545   //      will be the deopt blob if nmethod was deoptimized while we looked up
 546   //      handler regardless of whether handler existed in the nmethod.
 547 
 548   // only rax, is valid at this time, all other registers have been destroyed by the runtime call
 549   __ invalidate_registers(false, true, true, true, true, true);
 550 
 551   // Do we have an exception handler in the nmethod?
 552   Label no_handler;
 553   Label done;
 554   __ testl(rax, rax);
 555   __ jcc(Assembler::zero, no_handler);
 556 
 557   // exception handler found
 558   // patch the return address -> the stub will directly return to the exception handler
 559   __ movl(Address(rbp, 1*BytesPerWord), rax); 
 560 
 561   // restore registers
 562   restore_live_registers(sasm, save_fpu_registers);
 563 
 564   // return to exception handler
 565   __ leave();
 566   __ ret(0);
 567 
 568   __ bind(no_handler);
 569   // no exception handler found in this method, so the exception is 
 570   // forwarded to the caller (using the unwind code of the nmethod)
 571   // there is no need to restore the registers
 572 
 573   // restore the real return address that was saved before the RT-call
 574   __ movl(real_return_addr, Address(rsp, temp_1_off * BytesPerWord));
 575   __ movl(Address(rbp, 1*BytesPerWord), real_return_addr);
 576 
 577   // load address of JavaThread object for thread-local data
 578   __ get_thread(thread);
 579   // restore exception oop into rax, (convention for unwind code)
 580   __ movl(exception_oop, Address(thread, JavaThread::exception_oop_offset()));
 581 
 582   // clear exception fields in JavaThread because they are no longer needed
 583   // (fields must be cleared because they are processed by GC otherwise)
 584   __ movl(Address(thread, JavaThread::exception_oop_offset()), NULL_WORD);
 585   __ movl(Address(thread, JavaThread::exception_pc_offset()), NULL_WORD);
 586 
 587   // pop the stub frame off
 588   __ leave();
 589 
 590   generate_unwind_exception(sasm);
 591   __ stop("should not reach here");
 592 }
 593 
 594 
 595 void Runtime1::generate_unwind_exception(StubAssembler *sasm) {
 596   // incoming parameters
 597   const Register exception_oop = rax;
 598   // other registers used in this stub
 599   const Register exception_pc = rdx;
 600   const Register handler_addr = rbx;
 601   const Register thread = rdi;
 602 
 603   // verify that only rax, is valid at this time
 604   __ invalidate_registers(false, true, true, true, true, true);
 605 
 606 #ifdef ASSERT
 607   // check that fields in JavaThread for exception oop and issuing pc are empty
 608   __ get_thread(thread);
 609   Label oop_empty;
 610   __ cmpl(Address(thread, JavaThread::exception_oop_offset()), 0);
 611   __ jcc(Assembler::equal, oop_empty);
 612   __ stop("exception oop must be empty");
 613   __ bind(oop_empty);
 614 
 615   Label pc_empty;
 616   __ cmpl(Address(thread, JavaThread::exception_pc_offset()), 0);
 617   __ jcc(Assembler::equal, pc_empty);
 618   __ stop("exception pc must be empty");
 619   __ bind(pc_empty);
 620 #endif
 621 
 622   // clear the FPU stack in case any FPU results are left behind
 623   __ empty_FPU_stack();
 624 
 625   // leave activation of nmethod
 626   __ leave();                  
 627   // store return address (is on top of stack after leave)
 628   __ movl(exception_pc, Address(rsp, 0));  
 629 
 630   __ verify_oop(exception_oop);
 631 
 632   // save exception oop from rax, to stack before call
 633   __ pushl(exception_oop);
 634 
 635   // search the exception handler address of the caller (using the return address)
 636   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), exception_pc);
 637   // rax,: exception handler address of the caller
 638 
 639   // only rax, is valid at this time, all other registers have been destroyed by the call
 640   __ invalidate_registers(false, true, true, true, true, true);
 641   
 642   // move result of call into correct register
 643   __ movl(handler_addr, rax);
 644 
 645   // restore exception oop in rax, (required convention of exception handler)
 646   __ popl(exception_oop);
 647 
 648   __ verify_oop(exception_oop);
 649 
 650   // get throwing pc (= return address). 
 651   // rdx has been destroyed by the call, so it must be set again
 652   // the pop is also necessary to simulate the effect of a ret(0) 
 653   __ popl(exception_pc);
 654 
 655   // verify that that there is really a valid exception in rax,
 656   __ verify_not_null_oop(exception_oop);
 657 
 658   // continue at exception handler (return address removed)
 659   // note: do *not* remove arguments when unwinding the
 660   //       activation since the caller assumes having
 661   //       all arguments on the stack when entering the
 662   //       runtime to determine the exception handler
 663   //       (GC happens at call site with arguments!)
 664   // rax,: exception oop
 665   // rdx: throwing pc
 666   // rbx,: exception handler
 667   __ jmp(handler_addr);
 668 }
 669 
 670 
 671 OopMapSet* Runtime1::generate_patching(StubAssembler* sasm, address target) {
 672   // use the maximum number of runtime-arguments here because it is difficult to 
 673   // distinguish each RT-Call.
 674   // Note: This number affects also the RT-Call in generate_handle_exception because
 675   //       the oop-map is shared for all calls.
 676   const int num_rt_args = 2;  // thread + dummy
 677 
 678   DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
 679   assert(deopt_blob != NULL, "deoptimization blob must have been created");
 680 
 681   OopMap* oop_map = save_live_registers(sasm, num_rt_args);
 682 
 683   __ pushl(rax); // push dummy





 684 
 685   const Register thread = rdi; // is callee-saved register (Visual C++ calling conventions)
 686   // push java thread (becomes first argument of C function)
 687   __ get_thread(thread);
 688   __ pushl(thread);

 689   __ set_last_Java_frame(thread, noreg, rbp, NULL);
 690   // do the call
 691   __ call(RuntimeAddress(target));
 692   OopMapSet* oop_maps = new OopMapSet();
 693   oop_maps->add_gc_map(__ offset(), oop_map);
 694   // verify callee-saved register
 695 #ifdef ASSERT
 696   guarantee(thread != rax, "change this code");
 697   __ pushl(rax);
 698   { Label L;
 699     __ get_thread(rax);
 700     __ cmpl(thread, rax);
 701     __ jcc(Assembler::equal, L);
 702     __ stop("StubAssembler::call_RT: rdi not callee saved?");
 703     __ bind(L);
 704   }
 705   __ popl(rax);
 706 #endif
 707   __ reset_last_Java_frame(thread, true, false);
 708   __ popl(rcx); // discard thread arg
 709   __ popl(rcx); // discard dummy


 710 
 711   // check for pending exceptions
 712   { Label L;
 713     __ cmpl(Address(thread, Thread::pending_exception_offset()), NULL_WORD);
 714     __ jcc(Assembler::equal, L);
 715     // exception pending => remove activation and forward to exception handler
 716 
 717     __ testl(rax, rax);                                   // have we deoptimized?
 718     __ jump_cc(Assembler::equal,
 719                RuntimeAddress(Runtime1::entry_for(Runtime1::forward_exception_id)));
 720 
 721     // the deopt blob expects exceptions in the special fields of
 722     // JavaThread, so copy and clear pending exception.
 723 
 724     // load and clear pending exception
 725     __ movl(rax, Address(thread, Thread::pending_exception_offset()));
 726     __ movl(Address(thread, Thread::pending_exception_offset()), NULL_WORD);
 727 
 728     // check that there is really a valid exception 
 729     __ verify_not_null_oop(rax);
 730 
 731     // load throwing pc: this is the return address of the stub
 732     __ movl(rdx, Address(rsp, return_off * BytesPerWord));
 733 
 734 #ifdef ASSERT
 735     // check that fields in JavaThread for exception oop and issuing pc are empty
 736     Label oop_empty;
 737     __ cmpoop(Address(thread, JavaThread::exception_oop_offset()), 0);
 738     __ jcc(Assembler::equal, oop_empty);
 739     __ stop("exception oop must be empty");
 740     __ bind(oop_empty);
 741 
 742     Label pc_empty;
 743     __ cmpl(Address(thread, JavaThread::exception_pc_offset()), 0);
 744     __ jcc(Assembler::equal, pc_empty);
 745     __ stop("exception pc must be empty");
 746     __ bind(pc_empty);
 747 #endif
 748 
 749     // store exception oop and throwing pc to JavaThread
 750     __ movl(Address(thread, JavaThread::exception_oop_offset()), rax);
 751     __ movl(Address(thread, JavaThread::exception_pc_offset()), rdx);
 752 
 753     restore_live_registers(sasm);
 754 
 755     __ leave();
 756     __ addl(rsp, 4);  // remove return address from stack
 757 
 758     // Forward the exception directly to deopt blob. We can blow no
 759     // registers and must leave throwing pc on the stack.  A patch may
 760     // have values live in registers so the entry point with the
 761     // exception in tls.
 762     __ jump(RuntimeAddress(deopt_blob->unpack_with_exception_in_tls()));
 763 
 764     __ bind(L);
 765   }
 766 
 767 
 768   // Runtime will return true if the nmethod has been deoptimized during
 769   // the patching process. In that case we must do a deopt reexecute instead.
 770 
 771   Label reexecuteEntry, cont;
 772 
 773   __ testl(rax, rax);                                   // have we deoptimized?
 774   __ jcc(Assembler::equal, cont);                       // no
 775 
 776   // Will reexecute. Proper return address is already on the stack we just restore
 777   // registers, pop all of our frame but the return address and jump to the deopt blob
 778   restore_live_registers(sasm);
 779   __ leave();
 780   __ jump(RuntimeAddress(deopt_blob->unpack_with_reexecution()));
 781 
 782   __ bind(cont);
 783   restore_live_registers(sasm);
 784   __ leave();
 785   __ ret(0);
 786 
 787   return oop_maps;
 788 
 789 }
 790 
 791 
 792 OopMapSet* Runtime1::generate_code_for(StubID id, StubAssembler* sasm) {
 793 
 794   // for better readability
 795   const bool must_gc_arguments = true;
 796   const bool dont_gc_arguments = false;
 797 
 798   // default value; overwritten for some optimized stubs that are called from methods that do not use the fpu
 799   bool save_fpu_registers = true;
 800 
 801   // stub code & info for the different stubs
 802   OopMapSet* oop_maps = NULL;
 803   switch (id) {
 804     case forward_exception_id:
 805       {
 806         // we're handling an exception in the context of a compiled
 807         // frame.  The registers have been saved in the standard
 808         // places.  Perform an exception lookup in the caller and
 809         // dispatch to the handler if found.  Otherwise unwind and
 810         // dispatch to the callers exception handler.
 811 
 812         const Register thread = rdi;
 813         const Register exception_oop = rax;
 814         const Register exception_pc = rdx;
 815 
 816         // load pending exception oop into rax,
 817         __ movl(exception_oop, Address(thread, Thread::pending_exception_offset()));
 818         // clear pending exception
 819         __ movl(Address(thread, Thread::pending_exception_offset()), NULL_WORD);
 820 
 821         // load issuing PC (the return address for this stub) into rdx
 822         __ movl(exception_pc, Address(rbp, 1*BytesPerWord));
 823 
 824         // make sure that the vm_results are cleared (may be unnecessary)
 825         __ movl(Address(thread, JavaThread::vm_result_offset()), NULL_WORD);
 826         __ movl(Address(thread, JavaThread::vm_result_2_offset()), NULL_WORD);
 827 
 828         // verify that that there is really a valid exception in rax,
 829         __ verify_not_null_oop(exception_oop);
 830 
 831 
 832         oop_maps = new OopMapSet();
 833         OopMap* oop_map = generate_oop_map(sasm, 1);
 834         generate_handle_exception(sasm, oop_maps, oop_map);
 835         __ stop("should not reach here");
 836       }
 837       break;
 838 
 839     case new_instance_id:
 840     case fast_new_instance_id:
 841     case fast_new_instance_init_check_id:
 842       {
 843         Register klass = rdx; // Incoming
 844         Register obj   = rax; // Result
 845 
 846         if (id == new_instance_id) {
 847           __ set_info("new_instance", dont_gc_arguments);
 848         } else if (id == fast_new_instance_id) {
 849           __ set_info("fast new_instance", dont_gc_arguments);
 850         } else {
 851           assert(id == fast_new_instance_init_check_id, "bad StubID");
 852           __ set_info("fast new_instance init check", dont_gc_arguments);
 853         }
 854         
 855         if ((id == fast_new_instance_id || id == fast_new_instance_init_check_id) &&
 856             UseTLAB && FastTLABRefill) {
 857           Label slow_path;
 858           Register obj_size = rcx;
 859           Register t1       = rbx;
 860           Register t2       = rsi;
 861           assert_different_registers(klass, obj, obj_size, t1, t2);
 862         
 863           __ pushl(rdi);
 864           __ pushl(rbx);
 865 
 866           if (id == fast_new_instance_init_check_id) {
 867             // make sure the klass is initialized
 868             __ cmpl(Address(klass, instanceKlass::init_state_offset_in_bytes() + sizeof(oopDesc)), instanceKlass::fully_initialized);
 869             __ jcc(Assembler::notEqual, slow_path);
 870           }
 871 
 872 #ifdef ASSERT
 873           // assert object can be fast path allocated
 874           {
 875             Label ok, not_ok;
 876             __ movl(obj_size, Address(klass, Klass::layout_helper_offset_in_bytes() + sizeof(oopDesc)));
 877             __ cmpl(obj_size, 0);  // make sure it's an instance (LH > 0)
 878             __ jcc(Assembler::lessEqual, not_ok);
 879             __ testl(obj_size, Klass::_lh_instance_slow_path_bit);
 880             __ jcc(Assembler::zero, ok);
 881             __ bind(not_ok);
 882             __ stop("assert(can be fast path allocated)");
 883             __ should_not_reach_here();
 884             __ bind(ok);
 885           }
 886 #endif // ASSERT
 887 
 888           // if we got here then the TLAB allocation failed, so try
 889           // refilling the TLAB or allocating directly from eden.
 890           Label retry_tlab, try_eden;
 891           __ tlab_refill(retry_tlab, try_eden, slow_path); // does not destroy rdx (klass)
 892           
 893           __ bind(retry_tlab);
 894 
 895           // get the instance size
 896           __ movl(obj_size, Address(klass, klassOopDesc::header_size() * HeapWordSize + Klass::layout_helper_offset_in_bytes()));
 897           __ tlab_allocate(obj, obj_size, 0, t1, t2, slow_path);
 898           __ initialize_object(obj, klass, obj_size, 0, t1, t2);
 899           __ verify_oop(obj);
 900           __ popl(rbx);
 901           __ popl(rdi);
 902           __ ret(0);
 903 
 904           __ bind(try_eden);
 905           // get the instance size
 906           __ movl(obj_size, Address(klass, klassOopDesc::header_size() * HeapWordSize + Klass::layout_helper_offset_in_bytes()));
 907           __ eden_allocate(obj, obj_size, 0, t1, slow_path);
 908           __ initialize_object(obj, klass, obj_size, 0, t1, t2);
 909           __ verify_oop(obj);
 910           __ popl(rbx);
 911           __ popl(rdi);
 912           __ ret(0);
 913 
 914           __ bind(slow_path);
 915           __ popl(rbx);
 916           __ popl(rdi);
 917         }
 918         
 919         __ enter();
 920         OopMap* map = save_live_registers(sasm, 2);
 921         int call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_instance), klass);
 922         oop_maps = new OopMapSet();
 923         oop_maps->add_gc_map(call_offset, map);
 924         restore_live_registers_except_rax(sasm);
 925         __ verify_oop(obj);
 926         __ leave();
 927         __ ret(0);
 928 
 929         // rax,: new instance
 930       }
 931 
 932       break;
 933 
 934 #ifdef TIERED
 935     case counter_overflow_id:
 936       {


 982 
 983         if (UseTLAB && FastTLABRefill) {
 984           Register arr_size = rsi;
 985           Register t1       = rcx;  // must be rcx for use as shift count
 986           Register t2       = rdi;
 987           Label slow_path;
 988           assert_different_registers(length, klass, obj, arr_size, t1, t2);
 989 
 990           // check that array length is small enough for fast path.
 991           __ cmpl(length, C1_MacroAssembler::max_array_allocation_length);
 992           __ jcc(Assembler::above, slow_path);
 993 
 994           // if we got here then the TLAB allocation failed, so try
 995           // refilling the TLAB or allocating directly from eden.
 996           Label retry_tlab, try_eden;
 997           __ tlab_refill(retry_tlab, try_eden, slow_path); // preserves rbx, & rdx
 998           
 999           __ bind(retry_tlab);
1000 
1001           // get the allocation size: round_up(hdr + length << (layout_helper & 0x1F))

1002           __ movl(t1, Address(klass, klassOopDesc::header_size() * HeapWordSize + Klass::layout_helper_offset_in_bytes()));

1003           __ movl(arr_size, length);
1004           assert(t1 == rcx, "fixed register usage");
1005           __ shll(arr_size /* by t1=rcx, mod 32 */);
1006           __ shrl(t1, Klass::_lh_header_size_shift);
1007           __ andl(t1, Klass::_lh_header_size_mask);
1008           __ addl(arr_size, t1);
1009           __ addl(arr_size, MinObjAlignmentInBytesMask); // align up
1010           __ andl(arr_size, ~MinObjAlignmentInBytesMask);
1011 
1012           __ tlab_allocate(obj, arr_size, 0, t1, t2, slow_path);  // preserves arr_size
1013 
1014           __ initialize_header(obj, klass, length, t1, t2);
1015           __ movb(t1, Address(klass, klassOopDesc::header_size() * HeapWordSize + Klass::layout_helper_offset_in_bytes() + (Klass::_lh_header_size_shift / BitsPerByte)));
1016           assert(Klass::_lh_header_size_shift % BitsPerByte == 0, "bytewise");
1017           assert(Klass::_lh_header_size_mask <= 0xFF, "bytewise");
1018           __ andl(t1, Klass::_lh_header_size_mask);
1019           __ subl(arr_size, t1);  // body length
1020           __ addl(t1, obj);       // body start
1021           __ initialize_body(t1, arr_size, 0, t2);
1022           __ verify_oop(obj);
1023           __ ret(0);
1024 
1025           __ bind(try_eden);
1026           // get the allocation size: round_up(hdr + length << (layout_helper & 0x1F))

1027           __ movl(t1, Address(klass, klassOopDesc::header_size() * HeapWordSize + Klass::layout_helper_offset_in_bytes()));

1028           __ movl(arr_size, length);
1029           assert(t1 == rcx, "fixed register usage");
1030           __ shll(arr_size /* by t1=rcx, mod 32 */);
1031           __ shrl(t1, Klass::_lh_header_size_shift);
1032           __ andl(t1, Klass::_lh_header_size_mask);
1033           __ addl(arr_size, t1);
1034           __ addl(arr_size, MinObjAlignmentInBytesMask); // align up
1035           __ andl(arr_size, ~MinObjAlignmentInBytesMask);
1036 
1037           __ eden_allocate(obj, arr_size, 0, t1, slow_path);  // preserves arr_size
1038 
1039           __ initialize_header(obj, klass, length, t1, t2);
1040           __ movb(t1, Address(klass, klassOopDesc::header_size() * HeapWordSize + Klass::layout_helper_offset_in_bytes() + (Klass::_lh_header_size_shift / BitsPerByte)));
1041           assert(Klass::_lh_header_size_shift % BitsPerByte == 0, "bytewise");
1042           assert(Klass::_lh_header_size_mask <= 0xFF, "bytewise");
1043           __ andl(t1, Klass::_lh_header_size_mask);
1044           __ subl(arr_size, t1);  // body length
1045           __ addl(t1, obj);       // body start
1046           __ initialize_body(t1, arr_size, 0, t2);
1047           __ verify_oop(obj);
1048           __ ret(0);
1049 
1050           __ bind(slow_path);
1051         }
1052 
1053         __ enter();
1054         OopMap* map = save_live_registers(sasm, 3);
1055         int call_offset;
1056         if (id == new_type_array_id) {
1057           call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_type_array), klass, length);
1058         } else {
1059           call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_object_array), klass, length);
1060         }
1061 
1062         oop_maps = new OopMapSet();
1063         oop_maps->add_gc_map(call_offset, map);
1064         restore_live_registers_except_rax(sasm);
1065 


1075       { StubFrame f(sasm, "new_multi_array", dont_gc_arguments);
1076         // rax,: klass
1077         // rbx,: rank
1078         // rcx: address of 1st dimension
1079         OopMap* map = save_live_registers(sasm, 4);
1080         int call_offset = __ call_RT(rax, noreg, CAST_FROM_FN_PTR(address, new_multi_array), rax, rbx, rcx);
1081 
1082         oop_maps = new OopMapSet();
1083         oop_maps->add_gc_map(call_offset, map);
1084         restore_live_registers_except_rax(sasm);
1085 
1086         // rax,: new multi array
1087         __ verify_oop(rax);
1088       }
1089       break;
1090 
1091     case register_finalizer_id:
1092       {
1093         __ set_info("register_finalizer", dont_gc_arguments);
1094         







1095         // The object is passed on the stack and we haven't pushed a
1096         // frame yet so it's one work away from top of stack.
1097         __ movl(rax, Address(rsp, 1 * BytesPerWord));
1098         __ verify_oop(rax);

1099 
1100         // load the klass and check the has finalizer flag
1101         Label register_finalizer;
1102         Register t = rsi;
1103         __ movl(t, Address(rax, oopDesc::klass_offset_in_bytes()));
1104         __ movl(t, Address(t, Klass::access_flags_offset_in_bytes() + sizeof(oopDesc)));
1105         __ testl(t, JVM_ACC_HAS_FINALIZER);
1106         __ jcc(Assembler::notZero, register_finalizer);
1107         __ ret(0);
1108 
1109         __ bind(register_finalizer);
1110         __ enter();
1111         OopMap* oop_map = save_live_registers(sasm, 2 /*num_rt_args */);
1112         int call_offset = __ call_RT(noreg, noreg,
1113                                      CAST_FROM_FN_PTR(address, SharedRuntime::register_finalizer), rax);
1114         oop_maps = new OopMapSet();
1115         oop_maps->add_gc_map(call_offset, oop_map);
1116 
1117         // Now restore all the live registers
1118         restore_live_registers(sasm);
1119 
1120         __ leave();
1121         __ ret(0);
1122       }
1123       break;


1171         //     + 1: return address
1172         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_array_store_exception), false);
1173       }
1174       break;
1175 
1176     case throw_class_cast_exception_id:
1177       { StubFrame f(sasm, "throw_class_cast_exception", dont_gc_arguments);
1178         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_class_cast_exception), true);
1179       }
1180       break;
1181 
1182     case throw_incompatible_class_change_error_id:
1183       { StubFrame f(sasm, "throw_incompatible_class_cast_exception", dont_gc_arguments);
1184         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_incompatible_class_change_error), false);
1185       }
1186       break;
1187 
1188     case slow_subtype_check_id:
1189       {
1190         enum layout {
1191           rax_off,
1192           rcx_off,
1193           rsi_off,
1194           rdi_off,
1195           saved_rbp_off,
1196           return_off,
1197           sub_off,
1198           super_off,
1199           framesize
1200         };
1201         
1202         __ set_info("slow_subtype_check", dont_gc_arguments);
1203         __ pushl(rdi);
1204         __ pushl(rsi);
1205         __ pushl(rcx);
1206         __ pushl(rax);
1207         __ movl(rsi, Address(rsp, (super_off - 1) * BytesPerWord)); // super
1208         __ movl(rax, Address(rsp, (sub_off   - 1) * BytesPerWord)); // sub
1209 
1210         __ movl(rdi,Address(rsi,sizeof(oopDesc) + Klass::secondary_supers_offset_in_bytes()));
1211         __ movl(rcx,Address(rdi,arrayOopDesc::length_offset_in_bytes()));
1212         __ addl(rdi,arrayOopDesc::base_offset_in_bytes(T_OBJECT));



1213 
1214         Label miss;
1215         __ repne_scan();
1216         __ jcc(Assembler::notEqual, miss);
1217         __ movl(Address(rsi,sizeof(oopDesc) + Klass::secondary_super_cache_offset_in_bytes()), rax);
1218         __ movl(Address(rsp, (super_off   - 1) * BytesPerWord), 1); // result
1219         __ popl(rax);
1220         __ popl(rcx);
1221         __ popl(rsi);
1222         __ popl(rdi);
1223         __ ret(0);
1224 
1225         __ bind(miss);
1226         __ movl(Address(rsp, (super_off   - 1) * BytesPerWord), 0); // result
1227         __ popl(rax);
1228         __ popl(rcx);
1229         __ popl(rsi);
1230         __ popl(rdi);
1231         __ ret(0);
1232       }
1233       break;
1234 
1235     case monitorenter_nofpu_id:
1236       save_fpu_registers = false;
1237       // fall through
1238     case monitorenter_id:
1239       {
1240         StubFrame f(sasm, "monitorenter", dont_gc_arguments);
1241         OopMap* map = save_live_registers(sasm, 3, save_fpu_registers);
1242 


1243         f.load_argument(1, rax); // rax,: object
1244         f.load_argument(0, rbx); // rbx,: lock address
1245 
1246         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorenter), rax, rbx);
1247         
1248         oop_maps = new OopMapSet();
1249         oop_maps->add_gc_map(call_offset, map);
1250         restore_live_registers(sasm, save_fpu_registers);
1251       }
1252       break;
1253 
1254     case monitorexit_nofpu_id:
1255       save_fpu_registers = false;
1256       // fall through
1257     case monitorexit_id:
1258       { 
1259         StubFrame f(sasm, "monitorexit", dont_gc_arguments);
1260         OopMap* map = save_live_registers(sasm, 2, save_fpu_registers);
1261 


1262         f.load_argument(0, rax); // rax,: lock address
1263         
1264         // note: really a leaf routine but must setup last java sp
1265         //       => use call_RT for now (speed can be improved by
1266         //       doing last java sp setup manually)
1267         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorexit), rax);
1268 
1269         oop_maps = new OopMapSet();
1270         oop_maps->add_gc_map(call_offset, map);
1271         restore_live_registers(sasm, save_fpu_registers);
1272 
1273       }
1274       break;
1275 
1276     case access_field_patching_id:
1277       { StubFrame f(sasm, "access_field_patching", dont_gc_arguments);
1278         // we should set up register map
1279         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, access_field_patching));
1280       }
1281       break;


1290     case jvmti_exception_throw_id:
1291       { // rax,: exception oop
1292         StubFrame f(sasm, "jvmti_exception_throw", dont_gc_arguments);
1293         // Preserve all registers across this potentially blocking call
1294         const int num_rt_args = 2;  // thread, exception oop
1295         OopMap* map = save_live_registers(sasm, num_rt_args);
1296         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, Runtime1::post_jvmti_exception_throw), rax);
1297         oop_maps = new OopMapSet();
1298         oop_maps->add_gc_map(call_offset, map);
1299         restore_live_registers(sasm);
1300       }
1301       break;
1302 
1303     case dtrace_object_alloc_id:
1304       { // rax,: object
1305         StubFrame f(sasm, "dtrace_object_alloc", dont_gc_arguments);
1306         // we can't gc here so skip the oopmap but make sure that all
1307         // the live registers get saved.
1308         save_live_registers(sasm, 1);
1309 
1310         __ pushl(rax);
1311         __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc)));
1312         __ popl(rax);
1313 
1314         restore_live_registers(sasm);
1315       }
1316       break;
1317 
1318     case fpu2long_stub_id:
1319       {
1320         // rax, and rdx are destroyed, but should be free since the result is returned there
1321         // preserve rsi,ecx
1322         __ pushl(rsi);
1323         __ pushl(rcx);

1324         
1325         // check for NaN
1326         Label return0, do_return, return_min_jlong, do_convert;
1327         
1328         Address value_high_word(rsp, 8);
1329         Address value_low_word(rsp, 4);
1330         Address result_high_word(rsp, 16);
1331         Address result_low_word(rsp, 12);
1332         
1333         __ subl(rsp, 20);
1334         __ fst_d(value_low_word);
1335         __ movl(rax, value_high_word);
1336         __ andl(rax, 0x7ff00000);
1337         __ cmpl(rax, 0x7ff00000);
1338         __ jcc(Assembler::notEqual, do_convert);
1339         __ movl(rax, value_high_word);
1340         __ andl(rax, 0xfffff);
1341         __ orl(rax, value_low_word);
1342         __ jcc(Assembler::notZero, return0);
1343         
1344         __ bind(do_convert);
1345         __ fnstcw(Address(rsp, 0));
1346         __ movzxw(rax, Address(rsp, 0));
1347         __ orl(rax, 0xc00);
1348         __ movw(Address(rsp, 2), rax);
1349         __ fldcw(Address(rsp, 2));
1350         __ fwait();
1351         __ fistp_d(result_low_word);
1352         __ fldcw(Address(rsp, 0));
1353         __ fwait();
1354         __ movl(rax, result_low_word);


1355         __ movl(rdx, result_high_word);
1356         __ movl(rcx, rax);
1357         // What the heck is the point of the next instruction???
1358         __ xorl(rcx, 0x0);
1359         __ movl(rsi, 0x80000000);
1360         __ xorl(rsi, rdx);
1361         __ orl(rcx, rsi);
1362         __ jcc(Assembler::notEqual, do_return);
1363         __ fldz();
1364         __ fcomp_d(value_low_word);
1365         __ fnstsw_ax();




1366         __ sahf();
1367         __ jcc(Assembler::above, return_min_jlong);

1368         // return max_jlong

1369         __ movl(rdx, 0x7fffffff);
1370         __ movl(rax, 0xffffffff);



1371         __ jmp(do_return);
1372         
1373         __ bind(return_min_jlong);

1374         __ movl(rdx, 0x80000000);
1375         __ xorl(rax, rax);



1376         __ jmp(do_return);
1377         
1378         __ bind(return0);
1379         __ fpop();
1380         __ xorl(rdx,rdx);
1381         __ xorl(rax,rax);




1382         
1383         __ bind(do_return);
1384         __ addl(rsp, 20);
1385         __ popl(rcx);
1386         __ popl(rsi);

1387         __ ret(0);
1388       }
1389       break;
1390       
































































































































































1391     default:
1392       { StubFrame f(sasm, "unimplemented entry", dont_gc_arguments);
1393         __ movl(rax, (int)id);
1394         __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, unimplemented_entry), rax);
1395         __ should_not_reach_here();
1396       }
1397       break;
1398   }
1399   return oop_maps;
1400 }
1401 
1402 #undef __
1403 



   1 /*
   2  * Copyright 1999-2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 #include "incls/_precompiled.incl"
  26 #include "incls/_c1_Runtime1_x86.cpp.incl"
  27 
  28 
  29 // Implementation of StubAssembler
  30 
  31 int StubAssembler::call_RT(Register oop_result1, Register oop_result2, address entry, int args_size) {
  32   // setup registers
  33   const Register thread = NOT_LP64(rdi) LP64_ONLY(r15_thread); // is callee-saved register (Visual C++ calling conventions)
  34   assert(!(oop_result1->is_valid() || oop_result2->is_valid()) || oop_result1 != oop_result2, "registers must be different");
  35   assert(oop_result1 != thread && oop_result2 != thread, "registers must be different");
  36   assert(args_size >= 0, "illegal args_size");
  37 
  38 #ifdef _LP64
  39   mov(c_rarg0, thread);
  40   set_num_rt_args(0); // Nothing on stack
  41 #else
  42   set_num_rt_args(1 + args_size);
  43 
  44   // push java thread (becomes first argument of C function)
  45   get_thread(thread);
  46   push(thread);
  47 #endif // _LP64
  48 
  49   set_last_Java_frame(thread, noreg, rbp, NULL);
  50 
  51   // do the call
  52   call(RuntimeAddress(entry));
  53   int call_offset = offset();
  54   // verify callee-saved register
  55 #ifdef ASSERT
  56   guarantee(thread != rax, "change this code");
  57   push(rax);
  58   { Label L;
  59     get_thread(rax);
  60     cmpptr(thread, rax);
  61     jcc(Assembler::equal, L);
  62     int3();
  63     stop("StubAssembler::call_RT: rdi not callee saved?");
  64     bind(L);
  65   }
  66   pop(rax);
  67 #endif
  68   reset_last_Java_frame(thread, true, false);
  69 
  70   // discard thread and arguments
  71   NOT_LP64(addptr(rsp, num_rt_args()*BytesPerWord));
  72 
  73   // check for pending exceptions
  74   { Label L;
  75     cmpptr(Address(thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
  76     jcc(Assembler::equal, L);
  77     // exception pending => remove activation and forward to exception handler
  78     movptr(rax, Address(thread, Thread::pending_exception_offset()));
  79     // make sure that the vm_results are cleared
  80     if (oop_result1->is_valid()) {
  81       movptr(Address(thread, JavaThread::vm_result_offset()), (int32_t)NULL_WORD);
  82     }
  83     if (oop_result2->is_valid()) {
  84       movptr(Address(thread, JavaThread::vm_result_2_offset()), (int32_t)NULL_WORD);
  85     }
  86     if (frame_size() == no_frame_size) {
  87       leave();
  88       jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
  89     } else if (_stub_id == Runtime1::forward_exception_id) {
  90       should_not_reach_here();
  91     } else {
  92       jump(RuntimeAddress(Runtime1::entry_for(Runtime1::forward_exception_id)));
  93     }
  94     bind(L);
  95   }
  96   // get oop results if there are any and reset the values in the thread
  97   if (oop_result1->is_valid()) {
  98     movptr(oop_result1, Address(thread, JavaThread::vm_result_offset()));
  99     movptr(Address(thread, JavaThread::vm_result_offset()), (int32_t)NULL_WORD);
 100     verify_oop(oop_result1);
 101   }
 102   if (oop_result2->is_valid()) {
 103     movptr(oop_result2, Address(thread, JavaThread::vm_result_2_offset()));
 104     movptr(Address(thread, JavaThread::vm_result_2_offset()), (int32_t)NULL_WORD);
 105     verify_oop(oop_result2);
 106   }
 107   return call_offset;
 108 }
 109 
 110 
 111 int StubAssembler::call_RT(Register oop_result1, Register oop_result2, address entry, Register arg1) {
 112 #ifdef _LP64
 113   mov(c_rarg1, arg1);
 114 #else
 115   push(arg1);
 116 #endif // _LP64
 117   return call_RT(oop_result1, oop_result2, entry, 1);
 118 }
 119 
 120 
 121 int StubAssembler::call_RT(Register oop_result1, Register oop_result2, address entry, Register arg1, Register arg2) {
 122 #ifdef _LP64
 123   if (c_rarg1 == arg2) {
 124     if (c_rarg2 == arg1) {
 125       xchgq(arg1, arg2);
 126     } else {
 127       mov(c_rarg2, arg2);
 128       mov(c_rarg1, arg1);
 129     }
 130   } else {
 131     mov(c_rarg1, arg1);
 132     mov(c_rarg2, arg2);
 133   }
 134 #else
 135   push(arg2);
 136   push(arg1);
 137 #endif // _LP64
 138   return call_RT(oop_result1, oop_result2, entry, 2);
 139 }
 140 
 141 
 142 int StubAssembler::call_RT(Register oop_result1, Register oop_result2, address entry, Register arg1, Register arg2, Register arg3) {
 143 #ifdef _LP64
 144   // if there is any conflict use the stack
 145   if (arg1 == c_rarg2 || arg1 == c_rarg3 ||
 146       arg2 == c_rarg1 || arg1 == c_rarg3 ||
 147       arg3 == c_rarg1 || arg1 == c_rarg2) {
 148     push(arg3);
 149     push(arg2);
 150     push(arg1);
 151     pop(c_rarg1);
 152     pop(c_rarg2);
 153     pop(c_rarg3);
 154   } else {
 155     mov(c_rarg1, arg1);
 156     mov(c_rarg2, arg2);
 157     mov(c_rarg3, arg3);
 158   }
 159 #else
 160   push(arg3);
 161   push(arg2);
 162   push(arg1);
 163 #endif // _LP64
 164   return call_RT(oop_result1, oop_result2, entry, 3);
 165 }
 166 
 167 
 168 // Implementation of StubFrame
 169 
 170 class StubFrame: public StackObj {
 171  private:
 172   StubAssembler* _sasm;
 173 
 174  public:
 175   StubFrame(StubAssembler* sasm, const char* name, bool must_gc_arguments);
 176   void load_argument(int offset_in_words, Register reg);
 177 
 178   ~StubFrame();
 179 };
 180 
 181 
 182 #define __ _sasm->
 183 
 184 StubFrame::StubFrame(StubAssembler* sasm, const char* name, bool must_gc_arguments) {
 185   _sasm = sasm;
 186   __ set_info(name, must_gc_arguments);
 187   __ enter();
 188 }
 189 
 190 // load parameters that were stored with LIR_Assembler::store_parameter
 191 // Note: offsets for store_parameter and load_argument must match
 192 void StubFrame::load_argument(int offset_in_words, Register reg) {
 193   // rbp, + 0: link
 194   //     + 1: return address
 195   //     + 2: argument with offset 0
 196   //     + 3: argument with offset 1
 197   //     + 4: ...
 198 
 199   __ movptr(reg, Address(rbp, (offset_in_words + 2) * BytesPerWord));
 200 }
 201 
 202 
 203 StubFrame::~StubFrame() {
 204   __ leave();
 205   __ ret(0);
 206 }
 207 
 208 #undef __
 209 
 210 
 211 // Implementation of Runtime1
 212 
 213 #define __ sasm->
 214 
 215 const int float_regs_as_doubles_size_in_slots = pd_nof_fpu_regs_frame_map * 2;
 216 const int xmm_regs_as_doubles_size_in_slots = FrameMap::nof_xmm_regs * 2;
 217 
 218 // Stack layout for saving/restoring  all the registers needed during a runtime
 219 // call (this includes deoptimization)
 220 // Note: note that users of this frame may well have arguments to some runtime
 221 // while these values are on the stack. These positions neglect those arguments
 222 // but the code in save_live_registers will take the argument count into
 223 // account.
 224 //
 225 #ifdef _LP64
 226   #define SLOT2(x) x,
 227   #define SLOT_PER_WORD 2
 228 #else
 229   #define SLOT2(x)
 230   #define SLOT_PER_WORD 1
 231 #endif // _LP64
 232 
 233 enum reg_save_layout {
 234   // 64bit needs to keep stack 16 byte aligned. So we add some alignment dummies to make that
 235   // happen and will assert if the stack size we create is misaligned
 236 #ifdef _LP64
 237   align_dummy_0, align_dummy_1,
 238 #endif // _LP64
 239   dummy1, SLOT2(dummy1H)                                                                    // 0, 4
 240   dummy2, SLOT2(dummy2H)                                                                    // 8, 12
 241   // Two temps to be used as needed by users of save/restore callee registers
 242   temp_2_off, SLOT2(temp_2H_off)                                                            // 16, 20
 243   temp_1_off, SLOT2(temp_1H_off)                                                            // 24, 28
 244   xmm_regs_as_doubles_off,                                                                  // 32
 245   float_regs_as_doubles_off = xmm_regs_as_doubles_off + xmm_regs_as_doubles_size_in_slots,  // 160
 246   fpu_state_off = float_regs_as_doubles_off + float_regs_as_doubles_size_in_slots,          // 224
 247   // fpu_state_end_off is exclusive
 248   fpu_state_end_off = fpu_state_off + (FPUStateSizeInWords / SLOT_PER_WORD),                // 352
 249   marker = fpu_state_end_off, SLOT2(markerH)                                                // 352, 356
 250   extra_space_offset,                                                                       // 360
 251 #ifdef _LP64
 252   r15_off = extra_space_offset, r15H_off,                                                   // 360, 364
 253   r14_off, r14H_off,                                                                        // 368, 372
 254   r13_off, r13H_off,                                                                        // 376, 380
 255   r12_off, r12H_off,                                                                        // 384, 388
 256   r11_off, r11H_off,                                                                        // 392, 396
 257   r10_off, r10H_off,                                                                        // 400, 404
 258   r9_off, r9H_off,                                                                          // 408, 412
 259   r8_off, r8H_off,                                                                          // 416, 420
 260   rdi_off, rdiH_off,                                                                        // 424, 428
 261 #else
 262   rdi_off = extra_space_offset,
 263 #endif // _LP64
 264   rsi_off, SLOT2(rsiH_off)                                                                  // 432, 436
 265   rbp_off, SLOT2(rbpH_off)                                                                  // 440, 444
 266   rsp_off, SLOT2(rspH_off)                                                                  // 448, 452
 267   rbx_off, SLOT2(rbxH_off)                                                                  // 456, 460
 268   rdx_off, SLOT2(rdxH_off)                                                                  // 464, 468
 269   rcx_off, SLOT2(rcxH_off)                                                                  // 472, 476
 270   rax_off, SLOT2(raxH_off)                                                                  // 480, 484
 271   saved_rbp_off, SLOT2(saved_rbpH_off)                                                      // 488, 492
 272   return_off, SLOT2(returnH_off)                                                            // 496, 500
 273   reg_save_frame_size,  // As noted: neglects any parameters to runtime                     // 504
 274 
 275 #ifdef _WIN64
 276   c_rarg0_off = rcx_off,
 277 #else
 278   c_rarg0_off = rdi_off,
 279 #endif // WIN64
 280 
 281   // equates
 282 
 283   // illegal instruction handler
 284   continue_dest_off = temp_1_off,
 285 
 286   // deoptimization equates
 287   fp0_off = float_regs_as_doubles_off, // slot for java float/double return value
 288   xmm0_off = xmm_regs_as_doubles_off,  // slot for java float/double return value
 289   deopt_type = temp_2_off,             // slot for type of deopt in progress
 290   ret_type = temp_1_off                // slot for return type
 291 };
 292 
 293 
 294 
 295 // Save off registers which might be killed by calls into the runtime.
 296 // Tries to smart of about FP registers.  In particular we separate
 297 // saving and describing the FPU registers for deoptimization since we
 298 // have to save the FPU registers twice if we describe them and on P4
 299 // saving FPU registers which don't contain anything appears
 300 // expensive.  The deopt blob is the only thing which needs to
 301 // describe FPU registers.  In all other cases it should be sufficient
 302 // to simply save their current value.
 303 
 304 static OopMap* generate_oop_map(StubAssembler* sasm, int num_rt_args,
 305                                 bool save_fpu_registers = true) {
 306 
 307   // In 64bit all the args are in regs so there are no additional stack slots
 308   LP64_ONLY(num_rt_args = 0);
 309   LP64_ONLY(assert((reg_save_frame_size * VMRegImpl::stack_slot_size) % 16 == 0, "must be 16 byte aligned");)
 310   int frame_size_in_slots = reg_save_frame_size + num_rt_args; // args + thread
 311   sasm->set_frame_size(frame_size_in_slots / VMRegImpl::slots_per_word );
 312 
 313   // record saved value locations in an OopMap
 314   // locations are offsets from sp after runtime call; num_rt_args is number of arguments in call, including thread
 315   OopMap* map = new OopMap(frame_size_in_slots, 0);
 316   map->set_callee_saved(VMRegImpl::stack2reg(rax_off + num_rt_args), rax->as_VMReg());
 317   map->set_callee_saved(VMRegImpl::stack2reg(rcx_off + num_rt_args), rcx->as_VMReg());
 318   map->set_callee_saved(VMRegImpl::stack2reg(rdx_off + num_rt_args), rdx->as_VMReg());
 319   map->set_callee_saved(VMRegImpl::stack2reg(rbx_off + num_rt_args), rbx->as_VMReg());
 320   map->set_callee_saved(VMRegImpl::stack2reg(rsi_off + num_rt_args), rsi->as_VMReg());
 321   map->set_callee_saved(VMRegImpl::stack2reg(rdi_off + num_rt_args), rdi->as_VMReg());
 322 #ifdef _LP64
 323   map->set_callee_saved(VMRegImpl::stack2reg(r8_off + num_rt_args),  r8->as_VMReg());
 324   map->set_callee_saved(VMRegImpl::stack2reg(r9_off + num_rt_args),  r9->as_VMReg());
 325   map->set_callee_saved(VMRegImpl::stack2reg(r10_off + num_rt_args), r10->as_VMReg());
 326   map->set_callee_saved(VMRegImpl::stack2reg(r11_off + num_rt_args), r11->as_VMReg());
 327   map->set_callee_saved(VMRegImpl::stack2reg(r12_off + num_rt_args), r12->as_VMReg());
 328   map->set_callee_saved(VMRegImpl::stack2reg(r13_off + num_rt_args), r13->as_VMReg());
 329   map->set_callee_saved(VMRegImpl::stack2reg(r14_off + num_rt_args), r14->as_VMReg());
 330   map->set_callee_saved(VMRegImpl::stack2reg(r15_off + num_rt_args), r15->as_VMReg());
 331 
 332   // This is stupid but needed.
 333   map->set_callee_saved(VMRegImpl::stack2reg(raxH_off + num_rt_args), rax->as_VMReg()->next());
 334   map->set_callee_saved(VMRegImpl::stack2reg(rcxH_off + num_rt_args), rcx->as_VMReg()->next());
 335   map->set_callee_saved(VMRegImpl::stack2reg(rdxH_off + num_rt_args), rdx->as_VMReg()->next());
 336   map->set_callee_saved(VMRegImpl::stack2reg(rbxH_off + num_rt_args), rbx->as_VMReg()->next());
 337   map->set_callee_saved(VMRegImpl::stack2reg(rsiH_off + num_rt_args), rsi->as_VMReg()->next());
 338   map->set_callee_saved(VMRegImpl::stack2reg(rdiH_off + num_rt_args), rdi->as_VMReg()->next());
 339 
 340   map->set_callee_saved(VMRegImpl::stack2reg(r8H_off + num_rt_args),  r8->as_VMReg()->next());
 341   map->set_callee_saved(VMRegImpl::stack2reg(r9H_off + num_rt_args),  r9->as_VMReg()->next());
 342   map->set_callee_saved(VMRegImpl::stack2reg(r10H_off + num_rt_args), r10->as_VMReg()->next());
 343   map->set_callee_saved(VMRegImpl::stack2reg(r11H_off + num_rt_args), r11->as_VMReg()->next());
 344   map->set_callee_saved(VMRegImpl::stack2reg(r12H_off + num_rt_args), r12->as_VMReg()->next());
 345   map->set_callee_saved(VMRegImpl::stack2reg(r13H_off + num_rt_args), r13->as_VMReg()->next());
 346   map->set_callee_saved(VMRegImpl::stack2reg(r14H_off + num_rt_args), r14->as_VMReg()->next());
 347   map->set_callee_saved(VMRegImpl::stack2reg(r15H_off + num_rt_args), r15->as_VMReg()->next());
 348 #endif // _LP64
 349 
 350   if (save_fpu_registers) {
 351     if (UseSSE < 2) {
 352       int fpu_off = float_regs_as_doubles_off;
 353       for (int n = 0; n < FrameMap::nof_fpu_regs; n++) {
 354         VMReg fpu_name_0 = FrameMap::fpu_regname(n);
 355         map->set_callee_saved(VMRegImpl::stack2reg(fpu_off +     num_rt_args), fpu_name_0);
 356         // %%% This is really a waste but we'll keep things as they were for now
 357         if (true) {
 358           map->set_callee_saved(VMRegImpl::stack2reg(fpu_off + 1 + num_rt_args), fpu_name_0->next());
 359         }
 360         fpu_off += 2;
 361       }
 362       assert(fpu_off == fpu_state_off, "incorrect number of fpu stack slots");
 363     }
 364 
 365     if (UseSSE >= 2) {
 366       int xmm_off = xmm_regs_as_doubles_off;
 367       for (int n = 0; n < FrameMap::nof_xmm_regs; n++) {
 368         VMReg xmm_name_0 = as_XMMRegister(n)->as_VMReg();


 376       assert(xmm_off == float_regs_as_doubles_off, "incorrect number of xmm registers");
 377 
 378     } else if (UseSSE == 1) {
 379       int xmm_off = xmm_regs_as_doubles_off;
 380       for (int n = 0; n < FrameMap::nof_xmm_regs; n++) {
 381         VMReg xmm_name_0 = as_XMMRegister(n)->as_VMReg();
 382         map->set_callee_saved(VMRegImpl::stack2reg(xmm_off +     num_rt_args), xmm_name_0);
 383         xmm_off += 2;
 384       }
 385       assert(xmm_off == float_regs_as_doubles_off, "incorrect number of xmm registers");
 386     }
 387   }
 388 
 389   return map;
 390 }
 391 
 392 static OopMap* save_live_registers(StubAssembler* sasm, int num_rt_args,
 393                                    bool save_fpu_registers = true) {
 394   __ block_comment("save_live_registers");
 395 
 396   // 64bit passes the args in regs to the c++ runtime
 397   int frame_size_in_slots = reg_save_frame_size NOT_LP64(+ num_rt_args); // args + thread
 398   // frame_size = round_to(frame_size, 4);
 399   sasm->set_frame_size(frame_size_in_slots / VMRegImpl::slots_per_word );
 400 
 401   __ pusha();         // integer registers
 402 
 403   // assert(float_regs_as_doubles_off % 2 == 0, "misaligned offset");
 404   // assert(xmm_regs_as_doubles_off % 2 == 0, "misaligned offset");
 405 
 406   __ subptr(rsp, extra_space_offset * VMRegImpl::stack_slot_size);
 407 
 408 #ifdef ASSERT
 409   __ movptr(Address(rsp, marker * VMRegImpl::stack_slot_size), (int32_t)0xfeedbeef);
 410 #endif
 411 
 412   if (save_fpu_registers) {
 413     if (UseSSE < 2) {
 414       // save FPU stack
 415       __ fnsave(Address(rsp, fpu_state_off * VMRegImpl::stack_slot_size));
 416       __ fwait();
 417 
 418 #ifdef ASSERT
 419       Label ok;
 420       __ cmpw(Address(rsp, fpu_state_off * VMRegImpl::stack_slot_size), StubRoutines::fpu_cntrl_wrd_std());
 421       __ jccb(Assembler::equal, ok);
 422       __ stop("corrupted control word detected");
 423       __ bind(ok);
 424 #endif
 425 
 426       // Reset the control word to guard against exceptions being unmasked
 427       // since fstp_d can cause FPU stack underflow exceptions.  Write it
 428       // into the on stack copy and then reload that to make sure that the
 429       // current and future values are correct.
 430       __ movw(Address(rsp, fpu_state_off * VMRegImpl::stack_slot_size), StubRoutines::fpu_cntrl_wrd_std());
 431       __ frstor(Address(rsp, fpu_state_off * VMRegImpl::stack_slot_size));
 432 
 433       // Save the FPU registers in de-opt-able form
 434       __ fstp_d(Address(rsp, float_regs_as_doubles_off * VMRegImpl::stack_slot_size +  0));
 435       __ fstp_d(Address(rsp, float_regs_as_doubles_off * VMRegImpl::stack_slot_size +  8));
 436       __ fstp_d(Address(rsp, float_regs_as_doubles_off * VMRegImpl::stack_slot_size + 16));
 437       __ fstp_d(Address(rsp, float_regs_as_doubles_off * VMRegImpl::stack_slot_size + 24));
 438       __ fstp_d(Address(rsp, float_regs_as_doubles_off * VMRegImpl::stack_slot_size + 32));
 439       __ fstp_d(Address(rsp, float_regs_as_doubles_off * VMRegImpl::stack_slot_size + 40));
 440       __ fstp_d(Address(rsp, float_regs_as_doubles_off * VMRegImpl::stack_slot_size + 48));
 441       __ fstp_d(Address(rsp, float_regs_as_doubles_off * VMRegImpl::stack_slot_size + 56));
 442     }
 443 
 444     if (UseSSE >= 2) {
 445       // save XMM registers
 446       // XMM registers can contain float or double values, but this is not known here,
 447       // so always save them as doubles.
 448       // note that float values are _not_ converted automatically, so for float values
 449       // the second word contains only garbage data.
 450       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size +  0), xmm0);
 451       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size +  8), xmm1);
 452       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 16), xmm2);
 453       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 24), xmm3);
 454       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 32), xmm4);
 455       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 40), xmm5);
 456       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 48), xmm6);
 457       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 56), xmm7);
 458 #ifdef _LP64
 459       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 64), xmm8);
 460       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 72), xmm9);
 461       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 80), xmm10);
 462       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 88), xmm11);
 463       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 96), xmm12);
 464       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 104), xmm13);
 465       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 112), xmm14);
 466       __ movdbl(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 120), xmm15);
 467 #endif // _LP64
 468     } else if (UseSSE == 1) {
 469       // save XMM registers as float because double not supported without SSE2
 470       __ movflt(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size +  0), xmm0);
 471       __ movflt(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size +  8), xmm1);
 472       __ movflt(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 16), xmm2);
 473       __ movflt(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 24), xmm3);
 474       __ movflt(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 32), xmm4);
 475       __ movflt(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 40), xmm5);
 476       __ movflt(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 48), xmm6);
 477       __ movflt(Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 56), xmm7);
 478     }
 479   }
 480 
 481   // FPU stack must be empty now
 482   __ verify_FPU(0, "save_live_registers");
 483 
 484   return generate_oop_map(sasm, num_rt_args, save_fpu_registers);
 485 }
 486 
 487 
 488 static void restore_fpu(StubAssembler* sasm, bool restore_fpu_registers = true) {
 489   if (restore_fpu_registers) {
 490     if (UseSSE >= 2) {
 491       // restore XMM registers
 492       __ movdbl(xmm0, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size +  0));
 493       __ movdbl(xmm1, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size +  8));
 494       __ movdbl(xmm2, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 16));
 495       __ movdbl(xmm3, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 24));
 496       __ movdbl(xmm4, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 32));
 497       __ movdbl(xmm5, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 40));
 498       __ movdbl(xmm6, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 48));
 499       __ movdbl(xmm7, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 56));
 500 #ifdef _LP64
 501       __ movdbl(xmm8, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 64));
 502       __ movdbl(xmm9, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 72));
 503       __ movdbl(xmm10, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 80));
 504       __ movdbl(xmm11, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 88));
 505       __ movdbl(xmm12, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 96));
 506       __ movdbl(xmm13, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 104));
 507       __ movdbl(xmm14, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 112));
 508       __ movdbl(xmm15, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 120));
 509 #endif // _LP64
 510     } else if (UseSSE == 1) {
 511       // restore XMM registers
 512       __ movflt(xmm0, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size +  0));
 513       __ movflt(xmm1, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size +  8));
 514       __ movflt(xmm2, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 16));
 515       __ movflt(xmm3, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 24));
 516       __ movflt(xmm4, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 32));
 517       __ movflt(xmm5, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 40));
 518       __ movflt(xmm6, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 48));
 519       __ movflt(xmm7, Address(rsp, xmm_regs_as_doubles_off * VMRegImpl::stack_slot_size + 56));
 520     }
 521 
 522     if (UseSSE < 2) {
 523       __ frstor(Address(rsp, fpu_state_off * VMRegImpl::stack_slot_size));
 524     } else {
 525       // check that FPU stack is really empty
 526       __ verify_FPU(0, "restore_live_registers");
 527     }
 528 
 529   } else {
 530     // check that FPU stack is really empty
 531     __ verify_FPU(0, "restore_live_registers");
 532   }
 533 
 534 #ifdef ASSERT
 535   {
 536     Label ok;
 537     __ cmpptr(Address(rsp, marker * VMRegImpl::stack_slot_size), (int32_t)0xfeedbeef);
 538     __ jcc(Assembler::equal, ok);
 539     __ stop("bad offsets in frame");
 540     __ bind(ok);
 541   }
 542 #endif // ASSERT
 543 
 544   __ addptr(rsp, extra_space_offset * VMRegImpl::stack_slot_size);
 545 }
 546 
 547 
 548 static void restore_live_registers(StubAssembler* sasm, bool restore_fpu_registers = true) {
 549   __ block_comment("restore_live_registers");
 550 
 551   restore_fpu(sasm, restore_fpu_registers);
 552   __ popa();
 553 }
 554 
 555 
 556 static void restore_live_registers_except_rax(StubAssembler* sasm, bool restore_fpu_registers = true) {
 557   __ block_comment("restore_live_registers_except_rax");
 558 
 559   restore_fpu(sasm, restore_fpu_registers);
 560 
 561 #ifdef _LP64
 562   __ movptr(r15, Address(rsp, 0));
 563   __ movptr(r14, Address(rsp, wordSize));
 564   __ movptr(r13, Address(rsp, 2 * wordSize));
 565   __ movptr(r12, Address(rsp, 3 * wordSize));
 566   __ movptr(r11, Address(rsp, 4 * wordSize));
 567   __ movptr(r10, Address(rsp, 5 * wordSize));
 568   __ movptr(r9,  Address(rsp, 6 * wordSize));
 569   __ movptr(r8,  Address(rsp, 7 * wordSize));
 570   __ movptr(rdi, Address(rsp, 8 * wordSize));
 571   __ movptr(rsi, Address(rsp, 9 * wordSize));
 572   __ movptr(rbp, Address(rsp, 10 * wordSize));
 573   // skip rsp
 574   __ movptr(rbx, Address(rsp, 12 * wordSize));
 575   __ movptr(rdx, Address(rsp, 13 * wordSize));
 576   __ movptr(rcx, Address(rsp, 14 * wordSize));
 577 
 578   __ addptr(rsp, 16 * wordSize);
 579 #else
 580 
 581   __ pop(rdi);
 582   __ pop(rsi);
 583   __ pop(rbp);
 584   __ pop(rbx); // skip this value
 585   __ pop(rbx);
 586   __ pop(rdx);
 587   __ pop(rcx);
 588   __ addptr(rsp, BytesPerWord);
 589 #endif // _LP64
 590 }
 591 
 592 
 593 void Runtime1::initialize_pd() {
 594   // nothing to do
 595 }
 596 
 597 
 598 // target: the entry point of the method that creates and posts the exception oop
 599 // has_argument: true if the exception needs an argument (passed on stack because registers must be preserved)
 600 
 601 OopMapSet* Runtime1::generate_exception_throw(StubAssembler* sasm, address target, bool has_argument) {
 602   // preserve all registers
 603   int num_rt_args = has_argument ? 2 : 1;
 604   OopMap* oop_map = save_live_registers(sasm, num_rt_args);
 605 
 606   // now all registers are saved and can be used freely
 607   // verify that no old value is used accidentally
 608   __ invalidate_registers(true, true, true, true, true, true);
 609 
 610   // registers used by this stub
 611   const Register temp_reg = rbx;
 612 
 613   // load argument for exception that is passed as an argument into the stub
 614   if (has_argument) {
 615 #ifdef _LP64
 616     __ movptr(c_rarg1, Address(rbp, 2*BytesPerWord));
 617 #else
 618     __ movptr(temp_reg, Address(rbp, 2*BytesPerWord));
 619     __ push(temp_reg);
 620 #endif // _LP64
 621   }

 622   int call_offset = __ call_RT(noreg, noreg, target, num_rt_args - 1);
 623 
 624   OopMapSet* oop_maps = new OopMapSet();
 625   oop_maps->add_gc_map(call_offset, oop_map);
 626 
 627   __ stop("should not reach here");
 628 
 629   return oop_maps;
 630 }
 631 
 632 
 633 void Runtime1::generate_handle_exception(StubAssembler *sasm, OopMapSet* oop_maps, OopMap* oop_map, bool save_fpu_registers) {
 634   // incoming parameters
 635   const Register exception_oop = rax;
 636   const Register exception_pc = rdx;
 637   // other registers used in this stub
 638   const Register real_return_addr = rbx;
 639   const Register thread = NOT_LP64(rdi) LP64_ONLY(r15_thread);
 640 
 641   __ block_comment("generate_handle_exception");
 642 
 643 #ifdef TIERED
 644   // C2 can leave the fpu stack dirty
 645   if (UseSSE < 2 ) {
 646     __ empty_FPU_stack();
 647   }
 648 #endif // TIERED
 649 
 650   // verify that only rax, and rdx is valid at this time
 651   __ invalidate_registers(false, true, true, false, true, true);
 652   // verify that rax, contains a valid exception
 653   __ verify_not_null_oop(exception_oop);
 654 
 655   // load address of JavaThread object for thread-local data
 656   NOT_LP64(__ get_thread(thread);)
 657 
 658 #ifdef ASSERT
 659   // check that fields in JavaThread for exception oop and issuing pc are
 660   // empty before writing to them
 661   Label oop_empty;
 662   __ cmpptr(Address(thread, JavaThread::exception_oop_offset()), (int32_t) NULL_WORD);
 663   __ jcc(Assembler::equal, oop_empty);
 664   __ stop("exception oop already set");
 665   __ bind(oop_empty);
 666 
 667   Label pc_empty;
 668   __ cmpptr(Address(thread, JavaThread::exception_pc_offset()), 0);
 669   __ jcc(Assembler::equal, pc_empty);
 670   __ stop("exception pc already set");
 671   __ bind(pc_empty);
 672 #endif
 673 
 674   // save exception oop and issuing pc into JavaThread
 675   // (exception handler will load it from here)
 676   __ movptr(Address(thread, JavaThread::exception_oop_offset()), exception_oop);
 677   __ movptr(Address(thread, JavaThread::exception_pc_offset()), exception_pc);
 678 
 679   // save real return address (pc that called this stub)
 680   __ movptr(real_return_addr, Address(rbp, 1*BytesPerWord));
 681   __ movptr(Address(rsp, temp_1_off * VMRegImpl::stack_slot_size), real_return_addr);
 682 
 683   // patch throwing pc into return address (has bci & oop map)
 684   __ movptr(Address(rbp, 1*BytesPerWord), exception_pc);
 685 
 686   // compute the exception handler.
 687   // the exception oop and the throwing pc are read from the fields in JavaThread
 688   int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, exception_handler_for_pc));
 689   oop_maps->add_gc_map(call_offset, oop_map);
 690 
 691   // rax,: handler address or NULL if no handler exists
 692   //      will be the deopt blob if nmethod was deoptimized while we looked up
 693   //      handler regardless of whether handler existed in the nmethod.
 694 
 695   // only rax, is valid at this time, all other registers have been destroyed by the runtime call
 696   __ invalidate_registers(false, true, true, true, true, true);
 697 
 698   // Do we have an exception handler in the nmethod?
 699   Label no_handler;
 700   Label done;
 701   __ testptr(rax, rax);
 702   __ jcc(Assembler::zero, no_handler);
 703 
 704   // exception handler found
 705   // patch the return address -> the stub will directly return to the exception handler
 706   __ movptr(Address(rbp, 1*BytesPerWord), rax);
 707 
 708   // restore registers
 709   restore_live_registers(sasm, save_fpu_registers);
 710 
 711   // return to exception handler
 712   __ leave();
 713   __ ret(0);
 714 
 715   __ bind(no_handler);
 716   // no exception handler found in this method, so the exception is
 717   // forwarded to the caller (using the unwind code of the nmethod)
 718   // there is no need to restore the registers
 719 
 720   // restore the real return address that was saved before the RT-call
 721   __ movptr(real_return_addr, Address(rsp, temp_1_off * VMRegImpl::stack_slot_size));
 722   __ movptr(Address(rbp, 1*BytesPerWord), real_return_addr);
 723 
 724   // load address of JavaThread object for thread-local data
 725   NOT_LP64(__ get_thread(thread);)
 726   // restore exception oop into rax, (convention for unwind code)
 727   __ movptr(exception_oop, Address(thread, JavaThread::exception_oop_offset()));
 728 
 729   // clear exception fields in JavaThread because they are no longer needed
 730   // (fields must be cleared because they are processed by GC otherwise)
 731   __ movptr(Address(thread, JavaThread::exception_oop_offset()), (int32_t)NULL_WORD);
 732   __ movptr(Address(thread, JavaThread::exception_pc_offset()), (int32_t)NULL_WORD);
 733 
 734   // pop the stub frame off
 735   __ leave();
 736 
 737   generate_unwind_exception(sasm);
 738   __ stop("should not reach here");
 739 }
 740 
 741 
 742 void Runtime1::generate_unwind_exception(StubAssembler *sasm) {
 743   // incoming parameters
 744   const Register exception_oop = rax;
 745   // other registers used in this stub
 746   const Register exception_pc = rdx;
 747   const Register handler_addr = rbx;
 748   const Register thread = NOT_LP64(rdi) LP64_ONLY(r15_thread);
 749 
 750   // verify that only rax, is valid at this time
 751   __ invalidate_registers(false, true, true, true, true, true);
 752 
 753 #ifdef ASSERT
 754   // check that fields in JavaThread for exception oop and issuing pc are empty
 755   NOT_LP64(__ get_thread(thread);)
 756   Label oop_empty;
 757   __ cmpptr(Address(thread, JavaThread::exception_oop_offset()), 0);
 758   __ jcc(Assembler::equal, oop_empty);
 759   __ stop("exception oop must be empty");
 760   __ bind(oop_empty);
 761 
 762   Label pc_empty;
 763   __ cmpptr(Address(thread, JavaThread::exception_pc_offset()), 0);
 764   __ jcc(Assembler::equal, pc_empty);
 765   __ stop("exception pc must be empty");
 766   __ bind(pc_empty);
 767 #endif
 768 
 769   // clear the FPU stack in case any FPU results are left behind
 770   __ empty_FPU_stack();
 771 
 772   // leave activation of nmethod
 773   __ leave();
 774   // store return address (is on top of stack after leave)
 775   __ movptr(exception_pc, Address(rsp, 0));
 776 
 777   __ verify_oop(exception_oop);
 778 
 779   // save exception oop from rax, to stack before call
 780   __ push(exception_oop);
 781 
 782   // search the exception handler address of the caller (using the return address)
 783   __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), exception_pc);
 784   // rax,: exception handler address of the caller
 785 
 786   // only rax, is valid at this time, all other registers have been destroyed by the call
 787   __ invalidate_registers(false, true, true, true, true, true);
 788 
 789   // move result of call into correct register
 790   __ movptr(handler_addr, rax);
 791 
 792   // restore exception oop in rax, (required convention of exception handler)
 793   __ pop(exception_oop);
 794 
 795   __ verify_oop(exception_oop);
 796 
 797   // get throwing pc (= return address).
 798   // rdx has been destroyed by the call, so it must be set again
 799   // the pop is also necessary to simulate the effect of a ret(0)
 800   __ pop(exception_pc);
 801 
 802   // verify that that there is really a valid exception in rax,
 803   __ verify_not_null_oop(exception_oop);
 804 
 805   // continue at exception handler (return address removed)
 806   // note: do *not* remove arguments when unwinding the
 807   //       activation since the caller assumes having
 808   //       all arguments on the stack when entering the
 809   //       runtime to determine the exception handler
 810   //       (GC happens at call site with arguments!)
 811   // rax,: exception oop
 812   // rdx: throwing pc
 813   // rbx,: exception handler
 814   __ jmp(handler_addr);
 815 }
 816 
 817 
 818 OopMapSet* Runtime1::generate_patching(StubAssembler* sasm, address target) {
 819   // use the maximum number of runtime-arguments here because it is difficult to
 820   // distinguish each RT-Call.
 821   // Note: This number affects also the RT-Call in generate_handle_exception because
 822   //       the oop-map is shared for all calls.
 823   const int num_rt_args = 2;  // thread + dummy
 824 
 825   DeoptimizationBlob* deopt_blob = SharedRuntime::deopt_blob();
 826   assert(deopt_blob != NULL, "deoptimization blob must have been created");
 827 
 828   OopMap* oop_map = save_live_registers(sasm, num_rt_args);
 829 
 830 #ifdef _LP64
 831   const Register thread = r15_thread;
 832   // No need to worry about dummy
 833   __ mov(c_rarg0, thread);
 834 #else
 835   __ push(rax); // push dummy
 836 
 837   const Register thread = rdi; // is callee-saved register (Visual C++ calling conventions)
 838   // push java thread (becomes first argument of C function)
 839   __ get_thread(thread);
 840   __ push(thread);
 841 #endif // _LP64
 842   __ set_last_Java_frame(thread, noreg, rbp, NULL);
 843   // do the call
 844   __ call(RuntimeAddress(target));
 845   OopMapSet* oop_maps = new OopMapSet();
 846   oop_maps->add_gc_map(__ offset(), oop_map);
 847   // verify callee-saved register
 848 #ifdef ASSERT
 849   guarantee(thread != rax, "change this code");
 850   __ push(rax);
 851   { Label L;
 852     __ get_thread(rax);
 853     __ cmpptr(thread, rax);
 854     __ jcc(Assembler::equal, L);
 855     __ stop("StubAssembler::call_RT: rdi/r15 not callee saved?");
 856     __ bind(L);
 857   }
 858   __ pop(rax);
 859 #endif
 860   __ reset_last_Java_frame(thread, true, false);
 861 #ifndef _LP64
 862   __ pop(rcx); // discard thread arg
 863   __ pop(rcx); // discard dummy
 864 #endif // _LP64
 865 
 866   // check for pending exceptions
 867   { Label L;
 868     __ cmpptr(Address(thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 869     __ jcc(Assembler::equal, L);
 870     // exception pending => remove activation and forward to exception handler
 871 
 872     __ testptr(rax, rax);                                   // have we deoptimized?
 873     __ jump_cc(Assembler::equal,
 874                RuntimeAddress(Runtime1::entry_for(Runtime1::forward_exception_id)));
 875 
 876     // the deopt blob expects exceptions in the special fields of
 877     // JavaThread, so copy and clear pending exception.
 878 
 879     // load and clear pending exception
 880     __ movptr(rax, Address(thread, Thread::pending_exception_offset()));
 881     __ movptr(Address(thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 882 
 883     // check that there is really a valid exception
 884     __ verify_not_null_oop(rax);
 885 
 886     // load throwing pc: this is the return address of the stub
 887     __ movptr(rdx, Address(rsp, return_off * VMRegImpl::stack_slot_size));
 888 
 889 #ifdef ASSERT
 890     // check that fields in JavaThread for exception oop and issuing pc are empty
 891     Label oop_empty;
 892     __ cmpptr(Address(thread, JavaThread::exception_oop_offset()), (int32_t)NULL_WORD);
 893     __ jcc(Assembler::equal, oop_empty);
 894     __ stop("exception oop must be empty");
 895     __ bind(oop_empty);
 896 
 897     Label pc_empty;
 898     __ cmpptr(Address(thread, JavaThread::exception_pc_offset()), (int32_t)NULL_WORD);
 899     __ jcc(Assembler::equal, pc_empty);
 900     __ stop("exception pc must be empty");
 901     __ bind(pc_empty);
 902 #endif
 903 
 904     // store exception oop and throwing pc to JavaThread
 905     __ movptr(Address(thread, JavaThread::exception_oop_offset()), rax);
 906     __ movptr(Address(thread, JavaThread::exception_pc_offset()), rdx);
 907 
 908     restore_live_registers(sasm);
 909 
 910     __ leave();
 911     __ addptr(rsp, BytesPerWord);  // remove return address from stack
 912 
 913     // Forward the exception directly to deopt blob. We can blow no
 914     // registers and must leave throwing pc on the stack.  A patch may
 915     // have values live in registers so the entry point with the
 916     // exception in tls.
 917     __ jump(RuntimeAddress(deopt_blob->unpack_with_exception_in_tls()));
 918 
 919     __ bind(L);
 920   }
 921 
 922 
 923   // Runtime will return true if the nmethod has been deoptimized during
 924   // the patching process. In that case we must do a deopt reexecute instead.
 925 
 926   Label reexecuteEntry, cont;
 927 
 928   __ testptr(rax, rax);                                 // have we deoptimized?
 929   __ jcc(Assembler::equal, cont);                       // no
 930 
 931   // Will reexecute. Proper return address is already on the stack we just restore
 932   // registers, pop all of our frame but the return address and jump to the deopt blob
 933   restore_live_registers(sasm);
 934   __ leave();
 935   __ jump(RuntimeAddress(deopt_blob->unpack_with_reexecution()));
 936 
 937   __ bind(cont);
 938   restore_live_registers(sasm);
 939   __ leave();
 940   __ ret(0);
 941 
 942   return oop_maps;
 943 
 944 }
 945 
 946 
 947 OopMapSet* Runtime1::generate_code_for(StubID id, StubAssembler* sasm) {
 948 
 949   // for better readability
 950   const bool must_gc_arguments = true;
 951   const bool dont_gc_arguments = false;
 952 
 953   // default value; overwritten for some optimized stubs that are called from methods that do not use the fpu
 954   bool save_fpu_registers = true;
 955 
 956   // stub code & info for the different stubs
 957   OopMapSet* oop_maps = NULL;
 958   switch (id) {
 959     case forward_exception_id:
 960       {
 961         // we're handling an exception in the context of a compiled
 962         // frame.  The registers have been saved in the standard
 963         // places.  Perform an exception lookup in the caller and
 964         // dispatch to the handler if found.  Otherwise unwind and
 965         // dispatch to the callers exception handler.
 966 
 967         const Register thread = NOT_LP64(rdi) LP64_ONLY(r15_thread);
 968         const Register exception_oop = rax;
 969         const Register exception_pc = rdx;
 970 
 971         // load pending exception oop into rax,
 972         __ movptr(exception_oop, Address(thread, Thread::pending_exception_offset()));
 973         // clear pending exception
 974         __ movptr(Address(thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 975 
 976         // load issuing PC (the return address for this stub) into rdx
 977         __ movptr(exception_pc, Address(rbp, 1*BytesPerWord));
 978 
 979         // make sure that the vm_results are cleared (may be unnecessary)
 980         __ movptr(Address(thread, JavaThread::vm_result_offset()), (int32_t)NULL_WORD);
 981         __ movptr(Address(thread, JavaThread::vm_result_2_offset()), (int32_t)NULL_WORD);
 982 
 983         // verify that that there is really a valid exception in rax,
 984         __ verify_not_null_oop(exception_oop);
 985 
 986 
 987         oop_maps = new OopMapSet();
 988         OopMap* oop_map = generate_oop_map(sasm, 1);
 989         generate_handle_exception(sasm, oop_maps, oop_map);
 990         __ stop("should not reach here");
 991       }
 992       break;
 993 
 994     case new_instance_id:
 995     case fast_new_instance_id:
 996     case fast_new_instance_init_check_id:
 997       {
 998         Register klass = rdx; // Incoming
 999         Register obj   = rax; // Result
1000 
1001         if (id == new_instance_id) {
1002           __ set_info("new_instance", dont_gc_arguments);
1003         } else if (id == fast_new_instance_id) {
1004           __ set_info("fast new_instance", dont_gc_arguments);
1005         } else {
1006           assert(id == fast_new_instance_init_check_id, "bad StubID");
1007           __ set_info("fast new_instance init check", dont_gc_arguments);
1008         }
1009 
1010         if ((id == fast_new_instance_id || id == fast_new_instance_init_check_id) &&
1011             UseTLAB && FastTLABRefill) {
1012           Label slow_path;
1013           Register obj_size = rcx;
1014           Register t1       = rbx;
1015           Register t2       = rsi;
1016           assert_different_registers(klass, obj, obj_size, t1, t2);
1017 
1018           __ push(rdi);
1019           __ push(rbx);
1020 
1021           if (id == fast_new_instance_init_check_id) {
1022             // make sure the klass is initialized
1023             __ cmpl(Address(klass, instanceKlass::init_state_offset_in_bytes() + sizeof(oopDesc)), instanceKlass::fully_initialized);
1024             __ jcc(Assembler::notEqual, slow_path);
1025           }
1026 
1027 #ifdef ASSERT
1028           // assert object can be fast path allocated
1029           {
1030             Label ok, not_ok;
1031             __ movl(obj_size, Address(klass, Klass::layout_helper_offset_in_bytes() + sizeof(oopDesc)));
1032             __ cmpl(obj_size, 0);  // make sure it's an instance (LH > 0)
1033             __ jcc(Assembler::lessEqual, not_ok);
1034             __ testl(obj_size, Klass::_lh_instance_slow_path_bit);
1035             __ jcc(Assembler::zero, ok);
1036             __ bind(not_ok);
1037             __ stop("assert(can be fast path allocated)");
1038             __ should_not_reach_here();
1039             __ bind(ok);
1040           }
1041 #endif // ASSERT
1042 
1043           // if we got here then the TLAB allocation failed, so try
1044           // refilling the TLAB or allocating directly from eden.
1045           Label retry_tlab, try_eden;
1046           __ tlab_refill(retry_tlab, try_eden, slow_path); // does not destroy rdx (klass)
1047 
1048           __ bind(retry_tlab);
1049 
1050           // get the instance size (size is postive so movl is fine for 64bit)
1051           __ movl(obj_size, Address(klass, klassOopDesc::header_size() * HeapWordSize + Klass::layout_helper_offset_in_bytes()));
1052           __ tlab_allocate(obj, obj_size, 0, t1, t2, slow_path);
1053           __ initialize_object(obj, klass, obj_size, 0, t1, t2);
1054           __ verify_oop(obj);
1055           __ pop(rbx);
1056           __ pop(rdi);
1057           __ ret(0);
1058 
1059           __ bind(try_eden);
1060           // get the instance size (size is postive so movl is fine for 64bit)
1061           __ movl(obj_size, Address(klass, klassOopDesc::header_size() * HeapWordSize + Klass::layout_helper_offset_in_bytes()));
1062           __ eden_allocate(obj, obj_size, 0, t1, slow_path);
1063           __ initialize_object(obj, klass, obj_size, 0, t1, t2);
1064           __ verify_oop(obj);
1065           __ pop(rbx);
1066           __ pop(rdi);
1067           __ ret(0);
1068 
1069           __ bind(slow_path);
1070           __ pop(rbx);
1071           __ pop(rdi);
1072         }
1073 
1074         __ enter();
1075         OopMap* map = save_live_registers(sasm, 2);
1076         int call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_instance), klass);
1077         oop_maps = new OopMapSet();
1078         oop_maps->add_gc_map(call_offset, map);
1079         restore_live_registers_except_rax(sasm);
1080         __ verify_oop(obj);
1081         __ leave();
1082         __ ret(0);
1083 
1084         // rax,: new instance
1085       }
1086 
1087       break;
1088 
1089 #ifdef TIERED
1090     case counter_overflow_id:
1091       {


1137 
1138         if (UseTLAB && FastTLABRefill) {
1139           Register arr_size = rsi;
1140           Register t1       = rcx;  // must be rcx for use as shift count
1141           Register t2       = rdi;
1142           Label slow_path;
1143           assert_different_registers(length, klass, obj, arr_size, t1, t2);
1144 
1145           // check that array length is small enough for fast path.
1146           __ cmpl(length, C1_MacroAssembler::max_array_allocation_length);
1147           __ jcc(Assembler::above, slow_path);
1148 
1149           // if we got here then the TLAB allocation failed, so try
1150           // refilling the TLAB or allocating directly from eden.
1151           Label retry_tlab, try_eden;
1152           __ tlab_refill(retry_tlab, try_eden, slow_path); // preserves rbx, & rdx
1153 
1154           __ bind(retry_tlab);
1155 
1156           // get the allocation size: round_up(hdr + length << (layout_helper & 0x1F))
1157           // since size is postive movl does right thing on 64bit
1158           __ movl(t1, Address(klass, klassOopDesc::header_size() * HeapWordSize + Klass::layout_helper_offset_in_bytes()));
1159           // since size is postive movl does right thing on 64bit
1160           __ movl(arr_size, length);
1161           assert(t1 == rcx, "fixed register usage");
1162           __ shlptr(arr_size /* by t1=rcx, mod 32 */);
1163           __ shrptr(t1, Klass::_lh_header_size_shift);
1164           __ andptr(t1, Klass::_lh_header_size_mask);
1165           __ addptr(arr_size, t1);
1166           __ addptr(arr_size, MinObjAlignmentInBytesMask); // align up
1167           __ andptr(arr_size, ~MinObjAlignmentInBytesMask);
1168 
1169           __ tlab_allocate(obj, arr_size, 0, t1, t2, slow_path);  // preserves arr_size
1170 
1171           __ initialize_header(obj, klass, length, t1, t2);
1172           __ movb(t1, Address(klass, klassOopDesc::header_size() * HeapWordSize + Klass::layout_helper_offset_in_bytes() + (Klass::_lh_header_size_shift / BitsPerByte)));
1173           assert(Klass::_lh_header_size_shift % BitsPerByte == 0, "bytewise");
1174           assert(Klass::_lh_header_size_mask <= 0xFF, "bytewise");
1175           __ andptr(t1, Klass::_lh_header_size_mask);
1176           __ subptr(arr_size, t1);  // body length
1177           __ addptr(t1, obj);       // body start
1178           __ initialize_body(t1, arr_size, 0, t2);
1179           __ verify_oop(obj);
1180           __ ret(0);
1181 
1182           __ bind(try_eden);
1183           // get the allocation size: round_up(hdr + length << (layout_helper & 0x1F))
1184           // since size is postive movl does right thing on 64bit
1185           __ movl(t1, Address(klass, klassOopDesc::header_size() * HeapWordSize + Klass::layout_helper_offset_in_bytes()));
1186           // since size is postive movl does right thing on 64bit
1187           __ movl(arr_size, length);
1188           assert(t1 == rcx, "fixed register usage");
1189           __ shlptr(arr_size /* by t1=rcx, mod 32 */);
1190           __ shrptr(t1, Klass::_lh_header_size_shift);
1191           __ andptr(t1, Klass::_lh_header_size_mask);
1192           __ addptr(arr_size, t1);
1193           __ addptr(arr_size, MinObjAlignmentInBytesMask); // align up
1194           __ andptr(arr_size, ~MinObjAlignmentInBytesMask);
1195 
1196           __ eden_allocate(obj, arr_size, 0, t1, slow_path);  // preserves arr_size
1197 
1198           __ initialize_header(obj, klass, length, t1, t2);
1199           __ movb(t1, Address(klass, klassOopDesc::header_size() * HeapWordSize + Klass::layout_helper_offset_in_bytes() + (Klass::_lh_header_size_shift / BitsPerByte)));
1200           assert(Klass::_lh_header_size_shift % BitsPerByte == 0, "bytewise");
1201           assert(Klass::_lh_header_size_mask <= 0xFF, "bytewise");
1202           __ andptr(t1, Klass::_lh_header_size_mask);
1203           __ subptr(arr_size, t1);  // body length
1204           __ addptr(t1, obj);       // body start
1205           __ initialize_body(t1, arr_size, 0, t2);
1206           __ verify_oop(obj);
1207           __ ret(0);
1208 
1209           __ bind(slow_path);
1210         }
1211 
1212         __ enter();
1213         OopMap* map = save_live_registers(sasm, 3);
1214         int call_offset;
1215         if (id == new_type_array_id) {
1216           call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_type_array), klass, length);
1217         } else {
1218           call_offset = __ call_RT(obj, noreg, CAST_FROM_FN_PTR(address, new_object_array), klass, length);
1219         }
1220 
1221         oop_maps = new OopMapSet();
1222         oop_maps->add_gc_map(call_offset, map);
1223         restore_live_registers_except_rax(sasm);
1224 


1234       { StubFrame f(sasm, "new_multi_array", dont_gc_arguments);
1235         // rax,: klass
1236         // rbx,: rank
1237         // rcx: address of 1st dimension
1238         OopMap* map = save_live_registers(sasm, 4);
1239         int call_offset = __ call_RT(rax, noreg, CAST_FROM_FN_PTR(address, new_multi_array), rax, rbx, rcx);
1240 
1241         oop_maps = new OopMapSet();
1242         oop_maps->add_gc_map(call_offset, map);
1243         restore_live_registers_except_rax(sasm);
1244 
1245         // rax,: new multi array
1246         __ verify_oop(rax);
1247       }
1248       break;
1249 
1250     case register_finalizer_id:
1251       {
1252         __ set_info("register_finalizer", dont_gc_arguments);
1253 
1254         // This is called via call_runtime so the arguments
1255         // will be place in C abi locations
1256 
1257 #ifdef _LP64
1258         __ verify_oop(c_rarg0);
1259         __ mov(rax, c_rarg0);
1260 #else
1261         // The object is passed on the stack and we haven't pushed a
1262         // frame yet so it's one work away from top of stack.
1263         __ movptr(rax, Address(rsp, 1 * BytesPerWord));
1264         __ verify_oop(rax);
1265 #endif // _LP64
1266 
1267         // load the klass and check the has finalizer flag
1268         Label register_finalizer;
1269         Register t = rsi;
1270         __ movptr(t, Address(rax, oopDesc::klass_offset_in_bytes()));
1271         __ movl(t, Address(t, Klass::access_flags_offset_in_bytes() + sizeof(oopDesc)));
1272         __ testl(t, JVM_ACC_HAS_FINALIZER);
1273         __ jcc(Assembler::notZero, register_finalizer);
1274         __ ret(0);
1275 
1276         __ bind(register_finalizer);
1277         __ enter();
1278         OopMap* oop_map = save_live_registers(sasm, 2 /*num_rt_args */);
1279         int call_offset = __ call_RT(noreg, noreg,
1280                                      CAST_FROM_FN_PTR(address, SharedRuntime::register_finalizer), rax);
1281         oop_maps = new OopMapSet();
1282         oop_maps->add_gc_map(call_offset, oop_map);
1283 
1284         // Now restore all the live registers
1285         restore_live_registers(sasm);
1286 
1287         __ leave();
1288         __ ret(0);
1289       }
1290       break;


1338         //     + 1: return address
1339         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_array_store_exception), false);
1340       }
1341       break;
1342 
1343     case throw_class_cast_exception_id:
1344       { StubFrame f(sasm, "throw_class_cast_exception", dont_gc_arguments);
1345         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_class_cast_exception), true);
1346       }
1347       break;
1348 
1349     case throw_incompatible_class_change_error_id:
1350       { StubFrame f(sasm, "throw_incompatible_class_cast_exception", dont_gc_arguments);
1351         oop_maps = generate_exception_throw(sasm, CAST_FROM_FN_PTR(address, throw_incompatible_class_change_error), false);
1352       }
1353       break;
1354 
1355     case slow_subtype_check_id:
1356       {
1357         enum layout {
1358           rax_off, SLOT2(raxH_off)
1359           rcx_off, SLOT2(rcxH_off)
1360           rsi_off, SLOT2(rsiH_off)
1361           rdi_off, SLOT2(rdiH_off)
1362           // saved_rbp_off, SLOT2(saved_rbpH_off)
1363           return_off, SLOT2(returnH_off)
1364           sub_off, SLOT2(subH_off)
1365           super_off, SLOT2(superH_off)
1366           framesize
1367         };
1368 
1369         __ set_info("slow_subtype_check", dont_gc_arguments);
1370         __ push(rdi);
1371         __ push(rsi);
1372         __ push(rcx);
1373         __ push(rax);
1374 
1375         // This is called by pushing args and not with C abi
1376         __ movptr(rsi, Address(rsp, (super_off) * VMRegImpl::stack_slot_size)); // super
1377         __ movptr(rax, Address(rsp, (sub_off  ) * VMRegImpl::stack_slot_size)); // sub
1378 
1379         __ movptr(rdi,Address(rsi,sizeof(oopDesc) + Klass::secondary_supers_offset_in_bytes()));
1380         // since size is postive movl does right thing on 64bit
1381         __ movl(rcx, Address(rdi, arrayOopDesc::length_offset_in_bytes()));
1382         __ addptr(rdi, arrayOopDesc::base_offset_in_bytes(T_OBJECT));
1383 
1384         Label miss;
1385         __ repne_scan();
1386         __ jcc(Assembler::notEqual, miss);
1387         __ movptr(Address(rsi,sizeof(oopDesc) + Klass::secondary_super_cache_offset_in_bytes()), rax);
1388         __ movptr(Address(rsp, (super_off) * VMRegImpl::stack_slot_size), 1); // result
1389         __ pop(rax);
1390         __ pop(rcx);
1391         __ pop(rsi);
1392         __ pop(rdi);
1393         __ ret(0);
1394 
1395         __ bind(miss);
1396         __ movptr(Address(rsp, (super_off) * VMRegImpl::stack_slot_size), 0); // result
1397         __ pop(rax);
1398         __ pop(rcx);
1399         __ pop(rsi);
1400         __ pop(rdi);
1401         __ ret(0);
1402       }
1403       break;
1404 
1405     case monitorenter_nofpu_id:
1406       save_fpu_registers = false;
1407       // fall through
1408     case monitorenter_id:
1409       {
1410         StubFrame f(sasm, "monitorenter", dont_gc_arguments);
1411         OopMap* map = save_live_registers(sasm, 3, save_fpu_registers);
1412 
1413         // Called with store_parameter and not C abi
1414 
1415         f.load_argument(1, rax); // rax,: object
1416         f.load_argument(0, rbx); // rbx,: lock address
1417 
1418         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorenter), rax, rbx);
1419 
1420         oop_maps = new OopMapSet();
1421         oop_maps->add_gc_map(call_offset, map);
1422         restore_live_registers(sasm, save_fpu_registers);
1423       }
1424       break;
1425 
1426     case monitorexit_nofpu_id:
1427       save_fpu_registers = false;
1428       // fall through
1429     case monitorexit_id:
1430       {
1431         StubFrame f(sasm, "monitorexit", dont_gc_arguments);
1432         OopMap* map = save_live_registers(sasm, 2, save_fpu_registers);
1433 
1434         // Called with store_parameter and not C abi
1435 
1436         f.load_argument(0, rax); // rax,: lock address
1437 
1438         // note: really a leaf routine but must setup last java sp
1439         //       => use call_RT for now (speed can be improved by
1440         //       doing last java sp setup manually)
1441         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, monitorexit), rax);
1442 
1443         oop_maps = new OopMapSet();
1444         oop_maps->add_gc_map(call_offset, map);
1445         restore_live_registers(sasm, save_fpu_registers);
1446 
1447       }
1448       break;
1449 
1450     case access_field_patching_id:
1451       { StubFrame f(sasm, "access_field_patching", dont_gc_arguments);
1452         // we should set up register map
1453         oop_maps = generate_patching(sasm, CAST_FROM_FN_PTR(address, access_field_patching));
1454       }
1455       break;


1464     case jvmti_exception_throw_id:
1465       { // rax,: exception oop
1466         StubFrame f(sasm, "jvmti_exception_throw", dont_gc_arguments);
1467         // Preserve all registers across this potentially blocking call
1468         const int num_rt_args = 2;  // thread, exception oop
1469         OopMap* map = save_live_registers(sasm, num_rt_args);
1470         int call_offset = __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, Runtime1::post_jvmti_exception_throw), rax);
1471         oop_maps = new OopMapSet();
1472         oop_maps->add_gc_map(call_offset, map);
1473         restore_live_registers(sasm);
1474       }
1475       break;
1476 
1477     case dtrace_object_alloc_id:
1478       { // rax,: object
1479         StubFrame f(sasm, "dtrace_object_alloc", dont_gc_arguments);
1480         // we can't gc here so skip the oopmap but make sure that all
1481         // the live registers get saved.
1482         save_live_registers(sasm, 1);
1483 
1484         __ NOT_LP64(push(rax)) LP64_ONLY(mov(c_rarg0, rax));
1485         __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::dtrace_object_alloc)));
1486         NOT_LP64(__ pop(rax));
1487 
1488         restore_live_registers(sasm);
1489       }
1490       break;
1491 
1492     case fpu2long_stub_id:
1493       {
1494         // rax, and rdx are destroyed, but should be free since the result is returned there
1495         // preserve rsi,ecx
1496         __ push(rsi);
1497         __ push(rcx);
1498         LP64_ONLY(__ push(rdx);)
1499 
1500         // check for NaN
1501         Label return0, do_return, return_min_jlong, do_convert;
1502 
1503         Address value_high_word(rsp, wordSize + 4);
1504         Address value_low_word(rsp, wordSize);
1505         Address result_high_word(rsp, 3*wordSize + 4);
1506         Address result_low_word(rsp, 3*wordSize);
1507 
1508         __ subptr(rsp, 32);                    // more than enough on 32bit
1509         __ fst_d(value_low_word);
1510         __ movl(rax, value_high_word);
1511         __ andl(rax, 0x7ff00000);
1512         __ cmpl(rax, 0x7ff00000);
1513         __ jcc(Assembler::notEqual, do_convert);
1514         __ movl(rax, value_high_word);
1515         __ andl(rax, 0xfffff);
1516         __ orl(rax, value_low_word);
1517         __ jcc(Assembler::notZero, return0);
1518 
1519         __ bind(do_convert);
1520         __ fnstcw(Address(rsp, 0));
1521         __ movzwl(rax, Address(rsp, 0));
1522         __ orl(rax, 0xc00);
1523         __ movw(Address(rsp, 2), rax);
1524         __ fldcw(Address(rsp, 2));
1525         __ fwait();
1526         __ fistp_d(result_low_word);
1527         __ fldcw(Address(rsp, 0));
1528         __ fwait();
1529         // This gets the entire long in rax on 64bit
1530         __ movptr(rax, result_low_word);
1531         // testing of high bits
1532         __ movl(rdx, result_high_word);
1533         __ mov(rcx, rax);
1534         // What the heck is the point of the next instruction???
1535         __ xorl(rcx, 0x0);
1536         __ movl(rsi, 0x80000000);
1537         __ xorl(rsi, rdx);
1538         __ orl(rcx, rsi);
1539         __ jcc(Assembler::notEqual, do_return);
1540         __ fldz();
1541         __ fcomp_d(value_low_word);
1542         __ fnstsw_ax();
1543 #ifdef _LP64
1544         __ testl(rax, 0x4100);  // ZF & CF == 0
1545         __ jcc(Assembler::equal, return_min_jlong);
1546 #else
1547         __ sahf();
1548         __ jcc(Assembler::above, return_min_jlong);
1549 #endif // _LP64
1550         // return max_jlong
1551 #ifndef _LP64
1552         __ movl(rdx, 0x7fffffff);
1553         __ movl(rax, 0xffffffff);
1554 #else
1555         __ mov64(rax, CONST64(0x7fffffffffffffff));
1556 #endif // _LP64
1557         __ jmp(do_return);
1558 
1559         __ bind(return_min_jlong);
1560 #ifndef _LP64
1561         __ movl(rdx, 0x80000000);
1562         __ xorl(rax, rax);
1563 #else
1564         __ mov64(rax, CONST64(0x8000000000000000));
1565 #endif // _LP64
1566         __ jmp(do_return);
1567 
1568         __ bind(return0);
1569         __ fpop();
1570 #ifndef _LP64
1571         __ xorptr(rdx,rdx);
1572         __ xorptr(rax,rax);
1573 #else
1574         __ xorptr(rax, rax);
1575 #endif // _LP64
1576 
1577         __ bind(do_return);
1578         __ addptr(rsp, 32);
1579         LP64_ONLY(__ pop(rdx);)
1580         __ pop(rcx);
1581         __ pop(rsi);
1582         __ ret(0);
1583       }
1584       break;
1585 
1586 #ifndef SERIALGC
1587     case g1_pre_barrier_slow_id:
1588       {
1589         StubFrame f(sasm, "g1_pre_barrier", dont_gc_arguments);
1590         // arg0 : previous value of memory
1591 
1592         BarrierSet* bs = Universe::heap()->barrier_set();
1593         if (bs->kind() != BarrierSet::G1SATBCTLogging) {
1594           __ movptr(rax, (int)id);
1595           __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, unimplemented_entry), rax);
1596           __ should_not_reach_here();
1597           break;
1598         }
1599 
1600         __ push(rax);
1601         __ push(rdx);
1602 
1603         const Register pre_val = rax;
1604         const Register thread = NOT_LP64(rax) LP64_ONLY(r15_thread);
1605         const Register tmp = rdx;
1606 
1607         NOT_LP64(__ get_thread(thread);)
1608 
1609         Address in_progress(thread, in_bytes(JavaThread::satb_mark_queue_offset() +
1610                                              PtrQueue::byte_offset_of_active()));
1611 
1612         Address queue_index(thread, in_bytes(JavaThread::satb_mark_queue_offset() +
1613                                              PtrQueue::byte_offset_of_index()));
1614         Address buffer(thread, in_bytes(JavaThread::satb_mark_queue_offset() +
1615                                         PtrQueue::byte_offset_of_buf()));
1616 
1617 
1618         Label done;
1619         Label runtime;
1620 
1621         // Can we store original value in the thread's buffer?
1622 
1623         LP64_ONLY(__ movslq(tmp, queue_index);)
1624 #ifdef _LP64
1625         __ cmpq(tmp, 0);
1626 #else
1627         __ cmpl(queue_index, 0);
1628 #endif
1629         __ jcc(Assembler::equal, runtime);
1630 #ifdef _LP64
1631         __ subq(tmp, wordSize);
1632         __ movl(queue_index, tmp);
1633         __ addq(tmp, buffer);
1634 #else
1635         __ subl(queue_index, wordSize);
1636         __ movl(tmp, buffer);
1637         __ addl(tmp, queue_index);
1638 #endif
1639 
1640         // prev_val (rax)
1641         f.load_argument(0, pre_val);
1642         __ movptr(Address(tmp, 0), pre_val);
1643         __ jmp(done);
1644 
1645         __ bind(runtime);
1646         // load the pre-value
1647         __ push(rcx);
1648         f.load_argument(0, rcx);
1649         __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::g1_wb_pre), rcx, thread);
1650         __ pop(rcx);
1651 
1652         __ bind(done);
1653         __ pop(rdx);
1654         __ pop(rax);
1655       }
1656       break;
1657 
1658     case g1_post_barrier_slow_id:
1659       {
1660         StubFrame f(sasm, "g1_post_barrier", dont_gc_arguments);
1661 
1662 
1663         // arg0: store_address
1664         Address store_addr(rbp, 2*BytesPerWord);
1665 
1666         BarrierSet* bs = Universe::heap()->barrier_set();
1667         CardTableModRefBS* ct = (CardTableModRefBS*)bs;
1668         Label done;
1669         Label runtime;
1670 
1671         // At this point we know new_value is non-NULL and the new_value crosses regsion.
1672         // Must check to see if card is already dirty
1673 
1674         const Register thread = NOT_LP64(rax) LP64_ONLY(r15_thread);
1675 
1676         Address queue_index(thread, in_bytes(JavaThread::dirty_card_queue_offset() +
1677                                              PtrQueue::byte_offset_of_index()));
1678         Address buffer(thread, in_bytes(JavaThread::dirty_card_queue_offset() +
1679                                         PtrQueue::byte_offset_of_buf()));
1680 
1681         __ push(rax);
1682         __ push(rdx);
1683 
1684         NOT_LP64(__ get_thread(thread);)
1685         ExternalAddress cardtable((address)ct->byte_map_base);
1686         assert(sizeof(*ct->byte_map_base) == sizeof(jbyte), "adjust this code");
1687 
1688         const Register card_addr = rdx;
1689 #ifdef _LP64
1690         const Register tmp = rscratch1;
1691         f.load_argument(0, card_addr);
1692         __ shrq(card_addr, CardTableModRefBS::card_shift);
1693         __ lea(tmp, cardtable);
1694         // get the address of the card
1695         __ addq(card_addr, tmp);
1696 #else
1697         const Register card_index = rdx;
1698         f.load_argument(0, card_index);
1699         __ shrl(card_index, CardTableModRefBS::card_shift);
1700 
1701         Address index(noreg, card_index, Address::times_1);
1702         __ leal(card_addr, __ as_Address(ArrayAddress(cardtable, index)));
1703 #endif
1704 
1705         __ cmpb(Address(card_addr, 0), 0);
1706         __ jcc(Assembler::equal, done);
1707 
1708         // storing region crossing non-NULL, card is clean.
1709         // dirty card and log.
1710 
1711         __ movb(Address(card_addr, 0), 0);
1712 
1713         __ cmpl(queue_index, 0);
1714         __ jcc(Assembler::equal, runtime);
1715         __ subl(queue_index, wordSize);
1716 
1717         const Register buffer_addr = rbx;
1718         __ push(rbx);
1719 
1720         __ movptr(buffer_addr, buffer);
1721 
1722 #ifdef _LP64
1723         __ movslq(rscratch1, queue_index);
1724         __ addptr(buffer_addr, rscratch1);
1725 #else
1726         __ addptr(buffer_addr, queue_index);
1727 #endif
1728         __ movptr(Address(buffer_addr, 0), card_addr);
1729 
1730         __ pop(rbx);
1731         __ jmp(done);
1732 
1733         __ bind(runtime);
1734         NOT_LP64(__ push(rcx);)
1735         __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::g1_wb_post), card_addr, thread);
1736         NOT_LP64(__ pop(rcx);)
1737 
1738         __ bind(done);
1739         __ pop(rdx);
1740         __ pop(rax);
1741 
1742       }
1743       break;
1744 #endif // !SERIALGC
1745 
1746     default:
1747       { StubFrame f(sasm, "unimplemented entry", dont_gc_arguments);
1748         __ movptr(rax, (int)id);
1749         __ call_RT(noreg, noreg, CAST_FROM_FN_PTR(address, unimplemented_entry), rax);
1750         __ should_not_reach_here();
1751       }
1752       break;
1753   }
1754   return oop_maps;
1755 }
1756 
1757 #undef __