1 /*
   2  * Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/assembler.hpp"
  27 #include "asm/macroAssembler.hpp"
  28 #include "classfile/vmIntrinsics.hpp"
  29 #include "compiler/oopMap.hpp"
  30 #include "gc/shared/barrierSet.hpp"
  31 #include "gc/shared/barrierSetAssembler.hpp"
  32 #include "gc/shared/barrierSetNMethod.hpp"
  33 #include "gc/shared/gc_globals.hpp"
  34 #include "memory/universe.hpp"
  35 #include "prims/jvmtiExport.hpp"
  36 #include "runtime/arguments.hpp"
  37 #include "runtime/javaThread.hpp"
  38 #include "runtime/sharedRuntime.hpp"
  39 #include "runtime/stubRoutines.hpp"
  40 #include "utilities/macros.hpp"
  41 #include "vmreg_x86.inline.hpp"
  42 #include "stubGenerator_x86_64.hpp"
  43 #ifdef COMPILER2
  44 #include "opto/runtime.hpp"
  45 #include "opto/c2_globals.hpp"
  46 #endif
  47 #if INCLUDE_JVMCI
  48 #include "jvmci/jvmci_globals.hpp"
  49 #endif
  50 #if INCLUDE_ZGC
  51 #include "gc/z/zThreadLocalData.hpp"
  52 #endif
  53 #if INCLUDE_JFR
  54 #include "jfr/support/jfrIntrinsics.hpp"
  55 #endif
  56 
  57 // For a more detailed description of the stub routine structure
  58 // see the comment in stubRoutines.hpp
  59 
  60 #define __ _masm->
  61 #define TIMES_OOP (UseCompressedOops ? Address::times_4 : Address::times_8)
  62 
  63 #ifdef PRODUCT
  64 #define BLOCK_COMMENT(str) /* nothing */
  65 #else
  66 #define BLOCK_COMMENT(str) __ block_comment(str)
  67 #endif // PRODUCT
  68 
  69 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
  70 
  71 //
  72 // Linux Arguments:
  73 //    c_rarg0:   call wrapper address                   address
  74 //    c_rarg1:   result                                 address
  75 //    c_rarg2:   result type                            BasicType
  76 //    c_rarg3:   method                                 Method*
  77 //    c_rarg4:   (interpreter) entry point              address
  78 //    c_rarg5:   parameters                             intptr_t*
  79 //    16(rbp): parameter size (in words)              int
  80 //    24(rbp): thread                                 Thread*
  81 //
  82 //     [ return_from_Java     ] <--- rsp
  83 //     [ argument word n      ]
  84 //      ...
  85 // -12 [ argument word 1      ]
  86 // -11 [ saved r15            ] <--- rsp_after_call
  87 // -10 [ saved r14            ]
  88 //  -9 [ saved r13            ]
  89 //  -8 [ saved r12            ]
  90 //  -7 [ saved rbx            ]
  91 //  -6 [ call wrapper         ]
  92 //  -5 [ result               ]
  93 //  -4 [ result type          ]
  94 //  -3 [ method               ]
  95 //  -2 [ entry point          ]
  96 //  -1 [ parameters           ]
  97 //   0 [ saved rbp            ] <--- rbp
  98 //   1 [ return address       ]
  99 //   2 [ parameter size       ]
 100 //   3 [ thread               ]
 101 //
 102 // Windows Arguments:
 103 //    c_rarg0:   call wrapper address                   address
 104 //    c_rarg1:   result                                 address
 105 //    c_rarg2:   result type                            BasicType
 106 //    c_rarg3:   method                                 Method*
 107 //    48(rbp): (interpreter) entry point              address
 108 //    56(rbp): parameters                             intptr_t*
 109 //    64(rbp): parameter size (in words)              int
 110 //    72(rbp): thread                                 Thread*
 111 //
 112 //     [ return_from_Java     ] <--- rsp
 113 //     [ argument word n      ]
 114 //      ...
 115 // -60 [ argument word 1      ]
 116 // -59 [ saved xmm31          ] <--- rsp after_call
 117 //     [ saved xmm16-xmm30    ] (EVEX enabled, else the space is blank)
 118 // -27 [ saved xmm15          ]
 119 //     [ saved xmm7-xmm14     ]
 120 //  -9 [ saved xmm6           ] (each xmm register takes 2 slots)
 121 //  -7 [ saved r15            ]
 122 //  -6 [ saved r14            ]
 123 //  -5 [ saved r13            ]
 124 //  -4 [ saved r12            ]
 125 //  -3 [ saved rdi            ]
 126 //  -2 [ saved rsi            ]
 127 //  -1 [ saved rbx            ]
 128 //   0 [ saved rbp            ] <--- rbp
 129 //   1 [ return address       ]
 130 //   2 [ call wrapper         ]
 131 //   3 [ result               ]
 132 //   4 [ result type          ]
 133 //   5 [ method               ]
 134 //   6 [ entry point          ]
 135 //   7 [ parameters           ]
 136 //   8 [ parameter size       ]
 137 //   9 [ thread               ]
 138 //
 139 //    Windows reserves the callers stack space for arguments 1-4.
 140 //    We spill c_rarg0-c_rarg3 to this space.
 141 
 142 // Call stub stack layout word offsets from rbp
 143 #ifdef _WIN64
 144 enum call_stub_layout {
 145   xmm_save_first     = 6,  // save from xmm6
 146   xmm_save_last      = 31, // to xmm31
 147   xmm_save_base      = -9,
 148   rsp_after_call_off = xmm_save_base - 2 * (xmm_save_last - xmm_save_first), // -27
 149   r15_off            = -7,
 150   r14_off            = -6,
 151   r13_off            = -5,
 152   r12_off            = -4,
 153   rdi_off            = -3,
 154   rsi_off            = -2,
 155   rbx_off            = -1,
 156   rbp_off            =  0,
 157   retaddr_off        =  1,
 158   call_wrapper_off   =  2,
 159   result_off         =  3,
 160   result_type_off    =  4,
 161   method_off         =  5,
 162   entry_point_off    =  6,
 163   parameters_off     =  7,
 164   parameter_size_off =  8,
 165   thread_off         =  9
 166 };
 167 
 168 static Address xmm_save(int reg) {
 169   assert(reg >= xmm_save_first && reg <= xmm_save_last, "XMM register number out of range");
 170   return Address(rbp, (xmm_save_base - (reg - xmm_save_first) * 2) * wordSize);
 171 }
 172 #else // !_WIN64
 173 enum call_stub_layout {
 174   rsp_after_call_off = -12,
 175   mxcsr_off          = rsp_after_call_off,
 176   r15_off            = -11,
 177   r14_off            = -10,
 178   r13_off            = -9,
 179   r12_off            = -8,
 180   rbx_off            = -7,
 181   call_wrapper_off   = -6,
 182   result_off         = -5,
 183   result_type_off    = -4,
 184   method_off         = -3,
 185   entry_point_off    = -2,
 186   parameters_off     = -1,
 187   rbp_off            =  0,
 188   retaddr_off        =  1,
 189   parameter_size_off =  2,
 190   thread_off         =  3
 191 };
 192 #endif // _WIN64
 193 
 194 address StubGenerator::generate_call_stub(address& return_address) {
 195 
 196   assert((int)frame::entry_frame_after_call_words == -(int)rsp_after_call_off + 1 &&
 197          (int)frame::entry_frame_call_wrapper_offset == (int)call_wrapper_off,
 198          "adjust this code");
 199   StubCodeMark mark(this, "StubRoutines", "call_stub");
 200   address start = __ pc();
 201 
 202   // same as in generate_catch_exception()!
 203   const Address rsp_after_call(rbp, rsp_after_call_off * wordSize);
 204 
 205   const Address call_wrapper  (rbp, call_wrapper_off   * wordSize);
 206   const Address result        (rbp, result_off         * wordSize);
 207   const Address result_type   (rbp, result_type_off    * wordSize);
 208   const Address method        (rbp, method_off         * wordSize);
 209   const Address entry_point   (rbp, entry_point_off    * wordSize);
 210   const Address parameters    (rbp, parameters_off     * wordSize);
 211   const Address parameter_size(rbp, parameter_size_off * wordSize);
 212 
 213   // same as in generate_catch_exception()!
 214   const Address thread        (rbp, thread_off         * wordSize);
 215 
 216   const Address r15_save(rbp, r15_off * wordSize);
 217   const Address r14_save(rbp, r14_off * wordSize);
 218   const Address r13_save(rbp, r13_off * wordSize);
 219   const Address r12_save(rbp, r12_off * wordSize);
 220   const Address rbx_save(rbp, rbx_off * wordSize);
 221 
 222   // stub code
 223   __ enter();
 224   __ subptr(rsp, -rsp_after_call_off * wordSize);
 225 
 226   // save register parameters
 227 #ifndef _WIN64
 228   __ movptr(parameters,   c_rarg5); // parameters
 229   __ movptr(entry_point,  c_rarg4); // entry_point
 230 #endif
 231 
 232   __ movptr(method,       c_rarg3); // method
 233   __ movl(result_type,  c_rarg2);   // result type
 234   __ movptr(result,       c_rarg1); // result
 235   __ movptr(call_wrapper, c_rarg0); // call wrapper
 236 
 237   // save regs belonging to calling function
 238   __ movptr(rbx_save, rbx);
 239   __ movptr(r12_save, r12);
 240   __ movptr(r13_save, r13);
 241   __ movptr(r14_save, r14);
 242   __ movptr(r15_save, r15);
 243 
 244 #ifdef _WIN64
 245   int last_reg = 15;
 246   if (UseAVX > 2) {
 247     last_reg = 31;
 248   }
 249   if (VM_Version::supports_evex()) {
 250     for (int i = xmm_save_first; i <= last_reg; i++) {
 251       __ vextractf32x4(xmm_save(i), as_XMMRegister(i), 0);
 252     }
 253   } else {
 254     for (int i = xmm_save_first; i <= last_reg; i++) {
 255       __ movdqu(xmm_save(i), as_XMMRegister(i));
 256     }
 257   }
 258 
 259   const Address rdi_save(rbp, rdi_off * wordSize);
 260   const Address rsi_save(rbp, rsi_off * wordSize);
 261 
 262   __ movptr(rsi_save, rsi);
 263   __ movptr(rdi_save, rdi);
 264 #else
 265   const Address mxcsr_save(rbp, mxcsr_off * wordSize);
 266   {
 267     Label skip_ldmx;
 268     __ stmxcsr(mxcsr_save);
 269     __ movl(rax, mxcsr_save);
 270     __ andl(rax, 0xFFC0); // Mask out any pending exceptions (only check control and mask bits)
 271     ExternalAddress mxcsr_std(StubRoutines::x86::addr_mxcsr_std());
 272     __ cmp32(rax, mxcsr_std, rscratch1);
 273     __ jcc(Assembler::equal, skip_ldmx);
 274     __ ldmxcsr(mxcsr_std, rscratch1);
 275     __ bind(skip_ldmx);
 276   }
 277 #endif
 278 
 279   // Load up thread register
 280   __ movptr(r15_thread, thread);
 281   __ reinit_heapbase();
 282 
 283 #ifdef ASSERT
 284   // make sure we have no pending exceptions
 285   {
 286     Label L;
 287     __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), NULL_WORD);
 288     __ jcc(Assembler::equal, L);
 289     __ stop("StubRoutines::call_stub: entered with pending exception");
 290     __ bind(L);
 291   }
 292 #endif
 293 
 294   // pass parameters if any
 295   BLOCK_COMMENT("pass parameters if any");
 296   Label parameters_done;
 297   __ movl(c_rarg3, parameter_size);
 298   __ testl(c_rarg3, c_rarg3);
 299   __ jcc(Assembler::zero, parameters_done);
 300 
 301   Label loop;
 302   __ movptr(c_rarg2, parameters);       // parameter pointer
 303   __ movl(c_rarg1, c_rarg3);            // parameter counter is in c_rarg1
 304   __ BIND(loop);
 305   __ movptr(rax, Address(c_rarg2, 0));// get parameter
 306   __ addptr(c_rarg2, wordSize);       // advance to next parameter
 307   __ decrementl(c_rarg1);             // decrement counter
 308   __ push(rax);                       // pass parameter
 309   __ jcc(Assembler::notZero, loop);
 310 
 311   // call Java function
 312   __ BIND(parameters_done);
 313   __ movptr(rbx, method);             // get Method*
 314   __ movptr(c_rarg1, entry_point);    // get entry_point
 315   __ mov(r13, rsp);                   // set sender sp
 316   BLOCK_COMMENT("call Java function");
 317   __ call(c_rarg1);
 318 
 319   BLOCK_COMMENT("call_stub_return_address:");
 320   return_address = __ pc();
 321 
 322   // store result depending on type (everything that is not
 323   // T_OBJECT, T_PRIMITIVE_OBJECT, T_LONG, T_FLOAT or T_DOUBLE is treated as T_INT)
 324   __ movptr(r13, result);
 325   Label is_long, is_float, is_double, check_prim, exit;
 326   __ movl(rbx, result_type);
 327   __ cmpl(rbx, T_OBJECT);
 328   __ jcc(Assembler::equal, check_prim);
 329   __ cmpl(rbx, T_PRIMITIVE_OBJECT);
 330   __ jcc(Assembler::equal, check_prim);
 331   __ cmpl(rbx, T_LONG);
 332   __ jcc(Assembler::equal, is_long);
 333   __ cmpl(rbx, T_FLOAT);
 334   __ jcc(Assembler::equal, is_float);
 335   __ cmpl(rbx, T_DOUBLE);
 336   __ jcc(Assembler::equal, is_double);
 337 
 338   // handle T_INT case
 339   __ movl(Address(r13, 0), rax);
 340 
 341   __ BIND(exit);
 342 
 343   // pop parameters
 344   __ lea(rsp, rsp_after_call);
 345 
 346 #ifdef ASSERT
 347   // verify that threads correspond
 348   {
 349    Label L1, L2, L3;
 350     __ cmpptr(r15_thread, thread);
 351     __ jcc(Assembler::equal, L1);
 352     __ stop("StubRoutines::call_stub: r15_thread is corrupted");
 353     __ bind(L1);
 354     __ get_thread(rbx);
 355     __ cmpptr(r15_thread, thread);
 356     __ jcc(Assembler::equal, L2);
 357     __ stop("StubRoutines::call_stub: r15_thread is modified by call");
 358     __ bind(L2);
 359     __ cmpptr(r15_thread, rbx);
 360     __ jcc(Assembler::equal, L3);
 361     __ stop("StubRoutines::call_stub: threads must correspond");
 362     __ bind(L3);
 363   }
 364 #endif
 365 
 366   __ pop_cont_fastpath();
 367 
 368   // restore regs belonging to calling function
 369 #ifdef _WIN64
 370   // emit the restores for xmm regs
 371   if (VM_Version::supports_evex()) {
 372     for (int i = xmm_save_first; i <= last_reg; i++) {
 373       __ vinsertf32x4(as_XMMRegister(i), as_XMMRegister(i), xmm_save(i), 0);
 374     }
 375   } else {
 376     for (int i = xmm_save_first; i <= last_reg; i++) {
 377       __ movdqu(as_XMMRegister(i), xmm_save(i));
 378     }
 379   }
 380 #endif
 381   __ movptr(r15, r15_save);
 382   __ movptr(r14, r14_save);
 383   __ movptr(r13, r13_save);
 384   __ movptr(r12, r12_save);
 385   __ movptr(rbx, rbx_save);
 386 
 387 #ifdef _WIN64
 388   __ movptr(rdi, rdi_save);
 389   __ movptr(rsi, rsi_save);
 390 #else
 391   __ ldmxcsr(mxcsr_save);
 392 #endif
 393 
 394   // restore rsp
 395   __ addptr(rsp, -rsp_after_call_off * wordSize);
 396 
 397   // return
 398   __ vzeroupper();
 399   __ pop(rbp);
 400   __ ret(0);
 401 
 402   // handle return types different from T_INT
 403   __ BIND(check_prim);
 404   if (InlineTypeReturnedAsFields) {
 405     // Check for scalarized return value
 406     __ testptr(rax, 1);
 407     __ jcc(Assembler::zero, is_long);
 408     // Load pack handler address
 409     __ andptr(rax, -2);
 410     __ movptr(rax, Address(rax, InstanceKlass::adr_inlineklass_fixed_block_offset()));
 411     __ movptr(rbx, Address(rax, InlineKlass::pack_handler_jobject_offset()));
 412     // Call pack handler to initialize the buffer
 413     __ call(rbx);
 414     __ jmp(exit);
 415   }
 416   __ BIND(is_long);
 417   __ movq(Address(r13, 0), rax);
 418   __ jmp(exit);
 419 
 420   __ BIND(is_float);
 421   __ movflt(Address(r13, 0), xmm0);
 422   __ jmp(exit);
 423 
 424   __ BIND(is_double);
 425   __ movdbl(Address(r13, 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 StubGenerator::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 StubGenerator::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()), NULL_WORD);
 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()), 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 intptr_t OrderAccess::fence()
 557 //
 558 // Arguments :
 559 //
 560 // Result:
 561 address StubGenerator::generate_orderaccess_fence() {
 562   StubCodeMark mark(this, "StubRoutines", "orderaccess_fence");
 563   address start = __ pc();
 564 
 565   __ membar(Assembler::StoreLoad);
 566   __ ret(0);
 567 
 568   return start;
 569 }
 570 
 571 
 572 // Support for intptr_t get_previous_sp()
 573 //
 574 // This routine is used to find the previous stack pointer for the
 575 // caller.
 576 address StubGenerator::generate_get_previous_sp() {
 577   StubCodeMark mark(this, "StubRoutines", "get_previous_sp");
 578   address start = __ pc();
 579 
 580   __ movptr(rax, rsp);
 581   __ addptr(rax, 8); // return address is at the top of the stack.
 582   __ ret(0);
 583 
 584   return start;
 585 }
 586 
 587 //----------------------------------------------------------------------------------------------------
 588 // Support for void verify_mxcsr()
 589 //
 590 // This routine is used with -Xcheck:jni to verify that native
 591 // JNI code does not return to Java code without restoring the
 592 // MXCSR register to our expected state.
 593 
 594 address StubGenerator::generate_verify_mxcsr() {
 595   StubCodeMark mark(this, "StubRoutines", "verify_mxcsr");
 596   address start = __ pc();
 597 
 598   const Address mxcsr_save(rsp, 0);
 599 
 600   if (CheckJNICalls) {
 601     Label ok_ret;
 602     ExternalAddress mxcsr_std(StubRoutines::x86::addr_mxcsr_std());
 603     __ push(rax);
 604     __ subptr(rsp, wordSize);      // allocate a temp location
 605     __ stmxcsr(mxcsr_save);
 606     __ movl(rax, mxcsr_save);
 607     __ andl(rax, 0xFFC0); // Mask out any pending exceptions (only check control and mask bits)
 608     __ cmp32(rax, mxcsr_std, rscratch1);
 609     __ jcc(Assembler::equal, ok_ret);
 610 
 611     __ warn("MXCSR changed by native JNI code, use -XX:+RestoreMXCSROnJNICall");
 612 
 613     __ ldmxcsr(mxcsr_std, rscratch1);
 614 
 615     __ bind(ok_ret);
 616     __ addptr(rsp, wordSize);
 617     __ pop(rax);
 618   }
 619 
 620   __ ret(0);
 621 
 622   return start;
 623 }
 624 
 625 address StubGenerator::generate_f2i_fixup() {
 626   StubCodeMark mark(this, "StubRoutines", "f2i_fixup");
 627   Address inout(rsp, 5 * wordSize); // return address + 4 saves
 628 
 629   address start = __ pc();
 630 
 631   Label L;
 632 
 633   __ push(rax);
 634   __ push(c_rarg3);
 635   __ push(c_rarg2);
 636   __ push(c_rarg1);
 637 
 638   __ movl(rax, 0x7f800000);
 639   __ xorl(c_rarg3, c_rarg3);
 640   __ movl(c_rarg2, inout);
 641   __ movl(c_rarg1, c_rarg2);
 642   __ andl(c_rarg1, 0x7fffffff);
 643   __ cmpl(rax, c_rarg1); // NaN? -> 0
 644   __ jcc(Assembler::negative, L);
 645   __ testl(c_rarg2, c_rarg2); // signed ? min_jint : max_jint
 646   __ movl(c_rarg3, 0x80000000);
 647   __ movl(rax, 0x7fffffff);
 648   __ cmovl(Assembler::positive, c_rarg3, rax);
 649 
 650   __ bind(L);
 651   __ movptr(inout, c_rarg3);
 652 
 653   __ pop(c_rarg1);
 654   __ pop(c_rarg2);
 655   __ pop(c_rarg3);
 656   __ pop(rax);
 657 
 658   __ ret(0);
 659 
 660   return start;
 661 }
 662 
 663 address StubGenerator::generate_f2l_fixup() {
 664   StubCodeMark mark(this, "StubRoutines", "f2l_fixup");
 665   Address inout(rsp, 5 * wordSize); // return address + 4 saves
 666   address start = __ pc();
 667 
 668   Label L;
 669 
 670   __ push(rax);
 671   __ push(c_rarg3);
 672   __ push(c_rarg2);
 673   __ push(c_rarg1);
 674 
 675   __ movl(rax, 0x7f800000);
 676   __ xorl(c_rarg3, c_rarg3);
 677   __ movl(c_rarg2, inout);
 678   __ movl(c_rarg1, c_rarg2);
 679   __ andl(c_rarg1, 0x7fffffff);
 680   __ cmpl(rax, c_rarg1); // NaN? -> 0
 681   __ jcc(Assembler::negative, L);
 682   __ testl(c_rarg2, c_rarg2); // signed ? min_jlong : max_jlong
 683   __ mov64(c_rarg3, 0x8000000000000000);
 684   __ mov64(rax, 0x7fffffffffffffff);
 685   __ cmov(Assembler::positive, c_rarg3, rax);
 686 
 687   __ bind(L);
 688   __ movptr(inout, c_rarg3);
 689 
 690   __ pop(c_rarg1);
 691   __ pop(c_rarg2);
 692   __ pop(c_rarg3);
 693   __ pop(rax);
 694 
 695   __ ret(0);
 696 
 697   return start;
 698 }
 699 
 700 address StubGenerator::generate_d2i_fixup() {
 701   StubCodeMark mark(this, "StubRoutines", "d2i_fixup");
 702   Address inout(rsp, 6 * wordSize); // return address + 5 saves
 703 
 704   address start = __ pc();
 705 
 706   Label L;
 707 
 708   __ push(rax);
 709   __ push(c_rarg3);
 710   __ push(c_rarg2);
 711   __ push(c_rarg1);
 712   __ push(c_rarg0);
 713 
 714   __ movl(rax, 0x7ff00000);
 715   __ movq(c_rarg2, inout);
 716   __ movl(c_rarg3, c_rarg2);
 717   __ mov(c_rarg1, c_rarg2);
 718   __ mov(c_rarg0, c_rarg2);
 719   __ negl(c_rarg3);
 720   __ shrptr(c_rarg1, 0x20);
 721   __ orl(c_rarg3, c_rarg2);
 722   __ andl(c_rarg1, 0x7fffffff);
 723   __ xorl(c_rarg2, c_rarg2);
 724   __ shrl(c_rarg3, 0x1f);
 725   __ orl(c_rarg1, c_rarg3);
 726   __ cmpl(rax, c_rarg1);
 727   __ jcc(Assembler::negative, L); // NaN -> 0
 728   __ testptr(c_rarg0, c_rarg0); // signed ? min_jint : max_jint
 729   __ movl(c_rarg2, 0x80000000);
 730   __ movl(rax, 0x7fffffff);
 731   __ cmov(Assembler::positive, c_rarg2, rax);
 732 
 733   __ bind(L);
 734   __ movptr(inout, c_rarg2);
 735 
 736   __ pop(c_rarg0);
 737   __ pop(c_rarg1);
 738   __ pop(c_rarg2);
 739   __ pop(c_rarg3);
 740   __ pop(rax);
 741 
 742   __ ret(0);
 743 
 744   return start;
 745 }
 746 
 747 address StubGenerator::generate_d2l_fixup() {
 748   StubCodeMark mark(this, "StubRoutines", "d2l_fixup");
 749   Address inout(rsp, 6 * wordSize); // return address + 5 saves
 750 
 751   address start = __ pc();
 752 
 753   Label L;
 754 
 755   __ push(rax);
 756   __ push(c_rarg3);
 757   __ push(c_rarg2);
 758   __ push(c_rarg1);
 759   __ push(c_rarg0);
 760 
 761   __ movl(rax, 0x7ff00000);
 762   __ movq(c_rarg2, inout);
 763   __ movl(c_rarg3, c_rarg2);
 764   __ mov(c_rarg1, c_rarg2);
 765   __ mov(c_rarg0, c_rarg2);
 766   __ negl(c_rarg3);
 767   __ shrptr(c_rarg1, 0x20);
 768   __ orl(c_rarg3, c_rarg2);
 769   __ andl(c_rarg1, 0x7fffffff);
 770   __ xorl(c_rarg2, c_rarg2);
 771   __ shrl(c_rarg3, 0x1f);
 772   __ orl(c_rarg1, c_rarg3);
 773   __ cmpl(rax, c_rarg1);
 774   __ jcc(Assembler::negative, L); // NaN -> 0
 775   __ testq(c_rarg0, c_rarg0); // signed ? min_jlong : max_jlong
 776   __ mov64(c_rarg2, 0x8000000000000000);
 777   __ mov64(rax, 0x7fffffffffffffff);
 778   __ cmovq(Assembler::positive, c_rarg2, rax);
 779 
 780   __ bind(L);
 781   __ movq(inout, c_rarg2);
 782 
 783   __ pop(c_rarg0);
 784   __ pop(c_rarg1);
 785   __ pop(c_rarg2);
 786   __ pop(c_rarg3);
 787   __ pop(rax);
 788 
 789   __ ret(0);
 790 
 791   return start;
 792 }
 793 
 794 address StubGenerator::generate_count_leading_zeros_lut(const char *stub_name) {
 795   __ align64();
 796   StubCodeMark mark(this, "StubRoutines", stub_name);
 797   address start = __ pc();
 798 
 799   __ emit_data64(0x0101010102020304, relocInfo::none);
 800   __ emit_data64(0x0000000000000000, relocInfo::none);
 801   __ emit_data64(0x0101010102020304, relocInfo::none);
 802   __ emit_data64(0x0000000000000000, relocInfo::none);
 803   __ emit_data64(0x0101010102020304, relocInfo::none);
 804   __ emit_data64(0x0000000000000000, relocInfo::none);
 805   __ emit_data64(0x0101010102020304, relocInfo::none);
 806   __ emit_data64(0x0000000000000000, relocInfo::none);
 807 
 808   return start;
 809 }
 810 
 811 address StubGenerator::generate_popcount_avx_lut(const char *stub_name) {
 812   __ align64();
 813   StubCodeMark mark(this, "StubRoutines", stub_name);
 814   address start = __ pc();
 815 
 816   __ emit_data64(0x0302020102010100, relocInfo::none);
 817   __ emit_data64(0x0403030203020201, relocInfo::none);
 818   __ emit_data64(0x0302020102010100, relocInfo::none);
 819   __ emit_data64(0x0403030203020201, relocInfo::none);
 820   __ emit_data64(0x0302020102010100, relocInfo::none);
 821   __ emit_data64(0x0403030203020201, relocInfo::none);
 822   __ emit_data64(0x0302020102010100, relocInfo::none);
 823   __ emit_data64(0x0403030203020201, relocInfo::none);
 824 
 825   return start;
 826 }
 827 
 828 address StubGenerator::generate_iota_indices(const char *stub_name) {
 829   __ align(CodeEntryAlignment);
 830   StubCodeMark mark(this, "StubRoutines", stub_name);
 831   address start = __ pc();
 832 
 833   __ emit_data64(0x0706050403020100, relocInfo::none);
 834   __ emit_data64(0x0F0E0D0C0B0A0908, relocInfo::none);
 835   __ emit_data64(0x1716151413121110, relocInfo::none);
 836   __ emit_data64(0x1F1E1D1C1B1A1918, relocInfo::none);
 837   __ emit_data64(0x2726252423222120, relocInfo::none);
 838   __ emit_data64(0x2F2E2D2C2B2A2928, relocInfo::none);
 839   __ emit_data64(0x3736353433323130, relocInfo::none);
 840   __ emit_data64(0x3F3E3D3C3B3A3938, relocInfo::none);
 841 
 842   return start;
 843 }
 844 
 845 address StubGenerator::generate_vector_reverse_bit_lut(const char *stub_name) {
 846   __ align(CodeEntryAlignment);
 847   StubCodeMark mark(this, "StubRoutines", stub_name);
 848   address start = __ pc();
 849 
 850   __ emit_data64(0x0E060A020C040800, relocInfo::none);
 851   __ emit_data64(0x0F070B030D050901, relocInfo::none);
 852   __ emit_data64(0x0E060A020C040800, relocInfo::none);
 853   __ emit_data64(0x0F070B030D050901, relocInfo::none);
 854   __ emit_data64(0x0E060A020C040800, relocInfo::none);
 855   __ emit_data64(0x0F070B030D050901, relocInfo::none);
 856   __ emit_data64(0x0E060A020C040800, relocInfo::none);
 857   __ emit_data64(0x0F070B030D050901, relocInfo::none);
 858 
 859   return start;
 860 }
 861 
 862 address StubGenerator::generate_vector_reverse_byte_perm_mask_long(const char *stub_name) {
 863   __ align(CodeEntryAlignment);
 864   StubCodeMark mark(this, "StubRoutines", stub_name);
 865   address start = __ pc();
 866 
 867   __ emit_data64(0x0001020304050607, relocInfo::none);
 868   __ emit_data64(0x08090A0B0C0D0E0F, relocInfo::none);
 869   __ emit_data64(0x0001020304050607, relocInfo::none);
 870   __ emit_data64(0x08090A0B0C0D0E0F, relocInfo::none);
 871   __ emit_data64(0x0001020304050607, relocInfo::none);
 872   __ emit_data64(0x08090A0B0C0D0E0F, relocInfo::none);
 873   __ emit_data64(0x0001020304050607, relocInfo::none);
 874   __ emit_data64(0x08090A0B0C0D0E0F, relocInfo::none);
 875 
 876   return start;
 877 }
 878 
 879 address StubGenerator::generate_vector_reverse_byte_perm_mask_int(const char *stub_name) {
 880   __ align(CodeEntryAlignment);
 881   StubCodeMark mark(this, "StubRoutines", stub_name);
 882   address start = __ pc();
 883 
 884   __ emit_data64(0x0405060700010203, relocInfo::none);
 885   __ emit_data64(0x0C0D0E0F08090A0B, relocInfo::none);
 886   __ emit_data64(0x0405060700010203, relocInfo::none);
 887   __ emit_data64(0x0C0D0E0F08090A0B, relocInfo::none);
 888   __ emit_data64(0x0405060700010203, relocInfo::none);
 889   __ emit_data64(0x0C0D0E0F08090A0B, relocInfo::none);
 890   __ emit_data64(0x0405060700010203, relocInfo::none);
 891   __ emit_data64(0x0C0D0E0F08090A0B, relocInfo::none);
 892 
 893   return start;
 894 }
 895 
 896 address StubGenerator::generate_vector_reverse_byte_perm_mask_short(const char *stub_name) {
 897   __ align(CodeEntryAlignment);
 898   StubCodeMark mark(this, "StubRoutines", stub_name);
 899   address start = __ pc();
 900 
 901   __ emit_data64(0x0607040502030001, relocInfo::none);
 902   __ emit_data64(0x0E0F0C0D0A0B0809, relocInfo::none);
 903   __ emit_data64(0x0607040502030001, relocInfo::none);
 904   __ emit_data64(0x0E0F0C0D0A0B0809, relocInfo::none);
 905   __ emit_data64(0x0607040502030001, relocInfo::none);
 906   __ emit_data64(0x0E0F0C0D0A0B0809, relocInfo::none);
 907   __ emit_data64(0x0607040502030001, relocInfo::none);
 908   __ emit_data64(0x0E0F0C0D0A0B0809, relocInfo::none);
 909 
 910   return start;
 911 }
 912 
 913 address StubGenerator::generate_vector_byte_shuffle_mask(const char *stub_name) {
 914   __ align(CodeEntryAlignment);
 915   StubCodeMark mark(this, "StubRoutines", stub_name);
 916   address start = __ pc();
 917 
 918   __ emit_data64(0x7070707070707070, relocInfo::none);
 919   __ emit_data64(0x7070707070707070, relocInfo::none);
 920   __ emit_data64(0xF0F0F0F0F0F0F0F0, relocInfo::none);
 921   __ emit_data64(0xF0F0F0F0F0F0F0F0, relocInfo::none);
 922 
 923   return start;
 924 }
 925 
 926 address StubGenerator::generate_fp_mask(const char *stub_name, int64_t mask) {
 927   __ align(CodeEntryAlignment);
 928   StubCodeMark mark(this, "StubRoutines", stub_name);
 929   address start = __ pc();
 930 
 931   __ emit_data64( mask, relocInfo::none );
 932   __ emit_data64( mask, relocInfo::none );
 933 
 934   return start;
 935 }
 936 
 937 address StubGenerator::generate_vector_mask(const char *stub_name, int64_t mask) {
 938   __ align(CodeEntryAlignment);
 939   StubCodeMark mark(this, "StubRoutines", stub_name);
 940   address start = __ pc();
 941 
 942   __ emit_data64(mask, relocInfo::none);
 943   __ emit_data64(mask, relocInfo::none);
 944   __ emit_data64(mask, relocInfo::none);
 945   __ emit_data64(mask, relocInfo::none);
 946   __ emit_data64(mask, relocInfo::none);
 947   __ emit_data64(mask, relocInfo::none);
 948   __ emit_data64(mask, relocInfo::none);
 949   __ emit_data64(mask, relocInfo::none);
 950 
 951   return start;
 952 }
 953 
 954 address StubGenerator::generate_vector_byte_perm_mask(const char *stub_name) {
 955   __ align(CodeEntryAlignment);
 956   StubCodeMark mark(this, "StubRoutines", stub_name);
 957   address start = __ pc();
 958 
 959   __ emit_data64(0x0000000000000001, relocInfo::none);
 960   __ emit_data64(0x0000000000000003, relocInfo::none);
 961   __ emit_data64(0x0000000000000005, relocInfo::none);
 962   __ emit_data64(0x0000000000000007, relocInfo::none);
 963   __ emit_data64(0x0000000000000000, relocInfo::none);
 964   __ emit_data64(0x0000000000000002, relocInfo::none);
 965   __ emit_data64(0x0000000000000004, relocInfo::none);
 966   __ emit_data64(0x0000000000000006, relocInfo::none);
 967 
 968   return start;
 969 }
 970 
 971 address StubGenerator::generate_vector_fp_mask(const char *stub_name, int64_t mask) {
 972   __ align(CodeEntryAlignment);
 973   StubCodeMark mark(this, "StubRoutines", stub_name);
 974   address start = __ pc();
 975 
 976   __ emit_data64(mask, relocInfo::none);
 977   __ emit_data64(mask, relocInfo::none);
 978   __ emit_data64(mask, relocInfo::none);
 979   __ emit_data64(mask, relocInfo::none);
 980   __ emit_data64(mask, relocInfo::none);
 981   __ emit_data64(mask, relocInfo::none);
 982   __ emit_data64(mask, relocInfo::none);
 983   __ emit_data64(mask, relocInfo::none);
 984 
 985   return start;
 986 }
 987 
 988 address StubGenerator::generate_vector_custom_i32(const char *stub_name, Assembler::AvxVectorLen len,
 989                                    int32_t val0, int32_t val1, int32_t val2, int32_t val3,
 990                                    int32_t val4, int32_t val5, int32_t val6, int32_t val7,
 991                                    int32_t val8, int32_t val9, int32_t val10, int32_t val11,
 992                                    int32_t val12, int32_t val13, int32_t val14, int32_t val15) {
 993   __ align(CodeEntryAlignment);
 994   StubCodeMark mark(this, "StubRoutines", stub_name);
 995   address start = __ pc();
 996 
 997   assert(len != Assembler::AVX_NoVec, "vector len must be specified");
 998   __ emit_data(val0, relocInfo::none, 0);
 999   __ emit_data(val1, relocInfo::none, 0);
1000   __ emit_data(val2, relocInfo::none, 0);
1001   __ emit_data(val3, relocInfo::none, 0);
1002   if (len >= Assembler::AVX_256bit) {
1003     __ emit_data(val4, relocInfo::none, 0);
1004     __ emit_data(val5, relocInfo::none, 0);
1005     __ emit_data(val6, relocInfo::none, 0);
1006     __ emit_data(val7, relocInfo::none, 0);
1007     if (len >= Assembler::AVX_512bit) {
1008       __ emit_data(val8, relocInfo::none, 0);
1009       __ emit_data(val9, relocInfo::none, 0);
1010       __ emit_data(val10, relocInfo::none, 0);
1011       __ emit_data(val11, relocInfo::none, 0);
1012       __ emit_data(val12, relocInfo::none, 0);
1013       __ emit_data(val13, relocInfo::none, 0);
1014       __ emit_data(val14, relocInfo::none, 0);
1015       __ emit_data(val15, relocInfo::none, 0);
1016     }
1017   }
1018   return start;
1019 }
1020 
1021 // Non-destructive plausibility checks for oops
1022 //
1023 // Arguments:
1024 //    all args on stack!
1025 //
1026 // Stack after saving c_rarg3:
1027 //    [tos + 0]: saved c_rarg3
1028 //    [tos + 1]: saved c_rarg2
1029 //    [tos + 2]: saved r12 (several TemplateTable methods use it)
1030 //    [tos + 3]: saved flags
1031 //    [tos + 4]: return address
1032 //  * [tos + 5]: error message (char*)
1033 //  * [tos + 6]: object to verify (oop)
1034 //  * [tos + 7]: saved rax - saved by caller and bashed
1035 //  * [tos + 8]: saved r10 (rscratch1) - saved by caller
1036 //  * = popped on exit
1037 address StubGenerator::generate_verify_oop() {
1038   StubCodeMark mark(this, "StubRoutines", "verify_oop");
1039   address start = __ pc();
1040 
1041   Label exit, error;
1042 
1043   __ pushf();
1044   __ incrementl(ExternalAddress((address) StubRoutines::verify_oop_count_addr()), rscratch1);
1045 
1046   __ push(r12);
1047 
1048   // save c_rarg2 and c_rarg3
1049   __ push(c_rarg2);
1050   __ push(c_rarg3);
1051 
1052   enum {
1053     // After previous pushes.
1054     oop_to_verify = 6 * wordSize,
1055     saved_rax     = 7 * wordSize,
1056     saved_r10     = 8 * wordSize,
1057 
1058     // Before the call to MacroAssembler::debug(), see below.
1059     return_addr   = 16 * wordSize,
1060     error_msg     = 17 * wordSize
1061   };
1062 
1063   // get object
1064   __ movptr(rax, Address(rsp, oop_to_verify));
1065 
1066   // make sure object is 'reasonable'
1067   __ testptr(rax, rax);
1068   __ jcc(Assembler::zero, exit); // if obj is NULL it is OK
1069 
1070 #if INCLUDE_ZGC
1071   if (UseZGC) {
1072     // Check if metadata bits indicate a bad oop
1073     __ testptr(rax, Address(r15_thread, ZThreadLocalData::address_bad_mask_offset()));
1074     __ jcc(Assembler::notZero, error);
1075   }
1076 #endif
1077 
1078   // Check if the oop is in the right area of memory
1079   __ movptr(c_rarg2, rax);
1080   __ movptr(c_rarg3, (intptr_t) Universe::verify_oop_mask());
1081   __ andptr(c_rarg2, c_rarg3);
1082   __ movptr(c_rarg3, (intptr_t) Universe::verify_oop_bits());
1083   __ cmpptr(c_rarg2, c_rarg3);
1084   __ jcc(Assembler::notZero, error);
1085 
1086   // make sure klass is 'reasonable', which is not zero.
1087   __ load_klass(rax, rax, rscratch1);  // get klass
1088   __ testptr(rax, rax);
1089   __ jcc(Assembler::zero, error); // if klass is NULL it is broken
1090 
1091   // return if everything seems ok
1092   __ bind(exit);
1093   __ movptr(rax, Address(rsp, saved_rax));     // get saved rax back
1094   __ movptr(rscratch1, Address(rsp, saved_r10)); // get saved r10 back
1095   __ pop(c_rarg3);                             // restore c_rarg3
1096   __ pop(c_rarg2);                             // restore c_rarg2
1097   __ pop(r12);                                 // restore r12
1098   __ popf();                                   // restore flags
1099   __ ret(4 * wordSize);                        // pop caller saved stuff
1100 
1101   // handle errors
1102   __ bind(error);
1103   __ movptr(rax, Address(rsp, saved_rax));     // get saved rax back
1104   __ movptr(rscratch1, Address(rsp, saved_r10)); // get saved r10 back
1105   __ pop(c_rarg3);                             // get saved c_rarg3 back
1106   __ pop(c_rarg2);                             // get saved c_rarg2 back
1107   __ pop(r12);                                 // get saved r12 back
1108   __ popf();                                   // get saved flags off stack --
1109                                                // will be ignored
1110 
1111   __ pusha();                                  // push registers
1112                                                // (rip is already
1113                                                // already pushed)
1114   // debug(char* msg, int64_t pc, int64_t regs[])
1115   // We've popped the registers we'd saved (c_rarg3, c_rarg2 and flags), and
1116   // pushed all the registers, so now the stack looks like:
1117   //     [tos +  0] 16 saved registers
1118   //     [tos + 16] return address
1119   //   * [tos + 17] error message (char*)
1120   //   * [tos + 18] object to verify (oop)
1121   //   * [tos + 19] saved rax - saved by caller and bashed
1122   //   * [tos + 20] saved r10 (rscratch1) - saved by caller
1123   //   * = popped on exit
1124 
1125   __ movptr(c_rarg0, Address(rsp, error_msg));    // pass address of error message
1126   __ movptr(c_rarg1, Address(rsp, return_addr));  // pass return address
1127   __ movq(c_rarg2, rsp);                          // pass address of regs on stack
1128   __ mov(r12, rsp);                               // remember rsp
1129   __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
1130   __ andptr(rsp, -16);                            // align stack as required by ABI
1131   BLOCK_COMMENT("call MacroAssembler::debug");
1132   __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, MacroAssembler::debug64)));
1133   __ hlt();
1134 
1135   return start;
1136 }
1137 
1138 
1139 // Shuffle first three arg regs on Windows into Linux/Solaris locations.
1140 //
1141 // Outputs:
1142 //    rdi - rcx
1143 //    rsi - rdx
1144 //    rdx - r8
1145 //    rcx - r9
1146 //
1147 // Registers r9 and r10 are used to save rdi and rsi on Windows, which latter
1148 // are non-volatile.  r9 and r10 should not be used by the caller.
1149 //
1150 void StubGenerator::setup_arg_regs(int nargs) {
1151   const Register saved_rdi = r9;
1152   const Register saved_rsi = r10;
1153   assert(nargs == 3 || nargs == 4, "else fix");
1154 #ifdef _WIN64
1155   assert(c_rarg0 == rcx && c_rarg1 == rdx && c_rarg2 == r8 && c_rarg3 == r9,
1156          "unexpected argument registers");
1157   if (nargs >= 4)
1158     __ mov(rax, r9);  // r9 is also saved_rdi
1159   __ movptr(saved_rdi, rdi);
1160   __ movptr(saved_rsi, rsi);
1161   __ mov(rdi, rcx); // c_rarg0
1162   __ mov(rsi, rdx); // c_rarg1
1163   __ mov(rdx, r8);  // c_rarg2
1164   if (nargs >= 4)
1165     __ mov(rcx, rax); // c_rarg3 (via rax)
1166 #else
1167   assert(c_rarg0 == rdi && c_rarg1 == rsi && c_rarg2 == rdx && c_rarg3 == rcx,
1168          "unexpected argument registers");
1169 #endif
1170   DEBUG_ONLY(_regs_in_thread = false;)
1171 }
1172 
1173 
1174 void StubGenerator::restore_arg_regs() {
1175   assert(!_regs_in_thread, "wrong call to restore_arg_regs");
1176   const Register saved_rdi = r9;
1177   const Register saved_rsi = r10;
1178 #ifdef _WIN64
1179   __ movptr(rdi, saved_rdi);
1180   __ movptr(rsi, saved_rsi);
1181 #endif
1182 }
1183 
1184 
1185 // This is used in places where r10 is a scratch register, and can
1186 // be adapted if r9 is needed also.
1187 void StubGenerator::setup_arg_regs_using_thread() {
1188   const Register saved_r15 = r9;
1189 #ifdef _WIN64
1190   __ mov(saved_r15, r15);  // r15 is callee saved and needs to be restored
1191   __ get_thread(r15_thread);
1192   assert(c_rarg0 == rcx && c_rarg1 == rdx && c_rarg2 == r8 && c_rarg3 == r9,
1193          "unexpected argument registers");
1194   __ movptr(Address(r15_thread, in_bytes(JavaThread::windows_saved_rdi_offset())), rdi);
1195   __ movptr(Address(r15_thread, in_bytes(JavaThread::windows_saved_rsi_offset())), rsi);
1196 
1197   __ mov(rdi, rcx); // c_rarg0
1198   __ mov(rsi, rdx); // c_rarg1
1199   __ mov(rdx, r8);  // c_rarg2
1200 #else
1201   assert(c_rarg0 == rdi && c_rarg1 == rsi && c_rarg2 == rdx && c_rarg3 == rcx,
1202          "unexpected argument registers");
1203 #endif
1204   DEBUG_ONLY(_regs_in_thread = true;)
1205 }
1206 
1207 
1208 void StubGenerator::restore_arg_regs_using_thread() {
1209   assert(_regs_in_thread, "wrong call to restore_arg_regs");
1210   const Register saved_r15 = r9;
1211 #ifdef _WIN64
1212   __ get_thread(r15_thread);
1213   __ movptr(rsi, Address(r15_thread, in_bytes(JavaThread::windows_saved_rsi_offset())));
1214   __ movptr(rdi, Address(r15_thread, in_bytes(JavaThread::windows_saved_rdi_offset())));
1215   __ mov(r15, saved_r15);  // r15 is callee saved and needs to be restored
1216 #endif
1217 }
1218 
1219 
1220 void StubGenerator::setup_argument_regs(BasicType type) {
1221   if (type == T_BYTE || type == T_SHORT) {
1222     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1223                       // r9 and r10 may be used to save non-volatile registers
1224   } else {
1225     setup_arg_regs_using_thread(); // from => rdi, to => rsi, count => rdx
1226                                    // r9 is used to save r15_thread
1227   }
1228 }
1229 
1230 
1231 void StubGenerator::restore_argument_regs(BasicType type) {
1232   if (type == T_BYTE || type == T_SHORT) {
1233     restore_arg_regs();
1234   } else {
1235     restore_arg_regs_using_thread();
1236   }
1237 }
1238 
1239 address StubGenerator::generate_data_cache_writeback() {
1240   const Register src        = c_rarg0;  // source address
1241 
1242   __ align(CodeEntryAlignment);
1243 
1244   StubCodeMark mark(this, "StubRoutines", "_data_cache_writeback");
1245 
1246   address start = __ pc();
1247 
1248   __ enter();
1249   __ cache_wb(Address(src, 0));
1250   __ leave();
1251   __ ret(0);
1252 
1253   return start;
1254 }
1255 
1256 address StubGenerator::generate_data_cache_writeback_sync() {
1257   const Register is_pre    = c_rarg0;  // pre or post sync
1258 
1259   __ align(CodeEntryAlignment);
1260 
1261   StubCodeMark mark(this, "StubRoutines", "_data_cache_writeback_sync");
1262 
1263   // pre wbsync is a no-op
1264   // post wbsync translates to an sfence
1265 
1266   Label skip;
1267   address start = __ pc();
1268 
1269   __ enter();
1270   __ cmpl(is_pre, 0);
1271   __ jcc(Assembler::notEqual, skip);
1272   __ cache_wbsync(false);
1273   __ bind(skip);
1274   __ leave();
1275   __ ret(0);
1276 
1277   return start;
1278 }
1279 
1280 // ofs and limit are use for multi-block byte array.
1281 // int com.sun.security.provider.MD5.implCompress(byte[] b, int ofs)
1282 address StubGenerator::generate_md5_implCompress(bool multi_block, const char *name) {
1283   __ align(CodeEntryAlignment);
1284   StubCodeMark mark(this, "StubRoutines", name);
1285   address start = __ pc();
1286 
1287   const Register buf_param = r15;
1288   const Address state_param(rsp, 0 * wordSize);
1289   const Address ofs_param  (rsp, 1 * wordSize    );
1290   const Address limit_param(rsp, 1 * wordSize + 4);
1291 
1292   __ enter();
1293   __ push(rbx);
1294   __ push(rdi);
1295   __ push(rsi);
1296   __ push(r15);
1297   __ subptr(rsp, 2 * wordSize);
1298 
1299   __ movptr(buf_param, c_rarg0);
1300   __ movptr(state_param, c_rarg1);
1301   if (multi_block) {
1302     __ movl(ofs_param, c_rarg2);
1303     __ movl(limit_param, c_rarg3);
1304   }
1305   __ fast_md5(buf_param, state_param, ofs_param, limit_param, multi_block);
1306 
1307   __ addptr(rsp, 2 * wordSize);
1308   __ pop(r15);
1309   __ pop(rsi);
1310   __ pop(rdi);
1311   __ pop(rbx);
1312   __ leave();
1313   __ ret(0);
1314 
1315   return start;
1316 }
1317 
1318 address StubGenerator::generate_upper_word_mask() {
1319   __ align64();
1320   StubCodeMark mark(this, "StubRoutines", "upper_word_mask");
1321   address start = __ pc();
1322 
1323   __ emit_data64(0x0000000000000000, relocInfo::none);
1324   __ emit_data64(0xFFFFFFFF00000000, relocInfo::none);
1325 
1326   return start;
1327 }
1328 
1329 address StubGenerator::generate_shuffle_byte_flip_mask() {
1330   __ align64();
1331   StubCodeMark mark(this, "StubRoutines", "shuffle_byte_flip_mask");
1332   address start = __ pc();
1333 
1334   __ emit_data64(0x08090a0b0c0d0e0f, relocInfo::none);
1335   __ emit_data64(0x0001020304050607, relocInfo::none);
1336 
1337   return start;
1338 }
1339 
1340 // ofs and limit are use for multi-block byte array.
1341 // int com.sun.security.provider.DigestBase.implCompressMultiBlock(byte[] b, int ofs, int limit)
1342 address StubGenerator::generate_sha1_implCompress(bool multi_block, const char *name) {
1343   __ align(CodeEntryAlignment);
1344   StubCodeMark mark(this, "StubRoutines", name);
1345   address start = __ pc();
1346 
1347   Register buf = c_rarg0;
1348   Register state = c_rarg1;
1349   Register ofs = c_rarg2;
1350   Register limit = c_rarg3;
1351 
1352   const XMMRegister abcd = xmm0;
1353   const XMMRegister e0 = xmm1;
1354   const XMMRegister e1 = xmm2;
1355   const XMMRegister msg0 = xmm3;
1356 
1357   const XMMRegister msg1 = xmm4;
1358   const XMMRegister msg2 = xmm5;
1359   const XMMRegister msg3 = xmm6;
1360   const XMMRegister shuf_mask = xmm7;
1361 
1362   __ enter();
1363 
1364   __ subptr(rsp, 4 * wordSize);
1365 
1366   __ fast_sha1(abcd, e0, e1, msg0, msg1, msg2, msg3, shuf_mask,
1367     buf, state, ofs, limit, rsp, multi_block);
1368 
1369   __ addptr(rsp, 4 * wordSize);
1370 
1371   __ leave();
1372   __ ret(0);
1373 
1374   return start;
1375 }
1376 
1377 address StubGenerator::generate_pshuffle_byte_flip_mask() {
1378   __ align64();
1379   StubCodeMark mark(this, "StubRoutines", "pshuffle_byte_flip_mask");
1380   address start = __ pc();
1381 
1382   __ emit_data64(0x0405060700010203, relocInfo::none);
1383   __ emit_data64(0x0c0d0e0f08090a0b, relocInfo::none);
1384 
1385   if (VM_Version::supports_avx2()) {
1386     __ emit_data64(0x0405060700010203, relocInfo::none); // second copy
1387     __ emit_data64(0x0c0d0e0f08090a0b, relocInfo::none);
1388     // _SHUF_00BA
1389     __ emit_data64(0x0b0a090803020100, relocInfo::none);
1390     __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
1391     __ emit_data64(0x0b0a090803020100, relocInfo::none);
1392     __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
1393     // _SHUF_DC00
1394     __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
1395     __ emit_data64(0x0b0a090803020100, relocInfo::none);
1396     __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
1397     __ emit_data64(0x0b0a090803020100, relocInfo::none);
1398   }
1399 
1400   return start;
1401 }
1402 
1403 //Mask for byte-swapping a couple of qwords in an XMM register using (v)pshufb.
1404 address StubGenerator::generate_pshuffle_byte_flip_mask_sha512() {
1405   __ align32();
1406   StubCodeMark mark(this, "StubRoutines", "pshuffle_byte_flip_mask_sha512");
1407   address start = __ pc();
1408 
1409   if (VM_Version::supports_avx2()) {
1410     __ emit_data64(0x0001020304050607, relocInfo::none); // PSHUFFLE_BYTE_FLIP_MASK
1411     __ emit_data64(0x08090a0b0c0d0e0f, relocInfo::none);
1412     __ emit_data64(0x1011121314151617, relocInfo::none);
1413     __ emit_data64(0x18191a1b1c1d1e1f, relocInfo::none);
1414     __ emit_data64(0x0000000000000000, relocInfo::none); //MASK_YMM_LO
1415     __ emit_data64(0x0000000000000000, relocInfo::none);
1416     __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
1417     __ emit_data64(0xFFFFFFFFFFFFFFFF, relocInfo::none);
1418   }
1419 
1420   return start;
1421 }
1422 
1423 // ofs and limit are use for multi-block byte array.
1424 // int com.sun.security.provider.DigestBase.implCompressMultiBlock(byte[] b, int ofs, int limit)
1425 address StubGenerator::generate_sha256_implCompress(bool multi_block, const char *name) {
1426   assert(VM_Version::supports_sha() || VM_Version::supports_avx2(), "");
1427   __ align(CodeEntryAlignment);
1428   StubCodeMark mark(this, "StubRoutines", name);
1429   address start = __ pc();
1430 
1431   Register buf = c_rarg0;
1432   Register state = c_rarg1;
1433   Register ofs = c_rarg2;
1434   Register limit = c_rarg3;
1435 
1436   const XMMRegister msg = xmm0;
1437   const XMMRegister state0 = xmm1;
1438   const XMMRegister state1 = xmm2;
1439   const XMMRegister msgtmp0 = xmm3;
1440 
1441   const XMMRegister msgtmp1 = xmm4;
1442   const XMMRegister msgtmp2 = xmm5;
1443   const XMMRegister msgtmp3 = xmm6;
1444   const XMMRegister msgtmp4 = xmm7;
1445 
1446   const XMMRegister shuf_mask = xmm8;
1447 
1448   __ enter();
1449 
1450   __ subptr(rsp, 4 * wordSize);
1451 
1452   if (VM_Version::supports_sha()) {
1453     __ fast_sha256(msg, state0, state1, msgtmp0, msgtmp1, msgtmp2, msgtmp3, msgtmp4,
1454       buf, state, ofs, limit, rsp, multi_block, shuf_mask);
1455   } else if (VM_Version::supports_avx2()) {
1456     __ sha256_AVX2(msg, state0, state1, msgtmp0, msgtmp1, msgtmp2, msgtmp3, msgtmp4,
1457       buf, state, ofs, limit, rsp, multi_block, shuf_mask);
1458   }
1459   __ addptr(rsp, 4 * wordSize);
1460   __ vzeroupper();
1461   __ leave();
1462   __ ret(0);
1463 
1464   return start;
1465 }
1466 
1467 address StubGenerator::generate_sha512_implCompress(bool multi_block, const char *name) {
1468   assert(VM_Version::supports_avx2(), "");
1469   assert(VM_Version::supports_bmi2(), "");
1470   __ align(CodeEntryAlignment);
1471   StubCodeMark mark(this, "StubRoutines", name);
1472   address start = __ pc();
1473 
1474   Register buf = c_rarg0;
1475   Register state = c_rarg1;
1476   Register ofs = c_rarg2;
1477   Register limit = c_rarg3;
1478 
1479   const XMMRegister msg = xmm0;
1480   const XMMRegister state0 = xmm1;
1481   const XMMRegister state1 = xmm2;
1482   const XMMRegister msgtmp0 = xmm3;
1483   const XMMRegister msgtmp1 = xmm4;
1484   const XMMRegister msgtmp2 = xmm5;
1485   const XMMRegister msgtmp3 = xmm6;
1486   const XMMRegister msgtmp4 = xmm7;
1487 
1488   const XMMRegister shuf_mask = xmm8;
1489 
1490   __ enter();
1491 
1492   __ sha512_AVX2(msg, state0, state1, msgtmp0, msgtmp1, msgtmp2, msgtmp3, msgtmp4,
1493   buf, state, ofs, limit, rsp, multi_block, shuf_mask);
1494 
1495   __ vzeroupper();
1496   __ leave();
1497   __ ret(0);
1498 
1499   return start;
1500 }
1501 
1502 address StubGenerator::base64_shuffle_addr() {
1503   __ align64();
1504   StubCodeMark mark(this, "StubRoutines", "shuffle_base64");
1505   address start = __ pc();
1506 
1507   assert(((unsigned long long)start & 0x3f) == 0,
1508          "Alignment problem (0x%08llx)", (unsigned long long)start);
1509   __ emit_data64(0x0405030401020001, relocInfo::none);
1510   __ emit_data64(0x0a0b090a07080607, relocInfo::none);
1511   __ emit_data64(0x10110f100d0e0c0d, relocInfo::none);
1512   __ emit_data64(0x1617151613141213, relocInfo::none);
1513   __ emit_data64(0x1c1d1b1c191a1819, relocInfo::none);
1514   __ emit_data64(0x222321221f201e1f, relocInfo::none);
1515   __ emit_data64(0x2829272825262425, relocInfo::none);
1516   __ emit_data64(0x2e2f2d2e2b2c2a2b, relocInfo::none);
1517 
1518   return start;
1519 }
1520 
1521 address StubGenerator::base64_avx2_shuffle_addr() {
1522   __ align32();
1523   StubCodeMark mark(this, "StubRoutines", "avx2_shuffle_base64");
1524   address start = __ pc();
1525 
1526   __ emit_data64(0x0809070805060405, relocInfo::none);
1527   __ emit_data64(0x0e0f0d0e0b0c0a0b, relocInfo::none);
1528   __ emit_data64(0x0405030401020001, relocInfo::none);
1529   __ emit_data64(0x0a0b090a07080607, relocInfo::none);
1530 
1531   return start;
1532 }
1533 
1534 address StubGenerator::base64_avx2_input_mask_addr() {
1535   __ align32();
1536   StubCodeMark mark(this, "StubRoutines", "avx2_input_mask_base64");
1537   address start = __ pc();
1538 
1539   __ emit_data64(0x8000000000000000, relocInfo::none);
1540   __ emit_data64(0x8000000080000000, relocInfo::none);
1541   __ emit_data64(0x8000000080000000, relocInfo::none);
1542   __ emit_data64(0x8000000080000000, relocInfo::none);
1543 
1544   return start;
1545 }
1546 
1547 address StubGenerator::base64_avx2_lut_addr() {
1548   __ align32();
1549   StubCodeMark mark(this, "StubRoutines", "avx2_lut_base64");
1550   address start = __ pc();
1551 
1552   __ emit_data64(0xfcfcfcfcfcfc4741, relocInfo::none);
1553   __ emit_data64(0x0000f0edfcfcfcfc, relocInfo::none);
1554   __ emit_data64(0xfcfcfcfcfcfc4741, relocInfo::none);
1555   __ emit_data64(0x0000f0edfcfcfcfc, relocInfo::none);
1556 
1557   // URL LUT
1558   __ emit_data64(0xfcfcfcfcfcfc4741, relocInfo::none);
1559   __ emit_data64(0x000020effcfcfcfc, relocInfo::none);
1560   __ emit_data64(0xfcfcfcfcfcfc4741, relocInfo::none);
1561   __ emit_data64(0x000020effcfcfcfc, relocInfo::none);
1562 
1563   return start;
1564 }
1565 
1566 address StubGenerator::base64_encoding_table_addr() {
1567   __ align64();
1568   StubCodeMark mark(this, "StubRoutines", "encoding_table_base64");
1569   address start = __ pc();
1570 
1571   assert(((unsigned long long)start & 0x3f) == 0, "Alignment problem (0x%08llx)", (unsigned long long)start);
1572   __ emit_data64(0x4847464544434241, relocInfo::none);
1573   __ emit_data64(0x504f4e4d4c4b4a49, relocInfo::none);
1574   __ emit_data64(0x5857565554535251, relocInfo::none);
1575   __ emit_data64(0x6665646362615a59, relocInfo::none);
1576   __ emit_data64(0x6e6d6c6b6a696867, relocInfo::none);
1577   __ emit_data64(0x767574737271706f, relocInfo::none);
1578   __ emit_data64(0x333231307a797877, relocInfo::none);
1579   __ emit_data64(0x2f2b393837363534, relocInfo::none);
1580 
1581   // URL table
1582   __ emit_data64(0x4847464544434241, relocInfo::none);
1583   __ emit_data64(0x504f4e4d4c4b4a49, relocInfo::none);
1584   __ emit_data64(0x5857565554535251, relocInfo::none);
1585   __ emit_data64(0x6665646362615a59, relocInfo::none);
1586   __ emit_data64(0x6e6d6c6b6a696867, relocInfo::none);
1587   __ emit_data64(0x767574737271706f, relocInfo::none);
1588   __ emit_data64(0x333231307a797877, relocInfo::none);
1589   __ emit_data64(0x5f2d393837363534, relocInfo::none);
1590 
1591   return start;
1592 }
1593 
1594 // Code for generating Base64 encoding.
1595 // Intrinsic function prototype in Base64.java:
1596 // private void encodeBlock(byte[] src, int sp, int sl, byte[] dst, int dp,
1597 // boolean isURL) {
1598 address StubGenerator::generate_base64_encodeBlock()
1599 {
1600   __ align(CodeEntryAlignment);
1601   StubCodeMark mark(this, "StubRoutines", "implEncode");
1602   address start = __ pc();
1603 
1604   __ enter();
1605 
1606   // Save callee-saved registers before using them
1607   __ push(r12);
1608   __ push(r13);
1609   __ push(r14);
1610   __ push(r15);
1611 
1612   // arguments
1613   const Register source = c_rarg0;       // Source Array
1614   const Register start_offset = c_rarg1; // start offset
1615   const Register end_offset = c_rarg2;   // end offset
1616   const Register dest = c_rarg3;   // destination array
1617 
1618 #ifndef _WIN64
1619   const Register dp = c_rarg4;    // Position for writing to dest array
1620   const Register isURL = c_rarg5; // Base64 or URL character set
1621 #else
1622   const Address dp_mem(rbp, 6 * wordSize); // length is on stack on Win64
1623   const Address isURL_mem(rbp, 7 * wordSize);
1624   const Register isURL = r10; // pick the volatile windows register
1625   const Register dp = r12;
1626   __ movl(dp, dp_mem);
1627   __ movl(isURL, isURL_mem);
1628 #endif
1629 
1630   const Register length = r14;
1631   const Register encode_table = r13;
1632   Label L_process3, L_exit, L_processdata, L_vbmiLoop, L_not512, L_32byteLoop;
1633 
1634   // calculate length from offsets
1635   __ movl(length, end_offset);
1636   __ subl(length, start_offset);
1637   __ cmpl(length, 0);
1638   __ jcc(Assembler::lessEqual, L_exit);
1639 
1640   // Code for 512-bit VBMI encoding.  Encodes 48 input bytes into 64
1641   // output bytes. We read 64 input bytes and ignore the last 16, so be
1642   // sure not to read past the end of the input buffer.
1643   if (VM_Version::supports_avx512_vbmi()) {
1644     __ cmpl(length, 64); // Do not overrun input buffer.
1645     __ jcc(Assembler::below, L_not512);
1646 
1647     __ shll(isURL, 6); // index into decode table based on isURL
1648     __ lea(encode_table, ExternalAddress(StubRoutines::x86::base64_encoding_table_addr()));
1649     __ addptr(encode_table, isURL);
1650     __ shrl(isURL, 6); // restore isURL
1651 
1652     __ mov64(rax, 0x3036242a1016040aull); // Shifts
1653     __ evmovdquq(xmm3, ExternalAddress(StubRoutines::x86::base64_shuffle_addr()), Assembler::AVX_512bit, r15);
1654     __ evmovdquq(xmm2, Address(encode_table, 0), Assembler::AVX_512bit);
1655     __ evpbroadcastq(xmm1, rax, Assembler::AVX_512bit);
1656 
1657     __ align32();
1658     __ BIND(L_vbmiLoop);
1659 
1660     __ vpermb(xmm0, xmm3, Address(source, start_offset), Assembler::AVX_512bit);
1661     __ subl(length, 48);
1662 
1663     // Put the input bytes into the proper lanes for writing, then
1664     // encode them.
1665     __ evpmultishiftqb(xmm0, xmm1, xmm0, Assembler::AVX_512bit);
1666     __ vpermb(xmm0, xmm0, xmm2, Assembler::AVX_512bit);
1667 
1668     // Write to destination
1669     __ evmovdquq(Address(dest, dp), xmm0, Assembler::AVX_512bit);
1670 
1671     __ addptr(dest, 64);
1672     __ addptr(source, 48);
1673     __ cmpl(length, 64);
1674     __ jcc(Assembler::aboveEqual, L_vbmiLoop);
1675 
1676     __ vzeroupper();
1677   }
1678 
1679   __ BIND(L_not512);
1680   if (VM_Version::supports_avx2()
1681       && VM_Version::supports_avx512vlbw()) {
1682     /*
1683     ** This AVX2 encoder is based off the paper at:
1684     **      https://dl.acm.org/doi/10.1145/3132709
1685     **
1686     ** We use AVX2 SIMD instructions to encode 24 bytes into 32
1687     ** output bytes.
1688     **
1689     */
1690     // Lengths under 32 bytes are done with scalar routine
1691     __ cmpl(length, 31);
1692     __ jcc(Assembler::belowEqual, L_process3);
1693 
1694     // Set up supporting constant table data
1695     __ vmovdqu(xmm9, ExternalAddress(StubRoutines::x86::base64_avx2_shuffle_addr()), rax);
1696     // 6-bit mask for 2nd and 4th (and multiples) 6-bit values
1697     __ movl(rax, 0x0fc0fc00);
1698     __ vmovdqu(xmm1, ExternalAddress(StubRoutines::x86::base64_avx2_input_mask_addr()), rax);
1699     __ evpbroadcastd(xmm8, rax, Assembler::AVX_256bit);
1700 
1701     // Multiplication constant for "shifting" right by 6 and 10
1702     // bits
1703     __ movl(rax, 0x04000040);
1704 
1705     __ subl(length, 24);
1706     __ evpbroadcastd(xmm7, rax, Assembler::AVX_256bit);
1707 
1708     // For the first load, we mask off reading of the first 4
1709     // bytes into the register. This is so we can get 4 3-byte
1710     // chunks into each lane of the register, avoiding having to
1711     // handle end conditions.  We then shuffle these bytes into a
1712     // specific order so that manipulation is easier.
1713     //
1714     // The initial read loads the XMM register like this:
1715     //
1716     // Lower 128-bit lane:
1717     // +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
1718     // | XX | XX | XX | XX | A0 | A1 | A2 | B0 | B1 | B2 | C0 | C1
1719     // | C2 | D0 | D1 | D2 |
1720     // +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
1721     //
1722     // Upper 128-bit lane:
1723     // +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
1724     // | E0 | E1 | E2 | F0 | F1 | F2 | G0 | G1 | G2 | H0 | H1 | H2
1725     // | XX | XX | XX | XX |
1726     // +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
1727     //
1728     // Where A0 is the first input byte, B0 is the fourth, etc.
1729     // The alphabetical significance denotes the 3 bytes to be
1730     // consumed and encoded into 4 bytes.
1731     //
1732     // We then shuffle the register so each 32-bit word contains
1733     // the sequence:
1734     //    A1 A0 A2 A1, B1, B0, B2, B1, etc.
1735     // Each of these byte sequences are then manipulated into 4
1736     // 6-bit values ready for encoding.
1737     //
1738     // If we focus on one set of 3-byte chunks, changing the
1739     // nomenclature such that A0 => a, A1 => b, and A2 => c, we
1740     // shuffle such that each 24-bit chunk contains:
1741     //
1742     // b7 b6 b5 b4 b3 b2 b1 b0 | a7 a6 a5 a4 a3 a2 a1 a0 | c7 c6
1743     // c5 c4 c3 c2 c1 c0 | b7 b6 b5 b4 b3 b2 b1 b0
1744     // Explain this step.
1745     // b3 b2 b1 b0 c5 c4 c3 c2 | c1 c0 d5 d4 d3 d2 d1 d0 | a5 a4
1746     // a3 a2 a1 a0 b5 b4 | b3 b2 b1 b0 c5 c4 c3 c2
1747     //
1748     // W first and off all but bits 4-9 and 16-21 (c5..c0 and
1749     // a5..a0) and shift them using a vector multiplication
1750     // operation (vpmulhuw) which effectively shifts c right by 6
1751     // bits and a right by 10 bits.  We similarly mask bits 10-15
1752     // (d5..d0) and 22-27 (b5..b0) and shift them left by 8 and 4
1753     // bits respectively.  This is done using vpmullw.  We end up
1754     // with 4 6-bit values, thus splitting the 3 input bytes,
1755     // ready for encoding:
1756     //    0 0 d5..d0 0 0 c5..c0 0 0 b5..b0 0 0 a5..a0
1757     //
1758     // For translation, we recognize that there are 5 distinct
1759     // ranges of legal Base64 characters as below:
1760     //
1761     //   +-------------+-------------+------------+
1762     //   | 6-bit value | ASCII range |   offset   |
1763     //   +-------------+-------------+------------+
1764     //   |    0..25    |    A..Z     |     65     |
1765     //   |   26..51    |    a..z     |     71     |
1766     //   |   52..61    |    0..9     |     -4     |
1767     //   |     62      |   + or -    | -19 or -17 |
1768     //   |     63      |   / or _    | -16 or 32  |
1769     //   +-------------+-------------+------------+
1770     //
1771     // We note that vpshufb does a parallel lookup in a
1772     // destination register using the lower 4 bits of bytes from a
1773     // source register.  If we use a saturated subtraction and
1774     // subtract 51 from each 6-bit value, bytes from [0,51]
1775     // saturate to 0, and [52,63] map to a range of [1,12].  We
1776     // distinguish the [0,25] and [26,51] ranges by assigning a
1777     // value of 13 for all 6-bit values less than 26.  We end up
1778     // with:
1779     //
1780     //   +-------------+-------------+------------+
1781     //   | 6-bit value |   Reduced   |   offset   |
1782     //   +-------------+-------------+------------+
1783     //   |    0..25    |     13      |     65     |
1784     //   |   26..51    |      0      |     71     |
1785     //   |   52..61    |    0..9     |     -4     |
1786     //   |     62      |     11      | -19 or -17 |
1787     //   |     63      |     12      | -16 or 32  |
1788     //   +-------------+-------------+------------+
1789     //
1790     // We then use a final vpshufb to add the appropriate offset,
1791     // translating the bytes.
1792     //
1793     // Load input bytes - only 28 bytes.  Mask the first load to
1794     // not load into the full register.
1795     __ vpmaskmovd(xmm1, xmm1, Address(source, start_offset, Address::times_1, -4), Assembler::AVX_256bit);
1796 
1797     // Move 3-byte chunks of input (12 bytes) into 16 bytes,
1798     // ordering by:
1799     //   1, 0, 2, 1; 4, 3, 5, 4; etc.  This groups 6-bit chunks
1800     //   for easy masking
1801     __ vpshufb(xmm1, xmm1, xmm9, Assembler::AVX_256bit);
1802 
1803     __ addl(start_offset, 24);
1804 
1805     // Load masking register for first and third (and multiples)
1806     // 6-bit values.
1807     __ movl(rax, 0x003f03f0);
1808     __ evpbroadcastd(xmm6, rax, Assembler::AVX_256bit);
1809     // Multiplication constant for "shifting" left by 4 and 8 bits
1810     __ movl(rax, 0x01000010);
1811     __ evpbroadcastd(xmm5, rax, Assembler::AVX_256bit);
1812 
1813     // Isolate 6-bit chunks of interest
1814     __ vpand(xmm0, xmm8, xmm1, Assembler::AVX_256bit);
1815 
1816     // Load constants for encoding
1817     __ movl(rax, 0x19191919);
1818     __ evpbroadcastd(xmm3, rax, Assembler::AVX_256bit);
1819     __ movl(rax, 0x33333333);
1820     __ evpbroadcastd(xmm4, rax, Assembler::AVX_256bit);
1821 
1822     // Shift output bytes 0 and 2 into proper lanes
1823     __ vpmulhuw(xmm2, xmm0, xmm7, Assembler::AVX_256bit);
1824 
1825     // Mask and shift output bytes 1 and 3 into proper lanes and
1826     // combine
1827     __ vpand(xmm0, xmm6, xmm1, Assembler::AVX_256bit);
1828     __ vpmullw(xmm0, xmm5, xmm0, Assembler::AVX_256bit);
1829     __ vpor(xmm0, xmm0, xmm2, Assembler::AVX_256bit);
1830 
1831     // Find out which are 0..25.  This indicates which input
1832     // values fall in the range of 'A'-'Z', which require an
1833     // additional offset (see comments above)
1834     __ vpcmpgtb(xmm2, xmm0, xmm3, Assembler::AVX_256bit);
1835     __ vpsubusb(xmm1, xmm0, xmm4, Assembler::AVX_256bit);
1836     __ vpsubb(xmm1, xmm1, xmm2, Assembler::AVX_256bit);
1837 
1838     // Load the proper lookup table
1839     __ lea(r11, ExternalAddress(StubRoutines::x86::base64_avx2_lut_addr()));
1840     __ movl(r15, isURL);
1841     __ shll(r15, 5);
1842     __ vmovdqu(xmm2, Address(r11, r15));
1843 
1844     // Shuffle the offsets based on the range calculation done
1845     // above. This allows us to add the correct offset to the
1846     // 6-bit value corresponding to the range documented above.
1847     __ vpshufb(xmm1, xmm2, xmm1, Assembler::AVX_256bit);
1848     __ vpaddb(xmm0, xmm1, xmm0, Assembler::AVX_256bit);
1849 
1850     // Store the encoded bytes
1851     __ vmovdqu(Address(dest, dp), xmm0);
1852     __ addl(dp, 32);
1853 
1854     __ cmpl(length, 31);
1855     __ jcc(Assembler::belowEqual, L_process3);
1856 
1857     __ align32();
1858     __ BIND(L_32byteLoop);
1859 
1860     // Get next 32 bytes
1861     __ vmovdqu(xmm1, Address(source, start_offset, Address::times_1, -4));
1862 
1863     __ subl(length, 24);
1864     __ addl(start_offset, 24);
1865 
1866     // This logic is identical to the above, with only constant
1867     // register loads removed.  Shuffle the input, mask off 6-bit
1868     // chunks, shift them into place, then add the offset to
1869     // encode.
1870     __ vpshufb(xmm1, xmm1, xmm9, Assembler::AVX_256bit);
1871 
1872     __ vpand(xmm0, xmm8, xmm1, Assembler::AVX_256bit);
1873     __ vpmulhuw(xmm10, xmm0, xmm7, Assembler::AVX_256bit);
1874     __ vpand(xmm0, xmm6, xmm1, Assembler::AVX_256bit);
1875     __ vpmullw(xmm0, xmm5, xmm0, Assembler::AVX_256bit);
1876     __ vpor(xmm0, xmm0, xmm10, Assembler::AVX_256bit);
1877     __ vpcmpgtb(xmm10, xmm0, xmm3, Assembler::AVX_256bit);
1878     __ vpsubusb(xmm1, xmm0, xmm4, Assembler::AVX_256bit);
1879     __ vpsubb(xmm1, xmm1, xmm10, Assembler::AVX_256bit);
1880     __ vpshufb(xmm1, xmm2, xmm1, Assembler::AVX_256bit);
1881     __ vpaddb(xmm0, xmm1, xmm0, Assembler::AVX_256bit);
1882 
1883     // Store the encoded bytes
1884     __ vmovdqu(Address(dest, dp), xmm0);
1885     __ addl(dp, 32);
1886 
1887     __ cmpl(length, 31);
1888     __ jcc(Assembler::above, L_32byteLoop);
1889 
1890     __ BIND(L_process3);
1891     __ vzeroupper();
1892   } else {
1893     __ BIND(L_process3);
1894   }
1895 
1896   __ cmpl(length, 3);
1897   __ jcc(Assembler::below, L_exit);
1898 
1899   // Load the encoding table based on isURL
1900   __ lea(r11, ExternalAddress(StubRoutines::x86::base64_encoding_table_addr()));
1901   __ movl(r15, isURL);
1902   __ shll(r15, 6);
1903   __ addptr(r11, r15);
1904 
1905   __ BIND(L_processdata);
1906 
1907   // Load 3 bytes
1908   __ load_unsigned_byte(r15, Address(source, start_offset));
1909   __ load_unsigned_byte(r10, Address(source, start_offset, Address::times_1, 1));
1910   __ load_unsigned_byte(r13, Address(source, start_offset, Address::times_1, 2));
1911 
1912   // Build a 32-bit word with bytes 1, 2, 0, 1
1913   __ movl(rax, r10);
1914   __ shll(r10, 24);
1915   __ orl(rax, r10);
1916 
1917   __ subl(length, 3);
1918 
1919   __ shll(r15, 8);
1920   __ shll(r13, 16);
1921   __ orl(rax, r15);
1922 
1923   __ addl(start_offset, 3);
1924 
1925   __ orl(rax, r13);
1926   // At this point, rax contains | byte1 | byte2 | byte0 | byte1
1927   // r13 has byte2 << 16 - need low-order 6 bits to translate.
1928   // This translated byte is the fourth output byte.
1929   __ shrl(r13, 16);
1930   __ andl(r13, 0x3f);
1931 
1932   // The high-order 6 bits of r15 (byte0) is translated.
1933   // The translated byte is the first output byte.
1934   __ shrl(r15, 10);
1935 
1936   __ load_unsigned_byte(r13, Address(r11, r13));
1937   __ load_unsigned_byte(r15, Address(r11, r15));
1938 
1939   __ movb(Address(dest, dp, Address::times_1, 3), r13);
1940 
1941   // Extract high-order 4 bits of byte1 and low-order 2 bits of byte0.
1942   // This translated byte is the second output byte.
1943   __ shrl(rax, 4);
1944   __ movl(r10, rax);
1945   __ andl(rax, 0x3f);
1946 
1947   __ movb(Address(dest, dp, Address::times_1, 0), r15);
1948 
1949   __ load_unsigned_byte(rax, Address(r11, rax));
1950 
1951   // Extract low-order 2 bits of byte1 and high-order 4 bits of byte2.
1952   // This translated byte is the third output byte.
1953   __ shrl(r10, 18);
1954   __ andl(r10, 0x3f);
1955 
1956   __ load_unsigned_byte(r10, Address(r11, r10));
1957 
1958   __ movb(Address(dest, dp, Address::times_1, 1), rax);
1959   __ movb(Address(dest, dp, Address::times_1, 2), r10);
1960 
1961   __ addl(dp, 4);
1962   __ cmpl(length, 3);
1963   __ jcc(Assembler::aboveEqual, L_processdata);
1964 
1965   __ BIND(L_exit);
1966   __ pop(r15);
1967   __ pop(r14);
1968   __ pop(r13);
1969   __ pop(r12);
1970   __ leave();
1971   __ ret(0);
1972 
1973   return start;
1974 }
1975 
1976 // base64 AVX512vbmi tables
1977 address StubGenerator::base64_vbmi_lookup_lo_addr() {
1978   __ align64();
1979   StubCodeMark mark(this, "StubRoutines", "lookup_lo_base64");
1980   address start = __ pc();
1981 
1982   assert(((unsigned long long)start & 0x3f) == 0,
1983          "Alignment problem (0x%08llx)", (unsigned long long)start);
1984   __ emit_data64(0x8080808080808080, relocInfo::none);
1985   __ emit_data64(0x8080808080808080, relocInfo::none);
1986   __ emit_data64(0x8080808080808080, relocInfo::none);
1987   __ emit_data64(0x8080808080808080, relocInfo::none);
1988   __ emit_data64(0x8080808080808080, relocInfo::none);
1989   __ emit_data64(0x3f8080803e808080, relocInfo::none);
1990   __ emit_data64(0x3b3a393837363534, relocInfo::none);
1991   __ emit_data64(0x8080808080803d3c, relocInfo::none);
1992 
1993   return start;
1994 }
1995 
1996 address StubGenerator::base64_vbmi_lookup_hi_addr() {
1997   __ align64();
1998   StubCodeMark mark(this, "StubRoutines", "lookup_hi_base64");
1999   address start = __ pc();
2000 
2001   assert(((unsigned long long)start & 0x3f) == 0,
2002          "Alignment problem (0x%08llx)", (unsigned long long)start);
2003   __ emit_data64(0x0605040302010080, relocInfo::none);
2004   __ emit_data64(0x0e0d0c0b0a090807, relocInfo::none);
2005   __ emit_data64(0x161514131211100f, relocInfo::none);
2006   __ emit_data64(0x8080808080191817, relocInfo::none);
2007   __ emit_data64(0x201f1e1d1c1b1a80, relocInfo::none);
2008   __ emit_data64(0x2827262524232221, relocInfo::none);
2009   __ emit_data64(0x302f2e2d2c2b2a29, relocInfo::none);
2010   __ emit_data64(0x8080808080333231, relocInfo::none);
2011 
2012   return start;
2013 }
2014 address StubGenerator::base64_vbmi_lookup_lo_url_addr() {
2015   __ align64();
2016   StubCodeMark mark(this, "StubRoutines", "lookup_lo_base64url");
2017   address start = __ pc();
2018 
2019   assert(((unsigned long long)start & 0x3f) == 0,
2020          "Alignment problem (0x%08llx)", (unsigned long long)start);
2021   __ emit_data64(0x8080808080808080, relocInfo::none);
2022   __ emit_data64(0x8080808080808080, relocInfo::none);
2023   __ emit_data64(0x8080808080808080, relocInfo::none);
2024   __ emit_data64(0x8080808080808080, relocInfo::none);
2025   __ emit_data64(0x8080808080808080, relocInfo::none);
2026   __ emit_data64(0x80803e8080808080, relocInfo::none);
2027   __ emit_data64(0x3b3a393837363534, relocInfo::none);
2028   __ emit_data64(0x8080808080803d3c, relocInfo::none);
2029 
2030   return start;
2031 }
2032 
2033 address StubGenerator::base64_vbmi_lookup_hi_url_addr() {
2034   __ align64();
2035   StubCodeMark mark(this, "StubRoutines", "lookup_hi_base64url");
2036   address start = __ pc();
2037 
2038   assert(((unsigned long long)start & 0x3f) == 0,
2039          "Alignment problem (0x%08llx)", (unsigned long long)start);
2040   __ emit_data64(0x0605040302010080, relocInfo::none);
2041   __ emit_data64(0x0e0d0c0b0a090807, relocInfo::none);
2042   __ emit_data64(0x161514131211100f, relocInfo::none);
2043   __ emit_data64(0x3f80808080191817, relocInfo::none);
2044   __ emit_data64(0x201f1e1d1c1b1a80, relocInfo::none);
2045   __ emit_data64(0x2827262524232221, relocInfo::none);
2046   __ emit_data64(0x302f2e2d2c2b2a29, relocInfo::none);
2047   __ emit_data64(0x8080808080333231, relocInfo::none);
2048 
2049   return start;
2050 }
2051 
2052 address StubGenerator::base64_vbmi_pack_vec_addr() {
2053   __ align64();
2054   StubCodeMark mark(this, "StubRoutines", "pack_vec_base64");
2055   address start = __ pc();
2056 
2057   assert(((unsigned long long)start & 0x3f) == 0,
2058          "Alignment problem (0x%08llx)", (unsigned long long)start);
2059   __ emit_data64(0x090a040506000102, relocInfo::none);
2060   __ emit_data64(0x161011120c0d0e08, relocInfo::none);
2061   __ emit_data64(0x1c1d1e18191a1415, relocInfo::none);
2062   __ emit_data64(0x292a242526202122, relocInfo::none);
2063   __ emit_data64(0x363031322c2d2e28, relocInfo::none);
2064   __ emit_data64(0x3c3d3e38393a3435, relocInfo::none);
2065   __ emit_data64(0x0000000000000000, relocInfo::none);
2066   __ emit_data64(0x0000000000000000, relocInfo::none);
2067 
2068   return start;
2069 }
2070 
2071 address StubGenerator::base64_vbmi_join_0_1_addr() {
2072   __ align64();
2073   StubCodeMark mark(this, "StubRoutines", "join_0_1_base64");
2074   address start = __ pc();
2075 
2076   assert(((unsigned long long)start & 0x3f) == 0,
2077          "Alignment problem (0x%08llx)", (unsigned long long)start);
2078   __ emit_data64(0x090a040506000102, relocInfo::none);
2079   __ emit_data64(0x161011120c0d0e08, relocInfo::none);
2080   __ emit_data64(0x1c1d1e18191a1415, relocInfo::none);
2081   __ emit_data64(0x292a242526202122, relocInfo::none);
2082   __ emit_data64(0x363031322c2d2e28, relocInfo::none);
2083   __ emit_data64(0x3c3d3e38393a3435, relocInfo::none);
2084   __ emit_data64(0x494a444546404142, relocInfo::none);
2085   __ emit_data64(0x565051524c4d4e48, relocInfo::none);
2086 
2087   return start;
2088 }
2089 
2090 address StubGenerator::base64_vbmi_join_1_2_addr() {
2091   __ align64();
2092   StubCodeMark mark(this, "StubRoutines", "join_1_2_base64");
2093   address start = __ pc();
2094 
2095   assert(((unsigned long long)start & 0x3f) == 0,
2096          "Alignment problem (0x%08llx)", (unsigned long long)start);
2097   __ emit_data64(0x1c1d1e18191a1415, relocInfo::none);
2098   __ emit_data64(0x292a242526202122, relocInfo::none);
2099   __ emit_data64(0x363031322c2d2e28, relocInfo::none);
2100   __ emit_data64(0x3c3d3e38393a3435, relocInfo::none);
2101   __ emit_data64(0x494a444546404142, relocInfo::none);
2102   __ emit_data64(0x565051524c4d4e48, relocInfo::none);
2103   __ emit_data64(0x5c5d5e58595a5455, relocInfo::none);
2104   __ emit_data64(0x696a646566606162, relocInfo::none);
2105 
2106   return start;
2107 }
2108 
2109 address StubGenerator::base64_vbmi_join_2_3_addr() {
2110   __ align64();
2111   StubCodeMark mark(this, "StubRoutines", "join_2_3_base64");
2112   address start = __ pc();
2113 
2114   assert(((unsigned long long)start & 0x3f) == 0,
2115          "Alignment problem (0x%08llx)", (unsigned long long)start);
2116   __ emit_data64(0x363031322c2d2e28, relocInfo::none);
2117   __ emit_data64(0x3c3d3e38393a3435, relocInfo::none);
2118   __ emit_data64(0x494a444546404142, relocInfo::none);
2119   __ emit_data64(0x565051524c4d4e48, relocInfo::none);
2120   __ emit_data64(0x5c5d5e58595a5455, relocInfo::none);
2121   __ emit_data64(0x696a646566606162, relocInfo::none);
2122   __ emit_data64(0x767071726c6d6e68, relocInfo::none);
2123   __ emit_data64(0x7c7d7e78797a7475, relocInfo::none);
2124 
2125   return start;
2126 }
2127 
2128 address StubGenerator::base64_decoding_table_addr() {
2129   StubCodeMark mark(this, "StubRoutines", "decoding_table_base64");
2130   address start = __ pc();
2131 
2132   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2133   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2134   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2135   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2136   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2137   __ emit_data64(0x3fffffff3effffff, relocInfo::none);
2138   __ emit_data64(0x3b3a393837363534, relocInfo::none);
2139   __ emit_data64(0xffffffffffff3d3c, relocInfo::none);
2140   __ emit_data64(0x06050403020100ff, relocInfo::none);
2141   __ emit_data64(0x0e0d0c0b0a090807, relocInfo::none);
2142   __ emit_data64(0x161514131211100f, relocInfo::none);
2143   __ emit_data64(0xffffffffff191817, relocInfo::none);
2144   __ emit_data64(0x201f1e1d1c1b1aff, relocInfo::none);
2145   __ emit_data64(0x2827262524232221, relocInfo::none);
2146   __ emit_data64(0x302f2e2d2c2b2a29, relocInfo::none);
2147   __ emit_data64(0xffffffffff333231, relocInfo::none);
2148   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2149   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2150   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2151   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2152   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2153   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2154   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2155   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2156   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2157   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2158   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2159   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2160   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2161   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2162   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2163   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2164 
2165   // URL table
2166   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2167   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2168   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2169   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2170   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2171   __ emit_data64(0xffff3effffffffff, relocInfo::none);
2172   __ emit_data64(0x3b3a393837363534, relocInfo::none);
2173   __ emit_data64(0xffffffffffff3d3c, relocInfo::none);
2174   __ emit_data64(0x06050403020100ff, relocInfo::none);
2175   __ emit_data64(0x0e0d0c0b0a090807, relocInfo::none);
2176   __ emit_data64(0x161514131211100f, relocInfo::none);
2177   __ emit_data64(0x3fffffffff191817, relocInfo::none);
2178   __ emit_data64(0x201f1e1d1c1b1aff, relocInfo::none);
2179   __ emit_data64(0x2827262524232221, relocInfo::none);
2180   __ emit_data64(0x302f2e2d2c2b2a29, relocInfo::none);
2181   __ emit_data64(0xffffffffff333231, relocInfo::none);
2182   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2183   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2184   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2185   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2186   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2187   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2188   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2189   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2190   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2191   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2192   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2193   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2194   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2195   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2196   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2197   __ emit_data64(0xffffffffffffffff, relocInfo::none);
2198 
2199   return start;
2200 }
2201 
2202 
2203 // Code for generating Base64 decoding.
2204 //
2205 // Based on the article (and associated code) from https://arxiv.org/abs/1910.05109.
2206 //
2207 // Intrinsic function prototype in Base64.java:
2208 // private void decodeBlock(byte[] src, int sp, int sl, byte[] dst, int dp, boolean isURL, isMIME) {
2209 address StubGenerator::generate_base64_decodeBlock() {
2210   __ align(CodeEntryAlignment);
2211   StubCodeMark mark(this, "StubRoutines", "implDecode");
2212   address start = __ pc();
2213 
2214   __ enter();
2215 
2216   // Save callee-saved registers before using them
2217   __ push(r12);
2218   __ push(r13);
2219   __ push(r14);
2220   __ push(r15);
2221   __ push(rbx);
2222 
2223   // arguments
2224   const Register source = c_rarg0; // Source Array
2225   const Register start_offset = c_rarg1; // start offset
2226   const Register end_offset = c_rarg2; // end offset
2227   const Register dest = c_rarg3; // destination array
2228   const Register isMIME = rbx;
2229 
2230 #ifndef _WIN64
2231   const Register dp = c_rarg4;  // Position for writing to dest array
2232   const Register isURL = c_rarg5;// Base64 or URL character set
2233   __ movl(isMIME, Address(rbp, 2 * wordSize));
2234 #else
2235   const Address  dp_mem(rbp, 6 * wordSize);  // length is on stack on Win64
2236   const Address isURL_mem(rbp, 7 * wordSize);
2237   const Register isURL = r10;      // pick the volatile windows register
2238   const Register dp = r12;
2239   __ movl(dp, dp_mem);
2240   __ movl(isURL, isURL_mem);
2241   __ movl(isMIME, Address(rbp, 8 * wordSize));
2242 #endif
2243 
2244   const XMMRegister lookup_lo = xmm5;
2245   const XMMRegister lookup_hi = xmm6;
2246   const XMMRegister errorvec = xmm7;
2247   const XMMRegister pack16_op = xmm9;
2248   const XMMRegister pack32_op = xmm8;
2249   const XMMRegister input0 = xmm3;
2250   const XMMRegister input1 = xmm20;
2251   const XMMRegister input2 = xmm21;
2252   const XMMRegister input3 = xmm19;
2253   const XMMRegister join01 = xmm12;
2254   const XMMRegister join12 = xmm11;
2255   const XMMRegister join23 = xmm10;
2256   const XMMRegister translated0 = xmm2;
2257   const XMMRegister translated1 = xmm1;
2258   const XMMRegister translated2 = xmm0;
2259   const XMMRegister translated3 = xmm4;
2260 
2261   const XMMRegister merged0 = xmm2;
2262   const XMMRegister merged1 = xmm1;
2263   const XMMRegister merged2 = xmm0;
2264   const XMMRegister merged3 = xmm4;
2265   const XMMRegister merge_ab_bc0 = xmm2;
2266   const XMMRegister merge_ab_bc1 = xmm1;
2267   const XMMRegister merge_ab_bc2 = xmm0;
2268   const XMMRegister merge_ab_bc3 = xmm4;
2269 
2270   const XMMRegister pack24bits = xmm4;
2271 
2272   const Register length = r14;
2273   const Register output_size = r13;
2274   const Register output_mask = r15;
2275   const KRegister input_mask = k1;
2276 
2277   const XMMRegister input_initial_valid_b64 = xmm0;
2278   const XMMRegister tmp = xmm10;
2279   const XMMRegister mask = xmm0;
2280   const XMMRegister invalid_b64 = xmm1;
2281 
2282   Label L_process256, L_process64, L_process64Loop, L_exit, L_processdata, L_loadURL;
2283   Label L_continue, L_finalBit, L_padding, L_donePadding, L_bruteForce;
2284   Label L_forceLoop, L_bottomLoop, L_checkMIME, L_exit_no_vzero;
2285 
2286   // calculate length from offsets
2287   __ movl(length, end_offset);
2288   __ subl(length, start_offset);
2289   __ push(dest);          // Save for return value calc
2290 
2291   // If AVX512 VBMI not supported, just compile non-AVX code
2292   if(VM_Version::supports_avx512_vbmi() &&
2293      VM_Version::supports_avx512bw()) {
2294     __ cmpl(length, 128);     // 128-bytes is break-even for AVX-512
2295     __ jcc(Assembler::lessEqual, L_bruteForce);
2296 
2297     __ cmpl(isMIME, 0);
2298     __ jcc(Assembler::notEqual, L_bruteForce);
2299 
2300     // Load lookup tables based on isURL
2301     __ cmpl(isURL, 0);
2302     __ jcc(Assembler::notZero, L_loadURL);
2303 
2304     __ evmovdquq(lookup_lo, ExternalAddress(StubRoutines::x86::base64_vbmi_lookup_lo_addr()), Assembler::AVX_512bit, r13);
2305     __ evmovdquq(lookup_hi, ExternalAddress(StubRoutines::x86::base64_vbmi_lookup_hi_addr()), Assembler::AVX_512bit, r13);
2306 
2307     __ BIND(L_continue);
2308 
2309     __ movl(r15, 0x01400140);
2310     __ evpbroadcastd(pack16_op, r15, Assembler::AVX_512bit);
2311 
2312     __ movl(r15, 0x00011000);
2313     __ evpbroadcastd(pack32_op, r15, Assembler::AVX_512bit);
2314 
2315     __ cmpl(length, 0xff);
2316     __ jcc(Assembler::lessEqual, L_process64);
2317 
2318     // load masks required for decoding data
2319     __ BIND(L_processdata);
2320     __ evmovdquq(join01, ExternalAddress(StubRoutines::x86::base64_vbmi_join_0_1_addr()), Assembler::AVX_512bit,r13);
2321     __ evmovdquq(join12, ExternalAddress(StubRoutines::x86::base64_vbmi_join_1_2_addr()), Assembler::AVX_512bit, r13);
2322     __ evmovdquq(join23, ExternalAddress(StubRoutines::x86::base64_vbmi_join_2_3_addr()), Assembler::AVX_512bit, r13);
2323 
2324     __ align32();
2325     __ BIND(L_process256);
2326     // Grab input data
2327     __ evmovdquq(input0, Address(source, start_offset, Address::times_1, 0x00), Assembler::AVX_512bit);
2328     __ evmovdquq(input1, Address(source, start_offset, Address::times_1, 0x40), Assembler::AVX_512bit);
2329     __ evmovdquq(input2, Address(source, start_offset, Address::times_1, 0x80), Assembler::AVX_512bit);
2330     __ evmovdquq(input3, Address(source, start_offset, Address::times_1, 0xc0), Assembler::AVX_512bit);
2331 
2332     // Copy the low part of the lookup table into the destination of the permutation
2333     __ evmovdquq(translated0, lookup_lo, Assembler::AVX_512bit);
2334     __ evmovdquq(translated1, lookup_lo, Assembler::AVX_512bit);
2335     __ evmovdquq(translated2, lookup_lo, Assembler::AVX_512bit);
2336     __ evmovdquq(translated3, lookup_lo, Assembler::AVX_512bit);
2337 
2338     // Translate the base64 input into "decoded" bytes
2339     __ evpermt2b(translated0, input0, lookup_hi, Assembler::AVX_512bit);
2340     __ evpermt2b(translated1, input1, lookup_hi, Assembler::AVX_512bit);
2341     __ evpermt2b(translated2, input2, lookup_hi, Assembler::AVX_512bit);
2342     __ evpermt2b(translated3, input3, lookup_hi, Assembler::AVX_512bit);
2343 
2344     // OR all of the translations together to check for errors (high-order bit of byte set)
2345     __ vpternlogd(input0, 0xfe, input1, input2, Assembler::AVX_512bit);
2346 
2347     __ vpternlogd(input3, 0xfe, translated0, translated1, Assembler::AVX_512bit);
2348     __ vpternlogd(input0, 0xfe, translated2, translated3, Assembler::AVX_512bit);
2349     __ vpor(errorvec, input3, input0, Assembler::AVX_512bit);
2350 
2351     // Check if there was an error - if so, try 64-byte chunks
2352     __ evpmovb2m(k3, errorvec, Assembler::AVX_512bit);
2353     __ kortestql(k3, k3);
2354     __ jcc(Assembler::notZero, L_process64);
2355 
2356     // The merging and shuffling happens here
2357     // We multiply each byte pair [00dddddd | 00cccccc | 00bbbbbb | 00aaaaaa]
2358     // Multiply [00cccccc] by 2^6 added to [00dddddd] to get [0000cccc | ccdddddd]
2359     // The pack16_op is a vector of 0x01400140, so multiply D by 1 and C by 0x40
2360     __ vpmaddubsw(merge_ab_bc0, translated0, pack16_op, Assembler::AVX_512bit);
2361     __ vpmaddubsw(merge_ab_bc1, translated1, pack16_op, Assembler::AVX_512bit);
2362     __ vpmaddubsw(merge_ab_bc2, translated2, pack16_op, Assembler::AVX_512bit);
2363     __ vpmaddubsw(merge_ab_bc3, translated3, pack16_op, Assembler::AVX_512bit);
2364 
2365     // Now do the same with packed 16-bit values.
2366     // We start with [0000cccc | ccdddddd | 0000aaaa | aabbbbbb]
2367     // pack32_op is 0x00011000 (2^12, 1), so this multiplies [0000aaaa | aabbbbbb] by 2^12
2368     // and adds [0000cccc | ccdddddd] to yield [00000000 | aaaaaabb | bbbbcccc | ccdddddd]
2369     __ vpmaddwd(merged0, merge_ab_bc0, pack32_op, Assembler::AVX_512bit);
2370     __ vpmaddwd(merged1, merge_ab_bc1, pack32_op, Assembler::AVX_512bit);
2371     __ vpmaddwd(merged2, merge_ab_bc2, pack32_op, Assembler::AVX_512bit);
2372     __ vpmaddwd(merged3, merge_ab_bc3, pack32_op, Assembler::AVX_512bit);
2373 
2374     // The join vectors specify which byte from which vector goes into the outputs
2375     // One of every 4 bytes in the extended vector is zero, so we pack them into their
2376     // final positions in the register for storing (256 bytes in, 192 bytes out)
2377     __ evpermt2b(merged0, join01, merged1, Assembler::AVX_512bit);
2378     __ evpermt2b(merged1, join12, merged2, Assembler::AVX_512bit);
2379     __ evpermt2b(merged2, join23, merged3, Assembler::AVX_512bit);
2380 
2381     // Store result
2382     __ evmovdquq(Address(dest, dp, Address::times_1, 0x00), merged0, Assembler::AVX_512bit);
2383     __ evmovdquq(Address(dest, dp, Address::times_1, 0x40), merged1, Assembler::AVX_512bit);
2384     __ evmovdquq(Address(dest, dp, Address::times_1, 0x80), merged2, Assembler::AVX_512bit);
2385 
2386     __ addptr(source, 0x100);
2387     __ addptr(dest, 0xc0);
2388     __ subl(length, 0x100);
2389     __ cmpl(length, 64 * 4);
2390     __ jcc(Assembler::greaterEqual, L_process256);
2391 
2392     // At this point, we've decoded 64 * 4 * n bytes.
2393     // The remaining length will be <= 64 * 4 - 1.
2394     // UNLESS there was an error decoding the first 256-byte chunk.  In this
2395     // case, the length will be arbitrarily long.
2396     //
2397     // Note that this will be the path for MIME-encoded strings.
2398 
2399     __ BIND(L_process64);
2400 
2401     __ evmovdquq(pack24bits, ExternalAddress(StubRoutines::x86::base64_vbmi_pack_vec_addr()), Assembler::AVX_512bit, r13);
2402 
2403     __ cmpl(length, 63);
2404     __ jcc(Assembler::lessEqual, L_finalBit);
2405 
2406     __ mov64(rax, 0x0000ffffffffffff);
2407     __ kmovql(k2, rax);
2408 
2409     __ align32();
2410     __ BIND(L_process64Loop);
2411 
2412     // Handle first 64-byte block
2413 
2414     __ evmovdquq(input0, Address(source, start_offset), Assembler::AVX_512bit);
2415     __ evmovdquq(translated0, lookup_lo, Assembler::AVX_512bit);
2416     __ evpermt2b(translated0, input0, lookup_hi, Assembler::AVX_512bit);
2417 
2418     __ vpor(errorvec, translated0, input0, Assembler::AVX_512bit);
2419 
2420     // Check for error and bomb out before updating dest
2421     __ evpmovb2m(k3, errorvec, Assembler::AVX_512bit);
2422     __ kortestql(k3, k3);
2423     __ jcc(Assembler::notZero, L_exit);
2424 
2425     // Pack output register, selecting correct byte ordering
2426     __ vpmaddubsw(merge_ab_bc0, translated0, pack16_op, Assembler::AVX_512bit);
2427     __ vpmaddwd(merged0, merge_ab_bc0, pack32_op, Assembler::AVX_512bit);
2428     __ vpermb(merged0, pack24bits, merged0, Assembler::AVX_512bit);
2429 
2430     __ evmovdqub(Address(dest, dp), k2, merged0, true, Assembler::AVX_512bit);
2431 
2432     __ subl(length, 64);
2433     __ addptr(source, 64);
2434     __ addptr(dest, 48);
2435 
2436     __ cmpl(length, 64);
2437     __ jcc(Assembler::greaterEqual, L_process64Loop);
2438 
2439     __ cmpl(length, 0);
2440     __ jcc(Assembler::lessEqual, L_exit);
2441 
2442     __ BIND(L_finalBit);
2443     // Now have 1 to 63 bytes left to decode
2444 
2445     // I was going to let Java take care of the final fragment
2446     // however it will repeatedly call this routine for every 4 bytes
2447     // of input data, so handle the rest here.
2448     __ movq(rax, -1);
2449     __ bzhiq(rax, rax, length);    // Input mask in rax
2450 
2451     __ movl(output_size, length);
2452     __ shrl(output_size, 2);   // Find (len / 4) * 3 (output length)
2453     __ lea(output_size, Address(output_size, output_size, Address::times_2, 0));
2454     // output_size in r13
2455 
2456     // Strip pad characters, if any, and adjust length and mask
2457     __ cmpb(Address(source, length, Address::times_1, -1), '=');
2458     __ jcc(Assembler::equal, L_padding);
2459 
2460     __ BIND(L_donePadding);
2461 
2462     // Output size is (64 - output_size), output mask is (all 1s >> output_size).
2463     __ kmovql(input_mask, rax);
2464     __ movq(output_mask, -1);
2465     __ bzhiq(output_mask, output_mask, output_size);
2466 
2467     // Load initial input with all valid base64 characters.  Will be used
2468     // in merging source bytes to avoid masking when determining if an error occurred.
2469     __ movl(rax, 0x61616161);
2470     __ evpbroadcastd(input_initial_valid_b64, rax, Assembler::AVX_512bit);
2471 
2472     // A register containing all invalid base64 decoded values
2473     __ movl(rax, 0x80808080);
2474     __ evpbroadcastd(invalid_b64, rax, Assembler::AVX_512bit);
2475 
2476     // input_mask is in k1
2477     // output_size is in r13
2478     // output_mask is in r15
2479     // zmm0 - free
2480     // zmm1 - 0x00011000
2481     // zmm2 - 0x01400140
2482     // zmm3 - errorvec
2483     // zmm4 - pack vector
2484     // zmm5 - lookup_lo
2485     // zmm6 - lookup_hi
2486     // zmm7 - errorvec
2487     // zmm8 - 0x61616161
2488     // zmm9 - 0x80808080
2489 
2490     // Load only the bytes from source, merging into our "fully-valid" register
2491     __ evmovdqub(input_initial_valid_b64, input_mask, Address(source, start_offset, Address::times_1, 0x0), true, Assembler::AVX_512bit);
2492 
2493     // Decode all bytes within our merged input
2494     __ evmovdquq(tmp, lookup_lo, Assembler::AVX_512bit);
2495     __ evpermt2b(tmp, input_initial_valid_b64, lookup_hi, Assembler::AVX_512bit);
2496     __ vporq(mask, tmp, input_initial_valid_b64, Assembler::AVX_512bit);
2497 
2498     // Check for error.  Compare (decoded | initial) to all invalid.
2499     // If any bytes have their high-order bit set, then we have an error.
2500     __ evptestmb(k2, mask, invalid_b64, Assembler::AVX_512bit);
2501     __ kortestql(k2, k2);
2502 
2503     // If we have an error, use the brute force loop to decode what we can (4-byte chunks).
2504     __ jcc(Assembler::notZero, L_bruteForce);
2505 
2506     // Shuffle output bytes
2507     __ vpmaddubsw(tmp, tmp, pack16_op, Assembler::AVX_512bit);
2508     __ vpmaddwd(tmp, tmp, pack32_op, Assembler::AVX_512bit);
2509 
2510     __ vpermb(tmp, pack24bits, tmp, Assembler::AVX_512bit);
2511     __ kmovql(k1, output_mask);
2512     __ evmovdqub(Address(dest, dp), k1, tmp, true, Assembler::AVX_512bit);
2513 
2514     __ addptr(dest, output_size);
2515 
2516     __ BIND(L_exit);
2517     __ vzeroupper();
2518     __ pop(rax);             // Get original dest value
2519     __ subptr(dest, rax);      // Number of bytes converted
2520     __ movptr(rax, dest);
2521     __ pop(rbx);
2522     __ pop(r15);
2523     __ pop(r14);
2524     __ pop(r13);
2525     __ pop(r12);
2526     __ leave();
2527     __ ret(0);
2528 
2529     __ BIND(L_loadURL);
2530     __ evmovdquq(lookup_lo, ExternalAddress(StubRoutines::x86::base64_vbmi_lookup_lo_url_addr()), Assembler::AVX_512bit, r13);
2531     __ evmovdquq(lookup_hi, ExternalAddress(StubRoutines::x86::base64_vbmi_lookup_hi_url_addr()), Assembler::AVX_512bit, r13);
2532     __ jmp(L_continue);
2533 
2534     __ BIND(L_padding);
2535     __ decrementq(output_size, 1);
2536     __ shrq(rax, 1);
2537 
2538     __ cmpb(Address(source, length, Address::times_1, -2), '=');
2539     __ jcc(Assembler::notEqual, L_donePadding);
2540 
2541     __ decrementq(output_size, 1);
2542     __ shrq(rax, 1);
2543     __ jmp(L_donePadding);
2544 
2545     __ align32();
2546     __ BIND(L_bruteForce);
2547   }   // End of if(avx512_vbmi)
2548 
2549   // Use non-AVX code to decode 4-byte chunks into 3 bytes of output
2550 
2551   // Register state (Linux):
2552   // r12-15 - saved on stack
2553   // rdi - src
2554   // rsi - sp
2555   // rdx - sl
2556   // rcx - dst
2557   // r8 - dp
2558   // r9 - isURL
2559 
2560   // Register state (Windows):
2561   // r12-15 - saved on stack
2562   // rcx - src
2563   // rdx - sp
2564   // r8 - sl
2565   // r9 - dst
2566   // r12 - dp
2567   // r10 - isURL
2568 
2569   // Registers (common):
2570   // length (r14) - bytes in src
2571 
2572   const Register decode_table = r11;
2573   const Register out_byte_count = rbx;
2574   const Register byte1 = r13;
2575   const Register byte2 = r15;
2576   const Register byte3 = WIN64_ONLY(r8) NOT_WIN64(rdx);
2577   const Register byte4 = WIN64_ONLY(r10) NOT_WIN64(r9);
2578 
2579   __ shrl(length, 2);    // Multiple of 4 bytes only - length is # 4-byte chunks
2580   __ cmpl(length, 0);
2581   __ jcc(Assembler::lessEqual, L_exit_no_vzero);
2582 
2583   __ shll(isURL, 8);    // index into decode table based on isURL
2584   __ lea(decode_table, ExternalAddress(StubRoutines::x86::base64_decoding_table_addr()));
2585   __ addptr(decode_table, isURL);
2586 
2587   __ jmp(L_bottomLoop);
2588 
2589   __ align32();
2590   __ BIND(L_forceLoop);
2591   __ shll(byte1, 18);
2592   __ shll(byte2, 12);
2593   __ shll(byte3, 6);
2594   __ orl(byte1, byte2);
2595   __ orl(byte1, byte3);
2596   __ orl(byte1, byte4);
2597 
2598   __ addptr(source, 4);
2599 
2600   __ movb(Address(dest, dp, Address::times_1, 2), byte1);
2601   __ shrl(byte1, 8);
2602   __ movb(Address(dest, dp, Address::times_1, 1), byte1);
2603   __ shrl(byte1, 8);
2604   __ movb(Address(dest, dp, Address::times_1, 0), byte1);
2605 
2606   __ addptr(dest, 3);
2607   __ decrementl(length, 1);
2608   __ jcc(Assembler::zero, L_exit_no_vzero);
2609 
2610   __ BIND(L_bottomLoop);
2611   __ load_unsigned_byte(byte1, Address(source, start_offset, Address::times_1, 0x00));
2612   __ load_unsigned_byte(byte2, Address(source, start_offset, Address::times_1, 0x01));
2613   __ load_signed_byte(byte1, Address(decode_table, byte1));
2614   __ load_signed_byte(byte2, Address(decode_table, byte2));
2615   __ load_unsigned_byte(byte3, Address(source, start_offset, Address::times_1, 0x02));
2616   __ load_unsigned_byte(byte4, Address(source, start_offset, Address::times_1, 0x03));
2617   __ load_signed_byte(byte3, Address(decode_table, byte3));
2618   __ load_signed_byte(byte4, Address(decode_table, byte4));
2619 
2620   __ mov(rax, byte1);
2621   __ orl(rax, byte2);
2622   __ orl(rax, byte3);
2623   __ orl(rax, byte4);
2624   __ jcc(Assembler::positive, L_forceLoop);
2625 
2626   __ BIND(L_exit_no_vzero);
2627   __ pop(rax);             // Get original dest value
2628   __ subptr(dest, rax);      // Number of bytes converted
2629   __ movptr(rax, dest);
2630   __ pop(rbx);
2631   __ pop(r15);
2632   __ pop(r14);
2633   __ pop(r13);
2634   __ pop(r12);
2635   __ leave();
2636   __ ret(0);
2637 
2638   return start;
2639 }
2640 
2641 
2642 /**
2643  *  Arguments:
2644  *
2645  * Inputs:
2646  *   c_rarg0   - int crc
2647  *   c_rarg1   - byte* buf
2648  *   c_rarg2   - int length
2649  *
2650  * Output:
2651  *       rax   - int crc result
2652  */
2653 address StubGenerator::generate_updateBytesCRC32() {
2654   assert(UseCRC32Intrinsics, "need AVX and CLMUL instructions");
2655 
2656   __ align(CodeEntryAlignment);
2657   StubCodeMark mark(this, "StubRoutines", "updateBytesCRC32");
2658 
2659   address start = __ pc();
2660 
2661   // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
2662   // Unix:  rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
2663   // rscratch1: r10
2664   const Register crc   = c_rarg0;  // crc
2665   const Register buf   = c_rarg1;  // source java byte array address
2666   const Register len   = c_rarg2;  // length
2667   const Register table = c_rarg3;  // crc_table address (reuse register)
2668   const Register tmp1   = r11;
2669   const Register tmp2   = r10;
2670   assert_different_registers(crc, buf, len, table, tmp1, tmp2, rax);
2671 
2672   BLOCK_COMMENT("Entry:");
2673   __ enter(); // required for proper stackwalking of RuntimeStub frame
2674 
2675   if (VM_Version::supports_sse4_1() && VM_Version::supports_avx512_vpclmulqdq() &&
2676       VM_Version::supports_avx512bw() &&
2677       VM_Version::supports_avx512vl()) {
2678       // The constants used in the CRC32 algorithm requires the 1's compliment of the initial crc value.
2679       // However, the constant table for CRC32-C assumes the original crc value.  Account for this
2680       // difference before calling and after returning.
2681     __ lea(table, ExternalAddress(StubRoutines::x86::crc_table_avx512_addr()));
2682     __ notl(crc);
2683     __ kernel_crc32_avx512(crc, buf, len, table, tmp1, tmp2);
2684     __ notl(crc);
2685   } else {
2686     __ kernel_crc32(crc, buf, len, table, tmp1);
2687   }
2688 
2689   __ movl(rax, crc);
2690   __ vzeroupper();
2691   __ leave(); // required for proper stackwalking of RuntimeStub frame
2692   __ ret(0);
2693 
2694   return start;
2695 }
2696 
2697 /**
2698 *  Arguments:
2699 *
2700 * Inputs:
2701 *   c_rarg0   - int crc
2702 *   c_rarg1   - byte* buf
2703 *   c_rarg2   - long length
2704 *   c_rarg3   - table_start - optional (present only when doing a library_call,
2705 *              not used by x86 algorithm)
2706 *
2707 * Output:
2708 *       rax   - int crc result
2709 */
2710 address StubGenerator::generate_updateBytesCRC32C(bool is_pclmulqdq_supported) {
2711   assert(UseCRC32CIntrinsics, "need SSE4_2");
2712   __ align(CodeEntryAlignment);
2713   StubCodeMark mark(this, "StubRoutines", "updateBytesCRC32C");
2714   address start = __ pc();
2715 
2716   //reg.arg        int#0        int#1        int#2        int#3        int#4        int#5        float regs
2717   //Windows        RCX          RDX          R8           R9           none         none         XMM0..XMM3
2718   //Lin / Sol      RDI          RSI          RDX          RCX          R8           R9           XMM0..XMM7
2719   const Register crc = c_rarg0;  // crc
2720   const Register buf = c_rarg1;  // source java byte array address
2721   const Register len = c_rarg2;  // length
2722   const Register a = rax;
2723   const Register j = r9;
2724   const Register k = r10;
2725   const Register l = r11;
2726 #ifdef _WIN64
2727   const Register y = rdi;
2728   const Register z = rsi;
2729 #else
2730   const Register y = rcx;
2731   const Register z = r8;
2732 #endif
2733   assert_different_registers(crc, buf, len, a, j, k, l, y, z);
2734 
2735   BLOCK_COMMENT("Entry:");
2736   __ enter(); // required for proper stackwalking of RuntimeStub frame
2737   if (VM_Version::supports_sse4_1() && VM_Version::supports_avx512_vpclmulqdq() &&
2738       VM_Version::supports_avx512bw() &&
2739       VM_Version::supports_avx512vl()) {
2740     __ lea(j, ExternalAddress(StubRoutines::x86::crc32c_table_avx512_addr()));
2741     __ kernel_crc32_avx512(crc, buf, len, j, l, k);
2742   } else {
2743 #ifdef _WIN64
2744     __ push(y);
2745     __ push(z);
2746 #endif
2747     __ crc32c_ipl_alg2_alt2(crc, buf, len,
2748                             a, j, k,
2749                             l, y, z,
2750                             c_farg0, c_farg1, c_farg2,
2751                             is_pclmulqdq_supported);
2752 #ifdef _WIN64
2753     __ pop(z);
2754     __ pop(y);
2755 #endif
2756   }
2757   __ movl(rax, crc);
2758   __ vzeroupper();
2759   __ leave(); // required for proper stackwalking of RuntimeStub frame
2760   __ ret(0);
2761 
2762   return start;
2763 }
2764 
2765 
2766 /**
2767  *  Arguments:
2768  *
2769  *  Input:
2770  *    c_rarg0   - x address
2771  *    c_rarg1   - x length
2772  *    c_rarg2   - y address
2773  *    c_rarg3   - y length
2774  * not Win64
2775  *    c_rarg4   - z address
2776  *    c_rarg5   - z length
2777  * Win64
2778  *    rsp+40    - z address
2779  *    rsp+48    - z length
2780  */
2781 address StubGenerator::generate_multiplyToLen() {
2782   __ align(CodeEntryAlignment);
2783   StubCodeMark mark(this, "StubRoutines", "multiplyToLen");
2784   address start = __ pc();
2785 
2786   // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
2787   // Unix:  rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
2788   const Register x     = rdi;
2789   const Register xlen  = rax;
2790   const Register y     = rsi;
2791   const Register ylen  = rcx;
2792   const Register z     = r8;
2793   const Register zlen  = r11;
2794 
2795   // Next registers will be saved on stack in multiply_to_len().
2796   const Register tmp1  = r12;
2797   const Register tmp2  = r13;
2798   const Register tmp3  = r14;
2799   const Register tmp4  = r15;
2800   const Register tmp5  = rbx;
2801 
2802   BLOCK_COMMENT("Entry:");
2803   __ enter(); // required for proper stackwalking of RuntimeStub frame
2804 
2805 #ifndef _WIN64
2806   __ movptr(zlen, r9); // Save r9 in r11 - zlen
2807 #endif
2808   setup_arg_regs(4); // x => rdi, xlen => rsi, y => rdx
2809                      // ylen => rcx, z => r8, zlen => r11
2810                      // r9 and r10 may be used to save non-volatile registers
2811 #ifdef _WIN64
2812   // last 2 arguments (#4, #5) are on stack on Win64
2813   __ movptr(z, Address(rsp, 6 * wordSize));
2814   __ movptr(zlen, Address(rsp, 7 * wordSize));
2815 #endif
2816 
2817   __ movptr(xlen, rsi);
2818   __ movptr(y,    rdx);
2819   __ multiply_to_len(x, xlen, y, ylen, z, zlen, tmp1, tmp2, tmp3, tmp4, tmp5);
2820 
2821   restore_arg_regs();
2822 
2823   __ leave(); // required for proper stackwalking of RuntimeStub frame
2824   __ ret(0);
2825 
2826   return start;
2827 }
2828 
2829 /**
2830 *  Arguments:
2831 *
2832 *  Input:
2833 *    c_rarg0   - obja     address
2834 *    c_rarg1   - objb     address
2835 *    c_rarg3   - length   length
2836 *    c_rarg4   - scale    log2_array_indxscale
2837 *
2838 *  Output:
2839 *        rax   - int >= mismatched index, < 0 bitwise complement of tail
2840 */
2841 address StubGenerator::generate_vectorizedMismatch() {
2842   __ align(CodeEntryAlignment);
2843   StubCodeMark mark(this, "StubRoutines", "vectorizedMismatch");
2844   address start = __ pc();
2845 
2846   BLOCK_COMMENT("Entry:");
2847   __ enter();
2848 
2849 #ifdef _WIN64  // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
2850   const Register scale = c_rarg0;  //rcx, will exchange with r9
2851   const Register objb = c_rarg1;   //rdx
2852   const Register length = c_rarg2; //r8
2853   const Register obja = c_rarg3;   //r9
2854   __ xchgq(obja, scale);  //now obja and scale contains the correct contents
2855 
2856   const Register tmp1 = r10;
2857   const Register tmp2 = r11;
2858 #endif
2859 #ifndef _WIN64 // Unix:  rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
2860   const Register obja = c_rarg0;   //U:rdi
2861   const Register objb = c_rarg1;   //U:rsi
2862   const Register length = c_rarg2; //U:rdx
2863   const Register scale = c_rarg3;  //U:rcx
2864   const Register tmp1 = r8;
2865   const Register tmp2 = r9;
2866 #endif
2867   const Register result = rax; //return value
2868   const XMMRegister vec0 = xmm0;
2869   const XMMRegister vec1 = xmm1;
2870   const XMMRegister vec2 = xmm2;
2871 
2872   __ vectorized_mismatch(obja, objb, length, scale, result, tmp1, tmp2, vec0, vec1, vec2);
2873 
2874   __ vzeroupper();
2875   __ leave();
2876   __ ret(0);
2877 
2878   return start;
2879 }
2880 
2881 /**
2882  *  Arguments:
2883  *
2884 //  Input:
2885 //    c_rarg0   - x address
2886 //    c_rarg1   - x length
2887 //    c_rarg2   - z address
2888 //    c_rarg3   - z length
2889  *
2890  */
2891 address StubGenerator::generate_squareToLen() {
2892 
2893   __ align(CodeEntryAlignment);
2894   StubCodeMark mark(this, "StubRoutines", "squareToLen");
2895   address start = __ pc();
2896 
2897   // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
2898   // Unix:  rdi, rsi, rdx, rcx (c_rarg0, c_rarg1, ...)
2899   const Register x      = rdi;
2900   const Register len    = rsi;
2901   const Register z      = r8;
2902   const Register zlen   = rcx;
2903 
2904  const Register tmp1      = r12;
2905  const Register tmp2      = r13;
2906  const Register tmp3      = r14;
2907  const Register tmp4      = r15;
2908  const Register tmp5      = rbx;
2909 
2910   BLOCK_COMMENT("Entry:");
2911   __ enter(); // required for proper stackwalking of RuntimeStub frame
2912 
2913   setup_arg_regs(4); // x => rdi, len => rsi, z => rdx
2914                      // zlen => rcx
2915                      // r9 and r10 may be used to save non-volatile registers
2916   __ movptr(r8, rdx);
2917   __ square_to_len(x, len, z, zlen, tmp1, tmp2, tmp3, tmp4, tmp5, rdx, rax);
2918 
2919   restore_arg_regs();
2920 
2921   __ leave(); // required for proper stackwalking of RuntimeStub frame
2922   __ ret(0);
2923 
2924   return start;
2925 }
2926 
2927 address StubGenerator::generate_method_entry_barrier() {
2928   __ align(CodeEntryAlignment);
2929   StubCodeMark mark(this, "StubRoutines", "nmethod_entry_barrier");
2930   address start = __ pc();
2931 
2932   Label deoptimize_label;
2933 
2934   __ push(-1); // cookie, this is used for writing the new rsp when deoptimizing
2935 
2936   BLOCK_COMMENT("Entry:");
2937   __ enter(); // save rbp
2938 
2939   // save c_rarg0, because we want to use that value.
2940   // We could do without it but then we depend on the number of slots used by pusha
2941   __ push(c_rarg0);
2942 
2943   __ lea(c_rarg0, Address(rsp, wordSize * 3)); // 1 for cookie, 1 for rbp, 1 for c_rarg0 - this should be the return address
2944 
2945   __ pusha();
2946 
2947   // The method may have floats as arguments, and we must spill them before calling
2948   // the VM runtime.
2949   assert(Argument::n_float_register_parameters_j == 8, "Assumption");
2950   const int xmm_size = wordSize * 2;
2951   const int xmm_spill_size = xmm_size * Argument::n_float_register_parameters_j;
2952   __ subptr(rsp, xmm_spill_size);
2953   __ movdqu(Address(rsp, xmm_size * 7), xmm7);
2954   __ movdqu(Address(rsp, xmm_size * 6), xmm6);
2955   __ movdqu(Address(rsp, xmm_size * 5), xmm5);
2956   __ movdqu(Address(rsp, xmm_size * 4), xmm4);
2957   __ movdqu(Address(rsp, xmm_size * 3), xmm3);
2958   __ movdqu(Address(rsp, xmm_size * 2), xmm2);
2959   __ movdqu(Address(rsp, xmm_size * 1), xmm1);
2960   __ movdqu(Address(rsp, xmm_size * 0), xmm0);
2961 
2962   __ call_VM_leaf(CAST_FROM_FN_PTR(address, static_cast<int (*)(address*)>(BarrierSetNMethod::nmethod_stub_entry_barrier)), 1);
2963 
2964   __ movdqu(xmm0, Address(rsp, xmm_size * 0));
2965   __ movdqu(xmm1, Address(rsp, xmm_size * 1));
2966   __ movdqu(xmm2, Address(rsp, xmm_size * 2));
2967   __ movdqu(xmm3, Address(rsp, xmm_size * 3));
2968   __ movdqu(xmm4, Address(rsp, xmm_size * 4));
2969   __ movdqu(xmm5, Address(rsp, xmm_size * 5));
2970   __ movdqu(xmm6, Address(rsp, xmm_size * 6));
2971   __ movdqu(xmm7, Address(rsp, xmm_size * 7));
2972   __ addptr(rsp, xmm_spill_size);
2973 
2974   __ cmpl(rax, 1); // 1 means deoptimize
2975   __ jcc(Assembler::equal, deoptimize_label);
2976 
2977   __ popa();
2978   __ pop(c_rarg0);
2979 
2980   __ leave();
2981 
2982   __ addptr(rsp, 1 * wordSize); // cookie
2983   __ ret(0);
2984 
2985 
2986   __ BIND(deoptimize_label);
2987 
2988   __ popa();
2989   __ pop(c_rarg0);
2990 
2991   __ leave();
2992 
2993   // this can be taken out, but is good for verification purposes. getting a SIGSEGV
2994   // here while still having a correct stack is valuable
2995   __ testptr(rsp, Address(rsp, 0));
2996 
2997   __ movptr(rsp, Address(rsp, 0)); // new rsp was written in the barrier
2998   __ jmp(Address(rsp, -1 * wordSize)); // jmp target should be callers verified_entry_point
2999 
3000   return start;
3001 }
3002 
3003  /**
3004  *  Arguments:
3005  *
3006  *  Input:
3007  *    c_rarg0   - out address
3008  *    c_rarg1   - in address
3009  *    c_rarg2   - offset
3010  *    c_rarg3   - len
3011  * not Win64
3012  *    c_rarg4   - k
3013  * Win64
3014  *    rsp+40    - k
3015  */
3016 address StubGenerator::generate_mulAdd() {
3017   __ align(CodeEntryAlignment);
3018   StubCodeMark mark(this, "StubRoutines", "mulAdd");
3019   address start = __ pc();
3020 
3021   // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
3022   // Unix:  rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
3023   const Register out     = rdi;
3024   const Register in      = rsi;
3025   const Register offset  = r11;
3026   const Register len     = rcx;
3027   const Register k       = r8;
3028 
3029   // Next registers will be saved on stack in mul_add().
3030   const Register tmp1  = r12;
3031   const Register tmp2  = r13;
3032   const Register tmp3  = r14;
3033   const Register tmp4  = r15;
3034   const Register tmp5  = rbx;
3035 
3036   BLOCK_COMMENT("Entry:");
3037   __ enter(); // required for proper stackwalking of RuntimeStub frame
3038 
3039   setup_arg_regs(4); // out => rdi, in => rsi, offset => rdx
3040                      // len => rcx, k => r8
3041                      // r9 and r10 may be used to save non-volatile registers
3042 #ifdef _WIN64
3043   // last argument is on stack on Win64
3044   __ movl(k, Address(rsp, 6 * wordSize));
3045 #endif
3046   __ movptr(r11, rdx);  // move offset in rdx to offset(r11)
3047   __ mul_add(out, in, offset, len, k, tmp1, tmp2, tmp3, tmp4, tmp5, rdx, rax);
3048 
3049   restore_arg_regs();
3050 
3051   __ leave(); // required for proper stackwalking of RuntimeStub frame
3052   __ ret(0);
3053 
3054   return start;
3055 }
3056 
3057 address StubGenerator::generate_bigIntegerRightShift() {
3058   __ align(CodeEntryAlignment);
3059   StubCodeMark mark(this, "StubRoutines", "bigIntegerRightShiftWorker");
3060   address start = __ pc();
3061 
3062   Label Shift512Loop, ShiftTwo, ShiftTwoLoop, ShiftOne, Exit;
3063   // For Unix, the arguments are as follows: rdi, rsi, rdx, rcx, r8.
3064   const Register newArr = rdi;
3065   const Register oldArr = rsi;
3066   const Register newIdx = rdx;
3067   const Register shiftCount = rcx;  // It was intentional to have shiftCount in rcx since it is used implicitly for shift.
3068   const Register totalNumIter = r8;
3069 
3070   // For windows, we use r9 and r10 as temps to save rdi and rsi. Thus we cannot allocate them for our temps.
3071   // For everything else, we prefer using r9 and r10 since we do not have to save them before use.
3072   const Register tmp1 = r11;                    // Caller save.
3073   const Register tmp2 = rax;                    // Caller save.
3074   const Register tmp3 = WIN64_ONLY(r12) NOT_WIN64(r9);   // Windows: Callee save. Linux: Caller save.
3075   const Register tmp4 = WIN64_ONLY(r13) NOT_WIN64(r10);  // Windows: Callee save. Linux: Caller save.
3076   const Register tmp5 = r14;                    // Callee save.
3077   const Register tmp6 = r15;
3078 
3079   const XMMRegister x0 = xmm0;
3080   const XMMRegister x1 = xmm1;
3081   const XMMRegister x2 = xmm2;
3082 
3083   BLOCK_COMMENT("Entry:");
3084   __ enter(); // required for proper stackwalking of RuntimeStub frame
3085 
3086 #ifdef _WIN64
3087   setup_arg_regs(4);
3088   // For windows, since last argument is on stack, we need to move it to the appropriate register.
3089   __ movl(totalNumIter, Address(rsp, 6 * wordSize));
3090   // Save callee save registers.
3091   __ push(tmp3);
3092   __ push(tmp4);
3093 #endif
3094   __ push(tmp5);
3095 
3096   // Rename temps used throughout the code.
3097   const Register idx = tmp1;
3098   const Register nIdx = tmp2;
3099 
3100   __ xorl(idx, idx);
3101 
3102   // Start right shift from end of the array.
3103   // For example, if #iteration = 4 and newIdx = 1
3104   // then dest[4] = src[4] >> shiftCount  | src[3] <<< (shiftCount - 32)
3105   // if #iteration = 4 and newIdx = 0
3106   // then dest[3] = src[4] >> shiftCount  | src[3] <<< (shiftCount - 32)
3107   __ movl(idx, totalNumIter);
3108   __ movl(nIdx, idx);
3109   __ addl(nIdx, newIdx);
3110 
3111   // If vectorization is enabled, check if the number of iterations is at least 64
3112   // If not, then go to ShifTwo processing 2 iterations
3113   if (VM_Version::supports_avx512_vbmi2()) {
3114     __ cmpptr(totalNumIter, (AVX3Threshold/64));
3115     __ jcc(Assembler::less, ShiftTwo);
3116 
3117     if (AVX3Threshold < 16 * 64) {
3118       __ cmpl(totalNumIter, 16);
3119       __ jcc(Assembler::less, ShiftTwo);
3120     }
3121     __ evpbroadcastd(x0, shiftCount, Assembler::AVX_512bit);
3122     __ subl(idx, 16);
3123     __ subl(nIdx, 16);
3124     __ BIND(Shift512Loop);
3125     __ evmovdqul(x2, Address(oldArr, idx, Address::times_4, 4), Assembler::AVX_512bit);
3126     __ evmovdqul(x1, Address(oldArr, idx, Address::times_4), Assembler::AVX_512bit);
3127     __ vpshrdvd(x2, x1, x0, Assembler::AVX_512bit);
3128     __ evmovdqul(Address(newArr, nIdx, Address::times_4), x2, Assembler::AVX_512bit);
3129     __ subl(nIdx, 16);
3130     __ subl(idx, 16);
3131     __ jcc(Assembler::greaterEqual, Shift512Loop);
3132     __ addl(idx, 16);
3133     __ addl(nIdx, 16);
3134   }
3135   __ BIND(ShiftTwo);
3136   __ cmpl(idx, 2);
3137   __ jcc(Assembler::less, ShiftOne);
3138   __ subl(idx, 2);
3139   __ subl(nIdx, 2);
3140   __ BIND(ShiftTwoLoop);
3141   __ movl(tmp5, Address(oldArr, idx, Address::times_4, 8));
3142   __ movl(tmp4, Address(oldArr, idx, Address::times_4, 4));
3143   __ movl(tmp3, Address(oldArr, idx, Address::times_4));
3144   __ shrdl(tmp5, tmp4);
3145   __ shrdl(tmp4, tmp3);
3146   __ movl(Address(newArr, nIdx, Address::times_4, 4), tmp5);
3147   __ movl(Address(newArr, nIdx, Address::times_4), tmp4);
3148   __ subl(nIdx, 2);
3149   __ subl(idx, 2);
3150   __ jcc(Assembler::greaterEqual, ShiftTwoLoop);
3151   __ addl(idx, 2);
3152   __ addl(nIdx, 2);
3153 
3154   // Do the last iteration
3155   __ BIND(ShiftOne);
3156   __ cmpl(idx, 1);
3157   __ jcc(Assembler::less, Exit);
3158   __ subl(idx, 1);
3159   __ subl(nIdx, 1);
3160   __ movl(tmp4, Address(oldArr, idx, Address::times_4, 4));
3161   __ movl(tmp3, Address(oldArr, idx, Address::times_4));
3162   __ shrdl(tmp4, tmp3);
3163   __ movl(Address(newArr, nIdx, Address::times_4), tmp4);
3164   __ BIND(Exit);
3165   __ vzeroupper();
3166   // Restore callee save registers.
3167   __ pop(tmp5);
3168 #ifdef _WIN64
3169   __ pop(tmp4);
3170   __ pop(tmp3);
3171   restore_arg_regs();
3172 #endif
3173   __ leave(); // required for proper stackwalking of RuntimeStub frame
3174   __ ret(0);
3175 
3176   return start;
3177 }
3178 
3179  /**
3180  *  Arguments:
3181  *
3182  *  Input:
3183  *    c_rarg0   - newArr address
3184  *    c_rarg1   - oldArr address
3185  *    c_rarg2   - newIdx
3186  *    c_rarg3   - shiftCount
3187  * not Win64
3188  *    c_rarg4   - numIter
3189  * Win64
3190  *    rsp40    - numIter
3191  */
3192 address StubGenerator::generate_bigIntegerLeftShift() {
3193   __ align(CodeEntryAlignment);
3194   StubCodeMark mark(this,  "StubRoutines", "bigIntegerLeftShiftWorker");
3195   address start = __ pc();
3196 
3197   Label Shift512Loop, ShiftTwo, ShiftTwoLoop, ShiftOne, Exit;
3198   // For Unix, the arguments are as follows: rdi, rsi, rdx, rcx, r8.
3199   const Register newArr = rdi;
3200   const Register oldArr = rsi;
3201   const Register newIdx = rdx;
3202   const Register shiftCount = rcx;  // It was intentional to have shiftCount in rcx since it is used implicitly for shift.
3203   const Register totalNumIter = r8;
3204   // For windows, we use r9 and r10 as temps to save rdi and rsi. Thus we cannot allocate them for our temps.
3205   // For everything else, we prefer using r9 and r10 since we do not have to save them before use.
3206   const Register tmp1 = r11;                    // Caller save.
3207   const Register tmp2 = rax;                    // Caller save.
3208   const Register tmp3 = WIN64_ONLY(r12) NOT_WIN64(r9);   // Windows: Callee save. Linux: Caller save.
3209   const Register tmp4 = WIN64_ONLY(r13) NOT_WIN64(r10);  // Windows: Callee save. Linux: Caller save.
3210   const Register tmp5 = r14;                    // Callee save.
3211 
3212   const XMMRegister x0 = xmm0;
3213   const XMMRegister x1 = xmm1;
3214   const XMMRegister x2 = xmm2;
3215   BLOCK_COMMENT("Entry:");
3216   __ enter(); // required for proper stackwalking of RuntimeStub frame
3217 
3218 #ifdef _WIN64
3219   setup_arg_regs(4);
3220   // For windows, since last argument is on stack, we need to move it to the appropriate register.
3221   __ movl(totalNumIter, Address(rsp, 6 * wordSize));
3222   // Save callee save registers.
3223   __ push(tmp3);
3224   __ push(tmp4);
3225 #endif
3226   __ push(tmp5);
3227 
3228   // Rename temps used throughout the code
3229   const Register idx = tmp1;
3230   const Register numIterTmp = tmp2;
3231 
3232   // Start idx from zero.
3233   __ xorl(idx, idx);
3234   // Compute interior pointer for new array. We do this so that we can use same index for both old and new arrays.
3235   __ lea(newArr, Address(newArr, newIdx, Address::times_4));
3236   __ movl(numIterTmp, totalNumIter);
3237 
3238   // If vectorization is enabled, check if the number of iterations is at least 64
3239   // If not, then go to ShiftTwo shifting two numbers at a time
3240   if (VM_Version::supports_avx512_vbmi2()) {
3241     __ cmpl(totalNumIter, (AVX3Threshold/64));
3242     __ jcc(Assembler::less, ShiftTwo);
3243 
3244     if (AVX3Threshold < 16 * 64) {
3245       __ cmpl(totalNumIter, 16);
3246       __ jcc(Assembler::less, ShiftTwo);
3247     }
3248     __ evpbroadcastd(x0, shiftCount, Assembler::AVX_512bit);
3249     __ subl(numIterTmp, 16);
3250     __ BIND(Shift512Loop);
3251     __ evmovdqul(x1, Address(oldArr, idx, Address::times_4), Assembler::AVX_512bit);
3252     __ evmovdqul(x2, Address(oldArr, idx, Address::times_4, 0x4), Assembler::AVX_512bit);
3253     __ vpshldvd(x1, x2, x0, Assembler::AVX_512bit);
3254     __ evmovdqul(Address(newArr, idx, Address::times_4), x1, Assembler::AVX_512bit);
3255     __ addl(idx, 16);
3256     __ subl(numIterTmp, 16);
3257     __ jcc(Assembler::greaterEqual, Shift512Loop);
3258     __ addl(numIterTmp, 16);
3259   }
3260   __ BIND(ShiftTwo);
3261   __ cmpl(totalNumIter, 1);
3262   __ jcc(Assembler::less, Exit);
3263   __ movl(tmp3, Address(oldArr, idx, Address::times_4));
3264   __ subl(numIterTmp, 2);
3265   __ jcc(Assembler::less, ShiftOne);
3266 
3267   __ BIND(ShiftTwoLoop);
3268   __ movl(tmp4, Address(oldArr, idx, Address::times_4, 0x4));
3269   __ movl(tmp5, Address(oldArr, idx, Address::times_4, 0x8));
3270   __ shldl(tmp3, tmp4);
3271   __ shldl(tmp4, tmp5);
3272   __ movl(Address(newArr, idx, Address::times_4), tmp3);
3273   __ movl(Address(newArr, idx, Address::times_4, 0x4), tmp4);
3274   __ movl(tmp3, tmp5);
3275   __ addl(idx, 2);
3276   __ subl(numIterTmp, 2);
3277   __ jcc(Assembler::greaterEqual, ShiftTwoLoop);
3278 
3279   // Do the last iteration
3280   __ BIND(ShiftOne);
3281   __ addl(numIterTmp, 2);
3282   __ cmpl(numIterTmp, 1);
3283   __ jcc(Assembler::less, Exit);
3284   __ movl(tmp4, Address(oldArr, idx, Address::times_4, 0x4));
3285   __ shldl(tmp3, tmp4);
3286   __ movl(Address(newArr, idx, Address::times_4), tmp3);
3287 
3288   __ BIND(Exit);
3289   __ vzeroupper();
3290   // Restore callee save registers.
3291   __ pop(tmp5);
3292 #ifdef _WIN64
3293   __ pop(tmp4);
3294   __ pop(tmp3);
3295   restore_arg_regs();
3296 #endif
3297   __ leave(); // required for proper stackwalking of RuntimeStub frame
3298   __ ret(0);
3299 
3300   return start;
3301 }
3302 
3303 void StubGenerator::generate_libm_stubs() {
3304   if (UseLibmIntrinsic && InlineIntrinsics) {
3305     if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dsin)) {
3306       StubRoutines::_dsin = generate_libmSin(); // from stubGenerator_x86_64_sin.cpp
3307     }
3308     if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dcos)) {
3309       StubRoutines::_dcos = generate_libmCos(); // from stubGenerator_x86_64_cos.cpp
3310     }
3311     if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dtan)) {
3312       StubRoutines::_dtan = generate_libmTan();
3313     }
3314     if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dexp)) {
3315       StubRoutines::_dexp = generate_libmExp();
3316     }
3317     if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dpow)) {
3318       StubRoutines::_dpow = generate_libmPow();
3319     }
3320     if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dlog)) {
3321       StubRoutines::_dlog = generate_libmLog();
3322     }
3323     if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_dlog10)) {
3324       StubRoutines::_dlog10 = generate_libmLog10();
3325     }
3326   }
3327 }
3328 
3329 
3330 address StubGenerator::generate_cont_thaw(const char* label, Continuation::thaw_kind kind) {
3331   if (!Continuations::enabled()) return nullptr;
3332 
3333   bool return_barrier = Continuation::is_thaw_return_barrier(kind);
3334   bool return_barrier_exception = Continuation::is_thaw_return_barrier_exception(kind);
3335 
3336   StubCodeMark mark(this, "StubRoutines", label);
3337   address start = __ pc();
3338 
3339   // TODO: Handle Valhalla return types. May require generating different return barriers.
3340 
3341   if (!return_barrier) {
3342     // Pop return address. If we don't do this, we get a drift,
3343     // where the bottom-most frozen frame continuously grows.
3344     __ pop(c_rarg3);
3345   } else {
3346     __ movptr(rsp, Address(r15_thread, JavaThread::cont_entry_offset()));
3347   }
3348 
3349 #ifdef ASSERT
3350   {
3351     Label L_good_sp;
3352     __ cmpptr(rsp, Address(r15_thread, JavaThread::cont_entry_offset()));
3353     __ jcc(Assembler::equal, L_good_sp);
3354     __ stop("Incorrect rsp at thaw entry");
3355     __ BIND(L_good_sp);
3356   }
3357 #endif // ASSERT
3358 
3359   if (return_barrier) {
3360     // Preserve possible return value from a method returning to the return barrier.
3361     __ push(rax);
3362     __ push_d(xmm0);
3363   }
3364 
3365   __ movptr(c_rarg0, r15_thread);
3366   __ movptr(c_rarg1, (return_barrier ? 1 : 0));
3367   __ call_VM_leaf(CAST_FROM_FN_PTR(address, Continuation::prepare_thaw), 2);
3368   __ movptr(rbx, rax);
3369 
3370   if (return_barrier) {
3371     // Restore return value from a method returning to the return barrier.
3372     // No safepoint in the call to thaw, so even an oop return value should be OK.
3373     __ pop_d(xmm0);
3374     __ pop(rax);
3375   }
3376 
3377 #ifdef ASSERT
3378   {
3379     Label L_good_sp;
3380     __ cmpptr(rsp, Address(r15_thread, JavaThread::cont_entry_offset()));
3381     __ jcc(Assembler::equal, L_good_sp);
3382     __ stop("Incorrect rsp after prepare thaw");
3383     __ BIND(L_good_sp);
3384   }
3385 #endif // ASSERT
3386 
3387   // rbx contains the size of the frames to thaw, 0 if overflow or no more frames
3388   Label L_thaw_success;
3389   __ testptr(rbx, rbx);
3390   __ jccb(Assembler::notZero, L_thaw_success);
3391   __ jump(ExternalAddress(StubRoutines::throw_StackOverflowError_entry()));
3392   __ bind(L_thaw_success);
3393 
3394   // Make room for the thawed frames and align the stack.
3395   __ subptr(rsp, rbx);
3396   __ andptr(rsp, -StackAlignmentInBytes);
3397 
3398   if (return_barrier) {
3399     // Preserve possible return value from a method returning to the return barrier. (Again.)
3400     __ push(rax);
3401     __ push_d(xmm0);
3402   }
3403 
3404   // If we want, we can templatize thaw by kind, and have three different entries.
3405   __ movptr(c_rarg0, r15_thread);
3406   __ movptr(c_rarg1, kind);
3407   __ call_VM_leaf(Continuation::thaw_entry(), 2);
3408   __ movptr(rbx, rax);
3409 
3410   if (return_barrier) {
3411     // Restore return value from a method returning to the return barrier. (Again.)
3412     // No safepoint in the call to thaw, so even an oop return value should be OK.
3413     __ pop_d(xmm0);
3414     __ pop(rax);
3415   } else {
3416     // Return 0 (success) from doYield.
3417     __ xorptr(rax, rax);
3418   }
3419 
3420   // After thawing, rbx is the SP of the yielding frame.
3421   // Move there, and then to saved RBP slot.
3422   __ movptr(rsp, rbx);
3423   __ subptr(rsp, 2*wordSize);
3424 
3425   if (return_barrier_exception) {
3426     __ movptr(c_rarg0, r15_thread);
3427     __ movptr(c_rarg1, Address(rsp, wordSize)); // return address
3428 
3429     // rax still holds the original exception oop, save it before the call
3430     __ push(rax);
3431 
3432     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), 2);
3433     __ movptr(rbx, rax);
3434 
3435     // Continue at exception handler:
3436     //   rax: exception oop
3437     //   rbx: exception handler
3438     //   rdx: exception pc
3439     __ pop(rax);
3440     __ verify_oop(rax);
3441     __ pop(rbp); // pop out RBP here too
3442     __ pop(rdx);
3443     __ jmp(rbx);
3444   } else {
3445     // We are "returning" into the topmost thawed frame; see Thaw::push_return_frame
3446     __ pop(rbp);
3447     __ ret(0);
3448   }
3449 
3450   return start;
3451 }
3452 
3453 address StubGenerator::generate_cont_thaw() {
3454   return generate_cont_thaw("Cont thaw", Continuation::thaw_top);
3455 }
3456 
3457 // TODO: will probably need multiple return barriers depending on return type
3458 
3459 address StubGenerator::generate_cont_returnBarrier() {
3460   return generate_cont_thaw("Cont thaw return barrier", Continuation::thaw_return_barrier);
3461 }
3462 
3463 address StubGenerator::generate_cont_returnBarrier_exception() {
3464   return generate_cont_thaw("Cont thaw return barrier exception", Continuation::thaw_return_barrier_exception);
3465 }
3466 
3467 #if INCLUDE_JFR
3468 
3469 // For c2: c_rarg0 is junk, call to runtime to write a checkpoint.
3470 // It returns a jobject handle to the event writer.
3471 // The handle is dereferenced and the return value is the event writer oop.
3472 RuntimeStub* StubGenerator::generate_jfr_write_checkpoint() {
3473   enum layout {
3474     rbp_off,
3475     rbpH_off,
3476     return_off,
3477     return_off2,
3478     framesize // inclusive of return address
3479   };
3480 
3481   CodeBuffer code("jfr_write_checkpoint", 512, 64);
3482   MacroAssembler* _masm = new MacroAssembler(&code);
3483   address start = __ pc();
3484 
3485   __ enter();
3486   address the_pc = __ pc();
3487 
3488   int frame_complete = the_pc - start;
3489 
3490   __ set_last_Java_frame(rsp, rbp, the_pc, rscratch1);
3491   __ movptr(c_rarg0, r15_thread);
3492   __ call_VM_leaf(CAST_FROM_FN_PTR(address, JfrIntrinsicSupport::write_checkpoint), 1);
3493   __ reset_last_Java_frame(true);
3494 
3495   // rax is jobject handle result, unpack and process it through a barrier.
3496   Label L_null_jobject;
3497   __ testptr(rax, rax);
3498   __ jcc(Assembler::zero, L_null_jobject);
3499 
3500   BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
3501   bs->load_at(_masm, ACCESS_READ | IN_NATIVE, T_OBJECT, rax, Address(rax, 0), c_rarg0, r15_thread);
3502 
3503   __ bind(L_null_jobject);
3504 
3505   __ leave();
3506   __ ret(0);
3507 
3508   OopMapSet* oop_maps = new OopMapSet();
3509   OopMap* map = new OopMap(framesize, 1);
3510   oop_maps->add_gc_map(frame_complete, map);
3511 
3512   RuntimeStub* stub =
3513     RuntimeStub::new_runtime_stub(code.name(),
3514                                   &code,
3515                                   frame_complete,
3516                                   (framesize >> (LogBytesPerWord - LogBytesPerInt)),
3517                                   oop_maps,
3518                                   false);
3519   return stub;
3520 }
3521 
3522 #endif // INCLUDE_JFR
3523 
3524 // Continuation point for throwing of implicit exceptions that are
3525 // not handled in the current activation. Fabricates an exception
3526 // oop and initiates normal exception dispatching in this
3527 // frame. Since we need to preserve callee-saved values (currently
3528 // only for C2, but done for C1 as well) we need a callee-saved oop
3529 // map and therefore have to make these stubs into RuntimeStubs
3530 // rather than BufferBlobs.  If the compiler needs all registers to
3531 // be preserved between the fault point and the exception handler
3532 // then it must assume responsibility for that in
3533 // AbstractCompiler::continuation_for_implicit_null_exception or
3534 // continuation_for_implicit_division_by_zero_exception. All other
3535 // implicit exceptions (e.g., NullPointerException or
3536 // AbstractMethodError on entry) are either at call sites or
3537 // otherwise assume that stack unwinding will be initiated, so
3538 // caller saved registers were assumed volatile in the compiler.
3539 address StubGenerator::generate_throw_exception(const char* name,
3540                                                 address runtime_entry,
3541                                                 Register arg1,
3542                                                 Register arg2) {
3543   // Information about frame layout at time of blocking runtime call.
3544   // Note that we only have to preserve callee-saved registers since
3545   // the compilers are responsible for supplying a continuation point
3546   // if they expect all registers to be preserved.
3547   enum layout {
3548     rbp_off = frame::arg_reg_save_area_bytes/BytesPerInt,
3549     rbp_off2,
3550     return_off,
3551     return_off2,
3552     framesize // inclusive of return address
3553   };
3554 
3555   int insts_size = 512;
3556   int locs_size  = 64;
3557 
3558   CodeBuffer code(name, insts_size, locs_size);
3559   OopMapSet* oop_maps  = new OopMapSet();
3560   MacroAssembler* _masm = new MacroAssembler(&code);
3561 
3562   address start = __ pc();
3563 
3564   // This is an inlined and slightly modified version of call_VM
3565   // which has the ability to fetch the return PC out of
3566   // thread-local storage and also sets up last_Java_sp slightly
3567   // differently than the real call_VM
3568 
3569   __ enter(); // required for proper stackwalking of RuntimeStub frame
3570 
3571   assert(is_even(framesize/2), "sp not 16-byte aligned");
3572 
3573   // return address and rbp are already in place
3574   __ subptr(rsp, (framesize-4) << LogBytesPerInt); // prolog
3575 
3576   int frame_complete = __ pc() - start;
3577 
3578   // Set up last_Java_sp and last_Java_fp
3579   address the_pc = __ pc();
3580   __ set_last_Java_frame(rsp, rbp, the_pc, rscratch1);
3581   __ andptr(rsp, -(StackAlignmentInBytes));    // Align stack
3582 
3583   // Call runtime
3584   if (arg1 != noreg) {
3585     assert(arg2 != c_rarg1, "clobbered");
3586     __ movptr(c_rarg1, arg1);
3587   }
3588   if (arg2 != noreg) {
3589     __ movptr(c_rarg2, arg2);
3590   }
3591   __ movptr(c_rarg0, r15_thread);
3592   BLOCK_COMMENT("call runtime_entry");
3593   __ call(RuntimeAddress(runtime_entry));
3594 
3595   // Generate oop map
3596   OopMap* map = new OopMap(framesize, 0);
3597 
3598   oop_maps->add_gc_map(the_pc - start, map);
3599 
3600   __ reset_last_Java_frame(true);
3601 
3602   __ leave(); // required for proper stackwalking of RuntimeStub frame
3603 
3604   // check for pending exceptions
3605 #ifdef ASSERT
3606   Label L;
3607   __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), NULL_WORD);
3608   __ jcc(Assembler::notEqual, L);
3609   __ should_not_reach_here();
3610   __ bind(L);
3611 #endif // ASSERT
3612   __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
3613 
3614 
3615   // codeBlob framesize is in words (not VMRegImpl::slot_size)
3616   RuntimeStub* stub =
3617     RuntimeStub::new_runtime_stub(name,
3618                                   &code,
3619                                   frame_complete,
3620                                   (framesize >> (LogBytesPerWord - LogBytesPerInt)),
3621                                   oop_maps, false);
3622   return stub->entry_point();
3623 }
3624 
3625 void StubGenerator::create_control_words() {
3626   // Round to nearest, 64-bit mode, exceptions masked
3627   StubRoutines::x86::_mxcsr_std = 0x1F80;
3628 }
3629 
3630 // Initialization
3631 void StubGenerator::generate_initial() {
3632   // Generates all stubs and initializes the entry points
3633 
3634   // This platform-specific settings are needed by generate_call_stub()
3635   create_control_words();
3636 
3637   // entry points that exist in all platforms Note: This is code
3638   // that could be shared among different platforms - however the
3639   // benefit seems to be smaller than the disadvantage of having a
3640   // much more complicated generator structure. See also comment in
3641   // stubRoutines.hpp.
3642 
3643   StubRoutines::_forward_exception_entry = generate_forward_exception();
3644 
3645   // Generate these first because they are called from other stubs
3646   if (InlineTypeReturnedAsFields) {
3647     StubRoutines::_load_inline_type_fields_in_regs =
3648       generate_return_value_stub(CAST_FROM_FN_PTR(address, SharedRuntime::load_inline_type_fields_in_regs),
3649                                  "load_inline_type_fields_in_regs", false);
3650     StubRoutines::_store_inline_type_fields_to_buf =
3651       generate_return_value_stub(CAST_FROM_FN_PTR(address, SharedRuntime::store_inline_type_fields_to_buf),
3652                                  "store_inline_type_fields_to_buf", true);
3653   }
3654 
3655   StubRoutines::_call_stub_entry =
3656     generate_call_stub(StubRoutines::_call_stub_return_address);
3657 
3658   // is referenced by megamorphic call
3659   StubRoutines::_catch_exception_entry = generate_catch_exception();
3660 
3661   // atomic calls
3662   StubRoutines::_fence_entry                = generate_orderaccess_fence();
3663 
3664   // platform dependent
3665   StubRoutines::x86::_get_previous_sp_entry = generate_get_previous_sp();
3666 
3667   StubRoutines::x86::_verify_mxcsr_entry    = generate_verify_mxcsr();
3668 
3669   StubRoutines::x86::_f2i_fixup             = generate_f2i_fixup();
3670   StubRoutines::x86::_f2l_fixup             = generate_f2l_fixup();
3671   StubRoutines::x86::_d2i_fixup             = generate_d2i_fixup();
3672   StubRoutines::x86::_d2l_fixup             = generate_d2l_fixup();
3673 
3674   StubRoutines::x86::_float_sign_mask       = generate_fp_mask("float_sign_mask",  0x7FFFFFFF7FFFFFFF);
3675   StubRoutines::x86::_float_sign_flip       = generate_fp_mask("float_sign_flip",  0x8000000080000000);
3676   StubRoutines::x86::_double_sign_mask      = generate_fp_mask("double_sign_mask", 0x7FFFFFFFFFFFFFFF);
3677   StubRoutines::x86::_double_sign_flip      = generate_fp_mask("double_sign_flip", 0x8000000000000000);
3678 
3679   // Build this early so it's available for the interpreter.
3680   StubRoutines::_throw_StackOverflowError_entry =
3681     generate_throw_exception("StackOverflowError throw_exception",
3682                              CAST_FROM_FN_PTR(address,
3683                                               SharedRuntime::
3684                                               throw_StackOverflowError));
3685   StubRoutines::_throw_delayed_StackOverflowError_entry =
3686     generate_throw_exception("delayed StackOverflowError throw_exception",
3687                              CAST_FROM_FN_PTR(address,
3688                                               SharedRuntime::
3689                                               throw_delayed_StackOverflowError));
3690   if (UseCRC32Intrinsics) {
3691     // set table address before stub generation which use it
3692     StubRoutines::_crc_table_adr = (address)StubRoutines::x86::_crc_table;
3693     StubRoutines::_updateBytesCRC32 = generate_updateBytesCRC32();
3694   }
3695 
3696   if (UseCRC32CIntrinsics) {
3697     bool supports_clmul = VM_Version::supports_clmul();
3698     StubRoutines::x86::generate_CRC32C_table(supports_clmul);
3699     StubRoutines::_crc32c_table_addr = (address)StubRoutines::x86::_crc32c_table;
3700     StubRoutines::_updateBytesCRC32C = generate_updateBytesCRC32C(supports_clmul);
3701   }
3702 
3703   if (UseAdler32Intrinsics) {
3704      StubRoutines::_updateBytesAdler32 = generate_updateBytesAdler32();
3705   }
3706 }
3707 
3708 // Call here from the interpreter or compiled code to either load
3709 // multiple returned values from the inline type instance being
3710 // returned to registers or to store returned values to a newly
3711 // allocated inline type instance.
3712 // Register is a class, but it would be assigned numerical value.
3713 // "0" is assigned for xmm0. Thus we need to ignore -Wnonnull.
3714 PRAGMA_DIAG_PUSH
3715 PRAGMA_NONNULL_IGNORED
3716 address StubGenerator::generate_return_value_stub(address destination, const char* name, bool has_res) {
3717   // We need to save all registers the calling convention may use so
3718   // the runtime calls read or update those registers. This needs to
3719   // be in sync with SharedRuntime::java_return_convention().
3720   enum layout {
3721     pad_off = frame::arg_reg_save_area_bytes/BytesPerInt, pad_off_2,
3722     rax_off, rax_off_2,
3723     j_rarg5_off, j_rarg5_2,
3724     j_rarg4_off, j_rarg4_2,
3725     j_rarg3_off, j_rarg3_2,
3726     j_rarg2_off, j_rarg2_2,
3727     j_rarg1_off, j_rarg1_2,
3728     j_rarg0_off, j_rarg0_2,
3729     j_farg0_off, j_farg0_2,
3730     j_farg1_off, j_farg1_2,
3731     j_farg2_off, j_farg2_2,
3732     j_farg3_off, j_farg3_2,
3733     j_farg4_off, j_farg4_2,
3734     j_farg5_off, j_farg5_2,
3735     j_farg6_off, j_farg6_2,
3736     j_farg7_off, j_farg7_2,
3737     rbp_off, rbp_off_2,
3738     return_off, return_off_2,
3739 
3740     framesize
3741   };
3742 
3743   CodeBuffer buffer(name, 1000, 512);
3744   MacroAssembler* _masm = new MacroAssembler(&buffer);
3745 
3746   int frame_size_in_bytes = align_up(framesize*BytesPerInt, 16);
3747   assert(frame_size_in_bytes == framesize*BytesPerInt, "misaligned");
3748   int frame_size_in_slots = frame_size_in_bytes / BytesPerInt;
3749   int frame_size_in_words = frame_size_in_bytes / wordSize;
3750 
3751   OopMapSet *oop_maps = new OopMapSet();
3752   OopMap* map = new OopMap(frame_size_in_slots, 0);
3753 
3754   map->set_callee_saved(VMRegImpl::stack2reg(rax_off), rax->as_VMReg());
3755   map->set_callee_saved(VMRegImpl::stack2reg(j_rarg5_off), j_rarg5->as_VMReg());
3756   map->set_callee_saved(VMRegImpl::stack2reg(j_rarg4_off), j_rarg4->as_VMReg());
3757   map->set_callee_saved(VMRegImpl::stack2reg(j_rarg3_off), j_rarg3->as_VMReg());
3758   map->set_callee_saved(VMRegImpl::stack2reg(j_rarg2_off), j_rarg2->as_VMReg());
3759   map->set_callee_saved(VMRegImpl::stack2reg(j_rarg1_off), j_rarg1->as_VMReg());
3760   map->set_callee_saved(VMRegImpl::stack2reg(j_rarg0_off), j_rarg0->as_VMReg());
3761   map->set_callee_saved(VMRegImpl::stack2reg(j_farg0_off), j_farg0->as_VMReg());
3762   map->set_callee_saved(VMRegImpl::stack2reg(j_farg1_off), j_farg1->as_VMReg());
3763   map->set_callee_saved(VMRegImpl::stack2reg(j_farg2_off), j_farg2->as_VMReg());
3764   map->set_callee_saved(VMRegImpl::stack2reg(j_farg3_off), j_farg3->as_VMReg());
3765   map->set_callee_saved(VMRegImpl::stack2reg(j_farg4_off), j_farg4->as_VMReg());
3766   map->set_callee_saved(VMRegImpl::stack2reg(j_farg5_off), j_farg5->as_VMReg());
3767   map->set_callee_saved(VMRegImpl::stack2reg(j_farg6_off), j_farg6->as_VMReg());
3768   map->set_callee_saved(VMRegImpl::stack2reg(j_farg7_off), j_farg7->as_VMReg());
3769 
3770   int start = __ offset();
3771 
3772   __ subptr(rsp, frame_size_in_bytes - 8 /* return address*/);
3773 
3774   __ movptr(Address(rsp, rbp_off * BytesPerInt), rbp);
3775   __ movdbl(Address(rsp, j_farg7_off * BytesPerInt), j_farg7);
3776   __ movdbl(Address(rsp, j_farg6_off * BytesPerInt), j_farg6);
3777   __ movdbl(Address(rsp, j_farg5_off * BytesPerInt), j_farg5);
3778   __ movdbl(Address(rsp, j_farg4_off * BytesPerInt), j_farg4);
3779   __ movdbl(Address(rsp, j_farg3_off * BytesPerInt), j_farg3);
3780   __ movdbl(Address(rsp, j_farg2_off * BytesPerInt), j_farg2);
3781   __ movdbl(Address(rsp, j_farg1_off * BytesPerInt), j_farg1);
3782   __ movdbl(Address(rsp, j_farg0_off * BytesPerInt), j_farg0);
3783 
3784   __ movptr(Address(rsp, j_rarg0_off * BytesPerInt), j_rarg0);
3785   __ movptr(Address(rsp, j_rarg1_off * BytesPerInt), j_rarg1);
3786   __ movptr(Address(rsp, j_rarg2_off * BytesPerInt), j_rarg2);
3787   __ movptr(Address(rsp, j_rarg3_off * BytesPerInt), j_rarg3);
3788   __ movptr(Address(rsp, j_rarg4_off * BytesPerInt), j_rarg4);
3789   __ movptr(Address(rsp, j_rarg5_off * BytesPerInt), j_rarg5);
3790   __ movptr(Address(rsp, rax_off * BytesPerInt), rax);
3791 
3792   int frame_complete = __ offset();
3793 
3794   __ set_last_Java_frame(noreg, noreg, NULL, rscratch1);
3795 
3796   __ mov(c_rarg0, r15_thread);
3797   __ mov(c_rarg1, rax);
3798 
3799   __ call(RuntimeAddress(destination));
3800 
3801   // Set an oopmap for the call site.
3802 
3803   oop_maps->add_gc_map( __ offset() - start, map);
3804 
3805   // clear last_Java_sp
3806   __ reset_last_Java_frame(false);
3807 
3808   __ movptr(rbp, Address(rsp, rbp_off * BytesPerInt));
3809   __ movdbl(j_farg7, Address(rsp, j_farg7_off * BytesPerInt));
3810   __ movdbl(j_farg6, Address(rsp, j_farg6_off * BytesPerInt));
3811   __ movdbl(j_farg5, Address(rsp, j_farg5_off * BytesPerInt));
3812   __ movdbl(j_farg4, Address(rsp, j_farg4_off * BytesPerInt));
3813   __ movdbl(j_farg3, Address(rsp, j_farg3_off * BytesPerInt));
3814   __ movdbl(j_farg2, Address(rsp, j_farg2_off * BytesPerInt));
3815   __ movdbl(j_farg1, Address(rsp, j_farg1_off * BytesPerInt));
3816   __ movdbl(j_farg0, Address(rsp, j_farg0_off * BytesPerInt));
3817 
3818   __ movptr(j_rarg0, Address(rsp, j_rarg0_off * BytesPerInt));
3819   __ movptr(j_rarg1, Address(rsp, j_rarg1_off * BytesPerInt));
3820   __ movptr(j_rarg2, Address(rsp, j_rarg2_off * BytesPerInt));
3821   __ movptr(j_rarg3, Address(rsp, j_rarg3_off * BytesPerInt));
3822   __ movptr(j_rarg4, Address(rsp, j_rarg4_off * BytesPerInt));
3823   __ movptr(j_rarg5, Address(rsp, j_rarg5_off * BytesPerInt));
3824   __ movptr(rax, Address(rsp, rax_off * BytesPerInt));
3825 
3826   __ addptr(rsp, frame_size_in_bytes-8);
3827 
3828   // check for pending exceptions
3829   Label pending;
3830   __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
3831   __ jcc(Assembler::notEqual, pending);
3832 
3833   if (has_res) {
3834     __ get_vm_result(rax, r15_thread);
3835   }
3836 
3837   __ ret(0);
3838 
3839   __ bind(pending);
3840 
3841   __ movptr(rax, Address(r15_thread, Thread::pending_exception_offset()));
3842   __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
3843 
3844   // -------------
3845   // make sure all code is generated
3846   _masm->flush();
3847 
3848   RuntimeStub* stub = RuntimeStub::new_runtime_stub(name, &buffer, frame_complete, frame_size_in_words, oop_maps, false);
3849   return stub->entry_point();
3850 }
3851 
3852 void StubGenerator::generate_phase1() {
3853   // Continuation stubs:
3854   StubRoutines::_cont_thaw          = generate_cont_thaw();
3855   StubRoutines::_cont_returnBarrier = generate_cont_returnBarrier();
3856   StubRoutines::_cont_returnBarrierExc = generate_cont_returnBarrier_exception();
3857 
3858   JFR_ONLY(StubRoutines::_jfr_write_checkpoint_stub = generate_jfr_write_checkpoint();)
3859   JFR_ONLY(StubRoutines::_jfr_write_checkpoint = StubRoutines::_jfr_write_checkpoint_stub->entry_point();)
3860 }
3861 
3862 void StubGenerator::generate_all() {
3863   // Generates all stubs and initializes the entry points
3864 
3865   // These entry points require SharedInfo::stack0 to be set up in
3866   // non-core builds and need to be relocatable, so they each
3867   // fabricate a RuntimeStub internally.
3868   StubRoutines::_throw_AbstractMethodError_entry =
3869     generate_throw_exception("AbstractMethodError throw_exception",
3870                              CAST_FROM_FN_PTR(address,
3871                                               SharedRuntime::
3872                                               throw_AbstractMethodError));
3873 
3874   StubRoutines::_throw_IncompatibleClassChangeError_entry =
3875     generate_throw_exception("IncompatibleClassChangeError throw_exception",
3876                              CAST_FROM_FN_PTR(address,
3877                                               SharedRuntime::
3878                                               throw_IncompatibleClassChangeError));
3879 
3880   StubRoutines::_throw_NullPointerException_at_call_entry =
3881     generate_throw_exception("NullPointerException at call throw_exception",
3882                              CAST_FROM_FN_PTR(address,
3883                                               SharedRuntime::
3884                                               throw_NullPointerException_at_call));
3885 
3886   // entry points that are platform specific
3887   StubRoutines::x86::_vector_float_sign_mask = generate_vector_mask("vector_float_sign_mask", 0x7FFFFFFF7FFFFFFF);
3888   StubRoutines::x86::_vector_float_sign_flip = generate_vector_mask("vector_float_sign_flip", 0x8000000080000000);
3889   StubRoutines::x86::_vector_double_sign_mask = generate_vector_mask("vector_double_sign_mask", 0x7FFFFFFFFFFFFFFF);
3890   StubRoutines::x86::_vector_double_sign_flip = generate_vector_mask("vector_double_sign_flip", 0x8000000000000000);
3891   StubRoutines::x86::_vector_all_bits_set = generate_vector_mask("vector_all_bits_set", 0xFFFFFFFFFFFFFFFF);
3892   StubRoutines::x86::_vector_int_mask_cmp_bits = generate_vector_mask("vector_int_mask_cmp_bits", 0x0000000100000001);
3893   StubRoutines::x86::_vector_short_to_byte_mask = generate_vector_mask("vector_short_to_byte_mask", 0x00ff00ff00ff00ff);
3894   StubRoutines::x86::_vector_byte_perm_mask = generate_vector_byte_perm_mask("vector_byte_perm_mask");
3895   StubRoutines::x86::_vector_int_to_byte_mask = generate_vector_mask("vector_int_to_byte_mask", 0x000000ff000000ff);
3896   StubRoutines::x86::_vector_int_to_short_mask = generate_vector_mask("vector_int_to_short_mask", 0x0000ffff0000ffff);
3897   StubRoutines::x86::_vector_32_bit_mask = generate_vector_custom_i32("vector_32_bit_mask", Assembler::AVX_512bit,
3898                                                                       0xFFFFFFFF, 0, 0, 0);
3899   StubRoutines::x86::_vector_64_bit_mask = generate_vector_custom_i32("vector_64_bit_mask", Assembler::AVX_512bit,
3900                                                                       0xFFFFFFFF, 0xFFFFFFFF, 0, 0);
3901   StubRoutines::x86::_vector_int_shuffle_mask = generate_vector_mask("vector_int_shuffle_mask", 0x0302010003020100);
3902   StubRoutines::x86::_vector_byte_shuffle_mask = generate_vector_byte_shuffle_mask("vector_byte_shuffle_mask");
3903   StubRoutines::x86::_vector_short_shuffle_mask = generate_vector_mask("vector_short_shuffle_mask", 0x0100010001000100);
3904   StubRoutines::x86::_vector_long_shuffle_mask = generate_vector_mask("vector_long_shuffle_mask", 0x0000000100000000);
3905   StubRoutines::x86::_vector_long_sign_mask = generate_vector_mask("vector_long_sign_mask", 0x8000000000000000);
3906   StubRoutines::x86::_vector_iota_indices = generate_iota_indices("iota_indices");
3907   StubRoutines::x86::_vector_count_leading_zeros_lut = generate_count_leading_zeros_lut("count_leading_zeros_lut");
3908   StubRoutines::x86::_vector_reverse_bit_lut = generate_vector_reverse_bit_lut("reverse_bit_lut");
3909   StubRoutines::x86::_vector_reverse_byte_perm_mask_long = generate_vector_reverse_byte_perm_mask_long("perm_mask_long");
3910   StubRoutines::x86::_vector_reverse_byte_perm_mask_int = generate_vector_reverse_byte_perm_mask_int("perm_mask_int");
3911   StubRoutines::x86::_vector_reverse_byte_perm_mask_short = generate_vector_reverse_byte_perm_mask_short("perm_mask_short");
3912 
3913   if (VM_Version::supports_avx2() && !VM_Version::supports_avx512_vpopcntdq()) {
3914     // lut implementation influenced by counting 1s algorithm from section 5-1 of Hackers' Delight.
3915     StubRoutines::x86::_vector_popcount_lut = generate_popcount_avx_lut("popcount_lut");
3916   }
3917 
3918   // support for verify_oop (must happen after universe_init)
3919   if (VerifyOops) {
3920     StubRoutines::_verify_oop_subroutine_entry = generate_verify_oop();
3921   }
3922 
3923   // data cache line writeback
3924   StubRoutines::_data_cache_writeback = generate_data_cache_writeback();
3925   StubRoutines::_data_cache_writeback_sync = generate_data_cache_writeback_sync();
3926 
3927   // arraycopy stubs used by compilers
3928   generate_arraycopy_stubs();
3929 
3930   generate_aes_stubs();
3931 
3932   generate_ghash_stubs();
3933 
3934   if (UseMD5Intrinsics) {
3935     StubRoutines::_md5_implCompress = generate_md5_implCompress(false, "md5_implCompress");
3936     StubRoutines::_md5_implCompressMB = generate_md5_implCompress(true, "md5_implCompressMB");
3937   }
3938   if (UseSHA1Intrinsics) {
3939     StubRoutines::x86::_upper_word_mask_addr = generate_upper_word_mask();
3940     StubRoutines::x86::_shuffle_byte_flip_mask_addr = generate_shuffle_byte_flip_mask();
3941     StubRoutines::_sha1_implCompress = generate_sha1_implCompress(false, "sha1_implCompress");
3942     StubRoutines::_sha1_implCompressMB = generate_sha1_implCompress(true, "sha1_implCompressMB");
3943   }
3944   if (UseSHA256Intrinsics) {
3945     StubRoutines::x86::_k256_adr = (address)StubRoutines::x86::_k256;
3946     char* dst = (char*)StubRoutines::x86::_k256_W;
3947     char* src = (char*)StubRoutines::x86::_k256;
3948     for (int ii = 0; ii < 16; ++ii) {
3949       memcpy(dst + 32 * ii,      src + 16 * ii, 16);
3950       memcpy(dst + 32 * ii + 16, src + 16 * ii, 16);
3951     }
3952     StubRoutines::x86::_k256_W_adr = (address)StubRoutines::x86::_k256_W;
3953     StubRoutines::x86::_pshuffle_byte_flip_mask_addr = generate_pshuffle_byte_flip_mask();
3954     StubRoutines::_sha256_implCompress = generate_sha256_implCompress(false, "sha256_implCompress");
3955     StubRoutines::_sha256_implCompressMB = generate_sha256_implCompress(true, "sha256_implCompressMB");
3956   }
3957   if (UseSHA512Intrinsics) {
3958     StubRoutines::x86::_k512_W_addr = (address)StubRoutines::x86::_k512_W;
3959     StubRoutines::x86::_pshuffle_byte_flip_mask_addr_sha512 = generate_pshuffle_byte_flip_mask_sha512();
3960     StubRoutines::_sha512_implCompress = generate_sha512_implCompress(false, "sha512_implCompress");
3961     StubRoutines::_sha512_implCompressMB = generate_sha512_implCompress(true, "sha512_implCompressMB");
3962   }
3963 
3964   if (UseBASE64Intrinsics) {
3965     if(VM_Version::supports_avx2() &&
3966        VM_Version::supports_avx512bw() &&
3967        VM_Version::supports_avx512vl()) {
3968       StubRoutines::x86::_avx2_shuffle_base64 = base64_avx2_shuffle_addr();
3969       StubRoutines::x86::_avx2_input_mask_base64 = base64_avx2_input_mask_addr();
3970       StubRoutines::x86::_avx2_lut_base64 = base64_avx2_lut_addr();
3971     }
3972     StubRoutines::x86::_encoding_table_base64 = base64_encoding_table_addr();
3973     if (VM_Version::supports_avx512_vbmi()) {
3974       StubRoutines::x86::_shuffle_base64 = base64_shuffle_addr();
3975       StubRoutines::x86::_lookup_lo_base64 = base64_vbmi_lookup_lo_addr();
3976       StubRoutines::x86::_lookup_hi_base64 = base64_vbmi_lookup_hi_addr();
3977       StubRoutines::x86::_lookup_lo_base64url = base64_vbmi_lookup_lo_url_addr();
3978       StubRoutines::x86::_lookup_hi_base64url = base64_vbmi_lookup_hi_url_addr();
3979       StubRoutines::x86::_pack_vec_base64 = base64_vbmi_pack_vec_addr();
3980       StubRoutines::x86::_join_0_1_base64 = base64_vbmi_join_0_1_addr();
3981       StubRoutines::x86::_join_1_2_base64 = base64_vbmi_join_1_2_addr();
3982       StubRoutines::x86::_join_2_3_base64 = base64_vbmi_join_2_3_addr();
3983     }
3984     StubRoutines::x86::_decoding_table_base64 = base64_decoding_table_addr();
3985     StubRoutines::_base64_encodeBlock = generate_base64_encodeBlock();
3986     StubRoutines::_base64_decodeBlock = generate_base64_decodeBlock();
3987   }
3988 
3989   BarrierSetNMethod* bs_nm = BarrierSet::barrier_set()->barrier_set_nmethod();
3990   if (bs_nm != NULL) {
3991     StubRoutines::x86::_method_entry_barrier = generate_method_entry_barrier();
3992   }
3993 #ifdef COMPILER2
3994   if (UseMultiplyToLenIntrinsic) {
3995     StubRoutines::_multiplyToLen = generate_multiplyToLen();
3996   }
3997   if (UseSquareToLenIntrinsic) {
3998     StubRoutines::_squareToLen = generate_squareToLen();
3999   }
4000   if (UseMulAddIntrinsic) {
4001     StubRoutines::_mulAdd = generate_mulAdd();
4002   }
4003   if (VM_Version::supports_avx512_vbmi2()) {
4004     StubRoutines::_bigIntegerRightShiftWorker = generate_bigIntegerRightShift();
4005     StubRoutines::_bigIntegerLeftShiftWorker = generate_bigIntegerLeftShift();
4006   }
4007   if (UseMontgomeryMultiplyIntrinsic) {
4008     StubRoutines::_montgomeryMultiply
4009       = CAST_FROM_FN_PTR(address, SharedRuntime::montgomery_multiply);
4010   }
4011   if (UseMontgomerySquareIntrinsic) {
4012     StubRoutines::_montgomerySquare
4013       = CAST_FROM_FN_PTR(address, SharedRuntime::montgomery_square);
4014   }
4015 
4016   // Get svml stub routine addresses
4017   void *libjsvml = NULL;
4018   char ebuf[1024];
4019   char dll_name[JVM_MAXPATHLEN];
4020   if (os::dll_locate_lib(dll_name, sizeof(dll_name), Arguments::get_dll_dir(), "jsvml")) {
4021     libjsvml = os::dll_load(dll_name, ebuf, sizeof ebuf);
4022   }
4023   if (libjsvml != NULL) {
4024     // SVML method naming convention
4025     //   All the methods are named as __jsvml_op<T><N>_ha_<VV>
4026     //   Where:
4027     //      ha stands for high accuracy
4028     //      <T> is optional to indicate float/double
4029     //              Set to f for vector float operation
4030     //              Omitted for vector double operation
4031     //      <N> is the number of elements in the vector
4032     //              1, 2, 4, 8, 16
4033     //              e.g. 128 bit float vector has 4 float elements
4034     //      <VV> indicates the avx/sse level:
4035     //              z0 is AVX512, l9 is AVX2, e9 is AVX1 and ex is for SSE2
4036     //      e.g. __jsvml_expf16_ha_z0 is the method for computing 16 element vector float exp using AVX 512 insns
4037     //           __jsvml_exp8_ha_z0 is the method for computing 8 element vector double exp using AVX 512 insns
4038 
4039     log_info(library)("Loaded library %s, handle " INTPTR_FORMAT, JNI_LIB_PREFIX "jsvml" JNI_LIB_SUFFIX, p2i(libjsvml));
4040     if (UseAVX > 2) {
4041       for (int op = 0; op < VectorSupport::NUM_SVML_OP; op++) {
4042         int vop = VectorSupport::VECTOR_OP_SVML_START + op;
4043         if ((!VM_Version::supports_avx512dq()) &&
4044             (vop == VectorSupport::VECTOR_OP_LOG || vop == VectorSupport::VECTOR_OP_LOG10 || vop == VectorSupport::VECTOR_OP_POW)) {
4045           continue;
4046         }
4047         snprintf(ebuf, sizeof(ebuf), "__jsvml_%sf16_ha_z0", VectorSupport::svmlname[op]);
4048         StubRoutines::_vector_f_math[VectorSupport::VEC_SIZE_512][op] = (address)os::dll_lookup(libjsvml, ebuf);
4049 
4050         snprintf(ebuf, sizeof(ebuf), "__jsvml_%s8_ha_z0", VectorSupport::svmlname[op]);
4051         StubRoutines::_vector_d_math[VectorSupport::VEC_SIZE_512][op] = (address)os::dll_lookup(libjsvml, ebuf);
4052       }
4053     }
4054     const char* avx_sse_str = (UseAVX >= 2) ? "l9" : ((UseAVX == 1) ? "e9" : "ex");
4055     for (int op = 0; op < VectorSupport::NUM_SVML_OP; op++) {
4056       int vop = VectorSupport::VECTOR_OP_SVML_START + op;
4057       if (vop == VectorSupport::VECTOR_OP_POW) {
4058         continue;
4059       }
4060       snprintf(ebuf, sizeof(ebuf), "__jsvml_%sf4_ha_%s", VectorSupport::svmlname[op], avx_sse_str);
4061       StubRoutines::_vector_f_math[VectorSupport::VEC_SIZE_64][op] = (address)os::dll_lookup(libjsvml, ebuf);
4062 
4063       snprintf(ebuf, sizeof(ebuf), "__jsvml_%sf4_ha_%s", VectorSupport::svmlname[op], avx_sse_str);
4064       StubRoutines::_vector_f_math[VectorSupport::VEC_SIZE_128][op] = (address)os::dll_lookup(libjsvml, ebuf);
4065 
4066       snprintf(ebuf, sizeof(ebuf), "__jsvml_%sf8_ha_%s", VectorSupport::svmlname[op], avx_sse_str);
4067       StubRoutines::_vector_f_math[VectorSupport::VEC_SIZE_256][op] = (address)os::dll_lookup(libjsvml, ebuf);
4068 
4069       snprintf(ebuf, sizeof(ebuf), "__jsvml_%s1_ha_%s", VectorSupport::svmlname[op], avx_sse_str);
4070       StubRoutines::_vector_d_math[VectorSupport::VEC_SIZE_64][op] = (address)os::dll_lookup(libjsvml, ebuf);
4071 
4072       snprintf(ebuf, sizeof(ebuf), "__jsvml_%s2_ha_%s", VectorSupport::svmlname[op], avx_sse_str);
4073       StubRoutines::_vector_d_math[VectorSupport::VEC_SIZE_128][op] = (address)os::dll_lookup(libjsvml, ebuf);
4074 
4075       snprintf(ebuf, sizeof(ebuf), "__jsvml_%s4_ha_%s", VectorSupport::svmlname[op], avx_sse_str);
4076       StubRoutines::_vector_d_math[VectorSupport::VEC_SIZE_256][op] = (address)os::dll_lookup(libjsvml, ebuf);
4077     }
4078   }
4079 #endif // COMPILER2
4080 
4081   if (UseVectorizedMismatchIntrinsic) {
4082     StubRoutines::_vectorizedMismatch = generate_vectorizedMismatch();
4083   }
4084 }
4085 
4086 void StubGenerator_generate(CodeBuffer* code, int phase) {
4087   if (UnsafeCopyMemory::_table == NULL) {
4088     UnsafeCopyMemory::create_table(16);
4089   }
4090   StubGenerator g(code, phase);
4091 }
4092 
4093 #undef __