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