1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/macroAssembler.hpp"
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "interpreter/interpreter.hpp"
  29 #include "nativeInst_x86.hpp"
  30 #include "oops/instanceOop.hpp"
  31 #include "oops/method.hpp"
  32 #include "oops/objArrayKlass.hpp"
  33 #include "oops/oop.inline.hpp"
  34 #include "prims/methodHandles.hpp"
  35 #include "runtime/frame.inline.hpp"
  36 #include "runtime/handles.inline.hpp"
  37 #include "runtime/sharedRuntime.hpp"
  38 #include "runtime/stubCodeGenerator.hpp"
  39 #include "runtime/stubRoutines.hpp"
  40 #include "runtime/thread.inline.hpp"
  41 #include "utilities/top.hpp"
  42 #ifdef COMPILER2
  43 #include "opto/runtime.hpp"
  44 #endif
  45 
  46 // Declaration and definition of StubGenerator (no .hpp file).
  47 // For a more detailed description of the stub routine structure
  48 // see the comment in stubRoutines.hpp
  49 
  50 #define __ _masm->
  51 #define TIMES_OOP (UseCompressedOops ? Address::times_4 : Address::times_8)
  52 #define a__ ((Assembler*)_masm)->
  53 
  54 #ifdef PRODUCT
  55 #define BLOCK_COMMENT(str) /* nothing */
  56 #else
  57 #define BLOCK_COMMENT(str) __ block_comment(str)
  58 #endif
  59 
  60 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
  61 const int MXCSR_MASK = 0xFFC0;  // Mask out any pending exceptions
  62 
  63 // Stub Code definitions
  64 
  65 static address handle_unsafe_access() {
  66   JavaThread* thread = JavaThread::current();
  67   address pc = thread->saved_exception_pc();
  68   // pc is the instruction which we must emulate
  69   // doing a no-op is fine:  return garbage from the load
  70   // therefore, compute npc
  71   address npc = Assembler::locate_next_instruction(pc);
  72 
  73   // request an async exception
  74   thread->set_pending_unsafe_access_error();
  75 
  76   // return address of next instruction to execute
  77   return npc;
  78 }
  79 
  80 class StubGenerator: public StubCodeGenerator {
  81  private:
  82 
  83 #ifdef PRODUCT
  84 #define inc_counter_np(counter) ((void)0)
  85 #else
  86   void inc_counter_np_(int& counter) {
  87     // This can destroy rscratch1 if counter is far from the code cache
  88     __ incrementl(ExternalAddress((address)&counter));
  89   }
  90 #define inc_counter_np(counter) \
  91   BLOCK_COMMENT("inc_counter " #counter); \
  92   inc_counter_np_(counter);
  93 #endif
  94 
  95   // Call stubs are used to call Java from C
  96   //
  97   // Linux Arguments:
  98   //    c_rarg0:   call wrapper address                   address
  99   //    c_rarg1:   result                                 address
 100   //    c_rarg2:   result type                            BasicType
 101   //    c_rarg3:   method                                 Method*
 102   //    c_rarg4:   (interpreter) entry point              address
 103   //    c_rarg5:   parameters                             intptr_t*
 104   //    16(rbp): parameter size (in words)              int
 105   //    24(rbp): thread                                 Thread*
 106   //
 107   //     [ return_from_Java     ] <--- rsp
 108   //     [ argument word n      ]
 109   //      ...
 110   // -12 [ argument word 1      ]
 111   // -11 [ saved r15            ] <--- rsp_after_call
 112   // -10 [ saved r14            ]
 113   //  -9 [ saved r13            ]
 114   //  -8 [ saved r12            ]
 115   //  -7 [ saved rbx            ]
 116   //  -6 [ call wrapper         ]
 117   //  -5 [ result               ]
 118   //  -4 [ result type          ]
 119   //  -3 [ method               ]
 120   //  -2 [ entry point          ]
 121   //  -1 [ parameters           ]
 122   //   0 [ saved rbp            ] <--- rbp
 123   //   1 [ return address       ]
 124   //   2 [ parameter size       ]
 125   //   3 [ thread               ]
 126   //
 127   // Windows Arguments:
 128   //    c_rarg0:   call wrapper address                   address
 129   //    c_rarg1:   result                                 address
 130   //    c_rarg2:   result type                            BasicType
 131   //    c_rarg3:   method                                 Method*
 132   //    48(rbp): (interpreter) entry point              address
 133   //    56(rbp): parameters                             intptr_t*
 134   //    64(rbp): parameter size (in words)              int
 135   //    72(rbp): thread                                 Thread*
 136   //
 137   //     [ return_from_Java     ] <--- rsp
 138   //     [ argument word n      ]
 139   //      ...
 140   // -60 [ argument word 1      ]
 141   // -59 [ saved xmm31          ] <--- rsp after_call
 142   //     [ saved xmm16-xmm30    ] (EVEX enabled, else the space is blank)
 143   // -27 [ saved xmm15          ]
 144   //     [ saved xmm7-xmm14     ]
 145   //  -9 [ saved xmm6           ] (each xmm register takes 2 slots)
 146   //  -7 [ saved r15            ]
 147   //  -6 [ saved r14            ]
 148   //  -5 [ saved r13            ]
 149   //  -4 [ saved r12            ]
 150   //  -3 [ saved rdi            ]
 151   //  -2 [ saved rsi            ]
 152   //  -1 [ saved rbx            ]
 153   //   0 [ saved rbp            ] <--- rbp
 154   //   1 [ return address       ]
 155   //   2 [ call wrapper         ]
 156   //   3 [ result               ]
 157   //   4 [ result type          ]
 158   //   5 [ method               ]
 159   //   6 [ entry point          ]
 160   //   7 [ parameters           ]
 161   //   8 [ parameter size       ]
 162   //   9 [ thread               ]
 163   //
 164   //    Windows reserves the callers stack space for arguments 1-4.
 165   //    We spill c_rarg0-c_rarg3 to this space.
 166 
 167   // Call stub stack layout word offsets from rbp
 168   enum call_stub_layout {
 169 #ifdef _WIN64
 170     xmm_save_first     = 6,  // save from xmm6
 171     xmm_save_last      = 31, // to xmm31
 172     xmm_save_base      = -9,
 173     rsp_after_call_off = xmm_save_base - 2 * (xmm_save_last - xmm_save_first), // -27
 174     r15_off            = -7,
 175     r14_off            = -6,
 176     r13_off            = -5,
 177     r12_off            = -4,
 178     rdi_off            = -3,
 179     rsi_off            = -2,
 180     rbx_off            = -1,
 181     rbp_off            =  0,
 182     retaddr_off        =  1,
 183     call_wrapper_off   =  2,
 184     result_off         =  3,
 185     result_type_off    =  4,
 186     method_off         =  5,
 187     entry_point_off    =  6,
 188     parameters_off     =  7,
 189     parameter_size_off =  8,
 190     thread_off         =  9
 191 #else
 192     rsp_after_call_off = -12,
 193     mxcsr_off          = rsp_after_call_off,
 194     r15_off            = -11,
 195     r14_off            = -10,
 196     r13_off            = -9,
 197     r12_off            = -8,
 198     rbx_off            = -7,
 199     call_wrapper_off   = -6,
 200     result_off         = -5,
 201     result_type_off    = -4,
 202     method_off         = -3,
 203     entry_point_off    = -2,
 204     parameters_off     = -1,
 205     rbp_off            =  0,
 206     retaddr_off        =  1,
 207     parameter_size_off =  2,
 208     thread_off         =  3
 209 #endif
 210   };
 211 
 212 #ifdef _WIN64
 213   Address xmm_save(int reg) {
 214     assert(reg >= xmm_save_first && reg <= xmm_save_last, "XMM register number out of range");
 215     return Address(rbp, (xmm_save_base - (reg - xmm_save_first) * 2) * wordSize);
 216   }
 217 #endif
 218 
 219   address generate_call_stub(address& return_address) {
 220     assert((int)frame::entry_frame_after_call_words == -(int)rsp_after_call_off + 1 &&
 221            (int)frame::entry_frame_call_wrapper_offset == (int)call_wrapper_off,
 222            "adjust this code");
 223     StubCodeMark mark(this, "StubRoutines", "call_stub");
 224     address start = __ pc();
 225 
 226     // same as in generate_catch_exception()!
 227     const Address rsp_after_call(rbp, rsp_after_call_off * wordSize);
 228 
 229     const Address call_wrapper  (rbp, call_wrapper_off   * wordSize);
 230     const Address result        (rbp, result_off         * wordSize);
 231     const Address result_type   (rbp, result_type_off    * wordSize);
 232     const Address method        (rbp, method_off         * wordSize);
 233     const Address entry_point   (rbp, entry_point_off    * wordSize);
 234     const Address parameters    (rbp, parameters_off     * wordSize);
 235     const Address parameter_size(rbp, parameter_size_off * wordSize);
 236 
 237     // same as in generate_catch_exception()!
 238     const Address thread        (rbp, thread_off         * wordSize);
 239 
 240     const Address r15_save(rbp, r15_off * wordSize);
 241     const Address r14_save(rbp, r14_off * wordSize);
 242     const Address r13_save(rbp, r13_off * wordSize);
 243     const Address r12_save(rbp, r12_off * wordSize);
 244     const Address rbx_save(rbp, rbx_off * wordSize);
 245 
 246     // stub code
 247     __ enter();
 248     __ subptr(rsp, -rsp_after_call_off * wordSize);
 249 
 250     // save register parameters
 251 #ifndef _WIN64
 252     __ movptr(parameters,   c_rarg5); // parameters
 253     __ movptr(entry_point,  c_rarg4); // entry_point
 254 #endif
 255 
 256     __ movptr(method,       c_rarg3); // method
 257     __ movl(result_type,  c_rarg2);   // result type
 258     __ movptr(result,       c_rarg1); // result
 259     __ movptr(call_wrapper, c_rarg0); // call wrapper
 260 
 261     // save regs belonging to calling function
 262     __ movptr(rbx_save, rbx);
 263     __ movptr(r12_save, r12);
 264     __ movptr(r13_save, r13);
 265     __ movptr(r14_save, r14);
 266     __ movptr(r15_save, r15);
 267     if (UseAVX > 2) {
 268       __ movl(rbx, 0xffff);
 269       __ kmovql(k1, rbx);
 270     }
 271 #ifdef _WIN64
 272     if (UseAVX > 2) {
 273       for (int i = 6; i <= 31; i++) {
 274         __ movdqu(xmm_save(i), as_XMMRegister(i));
 275       }
 276     } else {
 277       for (int i = 6; i <= 15; i++) {
 278         __ movdqu(xmm_save(i), as_XMMRegister(i));
 279       }
 280     }
 281 
 282     const Address rdi_save(rbp, rdi_off * wordSize);
 283     const Address rsi_save(rbp, rsi_off * wordSize);
 284 
 285     __ movptr(rsi_save, rsi);
 286     __ movptr(rdi_save, rdi);
 287 #else
 288     const Address mxcsr_save(rbp, mxcsr_off * wordSize);
 289     {
 290       Label skip_ldmx;
 291       __ stmxcsr(mxcsr_save);
 292       __ movl(rax, mxcsr_save);
 293       __ andl(rax, MXCSR_MASK);    // Only check control and mask bits
 294       ExternalAddress mxcsr_std(StubRoutines::addr_mxcsr_std());
 295       __ cmp32(rax, mxcsr_std);
 296       __ jcc(Assembler::equal, skip_ldmx);
 297       __ ldmxcsr(mxcsr_std);
 298       __ bind(skip_ldmx);
 299     }
 300 #endif
 301 
 302     // Load up thread register
 303     __ movptr(r15_thread, thread);
 304     __ reinit_heapbase();
 305 
 306 #ifdef ASSERT
 307     // make sure we have no pending exceptions
 308     {
 309       Label L;
 310       __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 311       __ jcc(Assembler::equal, L);
 312       __ stop("StubRoutines::call_stub: entered with pending exception");
 313       __ bind(L);
 314     }
 315 #endif
 316 
 317     // pass parameters if any
 318     BLOCK_COMMENT("pass parameters if any");
 319     Label parameters_done;
 320     __ movl(c_rarg3, parameter_size);
 321     __ testl(c_rarg3, c_rarg3);
 322     __ jcc(Assembler::zero, parameters_done);
 323 
 324     Label loop;
 325     __ movptr(c_rarg2, parameters);       // parameter pointer
 326     __ movl(c_rarg1, c_rarg3);            // parameter counter is in c_rarg1
 327     __ BIND(loop);
 328     __ movptr(rax, Address(c_rarg2, 0));// get parameter
 329     __ addptr(c_rarg2, wordSize);       // advance to next parameter
 330     __ decrementl(c_rarg1);             // decrement counter
 331     __ push(rax);                       // pass parameter
 332     __ jcc(Assembler::notZero, loop);
 333 
 334     // call Java function
 335     __ BIND(parameters_done);
 336     __ movptr(rbx, method);             // get Method*
 337     __ movptr(c_rarg1, entry_point);    // get entry_point
 338     __ mov(r13, rsp);                   // set sender sp
 339     BLOCK_COMMENT("call Java function");
 340     __ call(c_rarg1);
 341 
 342     BLOCK_COMMENT("call_stub_return_address:");
 343     return_address = __ pc();
 344 
 345     // store result depending on type (everything that is not
 346     // T_OBJECT, T_LONG, T_FLOAT or T_DOUBLE is treated as T_INT)
 347     __ movptr(c_rarg0, result);
 348     Label is_long, is_float, is_double, exit;
 349     __ movl(c_rarg1, result_type);
 350     __ cmpl(c_rarg1, T_OBJECT);
 351     __ jcc(Assembler::equal, is_long);
 352     __ cmpl(c_rarg1, T_LONG);
 353     __ jcc(Assembler::equal, is_long);
 354     __ cmpl(c_rarg1, T_FLOAT);
 355     __ jcc(Assembler::equal, is_float);
 356     __ cmpl(c_rarg1, T_DOUBLE);
 357     __ jcc(Assembler::equal, is_double);
 358 
 359     // handle T_INT case
 360     __ movl(Address(c_rarg0, 0), rax);
 361 
 362     __ BIND(exit);
 363 
 364     // pop parameters
 365     __ lea(rsp, rsp_after_call);
 366 
 367 #ifdef ASSERT
 368     // verify that threads correspond
 369     {
 370       Label L, S;
 371       __ cmpptr(r15_thread, thread);
 372       __ jcc(Assembler::notEqual, S);
 373       __ get_thread(rbx);
 374       __ cmpptr(r15_thread, rbx);
 375       __ jcc(Assembler::equal, L);
 376       __ bind(S);
 377       __ jcc(Assembler::equal, L);
 378       __ stop("StubRoutines::call_stub: threads must correspond");
 379       __ bind(L);
 380     }
 381 #endif
 382 
 383     // restore regs belonging to calling function
 384 #ifdef _WIN64
 385     for (int i = 15; i >= 6; i--) {
 386       __ movdqu(as_XMMRegister(i), xmm_save(i));
 387     }
 388 #endif
 389     __ movptr(r15, r15_save);
 390     __ movptr(r14, r14_save);
 391     __ movptr(r13, r13_save);
 392     __ movptr(r12, r12_save);
 393     __ movptr(rbx, rbx_save);
 394 
 395 #ifdef _WIN64
 396     __ movptr(rdi, rdi_save);
 397     __ movptr(rsi, rsi_save);
 398 #else
 399     __ ldmxcsr(mxcsr_save);
 400 #endif
 401 
 402     // restore rsp
 403     __ addptr(rsp, -rsp_after_call_off * wordSize);
 404 
 405     // return
 406     __ pop(rbp);
 407     __ ret(0);
 408 
 409     // handle return types different from T_INT
 410     __ BIND(is_long);
 411     __ movq(Address(c_rarg0, 0), rax);
 412     __ jmp(exit);
 413 
 414     __ BIND(is_float);
 415     __ movflt(Address(c_rarg0, 0), xmm0);
 416     __ jmp(exit);
 417 
 418     __ BIND(is_double);
 419     __ movdbl(Address(c_rarg0, 0), xmm0);
 420     __ jmp(exit);
 421 
 422     return start;
 423   }
 424 
 425   // Return point for a Java call if there's an exception thrown in
 426   // Java code.  The exception is caught and transformed into a
 427   // pending exception stored in JavaThread that can be tested from
 428   // within the VM.
 429   //
 430   // Note: Usually the parameters are removed by the callee. In case
 431   // of an exception crossing an activation frame boundary, that is
 432   // not the case if the callee is compiled code => need to setup the
 433   // rsp.
 434   //
 435   // rax: exception oop
 436 
 437   address generate_catch_exception() {
 438     StubCodeMark mark(this, "StubRoutines", "catch_exception");
 439     address start = __ pc();
 440 
 441     // same as in generate_call_stub():
 442     const Address rsp_after_call(rbp, rsp_after_call_off * wordSize);
 443     const Address thread        (rbp, thread_off         * wordSize);
 444 
 445 #ifdef ASSERT
 446     // verify that threads correspond
 447     {
 448       Label L, S;
 449       __ cmpptr(r15_thread, thread);
 450       __ jcc(Assembler::notEqual, S);
 451       __ get_thread(rbx);
 452       __ cmpptr(r15_thread, rbx);
 453       __ jcc(Assembler::equal, L);
 454       __ bind(S);
 455       __ stop("StubRoutines::catch_exception: threads must correspond");
 456       __ bind(L);
 457     }
 458 #endif
 459 
 460     // set pending exception
 461     __ verify_oop(rax);
 462 
 463     __ movptr(Address(r15_thread, Thread::pending_exception_offset()), rax);
 464     __ lea(rscratch1, ExternalAddress((address)__FILE__));
 465     __ movptr(Address(r15_thread, Thread::exception_file_offset()), rscratch1);
 466     __ movl(Address(r15_thread, Thread::exception_line_offset()), (int)  __LINE__);
 467 
 468     // complete return to VM
 469     assert(StubRoutines::_call_stub_return_address != NULL,
 470            "_call_stub_return_address must have been generated before");
 471     __ jump(RuntimeAddress(StubRoutines::_call_stub_return_address));
 472 
 473     return start;
 474   }
 475 
 476   // Continuation point for runtime calls returning with a pending
 477   // exception.  The pending exception check happened in the runtime
 478   // or native call stub.  The pending exception in Thread is
 479   // converted into a Java-level exception.
 480   //
 481   // Contract with Java-level exception handlers:
 482   // rax: exception
 483   // rdx: throwing pc
 484   //
 485   // NOTE: At entry of this stub, exception-pc must be on stack !!
 486 
 487   address generate_forward_exception() {
 488     StubCodeMark mark(this, "StubRoutines", "forward exception");
 489     address start = __ pc();
 490 
 491     // Upon entry, the sp points to the return address returning into
 492     // Java (interpreted or compiled) code; i.e., the return address
 493     // becomes the throwing pc.
 494     //
 495     // Arguments pushed before the runtime call are still on the stack
 496     // but the exception handler will reset the stack pointer ->
 497     // ignore them.  A potential result in registers can be ignored as
 498     // well.
 499 
 500 #ifdef ASSERT
 501     // make sure this code is only executed if there is a pending exception
 502     {
 503       Label L;
 504       __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t) NULL);
 505       __ jcc(Assembler::notEqual, L);
 506       __ stop("StubRoutines::forward exception: no pending exception (1)");
 507       __ bind(L);
 508     }
 509 #endif
 510 
 511     // compute exception handler into rbx
 512     __ movptr(c_rarg0, Address(rsp, 0));
 513     BLOCK_COMMENT("call exception_handler_for_return_address");
 514     __ call_VM_leaf(CAST_FROM_FN_PTR(address,
 515                          SharedRuntime::exception_handler_for_return_address),
 516                     r15_thread, c_rarg0);
 517     __ mov(rbx, rax);
 518 
 519     // setup rax & rdx, remove return address & clear pending exception
 520     __ pop(rdx);
 521     __ movptr(rax, Address(r15_thread, Thread::pending_exception_offset()));
 522     __ movptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 523 
 524 #ifdef ASSERT
 525     // make sure exception is set
 526     {
 527       Label L;
 528       __ testptr(rax, rax);
 529       __ jcc(Assembler::notEqual, L);
 530       __ stop("StubRoutines::forward exception: no pending exception (2)");
 531       __ bind(L);
 532     }
 533 #endif
 534 
 535     // continue at exception handler (return address removed)
 536     // rax: exception
 537     // rbx: exception handler
 538     // rdx: throwing pc
 539     __ verify_oop(rax);
 540     __ jmp(rbx);
 541 
 542     return start;
 543   }
 544 
 545   // Support for jint atomic::xchg(jint exchange_value, volatile jint* dest)
 546   //
 547   // Arguments :
 548   //    c_rarg0: exchange_value
 549   //    c_rarg0: dest
 550   //
 551   // Result:
 552   //    *dest <- ex, return (orig *dest)
 553   address generate_atomic_xchg() {
 554     StubCodeMark mark(this, "StubRoutines", "atomic_xchg");
 555     address start = __ pc();
 556 
 557     __ movl(rax, c_rarg0); // Copy to eax we need a return value anyhow
 558     __ xchgl(rax, Address(c_rarg1, 0)); // automatic LOCK
 559     __ ret(0);
 560 
 561     return start;
 562   }
 563 
 564   // Support for intptr_t atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest)
 565   //
 566   // Arguments :
 567   //    c_rarg0: exchange_value
 568   //    c_rarg1: dest
 569   //
 570   // Result:
 571   //    *dest <- ex, return (orig *dest)
 572   address generate_atomic_xchg_ptr() {
 573     StubCodeMark mark(this, "StubRoutines", "atomic_xchg_ptr");
 574     address start = __ pc();
 575 
 576     __ movptr(rax, c_rarg0); // Copy to eax we need a return value anyhow
 577     __ xchgptr(rax, Address(c_rarg1, 0)); // automatic LOCK
 578     __ ret(0);
 579 
 580     return start;
 581   }
 582 
 583   // Support for jint atomic::atomic_cmpxchg(jint exchange_value, volatile jint* dest,
 584   //                                         jint compare_value)
 585   //
 586   // Arguments :
 587   //    c_rarg0: exchange_value
 588   //    c_rarg1: dest
 589   //    c_rarg2: compare_value
 590   //
 591   // Result:
 592   //    if ( compare_value == *dest ) {
 593   //       *dest = exchange_value
 594   //       return compare_value;
 595   //    else
 596   //       return *dest;
 597   address generate_atomic_cmpxchg() {
 598     StubCodeMark mark(this, "StubRoutines", "atomic_cmpxchg");
 599     address start = __ pc();
 600 
 601     __ movl(rax, c_rarg2);
 602    if ( os::is_MP() ) __ lock();
 603     __ cmpxchgl(c_rarg0, Address(c_rarg1, 0));
 604     __ ret(0);
 605 
 606     return start;
 607   }
 608 
 609   // Support for jbyte atomic::atomic_cmpxchg(jbyte exchange_value, volatile jbyte* dest,
 610   //                                          jbyte compare_value)
 611   //
 612   // Arguments :
 613   //    c_rarg0: exchange_value
 614   //    c_rarg1: dest
 615   //    c_rarg2: compare_value
 616   //
 617   // Result:
 618   //    if ( compare_value == *dest ) {
 619   //       *dest = exchange_value
 620   //       return compare_value;
 621   //    else
 622   //       return *dest;
 623   address generate_atomic_cmpxchg_byte() {
 624     StubCodeMark mark(this, "StubRoutines", "atomic_cmpxchg_byte");
 625     address start = __ pc();
 626 
 627     __ movsbq(rax, c_rarg2);
 628    if ( os::is_MP() ) __ lock();
 629     __ cmpxchgb(c_rarg0, Address(c_rarg1, 0));
 630     __ ret(0);
 631 
 632     return start;
 633   }
 634 
 635   // Support for jlong atomic::atomic_cmpxchg(jlong exchange_value,
 636   //                                          volatile jlong* dest,
 637   //                                          jlong compare_value)
 638   // Arguments :
 639   //    c_rarg0: exchange_value
 640   //    c_rarg1: dest
 641   //    c_rarg2: compare_value
 642   //
 643   // Result:
 644   //    if ( compare_value == *dest ) {
 645   //       *dest = exchange_value
 646   //       return compare_value;
 647   //    else
 648   //       return *dest;
 649   address generate_atomic_cmpxchg_long() {
 650     StubCodeMark mark(this, "StubRoutines", "atomic_cmpxchg_long");
 651     address start = __ pc();
 652 
 653     __ movq(rax, c_rarg2);
 654    if ( os::is_MP() ) __ lock();
 655     __ cmpxchgq(c_rarg0, Address(c_rarg1, 0));
 656     __ ret(0);
 657 
 658     return start;
 659   }
 660 
 661   // Support for jint atomic::add(jint add_value, volatile jint* dest)
 662   //
 663   // Arguments :
 664   //    c_rarg0: add_value
 665   //    c_rarg1: dest
 666   //
 667   // Result:
 668   //    *dest += add_value
 669   //    return *dest;
 670   address generate_atomic_add() {
 671     StubCodeMark mark(this, "StubRoutines", "atomic_add");
 672     address start = __ pc();
 673 
 674     __ movl(rax, c_rarg0);
 675    if ( os::is_MP() ) __ lock();
 676     __ xaddl(Address(c_rarg1, 0), c_rarg0);
 677     __ addl(rax, c_rarg0);
 678     __ ret(0);
 679 
 680     return start;
 681   }
 682 
 683   // Support for intptr_t atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest)
 684   //
 685   // Arguments :
 686   //    c_rarg0: add_value
 687   //    c_rarg1: dest
 688   //
 689   // Result:
 690   //    *dest += add_value
 691   //    return *dest;
 692   address generate_atomic_add_ptr() {
 693     StubCodeMark mark(this, "StubRoutines", "atomic_add_ptr");
 694     address start = __ pc();
 695 
 696     __ movptr(rax, c_rarg0); // Copy to eax we need a return value anyhow
 697    if ( os::is_MP() ) __ lock();
 698     __ xaddptr(Address(c_rarg1, 0), c_rarg0);
 699     __ addptr(rax, c_rarg0);
 700     __ ret(0);
 701 
 702     return start;
 703   }
 704 
 705   // Support for intptr_t OrderAccess::fence()
 706   //
 707   // Arguments :
 708   //
 709   // Result:
 710   address generate_orderaccess_fence() {
 711     StubCodeMark mark(this, "StubRoutines", "orderaccess_fence");
 712     address start = __ pc();
 713     __ membar(Assembler::StoreLoad);
 714     __ ret(0);
 715 
 716     return start;
 717   }
 718 
 719   // Support for intptr_t get_previous_fp()
 720   //
 721   // This routine is used to find the previous frame pointer for the
 722   // caller (current_frame_guess). This is used as part of debugging
 723   // ps() is seemingly lost trying to find frames.
 724   // This code assumes that caller current_frame_guess) has a frame.
 725   address generate_get_previous_fp() {
 726     StubCodeMark mark(this, "StubRoutines", "get_previous_fp");
 727     const Address old_fp(rbp, 0);
 728     const Address older_fp(rax, 0);
 729     address start = __ pc();
 730 
 731     __ enter();
 732     __ movptr(rax, old_fp); // callers fp
 733     __ movptr(rax, older_fp); // the frame for ps()
 734     __ pop(rbp);
 735     __ ret(0);
 736 
 737     return start;
 738   }
 739 
 740   // Support for intptr_t get_previous_sp()
 741   //
 742   // This routine is used to find the previous stack pointer for the
 743   // caller.
 744   address generate_get_previous_sp() {
 745     StubCodeMark mark(this, "StubRoutines", "get_previous_sp");
 746     address start = __ pc();
 747 
 748     __ movptr(rax, rsp);
 749     __ addptr(rax, 8); // return address is at the top of the stack.
 750     __ ret(0);
 751 
 752     return start;
 753   }
 754 
 755   //----------------------------------------------------------------------------------------------------
 756   // Support for void verify_mxcsr()
 757   //
 758   // This routine is used with -Xcheck:jni to verify that native
 759   // JNI code does not return to Java code without restoring the
 760   // MXCSR register to our expected state.
 761 
 762   address generate_verify_mxcsr() {
 763     StubCodeMark mark(this, "StubRoutines", "verify_mxcsr");
 764     address start = __ pc();
 765 
 766     const Address mxcsr_save(rsp, 0);
 767 
 768     if (CheckJNICalls) {
 769       Label ok_ret;
 770       ExternalAddress mxcsr_std(StubRoutines::addr_mxcsr_std());
 771       __ push(rax);
 772       __ subptr(rsp, wordSize);      // allocate a temp location
 773       __ stmxcsr(mxcsr_save);
 774       __ movl(rax, mxcsr_save);
 775       __ andl(rax, MXCSR_MASK);    // Only check control and mask bits
 776       __ cmp32(rax, mxcsr_std);
 777       __ jcc(Assembler::equal, ok_ret);
 778 
 779       __ warn("MXCSR changed by native JNI code, use -XX:+RestoreMXCSROnJNICall");
 780 
 781       __ ldmxcsr(mxcsr_std);
 782 
 783       __ bind(ok_ret);
 784       __ addptr(rsp, wordSize);
 785       __ pop(rax);
 786     }
 787 
 788     __ ret(0);
 789 
 790     return start;
 791   }
 792 
 793   address generate_f2i_fixup() {
 794     StubCodeMark mark(this, "StubRoutines", "f2i_fixup");
 795     Address inout(rsp, 5 * wordSize); // return address + 4 saves
 796 
 797     address start = __ pc();
 798 
 799     Label L;
 800 
 801     __ push(rax);
 802     __ push(c_rarg3);
 803     __ push(c_rarg2);
 804     __ push(c_rarg1);
 805 
 806     __ movl(rax, 0x7f800000);
 807     __ xorl(c_rarg3, c_rarg3);
 808     __ movl(c_rarg2, inout);
 809     __ movl(c_rarg1, c_rarg2);
 810     __ andl(c_rarg1, 0x7fffffff);
 811     __ cmpl(rax, c_rarg1); // NaN? -> 0
 812     __ jcc(Assembler::negative, L);
 813     __ testl(c_rarg2, c_rarg2); // signed ? min_jint : max_jint
 814     __ movl(c_rarg3, 0x80000000);
 815     __ movl(rax, 0x7fffffff);
 816     __ cmovl(Assembler::positive, c_rarg3, rax);
 817 
 818     __ bind(L);
 819     __ movptr(inout, c_rarg3);
 820 
 821     __ pop(c_rarg1);
 822     __ pop(c_rarg2);
 823     __ pop(c_rarg3);
 824     __ pop(rax);
 825 
 826     __ ret(0);
 827 
 828     return start;
 829   }
 830 
 831   address generate_f2l_fixup() {
 832     StubCodeMark mark(this, "StubRoutines", "f2l_fixup");
 833     Address inout(rsp, 5 * wordSize); // return address + 4 saves
 834     address start = __ pc();
 835 
 836     Label L;
 837 
 838     __ push(rax);
 839     __ push(c_rarg3);
 840     __ push(c_rarg2);
 841     __ push(c_rarg1);
 842 
 843     __ movl(rax, 0x7f800000);
 844     __ xorl(c_rarg3, c_rarg3);
 845     __ movl(c_rarg2, inout);
 846     __ movl(c_rarg1, c_rarg2);
 847     __ andl(c_rarg1, 0x7fffffff);
 848     __ cmpl(rax, c_rarg1); // NaN? -> 0
 849     __ jcc(Assembler::negative, L);
 850     __ testl(c_rarg2, c_rarg2); // signed ? min_jlong : max_jlong
 851     __ mov64(c_rarg3, 0x8000000000000000);
 852     __ mov64(rax, 0x7fffffffffffffff);
 853     __ cmov(Assembler::positive, c_rarg3, rax);
 854 
 855     __ bind(L);
 856     __ movptr(inout, c_rarg3);
 857 
 858     __ pop(c_rarg1);
 859     __ pop(c_rarg2);
 860     __ pop(c_rarg3);
 861     __ pop(rax);
 862 
 863     __ ret(0);
 864 
 865     return start;
 866   }
 867 
 868   address generate_d2i_fixup() {
 869     StubCodeMark mark(this, "StubRoutines", "d2i_fixup");
 870     Address inout(rsp, 6 * wordSize); // return address + 5 saves
 871 
 872     address start = __ pc();
 873 
 874     Label L;
 875 
 876     __ push(rax);
 877     __ push(c_rarg3);
 878     __ push(c_rarg2);
 879     __ push(c_rarg1);
 880     __ push(c_rarg0);
 881 
 882     __ movl(rax, 0x7ff00000);
 883     __ movq(c_rarg2, inout);
 884     __ movl(c_rarg3, c_rarg2);
 885     __ mov(c_rarg1, c_rarg2);
 886     __ mov(c_rarg0, c_rarg2);
 887     __ negl(c_rarg3);
 888     __ shrptr(c_rarg1, 0x20);
 889     __ orl(c_rarg3, c_rarg2);
 890     __ andl(c_rarg1, 0x7fffffff);
 891     __ xorl(c_rarg2, c_rarg2);
 892     __ shrl(c_rarg3, 0x1f);
 893     __ orl(c_rarg1, c_rarg3);
 894     __ cmpl(rax, c_rarg1);
 895     __ jcc(Assembler::negative, L); // NaN -> 0
 896     __ testptr(c_rarg0, c_rarg0); // signed ? min_jint : max_jint
 897     __ movl(c_rarg2, 0x80000000);
 898     __ movl(rax, 0x7fffffff);
 899     __ cmov(Assembler::positive, c_rarg2, rax);
 900 
 901     __ bind(L);
 902     __ movptr(inout, c_rarg2);
 903 
 904     __ pop(c_rarg0);
 905     __ pop(c_rarg1);
 906     __ pop(c_rarg2);
 907     __ pop(c_rarg3);
 908     __ pop(rax);
 909 
 910     __ ret(0);
 911 
 912     return start;
 913   }
 914 
 915   address generate_d2l_fixup() {
 916     StubCodeMark mark(this, "StubRoutines", "d2l_fixup");
 917     Address inout(rsp, 6 * wordSize); // return address + 5 saves
 918 
 919     address start = __ pc();
 920 
 921     Label L;
 922 
 923     __ push(rax);
 924     __ push(c_rarg3);
 925     __ push(c_rarg2);
 926     __ push(c_rarg1);
 927     __ push(c_rarg0);
 928 
 929     __ movl(rax, 0x7ff00000);
 930     __ movq(c_rarg2, inout);
 931     __ movl(c_rarg3, c_rarg2);
 932     __ mov(c_rarg1, c_rarg2);
 933     __ mov(c_rarg0, c_rarg2);
 934     __ negl(c_rarg3);
 935     __ shrptr(c_rarg1, 0x20);
 936     __ orl(c_rarg3, c_rarg2);
 937     __ andl(c_rarg1, 0x7fffffff);
 938     __ xorl(c_rarg2, c_rarg2);
 939     __ shrl(c_rarg3, 0x1f);
 940     __ orl(c_rarg1, c_rarg3);
 941     __ cmpl(rax, c_rarg1);
 942     __ jcc(Assembler::negative, L); // NaN -> 0
 943     __ testq(c_rarg0, c_rarg0); // signed ? min_jlong : max_jlong
 944     __ mov64(c_rarg2, 0x8000000000000000);
 945     __ mov64(rax, 0x7fffffffffffffff);
 946     __ cmovq(Assembler::positive, c_rarg2, rax);
 947 
 948     __ bind(L);
 949     __ movq(inout, c_rarg2);
 950 
 951     __ pop(c_rarg0);
 952     __ pop(c_rarg1);
 953     __ pop(c_rarg2);
 954     __ pop(c_rarg3);
 955     __ pop(rax);
 956 
 957     __ ret(0);
 958 
 959     return start;
 960   }
 961 
 962   address generate_fp_mask(const char *stub_name, int64_t mask) {
 963     __ align(CodeEntryAlignment);
 964     StubCodeMark mark(this, "StubRoutines", stub_name);
 965     address start = __ pc();
 966 
 967     __ emit_data64( mask, relocInfo::none );
 968     __ emit_data64( mask, relocInfo::none );
 969 
 970     return start;
 971   }
 972 
 973   // The following routine generates a subroutine to throw an
 974   // asynchronous UnknownError when an unsafe access gets a fault that
 975   // could not be reasonably prevented by the programmer.  (Example:
 976   // SIGBUS/OBJERR.)
 977   address generate_handler_for_unsafe_access() {
 978     StubCodeMark mark(this, "StubRoutines", "handler_for_unsafe_access");
 979     address start = __ pc();
 980 
 981     __ push(0);                       // hole for return address-to-be
 982     __ pusha();                       // push registers
 983     Address next_pc(rsp, RegisterImpl::number_of_registers * BytesPerWord);
 984 
 985     // FIXME: this probably needs alignment logic
 986 
 987     __ subptr(rsp, frame::arg_reg_save_area_bytes);
 988     BLOCK_COMMENT("call handle_unsafe_access");
 989     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, handle_unsafe_access)));
 990     __ addptr(rsp, frame::arg_reg_save_area_bytes);
 991 
 992     __ movptr(next_pc, rax);          // stuff next address
 993     __ popa();
 994     __ ret(0);                        // jump to next address
 995 
 996     return start;
 997   }
 998 
 999   // Non-destructive plausibility checks for oops
