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