1 /*
   2  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/assembler.hpp"
  27 #include "assembler_x86.inline.hpp"
  28 #include "interpreter/interpreter.hpp"
  29 #include "nativeInst_x86.hpp"
  30 #include "oops/instanceOop.hpp"
  31 #include "oops/methodOop.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 "utilities/top.hpp"
  41 #ifdef TARGET_OS_FAMILY_linux
  42 # include "thread_linux.inline.hpp"
  43 #endif
  44 #ifdef TARGET_OS_FAMILY_solaris
  45 # include "thread_solaris.inline.hpp"
  46 #endif
  47 #ifdef TARGET_OS_FAMILY_windows
  48 # include "thread_windows.inline.hpp"
  49 #endif
  50 #ifdef COMPILER2
  51 #include "opto/runtime.hpp"
  52 #endif
  53 
  54 // Declaration and definition of StubGenerator (no .hpp file).
  55 // For a more detailed description of the stub routine structure
  56 // see the comment in stubRoutines.hpp
  57 
  58 #define __ _masm->
  59 #define TIMES_OOP (UseCompressedOops ? Address::times_4 : Address::times_8)
  60 #define a__ ((Assembler*)_masm)->
  61 
  62 #ifdef PRODUCT
  63 #define BLOCK_COMMENT(str) /* nothing */
  64 #else
  65 #define BLOCK_COMMENT(str) __ block_comment(str)
  66 #endif
  67 
  68 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
  69 const int MXCSR_MASK = 0xFFC0;  // Mask out any pending exceptions
  70 
  71 // Stub Code definitions
  72 
  73 static address handle_unsafe_access() {
  74   JavaThread* thread = JavaThread::current();
  75   address pc = thread->saved_exception_pc();
  76   // pc is the instruction which we must emulate
  77   // doing a no-op is fine:  return garbage from the load
  78   // therefore, compute npc
  79   address npc = Assembler::locate_next_instruction(pc);
  80 
  81   // request an async exception
  82   thread->set_pending_unsafe_access_error();
  83 
  84   // return address of next instruction to execute
  85   return npc;
  86 }
  87 
  88 class StubGenerator: public StubCodeGenerator {
  89  private:
  90 
  91 #ifdef PRODUCT
  92 #define inc_counter_np(counter) (0)
  93 #else
  94   void inc_counter_np_(int& counter) {
  95     __ incrementl(ExternalAddress((address)&counter));
  96   }
  97 #define inc_counter_np(counter) \
  98   BLOCK_COMMENT("inc_counter " #counter); \
  99   inc_counter_np_(counter);
 100 #endif
 101 
 102   // Call stubs are used to call Java from C
 103   //
 104   // Linux Arguments:
 105   //    c_rarg0:   call wrapper address                   address
 106   //    c_rarg1:   result                                 address
 107   //    c_rarg2:   result type                            BasicType
 108   //    c_rarg3:   method                                 methodOop
 109   //    c_rarg4:   (interpreter) entry point              address
 110   //    c_rarg5:   parameters                             intptr_t*
 111   //    16(rbp): parameter size (in words)              int
 112   //    24(rbp): thread                                 Thread*
 113   //
 114   //     [ return_from_Java     ] <--- rsp
 115   //     [ argument word n      ]
 116   //      ...
 117   // -12 [ argument word 1      ]
 118   // -11 [ saved r15            ] <--- rsp_after_call
 119   // -10 [ saved r14            ]
 120   //  -9 [ saved r13            ]
 121   //  -8 [ saved r12            ]
 122   //  -7 [ saved rbx            ]
 123   //  -6 [ call wrapper         ]
 124   //  -5 [ result               ]
 125   //  -4 [ result type          ]
 126   //  -3 [ method               ]
 127   //  -2 [ entry point          ]
 128   //  -1 [ parameters           ]
 129   //   0 [ saved rbp            ] <--- rbp
 130   //   1 [ return address       ]
 131   //   2 [ parameter size       ]
 132   //   3 [ thread               ]
 133   //
 134   // Windows Arguments:
 135   //    c_rarg0:   call wrapper address                   address
 136   //    c_rarg1:   result                                 address
 137   //    c_rarg2:   result type                            BasicType
 138   //    c_rarg3:   method                                 methodOop
 139   //    48(rbp): (interpreter) entry point              address
 140   //    56(rbp): parameters                             intptr_t*
 141   //    64(rbp): parameter size (in words)              int
 142   //    72(rbp): thread                                 Thread*
 143   //
 144   //     [ return_from_Java     ] <--- rsp
 145   //     [ argument word n      ]
 146   //      ...
 147   //  -8 [ argument word 1      ]
 148   //  -7 [ saved r15            ] <--- rsp_after_call
 149   //  -6 [ saved r14            ]
 150   //  -5 [ saved r13            ]
 151   //  -4 [ saved r12            ]
 152   //  -3 [ saved rdi            ]
 153   //  -2 [ saved rsi            ]
 154   //  -1 [ saved rbx            ]
 155   //   0 [ saved rbp            ] <--- rbp
 156   //   1 [ return address       ]
 157   //   2 [ call wrapper         ]
 158   //   3 [ result               ]
 159   //   4 [ result type          ]
 160   //   5 [ method               ]
 161   //   6 [ entry point          ]
 162   //   7 [ parameters           ]
 163   //   8 [ parameter size       ]
 164   //   9 [ thread               ]
 165   //
 166   //    Windows reserves the callers stack space for arguments 1-4.
 167   //    We spill c_rarg0-c_rarg3 to this space.
 168 
 169   // Call stub stack layout word offsets from rbp
 170   enum call_stub_layout {
 171 #ifdef _WIN64
 172     rsp_after_call_off = -7,
 173     r15_off            = rsp_after_call_off,
 174     r14_off            = -6,
 175     r13_off            = -5,
 176     r12_off            = -4,
 177     rdi_off            = -3,
 178     rsi_off            = -2,
 179     rbx_off            = -1,
 180     rbp_off            =  0,
 181     retaddr_off        =  1,
 182     call_wrapper_off   =  2,
 183     result_off         =  3,
 184     result_type_off    =  4,
 185     method_off         =  5,
 186     entry_point_off    =  6,
 187     parameters_off     =  7,
 188     parameter_size_off =  8,
 189     thread_off         =  9
 190 #else
 191     rsp_after_call_off = -12,
 192     mxcsr_off          = rsp_after_call_off,
 193     r15_off            = -11,
 194     r14_off            = -10,
 195     r13_off            = -9,
 196     r12_off            = -8,
 197     rbx_off            = -7,
 198     call_wrapper_off   = -6,
 199     result_off         = -5,
 200     result_type_off    = -4,
 201     method_off         = -3,
 202     entry_point_off    = -2,
 203     parameters_off     = -1,
 204     rbp_off            =  0,
 205     retaddr_off        =  1,
 206     parameter_size_off =  2,
 207     thread_off         =  3
 208 #endif
 209   };
 210 
 211   address generate_call_stub(address& return_address) {
 212     assert((int)frame::entry_frame_after_call_words == -(int)rsp_after_call_off + 1 &&
 213            (int)frame::entry_frame_call_wrapper_offset == (int)call_wrapper_off,
 214            "adjust this code");
 215     StubCodeMark mark(this, "StubRoutines", "call_stub");
 216     address start = __ pc();
 217 
 218     // same as in generate_catch_exception()!
 219     const Address rsp_after_call(rbp, rsp_after_call_off * wordSize);
 220 
 221     const Address call_wrapper  (rbp, call_wrapper_off   * wordSize);
 222     const Address result        (rbp, result_off         * wordSize);
 223     const Address result_type   (rbp, result_type_off    * wordSize);
 224     const Address method        (rbp, method_off         * wordSize);
 225     const Address entry_point   (rbp, entry_point_off    * wordSize);
 226     const Address parameters    (rbp, parameters_off     * wordSize);
 227     const Address parameter_size(rbp, parameter_size_off * wordSize);
 228 
 229     // same as in generate_catch_exception()!
 230     const Address thread        (rbp, thread_off         * wordSize);
 231 
 232     const Address r15_save(rbp, r15_off * wordSize);
 233     const Address r14_save(rbp, r14_off * wordSize);
 234     const Address r13_save(rbp, r13_off * wordSize);
 235     const Address r12_save(rbp, r12_off * wordSize);
 236     const Address rbx_save(rbp, rbx_off * wordSize);
 237 
 238     // stub code
 239     __ enter();
 240     __ subptr(rsp, -rsp_after_call_off * wordSize);
 241 
 242     // save register parameters
 243 #ifndef _WIN64
 244     __ movptr(parameters,   c_rarg5); // parameters
 245     __ movptr(entry_point,  c_rarg4); // entry_point
 246 #endif
 247 
 248     __ movptr(method,       c_rarg3); // method
 249     __ movl(result_type,  c_rarg2);   // result type
 250     __ movptr(result,       c_rarg1); // result
 251     __ movptr(call_wrapper, c_rarg0); // call wrapper
 252 
 253     // save regs belonging to calling function
 254     __ movptr(rbx_save, rbx);
 255     __ movptr(r12_save, r12);
 256     __ movptr(r13_save, r13);
 257     __ movptr(r14_save, r14);
 258     __ movptr(r15_save, r15);
 259 
 260 #ifdef _WIN64
 261     const Address rdi_save(rbp, rdi_off * wordSize);
 262     const Address rsi_save(rbp, rsi_off * wordSize);
 263 
 264     __ movptr(rsi_save, rsi);
 265     __ movptr(rdi_save, rdi);
 266 #else
 267     const Address mxcsr_save(rbp, mxcsr_off * wordSize);
 268     {
 269       Label skip_ldmx;
 270       __ stmxcsr(mxcsr_save);
 271       __ movl(rax, mxcsr_save);
 272       __ andl(rax, MXCSR_MASK);    // Only check control and mask bits
 273       ExternalAddress mxcsr_std(StubRoutines::x86::mxcsr_std());
 274       __ cmp32(rax, mxcsr_std);
 275       __ jcc(Assembler::equal, skip_ldmx);
 276       __ ldmxcsr(mxcsr_std);
 277       __ bind(skip_ldmx);
 278     }
 279 #endif
 280 
 281     // Load up thread register
 282     __ movptr(r15_thread, thread);
 283     __ reinit_heapbase();
 284 
 285 #ifdef ASSERT
 286     // make sure we have no pending exceptions
 287     {
 288       Label L;
 289       __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 290       __ jcc(Assembler::equal, L);
 291       __ stop("StubRoutines::call_stub: entered with pending exception");
 292       __ bind(L);
 293     }
 294 #endif
 295 
 296     // pass parameters if any
 297     BLOCK_COMMENT("pass parameters if any");
 298     Label parameters_done;
 299     __ movl(c_rarg3, parameter_size);
 300     __ testl(c_rarg3, c_rarg3);
 301     __ jcc(Assembler::zero, parameters_done);
 302 
 303     Label loop;
 304     __ movptr(c_rarg2, parameters);       // parameter pointer
 305     __ movl(c_rarg1, c_rarg3);            // parameter counter is in c_rarg1
 306     __ BIND(loop);
 307     __ movptr(rax, Address(c_rarg2, 0));// get parameter
 308     __ addptr(c_rarg2, wordSize);       // advance to next parameter
 309     __ decrementl(c_rarg1);             // decrement counter
 310     __ push(rax);                       // pass parameter
 311     __ jcc(Assembler::notZero, loop);
 312 
 313     // call Java function
 314     __ BIND(parameters_done);
 315     __ movptr(rbx, method);             // get methodOop
 316     __ movptr(c_rarg1, entry_point);    // get entry_point
 317     __ mov(r13, rsp);                   // set sender sp
 318     BLOCK_COMMENT("call Java function");
 319     __ call(c_rarg1);
 320 
 321     BLOCK_COMMENT("call_stub_return_address:");
 322     return_address = __ pc();
 323 
 324     // store result depending on type (everything that is not
 325     // T_OBJECT, T_LONG, T_FLOAT or T_DOUBLE is treated as T_INT)
 326     __ movptr(c_rarg0, result);
 327     Label is_long, is_float, is_double, exit;
 328     __ movl(c_rarg1, result_type);
 329     __ cmpl(c_rarg1, T_OBJECT);
 330     __ jcc(Assembler::equal, is_long);
 331     __ cmpl(c_rarg1, T_LONG);
 332     __ jcc(Assembler::equal, is_long);
 333     __ cmpl(c_rarg1, T_FLOAT);
 334     __ jcc(Assembler::equal, is_float);
 335     __ cmpl(c_rarg1, T_DOUBLE);
 336     __ jcc(Assembler::equal, is_double);
 337 
 338     // handle T_INT case
 339     __ movl(Address(c_rarg0, 0), rax);
 340 
 341     __ BIND(exit);
 342 
 343     // pop parameters
 344     __ lea(rsp, rsp_after_call);
 345 
 346 #ifdef ASSERT
 347     // verify that threads correspond
 348     {
 349       Label L, S;
 350       __ cmpptr(r15_thread, thread);
 351       __ jcc(Assembler::notEqual, S);
 352       __ get_thread(rbx);
 353       __ cmpptr(r15_thread, rbx);
 354       __ jcc(Assembler::equal, L);
 355       __ bind(S);
 356       __ jcc(Assembler::equal, L);
 357       __ stop("StubRoutines::call_stub: threads must correspond");
 358       __ bind(L);
 359     }
 360 #endif
 361 
 362     // restore regs belonging to calling function
 363     __ movptr(r15, r15_save);
 364     __ movptr(r14, r14_save);
 365     __ movptr(r13, r13_save);
 366     __ movptr(r12, r12_save);
 367     __ movptr(rbx, rbx_save);
 368 
 369 #ifdef _WIN64
 370     __ movptr(rdi, rdi_save);
 371     __ movptr(rsi, rsi_save);
 372 #else
 373     __ ldmxcsr(mxcsr_save);
 374 #endif
 375 
 376     // restore rsp
 377     __ addptr(rsp, -rsp_after_call_off * wordSize);
 378 
 379     // return
 380     __ pop(rbp);
 381     __ ret(0);
 382 
 383     // handle return types different from T_INT
 384     __ BIND(is_long);
 385     __ movq(Address(c_rarg0, 0), rax);
 386     __ jmp(exit);
 387 
 388     __ BIND(is_float);
 389     __ movflt(Address(c_rarg0, 0), xmm0);
 390     __ jmp(exit);
 391 
 392     __ BIND(is_double);
 393     __ movdbl(Address(c_rarg0, 0), xmm0);
 394     __ jmp(exit);
 395 
 396     return start;
 397   }
 398 
 399   // Return point for a Java call if there's an exception thrown in
 400   // Java code.  The exception is caught and transformed into a
 401   // pending exception stored in JavaThread that can be tested from
 402   // within the VM.
 403   //
 404   // Note: Usually the parameters are removed by the callee. In case
 405   // of an exception crossing an activation frame boundary, that is
 406   // not the case if the callee is compiled code => need to setup the
 407   // rsp.
 408   //
 409   // rax: exception oop
 410 
 411   address generate_catch_exception() {
 412     StubCodeMark mark(this, "StubRoutines", "catch_exception");
 413     address start = __ pc();
 414 
 415     // same as in generate_call_stub():
 416     const Address rsp_after_call(rbp, rsp_after_call_off * wordSize);
 417     const Address thread        (rbp, thread_off         * wordSize);
 418 
 419 #ifdef ASSERT
 420     // verify that threads correspond
 421     {
 422       Label L, S;
 423       __ cmpptr(r15_thread, thread);
 424       __ jcc(Assembler::notEqual, S);
 425       __ get_thread(rbx);
 426       __ cmpptr(r15_thread, rbx);
 427       __ jcc(Assembler::equal, L);
 428       __ bind(S);
 429       __ stop("StubRoutines::catch_exception: threads must correspond");
 430       __ bind(L);
 431     }
 432 #endif
 433 
 434     // set pending exception
 435     __ verify_oop(rax);
 436 
 437     __ movptr(Address(r15_thread, Thread::pending_exception_offset()), rax);
 438     __ lea(rscratch1, ExternalAddress((address)__FILE__));
 439     __ movptr(Address(r15_thread, Thread::exception_file_offset()), rscratch1);
 440     __ movl(Address(r15_thread, Thread::exception_line_offset()), (int)  __LINE__);
 441 
 442     // complete return to VM
 443     assert(StubRoutines::_call_stub_return_address != NULL,
 444            "_call_stub_return_address must have been generated before");
 445     __ jump(RuntimeAddress(StubRoutines::_call_stub_return_address));
 446 
 447     return start;
 448   }
 449 
 450   // Continuation point for runtime calls returning with a pending
 451   // exception.  The pending exception check happened in the runtime
 452   // or native call stub.  The pending exception in Thread is
 453   // converted into a Java-level exception.
 454   //
 455   // Contract with Java-level exception handlers:
 456   // rax: exception
 457   // rdx: throwing pc
 458   //
 459   // NOTE: At entry of this stub, exception-pc must be on stack !!
 460 
 461   address generate_forward_exception() {
 462     StubCodeMark mark(this, "StubRoutines", "forward exception");
 463     address start = __ pc();
 464 
 465     // Upon entry, the sp points to the return address returning into
 466     // Java (interpreted or compiled) code; i.e., the return address
 467     // becomes the throwing pc.
 468     //
 469     // Arguments pushed before the runtime call are still on the stack
 470     // but the exception handler will reset the stack pointer ->
 471     // ignore them.  A potential result in registers can be ignored as
 472     // well.
 473 
 474 #ifdef ASSERT
 475     // make sure this code is only executed if there is a pending exception
 476     {
 477       Label L;
 478       __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t) NULL);
 479       __ jcc(Assembler::notEqual, L);
 480       __ stop("StubRoutines::forward exception: no pending exception (1)");
 481       __ bind(L);
 482     }
 483 #endif
 484 
 485     // compute exception handler into rbx
 486     __ movptr(c_rarg0, Address(rsp, 0));
 487     BLOCK_COMMENT("call exception_handler_for_return_address");
 488     __ call_VM_leaf(CAST_FROM_FN_PTR(address,
 489                          SharedRuntime::exception_handler_for_return_address),
 490                     r15_thread, c_rarg0);
 491     __ mov(rbx, rax);
 492 
 493     // setup rax & rdx, remove return address & clear pending exception
 494     __ pop(rdx);
 495     __ movptr(rax, Address(r15_thread, Thread::pending_exception_offset()));
 496     __ movptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 497 
 498 #ifdef ASSERT
 499     // make sure exception is set
 500     {
 501       Label L;
 502       __ testptr(rax, rax);
 503       __ jcc(Assembler::notEqual, L);
 504       __ stop("StubRoutines::forward exception: no pending exception (2)");
 505       __ bind(L);
 506     }
 507 #endif
 508 
 509     // continue at exception handler (return address removed)
 510     // rax: exception
 511     // rbx: exception handler
 512     // rdx: throwing pc
 513     __ verify_oop(rax);
 514     __ jmp(rbx);
 515 
 516     return start;
 517   }
 518 
 519   // Support for jint atomic::xchg(jint exchange_value, volatile jint* dest)
 520   //
 521   // Arguments :
 522   //    c_rarg0: exchange_value
 523   //    c_rarg0: dest
 524   //
 525   // Result:
 526   //    *dest <- ex, return (orig *dest)
 527   address generate_atomic_xchg() {
 528     StubCodeMark mark(this, "StubRoutines", "atomic_xchg");
 529     address start = __ pc();
 530 
 531     __ movl(rax, c_rarg0); // Copy to eax we need a return value anyhow
 532     __ xchgl(rax, Address(c_rarg1, 0)); // automatic LOCK
 533     __ ret(0);
 534 
 535     return start;
 536   }
 537 
 538   // Support for intptr_t atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest)
 539   //
 540   // Arguments :
 541   //    c_rarg0: exchange_value
 542   //    c_rarg1: dest
 543   //
 544   // Result:
 545   //    *dest <- ex, return (orig *dest)
 546   address generate_atomic_xchg_ptr() {
 547     StubCodeMark mark(this, "StubRoutines", "atomic_xchg_ptr");
 548     address start = __ pc();
 549 
 550     __ movptr(rax, c_rarg0); // Copy to eax we need a return value anyhow
 551     __ xchgptr(rax, Address(c_rarg1, 0)); // automatic LOCK
 552     __ ret(0);
 553 
 554     return start;
 555   }
 556 
 557   // Support for jint atomic::atomic_cmpxchg(jint exchange_value, volatile jint* dest,
 558   //                                         jint compare_value)
 559   //
 560   // Arguments :
 561   //    c_rarg0: exchange_value
 562   //    c_rarg1: dest
 563   //    c_rarg2: compare_value
 564   //
 565   // Result:
 566   //    if ( compare_value == *dest ) {
 567   //       *dest = exchange_value
 568   //       return compare_value;
 569   //    else
 570   //       return *dest;
 571   address generate_atomic_cmpxchg() {
 572     StubCodeMark mark(this, "StubRoutines", "atomic_cmpxchg");
 573     address start = __ pc();
 574 
 575     __ movl(rax, c_rarg2);
 576    if ( os::is_MP() ) __ lock();
 577     __ cmpxchgl(c_rarg0, Address(c_rarg1, 0));
 578     __ ret(0);
 579 
 580     return start;
 581   }
 582 
 583   // Support for jint atomic::atomic_cmpxchg_long(jlong exchange_value,
 584   //                                             volatile jlong* dest,
 585   //                                             jlong compare_value)
 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_long() {
 598     StubCodeMark mark(this, "StubRoutines", "atomic_cmpxchg_long");
 599     address start = __ pc();
 600 
 601     __ movq(rax, c_rarg2);
 602    if ( os::is_MP() ) __ lock();
 603     __ cmpxchgq(c_rarg0, Address(c_rarg1, 0));
 604     __ ret(0);
 605 
 606     return start;
 607   }
 608 
 609   // Support for jint atomic::add(jint add_value, volatile jint* dest)
 610   //
 611   // Arguments :
 612   //    c_rarg0: add_value
 613   //    c_rarg1: dest
 614   //
 615   // Result:
 616   //    *dest += add_value
 617   //    return *dest;
 618   address generate_atomic_add() {
 619     StubCodeMark mark(this, "StubRoutines", "atomic_add");
 620     address start = __ pc();
 621 
 622     __ movl(rax, c_rarg0);
 623    if ( os::is_MP() ) __ lock();
 624     __ xaddl(Address(c_rarg1, 0), c_rarg0);
 625     __ addl(rax, c_rarg0);
 626     __ ret(0);
 627 
 628     return start;
 629   }
 630 
 631   // Support for intptr_t atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest)
 632   //
 633   // Arguments :
 634   //    c_rarg0: add_value
 635   //    c_rarg1: dest
 636   //
 637   // Result:
 638   //    *dest += add_value
 639   //    return *dest;
 640   address generate_atomic_add_ptr() {
 641     StubCodeMark mark(this, "StubRoutines", "atomic_add_ptr");
 642     address start = __ pc();
 643 
 644     __ movptr(rax, c_rarg0); // Copy to eax we need a return value anyhow
 645    if ( os::is_MP() ) __ lock();
 646     __ xaddptr(Address(c_rarg1, 0), c_rarg0);
 647     __ addptr(rax, c_rarg0);
 648     __ ret(0);
 649 
 650     return start;
 651   }
 652 
 653   // Support for intptr_t OrderAccess::fence()
 654   //
 655   // Arguments :
 656   //
 657   // Result:
 658   address generate_orderaccess_fence() {
 659     StubCodeMark mark(this, "StubRoutines", "orderaccess_fence");
 660     address start = __ pc();
 661     __ membar(Assembler::StoreLoad);
 662     __ ret(0);
 663 
 664     return start;
 665   }
 666 
 667   // Support for intptr_t get_previous_fp()
 668   //
 669   // This routine is used to find the previous frame pointer for the
 670   // caller (current_frame_guess). This is used as part of debugging
 671   // ps() is seemingly lost trying to find frames.
 672   // This code assumes that caller current_frame_guess) has a frame.
 673   address generate_get_previous_fp() {
 674     StubCodeMark mark(this, "StubRoutines", "get_previous_fp");
 675     const Address old_fp(rbp, 0);
 676     const Address older_fp(rax, 0);
 677     address start = __ pc();
 678 
 679     __ enter();
 680     __ movptr(rax, old_fp); // callers fp
 681     __ movptr(rax, older_fp); // the frame for ps()
 682     __ pop(rbp);
 683     __ ret(0);
 684 
 685     return start;
 686   }
 687 
 688   //----------------------------------------------------------------------------------------------------
 689   // Support for void verify_mxcsr()
 690   //
 691   // This routine is used with -Xcheck:jni to verify that native
 692   // JNI code does not return to Java code without restoring the
 693   // MXCSR register to our expected state.
 694 
 695   address generate_verify_mxcsr() {
 696     StubCodeMark mark(this, "StubRoutines", "verify_mxcsr");
 697     address start = __ pc();
 698 
 699     const Address mxcsr_save(rsp, 0);
 700 
 701     if (CheckJNICalls) {
 702       Label ok_ret;
 703       __ push(rax);
 704       __ subptr(rsp, wordSize);      // allocate a temp location
 705       __ stmxcsr(mxcsr_save);
 706       __ movl(rax, mxcsr_save);
 707       __ andl(rax, MXCSR_MASK);    // Only check control and mask bits
 708       __ cmpl(rax, *(int *)(StubRoutines::x86::mxcsr_std()));
 709       __ jcc(Assembler::equal, ok_ret);
 710 
 711       __ warn("MXCSR changed by native JNI code, use -XX:+RestoreMXCSROnJNICall");
 712 
 713       __ ldmxcsr(ExternalAddress(StubRoutines::x86::mxcsr_std()));
 714 
 715       __ bind(ok_ret);
 716       __ addptr(rsp, wordSize);
 717       __ pop(rax);
 718     }
 719 
 720     __ ret(0);
 721 
 722     return start;
 723   }
 724 
 725   address generate_f2i_fixup() {
 726     StubCodeMark mark(this, "StubRoutines", "f2i_fixup");
 727     Address inout(rsp, 5 * wordSize); // return address + 4 saves
 728 
 729     address start = __ pc();
 730 
 731     Label L;
 732 
 733     __ push(rax);
 734     __ push(c_rarg3);
 735     __ push(c_rarg2);
 736     __ push(c_rarg1);
 737 
 738     __ movl(rax, 0x7f800000);
 739     __ xorl(c_rarg3, c_rarg3);
 740     __ movl(c_rarg2, inout);
 741     __ movl(c_rarg1, c_rarg2);
 742     __ andl(c_rarg1, 0x7fffffff);
 743     __ cmpl(rax, c_rarg1); // NaN? -> 0
 744     __ jcc(Assembler::negative, L);
 745     __ testl(c_rarg2, c_rarg2); // signed ? min_jint : max_jint
 746     __ movl(c_rarg3, 0x80000000);
 747     __ movl(rax, 0x7fffffff);
 748     __ cmovl(Assembler::positive, c_rarg3, rax);
 749 
 750     __ bind(L);
 751     __ movptr(inout, c_rarg3);
 752 
 753     __ pop(c_rarg1);
 754     __ pop(c_rarg2);
 755     __ pop(c_rarg3);
 756     __ pop(rax);
 757 
 758     __ ret(0);
 759 
 760     return start;
 761   }
 762 
 763   address generate_f2l_fixup() {
 764     StubCodeMark mark(this, "StubRoutines", "f2l_fixup");
 765     Address inout(rsp, 5 * wordSize); // return address + 4 saves
 766     address start = __ pc();
 767 
 768     Label L;
 769 
 770     __ push(rax);
 771     __ push(c_rarg3);
 772     __ push(c_rarg2);
 773     __ push(c_rarg1);
 774 
 775     __ movl(rax, 0x7f800000);
 776     __ xorl(c_rarg3, c_rarg3);
 777     __ movl(c_rarg2, inout);
 778     __ movl(c_rarg1, c_rarg2);
 779     __ andl(c_rarg1, 0x7fffffff);
 780     __ cmpl(rax, c_rarg1); // NaN? -> 0
 781     __ jcc(Assembler::negative, L);
 782     __ testl(c_rarg2, c_rarg2); // signed ? min_jlong : max_jlong
 783     __ mov64(c_rarg3, 0x8000000000000000);
 784     __ mov64(rax, 0x7fffffffffffffff);
 785     __ cmov(Assembler::positive, c_rarg3, rax);
 786 
 787     __ bind(L);
 788     __ movptr(inout, c_rarg3);
 789 
 790     __ pop(c_rarg1);
 791     __ pop(c_rarg2);
 792     __ pop(c_rarg3);
 793     __ pop(rax);
 794 
 795     __ ret(0);
 796 
 797     return start;
 798   }
 799 
 800   address generate_d2i_fixup() {
 801     StubCodeMark mark(this, "StubRoutines", "d2i_fixup");
 802     Address inout(rsp, 6 * wordSize); // return address + 5 saves
 803 
 804     address start = __ pc();
 805 
 806     Label L;
 807 
 808     __ push(rax);
 809     __ push(c_rarg3);
 810     __ push(c_rarg2);
 811     __ push(c_rarg1);
 812     __ push(c_rarg0);
 813 
 814     __ movl(rax, 0x7ff00000);
 815     __ movq(c_rarg2, inout);
 816     __ movl(c_rarg3, c_rarg2);
 817     __ mov(c_rarg1, c_rarg2);
 818     __ mov(c_rarg0, c_rarg2);
 819     __ negl(c_rarg3);
 820     __ shrptr(c_rarg1, 0x20);
 821     __ orl(c_rarg3, c_rarg2);
 822     __ andl(c_rarg1, 0x7fffffff);
 823     __ xorl(c_rarg2, c_rarg2);
 824     __ shrl(c_rarg3, 0x1f);
 825     __ orl(c_rarg1, c_rarg3);
 826     __ cmpl(rax, c_rarg1);
 827     __ jcc(Assembler::negative, L); // NaN -> 0
 828     __ testptr(c_rarg0, c_rarg0); // signed ? min_jint : max_jint
 829     __ movl(c_rarg2, 0x80000000);
 830     __ movl(rax, 0x7fffffff);
 831     __ cmov(Assembler::positive, c_rarg2, rax);
 832 
 833     __ bind(L);
 834     __ movptr(inout, c_rarg2);
 835 
 836     __ pop(c_rarg0);
 837     __ pop(c_rarg1);
 838     __ pop(c_rarg2);
 839     __ pop(c_rarg3);
 840     __ pop(rax);
 841 
 842     __ ret(0);
 843 
 844     return start;
 845   }
 846 
 847   address generate_d2l_fixup() {
 848     StubCodeMark mark(this, "StubRoutines", "d2l_fixup");
 849     Address inout(rsp, 6 * wordSize); // return address + 5 saves
 850 
 851     address start = __ pc();
 852 
 853     Label L;
 854 
 855     __ push(rax);
 856     __ push(c_rarg3);
 857     __ push(c_rarg2);
 858     __ push(c_rarg1);
 859     __ push(c_rarg0);
 860 
 861     __ movl(rax, 0x7ff00000);
 862     __ movq(c_rarg2, inout);
 863     __ movl(c_rarg3, c_rarg2);
 864     __ mov(c_rarg1, c_rarg2);
 865     __ mov(c_rarg0, c_rarg2);
 866     __ negl(c_rarg3);
 867     __ shrptr(c_rarg1, 0x20);
 868     __ orl(c_rarg3, c_rarg2);
 869     __ andl(c_rarg1, 0x7fffffff);
 870     __ xorl(c_rarg2, c_rarg2);
 871     __ shrl(c_rarg3, 0x1f);
 872     __ orl(c_rarg1, c_rarg3);
 873     __ cmpl(rax, c_rarg1);
 874     __ jcc(Assembler::negative, L); // NaN -> 0
 875     __ testq(c_rarg0, c_rarg0); // signed ? min_jlong : max_jlong
 876     __ mov64(c_rarg2, 0x8000000000000000);
 877     __ mov64(rax, 0x7fffffffffffffff);
 878     __ cmovq(Assembler::positive, c_rarg2, rax);
 879 
 880     __ bind(L);
 881     __ movq(inout, c_rarg2);
 882 
 883     __ pop(c_rarg0);
 884     __ pop(c_rarg1);
 885     __ pop(c_rarg2);
 886     __ pop(c_rarg3);
 887     __ pop(rax);
 888 
 889     __ ret(0);
 890 
 891     return start;
 892   }
 893 
 894   address generate_fp_mask(const char *stub_name, int64_t mask) {
 895     __ align(CodeEntryAlignment);
 896     StubCodeMark mark(this, "StubRoutines", stub_name);
 897     address start = __ pc();
 898 
 899     __ emit_data64( mask, relocInfo::none );
 900     __ emit_data64( mask, relocInfo::none );
 901 
 902     return start;
 903   }
 904 
 905   // The following routine generates a subroutine to throw an
 906   // asynchronous UnknownError when an unsafe access gets a fault that
 907   // could not be reasonably prevented by the programmer.  (Example:
 908   // SIGBUS/OBJERR.)
 909   address generate_handler_for_unsafe_access() {
 910     StubCodeMark mark(this, "StubRoutines", "handler_for_unsafe_access");
 911     address start = __ pc();
 912 
 913     __ push(0);                       // hole for return address-to-be
 914     __ pusha();                       // push registers
 915     Address next_pc(rsp, RegisterImpl::number_of_registers * BytesPerWord);
 916 
 917     __ subptr(rsp, frame::arg_reg_save_area_bytes);
 918     BLOCK_COMMENT("call handle_unsafe_access");
 919     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, handle_unsafe_access)));
 920     __ addptr(rsp, frame::arg_reg_save_area_bytes);
 921 
 922     __ movptr(next_pc, rax);          // stuff next address
 923     __ popa();
 924     __ ret(0);                        // jump to next address
 925 
 926     return start;
 927   }
 928 
 929   // Non-destructive plausibility checks for oops
 930   //
 931   // Arguments:
 932   //    all args on stack!
 933   //
 934   // Stack after saving c_rarg3:
 935   //    [tos + 0]: saved c_rarg3
 936   //    [tos + 1]: saved c_rarg2
 937   //    [tos + 2]: saved r12 (several TemplateTable methods use it)
 938   //    [tos + 3]: saved flags
 939   //    [tos + 4]: return address
 940   //  * [tos + 5]: error message (char*)
 941   //  * [tos + 6]: object to verify (oop)
 942   //  * [tos + 7]: saved rax - saved by caller and bashed
 943   //  * [tos + 8]: saved r10 (rscratch1) - saved by caller
 944   //  * = popped on exit
 945   address generate_verify_oop() {
 946     StubCodeMark mark(this, "StubRoutines", "verify_oop");
 947     address start = __ pc();
 948 
 949     Label exit, error;
 950 
 951     __ pushf();
 952     __ incrementl(ExternalAddress((address) StubRoutines::verify_oop_count_addr()));
 953 
 954     __ push(r12);
 955 
 956     // save c_rarg2 and c_rarg3
 957     __ push(c_rarg2);
 958     __ push(c_rarg3);
 959 
 960     enum {
 961            // After previous pushes.
 962            oop_to_verify = 6 * wordSize,
 963            saved_rax     = 7 * wordSize,
 964            saved_r10     = 8 * wordSize,
 965 
 966            // Before the call to MacroAssembler::debug(), see below.
 967            return_addr   = 16 * wordSize,
 968            error_msg     = 17 * wordSize
 969     };
 970 
 971     // get object
 972     __ movptr(rax, Address(rsp, oop_to_verify));
 973 
 974     // make sure object is 'reasonable'
 975     __ testptr(rax, rax);
 976     __ jcc(Assembler::zero, exit); // if obj is NULL it is OK
 977     // Check if the oop is in the right area of memory
 978     __ movptr(c_rarg2, rax);
 979     __ movptr(c_rarg3, (intptr_t) Universe::verify_oop_mask());
 980     __ andptr(c_rarg2, c_rarg3);
 981     __ movptr(c_rarg3, (intptr_t) Universe::verify_oop_bits());
 982     __ cmpptr(c_rarg2, c_rarg3);
 983     __ jcc(Assembler::notZero, error);
 984 
 985     // set r12 to heapbase for load_klass()
 986     __ reinit_heapbase();
 987 
 988     // make sure klass is 'reasonable'
 989     __ load_klass(rax, rax);  // get klass
 990     __ testptr(rax, rax);
 991     __ jcc(Assembler::zero, error); // if klass is NULL it is broken
 992     // Check if the klass is in the right area of memory
 993     __ mov(c_rarg2, rax);
 994     __ movptr(c_rarg3, (intptr_t) Universe::verify_klass_mask());
 995     __ andptr(c_rarg2, c_rarg3);
 996     __ movptr(c_rarg3, (intptr_t) Universe::verify_klass_bits());
 997     __ cmpptr(c_rarg2, c_rarg3);
 998     __ jcc(Assembler::notZero, error);
 999 
1000     // make sure klass' klass is 'reasonable'
1001     __ load_klass(rax, rax);
1002     __ testptr(rax, rax);
1003     __ jcc(Assembler::zero, error); // if klass' klass is NULL it is broken
1004     // Check if the klass' klass is in the right area of memory
1005     __ movptr(c_rarg3, (intptr_t) Universe::verify_klass_mask());
1006     __ andptr(rax, c_rarg3);
1007     __ movptr(c_rarg3, (intptr_t) Universe::verify_klass_bits());
1008     __ cmpptr(rax, c_rarg3);
1009     __ jcc(Assembler::notZero, error);
1010 
1011     // return if everything seems ok
1012     __ bind(exit);
1013     __ movptr(rax, Address(rsp, saved_rax));     // get saved rax back
1014     __ movptr(rscratch1, Address(rsp, saved_r10)); // get saved r10 back
1015     __ pop(c_rarg3);                             // restore c_rarg3
1016     __ pop(c_rarg2);                             // restore c_rarg2
1017     __ pop(r12);                                 // restore r12
1018     __ popf();                                   // restore flags
1019     __ ret(4 * wordSize);                        // pop caller saved stuff
1020 
1021     // handle errors
1022     __ bind(error);
1023     __ movptr(rax, Address(rsp, saved_rax));     // get saved rax back
1024     __ movptr(rscratch1, Address(rsp, saved_r10)); // get saved r10 back
1025     __ pop(c_rarg3);                             // get saved c_rarg3 back
1026     __ pop(c_rarg2);                             // get saved c_rarg2 back
1027     __ pop(r12);                                 // get saved r12 back
1028     __ popf();                                   // get saved flags off stack --
1029                                                  // will be ignored
1030 
1031     __ pusha();                                  // push registers
1032                                                  // (rip is already
1033                                                  // already pushed)
1034     // debug(char* msg, int64_t pc, int64_t regs[])
1035     // We've popped the registers we'd saved (c_rarg3, c_rarg2 and flags), and
1036     // pushed all the registers, so now the stack looks like:
1037     //     [tos +  0] 16 saved registers
1038     //     [tos + 16] return address
1039     //   * [tos + 17] error message (char*)
1040     //   * [tos + 18] object to verify (oop)
1041     //   * [tos + 19] saved rax - saved by caller and bashed
1042     //   * [tos + 20] saved r10 (rscratch1) - saved by caller
1043     //   * = popped on exit
1044 
1045     __ movptr(c_rarg0, Address(rsp, error_msg));    // pass address of error message
1046     __ movptr(c_rarg1, Address(rsp, return_addr));  // pass return address
1047     __ movq(c_rarg2, rsp);                          // pass address of regs on stack
1048     __ mov(r12, rsp);                               // remember rsp
1049     __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
1050     __ andptr(rsp, -16);                            // align stack as required by ABI
1051     BLOCK_COMMENT("call MacroAssembler::debug");
1052     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, MacroAssembler::debug64)));
1053     __ mov(rsp, r12);                               // restore rsp
1054     __ popa();                                      // pop registers (includes r12)
1055     __ ret(4 * wordSize);                           // pop caller saved stuff
1056 
1057     return start;
1058   }
1059 
1060   //
1061   // Verify that a register contains clean 32-bits positive value
1062   // (high 32-bits are 0) so it could be used in 64-bits shifts.
1063   //
1064   //  Input:
1065   //    Rint  -  32-bits value
1066   //    Rtmp  -  scratch
1067   //
1068   void assert_clean_int(Register Rint, Register Rtmp) {
1069 #ifdef ASSERT
1070     Label L;
1071     assert_different_registers(Rtmp, Rint);
1072     __ movslq(Rtmp, Rint);
1073     __ cmpq(Rtmp, Rint);
1074     __ jcc(Assembler::equal, L);
1075     __ stop("high 32-bits of int value are not 0");
1076     __ bind(L);
1077 #endif
1078   }
1079 
1080   //  Generate overlap test for array copy stubs
1081   //
1082   //  Input:
1083   //     c_rarg0 - from
1084   //     c_rarg1 - to
1085   //     c_rarg2 - element count
1086   //
1087   //  Output:
1088   //     rax   - &from[element count - 1]
1089   //
1090   void array_overlap_test(address no_overlap_target, Address::ScaleFactor sf) {
1091     assert(no_overlap_target != NULL, "must be generated");
1092     array_overlap_test(no_overlap_target, NULL, sf);
1093   }
1094   void array_overlap_test(Label& L_no_overlap, Address::ScaleFactor sf) {
1095     array_overlap_test(NULL, &L_no_overlap, sf);
1096   }
1097   void array_overlap_test(address no_overlap_target, Label* NOLp, Address::ScaleFactor sf) {
1098     const Register from     = c_rarg0;
1099     const Register to       = c_rarg1;
1100     const Register count    = c_rarg2;
1101     const Register end_from = rax;
1102 
1103     __ cmpptr(to, from);
1104     __ lea(end_from, Address(from, count, sf, 0));
1105     if (NOLp == NULL) {
1106       ExternalAddress no_overlap(no_overlap_target);
1107       __ jump_cc(Assembler::belowEqual, no_overlap);
1108       __ cmpptr(to, end_from);
1109       __ jump_cc(Assembler::aboveEqual, no_overlap);
1110     } else {
1111       __ jcc(Assembler::belowEqual, (*NOLp));
1112       __ cmpptr(to, end_from);
1113       __ jcc(Assembler::aboveEqual, (*NOLp));
1114     }
1115   }
1116 
1117   // Shuffle first three arg regs on Windows into Linux/Solaris locations.
1118   //
1119   // Outputs:
1120   //    rdi - rcx
1121   //    rsi - rdx
1122   //    rdx - r8
1123   //    rcx - r9
1124   //
1125   // Registers r9 and r10 are used to save rdi and rsi on Windows, which latter
1126   // are non-volatile.  r9 and r10 should not be used by the caller.
1127   //
1128   void setup_arg_regs(int nargs = 3) {
1129     const Register saved_rdi = r9;
1130     const Register saved_rsi = r10;
1131     assert(nargs == 3 || nargs == 4, "else fix");
1132 #ifdef _WIN64
1133     assert(c_rarg0 == rcx && c_rarg1 == rdx && c_rarg2 == r8 && c_rarg3 == r9,
1134            "unexpected argument registers");
1135     if (nargs >= 4)
1136       __ mov(rax, r9);  // r9 is also saved_rdi
1137     __ movptr(saved_rdi, rdi);
1138     __ movptr(saved_rsi, rsi);
1139     __ mov(rdi, rcx); // c_rarg0
1140     __ mov(rsi, rdx); // c_rarg1
1141     __ mov(rdx, r8);  // c_rarg2
1142     if (nargs >= 4)
1143       __ mov(rcx, rax); // c_rarg3 (via rax)
1144 #else
1145     assert(c_rarg0 == rdi && c_rarg1 == rsi && c_rarg2 == rdx && c_rarg3 == rcx,
1146            "unexpected argument registers");
1147 #endif
1148   }
1149 
1150   void restore_arg_regs() {
1151     const Register saved_rdi = r9;
1152     const Register saved_rsi = r10;
1153 #ifdef _WIN64
1154     __ movptr(rdi, saved_rdi);
1155     __ movptr(rsi, saved_rsi);
1156 #endif
1157   }
1158 
1159   // Generate code for an array write pre barrier
1160   //
1161   //     addr    -  starting address
1162   //     count   -  element count
1163   //     tmp     - scratch register
1164   //
1165   //     Destroy no registers!
1166   //
1167   void  gen_write_ref_array_pre_barrier(Register addr, Register count, bool dest_uninitialized) {
1168     BarrierSet* bs = Universe::heap()->barrier_set();
1169     switch (bs->kind()) {
1170       case BarrierSet::G1SATBCT:
1171       case BarrierSet::G1SATBCTLogging:
1172         // With G1, don't generate the call if we statically know that the target in uninitialized
1173         if (!dest_uninitialized) {
1174            __ pusha();                      // push registers
1175            if (count == c_rarg0) {
1176              if (addr == c_rarg1) {
1177                // exactly backwards!!
1178                __ xchgptr(c_rarg1, c_rarg0);
1179              } else {
1180                __ movptr(c_rarg1, count);
1181                __ movptr(c_rarg0, addr);
1182              }
1183            } else {
1184              __ movptr(c_rarg0, addr);
1185              __ movptr(c_rarg1, count);
1186            }
1187            __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_pre), 2);
1188            __ popa();
1189         }
1190          break;
1191       case BarrierSet::CardTableModRef:
1192       case BarrierSet::CardTableExtension:
1193       case BarrierSet::ModRef:
1194         break;
1195       default:
1196         ShouldNotReachHere();
1197 
1198     }
1199   }
1200 
1201   //
1202   // Generate code for an array write post barrier
1203   //
1204   //  Input:
1205   //     start    - register containing starting address of destination array
1206   //     end      - register containing ending address of destination array
1207   //     scratch  - scratch register
1208   //
1209   //  The input registers are overwritten.
1210   //  The ending address is inclusive.
1211   void  gen_write_ref_array_post_barrier(Register start, Register end, Register scratch) {
1212     assert_different_registers(start, end, scratch);
1213     BarrierSet* bs = Universe::heap()->barrier_set();
1214     switch (bs->kind()) {
1215       case BarrierSet::G1SATBCT:
1216       case BarrierSet::G1SATBCTLogging:
1217 
1218         {
1219           __ pusha();                      // push registers (overkill)
1220           // must compute element count unless barrier set interface is changed (other platforms supply count)
1221           assert_different_registers(start, end, scratch);
1222           __ lea(scratch, Address(end, BytesPerHeapOop));
1223           __ subptr(scratch, start);               // subtract start to get #bytes
1224           __ shrptr(scratch, LogBytesPerHeapOop);  // convert to element count
1225           __ mov(c_rarg0, start);
1226           __ mov(c_rarg1, scratch);
1227           __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_post), 2);
1228           __ popa();
1229         }
1230         break;
1231       case BarrierSet::CardTableModRef:
1232       case BarrierSet::CardTableExtension:
1233         {
1234           CardTableModRefBS* ct = (CardTableModRefBS*)bs;
1235           assert(sizeof(*ct->byte_map_base) == sizeof(jbyte), "adjust this code");
1236 
1237           Label L_loop;
1238 
1239            __ shrptr(start, CardTableModRefBS::card_shift);
1240            __ addptr(end, BytesPerHeapOop);
1241            __ shrptr(end, CardTableModRefBS::card_shift);
1242            __ subptr(end, start); // number of bytes to copy
1243 
1244           intptr_t disp = (intptr_t) ct->byte_map_base;
1245           if (__ is_simm32(disp)) {
1246             Address cardtable(noreg, noreg, Address::no_scale, disp);
1247             __ lea(scratch, cardtable);
1248           } else {
1249             ExternalAddress cardtable((address)disp);
1250             __ lea(scratch, cardtable);
1251           }
1252 
1253           const Register count = end; // 'end' register contains bytes count now
1254           __ addptr(start, scratch);
1255         __ BIND(L_loop);
1256           __ movb(Address(start, count, Address::times_1), 0);
1257           __ decrement(count);
1258           __ jcc(Assembler::greaterEqual, L_loop);
1259         }
1260         break;
1261       default:
1262         ShouldNotReachHere();
1263 
1264     }
1265   }
1266 
1267 
1268   // Copy big chunks forward
1269   //
1270   // Inputs:
1271   //   end_from     - source arrays end address
1272   //   end_to       - destination array end address
1273   //   qword_count  - 64-bits element count, negative
1274   //   to           - scratch
1275   //   L_copy_32_bytes - entry label
1276   //   L_copy_8_bytes  - exit  label
1277   //
1278   void copy_32_bytes_forward(Register end_from, Register end_to,
1279                              Register qword_count, Register to,
1280                              Label& L_copy_32_bytes, Label& L_copy_8_bytes) {
1281     DEBUG_ONLY(__ stop("enter at entry label, not here"));
1282     Label L_loop;
1283     __ align(OptoLoopAlignment);
1284   __ BIND(L_loop);
1285     if(UseUnalignedLoadStores) {
1286       __ movdqu(xmm0, Address(end_from, qword_count, Address::times_8, -24));
1287       __ movdqu(Address(end_to, qword_count, Address::times_8, -24), xmm0);
1288       __ movdqu(xmm1, Address(end_from, qword_count, Address::times_8, - 8));
1289       __ movdqu(Address(end_to, qword_count, Address::times_8, - 8), xmm1);
1290 
1291     } else {
1292       __ movq(to, Address(end_from, qword_count, Address::times_8, -24));
1293       __ movq(Address(end_to, qword_count, Address::times_8, -24), to);
1294       __ movq(to, Address(end_from, qword_count, Address::times_8, -16));
1295       __ movq(Address(end_to, qword_count, Address::times_8, -16), to);
1296       __ movq(to, Address(end_from, qword_count, Address::times_8, - 8));
1297       __ movq(Address(end_to, qword_count, Address::times_8, - 8), to);
1298       __ movq(to, Address(end_from, qword_count, Address::times_8, - 0));
1299       __ movq(Address(end_to, qword_count, Address::times_8, - 0), to);
1300     }
1301   __ BIND(L_copy_32_bytes);
1302     __ addptr(qword_count, 4);
1303     __ jcc(Assembler::lessEqual, L_loop);
1304     __ subptr(qword_count, 4);
1305     __ jcc(Assembler::less, L_copy_8_bytes); // Copy trailing qwords
1306   }
1307 
1308 
1309   // Copy big chunks backward
1310   //
1311   // Inputs:
1312   //   from         - source arrays address
1313   //   dest         - destination array address
1314   //   qword_count  - 64-bits element count
1315   //   to           - scratch
1316   //   L_copy_32_bytes - entry label
1317   //   L_copy_8_bytes  - exit  label
1318   //
1319   void copy_32_bytes_backward(Register from, Register dest,
1320                               Register qword_count, Register to,
1321                               Label& L_copy_32_bytes, Label& L_copy_8_bytes) {
1322     DEBUG_ONLY(__ stop("enter at entry label, not here"));
1323     Label L_loop;
1324     __ align(OptoLoopAlignment);
1325   __ BIND(L_loop);
1326     if(UseUnalignedLoadStores) {
1327       __ movdqu(xmm0, Address(from, qword_count, Address::times_8, 16));
1328       __ movdqu(Address(dest, qword_count, Address::times_8, 16), xmm0);
1329       __ movdqu(xmm1, Address(from, qword_count, Address::times_8,  0));
1330       __ movdqu(Address(dest, qword_count, Address::times_8,  0), xmm1);
1331 
1332     } else {
1333       __ movq(to, Address(from, qword_count, Address::times_8, 24));
1334       __ movq(Address(dest, qword_count, Address::times_8, 24), to);
1335       __ movq(to, Address(from, qword_count, Address::times_8, 16));
1336       __ movq(Address(dest, qword_count, Address::times_8, 16), to);
1337       __ movq(to, Address(from, qword_count, Address::times_8,  8));
1338       __ movq(Address(dest, qword_count, Address::times_8,  8), to);
1339       __ movq(to, Address(from, qword_count, Address::times_8,  0));
1340       __ movq(Address(dest, qword_count, Address::times_8,  0), to);
1341     }
1342   __ BIND(L_copy_32_bytes);
1343     __ subptr(qword_count, 4);
1344     __ jcc(Assembler::greaterEqual, L_loop);
1345     __ addptr(qword_count, 4);
1346     __ jcc(Assembler::greater, L_copy_8_bytes); // Copy trailing qwords
1347   }
1348 
1349 
1350   // Arguments:
1351   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1352   //             ignored
1353   //   name    - stub name string
1354   //
1355   // Inputs:
1356   //   c_rarg0   - source array address
1357   //   c_rarg1   - destination array address
1358   //   c_rarg2   - element count, treated as ssize_t, can be zero
1359   //
1360   // If 'from' and/or 'to' are aligned on 4-, 2-, or 1-byte boundaries,
1361   // we let the hardware handle it.  The one to eight bytes within words,
1362   // dwords or qwords that span cache line boundaries will still be loaded
1363   // and stored atomically.
1364   //
1365   // Side Effects:
1366   //   disjoint_byte_copy_entry is set to the no-overlap entry point
1367   //   used by generate_conjoint_byte_copy().
1368   //
1369   address generate_disjoint_byte_copy(bool aligned, address* entry, const char *name) {
1370     __ align(CodeEntryAlignment);
1371     StubCodeMark mark(this, "StubRoutines", name);
1372     address start = __ pc();
1373 
1374     Label L_copy_32_bytes, L_copy_8_bytes, L_copy_4_bytes, L_copy_2_bytes;
1375     Label L_copy_byte, L_exit;
1376     const Register from        = rdi;  // source array address
1377     const Register to          = rsi;  // destination array address
1378     const Register count       = rdx;  // elements count
1379     const Register byte_count  = rcx;
1380     const Register qword_count = count;
1381     const Register end_from    = from; // source array end address
1382     const Register end_to      = to;   // destination array end address
1383     // End pointers are inclusive, and if count is not zero they point
1384     // to the last unit copied:  end_to[0] := end_from[0]
1385 
1386     __ enter(); // required for proper stackwalking of RuntimeStub frame
1387     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1388 
1389     if (entry != NULL) {
1390       *entry = __ pc();
1391        // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1392       BLOCK_COMMENT("Entry:");
1393     }
1394 
1395     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1396                       // r9 and r10 may be used to save non-volatile registers
1397 
1398     // 'from', 'to' and 'count' are now valid
1399     __ movptr(byte_count, count);
1400     __ shrptr(count, 3); // count => qword_count
1401 
1402     // Copy from low to high addresses.  Use 'to' as scratch.
1403     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1404     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1405     __ negptr(qword_count); // make the count negative
1406     __ jmp(L_copy_32_bytes);
1407 
1408     // Copy trailing qwords
1409   __ BIND(L_copy_8_bytes);
1410     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
1411     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
1412     __ increment(qword_count);
1413     __ jcc(Assembler::notZero, L_copy_8_bytes);
1414 
1415     // Check for and copy trailing dword
1416   __ BIND(L_copy_4_bytes);
1417     __ testl(byte_count, 4);
1418     __ jccb(Assembler::zero, L_copy_2_bytes);
1419     __ movl(rax, Address(end_from, 8));
1420     __ movl(Address(end_to, 8), rax);
1421 
1422     __ addptr(end_from, 4);
1423     __ addptr(end_to, 4);
1424 
1425     // Check for and copy trailing word
1426   __ BIND(L_copy_2_bytes);
1427     __ testl(byte_count, 2);
1428     __ jccb(Assembler::zero, L_copy_byte);
1429     __ movw(rax, Address(end_from, 8));
1430     __ movw(Address(end_to, 8), rax);
1431 
1432     __ addptr(end_from, 2);
1433     __ addptr(end_to, 2);
1434 
1435     // Check for and copy trailing byte
1436   __ BIND(L_copy_byte);
1437     __ testl(byte_count, 1);
1438     __ jccb(Assembler::zero, L_exit);
1439     __ movb(rax, Address(end_from, 8));
1440     __ movb(Address(end_to, 8), rax);
1441 
1442   __ BIND(L_exit);
1443     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr);
1444     restore_arg_regs();
1445     __ xorptr(rax, rax); // return 0
1446     __ leave(); // required for proper stackwalking of RuntimeStub frame
1447     __ ret(0);
1448 
1449     // Copy in 32-bytes chunks
1450     copy_32_bytes_forward(end_from, end_to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
1451     __ jmp(L_copy_4_bytes);
1452 
1453     return start;
1454   }
1455 
1456   // Arguments:
1457   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1458   //             ignored
1459   //   name    - stub name string
1460   //
1461   // Inputs:
1462   //   c_rarg0   - source array address
1463   //   c_rarg1   - destination array address
1464   //   c_rarg2   - element count, treated as ssize_t, can be zero
1465   //
1466   // If 'from' and/or 'to' are aligned on 4-, 2-, or 1-byte boundaries,
1467   // we let the hardware handle it.  The one to eight bytes within words,
1468   // dwords or qwords that span cache line boundaries will still be loaded
1469   // and stored atomically.
1470   //
1471   address generate_conjoint_byte_copy(bool aligned, address nooverlap_target,
1472                                       address* entry, const char *name) {
1473     __ align(CodeEntryAlignment);
1474     StubCodeMark mark(this, "StubRoutines", name);
1475     address start = __ pc();
1476 
1477     Label L_copy_32_bytes, L_copy_8_bytes, L_copy_4_bytes, L_copy_2_bytes;
1478     const Register from        = rdi;  // source array address
1479     const Register to          = rsi;  // destination array address
1480     const Register count       = rdx;  // elements count
1481     const Register byte_count  = rcx;
1482     const Register qword_count = count;
1483 
1484     __ enter(); // required for proper stackwalking of RuntimeStub frame
1485     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1486 
1487     if (entry != NULL) {
1488       *entry = __ pc();
1489       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1490       BLOCK_COMMENT("Entry:");
1491     }
1492 
1493     array_overlap_test(nooverlap_target, Address::times_1);
1494     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1495                       // r9 and r10 may be used to save non-volatile registers
1496 
1497     // 'from', 'to' and 'count' are now valid
1498     __ movptr(byte_count, count);
1499     __ shrptr(count, 3);   // count => qword_count
1500 
1501     // Copy from high to low addresses.
1502 
1503     // Check for and copy trailing byte
1504     __ testl(byte_count, 1);
1505     __ jcc(Assembler::zero, L_copy_2_bytes);
1506     __ movb(rax, Address(from, byte_count, Address::times_1, -1));
1507     __ movb(Address(to, byte_count, Address::times_1, -1), rax);
1508     __ decrement(byte_count); // Adjust for possible trailing word
1509 
1510     // Check for and copy trailing word
1511   __ BIND(L_copy_2_bytes);
1512     __ testl(byte_count, 2);
1513     __ jcc(Assembler::zero, L_copy_4_bytes);
1514     __ movw(rax, Address(from, byte_count, Address::times_1, -2));
1515     __ movw(Address(to, byte_count, Address::times_1, -2), rax);
1516 
1517     // Check for and copy trailing dword
1518   __ BIND(L_copy_4_bytes);
1519     __ testl(byte_count, 4);
1520     __ jcc(Assembler::zero, L_copy_32_bytes);
1521     __ movl(rax, Address(from, qword_count, Address::times_8));
1522     __ movl(Address(to, qword_count, Address::times_8), rax);
1523     __ jmp(L_copy_32_bytes);
1524 
1525     // Copy trailing qwords
1526   __ BIND(L_copy_8_bytes);
1527     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
1528     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
1529     __ decrement(qword_count);
1530     __ jcc(Assembler::notZero, L_copy_8_bytes);
1531 
1532     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr);
1533     restore_arg_regs();
1534     __ xorptr(rax, rax); // return 0
1535     __ leave(); // required for proper stackwalking of RuntimeStub frame
1536     __ ret(0);
1537 
1538     // Copy in 32-bytes chunks
1539     copy_32_bytes_backward(from, to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
1540 
1541     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr);
1542     restore_arg_regs();
1543     __ xorptr(rax, rax); // return 0
1544     __ leave(); // required for proper stackwalking of RuntimeStub frame
1545     __ ret(0);
1546 
1547     return start;
1548   }
1549 
1550   // Arguments:
1551   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1552   //             ignored
1553   //   name    - stub name string
1554   //
1555   // Inputs:
1556   //   c_rarg0   - source array address
1557   //   c_rarg1   - destination array address
1558   //   c_rarg2   - element count, treated as ssize_t, can be zero
1559   //
1560   // If 'from' and/or 'to' are aligned on 4- or 2-byte boundaries, we
1561   // let the hardware handle it.  The two or four words within dwords
1562   // or qwords that span cache line boundaries will still be loaded
1563   // and stored atomically.
1564   //
1565   // Side Effects:
1566   //   disjoint_short_copy_entry is set to the no-overlap entry point
1567   //   used by generate_conjoint_short_copy().
1568   //
1569   address generate_disjoint_short_copy(bool aligned, address *entry, const char *name) {
1570     __ align(CodeEntryAlignment);
1571     StubCodeMark mark(this, "StubRoutines", name);
1572     address start = __ pc();
1573 
1574     Label L_copy_32_bytes, L_copy_8_bytes, L_copy_4_bytes,L_copy_2_bytes,L_exit;
1575     const Register from        = rdi;  // source array address
1576     const Register to          = rsi;  // destination array address
1577     const Register count       = rdx;  // elements count
1578     const Register word_count  = rcx;
1579     const Register qword_count = count;
1580     const Register end_from    = from; // source array end address
1581     const Register end_to      = to;   // destination array end address
1582     // End pointers are inclusive, and if count is not zero they point
1583     // to the last unit copied:  end_to[0] := end_from[0]
1584 
1585     __ enter(); // required for proper stackwalking of RuntimeStub frame
1586     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1587 
1588     if (entry != NULL) {
1589       *entry = __ pc();
1590       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1591       BLOCK_COMMENT("Entry:");
1592     }
1593 
1594     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1595                       // r9 and r10 may be used to save non-volatile registers
1596 
1597     // 'from', 'to' and 'count' are now valid
1598     __ movptr(word_count, count);
1599     __ shrptr(count, 2); // count => qword_count
1600 
1601     // Copy from low to high addresses.  Use 'to' as scratch.
1602     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1603     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1604     __ negptr(qword_count);
1605     __ jmp(L_copy_32_bytes);
1606 
1607     // Copy trailing qwords
1608   __ BIND(L_copy_8_bytes);
1609     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
1610     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
1611     __ increment(qword_count);
1612     __ jcc(Assembler::notZero, L_copy_8_bytes);
1613 
1614     // Original 'dest' is trashed, so we can't use it as a
1615     // base register for a possible trailing word copy
1616 
1617     // Check for and copy trailing dword
1618   __ BIND(L_copy_4_bytes);
1619     __ testl(word_count, 2);
1620     __ jccb(Assembler::zero, L_copy_2_bytes);
1621     __ movl(rax, Address(end_from, 8));
1622     __ movl(Address(end_to, 8), rax);
1623 
1624     __ addptr(end_from, 4);
1625     __ addptr(end_to, 4);
1626 
1627     // Check for and copy trailing word
1628   __ BIND(L_copy_2_bytes);
1629     __ testl(word_count, 1);
1630     __ jccb(Assembler::zero, L_exit);
1631     __ movw(rax, Address(end_from, 8));
1632     __ movw(Address(end_to, 8), rax);
1633 
1634   __ BIND(L_exit);
1635     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr);
1636     restore_arg_regs();
1637     __ xorptr(rax, rax); // return 0
1638     __ leave(); // required for proper stackwalking of RuntimeStub frame
1639     __ ret(0);
1640 
1641     // Copy in 32-bytes chunks
1642     copy_32_bytes_forward(end_from, end_to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
1643     __ jmp(L_copy_4_bytes);
1644 
1645     return start;
1646   }
1647 
1648   address generate_fill(BasicType t, bool aligned, const char *name) {
1649     __ align(CodeEntryAlignment);
1650     StubCodeMark mark(this, "StubRoutines", name);
1651     address start = __ pc();
1652 
1653     BLOCK_COMMENT("Entry:");
1654 
1655     const Register to       = c_rarg0;  // source array address
1656     const Register value    = c_rarg1;  // value
1657     const Register count    = c_rarg2;  // elements count
1658 
1659     __ enter(); // required for proper stackwalking of RuntimeStub frame
1660 
1661     __ generate_fill(t, aligned, to, value, count, rax, xmm0);
1662 
1663     __ leave(); // required for proper stackwalking of RuntimeStub frame
1664     __ ret(0);
1665     return start;
1666   }
1667 
1668   // Arguments:
1669   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1670   //             ignored
1671   //   name    - stub name string
1672   //
1673   // Inputs:
1674   //   c_rarg0   - source array address
1675   //   c_rarg1   - destination array address
1676   //   c_rarg2   - element count, treated as ssize_t, can be zero
1677   //
1678   // If 'from' and/or 'to' are aligned on 4- or 2-byte boundaries, we
1679   // let the hardware handle it.  The two or four words within dwords
1680   // or qwords that span cache line boundaries will still be loaded
1681   // and stored atomically.
1682   //
1683   address generate_conjoint_short_copy(bool aligned, address nooverlap_target,
1684                                        address *entry, const char *name) {
1685     __ align(CodeEntryAlignment);
1686     StubCodeMark mark(this, "StubRoutines", name);
1687     address start = __ pc();
1688 
1689     Label L_copy_32_bytes, L_copy_8_bytes, L_copy_4_bytes;
1690     const Register from        = rdi;  // source array address
1691     const Register to          = rsi;  // destination array address
1692     const Register count       = rdx;  // elements count
1693     const Register word_count  = rcx;
1694     const Register qword_count = count;
1695 
1696     __ enter(); // required for proper stackwalking of RuntimeStub frame
1697     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1698 
1699     if (entry != NULL) {
1700       *entry = __ pc();
1701       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1702       BLOCK_COMMENT("Entry:");
1703     }
1704 
1705     array_overlap_test(nooverlap_target, Address::times_2);
1706     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1707                       // r9 and r10 may be used to save non-volatile registers
1708 
1709     // 'from', 'to' and 'count' are now valid
1710     __ movptr(word_count, count);
1711     __ shrptr(count, 2); // count => qword_count
1712 
1713     // Copy from high to low addresses.  Use 'to' as scratch.
1714 
1715     // Check for and copy trailing word
1716     __ testl(word_count, 1);
1717     __ jccb(Assembler::zero, L_copy_4_bytes);
1718     __ movw(rax, Address(from, word_count, Address::times_2, -2));
1719     __ movw(Address(to, word_count, Address::times_2, -2), rax);
1720 
1721     // Check for and copy trailing dword
1722   __ BIND(L_copy_4_bytes);
1723     __ testl(word_count, 2);
1724     __ jcc(Assembler::zero, L_copy_32_bytes);
1725     __ movl(rax, Address(from, qword_count, Address::times_8));
1726     __ movl(Address(to, qword_count, Address::times_8), rax);
1727     __ jmp(L_copy_32_bytes);
1728 
1729     // Copy trailing qwords
1730   __ BIND(L_copy_8_bytes);
1731     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
1732     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
1733     __ decrement(qword_count);
1734     __ jcc(Assembler::notZero, L_copy_8_bytes);
1735 
1736     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr);
1737     restore_arg_regs();
1738     __ xorptr(rax, rax); // return 0
1739     __ leave(); // required for proper stackwalking of RuntimeStub frame
1740     __ ret(0);
1741 
1742     // Copy in 32-bytes chunks
1743     copy_32_bytes_backward(from, to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
1744 
1745     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr);
1746     restore_arg_regs();
1747     __ xorptr(rax, rax); // return 0
1748     __ leave(); // required for proper stackwalking of RuntimeStub frame
1749     __ ret(0);
1750 
1751     return start;
1752   }
1753 
1754   // Arguments:
1755   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1756   //             ignored
1757   //   is_oop  - true => oop array, so generate store check code
1758   //   name    - stub name string
1759   //
1760   // Inputs:
1761   //   c_rarg0   - source array address
1762   //   c_rarg1   - destination array address
1763   //   c_rarg2   - element count, treated as ssize_t, can be zero
1764   //
1765   // If 'from' and/or 'to' are aligned on 4-byte boundaries, we let
1766   // the hardware handle it.  The two dwords within qwords that span
1767   // cache line boundaries will still be loaded and stored atomicly.
1768   //
1769   // Side Effects:
1770   //   disjoint_int_copy_entry is set to the no-overlap entry point
1771   //   used by generate_conjoint_int_oop_copy().
1772   //
1773   address generate_disjoint_int_oop_copy(bool aligned, bool is_oop, address* entry,
1774                                          const char *name, bool dest_uninitialized = false) {
1775     __ align(CodeEntryAlignment);
1776     StubCodeMark mark(this, "StubRoutines", name);
1777     address start = __ pc();
1778 
1779     Label L_copy_32_bytes, L_copy_8_bytes, L_copy_4_bytes, L_exit;
1780     const Register from        = rdi;  // source array address
1781     const Register to          = rsi;  // destination array address
1782     const Register count       = rdx;  // elements count
1783     const Register dword_count = rcx;
1784     const Register qword_count = count;
1785     const Register end_from    = from; // source array end address
1786     const Register end_to      = to;   // destination array end address
1787     const Register saved_to    = r11;  // saved destination array address
1788     // End pointers are inclusive, and if count is not zero they point
1789     // to the last unit copied:  end_to[0] := end_from[0]
1790 
1791     __ enter(); // required for proper stackwalking of RuntimeStub frame
1792     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1793 
1794     if (entry != NULL) {
1795       *entry = __ pc();
1796       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1797       BLOCK_COMMENT("Entry:");
1798     }
1799 
1800     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1801                       // r9 and r10 may be used to save non-volatile registers
1802     if (is_oop) {
1803       __ movq(saved_to, to);
1804       gen_write_ref_array_pre_barrier(to, count, dest_uninitialized);
1805     }
1806 
1807     // 'from', 'to' and 'count' are now valid
1808     __ movptr(dword_count, count);
1809     __ shrptr(count, 1); // count => qword_count
1810 
1811     // Copy from low to high addresses.  Use 'to' as scratch.
1812     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1813     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1814     __ negptr(qword_count);
1815     __ jmp(L_copy_32_bytes);
1816 
1817     // Copy trailing qwords
1818   __ BIND(L_copy_8_bytes);
1819     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
1820     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
1821     __ increment(qword_count);
1822     __ jcc(Assembler::notZero, L_copy_8_bytes);
1823 
1824     // Check for and copy trailing dword
1825   __ BIND(L_copy_4_bytes);
1826     __ testl(dword_count, 1); // Only byte test since the value is 0 or 1
1827     __ jccb(Assembler::zero, L_exit);
1828     __ movl(rax, Address(end_from, 8));
1829     __ movl(Address(end_to, 8), rax);
1830 
1831   __ BIND(L_exit);
1832     if (is_oop) {
1833       __ leaq(end_to, Address(saved_to, dword_count, Address::times_4, -4));
1834       gen_write_ref_array_post_barrier(saved_to, end_to, rax);
1835     }
1836     inc_counter_np(SharedRuntime::_jint_array_copy_ctr);
1837     restore_arg_regs();
1838     __ xorptr(rax, rax); // return 0
1839     __ leave(); // required for proper stackwalking of RuntimeStub frame
1840     __ ret(0);
1841 
1842     // Copy 32-bytes chunks
1843     copy_32_bytes_forward(end_from, end_to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
1844     __ jmp(L_copy_4_bytes);
1845 
1846     return start;
1847   }
1848 
1849   // Arguments:
1850   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1851   //             ignored
1852   //   is_oop  - true => oop array, so generate store check code
1853   //   name    - stub name string
1854   //
1855   // Inputs:
1856   //   c_rarg0   - source array address
1857   //   c_rarg1   - destination array address
1858   //   c_rarg2   - element count, treated as ssize_t, can be zero
1859   //
1860   // If 'from' and/or 'to' are aligned on 4-byte boundaries, we let
1861   // the hardware handle it.  The two dwords within qwords that span
1862   // cache line boundaries will still be loaded and stored atomicly.
1863   //
1864   address generate_conjoint_int_oop_copy(bool aligned, bool is_oop, address nooverlap_target,
1865                                          address *entry, const char *name,
1866                                          bool dest_uninitialized = false) {
1867     __ align(CodeEntryAlignment);
1868     StubCodeMark mark(this, "StubRoutines", name);
1869     address start = __ pc();
1870 
1871     Label L_copy_32_bytes, L_copy_8_bytes, L_copy_2_bytes, L_exit;
1872     const Register from        = rdi;  // source array address
1873     const Register to          = rsi;  // destination array address
1874     const Register count       = rdx;  // elements count
1875     const Register dword_count = rcx;
1876     const Register qword_count = count;
1877 
1878     __ enter(); // required for proper stackwalking of RuntimeStub frame
1879     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1880 
1881     if (entry != NULL) {
1882       *entry = __ pc();
1883        // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1884       BLOCK_COMMENT("Entry:");
1885     }
1886 
1887     array_overlap_test(nooverlap_target, Address::times_4);
1888     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1889                       // r9 and r10 may be used to save non-volatile registers
1890 
1891     if (is_oop) {
1892       // no registers are destroyed by this call
1893       gen_write_ref_array_pre_barrier(to, count, dest_uninitialized);
1894     }
1895 
1896     assert_clean_int(count, rax); // Make sure 'count' is clean int.
1897     // 'from', 'to' and 'count' are now valid
1898     __ movptr(dword_count, count);
1899     __ shrptr(count, 1); // count => qword_count
1900 
1901     // Copy from high to low addresses.  Use 'to' as scratch.
1902 
1903     // Check for and copy trailing dword
1904     __ testl(dword_count, 1);
1905     __ jcc(Assembler::zero, L_copy_32_bytes);
1906     __ movl(rax, Address(from, dword_count, Address::times_4, -4));
1907     __ movl(Address(to, dword_count, Address::times_4, -4), rax);
1908     __ jmp(L_copy_32_bytes);
1909 
1910     // Copy trailing qwords
1911   __ BIND(L_copy_8_bytes);
1912     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
1913     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
1914     __ decrement(qword_count);
1915     __ jcc(Assembler::notZero, L_copy_8_bytes);
1916 
1917     inc_counter_np(SharedRuntime::_jint_array_copy_ctr);
1918     if (is_oop) {
1919       __ jmp(L_exit);
1920     }
1921     restore_arg_regs();
1922     __ xorptr(rax, rax); // return 0
1923     __ leave(); // required for proper stackwalking of RuntimeStub frame
1924     __ ret(0);
1925 
1926     // Copy in 32-bytes chunks
1927     copy_32_bytes_backward(from, to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
1928 
1929    inc_counter_np(SharedRuntime::_jint_array_copy_ctr);
1930    __ bind(L_exit);
1931      if (is_oop) {
1932        Register end_to = rdx;
1933        __ leaq(end_to, Address(to, dword_count, Address::times_4, -4));
1934        gen_write_ref_array_post_barrier(to, end_to, rax);
1935      }
1936     restore_arg_regs();
1937     __ xorptr(rax, rax); // return 0
1938     __ leave(); // required for proper stackwalking of RuntimeStub frame
1939     __ ret(0);
1940 
1941     return start;
1942   }
1943 
1944   // Arguments:
1945   //   aligned - true => Input and output aligned on a HeapWord boundary == 8 bytes
1946   //             ignored
1947   //   is_oop  - true => oop array, so generate store check code
1948   //   name    - stub name string
1949   //
1950   // Inputs:
1951   //   c_rarg0   - source array address
1952   //   c_rarg1   - destination array address
1953   //   c_rarg2   - element count, treated as ssize_t, can be zero
1954   //
1955  // Side Effects:
1956   //   disjoint_oop_copy_entry or disjoint_long_copy_entry is set to the
1957   //   no-overlap entry point used by generate_conjoint_long_oop_copy().
1958   //
1959   address generate_disjoint_long_oop_copy(bool aligned, bool is_oop, address *entry,
1960                                           const char *name, bool dest_uninitialized = false) {
1961     __ align(CodeEntryAlignment);
1962     StubCodeMark mark(this, "StubRoutines", name);
1963     address start = __ pc();
1964 
1965     Label L_copy_32_bytes, L_copy_8_bytes, L_exit;
1966     const Register from        = rdi;  // source array address
1967     const Register to          = rsi;  // destination array address
1968     const Register qword_count = rdx;  // elements count
1969     const Register end_from    = from; // source array end address
1970     const Register end_to      = rcx;  // destination array end address
1971     const Register saved_to    = to;
1972     // End pointers are inclusive, and if count is not zero they point
1973     // to the last unit copied:  end_to[0] := end_from[0]
1974 
1975     __ enter(); // required for proper stackwalking of RuntimeStub frame
1976     // Save no-overlap entry point for generate_conjoint_long_oop_copy()
1977     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1978 
1979     if (entry != NULL) {
1980       *entry = __ pc();
1981       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1982       BLOCK_COMMENT("Entry:");
1983     }
1984 
1985     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1986                       // r9 and r10 may be used to save non-volatile registers
1987     // 'from', 'to' and 'qword_count' are now valid
1988     if (is_oop) {
1989       // no registers are destroyed by this call
1990       gen_write_ref_array_pre_barrier(to, qword_count, dest_uninitialized);
1991     }
1992 
1993     // Copy from low to high addresses.  Use 'to' as scratch.
1994     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1995     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1996     __ negptr(qword_count);
1997     __ jmp(L_copy_32_bytes);
1998 
1999     // Copy trailing qwords
2000   __ BIND(L_copy_8_bytes);
2001     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
2002     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
2003     __ increment(qword_count);
2004     __ jcc(Assembler::notZero, L_copy_8_bytes);
2005 
2006     if (is_oop) {
2007       __ jmp(L_exit);
2008     } else {
2009       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr);
2010       restore_arg_regs();
2011       __ xorptr(rax, rax); // return 0
2012       __ leave(); // required for proper stackwalking of RuntimeStub frame
2013       __ ret(0);
2014     }
2015 
2016     // Copy 64-byte chunks
2017     copy_32_bytes_forward(end_from, end_to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
2018 
2019     if (is_oop) {
2020     __ BIND(L_exit);
2021       gen_write_ref_array_post_barrier(saved_to, end_to, rax);
2022       inc_counter_np(SharedRuntime::_oop_array_copy_ctr);
2023     } else {
2024       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr);
2025     }
2026     restore_arg_regs();
2027     __ xorptr(rax, rax); // return 0
2028     __ leave(); // required for proper stackwalking of RuntimeStub frame
2029     __ ret(0);
2030 
2031     return start;
2032   }
2033 
2034   // Arguments:
2035   //   aligned - true => Input and output aligned on a HeapWord boundary == 8 bytes
2036   //             ignored
2037   //   is_oop  - true => oop array, so generate store check code
2038   //   name    - stub name string
2039   //
2040   // Inputs:
2041   //   c_rarg0   - source array address
2042   //   c_rarg1   - destination array address
2043   //   c_rarg2   - element count, treated as ssize_t, can be zero
2044   //
2045   address generate_conjoint_long_oop_copy(bool aligned, bool is_oop,
2046                                           address nooverlap_target, address *entry,
2047                                           const char *name, bool dest_uninitialized = false) {
2048     __ align(CodeEntryAlignment);
2049     StubCodeMark mark(this, "StubRoutines", name);
2050     address start = __ pc();
2051 
2052     Label L_copy_32_bytes, L_copy_8_bytes, L_exit;
2053     const Register from        = rdi;  // source array address
2054     const Register to          = rsi;  // destination array address
2055     const Register qword_count = rdx;  // elements count
2056     const Register saved_count = rcx;
2057 
2058     __ enter(); // required for proper stackwalking of RuntimeStub frame
2059     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
2060 
2061     if (entry != NULL) {
2062       *entry = __ pc();
2063       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
2064       BLOCK_COMMENT("Entry:");
2065     }
2066 
2067     array_overlap_test(nooverlap_target, Address::times_8);
2068     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
2069                       // r9 and r10 may be used to save non-volatile registers
2070     // 'from', 'to' and 'qword_count' are now valid
2071     if (is_oop) {
2072       // Save to and count for store barrier
2073       __ movptr(saved_count, qword_count);
2074       // No registers are destroyed by this call
2075       gen_write_ref_array_pre_barrier(to, saved_count, dest_uninitialized);
2076     }
2077 
2078     __ jmp(L_copy_32_bytes);
2079 
2080     // Copy trailing qwords
2081   __ BIND(L_copy_8_bytes);
2082     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
2083     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
2084     __ decrement(qword_count);
2085     __ jcc(Assembler::notZero, L_copy_8_bytes);
2086 
2087     if (is_oop) {
2088       __ jmp(L_exit);
2089     } else {
2090       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr);
2091       restore_arg_regs();
2092       __ xorptr(rax, rax); // return 0
2093       __ leave(); // required for proper stackwalking of RuntimeStub frame
2094       __ ret(0);
2095     }
2096 
2097     // Copy in 32-bytes chunks
2098     copy_32_bytes_backward(from, to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
2099 
2100     if (is_oop) {
2101     __ BIND(L_exit);
2102       __ lea(rcx, Address(to, saved_count, Address::times_8, -8));
2103       gen_write_ref_array_post_barrier(to, rcx, rax);
2104       inc_counter_np(SharedRuntime::_oop_array_copy_ctr);
2105     } else {
2106       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr);
2107     }
2108     restore_arg_regs();
2109     __ xorptr(rax, rax); // return 0
2110     __ leave(); // required for proper stackwalking of RuntimeStub frame
2111     __ ret(0);
2112 
2113     return start;
2114   }
2115 
2116 
2117   // Helper for generating a dynamic type check.
2118   // Smashes no registers.
2119   void generate_type_check(Register sub_klass,
2120                            Register super_check_offset,
2121                            Register super_klass,
2122                            Label& L_success) {
2123     assert_different_registers(sub_klass, super_check_offset, super_klass);
2124 
2125     BLOCK_COMMENT("type_check:");
2126 
2127     Label L_miss;
2128 
2129     __ check_klass_subtype_fast_path(sub_klass, super_klass, noreg,        &L_success, &L_miss, NULL,
2130                                      super_check_offset);
2131     __ check_klass_subtype_slow_path(sub_klass, super_klass, noreg, noreg, &L_success, NULL);
2132 
2133     // Fall through on failure!
2134     __ BIND(L_miss);
2135   }
2136 
2137   //
2138   //  Generate checkcasting array copy stub
2139   //
2140   //  Input:
2141   //    c_rarg0   - source array address
2142   //    c_rarg1   - destination array address
2143   //    c_rarg2   - element count, treated as ssize_t, can be zero
2144   //    c_rarg3   - size_t ckoff (super_check_offset)
2145   // not Win64
2146   //    c_rarg4   - oop ckval (super_klass)
2147   // Win64
2148   //    rsp+40    - oop ckval (super_klass)
2149   //
2150   //  Output:
2151   //    rax ==  0  -  success
2152   //    rax == -1^K - failure, where K is partial transfer count
2153   //
2154   address generate_checkcast_copy(const char *name, address *entry,
2155                                   bool dest_uninitialized = false) {
2156 
2157     Label L_load_element, L_store_element, L_do_card_marks, L_done;
2158 
2159     // Input registers (after setup_arg_regs)
2160     const Register from        = rdi;   // source array address
2161     const Register to          = rsi;   // destination array address
2162     const Register length      = rdx;   // elements count
2163     const Register ckoff       = rcx;   // super_check_offset
2164     const Register ckval       = r8;    // super_klass
2165 
2166     // Registers used as temps (r13, r14 are save-on-entry)
2167     const Register end_from    = from;  // source array end address
2168     const Register end_to      = r13;   // destination array end address
2169     const Register count       = rdx;   // -(count_remaining)
2170     const Register r14_length  = r14;   // saved copy of length
2171     // End pointers are inclusive, and if length is not zero they point
2172     // to the last unit copied:  end_to[0] := end_from[0]
2173 
2174     const Register rax_oop    = rax;    // actual oop copied
2175     const Register r11_klass  = r11;    // oop._klass
2176 
2177     //---------------------------------------------------------------
2178     // Assembler stub will be used for this call to arraycopy
2179     // if the two arrays are subtypes of Object[] but the
2180     // destination array type is not equal to or a supertype
2181     // of the source type.  Each element must be separately
2182     // checked.
2183 
2184     __ align(CodeEntryAlignment);
2185     StubCodeMark mark(this, "StubRoutines", name);
2186     address start = __ pc();
2187 
2188     __ enter(); // required for proper stackwalking of RuntimeStub frame
2189 
2190 #ifdef ASSERT
2191     // caller guarantees that the arrays really are different
2192     // otherwise, we would have to make conjoint checks
2193     { Label L;
2194       array_overlap_test(L, TIMES_OOP);
2195       __ stop("checkcast_copy within a single array");
2196       __ bind(L);
2197     }
2198 #endif //ASSERT
2199 
2200     setup_arg_regs(4); // from => rdi, to => rsi, length => rdx
2201                        // ckoff => rcx, ckval => r8
2202                        // r9 and r10 may be used to save non-volatile registers
2203 #ifdef _WIN64
2204     // last argument (#4) is on stack on Win64
2205     __ movptr(ckval, Address(rsp, 6 * wordSize));
2206 #endif
2207 
2208     // Caller of this entry point must set up the argument registers.
2209     if (entry != NULL) {
2210       *entry = __ pc();
2211       BLOCK_COMMENT("Entry:");
2212     }
2213 
2214     // allocate spill slots for r13, r14
2215     enum {
2216       saved_r13_offset,
2217       saved_r14_offset,
2218       saved_rbp_offset
2219     };
2220     __ subptr(rsp, saved_rbp_offset * wordSize);
2221     __ movptr(Address(rsp, saved_r13_offset * wordSize), r13);
2222     __ movptr(Address(rsp, saved_r14_offset * wordSize), r14);
2223 
2224     // check that int operands are properly extended to size_t
2225     assert_clean_int(length, rax);
2226     assert_clean_int(ckoff, rax);
2227 
2228 #ifdef ASSERT
2229     BLOCK_COMMENT("assert consistent ckoff/ckval");
2230     // The ckoff and ckval must be mutually consistent,
2231     // even though caller generates both.
2232     { Label L;
2233       int sco_offset = (klassOopDesc::header_size() * HeapWordSize +
2234                         Klass::super_check_offset_offset_in_bytes());
2235       __ cmpl(ckoff, Address(ckval, sco_offset));
2236       __ jcc(Assembler::equal, L);
2237       __ stop("super_check_offset inconsistent");
2238       __ bind(L);
2239     }
2240 #endif //ASSERT
2241 
2242     // Loop-invariant addresses.  They are exclusive end pointers.
2243     Address end_from_addr(from, length, TIMES_OOP, 0);
2244     Address   end_to_addr(to,   length, TIMES_OOP, 0);
2245     // Loop-variant addresses.  They assume post-incremented count < 0.
2246     Address from_element_addr(end_from, count, TIMES_OOP, 0);
2247     Address   to_element_addr(end_to,   count, TIMES_OOP, 0);
2248 
2249     gen_write_ref_array_pre_barrier(to, count, dest_uninitialized);
2250 
2251     // Copy from low to high addresses, indexed from the end of each array.
2252     __ lea(end_from, end_from_addr);
2253     __ lea(end_to,   end_to_addr);
2254     __ movptr(r14_length, length);        // save a copy of the length
2255     assert(length == count, "");          // else fix next line:
2256     __ negptr(count);                     // negate and test the length
2257     __ jcc(Assembler::notZero, L_load_element);
2258 
2259     // Empty array:  Nothing to do.
2260     __ xorptr(rax, rax);                  // return 0 on (trivial) success
2261     __ jmp(L_done);
2262 
2263     // ======== begin loop ========
2264     // (Loop is rotated; its entry is L_load_element.)
2265     // Loop control:
2266     //   for (count = -count; count != 0; count++)
2267     // Base pointers src, dst are biased by 8*(count-1),to last element.
2268     __ align(OptoLoopAlignment);
2269 
2270     __ BIND(L_store_element);
2271     __ store_heap_oop(to_element_addr, rax_oop);  // store the oop
2272     __ increment(count);               // increment the count toward zero
2273     __ jcc(Assembler::zero, L_do_card_marks);
2274 
2275     // ======== loop entry is here ========
2276     __ BIND(L_load_element);
2277     __ load_heap_oop(rax_oop, from_element_addr); // load the oop
2278     __ testptr(rax_oop, rax_oop);
2279     __ jcc(Assembler::zero, L_store_element);
2280 
2281     __ load_klass(r11_klass, rax_oop);// query the object klass
2282     generate_type_check(r11_klass, ckoff, ckval, L_store_element);
2283     // ======== end loop ========
2284 
2285     // It was a real error; we must depend on the caller to finish the job.
2286     // Register rdx = -1 * number of *remaining* oops, r14 = *total* oops.
2287     // Emit GC store barriers for the oops we have copied (r14 + rdx),
2288     // and report their number to the caller.
2289     assert_different_registers(rax, r14_length, count, to, end_to, rcx);
2290     __ lea(end_to, to_element_addr);
2291     __ addptr(end_to, -heapOopSize);      // make an inclusive end pointer
2292     gen_write_ref_array_post_barrier(to, end_to, rscratch1);
2293     __ movptr(rax, r14_length);           // original oops
2294     __ addptr(rax, count);                // K = (original - remaining) oops
2295     __ notptr(rax);                       // report (-1^K) to caller
2296     __ jmp(L_done);
2297 
2298     // Come here on success only.
2299     __ BIND(L_do_card_marks);
2300     __ addptr(end_to, -heapOopSize);         // make an inclusive end pointer
2301     gen_write_ref_array_post_barrier(to, end_to, rscratch1);
2302     __ xorptr(rax, rax);                  // return 0 on success
2303 
2304     // Common exit point (success or failure).
2305     __ BIND(L_done);
2306     __ movptr(r13, Address(rsp, saved_r13_offset * wordSize));
2307     __ movptr(r14, Address(rsp, saved_r14_offset * wordSize));
2308     inc_counter_np(SharedRuntime::_checkcast_array_copy_ctr);
2309     restore_arg_regs();
2310     __ leave(); // required for proper stackwalking of RuntimeStub frame
2311     __ ret(0);
2312 
2313     return start;
2314   }
2315 
2316   //
2317   //  Generate 'unsafe' array copy stub
2318   //  Though just as safe as the other stubs, it takes an unscaled
2319   //  size_t argument instead of an element count.
2320   //
2321   //  Input:
2322   //    c_rarg0   - source array address
2323   //    c_rarg1   - destination array address
2324   //    c_rarg2   - byte count, treated as ssize_t, can be zero
2325   //
2326   // Examines the alignment of the operands and dispatches
2327   // to a long, int, short, or byte copy loop.
2328   //
2329   address generate_unsafe_copy(const char *name,
2330                                address byte_copy_entry, address short_copy_entry,
2331                                address int_copy_entry, address long_copy_entry) {
2332 
2333     Label L_long_aligned, L_int_aligned, L_short_aligned;
2334 
2335     // Input registers (before setup_arg_regs)
2336     const Register from        = c_rarg0;  // source array address
2337     const Register to          = c_rarg1;  // destination array address
2338     const Register size        = c_rarg2;  // byte count (size_t)
2339 
2340     // Register used as a temp
2341     const Register bits        = rax;      // test copy of low bits
2342 
2343     __ align(CodeEntryAlignment);
2344     StubCodeMark mark(this, "StubRoutines", name);
2345     address start = __ pc();
2346 
2347     __ enter(); // required for proper stackwalking of RuntimeStub frame
2348 
2349     // bump this on entry, not on exit:
2350     inc_counter_np(SharedRuntime::_unsafe_array_copy_ctr);
2351 
2352     __ mov(bits, from);
2353     __ orptr(bits, to);
2354     __ orptr(bits, size);
2355 
2356     __ testb(bits, BytesPerLong-1);
2357     __ jccb(Assembler::zero, L_long_aligned);
2358 
2359     __ testb(bits, BytesPerInt-1);
2360     __ jccb(Assembler::zero, L_int_aligned);
2361 
2362     __ testb(bits, BytesPerShort-1);
2363     __ jump_cc(Assembler::notZero, RuntimeAddress(byte_copy_entry));
2364 
2365     __ BIND(L_short_aligned);
2366     __ shrptr(size, LogBytesPerShort); // size => short_count
2367     __ jump(RuntimeAddress(short_copy_entry));
2368 
2369     __ BIND(L_int_aligned);
2370     __ shrptr(size, LogBytesPerInt); // size => int_count
2371     __ jump(RuntimeAddress(int_copy_entry));
2372 
2373     __ BIND(L_long_aligned);
2374     __ shrptr(size, LogBytesPerLong); // size => qword_count
2375     __ jump(RuntimeAddress(long_copy_entry));
2376 
2377     return start;
2378   }
2379 
2380   // Perform range checks on the proposed arraycopy.
2381   // Kills temp, but nothing else.
2382   // Also, clean the sign bits of src_pos and dst_pos.
2383   void arraycopy_range_checks(Register src,     // source array oop (c_rarg0)
2384                               Register src_pos, // source position (c_rarg1)
2385                               Register dst,     // destination array oo (c_rarg2)
2386                               Register dst_pos, // destination position (c_rarg3)
2387                               Register length,
2388                               Register temp,
2389                               Label& L_failed) {
2390     BLOCK_COMMENT("arraycopy_range_checks:");
2391 
2392     //  if (src_pos + length > arrayOop(src)->length())  FAIL;
2393     __ movl(temp, length);
2394     __ addl(temp, src_pos);             // src_pos + length
2395     __ cmpl(temp, Address(src, arrayOopDesc::length_offset_in_bytes()));
2396     __ jcc(Assembler::above, L_failed);
2397 
2398     //  if (dst_pos + length > arrayOop(dst)->length())  FAIL;
2399     __ movl(temp, length);
2400     __ addl(temp, dst_pos);             // dst_pos + length
2401     __ cmpl(temp, Address(dst, arrayOopDesc::length_offset_in_bytes()));
2402     __ jcc(Assembler::above, L_failed);
2403 
2404     // Have to clean up high 32-bits of 'src_pos' and 'dst_pos'.
2405     // Move with sign extension can be used since they are positive.
2406     __ movslq(src_pos, src_pos);
2407     __ movslq(dst_pos, dst_pos);
2408 
2409     BLOCK_COMMENT("arraycopy_range_checks done");
2410   }
2411 
2412   //
2413   //  Generate generic array copy stubs
2414   //
2415   //  Input:
2416   //    c_rarg0    -  src oop
2417   //    c_rarg1    -  src_pos (32-bits)
2418   //    c_rarg2    -  dst oop
2419   //    c_rarg3    -  dst_pos (32-bits)
2420   // not Win64
2421   //    c_rarg4    -  element count (32-bits)
2422   // Win64
2423   //    rsp+40     -  element count (32-bits)
2424   //
2425   //  Output:
2426   //    rax ==  0  -  success
2427   //    rax == -1^K - failure, where K is partial transfer count
2428   //
2429   address generate_generic_copy(const char *name,
2430                                 address byte_copy_entry, address short_copy_entry,
2431                                 address int_copy_entry, address long_copy_entry,
2432                                 address oop_copy_entry, address checkcast_copy_entry) {
2433 
2434     Label L_failed, L_failed_0, L_objArray;
2435     Label L_copy_bytes, L_copy_shorts, L_copy_ints, L_copy_longs;
2436 
2437     // Input registers
2438     const Register src        = c_rarg0;  // source array oop
2439     const Register src_pos    = c_rarg1;  // source position
2440     const Register dst        = c_rarg2;  // destination array oop
2441     const Register dst_pos    = c_rarg3;  // destination position
2442 #ifndef _WIN64
2443     const Register length     = c_rarg4;
2444 #else
2445     const Address  length(rsp, 6 * wordSize);  // elements count is on stack on Win64
2446 #endif
2447 
2448     { int modulus = CodeEntryAlignment;
2449       int target  = modulus - 5; // 5 = sizeof jmp(L_failed)
2450       int advance = target - (__ offset() % modulus);
2451       if (advance < 0)  advance += modulus;
2452       if (advance > 0)  __ nop(advance);
2453     }
2454     StubCodeMark mark(this, "StubRoutines", name);
2455 
2456     // Short-hop target to L_failed.  Makes for denser prologue code.
2457     __ BIND(L_failed_0);
2458     __ jmp(L_failed);
2459     assert(__ offset() % CodeEntryAlignment == 0, "no further alignment needed");
2460 
2461     __ align(CodeEntryAlignment);
2462     address start = __ pc();
2463 
2464     __ enter(); // required for proper stackwalking of RuntimeStub frame
2465 
2466     // bump this on entry, not on exit:
2467     inc_counter_np(SharedRuntime::_generic_array_copy_ctr);
2468 
2469     //-----------------------------------------------------------------------
2470     // Assembler stub will be used for this call to arraycopy
2471     // if the following conditions are met:
2472     //
2473     // (1) src and dst must not be null.
2474     // (2) src_pos must not be negative.
2475     // (3) dst_pos must not be negative.
2476     // (4) length  must not be negative.
2477     // (5) src klass and dst klass should be the same and not NULL.
2478     // (6) src and dst should be arrays.
2479     // (7) src_pos + length must not exceed length of src.
2480     // (8) dst_pos + length must not exceed length of dst.
2481     //
2482 
2483     //  if (src == NULL) return -1;
2484     __ testptr(src, src);         // src oop
2485     size_t j1off = __ offset();
2486     __ jccb(Assembler::zero, L_failed_0);
2487 
2488     //  if (src_pos < 0) return -1;
2489     __ testl(src_pos, src_pos); // src_pos (32-bits)
2490     __ jccb(Assembler::negative, L_failed_0);
2491 
2492     //  if (dst == NULL) return -1;
2493     __ testptr(dst, dst);         // dst oop
2494     __ jccb(Assembler::zero, L_failed_0);
2495 
2496     //  if (dst_pos < 0) return -1;
2497     __ testl(dst_pos, dst_pos); // dst_pos (32-bits)
2498     size_t j4off = __ offset();
2499     __ jccb(Assembler::negative, L_failed_0);
2500 
2501     // The first four tests are very dense code,
2502     // but not quite dense enough to put four
2503     // jumps in a 16-byte instruction fetch buffer.
2504     // That's good, because some branch predicters
2505     // do not like jumps so close together.
2506     // Make sure of this.
2507     guarantee(((j1off ^ j4off) & ~15) != 0, "I$ line of 1st & 4th jumps");
2508 
2509     // registers used as temp
2510     const Register r11_length    = r11; // elements count to copy
2511     const Register r10_src_klass = r10; // array klass
2512 
2513     //  if (length < 0) return -1;
2514     __ movl(r11_length, length);        // length (elements count, 32-bits value)
2515     __ testl(r11_length, r11_length);
2516     __ jccb(Assembler::negative, L_failed_0);
2517 
2518     __ load_klass(r10_src_klass, src);
2519 #ifdef ASSERT
2520     //  assert(src->klass() != NULL);
2521     {
2522       BLOCK_COMMENT("assert klasses not null {");
2523       Label L1, L2;
2524       __ testptr(r10_src_klass, r10_src_klass);
2525       __ jcc(Assembler::notZero, L2);   // it is broken if klass is NULL
2526       __ bind(L1);
2527       __ stop("broken null klass");
2528       __ bind(L2);
2529       __ load_klass(rax, dst);
2530       __ cmpq(rax, 0);
2531       __ jcc(Assembler::equal, L1);     // this would be broken also
2532       BLOCK_COMMENT("} assert klasses not null done");
2533     }
2534 #endif
2535 
2536     // Load layout helper (32-bits)
2537     //
2538     //  |array_tag|     | header_size | element_type |     |log2_element_size|
2539     // 32        30    24            16              8     2                 0
2540     //
2541     //   array_tag: typeArray = 0x3, objArray = 0x2, non-array = 0x0
2542     //
2543 
2544     const int lh_offset = klassOopDesc::header_size() * HeapWordSize +
2545                           Klass::layout_helper_offset_in_bytes();
2546 
2547     // Handle objArrays completely differently...
2548     const jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
2549     __ cmpl(Address(r10_src_klass, lh_offset), objArray_lh);
2550     __ jcc(Assembler::equal, L_objArray);
2551 
2552     //  if (src->klass() != dst->klass()) return -1;
2553     __ load_klass(rax, dst);
2554     __ cmpq(r10_src_klass, rax);
2555     __ jcc(Assembler::notEqual, L_failed);
2556 
2557     const Register rax_lh = rax;  // layout helper
2558     __ movl(rax_lh, Address(r10_src_klass, lh_offset));
2559 
2560     //  if (!src->is_Array()) return -1;
2561     __ cmpl(rax_lh, Klass::_lh_neutral_value);
2562     __ jcc(Assembler::greaterEqual, L_failed);
2563 
2564     // At this point, it is known to be a typeArray (array_tag 0x3).
2565 #ifdef ASSERT
2566     {
2567       BLOCK_COMMENT("assert primitive array {");
2568       Label L;
2569       __ cmpl(rax_lh, (Klass::_lh_array_tag_type_value << Klass::_lh_array_tag_shift));
2570       __ jcc(Assembler::greaterEqual, L);
2571       __ stop("must be a primitive array");
2572       __ bind(L);
2573       BLOCK_COMMENT("} assert primitive array done");
2574     }
2575 #endif
2576 
2577     arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
2578                            r10, L_failed);
2579 
2580     // typeArrayKlass
2581     //
2582     // src_addr = (src + array_header_in_bytes()) + (src_pos << log2elemsize);
2583     // dst_addr = (dst + array_header_in_bytes()) + (dst_pos << log2elemsize);
2584     //
2585 
2586     const Register r10_offset = r10;    // array offset
2587     const Register rax_elsize = rax_lh; // element size
2588 
2589     __ movl(r10_offset, rax_lh);
2590     __ shrl(r10_offset, Klass::_lh_header_size_shift);
2591     __ andptr(r10_offset, Klass::_lh_header_size_mask);   // array_offset
2592     __ addptr(src, r10_offset);           // src array offset
2593     __ addptr(dst, r10_offset);           // dst array offset
2594     BLOCK_COMMENT("choose copy loop based on element size");
2595     __ andl(rax_lh, Klass::_lh_log2_element_size_mask); // rax_lh -> rax_elsize
2596 
2597     // next registers should be set before the jump to corresponding stub
2598     const Register from     = c_rarg0;  // source array address
2599     const Register to       = c_rarg1;  // destination array address
2600     const Register count    = c_rarg2;  // elements count
2601 
2602     // 'from', 'to', 'count' registers should be set in such order
2603     // since they are the same as 'src', 'src_pos', 'dst'.
2604 
2605   __ BIND(L_copy_bytes);
2606     __ cmpl(rax_elsize, 0);
2607     __ jccb(Assembler::notEqual, L_copy_shorts);
2608     __ lea(from, Address(src, src_pos, Address::times_1, 0));// src_addr
2609     __ lea(to,   Address(dst, dst_pos, Address::times_1, 0));// dst_addr
2610     __ movl2ptr(count, r11_length); // length
2611     __ jump(RuntimeAddress(byte_copy_entry));
2612 
2613   __ BIND(L_copy_shorts);
2614     __ cmpl(rax_elsize, LogBytesPerShort);
2615     __ jccb(Assembler::notEqual, L_copy_ints);
2616     __ lea(from, Address(src, src_pos, Address::times_2, 0));// src_addr
2617     __ lea(to,   Address(dst, dst_pos, Address::times_2, 0));// dst_addr
2618     __ movl2ptr(count, r11_length); // length
2619     __ jump(RuntimeAddress(short_copy_entry));
2620 
2621   __ BIND(L_copy_ints);
2622     __ cmpl(rax_elsize, LogBytesPerInt);
2623     __ jccb(Assembler::notEqual, L_copy_longs);
2624     __ lea(from, Address(src, src_pos, Address::times_4, 0));// src_addr
2625     __ lea(to,   Address(dst, dst_pos, Address::times_4, 0));// dst_addr
2626     __ movl2ptr(count, r11_length); // length
2627     __ jump(RuntimeAddress(int_copy_entry));
2628 
2629   __ BIND(L_copy_longs);
2630 #ifdef ASSERT
2631     {
2632       BLOCK_COMMENT("assert long copy {");
2633       Label L;
2634       __ cmpl(rax_elsize, LogBytesPerLong);
2635       __ jcc(Assembler::equal, L);
2636       __ stop("must be long copy, but elsize is wrong");
2637       __ bind(L);
2638       BLOCK_COMMENT("} assert long copy done");
2639     }
2640 #endif
2641     __ lea(from, Address(src, src_pos, Address::times_8, 0));// src_addr
2642     __ lea(to,   Address(dst, dst_pos, Address::times_8, 0));// dst_addr
2643     __ movl2ptr(count, r11_length); // length
2644     __ jump(RuntimeAddress(long_copy_entry));
2645 
2646     // objArrayKlass
2647   __ BIND(L_objArray);
2648     // live at this point:  r10_src_klass, r11_length, src[_pos], dst[_pos]
2649 
2650     Label L_plain_copy, L_checkcast_copy;
2651     //  test array classes for subtyping
2652     __ load_klass(rax, dst);
2653     __ cmpq(r10_src_klass, rax); // usual case is exact equality
2654     __ jcc(Assembler::notEqual, L_checkcast_copy);
2655 
2656     // Identically typed arrays can be copied without element-wise checks.
2657     arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
2658                            r10, L_failed);
2659 
2660     __ lea(from, Address(src, src_pos, TIMES_OOP,
2661                  arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // src_addr
2662     __ lea(to,   Address(dst, dst_pos, TIMES_OOP,
2663                  arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // dst_addr
2664     __ movl2ptr(count, r11_length); // length
2665   __ BIND(L_plain_copy);
2666     __ jump(RuntimeAddress(oop_copy_entry));
2667 
2668   __ BIND(L_checkcast_copy);
2669     // live at this point:  r10_src_klass, r11_length, rax (dst_klass)
2670     {
2671       // Before looking at dst.length, make sure dst is also an objArray.
2672       __ cmpl(Address(rax, lh_offset), objArray_lh);
2673       __ jcc(Assembler::notEqual, L_failed);
2674 
2675       // It is safe to examine both src.length and dst.length.
2676       arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
2677                              rax, L_failed);
2678 
2679       const Register r11_dst_klass = r11;
2680       __ load_klass(r11_dst_klass, dst); // reload
2681 
2682       // Marshal the base address arguments now, freeing registers.
2683       __ lea(from, Address(src, src_pos, TIMES_OOP,
2684                    arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
2685       __ lea(to,   Address(dst, dst_pos, TIMES_OOP,
2686                    arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
2687       __ movl(count, length);           // length (reloaded)
2688       Register sco_temp = c_rarg3;      // this register is free now
2689       assert_different_registers(from, to, count, sco_temp,
2690                                  r11_dst_klass, r10_src_klass);
2691       assert_clean_int(count, sco_temp);
2692 
2693       // Generate the type check.
2694       const int sco_offset = (klassOopDesc::header_size() * HeapWordSize +
2695                               Klass::super_check_offset_offset_in_bytes());
2696       __ movl(sco_temp, Address(r11_dst_klass, sco_offset));
2697       assert_clean_int(sco_temp, rax);
2698       generate_type_check(r10_src_klass, sco_temp, r11_dst_klass, L_plain_copy);
2699 
2700       // Fetch destination element klass from the objArrayKlass header.
2701       int ek_offset = (klassOopDesc::header_size() * HeapWordSize +
2702                        objArrayKlass::element_klass_offset_in_bytes());
2703       __ movptr(r11_dst_klass, Address(r11_dst_klass, ek_offset));
2704       __ movl(  sco_temp,      Address(r11_dst_klass, sco_offset));
2705       assert_clean_int(sco_temp, rax);
2706 
2707       // the checkcast_copy loop needs two extra arguments:
2708       assert(c_rarg3 == sco_temp, "#3 already in place");
2709       // Set up arguments for checkcast_copy_entry.
2710       setup_arg_regs(4);
2711       __ movptr(r8, r11_dst_klass);  // dst.klass.element_klass, r8 is c_rarg4 on Linux/Solaris
2712       __ jump(RuntimeAddress(checkcast_copy_entry));
2713     }
2714 
2715   __ BIND(L_failed);
2716     __ xorptr(rax, rax);
2717     __ notptr(rax); // return -1
2718     __ leave();   // required for proper stackwalking of RuntimeStub frame
2719     __ ret(0);
2720 
2721     return start;
2722   }
2723 
2724   void generate_arraycopy_stubs() {
2725     address entry;
2726     address entry_jbyte_arraycopy;
2727     address entry_jshort_arraycopy;
2728     address entry_jint_arraycopy;
2729     address entry_oop_arraycopy;
2730     address entry_jlong_arraycopy;
2731     address entry_checkcast_arraycopy;
2732 
2733     StubRoutines::_jbyte_disjoint_arraycopy  = generate_disjoint_byte_copy(false, &entry,
2734                                                                            "jbyte_disjoint_arraycopy");
2735     StubRoutines::_jbyte_arraycopy           = generate_conjoint_byte_copy(false, entry, &entry_jbyte_arraycopy,
2736                                                                            "jbyte_arraycopy");
2737 
2738     StubRoutines::_jshort_disjoint_arraycopy = generate_disjoint_short_copy(false, &entry,
2739                                                                             "jshort_disjoint_arraycopy");
2740     StubRoutines::_jshort_arraycopy          = generate_conjoint_short_copy(false, entry, &entry_jshort_arraycopy,
2741                                                                             "jshort_arraycopy");
2742 
2743     StubRoutines::_jint_disjoint_arraycopy   = generate_disjoint_int_oop_copy(false, false, &entry,
2744                                                                               "jint_disjoint_arraycopy");
2745     StubRoutines::_jint_arraycopy            = generate_conjoint_int_oop_copy(false, false, entry,
2746                                                                               &entry_jint_arraycopy, "jint_arraycopy");
2747 
2748     StubRoutines::_jlong_disjoint_arraycopy  = generate_disjoint_long_oop_copy(false, false, &entry,
2749                                                                                "jlong_disjoint_arraycopy");
2750     StubRoutines::_jlong_arraycopy           = generate_conjoint_long_oop_copy(false, false, entry,
2751                                                                                &entry_jlong_arraycopy, "jlong_arraycopy");
2752 
2753 
2754     if (UseCompressedOops) {
2755       StubRoutines::_oop_disjoint_arraycopy  = generate_disjoint_int_oop_copy(false, true, &entry,
2756                                                                               "oop_disjoint_arraycopy");
2757       StubRoutines::_oop_arraycopy           = generate_conjoint_int_oop_copy(false, true, entry,
2758                                                                               &entry_oop_arraycopy, "oop_arraycopy");
2759       StubRoutines::_oop_disjoint_arraycopy_uninit  = generate_disjoint_int_oop_copy(false, true, &entry,
2760                                                                                      "oop_disjoint_arraycopy_uninit",
2761                                                                                      /*dest_uninitialized*/true);
2762       StubRoutines::_oop_arraycopy_uninit           = generate_conjoint_int_oop_copy(false, true, entry,
2763                                                                                      NULL, "oop_arraycopy_uninit",
2764                                                                                      /*dest_uninitialized*/true);
2765     } else {
2766       StubRoutines::_oop_disjoint_arraycopy  = generate_disjoint_long_oop_copy(false, true, &entry,
2767                                                                                "oop_disjoint_arraycopy");
2768       StubRoutines::_oop_arraycopy           = generate_conjoint_long_oop_copy(false, true, entry,
2769                                                                                &entry_oop_arraycopy, "oop_arraycopy");
2770       StubRoutines::_oop_disjoint_arraycopy_uninit  = generate_disjoint_long_oop_copy(false, true, &entry,
2771                                                                                       "oop_disjoint_arraycopy_uninit",
2772                                                                                       /*dest_uninitialized*/true);
2773       StubRoutines::_oop_arraycopy_uninit           = generate_conjoint_long_oop_copy(false, true, entry,
2774                                                                                       NULL, "oop_arraycopy_uninit",
2775                                                                                       /*dest_uninitialized*/true);
2776     }
2777 
2778     StubRoutines::_checkcast_arraycopy        = generate_checkcast_copy("checkcast_arraycopy", &entry_checkcast_arraycopy);
2779     StubRoutines::_checkcast_arraycopy_uninit = generate_checkcast_copy("checkcast_arraycopy_uninit", NULL,
2780                                                                         /*dest_uninitialized*/true);
2781 
2782     StubRoutines::_unsafe_arraycopy    = generate_unsafe_copy("unsafe_arraycopy",
2783                                                               entry_jbyte_arraycopy,
2784                                                               entry_jshort_arraycopy,
2785                                                               entry_jint_arraycopy,
2786                                                               entry_jlong_arraycopy);
2787     StubRoutines::_generic_arraycopy   = generate_generic_copy("generic_arraycopy",
2788                                                                entry_jbyte_arraycopy,
2789                                                                entry_jshort_arraycopy,
2790                                                                entry_jint_arraycopy,
2791                                                                entry_oop_arraycopy,
2792                                                                entry_jlong_arraycopy,
2793                                                                entry_checkcast_arraycopy);
2794 
2795     StubRoutines::_jbyte_fill = generate_fill(T_BYTE, false, "jbyte_fill");
2796     StubRoutines::_jshort_fill = generate_fill(T_SHORT, false, "jshort_fill");
2797     StubRoutines::_jint_fill = generate_fill(T_INT, false, "jint_fill");
2798     StubRoutines::_arrayof_jbyte_fill = generate_fill(T_BYTE, true, "arrayof_jbyte_fill");
2799     StubRoutines::_arrayof_jshort_fill = generate_fill(T_SHORT, true, "arrayof_jshort_fill");
2800     StubRoutines::_arrayof_jint_fill = generate_fill(T_INT, true, "arrayof_jint_fill");
2801 
2802     // We don't generate specialized code for HeapWord-aligned source
2803     // arrays, so just use the code we've already generated
2804     StubRoutines::_arrayof_jbyte_disjoint_arraycopy  = StubRoutines::_jbyte_disjoint_arraycopy;
2805     StubRoutines::_arrayof_jbyte_arraycopy           = StubRoutines::_jbyte_arraycopy;
2806 
2807     StubRoutines::_arrayof_jshort_disjoint_arraycopy = StubRoutines::_jshort_disjoint_arraycopy;
2808     StubRoutines::_arrayof_jshort_arraycopy          = StubRoutines::_jshort_arraycopy;
2809 
2810     StubRoutines::_arrayof_jint_disjoint_arraycopy   = StubRoutines::_jint_disjoint_arraycopy;
2811     StubRoutines::_arrayof_jint_arraycopy            = StubRoutines::_jint_arraycopy;
2812 
2813     StubRoutines::_arrayof_jlong_disjoint_arraycopy  = StubRoutines::_jlong_disjoint_arraycopy;
2814     StubRoutines::_arrayof_jlong_arraycopy           = StubRoutines::_jlong_arraycopy;
2815 
2816     StubRoutines::_arrayof_oop_disjoint_arraycopy    = StubRoutines::_oop_disjoint_arraycopy;
2817     StubRoutines::_arrayof_oop_arraycopy             = StubRoutines::_oop_arraycopy;
2818 
2819     StubRoutines::_arrayof_oop_disjoint_arraycopy_uninit    = StubRoutines::_oop_disjoint_arraycopy_uninit;
2820     StubRoutines::_arrayof_oop_arraycopy_uninit             = StubRoutines::_oop_arraycopy_uninit;
2821   }
2822 
2823   void generate_math_stubs() {
2824     {
2825       StubCodeMark mark(this, "StubRoutines", "log");
2826       StubRoutines::_intrinsic_log = (double (*)(double)) __ pc();
2827 
2828       __ subq(rsp, 8);
2829       __ movdbl(Address(rsp, 0), xmm0);
2830       __ fld_d(Address(rsp, 0));
2831       __ flog();
2832       __ fstp_d(Address(rsp, 0));
2833       __ movdbl(xmm0, Address(rsp, 0));
2834       __ addq(rsp, 8);
2835       __ ret(0);
2836     }
2837     {
2838       StubCodeMark mark(this, "StubRoutines", "log10");
2839       StubRoutines::_intrinsic_log10 = (double (*)(double)) __ pc();
2840 
2841       __ subq(rsp, 8);
2842       __ movdbl(Address(rsp, 0), xmm0);
2843       __ fld_d(Address(rsp, 0));
2844       __ flog10();
2845       __ fstp_d(Address(rsp, 0));
2846       __ movdbl(xmm0, Address(rsp, 0));
2847       __ addq(rsp, 8);
2848       __ ret(0);
2849     }
2850     {
2851       StubCodeMark mark(this, "StubRoutines", "sin");
2852       StubRoutines::_intrinsic_sin = (double (*)(double)) __ pc();
2853 
2854       __ subq(rsp, 8);
2855       __ movdbl(Address(rsp, 0), xmm0);
2856       __ fld_d(Address(rsp, 0));
2857       __ trigfunc('s');
2858       __ fstp_d(Address(rsp, 0));
2859       __ movdbl(xmm0, Address(rsp, 0));
2860       __ addq(rsp, 8);
2861       __ ret(0);
2862     }
2863     {
2864       StubCodeMark mark(this, "StubRoutines", "cos");
2865       StubRoutines::_intrinsic_cos = (double (*)(double)) __ pc();
2866 
2867       __ subq(rsp, 8);
2868       __ movdbl(Address(rsp, 0), xmm0);
2869       __ fld_d(Address(rsp, 0));
2870       __ trigfunc('c');
2871       __ fstp_d(Address(rsp, 0));
2872       __ movdbl(xmm0, Address(rsp, 0));
2873       __ addq(rsp, 8);
2874       __ ret(0);
2875     }
2876     {
2877       StubCodeMark mark(this, "StubRoutines", "tan");
2878       StubRoutines::_intrinsic_tan = (double (*)(double)) __ pc();
2879 
2880       __ subq(rsp, 8);
2881       __ movdbl(Address(rsp, 0), xmm0);
2882       __ fld_d(Address(rsp, 0));
2883       __ trigfunc('t');
2884       __ fstp_d(Address(rsp, 0));
2885       __ movdbl(xmm0, Address(rsp, 0));
2886       __ addq(rsp, 8);
2887       __ ret(0);
2888     }
2889 
2890     // The intrinsic version of these seem to return the same value as
2891     // the strict version.
2892     StubRoutines::_intrinsic_exp = SharedRuntime::dexp;
2893     StubRoutines::_intrinsic_pow = SharedRuntime::dpow;
2894   }
2895 
2896 #undef __
2897 #define __ masm->
2898 
2899   // Continuation point for throwing of implicit exceptions that are
2900   // not handled in the current activation. Fabricates an exception
2901   // oop and initiates normal exception dispatching in this
2902   // frame. Since we need to preserve callee-saved values (currently
2903   // only for C2, but done for C1 as well) we need a callee-saved oop
2904   // map and therefore have to make these stubs into RuntimeStubs
2905   // rather than BufferBlobs.  If the compiler needs all registers to
2906   // be preserved between the fault point and the exception handler
2907   // then it must assume responsibility for that in
2908   // AbstractCompiler::continuation_for_implicit_null_exception or
2909   // continuation_for_implicit_division_by_zero_exception. All other
2910   // implicit exceptions (e.g., NullPointerException or
2911   // AbstractMethodError on entry) are either at call sites or
2912   // otherwise assume that stack unwinding will be initiated, so
2913   // caller saved registers were assumed volatile in the compiler.
2914   address generate_throw_exception(const char* name,
2915                                    address runtime_entry,
2916                                    bool restore_saved_exception_pc) {
2917     // Information about frame layout at time of blocking runtime call.
2918     // Note that we only have to preserve callee-saved registers since
2919     // the compilers are responsible for supplying a continuation point
2920     // if they expect all registers to be preserved.
2921     enum layout {
2922       rbp_off = frame::arg_reg_save_area_bytes/BytesPerInt,
2923       rbp_off2,
2924       return_off,
2925       return_off2,
2926       framesize // inclusive of return address
2927     };
2928 
2929     int insts_size = 512;
2930     int locs_size  = 64;
2931 
2932     CodeBuffer code(name, insts_size, locs_size);
2933     OopMapSet* oop_maps  = new OopMapSet();
2934     MacroAssembler* masm = new MacroAssembler(&code);
2935 
2936     address start = __ pc();
2937 
2938     // This is an inlined and slightly modified version of call_VM
2939     // which has the ability to fetch the return PC out of
2940     // thread-local storage and also sets up last_Java_sp slightly
2941     // differently than the real call_VM
2942     if (restore_saved_exception_pc) {
2943       __ movptr(rax,
2944                 Address(r15_thread,
2945                         in_bytes(JavaThread::saved_exception_pc_offset())));
2946       __ push(rax);
2947     }
2948 
2949     __ enter(); // required for proper stackwalking of RuntimeStub frame
2950 
2951     assert(is_even(framesize/2), "sp not 16-byte aligned");
2952 
2953     // return address and rbp are already in place
2954     __ subptr(rsp, (framesize-4) << LogBytesPerInt); // prolog
2955 
2956     int frame_complete = __ pc() - start;
2957 
2958     // Set up last_Java_sp and last_Java_fp
2959     __ set_last_Java_frame(rsp, rbp, NULL);
2960 
2961     // Call runtime
2962     __ movptr(c_rarg0, r15_thread);
2963     BLOCK_COMMENT("call runtime_entry");
2964     __ call(RuntimeAddress(runtime_entry));
2965 
2966     // Generate oop map
2967     OopMap* map = new OopMap(framesize, 0);
2968 
2969     oop_maps->add_gc_map(__ pc() - start, map);
2970 
2971     __ reset_last_Java_frame(true, false);
2972 
2973     __ leave(); // required for proper stackwalking of RuntimeStub frame
2974 
2975     // check for pending exceptions
2976 #ifdef ASSERT
2977     Label L;
2978     __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()),
2979             (int32_t) NULL_WORD);
2980     __ jcc(Assembler::notEqual, L);
2981     __ should_not_reach_here();
2982     __ bind(L);
2983 #endif // ASSERT
2984     __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
2985 
2986 
2987     // codeBlob framesize is in words (not VMRegImpl::slot_size)
2988     RuntimeStub* stub =
2989       RuntimeStub::new_runtime_stub(name,
2990                                     &code,
2991                                     frame_complete,
2992                                     (framesize >> (LogBytesPerWord - LogBytesPerInt)),
2993                                     oop_maps, false);
2994     return stub->entry_point();
2995   }
2996 
2997   // Initialization
2998   void generate_initial() {
2999     // Generates all stubs and initializes the entry points
3000 
3001     // This platform-specific stub is needed by generate_call_stub()
3002     StubRoutines::x86::_mxcsr_std        = generate_fp_mask("mxcsr_std",        0x0000000000001F80);
3003 
3004     // entry points that exist in all platforms Note: This is code
3005     // that could be shared among different platforms - however the
3006     // benefit seems to be smaller than the disadvantage of having a
3007     // much more complicated generator structure. See also comment in
3008     // stubRoutines.hpp.
3009 
3010     StubRoutines::_forward_exception_entry = generate_forward_exception();
3011 
3012     StubRoutines::_call_stub_entry =
3013       generate_call_stub(StubRoutines::_call_stub_return_address);
3014 
3015     // is referenced by megamorphic call
3016     StubRoutines::_catch_exception_entry = generate_catch_exception();
3017 
3018     // atomic calls
3019     StubRoutines::_atomic_xchg_entry         = generate_atomic_xchg();
3020     StubRoutines::_atomic_xchg_ptr_entry     = generate_atomic_xchg_ptr();
3021     StubRoutines::_atomic_cmpxchg_entry      = generate_atomic_cmpxchg();
3022     StubRoutines::_atomic_cmpxchg_long_entry = generate_atomic_cmpxchg_long();
3023     StubRoutines::_atomic_add_entry          = generate_atomic_add();
3024     StubRoutines::_atomic_add_ptr_entry      = generate_atomic_add_ptr();
3025     StubRoutines::_fence_entry               = generate_orderaccess_fence();
3026 
3027     StubRoutines::_handler_for_unsafe_access_entry =
3028       generate_handler_for_unsafe_access();
3029 
3030     // platform dependent
3031     StubRoutines::x86::_get_previous_fp_entry = generate_get_previous_fp();
3032 
3033     StubRoutines::x86::_verify_mxcsr_entry    = generate_verify_mxcsr();
3034   }
3035 
3036   void generate_all() {
3037     // Generates all stubs and initializes the entry points
3038 
3039     // These entry points require SharedInfo::stack0 to be set up in
3040     // non-core builds and need to be relocatable, so they each
3041     // fabricate a RuntimeStub internally.
3042     StubRoutines::_throw_AbstractMethodError_entry =
3043       generate_throw_exception("AbstractMethodError throw_exception",
3044                                CAST_FROM_FN_PTR(address,
3045                                                 SharedRuntime::
3046                                                 throw_AbstractMethodError),
3047                                false);
3048 
3049     StubRoutines::_throw_IncompatibleClassChangeError_entry =
3050       generate_throw_exception("IncompatibleClassChangeError throw_exception",
3051                                CAST_FROM_FN_PTR(address,
3052                                                 SharedRuntime::
3053                                                 throw_IncompatibleClassChangeError),
3054                                false);
3055 
3056     StubRoutines::_throw_ArithmeticException_entry =
3057       generate_throw_exception("ArithmeticException throw_exception",
3058                                CAST_FROM_FN_PTR(address,
3059                                                 SharedRuntime::
3060                                                 throw_ArithmeticException),
3061                                true);
3062 
3063     StubRoutines::_throw_NullPointerException_entry =
3064       generate_throw_exception("NullPointerException throw_exception",
3065                                CAST_FROM_FN_PTR(address,
3066                                                 SharedRuntime::
3067                                                 throw_NullPointerException),
3068                                true);
3069 
3070     StubRoutines::_throw_NullPointerException_at_call_entry =
3071       generate_throw_exception("NullPointerException at call throw_exception",
3072                                CAST_FROM_FN_PTR(address,
3073                                                 SharedRuntime::
3074                                                 throw_NullPointerException_at_call),
3075                                false);
3076 
3077     StubRoutines::_throw_StackOverflowError_entry =
3078       generate_throw_exception("StackOverflowError throw_exception",
3079                                CAST_FROM_FN_PTR(address,
3080                                                 SharedRuntime::
3081                                                 throw_StackOverflowError),
3082                                false);
3083 
3084     // entry points that are platform specific
3085     StubRoutines::x86::_f2i_fixup = generate_f2i_fixup();
3086     StubRoutines::x86::_f2l_fixup = generate_f2l_fixup();
3087     StubRoutines::x86::_d2i_fixup = generate_d2i_fixup();
3088     StubRoutines::x86::_d2l_fixup = generate_d2l_fixup();
3089 
3090     StubRoutines::x86::_float_sign_mask  = generate_fp_mask("float_sign_mask",  0x7FFFFFFF7FFFFFFF);
3091     StubRoutines::x86::_float_sign_flip  = generate_fp_mask("float_sign_flip",  0x8000000080000000);
3092     StubRoutines::x86::_double_sign_mask = generate_fp_mask("double_sign_mask", 0x7FFFFFFFFFFFFFFF);
3093     StubRoutines::x86::_double_sign_flip = generate_fp_mask("double_sign_flip", 0x8000000000000000);
3094 
3095     // support for verify_oop (must happen after universe_init)
3096     StubRoutines::_verify_oop_subroutine_entry = generate_verify_oop();
3097 
3098     // arraycopy stubs used by compilers
3099     generate_arraycopy_stubs();
3100 
3101     generate_math_stubs();
3102   }
3103 
3104  public:
3105   StubGenerator(CodeBuffer* code, bool all) : StubCodeGenerator(code) {
3106     if (all) {
3107       generate_all();
3108     } else {
3109       generate_initial();
3110     }
3111   }
3112 }; // end class declaration
3113 
3114 void StubGenerator_generate(CodeBuffer* code, bool all) {
3115   StubGenerator g(code, all);
3116 }