1000   //
1001   // Arguments:
1002   //    all args on stack!
1003   //
1004   // Stack after saving c_rarg3:
1005   //    [tos + 0]: saved c_rarg3
1006   //    [tos + 1]: saved c_rarg2
1007   //    [tos + 2]: saved r12 (several TemplateTable methods use it)
1008   //    [tos + 3]: saved flags
1009   //    [tos + 4]: return address
1010   //  * [tos + 5]: error message (char*)
1011   //  * [tos + 6]: object to verify (oop)
1012   //  * [tos + 7]: saved rax - saved by caller and bashed
1013   //  * [tos + 8]: saved r10 (rscratch1) - saved by caller
1014   //  * = popped on exit
1015   address generate_verify_oop() {
1016     StubCodeMark mark(this, "StubRoutines", "verify_oop");
1017     address start = __ pc();
1018 
1019     Label exit, error;
1020 
1021     __ pushf();
1022     __ incrementl(ExternalAddress((address) StubRoutines::verify_oop_count_addr()));
1023 
1024     __ push(r12);
1025 
1026     // save c_rarg2 and c_rarg3
1027     __ push(c_rarg2);
1028     __ push(c_rarg3);
1029 
1030     enum {
1031            // After previous pushes.
1032            oop_to_verify = 6 * wordSize,
1033            saved_rax     = 7 * wordSize,
1034            saved_r10     = 8 * wordSize,
1035 
1036            // Before the call to MacroAssembler::debug(), see below.
1037            return_addr   = 16 * wordSize,
1038            error_msg     = 17 * wordSize
1039     };
1040 
1041     // get object
1042     __ movptr(rax, Address(rsp, oop_to_verify));
1043 
1044     // make sure object is 'reasonable'
1045     __ testptr(rax, rax);
1046     __ jcc(Assembler::zero, exit); // if obj is NULL it is OK
1047     // Check if the oop is in the right area of memory
1048     __ movptr(c_rarg2, rax);
1049     __ movptr(c_rarg3, (intptr_t) Universe::verify_oop_mask());
1050     __ andptr(c_rarg2, c_rarg3);
1051     __ movptr(c_rarg3, (intptr_t) Universe::verify_oop_bits());
1052     __ cmpptr(c_rarg2, c_rarg3);
1053     __ jcc(Assembler::notZero, error);
1054 
1055     // set r12 to heapbase for load_klass()
1056     __ reinit_heapbase();
1057 
1058     // make sure klass is 'reasonable', which is not zero.
1059     __ load_klass(rax, rax);  // get klass
1060     __ testptr(rax, rax);
1061     __ jcc(Assembler::zero, error); // if klass is NULL it is broken
1062 
1063     // return if everything seems ok
1064     __ bind(exit);
1065     __ movptr(rax, Address(rsp, saved_rax));     // get saved rax back
1066     __ movptr(rscratch1, Address(rsp, saved_r10)); // get saved r10 back
1067     __ pop(c_rarg3);                             // restore c_rarg3
1068     __ pop(c_rarg2);                             // restore c_rarg2
1069     __ pop(r12);                                 // restore r12
1070     __ popf();                                   // restore flags
1071     __ ret(4 * wordSize);                        // pop caller saved stuff
1072 
1073     // handle errors
1074     __ bind(error);
1075     __ movptr(rax, Address(rsp, saved_rax));     // get saved rax back
1076     __ movptr(rscratch1, Address(rsp, saved_r10)); // get saved r10 back
1077     __ pop(c_rarg3);                             // get saved c_rarg3 back
1078     __ pop(c_rarg2);                             // get saved c_rarg2 back
1079     __ pop(r12);                                 // get saved r12 back
1080     __ popf();                                   // get saved flags off stack --
1081                                                  // will be ignored
1082 
1083     __ pusha();                                  // push registers
1084                                                  // (rip is already
1085                                                  // already pushed)
1086     // debug(char* msg, int64_t pc, int64_t regs[])
1087     // We've popped the registers we'd saved (c_rarg3, c_rarg2 and flags), and
1088     // pushed all the registers, so now the stack looks like:
1089     //     [tos +  0] 16 saved registers
1090     //     [tos + 16] return address
1091     //   * [tos + 17] error message (char*)
1092     //   * [tos + 18] object to verify (oop)
1093     //   * [tos + 19] saved rax - saved by caller and bashed
1094     //   * [tos + 20] saved r10 (rscratch1) - saved by caller
1095     //   * = popped on exit
1096 
1097     __ movptr(c_rarg0, Address(rsp, error_msg));    // pass address of error message
1098     __ movptr(c_rarg1, Address(rsp, return_addr));  // pass return address
1099     __ movq(c_rarg2, rsp);                          // pass address of regs on stack
1100     __ mov(r12, rsp);                               // remember rsp
1101     __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
1102     __ andptr(rsp, -16);                            // align stack as required by ABI
1103     BLOCK_COMMENT("call MacroAssembler::debug");
1104     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, MacroAssembler::debug64)));
1105     __ mov(rsp, r12);                               // restore rsp
1106     __ popa();                                      // pop registers (includes r12)
1107     __ ret(4 * wordSize);                           // pop caller saved stuff
1108 
1109     return start;
1110   }
1111 
1112   //
1113   // Verify that a register contains clean 32-bits positive value
1114   // (high 32-bits are 0) so it could be used in 64-bits shifts.
1115   //
1116   //  Input:
1117   //    Rint  -  32-bits value
1118   //    Rtmp  -  scratch
1119   //
1120   void assert_clean_int(Register Rint, Register Rtmp) {
1121 #ifdef ASSERT
1122     Label L;
1123     assert_different_registers(Rtmp, Rint);
1124     __ movslq(Rtmp, Rint);
1125     __ cmpq(Rtmp, Rint);
1126     __ jcc(Assembler::equal, L);
1127     __ stop("high 32-bits of int value are not 0");
1128     __ bind(L);
1129 #endif
1130   }
1131 
1132   //  Generate overlap test for array copy stubs
1133   //
1134   //  Input:
1135   //     c_rarg0 - from
1136   //     c_rarg1 - to
1137   //     c_rarg2 - element count
1138   //
1139   //  Output:
1140   //     rax   - &from[element count - 1]
1141   //
1142   void array_overlap_test(address no_overlap_target, Address::ScaleFactor sf) {
1143     assert(no_overlap_target != NULL, "must be generated");
1144     array_overlap_test(no_overlap_target, NULL, sf);
1145   }
1146   void array_overlap_test(Label& L_no_overlap, Address::ScaleFactor sf) {
1147     array_overlap_test(NULL, &L_no_overlap, sf);
1148   }
1149   void array_overlap_test(address no_overlap_target, Label* NOLp, Address::ScaleFactor sf) {
1150     const Register from     = c_rarg0;
1151     const Register to       = c_rarg1;
1152     const Register count    = c_rarg2;
1153     const Register end_from = rax;
1154 
1155     __ cmpptr(to, from);
1156     __ lea(end_from, Address(from, count, sf, 0));
1157     if (NOLp == NULL) {
1158       ExternalAddress no_overlap(no_overlap_target);
1159       __ jump_cc(Assembler::belowEqual, no_overlap);
1160       __ cmpptr(to, end_from);
1161       __ jump_cc(Assembler::aboveEqual, no_overlap);
1162     } else {
1163       __ jcc(Assembler::belowEqual, (*NOLp));
1164       __ cmpptr(to, end_from);
1165       __ jcc(Assembler::aboveEqual, (*NOLp));
1166     }
1167   }
1168 
1169   // Shuffle first three arg regs on Windows into Linux/Solaris locations.
1170   //
1171   // Outputs:
1172   //    rdi - rcx
1173   //    rsi - rdx
1174   //    rdx - r8
1175   //    rcx - r9
1176   //
1177   // Registers r9 and r10 are used to save rdi and rsi on Windows, which latter
1178   // are non-volatile.  r9 and r10 should not be used by the caller.
1179   //
1180   void setup_arg_regs(int nargs = 3) {
1181     const Register saved_rdi = r9;
1182     const Register saved_rsi = r10;
1183     assert(nargs == 3 || nargs == 4, "else fix");
1184 #ifdef _WIN64
1185     assert(c_rarg0 == rcx && c_rarg1 == rdx && c_rarg2 == r8 && c_rarg3 == r9,
1186            "unexpected argument registers");
1187     if (nargs >= 4)
1188       __ mov(rax, r9);  // r9 is also saved_rdi
1189     __ movptr(saved_rdi, rdi);
1190     __ movptr(saved_rsi, rsi);
1191     __ mov(rdi, rcx); // c_rarg0
1192     __ mov(rsi, rdx); // c_rarg1
1193     __ mov(rdx, r8);  // c_rarg2
1194     if (nargs >= 4)
1195       __ mov(rcx, rax); // c_rarg3 (via rax)
1196 #else
1197     assert(c_rarg0 == rdi && c_rarg1 == rsi && c_rarg2 == rdx && c_rarg3 == rcx,
1198            "unexpected argument registers");
1199 #endif
1200   }
1201 
1202   void restore_arg_regs() {
1203     const Register saved_rdi = r9;
1204     const Register saved_rsi = r10;
1205 #ifdef _WIN64
1206     __ movptr(rdi, saved_rdi);
1207     __ movptr(rsi, saved_rsi);
1208 #endif
1209   }
1210 
1211   // Generate code for an array write pre barrier
1212   //
1213   //     addr    -  starting address
1214   //     count   -  element count
1215   //     tmp     - scratch register
1216   //
1217   //     Destroy no registers!
1218   //
1219   void  gen_write_ref_array_pre_barrier(Register addr, Register count, bool dest_uninitialized) {
1220     BarrierSet* bs = Universe::heap()->barrier_set();
1221     switch (bs->kind()) {
1222       case BarrierSet::G1SATBCTLogging:
1223         // With G1, don't generate the call if we statically know that the target in uninitialized
1224         if (!dest_uninitialized) {
1225            __ pusha();                      // push registers
1226            if (count == c_rarg0) {
1227              if (addr == c_rarg1) {
1228                // exactly backwards!!
1229                __ xchgptr(c_rarg1, c_rarg0);
1230              } else {
1231                __ movptr(c_rarg1, count);
1232                __ movptr(c_rarg0, addr);
1233              }
1234            } else {
1235              __ movptr(c_rarg0, addr);
1236              __ movptr(c_rarg1, count);
1237            }
1238            __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_pre), 2);
1239            __ popa();
1240         }
1241          break;
1242       case BarrierSet::CardTableModRef:
1243       case BarrierSet::CardTableExtension:
1244       case BarrierSet::ModRef:
1245         break;
1246       default:
1247         ShouldNotReachHere();
1248 
1249     }
1250   }
1251 
1252   //
1253   // Generate code for an array write post barrier
1254   //
1255   //  Input:
1256   //     start    - register containing starting address of destination array
1257   //     count    - elements count
1258   //     scratch  - scratch register
1259   //
1260   //  The input registers are overwritten.
1261   //
1262   void  gen_write_ref_array_post_barrier(Register start, Register count, Register scratch) {
1263     assert_different_registers(start, count, scratch);
1264     BarrierSet* bs = Universe::heap()->barrier_set();
1265     switch (bs->kind()) {
1266       case BarrierSet::G1SATBCTLogging:
1267         {
1268           __ pusha();             // push registers (overkill)
1269           if (c_rarg0 == count) { // On win64 c_rarg0 == rcx
1270             assert_different_registers(c_rarg1, start);
1271             __ mov(c_rarg1, count);
1272             __ mov(c_rarg0, start);
1273           } else {
1274             assert_different_registers(c_rarg0, count);
1275             __ mov(c_rarg0, start);
1276             __ mov(c_rarg1, count);
1277           }
1278           __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_post), 2);
1279           __ popa();
1280         }
1281         break;
1282       case BarrierSet::CardTableModRef:
1283       case BarrierSet::CardTableExtension:
1284         {
1285           CardTableModRefBS* ct = barrier_set_cast<CardTableModRefBS>(bs);
1286           assert(sizeof(*ct->byte_map_base) == sizeof(jbyte), "adjust this code");
1287 
1288           Label L_loop;
1289           const Register end = count;
1290 
1291           __ leaq(end, Address(start, count, TIMES_OOP, 0));  // end == start+count*oop_size
1292           __ subptr(end, BytesPerHeapOop); // end - 1 to make inclusive
1293           __ shrptr(start, CardTableModRefBS::card_shift);
1294           __ shrptr(end,   CardTableModRefBS::card_shift);
1295           __ subptr(end, start); // end --> cards count
1296 
1297           int64_t disp = (int64_t) ct->byte_map_base;
1298           __ mov64(scratch, disp);
1299           __ addptr(start, scratch);
1300         __ BIND(L_loop);
1301           __ movb(Address(start, count, Address::times_1), 0);
1302           __ decrement(count);
1303           __ jcc(Assembler::greaterEqual, L_loop);
1304         }
1305         break;
1306       default:
1307         ShouldNotReachHere();
1308 
1309     }
1310   }
1311 
1312 
1313   // Copy big chunks forward
1314   //
1315   // Inputs:
1316   //   end_from     - source arrays end address
1317   //   end_to       - destination array end address
1318   //   qword_count  - 64-bits element count, negative
1319   //   to           - scratch
1320   //   L_copy_bytes - entry label
1321   //   L_copy_8_bytes  - exit  label
1322   //
1323   void copy_bytes_forward(Register end_from, Register end_to,
1324                              Register qword_count, Register to,
1325                              Label& L_copy_bytes, Label& L_copy_8_bytes) {
1326     DEBUG_ONLY(__ stop("enter at entry label, not here"));
1327     Label L_loop;
1328     __ align(OptoLoopAlignment);
1329     if (UseUnalignedLoadStores) {
1330       Label L_end;
1331       // Copy 64-bytes per iteration
1332       __ BIND(L_loop);
1333       if (UseAVX > 2) {
1334         __ evmovdqu(xmm0, Address(end_from, qword_count, Address::times_8, -56), Assembler::AVX_512bit);
1335         __ evmovdqu(Address(end_to, qword_count, Address::times_8, -56), xmm0, Assembler::AVX_512bit);
1336       } else if (UseAVX == 2) {
1337         __ vmovdqu(xmm0, Address(end_from, qword_count, Address::times_8, -56));
1338         __ vmovdqu(Address(end_to, qword_count, Address::times_8, -56), xmm0);
1339         __ vmovdqu(xmm1, Address(end_from, qword_count, Address::times_8, -24));
1340         __ vmovdqu(Address(end_to, qword_count, Address::times_8, -24), xmm1);
1341       } else {
1342         __ movdqu(xmm0, Address(end_from, qword_count, Address::times_8, -56));
1343         __ movdqu(Address(end_to, qword_count, Address::times_8, -56), xmm0);
1344         __ movdqu(xmm1, Address(end_from, qword_count, Address::times_8, -40));
1345         __ movdqu(Address(end_to, qword_count, Address::times_8, -40), xmm1);
1346         __ movdqu(xmm2, Address(end_from, qword_count, Address::times_8, -24));
1347         __ movdqu(Address(end_to, qword_count, Address::times_8, -24), xmm2);
1348         __ movdqu(xmm3, Address(end_from, qword_count, Address::times_8, - 8));
1349         __ movdqu(Address(end_to, qword_count, Address::times_8, - 8), xmm3);
1350       }
1351       __ BIND(L_copy_bytes);
1352       __ addptr(qword_count, 8);
1353       __ jcc(Assembler::lessEqual, L_loop);
1354       __ subptr(qword_count, 4);  // sub(8) and add(4)
1355       __ jccb(Assembler::greater, L_end);
1356       // Copy trailing 32 bytes
1357       if (UseAVX >= 2) {
1358         __ vmovdqu(xmm0, Address(end_from, qword_count, Address::times_8, -24));
1359         __ vmovdqu(Address(end_to, qword_count, Address::times_8, -24), xmm0);
1360       } else {
1361         __ movdqu(xmm0, Address(end_from, qword_count, Address::times_8, -24));
1362         __ movdqu(Address(end_to, qword_count, Address::times_8, -24), xmm0);
1363         __ movdqu(xmm1, Address(end_from, qword_count, Address::times_8, - 8));
1364         __ movdqu(Address(end_to, qword_count, Address::times_8, - 8), xmm1);
1365       }
1366       __ addptr(qword_count, 4);
1367       __ BIND(L_end);
1368       if (UseAVX >= 2) {
1369         // clean upper bits of YMM registers
1370         __ vzeroupper();
1371       }
1372     } else {
1373       // Copy 32-bytes per iteration
1374       __ BIND(L_loop);
1375       __ movq(to, Address(end_from, qword_count, Address::times_8, -24));
1376       __ movq(Address(end_to, qword_count, Address::times_8, -24), to);
1377       __ movq(to, Address(end_from, qword_count, Address::times_8, -16));
1378       __ movq(Address(end_to, qword_count, Address::times_8, -16), to);
1379       __ movq(to, Address(end_from, qword_count, Address::times_8, - 8));
1380       __ movq(Address(end_to, qword_count, Address::times_8, - 8), to);
1381       __ movq(to, Address(end_from, qword_count, Address::times_8, - 0));
1382       __ movq(Address(end_to, qword_count, Address::times_8, - 0), to);
1383 
1384       __ BIND(L_copy_bytes);
1385       __ addptr(qword_count, 4);
1386       __ jcc(Assembler::lessEqual, L_loop);
1387     }
1388     __ subptr(qword_count, 4);
1389     __ jcc(Assembler::less, L_copy_8_bytes); // Copy trailing qwords
1390   }
1391 
1392   // Copy big chunks backward
1393   //
1394   // Inputs:
1395   //   from         - source arrays address
1396   //   dest         - destination array address
1397   //   qword_count  - 64-bits element count
1398   //   to           - scratch
1399   //   L_copy_bytes - entry label
1400   //   L_copy_8_bytes  - exit  label
1401   //
1402   void copy_bytes_backward(Register from, Register dest,
1403                               Register qword_count, Register to,
1404                               Label& L_copy_bytes, Label& L_copy_8_bytes) {
1405     DEBUG_ONLY(__ stop("enter at entry label, not here"));
1406     Label L_loop;
1407     __ align(OptoLoopAlignment);
1408     if (UseUnalignedLoadStores) {
1409       Label L_end;
1410       // Copy 64-bytes per iteration
1411       __ BIND(L_loop);
1412       if (UseAVX > 2) {
1413         __ evmovdqu(xmm0, Address(from, qword_count, Address::times_8, 32), Assembler::AVX_512bit);
1414         __ evmovdqu(Address(dest, qword_count, Address::times_8, 32), xmm0, Assembler::AVX_512bit);
1415       } else if (UseAVX == 2) {
1416         __ vmovdqu(xmm0, Address(from, qword_count, Address::times_8, 32));
1417         __ vmovdqu(Address(dest, qword_count, Address::times_8, 32), xmm0);
1418         __ vmovdqu(xmm1, Address(from, qword_count, Address::times_8,  0));
1419         __ vmovdqu(Address(dest, qword_count, Address::times_8,  0), xmm1);
1420       } else {
1421         __ movdqu(xmm0, Address(from, qword_count, Address::times_8, 48));
1422         __ movdqu(Address(dest, qword_count, Address::times_8, 48), xmm0);
1423         __ movdqu(xmm1, Address(from, qword_count, Address::times_8, 32));
1424         __ movdqu(Address(dest, qword_count, Address::times_8, 32), xmm1);
1425         __ movdqu(xmm2, Address(from, qword_count, Address::times_8, 16));
1426         __ movdqu(Address(dest, qword_count, Address::times_8, 16), xmm2);
1427         __ movdqu(xmm3, Address(from, qword_count, Address::times_8,  0));
1428         __ movdqu(Address(dest, qword_count, Address::times_8,  0), xmm3);
1429       }
1430       __ BIND(L_copy_bytes);
1431       __ subptr(qword_count, 8);
1432       __ jcc(Assembler::greaterEqual, L_loop);
1433 
1434       __ addptr(qword_count, 4);  // add(8) and sub(4)
1435       __ jccb(Assembler::less, L_end);
1436       // Copy trailing 32 bytes
1437       if (UseAVX >= 2) {
1438         __ vmovdqu(xmm0, Address(from, qword_count, Address::times_8, 0));
1439         __ vmovdqu(Address(dest, qword_count, Address::times_8, 0), xmm0);
1440       } else {
1441         __ movdqu(xmm0, Address(from, qword_count, Address::times_8, 16));
1442         __ movdqu(Address(dest, qword_count, Address::times_8, 16), xmm0);
1443         __ movdqu(xmm1, Address(from, qword_count, Address::times_8,  0));
1444         __ movdqu(Address(dest, qword_count, Address::times_8,  0), xmm1);
1445       }
1446       __ subptr(qword_count, 4);
1447       __ BIND(L_end);
1448       if (UseAVX >= 2) {
1449         // clean upper bits of YMM registers
1450         __ vzeroupper();
1451       }
1452     } else {
1453       // Copy 32-bytes per iteration
1454       __ BIND(L_loop);
1455       __ movq(to, Address(from, qword_count, Address::times_8, 24));
1456       __ movq(Address(dest, qword_count, Address::times_8, 24), to);
1457       __ movq(to, Address(from, qword_count, Address::times_8, 16));
1458       __ movq(Address(dest, qword_count, Address::times_8, 16), to);
1459       __ movq(to, Address(from, qword_count, Address::times_8,  8));
1460       __ movq(Address(dest, qword_count, Address::times_8,  8), to);
1461       __ movq(to, Address(from, qword_count, Address::times_8,  0));
1462       __ movq(Address(dest, qword_count, Address::times_8,  0), to);
1463 
1464       __ BIND(L_copy_bytes);
1465       __ subptr(qword_count, 4);
1466       __ jcc(Assembler::greaterEqual, L_loop);
1467     }
1468     __ addptr(qword_count, 4);
1469     __ jcc(Assembler::greater, L_copy_8_bytes); // Copy trailing qwords
1470   }
1471 
1472 
1473   // Arguments:
1474   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1475   //             ignored
1476   //   name    - stub name string
1477   //
1478   // Inputs:
1479   //   c_rarg0   - source array address
1480   //   c_rarg1   - destination array address
1481   //   c_rarg2   - element count, treated as ssize_t, can be zero
1482   //
1483   // If 'from' and/or 'to' are aligned on 4-, 2-, or 1-byte boundaries,
1484   // we let the hardware handle it.  The one to eight bytes within words,
1485   // dwords or qwords that span cache line boundaries will still be loaded
1486   // and stored atomically.
1487   //
1488   // Side Effects:
1489   //   disjoint_byte_copy_entry is set to the no-overlap entry point
1490   //   used by generate_conjoint_byte_copy().
1491   //
1492   address generate_disjoint_byte_copy(bool aligned, address* entry, const char *name) {
1493     __ align(CodeEntryAlignment);
1494     StubCodeMark mark(this, "StubRoutines", name);
1495     address start = __ pc();
1496 
1497     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes, L_copy_2_bytes;
1498     Label L_copy_byte, L_exit;
1499     const Register from        = rdi;  // source array address
1500     const Register to          = rsi;  // destination array address
1501     const Register count       = rdx;  // elements count
1502     const Register byte_count  = rcx;
1503     const Register qword_count = count;
1504     const Register end_from    = from; // source array end address
1505     const Register end_to      = to;   // destination array end address
1506     // End pointers are inclusive, and if count is not zero they point
1507     // to the last unit copied:  end_to[0] := end_from[0]
1508 
1509     __ enter(); // required for proper stackwalking of RuntimeStub frame
1510     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1511 
1512     if (entry != NULL) {
1513       *entry = __ pc();
1514        // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1515       BLOCK_COMMENT("Entry:");
1516     }
1517 
1518     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1519                       // r9 and r10 may be used to save non-volatile registers
1520 
1521     // 'from', 'to' and 'count' are now valid
1522     __ movptr(byte_count, count);
1523     __ shrptr(count, 3); // count => qword_count
1524 
1525     // Copy from low to high addresses.  Use 'to' as scratch.
1526     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1527     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1528     __ negptr(qword_count); // make the count negative
1529     __ jmp(L_copy_bytes);
1530 
1531     // Copy trailing qwords
1532   __ BIND(L_copy_8_bytes);
1533     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
1534     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
1535     __ increment(qword_count);
1536     __ jcc(Assembler::notZero, L_copy_8_bytes);
1537 
1538     // Check for and copy trailing dword
1539   __ BIND(L_copy_4_bytes);
1540     __ testl(byte_count, 4);
1541     __ jccb(Assembler::zero, L_copy_2_bytes);
1542     __ movl(rax, Address(end_from, 8));
1543     __ movl(Address(end_to, 8), rax);
1544 
1545     __ addptr(end_from, 4);
1546     __ addptr(end_to, 4);
1547 
1548     // Check for and copy trailing word
1549   __ BIND(L_copy_2_bytes);
1550     __ testl(byte_count, 2);
1551     __ jccb(Assembler::zero, L_copy_byte);
1552     __ movw(rax, Address(end_from, 8));
1553     __ movw(Address(end_to, 8), rax);
1554 
1555     __ addptr(end_from, 2);
1556     __ addptr(end_to, 2);
1557 
1558     // Check for and copy trailing byte
1559   __ BIND(L_copy_byte);
1560     __ testl(byte_count, 1);
1561     __ jccb(Assembler::zero, L_exit);
1562     __ movb(rax, Address(end_from, 8));
1563     __ movb(Address(end_to, 8), rax);
1564 
1565   __ BIND(L_exit);
1566     restore_arg_regs();
1567     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); // Update counter after rscratch1 is free
1568     __ xorptr(rax, rax); // return 0
1569     __ leave(); // required for proper stackwalking of RuntimeStub frame
1570     __ ret(0);
1571 
1572     // Copy in multi-bytes chunks
1573     copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1574     __ jmp(L_copy_4_bytes);
1575 
1576     return start;
1577   }
1578 
1579   // Arguments:
1580   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1581   //             ignored
1582   //   name    - stub name string
1583   //
1584   // Inputs:
1585   //   c_rarg0   - source array address
1586   //   c_rarg1   - destination array address
1587   //   c_rarg2   - element count, treated as ssize_t, can be zero
1588   //
1589   // If 'from' and/or 'to' are aligned on 4-, 2-, or 1-byte boundaries,
1590   // we let the hardware handle it.  The one to eight bytes within words,
1591   // dwords or qwords that span cache line boundaries will still be loaded
1592   // and stored atomically.
1593   //
1594   address generate_conjoint_byte_copy(bool aligned, address nooverlap_target,
1595                                       address* entry, const char *name) {
1596     __ align(CodeEntryAlignment);
1597     StubCodeMark mark(this, "StubRoutines", name);
1598     address start = __ pc();
1599 
1600     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes, L_copy_2_bytes;
1601     const Register from        = rdi;  // source array address
1602     const Register to          = rsi;  // destination array address
1603     const Register count       = rdx;  // elements count
1604     const Register byte_count  = rcx;
1605     const Register qword_count = count;
1606 
1607     __ enter(); // required for proper stackwalking of RuntimeStub frame
1608     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1609 
1610     if (entry != NULL) {
1611       *entry = __ pc();
1612       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1613       BLOCK_COMMENT("Entry:");
1614     }
1615 
1616     array_overlap_test(nooverlap_target, Address::times_1);
1617     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1618                       // r9 and r10 may be used to save non-volatile registers
1619 
1620     // 'from', 'to' and 'count' are now valid
1621     __ movptr(byte_count, count);
1622     __ shrptr(count, 3);   // count => qword_count
1623 
1624     // Copy from high to low addresses.
1625 
1626     // Check for and copy trailing byte
1627     __ testl(byte_count, 1);
1628     __ jcc(Assembler::zero, L_copy_2_bytes);
1629     __ movb(rax, Address(from, byte_count, Address::times_1, -1));
1630     __ movb(Address(to, byte_count, Address::times_1, -1), rax);
1631     __ decrement(byte_count); // Adjust for possible trailing word
1632 
1633     // Check for and copy trailing word
1634   __ BIND(L_copy_2_bytes);
1635     __ testl(byte_count, 2);
1636     __ jcc(Assembler::zero, L_copy_4_bytes);
1637     __ movw(rax, Address(from, byte_count, Address::times_1, -2));
1638     __ movw(Address(to, byte_count, Address::times_1, -2), rax);
1639 
1640     // Check for and copy trailing dword
1641   __ BIND(L_copy_4_bytes);
1642     __ testl(byte_count, 4);
1643     __ jcc(Assembler::zero, L_copy_bytes);
1644     __ movl(rax, Address(from, qword_count, Address::times_8));
1645     __ movl(Address(to, qword_count, Address::times_8), rax);
1646     __ jmp(L_copy_bytes);
1647 
1648     // Copy trailing qwords
1649   __ BIND(L_copy_8_bytes);
1650     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
1651     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
1652     __ decrement(qword_count);
1653     __ jcc(Assembler::notZero, L_copy_8_bytes);
1654 
1655     restore_arg_regs();
1656     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); // Update counter after rscratch1 is free
1657     __ xorptr(rax, rax); // return 0
1658     __ leave(); // required for proper stackwalking of RuntimeStub frame
1659     __ ret(0);
1660 
1661     // Copy in multi-bytes chunks
1662     copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1663 
1664     restore_arg_regs();
1665     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); // Update counter after rscratch1 is free
1666     __ xorptr(rax, rax); // return 0
1667     __ leave(); // required for proper stackwalking of RuntimeStub frame
1668     __ ret(0);
1669 
1670     return start;
1671   }
1672 
1673   // Arguments:
1674   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1675   //             ignored
1676   //   name    - stub name string
1677   //
1678   // Inputs:
1679   //   c_rarg0   - source array address
1680   //   c_rarg1   - destination array address
1681   //   c_rarg2   - element count, treated as ssize_t, can be zero
1682   //
1683   // If 'from' and/or 'to' are aligned on 4- or 2-byte boundaries, we
1684   // let the hardware handle it.  The two or four words within dwords
1685   // or qwords that span cache line boundaries will still be loaded
1686   // and stored atomically.
1687   //
1688   // Side Effects:
1689   //   disjoint_short_copy_entry is set to the no-overlap entry point
1690   //   used by generate_conjoint_short_copy().
1691   //
1692   address generate_disjoint_short_copy(bool aligned, address *entry, const char *name) {
1693     __ align(CodeEntryAlignment);
1694     StubCodeMark mark(this, "StubRoutines", name);
1695     address start = __ pc();
1696 
1697     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes,L_copy_2_bytes,L_exit;
1698     const Register from        = rdi;  // source array address
1699     const Register to          = rsi;  // destination array address
1700     const Register count       = rdx;  // elements count
1701     const Register word_count  = rcx;
1702     const Register qword_count = count;
1703     const Register end_from    = from; // source array end address
1704     const Register end_to      = to;   // destination array end address
1705     // End pointers are inclusive, and if count is not zero they point
1706     // to the last unit copied:  end_to[0] := end_from[0]
1707 
1708     __ enter(); // required for proper stackwalking of RuntimeStub frame
1709     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1710 
1711     if (entry != NULL) {
1712       *entry = __ pc();
1713       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1714       BLOCK_COMMENT("Entry:");
1715     }
1716 
1717     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1718                       // r9 and r10 may be used to save non-volatile registers
1719 
1720     // 'from', 'to' and 'count' are now valid
1721     __ movptr(word_count, count);
1722     __ shrptr(count, 2); // count => qword_count
1723 
1724     // Copy from low to high addresses.  Use 'to' as scratch.
1725     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1726     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1727     __ negptr(qword_count);
1728     __ jmp(L_copy_bytes);
1729 
1730     // Copy trailing qwords
1731   __ BIND(L_copy_8_bytes);
1732     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
1733     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
1734     __ increment(qword_count);
1735     __ jcc(Assembler::notZero, L_copy_8_bytes);
1736 
1737     // Original 'dest' is trashed, so we can't use it as a
1738     // base register for a possible trailing word copy
1739 
1740     // Check for and copy trailing dword
1741   __ BIND(L_copy_4_bytes);
1742     __ testl(word_count, 2);
1743     __ jccb(Assembler::zero, L_copy_2_bytes);
1744     __ movl(rax, Address(end_from, 8));
1745     __ movl(Address(end_to, 8), rax);
1746 
1747     __ addptr(end_from, 4);
1748     __ addptr(end_to, 4);
1749 
1750     // Check for and copy trailing word
1751   __ BIND(L_copy_2_bytes);
1752     __ testl(word_count, 1);
1753     __ jccb(Assembler::zero, L_exit);
1754     __ movw(rax, Address(end_from, 8));
1755     __ movw(Address(end_to, 8), rax);
1756 
1757   __ BIND(L_exit);
1758     restore_arg_regs();
1759     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); // Update counter after rscratch1 is free
1760     __ xorptr(rax, rax); // return 0
1761     __ leave(); // required for proper stackwalking of RuntimeStub frame
1762     __ ret(0);
1763 
1764     // Copy in multi-bytes chunks
1765     copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1766     __ jmp(L_copy_4_bytes);
1767 
1768     return start;
1769   }
1770 
1771   address generate_fill(BasicType t, bool aligned, const char *name) {
1772     __ align(CodeEntryAlignment);
1773     StubCodeMark mark(this, "StubRoutines", name);
1774     address start = __ pc();
1775 
1776     BLOCK_COMMENT("Entry:");
1777 
1778     const Register to       = c_rarg0;  // source array address
1779     const Register value    = c_rarg1;  // value
1780     const Register count    = c_rarg2;  // elements count
1781 
1782     __ enter(); // required for proper stackwalking of RuntimeStub frame
1783 
1784     __ generate_fill(t, aligned, to, value, count, rax, xmm0);
1785 
1786     __ leave(); // required for proper stackwalking of RuntimeStub frame
1787     __ ret(0);
1788     return start;
1789   }
1790 
1791   // Arguments:
1792   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1793   //             ignored
1794   //   name    - stub name string
1795   //
1796   // Inputs:
1797   //   c_rarg0   - source array address
1798   //   c_rarg1   - destination array address
1799   //   c_rarg2   - element count, treated as ssize_t, can be zero
1800   //
1801   // If 'from' and/or 'to' are aligned on 4- or 2-byte boundaries, we
1802   // let the hardware handle it.  The two or four words within dwords
1803   // or qwords that span cache line boundaries will still be loaded
1804   // and stored atomically.
1805   //
1806   address generate_conjoint_short_copy(bool aligned, address nooverlap_target,
1807                                        address *entry, const char *name) {
1808     __ align(CodeEntryAlignment);
1809     StubCodeMark mark(this, "StubRoutines", name);
1810     address start = __ pc();
1811 
1812     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes;
1813     const Register from        = rdi;  // source array address
1814     const Register to          = rsi;  // destination array address
1815     const Register count       = rdx;  // elements count
1816     const Register word_count  = rcx;
1817     const Register qword_count = count;
1818 
1819     __ enter(); // required for proper stackwalking of RuntimeStub frame
1820     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1821 
1822     if (entry != NULL) {
1823       *entry = __ pc();
1824       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1825       BLOCK_COMMENT("Entry:");
1826     }
1827 
1828     array_overlap_test(nooverlap_target, Address::times_2);
1829     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1830                       // r9 and r10 may be used to save non-volatile registers
1831 
1832     // 'from', 'to' and 'count' are now valid
1833     __ movptr(word_count, count);
1834     __ shrptr(count, 2); // count => qword_count
1835 
1836     // Copy from high to low addresses.  Use 'to' as scratch.
1837 
1838     // Check for and copy trailing word
1839     __ testl(word_count, 1);
1840     __ jccb(Assembler::zero, L_copy_4_bytes);
1841     __ movw(rax, Address(from, word_count, Address::times_2, -2));
1842     __ movw(Address(to, word_count, Address::times_2, -2), rax);
1843 
1844     // Check for and copy trailing dword
1845   __ BIND(L_copy_4_bytes);
1846     __ testl(word_count, 2);
1847     __ jcc(Assembler::zero, L_copy_bytes);
1848     __ movl(rax, Address(from, qword_count, Address::times_8));
1849     __ movl(Address(to, qword_count, Address::times_8), rax);
1850     __ jmp(L_copy_bytes);
1851 
1852     // Copy trailing qwords
1853   __ BIND(L_copy_8_bytes);
1854     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
1855     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
1856     __ decrement(qword_count);
1857     __ jcc(Assembler::notZero, L_copy_8_bytes);
1858 
1859     restore_arg_regs();
1860     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); // Update counter after rscratch1 is free
1861     __ xorptr(rax, rax); // return 0
1862     __ leave(); // required for proper stackwalking of RuntimeStub frame
1863     __ ret(0);
1864 
1865     // Copy in multi-bytes chunks
1866     copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1867 
1868     restore_arg_regs();
1869     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); // Update counter after rscratch1 is free
1870     __ xorptr(rax, rax); // return 0
1871     __ leave(); // required for proper stackwalking of RuntimeStub frame
1872     __ ret(0);
1873 
1874     return start;
1875   }
1876 
1877   // Arguments:
1878   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1879   //             ignored
1880   //   is_oop  - true => oop array, so generate store check code
1881   //   name    - stub name string
1882   //
1883   // Inputs:
1884   //   c_rarg0   - source array address
1885   //   c_rarg1   - destination array address
1886   //   c_rarg2   - element count, treated as ssize_t, can be zero
1887   //
1888   // If 'from' and/or 'to' are aligned on 4-byte boundaries, we let
1889   // the hardware handle it.  The two dwords within qwords that span
1890   // cache line boundaries will still be loaded and stored atomicly.
1891   //
1892   // Side Effects:
1893   //   disjoint_int_copy_entry is set to the no-overlap entry point
1894   //   used by generate_conjoint_int_oop_copy().
1895   //
1896   address generate_disjoint_int_oop_copy(bool aligned, bool is_oop, address* entry,
1897                                          const char *name, bool dest_uninitialized = false) {
1898     __ align(CodeEntryAlignment);
1899     StubCodeMark mark(this, "StubRoutines", name);
1900     address start = __ pc();
1901 
1902     Label L_copy_bytes, L_copy_8_bytes, L_copy_4_bytes, L_exit;
1903     const Register from        = rdi;  // source array address
1904     const Register to          = rsi;  // destination array address
1905     const Register count       = rdx;  // elements count
1906     const Register dword_count = rcx;
1907     const Register qword_count = count;
1908     const Register end_from    = from; // source array end address
1909     const Register end_to      = to;   // destination array end address
1910     const Register saved_to    = r11;  // saved destination array address
1911     // End pointers are inclusive, and if count is not zero they point
1912     // to the last unit copied:  end_to[0] := end_from[0]
1913 
1914     __ enter(); // required for proper stackwalking of RuntimeStub frame
1915     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1916 
1917     if (entry != NULL) {
1918       *entry = __ pc();
1919       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1920       BLOCK_COMMENT("Entry:");
1921     }
1922 
1923     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1924                       // r9 and r10 may be used to save non-volatile registers
1925     if (is_oop) {
1926       __ movq(saved_to, to);
1927       gen_write_ref_array_pre_barrier(to, count, dest_uninitialized);
1928     }
1929 
1930     // 'from', 'to' and 'count' are now valid
1931     __ movptr(dword_count, count);
1932     __ shrptr(count, 1); // count => qword_count
1933 
1934     // Copy from low to high addresses.  Use 'to' as scratch.
1935     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1936     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1937     __ negptr(qword_count);
1938     __ jmp(L_copy_bytes);
1939 
1940     // Copy trailing qwords
1941   __ BIND(L_copy_8_bytes);
1942     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
1943     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
1944     __ increment(qword_count);
1945     __ jcc(Assembler::notZero, L_copy_8_bytes);
1946 
1947     // Check for and copy trailing dword
1948   __ BIND(L_copy_4_bytes);
1949     __ testl(dword_count, 1); // Only byte test since the value is 0 or 1
1950     __ jccb(Assembler::zero, L_exit);
1951     __ movl(rax, Address(end_from, 8));
1952     __ movl(Address(end_to, 8), rax);
1953 
1954   __ BIND(L_exit);
1955     if (is_oop) {
1956       gen_write_ref_array_post_barrier(saved_to, dword_count, rax);
1957     }
1958     restore_arg_regs();
1959     inc_counter_np(SharedRuntime::_jint_array_copy_ctr); // Update counter after rscratch1 is free
1960     __ xorptr(rax, rax); // return 0
1961     __ leave(); // required for proper stackwalking of RuntimeStub frame
1962     __ ret(0);
1963 
1964     // Copy in multi-bytes chunks
1965     copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
1966     __ jmp(L_copy_4_bytes);
1967 
1968     return start;
1969   }
1970 
1971   // Arguments:
1972   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1973   //             ignored
1974   //   is_oop  - true => oop array, so generate store check code
1975   //   name    - stub name string
1976   //
1977   // Inputs:
1978   //   c_rarg0   - source array address
1979   //   c_rarg1   - destination array address
1980   //   c_rarg2   - element count, treated as ssize_t, can be zero
1981   //
1982   // If 'from' and/or 'to' are aligned on 4-byte boundaries, we let
1983   // the hardware handle it.  The two dwords within qwords that span
1984   // cache line boundaries will still be loaded and stored atomicly.
1985   //
1986   address generate_conjoint_int_oop_copy(bool aligned, bool is_oop, address nooverlap_target,
1987                                          address *entry, const char *name,
1988                                          bool dest_uninitialized = false) {
1989     __ align(CodeEntryAlignment);
1990     StubCodeMark mark(this, "StubRoutines", name);
1991     address start = __ pc();
1992 
1993     Label L_copy_bytes, L_copy_8_bytes, L_copy_2_bytes, L_exit;
1994     const Register from        = rdi;  // source array address
1995     const Register to          = rsi;  // destination array address
1996     const Register count       = rdx;  // elements count
1997     const Register dword_count = rcx;
1998     const Register qword_count = count;
1999 
2000     __ enter(); // required for proper stackwalking of RuntimeStub frame
2001     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
2002 
2003     if (entry != NULL) {
2004       *entry = __ pc();
2005        // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
2006       BLOCK_COMMENT("Entry:");
2007     }
2008 
2009     array_overlap_test(nooverlap_target, Address::times_4);
2010     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
2011                       // r9 and r10 may be used to save non-volatile registers
2012 
2013     if (is_oop) {
2014       // no registers are destroyed by this call
2015       gen_write_ref_array_pre_barrier(to, count, dest_uninitialized);
2016     }
2017 
2018     assert_clean_int(count, rax); // Make sure 'count' is clean int.
2019     // 'from', 'to' and 'count' are now valid
2020     __ movptr(dword_count, count);
2021     __ shrptr(count, 1); // count => qword_count
2022 
2023     // Copy from high to low addresses.  Use 'to' as scratch.
2024 
2025     // Check for and copy trailing dword
2026     __ testl(dword_count, 1);
2027     __ jcc(Assembler::zero, L_copy_bytes);
2028     __ movl(rax, Address(from, dword_count, Address::times_4, -4));
2029     __ movl(Address(to, dword_count, Address::times_4, -4), rax);
2030     __ jmp(L_copy_bytes);
2031 
2032     // Copy trailing qwords
2033   __ BIND(L_copy_8_bytes);
2034     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
2035     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
2036     __ decrement(qword_count);
2037     __ jcc(Assembler::notZero, L_copy_8_bytes);
2038 
2039     if (is_oop) {
2040       __ jmp(L_exit);
2041     }
2042     restore_arg_regs();
2043     inc_counter_np(SharedRuntime::_jint_array_copy_ctr); // Update counter after rscratch1 is free
2044     __ xorptr(rax, rax); // return 0
2045     __ leave(); // required for proper stackwalking of RuntimeStub frame
2046     __ ret(0);
2047 
2048     // Copy in multi-bytes chunks
2049     copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
2050 
2051   __ BIND(L_exit);
2052     if (is_oop) {
2053       gen_write_ref_array_post_barrier(to, dword_count, rax);
2054     }
2055     restore_arg_regs();
2056     inc_counter_np(SharedRuntime::_jint_array_copy_ctr); // Update counter after rscratch1 is free
2057     __ xorptr(rax, rax); // return 0
2058     __ leave(); // required for proper stackwalking of RuntimeStub frame
2059     __ ret(0);
2060 
2061     return start;
2062   }
2063 
2064   // Arguments:
2065   //   aligned - true => Input and output aligned on a HeapWord boundary == 8 bytes
2066   //             ignored
2067   //   is_oop  - true => oop array, so generate store check code
2068   //   name    - stub name string
2069   //
2070   // Inputs:
2071   //   c_rarg0   - source array address
2072   //   c_rarg1   - destination array address
2073   //   c_rarg2   - element count, treated as ssize_t, can be zero
2074   //
2075  // Side Effects:
2076   //   disjoint_oop_copy_entry or disjoint_long_copy_entry is set to the
2077   //   no-overlap entry point used by generate_conjoint_long_oop_copy().
2078   //
2079   address generate_disjoint_long_oop_copy(bool aligned, bool is_oop, address *entry,
2080                                           const char *name, bool dest_uninitialized = false) {
2081     __ align(CodeEntryAlignment);
2082     StubCodeMark mark(this, "StubRoutines", name);
2083     address start = __ pc();
2084 
2085     Label L_copy_bytes, L_copy_8_bytes, L_exit;
2086     const Register from        = rdi;  // source array address
2087     const Register to          = rsi;  // destination array address
2088     const Register qword_count = rdx;  // elements count
2089     const Register end_from    = from; // source array end address
2090     const Register end_to      = rcx;  // destination array end address
2091     const Register saved_to    = to;
2092     const Register saved_count = r11;
2093     // End pointers are inclusive, and if count is not zero they point
2094     // to the last unit copied:  end_to[0] := end_from[0]
2095 
2096     __ enter(); // required for proper stackwalking of RuntimeStub frame
2097     // Save no-overlap entry point for generate_conjoint_long_oop_copy()
2098     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
2099 
2100     if (entry != NULL) {
2101       *entry = __ pc();
2102       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
2103       BLOCK_COMMENT("Entry:");
2104     }
2105 
2106     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
2107                       // r9 and r10 may be used to save non-volatile registers
2108     // 'from', 'to' and 'qword_count' are now valid
2109     if (is_oop) {
2110       // Save to and count for store barrier
2111       __ movptr(saved_count, qword_count);
2112       // no registers are destroyed by this call
2113       gen_write_ref_array_pre_barrier(to, qword_count, dest_uninitialized);
2114     }
2115 
2116     // Copy from low to high addresses.  Use 'to' as scratch.
2117     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
2118     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
2119     __ negptr(qword_count);
2120     __ jmp(L_copy_bytes);
2121 
2122     // Copy trailing qwords
2123   __ BIND(L_copy_8_bytes);
2124     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
2125     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
2126     __ increment(qword_count);
2127     __ jcc(Assembler::notZero, L_copy_8_bytes);
2128 
2129     if (is_oop) {
2130       __ jmp(L_exit);
2131     } else {
2132       restore_arg_regs();
2133       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
2134       __ xorptr(rax, rax); // return 0
2135       __ leave(); // required for proper stackwalking of RuntimeStub frame
2136       __ ret(0);
2137     }
2138 
2139     // Copy in multi-bytes chunks
2140     copy_bytes_forward(end_from, end_to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
2141 
2142     if (is_oop) {
2143     __ BIND(L_exit);
2144       gen_write_ref_array_post_barrier(saved_to, saved_count, rax);
2145     }
2146     restore_arg_regs();
2147     if (is_oop) {
2148       inc_counter_np(SharedRuntime::_oop_array_copy_ctr); // Update counter after rscratch1 is free
2149     } else {
2150       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
2151     }
2152     __ xorptr(rax, rax); // return 0
2153     __ leave(); // required for proper stackwalking of RuntimeStub frame
2154     __ ret(0);
2155 
2156     return start;
2157   }
2158 
2159   // Arguments:
2160   //   aligned - true => Input and output aligned on a HeapWord boundary == 8 bytes
2161   //             ignored
2162   //   is_oop  - true => oop array, so generate store check code
2163   //   name    - stub name string
2164   //
2165   // Inputs:
2166   //   c_rarg0   - source array address
2167   //   c_rarg1   - destination array address
2168   //   c_rarg2   - element count, treated as ssize_t, can be zero
2169   //
2170   address generate_conjoint_long_oop_copy(bool aligned, bool is_oop,
2171                                           address nooverlap_target, address *entry,
2172                                           const char *name, bool dest_uninitialized = false) {
2173     __ align(CodeEntryAlignment);
2174     StubCodeMark mark(this, "StubRoutines", name);
2175     address start = __ pc();
2176 
2177     Label L_copy_bytes, L_copy_8_bytes, L_exit;
2178     const Register from        = rdi;  // source array address
2179     const Register to          = rsi;  // destination array address
2180     const Register qword_count = rdx;  // elements count
2181     const Register saved_count = rcx;
2182 
2183     __ enter(); // required for proper stackwalking of RuntimeStub frame
2184     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
2185 
2186     if (entry != NULL) {
2187       *entry = __ pc();
2188       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
2189       BLOCK_COMMENT("Entry:");
2190     }
2191 
2192     array_overlap_test(nooverlap_target, Address::times_8);
2193     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
2194                       // r9 and r10 may be used to save non-volatile registers
2195     // 'from', 'to' and 'qword_count' are now valid
2196     if (is_oop) {
2197       // Save to and count for store barrier
2198       __ movptr(saved_count, qword_count);
2199       // No registers are destroyed by this call
2200       gen_write_ref_array_pre_barrier(to, saved_count, dest_uninitialized);
2201     }
2202 
2203     __ jmp(L_copy_bytes);
2204 
2205     // Copy trailing qwords
2206   __ BIND(L_copy_8_bytes);
2207     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
2208     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
2209     __ decrement(qword_count);
2210     __ jcc(Assembler::notZero, L_copy_8_bytes);
2211 
2212     if (is_oop) {
2213       __ jmp(L_exit);
2214     } else {
2215       restore_arg_regs();
2216       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
2217       __ xorptr(rax, rax); // return 0
2218       __ leave(); // required for proper stackwalking of RuntimeStub frame
2219       __ ret(0);
2220     }
2221 
2222     // Copy in multi-bytes chunks
2223     copy_bytes_backward(from, to, qword_count, rax, L_copy_bytes, L_copy_8_bytes);
2224 
2225     if (is_oop) {
2226     __ BIND(L_exit);
2227       gen_write_ref_array_post_barrier(to, saved_count, rax);
2228     }
2229     restore_arg_regs();
2230     if (is_oop) {
2231       inc_counter_np(SharedRuntime::_oop_array_copy_ctr); // Update counter after rscratch1 is free
2232     } else {
2233       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
2234     }
2235     __ xorptr(rax, rax); // return 0
2236     __ leave(); // required for proper stackwalking of RuntimeStub frame
2237     __ ret(0);
2238 
2239     return start;
2240   }
2241 
2242 
2243   // Helper for generating a dynamic type check.
2244   // Smashes no registers.
2245   void generate_type_check(Register sub_klass,
2246                            Register super_check_offset,
2247                            Register super_klass,
2248                            Label& L_success) {
2249     assert_different_registers(sub_klass, super_check_offset, super_klass);
2250 
2251     BLOCK_COMMENT("type_check:");
2252 
2253     Label L_miss;
2254 
2255     __ check_klass_subtype_fast_path(sub_klass, super_klass, noreg,        &L_success, &L_miss, NULL,
2256                                      super_check_offset);
2257     __ check_klass_subtype_slow_path(sub_klass, super_klass, noreg, noreg, &L_success, NULL);
2258 
2259     // Fall through on failure!
2260     __ BIND(L_miss);
2261   }
2262 
2263   //
2264   //  Generate checkcasting array copy stub
2265   //
2266   //  Input:
2267   //    c_rarg0   - source array address
2268   //    c_rarg1   - destination array address
2269   //    c_rarg2   - element count, treated as ssize_t, can be zero
2270   //    c_rarg3   - size_t ckoff (super_check_offset)
2271   // not Win64
2272   //    c_rarg4   - oop ckval (super_klass)
2273   // Win64
2274   //    rsp+40    - oop ckval (super_klass)
2275   //
2276   //  Output:
2277   //    rax ==  0  -  success
2278   //    rax == -1^K - failure, where K is partial transfer count
2279   //
2280   address generate_checkcast_copy(const char *name, address *entry,
2281                                   bool dest_uninitialized = false) {
2282 
2283     Label L_load_element, L_store_element, L_do_card_marks, L_done;
2284 
2285     // Input registers (after setup_arg_regs)
2286     const Register from        = rdi;   // source array address
2287     const Register to          = rsi;   // destination array address
2288     const Register length      = rdx;   // elements count
2289     const Register ckoff       = rcx;   // super_check_offset
2290     const Register ckval       = r8;    // super_klass
2291 
2292     // Registers used as temps (r13, r14 are save-on-entry)
2293     const Register end_from    = from;  // source array end address
2294     const Register end_to      = r13;   // destination array end address
2295     const Register count       = rdx;   // -(count_remaining)
2296     const Register r14_length  = r14;   // saved copy of length
2297     // End pointers are inclusive, and if length is not zero they point
2298     // to the last unit copied:  end_to[0] := end_from[0]
2299 
2300     const Register rax_oop    = rax;    // actual oop copied
2301     const Register r11_klass  = r11;    // oop._klass
2302 
2303     //---------------------------------------------------------------
2304     // Assembler stub will be used for this call to arraycopy
2305     // if the two arrays are subtypes of Object[] but the
2306     // destination array type is not equal to or a supertype
2307     // of the source type.  Each element must be separately
2308     // checked.
2309 
2310     __ align(CodeEntryAlignment);
2311     StubCodeMark mark(this, "StubRoutines", name);
2312     address start = __ pc();
2313 
2314     __ enter(); // required for proper stackwalking of RuntimeStub frame
2315 
2316 #ifdef ASSERT
2317     // caller guarantees that the arrays really are different
2318     // otherwise, we would have to make conjoint checks
2319     { Label L;
2320       array_overlap_test(L, TIMES_OOP);
2321       __ stop("checkcast_copy within a single array");
2322       __ bind(L);
2323     }
2324 #endif //ASSERT
2325 
2326     setup_arg_regs(4); // from => rdi, to => rsi, length => rdx
2327                        // ckoff => rcx, ckval => r8
2328                        // r9 and r10 may be used to save non-volatile registers
2329 #ifdef _WIN64
2330     // last argument (#4) is on stack on Win64
2331     __ movptr(ckval, Address(rsp, 6 * wordSize));
2332 #endif
2333 
2334     // Caller of this entry point must set up the argument registers.
2335     if (entry != NULL) {
2336       *entry = __ pc();
2337       BLOCK_COMMENT("Entry:");
2338     }
2339 
2340     // allocate spill slots for r13, r14
2341     enum {
2342       saved_r13_offset,
2343       saved_r14_offset,
2344       saved_rbp_offset
2345     };
2346     __ subptr(rsp, saved_rbp_offset * wordSize);
2347     __ movptr(Address(rsp, saved_r13_offset * wordSize), r13);
2348     __ movptr(Address(rsp, saved_r14_offset * wordSize), r14);
2349 
2350     // check that int operands are properly extended to size_t
2351     assert_clean_int(length, rax);
2352     assert_clean_int(ckoff, rax);
2353 
2354 #ifdef ASSERT
2355     BLOCK_COMMENT("assert consistent ckoff/ckval");
2356     // The ckoff and ckval must be mutually consistent,
2357     // even though caller generates both.
2358     { Label L;
2359       int sco_offset = in_bytes(Klass::super_check_offset_offset());
2360       __ cmpl(ckoff, Address(ckval, sco_offset));
2361       __ jcc(Assembler::equal, L);
2362       __ stop("super_check_offset inconsistent");
2363       __ bind(L);
2364     }
2365 #endif //ASSERT
2366 
2367     // Loop-invariant addresses.  They are exclusive end pointers.
2368     Address end_from_addr(from, length, TIMES_OOP, 0);
2369     Address   end_to_addr(to,   length, TIMES_OOP, 0);
2370     // Loop-variant addresses.  They assume post-incremented count < 0.
2371     Address from_element_addr(end_from, count, TIMES_OOP, 0);
2372     Address   to_element_addr(end_to,   count, TIMES_OOP, 0);
2373 
2374     gen_write_ref_array_pre_barrier(to, count, dest_uninitialized);
2375 
2376     // Copy from low to high addresses, indexed from the end of each array.
2377     __ lea(end_from, end_from_addr);
2378     __ lea(end_to,   end_to_addr);
2379     __ movptr(r14_length, length);        // save a copy of the length
2380     assert(length == count, "");          // else fix next line:
2381     __ negptr(count);                     // negate and test the length
2382     __ jcc(Assembler::notZero, L_load_element);
2383 
2384     // Empty array:  Nothing to do.
2385     __ xorptr(rax, rax);                  // return 0 on (trivial) success
2386     __ jmp(L_done);
2387 
2388     // ======== begin loop ========
2389     // (Loop is rotated; its entry is L_load_element.)
2390     // Loop control:
2391     //   for (count = -count; count != 0; count++)
2392     // Base pointers src, dst are biased by 8*(count-1),to last element.
2393     __ align(OptoLoopAlignment);
2394 
2395     __ BIND(L_store_element);
2396     __ store_heap_oop(to_element_addr, rax_oop);  // store the oop
2397     __ increment(count);               // increment the count toward zero
2398     __ jcc(Assembler::zero, L_do_card_marks);
2399 
2400     // ======== loop entry is here ========
2401     __ BIND(L_load_element);
2402     __ load_heap_oop(rax_oop, from_element_addr); // load the oop
2403     __ testptr(rax_oop, rax_oop);
2404     __ jcc(Assembler::zero, L_store_element);
2405 
2406     __ load_klass(r11_klass, rax_oop);// query the object klass
2407     generate_type_check(r11_klass, ckoff, ckval, L_store_element);
2408     // ======== end loop ========
2409 
2410     // It was a real error; we must depend on the caller to finish the job.
2411     // Register rdx = -1 * number of *remaining* oops, r14 = *total* oops.
2412     // Emit GC store barriers for the oops we have copied (r14 + rdx),
2413     // and report their number to the caller.
2414     assert_different_registers(rax, r14_length, count, to, end_to, rcx, rscratch1);
2415     Label L_post_barrier;
2416     __ addptr(r14_length, count);     // K = (original - remaining) oops
2417     __ movptr(rax, r14_length);       // save the value
2418     __ notptr(rax);                   // report (-1^K) to caller (does not affect flags)
2419     __ jccb(Assembler::notZero, L_post_barrier);
2420     __ jmp(L_done); // K == 0, nothing was copied, skip post barrier
2421 
2422     // Come here on success only.
2423     __ BIND(L_do_card_marks);
2424     __ xorptr(rax, rax);              // return 0 on success
2425 
2426     __ BIND(L_post_barrier);
2427     gen_write_ref_array_post_barrier(to, r14_length, rscratch1);
2428 
2429     // Common exit point (success or failure).
2430     __ BIND(L_done);
2431     __ movptr(r13, Address(rsp, saved_r13_offset * wordSize));
2432     __ movptr(r14, Address(rsp, saved_r14_offset * wordSize));
2433     restore_arg_regs();
2434     inc_counter_np(SharedRuntime::_checkcast_array_copy_ctr); // Update counter after rscratch1 is free
2435     __ leave(); // required for proper stackwalking of RuntimeStub frame
2436     __ ret(0);
2437 
2438     return start;
2439   }
2440 
2441   //
2442   //  Generate 'unsafe' array copy stub
2443   //  Though just as safe as the other stubs, it takes an unscaled
2444   //  size_t argument instead of an element count.
2445   //
2446   //  Input:
2447   //    c_rarg0   - source array address
2448   //    c_rarg1   - destination array address
2449   //    c_rarg2   - byte count, treated as ssize_t, can be zero
2450   //
2451   // Examines the alignment of the operands and dispatches
2452   // to a long, int, short, or byte copy loop.
2453   //
2454   address generate_unsafe_copy(const char *name,
2455                                address byte_copy_entry, address short_copy_entry,
2456                                address int_copy_entry, address long_copy_entry) {
2457 
2458     Label L_long_aligned, L_int_aligned, L_short_aligned;
2459 
2460     // Input registers (before setup_arg_regs)
2461     const Register from        = c_rarg0;  // source array address
2462     const Register to          = c_rarg1;  // destination array address
2463     const Register size        = c_rarg2;  // byte count (size_t)
2464 
2465     // Register used as a temp
2466     const Register bits        = rax;      // test copy of low bits
2467 
2468     __ align(CodeEntryAlignment);
2469     StubCodeMark mark(this, "StubRoutines", name);
2470     address start = __ pc();
2471 
2472     __ enter(); // required for proper stackwalking of RuntimeStub frame
2473 
2474     // bump this on entry, not on exit:
2475     inc_counter_np(SharedRuntime::_unsafe_array_copy_ctr);
2476 
2477     __ mov(bits, from);
2478     __ orptr(bits, to);
2479     __ orptr(bits, size);
2480 
2481     __ testb(bits, BytesPerLong-1);
2482     __ jccb(Assembler::zero, L_long_aligned);
2483 
2484     __ testb(bits, BytesPerInt-1);
2485     __ jccb(Assembler::zero, L_int_aligned);
2486 
2487     __ testb(bits, BytesPerShort-1);
2488     __ jump_cc(Assembler::notZero, RuntimeAddress(byte_copy_entry));
2489 
2490     __ BIND(L_short_aligned);
2491     __ shrptr(size, LogBytesPerShort); // size => short_count
2492     __ jump(RuntimeAddress(short_copy_entry));
2493 
2494     __ BIND(L_int_aligned);
2495     __ shrptr(size, LogBytesPerInt); // size => int_count
2496     __ jump(RuntimeAddress(int_copy_entry));
2497 
2498     __ BIND(L_long_aligned);
2499     __ shrptr(size, LogBytesPerLong); // size => qword_count
2500     __ jump(RuntimeAddress(long_copy_entry));
2501 
2502     return start;
2503   }
2504 
2505   // Perform range checks on the proposed arraycopy.
2506   // Kills temp, but nothing else.
2507   // Also, clean the sign bits of src_pos and dst_pos.
2508   void arraycopy_range_checks(Register src,     // source array oop (c_rarg0)
2509                               Register src_pos, // source position (c_rarg1)
2510                               Register dst,     // destination array oo (c_rarg2)
2511                               Register dst_pos, // destination position (c_rarg3)
2512                               Register length,
2513                               Register temp,
2514                               Label& L_failed) {
2515     BLOCK_COMMENT("arraycopy_range_checks:");
2516 
2517     //  if (src_pos + length > arrayOop(src)->length())  FAIL;
2518     __ movl(temp, length);
2519     __ addl(temp, src_pos);             // src_pos + length
2520     __ cmpl(temp, Address(src, arrayOopDesc::length_offset_in_bytes()));
2521     __ jcc(Assembler::above, L_failed);
2522 
2523     //  if (dst_pos + length > arrayOop(dst)->length())  FAIL;
2524     __ movl(temp, length);
2525     __ addl(temp, dst_pos);             // dst_pos + length
2526     __ cmpl(temp, Address(dst, arrayOopDesc::length_offset_in_bytes()));
2527     __ jcc(Assembler::above, L_failed);
2528 
2529     // Have to clean up high 32-bits of 'src_pos' and 'dst_pos'.
2530     // Move with sign extension can be used since they are positive.
2531     __ movslq(src_pos, src_pos);
2532     __ movslq(dst_pos, dst_pos);
2533 
2534     BLOCK_COMMENT("arraycopy_range_checks done");
2535   }
2536 
2537   //
2538   //  Generate generic array copy stubs
2539   //
2540   //  Input:
2541   //    c_rarg0    -  src oop
2542   //    c_rarg1    -  src_pos (32-bits)
2543   //    c_rarg2    -  dst oop
2544   //    c_rarg3    -  dst_pos (32-bits)
2545   // not Win64
2546   //    c_rarg4    -  element count (32-bits)
2547   // Win64
2548   //    rsp+40     -  element count (32-bits)
2549   //
2550   //  Output:
2551   //    rax ==  0  -  success
2552   //    rax == -1^K - failure, where K is partial transfer count
2553   //
2554   address generate_generic_copy(const char *name,
2555                                 address byte_copy_entry, address short_copy_entry,
2556                                 address int_copy_entry, address oop_copy_entry,
2557                                 address long_copy_entry, address checkcast_copy_entry) {
2558 
2559     Label L_failed, L_failed_0, L_objArray;
2560     Label L_copy_bytes, L_copy_shorts, L_copy_ints, L_copy_longs;
2561 
2562     // Input registers
2563     const Register src        = c_rarg0;  // source array oop
2564     const Register src_pos    = c_rarg1;  // source position
2565     const Register dst        = c_rarg2;  // destination array oop
2566     const Register dst_pos    = c_rarg3;  // destination position
2567 #ifndef _WIN64
2568     const Register length     = c_rarg4;
2569 #else
2570     const Address  length(rsp, 6 * wordSize);  // elements count is on stack on Win64
2571 #endif
2572 
2573     { int modulus = CodeEntryAlignment;
2574       int target  = modulus - 5; // 5 = sizeof jmp(L_failed)
2575       int advance = target - (__ offset() % modulus);
2576       if (advance < 0)  advance += modulus;
2577       if (advance > 0)  __ nop(advance);
2578     }
2579     StubCodeMark mark(this, "StubRoutines", name);
2580 
2581     // Short-hop target to L_failed.  Makes for denser prologue code.
2582     __ BIND(L_failed_0);
2583     __ jmp(L_failed);
2584     assert(__ offset() % CodeEntryAlignment == 0, "no further alignment needed");
2585 
2586     __ align(CodeEntryAlignment);
2587     address start = __ pc();
2588 
2589     __ enter(); // required for proper stackwalking of RuntimeStub frame
2590 
2591     // bump this on entry, not on exit:
2592     inc_counter_np(SharedRuntime::_generic_array_copy_ctr);
2593 
2594     //-----------------------------------------------------------------------
2595     // Assembler stub will be used for this call to arraycopy
2596     // if the following conditions are met:
2597     //
2598     // (1) src and dst must not be null.
2599     // (2) src_pos must not be negative.
2600     // (3) dst_pos must not be negative.
2601     // (4) length  must not be negative.
2602     // (5) src klass and dst klass should be the same and not NULL.
2603     // (6) src and dst should be arrays.
2604     // (7) src_pos + length must not exceed length of src.
2605     // (8) dst_pos + length must not exceed length of dst.
2606     //
2607 
2608     //  if (src == NULL) return -1;
2609     __ testptr(src, src);         // src oop
2610     size_t j1off = __ offset();
2611     __ jccb(Assembler::zero, L_failed_0);
2612 
2613     //  if (src_pos < 0) return -1;
2614     __ testl(src_pos, src_pos); // src_pos (32-bits)
2615     __ jccb(Assembler::negative, L_failed_0);
2616 
2617     //  if (dst == NULL) return -1;
2618     __ testptr(dst, dst);         // dst oop
2619     __ jccb(Assembler::zero, L_failed_0);
2620 
2621     //  if (dst_pos < 0) return -1;
2622     __ testl(dst_pos, dst_pos); // dst_pos (32-bits)
2623     size_t j4off = __ offset();
2624     __ jccb(Assembler::negative, L_failed_0);
2625 
2626     // The first four tests are very dense code,
2627     // but not quite dense enough to put four
2628     // jumps in a 16-byte instruction fetch buffer.
2629     // That's good, because some branch predicters
2630     // do not like jumps so close together.
2631     // Make sure of this.
2632     guarantee(((j1off ^ j4off) & ~15) != 0, "I$ line of 1st & 4th jumps");
2633 
2634     // registers used as temp
2635     const Register r11_length    = r11; // elements count to copy
2636     const Register r10_src_klass = r10; // array klass
2637 
2638     //  if (length < 0) return -1;
2639     __ movl(r11_length, length);        // length (elements count, 32-bits value)
2640     __ testl(r11_length, r11_length);
2641     __ jccb(Assembler::negative, L_failed_0);
2642 
2643     __ load_klass(r10_src_klass, src);
2644 #ifdef ASSERT
2645     //  assert(src->klass() != NULL);
2646     {
2647       BLOCK_COMMENT("assert klasses not null {");
2648       Label L1, L2;
2649       __ testptr(r10_src_klass, r10_src_klass);
2650       __ jcc(Assembler::notZero, L2);   // it is broken if klass is NULL
2651       __ bind(L1);
2652       __ stop("broken null klass");
2653       __ bind(L2);
2654       __ load_klass(rax, dst);
2655       __ cmpq(rax, 0);
2656       __ jcc(Assembler::equal, L1);     // this would be broken also
2657       BLOCK_COMMENT("} assert klasses not null done");
2658     }
2659 #endif
2660 
2661     // Load layout helper (32-bits)
2662     //
2663     //  |array_tag|     | header_size | element_type |     |log2_element_size|
2664     // 32        30    24            16              8     2                 0
2665     //
2666     //   array_tag: typeArray = 0x3, objArray = 0x2, non-array = 0x0
2667     //
2668 
2669     const int lh_offset = in_bytes(Klass::layout_helper_offset());
2670 
2671     // Handle objArrays completely differently...
2672     const jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
2673     __ cmpl(Address(r10_src_klass, lh_offset), objArray_lh);
2674     __ jcc(Assembler::equal, L_objArray);
2675 
2676     //  if (src->klass() != dst->klass()) return -1;
2677     __ load_klass(rax, dst);
2678     __ cmpq(r10_src_klass, rax);
2679     __ jcc(Assembler::notEqual, L_failed);
2680 
2681     const Register rax_lh = rax;  // layout helper
2682     __ movl(rax_lh, Address(r10_src_klass, lh_offset));
2683 
2684     //  if (!src->is_Array()) return -1;
2685     __ cmpl(rax_lh, Klass::_lh_neutral_value);
2686     __ jcc(Assembler::greaterEqual, L_failed);
2687 
2688     // At this point, it is known to be a typeArray (array_tag 0x3).
2689 #ifdef ASSERT
2690     {
2691       BLOCK_COMMENT("assert primitive array {");
2692       Label L;
2693       __ cmpl(rax_lh, (Klass::_lh_array_tag_type_value << Klass::_lh_array_tag_shift));
2694       __ jcc(Assembler::greaterEqual, L);
2695       __ stop("must be a primitive array");
2696       __ bind(L);
2697       BLOCK_COMMENT("} assert primitive array done");
2698     }
2699 #endif
2700 
2701     arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
2702                            r10, L_failed);
2703 
2704     // TypeArrayKlass
2705     //
2706     // src_addr = (src + array_header_in_bytes()) + (src_pos << log2elemsize);
2707     // dst_addr = (dst + array_header_in_bytes()) + (dst_pos << log2elemsize);
2708     //
2709 
2710     const Register r10_offset = r10;    // array offset
2711     const Register rax_elsize = rax_lh; // element size
2712 
2713     __ movl(r10_offset, rax_lh);
2714     __ shrl(r10_offset, Klass::_lh_header_size_shift);
2715     __ andptr(r10_offset, Klass::_lh_header_size_mask);   // array_offset
2716     __ addptr(src, r10_offset);           // src array offset
2717     __ addptr(dst, r10_offset);           // dst array offset
2718     BLOCK_COMMENT("choose copy loop based on element size");
2719     __ andl(rax_lh, Klass::_lh_log2_element_size_mask); // rax_lh -> rax_elsize
2720 
2721     // next registers should be set before the jump to corresponding stub
2722     const Register from     = c_rarg0;  // source array address
2723     const Register to       = c_rarg1;  // destination array address
2724     const Register count    = c_rarg2;  // elements count
2725 
2726     // 'from', 'to', 'count' registers should be set in such order
2727     // since they are the same as 'src', 'src_pos', 'dst'.
2728 
2729   __ BIND(L_copy_bytes);
2730     __ cmpl(rax_elsize, 0);
2731     __ jccb(Assembler::notEqual, L_copy_shorts);
2732     __ lea(from, Address(src, src_pos, Address::times_1, 0));// src_addr
2733     __ lea(to,   Address(dst, dst_pos, Address::times_1, 0));// dst_addr
2734     __ movl2ptr(count, r11_length); // length
2735     __ jump(RuntimeAddress(byte_copy_entry));
2736 
2737   __ BIND(L_copy_shorts);
2738     __ cmpl(rax_elsize, LogBytesPerShort);
2739     __ jccb(Assembler::notEqual, L_copy_ints);
2740     __ lea(from, Address(src, src_pos, Address::times_2, 0));// src_addr
2741     __ lea(to,   Address(dst, dst_pos, Address::times_2, 0));// dst_addr
2742     __ movl2ptr(count, r11_length); // length
2743     __ jump(RuntimeAddress(short_copy_entry));
2744 
2745   __ BIND(L_copy_ints);
2746     __ cmpl(rax_elsize, LogBytesPerInt);
2747     __ jccb(Assembler::notEqual, L_copy_longs);
2748     __ lea(from, Address(src, src_pos, Address::times_4, 0));// src_addr
2749     __ lea(to,   Address(dst, dst_pos, Address::times_4, 0));// dst_addr
2750     __ movl2ptr(count, r11_length); // length
2751     __ jump(RuntimeAddress(int_copy_entry));
2752 
2753   __ BIND(L_copy_longs);
2754 #ifdef ASSERT
2755     {
2756       BLOCK_COMMENT("assert long copy {");
2757       Label L;
2758       __ cmpl(rax_elsize, LogBytesPerLong);
2759       __ jcc(Assembler::equal, L);
2760       __ stop("must be long copy, but elsize is wrong");
2761       __ bind(L);
2762       BLOCK_COMMENT("} assert long copy done");
2763     }
2764 #endif
2765     __ lea(from, Address(src, src_pos, Address::times_8, 0));// src_addr
2766     __ lea(to,   Address(dst, dst_pos, Address::times_8, 0));// dst_addr
2767     __ movl2ptr(count, r11_length); // length
2768     __ jump(RuntimeAddress(long_copy_entry));
2769 
2770     // ObjArrayKlass
2771   __ BIND(L_objArray);
2772     // live at this point:  r10_src_klass, r11_length, src[_pos], dst[_pos]
2773 
2774     Label L_plain_copy, L_checkcast_copy;
2775     //  test array classes for subtyping
2776     __ load_klass(rax, dst);
2777     __ cmpq(r10_src_klass, rax); // usual case is exact equality
2778     __ jcc(Assembler::notEqual, L_checkcast_copy);
2779 
2780     // Identically typed arrays can be copied without element-wise checks.
2781     arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
2782                            r10, L_failed);
2783 
2784     __ lea(from, Address(src, src_pos, TIMES_OOP,
2785                  arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // src_addr
2786     __ lea(to,   Address(dst, dst_pos, TIMES_OOP,
2787                  arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // dst_addr
2788     __ movl2ptr(count, r11_length); // length
2789   __ BIND(L_plain_copy);
2790     __ jump(RuntimeAddress(oop_copy_entry));
2791 
2792   __ BIND(L_checkcast_copy);
2793     // live at this point:  r10_src_klass, r11_length, rax (dst_klass)
2794     {
2795       // Before looking at dst.length, make sure dst is also an objArray.
2796       __ cmpl(Address(rax, lh_offset), objArray_lh);
2797       __ jcc(Assembler::notEqual, L_failed);
2798 
2799       // It is safe to examine both src.length and dst.length.
2800       arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
2801                              rax, L_failed);
2802 
2803       const Register r11_dst_klass = r11;
2804       __ load_klass(r11_dst_klass, dst); // reload
2805 
2806       // Marshal the base address arguments now, freeing registers.
2807       __ lea(from, Address(src, src_pos, TIMES_OOP,
2808                    arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
2809       __ lea(to,   Address(dst, dst_pos, TIMES_OOP,
2810                    arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
2811       __ movl(count, length);           // length (reloaded)
2812       Register sco_temp = c_rarg3;      // this register is free now
2813       assert_different_registers(from, to, count, sco_temp,
2814                                  r11_dst_klass, r10_src_klass);
2815       assert_clean_int(count, sco_temp);
2816 
2817       // Generate the type check.
2818       const int sco_offset = in_bytes(Klass::super_check_offset_offset());
2819       __ movl(sco_temp, Address(r11_dst_klass, sco_offset));
2820       assert_clean_int(sco_temp, rax);
2821       generate_type_check(r10_src_klass, sco_temp, r11_dst_klass, L_plain_copy);
2822 
2823       // Fetch destination element klass from the ObjArrayKlass header.
2824       int ek_offset = in_bytes(ObjArrayKlass::element_klass_offset());
2825       __ movptr(r11_dst_klass, Address(r11_dst_klass, ek_offset));
2826       __ movl(  sco_temp,      Address(r11_dst_klass, sco_offset));
2827       assert_clean_int(sco_temp, rax);
2828 
2829       // the checkcast_copy loop needs two extra arguments:
2830       assert(c_rarg3 == sco_temp, "#3 already in place");
2831       // Set up arguments for checkcast_copy_entry.
2832       setup_arg_regs(4);
2833       __ movptr(r8, r11_dst_klass);  // dst.klass.element_klass, r8 is c_rarg4 on Linux/Solaris
2834       __ jump(RuntimeAddress(checkcast_copy_entry));
2835     }
2836 
2837   __ BIND(L_failed);
2838     __ xorptr(rax, rax);
2839     __ notptr(rax); // return -1
2840     __ leave();   // required for proper stackwalking of RuntimeStub frame
2841     __ ret(0);
2842 
2843     return start;
2844   }
2845 
2846   void generate_arraycopy_stubs() {
2847     address entry;
2848     address entry_jbyte_arraycopy;
2849     address entry_jshort_arraycopy;
2850     address entry_jint_arraycopy;
2851     address entry_oop_arraycopy;
2852     address entry_jlong_arraycopy;
2853     address entry_checkcast_arraycopy;
2854 
2855     StubRoutines::_jbyte_disjoint_arraycopy  = generate_disjoint_byte_copy(false, &entry,
2856                                                                            "jbyte_disjoint_arraycopy");
2857     StubRoutines::_jbyte_arraycopy           = generate_conjoint_byte_copy(false, entry, &entry_jbyte_arraycopy,
2858                                                                            "jbyte_arraycopy");
2859 
2860     StubRoutines::_jshort_disjoint_arraycopy = generate_disjoint_short_copy(false, &entry,
2861                                                                             "jshort_disjoint_arraycopy");
2862     StubRoutines::_jshort_arraycopy          = generate_conjoint_short_copy(false, entry, &entry_jshort_arraycopy,
2863                                                                             "jshort_arraycopy");
2864 
2865     StubRoutines::_jint_disjoint_arraycopy   = generate_disjoint_int_oop_copy(false, false, &entry,
2866                                                                               "jint_disjoint_arraycopy");
2867     StubRoutines::_jint_arraycopy            = generate_conjoint_int_oop_copy(false, false, entry,
2868                                                                               &entry_jint_arraycopy, "jint_arraycopy");
2869 
2870     StubRoutines::_jlong_disjoint_arraycopy  = generate_disjoint_long_oop_copy(false, false, &entry,
2871                                                                                "jlong_disjoint_arraycopy");
2872     StubRoutines::_jlong_arraycopy           = generate_conjoint_long_oop_copy(false, false, entry,
2873                                                                                &entry_jlong_arraycopy, "jlong_arraycopy");
2874 
2875 
2876     if (UseCompressedOops) {
2877       StubRoutines::_oop_disjoint_arraycopy  = generate_disjoint_int_oop_copy(false, true, &entry,
2878                                                                               "oop_disjoint_arraycopy");
2879       StubRoutines::_oop_arraycopy           = generate_conjoint_int_oop_copy(false, true, entry,
2880                                                                               &entry_oop_arraycopy, "oop_arraycopy");
2881       StubRoutines::_oop_disjoint_arraycopy_uninit  = generate_disjoint_int_oop_copy(false, true, &entry,
2882                                                                                      "oop_disjoint_arraycopy_uninit",
2883                                                                                      /*dest_uninitialized*/true);
2884       StubRoutines::_oop_arraycopy_uninit           = generate_conjoint_int_oop_copy(false, true, entry,
2885                                                                                      NULL, "oop_arraycopy_uninit",
2886                                                                                      /*dest_uninitialized*/true);
2887     } else {
2888       StubRoutines::_oop_disjoint_arraycopy  = generate_disjoint_long_oop_copy(false, true, &entry,
2889                                                                                "oop_disjoint_arraycopy");
2890       StubRoutines::_oop_arraycopy           = generate_conjoint_long_oop_copy(false, true, entry,
2891                                                                                &entry_oop_arraycopy, "oop_arraycopy");
2892       StubRoutines::_oop_disjoint_arraycopy_uninit  = generate_disjoint_long_oop_copy(false, true, &entry,
2893                                                                                       "oop_disjoint_arraycopy_uninit",
2894                                                                                       /*dest_uninitialized*/true);
2895       StubRoutines::_oop_arraycopy_uninit           = generate_conjoint_long_oop_copy(false, true, entry,
2896                                                                                       NULL, "oop_arraycopy_uninit",
2897                                                                                       /*dest_uninitialized*/true);
2898     }
2899 
2900     StubRoutines::_checkcast_arraycopy        = generate_checkcast_copy("checkcast_arraycopy", &entry_checkcast_arraycopy);
2901     StubRoutines::_checkcast_arraycopy_uninit = generate_checkcast_copy("checkcast_arraycopy_uninit", NULL,
2902                                                                         /*dest_uninitialized*/true);
2903 
2904     StubRoutines::_unsafe_arraycopy    = generate_unsafe_copy("unsafe_arraycopy",
2905                                                               entry_jbyte_arraycopy,
2906                                                               entry_jshort_arraycopy,
2907                                                               entry_jint_arraycopy,
2908                                                               entry_jlong_arraycopy);
2909     StubRoutines::_generic_arraycopy   = generate_generic_copy("generic_arraycopy",
2910                                                                entry_jbyte_arraycopy,
2911                                                                entry_jshort_arraycopy,
2912                                                                entry_jint_arraycopy,
2913                                                                entry_oop_arraycopy,
2914                                                                entry_jlong_arraycopy,
2915                                                                entry_checkcast_arraycopy);
2916 
2917     StubRoutines::_jbyte_fill = generate_fill(T_BYTE, false, "jbyte_fill");
2918     StubRoutines::_jshort_fill = generate_fill(T_SHORT, false, "jshort_fill");
2919     StubRoutines::_jint_fill = generate_fill(T_INT, false, "jint_fill");
2920     StubRoutines::_arrayof_jbyte_fill = generate_fill(T_BYTE, true, "arrayof_jbyte_fill");
2921     StubRoutines::_arrayof_jshort_fill = generate_fill(T_SHORT, true, "arrayof_jshort_fill");
2922     StubRoutines::_arrayof_jint_fill = generate_fill(T_INT, true, "arrayof_jint_fill");
2923 
2924     // We don't generate specialized code for HeapWord-aligned source
2925     // arrays, so just use the code we've already generated
2926     StubRoutines::_arrayof_jbyte_disjoint_arraycopy  = StubRoutines::_jbyte_disjoint_arraycopy;
2927     StubRoutines::_arrayof_jbyte_arraycopy           = StubRoutines::_jbyte_arraycopy;
2928 
2929     StubRoutines::_arrayof_jshort_disjoint_arraycopy = StubRoutines::_jshort_disjoint_arraycopy;
2930     StubRoutines::_arrayof_jshort_arraycopy          = StubRoutines::_jshort_arraycopy;
2931 
2932     StubRoutines::_arrayof_jint_disjoint_arraycopy   = StubRoutines::_jint_disjoint_arraycopy;
2933     StubRoutines::_arrayof_jint_arraycopy            = StubRoutines::_jint_arraycopy;
2934 
2935     StubRoutines::_arrayof_jlong_disjoint_arraycopy  = StubRoutines::_jlong_disjoint_arraycopy;
2936     StubRoutines::_arrayof_jlong_arraycopy           = StubRoutines::_jlong_arraycopy;
2937 
2938     StubRoutines::_arrayof_oop_disjoint_arraycopy    = StubRoutines::_oop_disjoint_arraycopy;
2939     StubRoutines::_arrayof_oop_arraycopy             = StubRoutines::_oop_arraycopy;
2940 
2941     StubRoutines::_arrayof_oop_disjoint_arraycopy_uninit    = StubRoutines::_oop_disjoint_arraycopy_uninit;
2942     StubRoutines::_arrayof_oop_arraycopy_uninit             = StubRoutines::_oop_arraycopy_uninit;
2943   }
2944 
2945   void generate_math_stubs() {
2946     {
2947       StubCodeMark mark(this, "StubRoutines", "log");
2948       StubRoutines::_intrinsic_log = (double (*)(double)) __ pc();
2949 
2950       __ subq(rsp, 8);
2951       __ movdbl(Address(rsp, 0), xmm0);
2952       __ fld_d(Address(rsp, 0));
2953       __ flog();
2954       __ fstp_d(Address(rsp, 0));
2955       __ movdbl(xmm0, Address(rsp, 0));
2956       __ addq(rsp, 8);
2957       __ ret(0);
2958     }
2959     {
2960       StubCodeMark mark(this, "StubRoutines", "log10");
2961       StubRoutines::_intrinsic_log10 = (double (*)(double)) __ pc();
2962 
2963       __ subq(rsp, 8);
2964       __ movdbl(Address(rsp, 0), xmm0);
2965       __ fld_d(Address(rsp, 0));
2966       __ flog10();
2967       __ fstp_d(Address(rsp, 0));
2968       __ movdbl(xmm0, Address(rsp, 0));
2969       __ addq(rsp, 8);
2970       __ ret(0);
2971     }
2972     {
2973       StubCodeMark mark(this, "StubRoutines", "sin");
2974       StubRoutines::_intrinsic_sin = (double (*)(double)) __ pc();
2975 
2976       __ subq(rsp, 8);
2977       __ movdbl(Address(rsp, 0), xmm0);
2978       __ fld_d(Address(rsp, 0));
2979       __ trigfunc('s');
2980       __ fstp_d(Address(rsp, 0));
2981       __ movdbl(xmm0, Address(rsp, 0));
2982       __ addq(rsp, 8);
2983       __ ret(0);
2984     }
2985     {
2986       StubCodeMark mark(this, "StubRoutines", "cos");
2987       StubRoutines::_intrinsic_cos = (double (*)(double)) __ pc();
2988 
2989       __ subq(rsp, 8);
2990       __ movdbl(Address(rsp, 0), xmm0);
2991       __ fld_d(Address(rsp, 0));
2992       __ trigfunc('c');
2993       __ fstp_d(Address(rsp, 0));
2994       __ movdbl(xmm0, Address(rsp, 0));
2995       __ addq(rsp, 8);
2996       __ ret(0);
2997     }
2998     {
2999       StubCodeMark mark(this, "StubRoutines", "tan");
3000       StubRoutines::_intrinsic_tan = (double (*)(double)) __ pc();
3001 
3002       __ subq(rsp, 8);
3003       __ movdbl(Address(rsp, 0), xmm0);
3004       __ fld_d(Address(rsp, 0));
3005       __ trigfunc('t');
3006       __ fstp_d(Address(rsp, 0));
3007       __ movdbl(xmm0, Address(rsp, 0));
3008       __ addq(rsp, 8);
3009       __ ret(0);
3010     }
3011     {
3012       StubCodeMark mark(this, "StubRoutines", "exp");
3013       StubRoutines::_intrinsic_exp = (double (*)(double)) __ pc();
3014 
3015       __ subq(rsp, 8);
3016       __ movdbl(Address(rsp, 0), xmm0);
3017       __ fld_d(Address(rsp, 0));
3018       __ exp_with_fallback(0);
3019       __ fstp_d(Address(rsp, 0));
3020       __ movdbl(xmm0, Address(rsp, 0));
3021       __ addq(rsp, 8);
3022       __ ret(0);
3023     }
3024     {
3025       StubCodeMark mark(this, "StubRoutines", "pow");
3026       StubRoutines::_intrinsic_pow = (double (*)(double,double)) __ pc();
3027 
3028       __ subq(rsp, 8);
3029       __ movdbl(Address(rsp, 0), xmm1);
3030       __ fld_d(Address(rsp, 0));
3031       __ movdbl(Address(rsp, 0), xmm0);
3032       __ fld_d(Address(rsp, 0));
3033       __ pow_with_fallback(0);
3034       __ fstp_d(Address(rsp, 0));
3035       __ movdbl(xmm0, Address(rsp, 0));
3036       __ addq(rsp, 8);
3037       __ ret(0);
3038     }
3039   }
3040 
3041   // AES intrinsic stubs
3042   enum {AESBlockSize = 16};
3043 
3044   address generate_key_shuffle_mask() {
3045     __ align(16);
3046     StubCodeMark mark(this, "StubRoutines", "key_shuffle_mask");
3047     address start = __ pc();
3048     __ emit_data64( 0x0405060700010203, relocInfo::none );
3049     __ emit_data64( 0x0c0d0e0f08090a0b, relocInfo::none );
3050     return start;
3051   }
3052 
3053   // Utility routine for loading a 128-bit key word in little endian format
3054   // can optionally specify that the shuffle mask is already in an xmmregister
3055   void load_key(XMMRegister xmmdst, Register key, int offset, XMMRegister xmm_shuf_mask=NULL) {
3056     __ movdqu(xmmdst, Address(key, offset));
3057     if (xmm_shuf_mask != NULL) {
3058       __ pshufb(xmmdst, xmm_shuf_mask);
3059     } else {
3060       __ pshufb(xmmdst, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
3061     }
3062   }
3063 
3064   // Arguments:
3065   //
3066   // Inputs:
3067   //   c_rarg0   - source byte array address
3068   //   c_rarg1   - destination byte array address
3069   //   c_rarg2   - K (key) in little endian int array
3070   //
3071   address generate_aescrypt_encryptBlock() {
3072     assert(UseAES, "need AES instructions and misaligned SSE support");
3073     __ align(CodeEntryAlignment);
3074     StubCodeMark mark(this, "StubRoutines", "aescrypt_encryptBlock");
3075     Label L_doLast;
3076     address start = __ pc();
3077 
3078     const Register from        = c_rarg0;  // source array address
3079     const Register to          = c_rarg1;  // destination array address
3080     const Register key         = c_rarg2;  // key array address
3081     const Register keylen      = rax;
3082 
3083     const XMMRegister xmm_result = xmm0;
3084     const XMMRegister xmm_key_shuf_mask = xmm1;
3085     // On win64 xmm6-xmm15 must be preserved so don't use them.
3086     const XMMRegister xmm_temp1  = xmm2;
3087     const XMMRegister xmm_temp2  = xmm3;
3088     const XMMRegister xmm_temp3  = xmm4;
3089     const XMMRegister xmm_temp4  = xmm5;
3090 
3091     __ enter(); // required for proper stackwalking of RuntimeStub frame
3092 
3093     // keylen could be only {11, 13, 15} * 4 = {44, 52, 60}
3094     __ movl(keylen, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
3095 
3096     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
3097     __ movdqu(xmm_result, Address(from, 0));  // get 16 bytes of input
3098 
3099     // For encryption, the java expanded key ordering is just what we need
3100     // we don't know if the key is aligned, hence not using load-execute form
3101 
3102     load_key(xmm_temp1, key, 0x00, xmm_key_shuf_mask);
3103     __ pxor(xmm_result, xmm_temp1);
3104 
3105     load_key(xmm_temp1, key, 0x10, xmm_key_shuf_mask);
3106     load_key(xmm_temp2, key, 0x20, xmm_key_shuf_mask);
3107     load_key(xmm_temp3, key, 0x30, xmm_key_shuf_mask);
3108     load_key(xmm_temp4, key, 0x40, xmm_key_shuf_mask);
3109 
3110     __ aesenc(xmm_result, xmm_temp1);
3111     __ aesenc(xmm_result, xmm_temp2);
3112     __ aesenc(xmm_result, xmm_temp3);
3113     __ aesenc(xmm_result, xmm_temp4);
3114 
3115     load_key(xmm_temp1, key, 0x50, xmm_key_shuf_mask);
3116     load_key(xmm_temp2, key, 0x60, xmm_key_shuf_mask);
3117     load_key(xmm_temp3, key, 0x70, xmm_key_shuf_mask);
3118     load_key(xmm_temp4, key, 0x80, xmm_key_shuf_mask);
3119 
3120     __ aesenc(xmm_result, xmm_temp1);
3121     __ aesenc(xmm_result, xmm_temp2);
3122     __ aesenc(xmm_result, xmm_temp3);
3123     __ aesenc(xmm_result, xmm_temp4);
3124 
3125     load_key(xmm_temp1, key, 0x90, xmm_key_shuf_mask);
3126     load_key(xmm_temp2, key, 0xa0, xmm_key_shuf_mask);
3127 
3128     __ cmpl(keylen, 44);
3129     __ jccb(Assembler::equal, L_doLast);
3130 
3131     __ aesenc(xmm_result, xmm_temp1);
3132     __ aesenc(xmm_result, xmm_temp2);
3133 
3134     load_key(xmm_temp1, key, 0xb0, xmm_key_shuf_mask);
3135     load_key(xmm_temp2, key, 0xc0, xmm_key_shuf_mask);
3136 
3137     __ cmpl(keylen, 52);
3138     __ jccb(Assembler::equal, L_doLast);
3139 
3140     __ aesenc(xmm_result, xmm_temp1);
3141     __ aesenc(xmm_result, xmm_temp2);
3142 
3143     load_key(xmm_temp1, key, 0xd0, xmm_key_shuf_mask);
3144     load_key(xmm_temp2, key, 0xe0, xmm_key_shuf_mask);
3145 
3146     __ BIND(L_doLast);
3147     __ aesenc(xmm_result, xmm_temp1);
3148     __ aesenclast(xmm_result, xmm_temp2);
3149     __ movdqu(Address(to, 0), xmm_result);        // store the result
3150     __ xorptr(rax, rax); // return 0
3151     __ leave(); // required for proper stackwalking of RuntimeStub frame
3152     __ ret(0);
3153 
3154     return start;
3155   }
3156 
3157 
3158   // Arguments:
3159   //
3160   // Inputs:
3161   //   c_rarg0   - source byte array address
3162   //   c_rarg1   - destination byte array address
3163   //   c_rarg2   - K (key) in little endian int array
3164   //
3165   address generate_aescrypt_decryptBlock() {
3166     assert(UseAES, "need AES instructions and misaligned SSE support");
3167     __ align(CodeEntryAlignment);
3168     StubCodeMark mark(this, "StubRoutines", "aescrypt_decryptBlock");
3169     Label L_doLast;
3170     address start = __ pc();
3171 
3172     const Register from        = c_rarg0;  // source array address
3173     const Register to          = c_rarg1;  // destination array address
3174     const Register key         = c_rarg2;  // key array address
3175     const Register keylen      = rax;
3176 
3177     const XMMRegister xmm_result = xmm0;
3178     const XMMRegister xmm_key_shuf_mask = xmm1;
3179     // On win64 xmm6-xmm15 must be preserved so don't use them.
3180     const XMMRegister xmm_temp1  = xmm2;
3181     const XMMRegister xmm_temp2  = xmm3;
3182     const XMMRegister xmm_temp3  = xmm4;
3183     const XMMRegister xmm_temp4  = xmm5;
3184 
3185     __ enter(); // required for proper stackwalking of RuntimeStub frame
3186 
3187     // keylen could be only {11, 13, 15} * 4 = {44, 52, 60}
3188     __ movl(keylen, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
3189 
3190     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
3191     __ movdqu(xmm_result, Address(from, 0));
3192 
3193     // for decryption java expanded key ordering is rotated one position from what we want
3194     // so we start from 0x10 here and hit 0x00 last
3195     // we don't know if the key is aligned, hence not using load-execute form
3196     load_key(xmm_temp1, key, 0x10, xmm_key_shuf_mask);
3197     load_key(xmm_temp2, key, 0x20, xmm_key_shuf_mask);
3198     load_key(xmm_temp3, key, 0x30, xmm_key_shuf_mask);
3199     load_key(xmm_temp4, key, 0x40, xmm_key_shuf_mask);
3200 
3201     __ pxor  (xmm_result, xmm_temp1);
3202     __ aesdec(xmm_result, xmm_temp2);
3203     __ aesdec(xmm_result, xmm_temp3);
3204     __ aesdec(xmm_result, xmm_temp4);
3205 
3206     load_key(xmm_temp1, key, 0x50, xmm_key_shuf_mask);
3207     load_key(xmm_temp2, key, 0x60, xmm_key_shuf_mask);
3208     load_key(xmm_temp3, key, 0x70, xmm_key_shuf_mask);
3209     load_key(xmm_temp4, key, 0x80, xmm_key_shuf_mask);
3210 
3211     __ aesdec(xmm_result, xmm_temp1);
3212     __ aesdec(xmm_result, xmm_temp2);
3213     __ aesdec(xmm_result, xmm_temp3);
3214     __ aesdec(xmm_result, xmm_temp4);
3215 
3216     load_key(xmm_temp1, key, 0x90, xmm_key_shuf_mask);
3217     load_key(xmm_temp2, key, 0xa0, xmm_key_shuf_mask);
3218     load_key(xmm_temp3, key, 0x00, xmm_key_shuf_mask);
3219 
3220     __ cmpl(keylen, 44);
3221     __ jccb(Assembler::equal, L_doLast);
3222 
3223     __ aesdec(xmm_result, xmm_temp1);
3224     __ aesdec(xmm_result, xmm_temp2);
3225 
3226     load_key(xmm_temp1, key, 0xb0, xmm_key_shuf_mask);
3227     load_key(xmm_temp2, key, 0xc0, xmm_key_shuf_mask);
3228 
3229     __ cmpl(keylen, 52);
3230     __ jccb(Assembler::equal, L_doLast);
3231 
3232     __ aesdec(xmm_result, xmm_temp1);
3233     __ aesdec(xmm_result, xmm_temp2);
3234 
3235     load_key(xmm_temp1, key, 0xd0, xmm_key_shuf_mask);
3236     load_key(xmm_temp2, key, 0xe0, xmm_key_shuf_mask);
3237 
3238     __ BIND(L_doLast);
3239     __ aesdec(xmm_result, xmm_temp1);
3240     __ aesdec(xmm_result, xmm_temp2);
3241 
3242     // for decryption the aesdeclast operation is always on key+0x00
3243     __ aesdeclast(xmm_result, xmm_temp3);
3244     __ movdqu(Address(to, 0), xmm_result);  // store the result
3245     __ xorptr(rax, rax); // return 0
3246     __ leave(); // required for proper stackwalking of RuntimeStub frame
3247     __ ret(0);
3248 
3249     return start;
3250   }
3251 
3252 
3253   // Arguments:
3254   //
3255   // Inputs:
3256   //   c_rarg0   - source byte array address
3257   //   c_rarg1   - destination byte array address
3258   //   c_rarg2   - K (key) in little endian int array
3259   //   c_rarg3   - r vector byte array address
3260   //   c_rarg4   - input length
3261   //
3262   // Output:
3263   //   rax       - input length
3264   //
3265   address generate_cipherBlockChaining_encryptAESCrypt() {
3266     assert(UseAES, "need AES instructions and misaligned SSE support");
3267     __ align(CodeEntryAlignment);
3268     StubCodeMark mark(this, "StubRoutines", "cipherBlockChaining_encryptAESCrypt");
3269     address start = __ pc();
3270 
3271     Label L_exit, L_key_192_256, L_key_256, L_loopTop_128, L_loopTop_192, L_loopTop_256;
3272     const Register from        = c_rarg0;  // source array address
3273     const Register to          = c_rarg1;  // destination array address
3274     const Register key         = c_rarg2;  // key array address
3275     const Register rvec        = c_rarg3;  // r byte array initialized from initvector array address
3276                                            // and left with the results of the last encryption block
3277 #ifndef _WIN64
3278     const Register len_reg     = c_rarg4;  // src len (must be multiple of blocksize 16)
3279 #else
3280     const Address  len_mem(rbp, 6 * wordSize);  // length is on stack on Win64
3281     const Register len_reg     = r10;      // pick the first volatile windows register
3282 #endif
3283     const Register pos         = rax;
3284 
3285     // xmm register assignments for the loops below
3286     const XMMRegister xmm_result = xmm0;
3287     const XMMRegister xmm_temp   = xmm1;
3288     // keys 0-10 preloaded into xmm2-xmm12
3289     const int XMM_REG_NUM_KEY_FIRST = 2;
3290     const int XMM_REG_NUM_KEY_LAST  = 15;
3291     const XMMRegister xmm_key0   = as_XMMRegister(XMM_REG_NUM_KEY_FIRST);
3292     const XMMRegister xmm_key10  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+10);
3293     const XMMRegister xmm_key11  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+11);
3294     const XMMRegister xmm_key12  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+12);
3295     const XMMRegister xmm_key13  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+13);
3296 
3297     __ enter(); // required for proper stackwalking of RuntimeStub frame
3298 
3299 #ifdef _WIN64
3300     // on win64, fill len_reg from stack position
3301     __ movl(len_reg, len_mem);
3302     // save the xmm registers which must be preserved 6-15
3303     __ subptr(rsp, -rsp_after_call_off * wordSize);
3304     for (int i = 6; i <= XMM_REG_NUM_KEY_LAST; i++) {
3305       __ movdqu(xmm_save(i), as_XMMRegister(i));
3306     }
3307 #else
3308     __ push(len_reg); // Save
3309 #endif
3310 
3311     const XMMRegister xmm_key_shuf_mask = xmm_temp;  // used temporarily to swap key bytes up front
3312     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
3313     // load up xmm regs xmm2 thru xmm12 with key 0x00 - 0xa0
3314     for (int rnum = XMM_REG_NUM_KEY_FIRST, offset = 0x00; rnum <= XMM_REG_NUM_KEY_FIRST+10; rnum++) {
3315       load_key(as_XMMRegister(rnum), key, offset, xmm_key_shuf_mask);
3316       offset += 0x10;
3317     }
3318     __ movdqu(xmm_result, Address(rvec, 0x00));   // initialize xmm_result with r vec
3319 
3320     // now split to different paths depending on the keylen (len in ints of AESCrypt.KLE array (52=192, or 60=256))
3321     __ movl(rax, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
3322     __ cmpl(rax, 44);
3323     __ jcc(Assembler::notEqual, L_key_192_256);
3324 
3325     // 128 bit code follows here
3326     __ movptr(pos, 0);
3327     __ align(OptoLoopAlignment);
3328 
3329     __ BIND(L_loopTop_128);
3330     __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of input
3331     __ pxor  (xmm_result, xmm_temp);               // xor with the current r vector
3332     __ pxor  (xmm_result, xmm_key0);               // do the aes rounds
3333     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_FIRST + 9; rnum++) {
3334       __ aesenc(xmm_result, as_XMMRegister(rnum));
3335     }
3336     __ aesenclast(xmm_result, xmm_key10);
3337     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);     // store into the next 16 bytes of output
3338     // no need to store r to memory until we exit
3339     __ addptr(pos, AESBlockSize);
3340     __ subptr(len_reg, AESBlockSize);
3341     __ jcc(Assembler::notEqual, L_loopTop_128);
3342 
3343     __ BIND(L_exit);
3344     __ movdqu(Address(rvec, 0), xmm_result);     // final value of r stored in rvec of CipherBlockChaining object
3345 
3346 #ifdef _WIN64
3347     // restore xmm regs belonging to calling function
3348     for (int i = 6; i <= XMM_REG_NUM_KEY_LAST; i++) {
3349       __ movdqu(as_XMMRegister(i), xmm_save(i));
3350     }
3351     __ movl(rax, len_mem);
3352 #else
3353     __ pop(rax); // return length
3354 #endif
3355     __ leave(); // required for proper stackwalking of RuntimeStub frame
3356     __ ret(0);
3357 
3358     __ BIND(L_key_192_256);
3359     // here rax = len in ints of AESCrypt.KLE array (52=192, or 60=256)
3360     load_key(xmm_key11, key, 0xb0, xmm_key_shuf_mask);
3361     load_key(xmm_key12, key, 0xc0, xmm_key_shuf_mask);
3362     __ cmpl(rax, 52);
3363     __ jcc(Assembler::notEqual, L_key_256);
3364 
3365     // 192-bit code follows here (could be changed to use more xmm registers)
3366     __ movptr(pos, 0);
3367     __ align(OptoLoopAlignment);
3368 
3369     __ BIND(L_loopTop_192);
3370     __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of input
3371     __ pxor  (xmm_result, xmm_temp);               // xor with the current r vector
3372     __ pxor  (xmm_result, xmm_key0);               // do the aes rounds
3373     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum  <= XMM_REG_NUM_KEY_FIRST + 11; rnum++) {
3374       __ aesenc(xmm_result, as_XMMRegister(rnum));
3375     }
3376     __ aesenclast(xmm_result, xmm_key12);
3377     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);     // store into the next 16 bytes of output
3378     // no need to store r to memory until we exit
3379     __ addptr(pos, AESBlockSize);
3380     __ subptr(len_reg, AESBlockSize);
3381     __ jcc(Assembler::notEqual, L_loopTop_192);
3382     __ jmp(L_exit);
3383 
3384     __ BIND(L_key_256);
3385     // 256-bit code follows here (could be changed to use more xmm registers)
3386     load_key(xmm_key13, key, 0xd0, xmm_key_shuf_mask);
3387     __ movptr(pos, 0);
3388     __ align(OptoLoopAlignment);
3389 
3390     __ BIND(L_loopTop_256);
3391     __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of input
3392     __ pxor  (xmm_result, xmm_temp);               // xor with the current r vector
3393     __ pxor  (xmm_result, xmm_key0);               // do the aes rounds
3394     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum  <= XMM_REG_NUM_KEY_FIRST + 13; rnum++) {
3395       __ aesenc(xmm_result, as_XMMRegister(rnum));
3396     }
3397     load_key(xmm_temp, key, 0xe0);
3398     __ aesenclast(xmm_result, xmm_temp);
3399     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);     // store into the next 16 bytes of output
3400     // no need to store r to memory until we exit
3401     __ addptr(pos, AESBlockSize);
3402     __ subptr(len_reg, AESBlockSize);
3403     __ jcc(Assembler::notEqual, L_loopTop_256);
3404     __ jmp(L_exit);
3405 
3406     return start;
3407   }
3408 
3409   // Safefetch stubs.
3410   void generate_safefetch(const char* name, int size, address* entry,
3411                           address* fault_pc, address* continuation_pc) {
3412     // safefetch signatures:
3413     //   int      SafeFetch32(int*      adr, int      errValue);
3414     //   intptr_t SafeFetchN (intptr_t* adr, intptr_t errValue);
3415     //
3416     // arguments:
3417     //   c_rarg0 = adr
3418     //   c_rarg1 = errValue
3419     //
3420     // result:
3421     //   PPC_RET  = *adr or errValue
3422 
3423     StubCodeMark mark(this, "StubRoutines", name);
3424 
3425     // Entry point, pc or function descriptor.
3426     *entry = __ pc();
3427 
3428     // Load *adr into c_rarg1, may fault.
3429     *fault_pc = __ pc();
3430     switch (size) {
3431       case 4:
3432         // int32_t
3433         __ movl(c_rarg1, Address(c_rarg0, 0));
3434         break;
3435       case 8:
3436         // int64_t
3437         __ movq(c_rarg1, Address(c_rarg0, 0));
3438         break;
3439       default:
3440         ShouldNotReachHere();
3441     }
3442 
3443     // return errValue or *adr
3444     *continuation_pc = __ pc();
3445     __ movq(rax, c_rarg1);
3446     __ ret(0);
3447   }
3448 
3449   // This is a version of CBC/AES Decrypt which does 4 blocks in a loop at a time
3450   // to hide instruction latency
3451   //
3452   // Arguments:
3453   //
3454   // Inputs:
3455   //   c_rarg0   - source byte array address
3456   //   c_rarg1   - destination byte array address
3457   //   c_rarg2   - K (key) in little endian int array
3458   //   c_rarg3   - r vector byte array address
3459   //   c_rarg4   - input length
3460   //
3461   // Output:
3462   //   rax       - input length
3463   //
3464 
3465   address generate_cipherBlockChaining_decryptAESCrypt_Parallel() {
3466     assert(UseAES, "need AES instructions and misaligned SSE support");
3467     __ align(CodeEntryAlignment);
3468     StubCodeMark mark(this, "StubRoutines", "cipherBlockChaining_decryptAESCrypt");
3469     address start = __ pc();
3470 
3471     Label L_exit, L_key_192_256, L_key_256;
3472     Label L_singleBlock_loopTop_128, L_multiBlock_loopTop_128;
3473     Label L_singleBlock_loopTop_192, L_singleBlock_loopTop_256;
3474     const Register from        = c_rarg0;  // source array address
3475     const Register to          = c_rarg1;  // destination array address
3476     const Register key         = c_rarg2;  // key array address
3477     const Register rvec        = c_rarg3;  // r byte array initialized from initvector array address
3478                                            // and left with the results of the last encryption block
3479 #ifndef _WIN64
3480     const Register len_reg     = c_rarg4;  // src len (must be multiple of blocksize 16)
3481 #else
3482     const Address  len_mem(rbp, 6 * wordSize);  // length is on stack on Win64
3483     const Register len_reg     = r10;      // pick the first volatile windows register
3484 #endif
3485     const Register pos         = rax;
3486 
3487     // keys 0-10 preloaded into xmm2-xmm12
3488     const int XMM_REG_NUM_KEY_FIRST = 5;
3489     const int XMM_REG_NUM_KEY_LAST  = 15;
3490     const XMMRegister xmm_key_first = as_XMMRegister(XMM_REG_NUM_KEY_FIRST);
3491     const XMMRegister xmm_key_last  = as_XMMRegister(XMM_REG_NUM_KEY_LAST);
3492 
3493     __ enter(); // required for proper stackwalking of RuntimeStub frame
3494 
3495 #ifdef _WIN64
3496     // on win64, fill len_reg from stack position
3497     __ movl(len_reg, len_mem);
3498     // save the xmm registers which must be preserved 6-15
3499     __ subptr(rsp, -rsp_after_call_off * wordSize);
3500     for (int i = 6; i <= XMM_REG_NUM_KEY_LAST; i++) {
3501       __ movdqu(xmm_save(i), as_XMMRegister(i));
3502     }
3503 #else
3504     __ push(len_reg); // Save
3505 #endif
3506 
3507     // the java expanded key ordering is rotated one position from what we want
3508     // so we start from 0x10 here and hit 0x00 last
3509     const XMMRegister xmm_key_shuf_mask = xmm1;  // used temporarily to swap key bytes up front
3510     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
3511     // load up xmm regs 5 thru 15 with key 0x10 - 0xa0 - 0x00
3512     for (int rnum = XMM_REG_NUM_KEY_FIRST, offset = 0x10; rnum < XMM_REG_NUM_KEY_LAST; rnum++) {
3513       load_key(as_XMMRegister(rnum), key, offset, xmm_key_shuf_mask);
3514       offset += 0x10;
3515     }
3516     load_key(xmm_key_last, key, 0x00, xmm_key_shuf_mask);
3517 
3518     const XMMRegister xmm_prev_block_cipher = xmm1;  // holds cipher of previous block
3519 
3520     // registers holding the four results in the parallelized loop
3521     const XMMRegister xmm_result0 = xmm0;
3522     const XMMRegister xmm_result1 = xmm2;
3523     const XMMRegister xmm_result2 = xmm3;
3524     const XMMRegister xmm_result3 = xmm4;
3525 
3526     __ movdqu(xmm_prev_block_cipher, Address(rvec, 0x00));   // initialize with initial rvec
3527 
3528     // now split to different paths depending on the keylen (len in ints of AESCrypt.KLE array (52=192, or 60=256))
3529     __ movl(rax, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
3530     __ cmpl(rax, 44);
3531     __ jcc(Assembler::notEqual, L_key_192_256);
3532 
3533 
3534     // 128-bit code follows here, parallelized
3535     __ movptr(pos, 0);
3536     __ align(OptoLoopAlignment);
3537     __ BIND(L_multiBlock_loopTop_128);
3538     __ cmpptr(len_reg, 4*AESBlockSize);           // see if at least 4 blocks left
3539     __ jcc(Assembler::less, L_singleBlock_loopTop_128);
3540 
3541     __ movdqu(xmm_result0, Address(from, pos, Address::times_1, 0*AESBlockSize));   // get next 4 blocks into xmmresult registers
3542     __ movdqu(xmm_result1, Address(from, pos, Address::times_1, 1*AESBlockSize));
3543     __ movdqu(xmm_result2, Address(from, pos, Address::times_1, 2*AESBlockSize));
3544     __ movdqu(xmm_result3, Address(from, pos, Address::times_1, 3*AESBlockSize));
3545 
3546 #define DoFour(opc, src_reg)                    \
3547     __ opc(xmm_result0, src_reg);               \
3548     __ opc(xmm_result1, src_reg);               \
3549     __ opc(xmm_result2, src_reg);               \
3550     __ opc(xmm_result3, src_reg);
3551 
3552     DoFour(pxor, xmm_key_first);
3553     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum  <= XMM_REG_NUM_KEY_LAST - 1; rnum++) {
3554       DoFour(aesdec, as_XMMRegister(rnum));
3555     }
3556     DoFour(aesdeclast, xmm_key_last);
3557     // for each result, xor with the r vector of previous cipher block
3558     __ pxor(xmm_result0, xmm_prev_block_cipher);
3559     __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 0*AESBlockSize));
3560     __ pxor(xmm_result1, xmm_prev_block_cipher);
3561     __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 1*AESBlockSize));
3562     __ pxor(xmm_result2, xmm_prev_block_cipher);
3563     __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 2*AESBlockSize));
3564     __ pxor(xmm_result3, xmm_prev_block_cipher);
3565     __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 3*AESBlockSize));   // this will carry over to next set of blocks
3566 
3567     __ movdqu(Address(to, pos, Address::times_1, 0*AESBlockSize), xmm_result0);     // store 4 results into the next 64 bytes of output
3568     __ movdqu(Address(to, pos, Address::times_1, 1*AESBlockSize), xmm_result1);
3569     __ movdqu(Address(to, pos, Address::times_1, 2*AESBlockSize), xmm_result2);
3570     __ movdqu(Address(to, pos, Address::times_1, 3*AESBlockSize), xmm_result3);
3571 
3572     __ addptr(pos, 4*AESBlockSize);
3573     __ subptr(len_reg, 4*AESBlockSize);
3574     __ jmp(L_multiBlock_loopTop_128);
3575 
3576     // registers used in the non-parallelized loops
3577     // xmm register assignments for the loops below
3578     const XMMRegister xmm_result = xmm0;
3579     const XMMRegister xmm_prev_block_cipher_save = xmm2;
3580     const XMMRegister xmm_key11 = xmm3;
3581     const XMMRegister xmm_key12 = xmm4;
3582     const XMMRegister xmm_temp  = xmm4;
3583 
3584     __ align(OptoLoopAlignment);
3585     __ BIND(L_singleBlock_loopTop_128);
3586     __ cmpptr(len_reg, 0);           // any blocks left??
3587     __ jcc(Assembler::equal, L_exit);
3588     __ movdqu(xmm_result, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of cipher input
3589     __ movdqa(xmm_prev_block_cipher_save, xmm_result);              // save for next r vector
3590     __ pxor  (xmm_result, xmm_key_first);               // do the aes dec rounds
3591     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum  <= XMM_REG_NUM_KEY_LAST - 1; rnum++) {
3592       __ aesdec(xmm_result, as_XMMRegister(rnum));
3593     }
3594     __ aesdeclast(xmm_result, xmm_key_last);
3595     __ pxor  (xmm_result, xmm_prev_block_cipher);               // xor with the current r vector
3596     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);     // store into the next 16 bytes of output
3597     // no need to store r to memory until we exit
3598     __ movdqa(xmm_prev_block_cipher, xmm_prev_block_cipher_save);              // set up next r vector with cipher input from this block
3599 
3600     __ addptr(pos, AESBlockSize);
3601     __ subptr(len_reg, AESBlockSize);
3602     __ jmp(L_singleBlock_loopTop_128);
3603 
3604 
3605     __ BIND(L_exit);
3606     __ movdqu(Address(rvec, 0), xmm_prev_block_cipher);     // final value of r stored in rvec of CipherBlockChaining object
3607 #ifdef _WIN64
3608     // restore regs belonging to calling function
3609     for (int i = 6; i <= XMM_REG_NUM_KEY_LAST; i++) {
3610       __ movdqu(as_XMMRegister(i), xmm_save(i));
3611     }
3612     __ movl(rax, len_mem);
3613 #else
3614     __ pop(rax); // return length
3615 #endif
3616     __ leave(); // required for proper stackwalking of RuntimeStub frame
3617     __ ret(0);
3618 
3619 
3620     __ BIND(L_key_192_256);
3621     // here rax = len in ints of AESCrypt.KLE array (52=192, or 60=256)
3622     load_key(xmm_key11, key, 0xb0);
3623     __ cmpl(rax, 52);
3624     __ jcc(Assembler::notEqual, L_key_256);
3625 
3626     // 192-bit code follows here (could be optimized to use parallelism)
3627     load_key(xmm_key12, key, 0xc0);     // 192-bit key goes up to c0
3628     __ movptr(pos, 0);
3629     __ align(OptoLoopAlignment);
3630 
3631     __ BIND(L_singleBlock_loopTop_192);
3632     __ movdqu(xmm_result, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of cipher input
3633     __ movdqa(xmm_prev_block_cipher_save, xmm_result);              // save for next r vector
3634     __ pxor  (xmm_result, xmm_key_first);               // do the aes dec rounds
3635     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_LAST - 1; rnum++) {
3636       __ aesdec(xmm_result, as_XMMRegister(rnum));
3637     }
3638     __ aesdec(xmm_result, xmm_key11);
3639     __ aesdec(xmm_result, xmm_key12);
3640     __ aesdeclast(xmm_result, xmm_key_last);                    // xmm15 always came from key+0
3641     __ pxor  (xmm_result, xmm_prev_block_cipher);               // xor with the current r vector
3642     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);  // store into the next 16 bytes of output
3643     // no need to store r to memory until we exit
3644     __ movdqa(xmm_prev_block_cipher, xmm_prev_block_cipher_save);  // set up next r vector with cipher input from this block
3645     __ addptr(pos, AESBlockSize);
3646     __ subptr(len_reg, AESBlockSize);
3647     __ jcc(Assembler::notEqual,L_singleBlock_loopTop_192);
3648     __ jmp(L_exit);
3649 
3650     __ BIND(L_key_256);
3651     // 256-bit code follows here (could be optimized to use parallelism)
3652     __ movptr(pos, 0);
3653     __ align(OptoLoopAlignment);
3654 
3655     __ BIND(L_singleBlock_loopTop_256);
3656     __ movdqu(xmm_result, Address(from, pos, Address::times_1, 0)); // get next 16 bytes of cipher input
3657     __ movdqa(xmm_prev_block_cipher_save, xmm_result);              // save for next r vector
3658     __ pxor  (xmm_result, xmm_key_first);               // do the aes dec rounds
3659     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_LAST - 1; rnum++) {
3660       __ aesdec(xmm_result, as_XMMRegister(rnum));
3661     }
3662     __ aesdec(xmm_result, xmm_key11);
3663     load_key(xmm_temp, key, 0xc0);
3664     __ aesdec(xmm_result, xmm_temp);
3665     load_key(xmm_temp, key, 0xd0);
3666     __ aesdec(xmm_result, xmm_temp);
3667     load_key(xmm_temp, key, 0xe0);     // 256-bit key goes up to e0
3668     __ aesdec(xmm_result, xmm_temp);
3669     __ aesdeclast(xmm_result, xmm_key_last);          // xmm15 came from key+0
3670     __ pxor  (xmm_result, xmm_prev_block_cipher);               // xor with the current r vector
3671     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);  // store into the next 16 bytes of output
3672     // no need to store r to memory until we exit
3673     __ movdqa(xmm_prev_block_cipher, xmm_prev_block_cipher_save);  // set up next r vector with cipher input from this block
3674     __ addptr(pos, AESBlockSize);
3675     __ subptr(len_reg, AESBlockSize);
3676     __ jcc(Assembler::notEqual,L_singleBlock_loopTop_256);
3677     __ jmp(L_exit);
3678 
3679     return start;
3680   }
3681 
3682   /**
3683    *  Arguments:
3684    *
3685    * Inputs:
3686    *   c_rarg0   - int crc
3687    *   c_rarg1   - byte* buf
3688    *   c_rarg2   - int length
3689    *
3690    * Ouput:
3691    *       rax   - int crc result
3692    */
3693   address generate_updateBytesCRC32() {
3694     assert(UseCRC32Intrinsics, "need AVX and CLMUL instructions");
3695 
3696     __ align(CodeEntryAlignment);
3697     StubCodeMark mark(this, "StubRoutines", "updateBytesCRC32");
3698 
3699     address start = __ pc();
3700     // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
3701     // Unix:  rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
3702     // rscratch1: r10
3703     const Register crc   = c_rarg0;  // crc
3704     const Register buf   = c_rarg1;  // source java byte array address
3705     const Register len   = c_rarg2;  // length
3706     const Register table = c_rarg3;  // crc_table address (reuse register)
3707     const Register tmp   = r11;
3708     assert_different_registers(crc, buf, len, table, tmp, rax);
3709 
3710     BLOCK_COMMENT("Entry:");
3711     __ enter(); // required for proper stackwalking of RuntimeStub frame
3712 
3713     __ kernel_crc32(crc, buf, len, table, tmp);
3714 
3715     __ movl(rax, crc);
3716     __ leave(); // required for proper stackwalking of RuntimeStub frame
3717     __ ret(0);
3718 
3719     return start;
3720   }
3721 
3722 
3723   /**
3724    *  Arguments:
3725    *
3726    *  Input:
3727    *    c_rarg0   - x address
3728    *    c_rarg1   - x length
3729    *    c_rarg2   - y address
3730    *    c_rarg3   - y lenth
3731    * not Win64
3732    *    c_rarg4   - z address
3733    *    c_rarg5   - z length
3734    * Win64
3735    *    rsp+40    - z address
3736    *    rsp+48    - z length
3737    */
3738   address generate_multiplyToLen() {
3739     __ align(CodeEntryAlignment);
3740     StubCodeMark mark(this, "StubRoutines", "multiplyToLen");
3741 
3742     address start = __ pc();
3743     // Win64: rcx, rdx, r8, r9 (c_rarg0, c_rarg1, ...)
3744     // Unix:  rdi, rsi, rdx, rcx, r8, r9 (c_rarg0, c_rarg1, ...)
3745     const Register x     = rdi;
3746     const Register xlen  = rax;
3747     const Register y     = rsi;
3748     const Register ylen  = rcx;
3749     const Register z     = r8;
3750     const Register zlen  = r11;
3751 
3752     // Next registers will be saved on stack in multiply_to_len().
3753     const Register tmp1  = r12;
3754     const Register tmp2  = r13;
3755     const Register tmp3  = r14;
3756     const Register tmp4  = r15;
3757     const Register tmp5  = rbx;
3758 
3759     BLOCK_COMMENT("Entry:");
3760     __ enter(); // required for proper stackwalking of RuntimeStub frame
3761 
3762 #ifndef _WIN64
3763     __ movptr(zlen, r9); // Save r9 in r11 - zlen
3764 #endif
3765     setup_arg_regs(4); // x => rdi, xlen => rsi, y => rdx
3766                        // ylen => rcx, z => r8, zlen => r11
3767                        // r9 and r10 may be used to save non-volatile registers
3768 #ifdef _WIN64
3769     // last 2 arguments (#4, #5) are on stack on Win64
3770     __ movptr(z, Address(rsp, 6 * wordSize));
3771     __ movptr(zlen, Address(rsp, 7 * wordSize));
3772 #endif
3773 
3774     __ movptr(xlen, rsi);
3775     __ movptr(y,    rdx);
3776     __ multiply_to_len(x, xlen, y, ylen, z, zlen, tmp1, tmp2, tmp3, tmp4, tmp5);
3777 
3778     restore_arg_regs();
3779 
3780     __ leave(); // required for proper stackwalking of RuntimeStub frame
3781     __ ret(0);
3782 
3783     return start;
3784   }
3785 
3786 #undef __
3787 #define __ masm->
3788 
3789   // Continuation point for throwing of implicit exceptions that are
3790   // not handled in the current activation. Fabricates an exception
3791   // oop and initiates normal exception dispatching in this
3792   // frame. Since we need to preserve callee-saved values (currently
3793   // only for C2, but done for C1 as well) we need a callee-saved oop
3794   // map and therefore have to make these stubs into RuntimeStubs
3795   // rather than BufferBlobs.  If the compiler needs all registers to
3796   // be preserved between the fault point and the exception handler
3797   // then it must assume responsibility for that in
3798   // AbstractCompiler::continuation_for_implicit_null_exception or
3799   // continuation_for_implicit_division_by_zero_exception. All other
3800   // implicit exceptions (e.g., NullPointerException or
3801   // AbstractMethodError on entry) are either at call sites or
3802   // otherwise assume that stack unwinding will be initiated, so
3803   // caller saved registers were assumed volatile in the compiler.
3804   address generate_throw_exception(const char* name,
3805                                    address runtime_entry,
3806                                    Register arg1 = noreg,
3807                                    Register arg2 = noreg) {
3808     // Information about frame layout at time of blocking runtime call.
3809     // Note that we only have to preserve callee-saved registers since
3810     // the compilers are responsible for supplying a continuation point
3811     // if they expect all registers to be preserved.
3812     enum layout {
3813       rbp_off = frame::arg_reg_save_area_bytes/BytesPerInt,
3814       rbp_off2,
3815       return_off,
3816       return_off2,
3817       framesize // inclusive of return address
3818     };
3819 
3820     int insts_size = 512;
3821     int locs_size  = 64;
3822 
3823     CodeBuffer code(name, insts_size, locs_size);
3824     OopMapSet* oop_maps  = new OopMapSet();
3825     MacroAssembler* masm = new MacroAssembler(&code);
3826 
3827     address start = __ pc();
3828 
3829     // This is an inlined and slightly modified version of call_VM
3830     // which has the ability to fetch the return PC out of
3831     // thread-local storage and also sets up last_Java_sp slightly
3832     // differently than the real call_VM
3833 
3834     __ enter(); // required for proper stackwalking of RuntimeStub frame
3835 
3836     assert(is_even(framesize/2), "sp not 16-byte aligned");
3837 
3838     // return address and rbp are already in place
3839     __ subptr(rsp, (framesize-4) << LogBytesPerInt); // prolog
3840 
3841     int frame_complete = __ pc() - start;
3842 
3843     // Set up last_Java_sp and last_Java_fp
3844     address the_pc = __ pc();
3845     __ set_last_Java_frame(rsp, rbp, the_pc);
3846     __ andptr(rsp, -(StackAlignmentInBytes));    // Align stack
3847 
3848     // Call runtime
3849     if (arg1 != noreg) {
3850       assert(arg2 != c_rarg1, "clobbered");
3851       __ movptr(c_rarg1, arg1);
3852     }
3853     if (arg2 != noreg) {
3854       __ movptr(c_rarg2, arg2);
3855     }
3856     __ movptr(c_rarg0, r15_thread);
3857     BLOCK_COMMENT("call runtime_entry");
3858     __ call(RuntimeAddress(runtime_entry));
3859 
3860     // Generate oop map
3861     OopMap* map = new OopMap(framesize, 0);
3862 
3863     oop_maps->add_gc_map(the_pc - start, map);
3864 
3865     __ reset_last_Java_frame(true, true);
3866 
3867     __ leave(); // required for proper stackwalking of RuntimeStub frame
3868 
3869     // check for pending exceptions
3870 #ifdef ASSERT
3871     Label L;
3872     __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()),
3873             (int32_t) NULL_WORD);
3874     __ jcc(Assembler::notEqual, L);
3875     __ should_not_reach_here();
3876     __ bind(L);
3877 #endif // ASSERT
3878     __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
3879 
3880 
3881     // codeBlob framesize is in words (not VMRegImpl::slot_size)
3882     RuntimeStub* stub =
3883       RuntimeStub::new_runtime_stub(name,
3884                                     &code,
3885                                     frame_complete,
3886                                     (framesize >> (LogBytesPerWord - LogBytesPerInt)),
3887                                     oop_maps, false);
3888     return stub->entry_point();
3889   }
3890 
3891   void create_control_words() {
3892     // Round to nearest, 53-bit mode, exceptions masked
3893     StubRoutines::_fpu_cntrl_wrd_std   = 0x027F;
3894     // Round to zero, 53-bit mode, exception mased
3895     StubRoutines::_fpu_cntrl_wrd_trunc = 0x0D7F;
3896     // Round to nearest, 24-bit mode, exceptions masked
3897     StubRoutines::_fpu_cntrl_wrd_24    = 0x007F;
3898     // Round to nearest, 64-bit mode, exceptions masked
3899     StubRoutines::_fpu_cntrl_wrd_64    = 0x037F;
3900     // Round to nearest, 64-bit mode, exceptions masked
3901     StubRoutines::_mxcsr_std           = 0x1F80;
3902     // Note: the following two constants are 80-bit values
3903     //       layout is critical for correct loading by FPU.
3904     // Bias for strict fp multiply/divide
3905     StubRoutines::_fpu_subnormal_bias1[0]= 0x00000000; // 2^(-15360) == 0x03ff 8000 0000 0000 0000
3906     StubRoutines::_fpu_subnormal_bias1[1]= 0x80000000;
3907     StubRoutines::_fpu_subnormal_bias1[2]= 0x03ff;
3908     // Un-Bias for strict fp multiply/divide
3909     StubRoutines::_fpu_subnormal_bias2[0]= 0x00000000; // 2^(+15360) == 0x7bff 8000 0000 0000 0000
3910     StubRoutines::_fpu_subnormal_bias2[1]= 0x80000000;
3911     StubRoutines::_fpu_subnormal_bias2[2]= 0x7bff;
3912   }
3913 
3914   // Initialization
3915   void generate_initial() {
3916     // Generates all stubs and initializes the entry points
3917 
3918     // This platform-specific settings are needed by generate_call_stub()
3919     create_control_words();
3920 
3921     // entry points that exist in all platforms Note: This is code
3922     // that could be shared among different platforms - however the
3923     // benefit seems to be smaller than the disadvantage of having a
3924     // much more complicated generator structure. See also comment in
3925     // stubRoutines.hpp.
3926 
3927     StubRoutines::_forward_exception_entry = generate_forward_exception();
3928 
3929     StubRoutines::_call_stub_entry =
3930       generate_call_stub(StubRoutines::_call_stub_return_address);
3931 
3932     // is referenced by megamorphic call
3933     StubRoutines::_catch_exception_entry = generate_catch_exception();
3934 
3935     // atomic calls
3936     StubRoutines::_atomic_xchg_entry         = generate_atomic_xchg();
3937     StubRoutines::_atomic_xchg_ptr_entry     = generate_atomic_xchg_ptr();
3938     StubRoutines::_atomic_cmpxchg_entry      = generate_atomic_cmpxchg();
3939     StubRoutines::_atomic_cmpxchg_byte_entry = generate_atomic_cmpxchg_byte();
3940     StubRoutines::_atomic_cmpxchg_long_entry = generate_atomic_cmpxchg_long();
3941     StubRoutines::_atomic_add_entry          = generate_atomic_add();
3942     StubRoutines::_atomic_add_ptr_entry      = generate_atomic_add_ptr();
3943     StubRoutines::_fence_entry               = generate_orderaccess_fence();
3944 
3945     StubRoutines::_handler_for_unsafe_access_entry =
3946       generate_handler_for_unsafe_access();
3947 
3948     // platform dependent
3949     StubRoutines::x86::_get_previous_fp_entry = generate_get_previous_fp();
3950     StubRoutines::x86::_get_previous_sp_entry = generate_get_previous_sp();
3951 
3952     StubRoutines::x86::_verify_mxcsr_entry    = generate_verify_mxcsr();
3953 
3954     // Build this early so it's available for the interpreter.
3955     StubRoutines::_throw_StackOverflowError_entry =
3956       generate_throw_exception("StackOverflowError throw_exception",
3957                                CAST_FROM_FN_PTR(address,
3958                                                 SharedRuntime::
3959                                                 throw_StackOverflowError));
3960     if (UseCRC32Intrinsics) {
3961       // set table address before stub generation which use it
3962       StubRoutines::_crc_table_adr = (address)StubRoutines::x86::_crc_table;
3963       StubRoutines::_updateBytesCRC32 = generate_updateBytesCRC32();
3964     }
3965   }
3966 
3967   void generate_all() {
3968     // Generates all stubs and initializes the entry points
3969 
3970     // These entry points require SharedInfo::stack0 to be set up in
3971     // non-core builds and need to be relocatable, so they each
3972     // fabricate a RuntimeStub internally.
3973     StubRoutines::_throw_AbstractMethodError_entry =
3974       generate_throw_exception("AbstractMethodError throw_exception",
3975                                CAST_FROM_FN_PTR(address,
3976                                                 SharedRuntime::
3977                                                 throw_AbstractMethodError));
3978 
3979     StubRoutines::_throw_IncompatibleClassChangeError_entry =
3980       generate_throw_exception("IncompatibleClassChangeError throw_exception",
3981                                CAST_FROM_FN_PTR(address,
3982                                                 SharedRuntime::
3983                                                 throw_IncompatibleClassChangeError));
3984 
3985     StubRoutines::_throw_NullPointerException_at_call_entry =
3986       generate_throw_exception("NullPointerException at call throw_exception",
3987                                CAST_FROM_FN_PTR(address,
3988                                                 SharedRuntime::
3989                                                 throw_NullPointerException_at_call));
3990 
3991     // entry points that are platform specific
3992     StubRoutines::x86::_f2i_fixup = generate_f2i_fixup();
3993     StubRoutines::x86::_f2l_fixup = generate_f2l_fixup();
3994     StubRoutines::x86::_d2i_fixup = generate_d2i_fixup();
3995     StubRoutines::x86::_d2l_fixup = generate_d2l_fixup();
3996 
3997     StubRoutines::x86::_float_sign_mask  = generate_fp_mask("float_sign_mask",  0x7FFFFFFF7FFFFFFF);
3998     StubRoutines::x86::_float_sign_flip  = generate_fp_mask("float_sign_flip",  0x8000000080000000);
3999     StubRoutines::x86::_double_sign_mask = generate_fp_mask("double_sign_mask", 0x7FFFFFFFFFFFFFFF);
4000     StubRoutines::x86::_double_sign_flip = generate_fp_mask("double_sign_flip", 0x8000000000000000);
4001 
4002     // support for verify_oop (must happen after universe_init)
4003     StubRoutines::_verify_oop_subroutine_entry = generate_verify_oop();
4004 
4005     // arraycopy stubs used by compilers
4006     generate_arraycopy_stubs();
4007 
4008     generate_math_stubs();
4009 
4010     // don't bother generating these AES intrinsic stubs unless global flag is set
4011     if (UseAESIntrinsics) {
4012       StubRoutines::x86::_key_shuffle_mask_addr = generate_key_shuffle_mask();  // needed by the others
4013 
4014       StubRoutines::_aescrypt_encryptBlock = generate_aescrypt_encryptBlock();
4015       StubRoutines::_aescrypt_decryptBlock = generate_aescrypt_decryptBlock();
4016       StubRoutines::_cipherBlockChaining_encryptAESCrypt = generate_cipherBlockChaining_encryptAESCrypt();
4017       StubRoutines::_cipherBlockChaining_decryptAESCrypt = generate_cipherBlockChaining_decryptAESCrypt_Parallel();
4018     }
4019 
4020     // Safefetch stubs.
4021     generate_safefetch("SafeFetch32", sizeof(int),     &StubRoutines::_safefetch32_entry,
4022                                                        &StubRoutines::_safefetch32_fault_pc,
4023                                                        &StubRoutines::_safefetch32_continuation_pc);
4024     generate_safefetch("SafeFetchN", sizeof(intptr_t), &StubRoutines::_safefetchN_entry,
4025                                                        &StubRoutines::_safefetchN_fault_pc,
4026                                                        &StubRoutines::_safefetchN_continuation_pc);
4027 #ifdef COMPILER2
4028     if (UseMultiplyToLenIntrinsic) {
4029       StubRoutines::_multiplyToLen = generate_multiplyToLen();
4030     }
4031 #endif
4032   }
4033 
4034  public:
4035   StubGenerator(CodeBuffer* code, bool all) : StubCodeGenerator(code) {
4036     if (all) {
4037       generate_all();
4038     } else {
4039       generate_initial();
4040     }
4041   }
4042 }; // end class declaration
4043 
4044 void StubGenerator_generate(CodeBuffer* code, bool all) {
4045   StubGenerator g(code, all);
4046 }