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