1 /*
   2  * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/macroAssembler.hpp"
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "ci/ciUtilities.hpp"
  29 #include "gc/shared/barrierSet.hpp"
  30 #include "gc/shared/barrierSetAssembler.hpp"
  31 #include "gc/shared/barrierSetNMethod.hpp"
  32 #include "interpreter/interpreter.hpp"
  33 #include "nativeInst_x86.hpp"
  34 #include "oops/instanceOop.hpp"
  35 #include "oops/method.hpp"
  36 #include "oops/objArrayKlass.hpp"
  37 #include "oops/oop.inline.hpp"
  38 #include "prims/methodHandles.hpp"
  39 #include "runtime/frame.inline.hpp"
  40 #include "runtime/handles.inline.hpp"
  41 #include "runtime/sharedRuntime.hpp"
  42 #include "runtime/stubCodeGenerator.hpp"
  43 #include "runtime/stubRoutines.hpp"
  44 #include "runtime/thread.inline.hpp"
  45 #ifdef COMPILER2
  46 #include "opto/runtime.hpp"
  47 #endif
  48 #if INCLUDE_ZGC
  49 #include "gc/z/zThreadLocalData.hpp"
  50 #endif
  51 
  52 // Declaration and definition of StubGenerator (no .hpp file).
  53 // For a more detailed description of the stub routine structure
  54 // see the comment in stubRoutines.hpp
  55 
  56 #define __ _masm->
  57 #define TIMES_OOP (UseCompressedOops ? Address::times_4 : Address::times_8)
  58 #define a__ ((Assembler*)_masm)->
  59 
  60 #ifdef PRODUCT
  61 #define BLOCK_COMMENT(str) /* nothing */
  62 #else
  63 #define BLOCK_COMMENT(str) __ block_comment(str)
  64 #endif
  65 
  66 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
  67 const int MXCSR_MASK = 0xFFC0;  // Mask out any pending exceptions
  68 
  69 // Stub Code definitions
  70 
  71 class StubGenerator: public StubCodeGenerator {
  72  private:
  73 
  74 #ifdef PRODUCT
  75 #define inc_counter_np(counter) ((void)0)
  76 #else
  77   void inc_counter_np_(int& counter) {
  78     // This can destroy rscratch1 if counter is far from the code cache
  79     __ incrementl(ExternalAddress((address)&counter));
  80   }
  81 #define inc_counter_np(counter) \
  82   BLOCK_COMMENT("inc_counter " #counter); \
  83   inc_counter_np_(counter);
  84 #endif
  85 
  86   // Call stubs are used to call Java from C
  87   //
  88   // Linux Arguments:
  89   //    c_rarg0:   call wrapper address                   address
  90   //    c_rarg1:   result                                 address
  91   //    c_rarg2:   result type                            BasicType
  92   //    c_rarg3:   method                                 Method*
  93   //    c_rarg4:   (interpreter) entry point              address
  94   //    c_rarg5:   parameters                             intptr_t*
  95   //    16(rbp): parameter size (in words)              int
  96   //    24(rbp): thread                                 Thread*
  97   //
  98   //     [ return_from_Java     ] <--- rsp
  99   //     [ argument word n      ]
 100   //      ...
 101   // -12 [ argument word 1      ]
 102   // -11 [ saved r15            ] <--- rsp_after_call
 103   // -10 [ saved r14            ]
 104   //  -9 [ saved r13            ]
 105   //  -8 [ saved r12            ]
 106   //  -7 [ saved rbx            ]
 107   //  -6 [ call wrapper         ]
 108   //  -5 [ result               ]
 109   //  -4 [ result type          ]
 110   //  -3 [ method               ]
 111   //  -2 [ entry point          ]
 112   //  -1 [ parameters           ]
 113   //   0 [ saved rbp            ] <--- rbp
 114   //   1 [ return address       ]
 115   //   2 [ parameter size       ]
 116   //   3 [ thread               ]
 117   //
 118   // Windows Arguments:
 119   //    c_rarg0:   call wrapper address                   address
 120   //    c_rarg1:   result                                 address
 121   //    c_rarg2:   result type                            BasicType
 122   //    c_rarg3:   method                                 Method*
 123   //    48(rbp): (interpreter) entry point              address
 124   //    56(rbp): parameters                             intptr_t*
 125   //    64(rbp): parameter size (in words)              int
 126   //    72(rbp): thread                                 Thread*
 127   //
 128   //     [ return_from_Java     ] <--- rsp
 129   //     [ argument word n      ]
 130   //      ...
 131   // -60 [ argument word 1      ]
 132   // -59 [ saved xmm31          ] <--- rsp after_call
 133   //     [ saved xmm16-xmm30    ] (EVEX enabled, else the space is blank)
 134   // -27 [ saved xmm15          ]
 135   //     [ saved xmm7-xmm14     ]
 136   //  -9 [ saved xmm6           ] (each xmm register takes 2 slots)
 137   //  -7 [ saved r15            ]
 138   //  -6 [ saved r14            ]
 139   //  -5 [ saved r13            ]
 140   //  -4 [ saved r12            ]
 141   //  -3 [ saved rdi            ]
 142   //  -2 [ saved rsi            ]
 143   //  -1 [ saved rbx            ]
 144   //   0 [ saved rbp            ] <--- rbp
 145   //   1 [ return address       ]
 146   //   2 [ call wrapper         ]
 147   //   3 [ result               ]
 148   //   4 [ result type          ]
 149   //   5 [ method               ]
 150   //   6 [ entry point          ]
 151   //   7 [ parameters           ]
 152   //   8 [ parameter size       ]
 153   //   9 [ thread               ]
 154   //
 155   //    Windows reserves the callers stack space for arguments 1-4.
 156   //    We spill c_rarg0-c_rarg3 to this space.
 157 
 158   // Call stub stack layout word offsets from rbp
 159   enum call_stub_layout {
 160 #ifdef _WIN64
 161     xmm_save_first     = 6,  // save from xmm6
 162     xmm_save_last      = 31, // to xmm31
 163     xmm_save_base      = -9,
 164     rsp_after_call_off = xmm_save_base - 2 * (xmm_save_last - xmm_save_first), // -27
 165     r15_off            = -7,
 166     r14_off            = -6,
 167     r13_off            = -5,
 168     r12_off            = -4,
 169     rdi_off            = -3,
 170     rsi_off            = -2,
 171     rbx_off            = -1,
 172     rbp_off            =  0,
 173     retaddr_off        =  1,
 174     call_wrapper_off   =  2,
 175     result_off         =  3,
 176     result_type_off    =  4,
 177     method_off         =  5,
 178     entry_point_off    =  6,
 179     parameters_off     =  7,
 180     parameter_size_off =  8,
 181     thread_off         =  9
 182 #else
 183     rsp_after_call_off = -12,
 184     mxcsr_off          = rsp_after_call_off,
 185     r15_off            = -11,
 186     r14_off            = -10,
 187     r13_off            = -9,
 188     r12_off            = -8,
 189     rbx_off            = -7,
 190     call_wrapper_off   = -6,
 191     result_off         = -5,
 192     result_type_off    = -4,
 193     method_off         = -3,
 194     entry_point_off    = -2,
 195     parameters_off     = -1,
 196     rbp_off            =  0,
 197     retaddr_off        =  1,
 198     parameter_size_off =  2,
 199     thread_off         =  3
 200 #endif
 201   };
 202 
 203 #ifdef _WIN64
 204   Address xmm_save(int reg) {
 205     assert(reg >= xmm_save_first && reg <= xmm_save_last, "XMM register number out of range");
 206     return Address(rbp, (xmm_save_base - (reg - xmm_save_first) * 2) * wordSize);
 207   }
 208 #endif
 209 
 210   address generate_call_stub(address& return_address) {
 211     assert((int)frame::entry_frame_after_call_words == -(int)rsp_after_call_off + 1 &&
 212            (int)frame::entry_frame_call_wrapper_offset == (int)call_wrapper_off,
 213            "adjust this code");
 214     StubCodeMark mark(this, "StubRoutines", "call_stub");
 215     address start = __ pc();
 216 
 217     // same as in generate_catch_exception()!
 218     const Address rsp_after_call(rbp, rsp_after_call_off * wordSize);
 219 
 220     const Address call_wrapper  (rbp, call_wrapper_off   * wordSize);
 221     const Address result        (rbp, result_off         * wordSize);
 222     const Address result_type   (rbp, result_type_off    * wordSize);
 223     const Address method        (rbp, method_off         * wordSize);
 224     const Address entry_point   (rbp, entry_point_off    * wordSize);
 225     const Address parameters    (rbp, parameters_off     * wordSize);
 226     const Address parameter_size(rbp, parameter_size_off * wordSize);
 227 
 228     // same as in generate_catch_exception()!
 229     const Address thread        (rbp, thread_off         * wordSize);
 230 
 231     const Address r15_save(rbp, r15_off * wordSize);
 232     const Address r14_save(rbp, r14_off * wordSize);
 233     const Address r13_save(rbp, r13_off * wordSize);
 234     const Address r12_save(rbp, r12_off * wordSize);
 235     const Address rbx_save(rbp, rbx_off * wordSize);
 236 
 237     // stub code
 238     __ enter();
 239     __ subptr(rsp, -rsp_after_call_off * wordSize);
 240 
 241     // save register parameters
 242 #ifndef _WIN64
 243     __ movptr(parameters,   c_rarg5); // parameters
 244     __ movptr(entry_point,  c_rarg4); // entry_point
 245 #endif
 246 
 247     __ movptr(method,       c_rarg3); // method
 248     __ movl(result_type,  c_rarg2);   // result type
 249     __ movptr(result,       c_rarg1); // result
 250     __ movptr(call_wrapper, c_rarg0); // call wrapper
 251 
 252     // save regs belonging to calling function
 253     __ movptr(rbx_save, rbx);
 254     __ movptr(r12_save, r12);
 255     __ movptr(r13_save, r13);
 256     __ movptr(r14_save, r14);
 257     __ movptr(r15_save, r15);
 258 
 259 #ifdef _WIN64
 260     int last_reg = 15;
 261     if (UseAVX > 2) {
 262       last_reg = 31;
 263     }
 264     if (VM_Version::supports_evex()) {
 265       for (int i = xmm_save_first; i <= last_reg; i++) {
 266         __ vextractf32x4(xmm_save(i), as_XMMRegister(i), 0);
 267       }
 268     } else {
 269       for (int i = xmm_save_first; i <= last_reg; i++) {
 270         __ movdqu(xmm_save(i), as_XMMRegister(i));
 271       }
 272     }
 273 
 274     const Address rdi_save(rbp, rdi_off * wordSize);
 275     const Address rsi_save(rbp, rsi_off * wordSize);
 276 
 277     __ movptr(rsi_save, rsi);
 278     __ movptr(rdi_save, rdi);
 279 #else
 280     const Address mxcsr_save(rbp, mxcsr_off * wordSize);
 281     {
 282       Label skip_ldmx;
 283       __ stmxcsr(mxcsr_save);
 284       __ movl(rax, mxcsr_save);
 285       __ andl(rax, MXCSR_MASK);    // Only check control and mask bits
 286       ExternalAddress mxcsr_std(StubRoutines::addr_mxcsr_std());
 287       __ cmp32(rax, mxcsr_std);
 288       __ jcc(Assembler::equal, skip_ldmx);
 289       __ ldmxcsr(mxcsr_std);
 290       __ bind(skip_ldmx);
 291     }
 292 #endif
 293 
 294     // Load up thread register
 295     __ movptr(r15_thread, thread);
 296     __ reinit_heapbase();
 297 
 298 #ifdef ASSERT
 299     // make sure we have no pending exceptions
 300     {
 301       Label L;
 302       __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 303       __ jcc(Assembler::equal, L);
 304       __ stop("StubRoutines::call_stub: entered with pending exception");
 305       __ bind(L);
 306     }
 307 #endif
 308 
 309     // pass parameters if any
 310     BLOCK_COMMENT("pass parameters if any");
 311     Label parameters_done;
 312     __ movl(c_rarg3, parameter_size);
 313     __ testl(c_rarg3, c_rarg3);
 314     __ jcc(Assembler::zero, parameters_done);
 315 
 316     Label loop;
 317     __ movptr(c_rarg2, parameters);       // parameter pointer
 318     __ movl(c_rarg1, c_rarg3);            // parameter counter is in c_rarg1
 319     __ BIND(loop);
 320     __ movptr(rax, Address(c_rarg2, 0));// get parameter
 321     __ addptr(c_rarg2, wordSize);       // advance to next parameter
 322     __ decrementl(c_rarg1);             // decrement counter
 323     __ push(rax);                       // pass parameter
 324     __ jcc(Assembler::notZero, loop);
 325 
 326     // call Java function
 327     __ BIND(parameters_done);
 328     __ movptr(rbx, method);             // get Method*
 329     __ movptr(c_rarg1, entry_point);    // get entry_point
 330     __ mov(r13, rsp);                   // set sender sp
 331     BLOCK_COMMENT("call Java function");
 332     __ call(c_rarg1);
 333 
 334     BLOCK_COMMENT("call_stub_return_address:");
 335     return_address = __ pc();
 336 
 337     // store result depending on type (everything that is not
 338     // T_OBJECT, T_VALUETYPE, T_LONG, T_FLOAT or T_DOUBLE is treated as T_INT)
 339     __ movptr(c_rarg0, result);
 340     Label is_long, is_float, is_double, exit;
 341     __ movl(c_rarg1, result_type);
 342     __ cmpl(c_rarg1, T_OBJECT);
 343     __ jcc(Assembler::equal, is_long);
 344     __ cmpl(c_rarg1, T_VALUETYPE);
 345     __ jcc(Assembler::equal, is_long);
 346     __ cmpl(c_rarg1, T_LONG);
 347     __ jcc(Assembler::equal, is_long);
 348     __ cmpl(c_rarg1, T_FLOAT);
 349     __ jcc(Assembler::equal, is_float);
 350     __ cmpl(c_rarg1, T_DOUBLE);
 351     __ jcc(Assembler::equal, is_double);
 352 
 353     // handle T_INT case
 354     __ movl(Address(c_rarg0, 0), rax);
 355 
 356     __ BIND(exit);
 357 
 358     // pop parameters
 359     __ lea(rsp, rsp_after_call);
 360 
 361 #ifdef ASSERT
 362     // verify that threads correspond
 363     {
 364      Label L1, L2, L3;
 365       __ cmpptr(r15_thread, thread);
 366       __ jcc(Assembler::equal, L1);
 367       __ stop("StubRoutines::call_stub: r15_thread is corrupted");
 368       __ bind(L1);
 369       __ get_thread(rbx);
 370       __ cmpptr(r15_thread, thread);
 371       __ jcc(Assembler::equal, L2);
 372       __ stop("StubRoutines::call_stub: r15_thread is modified by call");
 373       __ bind(L2);
 374       __ cmpptr(r15_thread, rbx);
 375       __ jcc(Assembler::equal, L3);
 376       __ stop("StubRoutines::call_stub: threads must correspond");
 377       __ bind(L3);
 378     }
 379 #endif
 380 
 381     // restore regs belonging to calling function
 382 #ifdef _WIN64
 383     // emit the restores for xmm regs
 384     if (VM_Version::supports_evex()) {
 385       for (int i = xmm_save_first; i <= last_reg; i++) {
 386         __ vinsertf32x4(as_XMMRegister(i), as_XMMRegister(i), xmm_save(i), 0);
 387       }
 388     } else {
 389       for (int i = xmm_save_first; i <= last_reg; i++) {
 390         __ movdqu(as_XMMRegister(i), xmm_save(i));
 391       }
 392     }
 393 #endif
 394     __ movptr(r15, r15_save);
 395     __ movptr(r14, r14_save);
 396     __ movptr(r13, r13_save);
 397     __ movptr(r12, r12_save);
 398     __ movptr(rbx, rbx_save);
 399 
 400 #ifdef _WIN64
 401     __ movptr(rdi, rdi_save);
 402     __ movptr(rsi, rsi_save);
 403 #else
 404     __ ldmxcsr(mxcsr_save);
 405 #endif
 406 
 407     // restore rsp
 408     __ addptr(rsp, -rsp_after_call_off * wordSize);
 409 
 410     // return
 411     __ vzeroupper();
 412     __ pop(rbp);
 413     __ ret(0);
 414 
 415     // handle return types different from T_INT
 416     __ BIND(is_long);
 417     __ movq(Address(c_rarg0, 0), rax);
 418     __ jmp(exit);
 419 
 420     __ BIND(is_float);
 421     __ movflt(Address(c_rarg0, 0), xmm0);
 422     __ jmp(exit);
 423 
 424     __ BIND(is_double);
 425     __ movdbl(Address(c_rarg0, 0), xmm0);
 426     __ jmp(exit);
 427 
 428     return start;
 429   }
 430 
 431   // Return point for a Java call if there's an exception thrown in
 432   // Java code.  The exception is caught and transformed into a
 433   // pending exception stored in JavaThread that can be tested from
 434   // within the VM.
 435   //
 436   // Note: Usually the parameters are removed by the callee. In case
 437   // of an exception crossing an activation frame boundary, that is
 438   // not the case if the callee is compiled code => need to setup the
 439   // rsp.
 440   //
 441   // rax: exception oop
 442 
 443   address generate_catch_exception() {
 444     StubCodeMark mark(this, "StubRoutines", "catch_exception");
 445     address start = __ pc();
 446 
 447     // same as in generate_call_stub():
 448     const Address rsp_after_call(rbp, rsp_after_call_off * wordSize);
 449     const Address thread        (rbp, thread_off         * wordSize);
 450 
 451 #ifdef ASSERT
 452     // verify that threads correspond
 453     {
 454       Label L1, L2, L3;
 455       __ cmpptr(r15_thread, thread);
 456       __ jcc(Assembler::equal, L1);
 457       __ stop("StubRoutines::catch_exception: r15_thread is corrupted");
 458       __ bind(L1);
 459       __ get_thread(rbx);
 460       __ cmpptr(r15_thread, thread);
 461       __ jcc(Assembler::equal, L2);
 462       __ stop("StubRoutines::catch_exception: r15_thread is modified by call");
 463       __ bind(L2);
 464       __ cmpptr(r15_thread, rbx);
 465       __ jcc(Assembler::equal, L3);
 466       __ stop("StubRoutines::catch_exception: threads must correspond");
 467       __ bind(L3);
 468     }
 469 #endif
 470 
 471     // set pending exception
 472     __ verify_oop(rax);
 473 
 474     __ movptr(Address(r15_thread, Thread::pending_exception_offset()), rax);
 475     __ lea(rscratch1, ExternalAddress((address)__FILE__));
 476     __ movptr(Address(r15_thread, Thread::exception_file_offset()), rscratch1);
 477     __ movl(Address(r15_thread, Thread::exception_line_offset()), (int)  __LINE__);
 478 
 479     // complete return to VM
 480     assert(StubRoutines::_call_stub_return_address != NULL,
 481            "_call_stub_return_address must have been generated before");
 482     __ jump(RuntimeAddress(StubRoutines::_call_stub_return_address));
 483 
 484     return start;
 485   }
 486 
 487   // Continuation point for runtime calls returning with a pending
 488   // exception.  The pending exception check happened in the runtime
 489   // or native call stub.  The pending exception in Thread is
 490   // converted into a Java-level exception.
 491   //
 492   // Contract with Java-level exception handlers:
 493   // rax: exception
 494   // rdx: throwing pc
 495   //
 496   // NOTE: At entry of this stub, exception-pc must be on stack !!
 497 
 498   address generate_forward_exception() {
 499     StubCodeMark mark(this, "StubRoutines", "forward exception");
 500     address start = __ pc();
 501 
 502     // Upon entry, the sp points to the return address returning into
 503     // Java (interpreted or compiled) code; i.e., the return address
 504     // becomes the throwing pc.
 505     //
 506     // Arguments pushed before the runtime call are still on the stack
 507     // but the exception handler will reset the stack pointer ->
 508     // ignore them.  A potential result in registers can be ignored as
 509     // well.
 510 
 511 #ifdef ASSERT
 512     // make sure this code is only executed if there is a pending exception
 513     {
 514       Label L;
 515       __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t) NULL);
 516       __ jcc(Assembler::notEqual, L);
 517       __ stop("StubRoutines::forward exception: no pending exception (1)");
 518       __ bind(L);
 519     }
 520 #endif
 521 
 522     // compute exception handler into rbx
 523     __ movptr(c_rarg0, Address(rsp, 0));
 524     BLOCK_COMMENT("call exception_handler_for_return_address");
 525     __ call_VM_leaf(CAST_FROM_FN_PTR(address,
 526                          SharedRuntime::exception_handler_for_return_address),
 527                     r15_thread, c_rarg0);
 528     __ mov(rbx, rax);
 529 
 530     // setup rax & rdx, remove return address & clear pending exception
 531     __ pop(rdx);
 532     __ movptr(rax, Address(r15_thread, Thread::pending_exception_offset()));
 533     __ movptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 534 
 535 #ifdef ASSERT
 536     // make sure exception is set
 537     {
 538       Label L;
 539       __ testptr(rax, rax);
 540       __ jcc(Assembler::notEqual, L);
 541       __ stop("StubRoutines::forward exception: no pending exception (2)");
 542       __ bind(L);
 543     }
 544 #endif
 545 
 546     // continue at exception handler (return address removed)
 547     // rax: exception
 548     // rbx: exception handler
 549     // rdx: throwing pc
 550     __ verify_oop(rax);
 551     __ jmp(rbx);
 552 
 553     return start;
 554   }
 555 
 556   // Support for jint atomic::xchg(jint exchange_value, volatile jint* dest)
 557   //
 558   // Arguments :
 559   //    c_rarg0: exchange_value
 560   //    c_rarg0: dest
 561   //
 562   // Result:
 563   //    *dest <- ex, return (orig *dest)
 564   address generate_atomic_xchg() {
 565     StubCodeMark mark(this, "StubRoutines", "atomic_xchg");
 566     address start = __ pc();
 567 
 568     __ movl(rax, c_rarg0); // Copy to eax we need a return value anyhow
 569     __ xchgl(rax, Address(c_rarg1, 0)); // automatic LOCK
 570     __ ret(0);
 571 
 572     return start;
 573   }
 574 
 575   // Support for intptr_t atomic::xchg_long(jlong exchange_value, volatile jlong* dest)
 576   //
 577   // Arguments :
 578   //    c_rarg0: exchange_value
 579   //    c_rarg1: dest
 580   //
 581   // Result:
 582   //    *dest <- ex, return (orig *dest)
 583   address generate_atomic_xchg_long() {
 584     StubCodeMark mark(this, "StubRoutines", "atomic_xchg_long");
 585     address start = __ pc();
 586 
 587     __ movptr(rax, c_rarg0); // Copy to eax we need a return value anyhow
 588     __ xchgptr(rax, Address(c_rarg1, 0)); // automatic LOCK
 589     __ ret(0);
 590 
 591     return start;
 592   }
 593 
 594   // Support for jint atomic::atomic_cmpxchg(jint exchange_value, volatile jint* dest,
 595   //                                         jint compare_value)
 596   //
 597   // Arguments :
 598   //    c_rarg0: exchange_value
 599   //    c_rarg1: dest
 600   //    c_rarg2: compare_value
 601   //
 602   // Result:
 603   //    if ( compare_value == *dest ) {
 604   //       *dest = exchange_value
 605   //       return compare_value;
 606   //    else
 607   //       return *dest;
 608   address generate_atomic_cmpxchg() {
 609     StubCodeMark mark(this, "StubRoutines", "atomic_cmpxchg");
 610     address start = __ pc();
 611 
 612     __ movl(rax, c_rarg2);
 613     __ lock();
 614     __ cmpxchgl(c_rarg0, Address(c_rarg1, 0));
 615     __ ret(0);
 616 
 617     return start;
 618   }
 619 
 620   // Support for int8_t atomic::atomic_cmpxchg(int8_t exchange_value, volatile int8_t* dest,
 621   //                                           int8_t compare_value)
 622   //
 623   // Arguments :
 624   //    c_rarg0: exchange_value
 625   //    c_rarg1: dest
 626   //    c_rarg2: compare_value
 627   //
 628   // Result:
 629   //    if ( compare_value == *dest ) {
 630   //       *dest = exchange_value
 631   //       return compare_value;
 632   //    else
 633   //       return *dest;
 634   address generate_atomic_cmpxchg_byte() {
 635     StubCodeMark mark(this, "StubRoutines", "atomic_cmpxchg_byte");
 636     address start = __ pc();
 637 
 638     __ movsbq(rax, c_rarg2);
 639     __ lock();
 640     __ cmpxchgb(c_rarg0, Address(c_rarg1, 0));
 641     __ ret(0);
 642 
 643     return start;
 644   }
 645 
 646   // Support for int64_t atomic::atomic_cmpxchg(int64_t exchange_value,
 647   //                                            volatile int64_t* dest,
 648   //                                            int64_t compare_value)
 649   // Arguments :
 650   //    c_rarg0: exchange_value
 651   //    c_rarg1: dest
 652   //    c_rarg2: compare_value
 653   //
 654   // Result:
 655   //    if ( compare_value == *dest ) {
 656   //       *dest = exchange_value
 657   //       return compare_value;
 658   //    else
 659   //       return *dest;
 660   address generate_atomic_cmpxchg_long() {
 661     StubCodeMark mark(this, "StubRoutines", "atomic_cmpxchg_long");
 662     address start = __ pc();
 663 
 664     __ movq(rax, c_rarg2);
 665     __ lock();
 666     __ cmpxchgq(c_rarg0, Address(c_rarg1, 0));
 667     __ ret(0);
 668 
 669     return start;
 670   }
 671 
 672   // Support for jint atomic::add(jint add_value, volatile jint* dest)
 673   //
 674   // Arguments :
 675   //    c_rarg0: add_value
 676   //    c_rarg1: dest
 677   //
 678   // Result:
 679   //    *dest += add_value
 680   //    return *dest;
 681   address generate_atomic_add() {
 682     StubCodeMark mark(this, "StubRoutines", "atomic_add");
 683     address start = __ pc();
 684 
 685     __ movl(rax, c_rarg0);
 686     __ lock();
 687     __ xaddl(Address(c_rarg1, 0), c_rarg0);
 688     __ addl(rax, c_rarg0);
 689     __ ret(0);
 690 
 691     return start;
 692   }
 693 
 694   // Support for intptr_t atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest)
 695   //
 696   // Arguments :
 697   //    c_rarg0: add_value
 698   //    c_rarg1: dest
 699   //
 700   // Result:
 701   //    *dest += add_value
 702   //    return *dest;
 703   address generate_atomic_add_long() {
 704     StubCodeMark mark(this, "StubRoutines", "atomic_add_long");
 705     address start = __ pc();
 706 
 707     __ movptr(rax, c_rarg0); // Copy to eax we need a return value anyhow
 708     __ lock();
 709     __ xaddptr(Address(c_rarg1, 0), c_rarg0);
 710     __ addptr(rax, c_rarg0);
 711     __ ret(0);
 712 
 713     return start;
 714   }
 715 
 716   // Support for intptr_t OrderAccess::fence()
 717   //
 718   // Arguments :
 719   //
 720   // Result:
 721   address generate_orderaccess_fence() {
 722     StubCodeMark mark(this, "StubRoutines", "orderaccess_fence");
 723     address start = __ pc();
 724     __ membar(Assembler::StoreLoad);
 725     __ ret(0);
 726 
 727     return start;
 728   }
 729 
 730   // Support for intptr_t get_previous_fp()
 731   //
 732   // This routine is used to find the previous frame pointer for the
 733   // caller (current_frame_guess). This is used as part of debugging
 734   // ps() is seemingly lost trying to find frames.
 735   // This code assumes that caller current_frame_guess) has a frame.
 736   address generate_get_previous_fp() {
 737     StubCodeMark mark(this, "StubRoutines", "get_previous_fp");
 738     const Address old_fp(rbp, 0);
 739     const Address older_fp(rax, 0);
 740     address start = __ pc();
 741 
 742     __ enter();
 743     __ movptr(rax, old_fp); // callers fp
 744     __ movptr(rax, older_fp); // the frame for ps()
 745     __ pop(rbp);
 746     __ ret(0);
 747 
 748     return start;
 749   }
 750 
 751   // Support for intptr_t get_previous_sp()
 752   //
 753   // This routine is used to find the previous stack pointer for the
 754   // caller.
 755   address generate_get_previous_sp() {
 756     StubCodeMark mark(this, "StubRoutines", "get_previous_sp");
 757     address start = __ pc();
 758 
 759     __ movptr(rax, rsp);
 760     __ addptr(rax, 8); // return address is at the top of the stack.
 761     __ ret(0);
 762 
 763     return start;
 764   }
 765 
 766   //----------------------------------------------------------------------------------------------------
 767   // Support for void verify_mxcsr()
 768   //
 769   // This routine is used with -Xcheck:jni to verify that native
 770   // JNI code does not return to Java code without restoring the
 771   // MXCSR register to our expected state.
 772 
 773   address generate_verify_mxcsr() {
 774     StubCodeMark mark(this, "StubRoutines", "verify_mxcsr");
 775     address start = __ pc();
 776 
 777     const Address mxcsr_save(rsp, 0);
 778 
 779     if (CheckJNICalls) {
 780       Label ok_ret;
 781       ExternalAddress mxcsr_std(StubRoutines::addr_mxcsr_std());
 782       __ push(rax);
 783       __ subptr(rsp, wordSize);      // allocate a temp location
 784       __ stmxcsr(mxcsr_save);
 785       __ movl(rax, mxcsr_save);
 786       __ andl(rax, MXCSR_MASK);    // Only check control and mask bits
 787       __ cmp32(rax, mxcsr_std);
 788       __ jcc(Assembler::equal, ok_ret);
 789 
 790       __ warn("MXCSR changed by native JNI code, use -XX:+RestoreMXCSROnJNICall");
 791 
 792       __ ldmxcsr(mxcsr_std);
 793 
 794       __ bind(ok_ret);
 795       __ addptr(rsp, wordSize);
 796       __ pop(rax);
 797     }
 798 
 799     __ ret(0);
 800 
 801     return start;
 802   }
 803 
 804   address generate_f2i_fixup() {
 805     StubCodeMark mark(this, "StubRoutines", "f2i_fixup");
 806     Address inout(rsp, 5 * wordSize); // return address + 4 saves
 807 
 808     address start = __ pc();
 809 
 810     Label L;
 811 
 812     __ push(rax);
 813     __ push(c_rarg3);
 814     __ push(c_rarg2);
 815     __ push(c_rarg1);
 816 
 817     __ movl(rax, 0x7f800000);
 818     __ xorl(c_rarg3, c_rarg3);
 819     __ movl(c_rarg2, inout);
 820     __ movl(c_rarg1, c_rarg2);
 821     __ andl(c_rarg1, 0x7fffffff);
 822     __ cmpl(rax, c_rarg1); // NaN? -> 0
 823     __ jcc(Assembler::negative, L);
 824     __ testl(c_rarg2, c_rarg2); // signed ? min_jint : max_jint
 825     __ movl(c_rarg3, 0x80000000);
 826     __ movl(rax, 0x7fffffff);
 827     __ cmovl(Assembler::positive, c_rarg3, rax);
 828 
 829     __ bind(L);
 830     __ movptr(inout, c_rarg3);
 831 
 832     __ pop(c_rarg1);
 833     __ pop(c_rarg2);
 834     __ pop(c_rarg3);
 835     __ pop(rax);
 836 
 837     __ ret(0);
 838 
 839     return start;
 840   }
 841 
 842   address generate_f2l_fixup() {
 843     StubCodeMark mark(this, "StubRoutines", "f2l_fixup");
 844     Address inout(rsp, 5 * wordSize); // return address + 4 saves
 845     address start = __ pc();
 846 
 847     Label L;
 848 
 849     __ push(rax);
 850     __ push(c_rarg3);
 851     __ push(c_rarg2);
 852     __ push(c_rarg1);
 853 
 854     __ movl(rax, 0x7f800000);
 855     __ xorl(c_rarg3, c_rarg3);
 856     __ movl(c_rarg2, inout);
 857     __ movl(c_rarg1, c_rarg2);
 858     __ andl(c_rarg1, 0x7fffffff);
 859     __ cmpl(rax, c_rarg1); // NaN? -> 0
 860     __ jcc(Assembler::negative, L);
 861     __ testl(c_rarg2, c_rarg2); // signed ? min_jlong : max_jlong
 862     __ mov64(c_rarg3, 0x8000000000000000);
 863     __ mov64(rax, 0x7fffffffffffffff);
 864     __ cmov(Assembler::positive, c_rarg3, rax);
 865 
 866     __ bind(L);
 867     __ movptr(inout, c_rarg3);
 868 
 869     __ pop(c_rarg1);
 870     __ pop(c_rarg2);
 871     __ pop(c_rarg3);
 872     __ pop(rax);
 873 
 874     __ ret(0);
 875 
 876     return start;
 877   }
 878 
 879   address generate_d2i_fixup() {
 880     StubCodeMark mark(this, "StubRoutines", "d2i_fixup");
 881     Address inout(rsp, 6 * wordSize); // return address + 5 saves
 882 
 883     address start = __ pc();
 884 
 885     Label L;
 886 
 887     __ push(rax);
 888     __ push(c_rarg3);
 889     __ push(c_rarg2);
 890     __ push(c_rarg1);
 891     __ push(c_rarg0);
 892 
 893     __ movl(rax, 0x7ff00000);
 894     __ movq(c_rarg2, inout);
 895     __ movl(c_rarg3, c_rarg2);
 896     __ mov(c_rarg1, c_rarg2);
 897     __ mov(c_rarg0, c_rarg2);
 898     __ negl(c_rarg3);
 899     __ shrptr(c_rarg1, 0x20);
 900     __ orl(c_rarg3, c_rarg2);
 901     __ andl(c_rarg1, 0x7fffffff);
 902     __ xorl(c_rarg2, c_rarg2);
 903     __ shrl(c_rarg3, 0x1f);
 904     __ orl(c_rarg1, c_rarg3);
 905     __ cmpl(rax, c_rarg1);
 906     __ jcc(Assembler::negative, L); // NaN -> 0
 907     __ testptr(c_rarg0, c_rarg0); // signed ? min_jint : max_jint
 908     __ movl(c_rarg2, 0x80000000);
 909     __ movl(rax, 0x7fffffff);
 910     __ cmov(Assembler::positive, c_rarg2, rax);
 911 
 912     __ bind(L);
 913     __ movptr(inout, c_rarg2);
 914 
 915     __ pop(c_rarg0);
 916     __ pop(c_rarg1);
 917     __ pop(c_rarg2);
 918     __ pop(c_rarg3);
 919     __ pop(rax);
 920 
 921     __ ret(0);
 922 
 923     return start;
 924   }
 925 
 926   address generate_d2l_fixup() {
 927     StubCodeMark mark(this, "StubRoutines", "d2l_fixup");
 928     Address inout(rsp, 6 * wordSize); // return address + 5 saves
 929 
 930     address start = __ pc();
 931 
 932     Label L;
 933 
 934     __ push(rax);
 935     __ push(c_rarg3);
 936     __ push(c_rarg2);
 937     __ push(c_rarg1);
 938     __ push(c_rarg0);
 939 
 940     __ movl(rax, 0x7ff00000);
 941     __ movq(c_rarg2, inout);
 942     __ movl(c_rarg3, c_rarg2);
 943     __ mov(c_rarg1, c_rarg2);
 944     __ mov(c_rarg0, c_rarg2);
 945     __ negl(c_rarg3);
 946     __ shrptr(c_rarg1, 0x20);
 947     __ orl(c_rarg3, c_rarg2);
 948     __ andl(c_rarg1, 0x7fffffff);
 949     __ xorl(c_rarg2, c_rarg2);
 950     __ shrl(c_rarg3, 0x1f);
 951     __ orl(c_rarg1, c_rarg3);
 952     __ cmpl(rax, c_rarg1);
 953     __ jcc(Assembler::negative, L); // NaN -> 0
 954     __ testq(c_rarg0, c_rarg0); // signed ? min_jlong : max_jlong
 955     __ mov64(c_rarg2, 0x8000000000000000);
 956     __ mov64(rax, 0x7fffffffffffffff);
 957     __ cmovq(Assembler::positive, c_rarg2, rax);
 958 
 959     __ bind(L);
 960     __ movq(inout, c_rarg2);
 961 
 962     __ pop(c_rarg0);
 963     __ pop(c_rarg1);
 964     __ pop(c_rarg2);
 965     __ pop(c_rarg3);
 966     __ pop(rax);
 967 
 968     __ ret(0);
 969 
 970     return start;
 971   }
 972 
 973   address generate_fp_mask(const char *stub_name, int64_t mask) {
 974     __ align(CodeEntryAlignment);
 975     StubCodeMark mark(this, "StubRoutines", stub_name);
 976     address start = __ pc();
 977 
 978     __ emit_data64( mask, relocInfo::none );
 979     __ emit_data64( mask, relocInfo::none );
 980 
 981     return start;
 982   }
 983 
 984   // Non-destructive plausibility checks for oops
 985   //
 986   // Arguments:
 987   //    all args on stack!
 988   //
 989   // Stack after saving c_rarg3:
 990   //    [tos + 0]: saved c_rarg3
 991   //    [tos + 1]: saved c_rarg2
 992   //    [tos + 2]: saved r12 (several TemplateTable methods use it)
 993   //    [tos + 3]: saved flags
 994   //    [tos + 4]: return address
 995   //  * [tos + 5]: error message (char*)
 996   //  * [tos + 6]: object to verify (oop)
 997   //  * [tos + 7]: saved rax - saved by caller and bashed
 998   //  * [tos + 8]: saved r10 (rscratch1) - saved by caller
 999   //  * = popped on exit
