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