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