1000   address generate_verify_oop() {
1001     StubCodeMark mark(this, "StubRoutines", "verify_oop");
1002     address start = __ pc();
1003 
1004     Label exit, error, in_Java_heap;
1005 
1006     __ pushf();
1007     __ incrementl(ExternalAddress((address) StubRoutines::verify_oop_count_addr()));
1008 
1009     __ push(r12);
1010 
1011     // save c_rarg2 and c_rarg3
1012     __ push(c_rarg2);
1013     __ push(c_rarg3);
1014 
1015     enum {
1016            // After previous pushes.
1017            oop_to_verify = 6 * wordSize,
1018            saved_rax     = 7 * wordSize,
1019            saved_r10     = 8 * wordSize,
1020 
1021            // Before the call to MacroAssembler::debug(), see below.
1022            return_addr   = 16 * wordSize,
1023            error_msg     = 17 * wordSize
1024     };
1025 
1026     // get object
1027     __ movptr(rax, Address(rsp, oop_to_verify));
1028 
1029     // make sure object is 'reasonable'
1030     __ testptr(rax, rax);
1031     __ jcc(Assembler::zero, exit); // if obj is NULL it is OK
1032 
1033 #if INCLUDE_ZGC
1034     if (UseZGC) {
1035       // Check if metadata bits indicate a bad oop
1036       __ testptr(rax, Address(r15_thread, ZThreadLocalData::address_bad_mask_offset()));
1037       __ jcc(Assembler::notZero, error);
1038     }
1039 #endif
1040 
1041     // Check if the oop is in the right area of memory
1042     __ movptr(c_rarg2, rax);
1043     __ movptr(c_rarg3, (intptr_t) Universe::verify_oop_mask());
1044     __ andptr(c_rarg2, c_rarg3);
1045     __ movptr(c_rarg3, (intptr_t) Universe::verify_oop_bits());
1046     __ cmpptr(c_rarg2, c_rarg3);
1047     __ jcc(Assembler::zero, in_Java_heap);
1048     // Not in Java heap, but could be valid if it's a bufferable value type
1049     __ load_klass(c_rarg2, rax);
1050     __ movbool(c_rarg2, Address(c_rarg2, InstanceKlass::extra_flags_offset()));
1051     __ andptr(c_rarg2, InstanceKlass::_extra_is_bufferable);
1052     __ testbool(c_rarg2);
1053     __ jcc(Assembler::zero, error);
1054     __ bind(in_Java_heap);
1055 
1056     // set r12 to heapbase for load_klass()
1057     __ reinit_heapbase();
1058 
1059     // make sure klass is 'reasonable', which is not zero.
1060     __ load_klass(rax, rax);  // get klass
1061     __ testptr(rax, rax);
1062     __ jcc(Assembler::zero, error); // if klass is NULL it is broken
1063 
1064     // return if everything seems ok
1065     __ bind(exit);
1066     __ movptr(rax, Address(rsp, saved_rax));     // get saved rax back
1067     __ movptr(rscratch1, Address(rsp, saved_r10)); // get saved r10 back
1068     __ pop(c_rarg3);                             // restore c_rarg3
1069     __ pop(c_rarg2);                             // restore c_rarg2
1070     __ pop(r12);                                 // restore r12
1071     __ popf();                                   // restore flags
1072     __ ret(4 * wordSize);                        // pop caller saved stuff
1073 
1074     // handle errors
1075     __ bind(error);
1076     __ movptr(rax, Address(rsp, saved_rax));     // get saved rax back
1077     __ movptr(rscratch1, Address(rsp, saved_r10)); // get saved r10 back
1078     __ pop(c_rarg3);                             // get saved c_rarg3 back
1079     __ pop(c_rarg2);                             // get saved c_rarg2 back
1080     __ pop(r12);                                 // get saved r12 back
1081     __ popf();                                   // get saved flags off stack --
1082                                                  // will be ignored
1083 
1084     __ pusha();                                  // push registers
1085                                                  // (rip is already
1086                                                  // already pushed)
1087     // debug(char* msg, int64_t pc, int64_t regs[])
1088     // We've popped the registers we'd saved (c_rarg3, c_rarg2 and flags), and
1089     // pushed all the registers, so now the stack looks like:
1090     //     [tos +  0] 16 saved registers
1091     //     [tos + 16] return address
1092     //   * [tos + 17] error message (char*)
1093     //   * [tos + 18] object to verify (oop)
1094     //   * [tos + 19] saved rax - saved by caller and bashed
1095     //   * [tos + 20] saved r10 (rscratch1) - saved by caller
1096     //   * = popped on exit
1097 
1098     __ movptr(c_rarg0, Address(rsp, error_msg));    // pass address of error message
1099     __ movptr(c_rarg1, Address(rsp, return_addr));  // pass return address
1100     __ movq(c_rarg2, rsp);                          // pass address of regs on stack
1101     __ mov(r12, rsp);                               // remember rsp
1102     __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
1103     __ andptr(rsp, -16);                            // align stack as required by ABI
1104     BLOCK_COMMENT("call MacroAssembler::debug");
1105     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, MacroAssembler::debug64)));
1106     __ mov(rsp, r12);                               // restore rsp
1107     __ popa();                                      // pop registers (includes r12)
1108     __ ret(4 * wordSize);                           // pop caller saved stuff
1109 
1110     return start;
1111   }
1112 
1113   //
1114   // Verify that a register contains clean 32-bits positive value
1115   // (high 32-bits are 0) so it could be used in 64-bits shifts.
1116   //
1117   //  Input:
1118   //    Rint  -  32-bits value
1119   //    Rtmp  -  scratch
1120   //
1121   void assert_clean_int(Register Rint, Register Rtmp) {
1122 #ifdef ASSERT
1123     Label L;
1124     assert_different_registers(Rtmp, Rint);
1125     __ movslq(Rtmp, Rint);
1126     __ cmpq(Rtmp, Rint);
1127     __ jcc(Assembler::equal, L);
1128     __ stop("high 32-bits of int value are not 0");
1129     __ bind(L);
1130 #endif
1131   }
1132 
1133   //  Generate overlap test for array copy stubs
1134   //
1135   //  Input:
1136   //     c_rarg0 - from
1137   //     c_rarg1 - to
1138   //     c_rarg2 - element count
1139   //
1140   //  Output:
1141   //     rax   - &from[element count - 1]
1142   //
1143   void array_overlap_test(address no_overlap_target, Address::ScaleFactor sf) {
1144     assert(no_overlap_target != NULL, "must be generated");
1145     array_overlap_test(no_overlap_target, NULL, sf);
1146   }
1147   void array_overlap_test(Label& L_no_overlap, Address::ScaleFactor sf) {
1148     array_overlap_test(NULL, &L_no_overlap, sf);
1149   }
1150   void array_overlap_test(address no_overlap_target, Label* NOLp, Address::ScaleFactor sf) {
1151     const Register from     = c_rarg0;
1152     const Register to       = c_rarg1;
1153     const Register count    = c_rarg2;
1154     const Register end_from = rax;
1155 
1156     __ cmpptr(to, from);
1157     __ lea(end_from, Address(from, count, sf, 0));
1158     if (NOLp == NULL) {
1159       ExternalAddress no_overlap(no_overlap_target);
1160       __ jump_cc(Assembler::belowEqual, no_overlap);
1161       __ cmpptr(to, end_from);
1162       __ jump_cc(Assembler::aboveEqual, no_overlap);
1163     } else {
1164       __ jcc(Assembler::belowEqual, (*NOLp));
1165       __ cmpptr(to, end_from);
1166       __ jcc(Assembler::aboveEqual, (*NOLp));
1167     }
1168   }
1169 
1170   // Shuffle first three arg regs on Windows into Linux/Solaris locations.
1171   //
1172   // Outputs:
1173   //    rdi - rcx
1174   //    rsi - rdx
1175   //    rdx - r8
1176   //    rcx - r9
1177   //
1178   // Registers r9 and r10 are used to save rdi and rsi on Windows, which latter
1179   // are non-volatile.  r9 and r10 should not be used by the caller.
1180   //
1181   DEBUG_ONLY(bool regs_in_thread;)
1182 
1183   void setup_arg_regs(int nargs = 3) {
1184     const Register saved_rdi = r9;
1185     const Register saved_rsi = r10;
1186     assert(nargs == 3 || nargs == 4, "else fix");
1187 #ifdef _WIN64
1188     assert(c_rarg0 == rcx && c_rarg1 == rdx && c_rarg2 == r8 && c_rarg3 == r9,
1189            "unexpected argument registers");
1190     if (nargs >= 4)
1191       __ mov(rax, r9);  // r9 is also saved_rdi
1192     __ movptr(saved_rdi, rdi);
1193     __ movptr(saved_rsi, rsi);
1194     __ mov(rdi, rcx); // c_rarg0
1195     __ mov(rsi, rdx); // c_rarg1
1196     __ mov(rdx, r8);  // c_rarg2
1197     if (nargs >= 4)
1198       __ mov(rcx, rax); // c_rarg3 (via rax)
1199 #else
1200     assert(c_rarg0 == rdi && c_rarg1 == rsi && c_rarg2 == rdx && c_rarg3 == rcx,
1201            "unexpected argument registers");
1202 #endif
1203     DEBUG_ONLY(regs_in_thread = false;)
1204   }
1205 
1206   void restore_arg_regs() {
1207     assert(!regs_in_thread, "wrong call to restore_arg_regs");
1208     const Register saved_rdi = r9;
1209     const Register saved_rsi = r10;
1210 #ifdef _WIN64
1211     __ movptr(rdi, saved_rdi);
1212     __ movptr(rsi, saved_rsi);
1213 #endif
1214   }
1215 
1216   // This is used in places where r10 is a scratch register, and can
1217   // be adapted if r9 is needed also.
1218   void setup_arg_regs_using_thread() {
1219     const Register saved_r15 = r9;
1220 #ifdef _WIN64
1221     __ mov(saved_r15, r15);  // r15 is callee saved and needs to be restored
1222     __ get_thread(r15_thread);
1223     assert(c_rarg0 == rcx && c_rarg1 == rdx && c_rarg2 == r8 && c_rarg3 == r9,
1224            "unexpected argument registers");
1225     __ movptr(Address(r15_thread, in_bytes(JavaThread::windows_saved_rdi_offset())), rdi);
1226     __ movptr(Address(r15_thread, in_bytes(JavaThread::windows_saved_rsi_offset())), rsi);
1227 
1228     __ mov(rdi, rcx); // c_rarg0
1229     __ mov(rsi, rdx); // c_rarg1
1230     __ mov(rdx, r8);  // c_rarg2
1231 #else
1232     assert(c_rarg0 == rdi && c_rarg1 == rsi && c_rarg2 == rdx && c_rarg3 == rcx,
1233            "unexpected argument registers");
1234 #endif
1235     DEBUG_ONLY(regs_in_thread = true;)
1236   }
1237 
1238   void restore_arg_regs_using_thread() {
1239     assert(regs_in_thread, "wrong call to restore_arg_regs");
1240     const Register saved_r15 = r9;
1241 #ifdef _WIN64
1242     __ get_thread(r15_thread);
1243     __ movptr(rsi, Address(r15_thread, in_bytes(JavaThread::windows_saved_rsi_offset())));
1244     __ movptr(rdi, Address(r15_thread, in_bytes(JavaThread::windows_saved_rdi_offset())));
1245     __ mov(r15, saved_r15);  // r15 is callee saved and needs to be restored
1246 #endif
1247   }
1248 
1249   // Copy big chunks forward
1250   //
1251   // Inputs:
1252   //   end_from     - source arrays end address
1253   //   end_to       - destination array end address
1254   //   qword_count  - 64-bits element count, negative
1255   //   to           - scratch
1256   //   L_copy_bytes - entry label
1257   //   L_copy_8_bytes  - exit  label
1258   //
1259   void copy_bytes_forward(Register end_from, Register end_to,
1260                              Register qword_count, Register to,
1261                              Label& L_copy_bytes, Label& L_copy_8_bytes) {
1262     DEBUG_ONLY(__ stop("enter at entry label, not here"));
1263     Label L_loop;
1264     __ align(OptoLoopAlignment);
1265     if (UseUnalignedLoadStores) {
1266       Label L_end;
1267       // Copy 64-bytes per iteration
1268       __ BIND(L_loop);
1269       if (UseAVX > 2) {
1270         __ evmovdqul(xmm0, Address(end_from, qword_count, Address::times_8, -56), Assembler::AVX_512bit);
1271         __ evmovdqul(Address(end_to, qword_count, Address::times_8, -56), xmm0, Assembler::AVX_512bit);
1272       } else if (UseAVX == 2) {
1273         __ vmovdqu(xmm0, Address(end_from, qword_count, Address::times_8, -56));
1274         __ vmovdqu(Address(end_to, qword_count, Address::times_8, -56), xmm0);
1275         __ vmovdqu(xmm1, Address(end_from, qword_count, Address::times_8, -24));
1276         __ vmovdqu(Address(end_to, qword_count, Address::times_8, -24), xmm1);
1277       } else {
1278         __ movdqu(xmm0, Address(end_from, qword_count, Address::times_8, -56));
1279         __ movdqu(Address(end_to, qword_count, Address::times_8, -56), xmm0);
1280         __ movdqu(xmm1, Address(end_from, qword_count, Address::times_8, -40));
1281         __ movdqu(Address(end_to, qword_count, Address::times_8, -40), xmm1);
1282         __ movdqu(xmm2, Address(end_from, qword_count, Address::times_8, -24));
1283         __ movdqu(Address(end_to, qword_count, Address::times_8, -24), xmm2);
1284         __ movdqu(xmm3, Address(end_from, qword_count, Address::times_8, - 8));
1285         __ movdqu(Address(end_to, qword_count, Address::times_8, - 8), xmm3);
1286       }
1287       __ BIND(L_copy_bytes);
1288       __ addptr(qword_count, 8);
1289       __ jcc(Assembler::lessEqual, L_loop);
1290       __ subptr(qword_count, 4);  // sub(8) and add(4)
1291       __ jccb(Assembler::greater, L_end);
1292       // Copy trailing 32 bytes
1293       if (UseAVX >= 2) {
1294         __ vmovdqu(xmm0, Address(end_from, qword_count, Address::times_8, -24));
1295         __ vmovdqu(Address(end_to, qword_count, Address::times_8, -24), xmm0);
1296       } else {
1297         __ movdqu(xmm0, Address(end_from, qword_count, Address::times_8, -24));
1298         __ movdqu(Address(end_to, qword_count, Address::times_8, -24), xmm0);
1299         __ movdqu(xmm1, Address(end_from, qword_count, Address::times_8, - 8));
1300         __ movdqu(Address(end_to, qword_count, Address::times_8, - 8), xmm1);
1301       }
1302       __ addptr(qword_count, 4);
1303       __ BIND(L_end);
1304       if (UseAVX >= 2) {
1305         // clean upper bits of YMM registers
1306         __ vpxor(xmm0, xmm0);
1307         __ vpxor(xmm1, xmm1);
1308       }
1309     } else {
1310       // Copy 32-bytes per iteration
1311       __ BIND(L_loop);
1312       __ movq(to, Address(end_from, qword_count, Address::times_8, -24));
1313       __ movq(Address(end_to, qword_count, Address::times_8, -24), to);
1314       __ movq(to, Address(end_from, qword_count, Address::times_8, -16));
1315       __ movq(Address(end_to, qword_count, Address::times_8, -16), to);
1316       __ movq(to, Address(end_from, qword_count, Address::times_8, - 8));
1317       __ movq(Address(end_to, qword_count, Address::times_8, - 8), to);
1318       __ movq(to, Address(end_from, qword_count, Address::times_8, - 0));
1319       __ movq(Address(end_to, qword_count, Address::times_8, - 0), to);
1320 
1321       __ BIND(L_copy_bytes);
1322       __ addptr(qword_count, 4);
1323       __ jcc(Assembler::lessEqual, L_loop);
1324     }
1325     __ subptr(qword_count, 4);
1326     __ jcc(Assembler::less, L_copy_8_bytes); // Copy trailing qwords
1327   }
1328 
1329   // Copy big chunks backward
1330   //
1331   // Inputs:
1332   //   from         - source arrays address
1333   //   dest         - destination array address
1334   //   qword_count  - 64-bits element count
1335   //   to           - scratch
1336   //   L_copy_bytes - entry label
1337   //   L_copy_8_bytes  - exit  label
1338   //
1339   void copy_bytes_backward(Register from, Register dest,
1340                               Register qword_count, Register to,
1341                               Label& L_copy_bytes, Label& L_copy_8_bytes) {
1342     DEBUG_ONLY(__ stop("enter at entry label, not here"));
1343     Label L_loop;
1344     __ align(OptoLoopAlignment);
1345     if (UseUnalignedLoadStores) {
1346       Label L_end;
1347       // Copy 64-bytes per iteration
1348       __ BIND(L_loop);
1349       if (UseAVX > 2) {
1350         __ evmovdqul(xmm0, Address(from, qword_count, Address::times_8, 0), Assembler::AVX_512bit);
1351         __ evmovdqul(Address(dest, qword_count, Address::times_8, 0), xmm0, Assembler::AVX_512bit);
1352       } else if (UseAVX == 2) {
1353         __ vmovdqu(xmm0, Address(from, qword_count, Address::times_8, 32));
1354         __ vmovdqu(Address(dest, qword_count, Address::times_8, 32), xmm0);
1355         __ vmovdqu(xmm1, Address(from, qword_count, Address::times_8,  0));
1356         __ vmovdqu(Address(dest, qword_count, Address::times_8,  0), xmm1);
1357       } else {
1358         __ movdqu(xmm0, Address(from, qword_count, Address::times_8, 48));
1359         __ movdqu(Address(dest, qword_count, Address::times_8, 48), xmm0);
1360         __ movdqu(xmm1, Address(from, qword_count, Address::times_8, 32));
1361         __ movdqu(Address(dest, qword_count, Address::times_8, 32), xmm1);
1362         __ movdqu(xmm2, Address(from, qword_count, Address::times_8, 16));
1363         __ movdqu(Address(dest, qword_count, Address::times_8, 16), xmm2);
1364         __ movdqu(xmm3, Address(from, qword_count, Address::times_8,  0));
1365         __ movdqu(Address(dest, qword_count, Address::times_8,  0), xmm3);
1366       }
1367       __ BIND(L_copy_bytes);
1368       __ subptr(qword_count, 8);
1369       __ jcc(Assembler::greaterEqual, L_loop);
1370 
1371       __ addptr(qword_count, 4);  // add(8) and sub(4)
1372       __ jccb(Assembler::less, L_end);
1373       // Copy trailing 32 bytes
1374       if (UseAVX >= 2) {
1375         __ vmovdqu(xmm0, Address(from, qword_count, Address::times_8, 0));
1376         __ vmovdqu(Address(dest, qword_count, Address::times_8, 0), xmm0);
1377       } else {
1378         __ movdqu(xmm0, Address(from, qword_count, Address::times_8, 16));
1379         __ movdqu(Address(dest, qword_count, Address::times_8, 16), xmm0);
1380         __ movdqu(xmm1, Address(from, qword_count, Address::times_8,  0));
1381         __ movdqu(Address(dest, qword_count, Address::times_8,  0), xmm1);
1382       }
1383       __ subptr(qword_count, 4);
1384       __ BIND(L_end);
1385       if (UseAVX >= 2) {
1386         // clean upper bits of YMM registers
1387         __ vpxor(xmm0, xmm0);
1388         __ vpxor(xmm1, xmm1);
1389       }
1390     } else {
1391       // Copy 32-bytes per iteration
1392       __ BIND(L_loop);
1393       __ movq(to, Address(from, qword_count, Address::times_8, 24));
1394       __ movq(Address(dest, qword_count, Address::times_8, 24), to);
1395       __ movq(to, Address(from, qword_count, Address::times_8, 16));
1396       __ movq(Address(dest, qword_count, Address::times_8, 16), to);
1397       __ movq(to, Address(from, qword_count, Address::times_8,  8));
1398       __ movq(Address(dest, qword_count, Address::times_8,  8), to);
1399       __ movq(to, Address(from, qword_count, Address::times_8,  0));
1400       __ movq(Address(dest, qword_count, Address::times_8,  0), to);
1401 
1402       __ BIND(L_copy_bytes);
1403       __ subptr(qword_count, 4);
1404       __ jcc(Assembler::greaterEqual, L_loop);
1405     }
1406     __ addptr(qword_count, 4);
1407     __ jcc(Assembler::greater, L_copy_8_bytes); // Copy trailing qwords
1408   }
1409 
1410 
1411   // Arguments:
1412   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1413   //             ignored
1414   //   name    - stub name string
1415   //
1416   // Inputs:
1417   //   c_rarg0   - source array address
1418   //   c_rarg1   - destination array address
1419   //   c_rarg2   - element count, treated as ssize_t, can be zero
1420   //
1421   // If 'from' and/or 'to' are aligned on 4-, 2-, or 1-byte boundaries,
1422   // we let the hardware handle it.  The one to eight bytes within words,
1423   // dwords or qwords that span cache line boundaries will still be loaded
1424   // and stored atomically.
1425   //
1426   // Side Effects:
1427   //   disjoint_byte_copy_entry is set to the no-overlap entry point
1428   //   used by generate_conjoint_byte_copy().
1429   //
1430   address generate_disjoint_byte_copy(bool aligned, address* entry, const char *name) {
1431     __ align(CodeEntryAlignment);
1432     StubCodeMark mark(this, "StubRoutines", name);
1433     address start = __ pc();
1434 
1435     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes, L_copy_2_bytes;
1436     Label L_copy_byte, L_exit;
1437     const Register from        = rdi;  // source array address
1438     const Register to          = rsi;  // destination array address
1439     const Register count       = rdx;  // elements count
1440     const Register byte_count  = rcx;
1441     const Register qword_count = count;
1442     const Register end_from    = from; // source array end address
1443     const Register end_to      = to;   // destination array end address
1444     // End pointers are inclusive, and if count is not zero they point
1445     // to the last unit copied:  end_to[0] := end_from[0]
1446 
1447     __ enter(); // required for proper stackwalking of RuntimeStub frame
1448     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1449 
1450     if (entry != NULL) {
1451       *entry = __ pc();
1452        // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1453       BLOCK_COMMENT("Entry:");
1454     }
1455 
1456     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1457                       // r9 and r10 may be used to save non-volatile registers
1458 
1459     // 'from', 'to' and 'count' are now valid
1460     __ movptr(byte_count, count);
1461     __ shrptr(count, 3); // count => qword_count
1462 
1463     // Copy from low to high addresses.  Use 'to' as scratch.
1464     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1465     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1466     __ negptr(qword_count); // make the count negative
1467     __ jmp(L_copy_bytes);
1468 
1469     // Copy trailing qwords
1470   __ BIND(L_copy_8_bytes);
1471     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
1472     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
1473     __ increment(qword_count);
1474     __ jcc(Assembler::notZero, L_copy_8_bytes);
1475 
1476     // Check for and copy trailing dword
1477   __ BIND(L_copy_4_bytes);
1478     __ testl(byte_count, 4);
1479     __ jccb(Assembler::zero, L_copy_2_bytes);
1480     __ movl(rax, Address(end_from, 8));
1481     __ movl(Address(end_to, 8), rax);
1482 
1483     __ addptr(end_from, 4);
1484     __ addptr(end_to, 4);
1485 
1486     // Check for and copy trailing word
1487   __ BIND(L_copy_2_bytes);
1488     __ testl(byte_count, 2);
1489     __ jccb(Assembler::zero, L_copy_byte);
1490     __ movw(rax, Address(end_from, 8));
1491     __ movw(Address(end_to, 8), rax);
1492 
1493     __ addptr(end_from, 2);
1494     __ addptr(end_to, 2);
1495 
1496     // Check for and copy trailing byte
1497   __ BIND(L_copy_byte);
1498     __ testl(byte_count, 1);
1499     __ jccb(Assembler::zero, L_exit);
1500     __ movb(rax, Address(end_from, 8));
1501     __ movb(Address(end_to, 8), rax);
1502 
1503   __ BIND(L_exit);
1504     restore_arg_regs();
1505     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); // Update counter after rscratch1 is free
1506     __ xorptr(rax, rax); // return 0
1507     __ vzeroupper();
1508     __ leave(); // required for proper stackwalking of RuntimeStub frame
1509     __ ret(0);
1510 
1511     // Copy in multi-bytes chunks
1512     copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1513     __ jmp(L_copy_4_bytes);
1514 
1515     return start;
1516   }
1517 
1518   // Arguments:
1519   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1520   //             ignored
1521   //   name    - stub name string
1522   //
1523   // Inputs:
1524   //   c_rarg0   - source array address
1525   //   c_rarg1   - destination array address
1526   //   c_rarg2   - element count, treated as ssize_t, can be zero
1527   //
1528   // If 'from' and/or 'to' are aligned on 4-, 2-, or 1-byte boundaries,
1529   // we let the hardware handle it.  The one to eight bytes within words,
1530   // dwords or qwords that span cache line boundaries will still be loaded
1531   // and stored atomically.
1532   //
1533   address generate_conjoint_byte_copy(bool aligned, address nooverlap_target,
1534                                       address* entry, const char *name) {
1535     __ align(CodeEntryAlignment);
1536     StubCodeMark mark(this, "StubRoutines", name);
1537     address start = __ pc();
1538 
1539     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes, L_copy_2_bytes;
1540     const Register from        = rdi;  // source array address
1541     const Register to          = rsi;  // destination array address
1542     const Register count       = rdx;  // elements count
1543     const Register byte_count  = rcx;
1544     const Register qword_count = count;
1545 
1546     __ enter(); // required for proper stackwalking of RuntimeStub frame
1547     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1548 
1549     if (entry != NULL) {
1550       *entry = __ pc();
1551       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1552       BLOCK_COMMENT("Entry:");
1553     }
1554 
1555     array_overlap_test(nooverlap_target, Address::times_1);
1556     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1557                       // r9 and r10 may be used to save non-volatile registers
1558 
1559     // 'from', 'to' and 'count' are now valid
1560     __ movptr(byte_count, count);
1561     __ shrptr(count, 3);   // count => qword_count
1562 
1563     // Copy from high to low addresses.
1564 
1565     // Check for and copy trailing byte
1566     __ testl(byte_count, 1);
1567     __ jcc(Assembler::zero, L_copy_2_bytes);
1568     __ movb(rax, Address(from, byte_count, Address::times_1, -1));
1569     __ movb(Address(to, byte_count, Address::times_1, -1), rax);
1570     __ decrement(byte_count); // Adjust for possible trailing word
1571 
1572     // Check for and copy trailing word
1573   __ BIND(L_copy_2_bytes);
1574     __ testl(byte_count, 2);
1575     __ jcc(Assembler::zero, L_copy_4_bytes);
1576     __ movw(rax, Address(from, byte_count, Address::times_1, -2));
1577     __ movw(Address(to, byte_count, Address::times_1, -2), rax);
1578 
1579     // Check for and copy trailing dword
1580   __ BIND(L_copy_4_bytes);
1581     __ testl(byte_count, 4);
1582     __ jcc(Assembler::zero, L_copy_bytes);
1583     __ movl(rax, Address(from, qword_count, Address::times_8));
1584     __ movl(Address(to, qword_count, Address::times_8), rax);
1585     __ jmp(L_copy_bytes);
1586 
1587     // Copy trailing qwords
1588   __ BIND(L_copy_8_bytes);
1589     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
1590     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
1591     __ decrement(qword_count);
1592     __ jcc(Assembler::notZero, L_copy_8_bytes);
1593 
1594     restore_arg_regs();
1595     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); // Update counter after rscratch1 is free
1596     __ xorptr(rax, rax); // return 0
1597     __ vzeroupper();
1598     __ leave(); // required for proper stackwalking of RuntimeStub frame
1599     __ ret(0);
1600 
1601     // Copy in multi-bytes chunks
1602     copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1603 
1604     restore_arg_regs();
1605     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); // Update counter after rscratch1 is free
1606     __ xorptr(rax, rax); // return 0
1607     __ vzeroupper();
1608     __ leave(); // required for proper stackwalking of RuntimeStub frame
1609     __ ret(0);
1610 
1611     return start;
1612   }
1613 
1614   // Arguments:
1615   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1616   //             ignored
1617   //   name    - stub name string
1618   //
1619   // Inputs:
1620   //   c_rarg0   - source array address
1621   //   c_rarg1   - destination array address
1622   //   c_rarg2   - element count, treated as ssize_t, can be zero
1623   //
1624   // If 'from' and/or 'to' are aligned on 4- or 2-byte boundaries, we
1625   // let the hardware handle it.  The two or four words within dwords
1626   // or qwords that span cache line boundaries will still be loaded
1627   // and stored atomically.
1628   //
1629   // Side Effects:
1630   //   disjoint_short_copy_entry is set to the no-overlap entry point
1631   //   used by generate_conjoint_short_copy().
1632   //
1633   address generate_disjoint_short_copy(bool aligned, address *entry, const char *name) {
1634     __ align(CodeEntryAlignment);
1635     StubCodeMark mark(this, "StubRoutines", name);
1636     address start = __ pc();
1637 
1638     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes,L_copy_2_bytes,L_exit;
1639     const Register from        = rdi;  // source array address
1640     const Register to          = rsi;  // destination array address
1641     const Register count       = rdx;  // elements count
1642     const Register word_count  = rcx;
1643     const Register qword_count = count;
1644     const Register end_from    = from; // source array end address
1645     const Register end_to      = to;   // destination array end address
1646     // End pointers are inclusive, and if count is not zero they point
1647     // to the last unit copied:  end_to[0] := end_from[0]
1648 
1649     __ enter(); // required for proper stackwalking of RuntimeStub frame
1650     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1651 
1652     if (entry != NULL) {
1653       *entry = __ pc();
1654       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1655       BLOCK_COMMENT("Entry:");
1656     }
1657 
1658     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1659                       // r9 and r10 may be used to save non-volatile registers
1660 
1661     // 'from', 'to' and 'count' are now valid
1662     __ movptr(word_count, count);
1663     __ shrptr(count, 2); // count => qword_count
1664 
1665     // Copy from low to high addresses.  Use 'to' as scratch.
1666     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1667     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1668     __ negptr(qword_count);
1669     __ jmp(L_copy_bytes);
1670 
1671     // Copy trailing qwords
1672   __ BIND(L_copy_8_bytes);
1673     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
1674     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
1675     __ increment(qword_count);
1676     __ jcc(Assembler::notZero, L_copy_8_bytes);
1677 
1678     // Original 'dest' is trashed, so we can't use it as a
1679     // base register for a possible trailing word copy
1680 
1681     // Check for and copy trailing dword
1682   __ BIND(L_copy_4_bytes);
1683     __ testl(word_count, 2);
1684     __ jccb(Assembler::zero, L_copy_2_bytes);
1685     __ movl(rax, Address(end_from, 8));
1686     __ movl(Address(end_to, 8), rax);
1687 
1688     __ addptr(end_from, 4);
1689     __ addptr(end_to, 4);
1690 
1691     // Check for and copy trailing word
1692   __ BIND(L_copy_2_bytes);
1693     __ testl(word_count, 1);
1694     __ jccb(Assembler::zero, L_exit);
1695     __ movw(rax, Address(end_from, 8));
1696     __ movw(Address(end_to, 8), rax);
1697 
1698   __ BIND(L_exit);
1699     restore_arg_regs();
1700     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); // Update counter after rscratch1 is free
1701     __ xorptr(rax, rax); // return 0
1702     __ vzeroupper();
1703     __ leave(); // required for proper stackwalking of RuntimeStub frame
1704     __ ret(0);
1705 
1706     // Copy in multi-bytes chunks
1707     copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1708     __ jmp(L_copy_4_bytes);
1709 
1710     return start;
1711   }
1712 
1713   address generate_fill(BasicType t, bool aligned, const char *name) {
1714     __ align(CodeEntryAlignment);
1715     StubCodeMark mark(this, "StubRoutines", name);
1716     address start = __ pc();
1717 
1718     BLOCK_COMMENT("Entry:");
1719 
1720     const Register to       = c_rarg0;  // source array address
1721     const Register value    = c_rarg1;  // value
1722     const Register count    = c_rarg2;  // elements count
1723 
1724     __ enter(); // required for proper stackwalking of RuntimeStub frame
1725 
1726     __ generate_fill(t, aligned, to, value, count, rax, xmm0);
1727 
1728     __ vzeroupper();
1729     __ leave(); // required for proper stackwalking of RuntimeStub frame
1730     __ ret(0);
1731     return start;
1732   }
1733 
1734   // Arguments:
1735   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1736   //             ignored
1737   //   name    - stub name string
1738   //
1739   // Inputs:
1740   //   c_rarg0   - source array address
1741   //   c_rarg1   - destination array address
1742   //   c_rarg2   - element count, treated as ssize_t, can be zero
1743   //
1744   // If 'from' and/or 'to' are aligned on 4- or 2-byte boundaries, we
1745   // let the hardware handle it.  The two or four words within dwords
1746   // or qwords that span cache line boundaries will still be loaded
1747   // and stored atomically.
1748   //
1749   address generate_conjoint_short_copy(bool aligned, address nooverlap_target,
1750                                        address *entry, const char *name) {
1751     __ align(CodeEntryAlignment);
1752     StubCodeMark mark(this, "StubRoutines", name);
1753     address start = __ pc();
1754 
1755     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes;
1756     const Register from        = rdi;  // source array address
1757     const Register to          = rsi;  // destination array address
1758     const Register count       = rdx;  // elements count
1759     const Register word_count  = rcx;
1760     const Register qword_count = count;
1761 
1762     __ enter(); // required for proper stackwalking of RuntimeStub frame
1763     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1764 
1765     if (entry != NULL) {
1766       *entry = __ pc();
1767       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1768       BLOCK_COMMENT("Entry:");
1769     }
1770 
1771     array_overlap_test(nooverlap_target, Address::times_2);
1772     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1773                       // r9 and r10 may be used to save non-volatile registers
1774 
1775     // 'from', 'to' and 'count' are now valid
1776     __ movptr(word_count, count);
1777     __ shrptr(count, 2); // count => qword_count
1778 
1779     // Copy from high to low addresses.  Use 'to' as scratch.
1780 
1781     // Check for and copy trailing word
1782     __ testl(word_count, 1);
1783     __ jccb(Assembler::zero, L_copy_4_bytes);
1784     __ movw(rax, Address(from, word_count, Address::times_2, -2));
1785     __ movw(Address(to, word_count, Address::times_2, -2), rax);
1786 
1787     // Check for and copy trailing dword
1788   __ BIND(L_copy_4_bytes);
1789     __ testl(word_count, 2);
1790     __ jcc(Assembler::zero, L_copy_bytes);
1791     __ movl(rax, Address(from, qword_count, Address::times_8));
1792     __ movl(Address(to, qword_count, Address::times_8), rax);
1793     __ jmp(L_copy_bytes);
1794 
1795     // Copy trailing qwords
1796   __ BIND(L_copy_8_bytes);
1797     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
1798     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
1799     __ decrement(qword_count);
1800     __ jcc(Assembler::notZero, L_copy_8_bytes);
1801 
1802     restore_arg_regs();
1803     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); // Update counter after rscratch1 is free
1804     __ xorptr(rax, rax); // return 0
1805     __ vzeroupper();
1806     __ leave(); // required for proper stackwalking of RuntimeStub frame
1807     __ ret(0);
1808 
1809     // Copy in multi-bytes chunks
1810     copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1811 
1812     restore_arg_regs();
1813     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); // Update counter after rscratch1 is free
1814     __ xorptr(rax, rax); // return 0
1815     __ vzeroupper();
1816     __ leave(); // required for proper stackwalking of RuntimeStub frame
1817     __ ret(0);
1818 
1819     return start;
1820   }
1821 
1822   // Arguments:
1823   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1824   //             ignored
1825   //   is_oop  - true => oop array, so generate store check code
1826   //   name    - stub name string
1827   //
1828   // Inputs:
1829   //   c_rarg0   - source array address
1830   //   c_rarg1   - destination array address
1831   //   c_rarg2   - element count, treated as ssize_t, can be zero
1832   //
1833   // If 'from' and/or 'to' are aligned on 4-byte boundaries, we let
1834   // the hardware handle it.  The two dwords within qwords that span
1835   // cache line boundaries will still be loaded and stored atomicly.
1836   //
1837   // Side Effects:
1838   //   disjoint_int_copy_entry is set to the no-overlap entry point
1839   //   used by generate_conjoint_int_oop_copy().
1840   //
1841   address generate_disjoint_int_oop_copy(bool aligned, bool is_oop, address* entry,
1842                                          const char *name, bool dest_uninitialized = false) {
1843     __ align(CodeEntryAlignment);
1844     StubCodeMark mark(this, "StubRoutines", name);
1845     address start = __ pc();
1846 
1847     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes, L_exit;
1848     const Register from        = rdi;  // source array address
1849     const Register to          = rsi;  // destination array address
1850     const Register count       = rdx;  // elements count
1851     const Register dword_count = rcx;
1852     const Register qword_count = count;
1853     const Register end_from    = from; // source array end address
1854     const Register end_to      = to;   // destination array end address
1855     // End pointers are inclusive, and if count is not zero they point
1856     // to the last unit copied:  end_to[0] := end_from[0]
1857 
1858     __ enter(); // required for proper stackwalking of RuntimeStub frame
1859     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1860 
1861     if (entry != NULL) {
1862       *entry = __ pc();
1863       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1864       BLOCK_COMMENT("Entry:");
1865     }
1866 
1867     setup_arg_regs_using_thread(); // from => rdi, to => rsi, count => rdx
1868                                    // r9 is used to save r15_thread
1869 
1870     DecoratorSet decorators = IN_HEAP | IS_ARRAY | ARRAYCOPY_DISJOINT;
1871     if (dest_uninitialized) {
1872       decorators |= IS_DEST_UNINITIALIZED;
1873     }
1874     if (aligned) {
1875       decorators |= ARRAYCOPY_ALIGNED;
1876     }
1877 
1878     BasicType type = is_oop ? T_OBJECT : T_INT;
1879     BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
1880     bs->arraycopy_prologue(_masm, decorators, type, from, to, count);
1881 
1882     // 'from', 'to' and 'count' are now valid
1883     __ movptr(dword_count, count);
1884     __ shrptr(count, 1); // count => qword_count
1885 
1886     // Copy from low to high addresses.  Use 'to' as scratch.
1887     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1888     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1889     __ negptr(qword_count);
1890     __ jmp(L_copy_bytes);
1891 
1892     // Copy trailing qwords
1893   __ BIND(L_copy_8_bytes);
1894     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
1895     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
1896     __ increment(qword_count);
1897     __ jcc(Assembler::notZero, L_copy_8_bytes);
1898 
1899     // Check for and copy trailing dword
1900   __ BIND(L_copy_4_bytes);
1901     __ testl(dword_count, 1); // Only byte test since the value is 0 or 1
1902     __ jccb(Assembler::zero, L_exit);
1903     __ movl(rax, Address(end_from, 8));
1904     __ movl(Address(end_to, 8), rax);
1905 
1906   __ BIND(L_exit);
1907     bs->arraycopy_epilogue(_masm, decorators, type, from, to, dword_count);
1908     restore_arg_regs_using_thread();
1909     inc_counter_np(SharedRuntime::_jint_array_copy_ctr); // Update counter after rscratch1 is free
1910     __ vzeroupper();
1911     __ xorptr(rax, rax); // return 0
1912     __ leave(); // required for proper stackwalking of RuntimeStub frame
1913     __ ret(0);
1914 
1915     // Copy in multi-bytes chunks
1916     copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1917     __ jmp(L_copy_4_bytes);
1918 
1919     return start;
1920   }
1921 
1922   // Arguments:
1923   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1924   //             ignored
1925   //   is_oop  - true => oop array, so generate store check code
1926   //   name    - stub name string
1927   //
1928   // Inputs:
1929   //   c_rarg0   - source array address
1930   //   c_rarg1   - destination array address
1931   //   c_rarg2   - element count, treated as ssize_t, can be zero
1932   //
1933   // If 'from' and/or 'to' are aligned on 4-byte boundaries, we let
1934   // the hardware handle it.  The two dwords within qwords that span
1935   // cache line boundaries will still be loaded and stored atomicly.
1936   //
1937   address generate_conjoint_int_oop_copy(bool aligned, bool is_oop, address nooverlap_target,
1938                                          address *entry, const char *name,
1939                                          bool dest_uninitialized = false) {
1940     __ align(CodeEntryAlignment);
1941     StubCodeMark mark(this, "StubRoutines", name);
1942     address start = __ pc();
1943 
1944     Label L_copy_bytes, L_copy_8_bytes, L_exit;
1945     const Register from        = rdi;  // source array address
1946     const Register to          = rsi;  // destination array address
1947     const Register count       = rdx;  // elements count
1948     const Register dword_count = rcx;
1949     const Register qword_count = count;
1950 
1951     __ enter(); // required for proper stackwalking of RuntimeStub frame
1952     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1953 
1954     if (entry != NULL) {
1955       *entry = __ pc();
1956        // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1957       BLOCK_COMMENT("Entry:");
1958     }
1959 
1960     array_overlap_test(nooverlap_target, Address::times_4);
1961     setup_arg_regs_using_thread(); // from => rdi, to => rsi, count => rdx
1962                                    // r9 is used to save r15_thread
1963 
1964     DecoratorSet decorators = IN_HEAP | IS_ARRAY;
1965     if (dest_uninitialized) {
1966       decorators |= IS_DEST_UNINITIALIZED;
1967     }
1968     if (aligned) {
1969       decorators |= ARRAYCOPY_ALIGNED;
1970     }
1971 
1972     BasicType type = is_oop ? T_OBJECT : T_INT;
1973     BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
1974     // no registers are destroyed by this call
1975     bs->arraycopy_prologue(_masm, decorators, type, from, to, count);
1976 
1977     assert_clean_int(count, rax); // Make sure 'count' is clean int.
1978     // 'from', 'to' and 'count' are now valid
1979     __ movptr(dword_count, count);
1980     __ shrptr(count, 1); // count => qword_count
1981 
1982     // Copy from high to low addresses.  Use 'to' as scratch.
1983 
1984     // Check for and copy trailing dword
1985     __ testl(dword_count, 1);
1986     __ jcc(Assembler::zero, L_copy_bytes);
1987     __ movl(rax, Address(from, dword_count, Address::times_4, -4));
1988     __ movl(Address(to, dword_count, Address::times_4, -4), rax);
1989     __ jmp(L_copy_bytes);
1990 
1991     // Copy trailing qwords
1992   __ BIND(L_copy_8_bytes);
1993     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
1994     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
1995     __ decrement(qword_count);
1996     __ jcc(Assembler::notZero, L_copy_8_bytes);
1997 
1998     if (is_oop) {
1999       __ jmp(L_exit);
2000     }
2001     restore_arg_regs_using_thread();
2002     inc_counter_np(SharedRuntime::_jint_array_copy_ctr); // Update counter after rscratch1 is free
2003     __ xorptr(rax, rax); // return 0
2004     __ vzeroupper();
2005     __ leave(); // required for proper stackwalking of RuntimeStub frame
2006     __ ret(0);
2007 
2008     // Copy in multi-bytes chunks
2009     copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
2010 
2011   __ BIND(L_exit);
2012     bs->arraycopy_epilogue(_masm, decorators, type, from, to, dword_count);
2013     restore_arg_regs_using_thread();
2014     inc_counter_np(SharedRuntime::_jint_array_copy_ctr); // Update counter after rscratch1 is free
2015     __ xorptr(rax, rax); // return 0
2016     __ vzeroupper();
2017     __ leave(); // required for proper stackwalking of RuntimeStub frame
2018     __ ret(0);
2019 
2020     return start;
2021   }
2022 
2023   // Arguments:
2024   //   aligned - true => Input and output aligned on a HeapWord boundary == 8 bytes
2025   //             ignored
2026   //   is_oop  - true => oop array, so generate store check code
2027   //   name    - stub name string
2028   //
2029   // Inputs:
2030   //   c_rarg0   - source array address
2031   //   c_rarg1   - destination array address
2032   //   c_rarg2   - element count, treated as ssize_t, can be zero
2033   //
2034  // Side Effects:
2035   //   disjoint_oop_copy_entry or disjoint_long_copy_entry is set to the
2036   //   no-overlap entry point used by generate_conjoint_long_oop_copy().
2037   //
2038   address generate_disjoint_long_oop_copy(bool aligned, bool is_oop, address *entry,
2039                                           const char *name, bool dest_uninitialized = false) {
2040     __ align(CodeEntryAlignment);
2041     StubCodeMark mark(this, "StubRoutines", name);
2042     address start = __ pc();
2043 
2044     Label L_copy_bytes, L_copy_8_bytes, L_exit;
2045     const Register from        = rdi;  // source array address
2046     const Register to          = rsi;  // destination array address
2047     const Register qword_count = rdx;  // elements count
2048     const Register end_from    = from; // source array end address
2049     const Register end_to      = rcx;  // destination array end address
2050     const Register saved_count = r11;
2051     // End pointers are inclusive, and if count is not zero they point
2052     // to the last unit copied:  end_to[0] := end_from[0]
2053 
2054     __ enter(); // required for proper stackwalking of RuntimeStub frame
2055     // Save no-overlap entry point for generate_conjoint_long_oop_copy()
2056     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
2057 
2058     if (entry != NULL) {
2059       *entry = __ pc();
2060       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
2061       BLOCK_COMMENT("Entry:");
2062     }
2063 
2064     setup_arg_regs_using_thread(); // from => rdi, to => rsi, count => rdx
2065                                      // r9 is used to save r15_thread
2066     // 'from', 'to' and 'qword_count' are now valid
2067 
2068     DecoratorSet decorators = IN_HEAP | IS_ARRAY | ARRAYCOPY_DISJOINT;
2069     if (dest_uninitialized) {
2070       decorators |= IS_DEST_UNINITIALIZED;
2071     }
2072     if (aligned) {
2073       decorators |= ARRAYCOPY_ALIGNED;
2074     }
2075 
2076     BasicType type = is_oop ? T_OBJECT : T_LONG;
2077     BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
2078     bs->arraycopy_prologue(_masm, decorators, type, from, to, qword_count);
2079 
2080     // Copy from low to high addresses.  Use 'to' as scratch.
2081     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
2082     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
2083     __ negptr(qword_count);
2084     __ jmp(L_copy_bytes);
2085 
2086     // Copy trailing qwords
2087   __ BIND(L_copy_8_bytes);
2088     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
2089     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
2090     __ increment(qword_count);
2091     __ jcc(Assembler::notZero, L_copy_8_bytes);
2092 
2093     if (is_oop) {
2094       __ jmp(L_exit);
2095     } else {
2096       restore_arg_regs_using_thread();
2097       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
2098       __ xorptr(rax, rax); // return 0
2099       __ vzeroupper();
2100       __ leave(); // required for proper stackwalking of RuntimeStub frame
2101       __ ret(0);
2102     }
2103 
2104     // Copy in multi-bytes chunks
2105     copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
2106 
2107     __ BIND(L_exit);
2108     bs->arraycopy_epilogue(_masm, decorators, type, from, to, qword_count);
2109     restore_arg_regs_using_thread();
2110     if (is_oop) {
2111       inc_counter_np(SharedRuntime::_oop_array_copy_ctr); // Update counter after rscratch1 is free
2112     } else {
2113       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
2114     }
2115     __ vzeroupper();
2116     __ xorptr(rax, rax); // return 0
2117     __ leave(); // required for proper stackwalking of RuntimeStub frame
2118     __ ret(0);
2119 
2120     return start;
2121   }
2122 
2123   // Arguments:
2124   //   aligned - true => Input and output aligned on a HeapWord boundary == 8 bytes
2125   //             ignored
2126   //   is_oop  - true => oop array, so generate store check code
2127   //   name    - stub name string
2128   //
2129   // Inputs:
2130   //   c_rarg0   - source array address
2131   //   c_rarg1   - destination array address
2132   //   c_rarg2   - element count, treated as ssize_t, can be zero
2133   //
2134   address generate_conjoint_long_oop_copy(bool aligned, bool is_oop,
2135                                           address nooverlap_target, address *entry,
2136                                           const char *name, bool dest_uninitialized = false) {
2137     __ align(CodeEntryAlignment);
2138     StubCodeMark mark(this, "StubRoutines", name);
2139     address start = __ pc();
2140 
2141     Label L_copy_bytes, L_copy_8_bytes, L_exit;
2142     const Register from        = rdi;  // source array address
2143     const Register to          = rsi;  // destination array address
2144     const Register qword_count = rdx;  // elements count
2145     const Register saved_count = rcx;
2146 
2147     __ enter(); // required for proper stackwalking of RuntimeStub frame
2148     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
2149 
2150     if (entry != NULL) {
2151       *entry = __ pc();
2152       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
2153       BLOCK_COMMENT("Entry:");
2154     }
2155 
2156     array_overlap_test(nooverlap_target, Address::times_8);
2157     setup_arg_regs_using_thread(); // from => rdi, to => rsi, count => rdx
2158                                    // r9 is used to save r15_thread
2159     // 'from', 'to' and 'qword_count' are now valid
2160 
2161     DecoratorSet decorators = IN_HEAP | IS_ARRAY | ARRAYCOPY_DISJOINT;
2162     if (dest_uninitialized) {
2163       decorators |= IS_DEST_UNINITIALIZED;
2164     }
2165     if (aligned) {
2166       decorators |= ARRAYCOPY_ALIGNED;
2167     }
2168 
2169     BasicType type = is_oop ? T_OBJECT : T_LONG;
2170     BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
2171     bs->arraycopy_prologue(_masm, decorators, type, from, to, qword_count);
2172 
2173     __ jmp(L_copy_bytes);
2174 
2175     // Copy trailing qwords
2176   __ BIND(L_copy_8_bytes);
2177     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
2178     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
2179     __ decrement(qword_count);
2180     __ jcc(Assembler::notZero, L_copy_8_bytes);
2181 
2182     if (is_oop) {
2183       __ jmp(L_exit);
2184     } else {
2185       restore_arg_regs_using_thread();
2186       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
2187       __ xorptr(rax, rax); // return 0
2188       __ vzeroupper();
2189       __ leave(); // required for proper stackwalking of RuntimeStub frame
2190       __ ret(0);
2191     }
2192 
2193     // Copy in multi-bytes chunks
2194     copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
2195 
2196     __ BIND(L_exit);
2197     bs->arraycopy_epilogue(_masm, decorators, type, from, to, qword_count);
2198     restore_arg_regs_using_thread();
2199     if (is_oop) {
2200       inc_counter_np(SharedRuntime::_oop_array_copy_ctr); // Update counter after rscratch1 is free
2201     } else {
2202       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
2203     }
2204     __ vzeroupper();
2205     __ xorptr(rax, rax); // return 0
2206     __ leave(); // required for proper stackwalking of RuntimeStub frame
2207     __ ret(0);
2208 
2209     return start;
2210   }
2211 
2212 
2213   // Helper for generating a dynamic type check.
2214   // Smashes no registers.
2215   void generate_type_check(Register sub_klass,
2216                            Register super_check_offset,
2217                            Register super_klass,
2218                            Label& L_success) {
2219     assert_different_registers(sub_klass, super_check_offset, super_klass);
2220 
2221     BLOCK_COMMENT("type_check:");
2222 
2223     Label L_miss;
2224 
2225     __ check_klass_subtype_fast_path(sub_klass, super_klass, noreg,        &L_success, &L_miss, NULL,
2226                                      super_check_offset);
2227     __ check_klass_subtype_slow_path(sub_klass, super_klass, noreg, noreg, &L_success, NULL);
2228 
2229     // Fall through on failure!
2230     __ BIND(L_miss);
2231   }
2232 
2233   //
2234   //  Generate checkcasting array copy stub
2235   //
2236   //  Input:
2237   //    c_rarg0   - source array address
2238   //    c_rarg1   - destination array address
2239   //    c_rarg2   - element count, treated as ssize_t, can be zero
2240   //    c_rarg3   - size_t ckoff (super_check_offset)
2241   // not Win64
2242   //    c_rarg4   - oop ckval (super_klass)
2243   // Win64
2244   //    rsp+40    - oop ckval (super_klass)
2245   //
2246   //  Output:
2247   //    rax ==  0  -  success
2248   //    rax == -1^K - failure, where K is partial transfer count
2249   //
2250   address generate_checkcast_copy(const char *name, address *entry,
2251                                   bool dest_uninitialized = false) {
2252 
2253     Label L_load_element, L_store_element, L_do_card_marks, L_done;
2254 
2255     // Input registers (after setup_arg_regs)
2256     const Register from        = rdi;   // source array address
2257     const Register to          = rsi;   // destination array address
2258     const Register length      = rdx;   // elements count
2259     const Register ckoff       = rcx;   // super_check_offset
2260     const Register ckval       = r8;    // super_klass
2261 
2262     // Registers used as temps (r13, r14 are save-on-entry)
2263     const Register end_from    = from;  // source array end address
2264     const Register end_to      = r13;   // destination array end address
2265     const Register count       = rdx;   // -(count_remaining)
2266     const Register r14_length  = r14;   // saved copy of length
2267     // End pointers are inclusive, and if length is not zero they point
2268     // to the last unit copied:  end_to[0] := end_from[0]
2269 
2270     const Register rax_oop    = rax;    // actual oop copied
2271     const Register r11_klass  = r11;    // oop._klass
2272 
2273     //---------------------------------------------------------------
2274     // Assembler stub will be used for this call to arraycopy
2275     // if the two arrays are subtypes of Object[] but the
2276     // destination array type is not equal to or a supertype
2277     // of the source type.  Each element must be separately
2278     // checked.
2279 
2280     __ align(CodeEntryAlignment);
2281     StubCodeMark mark(this, "StubRoutines", name);
2282     address start = __ pc();
2283 
2284     __ enter(); // required for proper stackwalking of RuntimeStub frame
2285 
2286 #ifdef ASSERT
2287     // caller guarantees that the arrays really are different
2288     // otherwise, we would have to make conjoint checks
2289     { Label L;
2290       array_overlap_test(L, TIMES_OOP);
2291       __ stop("checkcast_copy within a single array");
2292       __ bind(L);
2293     }
2294 #endif //ASSERT
2295 
2296     setup_arg_regs(4); // from => rdi, to => rsi, length => rdx
2297                        // ckoff => rcx, ckval => r8
2298                        // r9 and r10 may be used to save non-volatile registers
2299 #ifdef _WIN64
2300     // last argument (#4) is on stack on Win64
2301     __ movptr(ckval, Address(rsp, 6 * wordSize));
2302 #endif
2303 
2304     // Caller of this entry point must set up the argument registers.
2305     if (entry != NULL) {
2306       *entry = __ pc();
2307       BLOCK_COMMENT("Entry:");
2308     }
2309 
2310     // allocate spill slots for r13, r14
2311     enum {
2312       saved_r13_offset,
2313       saved_r14_offset,
2314       saved_r10_offset,
2315       saved_rbp_offset
2316     };
2317     __ subptr(rsp, saved_rbp_offset * wordSize);
2318     __ movptr(Address(rsp, saved_r13_offset * wordSize), r13);
2319     __ movptr(Address(rsp, saved_r14_offset * wordSize), r14);
2320     __ movptr(Address(rsp, saved_r10_offset * wordSize), r10);
2321 
2322 #ifdef ASSERT
2323       Label L2;
2324       __ get_thread(r14);
2325       __ cmpptr(r15_thread, r14);
2326       __ jcc(Assembler::equal, L2);
2327       __ stop("StubRoutines::call_stub: r15_thread is modified by call");
2328       __ bind(L2);
2329 #endif // ASSERT
2330 
2331     // check that int operands are properly extended to size_t
2332     assert_clean_int(length, rax);
2333     assert_clean_int(ckoff, rax);
2334 
2335 #ifdef ASSERT
2336     BLOCK_COMMENT("assert consistent ckoff/ckval");
2337     // The ckoff and ckval must be mutually consistent,
2338     // even though caller generates both.
2339     { Label L;
2340       int sco_offset = in_bytes(Klass::super_check_offset_offset());
2341       __ cmpl(ckoff, Address(ckval, sco_offset));
2342       __ jcc(Assembler::equal, L);
2343       __ stop("super_check_offset inconsistent");
2344       __ bind(L);
2345     }
2346 #endif //ASSERT
2347 
2348     // Loop-invariant addresses.  They are exclusive end pointers.
2349     Address end_from_addr(from, length, TIMES_OOP, 0);
2350     Address   end_to_addr(to,   length, TIMES_OOP, 0);
2351     // Loop-variant addresses.  They assume post-incremented count < 0.
2352     Address from_element_addr(end_from, count, TIMES_OOP, 0);
2353     Address   to_element_addr(end_to,   count, TIMES_OOP, 0);
2354 
2355     DecoratorSet decorators = IN_HEAP | IS_ARRAY | ARRAYCOPY_CHECKCAST;
2356     if (dest_uninitialized) {
2357       decorators |= IS_DEST_UNINITIALIZED;
2358     }
2359 
2360     BasicType type = T_OBJECT;
2361     BarrierSetAssembler *bs = BarrierSet::barrier_set()->barrier_set_assembler();
2362     bs->arraycopy_prologue(_masm, decorators, type, from, to, count);
2363 
2364     // Copy from low to high addresses, indexed from the end of each array.
2365     __ lea(end_from, end_from_addr);
2366     __ lea(end_to,   end_to_addr);
2367     __ movptr(r14_length, length);        // save a copy of the length
2368     assert(length == count, "");          // else fix next line:
2369     __ negptr(count);                     // negate and test the length
2370     __ jcc(Assembler::notZero, L_load_element);
2371 
2372     // Empty array:  Nothing to do.
2373     __ xorptr(rax, rax);                  // return 0 on (trivial) success
2374     __ jmp(L_done);
2375 
2376     // ======== begin loop ========
2377     // (Loop is rotated; its entry is L_load_element.)
2378     // Loop control:
2379     //   for (count = -count; count != 0; count++)
2380     // Base pointers src, dst are biased by 8*(count-1),to last element.
2381     __ align(OptoLoopAlignment);
2382 
2383     __ BIND(L_store_element);
2384     __ store_heap_oop(to_element_addr, rax_oop, noreg, noreg, AS_RAW);  // store the oop
2385     __ increment(count);               // increment the count toward zero
2386     __ jcc(Assembler::zero, L_do_card_marks);
2387 
2388     // ======== loop entry is here ========
2389     __ BIND(L_load_element);
2390     __ load_heap_oop(rax_oop, from_element_addr, noreg, noreg, AS_RAW); // load the oop
2391     __ testptr(rax_oop, rax_oop);
2392     __ jcc(Assembler::zero, L_store_element);
2393 
2394     __ load_klass(r11_klass, rax_oop);// query the object klass
2395     generate_type_check(r11_klass, ckoff, ckval, L_store_element);
2396     // ======== end loop ========
2397 
2398     // It was a real error; we must depend on the caller to finish the job.
2399     // Register rdx = -1 * number of *remaining* oops, r14 = *total* oops.
2400     // Emit GC store barriers for the oops we have copied (r14 + rdx),
2401     // and report their number to the caller.
2402     assert_different_registers(rax, r14_length, count, to, end_to, rcx, rscratch1);
2403     Label L_post_barrier;
2404     __ addptr(r14_length, count);     // K = (original - remaining) oops
2405     __ movptr(rax, r14_length);       // save the value
2406     __ notptr(rax);                   // report (-1^K) to caller (does not affect flags)
2407     __ jccb(Assembler::notZero, L_post_barrier);
2408     __ jmp(L_done); // K == 0, nothing was copied, skip post barrier
2409 
2410     // Come here on success only.
2411     __ BIND(L_do_card_marks);
2412     __ xorptr(rax, rax);              // return 0 on success
2413 
2414     __ BIND(L_post_barrier);
2415     bs->arraycopy_epilogue(_masm, decorators, type, from, to, r14_length);
2416 
2417     // Common exit point (success or failure).
2418     __ BIND(L_done);
2419     __ movptr(r13, Address(rsp, saved_r13_offset * wordSize));
2420     __ movptr(r14, Address(rsp, saved_r14_offset * wordSize));
2421     __ movptr(r10, Address(rsp, saved_r10_offset * wordSize));
2422     restore_arg_regs();
2423     inc_counter_np(SharedRuntime::_checkcast_array_copy_ctr); // Update counter after rscratch1 is free
2424     __ leave(); // required for proper stackwalking of RuntimeStub frame
2425     __ ret(0);
2426 
2427     return start;
2428   }
2429 
2430   //
2431   //  Generate 'unsafe' array copy stub
2432   //  Though just as safe as the other stubs, it takes an unscaled
2433   //  size_t argument instead of an element count.
2434   //
2435   //  Input:
2436   //    c_rarg0   - source array address
2437   //    c_rarg1   - destination array address
2438   //    c_rarg2   - byte count, treated as ssize_t, can be zero
2439   //
2440   // Examines the alignment of the operands and dispatches
2441   // to a long, int, short, or byte copy loop.
2442   //
2443   address generate_unsafe_copy(const char *name,
2444                                address byte_copy_entry, address short_copy_entry,
2445                                address int_copy_entry, address long_copy_entry) {
2446 
2447     Label L_long_aligned, L_int_aligned, L_short_aligned;
2448 
2449     // Input registers (before setup_arg_regs)
2450     const Register from        = c_rarg0;  // source array address
2451     const Register to          = c_rarg1;  // destination array address
2452     const Register size        = c_rarg2;  // byte count (size_t)
2453 
2454     // Register used as a temp
2455     const Register bits        = rax;      // test copy of low bits
2456 
2457     __ align(CodeEntryAlignment);
2458     StubCodeMark mark(this, "StubRoutines", name);
2459     address start = __ pc();
2460 
2461     __ enter(); // required for proper stackwalking of RuntimeStub frame
2462 
2463     // bump this on entry, not on exit:
2464     inc_counter_np(SharedRuntime::_unsafe_array_copy_ctr);
2465 
2466     __ mov(bits, from);
2467     __ orptr(bits, to);
2468     __ orptr(bits, size);
2469 
2470     __ testb(bits, BytesPerLong-1);
2471     __ jccb(Assembler::zero, L_long_aligned);
2472 
2473     __ testb(bits, BytesPerInt-1);
2474     __ jccb(Assembler::zero, L_int_aligned);
2475 
2476     __ testb(bits, BytesPerShort-1);
2477     __ jump_cc(Assembler::notZero, RuntimeAddress(byte_copy_entry));
2478 
2479     __ BIND(L_short_aligned);
2480     __ shrptr(size, LogBytesPerShort); // size => short_count
2481     __ jump(RuntimeAddress(short_copy_entry));
2482 
2483     __ BIND(L_int_aligned);
2484     __ shrptr(size, LogBytesPerInt); // size => int_count
2485     __ jump(RuntimeAddress(int_copy_entry));
2486 
2487     __ BIND(L_long_aligned);
2488     __ shrptr(size, LogBytesPerLong); // size => qword_count
2489     __ jump(RuntimeAddress(long_copy_entry));
2490 
2491     return start;
2492   }
2493 
2494   // Perform range checks on the proposed arraycopy.
2495   // Kills temp, but nothing else.
2496   // Also, clean the sign bits of src_pos and dst_pos.
2497   void arraycopy_range_checks(Register src,     // source array oop (c_rarg0)
2498                               Register src_pos, // source position (c_rarg1)
2499                               Register dst,     // destination array oo (c_rarg2)
2500                               Register dst_pos, // destination position (c_rarg3)
2501                               Register length,
2502                               Register temp,
2503                               Label& L_failed) {
2504     BLOCK_COMMENT("arraycopy_range_checks:");
2505 
2506     //  if (src_pos + length > arrayOop(src)->length())  FAIL;
2507     __ movl(temp, length);
2508     __ addl(temp, src_pos);             // src_pos + length
2509     __ cmpl(temp, Address(src, arrayOopDesc::length_offset_in_bytes()));
2510     __ jcc(Assembler::above, L_failed);
2511 
2512     //  if (dst_pos + length > arrayOop(dst)->length())  FAIL;
2513     __ movl(temp, length);
2514     __ addl(temp, dst_pos);             // dst_pos + length
2515     __ cmpl(temp, Address(dst, arrayOopDesc::length_offset_in_bytes()));
2516     __ jcc(Assembler::above, L_failed);
2517 
2518     // Have to clean up high 32-bits of 'src_pos' and 'dst_pos'.
2519     // Move with sign extension can be used since they are positive.
2520     __ movslq(src_pos, src_pos);
2521     __ movslq(dst_pos, dst_pos);
2522 
2523     BLOCK_COMMENT("arraycopy_range_checks done");
2524   }
2525 
2526   //
2527   //  Generate generic array copy stubs
2528   //
2529   //  Input:
2530   //    c_rarg0    -  src oop
2531   //    c_rarg1    -  src_pos (32-bits)
2532   //    c_rarg2    -  dst oop
2533   //    c_rarg3    -  dst_pos (32-bits)
2534   // not Win64
2535   //    c_rarg4    -  element count (32-bits)
2536   // Win64
2537   //    rsp+40     -  element count (32-bits)
2538   //
2539   //  Output:
2540   //    rax ==  0  -  success
2541   //    rax == -1^K - failure, where K is partial transfer count
2542   //
2543   address generate_generic_copy(const char *name,
2544                                 address byte_copy_entry, address short_copy_entry,
2545                                 address int_copy_entry, address oop_copy_entry,
2546                                 address long_copy_entry, address checkcast_copy_entry) {
2547 
2548     Label L_failed, L_failed_0, L_objArray;
2549     Label L_copy_bytes, L_copy_shorts, L_copy_ints, L_copy_longs;
2550 
2551     // Input registers
2552     const Register src        = c_rarg0;  // source array oop
2553     const Register src_pos    = c_rarg1;  // source position
2554     const Register dst        = c_rarg2;  // destination array oop
2555     const Register dst_pos    = c_rarg3;  // destination position
2556 #ifndef _WIN64
2557     const Register length     = c_rarg4;
2558 #else
2559     const Address  length(rsp, 6 * wordSize);  // elements count is on stack on Win64
2560 #endif
2561 
2562     { int modulus = CodeEntryAlignment;
2563       int target  = modulus - 5; // 5 = sizeof jmp(L_failed)
2564       int advance = target - (__ offset() % modulus);
2565       if (advance < 0)  advance += modulus;
2566       if (advance > 0)  __ nop(advance);
2567     }
2568     StubCodeMark mark(this, "StubRoutines", name);
2569 
2570     // Short-hop target to L_failed.  Makes for denser prologue code.
2571     __ BIND(L_failed_0);
2572     __ jmp(L_failed);
2573     assert(__ offset() % CodeEntryAlignment == 0, "no further alignment needed");
2574 
2575     __ align(CodeEntryAlignment);
2576     address start = __ pc();
2577 
2578     __ enter(); // required for proper stackwalking of RuntimeStub frame
2579 
2580     // bump this on entry, not on exit:
2581     inc_counter_np(SharedRuntime::_generic_array_copy_ctr);
2582 
2583     //-----------------------------------------------------------------------
2584     // Assembler stub will be used for this call to arraycopy
2585     // if the following conditions are met:
2586     //
2587     // (1) src and dst must not be null.
2588     // (2) src_pos must not be negative.
2589     // (3) dst_pos must not be negative.
2590     // (4) length  must not be negative.
2591     // (5) src klass and dst klass should be the same and not NULL.
2592     // (6) src and dst should be arrays.
2593     // (7) src_pos + length must not exceed length of src.
2594     // (8) dst_pos + length must not exceed length of dst.
2595     //
2596 
2597     //  if (src == NULL) return -1;
2598     __ testptr(src, src);         // src oop
2599     size_t j1off = __ offset();
2600     __ jccb(Assembler::zero, L_failed_0);
2601 
2602     //  if (src_pos < 0) return -1;
2603     __ testl(src_pos, src_pos); // src_pos (32-bits)
2604     __ jccb(Assembler::negative, L_failed_0);
2605 
2606     //  if (dst == NULL) return -1;
2607     __ testptr(dst, dst);         // dst oop
2608     __ jccb(Assembler::zero, L_failed_0);
2609 
2610     //  if (dst_pos < 0) return -1;
2611     __ testl(dst_pos, dst_pos); // dst_pos (32-bits)
2612     size_t j4off = __ offset();
2613     __ jccb(Assembler::negative, L_failed_0);
2614 
2615     // The first four tests are very dense code,
2616     // but not quite dense enough to put four
2617     // jumps in a 16-byte instruction fetch buffer.
2618     // That's good, because some branch predicters
2619     // do not like jumps so close together.
2620     // Make sure of this.
2621     guarantee(((j1off ^ j4off) & ~15) != 0, "I$ line of 1st & 4th jumps");
2622 
2623     // registers used as temp
2624     const Register r11_length    = r11; // elements count to copy
2625     const Register r10_src_klass = r10; // array klass
2626 
2627     //  if (length < 0) return -1;
2628     __ movl(r11_length, length);        // length (elements count, 32-bits value)
2629     __ testl(r11_length, r11_length);
2630     __ jccb(Assembler::negative, L_failed_0);
2631 
2632     __ load_klass(r10_src_klass, src);
2633 #ifdef ASSERT
2634     //  assert(src->klass() != NULL);
2635     {
2636       BLOCK_COMMENT("assert klasses not null {");
2637       Label L1, L2;
2638       __ testptr(r10_src_klass, r10_src_klass);
2639       __ jcc(Assembler::notZero, L2);   // it is broken if klass is NULL
2640       __ bind(L1);
2641       __ stop("broken null klass");
2642       __ bind(L2);
2643       __ load_klass(rax, dst);
2644       __ cmpq(rax, 0);
2645       __ jcc(Assembler::equal, L1);     // this would be broken also
2646       BLOCK_COMMENT("} assert klasses not null done");
2647     }
2648 #endif
2649 
2650     // Load layout helper (32-bits)
2651     //
2652     //  |array_tag|     | header_size | element_type |     |log2_element_size|
2653     // 32        30    24            16              8     2                 0
2654     //
2655     //   array_tag: typeArray = 0x3, objArray = 0x2, non-array = 0x0
2656     //
2657 
2658     const int lh_offset = in_bytes(Klass::layout_helper_offset());
2659 
2660     // Handle objArrays completely differently...
2661     const jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
2662     __ cmpl(Address(r10_src_klass, lh_offset), objArray_lh);
2663     __ jcc(Assembler::equal, L_objArray);
2664 
2665     //  if (src->klass() != dst->klass()) return -1;
2666     __ load_klass(rax, dst);
2667     __ cmpq(r10_src_klass, rax);
2668     __ jcc(Assembler::notEqual, L_failed);
2669 
2670     const Register rax_lh = rax;  // layout helper
2671     __ movl(rax_lh, Address(r10_src_klass, lh_offset));
2672 
2673     //  if (!src->is_Array()) return -1;
2674     __ cmpl(rax_lh, Klass::_lh_neutral_value);
2675     __ jcc(Assembler::greaterEqual, L_failed);
2676 
2677     // At this point, it is known to be a typeArray (array_tag 0x3).
2678 #ifdef ASSERT
2679     {
2680       BLOCK_COMMENT("assert primitive array {");
2681       Label L;
2682       __ cmpl(rax_lh, (Klass::_lh_array_tag_type_value << Klass::_lh_array_tag_shift));
2683       __ jcc(Assembler::greaterEqual, L);
2684       __ stop("must be a primitive array");
2685       __ bind(L);
2686       BLOCK_COMMENT("} assert primitive array done");
2687     }
2688 #endif
2689 
2690     arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
2691                            r10, L_failed);
2692 
2693     // TypeArrayKlass
2694     //
2695     // src_addr = (src + array_header_in_bytes()) + (src_pos << log2elemsize);
2696     // dst_addr = (dst + array_header_in_bytes()) + (dst_pos << log2elemsize);
2697     //
2698 
2699     const Register r10_offset = r10;    // array offset
2700     const Register rax_elsize = rax_lh; // element size
2701 
2702     __ movl(r10_offset, rax_lh);
2703     __ shrl(r10_offset, Klass::_lh_header_size_shift);
2704     __ andptr(r10_offset, Klass::_lh_header_size_mask);   // array_offset
2705     __ addptr(src, r10_offset);           // src array offset
2706     __ addptr(dst, r10_offset);           // dst array offset
2707     BLOCK_COMMENT("choose copy loop based on element size");
2708     __ andl(rax_lh, Klass::_lh_log2_element_size_mask); // rax_lh -> rax_elsize
2709 
2710     // next registers should be set before the jump to corresponding stub
2711     const Register from     = c_rarg0;  // source array address
2712     const Register to       = c_rarg1;  // destination array address
2713     const Register count    = c_rarg2;  // elements count
2714 
2715     // 'from', 'to', 'count' registers should be set in such order
2716     // since they are the same as 'src', 'src_pos', 'dst'.
2717 
2718   __ BIND(L_copy_bytes);
2719     __ cmpl(rax_elsize, 0);
2720     __ jccb(Assembler::notEqual, L_copy_shorts);
2721     __ lea(from, Address(src, src_pos, Address::times_1, 0));// src_addr
2722     __ lea(to,   Address(dst, dst_pos, Address::times_1, 0));// dst_addr
2723     __ movl2ptr(count, r11_length); // length
2724     __ jump(RuntimeAddress(byte_copy_entry));
2725 
2726   __ BIND(L_copy_shorts);
2727     __ cmpl(rax_elsize, LogBytesPerShort);
2728     __ jccb(Assembler::notEqual, L_copy_ints);
2729     __ lea(from, Address(src, src_pos, Address::times_2, 0));// src_addr
2730     __ lea(to,   Address(dst, dst_pos, Address::times_2, 0));// dst_addr
2731     __ movl2ptr(count, r11_length); // length
2732     __ jump(RuntimeAddress(short_copy_entry));
2733 
2734   __ BIND(L_copy_ints);
2735     __ cmpl(rax_elsize, LogBytesPerInt);
2736     __ jccb(Assembler::notEqual, L_copy_longs);
2737     __ lea(from, Address(src, src_pos, Address::times_4, 0));// src_addr
2738     __ lea(to,   Address(dst, dst_pos, Address::times_4, 0));// dst_addr
2739     __ movl2ptr(count, r11_length); // length
2740     __ jump(RuntimeAddress(int_copy_entry));
2741 
2742   __ BIND(L_copy_longs);
2743 #ifdef ASSERT
2744     {
2745       BLOCK_COMMENT("assert long copy {");
2746       Label L;
2747       __ cmpl(rax_elsize, LogBytesPerLong);
2748       __ jcc(Assembler::equal, L);
2749       __ stop("must be long copy, but elsize is wrong");
2750       __ bind(L);
2751       BLOCK_COMMENT("} assert long copy done");
2752     }
2753 #endif
2754     __ lea(from, Address(src, src_pos, Address::times_8, 0));// src_addr
2755     __ lea(to,   Address(dst, dst_pos, Address::times_8, 0));// dst_addr
2756     __ movl2ptr(count, r11_length); // length
2757     __ jump(RuntimeAddress(long_copy_entry));
2758 
2759     // ObjArrayKlass
2760   __ BIND(L_objArray);
2761     // live at this point:  r10_src_klass, r11_length, src[_pos], dst[_pos]
2762 
2763     Label L_plain_copy, L_checkcast_copy;
2764     //  test array classes for subtyping
2765     __ load_klass(rax, dst);
2766     __ cmpq(r10_src_klass, rax); // usual case is exact equality
2767     __ jcc(Assembler::notEqual, L_checkcast_copy);
2768 
2769     // Identically typed arrays can be copied without element-wise checks.
2770     arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
2771                            r10, L_failed);
2772 
2773     __ lea(from, Address(src, src_pos, TIMES_OOP,
2774                  arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // src_addr
2775     __ lea(to,   Address(dst, dst_pos, TIMES_OOP,
2776                  arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // dst_addr
2777     __ movl2ptr(count, r11_length); // length
2778   __ BIND(L_plain_copy);
2779     __ jump(RuntimeAddress(oop_copy_entry));
2780 
2781   __ BIND(L_checkcast_copy);
2782     // live at this point:  r10_src_klass, r11_length, rax (dst_klass)
2783     {
2784       // Before looking at dst.length, make sure dst is also an objArray.
2785       __ cmpl(Address(rax, lh_offset), objArray_lh);
2786       __ jcc(Assembler::notEqual, L_failed);
2787 
2788       // It is safe to examine both src.length and dst.length.
2789       arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
2790                              rax, L_failed);
2791 
2792       const Register r11_dst_klass = r11;
2793       __ load_klass(r11_dst_klass, dst); // reload
2794 
2795       // Marshal the base address arguments now, freeing registers.
2796       __ lea(from, Address(src, src_pos, TIMES_OOP,
2797                    arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
2798       __ lea(to,   Address(dst, dst_pos, TIMES_OOP,
2799                    arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
2800       __ movl(count, length);           // length (reloaded)
2801       Register sco_temp = c_rarg3;      // this register is free now
2802       assert_different_registers(from, to, count, sco_temp,
2803                                  r11_dst_klass, r10_src_klass);
2804       assert_clean_int(count, sco_temp);
2805 
2806       // Generate the type check.
2807       const int sco_offset = in_bytes(Klass::super_check_offset_offset());
2808       __ movl(sco_temp, Address(r11_dst_klass, sco_offset));
2809       assert_clean_int(sco_temp, rax);
2810       generate_type_check(r10_src_klass, sco_temp, r11_dst_klass, L_plain_copy);
2811 
2812       // Fetch destination element klass from the ObjArrayKlass header.
2813       int ek_offset = in_bytes(ObjArrayKlass::element_klass_offset());
2814       __ movptr(r11_dst_klass, Address(r11_dst_klass, ek_offset));
2815       __ movl(  sco_temp,      Address(r11_dst_klass, sco_offset));
2816       assert_clean_int(sco_temp, rax);
2817 
2818       // the checkcast_copy loop needs two extra arguments:
2819       assert(c_rarg3 == sco_temp, "#3 already in place");
2820       // Set up arguments for checkcast_copy_entry.
2821       setup_arg_regs(4);
2822       __ movptr(r8, r11_dst_klass);  // dst.klass.element_klass, r8 is c_rarg4 on Linux/Solaris
2823       __ jump(RuntimeAddress(checkcast_copy_entry));
2824     }
2825 
2826   __ BIND(L_failed);
2827     __ xorptr(rax, rax);
2828     __ notptr(rax); // return -1
2829     __ leave();   // required for proper stackwalking of RuntimeStub frame
2830     __ ret(0);
2831 
2832     return start;
2833   }
2834 
2835   void generate_arraycopy_stubs() {
2836     address entry;
2837     address entry_jbyte_arraycopy;
2838     address entry_jshort_arraycopy;
2839     address entry_jint_arraycopy;
2840     address entry_oop_arraycopy;
2841     address entry_jlong_arraycopy;
2842     address entry_checkcast_arraycopy;
2843 
2844     StubRoutines::_jbyte_disjoint_arraycopy  = generate_disjoint_byte_copy(false, &entry,
2845                                                                            "jbyte_disjoint_arraycopy");
2846     StubRoutines::_jbyte_arraycopy           = generate_conjoint_byte_copy(false, entry, &entry_jbyte_arraycopy,
2847                                                                            "jbyte_arraycopy");
2848 
2849     StubRoutines::_jshort_disjoint_arraycopy = generate_disjoint_short_copy(false, &entry,
2850                                                                             "jshort_disjoint_arraycopy");
2851     StubRoutines::_jshort_arraycopy          = generate_conjoint_short_copy(false, entry, &entry_jshort_arraycopy,
2852                                                                             "jshort_arraycopy");
2853 
2854     StubRoutines::_jint_disjoint_arraycopy   = generate_disjoint_int_oop_copy(false, false, &entry,
2855                                                                               "jint_disjoint_arraycopy");
2856     StubRoutines::_jint_arraycopy            = generate_conjoint_int_oop_copy(false, false, entry,
2857                                                                               &entry_jint_arraycopy, "jint_arraycopy");
2858 
2859     StubRoutines::_jlong_disjoint_arraycopy  = generate_disjoint_long_oop_copy(false, false, &entry,
2860                                                                                "jlong_disjoint_arraycopy");
2861     StubRoutines::_jlong_arraycopy           = generate_conjoint_long_oop_copy(false, false, entry,
2862                                                                                &entry_jlong_arraycopy, "jlong_arraycopy");
2863 
2864 
2865     if (UseCompressedOops) {
2866       StubRoutines::_oop_disjoint_arraycopy  = generate_disjoint_int_oop_copy(false, true, &entry,
2867                                                                               "oop_disjoint_arraycopy");
2868       StubRoutines::_oop_arraycopy           = generate_conjoint_int_oop_copy(false, true, entry,
2869                                                                               &entry_oop_arraycopy, "oop_arraycopy");
2870       StubRoutines::_oop_disjoint_arraycopy_uninit  = generate_disjoint_int_oop_copy(false, true, &entry,
2871                                                                                      "oop_disjoint_arraycopy_uninit",
2872                                                                                      /*dest_uninitialized*/true);
2873       StubRoutines::_oop_arraycopy_uninit           = generate_conjoint_int_oop_copy(false, true, entry,
2874                                                                                      NULL, "oop_arraycopy_uninit",
2875                                                                                      /*dest_uninitialized*/true);
2876     } else {
2877       StubRoutines::_oop_disjoint_arraycopy  = generate_disjoint_long_oop_copy(false, true, &entry,
2878                                                                                "oop_disjoint_arraycopy");
2879       StubRoutines::_oop_arraycopy           = generate_conjoint_long_oop_copy(false, true, entry,
2880                                                                                &entry_oop_arraycopy, "oop_arraycopy");
2881       StubRoutines::_oop_disjoint_arraycopy_uninit  = generate_disjoint_long_oop_copy(false, true, &entry,
2882                                                                                       "oop_disjoint_arraycopy_uninit",
2883                                                                                       /*dest_uninitialized*/true);
2884       StubRoutines::_oop_arraycopy_uninit           = generate_conjoint_long_oop_copy(false, true, entry,
2885                                                                                       NULL, "oop_arraycopy_uninit",
2886                                                                                       /*dest_uninitialized*/true);
2887     }
2888 
2889     StubRoutines::_checkcast_arraycopy        = generate_checkcast_copy("checkcast_arraycopy", &entry_checkcast_arraycopy);
2890     StubRoutines::_checkcast_arraycopy_uninit = generate_checkcast_copy("checkcast_arraycopy_uninit", NULL,
2891                                                                         /*dest_uninitialized*/true);
2892 
2893     StubRoutines::_unsafe_arraycopy    = generate_unsafe_copy("unsafe_arraycopy",
2894                                                               entry_jbyte_arraycopy,
2895                                                               entry_jshort_arraycopy,
2896                                                               entry_jint_arraycopy,
2897                                                               entry_jlong_arraycopy);
2898     StubRoutines::_generic_arraycopy   = generate_generic_copy("generic_arraycopy",
2899                                                                entry_jbyte_arraycopy,
2900                                                                entry_jshort_arraycopy,
2901                                                                entry_jint_arraycopy,
2902                                                                entry_oop_arraycopy,
2903                                                                entry_jlong_arraycopy,
2904                                                                entry_checkcast_arraycopy);
2905 
2906     StubRoutines::_jbyte_fill = generate_fill(T_BYTE, false, "jbyte_fill");
2907     StubRoutines::_jshort_fill = generate_fill(T_SHORT, false, "jshort_fill");
2908     StubRoutines::_jint_fill = generate_fill(T_INT, false, "jint_fill");
2909     StubRoutines::_arrayof_jbyte_fill = generate_fill(T_BYTE, true, "arrayof_jbyte_fill");
2910     StubRoutines::_arrayof_jshort_fill = generate_fill(T_SHORT, true, "arrayof_jshort_fill");
2911     StubRoutines::_arrayof_jint_fill = generate_fill(T_INT, true, "arrayof_jint_fill");
2912 
2913     // We don't generate specialized code for HeapWord-aligned source
2914     // arrays, so just use the code we've already generated
2915     StubRoutines::_arrayof_jbyte_disjoint_arraycopy  = StubRoutines::_jbyte_disjoint_arraycopy;
2916     StubRoutines::_arrayof_jbyte_arraycopy           = StubRoutines::_jbyte_arraycopy;
2917 
2918     StubRoutines::_arrayof_jshort_disjoint_arraycopy = StubRoutines::_jshort_disjoint_arraycopy;
2919     StubRoutines::_arrayof_jshort_arraycopy          = StubRoutines::_jshort_arraycopy;
2920 
2921     StubRoutines::_arrayof_jint_disjoint_arraycopy   = StubRoutines::_jint_disjoint_arraycopy;
2922     StubRoutines::_arrayof_jint_arraycopy            = StubRoutines::_jint_arraycopy;
2923 
2924     StubRoutines::_arrayof_jlong_disjoint_arraycopy  = StubRoutines::_jlong_disjoint_arraycopy;
2925     StubRoutines::_arrayof_jlong_arraycopy           = StubRoutines::_jlong_arraycopy;
2926 
2927     StubRoutines::_arrayof_oop_disjoint_arraycopy    = StubRoutines::_oop_disjoint_arraycopy;
2928     StubRoutines::_arrayof_oop_arraycopy             = StubRoutines::_oop_arraycopy;
2929 
2930     StubRoutines::_arrayof_oop_disjoint_arraycopy_uninit    = StubRoutines::_oop_disjoint_arraycopy_uninit;
2931     StubRoutines::_arrayof_oop_arraycopy_uninit             = StubRoutines::_oop_arraycopy_uninit;
2932   }
2933 
2934   // AES intrinsic stubs
2935   enum {AESBlockSize = 16};
2936 
2937   address generate_key_shuffle_mask() {
2938     __ align(16);
2939     StubCodeMark mark(this, "StubRoutines", "key_shuffle_mask");
2940     address start = __ pc();
2941     __ emit_data64( 0x0405060700010203, relocInfo::none );
2942     __ emit_data64( 0x0c0d0e0f08090a0b, relocInfo::none );
2943     return start;
2944   }
2945 
2946   address generate_counter_shuffle_mask() {
2947     __ align(16);
2948     StubCodeMark mark(this, "StubRoutines", "counter_shuffle_mask");
2949     address start = __ pc();
2950     __ emit_data64(0x08090a0b0c0d0e0f, relocInfo::none);
2951     __ emit_data64(0x0001020304050607, relocInfo::none);
2952     return start;
2953   }
2954 
2955   // Utility routine for loading a 128-bit key word in little endian format
2956   // can optionally specify that the shuffle mask is already in an xmmregister
2957   void load_key(XMMRegister xmmdst, Register key, int offset, XMMRegister xmm_shuf_mask=NULL) {
2958     __ movdqu(xmmdst, Address(key, offset));
2959     if (xmm_shuf_mask != NULL) {
2960       __ pshufb(xmmdst, xmm_shuf_mask);
2961     } else {
2962       __ pshufb(xmmdst, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
2963     }
2964   }
2965 
2966   // Utility routine for increase 128bit counter (iv in CTR mode)
2967   void inc_counter(Register reg, XMMRegister xmmdst, int inc_delta, Label& next_block) {
2968     __ pextrq(reg, xmmdst, 0x0);
2969     __ addq(reg, inc_delta);
2970     __ pinsrq(xmmdst, reg, 0x0);
2971     __ jcc(Assembler::carryClear, next_block); // jump if no carry
2972     __ pextrq(reg, xmmdst, 0x01); // Carry
2973     __ addq(reg, 0x01);
2974     __ pinsrq(xmmdst, reg, 0x01); //Carry end
2975     __ BIND(next_block);          // next instruction
2976   }
2977 
2978   // Arguments:
2979   //
2980   // Inputs:
2981   //   c_rarg0   - source byte array address
2982   //   c_rarg1   - destination byte array address
2983   //   c_rarg2   - K (key) in little endian int array
2984   //
2985   address generate_aescrypt_encryptBlock() {
2986     assert(UseAES, "need AES instructions and misaligned SSE support");
2987     __ align(CodeEntryAlignment);
2988     StubCodeMark mark(this, "StubRoutines", "aescrypt_encryptBlock");
2989     Label L_doLast;
2990     address start = __ pc();
2991 
2992     const Register from        = c_rarg0;  // source array address
2993     const Register to          = c_rarg1;  // destination array address
2994     const Register key         = c_rarg2;  // key array address
2995     const Register keylen      = rax;
2996 
2997     const XMMRegister xmm_result = xmm0;
2998     const XMMRegister xmm_key_shuf_mask = xmm1;
2999     // On win64 xmm6-xmm15 must be preserved so don't use them.
3000     const XMMRegister xmm_temp1  = xmm2;
3001     const XMMRegister xmm_temp2  = xmm3;
3002     const XMMRegister xmm_temp3  = xmm4;
3003     const XMMRegister xmm_temp4  = xmm5;
3004 
3005     __ enter(); // required for proper stackwalking of RuntimeStub frame
3006 
3007     // keylen could be only {11, 13, 15} * 4 = {44, 52, 60}
3008     __ movl(keylen, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
3009 
3010     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
3011     __ movdqu(xmm_result, Address(from, 0));  // get 16 bytes of input
3012 
3013     // For encryption, the java expanded key ordering is just what we need
3014     // we don't know if the key is aligned, hence not using load-execute form
3015 
3016     load_key(xmm_temp1, key, 0x00, xmm_key_shuf_mask);
3017     __ pxor(xmm_result, xmm_temp1);
3018 
3019     load_key(xmm_temp1, key, 0x10, xmm_key_shuf_mask);
3020     load_key(xmm_temp2, key, 0x20, xmm_key_shuf_mask);
3021     load_key(xmm_temp3, key, 0x30, xmm_key_shuf_mask);
3022     load_key(xmm_temp4, key, 0x40, xmm_key_shuf_mask);
3023 
3024     __ aesenc(xmm_result, xmm_temp1);
3025     __ aesenc(xmm_result, xmm_temp2);
3026     __ aesenc(xmm_result, xmm_temp3);
3027     __ aesenc(xmm_result, xmm_temp4);
3028 
3029     load_key(xmm_temp1, key, 0x50, xmm_key_shuf_mask);
3030     load_key(xmm_temp2, key, 0x60, xmm_key_shuf_mask);
3031     load_key(xmm_temp3, key, 0x70, xmm_key_shuf_mask);
3032     load_key(xmm_temp4, key, 0x80, xmm_key_shuf_mask);
3033 
3034     __ aesenc(xmm_result, xmm_temp1);
3035     __ aesenc(xmm_result, xmm_temp2);
3036     __ aesenc(xmm_result, xmm_temp3);
3037     __ aesenc(xmm_result, xmm_temp4);
3038 
3039     load_key(xmm_temp1, key, 0x90, xmm_key_shuf_mask);
3040     load_key(xmm_temp2, key, 0xa0, xmm_key_shuf_mask);
3041 
3042     __ cmpl(keylen, 44);
3043     __ jccb(Assembler::equal, L_doLast);
3044 
3045     __ aesenc(xmm_result, xmm_temp1);
3046     __ aesenc(xmm_result, xmm_temp2);
3047 
3048     load_key(xmm_temp1, key, 0xb0, xmm_key_shuf_mask);
3049     load_key(xmm_temp2, key, 0xc0, xmm_key_shuf_mask);
3050 
3051     __ cmpl(keylen, 52);
3052     __ jccb(Assembler::equal, L_doLast);
3053 
3054     __ aesenc(xmm_result, xmm_temp1);
3055     __ aesenc(xmm_result, xmm_temp2);
3056 
3057     load_key(xmm_temp1, key, 0xd0, xmm_key_shuf_mask);
3058     load_key(xmm_temp2, key, 0xe0, xmm_key_shuf_mask);
3059 
3060     __ BIND(L_doLast);
3061     __ aesenc(xmm_result, xmm_temp1);
3062     __ aesenclast(xmm_result, xmm_temp2);
3063     __ movdqu(Address(to, 0), xmm_result);        // store the result
3064     __ xorptr(rax, rax); // return 0
3065     __ leave(); // required for proper stackwalking of RuntimeStub frame
3066     __ ret(0);
3067 
3068     return start;
3069   }
3070 
3071 
3072   // Arguments:
3073   //
3074   // Inputs:
3075   //   c_rarg0   - source byte array address
3076   //   c_rarg1   - destination byte array address
3077   //   c_rarg2   - K (key) in little endian int array
3078   //
3079   address generate_aescrypt_decryptBlock() {
3080     assert(UseAES, "need AES instructions and misaligned SSE support");
3081     __ align(CodeEntryAlignment);
3082     StubCodeMark mark(this, "StubRoutines", "aescrypt_decryptBlock");
3083     Label L_doLast;
3084     address start = __ pc();
3085 
3086     const Register from        = c_rarg0;  // source array address
3087     const Register to          = c_rarg1;  // destination array address
3088     const Register key         = c_rarg2;  // key array address
3089     const Register keylen      = rax;
3090 
3091     const XMMRegister xmm_result = xmm0;
3092     const XMMRegister xmm_key_shuf_mask = xmm1;
3093     // On win64 xmm6-xmm15 must be preserved so don't use them.
3094     const XMMRegister xmm_temp1  = xmm2;
3095     const XMMRegister xmm_temp2  = xmm3;
3096     const XMMRegister xmm_temp3  = xmm4;
3097     const XMMRegister xmm_temp4  = xmm5;
3098 
3099     __ enter(); // required for proper stackwalking of RuntimeStub frame
3100 
3101     // keylen could be only {11, 13, 15} * 4 = {44, 52, 60}
3102     __ movl(keylen, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
3103 
3104     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
3105     __ movdqu(xmm_result, Address(from, 0));
3106 
3107     // for decryption java expanded key ordering is rotated one position from what we want
3108     // so we start from 0x10 here and hit 0x00 last
3109     // we don't know if the key is aligned, hence not using load-execute form
3110     load_key(xmm_temp1, key, 0x10, xmm_key_shuf_mask);
3111     load_key(xmm_temp2, key, 0x20, xmm_key_shuf_mask);
3112     load_key(xmm_temp3, key, 0x30, xmm_key_shuf_mask);
3113     load_key(xmm_temp4, key, 0x40, xmm_key_shuf_mask);
3114 
3115     __ pxor  (xmm_result, xmm_temp1);
3116     __ aesdec(xmm_result, xmm_temp2);
3117     __ aesdec(xmm_result, xmm_temp3);
3118     __ aesdec(xmm_result, xmm_temp4);
3119 
3120     load_key(xmm_temp1, key, 0x50, xmm_key_shuf_mask);
3121     load_key(xmm_temp2, key, 0x60, xmm_key_shuf_mask);
3122     load_key(xmm_temp3, key, 0x70, xmm_key_shuf_mask);
3123     load_key(xmm_temp4, key, 0x80, xmm_key_shuf_mask);
3124 
3125     __ aesdec(xmm_result, xmm_temp1);
3126     __ aesdec(xmm_result, xmm_temp2);
3127     __ aesdec(xmm_result, xmm_temp3);
3128     __ aesdec(xmm_result, xmm_temp4);
3129 
3130     load_key(xmm_temp1, key, 0x90, xmm_key_shuf_mask);
3131     load_key(xmm_temp2, key, 0xa0, xmm_key_shuf_mask);
3132     load_key(xmm_temp3, key, 0x00, xmm_key_shuf_mask);
3133 
3134     __ cmpl(keylen, 44);
3135     __ jccb(Assembler::equal, L_doLast);
3136 
3137     __ aesdec(xmm_result, xmm_temp1);
3138     __ aesdec(xmm_result, xmm_temp2);
3139 
3140     load_key(xmm_temp1, key, 0xb0, xmm_key_shuf_mask);
3141     load_key(xmm_temp2, key, 0xc0, xmm_key_shuf_mask);
3142 
3143     __ cmpl(keylen, 52);
3144     __ jccb(Assembler::equal, L_doLast);
3145 
3146     __ aesdec(xmm_result, xmm_temp1);
3147     __ aesdec(xmm_result, xmm_temp2);
3148 
3149     load_key(xmm_temp1, key, 0xd0, xmm_key_shuf_mask);
3150     load_key(xmm_temp2, key, 0xe0, xmm_key_shuf_mask);
3151 
3152     __ BIND(L_doLast);
3153     __ aesdec(xmm_result, xmm_temp1);
3154     __ aesdec(xmm_result, xmm_temp2);
3155 
3156     // for decryption the aesdeclast operation is always on key+0x00
3157     __ aesdeclast(xmm_result, xmm_temp3);
3158     __ movdqu(Address(to, 0), xmm_result);  // store the result
3159     __ xorptr(rax, rax); // return 0
3160     __ leave(); // required for proper stackwalking of RuntimeStub frame
3161     __ ret(0);
3162 
3163     return start;
3164   }
3165 
3166 
3167   // Arguments:
3168   //
3169   // Inputs:
3170   //   c_rarg0   - source byte array address
3171   //   c_rarg1   - destination byte array address
3172   //   c_rarg2   - K (key) in little endian int array
3173   //   c_rarg3   - r vector byte array address
3174   //   c_rarg4   - input length
3175   //
3176   // Output:
3177   //   rax       - input length
3178   //
3179   address generate_cipherBlockChaining_encryptAESCrypt() {
3180     assert(UseAES, "need AES instructions and misaligned SSE support");
3181     __ align(CodeEntryAlignment);
3182     StubCodeMark mark(this, "StubRoutines", "cipherBlockChaining_encryptAESCrypt");
3183     address start = __ pc();
3184 
3185     Label L_exit, L_key_192_256, L_key_256, L_loopTop_128, L_loopTop_192, L_loopTop_256;
3186     const Register from        = c_rarg0;  // source array address
3187     const Register to          = c_rarg1;  // destination array address
3188     const Register key         = c_rarg2;  // key array address
3189     const Register rvec        = c_rarg3;  // r byte array initialized from initvector array address
3190                                            // and left with the results of the last encryption block
3191 #ifndef _WIN64
3192     const Register len_reg     = c_rarg4;  // src len (must be multiple of blocksize 16)
3193 #else
3194     const Address  len_mem(rbp, 6 * wordSize);  // length is on stack on Win64
3195     const Register len_reg     = r11;      // pick the volatile windows register
3196 #endif
3197     const Register pos         = rax;
3198 
3199     // xmm register assignments for the loops below
3200     const XMMRegister xmm_result = xmm0;
3201     const XMMRegister xmm_temp   = xmm1;
3202     // keys 0-10 preloaded into xmm2-xmm12
3203     const int XMM_REG_NUM_KEY_FIRST = 2;
3204     const int XMM_REG_NUM_KEY_LAST  = 15;
3205     const XMMRegister xmm_key0   = as_XMMRegister(XMM_REG_NUM_KEY_FIRST);
3206     const XMMRegister xmm_key10  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+10);
3207     const XMMRegister xmm_key11  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+11);
3208     const XMMRegister xmm_key12  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+12);
3209     const XMMRegister xmm_key13  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+13);
3210 
3211     __ enter(); // required for proper stackwalking of RuntimeStub frame
3212 
3213 #ifdef _WIN64
3214     // on win64, fill len_reg from stack position
3215     __ movl(len_reg, len_mem);
3216 #else
3217     __ push(len_reg); // Save
3218 #endif
3219 
3220     const XMMRegister xmm_key_shuf_mask = xmm_temp;  // used temporarily to swap key bytes up front
3221     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
3222     // load up xmm regs xmm2 thru xmm12 with key 0x00 - 0xa0
3223     for (int rnum = XMM_REG_NUM_KEY_FIRST, offset = 0x00; rnum <= XMM_REG_NUM_KEY_FIRST+10; rnum++) {
3224       load_key(as_XMMRegister(rnum), key, offset, xmm_key_shuf_mask);
3225       offset += 0x10;
3226     }
3227     __ movdqu(xmm_result, Address(rvec, 0x00));   // initialize xmm_result with r vec
3228 
3229     // now split to different paths depending on the keylen (len in ints of AESCrypt.KLE array (52=192, or 60=256))
3230     __ movl(rax, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
3231     __ cmpl(rax, 44);
3232     __ jcc(Assembler::notEqual, L_key_192_256);
3233 
3234     // 128 bit code follows here
3235     __ movptr(pos, 0);
3236     __ align(OptoLoopAlignment);
3237 
3238     __ BIND(L_loopTop_128);
3239     __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of input
3240     __ pxor  (xmm_result, xmm_temp);               // xor with the current r vector
3241     __ pxor  (xmm_result, xmm_key0);               // do the aes rounds
3242     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_FIRST + 9; rnum++) {
3243       __ aesenc(xmm_result, as_XMMRegister(rnum));
3244     }
3245     __ aesenclast(xmm_result, xmm_key10);
3246     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);     // store into the next 16 bytes of output
3247     // no need to store r to memory until we exit
3248     __ addptr(pos, AESBlockSize);
3249     __ subptr(len_reg, AESBlockSize);
3250     __ jcc(Assembler::notEqual, L_loopTop_128);
3251 
3252     __ BIND(L_exit);
3253     __ movdqu(Address(rvec, 0), xmm_result);     // final value of r stored in rvec of CipherBlockChaining object
3254 
3255 #ifdef _WIN64
3256     __ movl(rax, len_mem);
3257 #else
3258     __ pop(rax); // return length
3259 #endif
3260     __ leave(); // required for proper stackwalking of RuntimeStub frame
3261     __ ret(0);
3262 
3263     __ BIND(L_key_192_256);
3264     // here rax = len in ints of AESCrypt.KLE array (52=192, or 60=256)
3265     load_key(xmm_key11, key, 0xb0, xmm_key_shuf_mask);
3266     load_key(xmm_key12, key, 0xc0, xmm_key_shuf_mask);
3267     __ cmpl(rax, 52);
3268     __ jcc(Assembler::notEqual, L_key_256);
3269 
3270     // 192-bit code follows here (could be changed to use more xmm registers)
3271     __ movptr(pos, 0);
3272     __ align(OptoLoopAlignment);
3273 
3274     __ BIND(L_loopTop_192);
3275     __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of input
3276     __ pxor  (xmm_result, xmm_temp);               // xor with the current r vector
3277     __ pxor  (xmm_result, xmm_key0);               // do the aes rounds
3278     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum  <= XMM_REG_NUM_KEY_FIRST + 11; rnum++) {
3279       __ aesenc(xmm_result, as_XMMRegister(rnum));
3280     }
3281     __ aesenclast(xmm_result, xmm_key12);
3282     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);     // store into the next 16 bytes of output
3283     // no need to store r to memory until we exit
3284     __ addptr(pos, AESBlockSize);
3285     __ subptr(len_reg, AESBlockSize);
3286     __ jcc(Assembler::notEqual, L_loopTop_192);
3287     __ jmp(L_exit);
3288 
3289     __ BIND(L_key_256);
3290     // 256-bit code follows here (could be changed to use more xmm registers)
3291     load_key(xmm_key13, key, 0xd0, xmm_key_shuf_mask);
3292     __ movptr(pos, 0);
3293     __ align(OptoLoopAlignment);
3294 
3295     __ BIND(L_loopTop_256);
3296     __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of input
3297     __ pxor  (xmm_result, xmm_temp);               // xor with the current r vector
3298     __ pxor  (xmm_result, xmm_key0);               // do the aes rounds
3299     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum  <= XMM_REG_NUM_KEY_FIRST + 13; rnum++) {
3300       __ aesenc(xmm_result, as_XMMRegister(rnum));
3301     }
3302     load_key(xmm_temp, key, 0xe0);
3303     __ aesenclast(xmm_result, xmm_temp);
3304     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);     // store into the next 16 bytes of output
3305     // no need to store r to memory until we exit
3306     __ addptr(pos, AESBlockSize);
3307     __ subptr(len_reg, AESBlockSize);
3308     __ jcc(Assembler::notEqual, L_loopTop_256);
3309     __ jmp(L_exit);
3310 
3311     return start;
3312   }
3313 
3314   // Safefetch stubs.
3315   void generate_safefetch(const char* name, int size, address* entry,
3316                           address* fault_pc, address* continuation_pc) {
3317     // safefetch signatures:
3318     //   int      SafeFetch32(int*      adr, int      errValue);
3319     //   intptr_t SafeFetchN (intptr_t* adr, intptr_t errValue);
3320     //
3321     // arguments:
3322     //   c_rarg0 = adr
3323     //   c_rarg1 = errValue
3324     //
3325     // result:
3326     //   PPC_RET  = *adr or errValue
3327 
3328     StubCodeMark mark(this, "StubRoutines", name);
3329 
3330     // Entry point, pc or function descriptor.
3331     *entry = __ pc();
3332 
3333     // Load *adr into c_rarg1, may fault.
3334     *fault_pc = __ pc();
3335     switch (size) {
3336       case 4:
3337         // int32_t
3338         __ movl(c_rarg1, Address(c_rarg0, 0));
3339         break;
3340       case 8:
3341         // int64_t
3342         __ movq(c_rarg1, Address(c_rarg0, 0));
3343         break;
3344       default:
3345         ShouldNotReachHere();
3346     }
3347 
3348     // return errValue or *adr
3349     *continuation_pc = __ pc();
3350     __ movq(rax, c_rarg1);
3351     __ ret(0);
3352   }
3353 
3354   // This is a version of CBC/AES Decrypt which does 4 blocks in a loop at a time
3355   // to hide instruction latency
3356   //
3357   // Arguments:
3358   //
3359   // Inputs:
3360   //   c_rarg0   - source byte array address
3361   //   c_rarg1   - destination byte array address
3362   //   c_rarg2   - K (key) in little endian int array
3363   //   c_rarg3   - r vector byte array address
3364   //   c_rarg4   - input length
3365   //
3366   // Output:
3367   //   rax       - input length
3368   //
3369   address generate_cipherBlockChaining_decryptAESCrypt_Parallel() {
3370     assert(UseAES, "need AES instructions and misaligned SSE support");
3371     __ align(CodeEntryAlignment);
3372     StubCodeMark mark(this, "StubRoutines", "cipherBlockChaining_decryptAESCrypt");
3373     address start = __ pc();
3374 
3375     const Register from        = c_rarg0;  // source array address
3376     const Register to          = c_rarg1;  // destination array address
3377     const Register key         = c_rarg2;  // key array address
3378     const Register rvec        = c_rarg3;  // r byte array initialized from initvector array address
3379                                            // and left with the results of the last encryption block
3380 #ifndef _WIN64
3381     const Register len_reg     = c_rarg4;  // src len (must be multiple of blocksize 16)
3382 #else
3383     const Address  len_mem(rbp, 6 * wordSize);  // length is on stack on Win64
3384     const Register len_reg     = r11;      // pick the volatile windows register
3385 #endif
3386     const Register pos         = rax;
3387 
3388     const int PARALLEL_FACTOR = 4;
3389     const int ROUNDS[3] = { 10, 12, 14 }; // aes rounds for key128, key192, key256
3390 
3391     Label L_exit;
3392     Label L_singleBlock_loopTopHead[3]; // 128, 192, 256
3393     Label L_singleBlock_loopTopHead2[3]; // 128, 192, 256
3394     Label L_singleBlock_loopTop[3]; // 128, 192, 256
3395     Label L_multiBlock_loopTopHead[3]; // 128, 192, 256
3396     Label L_multiBlock_loopTop[3]; // 128, 192, 256
3397 
3398     // keys 0-10 preloaded into xmm5-xmm15
3399     const int XMM_REG_NUM_KEY_FIRST = 5;
3400     const int XMM_REG_NUM_KEY_LAST  = 15;
3401     const XMMRegister xmm_key_first = as_XMMRegister(XMM_REG_NUM_KEY_FIRST);
3402     const XMMRegister xmm_key_last  = as_XMMRegister(XMM_REG_NUM_KEY_LAST);
3403 
3404     __ enter(); // required for proper stackwalking of RuntimeStub frame
3405 
3406 #ifdef _WIN64
3407     // on win64, fill len_reg from stack position
3408     __ movl(len_reg, len_mem);
3409 #else
3410     __ push(len_reg); // Save
3411 #endif
3412     __ push(rbx);
3413     // the java expanded key ordering is rotated one position from what we want
3414     // so we start from 0x10 here and hit 0x00 last
3415     const XMMRegister xmm_key_shuf_mask = xmm1;  // used temporarily to swap key bytes up front
3416     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
3417     // load up xmm regs 5 thru 15 with key 0x10 - 0xa0 - 0x00
3418     for (int rnum = XMM_REG_NUM_KEY_FIRST, offset = 0x10; rnum < XMM_REG_NUM_KEY_LAST; rnum++) {
3419       load_key(as_XMMRegister(rnum), key, offset, xmm_key_shuf_mask);
3420       offset += 0x10;
3421     }
3422     load_key(xmm_key_last, key, 0x00, xmm_key_shuf_mask);
3423 
3424     const XMMRegister xmm_prev_block_cipher = xmm1;  // holds cipher of previous block
3425 
3426     // registers holding the four results in the parallelized loop
3427     const XMMRegister xmm_result0 = xmm0;
3428     const XMMRegister xmm_result1 = xmm2;
3429     const XMMRegister xmm_result2 = xmm3;
3430     const XMMRegister xmm_result3 = xmm4;
3431 
3432     __ movdqu(xmm_prev_block_cipher, Address(rvec, 0x00));   // initialize with initial rvec
3433 
3434     __ xorptr(pos, pos);
3435 
3436     // now split to different paths depending on the keylen (len in ints of AESCrypt.KLE array (52=192, or 60=256))
3437     __ movl(rbx, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
3438     __ cmpl(rbx, 52);
3439     __ jcc(Assembler::equal, L_multiBlock_loopTopHead[1]);
3440     __ cmpl(rbx, 60);
3441     __ jcc(Assembler::equal, L_multiBlock_loopTopHead[2]);
3442 
3443 #define DoFour(opc, src_reg)           \
3444   __ opc(xmm_result0, src_reg);         \
3445   __ opc(xmm_result1, src_reg);         \
3446   __ opc(xmm_result2, src_reg);         \
3447   __ opc(xmm_result3, src_reg);         \
3448 
3449     for (int k = 0; k < 3; ++k) {
3450       __ BIND(L_multiBlock_loopTopHead[k]);
3451       if (k != 0) {
3452         __ cmpptr(len_reg, PARALLEL_FACTOR * AESBlockSize); // see if at least 4 blocks left
3453         __ jcc(Assembler::less, L_singleBlock_loopTopHead2[k]);
3454       }
3455       if (k == 1) {
3456         __ subptr(rsp, 6 * wordSize);
3457         __ movdqu(Address(rsp, 0), xmm15); //save last_key from xmm15
3458         load_key(xmm15, key, 0xb0); // 0xb0; 192-bit key goes up to 0xc0
3459         __ movdqu(Address(rsp, 2 * wordSize), xmm15);
3460         load_key(xmm1, key, 0xc0);  // 0xc0;
3461         __ movdqu(Address(rsp, 4 * wordSize), xmm1);
3462       } else if (k == 2) {
3463         __ subptr(rsp, 10 * wordSize);
3464         __ movdqu(Address(rsp, 0), xmm15); //save last_key from xmm15
3465         load_key(xmm15, key, 0xd0); // 0xd0; 256-bit key goes upto 0xe0
3466         __ movdqu(Address(rsp, 6 * wordSize), xmm15);
3467         load_key(xmm1, key, 0xe0);  // 0xe0;
3468         __ movdqu(Address(rsp, 8 * wordSize), xmm1);
3469         load_key(xmm15, key, 0xb0); // 0xb0;
3470         __ movdqu(Address(rsp, 2 * wordSize), xmm15);
3471         load_key(xmm1, key, 0xc0);  // 0xc0;
3472         __ movdqu(Address(rsp, 4 * wordSize), xmm1);
3473       }
3474       __ align(OptoLoopAlignment);
3475       __ BIND(L_multiBlock_loopTop[k]);
3476       __ cmpptr(len_reg, PARALLEL_FACTOR * AESBlockSize); // see if at least 4 blocks left
3477       __ jcc(Assembler::less, L_singleBlock_loopTopHead[k]);
3478 
3479       if  (k != 0) {
3480         __ movdqu(xmm15, Address(rsp, 2 * wordSize));
3481         __ movdqu(xmm1, Address(rsp, 4 * wordSize));
3482       }
3483 
3484       __ movdqu(xmm_result0, Address(from, pos, Address::times_1, 0 * AESBlockSize)); // get next 4 blocks into xmmresult registers
3485       __ movdqu(xmm_result1, Address(from, pos, Address::times_1, 1 * AESBlockSize));
3486       __ movdqu(xmm_result2, Address(from, pos, Address::times_1, 2 * AESBlockSize));
3487       __ movdqu(xmm_result3, Address(from, pos, Address::times_1, 3 * AESBlockSize));
3488 
3489       DoFour(pxor, xmm_key_first);
3490       if (k == 0) {
3491         for (int rnum = 1; rnum < ROUNDS[k]; rnum++) {
3492           DoFour(aesdec, as_XMMRegister(rnum + XMM_REG_NUM_KEY_FIRST));
3493         }
3494         DoFour(aesdeclast, xmm_key_last);
3495       } else if (k == 1) {
3496         for (int rnum = 1; rnum <= ROUNDS[k]-2; rnum++) {
3497           DoFour(aesdec, as_XMMRegister(rnum + XMM_REG_NUM_KEY_FIRST));
3498         }
3499         __ movdqu(xmm_key_last, Address(rsp, 0)); // xmm15 needs to be loaded again.
3500         DoFour(aesdec, xmm1);  // key : 0xc0
3501         __ movdqu(xmm_prev_block_cipher, Address(rvec, 0x00));  // xmm1 needs to be loaded again
3502         DoFour(aesdeclast, xmm_key_last);
3503       } else if (k == 2) {
3504         for (int rnum = 1; rnum <= ROUNDS[k] - 4; rnum++) {
3505           DoFour(aesdec, as_XMMRegister(rnum + XMM_REG_NUM_KEY_FIRST));
3506         }
3507         DoFour(aesdec, xmm1);  // key : 0xc0
3508         __ movdqu(xmm15, Address(rsp, 6 * wordSize));
3509         __ movdqu(xmm1, Address(rsp, 8 * wordSize));
3510         DoFour(aesdec, xmm15);  // key : 0xd0
3511         __ movdqu(xmm_key_last, Address(rsp, 0)); // xmm15 needs to be loaded again.
3512         DoFour(aesdec, xmm1);  // key : 0xe0
3513         __ movdqu(xmm_prev_block_cipher, Address(rvec, 0x00));  // xmm1 needs to be loaded again
3514         DoFour(aesdeclast, xmm_key_last);
3515       }
3516 
3517       // for each result, xor with the r vector of previous cipher block
3518       __ pxor(xmm_result0, xmm_prev_block_cipher);
3519       __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 0 * AESBlockSize));
3520       __ pxor(xmm_result1, xmm_prev_block_cipher);
3521       __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 1 * AESBlockSize));
3522       __ pxor(xmm_result2, xmm_prev_block_cipher);
3523       __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 2 * AESBlockSize));
3524       __ pxor(xmm_result3, xmm_prev_block_cipher);
3525       __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 3 * AESBlockSize));   // this will carry over to next set of blocks
3526       if (k != 0) {
3527         __ movdqu(Address(rvec, 0x00), xmm_prev_block_cipher);
3528       }
3529 
3530       __ movdqu(Address(to, pos, Address::times_1, 0 * AESBlockSize), xmm_result0);     // store 4 results into the next 64 bytes of output
3531       __ movdqu(Address(to, pos, Address::times_1, 1 * AESBlockSize), xmm_result1);
3532       __ movdqu(Address(to, pos, Address::times_1, 2 * AESBlockSize), xmm_result2);
3533       __ movdqu(Address(to, pos, Address::times_1, 3 * AESBlockSize), xmm_result3);
3534 
3535       __ addptr(pos, PARALLEL_FACTOR * AESBlockSize);
3536       __ subptr(len_reg, PARALLEL_FACTOR * AESBlockSize);
3537       __ jmp(L_multiBlock_loopTop[k]);
3538 
3539       // registers used in the non-parallelized loops
3540       // xmm register assignments for the loops below
3541       const XMMRegister xmm_result = xmm0;
3542       const XMMRegister xmm_prev_block_cipher_save = xmm2;
3543       const XMMRegister xmm_key11 = xmm3;
3544       const XMMRegister xmm_key12 = xmm4;
3545       const XMMRegister key_tmp = xmm4;
3546 
3547       __ BIND(L_singleBlock_loopTopHead[k]);
3548       if (k == 1) {
3549         __ addptr(rsp, 6 * wordSize);
3550       } else if (k == 2) {
3551         __ addptr(rsp, 10 * wordSize);
3552       }
3553       __ cmpptr(len_reg, 0); // any blocks left??
3554       __ jcc(Assembler::equal, L_exit);
3555       __ BIND(L_singleBlock_loopTopHead2[k]);
3556       if (k == 1) {
3557         load_key(xmm_key11, key, 0xb0); // 0xb0; 192-bit key goes upto 0xc0
3558         load_key(xmm_key12, key, 0xc0); // 0xc0; 192-bit key goes upto 0xc0
3559       }
3560       if (k == 2) {
3561         load_key(xmm_key11, key, 0xb0); // 0xb0; 256-bit key goes upto 0xe0
3562       }
3563       __ align(OptoLoopAlignment);
3564       __ BIND(L_singleBlock_loopTop[k]);
3565       __ movdqu(xmm_result, Address(from, pos, Address::times_1, 0)); // get next 16 bytes of cipher input
3566       __ movdqa(xmm_prev_block_cipher_save, xmm_result); // save for next r vector
3567       __ pxor(xmm_result, xmm_key_first); // do the aes dec rounds
3568       for (int rnum = 1; rnum <= 9 ; rnum++) {
3569           __ aesdec(xmm_result, as_XMMRegister(rnum + XMM_REG_NUM_KEY_FIRST));
3570       }
3571       if (k == 1) {
3572         __ aesdec(xmm_result, xmm_key11);
3573         __ aesdec(xmm_result, xmm_key12);
3574       }
3575       if (k == 2) {
3576         __ aesdec(xmm_result, xmm_key11);
3577         load_key(key_tmp, key, 0xc0);
3578         __ aesdec(xmm_result, key_tmp);
3579         load_key(key_tmp, key, 0xd0);
3580         __ aesdec(xmm_result, key_tmp);
3581         load_key(key_tmp, key, 0xe0);
3582         __ aesdec(xmm_result, key_tmp);
3583       }
3584 
3585       __ aesdeclast(xmm_result, xmm_key_last); // xmm15 always came from key+0
3586       __ pxor(xmm_result, xmm_prev_block_cipher); // xor with the current r vector
3587       __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result); // store into the next 16 bytes of output
3588       // no need to store r to memory until we exit
3589       __ movdqa(xmm_prev_block_cipher, xmm_prev_block_cipher_save); // set up next r vector with cipher input from this block
3590       __ addptr(pos, AESBlockSize);
3591       __ subptr(len_reg, AESBlockSize);
3592       __ jcc(Assembler::notEqual, L_singleBlock_loopTop[k]);
3593       if (k != 2) {
3594         __ jmp(L_exit);
3595       }
3596     } //for 128/192/256
3597 
3598     __ BIND(L_exit);
3599     __ movdqu(Address(rvec, 0), xmm_prev_block_cipher);     // final value of r stored in rvec of CipherBlockChaining object
3600     __ pop(rbx);
3601 #ifdef _WIN64
3602     __ movl(rax, len_mem);
3603 #else
3604     __ pop(rax); // return length
3605 #endif
3606     __ leave(); // required for proper stackwalking of RuntimeStub frame
3607     __ ret(0);
3608     return start;
3609 }
3610 
3611   address generate_upper_word_mask() {
3612     __ align(64);
3613     StubCodeMark mark(this, "StubRoutines", "upper_word_mask");
3614     address start = __ pc();
3615     __ emit_data64(0x0000000000000000, relocInfo::none);
3616     __ emit_data64(0xFFFFFFFF00000000, relocInfo::none);
3617     return start;
3618   }
3619 
3620   address generate_shuffle_byte_flip_mask() {
3621     __ align(64);
3622     StubCodeMark mark(this, "StubRoutines", "shuffle_byte_flip_mask");
3623     address start = __ pc();
3624     __ emit_data64(0x08090a0b0c0d0e0f, relocInfo::none);
3625     __ emit_data64(0x0001020304050607, relocInfo::none);
3626     return start;
3627   }
3628 
3629   // ofs and limit are use for multi-block byte array.
3630   // int com.sun.security.provider.DigestBase.implCompressMultiBlock(byte[] b, int ofs, int limit)
3631   address generate_sha1_implCompress(bool multi_block, const char *name) {
3632     __ align(CodeEntryAlignment);
3633     StubCodeMark mark(this, "StubRoutines", name);
3634     address start = __ pc();
3635 
3636     Register buf = c_rarg0;
3637     Register state = c_rarg1;
3638     Register ofs = c_rarg2;
3639     Register limit = c_rarg3;
3640 
3641     const XMMRegister abcd = xmm0;
3642     const XMMRegister e0 = xmm1;
3643     const XMMRegister e1 = xmm2;
3644     const XMMRegister msg0 = xmm3;
3645 
3646     const XMMRegister msg1 = xmm4;
3647     const XMMRegister msg2 = xmm5;
3648     const XMMRegister msg3 = xmm6;
3649     const XMMRegister shuf_mask = xmm7;
3650 
3651     __ enter();
3652 
3653     __ subptr(rsp, 4 * wordSize);
3654 
3655     __ fast_sha1(abcd, e0, e1, msg0, msg1, msg2, msg3, shuf_mask,
3656       buf, state, ofs, limit, rsp, multi_block);
3657 
3658     __ addptr(rsp, 4 * wordSize);
3659 
3660     __ leave();
3661     __ ret(0);
3662     return start;
3663   }
3664 
3665   address generate_pshuffle_byte_flip_mask() {
3666     __ align(64);
3667     StubCodeMark mark(this, "StubRoutines", "pshuffle_byte_flip_mask");
3668     address start = __ pc();
3669     __ emit_data64(0x0405060700010203, relocInfo::none);
3670     __ emit_data64(0x0c0d0e0f08090a0b, relocInfo::none);
3671 
3672     if (VM_Version::supports_avx2()) {
3673       __ emit_data64(0x0405060700010203, relocInfo::none); // second copy
3674       __ emit_data64(0x0c0d0e0f08090a0b, relocInfo::none);
3675       // _SHUF_00BA
3676       __ emit_data64(0x0b0a090803020100, relocInfo::none);
3677       __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
3678       __ emit_data64(0x0b0a090803020100, relocInfo::none);
3679       __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
3680       // _SHUF_DC00
3681       __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
3682       __ emit_data64(0x0b0a090803020100, relocInfo::none);
3683       __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
3684       __ emit_data64(0x0b0a090803020100, relocInfo::none);
3685     }
3686 
3687     return start;
3688   }
3689 
3690   //Mask for byte-swapping a couple of qwords in an XMM register using (v)pshufb.
3691   address generate_pshuffle_byte_flip_mask_sha512() {
3692     __ align(32);
3693     StubCodeMark mark(this, "StubRoutines", "pshuffle_byte_flip_mask_sha512");
3694     address start = __ pc();
3695     if (VM_Version::supports_avx2()) {
3696       __ emit_data64(0x0001020304050607, relocInfo::none); // PSHUFFLE_BYTE_FLIP_MASK
3697       __ emit_data64(0x08090a0b0c0d0e0f, relocInfo::none);
3698       __ emit_data64(0x1011121314151617, relocInfo::none);
3699       __ emit_data64(0x18191a1b1c1d1e1f, relocInfo::none);
3700       __ emit_data64(0x0000000000000000, relocInfo::none); //MASK_YMM_LO
3701       __ emit_data64(0x0000000000000000, relocInfo::none);
3702       __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
3703       __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
3704     }
3705 
3706     return start;
3707   }
3708 
3709 // ofs and limit are use for multi-block byte array.
3710 // int com.sun.security.provider.DigestBase.implCompressMultiBlock(byte[] b, int ofs, int limit)
3711   address generate_sha256_implCompress(bool multi_block, const char *name) {
3712     assert(VM_Version::supports_sha() || VM_Version::supports_avx2(), "");
3713     __ align(CodeEntryAlignment);
3714     StubCodeMark mark(this, "StubRoutines", name);
3715     address start = __ pc();
3716 
3717     Register buf = c_rarg0;
3718     Register state = c_rarg1;
3719     Register ofs = c_rarg2;
3720     Register limit = c_rarg3;
3721 
3722     const XMMRegister msg = xmm0;
3723     const XMMRegister state0 = xmm1;
3724     const XMMRegister state1 = xmm2;
3725     const XMMRegister msgtmp0 = xmm3;
3726 
3727     const XMMRegister msgtmp1 = xmm4;
3728     const XMMRegister msgtmp2 = xmm5;
3729     const XMMRegister msgtmp3 = xmm6;
3730     const XMMRegister msgtmp4 = xmm7;
3731 
3732     const XMMRegister shuf_mask = xmm8;
3733 
3734     __ enter();
3735 
3736     __ subptr(rsp, 4 * wordSize);
3737 
3738     if (VM_Version::supports_sha()) {
3739       __ fast_sha256(msg, state0, state1, msgtmp0, msgtmp1, msgtmp2, msgtmp3, msgtmp4,
3740         buf, state, ofs, limit, rsp, multi_block, shuf_mask);
3741     } else if (VM_Version::supports_avx2()) {
3742       __ sha256_AVX2(msg, state0, state1, msgtmp0, msgtmp1, msgtmp2, msgtmp3, msgtmp4,
3743         buf, state, ofs, limit, rsp, multi_block, shuf_mask);
3744     }
3745     __ addptr(rsp, 4 * wordSize);
3746     __ vzeroupper();
3747     __ leave();
3748     __ ret(0);
3749     return start;
3750   }
3751 
3752   address generate_sha512_implCompress(bool multi_block, const char *name) {
3753     assert(VM_Version::supports_avx2(), "");
3754     assert(VM_Version::supports_bmi2(), "");
3755     __ align(CodeEntryAlignment);
3756     StubCodeMark mark(this, "StubRoutines", name);
3757     address start = __ pc();
3758 
3759     Register buf = c_rarg0;
3760     Register state = c_rarg1;
3761     Register ofs = c_rarg2;
3762     Register limit = c_rarg3;
3763 
3764     const XMMRegister msg = xmm0;
3765     const XMMRegister state0 = xmm1;
3766     const XMMRegister state1 = xmm2;
3767     const XMMRegister msgtmp0 = xmm3;
3768     const XMMRegister msgtmp1 = xmm4;
3769     const XMMRegister msgtmp2 = xmm5;
3770     const XMMRegister msgtmp3 = xmm6;
3771     const XMMRegister msgtmp4 = xmm7;
3772 
3773     const XMMRegister shuf_mask = xmm8;
3774 
3775     __ enter();
3776 
3777     __ sha512_AVX2(msg, state0, state1, msgtmp0, msgtmp1, msgtmp2, msgtmp3, msgtmp4,
3778     buf, state, ofs, limit, rsp, multi_block, shuf_mask);
3779 
3780     __ vzeroupper();
3781     __ leave();
3782     __ ret(0);
3783     return start;
3784   }
3785 
3786   // This is a version of CTR/AES crypt which does 6 blocks in a loop at a time
3787   // to hide instruction latency
3788   //
3789   // Arguments:
3790   //
3791   // Inputs:
3792   //   c_rarg0   - source byte array address
3793   //   c_rarg1   - destination byte array address
3794   //   c_rarg2   - K (key) in little endian int array
3795   //   c_rarg3   - counter vector byte array address
3796   //   Linux
3797   //     c_rarg4   -          input length
3798   //     c_rarg5   -          saved encryptedCounter start
3799   //     rbp + 6 * wordSize - saved used length
3800   //   Windows
3801   //     rbp + 6 * wordSize - input length
3802   //     rbp + 7 * wordSize - saved encryptedCounter start
3803   //     rbp + 8 * wordSize - saved used length
3804   //
3805   // Output:
3806   //   rax       - input length
3807   //
3808   address generate_counterMode_AESCrypt_Parallel() {
3809     assert(UseAES, "need AES instructions and misaligned SSE support");
3810     __ align(CodeEntryAlignment);
3811     StubCodeMark mark(this, "StubRoutines", "counterMode_AESCrypt");
3812     address start = __ pc();
3813     const Register from = c_rarg0; // source array address
3814     const Register to = c_rarg1; // destination array address
3815     const Register key = c_rarg2; // key array address
3816     const Register counter = c_rarg3; // counter byte array initialized from counter array address
3817                                       // and updated with the incremented counter in the end
3818 #ifndef _WIN64
3819     const Register len_reg = c_rarg4;
3820     const Register saved_encCounter_start = c_rarg5;
3821     const Register used_addr = r10;
3822     const Address  used_mem(rbp, 2 * wordSize);
3823     const Register used = r11;
3824 #else
3825     const Address len_mem(rbp, 6 * wordSize); // length is on stack on Win64
3826     const Address saved_encCounter_mem(rbp, 7 * wordSize); // length is on stack on Win64
3827     const Address used_mem(rbp, 8 * wordSize); // length is on stack on Win64
3828     const Register len_reg = r10; // pick the first volatile windows register
3829     const Register saved_encCounter_start = r11;
3830     const Register used_addr = r13;
3831     const Register used = r14;
3832 #endif
3833     const Register pos = rax;
3834 
3835     const int PARALLEL_FACTOR = 6;
3836     const XMMRegister xmm_counter_shuf_mask = xmm0;
3837     const XMMRegister xmm_key_shuf_mask = xmm1; // used temporarily to swap key bytes up front
3838     const XMMRegister xmm_curr_counter = xmm2;
3839 
3840     const XMMRegister xmm_key_tmp0 = xmm3;
3841     const XMMRegister xmm_key_tmp1 = xmm4;
3842 
3843     // registers holding the four results in the parallelized loop
3844     const XMMRegister xmm_result0 = xmm5;
3845     const XMMRegister xmm_result1 = xmm6;
3846     const XMMRegister xmm_result2 = xmm7;
3847     const XMMRegister xmm_result3 = xmm8;
3848     const XMMRegister xmm_result4 = xmm9;
3849     const XMMRegister xmm_result5 = xmm10;
3850 
3851     const XMMRegister xmm_from0 = xmm11;
3852     const XMMRegister xmm_from1 = xmm12;
3853     const XMMRegister xmm_from2 = xmm13;
3854     const XMMRegister xmm_from3 = xmm14; //the last one is xmm14. we have to preserve it on WIN64.
3855     const XMMRegister xmm_from4 = xmm3; //reuse xmm3~4. Because xmm_key_tmp0~1 are useless when loading input text
3856     const XMMRegister xmm_from5 = xmm4;
3857 
3858     //for key_128, key_192, key_256
3859     const int rounds[3] = {10, 12, 14};
3860     Label L_exit_preLoop, L_preLoop_start;
3861     Label L_multiBlock_loopTop[3];
3862     Label L_singleBlockLoopTop[3];
3863     Label L__incCounter[3][6]; //for 6 blocks
3864     Label L__incCounter_single[3]; //for single block, key128, key192, key256
3865     Label L_processTail_insr[3], L_processTail_4_insr[3], L_processTail_2_insr[3], L_processTail_1_insr[3], L_processTail_exit_insr[3];
3866     Label L_processTail_4_extr[3], L_processTail_2_extr[3], L_processTail_1_extr[3], L_processTail_exit_extr[3];
3867 
3868     Label L_exit;
3869 
3870     __ enter(); // required for proper stackwalking of RuntimeStub frame
3871 
3872 #ifdef _WIN64
3873     // allocate spill slots for r13, r14
3874     enum {
3875         saved_r13_offset,
3876         saved_r14_offset
3877     };
3878     __ subptr(rsp, 2 * wordSize);
3879     __ movptr(Address(rsp, saved_r13_offset * wordSize), r13);
3880     __ movptr(Address(rsp, saved_r14_offset * wordSize), r14);
3881 
3882     // on win64, fill len_reg from stack position
3883     __ movl(len_reg, len_mem);
3884     __ movptr(saved_encCounter_start, saved_encCounter_mem);
3885     __ movptr(used_addr, used_mem);
3886     __ movl(used, Address(used_addr, 0));
3887 #else
3888     __ push(len_reg); // Save
3889     __ movptr(used_addr, used_mem);
3890     __ movl(used, Address(used_addr, 0));
3891 #endif
3892 
3893     __ push(rbx); // Save RBX
3894     __ movdqu(xmm_curr_counter, Address(counter, 0x00)); // initialize counter with initial counter
3895     __ movdqu(xmm_counter_shuf_mask, ExternalAddress(StubRoutines::x86::counter_shuffle_mask_addr()), pos); // pos as scratch
3896     __ pshufb(xmm_curr_counter, xmm_counter_shuf_mask); //counter is shuffled
3897     __ movptr(pos, 0);
3898 
3899     // Use the partially used encrpyted counter from last invocation
3900     __ BIND(L_preLoop_start);
3901     __ cmpptr(used, 16);
3902     __ jcc(Assembler::aboveEqual, L_exit_preLoop);
3903       __ cmpptr(len_reg, 0);
3904       __ jcc(Assembler::lessEqual, L_exit_preLoop);
3905       __ movb(rbx, Address(saved_encCounter_start, used));
3906       __ xorb(rbx, Address(from, pos));
3907       __ movb(Address(to, pos), rbx);
3908       __ addptr(pos, 1);
3909       __ addptr(used, 1);
3910       __ subptr(len_reg, 1);
3911 
3912     __ jmp(L_preLoop_start);
3913 
3914     __ BIND(L_exit_preLoop);
3915     __ movl(Address(used_addr, 0), used);
3916 
3917     // key length could be only {11, 13, 15} * 4 = {44, 52, 60}
3918     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()), rbx); // rbx as scratch
3919     __ movl(rbx, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
3920     __ cmpl(rbx, 52);
3921     __ jcc(Assembler::equal, L_multiBlock_loopTop[1]);
3922     __ cmpl(rbx, 60);
3923     __ jcc(Assembler::equal, L_multiBlock_loopTop[2]);
3924 
3925 #define CTR_DoSix(opc, src_reg)                \
3926     __ opc(xmm_result0, src_reg);              \
3927     __ opc(xmm_result1, src_reg);              \
3928     __ opc(xmm_result2, src_reg);              \
3929     __ opc(xmm_result3, src_reg);              \
3930     __ opc(xmm_result4, src_reg);              \
3931     __ opc(xmm_result5, src_reg);
3932 
3933     // k == 0 :  generate code for key_128
3934     // k == 1 :  generate code for key_192
3935     // k == 2 :  generate code for key_256
3936     for (int k = 0; k < 3; ++k) {
3937       //multi blocks starts here
3938       __ align(OptoLoopAlignment);
3939       __ BIND(L_multiBlock_loopTop[k]);
3940       __ cmpptr(len_reg, PARALLEL_FACTOR * AESBlockSize); // see if at least PARALLEL_FACTOR blocks left
3941       __ jcc(Assembler::less, L_singleBlockLoopTop[k]);
3942       load_key(xmm_key_tmp0, key, 0x00, xmm_key_shuf_mask);
3943 
3944       //load, then increase counters
3945       CTR_DoSix(movdqa, xmm_curr_counter);
3946       inc_counter(rbx, xmm_result1, 0x01, L__incCounter[k][0]);
3947       inc_counter(rbx, xmm_result2, 0x02, L__incCounter[k][1]);
3948       inc_counter(rbx, xmm_result3, 0x03, L__incCounter[k][2]);
3949       inc_counter(rbx, xmm_result4, 0x04, L__incCounter[k][3]);
3950       inc_counter(rbx, xmm_result5,  0x05, L__incCounter[k][4]);
3951       inc_counter(rbx, xmm_curr_counter, 0x06, L__incCounter[k][5]);
3952       CTR_DoSix(pshufb, xmm_counter_shuf_mask); // after increased, shuffled counters back for PXOR
3953       CTR_DoSix(pxor, xmm_key_tmp0);   //PXOR with Round 0 key
3954 
3955       //load two ROUND_KEYs at a time
3956       for (int i = 1; i < rounds[k]; ) {
3957         load_key(xmm_key_tmp1, key, (0x10 * i), xmm_key_shuf_mask);
3958         load_key(xmm_key_tmp0, key, (0x10 * (i+1)), xmm_key_shuf_mask);
3959         CTR_DoSix(aesenc, xmm_key_tmp1);
3960         i++;
3961         if (i != rounds[k]) {
3962           CTR_DoSix(aesenc, xmm_key_tmp0);
3963         } else {
3964           CTR_DoSix(aesenclast, xmm_key_tmp0);
3965         }
3966         i++;
3967       }
3968 
3969       // get next PARALLEL_FACTOR blocks into xmm_result registers
3970       __ movdqu(xmm_from0, Address(from, pos, Address::times_1, 0 * AESBlockSize));
3971       __ movdqu(xmm_from1, Address(from, pos, Address::times_1, 1 * AESBlockSize));
3972       __ movdqu(xmm_from2, Address(from, pos, Address::times_1, 2 * AESBlockSize));
3973       __ movdqu(xmm_from3, Address(from, pos, Address::times_1, 3 * AESBlockSize));
3974       __ movdqu(xmm_from4, Address(from, pos, Address::times_1, 4 * AESBlockSize));
3975       __ movdqu(xmm_from5, Address(from, pos, Address::times_1, 5 * AESBlockSize));
3976 
3977       __ pxor(xmm_result0, xmm_from0);
3978       __ pxor(xmm_result1, xmm_from1);
3979       __ pxor(xmm_result2, xmm_from2);
3980       __ pxor(xmm_result3, xmm_from3);
3981       __ pxor(xmm_result4, xmm_from4);
3982       __ pxor(xmm_result5, xmm_from5);
3983 
3984       // store 6 results into the next 64 bytes of output
3985       __ movdqu(Address(to, pos, Address::times_1, 0 * AESBlockSize), xmm_result0);
3986       __ movdqu(Address(to, pos, Address::times_1, 1 * AESBlockSize), xmm_result1);
3987       __ movdqu(Address(to, pos, Address::times_1, 2 * AESBlockSize), xmm_result2);
3988       __ movdqu(Address(to, pos, Address::times_1, 3 * AESBlockSize), xmm_result3);
3989       __ movdqu(Address(to, pos, Address::times_1, 4 * AESBlockSize), xmm_result4);
3990       __ movdqu(Address(to, pos, Address::times_1, 5 * AESBlockSize), xmm_result5);
3991 
3992       __ addptr(pos, PARALLEL_FACTOR * AESBlockSize); // increase the length of crypt text
3993       __ subptr(len_reg, PARALLEL_FACTOR * AESBlockSize); // decrease the remaining length
3994       __ jmp(L_multiBlock_loopTop[k]);
3995 
3996       // singleBlock starts here
3997       __ align(OptoLoopAlignment);
3998       __ BIND(L_singleBlockLoopTop[k]);
3999       __ cmpptr(len_reg, 0);
4000       __ jcc(Assembler::lessEqual, L_exit);
4001       load_key(xmm_key_tmp0, key, 0x00, xmm_key_shuf_mask);
4002       __ movdqa(xmm_result0, xmm_curr_counter);
4003       inc_counter(rbx, xmm_curr_counter, 0x01, L__incCounter_single[k]);
4004       __ pshufb(xmm_result0, xmm_counter_shuf_mask);
4005       __ pxor(xmm_result0, xmm_key_tmp0);
4006       for (int i = 1; i < rounds[k]; i++) {
4007         load_key(xmm_key_tmp0, key, (0x10 * i), xmm_key_shuf_mask);
4008         __ aesenc(xmm_result0, xmm_key_tmp0);
4009       }
4010       load_key(xmm_key_tmp0, key, (rounds[k] * 0x10), xmm_key_shuf_mask);
4011       __ aesenclast(xmm_result0, xmm_key_tmp0);
4012       __ cmpptr(len_reg, AESBlockSize);
4013       __ jcc(Assembler::less, L_processTail_insr[k]);
4014         __ movdqu(xmm_from0, Address(from, pos, Address::times_1, 0 * AESBlockSize));
4015         __ pxor(xmm_result0, xmm_from0);
4016         __ movdqu(Address(to, pos, Address::times_1, 0 * AESBlockSize), xmm_result0);
4017         __ addptr(pos, AESBlockSize);
4018         __ subptr(len_reg, AESBlockSize);
4019         __ jmp(L_singleBlockLoopTop[k]);
4020       __ BIND(L_processTail_insr[k]);                               // Process the tail part of the input array
4021         __ addptr(pos, len_reg);                                    // 1. Insert bytes from src array into xmm_from0 register
4022         __ testptr(len_reg, 8);
4023         __ jcc(Assembler::zero, L_processTail_4_insr[k]);
4024           __ subptr(pos,8);
4025           __ pinsrq(xmm_from0, Address(from, pos), 0);
4026         __ BIND(L_processTail_4_insr[k]);
4027         __ testptr(len_reg, 4);
4028         __ jcc(Assembler::zero, L_processTail_2_insr[k]);
4029           __ subptr(pos,4);
4030           __ pslldq(xmm_from0, 4);
4031           __ pinsrd(xmm_from0, Address(from, pos), 0);
4032         __ BIND(L_processTail_2_insr[k]);
4033         __ testptr(len_reg, 2);
4034         __ jcc(Assembler::zero, L_processTail_1_insr[k]);
4035           __ subptr(pos, 2);
4036           __ pslldq(xmm_from0, 2);
4037           __ pinsrw(xmm_from0, Address(from, pos), 0);
4038         __ BIND(L_processTail_1_insr[k]);
4039         __ testptr(len_reg, 1);
4040         __ jcc(Assembler::zero, L_processTail_exit_insr[k]);
4041           __ subptr(pos, 1);
4042           __ pslldq(xmm_from0, 1);
4043           __ pinsrb(xmm_from0, Address(from, pos), 0);
4044         __ BIND(L_processTail_exit_insr[k]);
4045 
4046         __ movdqu(Address(saved_encCounter_start, 0), xmm_result0);  // 2. Perform pxor of the encrypted counter and plaintext Bytes.
4047         __ pxor(xmm_result0, xmm_from0);                             //    Also the encrypted counter is saved for next invocation.
4048 
4049         __ testptr(len_reg, 8);
4050         __ jcc(Assembler::zero, L_processTail_4_extr[k]);            // 3. Extract bytes from xmm_result0 into the dest. array
4051           __ pextrq(Address(to, pos), xmm_result0, 0);
4052           __ psrldq(xmm_result0, 8);
4053           __ addptr(pos, 8);
4054         __ BIND(L_processTail_4_extr[k]);
4055         __ testptr(len_reg, 4);
4056         __ jcc(Assembler::zero, L_processTail_2_extr[k]);
4057           __ pextrd(Address(to, pos), xmm_result0, 0);
4058           __ psrldq(xmm_result0, 4);
4059           __ addptr(pos, 4);
4060         __ BIND(L_processTail_2_extr[k]);
4061         __ testptr(len_reg, 2);
4062         __ jcc(Assembler::zero, L_processTail_1_extr[k]);
4063           __ pextrw(Address(to, pos), xmm_result0, 0);
4064           __ psrldq(xmm_result0, 2);
4065           __ addptr(pos, 2);
4066         __ BIND(L_processTail_1_extr[k]);
4067         __ testptr(len_reg, 1);
4068         __ jcc(Assembler::zero, L_processTail_exit_extr[k]);
4069           __ pextrb(Address(to, pos), xmm_result0, 0);
4070 
4071         __ BIND(L_processTail_exit_extr[k]);
4072         __ movl(Address(used_addr, 0), len_reg);
4073         __ jmp(L_exit);
4074 
4075     }
4076 
4077     __ BIND(L_exit);
4078     __ pshufb(xmm_curr_counter, xmm_counter_shuf_mask); //counter is shuffled back.
4079     __ movdqu(Address(counter, 0), xmm_curr_counter); //save counter back
4080     __ pop(rbx); // pop the saved RBX.
4081 #ifdef _WIN64
4082     __ movl(rax, len_mem);
4083     __ movptr(r13, Address(rsp, saved_r13_offset * wordSize));
4084     __ movptr(r14, Address(rsp, saved_r14_offset * wordSize));
4085     __ addptr(rsp, 2 * wordSize);
4086 #else
4087     __ pop(rax); // return 'len'
4088 #endif
4089     __ leave(); // required for proper stackwalking of RuntimeStub frame
4090     __ ret(0);
4091     return start;
4092   }
4093 
4094 void roundDec(XMMRegister xmm_reg) {
4095   __ vaesdec(xmm1, xmm1, xmm_reg, Assembler::AVX_512bit);
4096   __ vaesdec(xmm2, xmm2, xmm_reg, Assembler::AVX_512bit);
4097   __ vaesdec(xmm3, xmm3, xmm_reg, Assembler::AVX_512bit);
4098   __ vaesdec(xmm4, xmm4, xmm_reg, Assembler::AVX_512bit);
4099   __ vaesdec(xmm5, xmm5, xmm_reg, Assembler::AVX_512bit);
4100   __ vaesdec(xmm6, xmm6, xmm_reg, Assembler::AVX_512bit);
4101   __ vaesdec(xmm7, xmm7, xmm_reg, Assembler::AVX_512bit);
4102   __ vaesdec(xmm8, xmm8, xmm_reg, Assembler::AVX_512bit);
4103 }
4104 
4105 void roundDeclast(XMMRegister xmm_reg) {
4106   __ vaesdeclast(xmm1, xmm1, xmm_reg, Assembler::AVX_512bit);
4107   __ vaesdeclast(xmm2, xmm2, xmm_reg, Assembler::AVX_512bit);
4108   __ vaesdeclast(xmm3, xmm3, xmm_reg, Assembler::AVX_512bit);
4109   __ vaesdeclast(xmm4, xmm4, xmm_reg, Assembler::AVX_512bit);
4110   __ vaesdeclast(xmm5, xmm5, xmm_reg, Assembler::AVX_512bit);
4111   __ vaesdeclast(xmm6, xmm6, xmm_reg, Assembler::AVX_512bit);
4112   __ vaesdeclast(xmm7, xmm7, xmm_reg, Assembler::AVX_512bit);
4113   __ vaesdeclast(xmm8, xmm8, xmm_reg, Assembler::AVX_512bit);
4114 }
4115 
4116   void ev_load_key(XMMRegister xmmdst, Register key, int offset, XMMRegister xmm_shuf_mask = NULL) {
4117     __ movdqu(xmmdst, Address(key, offset));
4118     if (xmm_shuf_mask != NULL) {
4119       __ pshufb(xmmdst, xmm_shuf_mask);
4120     } else {
4121       __ pshufb(xmmdst, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
4122     }
4123     __ evshufi64x2(xmmdst, xmmdst, xmmdst, 0x0, Assembler::AVX_512bit);
4124 
4125   }
4126 
4127 address generate_cipherBlockChaining_decryptVectorAESCrypt() {
4128     assert(VM_Version::supports_vaes(), "need AES instructions and misaligned SSE support");
4129     __ align(CodeEntryAlignment);
4130     StubCodeMark mark(this, "StubRoutines", "cipherBlockChaining_decryptAESCrypt");
4131     address start = __ pc();
4132 
4133     const Register from = c_rarg0;  // source array address
4134     const Register to = c_rarg1;  // destination array address
4135     const Register key = c_rarg2;  // key array address
4136     const Register rvec = c_rarg3;  // r byte array initialized from initvector array address
4137     // and left with the results of the last encryption block
4138 #ifndef _WIN64
4139     const Register len_reg = c_rarg4;  // src len (must be multiple of blocksize 16)
4140 #else
4141     const Address  len_mem(rbp, 6 * wordSize);  // length is on stack on Win64
4142     const Register len_reg = r11;      // pick the volatile windows register
4143 #endif
4144 
4145     Label Loop, Loop1, L_128, L_256, L_192, KEY_192, KEY_256, Loop2, Lcbc_dec_rem_loop,
4146           Lcbc_dec_rem_last, Lcbc_dec_ret, Lcbc_dec_rem, Lcbc_exit;
4147 
4148     __ enter();
4149 
4150 #ifdef _WIN64
4151   // on win64, fill len_reg from stack position
4152     __ movl(len_reg, len_mem);
4153 #else
4154     __ push(len_reg); // Save
4155 #endif
4156     __ push(rbx);
4157     __ vzeroupper();
4158 
4159     // Temporary variable declaration for swapping key bytes
4160     const XMMRegister xmm_key_shuf_mask = xmm1;
4161     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
4162 
4163     // Calculate number of rounds from key size: 44 for 10-rounds, 52 for 12-rounds, 60 for 14-rounds
4164     const Register rounds = rbx;
4165     __ movl(rounds, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
4166 
4167     const XMMRegister IV = xmm0;
4168     // Load IV and broadcast value to 512-bits
4169     __ evbroadcasti64x2(IV, Address(rvec, 0), Assembler::AVX_512bit);
4170 
4171     // Temporary variables for storing round keys
4172     const XMMRegister RK0 = xmm30;
4173     const XMMRegister RK1 = xmm9;
4174     const XMMRegister RK2 = xmm18;
4175     const XMMRegister RK3 = xmm19;
4176     const XMMRegister RK4 = xmm20;
4177     const XMMRegister RK5 = xmm21;
4178     const XMMRegister RK6 = xmm22;
4179     const XMMRegister RK7 = xmm23;
4180     const XMMRegister RK8 = xmm24;
4181     const XMMRegister RK9 = xmm25;
4182     const XMMRegister RK10 = xmm26;
4183 
4184      // Load and shuffle key
4185     // the java expanded key ordering is rotated one position from what we want
4186     // so we start from 1*16 here and hit 0*16 last
4187     ev_load_key(RK1, key, 1 * 16, xmm_key_shuf_mask);
4188     ev_load_key(RK2, key, 2 * 16, xmm_key_shuf_mask);
4189     ev_load_key(RK3, key, 3 * 16, xmm_key_shuf_mask);
4190     ev_load_key(RK4, key, 4 * 16, xmm_key_shuf_mask);
4191     ev_load_key(RK5, key, 5 * 16, xmm_key_shuf_mask);
4192     ev_load_key(RK6, key, 6 * 16, xmm_key_shuf_mask);
4193     ev_load_key(RK7, key, 7 * 16, xmm_key_shuf_mask);
4194     ev_load_key(RK8, key, 8 * 16, xmm_key_shuf_mask);
4195     ev_load_key(RK9, key, 9 * 16, xmm_key_shuf_mask);
4196     ev_load_key(RK10, key, 10 * 16, xmm_key_shuf_mask);
4197     ev_load_key(RK0, key, 0*16, xmm_key_shuf_mask);
4198 
4199     // Variables for storing source cipher text
4200     const XMMRegister S0 = xmm10;
4201     const XMMRegister S1 = xmm11;
4202     const XMMRegister S2 = xmm12;
4203     const XMMRegister S3 = xmm13;
4204     const XMMRegister S4 = xmm14;
4205     const XMMRegister S5 = xmm15;
4206     const XMMRegister S6 = xmm16;
4207     const XMMRegister S7 = xmm17;
4208 
4209     // Variables for storing decrypted text
4210     const XMMRegister B0 = xmm1;
4211     const XMMRegister B1 = xmm2;
4212     const XMMRegister B2 = xmm3;
4213     const XMMRegister B3 = xmm4;
4214     const XMMRegister B4 = xmm5;
4215     const XMMRegister B5 = xmm6;
4216     const XMMRegister B6 = xmm7;
4217     const XMMRegister B7 = xmm8;
4218 
4219     __ cmpl(rounds, 44);
4220     __ jcc(Assembler::greater, KEY_192);
4221     __ jmp(Loop);
4222 
4223     __ BIND(KEY_192);
4224     const XMMRegister RK11 = xmm27;
4225     const XMMRegister RK12 = xmm28;
4226     ev_load_key(RK11, key, 11*16, xmm_key_shuf_mask);
4227     ev_load_key(RK12, key, 12*16, xmm_key_shuf_mask);
4228 
4229     __ cmpl(rounds, 52);
4230     __ jcc(Assembler::greater, KEY_256);
4231     __ jmp(Loop);
4232 
4233     __ BIND(KEY_256);
4234     const XMMRegister RK13 = xmm29;
4235     const XMMRegister RK14 = xmm31;
4236     ev_load_key(RK13, key, 13*16, xmm_key_shuf_mask);
4237     ev_load_key(RK14, key, 14*16, xmm_key_shuf_mask);
4238 
4239     __ BIND(Loop);
4240     __ cmpl(len_reg, 512);
4241     __ jcc(Assembler::below, Lcbc_dec_rem);
4242     __ BIND(Loop1);
4243     __ subl(len_reg, 512);
4244     __ evmovdquq(S0, Address(from, 0 * 64), Assembler::AVX_512bit);
4245     __ evmovdquq(S1, Address(from, 1 * 64), Assembler::AVX_512bit);
4246     __ evmovdquq(S2, Address(from, 2 * 64), Assembler::AVX_512bit);
4247     __ evmovdquq(S3, Address(from, 3 * 64), Assembler::AVX_512bit);
4248     __ evmovdquq(S4, Address(from, 4 * 64), Assembler::AVX_512bit);
4249     __ evmovdquq(S5, Address(from, 5 * 64), Assembler::AVX_512bit);
4250     __ evmovdquq(S6, Address(from, 6 * 64), Assembler::AVX_512bit);
4251     __ evmovdquq(S7, Address(from, 7 * 64), Assembler::AVX_512bit);
4252     __ leaq(from, Address(from, 8 * 64));
4253 
4254     __ evpxorq(B0, S0, RK1, Assembler::AVX_512bit);
4255     __ evpxorq(B1, S1, RK1, Assembler::AVX_512bit);
4256     __ evpxorq(B2, S2, RK1, Assembler::AVX_512bit);
4257     __ evpxorq(B3, S3, RK1, Assembler::AVX_512bit);
4258     __ evpxorq(B4, S4, RK1, Assembler::AVX_512bit);
4259     __ evpxorq(B5, S5, RK1, Assembler::AVX_512bit);
4260     __ evpxorq(B6, S6, RK1, Assembler::AVX_512bit);
4261     __ evpxorq(B7, S7, RK1, Assembler::AVX_512bit);
4262 
4263     __ evalignq(IV, S0, IV, 0x06);
4264     __ evalignq(S0, S1, S0, 0x06);
4265     __ evalignq(S1, S2, S1, 0x06);
4266     __ evalignq(S2, S3, S2, 0x06);
4267     __ evalignq(S3, S4, S3, 0x06);
4268     __ evalignq(S4, S5, S4, 0x06);
4269     __ evalignq(S5, S6, S5, 0x06);
4270     __ evalignq(S6, S7, S6, 0x06);
4271 
4272     roundDec(RK2);
4273     roundDec(RK3);
4274     roundDec(RK4);
4275     roundDec(RK5);
4276     roundDec(RK6);
4277     roundDec(RK7);
4278     roundDec(RK8);
4279     roundDec(RK9);
4280     roundDec(RK10);
4281 
4282     __ cmpl(rounds, 44);
4283     __ jcc(Assembler::belowEqual, L_128);
4284     roundDec(RK11);
4285     roundDec(RK12);
4286 
4287     __ cmpl(rounds, 52);
4288     __ jcc(Assembler::belowEqual, L_192);
4289     roundDec(RK13);
4290     roundDec(RK14);
4291 
4292     __ BIND(L_256);
4293     roundDeclast(RK0);
4294     __ jmp(Loop2);
4295 
4296     __ BIND(L_128);
4297     roundDeclast(RK0);
4298     __ jmp(Loop2);
4299 
4300     __ BIND(L_192);
4301     roundDeclast(RK0);
4302 
4303     __ BIND(Loop2);
4304     __ evpxorq(B0, B0, IV, Assembler::AVX_512bit);
4305     __ evpxorq(B1, B1, S0, Assembler::AVX_512bit);
4306     __ evpxorq(B2, B2, S1, Assembler::AVX_512bit);
4307     __ evpxorq(B3, B3, S2, Assembler::AVX_512bit);
4308     __ evpxorq(B4, B4, S3, Assembler::AVX_512bit);
4309     __ evpxorq(B5, B5, S4, Assembler::AVX_512bit);
4310     __ evpxorq(B6, B6, S5, Assembler::AVX_512bit);
4311     __ evpxorq(B7, B7, S6, Assembler::AVX_512bit);
4312     __ evmovdquq(IV, S7, Assembler::AVX_512bit);
4313 
4314     __ evmovdquq(Address(to, 0 * 64), B0, Assembler::AVX_512bit);
4315     __ evmovdquq(Address(to, 1 * 64), B1, Assembler::AVX_512bit);
4316     __ evmovdquq(Address(to, 2 * 64), B2, Assembler::AVX_512bit);
4317     __ evmovdquq(Address(to, 3 * 64), B3, Assembler::AVX_512bit);
4318     __ evmovdquq(Address(to, 4 * 64), B4, Assembler::AVX_512bit);
4319     __ evmovdquq(Address(to, 5 * 64), B5, Assembler::AVX_512bit);
4320     __ evmovdquq(Address(to, 6 * 64), B6, Assembler::AVX_512bit);
4321     __ evmovdquq(Address(to, 7 * 64), B7, Assembler::AVX_512bit);
4322     __ leaq(to, Address(to, 8 * 64));
4323     __ jmp(Loop);
4324 
4325     __ BIND(Lcbc_dec_rem);
4326     __ evshufi64x2(IV, IV, IV, 0x03, Assembler::AVX_512bit);
4327 
4328     __ BIND(Lcbc_dec_rem_loop);
4329     __ subl(len_reg, 16);
4330     __ jcc(Assembler::carrySet, Lcbc_dec_ret);
4331 
4332     __ movdqu(S0, Address(from, 0));
4333     __ evpxorq(B0, S0, RK1, Assembler::AVX_512bit);
4334     __ vaesdec(B0, B0, RK2, Assembler::AVX_512bit);
4335     __ vaesdec(B0, B0, RK3, Assembler::AVX_512bit);
4336     __ vaesdec(B0, B0, RK4, Assembler::AVX_512bit);
4337     __ vaesdec(B0, B0, RK5, Assembler::AVX_512bit);
4338     __ vaesdec(B0, B0, RK6, Assembler::AVX_512bit);
4339     __ vaesdec(B0, B0, RK7, Assembler::AVX_512bit);
4340     __ vaesdec(B0, B0, RK8, Assembler::AVX_512bit);
4341     __ vaesdec(B0, B0, RK9, Assembler::AVX_512bit);
4342     __ vaesdec(B0, B0, RK10, Assembler::AVX_512bit);
4343     __ cmpl(rounds, 44);
4344     __ jcc(Assembler::belowEqual, Lcbc_dec_rem_last);
4345 
4346     __ vaesdec(B0, B0, RK11, Assembler::AVX_512bit);
4347     __ vaesdec(B0, B0, RK12, Assembler::AVX_512bit);
4348     __ cmpl(rounds, 52);
4349     __ jcc(Assembler::belowEqual, Lcbc_dec_rem_last);
4350 
4351     __ vaesdec(B0, B0, RK13, Assembler::AVX_512bit);
4352     __ vaesdec(B0, B0, RK14, Assembler::AVX_512bit);
4353 
4354     __ BIND(Lcbc_dec_rem_last);
4355     __ vaesdeclast(B0, B0, RK0, Assembler::AVX_512bit);
4356 
4357     __ evpxorq(B0, B0, IV, Assembler::AVX_512bit);
4358     __ evmovdquq(IV, S0, Assembler::AVX_512bit);
4359     __ movdqu(Address(to, 0), B0);
4360     __ leaq(from, Address(from, 16));
4361     __ leaq(to, Address(to, 16));
4362     __ jmp(Lcbc_dec_rem_loop);
4363 
4364     __ BIND(Lcbc_dec_ret);
4365     __ movdqu(Address(rvec, 0), IV);
4366 
4367     // Zero out the round keys
4368     __ evpxorq(RK0, RK0, RK0, Assembler::AVX_512bit);
4369     __ evpxorq(RK1, RK1, RK1, Assembler::AVX_512bit);
4370     __ evpxorq(RK2, RK2, RK2, Assembler::AVX_512bit);
4371     __ evpxorq(RK3, RK3, RK3, Assembler::AVX_512bit);
4372     __ evpxorq(RK4, RK4, RK4, Assembler::AVX_512bit);
4373     __ evpxorq(RK5, RK5, RK5, Assembler::AVX_512bit);
4374     __ evpxorq(RK6, RK6, RK6, Assembler::AVX_512bit);
4375     __ evpxorq(RK7, RK7, RK7, Assembler::AVX_512bit);
4376     __ evpxorq(RK8, RK8, RK8, Assembler::AVX_512bit);
4377     __ evpxorq(RK9, RK9, RK9, Assembler::AVX_512bit);
4378     __ evpxorq(RK10, RK10, RK10, Assembler::AVX_512bit);
4379     __ cmpl(rounds, 44);
4380     __ jcc(Assembler::belowEqual, Lcbc_exit);
4381     __ evpxorq(RK11, RK11, RK11, Assembler::AVX_512bit);
4382     __ evpxorq(RK12, RK12, RK12, Assembler::AVX_512bit);
4383     __ cmpl(rounds, 52);
4384     __ jcc(Assembler::belowEqual, Lcbc_exit);
4385     __ evpxorq(RK13, RK13, RK13, Assembler::AVX_512bit);
4386     __ evpxorq(RK14, RK14, RK14, Assembler::AVX_512bit);
4387 
4388     __ BIND(Lcbc_exit);
4389     __ pop(rbx);
4390 #ifdef _WIN64
4391     __ movl(rax, len_mem);
4392 #else
4393     __ pop(rax); // return length
4394 #endif
4395     __ leave(); // required for proper stackwalking of RuntimeStub frame
4396     __ ret(0);
4397     return start;
4398 }
4399 
4400 // Polynomial x^128+x^127+x^126+x^121+1
4401 address ghash_polynomial_addr() {
4402     __ align(CodeEntryAlignment);
4403     StubCodeMark mark(this, "StubRoutines", "_ghash_poly_addr");
4404     address start = __ pc();
4405     __ emit_data64(0x0000000000000001, relocInfo::none);
4406     __ emit_data64(0xc200000000000000, relocInfo::none);
4407     return start;
4408 }
4409 
4410 address ghash_shufflemask_addr() {
4411     __ align(CodeEntryAlignment);
4412     StubCodeMark mark(this, "StubRoutines", "_ghash_shuffmask_addr");
4413     address start = __ pc();
4414     __ emit_data64(0x0f0f0f0f0f0f0f0f, relocInfo::none);
4415     __ emit_data64(0x0f0f0f0f0f0f0f0f, relocInfo::none);
4416     return start;
4417 }
4418 
4419 // Ghash single and multi block operations using AVX instructions
4420 address generate_avx_ghash_processBlocks() {
4421     __ align(CodeEntryAlignment);
4422 
4423     StubCodeMark mark(this, "StubRoutines", "ghash_processBlocks");
4424     address start = __ pc();
4425 
4426     // arguments
4427     const Register state = c_rarg0;
4428     const Register htbl = c_rarg1;
4429     const Register data = c_rarg2;
4430     const Register blocks = c_rarg3;
4431     __ enter();
4432    // Save state before entering routine
4433     __ avx_ghash(state, htbl, data, blocks);
4434     __ leave(); // required for proper stackwalking of RuntimeStub frame
4435     __ ret(0);
4436     return start;
4437 }
4438 
4439   // byte swap x86 long
4440   address generate_ghash_long_swap_mask() {
4441     __ align(CodeEntryAlignment);
4442     StubCodeMark mark(this, "StubRoutines", "ghash_long_swap_mask");
4443     address start = __ pc();
4444     __ emit_data64(0x0f0e0d0c0b0a0908, relocInfo::none );
4445     __ emit_data64(0x0706050403020100, relocInfo::none );
4446   return start;
4447   }
4448 
4449   // byte swap x86 byte array
4450   address generate_ghash_byte_swap_mask() {
4451     __ align(CodeEntryAlignment);
4452     StubCodeMark mark(this, "StubRoutines", "ghash_byte_swap_mask");
4453     address start = __ pc();
4454     __ emit_data64(0x08090a0b0c0d0e0f, relocInfo::none );
4455     __ emit_data64(0x0001020304050607, relocInfo::none );
4456   return start;
4457   }
4458 
4459   /* Single and multi-block ghash operations */
4460   address generate_ghash_processBlocks() {
4461     __ align(CodeEntryAlignment);
4462     Label L_ghash_loop, L_exit;
4463     StubCodeMark mark(this, "StubRoutines", "ghash_processBlocks");
4464     address start = __ pc();
4465 
4466     const Register state        = c_rarg0;
4467     const Register subkeyH      = c_rarg1;
4468     const Register data         = c_rarg2;
4469     const Register blocks       = c_rarg3;
4470 
4471     const XMMRegister xmm_temp0 = xmm0;
4472     const XMMRegister xmm_temp1 = xmm1;
4473     const XMMRegister xmm_temp2 = xmm2;
4474     const XMMRegister xmm_temp3 = xmm3;
4475     const XMMRegister xmm_temp4 = xmm4;
4476     const XMMRegister xmm_temp5 = xmm5;
4477     const XMMRegister xmm_temp6 = xmm6;
4478     const XMMRegister xmm_temp7 = xmm7;
4479     const XMMRegister xmm_temp8 = xmm8;
4480     const XMMRegister xmm_temp9 = xmm9;
4481     const XMMRegister xmm_temp10 = xmm10;
4482 
4483     __ enter();
4484 
4485     __ movdqu(xmm_temp10, ExternalAddress(StubRoutines::x86::ghash_long_swap_mask_addr()));
4486 
4487     __ movdqu(xmm_temp0, Address(state, 0));
4488     __ pshufb(xmm_temp0, xmm_temp10);
4489 
4490 
4491     __ BIND(L_ghash_loop);
4492     __ movdqu(xmm_temp2, Address(data, 0));
4493     __ pshufb(xmm_temp2, ExternalAddress(StubRoutines::x86::ghash_byte_swap_mask_addr()));
4494 
4495     __ movdqu(xmm_temp1, Address(subkeyH, 0));
4496     __ pshufb(xmm_temp1, xmm_temp10);
4497 
4498     __ pxor(xmm_temp0, xmm_temp2);
4499 
4500     //
4501     // Multiply with the hash key
4502     //
4503     __ movdqu(xmm_temp3, xmm_temp0);
4504     __ pclmulqdq(xmm_temp3, xmm_temp1, 0);      // xmm3 holds a0*b0
4505     __ movdqu(xmm_temp4, xmm_temp0);
4506     __ pclmulqdq(xmm_temp4, xmm_temp1, 16);     // xmm4 holds a0*b1
4507 
4508     __ movdqu(xmm_temp5, xmm_temp0);
4509     __ pclmulqdq(xmm_temp5, xmm_temp1, 1);      // xmm5 holds a1*b0
4510     __ movdqu(xmm_temp6, xmm_temp0);
4511     __ pclmulqdq(xmm_temp6, xmm_temp1, 17);     // xmm6 holds a1*b1
4512 
4513     __ pxor(xmm_temp4, xmm_temp5);      // xmm4 holds a0*b1 + a1*b0
4514 
4515     __ movdqu(xmm_temp5, xmm_temp4);    // move the contents of xmm4 to xmm5
4516     __ psrldq(xmm_temp4, 8);    // shift by xmm4 64 bits to the right
4517     __ pslldq(xmm_temp5, 8);    // shift by xmm5 64 bits to the left
4518     __ pxor(xmm_temp3, xmm_temp5);
4519     __ pxor(xmm_temp6, xmm_temp4);      // Register pair <xmm6:xmm3> holds the result
4520                                         // of the carry-less multiplication of
4521                                         // xmm0 by xmm1.
4522 
4523     // We shift the result of the multiplication by one bit position
4524     // to the left to cope for the fact that the bits are reversed.
4525     __ movdqu(xmm_temp7, xmm_temp3);
4526     __ movdqu(xmm_temp8, xmm_temp6);
4527     __ pslld(xmm_temp3, 1);
4528     __ pslld(xmm_temp6, 1);
4529     __ psrld(xmm_temp7, 31);
4530     __ psrld(xmm_temp8, 31);
4531     __ movdqu(xmm_temp9, xmm_temp7);
4532     __ pslldq(xmm_temp8, 4);
4533     __ pslldq(xmm_temp7, 4);
4534     __ psrldq(xmm_temp9, 12);
4535     __ por(xmm_temp3, xmm_temp7);
4536     __ por(xmm_temp6, xmm_temp8);
4537     __ por(xmm_temp6, xmm_temp9);
4538 
4539     //
4540     // First phase of the reduction
4541     //
4542     // Move xmm3 into xmm7, xmm8, xmm9 in order to perform the shifts
4543     // independently.
4544     __ movdqu(xmm_temp7, xmm_temp3);
4545     __ movdqu(xmm_temp8, xmm_temp3);
4546     __ movdqu(xmm_temp9, xmm_temp3);
4547     __ pslld(xmm_temp7, 31);    // packed right shift shifting << 31
4548     __ pslld(xmm_temp8, 30);    // packed right shift shifting << 30
4549     __ pslld(xmm_temp9, 25);    // packed right shift shifting << 25
4550     __ pxor(xmm_temp7, xmm_temp8);      // xor the shifted versions
4551     __ pxor(xmm_temp7, xmm_temp9);
4552     __ movdqu(xmm_temp8, xmm_temp7);
4553     __ pslldq(xmm_temp7, 12);
4554     __ psrldq(xmm_temp8, 4);
4555     __ pxor(xmm_temp3, xmm_temp7);      // first phase of the reduction complete
4556 
4557     //
4558     // Second phase of the reduction
4559     //
4560     // Make 3 copies of xmm3 in xmm2, xmm4, xmm5 for doing these
4561     // shift operations.
4562     __ movdqu(xmm_temp2, xmm_temp3);
4563     __ movdqu(xmm_temp4, xmm_temp3);
4564     __ movdqu(xmm_temp5, xmm_temp3);
4565     __ psrld(xmm_temp2, 1);     // packed left shifting >> 1
4566     __ psrld(xmm_temp4, 2);     // packed left shifting >> 2
4567     __ psrld(xmm_temp5, 7);     // packed left shifting >> 7
4568     __ pxor(xmm_temp2, xmm_temp4);      // xor the shifted versions
4569     __ pxor(xmm_temp2, xmm_temp5);
4570     __ pxor(xmm_temp2, xmm_temp8);
4571     __ pxor(xmm_temp3, xmm_temp2);
4572     __ pxor(xmm_temp6, xmm_temp3);      // the result is in xmm6
4573 
4574     __ decrement(blocks);
4575     __ jcc(Assembler::zero, L_exit);
4576     __ movdqu(xmm_temp0, xmm_temp6);
4577     __ addptr(data, 16);
4578     __ jmp(L_ghash_loop);
4579 
4580     __ BIND(L_exit);
4581     __ pshufb(xmm_temp6, xmm_temp10);          // Byte swap 16-byte result
4582     __ movdqu(Address(state, 0), xmm_temp6);   // store the result
4583     __ leave();
4584     __ ret(0);
4585     return start;
4586   }
4587 
4588   //base64 character set
4589   address base64_charset_addr() {
4590     __ align(CodeEntryAlignment);
4591     StubCodeMark mark(this, "StubRoutines", "base64_charset");
4592     address start = __ pc();
4593     __ emit_data64(0x0000004200000041, relocInfo::none);
4594     __ emit_data64(0x0000004400000043, relocInfo::none);
4595     __ emit_data64(0x0000004600000045, relocInfo::none);
4596     __ emit_data64(0x0000004800000047, relocInfo::none);
4597     __ emit_data64(0x0000004a00000049, relocInfo::none);
4598     __ emit_data64(0x0000004c0000004b, relocInfo::none);
4599     __ emit_data64(0x0000004e0000004d, relocInfo::none);
4600     __ emit_data64(0x000000500000004f, relocInfo::none);
4601     __ emit_data64(0x0000005200000051, relocInfo::none);
4602     __ emit_data64(0x0000005400000053, relocInfo::none);
4603     __ emit_data64(0x0000005600000055, relocInfo::none);
4604     __ emit_data64(0x0000005800000057, relocInfo::none);
4605     __ emit_data64(0x0000005a00000059, relocInfo::none);
4606     __ emit_data64(0x0000006200000061, relocInfo::none);
4607     __ emit_data64(0x0000006400000063, relocInfo::none);
4608     __ emit_data64(0x0000006600000065, relocInfo::none);
4609     __ emit_data64(0x0000006800000067, relocInfo::none);
4610     __ emit_data64(0x0000006a00000069, relocInfo::none);
4611     __ emit_data64(0x0000006c0000006b, relocInfo::none);
4612     __ emit_data64(0x0000006e0000006d, relocInfo::none);
4613     __ emit_data64(0x000000700000006f, relocInfo::none);
4614     __ emit_data64(0x0000007200000071, relocInfo::none);
4615     __ emit_data64(0x0000007400000073, relocInfo::none);
4616     __ emit_data64(0x0000007600000075, relocInfo::none);
4617     __ emit_data64(0x0000007800000077, relocInfo::none);
4618     __ emit_data64(0x0000007a00000079, relocInfo::none);
4619     __ emit_data64(0x0000003100000030, relocInfo::none);
4620     __ emit_data64(0x0000003300000032, relocInfo::none);
4621     __ emit_data64(0x0000003500000034, relocInfo::none);
4622     __ emit_data64(0x0000003700000036, relocInfo::none);
4623     __ emit_data64(0x0000003900000038, relocInfo::none);
4624     __ emit_data64(0x0000002f0000002b, relocInfo::none);
4625     return start;
4626   }
4627 
4628   //base64 url character set
4629   address base64url_charset_addr() {
4630     __ align(CodeEntryAlignment);
4631     StubCodeMark mark(this, "StubRoutines", "base64url_charset");
4632     address start = __ pc();
4633     __ emit_data64(0x0000004200000041, relocInfo::none);
4634     __ emit_data64(0x0000004400000043, relocInfo::none);
4635     __ emit_data64(0x0000004600000045, relocInfo::none);
4636     __ emit_data64(0x0000004800000047, relocInfo::none);
4637     __ emit_data64(0x0000004a00000049, relocInfo::none);
4638     __ emit_data64(0x0000004c0000004b, relocInfo::none);
4639     __ emit_data64(0x0000004e0000004d, relocInfo::none);
4640     __ emit_data64(0x000000500000004f, relocInfo::none);
4641     __ emit_data64(0x0000005200000051, relocInfo::none);
4642     __ emit_data64(0x0000005400000053, relocInfo::none);
4643     __ emit_data64(0x0000005600000055, relocInfo::none);
4644     __ emit_data64(0x0000005800000057, relocInfo::none);
4645     __ emit_data64(0x0000005a00000059, relocInfo::none);
4646     __ emit_data64(0x0000006200000061, relocInfo::none);
4647     __ emit_data64(0x0000006400000063, relocInfo::none);
4648     __ emit_data64(0x0000006600000065, relocInfo::none);
4649     __ emit_data64(0x0000006800000067, relocInfo::none);
4650     __ emit_data64(0x0000006a00000069, relocInfo::none);
4651     __ emit_data64(0x0000006c0000006b, relocInfo::none);
4652     __ emit_data64(0x0000006e0000006d, relocInfo::none);
4653     __ emit_data64(0x000000700000006f, relocInfo::none);
4654     __ emit_data64(0x0000007200000071, relocInfo::none);
4655     __ emit_data64(0x0000007400000073, relocInfo::none);
4656     __ emit_data64(0x0000007600000075, relocInfo::none);
4657     __ emit_data64(0x0000007800000077, relocInfo::none);
4658     __ emit_data64(0x0000007a00000079, relocInfo::none);
4659     __ emit_data64(0x0000003100000030, relocInfo::none);
4660     __ emit_data64(0x0000003300000032, relocInfo::none);
4661     __ emit_data64(0x0000003500000034, relocInfo::none);
4662     __ emit_data64(0x0000003700000036, relocInfo::none);
4663     __ emit_data64(0x0000003900000038, relocInfo::none);
4664     __ emit_data64(0x0000005f0000002d, relocInfo::none);
4665 
4666     return start;
4667   }
4668 
4669   address base64_bswap_mask_addr() {
4670     __ align(CodeEntryAlignment);
4671     StubCodeMark mark(this, "StubRoutines", "bswap_mask_base64");
4672     address start = __ pc();
4673     __ emit_data64(0x0504038002010080, relocInfo::none);
4674     __ emit_data64(0x0b0a098008070680, relocInfo::none);
4675     __ emit_data64(0x0908078006050480, relocInfo::none);
4676     __ emit_data64(0x0f0e0d800c0b0a80, relocInfo::none);
4677     __ emit_data64(0x0605048003020180, relocInfo::none);
4678     __ emit_data64(0x0c0b0a8009080780, relocInfo::none);
4679     __ emit_data64(0x0504038002010080, relocInfo::none);
4680     __ emit_data64(0x0b0a098008070680, relocInfo::none);
4681 
4682     return start;
4683   }
4684 
4685   address base64_right_shift_mask_addr() {
4686     __ align(CodeEntryAlignment);
4687     StubCodeMark mark(this, "StubRoutines", "right_shift_mask");
4688     address start = __ pc();
4689     __ emit_data64(0x0006000400020000, relocInfo::none);
4690     __ emit_data64(0x0006000400020000, relocInfo::none);
4691     __ emit_data64(0x0006000400020000, relocInfo::none);
4692     __ emit_data64(0x0006000400020000, relocInfo::none);
4693     __ emit_data64(0x0006000400020000, relocInfo::none);
4694     __ emit_data64(0x0006000400020000, relocInfo::none);
4695     __ emit_data64(0x0006000400020000, relocInfo::none);
4696     __ emit_data64(0x0006000400020000, relocInfo::none);
4697 
4698     return start;
4699   }
4700 
4701   address base64_left_shift_mask_addr() {
4702     __ align(CodeEntryAlignment);
4703     StubCodeMark mark(this, "StubRoutines", "left_shift_mask");
4704     address start = __ pc();
4705     __ emit_data64(0x0000000200040000, relocInfo::none);
4706     __ emit_data64(0x0000000200040000, relocInfo::none);
4707     __ emit_data64(0x0000000200040000, relocInfo::none);
4708     __ emit_data64(0x0000000200040000, relocInfo::none);
4709     __ emit_data64(0x0000000200040000, relocInfo::none);
4710     __ emit_data64(0x0000000200040000, relocInfo::none);
4711     __ emit_data64(0x0000000200040000, relocInfo::none);
4712     __ emit_data64(0x0000000200040000, relocInfo::none);
4713 
4714     return start;
4715   }
4716 
4717   address base64_and_mask_addr() {
4718     __ align(CodeEntryAlignment);
4719     StubCodeMark mark(this, "StubRoutines", "and_mask");
4720     address start = __ pc();
4721     __ emit_data64(0x3f003f003f000000, relocInfo::none);
4722     __ emit_data64(0x3f003f003f000000, relocInfo::none);
4723     __ emit_data64(0x3f003f003f000000, relocInfo::none);
4724     __ emit_data64(0x3f003f003f000000, relocInfo::none);
4725     __ emit_data64(0x3f003f003f000000, relocInfo::none);
4726     __ emit_data64(0x3f003f003f000000, relocInfo::none);
4727     __ emit_data64(0x3f003f003f000000, relocInfo::none);
4728     __ emit_data64(0x3f003f003f000000, relocInfo::none);
4729     return start;
4730   }
4731 
4732   address base64_gather_mask_addr() {
4733     __ align(CodeEntryAlignment);
4734     StubCodeMark mark(this, "StubRoutines", "gather_mask");
4735     address start = __ pc();
4736     __ emit_data64(0xffffffffffffffff, relocInfo::none);
4737     return start;
4738   }
4739 
4740 // Code for generating Base64 encoding.
4741 // Intrinsic function prototype in Base64.java:
4742 // private void encodeBlock(byte[] src, int sp, int sl, byte[] dst, int dp, boolean isURL) {
4743   address generate_base64_encodeBlock() {
4744     __ align(CodeEntryAlignment);
4745     StubCodeMark mark(this, "StubRoutines", "implEncode");
4746     address start = __ pc();
4747     __ enter();
4748 
4749     // Save callee-saved registers before using them
4750     __ push(r12);
4751     __ push(r13);
4752     __ push(r14);
4753     __ push(r15);
4754 
4755     // arguments
4756     const Register source = c_rarg0; // Source Array
4757     const Register start_offset = c_rarg1; // start offset
4758     const Register end_offset = c_rarg2; // end offset
4759     const Register dest = c_rarg3; // destination array
4760 
4761 #ifndef _WIN64
4762     const Register dp = c_rarg4;  // Position for writing to dest array
4763     const Register isURL = c_rarg5;// Base64 or URL character set
4764 #else
4765     const Address  dp_mem(rbp, 6 * wordSize);  // length is on stack on Win64
4766     const Address isURL_mem(rbp, 7 * wordSize);
4767     const Register isURL = r10;      // pick the volatile windows register
4768     const Register dp = r12;
4769     __ movl(dp, dp_mem);
4770     __ movl(isURL, isURL_mem);
4771 #endif
4772 
4773     const Register length = r14;
4774     Label L_process80, L_process32, L_process3, L_exit, L_processdata;
4775 
4776     // calculate length from offsets
4777     __ movl(length, end_offset);
4778     __ subl(length, start_offset);
4779     __ cmpl(length, 0);
4780     __ jcc(Assembler::lessEqual, L_exit);
4781 
4782     __ lea(r11, ExternalAddress(StubRoutines::x86::base64_charset_addr()));
4783     // check if base64 charset(isURL=0) or base64 url charset(isURL=1) needs to be loaded
4784     __ cmpl(isURL, 0);
4785     __ jcc(Assembler::equal, L_processdata);
4786     __ lea(r11, ExternalAddress(StubRoutines::x86::base64url_charset_addr()));
4787 
4788     // load masks required for encoding data
4789     __ BIND(L_processdata);
4790     __ movdqu(xmm16, ExternalAddress(StubRoutines::x86::base64_gather_mask_addr()));
4791     // Set 64 bits of K register.
4792     __ evpcmpeqb(k3, xmm16, xmm16, Assembler::AVX_512bit);
4793     __ evmovdquq(xmm12, ExternalAddress(StubRoutines::x86::base64_bswap_mask_addr()), Assembler::AVX_256bit, r13);
4794     __ evmovdquq(xmm13, ExternalAddress(StubRoutines::x86::base64_right_shift_mask_addr()), Assembler::AVX_512bit, r13);
4795     __ evmovdquq(xmm14, ExternalAddress(StubRoutines::x86::base64_left_shift_mask_addr()), Assembler::AVX_512bit, r13);
4796     __ evmovdquq(xmm15, ExternalAddress(StubRoutines::x86::base64_and_mask_addr()), Assembler::AVX_512bit, r13);
4797 
4798     // Vector Base64 implementation, producing 96 bytes of encoded data
4799     __ BIND(L_process80);
4800     __ cmpl(length, 80);
4801     __ jcc(Assembler::below, L_process32);
4802     __ evmovdquq(xmm0, Address(source, start_offset, Address::times_1, 0), Assembler::AVX_256bit);
4803     __ evmovdquq(xmm1, Address(source, start_offset, Address::times_1, 24), Assembler::AVX_256bit);
4804     __ evmovdquq(xmm2, Address(source, start_offset, Address::times_1, 48), Assembler::AVX_256bit);
4805 
4806     //permute the input data in such a manner that we have continuity of the source
4807     __ vpermq(xmm3, xmm0, 148, Assembler::AVX_256bit);
4808     __ vpermq(xmm4, xmm1, 148, Assembler::AVX_256bit);
4809     __ vpermq(xmm5, xmm2, 148, Assembler::AVX_256bit);
4810 
4811     //shuffle input and group 3 bytes of data and to it add 0 as the 4th byte.
4812     //we can deal with 12 bytes at a time in a 128 bit register
4813     __ vpshufb(xmm3, xmm3, xmm12, Assembler::AVX_256bit);
4814     __ vpshufb(xmm4, xmm4, xmm12, Assembler::AVX_256bit);
4815     __ vpshufb(xmm5, xmm5, xmm12, Assembler::AVX_256bit);
4816 
4817     //convert byte to word. Each 128 bit register will have 6 bytes for processing
4818     __ vpmovzxbw(xmm3, xmm3, Assembler::AVX_512bit);
4819     __ vpmovzxbw(xmm4, xmm4, Assembler::AVX_512bit);
4820     __ vpmovzxbw(xmm5, xmm5, Assembler::AVX_512bit);
4821 
4822     // Extract bits in the following pattern 6, 4+2, 2+4, 6 to convert 3, 8 bit numbers to 4, 6 bit numbers
4823     __ evpsrlvw(xmm0, xmm3, xmm13,  Assembler::AVX_512bit);
4824     __ evpsrlvw(xmm1, xmm4, xmm13, Assembler::AVX_512bit);
4825     __ evpsrlvw(xmm2, xmm5, xmm13, Assembler::AVX_512bit);
4826 
4827     __ evpsllvw(xmm3, xmm3, xmm14, Assembler::AVX_512bit);
4828     __ evpsllvw(xmm4, xmm4, xmm14, Assembler::AVX_512bit);
4829     __ evpsllvw(xmm5, xmm5, xmm14, Assembler::AVX_512bit);
4830 
4831     __ vpsrlq(xmm0, xmm0, 8, Assembler::AVX_512bit);
4832     __ vpsrlq(xmm1, xmm1, 8, Assembler::AVX_512bit);
4833     __ vpsrlq(xmm2, xmm2, 8, Assembler::AVX_512bit);
4834 
4835     __ vpsllq(xmm3, xmm3, 8, Assembler::AVX_512bit);
4836     __ vpsllq(xmm4, xmm4, 8, Assembler::AVX_512bit);
4837     __ vpsllq(xmm5, xmm5, 8, Assembler::AVX_512bit);
4838 
4839     __ vpandq(xmm3, xmm3, xmm15, Assembler::AVX_512bit);
4840     __ vpandq(xmm4, xmm4, xmm15, Assembler::AVX_512bit);
4841     __ vpandq(xmm5, xmm5, xmm15, Assembler::AVX_512bit);
4842 
4843     // Get the final 4*6 bits base64 encoding
4844     __ vporq(xmm3, xmm3, xmm0, Assembler::AVX_512bit);
4845     __ vporq(xmm4, xmm4, xmm1, Assembler::AVX_512bit);
4846     __ vporq(xmm5, xmm5, xmm2, Assembler::AVX_512bit);
4847 
4848     // Shift
4849     __ vpsrlq(xmm3, xmm3, 8, Assembler::AVX_512bit);
4850     __ vpsrlq(xmm4, xmm4, 8, Assembler::AVX_512bit);
4851     __ vpsrlq(xmm5, xmm5, 8, Assembler::AVX_512bit);
4852 
4853     // look up 6 bits in the base64 character set to fetch the encoding
4854     // we are converting word to dword as gather instructions need dword indices for looking up encoding
4855     __ vextracti64x4(xmm6, xmm3, 0);
4856     __ vpmovzxwd(xmm0, xmm6, Assembler::AVX_512bit);
4857     __ vextracti64x4(xmm6, xmm3, 1);
4858     __ vpmovzxwd(xmm1, xmm6, Assembler::AVX_512bit);
4859 
4860     __ vextracti64x4(xmm6, xmm4, 0);
4861     __ vpmovzxwd(xmm2, xmm6, Assembler::AVX_512bit);
4862     __ vextracti64x4(xmm6, xmm4, 1);
4863     __ vpmovzxwd(xmm3, xmm6, Assembler::AVX_512bit);
4864 
4865     __ vextracti64x4(xmm4, xmm5, 0);
4866     __ vpmovzxwd(xmm6, xmm4, Assembler::AVX_512bit);
4867 
4868     __ vextracti64x4(xmm4, xmm5, 1);
4869     __ vpmovzxwd(xmm7, xmm4, Assembler::AVX_512bit);
4870 
4871     __ kmovql(k2, k3);
4872     __ evpgatherdd(xmm4, k2, Address(r11, xmm0, Address::times_4, 0), Assembler::AVX_512bit);
4873     __ kmovql(k2, k3);
4874     __ evpgatherdd(xmm5, k2, Address(r11, xmm1, Address::times_4, 0), Assembler::AVX_512bit);
4875     __ kmovql(k2, k3);
4876     __ evpgatherdd(xmm8, k2, Address(r11, xmm2, Address::times_4, 0), Assembler::AVX_512bit);
4877     __ kmovql(k2, k3);
4878     __ evpgatherdd(xmm9, k2, Address(r11, xmm3, Address::times_4, 0), Assembler::AVX_512bit);
4879     __ kmovql(k2, k3);
4880     __ evpgatherdd(xmm10, k2, Address(r11, xmm6, Address::times_4, 0), Assembler::AVX_512bit);
4881     __ kmovql(k2, k3);
4882     __ evpgatherdd(xmm11, k2, Address(r11, xmm7, Address::times_4, 0), Assembler::AVX_512bit);
4883 
4884     //Down convert dword to byte. Final output is 16*6 = 96 bytes long
4885     __ evpmovdb(Address(dest, dp, Address::times_1, 0), xmm4, Assembler::AVX_512bit);
4886     __ evpmovdb(Address(dest, dp, Address::times_1, 16), xmm5, Assembler::AVX_512bit);
4887     __ evpmovdb(Address(dest, dp, Address::times_1, 32), xmm8, Assembler::AVX_512bit);
4888     __ evpmovdb(Address(dest, dp, Address::times_1, 48), xmm9, Assembler::AVX_512bit);
4889     __ evpmovdb(Address(dest, dp, Address::times_1, 64), xmm10, Assembler::AVX_512bit);
4890     __ evpmovdb(Address(dest, dp, Address::times_1, 80), xmm11, Assembler::AVX_512bit);
4891 
4892     __ addq(dest, 96);
4893     __ addq(source, 72);
4894     __ subq(length, 72);
4895     __ jmp(L_process80);
4896 
4897     // Vector Base64 implementation generating 32 bytes of encoded data
4898     __ BIND(L_process32);
4899     __ cmpl(length, 32);
4900     __ jcc(Assembler::below, L_process3);
4901     __ evmovdquq(xmm0, Address(source, start_offset), Assembler::AVX_256bit);
4902     __ vpermq(xmm0, xmm0, 148, Assembler::AVX_256bit);
4903     __ vpshufb(xmm6, xmm0, xmm12, Assembler::AVX_256bit);
4904     __ vpmovzxbw(xmm6, xmm6, Assembler::AVX_512bit);
4905     __ evpsrlvw(xmm2, xmm6, xmm13, Assembler::AVX_512bit);
4906     __ evpsllvw(xmm3, xmm6, xmm14, Assembler::AVX_512bit);
4907 
4908     __ vpsrlq(xmm2, xmm2, 8, Assembler::AVX_512bit);
4909     __ vpsllq(xmm3, xmm3, 8, Assembler::AVX_512bit);
4910     __ vpandq(xmm3, xmm3, xmm15, Assembler::AVX_512bit);
4911     __ vporq(xmm1, xmm2, xmm3, Assembler::AVX_512bit);
4912     __ vpsrlq(xmm1, xmm1, 8, Assembler::AVX_512bit);
4913     __ vextracti64x4(xmm9, xmm1, 0);
4914     __ vpmovzxwd(xmm6, xmm9, Assembler::AVX_512bit);
4915     __ vextracti64x4(xmm9, xmm1, 1);
4916     __ vpmovzxwd(xmm5, xmm9,  Assembler::AVX_512bit);
4917     __ kmovql(k2, k3);
4918     __ evpgatherdd(xmm8, k2, Address(r11, xmm6, Address::times_4, 0), Assembler::AVX_512bit);
4919     __ kmovql(k2, k3);
4920     __ evpgatherdd(xmm10, k2, Address(r11, xmm5, Address::times_4, 0), Assembler::AVX_512bit);
4921     __ evpmovdb(Address(dest, dp, Address::times_1, 0), xmm8, Assembler::AVX_512bit);
4922     __ evpmovdb(Address(dest, dp, Address::times_1, 16), xmm10, Assembler::AVX_512bit);
4923     __ subq(length, 24);
4924     __ addq(dest, 32);
4925     __ addq(source, 24);
4926     __ jmp(L_process32);
4927 
4928     // Scalar data processing takes 3 bytes at a time and produces 4 bytes of encoded data
4929     /* This code corresponds to the scalar version of the following snippet in Base64.java
4930     ** int bits = (src[sp0++] & 0xff) << 16 |(src[sp0++] & 0xff) << 8 |(src[sp0++] & 0xff);
4931     ** dst[dp0++] = (byte)base64[(bits >> > 18) & 0x3f];
4932     ** dst[dp0++] = (byte)base64[(bits >> > 12) & 0x3f];
4933     ** dst[dp0++] = (byte)base64[(bits >> > 6) & 0x3f];
4934     ** dst[dp0++] = (byte)base64[bits & 0x3f];*/
4935     __ BIND(L_process3);
4936     __ cmpl(length, 3);
4937     __ jcc(Assembler::below, L_exit);
4938     // Read 1 byte at a time
4939     __ movzbl(rax, Address(source, start_offset));
4940     __ shll(rax, 0x10);
4941     __ movl(r15, rax);
4942     __ movzbl(rax, Address(source, start_offset, Address::times_1, 1));
4943     __ shll(rax, 0x8);
4944     __ movzwl(rax, rax);
4945     __ orl(r15, rax);
4946     __ movzbl(rax, Address(source, start_offset, Address::times_1, 2));
4947     __ orl(rax, r15);
4948     // Save 3 bytes read in r15
4949     __ movl(r15, rax);
4950     __ shrl(rax, 0x12);
4951     __ andl(rax, 0x3f);
4952     // rax contains the index, r11 contains base64 lookup table
4953     __ movb(rax, Address(r11, rax, Address::times_4));
4954     // Write the encoded byte to destination
4955     __ movb(Address(dest, dp, Address::times_1, 0), rax);
4956     __ movl(rax, r15);
4957     __ shrl(rax, 0xc);
4958     __ andl(rax, 0x3f);
4959     __ movb(rax, Address(r11, rax, Address::times_4));
4960     __ movb(Address(dest, dp, Address::times_1, 1), rax);
4961     __ movl(rax, r15);
4962     __ shrl(rax, 0x6);
4963     __ andl(rax, 0x3f);
4964     __ movb(rax, Address(r11, rax, Address::times_4));
4965     __ movb(Address(dest, dp, Address::times_1, 2), rax);
4966     __ movl(rax, r15);
4967     __ andl(rax, 0x3f);
4968     __ movb(rax, Address(r11, rax, Address::times_4));
4969     __ movb(Address(dest, dp, Address::times_1, 3), rax);
4970     __ subl(length, 3);
4971     __ addq(dest, 4);
4972     __ addq(source, 3);
4973     __ jmp(L_process3);
4974     __ BIND(L_exit);
4975     __ pop(r15);
4976     __ pop(r14);
4977     __ pop(r13);
4978     __ pop(r12);
4979     __ leave();
4980     __ ret(0);
4981     return start;
4982   }
4983 
4984   /**
4985    *  Arguments:
4986    *
4987    * Inputs:
4988    *   c_rarg0   - int crc
4989    *   c_rarg1   - byte* buf
4990    *   c_rarg2   - int length
4991    *
4992    * Ouput:
4993    *       rax   - int crc result
4994    */
4995   address generate_updateBytesCRC32() {
4996     assert(UseCRC32Intrinsics, "need AVX and CLMUL instructions");
4997 
4998     __ align(CodeEntryAlignment);
4999     StubCodeMark mark(this, "StubRoutines", "updateBytesCRC32");
5000 
5001     address start = __ pc();
5002     // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
5003     // Unix:  rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
5004     // rscratch1: r10
5005     const Register crc   = c_rarg0;  // crc
5006     const Register buf   = c_rarg1;  // source java byte array address
5007     const Register len   = c_rarg2;  // length
5008     const Register table = c_rarg3;  // crc_table address (reuse register)
5009     const Register tmp   = r11;
5010     assert_different_registers(crc, buf, len, table, tmp, rax);
5011 
5012     BLOCK_COMMENT("Entry:");
5013     __ enter(); // required for proper stackwalking of RuntimeStub frame
5014 
5015     __ kernel_crc32(crc, buf, len, table, tmp);
5016 
5017     __ movl(rax, crc);
5018     __ vzeroupper();
5019     __ leave(); // required for proper stackwalking of RuntimeStub frame
5020     __ ret(0);
5021 
5022     return start;
5023   }
5024 
5025   /**
5026   *  Arguments:
5027   *
5028   * Inputs:
5029   *   c_rarg0   - int crc
5030   *   c_rarg1   - byte* buf
5031   *   c_rarg2   - long length
5032   *   c_rarg3   - table_start - optional (present only when doing a library_call,
5033   *              not used by x86 algorithm)
5034   *
5035   * Ouput:
5036   *       rax   - int crc result
5037   */
5038   address generate_updateBytesCRC32C(bool is_pclmulqdq_supported) {
5039       assert(UseCRC32CIntrinsics, "need SSE4_2");
5040       __ align(CodeEntryAlignment);
5041       StubCodeMark mark(this, "StubRoutines", "updateBytesCRC32C");
5042       address start = __ pc();
5043       //reg.arg        int#0        int#1        int#2        int#3        int#4        int#5        float regs
5044       //Windows        RCX          RDX          R8           R9           none         none         XMM0..XMM3
5045       //Lin / Sol      RDI          RSI          RDX          RCX          R8           R9           XMM0..XMM7
5046       const Register crc = c_rarg0;  // crc
5047       const Register buf = c_rarg1;  // source java byte array address
5048       const Register len = c_rarg2;  // length
5049       const Register a = rax;
5050       const Register j = r9;
5051       const Register k = r10;
5052       const Register l = r11;
5053 #ifdef _WIN64
5054       const Register y = rdi;
5055       const Register z = rsi;
5056 #else
5057       const Register y = rcx;
5058       const Register z = r8;
5059 #endif
5060       assert_different_registers(crc, buf, len, a, j, k, l, y, z);
5061 
5062       BLOCK_COMMENT("Entry:");
5063       __ enter(); // required for proper stackwalking of RuntimeStub frame
5064 #ifdef _WIN64
5065       __ push(y);
5066       __ push(z);
5067 #endif
5068       __ crc32c_ipl_alg2_alt2(crc, buf, len,
5069                               a, j, k,
5070                               l, y, z,
5071                               c_farg0, c_farg1, c_farg2,
5072                               is_pclmulqdq_supported);
5073       __ movl(rax, crc);
5074 #ifdef _WIN64
5075       __ pop(z);
5076       __ pop(y);
5077 #endif
5078       __ vzeroupper();
5079       __ leave(); // required for proper stackwalking of RuntimeStub frame
5080       __ ret(0);
5081 
5082       return start;
5083   }
5084 
5085   /**
5086    *  Arguments:
5087    *
5088    *  Input:
5089    *    c_rarg0   - x address
5090    *    c_rarg1   - x length
5091    *    c_rarg2   - y address
5092    *    c_rarg3   - y length
5093    * not Win64
5094    *    c_rarg4   - z address
5095    *    c_rarg5   - z length
5096    * Win64
5097    *    rsp+40    - z address
5098    *    rsp+48    - z length
5099    */
5100   address generate_multiplyToLen() {
5101     __ align(CodeEntryAlignment);
5102     StubCodeMark mark(this, "StubRoutines", "multiplyToLen");
5103 
5104     address start = __ pc();
5105     // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
5106     // Unix:  rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
5107     const Register x     = rdi;
5108     const Register xlen  = rax;
5109     const Register y     = rsi;
5110     const Register ylen  = rcx;
5111     const Register z     = r8;
5112     const Register zlen  = r11;
5113 
5114     // Next registers will be saved on stack in multiply_to_len().
5115     const Register tmp1  = r12;
5116     const Register tmp2  = r13;
5117     const Register tmp3  = r14;
5118     const Register tmp4  = r15;
5119     const Register tmp5  = rbx;
5120 
5121     BLOCK_COMMENT("Entry:");
5122     __ enter(); // required for proper stackwalking of RuntimeStub frame
5123 
5124 #ifndef _WIN64
5125     __ movptr(zlen, r9); // Save r9 in r11 - zlen
5126 #endif
5127     setup_arg_regs(4); // x => rdi, xlen => rsi, y => rdx
5128                        // ylen => rcx, z => r8, zlen => r11
5129                        // r9 and r10 may be used to save non-volatile registers
5130 #ifdef _WIN64
5131     // last 2 arguments (#4, #5) are on stack on Win64
5132     __ movptr(z, Address(rsp, 6 * wordSize));
5133     __ movptr(zlen, Address(rsp, 7 * wordSize));
5134 #endif
5135 
5136     __ movptr(xlen, rsi);
5137     __ movptr(y,    rdx);
5138     __ multiply_to_len(x, xlen, y, ylen, z, zlen, tmp1, tmp2, tmp3, tmp4, tmp5);
5139 
5140     restore_arg_regs();
5141 
5142     __ leave(); // required for proper stackwalking of RuntimeStub frame
5143     __ ret(0);
5144 
5145     return start;
5146   }
5147 
5148   /**
5149   *  Arguments:
5150   *
5151   *  Input:
5152   *    c_rarg0   - obja     address
5153   *    c_rarg1   - objb     address
5154   *    c_rarg3   - length   length
5155   *    c_rarg4   - scale    log2_array_indxscale
5156   *
5157   *  Output:
5158   *        rax   - int >= mismatched index, < 0 bitwise complement of tail
5159   */
5160   address generate_vectorizedMismatch() {
5161     __ align(CodeEntryAlignment);
5162     StubCodeMark mark(this, "StubRoutines", "vectorizedMismatch");
5163     address start = __ pc();
5164 
5165     BLOCK_COMMENT("Entry:");
5166     __ enter();
5167 
5168 #ifdef _WIN64  // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
5169     const Register scale = c_rarg0;  //rcx, will exchange with r9
5170     const Register objb = c_rarg1;   //rdx
5171     const Register length = c_rarg2; //r8
5172     const Register obja = c_rarg3;   //r9
5173     __ xchgq(obja, scale);  //now obja and scale contains the correct contents
5174 
5175     const Register tmp1 = r10;
5176     const Register tmp2 = r11;
5177 #endif
5178 #ifndef _WIN64 // Unix:  rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
5179     const Register obja = c_rarg0;   //U:rdi
5180     const Register objb = c_rarg1;   //U:rsi
5181     const Register length = c_rarg2; //U:rdx
5182     const Register scale = c_rarg3;  //U:rcx
5183     const Register tmp1 = r8;
5184     const Register tmp2 = r9;
5185 #endif
5186     const Register result = rax; //return value
5187     const XMMRegister vec0 = xmm0;
5188     const XMMRegister vec1 = xmm1;
5189     const XMMRegister vec2 = xmm2;
5190 
5191     __ vectorized_mismatch(obja, objb, length, scale, result, tmp1, tmp2, vec0, vec1, vec2);
5192 
5193     __ vzeroupper();
5194     __ leave();
5195     __ ret(0);
5196 
5197     return start;
5198   }
5199 
5200 /**
5201    *  Arguments:
5202    *
5203   //  Input:
5204   //    c_rarg0   - x address
5205   //    c_rarg1   - x length
5206   //    c_rarg2   - z address
5207   //    c_rarg3   - z lenth
5208    *
5209    */
5210   address generate_squareToLen() {
5211 
5212     __ align(CodeEntryAlignment);
5213     StubCodeMark mark(this, "StubRoutines", "squareToLen");
5214 
5215     address start = __ pc();
5216     // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
5217     // Unix:  rdi, rsi, rdx, rcx (c_rarg0, c_rarg1, ...)
5218     const Register x      = rdi;
5219     const Register len    = rsi;
5220     const Register z      = r8;
5221     const Register zlen   = rcx;
5222 
5223    const Register tmp1      = r12;
5224    const Register tmp2      = r13;
5225    const Register tmp3      = r14;
5226    const Register tmp4      = r15;
5227    const Register tmp5      = rbx;
5228 
5229     BLOCK_COMMENT("Entry:");
5230     __ enter(); // required for proper stackwalking of RuntimeStub frame
5231 
5232     setup_arg_regs(4); // x => rdi, len => rsi, z => rdx
5233                        // zlen => rcx
5234                        // r9 and r10 may be used to save non-volatile registers
5235     __ movptr(r8, rdx);
5236     __ square_to_len(x, len, z, zlen, tmp1, tmp2, tmp3, tmp4, tmp5, rdx, rax);
5237 
5238     restore_arg_regs();
5239 
5240     __ leave(); // required for proper stackwalking of RuntimeStub frame
5241     __ ret(0);
5242 
5243     return start;
5244   }
5245 
5246   address generate_method_entry_barrier() {
5247     __ align(CodeEntryAlignment);
5248     StubCodeMark mark(this, "StubRoutines", "nmethod_entry_barrier");
5249 
5250     Label deoptimize_label;
5251 
5252     address start = __ pc();
5253 
5254     __ push(-1); // cookie, this is used for writing the new rsp when deoptimizing
5255 
5256     BLOCK_COMMENT("Entry:");
5257     __ enter(); // save rbp
5258 
5259     // save c_rarg0, because we want to use that value.
5260     // We could do without it but then we depend on the number of slots used by pusha
5261     __ push(c_rarg0);
5262 
5263     __ lea(c_rarg0, Address(rsp, wordSize * 3)); // 1 for cookie, 1 for rbp, 1 for c_rarg0 - this should be the return address
5264 
5265     __ pusha();
5266 
5267     // The method may have floats as arguments, and we must spill them before calling
5268     // the VM runtime.
5269     assert(Argument::n_float_register_parameters_j == 8, "Assumption");
5270     const int xmm_size = wordSize * 2;
5271     const int xmm_spill_size = xmm_size * Argument::n_float_register_parameters_j;
5272     __ subptr(rsp, xmm_spill_size);
5273     __ movdqu(Address(rsp, xmm_size * 7), xmm7);
5274     __ movdqu(Address(rsp, xmm_size * 6), xmm6);
5275     __ movdqu(Address(rsp, xmm_size * 5), xmm5);
5276     __ movdqu(Address(rsp, xmm_size * 4), xmm4);
5277     __ movdqu(Address(rsp, xmm_size * 3), xmm3);
5278     __ movdqu(Address(rsp, xmm_size * 2), xmm2);
5279     __ movdqu(Address(rsp, xmm_size * 1), xmm1);
5280     __ movdqu(Address(rsp, xmm_size * 0), xmm0);
5281 
5282     __ call_VM_leaf(CAST_FROM_FN_PTR(address, static_cast<int (*)(address*)>(BarrierSetNMethod::nmethod_stub_entry_barrier)), 1);
5283 
5284     __ movdqu(xmm0, Address(rsp, xmm_size * 0));
5285     __ movdqu(xmm1, Address(rsp, xmm_size * 1));
5286     __ movdqu(xmm2, Address(rsp, xmm_size * 2));
5287     __ movdqu(xmm3, Address(rsp, xmm_size * 3));
5288     __ movdqu(xmm4, Address(rsp, xmm_size * 4));
5289     __ movdqu(xmm5, Address(rsp, xmm_size * 5));
5290     __ movdqu(xmm6, Address(rsp, xmm_size * 6));
5291     __ movdqu(xmm7, Address(rsp, xmm_size * 7));
5292     __ addptr(rsp, xmm_spill_size);
5293 
5294     __ cmpl(rax, 1); // 1 means deoptimize
5295     __ jcc(Assembler::equal, deoptimize_label);
5296 
5297     __ popa();
5298     __ pop(c_rarg0);
5299 
5300     __ leave();
5301 
5302     __ addptr(rsp, 1 * wordSize); // cookie
5303     __ ret(0);
5304 
5305 
5306     __ BIND(deoptimize_label);
5307 
5308     __ popa();
5309     __ pop(c_rarg0);
5310 
5311     __ leave();
5312 
5313     // this can be taken out, but is good for verification purposes. getting a SIGSEGV
5314     // here while still having a correct stack is valuable
5315     __ testptr(rsp, Address(rsp, 0));
5316 
5317     __ movptr(rsp, Address(rsp, 0)); // new rsp was written in the barrier
5318     __ jmp(Address(rsp, -1 * wordSize)); // jmp target should be callers verified_entry_point
5319 
5320     return start;
5321   }
5322 
5323    /**
5324    *  Arguments:
5325    *
5326    *  Input:
5327    *    c_rarg0   - out address
5328    *    c_rarg1   - in address
5329    *    c_rarg2   - offset
5330    *    c_rarg3   - len
5331    * not Win64
5332    *    c_rarg4   - k
5333    * Win64
5334    *    rsp+40    - k
5335    */
5336   address generate_mulAdd() {
5337     __ align(CodeEntryAlignment);
5338     StubCodeMark mark(this, "StubRoutines", "mulAdd");
5339 
5340     address start = __ pc();
5341     // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
5342     // Unix:  rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
5343     const Register out     = rdi;
5344     const Register in      = rsi;
5345     const Register offset  = r11;
5346     const Register len     = rcx;
5347     const Register k       = r8;
5348 
5349     // Next registers will be saved on stack in mul_add().
5350     const Register tmp1  = r12;
5351     const Register tmp2  = r13;
5352     const Register tmp3  = r14;
5353     const Register tmp4  = r15;
5354     const Register tmp5  = rbx;
5355 
5356     BLOCK_COMMENT("Entry:");
5357     __ enter(); // required for proper stackwalking of RuntimeStub frame
5358 
5359     setup_arg_regs(4); // out => rdi, in => rsi, offset => rdx
5360                        // len => rcx, k => r8
5361                        // r9 and r10 may be used to save non-volatile registers
5362 #ifdef _WIN64
5363     // last argument is on stack on Win64
5364     __ movl(k, Address(rsp, 6 * wordSize));
5365 #endif
5366     __ movptr(r11, rdx);  // move offset in rdx to offset(r11)
5367     __ mul_add(out, in, offset, len, k, tmp1, tmp2, tmp3, tmp4, tmp5, rdx, rax);
5368 
5369     restore_arg_regs();
5370 
5371     __ leave(); // required for proper stackwalking of RuntimeStub frame
5372     __ ret(0);
5373 
5374     return start;
5375   }
5376 
5377   address generate_libmExp() {
5378     StubCodeMark mark(this, "StubRoutines", "libmExp");
5379 
5380     address start = __ pc();
5381 
5382     const XMMRegister x0  = xmm0;
5383     const XMMRegister x1  = xmm1;
5384     const XMMRegister x2  = xmm2;
5385     const XMMRegister x3  = xmm3;
5386 
5387     const XMMRegister x4  = xmm4;
5388     const XMMRegister x5  = xmm5;
5389     const XMMRegister x6  = xmm6;
5390     const XMMRegister x7  = xmm7;
5391 
5392     const Register tmp   = r11;
5393 
5394     BLOCK_COMMENT("Entry:");
5395     __ enter(); // required for proper stackwalking of RuntimeStub frame
5396 
5397     __ fast_exp(x0, x1, x2, x3, x4, x5, x6, x7, rax, rcx, rdx, tmp);
5398 
5399     __ leave(); // required for proper stackwalking of RuntimeStub frame
5400     __ ret(0);
5401 
5402     return start;
5403 
5404   }
5405 
5406   address generate_libmLog() {
5407     StubCodeMark mark(this, "StubRoutines", "libmLog");
5408 
5409     address start = __ pc();
5410 
5411     const XMMRegister x0 = xmm0;
5412     const XMMRegister x1 = xmm1;
5413     const XMMRegister x2 = xmm2;
5414     const XMMRegister x3 = xmm3;
5415 
5416     const XMMRegister x4 = xmm4;
5417     const XMMRegister x5 = xmm5;
5418     const XMMRegister x6 = xmm6;
5419     const XMMRegister x7 = xmm7;
5420 
5421     const Register tmp1 = r11;
5422     const Register tmp2 = r8;
5423 
5424     BLOCK_COMMENT("Entry:");
5425     __ enter(); // required for proper stackwalking of RuntimeStub frame
5426 
5427     __ fast_log(x0, x1, x2, x3, x4, x5, x6, x7, rax, rcx, rdx, tmp1, tmp2);
5428 
5429     __ leave(); // required for proper stackwalking of RuntimeStub frame
5430     __ ret(0);
5431 
5432     return start;
5433 
5434   }
5435 
5436   address generate_libmLog10() {
5437     StubCodeMark mark(this, "StubRoutines", "libmLog10");
5438 
5439     address start = __ pc();
5440 
5441     const XMMRegister x0 = xmm0;
5442     const XMMRegister x1 = xmm1;
5443     const XMMRegister x2 = xmm2;
5444     const XMMRegister x3 = xmm3;
5445 
5446     const XMMRegister x4 = xmm4;
5447     const XMMRegister x5 = xmm5;
5448     const XMMRegister x6 = xmm6;
5449     const XMMRegister x7 = xmm7;
5450 
5451     const Register tmp = r11;
5452 
5453     BLOCK_COMMENT("Entry:");
5454     __ enter(); // required for proper stackwalking of RuntimeStub frame
5455 
5456     __ fast_log10(x0, x1, x2, x3, x4, x5, x6, x7, rax, rcx, rdx, tmp);
5457 
5458     __ leave(); // required for proper stackwalking of RuntimeStub frame
5459     __ ret(0);
5460 
5461     return start;
5462 
5463   }
5464 
5465   address generate_libmPow() {
5466     StubCodeMark mark(this, "StubRoutines", "libmPow");
5467 
5468     address start = __ pc();
5469 
5470     const XMMRegister x0 = xmm0;
5471     const XMMRegister x1 = xmm1;
5472     const XMMRegister x2 = xmm2;
5473     const XMMRegister x3 = xmm3;
5474 
5475     const XMMRegister x4 = xmm4;
5476     const XMMRegister x5 = xmm5;
5477     const XMMRegister x6 = xmm6;
5478     const XMMRegister x7 = xmm7;
5479 
5480     const Register tmp1 = r8;
5481     const Register tmp2 = r9;
5482     const Register tmp3 = r10;
5483     const Register tmp4 = r11;
5484 
5485     BLOCK_COMMENT("Entry:");
5486     __ enter(); // required for proper stackwalking of RuntimeStub frame
5487 
5488     __ fast_pow(x0, x1, x2, x3, x4, x5, x6, x7, rax, rcx, rdx, tmp1, tmp2, tmp3, tmp4);
5489 
5490     __ leave(); // required for proper stackwalking of RuntimeStub frame
5491     __ ret(0);
5492 
5493     return start;
5494 
5495   }
5496 
5497   address generate_libmSin() {
5498     StubCodeMark mark(this, "StubRoutines", "libmSin");
5499 
5500     address start = __ pc();
5501 
5502     const XMMRegister x0 = xmm0;
5503     const XMMRegister x1 = xmm1;
5504     const XMMRegister x2 = xmm2;
5505     const XMMRegister x3 = xmm3;
5506 
5507     const XMMRegister x4 = xmm4;
5508     const XMMRegister x5 = xmm5;
5509     const XMMRegister x6 = xmm6;
5510     const XMMRegister x7 = xmm7;
5511 
5512     const Register tmp1 = r8;
5513     const Register tmp2 = r9;
5514     const Register tmp3 = r10;
5515     const Register tmp4 = r11;
5516 
5517     BLOCK_COMMENT("Entry:");
5518     __ enter(); // required for proper stackwalking of RuntimeStub frame
5519 
5520 #ifdef _WIN64
5521     __ push(rsi);
5522     __ push(rdi);
5523 #endif
5524     __ fast_sin(x0, x1, x2, x3, x4, x5, x6, x7, rax, rbx, rcx, rdx, tmp1, tmp2, tmp3, tmp4);
5525 
5526 #ifdef _WIN64
5527     __ pop(rdi);
5528     __ pop(rsi);
5529 #endif
5530 
5531     __ leave(); // required for proper stackwalking of RuntimeStub frame
5532     __ ret(0);
5533 
5534     return start;
5535 
5536   }
5537 
5538   address generate_libmCos() {
5539     StubCodeMark mark(this, "StubRoutines", "libmCos");
5540 
5541     address start = __ pc();
5542 
5543     const XMMRegister x0 = xmm0;
5544     const XMMRegister x1 = xmm1;
5545     const XMMRegister x2 = xmm2;
5546     const XMMRegister x3 = xmm3;
5547 
5548     const XMMRegister x4 = xmm4;
5549     const XMMRegister x5 = xmm5;
5550     const XMMRegister x6 = xmm6;
5551     const XMMRegister x7 = xmm7;
5552 
5553     const Register tmp1 = r8;
5554     const Register tmp2 = r9;
5555     const Register tmp3 = r10;
5556     const Register tmp4 = r11;
5557 
5558     BLOCK_COMMENT("Entry:");
5559     __ enter(); // required for proper stackwalking of RuntimeStub frame
5560 
5561 #ifdef _WIN64
5562     __ push(rsi);
5563     __ push(rdi);
5564 #endif
5565     __ fast_cos(x0, x1, x2, x3, x4, x5, x6, x7, rax, rcx, rdx, tmp1, tmp2, tmp3, tmp4);
5566 
5567 #ifdef _WIN64
5568     __ pop(rdi);
5569     __ pop(rsi);
5570 #endif
5571 
5572     __ leave(); // required for proper stackwalking of RuntimeStub frame
5573     __ ret(0);
5574 
5575     return start;
5576 
5577   }
5578 
5579   address generate_libmTan() {
5580     StubCodeMark mark(this, "StubRoutines", "libmTan");
5581 
5582     address start = __ pc();
5583 
5584     const XMMRegister x0 = xmm0;
5585     const XMMRegister x1 = xmm1;
5586     const XMMRegister x2 = xmm2;
5587     const XMMRegister x3 = xmm3;
5588 
5589     const XMMRegister x4 = xmm4;
5590     const XMMRegister x5 = xmm5;
5591     const XMMRegister x6 = xmm6;
5592     const XMMRegister x7 = xmm7;
5593 
5594     const Register tmp1 = r8;
5595     const Register tmp2 = r9;
5596     const Register tmp3 = r10;
5597     const Register tmp4 = r11;
5598 
5599     BLOCK_COMMENT("Entry:");
5600     __ enter(); // required for proper stackwalking of RuntimeStub frame
5601 
5602 #ifdef _WIN64
5603     __ push(rsi);
5604     __ push(rdi);
5605 #endif
5606     __ fast_tan(x0, x1, x2, x3, x4, x5, x6, x7, rax, rcx, rdx, tmp1, tmp2, tmp3, tmp4);
5607 
5608 #ifdef _WIN64
5609     __ pop(rdi);
5610     __ pop(rsi);
5611 #endif
5612 
5613     __ leave(); // required for proper stackwalking of RuntimeStub frame
5614     __ ret(0);
5615 
5616     return start;
5617 
5618   }
5619 
5620 #undef __
5621 #define __ masm->
5622 
5623   // Continuation point for throwing of implicit exceptions that are
5624   // not handled in the current activation. Fabricates an exception
5625   // oop and initiates normal exception dispatching in this
5626   // frame. Since we need to preserve callee-saved values (currently
5627   // only for C2, but done for C1 as well) we need a callee-saved oop
5628   // map and therefore have to make these stubs into RuntimeStubs
5629   // rather than BufferBlobs.  If the compiler needs all registers to
5630   // be preserved between the fault point and the exception handler
5631   // then it must assume responsibility for that in
5632   // AbstractCompiler::continuation_for_implicit_null_exception or
5633   // continuation_for_implicit_division_by_zero_exception. All other
5634   // implicit exceptions (e.g., NullPointerException or
5635   // AbstractMethodError on entry) are either at call sites or
5636   // otherwise assume that stack unwinding will be initiated, so
5637   // caller saved registers were assumed volatile in the compiler.
5638   address generate_throw_exception(const char* name,
5639                                    address runtime_entry,
5640                                    Register arg1 = noreg,
5641                                    Register arg2 = noreg) {
5642     // Information about frame layout at time of blocking runtime call.
5643     // Note that we only have to preserve callee-saved registers since
5644     // the compilers are responsible for supplying a continuation point
5645     // if they expect all registers to be preserved.
5646     enum layout {
5647       rbp_off = frame::arg_reg_save_area_bytes/BytesPerInt,
5648       rbp_off2,
5649       return_off,
5650       return_off2,
5651       framesize // inclusive of return address
5652     };
5653 
5654     int insts_size = 512;
5655     int locs_size  = 64;
5656 
5657     CodeBuffer code(name, insts_size, locs_size);
5658     OopMapSet* oop_maps  = new OopMapSet();
5659     MacroAssembler* masm = new MacroAssembler(&code);
5660 
5661     address start = __ pc();
5662 
5663     // This is an inlined and slightly modified version of call_VM
5664     // which has the ability to fetch the return PC out of
5665     // thread-local storage and also sets up last_Java_sp slightly
5666     // differently than the real call_VM
5667 
5668     __ enter(); // required for proper stackwalking of RuntimeStub frame
5669 
5670     assert(is_even(framesize/2), "sp not 16-byte aligned");
5671 
5672     // return address and rbp are already in place
5673     __ subptr(rsp, (framesize-4) << LogBytesPerInt); // prolog
5674 
5675     int frame_complete = __ pc() - start;
5676 
5677     // Set up last_Java_sp and last_Java_fp
5678     address the_pc = __ pc();
5679     __ set_last_Java_frame(rsp, rbp, the_pc);
5680     __ andptr(rsp, -(StackAlignmentInBytes));    // Align stack
5681 
5682     // Call runtime
5683     if (arg1 != noreg) {
5684       assert(arg2 != c_rarg1, "clobbered");
5685       __ movptr(c_rarg1, arg1);
5686     }
5687     if (arg2 != noreg) {
5688       __ movptr(c_rarg2, arg2);
5689     }
5690     __ movptr(c_rarg0, r15_thread);
5691     BLOCK_COMMENT("call runtime_entry");
5692     __ call(RuntimeAddress(runtime_entry));
5693 
5694     // Generate oop map
5695     OopMap* map = new OopMap(framesize, 0);
5696 
5697     oop_maps->add_gc_map(the_pc - start, map);
5698 
5699     __ reset_last_Java_frame(true);
5700 
5701     __ leave(); // required for proper stackwalking of RuntimeStub frame
5702 
5703     // check for pending exceptions
5704 #ifdef ASSERT
5705     Label L;
5706     __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()),
5707             (int32_t) NULL_WORD);
5708     __ jcc(Assembler::notEqual, L);
5709     __ should_not_reach_here();
5710     __ bind(L);
5711 #endif // ASSERT
5712     __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
5713 
5714 
5715     // codeBlob framesize is in words (not VMRegImpl::slot_size)
5716     RuntimeStub* stub =
5717       RuntimeStub::new_runtime_stub(name,
5718                                     &code,
5719                                     frame_complete,
5720                                     (framesize >> (LogBytesPerWord - LogBytesPerInt)),
5721                                     oop_maps, false);
5722     return stub->entry_point();
5723   }
5724 
5725   void create_control_words() {
5726     // Round to nearest, 53-bit mode, exceptions masked
5727     StubRoutines::_fpu_cntrl_wrd_std   = 0x027F;
5728     // Round to zero, 53-bit mode, exception mased
5729     StubRoutines::_fpu_cntrl_wrd_trunc = 0x0D7F;
5730     // Round to nearest, 24-bit mode, exceptions masked
5731     StubRoutines::_fpu_cntrl_wrd_24    = 0x007F;
5732     // Round to nearest, 64-bit mode, exceptions masked
5733     StubRoutines::_mxcsr_std           = 0x1F80;
5734     // Note: the following two constants are 80-bit values
5735     //       layout is critical for correct loading by FPU.
5736     // Bias for strict fp multiply/divide
5737     StubRoutines::_fpu_subnormal_bias1[0]= 0x00000000; // 2^(-15360) == 0x03ff 8000 0000 0000 0000
5738     StubRoutines::_fpu_subnormal_bias1[1]= 0x80000000;
5739     StubRoutines::_fpu_subnormal_bias1[2]= 0x03ff;
5740     // Un-Bias for strict fp multiply/divide
5741     StubRoutines::_fpu_subnormal_bias2[0]= 0x00000000; // 2^(+15360) == 0x7bff 8000 0000 0000 0000
5742     StubRoutines::_fpu_subnormal_bias2[1]= 0x80000000;
5743     StubRoutines::_fpu_subnormal_bias2[2]= 0x7bff;
5744   }
5745 
5746   // Call here from the interpreter or compiled code to either load
5747   // multiple returned values from the value type instance being
5748   // returned to registers or to store returned values to a newly
5749   // allocated value type instance.
5750   address generate_return_value_stub(address destination, const char* name, bool has_res) {
5751     // We need to save all registers the calling convention may use so
5752     // the runtime calls read or update those registers. This needs to
5753     // be in sync with SharedRuntime::java_return_convention().
5754     enum layout {
5755       pad_off = frame::arg_reg_save_area_bytes/BytesPerInt, pad_off_2,
5756       rax_off, rax_off_2,
5757       j_rarg5_off, j_rarg5_2,
5758       j_rarg4_off, j_rarg4_2,
5759       j_rarg3_off, j_rarg3_2,
5760       j_rarg2_off, j_rarg2_2,
5761       j_rarg1_off, j_rarg1_2,
5762       j_rarg0_off, j_rarg0_2,
5763       j_farg0_off, j_farg0_2,
5764       j_farg1_off, j_farg1_2,
5765       j_farg2_off, j_farg2_2,
5766       j_farg3_off, j_farg3_2,
5767       j_farg4_off, j_farg4_2,
5768       j_farg5_off, j_farg5_2,
5769       j_farg6_off, j_farg6_2,
5770       j_farg7_off, j_farg7_2,
5771       rbp_off, rbp_off_2,
5772       return_off, return_off_2,
5773 
5774       framesize
5775     };
5776 
5777     CodeBuffer buffer(name, 1000, 512);
5778     MacroAssembler* masm = new MacroAssembler(&buffer);
5779 
5780     int frame_size_in_bytes = align_up(framesize*BytesPerInt, 16);
5781     assert(frame_size_in_bytes == framesize*BytesPerInt, "misaligned");
5782     int frame_size_in_slots = frame_size_in_bytes / BytesPerInt;
5783     int frame_size_in_words = frame_size_in_bytes / wordSize;
5784 
5785     OopMapSet *oop_maps = new OopMapSet();
5786     OopMap* map = new OopMap(frame_size_in_slots, 0);
5787 
5788     map->set_callee_saved(VMRegImpl::stack2reg(rax_off), rax->as_VMReg());
5789     map->set_callee_saved(VMRegImpl::stack2reg(j_rarg5_off), j_rarg5->as_VMReg());
5790     map->set_callee_saved(VMRegImpl::stack2reg(j_rarg4_off), j_rarg4->as_VMReg());
5791     map->set_callee_saved(VMRegImpl::stack2reg(j_rarg3_off), j_rarg3->as_VMReg());
5792     map->set_callee_saved(VMRegImpl::stack2reg(j_rarg2_off), j_rarg2->as_VMReg());
5793     map->set_callee_saved(VMRegImpl::stack2reg(j_rarg1_off), j_rarg1->as_VMReg());
5794     map->set_callee_saved(VMRegImpl::stack2reg(j_rarg0_off), j_rarg0->as_VMReg());
5795     map->set_callee_saved(VMRegImpl::stack2reg(j_farg0_off), j_farg0->as_VMReg());
5796     map->set_callee_saved(VMRegImpl::stack2reg(j_farg1_off), j_farg1->as_VMReg());
5797     map->set_callee_saved(VMRegImpl::stack2reg(j_farg2_off), j_farg2->as_VMReg());
5798     map->set_callee_saved(VMRegImpl::stack2reg(j_farg3_off), j_farg3->as_VMReg());
5799     map->set_callee_saved(VMRegImpl::stack2reg(j_farg4_off), j_farg4->as_VMReg());
5800     map->set_callee_saved(VMRegImpl::stack2reg(j_farg5_off), j_farg5->as_VMReg());
5801     map->set_callee_saved(VMRegImpl::stack2reg(j_farg6_off), j_farg6->as_VMReg());
5802     map->set_callee_saved(VMRegImpl::stack2reg(j_farg7_off), j_farg7->as_VMReg());
5803 
5804     int start = __ offset();
5805 
5806     __ subptr(rsp, frame_size_in_bytes - 8 /* return address*/);
5807 
5808     __ movptr(Address(rsp, rbp_off * BytesPerInt), rbp);
5809     __ movdbl(Address(rsp, j_farg7_off * BytesPerInt), j_farg7);
5810     __ movdbl(Address(rsp, j_farg6_off * BytesPerInt), j_farg6);
5811     __ movdbl(Address(rsp, j_farg5_off * BytesPerInt), j_farg5);
5812     __ movdbl(Address(rsp, j_farg4_off * BytesPerInt), j_farg4);
5813     __ movdbl(Address(rsp, j_farg3_off * BytesPerInt), j_farg3);
5814     __ movdbl(Address(rsp, j_farg2_off * BytesPerInt), j_farg2);
5815     __ movdbl(Address(rsp, j_farg1_off * BytesPerInt), j_farg1);
5816     __ movdbl(Address(rsp, j_farg0_off * BytesPerInt), j_farg0);
5817 
5818     __ movptr(Address(rsp, j_rarg0_off * BytesPerInt), j_rarg0);
5819     __ movptr(Address(rsp, j_rarg1_off * BytesPerInt), j_rarg1);
5820     __ movptr(Address(rsp, j_rarg2_off * BytesPerInt), j_rarg2);
5821     __ movptr(Address(rsp, j_rarg3_off * BytesPerInt), j_rarg3);
5822     __ movptr(Address(rsp, j_rarg4_off * BytesPerInt), j_rarg4);
5823     __ movptr(Address(rsp, j_rarg5_off * BytesPerInt), j_rarg5);
5824     __ movptr(Address(rsp, rax_off * BytesPerInt), rax);
5825 
5826     int frame_complete = __ offset();
5827 
5828     __ set_last_Java_frame(noreg, noreg, NULL);
5829 
5830     __ mov(c_rarg0, r15_thread);
5831     __ mov(c_rarg1, rax);
5832 
5833     __ call(RuntimeAddress(destination));
5834 
5835     // Set an oopmap for the call site.
5836 
5837     oop_maps->add_gc_map( __ offset() - start, map);
5838 
5839     // clear last_Java_sp
5840     __ reset_last_Java_frame(false);
5841 
5842     __ movptr(rbp, Address(rsp, rbp_off * BytesPerInt));
5843     __ movdbl(j_farg7, Address(rsp, j_farg7_off * BytesPerInt));
5844     __ movdbl(j_farg6, Address(rsp, j_farg6_off * BytesPerInt));
5845     __ movdbl(j_farg5, Address(rsp, j_farg5_off * BytesPerInt));
5846     __ movdbl(j_farg4, Address(rsp, j_farg4_off * BytesPerInt));
5847     __ movdbl(j_farg3, Address(rsp, j_farg3_off * BytesPerInt));
5848     __ movdbl(j_farg2, Address(rsp, j_farg2_off * BytesPerInt));
5849     __ movdbl(j_farg1, Address(rsp, j_farg1_off * BytesPerInt));
5850     __ movdbl(j_farg0, Address(rsp, j_farg0_off * BytesPerInt));
5851 
5852     __ movptr(j_rarg0, Address(rsp, j_rarg0_off * BytesPerInt));
5853     __ movptr(j_rarg1, Address(rsp, j_rarg1_off * BytesPerInt));
5854     __ movptr(j_rarg2, Address(rsp, j_rarg2_off * BytesPerInt));
5855     __ movptr(j_rarg3, Address(rsp, j_rarg3_off * BytesPerInt));
5856     __ movptr(j_rarg4, Address(rsp, j_rarg4_off * BytesPerInt));
5857     __ movptr(j_rarg5, Address(rsp, j_rarg5_off * BytesPerInt));
5858     __ movptr(rax, Address(rsp, rax_off * BytesPerInt));
5859 
5860     __ addptr(rsp, frame_size_in_bytes-8);
5861 
5862     // check for pending exceptions
5863     Label pending;
5864     __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
5865     __ jcc(Assembler::notEqual, pending);
5866 
5867     if (has_res) {
5868       __ get_vm_result(rax, r15_thread);
5869     }
5870 
5871     __ ret(0);
5872 
5873     __ bind(pending);
5874 
5875     __ movptr(rax, Address(r15_thread, Thread::pending_exception_offset()));
5876     __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
5877 
5878     // -------------
5879     // make sure all code is generated
5880     masm->flush();
5881 
5882     RuntimeStub* stub = RuntimeStub::new_runtime_stub(name, &buffer, frame_complete, frame_size_in_words, oop_maps, false);
5883     return stub->entry_point();
5884   }
5885 
5886   // Initialization
5887   void generate_initial() {
5888     // Generates all stubs and initializes the entry points
5889 
5890     // This platform-specific settings are needed by generate_call_stub()
5891     create_control_words();
5892 
5893     // entry points that exist in all platforms Note: This is code
5894     // that could be shared among different platforms - however the
5895     // benefit seems to be smaller than the disadvantage of having a
5896     // much more complicated generator structure. See also comment in
5897     // stubRoutines.hpp.
5898 
5899     StubRoutines::_forward_exception_entry = generate_forward_exception();
5900 
5901     StubRoutines::_call_stub_entry =
5902       generate_call_stub(StubRoutines::_call_stub_return_address);
5903 
5904     // is referenced by megamorphic call
5905     StubRoutines::_catch_exception_entry = generate_catch_exception();
5906 
5907     // atomic calls
5908     StubRoutines::_atomic_xchg_entry          = generate_atomic_xchg();
5909     StubRoutines::_atomic_xchg_long_entry     = generate_atomic_xchg_long();
5910     StubRoutines::_atomic_cmpxchg_entry       = generate_atomic_cmpxchg();
5911     StubRoutines::_atomic_cmpxchg_byte_entry  = generate_atomic_cmpxchg_byte();
5912     StubRoutines::_atomic_cmpxchg_long_entry  = generate_atomic_cmpxchg_long();
5913     StubRoutines::_atomic_add_entry           = generate_atomic_add();
5914     StubRoutines::_atomic_add_long_entry      = generate_atomic_add_long();
5915     StubRoutines::_fence_entry                = generate_orderaccess_fence();
5916 
5917     // platform dependent
5918     StubRoutines::x86::_get_previous_fp_entry = generate_get_previous_fp();
5919     StubRoutines::x86::_get_previous_sp_entry = generate_get_previous_sp();
5920 
5921     StubRoutines::x86::_verify_mxcsr_entry    = generate_verify_mxcsr();
5922 
5923     // Build this early so it's available for the interpreter.
5924     StubRoutines::_throw_StackOverflowError_entry =
5925       generate_throw_exception("StackOverflowError throw_exception",
5926                                CAST_FROM_FN_PTR(address,
5927                                                 SharedRuntime::
5928                                                 throw_StackOverflowError));
5929     StubRoutines::_throw_delayed_StackOverflowError_entry =
5930       generate_throw_exception("delayed StackOverflowError throw_exception",
5931                                CAST_FROM_FN_PTR(address,
5932                                                 SharedRuntime::
5933                                                 throw_delayed_StackOverflowError));
5934     if (UseCRC32Intrinsics) {
5935       // set table address before stub generation which use it
5936       StubRoutines::_crc_table_adr = (address)StubRoutines::x86::_crc_table;
5937       StubRoutines::_updateBytesCRC32 = generate_updateBytesCRC32();
5938     }
5939 
5940     if (UseCRC32CIntrinsics) {
5941       bool supports_clmul = VM_Version::supports_clmul();
5942       StubRoutines::x86::generate_CRC32C_table(supports_clmul);
5943       StubRoutines::_crc32c_table_addr = (address)StubRoutines::x86::_crc32c_table;
5944       StubRoutines::_updateBytesCRC32C = generate_updateBytesCRC32C(supports_clmul);
5945     }
5946     if (VM_Version::supports_sse2() && UseLibmIntrinsic && InlineIntrinsics) {
5947       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dsin) ||
5948           vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dcos) ||
5949           vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dtan)) {
5950         StubRoutines::x86::_ONEHALF_adr = (address)StubRoutines::x86::_ONEHALF;
5951         StubRoutines::x86::_P_2_adr = (address)StubRoutines::x86::_P_2;
5952         StubRoutines::x86::_SC_4_adr = (address)StubRoutines::x86::_SC_4;
5953         StubRoutines::x86::_Ctable_adr = (address)StubRoutines::x86::_Ctable;
5954         StubRoutines::x86::_SC_2_adr = (address)StubRoutines::x86::_SC_2;
5955         StubRoutines::x86::_SC_3_adr = (address)StubRoutines::x86::_SC_3;
5956         StubRoutines::x86::_SC_1_adr = (address)StubRoutines::x86::_SC_1;
5957         StubRoutines::x86::_PI_INV_TABLE_adr = (address)StubRoutines::x86::_PI_INV_TABLE;
5958         StubRoutines::x86::_PI_4_adr = (address)StubRoutines::x86::_PI_4;
5959         StubRoutines::x86::_PI32INV_adr = (address)StubRoutines::x86::_PI32INV;
5960         StubRoutines::x86::_SIGN_MASK_adr = (address)StubRoutines::x86::_SIGN_MASK;
5961         StubRoutines::x86::_P_1_adr = (address)StubRoutines::x86::_P_1;
5962         StubRoutines::x86::_P_3_adr = (address)StubRoutines::x86::_P_3;
5963         StubRoutines::x86::_NEG_ZERO_adr = (address)StubRoutines::x86::_NEG_ZERO;
5964       }
5965       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dexp)) {
5966         StubRoutines::_dexp = generate_libmExp();
5967       }
5968       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dlog)) {
5969         StubRoutines::_dlog = generate_libmLog();
5970       }
5971       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dlog10)) {
5972         StubRoutines::_dlog10 = generate_libmLog10();
5973       }
5974       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dpow)) {
5975         StubRoutines::_dpow = generate_libmPow();
5976       }
5977       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dsin)) {
5978         StubRoutines::_dsin = generate_libmSin();
5979       }
5980       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dcos)) {
5981         StubRoutines::_dcos = generate_libmCos();
5982       }
5983       if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dtan)) {
5984         StubRoutines::_dtan = generate_libmTan();
5985       }
5986     }
5987 
5988     StubRoutines::_load_value_type_fields_in_regs = generate_return_value_stub(CAST_FROM_FN_PTR(address, SharedRuntime::load_value_type_fields_in_regs), "load_value_type_fields_in_regs", false);
5989     StubRoutines::_store_value_type_fields_to_buf = generate_return_value_stub(CAST_FROM_FN_PTR(address, SharedRuntime::store_value_type_fields_to_buf), "store_value_type_fields_to_buf", true);
5990   }
5991 
5992   void generate_all() {
5993     // Generates all stubs and initializes the entry points
5994 
5995     // These entry points require SharedInfo::stack0 to be set up in
5996     // non-core builds and need to be relocatable, so they each
5997     // fabricate a RuntimeStub internally.
5998     StubRoutines::_throw_AbstractMethodError_entry =
5999       generate_throw_exception("AbstractMethodError throw_exception",
6000                                CAST_FROM_FN_PTR(address,
6001                                                 SharedRuntime::
6002                                                 throw_AbstractMethodError));
6003 
6004     StubRoutines::_throw_IncompatibleClassChangeError_entry =
6005       generate_throw_exception("IncompatibleClassChangeError throw_exception",
6006                                CAST_FROM_FN_PTR(address,
6007                                                 SharedRuntime::
6008                                                 throw_IncompatibleClassChangeError));
6009 
6010     StubRoutines::_throw_NullPointerException_at_call_entry =
6011       generate_throw_exception("NullPointerException at call throw_exception",
6012                                CAST_FROM_FN_PTR(address,
6013                                                 SharedRuntime::
6014                                                 throw_NullPointerException_at_call));
6015 
6016     // entry points that are platform specific
6017     StubRoutines::x86::_f2i_fixup = generate_f2i_fixup();
6018     StubRoutines::x86::_f2l_fixup = generate_f2l_fixup();
6019     StubRoutines::x86::_d2i_fixup = generate_d2i_fixup();
6020     StubRoutines::x86::_d2l_fixup = generate_d2l_fixup();
6021 
6022     StubRoutines::x86::_float_sign_mask  = generate_fp_mask("float_sign_mask",  0x7FFFFFFF7FFFFFFF);
6023     StubRoutines::x86::_float_sign_flip  = generate_fp_mask("float_sign_flip",  0x8000000080000000);
6024     StubRoutines::x86::_double_sign_mask = generate_fp_mask("double_sign_mask", 0x7FFFFFFFFFFFFFFF);
6025     StubRoutines::x86::_double_sign_flip = generate_fp_mask("double_sign_flip", 0x8000000000000000);
6026 
6027     // support for verify_oop (must happen after universe_init)
6028     StubRoutines::_verify_oop_subroutine_entry = generate_verify_oop();
6029 
6030     // arraycopy stubs used by compilers
6031     generate_arraycopy_stubs();
6032 
6033     // don't bother generating these AES intrinsic stubs unless global flag is set
6034     if (UseAESIntrinsics) {
6035       StubRoutines::x86::_key_shuffle_mask_addr = generate_key_shuffle_mask();  // needed by the others
6036       StubRoutines::_aescrypt_encryptBlock = generate_aescrypt_encryptBlock();
6037       StubRoutines::_aescrypt_decryptBlock = generate_aescrypt_decryptBlock();
6038       StubRoutines::_cipherBlockChaining_encryptAESCrypt = generate_cipherBlockChaining_encryptAESCrypt();
6039       if (VM_Version::supports_vaes() &&  VM_Version::supports_avx512vl() && VM_Version::supports_avx512dq() ) {
6040         StubRoutines::_cipherBlockChaining_decryptAESCrypt = generate_cipherBlockChaining_decryptVectorAESCrypt();
6041       } else {
6042         StubRoutines::_cipherBlockChaining_decryptAESCrypt = generate_cipherBlockChaining_decryptAESCrypt_Parallel();
6043       }
6044     }
6045     if (UseAESCTRIntrinsics){
6046       StubRoutines::x86::_counter_shuffle_mask_addr = generate_counter_shuffle_mask();
6047       StubRoutines::_counterMode_AESCrypt = generate_counterMode_AESCrypt_Parallel();
6048     }
6049 
6050     if (UseSHA1Intrinsics) {
6051       StubRoutines::x86::_upper_word_mask_addr = generate_upper_word_mask();
6052       StubRoutines::x86::_shuffle_byte_flip_mask_addr = generate_shuffle_byte_flip_mask();
6053       StubRoutines::_sha1_implCompress = generate_sha1_implCompress(false, "sha1_implCompress");
6054       StubRoutines::_sha1_implCompressMB = generate_sha1_implCompress(true, "sha1_implCompressMB");
6055     }
6056     if (UseSHA256Intrinsics) {
6057       StubRoutines::x86::_k256_adr = (address)StubRoutines::x86::_k256;
6058       char* dst = (char*)StubRoutines::x86::_k256_W;
6059       char* src = (char*)StubRoutines::x86::_k256;
6060       for (int ii = 0; ii < 16; ++ii) {
6061         memcpy(dst + 32 * ii,      src + 16 * ii, 16);
6062         memcpy(dst + 32 * ii + 16, src + 16 * ii, 16);
6063       }
6064       StubRoutines::x86::_k256_W_adr = (address)StubRoutines::x86::_k256_W;
6065       StubRoutines::x86::_pshuffle_byte_flip_mask_addr = generate_pshuffle_byte_flip_mask();
6066       StubRoutines::_sha256_implCompress = generate_sha256_implCompress(false, "sha256_implCompress");
6067       StubRoutines::_sha256_implCompressMB = generate_sha256_implCompress(true, "sha256_implCompressMB");
6068     }
6069     if (UseSHA512Intrinsics) {
6070       StubRoutines::x86::_k512_W_addr = (address)StubRoutines::x86::_k512_W;
6071       StubRoutines::x86::_pshuffle_byte_flip_mask_addr_sha512 = generate_pshuffle_byte_flip_mask_sha512();
6072       StubRoutines::_sha512_implCompress = generate_sha512_implCompress(false, "sha512_implCompress");
6073       StubRoutines::_sha512_implCompressMB = generate_sha512_implCompress(true, "sha512_implCompressMB");
6074     }
6075 
6076     // Generate GHASH intrinsics code
6077     if (UseGHASHIntrinsics) {
6078     StubRoutines::x86::_ghash_long_swap_mask_addr = generate_ghash_long_swap_mask();
6079     StubRoutines::x86::_ghash_byte_swap_mask_addr = generate_ghash_byte_swap_mask();
6080       if (VM_Version::supports_avx()) {
6081         StubRoutines::x86::_ghash_shuffmask_addr = ghash_shufflemask_addr();
6082         StubRoutines::x86::_ghash_poly_addr = ghash_polynomial_addr();
6083         StubRoutines::_ghash_processBlocks = generate_avx_ghash_processBlocks();
6084       } else {
6085         StubRoutines::_ghash_processBlocks = generate_ghash_processBlocks();
6086       }
6087     }
6088 
6089     if (UseBASE64Intrinsics) {
6090       StubRoutines::x86::_and_mask = base64_and_mask_addr();
6091       StubRoutines::x86::_bswap_mask = base64_bswap_mask_addr();
6092       StubRoutines::x86::_base64_charset = base64_charset_addr();
6093       StubRoutines::x86::_url_charset = base64url_charset_addr();
6094       StubRoutines::x86::_gather_mask = base64_gather_mask_addr();
6095       StubRoutines::x86::_left_shift_mask = base64_left_shift_mask_addr();
6096       StubRoutines::x86::_right_shift_mask = base64_right_shift_mask_addr();
6097       StubRoutines::_base64_encodeBlock = generate_base64_encodeBlock();
6098     }
6099 
6100     // Safefetch stubs.
6101     generate_safefetch("SafeFetch32", sizeof(int),     &StubRoutines::_safefetch32_entry,
6102                                                        &StubRoutines::_safefetch32_fault_pc,
6103                                                        &StubRoutines::_safefetch32_continuation_pc);
6104     generate_safefetch("SafeFetchN", sizeof(intptr_t), &StubRoutines::_safefetchN_entry,
6105                                                        &StubRoutines::_safefetchN_fault_pc,
6106                                                        &StubRoutines::_safefetchN_continuation_pc);
6107 
6108     BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
6109     if (bs_nm != NULL) {
6110       StubRoutines::x86::_method_entry_barrier = generate_method_entry_barrier();
6111     }
6112 #ifdef COMPILER2
6113     if (UseMultiplyToLenIntrinsic) {
6114       StubRoutines::_multiplyToLen = generate_multiplyToLen();
6115     }
6116     if (UseSquareToLenIntrinsic) {
6117       StubRoutines::_squareToLen = generate_squareToLen();
6118     }
6119     if (UseMulAddIntrinsic) {
6120       StubRoutines::_mulAdd = generate_mulAdd();
6121     }
6122 #ifndef _WINDOWS
6123     if (UseMontgomeryMultiplyIntrinsic) {
6124       StubRoutines::_montgomeryMultiply
6125         = CAST_FROM_FN_PTR(address, SharedRuntime::montgomery_multiply);
6126     }
6127     if (UseMontgomerySquareIntrinsic) {
6128       StubRoutines::_montgomerySquare
6129         = CAST_FROM_FN_PTR(address, SharedRuntime::montgomery_square);
6130     }
6131 #endif // WINDOWS
6132 #endif // COMPILER2
6133 
6134     if (UseVectorizedMismatchIntrinsic) {
6135       StubRoutines::_vectorizedMismatch = generate_vectorizedMismatch();
6136     }
6137   }
6138 
6139  public:
6140   StubGenerator(CodeBuffer* code, bool all) : StubCodeGenerator(code) {
6141     if (all) {
6142       generate_all();
6143     } else {
6144       generate_initial();
6145     }
6146   }
6147 }; // end class declaration
6148 
6149 void StubGenerator_generate(CodeBuffer* code, bool all) {
6150   StubGenerator g(code, all);
6151 }