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