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