1 /*
   2  * Copyright (c) 2003, 2013, 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/macroAssembler.hpp"
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "interpreter/interpreter.hpp"
  29 #include "nativeInst_x86.hpp"
  30 #include "oops/instanceOop.hpp"
  31 #include "oops/method.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 "runtime/thread.inline.hpp"
  41 #include "utilities/top.hpp"
  42 #ifdef COMPILER2
  43 #include "opto/runtime.hpp"
  44 #endif
  45 
  46 // Declaration and definition of StubGenerator (no .hpp file).
  47 // For a more detailed description of the stub routine structure
  48 // see the comment in stubRoutines.hpp
  49 
  50 #define __ _masm->
  51 #define TIMES_OOP (UseCompressedOops ? Address::times_4 : Address::times_8)
  52 #define a__ ((Assembler*)_masm)->
  53 
  54 #ifdef PRODUCT
  55 #define BLOCK_COMMENT(str) /* nothing */
  56 #else
  57 #define BLOCK_COMMENT(str) __ block_comment(str)
  58 #endif
  59 
  60 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
  61 const int MXCSR_MASK = 0xFFC0;  // Mask out any pending exceptions
  62 
  63 // Stub Code definitions
  64 
  65 static address handle_unsafe_access() {
  66   JavaThread* thread = JavaThread::current();
  67   address pc = thread->saved_exception_pc();
  68   // pc is the instruction which we must emulate
  69   // doing a no-op is fine:  return garbage from the load
  70   // therefore, compute npc
  71   address npc = Assembler::locate_next_instruction(pc);
  72 
  73   // request an async exception
  74   thread->set_pending_unsafe_access_error();
  75 
  76   // return address of next instruction to execute
  77   return npc;
  78 }
  79 
  80 class StubGenerator: public StubCodeGenerator {
  81  private:
  82 
  83 #ifdef PRODUCT
  84 #define inc_counter_np(counter) ((void)0)
  85 #else
  86   void inc_counter_np_(int& counter) {
  87     // This can destroy rscratch1 if counter is far from the code cache
  88     __ incrementl(ExternalAddress((address)&counter));
  89   }
  90 #define inc_counter_np(counter) \
  91   BLOCK_COMMENT("inc_counter " #counter); \
  92   inc_counter_np_(counter);
  93 #endif
  94 
  95   // Call stubs are used to call Java from C
  96   //
  97   // Linux Arguments:
  98   //    c_rarg0:   call wrapper address                   address
  99   //    c_rarg1:   result                                 address
 100   //    c_rarg2:   result type                            BasicType
 101   //    c_rarg3:   method                                 Method*
 102   //    c_rarg4:   (interpreter) entry point              address
 103   //    c_rarg5:   parameters                             intptr_t*
 104   //    16(rbp): parameter size (in words)              int
 105   //    24(rbp): thread                                 Thread*
 106   //
 107   //     [ return_from_Java     ] <--- rsp
 108   //     [ argument word n      ]
 109   //      ...
 110   // -12 [ argument word 1      ]
 111   // -11 [ saved r15            ] <--- rsp_after_call
 112   // -10 [ saved r14            ]
 113   //  -9 [ saved r13            ]
 114   //  -8 [ saved r12            ]
 115   //  -7 [ saved rbx            ]
 116   //  -6 [ call wrapper         ]
 117   //  -5 [ result               ]
 118   //  -4 [ result type          ]
 119   //  -3 [ method               ]
 120   //  -2 [ entry point          ]
 121   //  -1 [ parameters           ]
 122   //   0 [ saved rbp            ] <--- rbp
 123   //   1 [ return address       ]
 124   //   2 [ parameter size       ]
 125   //   3 [ thread               ]
 126   //
 127   // Windows Arguments:
 128   //    c_rarg0:   call wrapper address                   address
 129   //    c_rarg1:   result                                 address
 130   //    c_rarg2:   result type                            BasicType
 131   //    c_rarg3:   method                                 Method*
 132   //    48(rbp): (interpreter) entry point              address
 133   //    56(rbp): parameters                             intptr_t*
 134   //    64(rbp): parameter size (in words)              int
 135   //    72(rbp): thread                                 Thread*
 136   //
 137   //     [ return_from_Java     ] <--- rsp
 138   //     [ argument word n      ]
 139   //      ...
 140   // -28 [ argument word 1      ]
 141   // -27 [ saved xmm15          ] <--- rsp_after_call
 142   //     [ saved xmm7-xmm14     ]
 143   //  -9 [ saved xmm6           ] (each xmm register takes 2 slots)
 144   //  -7 [ saved r15            ]
 145   //  -6 [ saved r14            ]
 146   //  -5 [ saved r13            ]
 147   //  -4 [ saved r12            ]
 148   //  -3 [ saved rdi            ]
 149   //  -2 [ saved rsi            ]
 150   //  -1 [ saved rbx            ]
 151   //   0 [ saved rbp            ] <--- rbp
 152   //   1 [ return address       ]
 153   //   2 [ call wrapper         ]
 154   //   3 [ result               ]
 155   //   4 [ result type          ]
 156   //   5 [ method               ]
 157   //   6 [ entry point          ]
 158   //   7 [ parameters           ]
 159   //   8 [ parameter size       ]
 160   //   9 [ thread               ]
 161   //
 162   //    Windows reserves the callers stack space for arguments 1-4.
 163   //    We spill c_rarg0-c_rarg3 to this space.
 164 
 165   // Call stub stack layout word offsets from rbp
 166   enum call_stub_layout {
 167 #ifdef _WIN64
 168     xmm_save_first     = 6,  // save from xmm6
 169     xmm_save_last      = 15, // to xmm15
 170     xmm_save_base      = -9,
 171     rsp_after_call_off = xmm_save_base - 2 * (xmm_save_last - xmm_save_first), // -27
 172     r15_off            = -7,
 173     r14_off            = -6,
 174     r13_off            = -5,
 175     r12_off            = -4,
 176     rdi_off            = -3,
 177     rsi_off            = -2,
 178     rbx_off            = -1,
 179     rbp_off            =  0,
 180     retaddr_off        =  1,
 181     call_wrapper_off   =  2,
 182     result_off         =  3,
 183     result_type_off    =  4,
 184     method_off         =  5,
 185     entry_point_off    =  6,
 186     parameters_off     =  7,
 187     parameter_size_off =  8,
 188     thread_off         =  9
 189 #else
 190     rsp_after_call_off = -12,
 191     mxcsr_off          = rsp_after_call_off,
 192     r15_off            = -11,
 193     r14_off            = -10,
 194     r13_off            = -9,
 195     r12_off            = -8,
 196     rbx_off            = -7,
 197     call_wrapper_off   = -6,
 198     result_off         = -5,
 199     result_type_off    = -4,
 200     method_off         = -3,
 201     entry_point_off    = -2,
 202     parameters_off     = -1,
 203     rbp_off            =  0,
 204     retaddr_off        =  1,
 205     parameter_size_off =  2,
 206     thread_off         =  3
 207 #endif
 208   };
 209 
 210 #ifdef _WIN64
 211   Address xmm_save(int reg) {
 212     assert(reg >= xmm_save_first && reg <= xmm_save_last, "XMM register number out of range");
 213     return Address(rbp, (xmm_save_base - (reg - xmm_save_first) * 2) * wordSize);
 214   }
 215 #endif
 216 
 217   address generate_call_stub(address& return_address) {
 218     assert((int)frame::entry_frame_after_call_words == -(int)rsp_after_call_off + 1 &&
 219            (int)frame::entry_frame_call_wrapper_offset == (int)call_wrapper_off,
 220            "adjust this code");
 221     StubCodeMark mark(this, "StubRoutines", "call_stub");
 222     address start = __ pc();
 223 
 224     // same as in generate_catch_exception()!
 225     const Address rsp_after_call(rbp, rsp_after_call_off * wordSize);
 226 
 227     const Address call_wrapper  (rbp, call_wrapper_off   * wordSize);
 228     const Address result        (rbp, result_off         * wordSize);
 229     const Address result_type   (rbp, result_type_off    * wordSize);
 230     const Address method        (rbp, method_off         * wordSize);
 231     const Address entry_point   (rbp, entry_point_off    * wordSize);
 232     const Address parameters    (rbp, parameters_off     * wordSize);
 233     const Address parameter_size(rbp, parameter_size_off * wordSize);
 234 
 235     // same as in generate_catch_exception()!
 236     const Address thread        (rbp, thread_off         * wordSize);
 237 
 238     const Address r15_save(rbp, r15_off * wordSize);
 239     const Address r14_save(rbp, r14_off * wordSize);
 240     const Address r13_save(rbp, r13_off * wordSize);
 241     const Address r12_save(rbp, r12_off * wordSize);
 242     const Address rbx_save(rbp, rbx_off * wordSize);
 243 
 244     // stub code
 245     __ enter();
 246     __ subptr(rsp, -rsp_after_call_off * wordSize);
 247 
 248     // save register parameters
 249 #ifndef _WIN64
 250     __ movptr(parameters,   c_rarg5); // parameters
 251     __ movptr(entry_point,  c_rarg4); // entry_point
 252 #endif
 253 
 254     __ movptr(method,       c_rarg3); // method
 255     __ movl(result_type,  c_rarg2);   // result type
 256     __ movptr(result,       c_rarg1); // result
 257     __ movptr(call_wrapper, c_rarg0); // call wrapper
 258 
 259     // save regs belonging to calling function
 260     __ movptr(rbx_save, rbx);
 261     __ movptr(r12_save, r12);
 262     __ movptr(r13_save, r13);
 263     __ movptr(r14_save, r14);
 264     __ movptr(r15_save, r15);
 265 #ifdef _WIN64
 266     for (int i = 6; i <= 15; i++) {
 267       __ movdqu(xmm_save(i), as_XMMRegister(i));
 268     }
 269 
 270     const Address rdi_save(rbp, rdi_off * wordSize);
 271     const Address rsi_save(rbp, rsi_off * wordSize);
 272 
 273     __ movptr(rsi_save, rsi);
 274     __ movptr(rdi_save, rdi);
 275 #else
 276     const Address mxcsr_save(rbp, mxcsr_off * wordSize);
 277     {
 278       Label skip_ldmx;
 279       __ stmxcsr(mxcsr_save);
 280       __ movl(rax, mxcsr_save);
 281       __ andl(rax, MXCSR_MASK);    // Only check control and mask bits
 282       ExternalAddress mxcsr_std(StubRoutines::addr_mxcsr_std());
 283       __ cmp32(rax, mxcsr_std);
 284       __ jcc(Assembler::equal, skip_ldmx);
 285       __ ldmxcsr(mxcsr_std);
 286       __ bind(skip_ldmx);
 287     }
 288 #endif
 289 
 290     // Load up thread register
 291     __ movptr(r15_thread, thread);
 292     __ reinit_heapbase();
 293 
 294 #ifdef ASSERT
 295     // make sure we have no pending exceptions
 296     {
 297       Label L;
 298       __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 299       __ jcc(Assembler::equal, L);
 300       __ stop("StubRoutines::call_stub: entered with pending exception");
 301       __ bind(L);
 302     }
 303 #endif
 304 
 305     // pass parameters if any
 306     BLOCK_COMMENT("pass parameters if any");
 307     Label parameters_done;
 308     __ movl(c_rarg3, parameter_size);
 309     __ testl(c_rarg3, c_rarg3);
 310     __ jcc(Assembler::zero, parameters_done);
 311 
 312     Label loop;
 313     __ movptr(c_rarg2, parameters);       // parameter pointer
 314     __ movl(c_rarg1, c_rarg3);            // parameter counter is in c_rarg1
 315     __ BIND(loop);
 316     __ movptr(rax, Address(c_rarg2, 0));// get parameter
 317     __ addptr(c_rarg2, wordSize);       // advance to next parameter
 318     __ decrementl(c_rarg1);             // decrement counter
 319     __ push(rax);                       // pass parameter
 320     __ jcc(Assembler::notZero, loop);
 321 
 322     // call Java function
 323     __ BIND(parameters_done);
 324     __ movptr(rbx, method);             // get Method*
 325     __ movptr(c_rarg1, entry_point);    // get entry_point
 326     __ mov(r13, rsp);                   // set sender sp
 327     BLOCK_COMMENT("call Java function");
 328     __ call(c_rarg1);
 329 
 330     BLOCK_COMMENT("call_stub_return_address:");
 331     return_address = __ pc();
 332 
 333     // store result depending on type (everything that is not
 334     // T_OBJECT, T_LONG, T_FLOAT or T_DOUBLE is treated as T_INT)
 335     __ movptr(c_rarg0, result);
 336     Label is_long, is_float, is_double, exit;
 337     __ movl(c_rarg1, result_type);
 338     __ cmpl(c_rarg1, T_OBJECT);
 339     __ jcc(Assembler::equal, is_long);
 340     __ cmpl(c_rarg1, T_LONG);
 341     __ jcc(Assembler::equal, is_long);
 342     __ cmpl(c_rarg1, T_FLOAT);
 343     __ jcc(Assembler::equal, is_float);
 344     __ cmpl(c_rarg1, T_DOUBLE);
 345     __ jcc(Assembler::equal, is_double);
 346 
 347     // handle T_INT case
 348     __ movl(Address(c_rarg0, 0), rax);
 349 
 350     __ BIND(exit);
 351 
 352     // pop parameters
 353     __ lea(rsp, rsp_after_call);
 354 
 355 #ifdef ASSERT
 356     // verify that threads correspond
 357     {
 358       Label L, S;
 359       __ cmpptr(r15_thread, thread);
 360       __ jcc(Assembler::notEqual, S);
 361       __ get_thread(rbx);
 362       __ cmpptr(r15_thread, rbx);
 363       __ jcc(Assembler::equal, L);
 364       __ bind(S);
 365       __ jcc(Assembler::equal, L);
 366       __ stop("StubRoutines::call_stub: threads must correspond");
 367       __ bind(L);
 368     }
 369 #endif
 370 
 371     // restore regs belonging to calling function
 372 #ifdef _WIN64
 373     for (int i = 15; i >= 6; i--) {
 374       __ movdqu(as_XMMRegister(i), xmm_save(i));
 375     }
 376 #endif
 377     __ movptr(r15, r15_save);
 378     __ movptr(r14, r14_save);
 379     __ movptr(r13, r13_save);
 380     __ movptr(r12, r12_save);
 381     __ movptr(rbx, rbx_save);
 382 
 383 #ifdef _WIN64
 384     __ movptr(rdi, rdi_save);
 385     __ movptr(rsi, rsi_save);
 386 #else
 387     __ ldmxcsr(mxcsr_save);
 388 #endif
 389 
 390     // restore rsp
 391     __ addptr(rsp, -rsp_after_call_off * wordSize);
 392 
 393     // return
 394     __ pop(rbp);
 395     __ ret(0);
 396 
 397     // handle return types different from T_INT
 398     __ BIND(is_long);
 399     __ movq(Address(c_rarg0, 0), rax);
 400     __ jmp(exit);
 401 
 402     __ BIND(is_float);
 403     __ movflt(Address(c_rarg0, 0), xmm0);
 404     __ jmp(exit);
 405 
 406     __ BIND(is_double);
 407     __ movdbl(Address(c_rarg0, 0), xmm0);
 408     __ jmp(exit);
 409 
 410     return start;
 411   }
 412 
 413   // Return point for a Java call if there's an exception thrown in
 414   // Java code.  The exception is caught and transformed into a
 415   // pending exception stored in JavaThread that can be tested from
 416   // within the VM.
 417   //
 418   // Note: Usually the parameters are removed by the callee. In case
 419   // of an exception crossing an activation frame boundary, that is
 420   // not the case if the callee is compiled code => need to setup the
 421   // rsp.
 422   //
 423   // rax: exception oop
 424 
 425   address generate_catch_exception() {
 426     StubCodeMark mark(this, "StubRoutines", "catch_exception");
 427     address start = __ pc();
 428 
 429     // same as in generate_call_stub():
 430     const Address rsp_after_call(rbp, rsp_after_call_off * wordSize);
 431     const Address thread        (rbp, thread_off         * wordSize);
 432 
 433 #ifdef ASSERT
 434     // verify that threads correspond
 435     {
 436       Label L, S;
 437       __ cmpptr(r15_thread, thread);
 438       __ jcc(Assembler::notEqual, S);
 439       __ get_thread(rbx);
 440       __ cmpptr(r15_thread, rbx);
 441       __ jcc(Assembler::equal, L);
 442       __ bind(S);
 443       __ stop("StubRoutines::catch_exception: threads must correspond");
 444       __ bind(L);
 445     }
 446 #endif
 447 
 448     // set pending exception
 449     __ verify_oop(rax);
 450 
 451     __ movptr(Address(r15_thread, Thread::pending_exception_offset()), rax);
 452     __ lea(rscratch1, ExternalAddress((address)__FILE__));
 453     __ movptr(Address(r15_thread, Thread::exception_file_offset()), rscratch1);
 454     __ movl(Address(r15_thread, Thread::exception_line_offset()), (int)  __LINE__);
 455 
 456     // complete return to VM
 457     assert(StubRoutines::_call_stub_return_address != NULL,
 458            "_call_stub_return_address must have been generated before");
 459     __ jump(RuntimeAddress(StubRoutines::_call_stub_return_address));
 460 
 461     return start;
 462   }
 463 
 464   // Continuation point for runtime calls returning with a pending
 465   // exception.  The pending exception check happened in the runtime
 466   // or native call stub.  The pending exception in Thread is
 467   // converted into a Java-level exception.
 468   //
 469   // Contract with Java-level exception handlers:
 470   // rax: exception
 471   // rdx: throwing pc
 472   //
 473   // NOTE: At entry of this stub, exception-pc must be on stack !!
 474 
 475   address generate_forward_exception() {
 476     StubCodeMark mark(this, "StubRoutines", "forward exception");
 477     address start = __ pc();
 478 
 479     // Upon entry, the sp points to the return address returning into
 480     // Java (interpreted or compiled) code; i.e., the return address
 481     // becomes the throwing pc.
 482     //
 483     // Arguments pushed before the runtime call are still on the stack
 484     // but the exception handler will reset the stack pointer ->
 485     // ignore them.  A potential result in registers can be ignored as
 486     // well.
 487 
 488 #ifdef ASSERT
 489     // make sure this code is only executed if there is a pending exception
 490     {
 491       Label L;
 492       __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t) NULL);
 493       __ jcc(Assembler::notEqual, L);
 494       __ stop("StubRoutines::forward exception: no pending exception (1)");
 495       __ bind(L);
 496     }
 497 #endif
 498 
 499     // compute exception handler into rbx
 500     __ movptr(c_rarg0, Address(rsp, 0));
 501     BLOCK_COMMENT("call exception_handler_for_return_address");
 502     __ call_VM_leaf(CAST_FROM_FN_PTR(address,
 503                          SharedRuntime::exception_handler_for_return_address),
 504                     r15_thread, c_rarg0);
 505     __ mov(rbx, rax);
 506 
 507     // setup rax & rdx, remove return address & clear pending exception
 508     __ pop(rdx);
 509     __ movptr(rax, Address(r15_thread, Thread::pending_exception_offset()));
 510     __ movptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 511 
 512 #ifdef ASSERT
 513     // make sure exception is set
 514     {
 515       Label L;
 516       __ testptr(rax, rax);
 517       __ jcc(Assembler::notEqual, L);
 518       __ stop("StubRoutines::forward exception: no pending exception (2)");
 519       __ bind(L);
 520     }
 521 #endif
 522 
 523     // continue at exception handler (return address removed)
 524     // rax: exception
 525     // rbx: exception handler
 526     // rdx: throwing pc
 527     __ verify_oop(rax);
 528     __ jmp(rbx);
 529 
 530     return start;
 531   }
 532 
 533   // Support for jint atomic::xchg(jint exchange_value, volatile jint* dest)
 534   //
 535   // Arguments :
 536   //    c_rarg0: exchange_value
 537   //    c_rarg0: dest
 538   //
 539   // Result:
 540   //    *dest <- ex, return (orig *dest)
 541   address generate_atomic_xchg() {
 542     StubCodeMark mark(this, "StubRoutines", "atomic_xchg");
 543     address start = __ pc();
 544 
 545     __ movl(rax, c_rarg0); // Copy to eax we need a return value anyhow
 546     __ xchgl(rax, Address(c_rarg1, 0)); // automatic LOCK
 547     __ ret(0);
 548 
 549     return start;
 550   }
 551 
 552   // Support for intptr_t atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest)
 553   //
 554   // Arguments :
 555   //    c_rarg0: exchange_value
 556   //    c_rarg1: dest
 557   //
 558   // Result:
 559   //    *dest <- ex, return (orig *dest)
 560   address generate_atomic_xchg_ptr() {
 561     StubCodeMark mark(this, "StubRoutines", "atomic_xchg_ptr");
 562     address start = __ pc();
 563 
 564     __ movptr(rax, c_rarg0); // Copy to eax we need a return value anyhow
 565     __ xchgptr(rax, Address(c_rarg1, 0)); // automatic LOCK
 566     __ ret(0);
 567 
 568     return start;
 569   }
 570 
 571   // Support for jint atomic::atomic_cmpxchg(jint exchange_value, volatile jint* dest,
 572   //                                         jint compare_value)
 573   //
 574   // Arguments :
 575   //    c_rarg0: exchange_value
 576   //    c_rarg1: dest
 577   //    c_rarg2: compare_value
 578   //
 579   // Result:
 580   //    if ( compare_value == *dest ) {
 581   //       *dest = exchange_value
 582   //       return compare_value;
 583   //    else
 584   //       return *dest;
 585   address generate_atomic_cmpxchg() {
 586     StubCodeMark mark(this, "StubRoutines", "atomic_cmpxchg");
 587     address start = __ pc();
 588 
 589     __ movl(rax, c_rarg2);
 590    if ( os::is_MP() ) __ lock();
 591     __ cmpxchgl(c_rarg0, Address(c_rarg1, 0));
 592     __ ret(0);
 593 
 594     return start;
 595   }
 596 
 597   // Support for jint atomic::atomic_cmpxchg_long(jlong exchange_value,
 598   //                                             volatile jlong* dest,
 599   //                                             jlong compare_value)
 600   // Arguments :
 601   //    c_rarg0: exchange_value
 602   //    c_rarg1: dest
 603   //    c_rarg2: compare_value
 604   //
 605   // Result:
 606   //    if ( compare_value == *dest ) {
 607   //       *dest = exchange_value
 608   //       return compare_value;
 609   //    else
 610   //       return *dest;
 611   address generate_atomic_cmpxchg_long() {
 612     StubCodeMark mark(this, "StubRoutines", "atomic_cmpxchg_long");
 613     address start = __ pc();
 614 
 615     __ movq(rax, c_rarg2);
 616    if ( os::is_MP() ) __ lock();
 617     __ cmpxchgq(c_rarg0, Address(c_rarg1, 0));
 618     __ ret(0);
 619 
 620     return start;
 621   }
 622 
 623   // Support for jint atomic::add(jint add_value, volatile jint* dest)
 624   //
 625   // Arguments :
 626   //    c_rarg0: add_value
 627   //    c_rarg1: dest
 628   //
 629   // Result:
 630   //    *dest += add_value
 631   //    return *dest;
 632   address generate_atomic_add() {
 633     StubCodeMark mark(this, "StubRoutines", "atomic_add");
 634     address start = __ pc();
 635 
 636     __ movl(rax, c_rarg0);
 637    if ( os::is_MP() ) __ lock();
 638     __ xaddl(Address(c_rarg1, 0), c_rarg0);
 639     __ addl(rax, c_rarg0);
 640     __ ret(0);
 641 
 642     return start;
 643   }
 644 
 645   // Support for intptr_t atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest)
 646   //
 647   // Arguments :
 648   //    c_rarg0: add_value
 649   //    c_rarg1: dest
 650   //
 651   // Result:
 652   //    *dest += add_value
 653   //    return *dest;
 654   address generate_atomic_add_ptr() {
 655     StubCodeMark mark(this, "StubRoutines", "atomic_add_ptr");
 656     address start = __ pc();
 657 
 658     __ movptr(rax, c_rarg0); // Copy to eax we need a return value anyhow
 659    if ( os::is_MP() ) __ lock();
 660     __ xaddptr(Address(c_rarg1, 0), c_rarg0);
 661     __ addptr(rax, c_rarg0);
 662     __ ret(0);
 663 
 664     return start;
 665   }
 666 
 667   // Support for intptr_t OrderAccess::fence()
 668   //
 669   // Arguments :
 670   //
 671   // Result:
 672   address generate_orderaccess_fence() {
 673     StubCodeMark mark(this, "StubRoutines", "orderaccess_fence");
 674     address start = __ pc();
 675     __ membar(Assembler::StoreLoad);
 676     __ ret(0);
 677 
 678     return start;
 679   }
 680 
 681   // Support for intptr_t get_previous_fp()
 682   //
 683   // This routine is used to find the previous frame pointer for the
 684   // caller (current_frame_guess). This is used as part of debugging
 685   // ps() is seemingly lost trying to find frames.
 686   // This code assumes that caller current_frame_guess) has a frame.
 687   address generate_get_previous_fp() {
 688     StubCodeMark mark(this, "StubRoutines", "get_previous_fp");
 689     const Address old_fp(rbp, 0);
 690     const Address older_fp(rax, 0);
 691     address start = __ pc();
 692 
 693     __ enter();
 694     __ movptr(rax, old_fp); // callers fp
 695     __ movptr(rax, older_fp); // the frame for ps()
 696     __ pop(rbp);
 697     __ ret(0);
 698 
 699     return start;
 700   }
 701 
 702   // Support for intptr_t get_previous_sp()
 703   //
 704   // This routine is used to find the previous stack pointer for the
 705   // caller.
 706   address generate_get_previous_sp() {
 707     StubCodeMark mark(this, "StubRoutines", "get_previous_sp");
 708     address start = __ pc();
 709 
 710     __ movptr(rax, rsp);
 711     __ addptr(rax, 8); // return address is at the top of the stack.
 712     __ ret(0);
 713 
 714     return start;
 715   }
 716 
 717   //----------------------------------------------------------------------------------------------------
 718   // Support for void verify_mxcsr()
 719   //
 720   // This routine is used with -Xcheck:jni to verify that native
 721   // JNI code does not return to Java code without restoring the
 722   // MXCSR register to our expected state.
 723 
 724   address generate_verify_mxcsr() {
 725     StubCodeMark mark(this, "StubRoutines", "verify_mxcsr");
 726     address start = __ pc();
 727 
 728     const Address mxcsr_save(rsp, 0);
 729 
 730     if (CheckJNICalls) {
 731       Label ok_ret;
 732       ExternalAddress mxcsr_std(StubRoutines::addr_mxcsr_std());
 733       __ push(rax);
 734       __ subptr(rsp, wordSize);      // allocate a temp location
 735       __ stmxcsr(mxcsr_save);
 736       __ movl(rax, mxcsr_save);
 737       __ andl(rax, MXCSR_MASK);    // Only check control and mask bits
 738       __ cmp32(rax, mxcsr_std);
 739       __ jcc(Assembler::equal, ok_ret);
 740 
 741       __ warn("MXCSR changed by native JNI code, use -XX:+RestoreMXCSROnJNICall");
 742 
 743       __ ldmxcsr(mxcsr_std);
 744 
 745       __ bind(ok_ret);
 746       __ addptr(rsp, wordSize);
 747       __ pop(rax);
 748     }
 749 
 750     __ ret(0);
 751 
 752     return start;
 753   }
 754 
 755   address generate_f2i_fixup() {
 756     StubCodeMark mark(this, "StubRoutines", "f2i_fixup");
 757     Address inout(rsp, 5 * wordSize); // return address + 4 saves
 758 
 759     address start = __ pc();
 760 
 761     Label L;
 762 
 763     __ push(rax);
 764     __ push(c_rarg3);
 765     __ push(c_rarg2);
 766     __ push(c_rarg1);
 767 
 768     __ movl(rax, 0x7f800000);
 769     __ xorl(c_rarg3, c_rarg3);
 770     __ movl(c_rarg2, inout);
 771     __ movl(c_rarg1, c_rarg2);
 772     __ andl(c_rarg1, 0x7fffffff);
 773     __ cmpl(rax, c_rarg1); // NaN? -> 0
 774     __ jcc(Assembler::negative, L);
 775     __ testl(c_rarg2, c_rarg2); // signed ? min_jint : max_jint
 776     __ movl(c_rarg3, 0x80000000);
 777     __ movl(rax, 0x7fffffff);
 778     __ cmovl(Assembler::positive, c_rarg3, rax);
 779 
 780     __ bind(L);
 781     __ movptr(inout, c_rarg3);
 782 
 783     __ pop(c_rarg1);
 784     __ pop(c_rarg2);
 785     __ pop(c_rarg3);
 786     __ pop(rax);
 787 
 788     __ ret(0);
 789 
 790     return start;
 791   }
 792 
 793   address generate_f2l_fixup() {
 794     StubCodeMark mark(this, "StubRoutines", "f2l_fixup");
 795     Address inout(rsp, 5 * wordSize); // return address + 4 saves
 796     address start = __ pc();
 797 
 798     Label L;
 799 
 800     __ push(rax);
 801     __ push(c_rarg3);
 802     __ push(c_rarg2);
 803     __ push(c_rarg1);
 804 
 805     __ movl(rax, 0x7f800000);
 806     __ xorl(c_rarg3, c_rarg3);
 807     __ movl(c_rarg2, inout);
 808     __ movl(c_rarg1, c_rarg2);
 809     __ andl(c_rarg1, 0x7fffffff);
 810     __ cmpl(rax, c_rarg1); // NaN? -> 0
 811     __ jcc(Assembler::negative, L);
 812     __ testl(c_rarg2, c_rarg2); // signed ? min_jlong : max_jlong
 813     __ mov64(c_rarg3, 0x8000000000000000);
 814     __ mov64(rax, 0x7fffffffffffffff);
 815     __ cmov(Assembler::positive, c_rarg3, rax);
 816 
 817     __ bind(L);
 818     __ movptr(inout, c_rarg3);
 819 
 820     __ pop(c_rarg1);
 821     __ pop(c_rarg2);
 822     __ pop(c_rarg3);
 823     __ pop(rax);
 824 
 825     __ ret(0);
 826 
 827     return start;
 828   }
 829 
 830   address generate_d2i_fixup() {
 831     StubCodeMark mark(this, "StubRoutines", "d2i_fixup");
 832     Address inout(rsp, 6 * wordSize); // return address + 5 saves
 833 
 834     address start = __ pc();
 835 
 836     Label L;
 837 
 838     __ push(rax);
 839     __ push(c_rarg3);
 840     __ push(c_rarg2);
 841     __ push(c_rarg1);
 842     __ push(c_rarg0);
 843 
 844     __ movl(rax, 0x7ff00000);
 845     __ movq(c_rarg2, inout);
 846     __ movl(c_rarg3, c_rarg2);
 847     __ mov(c_rarg1, c_rarg2);
 848     __ mov(c_rarg0, c_rarg2);
 849     __ negl(c_rarg3);
 850     __ shrptr(c_rarg1, 0x20);
 851     __ orl(c_rarg3, c_rarg2);
 852     __ andl(c_rarg1, 0x7fffffff);
 853     __ xorl(c_rarg2, c_rarg2);
 854     __ shrl(c_rarg3, 0x1f);
 855     __ orl(c_rarg1, c_rarg3);
 856     __ cmpl(rax, c_rarg1);
 857     __ jcc(Assembler::negative, L); // NaN -> 0
 858     __ testptr(c_rarg0, c_rarg0); // signed ? min_jint : max_jint
 859     __ movl(c_rarg2, 0x80000000);
 860     __ movl(rax, 0x7fffffff);
 861     __ cmov(Assembler::positive, c_rarg2, rax);
 862 
 863     __ bind(L);
 864     __ movptr(inout, c_rarg2);
 865 
 866     __ pop(c_rarg0);
 867     __ pop(c_rarg1);
 868     __ pop(c_rarg2);
 869     __ pop(c_rarg3);
 870     __ pop(rax);
 871 
 872     __ ret(0);
 873 
 874     return start;
 875   }
 876 
 877   address generate_d2l_fixup() {
 878     StubCodeMark mark(this, "StubRoutines", "d2l_fixup");
 879     Address inout(rsp, 6 * wordSize); // return address + 5 saves
 880 
 881     address start = __ pc();
 882 
 883     Label L;
 884 
 885     __ push(rax);
 886     __ push(c_rarg3);
 887     __ push(c_rarg2);
 888     __ push(c_rarg1);
 889     __ push(c_rarg0);
 890 
 891     __ movl(rax, 0x7ff00000);
 892     __ movq(c_rarg2, inout);
 893     __ movl(c_rarg3, c_rarg2);
 894     __ mov(c_rarg1, c_rarg2);
 895     __ mov(c_rarg0, c_rarg2);
 896     __ negl(c_rarg3);
 897     __ shrptr(c_rarg1, 0x20);
 898     __ orl(c_rarg3, c_rarg2);
 899     __ andl(c_rarg1, 0x7fffffff);
 900     __ xorl(c_rarg2, c_rarg2);
 901     __ shrl(c_rarg3, 0x1f);
 902     __ orl(c_rarg1, c_rarg3);
 903     __ cmpl(rax, c_rarg1);
 904     __ jcc(Assembler::negative, L); // NaN -> 0
 905     __ testq(c_rarg0, c_rarg0); // signed ? min_jlong : max_jlong
 906     __ mov64(c_rarg2, 0x8000000000000000);
 907     __ mov64(rax, 0x7fffffffffffffff);
 908     __ cmovq(Assembler::positive, c_rarg2, rax);
 909 
 910     __ bind(L);
 911     __ movq(inout, c_rarg2);
 912 
 913     __ pop(c_rarg0);
 914     __ pop(c_rarg1);
 915     __ pop(c_rarg2);
 916     __ pop(c_rarg3);
 917     __ pop(rax);
 918 
 919     __ ret(0);
 920 
 921     return start;
 922   }
 923 
 924   address generate_fp_mask(const char *stub_name, int64_t mask) {
 925     __ align(CodeEntryAlignment);
 926     StubCodeMark mark(this, "StubRoutines", stub_name);
 927     address start = __ pc();
 928 
 929     __ emit_data64( mask, relocInfo::none );
 930     __ emit_data64( mask, relocInfo::none );
 931 
 932     return start;
 933   }
 934 
 935   // The following routine generates a subroutine to throw an
 936   // asynchronous UnknownError when an unsafe access gets a fault that
 937   // could not be reasonably prevented by the programmer.  (Example:
 938   // SIGBUS/OBJERR.)
 939   address generate_handler_for_unsafe_access() {
 940     StubCodeMark mark(this, "StubRoutines", "handler_for_unsafe_access");
 941     address start = __ pc();
 942 
 943     __ push(0);                       // hole for return address-to-be
 944     __ pusha();                       // push registers
 945     Address next_pc(rsp, RegisterImpl::number_of_registers * BytesPerWord);
 946 
 947     // FIXME: this probably needs alignment logic
 948 
 949     __ subptr(rsp, frame::arg_reg_save_area_bytes);
 950     BLOCK_COMMENT("call handle_unsafe_access");
 951     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, handle_unsafe_access)));
 952     __ addptr(rsp, frame::arg_reg_save_area_bytes);
 953 
 954     __ movptr(next_pc, rax);          // stuff next address
 955     __ popa();
 956     __ ret(0);                        // jump to next address
 957 
 958     return start;
 959   }
 960 
 961   // Non-destructive plausibility checks for oops
 962   //
 963   // Arguments:
 964   //    all args on stack!
 965   //
 966   // Stack after saving c_rarg3:
 967   //    [tos + 0]: saved c_rarg3
 968   //    [tos + 1]: saved c_rarg2
 969   //    [tos + 2]: saved r12 (several TemplateTable methods use it)
 970   //    [tos + 3]: saved flags
 971   //    [tos + 4]: return address
 972   //  * [tos + 5]: error message (char*)
 973   //  * [tos + 6]: object to verify (oop)
 974   //  * [tos + 7]: saved rax - saved by caller and bashed
 975   //  * [tos + 8]: saved r10 (rscratch1) - saved by caller
 976   //  * = popped on exit
 977   address generate_verify_oop() {
 978     StubCodeMark mark(this, "StubRoutines", "verify_oop");
 979     address start = __ pc();
 980 
 981     Label exit, error;
 982 
 983     __ pushf();
 984     __ incrementl(ExternalAddress((address) StubRoutines::verify_oop_count_addr()));
 985 
 986     __ push(r12);
 987 
 988     // save c_rarg2 and c_rarg3
 989     __ push(c_rarg2);
 990     __ push(c_rarg3);
 991 
 992     enum {
 993            // After previous pushes.
 994            oop_to_verify = 6 * wordSize,
 995            saved_rax     = 7 * wordSize,
 996            saved_r10     = 8 * wordSize,
 997 
 998            // Before the call to MacroAssembler::debug(), see below.
 999            return_addr   = 16 * wordSize,
1000            error_msg     = 17 * wordSize
1001     };
1002 
1003     // get object
1004     __ movptr(rax, Address(rsp, oop_to_verify));
1005 
1006     // make sure object is 'reasonable'
1007     __ testptr(rax, rax);
1008     __ jcc(Assembler::zero, exit); // if obj is NULL it is OK
1009     // Check if the oop is in the right area of memory
1010     __ movptr(c_rarg2, rax);
1011     __ movptr(c_rarg3, (intptr_t) Universe::verify_oop_mask());
1012     __ andptr(c_rarg2, c_rarg3);
1013     __ movptr(c_rarg3, (intptr_t) Universe::verify_oop_bits());
1014     __ cmpptr(c_rarg2, c_rarg3);
1015     __ jcc(Assembler::notZero, error);
1016 
1017     // set r12 to heapbase for load_klass()
1018     __ reinit_heapbase();
1019 
1020     // make sure klass is 'reasonable', which is not zero.
1021     __ load_klass(rax, rax);  // get klass
1022     __ testptr(rax, rax);
1023     __ jcc(Assembler::zero, error); // if klass is NULL it is broken
1024 
1025     // return if everything seems ok
1026     __ bind(exit);
1027     __ movptr(rax, Address(rsp, saved_rax));     // get saved rax back
1028     __ movptr(rscratch1, Address(rsp, saved_r10)); // get saved r10 back
1029     __ pop(c_rarg3);                             // restore c_rarg3
1030     __ pop(c_rarg2);                             // restore c_rarg2
1031     __ pop(r12);                                 // restore r12
1032     __ popf();                                   // restore flags
1033     __ ret(4 * wordSize);                        // pop caller saved stuff
1034 
1035     // handle errors
1036     __ bind(error);
1037     __ movptr(rax, Address(rsp, saved_rax));     // get saved rax back
1038     __ movptr(rscratch1, Address(rsp, saved_r10)); // get saved r10 back
1039     __ pop(c_rarg3);                             // get saved c_rarg3 back
1040     __ pop(c_rarg2);                             // get saved c_rarg2 back
1041     __ pop(r12);                                 // get saved r12 back
1042     __ popf();                                   // get saved flags off stack --
1043                                                  // will be ignored
1044 
1045     __ pusha();                                  // push registers
1046                                                  // (rip is already
1047                                                  // already pushed)
1048     // debug(char* msg, int64_t pc, int64_t regs[])
1049     // We've popped the registers we'd saved (c_rarg3, c_rarg2 and flags), and
1050     // pushed all the registers, so now the stack looks like:
1051     //     [tos +  0] 16 saved registers
1052     //     [tos + 16] return address
1053     //   * [tos + 17] error message (char*)
1054     //   * [tos + 18] object to verify (oop)
1055     //   * [tos + 19] saved rax - saved by caller and bashed
1056     //   * [tos + 20] saved r10 (rscratch1) - saved by caller
1057     //   * = popped on exit
1058 
1059     __ movptr(c_rarg0, Address(rsp, error_msg));    // pass address of error message
1060     __ movptr(c_rarg1, Address(rsp, return_addr));  // pass return address
1061     __ movq(c_rarg2, rsp);                          // pass address of regs on stack
1062     __ mov(r12, rsp);                               // remember rsp
1063     __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
1064     __ andptr(rsp, -16);                            // align stack as required by ABI
1065     BLOCK_COMMENT("call MacroAssembler::debug");
1066     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, MacroAssembler::debug64)));
1067     __ mov(rsp, r12);                               // restore rsp
1068     __ popa();                                      // pop registers (includes r12)
1069     __ ret(4 * wordSize);                           // pop caller saved stuff
1070 
1071     return start;
1072   }
1073 
1074   //
1075   // Verify that a register contains clean 32-bits positive value
1076   // (high 32-bits are 0) so it could be used in 64-bits shifts.
1077   //
1078   //  Input:
1079   //    Rint  -  32-bits value
1080   //    Rtmp  -  scratch
1081   //
1082   void assert_clean_int(Register Rint, Register Rtmp) {
1083 #ifdef ASSERT
1084     Label L;
1085     assert_different_registers(Rtmp, Rint);
1086     __ movslq(Rtmp, Rint);
1087     __ cmpq(Rtmp, Rint);
1088     __ jcc(Assembler::equal, L);
1089     __ stop("high 32-bits of int value are not 0");
1090     __ bind(L);
1091 #endif
1092   }
1093 
1094   //  Generate overlap test for array copy stubs
1095   //
1096   //  Input:
1097   //     c_rarg0 - from
1098   //     c_rarg1 - to
1099   //     c_rarg2 - element count
1100   //
1101   //  Output:
1102   //     rax   - &from[element count - 1]
1103   //
1104   void array_overlap_test(address no_overlap_target, Address::ScaleFactor sf) {
1105     assert(no_overlap_target != NULL, "must be generated");
1106     array_overlap_test(no_overlap_target, NULL, sf);
1107   }
1108   void array_overlap_test(Label& L_no_overlap, Address::ScaleFactor sf) {
1109     array_overlap_test(NULL, &L_no_overlap, sf);
1110   }
1111   void array_overlap_test(address no_overlap_target, Label* NOLp, Address::ScaleFactor sf) {
1112     const Register from     = c_rarg0;
1113     const Register to       = c_rarg1;
1114     const Register count    = c_rarg2;
1115     const Register end_from = rax;
1116 
1117     __ cmpptr(to, from);
1118     __ lea(end_from, Address(from, count, sf, 0));
1119     if (NOLp == NULL) {
1120       ExternalAddress no_overlap(no_overlap_target);
1121       __ jump_cc(Assembler::belowEqual, no_overlap);
1122       __ cmpptr(to, end_from);
1123       __ jump_cc(Assembler::aboveEqual, no_overlap);
1124     } else {
1125       __ jcc(Assembler::belowEqual, (*NOLp));
1126       __ cmpptr(to, end_from);
1127       __ jcc(Assembler::aboveEqual, (*NOLp));
1128     }
1129   }
1130 
1131   // Shuffle first three arg regs on Windows into Linux/Solaris locations.
1132   //
1133   // Outputs:
1134   //    rdi - rcx
1135   //    rsi - rdx
1136   //    rdx - r8
1137   //    rcx - r9
1138   //
1139   // Registers r9 and r10 are used to save rdi and rsi on Windows, which latter
1140   // are non-volatile.  r9 and r10 should not be used by the caller.
1141   //
1142   void setup_arg_regs(int nargs = 3) {
1143     const Register saved_rdi = r9;
1144     const Register saved_rsi = r10;
1145     assert(nargs == 3 || nargs == 4, "else fix");
1146 #ifdef _WIN64
1147     assert(c_rarg0 == rcx && c_rarg1 == rdx && c_rarg2 == r8 && c_rarg3 == r9,
1148            "unexpected argument registers");
1149     if (nargs >= 4)
1150       __ mov(rax, r9);  // r9 is also saved_rdi
1151     __ movptr(saved_rdi, rdi);
1152     __ movptr(saved_rsi, rsi);
1153     __ mov(rdi, rcx); // c_rarg0
1154     __ mov(rsi, rdx); // c_rarg1
1155     __ mov(rdx, r8);  // c_rarg2
1156     if (nargs >= 4)
1157       __ mov(rcx, rax); // c_rarg3 (via rax)
1158 #else
1159     assert(c_rarg0 == rdi && c_rarg1 == rsi && c_rarg2 == rdx && c_rarg3 == rcx,
1160            "unexpected argument registers");
1161 #endif
1162   }
1163 
1164   void restore_arg_regs() {
1165     const Register saved_rdi = r9;
1166     const Register saved_rsi = r10;
1167 #ifdef _WIN64
1168     __ movptr(rdi, saved_rdi);
1169     __ movptr(rsi, saved_rsi);
1170 #endif
1171   }
1172 
1173   // Generate code for an array write pre barrier
1174   //
1175   //     addr    -  starting address
1176   //     count   -  element count
1177   //     tmp     - scratch register
1178   //
1179   //     Destroy no registers!
1180   //
1181   void  gen_write_ref_array_pre_barrier(Register addr, Register count, bool dest_uninitialized) {
1182     BarrierSet* bs = Universe::heap()->barrier_set();
1183     switch (bs->kind()) {
1184       case BarrierSet::G1SATBCT:
1185       case BarrierSet::G1SATBCTLogging:
1186         // With G1, don't generate the call if we statically know that the target in uninitialized
1187         if (!dest_uninitialized) {
1188            __ pusha();                      // push registers
1189            if (count == c_rarg0) {
1190              if (addr == c_rarg1) {
1191                // exactly backwards!!
1192                __ xchgptr(c_rarg1, c_rarg0);
1193              } else {
1194                __ movptr(c_rarg1, count);
1195                __ movptr(c_rarg0, addr);
1196              }
1197            } else {
1198              __ movptr(c_rarg0, addr);
1199              __ movptr(c_rarg1, count);
1200            }
1201            __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_pre), 2);
1202            __ popa();
1203         }
1204          break;
1205       case BarrierSet::CardTableModRef:
1206       case BarrierSet::CardTableExtension:
1207       case BarrierSet::ModRef:
1208       case BarrierSet::Epsilon:
1209         break;
1210       default:
1211         ShouldNotReachHere();
1212 
1213     }
1214   }
1215 
1216   //
1217   // Generate code for an array write post barrier
1218   //
1219   //  Input:
1220   //     start    - register containing starting address of destination array
1221   //     count    - elements count
1222   //     scratch  - scratch register
1223   //
1224   //  The input registers are overwritten.
1225   //
1226   void  gen_write_ref_array_post_barrier(Register start, Register count, Register scratch) {
1227     assert_different_registers(start, count, scratch);
1228     BarrierSet* bs = Universe::heap()->barrier_set();
1229     switch (bs->kind()) {
1230       case BarrierSet::G1SATBCT:
1231       case BarrierSet::G1SATBCTLogging:
1232         {
1233           __ pusha();             // push registers (overkill)
1234           if (c_rarg0 == count) { // On win64 c_rarg0 == rcx
1235             assert_different_registers(c_rarg1, start);
1236             __ mov(c_rarg1, count);
1237             __ mov(c_rarg0, start);
1238           } else {
1239             assert_different_registers(c_rarg0, count);
1240             __ mov(c_rarg0, start);
1241             __ mov(c_rarg1, count);
1242           }
1243           __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_post), 2);
1244           __ popa();
1245         }
1246         break;
1247       case BarrierSet::CardTableModRef:
1248       case BarrierSet::CardTableExtension:
1249         {
1250           CardTableModRefBS* ct = (CardTableModRefBS*)bs;
1251           assert(sizeof(*ct->byte_map_base) == sizeof(jbyte), "adjust this code");
1252 
1253           Label L_loop;
1254           const Register end = count;
1255 
1256           __ leaq(end, Address(start, count, TIMES_OOP, 0));  // end == start+count*oop_size
1257           __ subptr(end, BytesPerHeapOop); // end - 1 to make inclusive
1258           __ shrptr(start, CardTableModRefBS::card_shift);
1259           __ shrptr(end,   CardTableModRefBS::card_shift);
1260           __ subptr(end, start); // end --> cards count
1261 
1262           int64_t disp = (int64_t) ct->byte_map_base;
1263           __ mov64(scratch, disp);
1264           __ addptr(start, scratch);
1265         __ BIND(L_loop);
1266           __ movb(Address(start, count, Address::times_1), 0);
1267           __ decrement(count);
1268           __ jcc(Assembler::greaterEqual, L_loop);
1269         }
1270         break;
1271       case BarrierSet::Epsilon:
1272         // No barriers
1273         break;
1274       default:
1275         ShouldNotReachHere();
1276 
1277     }
1278   }
1279 
1280 
1281   // Copy big chunks forward
1282   //
1283   // Inputs:
1284   //   end_from     - source arrays end address
1285   //   end_to       - destination array end address
1286   //   qword_count  - 64-bits element count, negative
1287   //   to           - scratch
1288   //   L_copy_bytes - entry label
1289   //   L_copy_8_bytes  - exit  label
1290   //
1291   void copy_bytes_forward(Register end_from, Register end_to,
1292                              Register qword_count, Register to,
1293                              Label& L_copy_bytes, Label& L_copy_8_bytes) {
1294     DEBUG_ONLY(__ stop("enter at entry label, not here"));
1295     Label L_loop;
1296     __ align(OptoLoopAlignment);
1297     if (UseUnalignedLoadStores) {
1298       Label L_end;
1299       // Copy 64-bytes per iteration
1300       __ BIND(L_loop);
1301       if (UseAVX >= 2) {
1302         __ vmovdqu(xmm0, Address(end_from, qword_count, Address::times_8, -56));
1303         __ vmovdqu(Address(end_to, qword_count, Address::times_8, -56), xmm0);
1304         __ vmovdqu(xmm1, Address(end_from, qword_count, Address::times_8, -24));
1305         __ vmovdqu(Address(end_to, qword_count, Address::times_8, -24), xmm1);
1306       } else {
1307         __ movdqu(xmm0, Address(end_from, qword_count, Address::times_8, -56));
1308         __ movdqu(Address(end_to, qword_count, Address::times_8, -56), xmm0);
1309         __ movdqu(xmm1, Address(end_from, qword_count, Address::times_8, -40));
1310         __ movdqu(Address(end_to, qword_count, Address::times_8, -40), xmm1);
1311         __ movdqu(xmm2, Address(end_from, qword_count, Address::times_8, -24));
1312         __ movdqu(Address(end_to, qword_count, Address::times_8, -24), xmm2);
1313         __ movdqu(xmm3, Address(end_from, qword_count, Address::times_8, - 8));
1314         __ movdqu(Address(end_to, qword_count, Address::times_8, - 8), xmm3);
1315       }
1316       __ BIND(L_copy_bytes);
1317       __ addptr(qword_count, 8);
1318       __ jcc(Assembler::lessEqual, L_loop);
1319       __ subptr(qword_count, 4);  // sub(8) and add(4)
1320       __ jccb(Assembler::greater, L_end);
1321       // Copy trailing 32 bytes
1322       if (UseAVX >= 2) {
1323         __ vmovdqu(xmm0, Address(end_from, qword_count, Address::times_8, -24));
1324         __ vmovdqu(Address(end_to, qword_count, Address::times_8, -24), xmm0);
1325       } else {
1326         __ movdqu(xmm0, Address(end_from, qword_count, Address::times_8, -24));
1327         __ movdqu(Address(end_to, qword_count, Address::times_8, -24), xmm0);
1328         __ movdqu(xmm1, Address(end_from, qword_count, Address::times_8, - 8));
1329         __ movdqu(Address(end_to, qword_count, Address::times_8, - 8), xmm1);
1330       }
1331       __ addptr(qword_count, 4);
1332       __ BIND(L_end);
1333       if (UseAVX >= 2) {
1334         // clean upper bits of YMM registers
1335         __ vpxor(xmm0, xmm0);
1336         __ vpxor(xmm1, xmm1);
1337       }
1338     } else {
1339       // Copy 32-bytes per iteration
1340       __ BIND(L_loop);
1341       __ movq(to, Address(end_from, qword_count, Address::times_8, -24));
1342       __ movq(Address(end_to, qword_count, Address::times_8, -24), to);
1343       __ movq(to, Address(end_from, qword_count, Address::times_8, -16));
1344       __ movq(Address(end_to, qword_count, Address::times_8, -16), to);
1345       __ movq(to, Address(end_from, qword_count, Address::times_8, - 8));
1346       __ movq(Address(end_to, qword_count, Address::times_8, - 8), to);
1347       __ movq(to, Address(end_from, qword_count, Address::times_8, - 0));
1348       __ movq(Address(end_to, qword_count, Address::times_8, - 0), to);
1349 
1350       __ BIND(L_copy_bytes);
1351       __ addptr(qword_count, 4);
1352       __ jcc(Assembler::lessEqual, L_loop);
1353     }
1354     __ subptr(qword_count, 4);
1355     __ jcc(Assembler::less, L_copy_8_bytes); // Copy trailing qwords
1356   }
1357 
1358   // Copy big chunks backward
1359   //
1360   // Inputs:
1361   //   from         - source arrays address
1362   //   dest         - destination array address
1363   //   qword_count  - 64-bits element count
1364   //   to           - scratch
1365   //   L_copy_bytes - entry label
1366   //   L_copy_8_bytes  - exit  label
1367   //
1368   void copy_bytes_backward(Register from, Register dest,
1369                               Register qword_count, Register to,
1370                               Label& L_copy_bytes, Label& L_copy_8_bytes) {
1371     DEBUG_ONLY(__ stop("enter at entry label, not here"));
1372     Label L_loop;
1373     __ align(OptoLoopAlignment);
1374     if (UseUnalignedLoadStores) {
1375       Label L_end;
1376       // Copy 64-bytes per iteration
1377       __ BIND(L_loop);
1378       if (UseAVX >= 2) {
1379         __ vmovdqu(xmm0, Address(from, qword_count, Address::times_8, 32));
1380         __ vmovdqu(Address(dest, qword_count, Address::times_8, 32), xmm0);
1381         __ vmovdqu(xmm1, Address(from, qword_count, Address::times_8,  0));
1382         __ vmovdqu(Address(dest, qword_count, Address::times_8,  0), xmm1);
1383       } else {
1384         __ movdqu(xmm0, Address(from, qword_count, Address::times_8, 48));
1385         __ movdqu(Address(dest, qword_count, Address::times_8, 48), xmm0);
1386         __ movdqu(xmm1, Address(from, qword_count, Address::times_8, 32));
1387         __ movdqu(Address(dest, qword_count, Address::times_8, 32), xmm1);
1388         __ movdqu(xmm2, Address(from, qword_count, Address::times_8, 16));
1389         __ movdqu(Address(dest, qword_count, Address::times_8, 16), xmm2);
1390         __ movdqu(xmm3, Address(from, qword_count, Address::times_8,  0));
1391         __ movdqu(Address(dest, qword_count, Address::times_8,  0), xmm3);
1392       }
1393       __ BIND(L_copy_bytes);
1394       __ subptr(qword_count, 8);
1395       __ jcc(Assembler::greaterEqual, L_loop);
1396 
1397       __ addptr(qword_count, 4);  // add(8) and sub(4)
1398       __ jccb(Assembler::less, L_end);
1399       // Copy trailing 32 bytes
1400       if (UseAVX >= 2) {
1401         __ vmovdqu(xmm0, Address(from, qword_count, Address::times_8, 0));
1402         __ vmovdqu(Address(dest, qword_count, Address::times_8, 0), xmm0);
1403       } else {
1404         __ movdqu(xmm0, Address(from, qword_count, Address::times_8, 16));
1405         __ movdqu(Address(dest, qword_count, Address::times_8, 16), xmm0);
1406         __ movdqu(xmm1, Address(from, qword_count, Address::times_8,  0));
1407         __ movdqu(Address(dest, qword_count, Address::times_8,  0), xmm1);
1408       }
1409       __ subptr(qword_count, 4);
1410       __ BIND(L_end);
1411       if (UseAVX >= 2) {
1412         // clean upper bits of YMM registers
1413         __ vpxor(xmm0, xmm0);
1414         __ vpxor(xmm1, xmm1);
1415       }
1416     } else {
1417       // Copy 32-bytes per iteration
1418       __ BIND(L_loop);
1419       __ movq(to, Address(from, qword_count, Address::times_8, 24));
1420       __ movq(Address(dest, qword_count, Address::times_8, 24), to);
1421       __ movq(to, Address(from, qword_count, Address::times_8, 16));
1422       __ movq(Address(dest, qword_count, Address::times_8, 16), to);
1423       __ movq(to, Address(from, qword_count, Address::times_8,  8));
1424       __ movq(Address(dest, qword_count, Address::times_8,  8), to);
1425       __ movq(to, Address(from, qword_count, Address::times_8,  0));
1426       __ movq(Address(dest, qword_count, Address::times_8,  0), to);
1427 
1428       __ BIND(L_copy_bytes);
1429       __ subptr(qword_count, 4);
1430       __ jcc(Assembler::greaterEqual, L_loop);
1431     }
1432     __ addptr(qword_count, 4);
1433     __ jcc(Assembler::greater, L_copy_8_bytes); // Copy trailing qwords
1434   }
1435 
1436 
1437   // Arguments:
1438   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1439   //             ignored
1440   //   name    - stub name string
1441   //
1442   // Inputs:
1443   //   c_rarg0   - source array address
1444   //   c_rarg1   - destination array address
1445   //   c_rarg2   - element count, treated as ssize_t, can be zero
1446   //
1447   // If 'from' and/or 'to' are aligned on 4-, 2-, or 1-byte boundaries,
1448   // we let the hardware handle it.  The one to eight bytes within words,
1449   // dwords or qwords that span cache line boundaries will still be loaded
1450   // and stored atomically.
1451   //
1452   // Side Effects:
1453   //   disjoint_byte_copy_entry is set to the no-overlap entry point
1454   //   used by generate_conjoint_byte_copy().
1455   //
1456   address generate_disjoint_byte_copy(bool aligned, address* entry, const char *name) {
1457     __ align(CodeEntryAlignment);
1458     StubCodeMark mark(this, "StubRoutines", name);
1459     address start = __ pc();
1460 
1461     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes, L_copy_2_bytes;
1462     Label L_copy_byte, L_exit;
1463     const Register from        = rdi;  // source array address
1464     const Register to          = rsi;  // destination array address
1465     const Register count       = rdx;  // elements count
1466     const Register byte_count  = rcx;
1467     const Register qword_count = count;
1468     const Register end_from    = from; // source array end address
1469     const Register end_to      = to;   // destination array end address
1470     // End pointers are inclusive, and if count is not zero they point
1471     // to the last unit copied:  end_to[0] := end_from[0]
1472 
1473     __ enter(); // required for proper stackwalking of RuntimeStub frame
1474     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1475 
1476     if (entry != NULL) {
1477       *entry = __ pc();
1478        // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1479       BLOCK_COMMENT("Entry:");
1480     }
1481 
1482     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1483                       // r9 and r10 may be used to save non-volatile registers
1484 
1485     // 'from', 'to' and 'count' are now valid
1486     __ movptr(byte_count, count);
1487     __ shrptr(count, 3); // count => qword_count
1488 
1489     // Copy from low to high addresses.  Use 'to' as scratch.
1490     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1491     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1492     __ negptr(qword_count); // make the count negative
1493     __ jmp(L_copy_bytes);
1494 
1495     // Copy trailing qwords
1496   __ BIND(L_copy_8_bytes);
1497     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
1498     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
1499     __ increment(qword_count);
1500     __ jcc(Assembler::notZero, L_copy_8_bytes);
1501 
1502     // Check for and copy trailing dword
1503   __ BIND(L_copy_4_bytes);
1504     __ testl(byte_count, 4);
1505     __ jccb(Assembler::zero, L_copy_2_bytes);
1506     __ movl(rax, Address(end_from, 8));
1507     __ movl(Address(end_to, 8), rax);
1508 
1509     __ addptr(end_from, 4);
1510     __ addptr(end_to, 4);
1511 
1512     // Check for and copy trailing word
1513   __ BIND(L_copy_2_bytes);
1514     __ testl(byte_count, 2);
1515     __ jccb(Assembler::zero, L_copy_byte);
1516     __ movw(rax, Address(end_from, 8));
1517     __ movw(Address(end_to, 8), rax);
1518 
1519     __ addptr(end_from, 2);
1520     __ addptr(end_to, 2);
1521 
1522     // Check for and copy trailing byte
1523   __ BIND(L_copy_byte);
1524     __ testl(byte_count, 1);
1525     __ jccb(Assembler::zero, L_exit);
1526     __ movb(rax, Address(end_from, 8));
1527     __ movb(Address(end_to, 8), rax);
1528 
1529   __ BIND(L_exit);
1530     restore_arg_regs();
1531     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); // Update counter after rscratch1 is free
1532     __ xorptr(rax, rax); // return 0
1533     __ leave(); // required for proper stackwalking of RuntimeStub frame
1534     __ ret(0);
1535 
1536     // Copy in multi-bytes chunks
1537     copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1538     __ jmp(L_copy_4_bytes);
1539 
1540     return start;
1541   }
1542 
1543   // Arguments:
1544   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1545   //             ignored
1546   //   name    - stub name string
1547   //
1548   // Inputs:
1549   //   c_rarg0   - source array address
1550   //   c_rarg1   - destination array address
1551   //   c_rarg2   - element count, treated as ssize_t, can be zero
1552   //
1553   // If 'from' and/or 'to' are aligned on 4-, 2-, or 1-byte boundaries,
1554   // we let the hardware handle it.  The one to eight bytes within words,
1555   // dwords or qwords that span cache line boundaries will still be loaded
1556   // and stored atomically.
1557   //
1558   address generate_conjoint_byte_copy(bool aligned, address nooverlap_target,
1559                                       address* entry, const char *name) {
1560     __ align(CodeEntryAlignment);
1561     StubCodeMark mark(this, "StubRoutines", name);
1562     address start = __ pc();
1563 
1564     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes, L_copy_2_bytes;
1565     const Register from        = rdi;  // source array address
1566     const Register to          = rsi;  // destination array address
1567     const Register count       = rdx;  // elements count
1568     const Register byte_count  = rcx;
1569     const Register qword_count = count;
1570 
1571     __ enter(); // required for proper stackwalking of RuntimeStub frame
1572     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1573 
1574     if (entry != NULL) {
1575       *entry = __ pc();
1576       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1577       BLOCK_COMMENT("Entry:");
1578     }
1579 
1580     array_overlap_test(nooverlap_target, Address::times_1);
1581     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1582                       // r9 and r10 may be used to save non-volatile registers
1583 
1584     // 'from', 'to' and 'count' are now valid
1585     __ movptr(byte_count, count);
1586     __ shrptr(count, 3);   // count => qword_count
1587 
1588     // Copy from high to low addresses.
1589 
1590     // Check for and copy trailing byte
1591     __ testl(byte_count, 1);
1592     __ jcc(Assembler::zero, L_copy_2_bytes);
1593     __ movb(rax, Address(from, byte_count, Address::times_1, -1));
1594     __ movb(Address(to, byte_count, Address::times_1, -1), rax);
1595     __ decrement(byte_count); // Adjust for possible trailing word
1596 
1597     // Check for and copy trailing word
1598   __ BIND(L_copy_2_bytes);
1599     __ testl(byte_count, 2);
1600     __ jcc(Assembler::zero, L_copy_4_bytes);
1601     __ movw(rax, Address(from, byte_count, Address::times_1, -2));
1602     __ movw(Address(to, byte_count, Address::times_1, -2), rax);
1603 
1604     // Check for and copy trailing dword
1605   __ BIND(L_copy_4_bytes);
1606     __ testl(byte_count, 4);
1607     __ jcc(Assembler::zero, L_copy_bytes);
1608     __ movl(rax, Address(from, qword_count, Address::times_8));
1609     __ movl(Address(to, qword_count, Address::times_8), rax);
1610     __ jmp(L_copy_bytes);
1611 
1612     // Copy trailing qwords
1613   __ BIND(L_copy_8_bytes);
1614     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
1615     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
1616     __ decrement(qword_count);
1617     __ jcc(Assembler::notZero, L_copy_8_bytes);
1618 
1619     restore_arg_regs();
1620     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); // Update counter after rscratch1 is free
1621     __ xorptr(rax, rax); // return 0
1622     __ leave(); // required for proper stackwalking of RuntimeStub frame
1623     __ ret(0);
1624 
1625     // Copy in multi-bytes chunks
1626     copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1627 
1628     restore_arg_regs();
1629     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); // Update counter after rscratch1 is free
1630     __ xorptr(rax, rax); // return 0
1631     __ leave(); // required for proper stackwalking of RuntimeStub frame
1632     __ ret(0);
1633 
1634     return start;
1635   }
1636 
1637   // Arguments:
1638   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1639   //             ignored
1640   //   name    - stub name string
1641   //
1642   // Inputs:
1643   //   c_rarg0   - source array address
1644   //   c_rarg1   - destination array address
1645   //   c_rarg2   - element count, treated as ssize_t, can be zero
1646   //
1647   // If 'from' and/or 'to' are aligned on 4- or 2-byte boundaries, we
1648   // let the hardware handle it.  The two or four words within dwords
1649   // or qwords that span cache line boundaries will still be loaded
1650   // and stored atomically.
1651   //
1652   // Side Effects:
1653   //   disjoint_short_copy_entry is set to the no-overlap entry point
1654   //   used by generate_conjoint_short_copy().
1655   //
1656   address generate_disjoint_short_copy(bool aligned, address *entry, const char *name) {
1657     __ align(CodeEntryAlignment);
1658     StubCodeMark mark(this, "StubRoutines", name);
1659     address start = __ pc();
1660 
1661     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes,L_copy_2_bytes,L_exit;
1662     const Register from        = rdi;  // source array address
1663     const Register to          = rsi;  // destination array address
1664     const Register count       = rdx;  // elements count
1665     const Register word_count  = rcx;
1666     const Register qword_count = count;
1667     const Register end_from    = from; // source array end address
1668     const Register end_to      = to;   // destination array end address
1669     // End pointers are inclusive, and if count is not zero they point
1670     // to the last unit copied:  end_to[0] := end_from[0]
1671 
1672     __ enter(); // required for proper stackwalking of RuntimeStub frame
1673     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1674 
1675     if (entry != NULL) {
1676       *entry = __ pc();
1677       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1678       BLOCK_COMMENT("Entry:");
1679     }
1680 
1681     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1682                       // r9 and r10 may be used to save non-volatile registers
1683 
1684     // 'from', 'to' and 'count' are now valid
1685     __ movptr(word_count, count);
1686     __ shrptr(count, 2); // count => qword_count
1687 
1688     // Copy from low to high addresses.  Use 'to' as scratch.
1689     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1690     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1691     __ negptr(qword_count);
1692     __ jmp(L_copy_bytes);
1693 
1694     // Copy trailing qwords
1695   __ BIND(L_copy_8_bytes);
1696     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
1697     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
1698     __ increment(qword_count);
1699     __ jcc(Assembler::notZero, L_copy_8_bytes);
1700 
1701     // Original 'dest' is trashed, so we can't use it as a
1702     // base register for a possible trailing word copy
1703 
1704     // Check for and copy trailing dword
1705   __ BIND(L_copy_4_bytes);
1706     __ testl(word_count, 2);
1707     __ jccb(Assembler::zero, L_copy_2_bytes);
1708     __ movl(rax, Address(end_from, 8));
1709     __ movl(Address(end_to, 8), rax);
1710 
1711     __ addptr(end_from, 4);
1712     __ addptr(end_to, 4);
1713 
1714     // Check for and copy trailing word
1715   __ BIND(L_copy_2_bytes);
1716     __ testl(word_count, 1);
1717     __ jccb(Assembler::zero, L_exit);
1718     __ movw(rax, Address(end_from, 8));
1719     __ movw(Address(end_to, 8), rax);
1720 
1721   __ BIND(L_exit);
1722     restore_arg_regs();
1723     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); // Update counter after rscratch1 is free
1724     __ xorptr(rax, rax); // return 0
1725     __ leave(); // required for proper stackwalking of RuntimeStub frame
1726     __ ret(0);
1727 
1728     // Copy in multi-bytes chunks
1729     copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1730     __ jmp(L_copy_4_bytes);
1731 
1732     return start;
1733   }
1734 
1735   address generate_fill(BasicType t, bool aligned, const char *name) {
1736     __ align(CodeEntryAlignment);
1737     StubCodeMark mark(this, "StubRoutines", name);
1738     address start = __ pc();
1739 
1740     BLOCK_COMMENT("Entry:");
1741 
1742     const Register to       = c_rarg0;  // source array address
1743     const Register value    = c_rarg1;  // value
1744     const Register count    = c_rarg2;  // elements count
1745 
1746     __ enter(); // required for proper stackwalking of RuntimeStub frame
1747 
1748     __ generate_fill(t, aligned, to, value, count, rax, xmm0);
1749 
1750     __ leave(); // required for proper stackwalking of RuntimeStub frame
1751     __ ret(0);
1752     return start;
1753   }
1754 
1755   // Arguments:
1756   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1757   //             ignored
1758   //   name    - stub name string
1759   //
1760   // Inputs:
1761   //   c_rarg0   - source array address
1762   //   c_rarg1   - destination array address
1763   //   c_rarg2   - element count, treated as ssize_t, can be zero
1764   //
1765   // If 'from' and/or 'to' are aligned on 4- or 2-byte boundaries, we
1766   // let the hardware handle it.  The two or four words within dwords
1767   // or qwords that span cache line boundaries will still be loaded
1768   // and stored atomically.
1769   //
1770   address generate_conjoint_short_copy(bool aligned, address nooverlap_target,
1771                                        address *entry, const char *name) {
1772     __ align(CodeEntryAlignment);
1773     StubCodeMark mark(this, "StubRoutines", name);
1774     address start = __ pc();
1775 
1776     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes;
1777     const Register from        = rdi;  // source array address
1778     const Register to          = rsi;  // destination array address
1779     const Register count       = rdx;  // elements count
1780     const Register word_count  = rcx;
1781     const Register qword_count = count;
1782 
1783     __ enter(); // required for proper stackwalking of RuntimeStub frame
1784     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1785 
1786     if (entry != NULL) {
1787       *entry = __ pc();
1788       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1789       BLOCK_COMMENT("Entry:");
1790     }
1791 
1792     array_overlap_test(nooverlap_target, Address::times_2);
1793     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1794                       // r9 and r10 may be used to save non-volatile registers
1795 
1796     // 'from', 'to' and 'count' are now valid
1797     __ movptr(word_count, count);
1798     __ shrptr(count, 2); // count => qword_count
1799 
1800     // Copy from high to low addresses.  Use 'to' as scratch.
1801 
1802     // Check for and copy trailing word
1803     __ testl(word_count, 1);
1804     __ jccb(Assembler::zero, L_copy_4_bytes);
1805     __ movw(rax, Address(from, word_count, Address::times_2, -2));
1806     __ movw(Address(to, word_count, Address::times_2, -2), rax);
1807 
1808     // Check for and copy trailing dword
1809   __ BIND(L_copy_4_bytes);
1810     __ testl(word_count, 2);
1811     __ jcc(Assembler::zero, L_copy_bytes);
1812     __ movl(rax, Address(from, qword_count, Address::times_8));
1813     __ movl(Address(to, qword_count, Address::times_8), rax);
1814     __ jmp(L_copy_bytes);
1815 
1816     // Copy trailing qwords
1817   __ BIND(L_copy_8_bytes);
1818     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
1819     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
1820     __ decrement(qword_count);
1821     __ jcc(Assembler::notZero, L_copy_8_bytes);
1822 
1823     restore_arg_regs();
1824     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); // Update counter after rscratch1 is free
1825     __ xorptr(rax, rax); // return 0
1826     __ leave(); // required for proper stackwalking of RuntimeStub frame
1827     __ ret(0);
1828 
1829     // Copy in multi-bytes chunks
1830     copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1831 
1832     restore_arg_regs();
1833     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); // Update counter after rscratch1 is free
1834     __ xorptr(rax, rax); // return 0
1835     __ leave(); // required for proper stackwalking of RuntimeStub frame
1836     __ ret(0);
1837 
1838     return start;
1839   }
1840 
1841   // Arguments:
1842   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1843   //             ignored
1844   //   is_oop  - true => oop array, so generate store check code
1845   //   name    - stub name string
1846   //
1847   // Inputs:
1848   //   c_rarg0   - source array address
1849   //   c_rarg1   - destination array address
1850   //   c_rarg2   - element count, treated as ssize_t, can be zero
1851   //
1852   // If 'from' and/or 'to' are aligned on 4-byte boundaries, we let
1853   // the hardware handle it.  The two dwords within qwords that span
1854   // cache line boundaries will still be loaded and stored atomicly.
1855   //
1856   // Side Effects:
1857   //   disjoint_int_copy_entry is set to the no-overlap entry point
1858   //   used by generate_conjoint_int_oop_copy().
1859   //
1860   address generate_disjoint_int_oop_copy(bool aligned, bool is_oop, address* entry,
1861                                          const char *name, bool dest_uninitialized = false) {
1862     __ align(CodeEntryAlignment);
1863     StubCodeMark mark(this, "StubRoutines", name);
1864     address start = __ pc();
1865 
1866     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes, L_exit;
1867     const Register from        = rdi;  // source array address
1868     const Register to          = rsi;  // destination array address
1869     const Register count       = rdx;  // elements count
1870     const Register dword_count = rcx;
1871     const Register qword_count = count;
1872     const Register end_from    = from; // source array end address
1873     const Register end_to      = to;   // destination array end address
1874     const Register saved_to    = r11;  // saved destination array address
1875     // End pointers are inclusive, and if count is not zero they point
1876     // to the last unit copied:  end_to[0] := end_from[0]
1877 
1878     __ enter(); // required for proper stackwalking of RuntimeStub frame
1879     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1880 
1881     if (entry != NULL) {
1882       *entry = __ pc();
1883       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1884       BLOCK_COMMENT("Entry:");
1885     }
1886 
1887     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1888                       // r9 and r10 may be used to save non-volatile registers
1889     if (is_oop) {
1890       __ movq(saved_to, to);
1891       gen_write_ref_array_pre_barrier(to, count, dest_uninitialized);
1892     }
1893 
1894     // 'from', 'to' and 'count' are now valid
1895     __ movptr(dword_count, count);
1896     __ shrptr(count, 1); // count => qword_count
1897 
1898     // Copy from low to high addresses.  Use 'to' as scratch.
1899     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1900     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1901     __ negptr(qword_count);
1902     __ jmp(L_copy_bytes);
1903 
1904     // Copy trailing qwords
1905   __ BIND(L_copy_8_bytes);
1906     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
1907     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
1908     __ increment(qword_count);
1909     __ jcc(Assembler::notZero, L_copy_8_bytes);
1910 
1911     // Check for and copy trailing dword
1912   __ BIND(L_copy_4_bytes);
1913     __ testl(dword_count, 1); // Only byte test since the value is 0 or 1
1914     __ jccb(Assembler::zero, L_exit);
1915     __ movl(rax, Address(end_from, 8));
1916     __ movl(Address(end_to, 8), rax);
1917 
1918   __ BIND(L_exit);
1919     if (is_oop) {
1920       gen_write_ref_array_post_barrier(saved_to, dword_count, rax);
1921     }
1922     restore_arg_regs();
1923     inc_counter_np(SharedRuntime::_jint_array_copy_ctr); // Update counter after rscratch1 is free
1924     __ xorptr(rax, rax); // return 0
1925     __ leave(); // required for proper stackwalking of RuntimeStub frame
1926     __ ret(0);
1927 
1928     // Copy in multi-bytes chunks
1929     copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1930     __ jmp(L_copy_4_bytes);
1931 
1932     return start;
1933   }
1934 
1935   // Arguments:
1936   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1937   //             ignored
1938   //   is_oop  - true => oop array, so generate store check code
1939   //   name    - stub name string
1940   //
1941   // Inputs:
1942   //   c_rarg0   - source array address
1943   //   c_rarg1   - destination array address
1944   //   c_rarg2   - element count, treated as ssize_t, can be zero
1945   //
1946   // If 'from' and/or 'to' are aligned on 4-byte boundaries, we let
1947   // the hardware handle it.  The two dwords within qwords that span
1948   // cache line boundaries will still be loaded and stored atomicly.
1949   //
1950   address generate_conjoint_int_oop_copy(bool aligned, bool is_oop, address nooverlap_target,
1951                                          address *entry, const char *name,
1952                                          bool dest_uninitialized = false) {
1953     __ align(CodeEntryAlignment);
1954     StubCodeMark mark(this, "StubRoutines", name);
1955     address start = __ pc();
1956 
1957     Label L_copy_bytes, L_copy_8_bytes, L_copy_2_bytes, L_exit;
1958     const Register from        = rdi;  // source array address
1959     const Register to          = rsi;  // destination array address
1960     const Register count       = rdx;  // elements count
1961     const Register dword_count = rcx;
1962     const Register qword_count = count;
1963 
1964     __ enter(); // required for proper stackwalking of RuntimeStub frame
1965     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1966 
1967     if (entry != NULL) {
1968       *entry = __ pc();
1969        // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1970       BLOCK_COMMENT("Entry:");
1971     }
1972 
1973     array_overlap_test(nooverlap_target, Address::times_4);
1974     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1975                       // r9 and r10 may be used to save non-volatile registers
1976 
1977     if (is_oop) {
1978       // no registers are destroyed by this call
1979       gen_write_ref_array_pre_barrier(to, count, dest_uninitialized);
1980     }
1981 
1982     assert_clean_int(count, rax); // Make sure 'count' is clean int.
1983     // 'from', 'to' and 'count' are now valid
1984     __ movptr(dword_count, count);
1985     __ shrptr(count, 1); // count => qword_count
1986 
1987     // Copy from high to low addresses.  Use 'to' as scratch.
1988 
1989     // Check for and copy trailing dword
1990     __ testl(dword_count, 1);
1991     __ jcc(Assembler::zero, L_copy_bytes);
1992     __ movl(rax, Address(from, dword_count, Address::times_4, -4));
1993     __ movl(Address(to, dword_count, Address::times_4, -4), rax);
1994     __ jmp(L_copy_bytes);
1995 
1996     // Copy trailing qwords
1997   __ BIND(L_copy_8_bytes);
1998     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
1999     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
2000     __ decrement(qword_count);
2001     __ jcc(Assembler::notZero, L_copy_8_bytes);
2002 
2003     if (is_oop) {
2004       __ jmp(L_exit);
2005     }
2006     restore_arg_regs();
2007     inc_counter_np(SharedRuntime::_jint_array_copy_ctr); // Update counter after rscratch1 is free
2008     __ xorptr(rax, rax); // return 0
2009     __ leave(); // required for proper stackwalking of RuntimeStub frame
2010     __ ret(0);
2011 
2012     // Copy in multi-bytes chunks
2013     copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
2014 
2015   __ BIND(L_exit);
2016     if (is_oop) {
2017       gen_write_ref_array_post_barrier(to, dword_count, rax);
2018     }
2019     restore_arg_regs();
2020     inc_counter_np(SharedRuntime::_jint_array_copy_ctr); // Update counter after rscratch1 is free
2021     __ xorptr(rax, rax); // return 0
2022     __ leave(); // required for proper stackwalking of RuntimeStub frame
2023     __ ret(0);
2024 
2025     return start;
2026   }
2027 
2028   // Arguments:
2029   //   aligned - true => Input and output aligned on a HeapWord boundary == 8 bytes
2030   //             ignored
2031   //   is_oop  - true => oop array, so generate store check code
2032   //   name    - stub name string
2033   //
2034   // Inputs:
2035   //   c_rarg0   - source array address
2036   //   c_rarg1   - destination array address
2037   //   c_rarg2   - element count, treated as ssize_t, can be zero
2038   //
2039  // Side Effects:
2040   //   disjoint_oop_copy_entry or disjoint_long_copy_entry is set to the
2041   //   no-overlap entry point used by generate_conjoint_long_oop_copy().
2042   //
2043   address generate_disjoint_long_oop_copy(bool aligned, bool is_oop, address *entry,
2044                                           const char *name, bool dest_uninitialized = false) {
2045     __ align(CodeEntryAlignment);
2046     StubCodeMark mark(this, "StubRoutines", name);
2047     address start = __ pc();
2048 
2049     Label L_copy_bytes, L_copy_8_bytes, L_exit;
2050     const Register from        = rdi;  // source array address
2051     const Register to          = rsi;  // destination array address
2052     const Register qword_count = rdx;  // elements count
2053     const Register end_from    = from; // source array end address
2054     const Register end_to      = rcx;  // destination array end address
2055     const Register saved_to    = to;
2056     const Register saved_count = r11;
2057     // End pointers are inclusive, and if count is not zero they point
2058     // to the last unit copied:  end_to[0] := end_from[0]
2059 
2060     __ enter(); // required for proper stackwalking of RuntimeStub frame
2061     // Save no-overlap entry point for generate_conjoint_long_oop_copy()
2062     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
2063 
2064     if (entry != NULL) {
2065       *entry = __ pc();
2066       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
2067       BLOCK_COMMENT("Entry:");
2068     }
2069 
2070     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
2071                       // r9 and r10 may be used to save non-volatile registers
2072     // 'from', 'to' and 'qword_count' are now valid
2073     if (is_oop) {
2074       // Save to and count for store barrier
2075       __ movptr(saved_count, qword_count);
2076       // no registers are destroyed by this call
2077       gen_write_ref_array_pre_barrier(to, qword_count, dest_uninitialized);
2078     }
2079 
2080     // Copy from low to high addresses.  Use 'to' as scratch.
2081     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
2082     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
2083     __ negptr(qword_count);
2084     __ jmp(L_copy_bytes);
2085 
2086     // Copy trailing qwords
2087   __ BIND(L_copy_8_bytes);
2088     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
2089     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
2090     __ increment(qword_count);
2091     __ jcc(Assembler::notZero, L_copy_8_bytes);
2092 
2093     if (is_oop) {
2094       __ jmp(L_exit);
2095     } else {
2096       restore_arg_regs();
2097       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
2098       __ xorptr(rax, rax); // return 0
2099       __ leave(); // required for proper stackwalking of RuntimeStub frame
2100       __ ret(0);
2101     }
2102 
2103     // Copy in multi-bytes chunks
2104     copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
2105 
2106     if (is_oop) {
2107     __ BIND(L_exit);
2108       gen_write_ref_array_post_barrier(saved_to, saved_count, rax);
2109     }
2110     restore_arg_regs();
2111     if (is_oop) {
2112       inc_counter_np(SharedRuntime::_oop_array_copy_ctr); // Update counter after rscratch1 is free
2113     } else {
2114       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
2115     }
2116     __ xorptr(rax, rax); // return 0
2117     __ leave(); // required for proper stackwalking of RuntimeStub frame
2118     __ ret(0);
2119 
2120     return start;
2121   }
2122 
2123   // Arguments:
2124   //   aligned - true => Input and output aligned on a HeapWord boundary == 8 bytes
2125   //             ignored
2126   //   is_oop  - true => oop array, so generate store check code
2127   //   name    - stub name string
2128   //
2129   // Inputs:
2130   //   c_rarg0   - source array address
2131   //   c_rarg1   - destination array address
2132   //   c_rarg2   - element count, treated as ssize_t, can be zero
2133   //
2134   address generate_conjoint_long_oop_copy(bool aligned, bool is_oop,
2135                                           address nooverlap_target, address *entry,
2136                                           const char *name, bool dest_uninitialized = false) {
2137     __ align(CodeEntryAlignment);
2138     StubCodeMark mark(this, "StubRoutines", name);
2139     address start = __ pc();
2140 
2141     Label L_copy_bytes, L_copy_8_bytes, L_exit;
2142     const Register from        = rdi;  // source array address
2143     const Register to          = rsi;  // destination array address
2144     const Register qword_count = rdx;  // elements count
2145     const Register saved_count = rcx;
2146 
2147     __ enter(); // required for proper stackwalking of RuntimeStub frame
2148     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
2149 
2150     if (entry != NULL) {
2151       *entry = __ pc();
2152       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
2153       BLOCK_COMMENT("Entry:");
2154     }
2155 
2156     array_overlap_test(nooverlap_target, Address::times_8);
2157     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
2158                       // r9 and r10 may be used to save non-volatile registers
2159     // 'from', 'to' and 'qword_count' are now valid
2160     if (is_oop) {
2161       // Save to and count for store barrier
2162       __ movptr(saved_count, qword_count);
2163       // No registers are destroyed by this call
2164       gen_write_ref_array_pre_barrier(to, saved_count, dest_uninitialized);
2165     }
2166 
2167     __ jmp(L_copy_bytes);
2168 
2169     // Copy trailing qwords
2170   __ BIND(L_copy_8_bytes);
2171     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
2172     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
2173     __ decrement(qword_count);
2174     __ jcc(Assembler::notZero, L_copy_8_bytes);
2175 
2176     if (is_oop) {
2177       __ jmp(L_exit);
2178     } else {
2179       restore_arg_regs();
2180       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
2181       __ xorptr(rax, rax); // return 0
2182       __ leave(); // required for proper stackwalking of RuntimeStub frame
2183       __ ret(0);
2184     }
2185 
2186     // Copy in multi-bytes chunks
2187     copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
2188 
2189     if (is_oop) {
2190     __ BIND(L_exit);
2191       gen_write_ref_array_post_barrier(to, saved_count, rax);
2192     }
2193     restore_arg_regs();
2194     if (is_oop) {
2195       inc_counter_np(SharedRuntime::_oop_array_copy_ctr); // Update counter after rscratch1 is free
2196     } else {
2197       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
2198     }
2199     __ xorptr(rax, rax); // return 0
2200     __ leave(); // required for proper stackwalking of RuntimeStub frame
2201     __ ret(0);
2202 
2203     return start;
2204   }
2205 
2206 
2207   // Helper for generating a dynamic type check.
2208   // Smashes no registers.
2209   void generate_type_check(Register sub_klass,
2210                            Register super_check_offset,
2211                            Register super_klass,
2212                            Label& L_success) {
2213     assert_different_registers(sub_klass, super_check_offset, super_klass);
2214 
2215     BLOCK_COMMENT("type_check:");
2216 
2217     Label L_miss;
2218 
2219     __ check_klass_subtype_fast_path(sub_klass, super_klass, noreg,        &L_success, &L_miss, NULL,
2220                                      super_check_offset);
2221     __ check_klass_subtype_slow_path(sub_klass, super_klass, noreg, noreg, &L_success, NULL);
2222 
2223     // Fall through on failure!
2224     __ BIND(L_miss);
2225   }
2226 
2227   //
2228   //  Generate checkcasting array copy stub
2229   //
2230   //  Input:
2231   //    c_rarg0   - source array address
2232   //    c_rarg1   - destination array address
2233   //    c_rarg2   - element count, treated as ssize_t, can be zero
2234   //    c_rarg3   - size_t ckoff (super_check_offset)
2235   // not Win64
2236   //    c_rarg4   - oop ckval (super_klass)
2237   // Win64
2238   //    rsp+40    - oop ckval (super_klass)
2239   //
2240   //  Output:
2241   //    rax ==  0  -  success
2242   //    rax == -1^K - failure, where K is partial transfer count
2243   //
2244   address generate_checkcast_copy(const char *name, address *entry,
2245                                   bool dest_uninitialized = false) {
2246 
2247     Label L_load_element, L_store_element, L_do_card_marks, L_done;
2248 
2249     // Input registers (after setup_arg_regs)
2250     const Register from        = rdi;   // source array address
2251     const Register to          = rsi;   // destination array address
2252     const Register length      = rdx;   // elements count
2253     const Register ckoff       = rcx;   // super_check_offset
2254     const Register ckval       = r8;    // super_klass
2255 
2256     // Registers used as temps (r13, r14 are save-on-entry)
2257     const Register end_from    = from;  // source array end address
2258     const Register end_to      = r13;   // destination array end address
2259     const Register count       = rdx;   // -(count_remaining)
2260     const Register r14_length  = r14;   // saved copy of length
2261     // End pointers are inclusive, and if length is not zero they point
2262     // to the last unit copied:  end_to[0] := end_from[0]
2263 
2264     const Register rax_oop    = rax;    // actual oop copied
2265     const Register r11_klass  = r11;    // oop._klass
2266 
2267     //---------------------------------------------------------------
2268     // Assembler stub will be used for this call to arraycopy
2269     // if the two arrays are subtypes of Object[] but the
2270     // destination array type is not equal to or a supertype
2271     // of the source type.  Each element must be separately
2272     // checked.
2273 
2274     __ align(CodeEntryAlignment);
2275     StubCodeMark mark(this, "StubRoutines", name);
2276     address start = __ pc();
2277 
2278     __ enter(); // required for proper stackwalking of RuntimeStub frame
2279 
2280 #ifdef ASSERT
2281     // caller guarantees that the arrays really are different
2282     // otherwise, we would have to make conjoint checks
2283     { Label L;
2284       array_overlap_test(L, TIMES_OOP);
2285       __ stop("checkcast_copy within a single array");
2286       __ bind(L);
2287     }
2288 #endif //ASSERT
2289 
2290     setup_arg_regs(4); // from => rdi, to => rsi, length => rdx
2291                        // ckoff => rcx, ckval => r8
2292                        // r9 and r10 may be used to save non-volatile registers
2293 #ifdef _WIN64
2294     // last argument (#4) is on stack on Win64
2295     __ movptr(ckval, Address(rsp, 6 * wordSize));
2296 #endif
2297 
2298     // Caller of this entry point must set up the argument registers.
2299     if (entry != NULL) {
2300       *entry = __ pc();
2301       BLOCK_COMMENT("Entry:");
2302     }
2303 
2304     // allocate spill slots for r13, r14
2305     enum {
2306       saved_r13_offset,
2307       saved_r14_offset,
2308       saved_rbp_offset
2309     };
2310     __ subptr(rsp, saved_rbp_offset * wordSize);
2311     __ movptr(Address(rsp, saved_r13_offset * wordSize), r13);
2312     __ movptr(Address(rsp, saved_r14_offset * wordSize), r14);
2313 
2314     // check that int operands are properly extended to size_t
2315     assert_clean_int(length, rax);
2316     assert_clean_int(ckoff, rax);
2317 
2318 #ifdef ASSERT
2319     BLOCK_COMMENT("assert consistent ckoff/ckval");
2320     // The ckoff and ckval must be mutually consistent,
2321     // even though caller generates both.
2322     { Label L;
2323       int sco_offset = in_bytes(Klass::super_check_offset_offset());
2324       __ cmpl(ckoff, Address(ckval, sco_offset));
2325       __ jcc(Assembler::equal, L);
2326       __ stop("super_check_offset inconsistent");
2327       __ bind(L);
2328     }
2329 #endif //ASSERT
2330 
2331     // Loop-invariant addresses.  They are exclusive end pointers.
2332     Address end_from_addr(from, length, TIMES_OOP, 0);
2333     Address   end_to_addr(to,   length, TIMES_OOP, 0);
2334     // Loop-variant addresses.  They assume post-incremented count < 0.
2335     Address from_element_addr(end_from, count, TIMES_OOP, 0);
2336     Address   to_element_addr(end_to,   count, TIMES_OOP, 0);
2337 
2338     gen_write_ref_array_pre_barrier(to, count, dest_uninitialized);
2339 
2340     // Copy from low to high addresses, indexed from the end of each array.
2341     __ lea(end_from, end_from_addr);
2342     __ lea(end_to,   end_to_addr);
2343     __ movptr(r14_length, length);        // save a copy of the length
2344     assert(length == count, "");          // else fix next line:
2345     __ negptr(count);                     // negate and test the length
2346     __ jcc(Assembler::notZero, L_load_element);
2347 
2348     // Empty array:  Nothing to do.
2349     __ xorptr(rax, rax);                  // return 0 on (trivial) success
2350     __ jmp(L_done);
2351 
2352     // ======== begin loop ========
2353     // (Loop is rotated; its entry is L_load_element.)
2354     // Loop control:
2355     //   for (count = -count; count != 0; count++)
2356     // Base pointers src, dst are biased by 8*(count-1),to last element.
2357     __ align(OptoLoopAlignment);
2358 
2359     __ BIND(L_store_element);
2360     __ store_heap_oop(to_element_addr, rax_oop);  // store the oop
2361     __ increment(count);               // increment the count toward zero
2362     __ jcc(Assembler::zero, L_do_card_marks);
2363 
2364     // ======== loop entry is here ========
2365     __ BIND(L_load_element);
2366     __ load_heap_oop(rax_oop, from_element_addr); // load the oop
2367     __ testptr(rax_oop, rax_oop);
2368     __ jcc(Assembler::zero, L_store_element);
2369 
2370     __ load_klass(r11_klass, rax_oop);// query the object klass
2371     generate_type_check(r11_klass, ckoff, ckval, L_store_element);
2372     // ======== end loop ========
2373 
2374     // It was a real error; we must depend on the caller to finish the job.
2375     // Register rdx = -1 * number of *remaining* oops, r14 = *total* oops.
2376     // Emit GC store barriers for the oops we have copied (r14 + rdx),
2377     // and report their number to the caller.
2378     assert_different_registers(rax, r14_length, count, to, end_to, rcx, rscratch1);
2379     Label L_post_barrier;
2380     __ addptr(r14_length, count);     // K = (original - remaining) oops
2381     __ movptr(rax, r14_length);       // save the value
2382     __ notptr(rax);                   // report (-1^K) to caller (does not affect flags)
2383     __ jccb(Assembler::notZero, L_post_barrier);
2384     __ jmp(L_done); // K == 0, nothing was copied, skip post barrier
2385 
2386     // Come here on success only.
2387     __ BIND(L_do_card_marks);
2388     __ xorptr(rax, rax);              // return 0 on success
2389 
2390     __ BIND(L_post_barrier);
2391     gen_write_ref_array_post_barrier(to, r14_length, rscratch1);
2392 
2393     // Common exit point (success or failure).
2394     __ BIND(L_done);
2395     __ movptr(r13, Address(rsp, saved_r13_offset * wordSize));
2396     __ movptr(r14, Address(rsp, saved_r14_offset * wordSize));
2397     restore_arg_regs();
2398     inc_counter_np(SharedRuntime::_checkcast_array_copy_ctr); // Update counter after rscratch1 is free
2399     __ leave(); // required for proper stackwalking of RuntimeStub frame
2400     __ ret(0);
2401 
2402     return start;
2403   }
2404 
2405   //
2406   //  Generate 'unsafe' array copy stub
2407   //  Though just as safe as the other stubs, it takes an unscaled
2408   //  size_t argument instead of an element count.
2409   //
2410   //  Input:
2411   //    c_rarg0   - source array address
2412   //    c_rarg1   - destination array address
2413   //    c_rarg2   - byte count, treated as ssize_t, can be zero
2414   //
2415   // Examines the alignment of the operands and dispatches
2416   // to a long, int, short, or byte copy loop.
2417   //
2418   address generate_unsafe_copy(const char *name,
2419                                address byte_copy_entry, address short_copy_entry,
2420                                address int_copy_entry, address long_copy_entry) {
2421 
2422     Label L_long_aligned, L_int_aligned, L_short_aligned;
2423 
2424     // Input registers (before setup_arg_regs)
2425     const Register from        = c_rarg0;  // source array address
2426     const Register to          = c_rarg1;  // destination array address
2427     const Register size        = c_rarg2;  // byte count (size_t)
2428 
2429     // Register used as a temp
2430     const Register bits        = rax;      // test copy of low bits
2431 
2432     __ align(CodeEntryAlignment);
2433     StubCodeMark mark(this, "StubRoutines", name);
2434     address start = __ pc();
2435 
2436     __ enter(); // required for proper stackwalking of RuntimeStub frame
2437 
2438     // bump this on entry, not on exit:
2439     inc_counter_np(SharedRuntime::_unsafe_array_copy_ctr);
2440 
2441     __ mov(bits, from);
2442     __ orptr(bits, to);
2443     __ orptr(bits, size);
2444 
2445     __ testb(bits, BytesPerLong-1);
2446     __ jccb(Assembler::zero, L_long_aligned);
2447 
2448     __ testb(bits, BytesPerInt-1);
2449     __ jccb(Assembler::zero, L_int_aligned);
2450 
2451     __ testb(bits, BytesPerShort-1);
2452     __ jump_cc(Assembler::notZero, RuntimeAddress(byte_copy_entry));
2453 
2454     __ BIND(L_short_aligned);
2455     __ shrptr(size, LogBytesPerShort); // size => short_count
2456     __ jump(RuntimeAddress(short_copy_entry));
2457 
2458     __ BIND(L_int_aligned);
2459     __ shrptr(size, LogBytesPerInt); // size => int_count
2460     __ jump(RuntimeAddress(int_copy_entry));
2461 
2462     __ BIND(L_long_aligned);
2463     __ shrptr(size, LogBytesPerLong); // size => qword_count
2464     __ jump(RuntimeAddress(long_copy_entry));
2465 
2466     return start;
2467   }
2468 
2469   // Perform range checks on the proposed arraycopy.
2470   // Kills temp, but nothing else.
2471   // Also, clean the sign bits of src_pos and dst_pos.
2472   void arraycopy_range_checks(Register src,     // source array oop (c_rarg0)
2473                               Register src_pos, // source position (c_rarg1)
2474                               Register dst,     // destination array oo (c_rarg2)
2475                               Register dst_pos, // destination position (c_rarg3)
2476                               Register length,
2477                               Register temp,
2478                               Label& L_failed) {
2479     BLOCK_COMMENT("arraycopy_range_checks:");
2480 
2481     //  if (src_pos + length > arrayOop(src)->length())  FAIL;
2482     __ movl(temp, length);
2483     __ addl(temp, src_pos);             // src_pos + length
2484     __ cmpl(temp, Address(src, arrayOopDesc::length_offset_in_bytes()));
2485     __ jcc(Assembler::above, L_failed);
2486 
2487     //  if (dst_pos + length > arrayOop(dst)->length())  FAIL;
2488     __ movl(temp, length);
2489     __ addl(temp, dst_pos);             // dst_pos + length
2490     __ cmpl(temp, Address(dst, arrayOopDesc::length_offset_in_bytes()));
2491     __ jcc(Assembler::above, L_failed);
2492 
2493     // Have to clean up high 32-bits of 'src_pos' and 'dst_pos'.
2494     // Move with sign extension can be used since they are positive.
2495     __ movslq(src_pos, src_pos);
2496     __ movslq(dst_pos, dst_pos);
2497 
2498     BLOCK_COMMENT("arraycopy_range_checks done");
2499   }
2500 
2501   //
2502   //  Generate generic array copy stubs
2503   //
2504   //  Input:
2505   //    c_rarg0    -  src oop
2506   //    c_rarg1    -  src_pos (32-bits)
2507   //    c_rarg2    -  dst oop
2508   //    c_rarg3    -  dst_pos (32-bits)
2509   // not Win64
2510   //    c_rarg4    -  element count (32-bits)
2511   // Win64
2512   //    rsp+40     -  element count (32-bits)
2513   //
2514   //  Output:
2515   //    rax ==  0  -  success
2516   //    rax == -1^K - failure, where K is partial transfer count
2517   //
2518   address generate_generic_copy(const char *name,
2519                                 address byte_copy_entry, address short_copy_entry,
2520                                 address int_copy_entry, address oop_copy_entry,
2521                                 address long_copy_entry, address checkcast_copy_entry) {
2522 
2523     Label L_failed, L_failed_0, L_objArray;
2524     Label L_copy_bytes, L_copy_shorts, L_copy_ints, L_copy_longs;
2525 
2526     // Input registers
2527     const Register src        = c_rarg0;  // source array oop
2528     const Register src_pos    = c_rarg1;  // source position
2529     const Register dst        = c_rarg2;  // destination array oop
2530     const Register dst_pos    = c_rarg3;  // destination position
2531 #ifndef _WIN64
2532     const Register length     = c_rarg4;
2533 #else
2534     const Address  length(rsp, 6 * wordSize);  // elements count is on stack on Win64
2535 #endif
2536 
2537     { int modulus = CodeEntryAlignment;
2538       int target  = modulus - 5; // 5 = sizeof jmp(L_failed)
2539       int advance = target - (__ offset() % modulus);
2540       if (advance < 0)  advance += modulus;
2541       if (advance > 0)  __ nop(advance);
2542     }
2543     StubCodeMark mark(this, "StubRoutines", name);
2544 
2545     // Short-hop target to L_failed.  Makes for denser prologue code.
2546     __ BIND(L_failed_0);
2547     __ jmp(L_failed);
2548     assert(__ offset() % CodeEntryAlignment == 0, "no further alignment needed");
2549 
2550     __ align(CodeEntryAlignment);
2551     address start = __ pc();
2552 
2553     __ enter(); // required for proper stackwalking of RuntimeStub frame
2554 
2555     // bump this on entry, not on exit:
2556     inc_counter_np(SharedRuntime::_generic_array_copy_ctr);
2557 
2558     //-----------------------------------------------------------------------
2559     // Assembler stub will be used for this call to arraycopy
2560     // if the following conditions are met:
2561     //
2562     // (1) src and dst must not be null.
2563     // (2) src_pos must not be negative.
2564     // (3) dst_pos must not be negative.
2565     // (4) length  must not be negative.
2566     // (5) src klass and dst klass should be the same and not NULL.
2567     // (6) src and dst should be arrays.
2568     // (7) src_pos + length must not exceed length of src.
2569     // (8) dst_pos + length must not exceed length of dst.
2570     //
2571 
2572     //  if (src == NULL) return -1;
2573     __ testptr(src, src);         // src oop
2574     size_t j1off = __ offset();
2575     __ jccb(Assembler::zero, L_failed_0);
2576 
2577     //  if (src_pos < 0) return -1;
2578     __ testl(src_pos, src_pos); // src_pos (32-bits)
2579     __ jccb(Assembler::negative, L_failed_0);
2580 
2581     //  if (dst == NULL) return -1;
2582     __ testptr(dst, dst);         // dst oop
2583     __ jccb(Assembler::zero, L_failed_0);
2584 
2585     //  if (dst_pos < 0) return -1;
2586     __ testl(dst_pos, dst_pos); // dst_pos (32-bits)
2587     size_t j4off = __ offset();
2588     __ jccb(Assembler::negative, L_failed_0);
2589 
2590     // The first four tests are very dense code,
2591     // but not quite dense enough to put four
2592     // jumps in a 16-byte instruction fetch buffer.
2593     // That's good, because some branch predicters
2594     // do not like jumps so close together.
2595     // Make sure of this.
2596     guarantee(((j1off ^ j4off) & ~15) != 0, "I$ line of 1st & 4th jumps");
2597 
2598     // registers used as temp
2599     const Register r11_length    = r11; // elements count to copy
2600     const Register r10_src_klass = r10; // array klass
2601 
2602     //  if (length < 0) return -1;
2603     __ movl(r11_length, length);        // length (elements count, 32-bits value)
2604     __ testl(r11_length, r11_length);
2605     __ jccb(Assembler::negative, L_failed_0);
2606 
2607     __ load_klass(r10_src_klass, src);
2608 #ifdef ASSERT
2609     //  assert(src->klass() != NULL);
2610     {
2611       BLOCK_COMMENT("assert klasses not null {");
2612       Label L1, L2;
2613       __ testptr(r10_src_klass, r10_src_klass);
2614       __ jcc(Assembler::notZero, L2);   // it is broken if klass is NULL
2615       __ bind(L1);
2616       __ stop("broken null klass");
2617       __ bind(L2);
2618       __ load_klass(rax, dst);
2619       __ cmpq(rax, 0);
2620       __ jcc(Assembler::equal, L1);     // this would be broken also
2621       BLOCK_COMMENT("} assert klasses not null done");
2622     }
2623 #endif
2624 
2625     // Load layout helper (32-bits)
2626     //
2627     //  |array_tag|     | header_size | element_type |     |log2_element_size|
2628     // 32        30    24            16              8     2                 0
2629     //
2630     //   array_tag: typeArray = 0x3, objArray = 0x2, non-array = 0x0
2631     //
2632 
2633     const int lh_offset = in_bytes(Klass::layout_helper_offset());
2634 
2635     // Handle objArrays completely differently...
2636     const jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
2637     __ cmpl(Address(r10_src_klass, lh_offset), objArray_lh);
2638     __ jcc(Assembler::equal, L_objArray);
2639 
2640     //  if (src->klass() != dst->klass()) return -1;
2641     __ load_klass(rax, dst);
2642     __ cmpq(r10_src_klass, rax);
2643     __ jcc(Assembler::notEqual, L_failed);
2644 
2645     const Register rax_lh = rax;  // layout helper
2646     __ movl(rax_lh, Address(r10_src_klass, lh_offset));
2647 
2648     //  if (!src->is_Array()) return -1;
2649     __ cmpl(rax_lh, Klass::_lh_neutral_value);
2650     __ jcc(Assembler::greaterEqual, L_failed);
2651 
2652     // At this point, it is known to be a typeArray (array_tag 0x3).
2653 #ifdef ASSERT
2654     {
2655       BLOCK_COMMENT("assert primitive array {");
2656       Label L;
2657       __ cmpl(rax_lh, (Klass::_lh_array_tag_type_value << Klass::_lh_array_tag_shift));
2658       __ jcc(Assembler::greaterEqual, L);
2659       __ stop("must be a primitive array");
2660       __ bind(L);
2661       BLOCK_COMMENT("} assert primitive array done");
2662     }
2663 #endif
2664 
2665     arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
2666                            r10, L_failed);
2667 
2668     // TypeArrayKlass
2669     //
2670     // src_addr = (src + array_header_in_bytes()) + (src_pos << log2elemsize);
2671     // dst_addr = (dst + array_header_in_bytes()) + (dst_pos << log2elemsize);
2672     //
2673 
2674     const Register r10_offset = r10;    // array offset
2675     const Register rax_elsize = rax_lh; // element size
2676 
2677     __ movl(r10_offset, rax_lh);
2678     __ shrl(r10_offset, Klass::_lh_header_size_shift);
2679     __ andptr(r10_offset, Klass::_lh_header_size_mask);   // array_offset
2680     __ addptr(src, r10_offset);           // src array offset
2681     __ addptr(dst, r10_offset);           // dst array offset
2682     BLOCK_COMMENT("choose copy loop based on element size");
2683     __ andl(rax_lh, Klass::_lh_log2_element_size_mask); // rax_lh -> rax_elsize
2684 
2685     // next registers should be set before the jump to corresponding stub
2686     const Register from     = c_rarg0;  // source array address
2687     const Register to       = c_rarg1;  // destination array address
2688     const Register count    = c_rarg2;  // elements count
2689 
2690     // 'from', 'to', 'count' registers should be set in such order
2691     // since they are the same as 'src', 'src_pos', 'dst'.
2692 
2693   __ BIND(L_copy_bytes);
2694     __ cmpl(rax_elsize, 0);
2695     __ jccb(Assembler::notEqual, L_copy_shorts);
2696     __ lea(from, Address(src, src_pos, Address::times_1, 0));// src_addr
2697     __ lea(to,   Address(dst, dst_pos, Address::times_1, 0));// dst_addr
2698     __ movl2ptr(count, r11_length); // length
2699     __ jump(RuntimeAddress(byte_copy_entry));
2700 
2701   __ BIND(L_copy_shorts);
2702     __ cmpl(rax_elsize, LogBytesPerShort);
2703     __ jccb(Assembler::notEqual, L_copy_ints);
2704     __ lea(from, Address(src, src_pos, Address::times_2, 0));// src_addr
2705     __ lea(to,   Address(dst, dst_pos, Address::times_2, 0));// dst_addr
2706     __ movl2ptr(count, r11_length); // length
2707     __ jump(RuntimeAddress(short_copy_entry));
2708 
2709   __ BIND(L_copy_ints);
2710     __ cmpl(rax_elsize, LogBytesPerInt);
2711     __ jccb(Assembler::notEqual, L_copy_longs);
2712     __ lea(from, Address(src, src_pos, Address::times_4, 0));// src_addr
2713     __ lea(to,   Address(dst, dst_pos, Address::times_4, 0));// dst_addr
2714     __ movl2ptr(count, r11_length); // length
2715     __ jump(RuntimeAddress(int_copy_entry));
2716 
2717   __ BIND(L_copy_longs);
2718 #ifdef ASSERT
2719     {
2720       BLOCK_COMMENT("assert long copy {");
2721       Label L;
2722       __ cmpl(rax_elsize, LogBytesPerLong);
2723       __ jcc(Assembler::equal, L);
2724       __ stop("must be long copy, but elsize is wrong");
2725       __ bind(L);
2726       BLOCK_COMMENT("} assert long copy done");
2727     }
2728 #endif
2729     __ lea(from, Address(src, src_pos, Address::times_8, 0));// src_addr
2730     __ lea(to,   Address(dst, dst_pos, Address::times_8, 0));// dst_addr
2731     __ movl2ptr(count, r11_length); // length
2732     __ jump(RuntimeAddress(long_copy_entry));
2733 
2734     // ObjArrayKlass
2735   __ BIND(L_objArray);
2736     // live at this point:  r10_src_klass, r11_length, src[_pos], dst[_pos]
2737 
2738     Label L_plain_copy, L_checkcast_copy;
2739     //  test array classes for subtyping
2740     __ load_klass(rax, dst);
2741     __ cmpq(r10_src_klass, rax); // usual case is exact equality
2742     __ jcc(Assembler::notEqual, L_checkcast_copy);
2743 
2744     // Identically typed arrays can be copied without element-wise checks.
2745     arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
2746                            r10, L_failed);
2747 
2748     __ lea(from, Address(src, src_pos, TIMES_OOP,
2749                  arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // src_addr
2750     __ lea(to,   Address(dst, dst_pos, TIMES_OOP,
2751                  arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // dst_addr
2752     __ movl2ptr(count, r11_length); // length
2753   __ BIND(L_plain_copy);
2754     __ jump(RuntimeAddress(oop_copy_entry));
2755 
2756   __ BIND(L_checkcast_copy);
2757     // live at this point:  r10_src_klass, r11_length, rax (dst_klass)
2758     {
2759       // Before looking at dst.length, make sure dst is also an objArray.
2760       __ cmpl(Address(rax, lh_offset), objArray_lh);
2761       __ jcc(Assembler::notEqual, L_failed);
2762 
2763       // It is safe to examine both src.length and dst.length.
2764       arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
2765                              rax, L_failed);
2766 
2767       const Register r11_dst_klass = r11;
2768       __ load_klass(r11_dst_klass, dst); // reload
2769 
2770       // Marshal the base address arguments now, freeing registers.
2771       __ lea(from, Address(src, src_pos, TIMES_OOP,
2772                    arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
2773       __ lea(to,   Address(dst, dst_pos, TIMES_OOP,
2774                    arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
2775       __ movl(count, length);           // length (reloaded)
2776       Register sco_temp = c_rarg3;      // this register is free now
2777       assert_different_registers(from, to, count, sco_temp,
2778                                  r11_dst_klass, r10_src_klass);
2779       assert_clean_int(count, sco_temp);
2780 
2781       // Generate the type check.
2782       const int sco_offset = in_bytes(Klass::super_check_offset_offset());
2783       __ movl(sco_temp, Address(r11_dst_klass, sco_offset));
2784       assert_clean_int(sco_temp, rax);
2785       generate_type_check(r10_src_klass, sco_temp, r11_dst_klass, L_plain_copy);
2786 
2787       // Fetch destination element klass from the ObjArrayKlass header.
2788       int ek_offset = in_bytes(ObjArrayKlass::element_klass_offset());
2789       __ movptr(r11_dst_klass, Address(r11_dst_klass, ek_offset));
2790       __ movl(  sco_temp,      Address(r11_dst_klass, sco_offset));
2791       assert_clean_int(sco_temp, rax);
2792 
2793       // the checkcast_copy loop needs two extra arguments:
2794       assert(c_rarg3 == sco_temp, "#3 already in place");
2795       // Set up arguments for checkcast_copy_entry.
2796       setup_arg_regs(4);
2797       __ movptr(r8, r11_dst_klass);  // dst.klass.element_klass, r8 is c_rarg4 on Linux/Solaris
2798       __ jump(RuntimeAddress(checkcast_copy_entry));
2799     }
2800 
2801   __ BIND(L_failed);
2802     __ xorptr(rax, rax);
2803     __ notptr(rax); // return -1
2804     __ leave();   // required for proper stackwalking of RuntimeStub frame
2805     __ ret(0);
2806 
2807     return start;
2808   }
2809 
2810   void generate_arraycopy_stubs() {
2811     address entry;
2812     address entry_jbyte_arraycopy;
2813     address entry_jshort_arraycopy;
2814     address entry_jint_arraycopy;
2815     address entry_oop_arraycopy;
2816     address entry_jlong_arraycopy;
2817     address entry_checkcast_arraycopy;
2818 
2819     StubRoutines::_jbyte_disjoint_arraycopy  = generate_disjoint_byte_copy(false, &entry,
2820                                                                            "jbyte_disjoint_arraycopy");
2821     StubRoutines::_jbyte_arraycopy           = generate_conjoint_byte_copy(false, entry, &entry_jbyte_arraycopy,
2822                                                                            "jbyte_arraycopy");
2823 
2824     StubRoutines::_jshort_disjoint_arraycopy = generate_disjoint_short_copy(false, &entry,
2825                                                                             "jshort_disjoint_arraycopy");
2826     StubRoutines::_jshort_arraycopy          = generate_conjoint_short_copy(false, entry, &entry_jshort_arraycopy,
2827                                                                             "jshort_arraycopy");
2828 
2829     StubRoutines::_jint_disjoint_arraycopy   = generate_disjoint_int_oop_copy(false, false, &entry,
2830                                                                               "jint_disjoint_arraycopy");
2831     StubRoutines::_jint_arraycopy            = generate_conjoint_int_oop_copy(false, false, entry,
2832                                                                               &entry_jint_arraycopy, "jint_arraycopy");
2833 
2834     StubRoutines::_jlong_disjoint_arraycopy  = generate_disjoint_long_oop_copy(false, false, &entry,
2835                                                                                "jlong_disjoint_arraycopy");
2836     StubRoutines::_jlong_arraycopy           = generate_conjoint_long_oop_copy(false, false, entry,
2837                                                                                &entry_jlong_arraycopy, "jlong_arraycopy");
2838 
2839 
2840     if (UseCompressedOops) {
2841       StubRoutines::_oop_disjoint_arraycopy  = generate_disjoint_int_oop_copy(false, true, &entry,
2842                                                                               "oop_disjoint_arraycopy");
2843       StubRoutines::_oop_arraycopy           = generate_conjoint_int_oop_copy(false, true, entry,
2844                                                                               &entry_oop_arraycopy, "oop_arraycopy");
2845       StubRoutines::_oop_disjoint_arraycopy_uninit  = generate_disjoint_int_oop_copy(false, true, &entry,
2846                                                                                      "oop_disjoint_arraycopy_uninit",
2847                                                                                      /*dest_uninitialized*/true);
2848       StubRoutines::_oop_arraycopy_uninit           = generate_conjoint_int_oop_copy(false, true, entry,
2849                                                                                      NULL, "oop_arraycopy_uninit",
2850                                                                                      /*dest_uninitialized*/true);
2851     } else {
2852       StubRoutines::_oop_disjoint_arraycopy  = generate_disjoint_long_oop_copy(false, true, &entry,
2853                                                                                "oop_disjoint_arraycopy");
2854       StubRoutines::_oop_arraycopy           = generate_conjoint_long_oop_copy(false, true, entry,
2855                                                                                &entry_oop_arraycopy, "oop_arraycopy");
2856       StubRoutines::_oop_disjoint_arraycopy_uninit  = generate_disjoint_long_oop_copy(false, true, &entry,
2857                                                                                       "oop_disjoint_arraycopy_uninit",
2858                                                                                       /*dest_uninitialized*/true);
2859       StubRoutines::_oop_arraycopy_uninit           = generate_conjoint_long_oop_copy(false, true, entry,
2860                                                                                       NULL, "oop_arraycopy_uninit",
2861                                                                                       /*dest_uninitialized*/true);
2862     }
2863 
2864     StubRoutines::_checkcast_arraycopy        = generate_checkcast_copy("checkcast_arraycopy", &entry_checkcast_arraycopy);
2865     StubRoutines::_checkcast_arraycopy_uninit = generate_checkcast_copy("checkcast_arraycopy_uninit", NULL,
2866                                                                         /*dest_uninitialized*/true);
2867 
2868     StubRoutines::_unsafe_arraycopy    = generate_unsafe_copy("unsafe_arraycopy",
2869                                                               entry_jbyte_arraycopy,
2870                                                               entry_jshort_arraycopy,
2871                                                               entry_jint_arraycopy,
2872                                                               entry_jlong_arraycopy);
2873     StubRoutines::_generic_arraycopy   = generate_generic_copy("generic_arraycopy",
2874                                                                entry_jbyte_arraycopy,
2875                                                                entry_jshort_arraycopy,
2876                                                                entry_jint_arraycopy,
2877                                                                entry_oop_arraycopy,
2878                                                                entry_jlong_arraycopy,
2879                                                                entry_checkcast_arraycopy);
2880 
2881     StubRoutines::_jbyte_fill = generate_fill(T_BYTE, false, "jbyte_fill");
2882     StubRoutines::_jshort_fill = generate_fill(T_SHORT, false, "jshort_fill");
2883     StubRoutines::_jint_fill = generate_fill(T_INT, false, "jint_fill");
2884     StubRoutines::_arrayof_jbyte_fill = generate_fill(T_BYTE, true, "arrayof_jbyte_fill");
2885     StubRoutines::_arrayof_jshort_fill = generate_fill(T_SHORT, true, "arrayof_jshort_fill");
2886     StubRoutines::_arrayof_jint_fill = generate_fill(T_INT, true, "arrayof_jint_fill");
2887 
2888     // We don't generate specialized code for HeapWord-aligned source
2889     // arrays, so just use the code we've already generated
2890     StubRoutines::_arrayof_jbyte_disjoint_arraycopy  = StubRoutines::_jbyte_disjoint_arraycopy;
2891     StubRoutines::_arrayof_jbyte_arraycopy           = StubRoutines::_jbyte_arraycopy;
2892 
2893     StubRoutines::_arrayof_jshort_disjoint_arraycopy = StubRoutines::_jshort_disjoint_arraycopy;
2894     StubRoutines::_arrayof_jshort_arraycopy          = StubRoutines::_jshort_arraycopy;
2895 
2896     StubRoutines::_arrayof_jint_disjoint_arraycopy   = StubRoutines::_jint_disjoint_arraycopy;
2897     StubRoutines::_arrayof_jint_arraycopy            = StubRoutines::_jint_arraycopy;
2898 
2899     StubRoutines::_arrayof_jlong_disjoint_arraycopy  = StubRoutines::_jlong_disjoint_arraycopy;
2900     StubRoutines::_arrayof_jlong_arraycopy           = StubRoutines::_jlong_arraycopy;
2901 
2902     StubRoutines::_arrayof_oop_disjoint_arraycopy    = StubRoutines::_oop_disjoint_arraycopy;
2903     StubRoutines::_arrayof_oop_arraycopy             = StubRoutines::_oop_arraycopy;
2904 
2905     StubRoutines::_arrayof_oop_disjoint_arraycopy_uninit    = StubRoutines::_oop_disjoint_arraycopy_uninit;
2906     StubRoutines::_arrayof_oop_arraycopy_uninit             = StubRoutines::_oop_arraycopy_uninit;
2907   }
2908 
2909   void generate_math_stubs() {
2910     {
2911       StubCodeMark mark(this, "StubRoutines", "log");
2912       StubRoutines::_intrinsic_log = (double (*)(double)) __ pc();
2913 
2914       __ subq(rsp, 8);
2915       __ movdbl(Address(rsp, 0), xmm0);
2916       __ fld_d(Address(rsp, 0));
2917       __ flog();
2918       __ fstp_d(Address(rsp, 0));
2919       __ movdbl(xmm0, Address(rsp, 0));
2920       __ addq(rsp, 8);
2921       __ ret(0);
2922     }
2923     {
2924       StubCodeMark mark(this, "StubRoutines", "log10");
2925       StubRoutines::_intrinsic_log10 = (double (*)(double)) __ pc();
2926 
2927       __ subq(rsp, 8);
2928       __ movdbl(Address(rsp, 0), xmm0);
2929       __ fld_d(Address(rsp, 0));
2930       __ flog10();
2931       __ fstp_d(Address(rsp, 0));
2932       __ movdbl(xmm0, Address(rsp, 0));
2933       __ addq(rsp, 8);
2934       __ ret(0);
2935     }
2936     {
2937       StubCodeMark mark(this, "StubRoutines", "sin");
2938       StubRoutines::_intrinsic_sin = (double (*)(double)) __ pc();
2939 
2940       __ subq(rsp, 8);
2941       __ movdbl(Address(rsp, 0), xmm0);
2942       __ fld_d(Address(rsp, 0));
2943       __ trigfunc('s');
2944       __ fstp_d(Address(rsp, 0));
2945       __ movdbl(xmm0, Address(rsp, 0));
2946       __ addq(rsp, 8);
2947       __ ret(0);
2948     }
2949     {
2950       StubCodeMark mark(this, "StubRoutines", "cos");
2951       StubRoutines::_intrinsic_cos = (double (*)(double)) __ pc();
2952 
2953       __ subq(rsp, 8);
2954       __ movdbl(Address(rsp, 0), xmm0);
2955       __ fld_d(Address(rsp, 0));
2956       __ trigfunc('c');
2957       __ fstp_d(Address(rsp, 0));
2958       __ movdbl(xmm0, Address(rsp, 0));
2959       __ addq(rsp, 8);
2960       __ ret(0);
2961     }
2962     {
2963       StubCodeMark mark(this, "StubRoutines", "tan");
2964       StubRoutines::_intrinsic_tan = (double (*)(double)) __ pc();
2965 
2966       __ subq(rsp, 8);
2967       __ movdbl(Address(rsp, 0), xmm0);
2968       __ fld_d(Address(rsp, 0));
2969       __ trigfunc('t');
2970       __ fstp_d(Address(rsp, 0));
2971       __ movdbl(xmm0, Address(rsp, 0));
2972       __ addq(rsp, 8);
2973       __ ret(0);
2974     }
2975     {
2976       StubCodeMark mark(this, "StubRoutines", "exp");
2977       StubRoutines::_intrinsic_exp = (double (*)(double)) __ pc();
2978 
2979       __ subq(rsp, 8);
2980       __ movdbl(Address(rsp, 0), xmm0);
2981       __ fld_d(Address(rsp, 0));
2982       __ exp_with_fallback(0);
2983       __ fstp_d(Address(rsp, 0));
2984       __ movdbl(xmm0, Address(rsp, 0));
2985       __ addq(rsp, 8);
2986       __ ret(0);
2987     }
2988     {
2989       StubCodeMark mark(this, "StubRoutines", "pow");
2990       StubRoutines::_intrinsic_pow = (double (*)(double,double)) __ pc();
2991 
2992       __ subq(rsp, 8);
2993       __ movdbl(Address(rsp, 0), xmm1);
2994       __ fld_d(Address(rsp, 0));
2995       __ movdbl(Address(rsp, 0), xmm0);
2996       __ fld_d(Address(rsp, 0));
2997       __ pow_with_fallback(0);
2998       __ fstp_d(Address(rsp, 0));
2999       __ movdbl(xmm0, Address(rsp, 0));
3000       __ addq(rsp, 8);
3001       __ ret(0);
3002     }
3003   }
3004 
3005   // AES intrinsic stubs
3006   enum {AESBlockSize = 16};
3007 
3008   address generate_key_shuffle_mask() {
3009     __ align(16);
3010     StubCodeMark mark(this, "StubRoutines", "key_shuffle_mask");
3011     address start = __ pc();
3012     __ emit_data64( 0x0405060700010203, relocInfo::none );
3013     __ emit_data64( 0x0c0d0e0f08090a0b, relocInfo::none );
3014     return start;
3015   }
3016 
3017   // Utility routine for loading a 128-bit key word in little endian format
3018   // can optionally specify that the shuffle mask is already in an xmmregister
3019   void load_key(XMMRegister xmmdst, Register key, int offset, XMMRegister xmm_shuf_mask=NULL) {
3020     __ movdqu(xmmdst, Address(key, offset));
3021     if (xmm_shuf_mask != NULL) {
3022       __ pshufb(xmmdst, xmm_shuf_mask);
3023     } else {
3024       __ pshufb(xmmdst, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
3025     }
3026   }
3027 
3028   // Arguments:
3029   //
3030   // Inputs:
3031   //   c_rarg0   - source byte array address
3032   //   c_rarg1   - destination byte array address
3033   //   c_rarg2   - K (key) in little endian int array
3034   //
3035   address generate_aescrypt_encryptBlock() {
3036     assert(UseAES, "need AES instructions and misaligned SSE support");
3037     __ align(CodeEntryAlignment);
3038     StubCodeMark mark(this, "StubRoutines", "aescrypt_encryptBlock");
3039     Label L_doLast;
3040     address start = __ pc();
3041 
3042     const Register from        = c_rarg0;  // source array address
3043     const Register to          = c_rarg1;  // destination array address
3044     const Register key         = c_rarg2;  // key array address
3045     const Register keylen      = rax;
3046 
3047     const XMMRegister xmm_result = xmm0;
3048     const XMMRegister xmm_key_shuf_mask = xmm1;
3049     // On win64 xmm6-xmm15 must be preserved so don't use them.
3050     const XMMRegister xmm_temp1  = xmm2;
3051     const XMMRegister xmm_temp2  = xmm3;
3052     const XMMRegister xmm_temp3  = xmm4;
3053     const XMMRegister xmm_temp4  = xmm5;
3054 
3055     __ enter(); // required for proper stackwalking of RuntimeStub frame
3056 
3057     // keylen could be only {11, 13, 15} * 4 = {44, 52, 60}
3058     __ movl(keylen, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
3059 
3060     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
3061     __ movdqu(xmm_result, Address(from, 0));  // get 16 bytes of input
3062 
3063     // For encryption, the java expanded key ordering is just what we need
3064     // we don't know if the key is aligned, hence not using load-execute form
3065 
3066     load_key(xmm_temp1, key, 0x00, xmm_key_shuf_mask);
3067     __ pxor(xmm_result, xmm_temp1);
3068 
3069     load_key(xmm_temp1, key, 0x10, xmm_key_shuf_mask);
3070     load_key(xmm_temp2, key, 0x20, xmm_key_shuf_mask);
3071     load_key(xmm_temp3, key, 0x30, xmm_key_shuf_mask);
3072     load_key(xmm_temp4, key, 0x40, xmm_key_shuf_mask);
3073 
3074     __ aesenc(xmm_result, xmm_temp1);
3075     __ aesenc(xmm_result, xmm_temp2);
3076     __ aesenc(xmm_result, xmm_temp3);
3077     __ aesenc(xmm_result, xmm_temp4);
3078 
3079     load_key(xmm_temp1, key, 0x50, xmm_key_shuf_mask);
3080     load_key(xmm_temp2, key, 0x60, xmm_key_shuf_mask);
3081     load_key(xmm_temp3, key, 0x70, xmm_key_shuf_mask);
3082     load_key(xmm_temp4, key, 0x80, xmm_key_shuf_mask);
3083 
3084     __ aesenc(xmm_result, xmm_temp1);
3085     __ aesenc(xmm_result, xmm_temp2);
3086     __ aesenc(xmm_result, xmm_temp3);
3087     __ aesenc(xmm_result, xmm_temp4);
3088 
3089     load_key(xmm_temp1, key, 0x90, xmm_key_shuf_mask);
3090     load_key(xmm_temp2, key, 0xa0, xmm_key_shuf_mask);
3091 
3092     __ cmpl(keylen, 44);
3093     __ jccb(Assembler::equal, L_doLast);
3094 
3095     __ aesenc(xmm_result, xmm_temp1);
3096     __ aesenc(xmm_result, xmm_temp2);
3097 
3098     load_key(xmm_temp1, key, 0xb0, xmm_key_shuf_mask);
3099     load_key(xmm_temp2, key, 0xc0, xmm_key_shuf_mask);
3100 
3101     __ cmpl(keylen, 52);
3102     __ jccb(Assembler::equal, L_doLast);
3103 
3104     __ aesenc(xmm_result, xmm_temp1);
3105     __ aesenc(xmm_result, xmm_temp2);
3106 
3107     load_key(xmm_temp1, key, 0xd0, xmm_key_shuf_mask);
3108     load_key(xmm_temp2, key, 0xe0, xmm_key_shuf_mask);
3109 
3110     __ BIND(L_doLast);
3111     __ aesenc(xmm_result, xmm_temp1);
3112     __ aesenclast(xmm_result, xmm_temp2);
3113     __ movdqu(Address(to, 0), xmm_result);        // store the result
3114     __ xorptr(rax, rax); // return 0
3115     __ leave(); // required for proper stackwalking of RuntimeStub frame
3116     __ ret(0);
3117 
3118     return start;
3119   }
3120 
3121 
3122   // Arguments:
3123   //
3124   // Inputs:
3125   //   c_rarg0   - source byte array address
3126   //   c_rarg1   - destination byte array address
3127   //   c_rarg2   - K (key) in little endian int array
3128   //
3129   address generate_aescrypt_decryptBlock() {
3130     assert(UseAES, "need AES instructions and misaligned SSE support");
3131     __ align(CodeEntryAlignment);
3132     StubCodeMark mark(this, "StubRoutines", "aescrypt_decryptBlock");
3133     Label L_doLast;
3134     address start = __ pc();
3135 
3136     const Register from        = c_rarg0;  // source array address
3137     const Register to          = c_rarg1;  // destination array address
3138     const Register key         = c_rarg2;  // key array address
3139     const Register keylen      = rax;
3140 
3141     const XMMRegister xmm_result = xmm0;
3142     const XMMRegister xmm_key_shuf_mask = xmm1;
3143     // On win64 xmm6-xmm15 must be preserved so don't use them.
3144     const XMMRegister xmm_temp1  = xmm2;
3145     const XMMRegister xmm_temp2  = xmm3;
3146     const XMMRegister xmm_temp3  = xmm4;
3147     const XMMRegister xmm_temp4  = xmm5;
3148 
3149     __ enter(); // required for proper stackwalking of RuntimeStub frame
3150 
3151     // keylen could be only {11, 13, 15} * 4 = {44, 52, 60}
3152     __ movl(keylen, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
3153 
3154     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
3155     __ movdqu(xmm_result, Address(from, 0));
3156 
3157     // for decryption java expanded key ordering is rotated one position from what we want
3158     // so we start from 0x10 here and hit 0x00 last
3159     // we don't know if the key is aligned, hence not using load-execute form
3160     load_key(xmm_temp1, key, 0x10, xmm_key_shuf_mask);
3161     load_key(xmm_temp2, key, 0x20, xmm_key_shuf_mask);
3162     load_key(xmm_temp3, key, 0x30, xmm_key_shuf_mask);
3163     load_key(xmm_temp4, key, 0x40, xmm_key_shuf_mask);
3164 
3165     __ pxor  (xmm_result, xmm_temp1);
3166     __ aesdec(xmm_result, xmm_temp2);
3167     __ aesdec(xmm_result, xmm_temp3);
3168     __ aesdec(xmm_result, xmm_temp4);
3169 
3170     load_key(xmm_temp1, key, 0x50, xmm_key_shuf_mask);
3171     load_key(xmm_temp2, key, 0x60, xmm_key_shuf_mask);
3172     load_key(xmm_temp3, key, 0x70, xmm_key_shuf_mask);
3173     load_key(xmm_temp4, key, 0x80, xmm_key_shuf_mask);
3174 
3175     __ aesdec(xmm_result, xmm_temp1);
3176     __ aesdec(xmm_result, xmm_temp2);
3177     __ aesdec(xmm_result, xmm_temp3);
3178     __ aesdec(xmm_result, xmm_temp4);
3179 
3180     load_key(xmm_temp1, key, 0x90, xmm_key_shuf_mask);
3181     load_key(xmm_temp2, key, 0xa0, xmm_key_shuf_mask);
3182     load_key(xmm_temp3, key, 0x00, xmm_key_shuf_mask);
3183 
3184     __ cmpl(keylen, 44);
3185     __ jccb(Assembler::equal, L_doLast);
3186 
3187     __ aesdec(xmm_result, xmm_temp1);
3188     __ aesdec(xmm_result, xmm_temp2);
3189 
3190     load_key(xmm_temp1, key, 0xb0, xmm_key_shuf_mask);
3191     load_key(xmm_temp2, key, 0xc0, xmm_key_shuf_mask);
3192 
3193     __ cmpl(keylen, 52);
3194     __ jccb(Assembler::equal, L_doLast);
3195 
3196     __ aesdec(xmm_result, xmm_temp1);
3197     __ aesdec(xmm_result, xmm_temp2);
3198 
3199     load_key(xmm_temp1, key, 0xd0, xmm_key_shuf_mask);
3200     load_key(xmm_temp2, key, 0xe0, xmm_key_shuf_mask);
3201 
3202     __ BIND(L_doLast);
3203     __ aesdec(xmm_result, xmm_temp1);
3204     __ aesdec(xmm_result, xmm_temp2);
3205 
3206     // for decryption the aesdeclast operation is always on key+0x00
3207     __ aesdeclast(xmm_result, xmm_temp3);
3208     __ movdqu(Address(to, 0), xmm_result);  // store the result
3209     __ xorptr(rax, rax); // return 0
3210     __ leave(); // required for proper stackwalking of RuntimeStub frame
3211     __ ret(0);
3212 
3213     return start;
3214   }
3215 
3216 
3217   // Arguments:
3218   //
3219   // Inputs:
3220   //   c_rarg0   - source byte array address
3221   //   c_rarg1   - destination byte array address
3222   //   c_rarg2   - K (key) in little endian int array
3223   //   c_rarg3   - r vector byte array address
3224   //   c_rarg4   - input length
3225   //
3226   // Output:
3227   //   rax       - input length
3228   //
3229   address generate_cipherBlockChaining_encryptAESCrypt() {
3230     assert(UseAES, "need AES instructions and misaligned SSE support");
3231     __ align(CodeEntryAlignment);
3232     StubCodeMark mark(this, "StubRoutines", "cipherBlockChaining_encryptAESCrypt");
3233     address start = __ pc();
3234 
3235     Label L_exit, L_key_192_256, L_key_256, L_loopTop_128, L_loopTop_192, L_loopTop_256;
3236     const Register from        = c_rarg0;  // source array address
3237     const Register to          = c_rarg1;  // destination array address
3238     const Register key         = c_rarg2;  // key array address
3239     const Register rvec        = c_rarg3;  // r byte array initialized from initvector array address
3240                                            // and left with the results of the last encryption block
3241 #ifndef _WIN64
3242     const Register len_reg     = c_rarg4;  // src len (must be multiple of blocksize 16)
3243 #else
3244     const Address  len_mem(rbp, 6 * wordSize);  // length is on stack on Win64
3245     const Register len_reg     = r10;      // pick the first volatile windows register
3246 #endif
3247     const Register pos         = rax;
3248 
3249     // xmm register assignments for the loops below
3250     const XMMRegister xmm_result = xmm0;
3251     const XMMRegister xmm_temp   = xmm1;
3252     // keys 0-10 preloaded into xmm2-xmm12
3253     const int XMM_REG_NUM_KEY_FIRST = 2;
3254     const int XMM_REG_NUM_KEY_LAST  = 15;
3255     const XMMRegister xmm_key0   = as_XMMRegister(XMM_REG_NUM_KEY_FIRST);
3256     const XMMRegister xmm_key10  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+10);
3257     const XMMRegister xmm_key11  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+11);
3258     const XMMRegister xmm_key12  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+12);
3259     const XMMRegister xmm_key13  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+13);
3260 
3261     __ enter(); // required for proper stackwalking of RuntimeStub frame
3262 
3263 #ifdef _WIN64
3264     // on win64, fill len_reg from stack position
3265     __ movl(len_reg, len_mem);
3266     // save the xmm registers which must be preserved 6-15
3267     __ subptr(rsp, -rsp_after_call_off * wordSize);
3268     for (int i = 6; i <= XMM_REG_NUM_KEY_LAST; i++) {
3269       __ movdqu(xmm_save(i), as_XMMRegister(i));
3270     }
3271 #else
3272     __ push(len_reg); // Save
3273 #endif
3274 
3275     const XMMRegister xmm_key_shuf_mask = xmm_temp;  // used temporarily to swap key bytes up front
3276     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
3277     // load up xmm regs xmm2 thru xmm12 with key 0x00 - 0xa0
3278     for (int rnum = XMM_REG_NUM_KEY_FIRST, offset = 0x00; rnum <= XMM_REG_NUM_KEY_FIRST+10; rnum++) {
3279       load_key(as_XMMRegister(rnum), key, offset, xmm_key_shuf_mask);
3280       offset += 0x10;
3281     }
3282     __ movdqu(xmm_result, Address(rvec, 0x00));   // initialize xmm_result with r vec
3283 
3284     // now split to different paths depending on the keylen (len in ints of AESCrypt.KLE array (52=192, or 60=256))
3285     __ movl(rax, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
3286     __ cmpl(rax, 44);
3287     __ jcc(Assembler::notEqual, L_key_192_256);
3288 
3289     // 128 bit code follows here
3290     __ movptr(pos, 0);
3291     __ align(OptoLoopAlignment);
3292 
3293     __ BIND(L_loopTop_128);
3294     __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of input
3295     __ pxor  (xmm_result, xmm_temp);               // xor with the current r vector
3296     __ pxor  (xmm_result, xmm_key0);               // do the aes rounds
3297     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_FIRST + 9; rnum++) {
3298       __ aesenc(xmm_result, as_XMMRegister(rnum));
3299     }
3300     __ aesenclast(xmm_result, xmm_key10);
3301     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);     // store into the next 16 bytes of output
3302     // no need to store r to memory until we exit
3303     __ addptr(pos, AESBlockSize);
3304     __ subptr(len_reg, AESBlockSize);
3305     __ jcc(Assembler::notEqual, L_loopTop_128);
3306 
3307     __ BIND(L_exit);
3308     __ movdqu(Address(rvec, 0), xmm_result);     // final value of r stored in rvec of CipherBlockChaining object
3309 
3310 #ifdef _WIN64
3311     // restore xmm regs belonging to calling function
3312     for (int i = 6; i <= XMM_REG_NUM_KEY_LAST; i++) {
3313       __ movdqu(as_XMMRegister(i), xmm_save(i));
3314     }
3315     __ movl(rax, len_mem);
3316 #else
3317     __ pop(rax); // return length
3318 #endif
3319     __ leave(); // required for proper stackwalking of RuntimeStub frame
3320     __ ret(0);
3321 
3322     __ BIND(L_key_192_256);
3323     // here rax = len in ints of AESCrypt.KLE array (52=192, or 60=256)
3324     load_key(xmm_key11, key, 0xb0, xmm_key_shuf_mask);
3325     load_key(xmm_key12, key, 0xc0, xmm_key_shuf_mask);
3326     __ cmpl(rax, 52);
3327     __ jcc(Assembler::notEqual, L_key_256);
3328 
3329     // 192-bit code follows here (could be changed to use more xmm registers)
3330     __ movptr(pos, 0);
3331     __ align(OptoLoopAlignment);
3332 
3333     __ BIND(L_loopTop_192);
3334     __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of input
3335     __ pxor  (xmm_result, xmm_temp);               // xor with the current r vector
3336     __ pxor  (xmm_result, xmm_key0);               // do the aes rounds
3337     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum  <= XMM_REG_NUM_KEY_FIRST + 11; rnum++) {
3338       __ aesenc(xmm_result, as_XMMRegister(rnum));
3339     }
3340     __ aesenclast(xmm_result, xmm_key12);
3341     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);     // store into the next 16 bytes of output
3342     // no need to store r to memory until we exit
3343     __ addptr(pos, AESBlockSize);
3344     __ subptr(len_reg, AESBlockSize);
3345     __ jcc(Assembler::notEqual, L_loopTop_192);
3346     __ jmp(L_exit);
3347 
3348     __ BIND(L_key_256);
3349     // 256-bit code follows here (could be changed to use more xmm registers)
3350     load_key(xmm_key13, key, 0xd0, xmm_key_shuf_mask);
3351     __ movptr(pos, 0);
3352     __ align(OptoLoopAlignment);
3353 
3354     __ BIND(L_loopTop_256);
3355     __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of input
3356     __ pxor  (xmm_result, xmm_temp);               // xor with the current r vector
3357     __ pxor  (xmm_result, xmm_key0);               // do the aes rounds
3358     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum  <= XMM_REG_NUM_KEY_FIRST + 13; rnum++) {
3359       __ aesenc(xmm_result, as_XMMRegister(rnum));
3360     }
3361     load_key(xmm_temp, key, 0xe0);
3362     __ aesenclast(xmm_result, xmm_temp);
3363     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);     // store into the next 16 bytes of output
3364     // no need to store r to memory until we exit
3365     __ addptr(pos, AESBlockSize);
3366     __ subptr(len_reg, AESBlockSize);
3367     __ jcc(Assembler::notEqual, L_loopTop_256);
3368     __ jmp(L_exit);
3369 
3370     return start;
3371   }
3372 
3373   // Safefetch stubs.
3374   void generate_safefetch(const char* name, int size, address* entry,
3375                           address* fault_pc, address* continuation_pc) {
3376     // safefetch signatures:
3377     //   int      SafeFetch32(int*      adr, int      errValue);
3378     //   intptr_t SafeFetchN (intptr_t* adr, intptr_t errValue);
3379     //
3380     // arguments:
3381     //   c_rarg0 = adr
3382     //   c_rarg1 = errValue
3383     //
3384     // result:
3385     //   PPC_RET  = *adr or errValue
3386 
3387     StubCodeMark mark(this, "StubRoutines", name);
3388 
3389     // Entry point, pc or function descriptor.
3390     *entry = __ pc();
3391 
3392     // Load *adr into c_rarg1, may fault.
3393     *fault_pc = __ pc();
3394     switch (size) {
3395       case 4:
3396         // int32_t
3397         __ movl(c_rarg1, Address(c_rarg0, 0));
3398         break;
3399       case 8:
3400         // int64_t
3401         __ movq(c_rarg1, Address(c_rarg0, 0));
3402         break;
3403       default:
3404         ShouldNotReachHere();
3405     }
3406 
3407     // return errValue or *adr
3408     *continuation_pc = __ pc();
3409     __ movq(rax, c_rarg1);
3410     __ ret(0);
3411   }
3412 
3413   // This is a version of CBC/AES Decrypt which does 4 blocks in a loop at a time
3414   // to hide instruction latency
3415   //
3416   // Arguments:
3417   //
3418   // Inputs:
3419   //   c_rarg0   - source byte array address
3420   //   c_rarg1   - destination byte array address
3421   //   c_rarg2   - K (key) in little endian int array
3422   //   c_rarg3   - r vector byte array address
3423   //   c_rarg4   - input length
3424   //
3425   // Output:
3426   //   rax       - input length
3427   //
3428 
3429   address generate_cipherBlockChaining_decryptAESCrypt_Parallel() {
3430     assert(UseAES, "need AES instructions and misaligned SSE support");
3431     __ align(CodeEntryAlignment);
3432     StubCodeMark mark(this, "StubRoutines", "cipherBlockChaining_decryptAESCrypt");
3433     address start = __ pc();
3434 
3435     Label L_exit, L_key_192_256, L_key_256;
3436     Label L_singleBlock_loopTop_128, L_multiBlock_loopTop_128;
3437     Label L_singleBlock_loopTop_192, L_singleBlock_loopTop_256;
3438     const Register from        = c_rarg0;  // source array address
3439     const Register to          = c_rarg1;  // destination array address
3440     const Register key         = c_rarg2;  // key array address
3441     const Register rvec        = c_rarg3;  // r byte array initialized from initvector array address
3442                                            // and left with the results of the last encryption block
3443 #ifndef _WIN64
3444     const Register len_reg     = c_rarg4;  // src len (must be multiple of blocksize 16)
3445 #else
3446     const Address  len_mem(rbp, 6 * wordSize);  // length is on stack on Win64
3447     const Register len_reg     = r10;      // pick the first volatile windows register
3448 #endif
3449     const Register pos         = rax;
3450 
3451     // keys 0-10 preloaded into xmm2-xmm12
3452     const int XMM_REG_NUM_KEY_FIRST = 5;
3453     const int XMM_REG_NUM_KEY_LAST  = 15;
3454     const XMMRegister xmm_key_first = as_XMMRegister(XMM_REG_NUM_KEY_FIRST);
3455     const XMMRegister xmm_key_last  = as_XMMRegister(XMM_REG_NUM_KEY_LAST);
3456 
3457     __ enter(); // required for proper stackwalking of RuntimeStub frame
3458 
3459 #ifdef _WIN64
3460     // on win64, fill len_reg from stack position
3461     __ movl(len_reg, len_mem);
3462     // save the xmm registers which must be preserved 6-15
3463     __ subptr(rsp, -rsp_after_call_off * wordSize);
3464     for (int i = 6; i <= XMM_REG_NUM_KEY_LAST; i++) {
3465       __ movdqu(xmm_save(i), as_XMMRegister(i));
3466     }
3467 #else
3468     __ push(len_reg); // Save
3469 #endif
3470 
3471     // the java expanded key ordering is rotated one position from what we want
3472     // so we start from 0x10 here and hit 0x00 last
3473     const XMMRegister xmm_key_shuf_mask = xmm1;  // used temporarily to swap key bytes up front
3474     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
3475     // load up xmm regs 5 thru 15 with key 0x10 - 0xa0 - 0x00
3476     for (int rnum = XMM_REG_NUM_KEY_FIRST, offset = 0x10; rnum < XMM_REG_NUM_KEY_LAST; rnum++) {
3477       load_key(as_XMMRegister(rnum), key, offset, xmm_key_shuf_mask);
3478       offset += 0x10;
3479     }
3480     load_key(xmm_key_last, key, 0x00, xmm_key_shuf_mask);
3481 
3482     const XMMRegister xmm_prev_block_cipher = xmm1;  // holds cipher of previous block
3483 
3484     // registers holding the four results in the parallelized loop
3485     const XMMRegister xmm_result0 = xmm0;
3486     const XMMRegister xmm_result1 = xmm2;
3487     const XMMRegister xmm_result2 = xmm3;
3488     const XMMRegister xmm_result3 = xmm4;
3489 
3490     __ movdqu(xmm_prev_block_cipher, Address(rvec, 0x00));   // initialize with initial rvec
3491 
3492     // now split to different paths depending on the keylen (len in ints of AESCrypt.KLE array (52=192, or 60=256))
3493     __ movl(rax, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
3494     __ cmpl(rax, 44);
3495     __ jcc(Assembler::notEqual, L_key_192_256);
3496 
3497 
3498     // 128-bit code follows here, parallelized
3499     __ movptr(pos, 0);
3500     __ align(OptoLoopAlignment);
3501     __ BIND(L_multiBlock_loopTop_128);
3502     __ cmpptr(len_reg, 4*AESBlockSize);           // see if at least 4 blocks left
3503     __ jcc(Assembler::less, L_singleBlock_loopTop_128);
3504 
3505     __ movdqu(xmm_result0, Address(from, pos, Address::times_1, 0*AESBlockSize));   // get next 4 blocks into xmmresult registers
3506     __ movdqu(xmm_result1, Address(from, pos, Address::times_1, 1*AESBlockSize));
3507     __ movdqu(xmm_result2, Address(from, pos, Address::times_1, 2*AESBlockSize));
3508     __ movdqu(xmm_result3, Address(from, pos, Address::times_1, 3*AESBlockSize));
3509 
3510 #define DoFour(opc, src_reg)                    \
3511     __ opc(xmm_result0, src_reg);               \
3512     __ opc(xmm_result1, src_reg);               \
3513     __ opc(xmm_result2, src_reg);               \
3514     __ opc(xmm_result3, src_reg);
3515 
3516     DoFour(pxor, xmm_key_first);
3517     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum  <= XMM_REG_NUM_KEY_LAST - 1; rnum++) {
3518       DoFour(aesdec, as_XMMRegister(rnum));
3519     }
3520     DoFour(aesdeclast, xmm_key_last);
3521     // for each result, xor with the r vector of previous cipher block
3522     __ pxor(xmm_result0, xmm_prev_block_cipher);
3523     __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 0*AESBlockSize));
3524     __ pxor(xmm_result1, xmm_prev_block_cipher);
3525     __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 1*AESBlockSize));
3526     __ pxor(xmm_result2, xmm_prev_block_cipher);
3527     __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 2*AESBlockSize));
3528     __ pxor(xmm_result3, xmm_prev_block_cipher);
3529     __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 3*AESBlockSize));   // this will carry over to next set of blocks
3530 
3531     __ movdqu(Address(to, pos, Address::times_1, 0*AESBlockSize), xmm_result0);     // store 4 results into the next 64 bytes of output
3532     __ movdqu(Address(to, pos, Address::times_1, 1*AESBlockSize), xmm_result1);
3533     __ movdqu(Address(to, pos, Address::times_1, 2*AESBlockSize), xmm_result2);
3534     __ movdqu(Address(to, pos, Address::times_1, 3*AESBlockSize), xmm_result3);
3535 
3536     __ addptr(pos, 4*AESBlockSize);
3537     __ subptr(len_reg, 4*AESBlockSize);
3538     __ jmp(L_multiBlock_loopTop_128);
3539 
3540     // registers used in the non-parallelized loops
3541     // xmm register assignments for the loops below
3542     const XMMRegister xmm_result = xmm0;
3543     const XMMRegister xmm_prev_block_cipher_save = xmm2;
3544     const XMMRegister xmm_key11 = xmm3;
3545     const XMMRegister xmm_key12 = xmm4;
3546     const XMMRegister xmm_temp  = xmm4;
3547 
3548     __ align(OptoLoopAlignment);
3549     __ BIND(L_singleBlock_loopTop_128);
3550     __ cmpptr(len_reg, 0);           // any blocks left??
3551     __ jcc(Assembler::equal, L_exit);
3552     __ movdqu(xmm_result, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of cipher input
3553     __ movdqa(xmm_prev_block_cipher_save, xmm_result);              // save for next r vector
3554     __ pxor  (xmm_result, xmm_key_first);               // do the aes dec rounds
3555     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum  <= XMM_REG_NUM_KEY_LAST - 1; rnum++) {
3556       __ aesdec(xmm_result, as_XMMRegister(rnum));
3557     }
3558     __ aesdeclast(xmm_result, xmm_key_last);
3559     __ pxor  (xmm_result, xmm_prev_block_cipher);               // xor with the current r vector
3560     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);     // store into the next 16 bytes of output
3561     // no need to store r to memory until we exit
3562     __ movdqa(xmm_prev_block_cipher, xmm_prev_block_cipher_save);              // set up next r vector with cipher input from this block
3563 
3564     __ addptr(pos, AESBlockSize);
3565     __ subptr(len_reg, AESBlockSize);
3566     __ jmp(L_singleBlock_loopTop_128);
3567 
3568 
3569     __ BIND(L_exit);
3570     __ movdqu(Address(rvec, 0), xmm_prev_block_cipher);     // final value of r stored in rvec of CipherBlockChaining object
3571 #ifdef _WIN64
3572     // restore regs belonging to calling function
3573     for (int i = 6; i <= XMM_REG_NUM_KEY_LAST; i++) {
3574       __ movdqu(as_XMMRegister(i), xmm_save(i));
3575     }
3576     __ movl(rax, len_mem);
3577 #else
3578     __ pop(rax); // return length
3579 #endif
3580     __ leave(); // required for proper stackwalking of RuntimeStub frame
3581     __ ret(0);
3582 
3583 
3584     __ BIND(L_key_192_256);
3585     // here rax = len in ints of AESCrypt.KLE array (52=192, or 60=256)
3586     load_key(xmm_key11, key, 0xb0);
3587     __ cmpl(rax, 52);
3588     __ jcc(Assembler::notEqual, L_key_256);
3589 
3590     // 192-bit code follows here (could be optimized to use parallelism)
3591     load_key(xmm_key12, key, 0xc0);     // 192-bit key goes up to c0
3592     __ movptr(pos, 0);
3593     __ align(OptoLoopAlignment);
3594 
3595     __ BIND(L_singleBlock_loopTop_192);
3596     __ movdqu(xmm_result, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of cipher input
3597     __ movdqa(xmm_prev_block_cipher_save, xmm_result);              // save for next r vector
3598     __ pxor  (xmm_result, xmm_key_first);               // do the aes dec rounds
3599     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_LAST - 1; rnum++) {
3600       __ aesdec(xmm_result, as_XMMRegister(rnum));
3601     }
3602     __ aesdec(xmm_result, xmm_key11);
3603     __ aesdec(xmm_result, xmm_key12);
3604     __ aesdeclast(xmm_result, xmm_key_last);                    // xmm15 always came from key+0
3605     __ pxor  (xmm_result, xmm_prev_block_cipher);               // xor with the current r vector
3606     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);  // store into the next 16 bytes of output
3607     // no need to store r to memory until we exit
3608     __ movdqa(xmm_prev_block_cipher, xmm_prev_block_cipher_save);  // set up next r vector with cipher input from this block
3609     __ addptr(pos, AESBlockSize);
3610     __ subptr(len_reg, AESBlockSize);
3611     __ jcc(Assembler::notEqual,L_singleBlock_loopTop_192);
3612     __ jmp(L_exit);
3613 
3614     __ BIND(L_key_256);
3615     // 256-bit code follows here (could be optimized to use parallelism)
3616     __ movptr(pos, 0);
3617     __ align(OptoLoopAlignment);
3618 
3619     __ BIND(L_singleBlock_loopTop_256);
3620     __ movdqu(xmm_result, Address(from, pos, Address::times_1, 0)); // get next 16 bytes of cipher input
3621     __ movdqa(xmm_prev_block_cipher_save, xmm_result);              // save for next r vector
3622     __ pxor  (xmm_result, xmm_key_first);               // do the aes dec rounds
3623     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_LAST - 1; rnum++) {
3624       __ aesdec(xmm_result, as_XMMRegister(rnum));
3625     }
3626     __ aesdec(xmm_result, xmm_key11);
3627     load_key(xmm_temp, key, 0xc0);
3628     __ aesdec(xmm_result, xmm_temp);
3629     load_key(xmm_temp, key, 0xd0);
3630     __ aesdec(xmm_result, xmm_temp);
3631     load_key(xmm_temp, key, 0xe0);     // 256-bit key goes up to e0
3632     __ aesdec(xmm_result, xmm_temp);
3633     __ aesdeclast(xmm_result, xmm_key_last);          // xmm15 came from key+0
3634     __ pxor  (xmm_result, xmm_prev_block_cipher);               // xor with the current r vector
3635     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);  // store into the next 16 bytes of output
3636     // no need to store r to memory until we exit
3637     __ movdqa(xmm_prev_block_cipher, xmm_prev_block_cipher_save);  // set up next r vector with cipher input from this block
3638     __ addptr(pos, AESBlockSize);
3639     __ subptr(len_reg, AESBlockSize);
3640     __ jcc(Assembler::notEqual,L_singleBlock_loopTop_256);
3641     __ jmp(L_exit);
3642 
3643     return start;
3644   }
3645 
3646   /**
3647    *  Arguments:
3648    *
3649    * Inputs:
3650    *   c_rarg0   - int crc
3651    *   c_rarg1   - byte* buf
3652    *   c_rarg2   - int length
3653    *
3654    * Ouput:
3655    *       rax   - int crc result
3656    */
3657   address generate_updateBytesCRC32() {
3658     assert(UseCRC32Intrinsics, "need AVX and CLMUL instructions");
3659 
3660     __ align(CodeEntryAlignment);
3661     StubCodeMark mark(this, "StubRoutines", "updateBytesCRC32");
3662 
3663     address start = __ pc();
3664     // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
3665     // Unix:  rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
3666     // rscratch1: r10
3667     const Register crc   = c_rarg0;  // crc
3668     const Register buf   = c_rarg1;  // source java byte array address
3669     const Register len   = c_rarg2;  // length
3670     const Register table = c_rarg3;  // crc_table address (reuse register)
3671     const Register tmp   = r11;
3672     assert_different_registers(crc, buf, len, table, tmp, rax);
3673 
3674     BLOCK_COMMENT("Entry:");
3675     __ enter(); // required for proper stackwalking of RuntimeStub frame
3676 
3677     __ kernel_crc32(crc, buf, len, table, tmp);
3678 
3679     __ movl(rax, crc);
3680     __ leave(); // required for proper stackwalking of RuntimeStub frame
3681     __ ret(0);
3682 
3683     return start;
3684   }
3685 
3686 
3687   /**
3688    *  Arguments:
3689    *
3690    *  Input:
3691    *    c_rarg0   - x address
3692    *    c_rarg1   - x length
3693    *    c_rarg2   - y address
3694    *    c_rarg3   - y lenth
3695    * not Win64
3696    *    c_rarg4   - z address
3697    *    c_rarg5   - z length
3698    * Win64
3699    *    rsp+40    - z address
3700    *    rsp+48    - z length
3701    */
3702   address generate_multiplyToLen() {
3703     __ align(CodeEntryAlignment);
3704     StubCodeMark mark(this, "StubRoutines", "multiplyToLen");
3705 
3706     address start = __ pc();
3707     // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
3708     // Unix:  rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
3709     const Register x     = rdi;
3710     const Register xlen  = rax;
3711     const Register y     = rsi;
3712     const Register ylen  = rcx;
3713     const Register z     = r8;
3714     const Register zlen  = r11;
3715 
3716     // Next registers will be saved on stack in multiply_to_len().
3717     const Register tmp1  = r12;
3718     const Register tmp2  = r13;
3719     const Register tmp3  = r14;
3720     const Register tmp4  = r15;
3721     const Register tmp5  = rbx;
3722 
3723     BLOCK_COMMENT("Entry:");
3724     __ enter(); // required for proper stackwalking of RuntimeStub frame
3725 
3726 #ifndef _WIN64
3727     __ movptr(zlen, r9); // Save r9 in r11 - zlen
3728 #endif
3729     setup_arg_regs(4); // x => rdi, xlen => rsi, y => rdx
3730                        // ylen => rcx, z => r8, zlen => r11
3731                        // r9 and r10 may be used to save non-volatile registers
3732 #ifdef _WIN64
3733     // last 2 arguments (#4, #5) are on stack on Win64
3734     __ movptr(z, Address(rsp, 6 * wordSize));
3735     __ movptr(zlen, Address(rsp, 7 * wordSize));
3736 #endif
3737 
3738     __ movptr(xlen, rsi);
3739     __ movptr(y,    rdx);
3740     __ multiply_to_len(x, xlen, y, ylen, z, zlen, tmp1, tmp2, tmp3, tmp4, tmp5);
3741 
3742     restore_arg_regs();
3743 
3744     __ leave(); // required for proper stackwalking of RuntimeStub frame
3745     __ ret(0);
3746 
3747     return start;
3748   }
3749 
3750 /**
3751    *  Arguments:
3752    *
3753   //  Input:
3754   //    c_rarg0   - x address
3755   //    c_rarg1   - x length
3756   //    c_rarg2   - z address
3757   //    c_rarg3   - z lenth
3758    *
3759    */
3760   address generate_squareToLen() {
3761 
3762     __ align(CodeEntryAlignment);
3763     StubCodeMark mark(this, "StubRoutines", "squareToLen");
3764 
3765     address start = __ pc();
3766     // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
3767     // Unix:  rdi, rsi, rdx, rcx (c_rarg0, c_rarg1, ...)
3768     const Register x      = rdi;
3769     const Register len    = rsi;
3770     const Register z      = r8;
3771     const Register zlen   = rcx;
3772 
3773    const Register tmp1      = r12;
3774    const Register tmp2      = r13;
3775    const Register tmp3      = r14;
3776    const Register tmp4      = r15;
3777    const Register tmp5      = rbx;
3778 
3779     BLOCK_COMMENT("Entry:");
3780     __ enter(); // required for proper stackwalking of RuntimeStub frame
3781 
3782        setup_arg_regs(4); // x => rdi, len => rsi, z => rdx
3783                           // zlen => rcx
3784                           // r9 and r10 may be used to save non-volatile registers
3785     __ movptr(r8, rdx);
3786     __ square_to_len(x, len, z, zlen, tmp1, tmp2, tmp3, tmp4, tmp5, rdx, rax);
3787 
3788     restore_arg_regs();
3789 
3790     __ leave(); // required for proper stackwalking of RuntimeStub frame
3791     __ ret(0);
3792 
3793     return start;
3794   }
3795 
3796    /**
3797    *  Arguments:
3798    *
3799    *  Input:
3800    *    c_rarg0   - out address
3801    *    c_rarg1   - in address
3802    *    c_rarg2   - offset
3803    *    c_rarg3   - len
3804    * not Win64
3805    *    c_rarg4   - k
3806    * Win64
3807    *    rsp+40    - k
3808    */
3809   address generate_mulAdd() {
3810     __ align(CodeEntryAlignment);
3811     StubCodeMark mark(this, "StubRoutines", "mulAdd");
3812 
3813     address start = __ pc();
3814     // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
3815     // Unix:  rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
3816     const Register out     = rdi;
3817     const Register in      = rsi;
3818     const Register offset  = r11;
3819     const Register len     = rcx;
3820     const Register k       = r8;
3821 
3822     // Next registers will be saved on stack in mul_add().
3823     const Register tmp1  = r12;
3824     const Register tmp2  = r13;
3825     const Register tmp3  = r14;
3826     const Register tmp4  = r15;
3827     const Register tmp5  = rbx;
3828 
3829     BLOCK_COMMENT("Entry:");
3830     __ enter(); // required for proper stackwalking of RuntimeStub frame
3831 
3832     setup_arg_regs(4); // out => rdi, in => rsi, offset => rdx
3833                        // len => rcx, k => r8
3834                        // r9 and r10 may be used to save non-volatile registers
3835 #ifdef _WIN64
3836     // last argument is on stack on Win64
3837     __ movl(k, Address(rsp, 6 * wordSize));
3838 #endif
3839     __ movptr(r11, rdx);  // move offset in rdx to offset(r11)
3840     __ mul_add(out, in, offset, len, k, tmp1, tmp2, tmp3, tmp4, tmp5, rdx, rax);
3841 
3842     restore_arg_regs();
3843 
3844     __ leave(); // required for proper stackwalking of RuntimeStub frame
3845     __ ret(0);
3846 
3847     return start;
3848   }
3849 
3850 
3851 #undef __
3852 #define __ masm->
3853 
3854   // Continuation point for throwing of implicit exceptions that are
3855   // not handled in the current activation. Fabricates an exception
3856   // oop and initiates normal exception dispatching in this
3857   // frame. Since we need to preserve callee-saved values (currently
3858   // only for C2, but done for C1 as well) we need a callee-saved oop
3859   // map and therefore have to make these stubs into RuntimeStubs
3860   // rather than BufferBlobs.  If the compiler needs all registers to
3861   // be preserved between the fault point and the exception handler
3862   // then it must assume responsibility for that in
3863   // AbstractCompiler::continuation_for_implicit_null_exception or
3864   // continuation_for_implicit_division_by_zero_exception. All other
3865   // implicit exceptions (e.g., NullPointerException or
3866   // AbstractMethodError on entry) are either at call sites or
3867   // otherwise assume that stack unwinding will be initiated, so
3868   // caller saved registers were assumed volatile in the compiler.
3869   address generate_throw_exception(const char* name,
3870                                    address runtime_entry,
3871                                    Register arg1 = noreg,
3872                                    Register arg2 = noreg) {
3873     // Information about frame layout at time of blocking runtime call.
3874     // Note that we only have to preserve callee-saved registers since
3875     // the compilers are responsible for supplying a continuation point
3876     // if they expect all registers to be preserved.
3877     enum layout {
3878       rbp_off = frame::arg_reg_save_area_bytes/BytesPerInt,
3879       rbp_off2,
3880       return_off,
3881       return_off2,
3882       framesize // inclusive of return address
3883     };
3884 
3885     int insts_size = 512;
3886     int locs_size  = 64;
3887 
3888     CodeBuffer code(name, insts_size, locs_size);
3889     OopMapSet* oop_maps  = new OopMapSet();
3890     MacroAssembler* masm = new MacroAssembler(&code);
3891 
3892     address start = __ pc();
3893 
3894     // This is an inlined and slightly modified version of call_VM
3895     // which has the ability to fetch the return PC out of
3896     // thread-local storage and also sets up last_Java_sp slightly
3897     // differently than the real call_VM
3898 
3899     __ enter(); // required for proper stackwalking of RuntimeStub frame
3900 
3901     assert(is_even(framesize/2), "sp not 16-byte aligned");
3902 
3903     // return address and rbp are already in place
3904     __ subptr(rsp, (framesize-4) << LogBytesPerInt); // prolog
3905 
3906     int frame_complete = __ pc() - start;
3907 
3908     // Set up last_Java_sp and last_Java_fp
3909     address the_pc = __ pc();
3910     __ set_last_Java_frame(rsp, rbp, the_pc);
3911     __ andptr(rsp, -(StackAlignmentInBytes));    // Align stack
3912 
3913     // Call runtime
3914     if (arg1 != noreg) {
3915       assert(arg2 != c_rarg1, "clobbered");
3916       __ movptr(c_rarg1, arg1);
3917     }
3918     if (arg2 != noreg) {
3919       __ movptr(c_rarg2, arg2);
3920     }
3921     __ movptr(c_rarg0, r15_thread);
3922     BLOCK_COMMENT("call runtime_entry");
3923     __ call(RuntimeAddress(runtime_entry));
3924 
3925     // Generate oop map
3926     OopMap* map = new OopMap(framesize, 0);
3927 
3928     oop_maps->add_gc_map(the_pc - start, map);
3929 
3930     __ reset_last_Java_frame(true);
3931 
3932     __ leave(); // required for proper stackwalking of RuntimeStub frame
3933 
3934     // check for pending exceptions
3935 #ifdef ASSERT
3936     Label L;
3937     __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()),
3938             (int32_t) NULL_WORD);
3939     __ jcc(Assembler::notEqual, L);
3940     __ should_not_reach_here();
3941     __ bind(L);
3942 #endif // ASSERT
3943     __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
3944 
3945 
3946     // codeBlob framesize is in words (not VMRegImpl::slot_size)
3947     RuntimeStub* stub =
3948       RuntimeStub::new_runtime_stub(name,
3949                                     &code,
3950                                     frame_complete,
3951                                     (framesize >> (LogBytesPerWord - LogBytesPerInt)),
3952                                     oop_maps, false);
3953     return stub->entry_point();
3954   }
3955 
3956   void create_control_words() {
3957     // Round to nearest, 53-bit mode, exceptions masked
3958     StubRoutines::_fpu_cntrl_wrd_std   = 0x027F;
3959     // Round to zero, 53-bit mode, exception mased
3960     StubRoutines::_fpu_cntrl_wrd_trunc = 0x0D7F;
3961     // Round to nearest, 24-bit mode, exceptions masked
3962     StubRoutines::_fpu_cntrl_wrd_24    = 0x007F;
3963     // Round to nearest, 64-bit mode, exceptions masked
3964     StubRoutines::_fpu_cntrl_wrd_64    = 0x037F;
3965     // Round to nearest, 64-bit mode, exceptions masked
3966     StubRoutines::_mxcsr_std           = 0x1F80;
3967     // Note: the following two constants are 80-bit values
3968     //       layout is critical for correct loading by FPU.
3969     // Bias for strict fp multiply/divide
3970     StubRoutines::_fpu_subnormal_bias1[0]= 0x00000000; // 2^(-15360) == 0x03ff 8000 0000 0000 0000
3971     StubRoutines::_fpu_subnormal_bias1[1]= 0x80000000;
3972     StubRoutines::_fpu_subnormal_bias1[2]= 0x03ff;
3973     // Un-Bias for strict fp multiply/divide
3974     StubRoutines::_fpu_subnormal_bias2[0]= 0x00000000; // 2^(+15360) == 0x7bff 8000 0000 0000 0000
3975     StubRoutines::_fpu_subnormal_bias2[1]= 0x80000000;
3976     StubRoutines::_fpu_subnormal_bias2[2]= 0x7bff;
3977   }
3978 
3979   // Initialization
3980   void generate_initial() {
3981     // Generates all stubs and initializes the entry points
3982 
3983     // This platform-specific settings are needed by generate_call_stub()
3984     create_control_words();
3985 
3986     // entry points that exist in all platforms Note: This is code
3987     // that could be shared among different platforms - however the
3988     // benefit seems to be smaller than the disadvantage of having a
3989     // much more complicated generator structure. See also comment in
3990     // stubRoutines.hpp.
3991 
3992     StubRoutines::_forward_exception_entry = generate_forward_exception();
3993 
3994     StubRoutines::_call_stub_entry =
3995       generate_call_stub(StubRoutines::_call_stub_return_address);
3996 
3997     // is referenced by megamorphic call
3998     StubRoutines::_catch_exception_entry = generate_catch_exception();
3999 
4000     // atomic calls
4001     StubRoutines::_atomic_xchg_entry         = generate_atomic_xchg();
4002     StubRoutines::_atomic_xchg_ptr_entry     = generate_atomic_xchg_ptr();
4003     StubRoutines::_atomic_cmpxchg_entry      = generate_atomic_cmpxchg();
4004     StubRoutines::_atomic_cmpxchg_long_entry = generate_atomic_cmpxchg_long();
4005     StubRoutines::_atomic_add_entry          = generate_atomic_add();
4006     StubRoutines::_atomic_add_ptr_entry      = generate_atomic_add_ptr();
4007     StubRoutines::_fence_entry               = generate_orderaccess_fence();
4008 
4009     StubRoutines::_handler_for_unsafe_access_entry =
4010       generate_handler_for_unsafe_access();
4011 
4012     // platform dependent
4013     StubRoutines::x86::_get_previous_fp_entry = generate_get_previous_fp();
4014     StubRoutines::x86::_get_previous_sp_entry = generate_get_previous_sp();
4015 
4016     StubRoutines::x86::_verify_mxcsr_entry    = generate_verify_mxcsr();
4017 
4018     // Build this early so it's available for the interpreter.
4019     StubRoutines::_throw_StackOverflowError_entry =
4020       generate_throw_exception("StackOverflowError throw_exception",
4021                                CAST_FROM_FN_PTR(address,
4022                                                 SharedRuntime::
4023                                                 throw_StackOverflowError));
4024     if (UseCRC32Intrinsics) {
4025       // set table address before stub generation which use it
4026       StubRoutines::_crc_table_adr = (address)StubRoutines::x86::_crc_table;
4027       StubRoutines::_updateBytesCRC32 = generate_updateBytesCRC32();
4028     }
4029   }
4030 
4031   void generate_all() {
4032     // Generates all stubs and initializes the entry points
4033 
4034     // These entry points require SharedInfo::stack0 to be set up in
4035     // non-core builds and need to be relocatable, so they each
4036     // fabricate a RuntimeStub internally.
4037     StubRoutines::_throw_AbstractMethodError_entry =
4038       generate_throw_exception("AbstractMethodError throw_exception",
4039                                CAST_FROM_FN_PTR(address,
4040                                                 SharedRuntime::
4041                                                 throw_AbstractMethodError));
4042 
4043     StubRoutines::_throw_IncompatibleClassChangeError_entry =
4044       generate_throw_exception("IncompatibleClassChangeError throw_exception",
4045                                CAST_FROM_FN_PTR(address,
4046                                                 SharedRuntime::
4047                                                 throw_IncompatibleClassChangeError));
4048 
4049     StubRoutines::_throw_NullPointerException_at_call_entry =
4050       generate_throw_exception("NullPointerException at call throw_exception",
4051                                CAST_FROM_FN_PTR(address,
4052                                                 SharedRuntime::
4053                                                 throw_NullPointerException_at_call));
4054 
4055     // entry points that are platform specific
4056     StubRoutines::x86::_f2i_fixup = generate_f2i_fixup();
4057     StubRoutines::x86::_f2l_fixup = generate_f2l_fixup();
4058     StubRoutines::x86::_d2i_fixup = generate_d2i_fixup();
4059     StubRoutines::x86::_d2l_fixup = generate_d2l_fixup();
4060 
4061     StubRoutines::x86::_float_sign_mask  = generate_fp_mask("float_sign_mask",  0x7FFFFFFF7FFFFFFF);
4062     StubRoutines::x86::_float_sign_flip  = generate_fp_mask("float_sign_flip",  0x8000000080000000);
4063     StubRoutines::x86::_double_sign_mask = generate_fp_mask("double_sign_mask", 0x7FFFFFFFFFFFFFFF);
4064     StubRoutines::x86::_double_sign_flip = generate_fp_mask("double_sign_flip", 0x8000000000000000);
4065 
4066     // support for verify_oop (must happen after universe_init)
4067     StubRoutines::_verify_oop_subroutine_entry = generate_verify_oop();
4068 
4069     // arraycopy stubs used by compilers
4070     generate_arraycopy_stubs();
4071 
4072     generate_math_stubs();
4073 
4074     // don't bother generating these AES intrinsic stubs unless global flag is set
4075     if (UseAESIntrinsics) {
4076       StubRoutines::x86::_key_shuffle_mask_addr = generate_key_shuffle_mask();  // needed by the others
4077 
4078       StubRoutines::_aescrypt_encryptBlock = generate_aescrypt_encryptBlock();
4079       StubRoutines::_aescrypt_decryptBlock = generate_aescrypt_decryptBlock();
4080       StubRoutines::_cipherBlockChaining_encryptAESCrypt = generate_cipherBlockChaining_encryptAESCrypt();
4081       StubRoutines::_cipherBlockChaining_decryptAESCrypt = generate_cipherBlockChaining_decryptAESCrypt_Parallel();
4082     }
4083 
4084     // Safefetch stubs.
4085     generate_safefetch("SafeFetch32", sizeof(int),     &StubRoutines::_safefetch32_entry,
4086                                                        &StubRoutines::_safefetch32_fault_pc,
4087                                                        &StubRoutines::_safefetch32_continuation_pc);
4088     generate_safefetch("SafeFetchN", sizeof(intptr_t), &StubRoutines::_safefetchN_entry,
4089                                                        &StubRoutines::_safefetchN_fault_pc,
4090                                                        &StubRoutines::_safefetchN_continuation_pc);
4091 #ifdef COMPILER2
4092     if (UseMultiplyToLenIntrinsic) {
4093       StubRoutines::_multiplyToLen = generate_multiplyToLen();
4094     }
4095     if (UseSquareToLenIntrinsic) {
4096       StubRoutines::_squareToLen = generate_squareToLen();
4097     }
4098     if (UseMulAddIntrinsic) {
4099       StubRoutines::_mulAdd = generate_mulAdd();
4100     }
4101 
4102 #ifndef _WINDOWS
4103     if (UseMontgomeryMultiplyIntrinsic) {
4104       StubRoutines::_montgomeryMultiply
4105         = CAST_FROM_FN_PTR(address, SharedRuntime::montgomery_multiply);
4106     }
4107     if (UseMontgomerySquareIntrinsic) {
4108       StubRoutines::_montgomerySquare
4109         = CAST_FROM_FN_PTR(address, SharedRuntime::montgomery_square);
4110     }
4111 #endif // WINDOWS
4112 #endif // COMPILER2
4113   }
4114 
4115  public:
4116   StubGenerator(CodeBuffer* code, bool all) : StubCodeGenerator(code) {
4117     if (all) {
4118       generate_all();
4119     } else {
4120       generate_initial();
4121     }
4122   }
4123 }; // end class declaration
4124 
4125 void StubGenerator_generate(CodeBuffer* code, bool all) {
4126   StubGenerator g(code, all);
4127 }