1 /*
   2  * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/assembler.hpp"
  27 #include "assembler_x86.inline.hpp"
  28 #include "interpreter/interpreter.hpp"
  29 #include "nativeInst_x86.hpp"
  30 #include "oops/instanceOop.hpp"
  31 #include "oops/methodOop.hpp"
  32 #include "oops/objArrayKlass.hpp"
  33 #include "oops/oop.inline.hpp"
  34 #include "prims/methodHandles.hpp"
  35 #include "runtime/frame.inline.hpp"
  36 #include "runtime/handles.inline.hpp"
  37 #include "runtime/sharedRuntime.hpp"
  38 #include "runtime/stubCodeGenerator.hpp"
  39 #include "runtime/stubRoutines.hpp"
  40 #include "utilities/top.hpp"
  41 #ifdef TARGET_OS_FAMILY_linux
  42 # include "thread_linux.inline.hpp"
  43 #endif
  44 #ifdef TARGET_OS_FAMILY_solaris
  45 # include "thread_solaris.inline.hpp"
  46 #endif
  47 #ifdef TARGET_OS_FAMILY_windows
  48 # include "thread_windows.inline.hpp"
  49 #endif
  50 #ifdef COMPILER2
  51 #include "opto/runtime.hpp"
  52 #endif
  53 
  54 // Declaration and definition of StubGenerator (no .hpp file).
  55 // For a more detailed description of the stub routine structure
  56 // see the comment in stubRoutines.hpp
  57 
  58 #define __ _masm->
  59 #define a__ ((Assembler*)_masm)->
  60 
  61 #ifdef PRODUCT
  62 #define BLOCK_COMMENT(str) /* nothing */
  63 #else
  64 #define BLOCK_COMMENT(str) __ block_comment(str)
  65 #endif
  66 
  67 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
  68 
  69 const int MXCSR_MASK  = 0xFFC0;  // Mask out any pending exceptions
  70 const int FPU_CNTRL_WRD_MASK = 0xFFFF;
  71 
  72 // -------------------------------------------------------------------------------------------------------------------------
  73 // Stub Code definitions
  74 
  75 static address handle_unsafe_access() {
  76   JavaThread* thread = JavaThread::current();
  77   address pc  = thread->saved_exception_pc();
  78   // pc is the instruction which we must emulate
  79   // doing a no-op is fine:  return garbage from the load
  80   // therefore, compute npc
  81   address npc = Assembler::locate_next_instruction(pc);
  82 
  83   // request an async exception
  84   thread->set_pending_unsafe_access_error();
  85 
  86   // return address of next instruction to execute
  87   return npc;
  88 }
  89 
  90 class StubGenerator: public StubCodeGenerator {
  91  private:
  92 
  93 #ifdef PRODUCT
  94 #define inc_counter_np(counter) (0)
  95 #else
  96   void inc_counter_np_(int& counter) {
  97     __ incrementl(ExternalAddress((address)&counter));
  98   }
  99 #define inc_counter_np(counter) \
 100   BLOCK_COMMENT("inc_counter " #counter); \
 101   inc_counter_np_(counter);
 102 #endif //PRODUCT
 103 
 104   void inc_copy_counter_np(BasicType t) {
 105 #ifndef PRODUCT
 106     switch (t) {
 107     case T_BYTE:    inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); return;
 108     case T_SHORT:   inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); return;
 109     case T_INT:     inc_counter_np(SharedRuntime::_jint_array_copy_ctr); return;
 110     case T_LONG:    inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); return;
 111     case T_OBJECT:  inc_counter_np(SharedRuntime::_oop_array_copy_ctr); return;
 112     }
 113     ShouldNotReachHere();
 114 #endif //PRODUCT
 115   }
 116 
 117   //------------------------------------------------------------------------------------------------------------------------
 118   // Call stubs are used to call Java from C
 119   //
 120   //    [ return_from_Java     ] <--- rsp
 121   //    [ argument word n      ]
 122   //      ...
 123   // -N [ argument word 1      ]
 124   // -7 [ Possible padding for stack alignment ]
 125   // -6 [ Possible padding for stack alignment ]
 126   // -5 [ Possible padding for stack alignment ]
 127   // -4 [ mxcsr save           ] <--- rsp_after_call
 128   // -3 [ saved rbx,            ]
 129   // -2 [ saved rsi            ]
 130   // -1 [ saved rdi            ]
 131   //  0 [ saved rbp,            ] <--- rbp,
 132   //  1 [ return address       ]
 133   //  2 [ ptr. to call wrapper ]
 134   //  3 [ result               ]
 135   //  4 [ result_type          ]
 136   //  5 [ method               ]
 137   //  6 [ entry_point          ]
 138   //  7 [ parameters           ]
 139   //  8 [ parameter_size       ]
 140   //  9 [ thread               ]
 141 
 142 
 143   address generate_call_stub(address& return_address) {
 144     StubCodeMark mark(this, "StubRoutines", "call_stub");
 145     address start = __ pc();
 146 
 147     // stub code parameters / addresses
 148     assert(frame::entry_frame_call_wrapper_offset == 2, "adjust this code");
 149     bool  sse_save = false;
 150     const Address rsp_after_call(rbp, -4 * wordSize); // same as in generate_catch_exception()!
 151     const int     locals_count_in_bytes  (4*wordSize);
 152     const Address mxcsr_save    (rbp, -4 * wordSize);
 153     const Address saved_rbx     (rbp, -3 * wordSize);
 154     const Address saved_rsi     (rbp, -2 * wordSize);
 155     const Address saved_rdi     (rbp, -1 * wordSize);
 156     const Address result        (rbp,  3 * wordSize);
 157     const Address result_type   (rbp,  4 * wordSize);
 158     const Address method        (rbp,  5 * wordSize);
 159     const Address entry_point   (rbp,  6 * wordSize);
 160     const Address parameters    (rbp,  7 * wordSize);
 161     const Address parameter_size(rbp,  8 * wordSize);
 162     const Address thread        (rbp,  9 * wordSize); // same as in generate_catch_exception()!
 163     sse_save =  UseSSE > 0;
 164 
 165     // stub code
 166     __ enter();
 167     __ movptr(rcx, parameter_size);              // parameter counter
 168     __ shlptr(rcx, Interpreter::logStackElementSize); // convert parameter count to bytes
 169     __ addptr(rcx, locals_count_in_bytes);       // reserve space for register saves
 170     __ subptr(rsp, rcx);
 171     __ andptr(rsp, -(StackAlignmentInBytes));    // Align stack
 172 
 173     // save rdi, rsi, & rbx, according to C calling conventions
 174     __ movptr(saved_rdi, rdi);
 175     __ movptr(saved_rsi, rsi);
 176     __ movptr(saved_rbx, rbx);
 177     // save and initialize %mxcsr
 178     if (sse_save) {
 179       Label skip_ldmx;
 180       __ stmxcsr(mxcsr_save);
 181       __ movl(rax, mxcsr_save);
 182       __ andl(rax, MXCSR_MASK);    // Only check control and mask bits
 183       ExternalAddress mxcsr_std(StubRoutines::addr_mxcsr_std());
 184       __ cmp32(rax, mxcsr_std);
 185       __ jcc(Assembler::equal, skip_ldmx);
 186       __ ldmxcsr(mxcsr_std);
 187       __ bind(skip_ldmx);
 188     }
 189 
 190     // make sure the control word is correct.
 191     __ fldcw(ExternalAddress(StubRoutines::addr_fpu_cntrl_wrd_std()));
 192 
 193 #ifdef ASSERT
 194     // make sure we have no pending exceptions
 195     { Label L;
 196       __ movptr(rcx, thread);
 197       __ cmpptr(Address(rcx, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 198       __ jcc(Assembler::equal, L);
 199       __ stop("StubRoutines::call_stub: entered with pending exception");
 200       __ bind(L);
 201     }
 202 #endif
 203 
 204     // pass parameters if any
 205     BLOCK_COMMENT("pass parameters if any");
 206     Label parameters_done;
 207     __ movl(rcx, parameter_size);  // parameter counter
 208     __ testl(rcx, rcx);
 209     __ jcc(Assembler::zero, parameters_done);
 210 
 211     // parameter passing loop
 212 
 213     Label loop;
 214     // Copy Java parameters in reverse order (receiver last)
 215     // Note that the argument order is inverted in the process
 216     // source is rdx[rcx: N-1..0]
 217     // dest   is rsp[rbx: 0..N-1]
 218 
 219     __ movptr(rdx, parameters);          // parameter pointer
 220     __ xorptr(rbx, rbx);
 221 
 222     __ BIND(loop);
 223 
 224     // get parameter
 225     __ movptr(rax, Address(rdx, rcx, Interpreter::stackElementScale(), -wordSize));
 226     __ movptr(Address(rsp, rbx, Interpreter::stackElementScale(),
 227                     Interpreter::expr_offset_in_bytes(0)), rax);          // store parameter
 228     __ increment(rbx);
 229     __ decrement(rcx);
 230     __ jcc(Assembler::notZero, loop);
 231 
 232     // call Java function
 233     __ BIND(parameters_done);
 234     __ movptr(rbx, method);           // get methodOop
 235     __ movptr(rax, entry_point);      // get entry_point
 236     __ mov(rsi, rsp);                 // set sender sp
 237     BLOCK_COMMENT("call Java function");
 238     __ call(rax);
 239 
 240     BLOCK_COMMENT("call_stub_return_address:");
 241     return_address = __ pc();
 242 
 243     Label common_return;
 244 
 245     __ BIND(common_return);
 246 
 247     // store result depending on type
 248     // (everything that is not T_LONG, T_FLOAT or T_DOUBLE is treated as T_INT)
 249     __ movptr(rdi, result);
 250     Label is_long, is_float, is_double, exit;
 251     __ movl(rsi, result_type);
 252     __ cmpl(rsi, T_LONG);
 253     __ jcc(Assembler::equal, is_long);
 254     __ cmpl(rsi, T_FLOAT);
 255     __ jcc(Assembler::equal, is_float);
 256     __ cmpl(rsi, T_DOUBLE);
 257     __ jcc(Assembler::equal, is_double);
 258 
 259     // handle T_INT case
 260     __ movl(Address(rdi, 0), rax);
 261     __ BIND(exit);
 262 
 263     // check that FPU stack is empty
 264     __ verify_FPU(0, "generate_call_stub");
 265 
 266     // pop parameters
 267     __ lea(rsp, rsp_after_call);
 268 
 269     // restore %mxcsr
 270     if (sse_save) {
 271       __ ldmxcsr(mxcsr_save);
 272     }
 273 
 274     // restore rdi, rsi and rbx,
 275     __ movptr(rbx, saved_rbx);
 276     __ movptr(rsi, saved_rsi);
 277     __ movptr(rdi, saved_rdi);
 278     __ addptr(rsp, 4*wordSize);
 279 
 280     // return
 281     __ pop(rbp);
 282     __ ret(0);
 283 
 284     // handle return types different from T_INT
 285     __ BIND(is_long);
 286     __ movl(Address(rdi, 0 * wordSize), rax);
 287     __ movl(Address(rdi, 1 * wordSize), rdx);
 288     __ jmp(exit);
 289 
 290     __ BIND(is_float);
 291     // interpreter uses xmm0 for return values
 292     if (UseSSE >= 1) {
 293       __ movflt(Address(rdi, 0), xmm0);
 294     } else {
 295       __ fstp_s(Address(rdi, 0));
 296     }
 297     __ jmp(exit);
 298 
 299     __ BIND(is_double);
 300     // interpreter uses xmm0 for return values
 301     if (UseSSE >= 2) {
 302       __ movdbl(Address(rdi, 0), xmm0);
 303     } else {
 304       __ fstp_d(Address(rdi, 0));
 305     }
 306     __ jmp(exit);
 307 
 308     // If we call compiled code directly from the call stub we will
 309     // need to adjust the return back to the call stub to a specialized
 310     // piece of code that can handle compiled results and cleaning the fpu
 311     // stack. compiled code will be set to return here instead of the
 312     // return above that handles interpreter returns.
 313 
 314     BLOCK_COMMENT("call_stub_compiled_return:");
 315     StubRoutines::x86::set_call_stub_compiled_return( __ pc());
 316 
 317 #ifdef COMPILER2
 318     if (UseSSE >= 2) {
 319       __ verify_FPU(0, "call_stub_compiled_return");
 320     } else {
 321       for (int i = 1; i < 8; i++) {
 322         __ ffree(i);
 323       }
 324 
 325       // UseSSE <= 1 so double result should be left on TOS
 326       __ movl(rsi, result_type);
 327       __ cmpl(rsi, T_DOUBLE);
 328       __ jcc(Assembler::equal, common_return);
 329       if (UseSSE == 0) {
 330         // UseSSE == 0 so float result should be left on TOS
 331         __ cmpl(rsi, T_FLOAT);
 332         __ jcc(Assembler::equal, common_return);
 333       }
 334       __ ffree(0);
 335     }
 336 #endif /* COMPILER2 */
 337     __ jmp(common_return);
 338 
 339     return start;
 340   }
 341 
 342 
 343   //------------------------------------------------------------------------------------------------------------------------
 344   // Return point for a Java call if there's an exception thrown in Java code.
 345   // The exception is caught and transformed into a pending exception stored in
 346   // JavaThread that can be tested from within the VM.
 347   //
 348   // Note: Usually the parameters are removed by the callee. In case of an exception
 349   //       crossing an activation frame boundary, that is not the case if the callee
 350   //       is compiled code => need to setup the rsp.
 351   //
 352   // rax,: exception oop
 353 
 354   address generate_catch_exception() {
 355     StubCodeMark mark(this, "StubRoutines", "catch_exception");
 356     const Address rsp_after_call(rbp, -4 * wordSize); // same as in generate_call_stub()!
 357     const Address thread        (rbp,  9 * wordSize); // same as in generate_call_stub()!
 358     address start = __ pc();
 359 
 360     // get thread directly
 361     __ movptr(rcx, thread);
 362 #ifdef ASSERT
 363     // verify that threads correspond
 364     { Label L;
 365       __ get_thread(rbx);
 366       __ cmpptr(rbx, rcx);
 367       __ jcc(Assembler::equal, L);
 368       __ stop("StubRoutines::catch_exception: threads must correspond");
 369       __ bind(L);
 370     }
 371 #endif
 372     // set pending exception
 373     __ verify_oop(rax);
 374     __ movptr(Address(rcx, Thread::pending_exception_offset()), rax          );
 375     __ lea(Address(rcx, Thread::exception_file_offset   ()),
 376            ExternalAddress((address)__FILE__));
 377     __ movl(Address(rcx, Thread::exception_line_offset   ()), __LINE__ );
 378     // complete return to VM
 379     assert(StubRoutines::_call_stub_return_address != NULL, "_call_stub_return_address must have been generated before");
 380     __ jump(RuntimeAddress(StubRoutines::_call_stub_return_address));
 381 
 382     return start;
 383   }
 384 
 385 
 386   //------------------------------------------------------------------------------------------------------------------------
 387   // Continuation point for runtime calls returning with a pending exception.
 388   // The pending exception check happened in the runtime or native call stub.
 389   // The pending exception in Thread is converted into a Java-level exception.
 390   //
 391   // Contract with Java-level exception handlers:
 392   // rax: exception
 393   // rdx: throwing pc
 394   //
 395   // NOTE: At entry of this stub, exception-pc must be on stack !!
 396 
 397   address generate_forward_exception() {
 398     StubCodeMark mark(this, "StubRoutines", "forward exception");
 399     address start = __ pc();
 400     const Register thread = rcx;
 401 
 402     // other registers used in this stub
 403     const Register exception_oop = rax;
 404     const Register handler_addr  = rbx;
 405     const Register exception_pc  = rdx;
 406 
 407     // Upon entry, the sp points to the return address returning into Java
 408     // (interpreted or compiled) code; i.e., the return address becomes the
 409     // throwing pc.
 410     //
 411     // Arguments pushed before the runtime call are still on the stack but
 412     // the exception handler will reset the stack pointer -> ignore them.
 413     // A potential result in registers can be ignored as well.
 414 
 415 #ifdef ASSERT
 416     // make sure this code is only executed if there is a pending exception
 417     { Label L;
 418       __ get_thread(thread);
 419       __ cmpptr(Address(thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 420       __ jcc(Assembler::notEqual, L);
 421       __ stop("StubRoutines::forward exception: no pending exception (1)");
 422       __ bind(L);
 423     }
 424 #endif
 425 
 426     // compute exception handler into rbx,
 427     __ get_thread(thread);
 428     __ movptr(exception_pc, Address(rsp, 0));
 429     BLOCK_COMMENT("call exception_handler_for_return_address");
 430     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), thread, exception_pc);
 431     __ mov(handler_addr, rax);
 432 
 433     // setup rax & rdx, remove return address & clear pending exception
 434     __ get_thread(thread);
 435     __ pop(exception_pc);
 436     __ movptr(exception_oop, Address(thread, Thread::pending_exception_offset()));
 437     __ movptr(Address(thread, Thread::pending_exception_offset()), NULL_WORD);
 438 
 439 #ifdef ASSERT
 440     // make sure exception is set
 441     { Label L;
 442       __ testptr(exception_oop, exception_oop);
 443       __ jcc(Assembler::notEqual, L);
 444       __ stop("StubRoutines::forward exception: no pending exception (2)");
 445       __ bind(L);
 446     }
 447 #endif
 448 
 449     // Verify that there is really a valid exception in RAX.
 450     __ verify_oop(exception_oop);
 451 
 452     // Restore SP from BP if the exception PC is a MethodHandle call site.
 453     __ cmpl(Address(thread, JavaThread::is_method_handle_return_offset()), 0);
 454     __ cmovptr(Assembler::notEqual, rsp, rbp);
 455 
 456     // continue at exception handler (return address removed)
 457     // rax: exception
 458     // rbx: exception handler
 459     // rdx: throwing pc
 460     __ jmp(handler_addr);
 461 
 462     return start;
 463   }
 464 
 465 
 466   //----------------------------------------------------------------------------------------------------
 467   // Support for jint Atomic::xchg(jint exchange_value, volatile jint* dest)
 468   //
 469   // xchg exists as far back as 8086, lock needed for MP only
 470   // Stack layout immediately after call:
 471   //
 472   // 0 [ret addr ] <--- rsp
 473   // 1 [  ex     ]
 474   // 2 [  dest   ]
 475   //
 476   // Result:   *dest <- ex, return (old *dest)
 477   //
 478   // Note: win32 does not currently use this code
 479 
 480   address generate_atomic_xchg() {
 481     StubCodeMark mark(this, "StubRoutines", "atomic_xchg");
 482     address start = __ pc();
 483 
 484     __ push(rdx);
 485     Address exchange(rsp, 2 * wordSize);
 486     Address dest_addr(rsp, 3 * wordSize);
 487     __ movl(rax, exchange);
 488     __ movptr(rdx, dest_addr);
 489     __ xchgl(rax, Address(rdx, 0));
 490     __ pop(rdx);
 491     __ ret(0);
 492 
 493     return start;
 494   }
 495 
 496   //----------------------------------------------------------------------------------------------------
 497   // Support for void verify_mxcsr()
 498   //
 499   // This routine is used with -Xcheck:jni to verify that native
 500   // JNI code does not return to Java code without restoring the
 501   // MXCSR register to our expected state.
 502 
 503 
 504   address generate_verify_mxcsr() {
 505     StubCodeMark mark(this, "StubRoutines", "verify_mxcsr");
 506     address start = __ pc();
 507 
 508     const Address mxcsr_save(rsp, 0);
 509 
 510     if (CheckJNICalls && UseSSE > 0 ) {
 511       Label ok_ret;
 512       ExternalAddress mxcsr_std(StubRoutines::addr_mxcsr_std());
 513       __ push(rax);
 514       __ subptr(rsp, wordSize);      // allocate a temp location
 515       __ stmxcsr(mxcsr_save);
 516       __ movl(rax, mxcsr_save);
 517       __ andl(rax, MXCSR_MASK);
 518       __ cmp32(rax, mxcsr_std);
 519       __ jcc(Assembler::equal, ok_ret);
 520 
 521       __ warn("MXCSR changed by native JNI code.");
 522 
 523       __ ldmxcsr(mxcsr_std);
 524 
 525       __ bind(ok_ret);
 526       __ addptr(rsp, wordSize);
 527       __ pop(rax);
 528     }
 529 
 530     __ ret(0);
 531 
 532     return start;
 533   }
 534 
 535 
 536   //---------------------------------------------------------------------------
 537   // Support for void verify_fpu_cntrl_wrd()
 538   //
 539   // This routine is used with -Xcheck:jni to verify that native
 540   // JNI code does not return to Java code without restoring the
 541   // FP control word to our expected state.
 542 
 543   address generate_verify_fpu_cntrl_wrd() {
 544     StubCodeMark mark(this, "StubRoutines", "verify_spcw");
 545     address start = __ pc();
 546 
 547     const Address fpu_cntrl_wrd_save(rsp, 0);
 548 
 549     if (CheckJNICalls) {
 550       Label ok_ret;
 551       __ push(rax);
 552       __ subptr(rsp, wordSize);      // allocate a temp location
 553       __ fnstcw(fpu_cntrl_wrd_save);
 554       __ movl(rax, fpu_cntrl_wrd_save);
 555       __ andl(rax, FPU_CNTRL_WRD_MASK);
 556       ExternalAddress fpu_std(StubRoutines::addr_fpu_cntrl_wrd_std());
 557       __ cmp32(rax, fpu_std);
 558       __ jcc(Assembler::equal, ok_ret);
 559 
 560       __ warn("Floating point control word changed by native JNI code.");
 561 
 562       __ fldcw(fpu_std);
 563 
 564       __ bind(ok_ret);
 565       __ addptr(rsp, wordSize);
 566       __ pop(rax);
 567     }
 568 
 569     __ ret(0);
 570 
 571     return start;
 572   }
 573 
 574   //---------------------------------------------------------------------------
 575   // Wrapper for slow-case handling of double-to-integer conversion
 576   // d2i or f2i fast case failed either because it is nan or because
 577   // of under/overflow.
 578   // Input:  FPU TOS: float value
 579   // Output: rax, (rdx): integer (long) result
 580 
 581   address generate_d2i_wrapper(BasicType t, address fcn) {
 582     StubCodeMark mark(this, "StubRoutines", "d2i_wrapper");
 583     address start = __ pc();
 584 
 585   // Capture info about frame layout
 586   enum layout { FPUState_off         = 0,
 587                 rbp_off              = FPUStateSizeInWords,
 588                 rdi_off,
 589                 rsi_off,
 590                 rcx_off,
 591                 rbx_off,
 592                 saved_argument_off,
 593                 saved_argument_off2, // 2nd half of double
 594                 framesize
 595   };
 596 
 597   assert(FPUStateSizeInWords == 27, "update stack layout");
 598 
 599     // Save outgoing argument to stack across push_FPU_state()
 600     __ subptr(rsp, wordSize * 2);
 601     __ fstp_d(Address(rsp, 0));
 602 
 603     // Save CPU & FPU state
 604     __ push(rbx);
 605     __ push(rcx);
 606     __ push(rsi);
 607     __ push(rdi);
 608     __ push(rbp);
 609     __ push_FPU_state();
 610 
 611     // push_FPU_state() resets the FP top of stack
 612     // Load original double into FP top of stack
 613     __ fld_d(Address(rsp, saved_argument_off * wordSize));
 614     // Store double into stack as outgoing argument
 615     __ subptr(rsp, wordSize*2);
 616     __ fst_d(Address(rsp, 0));
 617 
 618     // Prepare FPU for doing math in C-land
 619     __ empty_FPU_stack();
 620     // Call the C code to massage the double.  Result in EAX
 621     if (t == T_INT)
 622       { BLOCK_COMMENT("SharedRuntime::d2i"); }
 623     else if (t == T_LONG)
 624       { BLOCK_COMMENT("SharedRuntime::d2l"); }
 625     __ call_VM_leaf( fcn, 2 );
 626 
 627     // Restore CPU & FPU state
 628     __ pop_FPU_state();
 629     __ pop(rbp);
 630     __ pop(rdi);
 631     __ pop(rsi);
 632     __ pop(rcx);
 633     __ pop(rbx);
 634     __ addptr(rsp, wordSize * 2);
 635 
 636     __ ret(0);
 637 
 638     return start;
 639   }
 640 
 641 
 642   //---------------------------------------------------------------------------
 643   // The following routine generates a subroutine to throw an asynchronous
 644   // UnknownError when an unsafe access gets a fault that could not be
 645   // reasonably prevented by the programmer.  (Example: SIGBUS/OBJERR.)
 646   address generate_handler_for_unsafe_access() {
 647     StubCodeMark mark(this, "StubRoutines", "handler_for_unsafe_access");
 648     address start = __ pc();
 649 
 650     __ push(0);                       // hole for return address-to-be
 651     __ pusha();                       // push registers
 652     Address next_pc(rsp, RegisterImpl::number_of_registers * BytesPerWord);
 653     BLOCK_COMMENT("call handle_unsafe_access");
 654     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, handle_unsafe_access)));
 655     __ movptr(next_pc, rax);          // stuff next address
 656     __ popa();
 657     __ ret(0);                        // jump to next address
 658 
 659     return start;
 660   }
 661 
 662 
 663   //----------------------------------------------------------------------------------------------------
 664   // Non-destructive plausibility checks for oops
 665 
 666   address generate_verify_oop() {
 667     StubCodeMark mark(this, "StubRoutines", "verify_oop");
 668     address start = __ pc();
 669 
 670     // Incoming arguments on stack after saving rax,:
 671     //
 672     // [tos    ]: saved rdx
 673     // [tos + 1]: saved EFLAGS
 674     // [tos + 2]: return address
 675     // [tos + 3]: char* error message
 676     // [tos + 4]: oop   object to verify
 677     // [tos + 5]: saved rax, - saved by caller and bashed
 678 
 679     Label exit, error;
 680     __ pushf();
 681     __ incrementl(ExternalAddress((address) StubRoutines::verify_oop_count_addr()));
 682     __ push(rdx);                                // save rdx
 683     // make sure object is 'reasonable'
 684     __ movptr(rax, Address(rsp, 4 * wordSize));    // get object
 685     __ testptr(rax, rax);
 686     __ jcc(Assembler::zero, exit);               // if obj is NULL it is ok
 687 
 688     // Check if the oop is in the right area of memory
 689     const int oop_mask = Universe::verify_oop_mask();
 690     const int oop_bits = Universe::verify_oop_bits();
 691     __ mov(rdx, rax);
 692     __ andptr(rdx, oop_mask);
 693     __ cmpptr(rdx, oop_bits);
 694     __ jcc(Assembler::notZero, error);
 695 
 696     // make sure klass is 'reasonable'
 697     __ movptr(rax, Address(rax, oopDesc::klass_offset_in_bytes())); // get klass
 698     __ testptr(rax, rax);
 699     __ jcc(Assembler::zero, error);              // if klass is NULL it is broken
 700 
 701     // Check if the klass is in the right area of memory
 702     const int klass_mask = Universe::verify_klass_mask();
 703     const int klass_bits = Universe::verify_klass_bits();
 704     __ mov(rdx, rax);
 705     __ andptr(rdx, klass_mask);
 706     __ cmpptr(rdx, klass_bits);
 707     __ jcc(Assembler::notZero, error);
 708 
 709     // make sure klass' klass is 'reasonable'
 710     __ movptr(rax, Address(rax, oopDesc::klass_offset_in_bytes())); // get klass' klass
 711     __ testptr(rax, rax);
 712     __ jcc(Assembler::zero, error);              // if klass' klass is NULL it is broken
 713 
 714     __ mov(rdx, rax);
 715     __ andptr(rdx, klass_mask);
 716     __ cmpptr(rdx, klass_bits);
 717     __ jcc(Assembler::notZero, error);           // if klass not in right area
 718                                                  // of memory it is broken too.
 719 
 720     // return if everything seems ok
 721     __ bind(exit);
 722     __ movptr(rax, Address(rsp, 5 * wordSize));  // get saved rax, back
 723     __ pop(rdx);                                 // restore rdx
 724     __ popf();                                   // restore EFLAGS
 725     __ ret(3 * wordSize);                        // pop arguments
 726 
 727     // handle errors
 728     __ bind(error);
 729     __ movptr(rax, Address(rsp, 5 * wordSize));  // get saved rax, back
 730     __ pop(rdx);                                 // get saved rdx back
 731     __ popf();                                   // get saved EFLAGS off stack -- will be ignored
 732     __ pusha();                                  // push registers (eip = return address & msg are already pushed)
 733     BLOCK_COMMENT("call MacroAssembler::debug");
 734     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, MacroAssembler::debug32)));
 735     __ popa();
 736     __ ret(3 * wordSize);                        // pop arguments
 737     return start;
 738   }
 739 
 740   //
 741   //  Generate pre-barrier for array stores
 742   //
 743   //  Input:
 744   //     start   -  starting address
 745   //     count   -  element count
 746   void  gen_write_ref_array_pre_barrier(Register start, Register count) {
 747     assert_different_registers(start, count);
 748     BarrierSet* bs = Universe::heap()->barrier_set();
 749     switch (bs->kind()) {
 750       case BarrierSet::G1SATBCT:
 751       case BarrierSet::G1SATBCTLogging:
 752         {
 753           __ pusha();                      // push registers
 754           __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_pre),
 755                           start, count);
 756           __ popa();
 757         }
 758         break;
 759       case BarrierSet::CardTableModRef:
 760       case BarrierSet::CardTableExtension:
 761       case BarrierSet::ModRef:
 762         break;
 763       default      :
 764         ShouldNotReachHere();
 765 
 766     }
 767   }
 768 
 769 
 770   //
 771   // Generate a post-barrier for an array store
 772   //
 773   //     start    -  starting address
 774   //     count    -  element count
 775   //
 776   //  The two input registers are overwritten.
 777   //
 778   void  gen_write_ref_array_post_barrier(Register start, Register count) {
 779     BarrierSet* bs = Universe::heap()->barrier_set();
 780     assert_different_registers(start, count);
 781     switch (bs->kind()) {
 782       case BarrierSet::G1SATBCT:
 783       case BarrierSet::G1SATBCTLogging:
 784         {
 785           __ pusha();                      // push registers
 786           __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_post),
 787                           start, count);
 788           __ popa();
 789         }
 790         break;
 791 
 792       case BarrierSet::CardTableModRef:
 793       case BarrierSet::CardTableExtension:
 794         {
 795           CardTableModRefBS* ct = (CardTableModRefBS*)bs;
 796           assert(sizeof(*ct->byte_map_base) == sizeof(jbyte), "adjust this code");
 797 
 798           Label L_loop;
 799           const Register end = count;  // elements count; end == start+count-1
 800           assert_different_registers(start, end);
 801 
 802           __ lea(end,  Address(start, count, Address::times_ptr, -wordSize));
 803           __ shrptr(start, CardTableModRefBS::card_shift);
 804           __ shrptr(end,   CardTableModRefBS::card_shift);
 805           __ subptr(end, start); // end --> count
 806         __ BIND(L_loop);
 807           intptr_t disp = (intptr_t) ct->byte_map_base;
 808           Address cardtable(start, count, Address::times_1, disp);
 809           __ movb(cardtable, 0);
 810           __ decrement(count);
 811           __ jcc(Assembler::greaterEqual, L_loop);
 812         }
 813         break;
 814       case BarrierSet::ModRef:
 815         break;
 816       default      :
 817         ShouldNotReachHere();
 818 
 819     }
 820   }
 821 
 822 
 823   // Copy 64 bytes chunks
 824   //
 825   // Inputs:
 826   //   from        - source array address
 827   //   to_from     - destination array address - from
 828   //   qword_count - 8-bytes element count, negative
 829   //
 830   void xmm_copy_forward(Register from, Register to_from, Register qword_count) {
 831     assert( UseSSE >= 2, "supported cpu only" );
 832     Label L_copy_64_bytes_loop, L_copy_64_bytes, L_copy_8_bytes, L_exit;
 833     // Copy 64-byte chunks
 834     __ jmpb(L_copy_64_bytes);
 835     __ align(OptoLoopAlignment);
 836   __ BIND(L_copy_64_bytes_loop);
 837 
 838     if(UseUnalignedLoadStores) {
 839       __ movdqu(xmm0, Address(from, 0));
 840       __ movdqu(Address(from, to_from, Address::times_1, 0), xmm0);
 841       __ movdqu(xmm1, Address(from, 16));
 842       __ movdqu(Address(from, to_from, Address::times_1, 16), xmm1);
 843       __ movdqu(xmm2, Address(from, 32));
 844       __ movdqu(Address(from, to_from, Address::times_1, 32), xmm2);
 845       __ movdqu(xmm3, Address(from, 48));
 846       __ movdqu(Address(from, to_from, Address::times_1, 48), xmm3);
 847 
 848     } else {
 849       __ movq(xmm0, Address(from, 0));
 850       __ movq(Address(from, to_from, Address::times_1, 0), xmm0);
 851       __ movq(xmm1, Address(from, 8));
 852       __ movq(Address(from, to_from, Address::times_1, 8), xmm1);
 853       __ movq(xmm2, Address(from, 16));
 854       __ movq(Address(from, to_from, Address::times_1, 16), xmm2);
 855       __ movq(xmm3, Address(from, 24));
 856       __ movq(Address(from, to_from, Address::times_1, 24), xmm3);
 857       __ movq(xmm4, Address(from, 32));
 858       __ movq(Address(from, to_from, Address::times_1, 32), xmm4);
 859       __ movq(xmm5, Address(from, 40));
 860       __ movq(Address(from, to_from, Address::times_1, 40), xmm5);
 861       __ movq(xmm6, Address(from, 48));
 862       __ movq(Address(from, to_from, Address::times_1, 48), xmm6);
 863       __ movq(xmm7, Address(from, 56));
 864       __ movq(Address(from, to_from, Address::times_1, 56), xmm7);
 865     }
 866 
 867     __ addl(from, 64);
 868   __ BIND(L_copy_64_bytes);
 869     __ subl(qword_count, 8);
 870     __ jcc(Assembler::greaterEqual, L_copy_64_bytes_loop);
 871     __ addl(qword_count, 8);
 872     __ jccb(Assembler::zero, L_exit);
 873     //
 874     // length is too short, just copy qwords
 875     //
 876   __ BIND(L_copy_8_bytes);
 877     __ movq(xmm0, Address(from, 0));
 878     __ movq(Address(from, to_from, Address::times_1), xmm0);
 879     __ addl(from, 8);
 880     __ decrement(qword_count);
 881     __ jcc(Assembler::greater, L_copy_8_bytes);
 882   __ BIND(L_exit);
 883   }
 884 
 885   // Copy 64 bytes chunks
 886   //
 887   // Inputs:
 888   //   from        - source array address
 889   //   to_from     - destination array address - from
 890   //   qword_count - 8-bytes element count, negative
 891   //
 892   void mmx_copy_forward(Register from, Register to_from, Register qword_count) {
 893     assert( VM_Version::supports_mmx(), "supported cpu only" );
 894     Label L_copy_64_bytes_loop, L_copy_64_bytes, L_copy_8_bytes, L_exit;
 895     // Copy 64-byte chunks
 896     __ jmpb(L_copy_64_bytes);
 897     __ align(OptoLoopAlignment);
 898   __ BIND(L_copy_64_bytes_loop);
 899     __ movq(mmx0, Address(from, 0));
 900     __ movq(mmx1, Address(from, 8));
 901     __ movq(mmx2, Address(from, 16));
 902     __ movq(Address(from, to_from, Address::times_1, 0), mmx0);
 903     __ movq(mmx3, Address(from, 24));
 904     __ movq(Address(from, to_from, Address::times_1, 8), mmx1);
 905     __ movq(mmx4, Address(from, 32));
 906     __ movq(Address(from, to_from, Address::times_1, 16), mmx2);
 907     __ movq(mmx5, Address(from, 40));
 908     __ movq(Address(from, to_from, Address::times_1, 24), mmx3);
 909     __ movq(mmx6, Address(from, 48));
 910     __ movq(Address(from, to_from, Address::times_1, 32), mmx4);
 911     __ movq(mmx7, Address(from, 56));
 912     __ movq(Address(from, to_from, Address::times_1, 40), mmx5);
 913     __ movq(Address(from, to_from, Address::times_1, 48), mmx6);
 914     __ movq(Address(from, to_from, Address::times_1, 56), mmx7);
 915     __ addptr(from, 64);
 916   __ BIND(L_copy_64_bytes);
 917     __ subl(qword_count, 8);
 918     __ jcc(Assembler::greaterEqual, L_copy_64_bytes_loop);
 919     __ addl(qword_count, 8);
 920     __ jccb(Assembler::zero, L_exit);
 921     //
 922     // length is too short, just copy qwords
 923     //
 924   __ BIND(L_copy_8_bytes);
 925     __ movq(mmx0, Address(from, 0));
 926     __ movq(Address(from, to_from, Address::times_1), mmx0);
 927     __ addptr(from, 8);
 928     __ decrement(qword_count);
 929     __ jcc(Assembler::greater, L_copy_8_bytes);
 930   __ BIND(L_exit);
 931     __ emms();
 932   }
 933 
 934   address generate_disjoint_copy(BasicType t, bool aligned,
 935                                  Address::ScaleFactor sf,
 936                                  address* entry, const char *name) {
 937     __ align(CodeEntryAlignment);
 938     StubCodeMark mark(this, "StubRoutines", name);
 939     address start = __ pc();
 940 
 941     Label L_0_count, L_exit, L_skip_align1, L_skip_align2, L_copy_byte;
 942     Label L_copy_2_bytes, L_copy_4_bytes, L_copy_64_bytes;
 943 
 944     int shift = Address::times_ptr - sf;
 945 
 946     const Register from     = rsi;  // source array address
 947     const Register to       = rdi;  // destination array address
 948     const Register count    = rcx;  // elements count
 949     const Register to_from  = to;   // (to - from)
 950     const Register saved_to = rdx;  // saved destination array address
 951 
 952     __ enter(); // required for proper stackwalking of RuntimeStub frame
 953     __ push(rsi);
 954     __ push(rdi);
 955     __ movptr(from , Address(rsp, 12+ 4));
 956     __ movptr(to   , Address(rsp, 12+ 8));
 957     __ movl(count, Address(rsp, 12+ 12));
 958 
 959     if (entry != NULL) {
 960       *entry = __ pc(); // Entry point from conjoint arraycopy stub.
 961       BLOCK_COMMENT("Entry:");
 962     }
 963 
 964     if (t == T_OBJECT) {
 965       __ testl(count, count);
 966       __ jcc(Assembler::zero, L_0_count);
 967       gen_write_ref_array_pre_barrier(to, count);
 968       __ mov(saved_to, to);          // save 'to'
 969     }
 970 
 971     __ subptr(to, from); // to --> to_from
 972     __ cmpl(count, 2<<shift); // Short arrays (< 8 bytes) copy by element
 973     __ jcc(Assembler::below, L_copy_4_bytes); // use unsigned cmp
 974     if (!UseUnalignedLoadStores && !aligned && (t == T_BYTE || t == T_SHORT)) {
 975       // align source address at 4 bytes address boundary
 976       if (t == T_BYTE) {
 977         // One byte misalignment happens only for byte arrays
 978         __ testl(from, 1);
 979         __ jccb(Assembler::zero, L_skip_align1);
 980         __ movb(rax, Address(from, 0));
 981         __ movb(Address(from, to_from, Address::times_1, 0), rax);
 982         __ increment(from);
 983         __ decrement(count);
 984       __ BIND(L_skip_align1);
 985       }
 986       // Two bytes misalignment happens only for byte and short (char) arrays
 987       __ testl(from, 2);
 988       __ jccb(Assembler::zero, L_skip_align2);
 989       __ movw(rax, Address(from, 0));
 990       __ movw(Address(from, to_from, Address::times_1, 0), rax);
 991       __ addptr(from, 2);
 992       __ subl(count, 1<<(shift-1));
 993     __ BIND(L_skip_align2);
 994     }
 995     if (!VM_Version::supports_mmx()) {
 996       __ mov(rax, count);      // save 'count'
 997       __ shrl(count, shift); // bytes count
 998       __ addptr(to_from, from);// restore 'to'
 999       __ rep_mov();
1000       __ subptr(to_from, from);// restore 'to_from'
1001       __ mov(count, rax);      // restore 'count'
1002       __ jmpb(L_copy_2_bytes); // all dwords were copied
1003     } else {
1004       if (!UseUnalignedLoadStores) {
1005         // align to 8 bytes, we know we are 4 byte aligned to start
1006         __ testptr(from, 4);
1007         __ jccb(Assembler::zero, L_copy_64_bytes);
1008         __ movl(rax, Address(from, 0));
1009         __ movl(Address(from, to_from, Address::times_1, 0), rax);
1010         __ addptr(from, 4);
1011         __ subl(count, 1<<shift);
1012       }
1013     __ BIND(L_copy_64_bytes);
1014       __ mov(rax, count);
1015       __ shrl(rax, shift+1);  // 8 bytes chunk count
1016       //
1017       // Copy 8-byte chunks through MMX registers, 8 per iteration of the loop
1018       //
1019       if (UseXMMForArrayCopy) {
1020         xmm_copy_forward(from, to_from, rax);
1021       } else {
1022         mmx_copy_forward(from, to_from, rax);
1023       }
1024     }
1025     // copy tailing dword
1026   __ BIND(L_copy_4_bytes);
1027     __ testl(count, 1<<shift);
1028     __ jccb(Assembler::zero, L_copy_2_bytes);
1029     __ movl(rax, Address(from, 0));
1030     __ movl(Address(from, to_from, Address::times_1, 0), rax);
1031     if (t == T_BYTE || t == T_SHORT) {
1032       __ addptr(from, 4);
1033     __ BIND(L_copy_2_bytes);
1034       // copy tailing word
1035       __ testl(count, 1<<(shift-1));
1036       __ jccb(Assembler::zero, L_copy_byte);
1037       __ movw(rax, Address(from, 0));
1038       __ movw(Address(from, to_from, Address::times_1, 0), rax);
1039       if (t == T_BYTE) {
1040         __ addptr(from, 2);
1041       __ BIND(L_copy_byte);
1042         // copy tailing byte
1043         __ testl(count, 1);
1044         __ jccb(Assembler::zero, L_exit);
1045         __ movb(rax, Address(from, 0));
1046         __ movb(Address(from, to_from, Address::times_1, 0), rax);
1047       __ BIND(L_exit);
1048       } else {
1049       __ BIND(L_copy_byte);
1050       }
1051     } else {
1052     __ BIND(L_copy_2_bytes);
1053     }
1054 
1055     if (t == T_OBJECT) {
1056       __ movl(count, Address(rsp, 12+12)); // reread 'count'
1057       __ mov(to, saved_to); // restore 'to'
1058       gen_write_ref_array_post_barrier(to, count);
1059     __ BIND(L_0_count);
1060     }
1061     inc_copy_counter_np(t);
1062     __ pop(rdi);
1063     __ pop(rsi);
1064     __ leave(); // required for proper stackwalking of RuntimeStub frame
1065     __ xorptr(rax, rax); // return 0
1066     __ ret(0);
1067     return start;
1068   }
1069 
1070 
1071   address generate_fill(BasicType t, bool aligned, const char *name) {
1072     __ align(CodeEntryAlignment);
1073     StubCodeMark mark(this, "StubRoutines", name);
1074     address start = __ pc();
1075 
1076     BLOCK_COMMENT("Entry:");
1077 
1078     const Register to       = rdi;  // source array address
1079     const Register value    = rdx;  // value
1080     const Register count    = rsi;  // elements count
1081 
1082     __ enter(); // required for proper stackwalking of RuntimeStub frame
1083     __ push(rsi);
1084     __ push(rdi);
1085     __ movptr(to   , Address(rsp, 12+ 4));
1086     __ movl(value, Address(rsp, 12+ 8));
1087     __ movl(count, Address(rsp, 12+ 12));
1088 
1089     __ generate_fill(t, aligned, to, value, count, rax, xmm0);
1090 
1091     __ pop(rdi);
1092     __ pop(rsi);
1093     __ leave(); // required for proper stackwalking of RuntimeStub frame
1094     __ ret(0);
1095     return start;
1096   }
1097 
1098   address generate_conjoint_copy(BasicType t, bool aligned,
1099                                  Address::ScaleFactor sf,
1100                                  address nooverlap_target,
1101                                  address* entry, const char *name) {
1102     __ align(CodeEntryAlignment);
1103     StubCodeMark mark(this, "StubRoutines", name);
1104     address start = __ pc();
1105 
1106     Label L_0_count, L_exit, L_skip_align1, L_skip_align2, L_copy_byte;
1107     Label L_copy_2_bytes, L_copy_4_bytes, L_copy_8_bytes, L_copy_8_bytes_loop;
1108 
1109     int shift = Address::times_ptr - sf;
1110 
1111     const Register src   = rax;  // source array address
1112     const Register dst   = rdx;  // destination array address
1113     const Register from  = rsi;  // source array address
1114     const Register to    = rdi;  // destination array address
1115     const Register count = rcx;  // elements count
1116     const Register end   = rax;  // array end address
1117 
1118     __ enter(); // required for proper stackwalking of RuntimeStub frame
1119     __ push(rsi);
1120     __ push(rdi);
1121     __ movptr(src  , Address(rsp, 12+ 4));   // from
1122     __ movptr(dst  , Address(rsp, 12+ 8));   // to
1123     __ movl2ptr(count, Address(rsp, 12+12)); // count
1124 
1125     if (entry != NULL) {
1126       *entry = __ pc(); // Entry point from generic arraycopy stub.
1127       BLOCK_COMMENT("Entry:");
1128     }
1129 
1130     // nooverlap_target expects arguments in rsi and rdi.
1131     __ mov(from, src);
1132     __ mov(to  , dst);
1133 
1134     // arrays overlap test: dispatch to disjoint stub if necessary.
1135     RuntimeAddress nooverlap(nooverlap_target);
1136     __ cmpptr(dst, src);
1137     __ lea(end, Address(src, count, sf, 0)); // src + count * elem_size
1138     __ jump_cc(Assembler::belowEqual, nooverlap);
1139     __ cmpptr(dst, end);
1140     __ jump_cc(Assembler::aboveEqual, nooverlap);
1141 
1142     if (t == T_OBJECT) {
1143       __ testl(count, count);
1144       __ jcc(Assembler::zero, L_0_count);
1145        gen_write_ref_array_pre_barrier(dst, count);
1146     }
1147 
1148     // copy from high to low
1149     __ cmpl(count, 2<<shift); // Short arrays (< 8 bytes) copy by element
1150     __ jcc(Assembler::below, L_copy_4_bytes); // use unsigned cmp
1151     if (t == T_BYTE || t == T_SHORT) {
1152       // Align the end of destination array at 4 bytes address boundary
1153       __ lea(end, Address(dst, count, sf, 0));
1154       if (t == T_BYTE) {
1155         // One byte misalignment happens only for byte arrays
1156         __ testl(end, 1);
1157         __ jccb(Assembler::zero, L_skip_align1);
1158         __ decrement(count);
1159         __ movb(rdx, Address(from, count, sf, 0));
1160         __ movb(Address(to, count, sf, 0), rdx);
1161       __ BIND(L_skip_align1);
1162       }
1163       // Two bytes misalignment happens only for byte and short (char) arrays
1164       __ testl(end, 2);
1165       __ jccb(Assembler::zero, L_skip_align2);
1166       __ subptr(count, 1<<(shift-1));
1167       __ movw(rdx, Address(from, count, sf, 0));
1168       __ movw(Address(to, count, sf, 0), rdx);
1169     __ BIND(L_skip_align2);
1170       __ cmpl(count, 2<<shift); // Short arrays (< 8 bytes) copy by element
1171       __ jcc(Assembler::below, L_copy_4_bytes);
1172     }
1173 
1174     if (!VM_Version::supports_mmx()) {
1175       __ std();
1176       __ mov(rax, count); // Save 'count'
1177       __ mov(rdx, to);    // Save 'to'
1178       __ lea(rsi, Address(from, count, sf, -4));
1179       __ lea(rdi, Address(to  , count, sf, -4));
1180       __ shrptr(count, shift); // bytes count
1181       __ rep_mov();
1182       __ cld();
1183       __ mov(count, rax); // restore 'count'
1184       __ andl(count, (1<<shift)-1);      // mask the number of rest elements
1185       __ movptr(from, Address(rsp, 12+4)); // reread 'from'
1186       __ mov(to, rdx);   // restore 'to'
1187       __ jmpb(L_copy_2_bytes); // all dword were copied
1188    } else {
1189       // Align to 8 bytes the end of array. It is aligned to 4 bytes already.
1190       __ testptr(end, 4);
1191       __ jccb(Assembler::zero, L_copy_8_bytes);
1192       __ subl(count, 1<<shift);
1193       __ movl(rdx, Address(from, count, sf, 0));
1194       __ movl(Address(to, count, sf, 0), rdx);
1195       __ jmpb(L_copy_8_bytes);
1196 
1197       __ align(OptoLoopAlignment);
1198       // Move 8 bytes
1199     __ BIND(L_copy_8_bytes_loop);
1200       if (UseXMMForArrayCopy) {
1201         __ movq(xmm0, Address(from, count, sf, 0));
1202         __ movq(Address(to, count, sf, 0), xmm0);
1203       } else {
1204         __ movq(mmx0, Address(from, count, sf, 0));
1205         __ movq(Address(to, count, sf, 0), mmx0);
1206       }
1207     __ BIND(L_copy_8_bytes);
1208       __ subl(count, 2<<shift);
1209       __ jcc(Assembler::greaterEqual, L_copy_8_bytes_loop);
1210       __ addl(count, 2<<shift);
1211       if (!UseXMMForArrayCopy) {
1212         __ emms();
1213       }
1214     }
1215   __ BIND(L_copy_4_bytes);
1216     // copy prefix qword
1217     __ testl(count, 1<<shift);
1218     __ jccb(Assembler::zero, L_copy_2_bytes);
1219     __ movl(rdx, Address(from, count, sf, -4));
1220     __ movl(Address(to, count, sf, -4), rdx);
1221 
1222     if (t == T_BYTE || t == T_SHORT) {
1223         __ subl(count, (1<<shift));
1224       __ BIND(L_copy_2_bytes);
1225         // copy prefix dword
1226         __ testl(count, 1<<(shift-1));
1227         __ jccb(Assembler::zero, L_copy_byte);
1228         __ movw(rdx, Address(from, count, sf, -2));
1229         __ movw(Address(to, count, sf, -2), rdx);
1230         if (t == T_BYTE) {
1231           __ subl(count, 1<<(shift-1));
1232         __ BIND(L_copy_byte);
1233           // copy prefix byte
1234           __ testl(count, 1);
1235           __ jccb(Assembler::zero, L_exit);
1236           __ movb(rdx, Address(from, 0));
1237           __ movb(Address(to, 0), rdx);
1238         __ BIND(L_exit);
1239         } else {
1240         __ BIND(L_copy_byte);
1241         }
1242     } else {
1243     __ BIND(L_copy_2_bytes);
1244     }
1245     if (t == T_OBJECT) {
1246       __ movl2ptr(count, Address(rsp, 12+12)); // reread count
1247       gen_write_ref_array_post_barrier(to, count);
1248     __ BIND(L_0_count);
1249     }
1250     inc_copy_counter_np(t);
1251     __ pop(rdi);
1252     __ pop(rsi);
1253     __ leave(); // required for proper stackwalking of RuntimeStub frame
1254     __ xorptr(rax, rax); // return 0
1255     __ ret(0);
1256     return start;
1257   }
1258 
1259 
1260   address generate_disjoint_long_copy(address* entry, const char *name) {
1261     __ align(CodeEntryAlignment);
1262     StubCodeMark mark(this, "StubRoutines", name);
1263     address start = __ pc();
1264 
1265     Label L_copy_8_bytes, L_copy_8_bytes_loop;
1266     const Register from       = rax;  // source array address
1267     const Register to         = rdx;  // destination array address
1268     const Register count      = rcx;  // elements count
1269     const Register to_from    = rdx;  // (to - from)
1270 
1271     __ enter(); // required for proper stackwalking of RuntimeStub frame
1272     __ movptr(from , Address(rsp, 8+0));       // from
1273     __ movptr(to   , Address(rsp, 8+4));       // to
1274     __ movl2ptr(count, Address(rsp, 8+8));     // count
1275 
1276     *entry = __ pc(); // Entry point from conjoint arraycopy stub.
1277     BLOCK_COMMENT("Entry:");
1278 
1279     __ subptr(to, from); // to --> to_from
1280     if (VM_Version::supports_mmx()) {
1281       if (UseXMMForArrayCopy) {
1282         xmm_copy_forward(from, to_from, count);
1283       } else {
1284         mmx_copy_forward(from, to_from, count);
1285       }
1286     } else {
1287       __ jmpb(L_copy_8_bytes);
1288       __ align(OptoLoopAlignment);
1289     __ BIND(L_copy_8_bytes_loop);
1290       __ fild_d(Address(from, 0));
1291       __ fistp_d(Address(from, to_from, Address::times_1));
1292       __ addptr(from, 8);
1293     __ BIND(L_copy_8_bytes);
1294       __ decrement(count);
1295       __ jcc(Assembler::greaterEqual, L_copy_8_bytes_loop);
1296     }
1297     inc_copy_counter_np(T_LONG);
1298     __ leave(); // required for proper stackwalking of RuntimeStub frame
1299     __ xorptr(rax, rax); // return 0
1300     __ ret(0);
1301     return start;
1302   }
1303 
1304   address generate_conjoint_long_copy(address nooverlap_target,
1305                                       address* entry, const char *name) {
1306     __ align(CodeEntryAlignment);
1307     StubCodeMark mark(this, "StubRoutines", name);
1308     address start = __ pc();
1309 
1310     Label L_copy_8_bytes, L_copy_8_bytes_loop;
1311     const Register from       = rax;  // source array address
1312     const Register to         = rdx;  // destination array address
1313     const Register count      = rcx;  // elements count
1314     const Register end_from   = rax;  // source array end address
1315 
1316     __ enter(); // required for proper stackwalking of RuntimeStub frame
1317     __ movptr(from , Address(rsp, 8+0));       // from
1318     __ movptr(to   , Address(rsp, 8+4));       // to
1319     __ movl2ptr(count, Address(rsp, 8+8));     // count
1320 
1321     *entry = __ pc(); // Entry point from generic arraycopy stub.
1322     BLOCK_COMMENT("Entry:");
1323 
1324     // arrays overlap test
1325     __ cmpptr(to, from);
1326     RuntimeAddress nooverlap(nooverlap_target);
1327     __ jump_cc(Assembler::belowEqual, nooverlap);
1328     __ lea(end_from, Address(from, count, Address::times_8, 0));
1329     __ cmpptr(to, end_from);
1330     __ movptr(from, Address(rsp, 8));  // from
1331     __ jump_cc(Assembler::aboveEqual, nooverlap);
1332 
1333     __ jmpb(L_copy_8_bytes);
1334 
1335     __ align(OptoLoopAlignment);
1336   __ BIND(L_copy_8_bytes_loop);
1337     if (VM_Version::supports_mmx()) {
1338       if (UseXMMForArrayCopy) {
1339         __ movq(xmm0, Address(from, count, Address::times_8));
1340         __ movq(Address(to, count, Address::times_8), xmm0);
1341       } else {
1342         __ movq(mmx0, Address(from, count, Address::times_8));
1343         __ movq(Address(to, count, Address::times_8), mmx0);
1344       }
1345     } else {
1346       __ fild_d(Address(from, count, Address::times_8));
1347       __ fistp_d(Address(to, count, Address::times_8));
1348     }
1349   __ BIND(L_copy_8_bytes);
1350     __ decrement(count);
1351     __ jcc(Assembler::greaterEqual, L_copy_8_bytes_loop);
1352 
1353     if (VM_Version::supports_mmx() && !UseXMMForArrayCopy) {
1354       __ emms();
1355     }
1356     inc_copy_counter_np(T_LONG);
1357     __ leave(); // required for proper stackwalking of RuntimeStub frame
1358     __ xorptr(rax, rax); // return 0
1359     __ ret(0);
1360     return start;
1361   }
1362 
1363 
1364   // Helper for generating a dynamic type check.
1365   // The sub_klass must be one of {rbx, rdx, rsi}.
1366   // The temp is killed.
1367   void generate_type_check(Register sub_klass,
1368                            Address& super_check_offset_addr,
1369                            Address& super_klass_addr,
1370                            Register temp,
1371                            Label* L_success, Label* L_failure) {
1372     BLOCK_COMMENT("type_check:");
1373 
1374     Label L_fallthrough;
1375 #define LOCAL_JCC(assembler_con, label_ptr)                             \
1376     if (label_ptr != NULL)  __ jcc(assembler_con, *(label_ptr));        \
1377     else                    __ jcc(assembler_con, L_fallthrough) /*omit semi*/
1378 
1379     // The following is a strange variation of the fast path which requires
1380     // one less register, because needed values are on the argument stack.
1381     // __ check_klass_subtype_fast_path(sub_klass, *super_klass*, temp,
1382     //                                  L_success, L_failure, NULL);
1383     assert_different_registers(sub_klass, temp);
1384 
1385     int sc_offset = (klassOopDesc::header_size() * HeapWordSize +
1386                      Klass::secondary_super_cache_offset_in_bytes());
1387 
1388     // if the pointers are equal, we are done (e.g., String[] elements)
1389     __ cmpptr(sub_klass, super_klass_addr);
1390     LOCAL_JCC(Assembler::equal, L_success);
1391 
1392     // check the supertype display:
1393     __ movl2ptr(temp, super_check_offset_addr);
1394     Address super_check_addr(sub_klass, temp, Address::times_1, 0);
1395     __ movptr(temp, super_check_addr); // load displayed supertype
1396     __ cmpptr(temp, super_klass_addr); // test the super type
1397     LOCAL_JCC(Assembler::equal, L_success);
1398 
1399     // if it was a primary super, we can just fail immediately
1400     __ cmpl(super_check_offset_addr, sc_offset);
1401     LOCAL_JCC(Assembler::notEqual, L_failure);
1402 
1403     // The repne_scan instruction uses fixed registers, which will get spilled.
1404     // We happen to know this works best when super_klass is in rax.
1405     Register super_klass = temp;
1406     __ movptr(super_klass, super_klass_addr);
1407     __ check_klass_subtype_slow_path(sub_klass, super_klass, noreg, noreg,
1408                                      L_success, L_failure);
1409 
1410     __ bind(L_fallthrough);
1411 
1412     if (L_success == NULL) { BLOCK_COMMENT("L_success:"); }
1413     if (L_failure == NULL) { BLOCK_COMMENT("L_failure:"); }
1414 
1415 #undef LOCAL_JCC
1416   }
1417 
1418   //
1419   //  Generate checkcasting array copy stub
1420   //
1421   //  Input:
1422   //    4(rsp)   - source array address
1423   //    8(rsp)   - destination array address
1424   //   12(rsp)   - element count, can be zero
1425   //   16(rsp)   - size_t ckoff (super_check_offset)
1426   //   20(rsp)   - oop ckval (super_klass)
1427   //
1428   //  Output:
1429   //    rax, ==  0  -  success
1430   //    rax, == -1^K - failure, where K is partial transfer count
1431   //
1432   address generate_checkcast_copy(const char *name, address* entry) {
1433     __ align(CodeEntryAlignment);
1434     StubCodeMark mark(this, "StubRoutines", name);
1435     address start = __ pc();
1436 
1437     Label L_load_element, L_store_element, L_do_card_marks, L_done;
1438 
1439     // register use:
1440     //  rax, rdx, rcx -- loop control (end_from, end_to, count)
1441     //  rdi, rsi      -- element access (oop, klass)
1442     //  rbx,           -- temp
1443     const Register from       = rax;    // source array address
1444     const Register to         = rdx;    // destination array address
1445     const Register length     = rcx;    // elements count
1446     const Register elem       = rdi;    // each oop copied
1447     const Register elem_klass = rsi;    // each elem._klass (sub_klass)
1448     const Register temp       = rbx;    // lone remaining temp
1449 
1450     __ enter(); // required for proper stackwalking of RuntimeStub frame
1451 
1452     __ push(rsi);
1453     __ push(rdi);
1454     __ push(rbx);
1455 
1456     Address   from_arg(rsp, 16+ 4);     // from
1457     Address     to_arg(rsp, 16+ 8);     // to
1458     Address length_arg(rsp, 16+12);     // elements count
1459     Address  ckoff_arg(rsp, 16+16);     // super_check_offset
1460     Address  ckval_arg(rsp, 16+20);     // super_klass
1461 
1462     // Load up:
1463     __ movptr(from,     from_arg);
1464     __ movptr(to,         to_arg);
1465     __ movl2ptr(length, length_arg);
1466 
1467     if (entry != NULL) {
1468       *entry = __ pc(); // Entry point from generic arraycopy stub.
1469       BLOCK_COMMENT("Entry:");
1470     }
1471 
1472     //---------------------------------------------------------------
1473     // Assembler stub will be used for this call to arraycopy
1474     // if the two arrays are subtypes of Object[] but the
1475     // destination array type is not equal to or a supertype
1476     // of the source type.  Each element must be separately
1477     // checked.
1478 
1479     // Loop-invariant addresses.  They are exclusive end pointers.
1480     Address end_from_addr(from, length, Address::times_ptr, 0);
1481     Address   end_to_addr(to,   length, Address::times_ptr, 0);
1482 
1483     Register end_from = from;           // re-use
1484     Register end_to   = to;             // re-use
1485     Register count    = length;         // re-use
1486 
1487     // Loop-variant addresses.  They assume post-incremented count < 0.
1488     Address from_element_addr(end_from, count, Address::times_ptr, 0);
1489     Address   to_element_addr(end_to,   count, Address::times_ptr, 0);
1490     Address elem_klass_addr(elem, oopDesc::klass_offset_in_bytes());
1491 
1492     // Copy from low to high addresses, indexed from the end of each array.
1493     gen_write_ref_array_pre_barrier(to, count);
1494     __ lea(end_from, end_from_addr);
1495     __ lea(end_to,   end_to_addr);
1496     assert(length == count, "");        // else fix next line:
1497     __ negptr(count);                   // negate and test the length
1498     __ jccb(Assembler::notZero, L_load_element);
1499 
1500     // Empty array:  Nothing to do.
1501     __ xorptr(rax, rax);                  // return 0 on (trivial) success
1502     __ jmp(L_done);
1503 
1504     // ======== begin loop ========
1505     // (Loop is rotated; its entry is L_load_element.)
1506     // Loop control:
1507     //   for (count = -count; count != 0; count++)
1508     // Base pointers src, dst are biased by 8*count,to last element.
1509     __ align(OptoLoopAlignment);
1510 
1511     __ BIND(L_store_element);
1512     __ movptr(to_element_addr, elem);     // store the oop
1513     __ increment(count);                // increment the count toward zero
1514     __ jccb(Assembler::zero, L_do_card_marks);
1515 
1516     // ======== loop entry is here ========
1517     __ BIND(L_load_element);
1518     __ movptr(elem, from_element_addr);   // load the oop
1519     __ testptr(elem, elem);
1520     __ jccb(Assembler::zero, L_store_element);
1521 
1522     // (Could do a trick here:  Remember last successful non-null
1523     // element stored and make a quick oop equality check on it.)
1524 
1525     __ movptr(elem_klass, elem_klass_addr); // query the object klass
1526     generate_type_check(elem_klass, ckoff_arg, ckval_arg, temp,
1527                         &L_store_element, NULL);
1528       // (On fall-through, we have failed the element type check.)
1529     // ======== end loop ========
1530 
1531     // It was a real error; we must depend on the caller to finish the job.
1532     // Register "count" = -1 * number of *remaining* oops, length_arg = *total* oops.
1533     // Emit GC store barriers for the oops we have copied (length_arg + count),
1534     // and report their number to the caller.
1535     __ addl(count, length_arg);         // transfers = (length - remaining)
1536     __ movl2ptr(rax, count);            // save the value
1537     __ notptr(rax);                     // report (-1^K) to caller
1538     __ movptr(to, to_arg);              // reload
1539     assert_different_registers(to, count, rax);
1540     gen_write_ref_array_post_barrier(to, count);
1541     __ jmpb(L_done);
1542 
1543     // Come here on success only.
1544     __ BIND(L_do_card_marks);
1545     __ movl2ptr(count, length_arg);
1546     __ movptr(to, to_arg);                // reload
1547     gen_write_ref_array_post_barrier(to, count);
1548     __ xorptr(rax, rax);                  // return 0 on success
1549 
1550     // Common exit point (success or failure).
1551     __ BIND(L_done);
1552     __ pop(rbx);
1553     __ pop(rdi);
1554     __ pop(rsi);
1555     inc_counter_np(SharedRuntime::_checkcast_array_copy_ctr);
1556     __ leave(); // required for proper stackwalking of RuntimeStub frame
1557     __ ret(0);
1558 
1559     return start;
1560   }
1561 
1562   //
1563   //  Generate 'unsafe' array copy stub
1564   //  Though just as safe as the other stubs, it takes an unscaled
1565   //  size_t argument instead of an element count.
1566   //
1567   //  Input:
1568   //    4(rsp)   - source array address
1569   //    8(rsp)   - destination array address
1570   //   12(rsp)   - byte count, can be zero
1571   //
1572   //  Output:
1573   //    rax, ==  0  -  success
1574   //    rax, == -1  -  need to call System.arraycopy
1575   //
1576   // Examines the alignment of the operands and dispatches
1577   // to a long, int, short, or byte copy loop.
1578   //
1579   address generate_unsafe_copy(const char *name,
1580                                address byte_copy_entry,
1581                                address short_copy_entry,
1582                                address int_copy_entry,
1583                                address long_copy_entry) {
1584 
1585     Label L_long_aligned, L_int_aligned, L_short_aligned;
1586 
1587     __ align(CodeEntryAlignment);
1588     StubCodeMark mark(this, "StubRoutines", name);
1589     address start = __ pc();
1590 
1591     const Register from       = rax;  // source array address
1592     const Register to         = rdx;  // destination array address
1593     const Register count      = rcx;  // elements count
1594 
1595     __ enter(); // required for proper stackwalking of RuntimeStub frame
1596     __ push(rsi);
1597     __ push(rdi);
1598     Address  from_arg(rsp, 12+ 4);      // from
1599     Address    to_arg(rsp, 12+ 8);      // to
1600     Address count_arg(rsp, 12+12);      // byte count
1601 
1602     // Load up:
1603     __ movptr(from ,  from_arg);
1604     __ movptr(to   ,    to_arg);
1605     __ movl2ptr(count, count_arg);
1606 
1607     // bump this on entry, not on exit:
1608     inc_counter_np(SharedRuntime::_unsafe_array_copy_ctr);
1609 
1610     const Register bits = rsi;
1611     __ mov(bits, from);
1612     __ orptr(bits, to);
1613     __ orptr(bits, count);
1614 
1615     __ testl(bits, BytesPerLong-1);
1616     __ jccb(Assembler::zero, L_long_aligned);
1617 
1618     __ testl(bits, BytesPerInt-1);
1619     __ jccb(Assembler::zero, L_int_aligned);
1620 
1621     __ testl(bits, BytesPerShort-1);
1622     __ jump_cc(Assembler::notZero, RuntimeAddress(byte_copy_entry));
1623 
1624     __ BIND(L_short_aligned);
1625     __ shrptr(count, LogBytesPerShort); // size => short_count
1626     __ movl(count_arg, count);          // update 'count'
1627     __ jump(RuntimeAddress(short_copy_entry));
1628 
1629     __ BIND(L_int_aligned);
1630     __ shrptr(count, LogBytesPerInt); // size => int_count
1631     __ movl(count_arg, count);          // update 'count'
1632     __ jump(RuntimeAddress(int_copy_entry));
1633 
1634     __ BIND(L_long_aligned);
1635     __ shrptr(count, LogBytesPerLong); // size => qword_count
1636     __ movl(count_arg, count);          // update 'count'
1637     __ pop(rdi); // Do pops here since jlong_arraycopy stub does not do it.
1638     __ pop(rsi);
1639     __ jump(RuntimeAddress(long_copy_entry));
1640 
1641     return start;
1642   }
1643 
1644 
1645   // Perform range checks on the proposed arraycopy.
1646   // Smashes src_pos and dst_pos.  (Uses them up for temps.)
1647   void arraycopy_range_checks(Register src,
1648                               Register src_pos,
1649                               Register dst,
1650                               Register dst_pos,
1651                               Address& length,
1652                               Label& L_failed) {
1653     BLOCK_COMMENT("arraycopy_range_checks:");
1654     const Register src_end = src_pos;   // source array end position
1655     const Register dst_end = dst_pos;   // destination array end position
1656     __ addl(src_end, length); // src_pos + length
1657     __ addl(dst_end, length); // dst_pos + length
1658 
1659     //  if (src_pos + length > arrayOop(src)->length() ) FAIL;
1660     __ cmpl(src_end, Address(src, arrayOopDesc::length_offset_in_bytes()));
1661     __ jcc(Assembler::above, L_failed);
1662 
1663     //  if (dst_pos + length > arrayOop(dst)->length() ) FAIL;
1664     __ cmpl(dst_end, Address(dst, arrayOopDesc::length_offset_in_bytes()));
1665     __ jcc(Assembler::above, L_failed);
1666 
1667     BLOCK_COMMENT("arraycopy_range_checks done");
1668   }
1669 
1670 
1671   //
1672   //  Generate generic array copy stubs
1673   //
1674   //  Input:
1675   //     4(rsp)    -  src oop
1676   //     8(rsp)    -  src_pos
1677   //    12(rsp)    -  dst oop
1678   //    16(rsp)    -  dst_pos
1679   //    20(rsp)    -  element count
1680   //
1681   //  Output:
1682   //    rax, ==  0  -  success
1683   //    rax, == -1^K - failure, where K is partial transfer count
1684   //
1685   address generate_generic_copy(const char *name,
1686                                 address entry_jbyte_arraycopy,
1687                                 address entry_jshort_arraycopy,
1688                                 address entry_jint_arraycopy,
1689                                 address entry_oop_arraycopy,
1690                                 address entry_jlong_arraycopy,
1691                                 address entry_checkcast_arraycopy) {
1692     Label L_failed, L_failed_0, L_objArray;
1693 
1694     { int modulus = CodeEntryAlignment;
1695       int target  = modulus - 5; // 5 = sizeof jmp(L_failed)
1696       int advance = target - (__ offset() % modulus);
1697       if (advance < 0)  advance += modulus;
1698       if (advance > 0)  __ nop(advance);
1699     }
1700     StubCodeMark mark(this, "StubRoutines", name);
1701 
1702     // Short-hop target to L_failed.  Makes for denser prologue code.
1703     __ BIND(L_failed_0);
1704     __ jmp(L_failed);
1705     assert(__ offset() % CodeEntryAlignment == 0, "no further alignment needed");
1706 
1707     __ align(CodeEntryAlignment);
1708     address start = __ pc();
1709 
1710     __ enter(); // required for proper stackwalking of RuntimeStub frame
1711     __ push(rsi);
1712     __ push(rdi);
1713 
1714     // bump this on entry, not on exit:
1715     inc_counter_np(SharedRuntime::_generic_array_copy_ctr);
1716 
1717     // Input values
1718     Address SRC     (rsp, 12+ 4);
1719     Address SRC_POS (rsp, 12+ 8);
1720     Address DST     (rsp, 12+12);
1721     Address DST_POS (rsp, 12+16);
1722     Address LENGTH  (rsp, 12+20);
1723 
1724     //-----------------------------------------------------------------------
1725     // Assembler stub will be used for this call to arraycopy
1726     // if the following conditions are met:
1727     //
1728     // (1) src and dst must not be null.
1729     // (2) src_pos must not be negative.
1730     // (3) dst_pos must not be negative.
1731     // (4) length  must not be negative.
1732     // (5) src klass and dst klass should be the same and not NULL.
1733     // (6) src and dst should be arrays.
1734     // (7) src_pos + length must not exceed length of src.
1735     // (8) dst_pos + length must not exceed length of dst.
1736     //
1737 
1738     const Register src     = rax;       // source array oop
1739     const Register src_pos = rsi;
1740     const Register dst     = rdx;       // destination array oop
1741     const Register dst_pos = rdi;
1742     const Register length  = rcx;       // transfer count
1743 
1744     //  if (src == NULL) return -1;
1745     __ movptr(src, SRC);      // src oop
1746     __ testptr(src, src);
1747     __ jccb(Assembler::zero, L_failed_0);
1748 
1749     //  if (src_pos < 0) return -1;
1750     __ movl2ptr(src_pos, SRC_POS);  // src_pos
1751     __ testl(src_pos, src_pos);
1752     __ jccb(Assembler::negative, L_failed_0);
1753 
1754     //  if (dst == NULL) return -1;
1755     __ movptr(dst, DST);      // dst oop
1756     __ testptr(dst, dst);
1757     __ jccb(Assembler::zero, L_failed_0);
1758 
1759     //  if (dst_pos < 0) return -1;
1760     __ movl2ptr(dst_pos, DST_POS);  // dst_pos
1761     __ testl(dst_pos, dst_pos);
1762     __ jccb(Assembler::negative, L_failed_0);
1763 
1764     //  if (length < 0) return -1;
1765     __ movl2ptr(length, LENGTH);   // length
1766     __ testl(length, length);
1767     __ jccb(Assembler::negative, L_failed_0);
1768 
1769     //  if (src->klass() == NULL) return -1;
1770     Address src_klass_addr(src, oopDesc::klass_offset_in_bytes());
1771     Address dst_klass_addr(dst, oopDesc::klass_offset_in_bytes());
1772     const Register rcx_src_klass = rcx;    // array klass
1773     __ movptr(rcx_src_klass, Address(src, oopDesc::klass_offset_in_bytes()));
1774 
1775 #ifdef ASSERT
1776     //  assert(src->klass() != NULL);
1777     BLOCK_COMMENT("assert klasses not null");
1778     { Label L1, L2;
1779       __ testptr(rcx_src_klass, rcx_src_klass);
1780       __ jccb(Assembler::notZero, L2);   // it is broken if klass is NULL
1781       __ bind(L1);
1782       __ stop("broken null klass");
1783       __ bind(L2);
1784       __ cmpptr(dst_klass_addr, (int32_t)NULL_WORD);
1785       __ jccb(Assembler::equal, L1);      // this would be broken also
1786       BLOCK_COMMENT("assert done");
1787     }
1788 #endif //ASSERT
1789 
1790     // Load layout helper (32-bits)
1791     //
1792     //  |array_tag|     | header_size | element_type |     |log2_element_size|
1793     // 32        30    24            16              8     2                 0
1794     //
1795     //   array_tag: typeArray = 0x3, objArray = 0x2, non-array = 0x0
1796     //
1797 
1798     int lh_offset = klassOopDesc::header_size() * HeapWordSize +
1799                     Klass::layout_helper_offset_in_bytes();
1800     Address src_klass_lh_addr(rcx_src_klass, lh_offset);
1801 
1802     // Handle objArrays completely differently...
1803     jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
1804     __ cmpl(src_klass_lh_addr, objArray_lh);
1805     __ jcc(Assembler::equal, L_objArray);
1806 
1807     //  if (src->klass() != dst->klass()) return -1;
1808     __ cmpptr(rcx_src_klass, dst_klass_addr);
1809     __ jccb(Assembler::notEqual, L_failed_0);
1810 
1811     const Register rcx_lh = rcx;  // layout helper
1812     assert(rcx_lh == rcx_src_klass, "known alias");
1813     __ movl(rcx_lh, src_klass_lh_addr);
1814 
1815     //  if (!src->is_Array()) return -1;
1816     __ cmpl(rcx_lh, Klass::_lh_neutral_value);
1817     __ jcc(Assembler::greaterEqual, L_failed_0); // signed cmp
1818 
1819     // At this point, it is known to be a typeArray (array_tag 0x3).
1820 #ifdef ASSERT
1821     { Label L;
1822       __ cmpl(rcx_lh, (Klass::_lh_array_tag_type_value << Klass::_lh_array_tag_shift));
1823       __ jcc(Assembler::greaterEqual, L); // signed cmp
1824       __ stop("must be a primitive array");
1825       __ bind(L);
1826     }
1827 #endif
1828 
1829     assert_different_registers(src, src_pos, dst, dst_pos, rcx_lh);
1830     arraycopy_range_checks(src, src_pos, dst, dst_pos, LENGTH, L_failed);
1831 
1832     // typeArrayKlass
1833     //
1834     // src_addr = (src + array_header_in_bytes()) + (src_pos << log2elemsize);
1835     // dst_addr = (dst + array_header_in_bytes()) + (dst_pos << log2elemsize);
1836     //
1837     const Register rsi_offset = rsi; // array offset
1838     const Register src_array  = src; // src array offset
1839     const Register dst_array  = dst; // dst array offset
1840     const Register rdi_elsize = rdi; // log2 element size
1841 
1842     __ mov(rsi_offset, rcx_lh);
1843     __ shrptr(rsi_offset, Klass::_lh_header_size_shift);
1844     __ andptr(rsi_offset, Klass::_lh_header_size_mask);   // array_offset
1845     __ addptr(src_array, rsi_offset);  // src array offset
1846     __ addptr(dst_array, rsi_offset);  // dst array offset
1847     __ andptr(rcx_lh, Klass::_lh_log2_element_size_mask); // log2 elsize
1848 
1849     // next registers should be set before the jump to corresponding stub
1850     const Register from       = src; // source array address
1851     const Register to         = dst; // destination array address
1852     const Register count      = rcx; // elements count
1853     // some of them should be duplicated on stack
1854 #define FROM   Address(rsp, 12+ 4)
1855 #define TO     Address(rsp, 12+ 8)   // Not used now
1856 #define COUNT  Address(rsp, 12+12)   // Only for oop arraycopy
1857 
1858     BLOCK_COMMENT("scale indexes to element size");
1859     __ movl2ptr(rsi, SRC_POS);  // src_pos
1860     __ shlptr(rsi);             // src_pos << rcx (log2 elsize)
1861     assert(src_array == from, "");
1862     __ addptr(from, rsi);       // from = src_array + SRC_POS << log2 elsize
1863     __ movl2ptr(rdi, DST_POS);  // dst_pos
1864     __ shlptr(rdi);             // dst_pos << rcx (log2 elsize)
1865     assert(dst_array == to, "");
1866     __ addptr(to,  rdi);        // to   = dst_array + DST_POS << log2 elsize
1867     __ movptr(FROM, from);      // src_addr
1868     __ mov(rdi_elsize, rcx_lh); // log2 elsize
1869     __ movl2ptr(count, LENGTH); // elements count
1870 
1871     BLOCK_COMMENT("choose copy loop based on element size");
1872     __ cmpl(rdi_elsize, 0);
1873 
1874     __ jump_cc(Assembler::equal, RuntimeAddress(entry_jbyte_arraycopy));
1875     __ cmpl(rdi_elsize, LogBytesPerShort);
1876     __ jump_cc(Assembler::equal, RuntimeAddress(entry_jshort_arraycopy));
1877     __ cmpl(rdi_elsize, LogBytesPerInt);
1878     __ jump_cc(Assembler::equal, RuntimeAddress(entry_jint_arraycopy));
1879 #ifdef ASSERT
1880     __ cmpl(rdi_elsize, LogBytesPerLong);
1881     __ jccb(Assembler::notEqual, L_failed);
1882 #endif
1883     __ pop(rdi); // Do pops here since jlong_arraycopy stub does not do it.
1884     __ pop(rsi);
1885     __ jump(RuntimeAddress(entry_jlong_arraycopy));
1886 
1887   __ BIND(L_failed);
1888     __ xorptr(rax, rax);
1889     __ notptr(rax); // return -1
1890     __ pop(rdi);
1891     __ pop(rsi);
1892     __ leave(); // required for proper stackwalking of RuntimeStub frame
1893     __ ret(0);
1894 
1895     // objArrayKlass
1896   __ BIND(L_objArray);
1897     // live at this point:  rcx_src_klass, src[_pos], dst[_pos]
1898 
1899     Label L_plain_copy, L_checkcast_copy;
1900     //  test array classes for subtyping
1901     __ cmpptr(rcx_src_klass, dst_klass_addr); // usual case is exact equality
1902     __ jccb(Assembler::notEqual, L_checkcast_copy);
1903 
1904     // Identically typed arrays can be copied without element-wise checks.
1905     assert_different_registers(src, src_pos, dst, dst_pos, rcx_src_klass);
1906     arraycopy_range_checks(src, src_pos, dst, dst_pos, LENGTH, L_failed);
1907 
1908   __ BIND(L_plain_copy);
1909     __ movl2ptr(count, LENGTH); // elements count
1910     __ movl2ptr(src_pos, SRC_POS);  // reload src_pos
1911     __ lea(from, Address(src, src_pos, Address::times_ptr,
1912                  arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // src_addr
1913     __ movl2ptr(dst_pos, DST_POS);  // reload dst_pos
1914     __ lea(to,   Address(dst, dst_pos, Address::times_ptr,
1915                  arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // dst_addr
1916     __ movptr(FROM,  from);   // src_addr
1917     __ movptr(TO,    to);     // dst_addr
1918     __ movl(COUNT, count);  // count
1919     __ jump(RuntimeAddress(entry_oop_arraycopy));
1920 
1921   __ BIND(L_checkcast_copy);
1922     // live at this point:  rcx_src_klass, dst[_pos], src[_pos]
1923     {
1924       // Handy offsets:
1925       int  ek_offset = (klassOopDesc::header_size() * HeapWordSize +
1926                         objArrayKlass::element_klass_offset_in_bytes());
1927       int sco_offset = (klassOopDesc::header_size() * HeapWordSize +
1928                         Klass::super_check_offset_offset_in_bytes());
1929 
1930       Register rsi_dst_klass = rsi;
1931       Register rdi_temp      = rdi;
1932       assert(rsi_dst_klass == src_pos, "expected alias w/ src_pos");
1933       assert(rdi_temp      == dst_pos, "expected alias w/ dst_pos");
1934       Address dst_klass_lh_addr(rsi_dst_klass, lh_offset);
1935 
1936       // Before looking at dst.length, make sure dst is also an objArray.
1937       __ movptr(rsi_dst_klass, dst_klass_addr);
1938       __ cmpl(dst_klass_lh_addr, objArray_lh);
1939       __ jccb(Assembler::notEqual, L_failed);
1940 
1941       // It is safe to examine both src.length and dst.length.
1942       __ movl2ptr(src_pos, SRC_POS);        // reload rsi
1943       arraycopy_range_checks(src, src_pos, dst, dst_pos, LENGTH, L_failed);
1944       // (Now src_pos and dst_pos are killed, but not src and dst.)
1945 
1946       // We'll need this temp (don't forget to pop it after the type check).
1947       __ push(rbx);
1948       Register rbx_src_klass = rbx;
1949 
1950       __ mov(rbx_src_klass, rcx_src_klass); // spill away from rcx
1951       __ movptr(rsi_dst_klass, dst_klass_addr);
1952       Address super_check_offset_addr(rsi_dst_klass, sco_offset);
1953       Label L_fail_array_check;
1954       generate_type_check(rbx_src_klass,
1955                           super_check_offset_addr, dst_klass_addr,
1956                           rdi_temp, NULL, &L_fail_array_check);
1957       // (On fall-through, we have passed the array type check.)
1958       __ pop(rbx);
1959       __ jmp(L_plain_copy);
1960 
1961       __ BIND(L_fail_array_check);
1962       // Reshuffle arguments so we can call checkcast_arraycopy:
1963 
1964       // match initial saves for checkcast_arraycopy
1965       // push(rsi);    // already done; see above
1966       // push(rdi);    // already done; see above
1967       // push(rbx);    // already done; see above
1968 
1969       // Marshal outgoing arguments now, freeing registers.
1970       Address   from_arg(rsp, 16+ 4);   // from
1971       Address     to_arg(rsp, 16+ 8);   // to
1972       Address length_arg(rsp, 16+12);   // elements count
1973       Address  ckoff_arg(rsp, 16+16);   // super_check_offset
1974       Address  ckval_arg(rsp, 16+20);   // super_klass
1975 
1976       Address SRC_POS_arg(rsp, 16+ 8);
1977       Address DST_POS_arg(rsp, 16+16);
1978       Address  LENGTH_arg(rsp, 16+20);
1979       // push rbx, changed the incoming offsets (why not just use rbp,??)
1980       // assert(SRC_POS_arg.disp() == SRC_POS.disp() + 4, "");
1981 
1982       __ movptr(rbx, Address(rsi_dst_klass, ek_offset));
1983       __ movl2ptr(length, LENGTH_arg);    // reload elements count
1984       __ movl2ptr(src_pos, SRC_POS_arg);  // reload src_pos
1985       __ movl2ptr(dst_pos, DST_POS_arg);  // reload dst_pos
1986 
1987       __ movptr(ckval_arg, rbx);          // destination element type
1988       __ movl(rbx, Address(rbx, sco_offset));
1989       __ movl(ckoff_arg, rbx);          // corresponding class check offset
1990 
1991       __ movl(length_arg, length);      // outgoing length argument
1992 
1993       __ lea(from, Address(src, src_pos, Address::times_ptr,
1994                             arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
1995       __ movptr(from_arg, from);
1996 
1997       __ lea(to, Address(dst, dst_pos, Address::times_ptr,
1998                           arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
1999       __ movptr(to_arg, to);
2000       __ jump(RuntimeAddress(entry_checkcast_arraycopy));
2001     }
2002 
2003     return start;
2004   }
2005 
2006   void generate_arraycopy_stubs() {
2007     address entry;
2008     address entry_jbyte_arraycopy;
2009     address entry_jshort_arraycopy;
2010     address entry_jint_arraycopy;
2011     address entry_oop_arraycopy;
2012     address entry_jlong_arraycopy;
2013     address entry_checkcast_arraycopy;
2014 
2015     StubRoutines::_arrayof_jbyte_disjoint_arraycopy =
2016         generate_disjoint_copy(T_BYTE,  true, Address::times_1, &entry,
2017                                "arrayof_jbyte_disjoint_arraycopy");
2018     StubRoutines::_arrayof_jbyte_arraycopy =
2019         generate_conjoint_copy(T_BYTE,  true, Address::times_1,  entry,
2020                                NULL, "arrayof_jbyte_arraycopy");
2021     StubRoutines::_jbyte_disjoint_arraycopy =
2022         generate_disjoint_copy(T_BYTE, false, Address::times_1, &entry,
2023                                "jbyte_disjoint_arraycopy");
2024     StubRoutines::_jbyte_arraycopy =
2025         generate_conjoint_copy(T_BYTE, false, Address::times_1,  entry,
2026                                &entry_jbyte_arraycopy, "jbyte_arraycopy");
2027 
2028     StubRoutines::_arrayof_jshort_disjoint_arraycopy =
2029         generate_disjoint_copy(T_SHORT,  true, Address::times_2, &entry,
2030                                "arrayof_jshort_disjoint_arraycopy");
2031     StubRoutines::_arrayof_jshort_arraycopy =
2032         generate_conjoint_copy(T_SHORT,  true, Address::times_2,  entry,
2033                                NULL, "arrayof_jshort_arraycopy");
2034     StubRoutines::_jshort_disjoint_arraycopy =
2035         generate_disjoint_copy(T_SHORT, false, Address::times_2, &entry,
2036                                "jshort_disjoint_arraycopy");
2037     StubRoutines::_jshort_arraycopy =
2038         generate_conjoint_copy(T_SHORT, false, Address::times_2,  entry,
2039                                &entry_jshort_arraycopy, "jshort_arraycopy");
2040 
2041     // Next arrays are always aligned on 4 bytes at least.
2042     StubRoutines::_jint_disjoint_arraycopy =
2043         generate_disjoint_copy(T_INT, true, Address::times_4, &entry,
2044                                "jint_disjoint_arraycopy");
2045     StubRoutines::_jint_arraycopy =
2046         generate_conjoint_copy(T_INT, true, Address::times_4,  entry,
2047                                &entry_jint_arraycopy, "jint_arraycopy");
2048 
2049     StubRoutines::_oop_disjoint_arraycopy =
2050         generate_disjoint_copy(T_OBJECT, true, Address::times_ptr, &entry,
2051                                "oop_disjoint_arraycopy");
2052     StubRoutines::_oop_arraycopy =
2053         generate_conjoint_copy(T_OBJECT, true, Address::times_ptr,  entry,
2054                                &entry_oop_arraycopy, "oop_arraycopy");
2055 
2056     StubRoutines::_jlong_disjoint_arraycopy =
2057         generate_disjoint_long_copy(&entry, "jlong_disjoint_arraycopy");
2058     StubRoutines::_jlong_arraycopy =
2059         generate_conjoint_long_copy(entry, &entry_jlong_arraycopy,
2060                                     "jlong_arraycopy");
2061 
2062     StubRoutines::_jbyte_fill = generate_fill(T_BYTE, false, "jbyte_fill");
2063     StubRoutines::_jshort_fill = generate_fill(T_SHORT, false, "jshort_fill");
2064     StubRoutines::_jint_fill = generate_fill(T_INT, false, "jint_fill");
2065     StubRoutines::_arrayof_jbyte_fill = generate_fill(T_BYTE, true, "arrayof_jbyte_fill");
2066     StubRoutines::_arrayof_jshort_fill = generate_fill(T_SHORT, true, "arrayof_jshort_fill");
2067     StubRoutines::_arrayof_jint_fill = generate_fill(T_INT, true, "arrayof_jint_fill");
2068 
2069     StubRoutines::_arrayof_jint_disjoint_arraycopy  =
2070         StubRoutines::_jint_disjoint_arraycopy;
2071     StubRoutines::_arrayof_oop_disjoint_arraycopy   =
2072         StubRoutines::_oop_disjoint_arraycopy;
2073     StubRoutines::_arrayof_jlong_disjoint_arraycopy =
2074         StubRoutines::_jlong_disjoint_arraycopy;
2075 
2076     StubRoutines::_arrayof_jint_arraycopy  = StubRoutines::_jint_arraycopy;
2077     StubRoutines::_arrayof_oop_arraycopy   = StubRoutines::_oop_arraycopy;
2078     StubRoutines::_arrayof_jlong_arraycopy = StubRoutines::_jlong_arraycopy;
2079 
2080     StubRoutines::_checkcast_arraycopy =
2081         generate_checkcast_copy("checkcast_arraycopy",
2082                                   &entry_checkcast_arraycopy);
2083 
2084     StubRoutines::_unsafe_arraycopy =
2085         generate_unsafe_copy("unsafe_arraycopy",
2086                                entry_jbyte_arraycopy,
2087                                entry_jshort_arraycopy,
2088                                entry_jint_arraycopy,
2089                                entry_jlong_arraycopy);
2090 
2091     StubRoutines::_generic_arraycopy =
2092         generate_generic_copy("generic_arraycopy",
2093                                entry_jbyte_arraycopy,
2094                                entry_jshort_arraycopy,
2095                                entry_jint_arraycopy,
2096                                entry_oop_arraycopy,
2097                                entry_jlong_arraycopy,
2098                                entry_checkcast_arraycopy);
2099   }
2100 
2101   void generate_math_stubs() {
2102     {
2103       StubCodeMark mark(this, "StubRoutines", "log");
2104       StubRoutines::_intrinsic_log = (double (*)(double)) __ pc();
2105 
2106       __ fld_d(Address(rsp, 4));
2107       __ flog();
2108       __ ret(0);
2109     }
2110     {
2111       StubCodeMark mark(this, "StubRoutines", "log10");
2112       StubRoutines::_intrinsic_log10 = (double (*)(double)) __ pc();
2113 
2114       __ fld_d(Address(rsp, 4));
2115       __ flog10();
2116       __ ret(0);
2117     }
2118     {
2119       StubCodeMark mark(this, "StubRoutines", "sin");
2120       StubRoutines::_intrinsic_sin = (double (*)(double))  __ pc();
2121 
2122       __ fld_d(Address(rsp, 4));
2123       __ trigfunc('s');
2124       __ ret(0);
2125     }
2126     {
2127       StubCodeMark mark(this, "StubRoutines", "cos");
2128       StubRoutines::_intrinsic_cos = (double (*)(double)) __ pc();
2129 
2130       __ fld_d(Address(rsp, 4));
2131       __ trigfunc('c');
2132       __ ret(0);
2133     }
2134     {
2135       StubCodeMark mark(this, "StubRoutines", "tan");
2136       StubRoutines::_intrinsic_tan = (double (*)(double)) __ pc();
2137 
2138       __ fld_d(Address(rsp, 4));
2139       __ trigfunc('t');
2140       __ ret(0);
2141     }
2142 
2143     // The intrinsic version of these seem to return the same value as
2144     // the strict version.
2145     StubRoutines::_intrinsic_exp = SharedRuntime::dexp;
2146     StubRoutines::_intrinsic_pow = SharedRuntime::dpow;
2147   }
2148 
2149  public:
2150   // Information about frame layout at time of blocking runtime call.
2151   // Note that we only have to preserve callee-saved registers since
2152   // the compilers are responsible for supplying a continuation point
2153   // if they expect all registers to be preserved.
2154   enum layout {
2155     thread_off,    // last_java_sp
2156     rbp_off,       // callee saved register
2157     ret_pc,
2158     framesize
2159   };
2160 
2161  private:
2162 
2163 #undef  __
2164 #define __ masm->
2165 
2166   //------------------------------------------------------------------------------------------------------------------------
2167   // Continuation point for throwing of implicit exceptions that are not handled in
2168   // the current activation. Fabricates an exception oop and initiates normal
2169   // exception dispatching in this frame.
2170   //
2171   // Previously the compiler (c2) allowed for callee save registers on Java calls.
2172   // This is no longer true after adapter frames were removed but could possibly
2173   // be brought back in the future if the interpreter code was reworked and it
2174   // was deemed worthwhile. The comment below was left to describe what must
2175   // happen here if callee saves were resurrected. As it stands now this stub
2176   // could actually be a vanilla BufferBlob and have now oopMap at all.
2177   // Since it doesn't make much difference we've chosen to leave it the
2178   // way it was in the callee save days and keep the comment.
2179 
2180   // If we need to preserve callee-saved values we need a callee-saved oop map and
2181   // therefore have to make these stubs into RuntimeStubs rather than BufferBlobs.
2182   // If the compiler needs all registers to be preserved between the fault
2183   // point and the exception handler then it must assume responsibility for that in
2184   // AbstractCompiler::continuation_for_implicit_null_exception or
2185   // continuation_for_implicit_division_by_zero_exception. All other implicit
2186   // exceptions (e.g., NullPointerException or AbstractMethodError on entry) are
2187   // either at call sites or otherwise assume that stack unwinding will be initiated,
2188   // so caller saved registers were assumed volatile in the compiler.
2189   address generate_throw_exception(const char* name, address runtime_entry,
2190                                    bool restore_saved_exception_pc) {
2191 
2192     int insts_size = 256;
2193     int locs_size  = 32;
2194 
2195     CodeBuffer code(name, insts_size, locs_size);
2196     OopMapSet* oop_maps  = new OopMapSet();
2197     MacroAssembler* masm = new MacroAssembler(&code);
2198 
2199     address start = __ pc();
2200 
2201     // This is an inlined and slightly modified version of call_VM
2202     // which has the ability to fetch the return PC out of
2203     // thread-local storage and also sets up last_Java_sp slightly
2204     // differently than the real call_VM
2205     Register java_thread = rbx;
2206     __ get_thread(java_thread);
2207     if (restore_saved_exception_pc) {
2208       __ movptr(rax, Address(java_thread, in_bytes(JavaThread::saved_exception_pc_offset())));
2209       __ push(rax);
2210     }
2211 
2212     __ enter(); // required for proper stackwalking of RuntimeStub frame
2213 
2214     // pc and rbp, already pushed
2215     __ subptr(rsp, (framesize-2) * wordSize); // prolog
2216 
2217     // Frame is now completed as far as size and linkage.
2218 
2219     int frame_complete = __ pc() - start;
2220 
2221     // push java thread (becomes first argument of C function)
2222     __ movptr(Address(rsp, thread_off * wordSize), java_thread);
2223 
2224     // Set up last_Java_sp and last_Java_fp
2225     __ set_last_Java_frame(java_thread, rsp, rbp, NULL);
2226 
2227     // Call runtime
2228     BLOCK_COMMENT("call runtime_entry");
2229     __ call(RuntimeAddress(runtime_entry));
2230     // Generate oop map
2231     OopMap* map =  new OopMap(framesize, 0);
2232     oop_maps->add_gc_map(__ pc() - start, map);
2233 
2234     // restore the thread (cannot use the pushed argument since arguments
2235     // may be overwritten by C code generated by an optimizing compiler);
2236     // however can use the register value directly if it is callee saved.
2237     __ get_thread(java_thread);
2238 
2239     __ reset_last_Java_frame(java_thread, true, false);
2240 
2241     __ leave(); // required for proper stackwalking of RuntimeStub frame
2242 
2243     // check for pending exceptions
2244 #ifdef ASSERT
2245     Label L;
2246     __ cmpptr(Address(java_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
2247     __ jcc(Assembler::notEqual, L);
2248     __ should_not_reach_here();
2249     __ bind(L);
2250 #endif /* ASSERT */
2251     __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
2252 
2253 
2254     RuntimeStub* stub = RuntimeStub::new_runtime_stub(name, &code, frame_complete, framesize, oop_maps, false);
2255     return stub->entry_point();
2256   }
2257 
2258 
2259   void create_control_words() {
2260     // Round to nearest, 53-bit mode, exceptions masked
2261     StubRoutines::_fpu_cntrl_wrd_std   = 0x027F;
2262     // Round to zero, 53-bit mode, exception mased
2263     StubRoutines::_fpu_cntrl_wrd_trunc = 0x0D7F;
2264     // Round to nearest, 24-bit mode, exceptions masked
2265     StubRoutines::_fpu_cntrl_wrd_24    = 0x007F;
2266     // Round to nearest, 64-bit mode, exceptions masked
2267     StubRoutines::_fpu_cntrl_wrd_64    = 0x037F;
2268     // Round to nearest, 64-bit mode, exceptions masked
2269     StubRoutines::_mxcsr_std           = 0x1F80;
2270     // Note: the following two constants are 80-bit values
2271     //       layout is critical for correct loading by FPU.
2272     // Bias for strict fp multiply/divide
2273     StubRoutines::_fpu_subnormal_bias1[0]= 0x00000000; // 2^(-15360) == 0x03ff 8000 0000 0000 0000
2274     StubRoutines::_fpu_subnormal_bias1[1]= 0x80000000;
2275     StubRoutines::_fpu_subnormal_bias1[2]= 0x03ff;
2276     // Un-Bias for strict fp multiply/divide
2277     StubRoutines::_fpu_subnormal_bias2[0]= 0x00000000; // 2^(+15360) == 0x7bff 8000 0000 0000 0000
2278     StubRoutines::_fpu_subnormal_bias2[1]= 0x80000000;
2279     StubRoutines::_fpu_subnormal_bias2[2]= 0x7bff;
2280   }
2281 
2282   //---------------------------------------------------------------------------
2283   // Initialization
2284 
2285   void generate_initial() {
2286     // Generates all stubs and initializes the entry points
2287 
2288     //------------------------------------------------------------------------------------------------------------------------
2289     // entry points that exist in all platforms
2290     // Note: This is code that could be shared among different platforms - however the benefit seems to be smaller than
2291     //       the disadvantage of having a much more complicated generator structure. See also comment in stubRoutines.hpp.
2292     StubRoutines::_forward_exception_entry      = generate_forward_exception();
2293 
2294     StubRoutines::_call_stub_entry              =
2295       generate_call_stub(StubRoutines::_call_stub_return_address);
2296     // is referenced by megamorphic call
2297     StubRoutines::_catch_exception_entry        = generate_catch_exception();
2298 
2299     // These are currently used by Solaris/Intel
2300     StubRoutines::_atomic_xchg_entry            = generate_atomic_xchg();
2301 
2302     StubRoutines::_handler_for_unsafe_access_entry =
2303       generate_handler_for_unsafe_access();
2304 
2305     // platform dependent
2306     create_control_words();
2307 
2308     StubRoutines::x86::_verify_mxcsr_entry                 = generate_verify_mxcsr();
2309     StubRoutines::x86::_verify_fpu_cntrl_wrd_entry         = generate_verify_fpu_cntrl_wrd();
2310     StubRoutines::_d2i_wrapper                              = generate_d2i_wrapper(T_INT,
2311                                                                                    CAST_FROM_FN_PTR(address, SharedRuntime::d2i));
2312     StubRoutines::_d2l_wrapper                              = generate_d2i_wrapper(T_LONG,
2313                                                                                    CAST_FROM_FN_PTR(address, SharedRuntime::d2l));
2314   }
2315 
2316 
2317   void generate_all() {
2318     // Generates all stubs and initializes the entry points
2319 
2320     // These entry points require SharedInfo::stack0 to be set up in non-core builds
2321     // and need to be relocatable, so they each fabricate a RuntimeStub internally.
2322     StubRoutines::_throw_AbstractMethodError_entry         = generate_throw_exception("AbstractMethodError throw_exception",          CAST_FROM_FN_PTR(address, SharedRuntime::throw_AbstractMethodError),  false);
2323     StubRoutines::_throw_IncompatibleClassChangeError_entry= generate_throw_exception("IncompatibleClassChangeError throw_exception", CAST_FROM_FN_PTR(address, SharedRuntime::throw_IncompatibleClassChangeError),  false);
2324     StubRoutines::_throw_ArithmeticException_entry         = generate_throw_exception("ArithmeticException throw_exception",          CAST_FROM_FN_PTR(address, SharedRuntime::throw_ArithmeticException),  true);
2325     StubRoutines::_throw_NullPointerException_entry        = generate_throw_exception("NullPointerException throw_exception",         CAST_FROM_FN_PTR(address, SharedRuntime::throw_NullPointerException), true);
2326     StubRoutines::_throw_NullPointerException_at_call_entry= generate_throw_exception("NullPointerException at call throw_exception", CAST_FROM_FN_PTR(address, SharedRuntime::throw_NullPointerException_at_call), false);
2327     StubRoutines::_throw_StackOverflowError_entry          = generate_throw_exception("StackOverflowError throw_exception",           CAST_FROM_FN_PTR(address, SharedRuntime::throw_StackOverflowError),   false);
2328 
2329     //------------------------------------------------------------------------------------------------------------------------
2330     // entry points that are platform specific
2331 
2332     // support for verify_oop (must happen after universe_init)
2333     StubRoutines::_verify_oop_subroutine_entry     = generate_verify_oop();
2334 
2335     // arraycopy stubs used by compilers
2336     generate_arraycopy_stubs();
2337 
2338     generate_math_stubs();
2339   }
2340 
2341 
2342  public:
2343   StubGenerator(CodeBuffer* code, bool all) : StubCodeGenerator(code) {
2344     if (all) {
2345       generate_all();
2346     } else {
2347       generate_initial();
2348     }
2349   }
2350 }; // end class declaration
2351 
2352 
2353 void StubGenerator_generate(CodeBuffer* code, bool all) {
2354   StubGenerator g(code, all);
2355 }