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