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