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