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   //
1164   //     Destroy no registers!
1165   //
1166   void  gen_write_ref_array_pre_barrier(Register addr, Register count) {
1167     BarrierSet* bs = Universe::heap()->barrier_set();
1168     switch (bs->kind()) {
1169       case BarrierSet::G1SATBCT:
1170       case BarrierSet::G1SATBCTLogging:
1171         {
1172           __ pusha();                      // push registers
1173           if (count == c_rarg0) {
1174             if (addr == c_rarg1) {
1175               // exactly backwards!!
1176               __ xchgptr(c_rarg1, c_rarg0);
1177             } else {
1178               __ movptr(c_rarg1, count);
1179               __ movptr(c_rarg0, addr);
1180             }
1181 
1182           } else {
1183             __ movptr(c_rarg0, addr);
1184             __ movptr(c_rarg1, count);
1185           }
1186           __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_pre), 2);
1187           __ popa();
1188         }
1189         break;
1190       case BarrierSet::CardTableModRef:
1191       case BarrierSet::CardTableExtension:
1192       case BarrierSet::ModRef:
1193         break;
1194       default:
1195         ShouldNotReachHere();
1196 
1197     }
1198   }
1199 
1200   //
1201   // Generate code for an array write post barrier
1202   //
1203   //  Input:
1204   //     start    - register containing starting address of destination array
1205   //     end      - register containing ending address of destination array
1206   //     scratch  - scratch register
1207   //
1208   //  The input registers are overwritten.
1209   //  The ending address is inclusive.
1210   void  gen_write_ref_array_post_barrier(Register start, Register end, Register scratch) {
1211     assert_different_registers(start, end, scratch);
1212     BarrierSet* bs = Universe::heap()->barrier_set();
1213     switch (bs->kind()) {
1214       case BarrierSet::G1SATBCT:
1215       case BarrierSet::G1SATBCTLogging:
1216 
1217         {
1218           __ pusha();                      // push registers (overkill)
1219           // must compute element count unless barrier set interface is changed (other platforms supply count)
1220           assert_different_registers(start, end, scratch);
1221           __ lea(scratch, Address(end, BytesPerHeapOop));
1222           __ subptr(scratch, start);               // subtract start to get #bytes
1223           __ shrptr(scratch, LogBytesPerHeapOop);  // convert to element count
1224           __ mov(c_rarg0, start);
1225           __ mov(c_rarg1, scratch);
1226           __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_post), 2);
1227           __ popa();
1228         }
1229         break;
1230       case BarrierSet::CardTableModRef:
1231       case BarrierSet::CardTableExtension:
1232         {
1233           CardTableModRefBS* ct = (CardTableModRefBS*)bs;
1234           assert(sizeof(*ct->byte_map_base) == sizeof(jbyte), "adjust this code");
1235 
1236           Label L_loop;
1237 
1238            __ shrptr(start, CardTableModRefBS::card_shift);
1239            __ addptr(end, BytesPerHeapOop);
1240            __ shrptr(end, CardTableModRefBS::card_shift);
1241            __ subptr(end, start); // number of bytes to copy
1242 
1243           intptr_t disp = (intptr_t) ct->byte_map_base;
1244           if (__ is_simm32(disp)) {
1245             Address cardtable(noreg, noreg, Address::no_scale, disp);
1246             __ lea(scratch, cardtable);
1247           } else {
1248             ExternalAddress cardtable((address)disp);
1249             __ lea(scratch, cardtable);
1250           }
1251 
1252           const Register count = end; // 'end' register contains bytes count now
1253           __ addptr(start, scratch);
1254         __ BIND(L_loop);
1255           __ movb(Address(start, count, Address::times_1), 0);
1256           __ decrement(count);
1257           __ jcc(Assembler::greaterEqual, L_loop);
1258         }
1259         break;
1260       default:
1261         ShouldNotReachHere();
1262 
1263     }
1264   }
1265 
1266 
1267   // Copy big chunks forward
1268   //
1269   // Inputs:
1270   //   end_from     - source arrays end address
1271   //   end_to       - destination array end address
1272   //   qword_count  - 64-bits element count, negative
1273   //   to           - scratch
1274   //   L_copy_32_bytes - entry label
1275   //   L_copy_8_bytes  - exit  label
1276   //
1277   void copy_32_bytes_forward(Register end_from, Register end_to,
1278                              Register qword_count, Register to,
1279                              Label& L_copy_32_bytes, Label& L_copy_8_bytes) {
1280     DEBUG_ONLY(__ stop("enter at entry label, not here"));
1281     Label L_loop;
1282     __ align(OptoLoopAlignment);
1283   __ BIND(L_loop);
1284     if(UseUnalignedLoadStores) {
1285       __ movdqu(xmm0, Address(end_from, qword_count, Address::times_8, -24));
1286       __ movdqu(Address(end_to, qword_count, Address::times_8, -24), xmm0);
1287       __ movdqu(xmm1, Address(end_from, qword_count, Address::times_8, - 8));
1288       __ movdqu(Address(end_to, qword_count, Address::times_8, - 8), xmm1);
1289 
1290     } else {
1291       __ movq(to, Address(end_from, qword_count, Address::times_8, -24));
1292       __ movq(Address(end_to, qword_count, Address::times_8, -24), to);
1293       __ movq(to, Address(end_from, qword_count, Address::times_8, -16));
1294       __ movq(Address(end_to, qword_count, Address::times_8, -16), to);
1295       __ movq(to, Address(end_from, qword_count, Address::times_8, - 8));
1296       __ movq(Address(end_to, qword_count, Address::times_8, - 8), to);
1297       __ movq(to, Address(end_from, qword_count, Address::times_8, - 0));
1298       __ movq(Address(end_to, qword_count, Address::times_8, - 0), to);
1299     }
1300   __ BIND(L_copy_32_bytes);
1301     __ addptr(qword_count, 4);
1302     __ jcc(Assembler::lessEqual, L_loop);
1303     __ subptr(qword_count, 4);
1304     __ jcc(Assembler::less, L_copy_8_bytes); // Copy trailing qwords
1305   }
1306 
1307 
1308   // Copy big chunks backward
1309   //
1310   // Inputs:
1311   //   from         - source arrays address
1312   //   dest         - destination array address
1313   //   qword_count  - 64-bits element count
1314   //   to           - scratch
1315   //   L_copy_32_bytes - entry label
1316   //   L_copy_8_bytes  - exit  label
1317   //
1318   void copy_32_bytes_backward(Register from, Register dest,
1319                               Register qword_count, Register to,
1320                               Label& L_copy_32_bytes, Label& L_copy_8_bytes) {
1321     DEBUG_ONLY(__ stop("enter at entry label, not here"));
1322     Label L_loop;
1323     __ align(OptoLoopAlignment);
1324   __ BIND(L_loop);
1325     if(UseUnalignedLoadStores) {
1326       __ movdqu(xmm0, Address(from, qword_count, Address::times_8, 16));
1327       __ movdqu(Address(dest, qword_count, Address::times_8, 16), xmm0);
1328       __ movdqu(xmm1, Address(from, qword_count, Address::times_8,  0));
1329       __ movdqu(Address(dest, qword_count, Address::times_8,  0), xmm1);
1330 
1331     } else {
1332       __ movq(to, Address(from, qword_count, Address::times_8, 24));
1333       __ movq(Address(dest, qword_count, Address::times_8, 24), to);
1334       __ movq(to, Address(from, qword_count, Address::times_8, 16));
1335       __ movq(Address(dest, qword_count, Address::times_8, 16), to);
1336       __ movq(to, Address(from, qword_count, Address::times_8,  8));
1337       __ movq(Address(dest, qword_count, Address::times_8,  8), to);
1338       __ movq(to, Address(from, qword_count, Address::times_8,  0));
1339       __ movq(Address(dest, qword_count, Address::times_8,  0), to);
1340     }
1341   __ BIND(L_copy_32_bytes);
1342     __ subptr(qword_count, 4);
1343     __ jcc(Assembler::greaterEqual, L_loop);
1344     __ addptr(qword_count, 4);
1345     __ jcc(Assembler::greater, L_copy_8_bytes); // Copy trailing qwords
1346   }
1347 
1348 
1349   // Arguments:
1350   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1351   //             ignored
1352   //   name    - stub name string
1353   //
1354   // Inputs:
1355   //   c_rarg0   - source array address
1356   //   c_rarg1   - destination array address
1357   //   c_rarg2   - element count, treated as ssize_t, can be zero
1358   //
1359   // If 'from' and/or 'to' are aligned on 4-, 2-, or 1-byte boundaries,
1360   // we let the hardware handle it.  The one to eight bytes within words,
1361   // dwords or qwords that span cache line boundaries will still be loaded
1362   // and stored atomically.
1363   //
1364   // Side Effects:
1365   //   disjoint_byte_copy_entry is set to the no-overlap entry point
1366   //   used by generate_conjoint_byte_copy().
1367   //
1368   address generate_disjoint_byte_copy(bool aligned, address* entry, const char *name) {
1369     __ align(CodeEntryAlignment);
1370     StubCodeMark mark(this, "StubRoutines", name);
1371     address start = __ pc();
1372 
1373     Label L_copy_32_bytes, L_copy_8_bytes, L_copy_4_bytes, L_copy_2_bytes;
1374     Label L_copy_byte, L_exit;
1375     const Register from        = rdi;  // source array address
1376     const Register to          = rsi;  // destination array address
1377     const Register count       = rdx;  // elements count
1378     const Register byte_count  = rcx;
1379     const Register qword_count = count;
1380     const Register end_from    = from; // source array end address
1381     const Register end_to      = to;   // destination array end address
1382     // End pointers are inclusive, and if count is not zero they point
1383     // to the last unit copied:  end_to[0] := end_from[0]
1384 
1385     __ enter(); // required for proper stackwalking of RuntimeStub frame
1386     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1387 
1388     if (entry != NULL) {
1389       *entry = __ pc();
1390        // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1391       BLOCK_COMMENT("Entry:");
1392     }
1393 
1394     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1395                       // r9 and r10 may be used to save non-volatile registers
1396 
1397     // 'from', 'to' and 'count' are now valid
1398     __ movptr(byte_count, count);
1399     __ shrptr(count, 3); // count => qword_count
1400 
1401     // Copy from low to high addresses.  Use 'to' as scratch.
1402     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1403     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1404     __ negptr(qword_count); // make the count negative
1405     __ jmp(L_copy_32_bytes);
1406 
1407     // Copy trailing qwords
1408   __ BIND(L_copy_8_bytes);
1409     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
1410     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
1411     __ increment(qword_count);
1412     __ jcc(Assembler::notZero, L_copy_8_bytes);
1413 
1414     // Check for and copy trailing dword
1415   __ BIND(L_copy_4_bytes);
1416     __ testl(byte_count, 4);
1417     __ jccb(Assembler::zero, L_copy_2_bytes);
1418     __ movl(rax, Address(end_from, 8));
1419     __ movl(Address(end_to, 8), rax);
1420 
1421     __ addptr(end_from, 4);
1422     __ addptr(end_to, 4);
1423 
1424     // Check for and copy trailing word
1425   __ BIND(L_copy_2_bytes);
1426     __ testl(byte_count, 2);
1427     __ jccb(Assembler::zero, L_copy_byte);
1428     __ movw(rax, Address(end_from, 8));
1429     __ movw(Address(end_to, 8), rax);
1430 
1431     __ addptr(end_from, 2);
1432     __ addptr(end_to, 2);
1433 
1434     // Check for and copy trailing byte
1435   __ BIND(L_copy_byte);
1436     __ testl(byte_count, 1);
1437     __ jccb(Assembler::zero, L_exit);
1438     __ movb(rax, Address(end_from, 8));
1439     __ movb(Address(end_to, 8), rax);
1440 
1441   __ BIND(L_exit);
1442     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr);
1443     restore_arg_regs();
1444     __ xorptr(rax, rax); // return 0
1445     __ leave(); // required for proper stackwalking of RuntimeStub frame
1446     __ ret(0);
1447 
1448     // Copy in 32-bytes chunks
1449     copy_32_bytes_forward(end_from, end_to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
1450     __ jmp(L_copy_4_bytes);
1451 
1452     return start;
1453   }
1454 
1455   // Arguments:
1456   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1457   //             ignored
1458   //   name    - stub name string
1459   //
1460   // Inputs:
1461   //   c_rarg0   - source array address
1462   //   c_rarg1   - destination array address
1463   //   c_rarg2   - element count, treated as ssize_t, can be zero
1464   //
1465   // If 'from' and/or 'to' are aligned on 4-, 2-, or 1-byte boundaries,
1466   // we let the hardware handle it.  The one to eight bytes within words,
1467   // dwords or qwords that span cache line boundaries will still be loaded
1468   // and stored atomically.
1469   //
1470   address generate_conjoint_byte_copy(bool aligned, address nooverlap_target,
1471                                       address* entry, const char *name) {
1472     __ align(CodeEntryAlignment);
1473     StubCodeMark mark(this, "StubRoutines", name);
1474     address start = __ pc();
1475 
1476     Label L_copy_32_bytes, L_copy_8_bytes, L_copy_4_bytes, L_copy_2_bytes;
1477     const Register from        = rdi;  // source array address
1478     const Register to          = rsi;  // destination array address
1479     const Register count       = rdx;  // elements count
1480     const Register byte_count  = rcx;
1481     const Register qword_count = count;
1482 
1483     __ enter(); // required for proper stackwalking of RuntimeStub frame
1484     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1485 
1486     if (entry != NULL) {
1487       *entry = __ pc();
1488       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1489       BLOCK_COMMENT("Entry:");
1490     }
1491 
1492     array_overlap_test(nooverlap_target, Address::times_1);
1493     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1494                       // r9 and r10 may be used to save non-volatile registers
1495 
1496     // 'from', 'to' and 'count' are now valid
1497     __ movptr(byte_count, count);
1498     __ shrptr(count, 3);   // count => qword_count
1499 
1500     // Copy from high to low addresses.
1501 
1502     // Check for and copy trailing byte
1503     __ testl(byte_count, 1);
1504     __ jcc(Assembler::zero, L_copy_2_bytes);
1505     __ movb(rax, Address(from, byte_count, Address::times_1, -1));
1506     __ movb(Address(to, byte_count, Address::times_1, -1), rax);
1507     __ decrement(byte_count); // Adjust for possible trailing word
1508 
1509     // Check for and copy trailing word
1510   __ BIND(L_copy_2_bytes);
1511     __ testl(byte_count, 2);
1512     __ jcc(Assembler::zero, L_copy_4_bytes);
1513     __ movw(rax, Address(from, byte_count, Address::times_1, -2));
1514     __ movw(Address(to, byte_count, Address::times_1, -2), rax);
1515 
1516     // Check for and copy trailing dword
1517   __ BIND(L_copy_4_bytes);
1518     __ testl(byte_count, 4);
1519     __ jcc(Assembler::zero, L_copy_32_bytes);
1520     __ movl(rax, Address(from, qword_count, Address::times_8));
1521     __ movl(Address(to, qword_count, Address::times_8), rax);
1522     __ jmp(L_copy_32_bytes);
1523 
1524     // Copy trailing qwords
1525   __ BIND(L_copy_8_bytes);
1526     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
1527     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
1528     __ decrement(qword_count);
1529     __ jcc(Assembler::notZero, L_copy_8_bytes);
1530 
1531     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr);
1532     restore_arg_regs();
1533     __ xorptr(rax, rax); // return 0
1534     __ leave(); // required for proper stackwalking of RuntimeStub frame
1535     __ ret(0);
1536 
1537     // Copy in 32-bytes chunks
1538     copy_32_bytes_backward(from, to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
1539 
1540     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr);
1541     restore_arg_regs();
1542     __ xorptr(rax, rax); // return 0
1543     __ leave(); // required for proper stackwalking of RuntimeStub frame
1544     __ ret(0);
1545 
1546     return start;
1547   }
1548 
1549   // Arguments:
1550   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1551   //             ignored
1552   //   name    - stub name string
1553   //
1554   // Inputs:
1555   //   c_rarg0   - source array address
1556   //   c_rarg1   - destination array address
1557   //   c_rarg2   - element count, treated as ssize_t, can be zero
1558   //
1559   // If 'from' and/or 'to' are aligned on 4- or 2-byte boundaries, we
1560   // let the hardware handle it.  The two or four words within dwords
1561   // or qwords that span cache line boundaries will still be loaded
1562   // and stored atomically.
1563   //
1564   // Side Effects:
1565   //   disjoint_short_copy_entry is set to the no-overlap entry point
1566   //   used by generate_conjoint_short_copy().
1567   //
1568   address generate_disjoint_short_copy(bool aligned, address *entry, const char *name) {
1569     __ align(CodeEntryAlignment);
1570     StubCodeMark mark(this, "StubRoutines", name);
1571     address start = __ pc();
1572 
1573     Label L_copy_32_bytes, L_copy_8_bytes, L_copy_4_bytes,L_copy_2_bytes,L_exit;
1574     const Register from        = rdi;  // source array address
1575     const Register to          = rsi;  // destination array address
1576     const Register count       = rdx;  // elements count
1577     const Register word_count  = rcx;
1578     const Register qword_count = count;
1579     const Register end_from    = from; // source array end address
1580     const Register end_to      = to;   // destination array end address
1581     // End pointers are inclusive, and if count is not zero they point
1582     // to the last unit copied:  end_to[0] := end_from[0]
1583 
1584     __ enter(); // required for proper stackwalking of RuntimeStub frame
1585     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1586 
1587     if (entry != NULL) {
1588       *entry = __ pc();
1589       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1590       BLOCK_COMMENT("Entry:");
1591     }
1592 
1593     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1594                       // r9 and r10 may be used to save non-volatile registers
1595 
1596     // 'from', 'to' and 'count' are now valid
1597     __ movptr(word_count, count);
1598     __ shrptr(count, 2); // count => qword_count
1599 
1600     // Copy from low to high addresses.  Use 'to' as scratch.
1601     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1602     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1603     __ negptr(qword_count);
1604     __ jmp(L_copy_32_bytes);
1605 
1606     // Copy trailing qwords
1607   __ BIND(L_copy_8_bytes);
1608     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
1609     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
1610     __ increment(qword_count);
1611     __ jcc(Assembler::notZero, L_copy_8_bytes);
1612 
1613     // Original 'dest' is trashed, so we can't use it as a
1614     // base register for a possible trailing word copy
1615 
1616     // Check for and copy trailing dword
1617   __ BIND(L_copy_4_bytes);
1618     __ testl(word_count, 2);
1619     __ jccb(Assembler::zero, L_copy_2_bytes);
1620     __ movl(rax, Address(end_from, 8));
1621     __ movl(Address(end_to, 8), rax);
1622 
1623     __ addptr(end_from, 4);
1624     __ addptr(end_to, 4);
1625 
1626     // Check for and copy trailing word
1627   __ BIND(L_copy_2_bytes);
1628     __ testl(word_count, 1);
1629     __ jccb(Assembler::zero, L_exit);
1630     __ movw(rax, Address(end_from, 8));
1631     __ movw(Address(end_to, 8), rax);
1632 
1633   __ BIND(L_exit);
1634     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr);
1635     restore_arg_regs();
1636     __ xorptr(rax, rax); // return 0
1637     __ leave(); // required for proper stackwalking of RuntimeStub frame
1638     __ ret(0);
1639 
1640     // Copy in 32-bytes chunks
1641     copy_32_bytes_forward(end_from, end_to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
1642     __ jmp(L_copy_4_bytes);
1643 
1644     return start;
1645   }
1646 
1647   address generate_fill(BasicType t, bool aligned, const char *name) {
1648     __ align(CodeEntryAlignment);
1649     StubCodeMark mark(this, "StubRoutines", name);
1650     address start = __ pc();
1651 
1652     BLOCK_COMMENT("Entry:");
1653 
1654     const Register to       = c_rarg0;  // source array address
1655     const Register value    = c_rarg1;  // value
1656     const Register count    = c_rarg2;  // elements count
1657 
1658     __ enter(); // required for proper stackwalking of RuntimeStub frame
1659 
1660     __ generate_fill(t, aligned, to, value, count, rax, xmm0);
1661 
1662     __ leave(); // required for proper stackwalking of RuntimeStub frame
1663     __ ret(0);
1664     return start;
1665   }
1666 
1667   // Arguments:
1668   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1669   //             ignored
1670   //   name    - stub name string
1671   //
1672   // Inputs:
1673   //   c_rarg0   - source array address
1674   //   c_rarg1   - destination array address
1675   //   c_rarg2   - element count, treated as ssize_t, can be zero
1676   //
1677   // If 'from' and/or 'to' are aligned on 4- or 2-byte boundaries, we
1678   // let the hardware handle it.  The two or four words within dwords
1679   // or qwords that span cache line boundaries will still be loaded
1680   // and stored atomically.
1681   //
1682   address generate_conjoint_short_copy(bool aligned, address nooverlap_target,
1683                                        address *entry, const char *name) {
1684     __ align(CodeEntryAlignment);
1685     StubCodeMark mark(this, "StubRoutines", name);
1686     address start = __ pc();
1687 
1688     Label L_copy_32_bytes, L_copy_8_bytes, L_copy_4_bytes;
1689     const Register from        = rdi;  // source array address
1690     const Register to          = rsi;  // destination array address
1691     const Register count       = rdx;  // elements count
1692     const Register word_count  = rcx;
1693     const Register qword_count = count;
1694 
1695     __ enter(); // required for proper stackwalking of RuntimeStub frame
1696     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1697 
1698     if (entry != NULL) {
1699       *entry = __ pc();
1700       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1701       BLOCK_COMMENT("Entry:");
1702     }
1703 
1704     array_overlap_test(nooverlap_target, Address::times_2);
1705     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1706                       // r9 and r10 may be used to save non-volatile registers
1707 
1708     // 'from', 'to' and 'count' are now valid
1709     __ movptr(word_count, count);
1710     __ shrptr(count, 2); // count => qword_count
1711 
1712     // Copy from high to low addresses.  Use 'to' as scratch.
1713 
1714     // Check for and copy trailing word
1715     __ testl(word_count, 1);
1716     __ jccb(Assembler::zero, L_copy_4_bytes);
1717     __ movw(rax, Address(from, word_count, Address::times_2, -2));
1718     __ movw(Address(to, word_count, Address::times_2, -2), rax);
1719 
1720     // Check for and copy trailing dword
1721   __ BIND(L_copy_4_bytes);
1722     __ testl(word_count, 2);
1723     __ jcc(Assembler::zero, L_copy_32_bytes);
1724     __ movl(rax, Address(from, qword_count, Address::times_8));
1725     __ movl(Address(to, qword_count, Address::times_8), rax);
1726     __ jmp(L_copy_32_bytes);
1727 
1728     // Copy trailing qwords
1729   __ BIND(L_copy_8_bytes);
1730     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
1731     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
1732     __ decrement(qword_count);
1733     __ jcc(Assembler::notZero, L_copy_8_bytes);
1734 
1735     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr);
1736     restore_arg_regs();
1737     __ xorptr(rax, rax); // return 0
1738     __ leave(); // required for proper stackwalking of RuntimeStub frame
1739     __ ret(0);
1740 
1741     // Copy in 32-bytes chunks
1742     copy_32_bytes_backward(from, to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
1743 
1744     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr);
1745     restore_arg_regs();
1746     __ xorptr(rax, rax); // return 0
1747     __ leave(); // required for proper stackwalking of RuntimeStub frame
1748     __ ret(0);
1749 
1750     return start;
1751   }
1752 
1753   // Arguments:
1754   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1755   //             ignored
1756   //   is_oop  - true => oop array, so generate store check code
1757   //   name    - stub name string
1758   //
1759   // Inputs:
1760   //   c_rarg0   - source array address
1761   //   c_rarg1   - destination array address
1762   //   c_rarg2   - element count, treated as ssize_t, can be zero
1763   //
1764   // If 'from' and/or 'to' are aligned on 4-byte boundaries, we let
1765   // the hardware handle it.  The two dwords within qwords that span
1766   // cache line boundaries will still be loaded and stored atomicly.
1767   //
1768   // Side Effects:
1769   //   disjoint_int_copy_entry is set to the no-overlap entry point
1770   //   used by generate_conjoint_int_oop_copy().
1771   //
1772   address generate_disjoint_int_oop_copy(bool aligned, bool is_oop, address* entry, const char *name) {
1773     __ align(CodeEntryAlignment);
1774     StubCodeMark mark(this, "StubRoutines", name);
1775     address start = __ pc();
1776 
1777     Label L_copy_32_bytes, L_copy_8_bytes, L_copy_4_bytes, L_exit;
1778     const Register from        = rdi;  // source array address
1779     const Register to          = rsi;  // destination array address
1780     const Register count       = rdx;  // elements count
1781     const Register dword_count = rcx;
1782     const Register qword_count = count;
1783     const Register end_from    = from; // source array end address
1784     const Register end_to      = to;   // destination array end address
1785     const Register saved_to    = r11;  // saved destination array address
1786     // End pointers are inclusive, and if count is not zero they point
1787     // to the last unit copied:  end_to[0] := end_from[0]
1788 
1789     __ enter(); // required for proper stackwalking of RuntimeStub frame
1790     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1791 
1792     if (entry != NULL) {
1793       *entry = __ pc();
1794       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1795       BLOCK_COMMENT("Entry:");
1796     }
1797 
1798     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1799                       // r9 and r10 may be used to save non-volatile registers
1800     if (is_oop) {
1801       __ movq(saved_to, to);
1802       gen_write_ref_array_pre_barrier(to, count);
1803     }
1804 
1805     // 'from', 'to' and 'count' are now valid
1806     __ movptr(dword_count, count);
1807     __ shrptr(count, 1); // count => qword_count
1808 
1809     // Copy from low to high addresses.  Use 'to' as scratch.
1810     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1811     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1812     __ negptr(qword_count);
1813     __ jmp(L_copy_32_bytes);
1814 
1815     // Copy trailing qwords
1816   __ BIND(L_copy_8_bytes);
1817     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
1818     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
1819     __ increment(qword_count);
1820     __ jcc(Assembler::notZero, L_copy_8_bytes);
1821 
1822     // Check for and copy trailing dword
1823   __ BIND(L_copy_4_bytes);
1824     __ testl(dword_count, 1); // Only byte test since the value is 0 or 1
1825     __ jccb(Assembler::zero, L_exit);
1826     __ movl(rax, Address(end_from, 8));
1827     __ movl(Address(end_to, 8), rax);
1828 
1829   __ BIND(L_exit);
1830     if (is_oop) {
1831       __ leaq(end_to, Address(saved_to, dword_count, Address::times_4, -4));
1832       gen_write_ref_array_post_barrier(saved_to, end_to, rax);
1833     }
1834     inc_counter_np(SharedRuntime::_jint_array_copy_ctr);
1835     restore_arg_regs();
1836     __ xorptr(rax, rax); // return 0
1837     __ leave(); // required for proper stackwalking of RuntimeStub frame
1838     __ ret(0);
1839 
1840     // Copy 32-bytes chunks
1841     copy_32_bytes_forward(end_from, end_to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
1842     __ jmp(L_copy_4_bytes);
1843 
1844     return start;
1845   }
1846 
1847   // Arguments:
1848   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
1849   //             ignored
1850   //   is_oop  - true => oop array, so generate store check code
1851   //   name    - stub name string
1852   //
1853   // Inputs:
1854   //   c_rarg0   - source array address
1855   //   c_rarg1   - destination array address
1856   //   c_rarg2   - element count, treated as ssize_t, can be zero
1857   //
1858   // If 'from' and/or 'to' are aligned on 4-byte boundaries, we let
1859   // the hardware handle it.  The two dwords within qwords that span
1860   // cache line boundaries will still be loaded and stored atomicly.
1861   //
1862   address generate_conjoint_int_oop_copy(bool aligned, bool is_oop, address nooverlap_target,
1863                                          address *entry, const char *name) {
1864     __ align(CodeEntryAlignment);
1865     StubCodeMark mark(this, "StubRoutines", name);
1866     address start = __ pc();
1867 
1868     Label L_copy_32_bytes, L_copy_8_bytes, L_copy_2_bytes, L_exit;
1869     const Register from        = rdi;  // source array address
1870     const Register to          = rsi;  // destination array address
1871     const Register count       = rdx;  // elements count
1872     const Register dword_count = rcx;
1873     const Register qword_count = count;
1874 
1875     __ enter(); // required for proper stackwalking of RuntimeStub frame
1876     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1877 
1878     if (entry != NULL) {
1879       *entry = __ pc();
1880        // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1881       BLOCK_COMMENT("Entry:");
1882     }
1883 
1884     array_overlap_test(nooverlap_target, Address::times_4);
1885     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1886                       // r9 and r10 may be used to save non-volatile registers
1887 
1888     if (is_oop) {
1889       // no registers are destroyed by this call
1890       gen_write_ref_array_pre_barrier(to, count);
1891     }
1892 
1893     assert_clean_int(count, rax); // Make sure 'count' is clean int.
1894     // 'from', 'to' and 'count' are now valid
1895     __ movptr(dword_count, count);
1896     __ shrptr(count, 1); // count => qword_count
1897 
1898     // Copy from high to low addresses.  Use 'to' as scratch.
1899 
1900     // Check for and copy trailing dword
1901     __ testl(dword_count, 1);
1902     __ jcc(Assembler::zero, L_copy_32_bytes);
1903     __ movl(rax, Address(from, dword_count, Address::times_4, -4));
1904     __ movl(Address(to, dword_count, Address::times_4, -4), rax);
1905     __ jmp(L_copy_32_bytes);
1906 
1907     // Copy trailing qwords
1908   __ BIND(L_copy_8_bytes);
1909     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
1910     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
1911     __ decrement(qword_count);
1912     __ jcc(Assembler::notZero, L_copy_8_bytes);
1913 
1914     inc_counter_np(SharedRuntime::_jint_array_copy_ctr);
1915     if (is_oop) {
1916       __ jmp(L_exit);
1917     }
1918     restore_arg_regs();
1919     __ xorptr(rax, rax); // return 0
1920     __ leave(); // required for proper stackwalking of RuntimeStub frame
1921     __ ret(0);
1922 
1923     // Copy in 32-bytes chunks
1924     copy_32_bytes_backward(from, to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
1925 
1926    inc_counter_np(SharedRuntime::_jint_array_copy_ctr);
1927    __ bind(L_exit);
1928      if (is_oop) {
1929        Register end_to = rdx;
1930        __ leaq(end_to, Address(to, dword_count, Address::times_4, -4));
1931        gen_write_ref_array_post_barrier(to, end_to, rax);
1932      }
1933     restore_arg_regs();
1934     __ xorptr(rax, rax); // return 0
1935     __ leave(); // required for proper stackwalking of RuntimeStub frame
1936     __ ret(0);
1937 
1938     return start;
1939   }
1940 
1941   // Arguments:
1942   //   aligned - true => Input and output aligned on a HeapWord boundary == 8 bytes
1943   //             ignored
1944   //   is_oop  - true => oop array, so generate store check code
1945   //   name    - stub name string
1946   //
1947   // Inputs:
1948   //   c_rarg0   - source array address
1949   //   c_rarg1   - destination array address
1950   //   c_rarg2   - element count, treated as ssize_t, can be zero
1951   //
1952  // Side Effects:
1953   //   disjoint_oop_copy_entry or disjoint_long_copy_entry is set to the
1954   //   no-overlap entry point used by generate_conjoint_long_oop_copy().
1955   //
1956   address generate_disjoint_long_oop_copy(bool aligned, bool is_oop, address *entry, const char *name) {
1957     __ align(CodeEntryAlignment);
1958     StubCodeMark mark(this, "StubRoutines", name);
1959     address start = __ pc();
1960 
1961     Label L_copy_32_bytes, L_copy_8_bytes, L_exit;
1962     const Register from        = rdi;  // source array address
1963     const Register to          = rsi;  // destination array address
1964     const Register qword_count = rdx;  // elements count
1965     const Register end_from    = from; // source array end address
1966     const Register end_to      = rcx;  // destination array end address
1967     const Register saved_to    = to;
1968     // End pointers are inclusive, and if count is not zero they point
1969     // to the last unit copied:  end_to[0] := end_from[0]
1970 
1971     __ enter(); // required for proper stackwalking of RuntimeStub frame
1972     // Save no-overlap entry point for generate_conjoint_long_oop_copy()
1973     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
1974 
1975     if (entry != NULL) {
1976       *entry = __ pc();
1977       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
1978       BLOCK_COMMENT("Entry:");
1979     }
1980 
1981     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
1982                       // r9 and r10 may be used to save non-volatile registers
1983     // 'from', 'to' and 'qword_count' are now valid
1984     if (is_oop) {
1985       // no registers are destroyed by this call
1986       gen_write_ref_array_pre_barrier(to, qword_count);
1987     }
1988 
1989     // Copy from low to high addresses.  Use 'to' as scratch.
1990     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
1991     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
1992     __ negptr(qword_count);
1993     __ jmp(L_copy_32_bytes);
1994 
1995     // Copy trailing qwords
1996   __ BIND(L_copy_8_bytes);
1997     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
1998     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
1999     __ increment(qword_count);
2000     __ jcc(Assembler::notZero, L_copy_8_bytes);
2001 
2002     if (is_oop) {
2003       __ jmp(L_exit);
2004     } else {
2005       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr);
2006       restore_arg_regs();
2007       __ xorptr(rax, rax); // return 0
2008       __ leave(); // required for proper stackwalking of RuntimeStub frame
2009       __ ret(0);
2010     }
2011 
2012     // Copy 64-byte chunks
2013     copy_32_bytes_forward(end_from, end_to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
2014 
2015     if (is_oop) {
2016     __ BIND(L_exit);
2017       gen_write_ref_array_post_barrier(saved_to, end_to, rax);
2018       inc_counter_np(SharedRuntime::_oop_array_copy_ctr);
2019     } else {
2020       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr);
2021     }
2022     restore_arg_regs();
2023     __ xorptr(rax, rax); // return 0
2024     __ leave(); // required for proper stackwalking of RuntimeStub frame
2025     __ ret(0);
2026 
2027     return start;
2028   }
2029 
2030   // Arguments:
2031   //   aligned - true => Input and output aligned on a HeapWord boundary == 8 bytes
2032   //             ignored
2033   //   is_oop  - true => oop array, so generate store check code
2034   //   name    - stub name string
2035   //
2036   // Inputs:
2037   //   c_rarg0   - source array address
2038   //   c_rarg1   - destination array address
2039   //   c_rarg2   - element count, treated as ssize_t, can be zero
2040   //
2041   address generate_conjoint_long_oop_copy(bool aligned, bool is_oop, address nooverlap_target,
2042                                           address *entry, const char *name) {
2043     __ align(CodeEntryAlignment);
2044     StubCodeMark mark(this, "StubRoutines", name);
2045     address start = __ pc();
2046 
2047     Label L_copy_32_bytes, L_copy_8_bytes, L_exit;
2048     const Register from        = rdi;  // source array address
2049     const Register to          = rsi;  // destination array address
2050     const Register qword_count = rdx;  // elements count
2051     const Register saved_count = rcx;
2052 
2053     __ enter(); // required for proper stackwalking of RuntimeStub frame
2054     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
2055 
2056     if (entry != NULL) {
2057       *entry = __ pc();
2058       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
2059       BLOCK_COMMENT("Entry:");
2060     }
2061 
2062     array_overlap_test(nooverlap_target, Address::times_8);
2063     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
2064                       // r9 and r10 may be used to save non-volatile registers
2065     // 'from', 'to' and 'qword_count' are now valid
2066     if (is_oop) {
2067       // Save to and count for store barrier
2068       __ movptr(saved_count, qword_count);
2069       // No registers are destroyed by this call
2070       gen_write_ref_array_pre_barrier(to, saved_count);
2071     }
2072 
2073     __ jmp(L_copy_32_bytes);
2074 
2075     // Copy trailing qwords
2076   __ BIND(L_copy_8_bytes);
2077     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
2078     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
2079     __ decrement(qword_count);
2080     __ jcc(Assembler::notZero, L_copy_8_bytes);
2081 
2082     if (is_oop) {
2083       __ jmp(L_exit);
2084     } else {
2085       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr);
2086       restore_arg_regs();
2087       __ xorptr(rax, rax); // return 0
2088       __ leave(); // required for proper stackwalking of RuntimeStub frame
2089       __ ret(0);
2090     }
2091 
2092     // Copy in 32-bytes chunks
2093     copy_32_bytes_backward(from, to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
2094 
2095     if (is_oop) {
2096     __ BIND(L_exit);
2097       __ lea(rcx, Address(to, saved_count, Address::times_8, -8));
2098       gen_write_ref_array_post_barrier(to, rcx, rax);
2099       inc_counter_np(SharedRuntime::_oop_array_copy_ctr);
2100     } else {
2101       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr);
2102     }
2103     restore_arg_regs();
2104     __ xorptr(rax, rax); // return 0
2105     __ leave(); // required for proper stackwalking of RuntimeStub frame
2106     __ ret(0);
2107 
2108     return start;
2109   }
2110 
2111 
2112   // Helper for generating a dynamic type check.
2113   // Smashes no registers.
2114   void generate_type_check(Register sub_klass,
2115                            Register super_check_offset,
2116                            Register super_klass,
2117                            Label& L_success) {
2118     assert_different_registers(sub_klass, super_check_offset, super_klass);
2119 
2120     BLOCK_COMMENT("type_check:");
2121 
2122     Label L_miss;
2123 
2124     __ check_klass_subtype_fast_path(sub_klass, super_klass, noreg,        &L_success, &L_miss, NULL,
2125                                      super_check_offset);
2126     __ check_klass_subtype_slow_path(sub_klass, super_klass, noreg, noreg, &L_success, NULL);
2127 
2128     // Fall through on failure!
2129     __ BIND(L_miss);
2130   }
2131 
2132   //
2133   //  Generate checkcasting array copy stub
2134   //
2135   //  Input:
2136   //    c_rarg0   - source array address
2137   //    c_rarg1   - destination array address
2138   //    c_rarg2   - element count, treated as ssize_t, can be zero
2139   //    c_rarg3   - size_t ckoff (super_check_offset)
2140   // not Win64
2141   //    c_rarg4   - oop ckval (super_klass)
2142   // Win64
2143   //    rsp+40    - oop ckval (super_klass)
2144   //
2145   //  Output:
2146   //    rax ==  0  -  success
2147   //    rax == -1^K - failure, where K is partial transfer count
2148   //
2149   address generate_checkcast_copy(const char *name, address *entry) {
2150 
2151     Label L_load_element, L_store_element, L_do_card_marks, L_done;
2152 
2153     // Input registers (after setup_arg_regs)
2154     const Register from        = rdi;   // source array address
2155     const Register to          = rsi;   // destination array address
2156     const Register length      = rdx;   // elements count
2157     const Register ckoff       = rcx;   // super_check_offset
2158     const Register ckval       = r8;    // super_klass
2159 
2160     // Registers used as temps (r13, r14 are save-on-entry)
2161     const Register end_from    = from;  // source array end address
2162     const Register end_to      = r13;   // destination array end address
2163     const Register count       = rdx;   // -(count_remaining)
2164     const Register r14_length  = r14;   // saved copy of length
2165     // End pointers are inclusive, and if length is not zero they point
2166     // to the last unit copied:  end_to[0] := end_from[0]
2167 
2168     const Register rax_oop    = rax;    // actual oop copied
2169     const Register r11_klass  = r11;    // oop._klass
2170 
2171     //---------------------------------------------------------------
2172     // Assembler stub will be used for this call to arraycopy
2173     // if the two arrays are subtypes of Object[] but the
2174     // destination array type is not equal to or a supertype
2175     // of the source type.  Each element must be separately
2176     // checked.
2177 
2178     __ align(CodeEntryAlignment);
2179     StubCodeMark mark(this, "StubRoutines", name);
2180     address start = __ pc();
2181 
2182     __ enter(); // required for proper stackwalking of RuntimeStub frame
2183 
2184 #ifdef ASSERT
2185     // caller guarantees that the arrays really are different
2186     // otherwise, we would have to make conjoint checks
2187     { Label L;
2188       array_overlap_test(L, TIMES_OOP);
2189       __ stop("checkcast_copy within a single array");
2190       __ bind(L);
2191     }
2192 #endif //ASSERT
2193 
2194     setup_arg_regs(4); // from => rdi, to => rsi, length => rdx
2195                        // ckoff => rcx, ckval => r8
2196                        // r9 and r10 may be used to save non-volatile registers
2197 #ifdef _WIN64
2198     // last argument (#4) is on stack on Win64
2199     __ movptr(ckval, Address(rsp, 6 * wordSize));
2200 #endif
2201 
2202     // Caller of this entry point must set up the argument registers.
2203     if (entry != NULL) {
2204       *entry = __ pc();
2205       BLOCK_COMMENT("Entry:");
2206     }
2207 
2208     // allocate spill slots for r13, r14
2209     enum {
2210       saved_r13_offset,
2211       saved_r14_offset,
2212       saved_rbp_offset
2213     };
2214     __ subptr(rsp, saved_rbp_offset * wordSize);
2215     __ movptr(Address(rsp, saved_r13_offset * wordSize), r13);
2216     __ movptr(Address(rsp, saved_r14_offset * wordSize), r14);
2217 
2218     // check that int operands are properly extended to size_t
2219     assert_clean_int(length, rax);
2220     assert_clean_int(ckoff, rax);
2221 
2222 #ifdef ASSERT
2223     BLOCK_COMMENT("assert consistent ckoff/ckval");
2224     // The ckoff and ckval must be mutually consistent,
2225     // even though caller generates both.
2226     { Label L;
2227       int sco_offset = (klassOopDesc::header_size() * HeapWordSize +
2228                         Klass::super_check_offset_offset_in_bytes());
2229       __ cmpl(ckoff, Address(ckval, sco_offset));
2230       __ jcc(Assembler::equal, L);
2231       __ stop("super_check_offset inconsistent");
2232       __ bind(L);
2233     }
2234 #endif //ASSERT
2235 
2236     // Loop-invariant addresses.  They are exclusive end pointers.
2237     Address end_from_addr(from, length, TIMES_OOP, 0);
2238     Address   end_to_addr(to,   length, TIMES_OOP, 0);
2239     // Loop-variant addresses.  They assume post-incremented count < 0.
2240     Address from_element_addr(end_from, count, TIMES_OOP, 0);
2241     Address   to_element_addr(end_to,   count, TIMES_OOP, 0);
2242 
2243     gen_write_ref_array_pre_barrier(to, count);
2244 
2245     // Copy from low to high addresses, indexed from the end of each array.
2246     __ lea(end_from, end_from_addr);
2247     __ lea(end_to,   end_to_addr);
2248     __ movptr(r14_length, length);        // save a copy of the length
2249     assert(length == count, "");          // else fix next line:
2250     __ negptr(count);                     // negate and test the length
2251     __ jcc(Assembler::notZero, L_load_element);
2252 
2253     // Empty array:  Nothing to do.
2254     __ xorptr(rax, rax);                  // return 0 on (trivial) success
2255     __ jmp(L_done);
2256 
2257     // ======== begin loop ========
2258     // (Loop is rotated; its entry is L_load_element.)
2259     // Loop control:
2260     //   for (count = -count; count != 0; count++)
2261     // Base pointers src, dst are biased by 8*(count-1),to last element.
2262     __ align(OptoLoopAlignment);
2263 
2264     __ BIND(L_store_element);
2265     __ store_heap_oop(to_element_addr, rax_oop);  // store the oop
2266     __ increment(count);               // increment the count toward zero
2267     __ jcc(Assembler::zero, L_do_card_marks);
2268 
2269     // ======== loop entry is here ========
2270     __ BIND(L_load_element);
2271     __ load_heap_oop(rax_oop, from_element_addr); // load the oop
2272     __ testptr(rax_oop, rax_oop);
2273     __ jcc(Assembler::zero, L_store_element);
2274 
2275     __ load_klass(r11_klass, rax_oop);// query the object klass
2276     generate_type_check(r11_klass, ckoff, ckval, L_store_element);
2277     // ======== end loop ========
2278 
2279     // It was a real error; we must depend on the caller to finish the job.
2280     // Register rdx = -1 * number of *remaining* oops, r14 = *total* oops.
2281     // Emit GC store barriers for the oops we have copied (r14 + rdx),
2282     // and report their number to the caller.
2283     assert_different_registers(rax, r14_length, count, to, end_to, rcx);
2284     __ lea(end_to, to_element_addr);
2285     __ addptr(end_to, -heapOopSize);      // make an inclusive end pointer
2286     gen_write_ref_array_post_barrier(to, end_to, rscratch1);
2287     __ movptr(rax, r14_length);           // original oops
2288     __ addptr(rax, count);                // K = (original - remaining) oops
2289     __ notptr(rax);                       // report (-1^K) to caller
2290     __ jmp(L_done);
2291 
2292     // Come here on success only.
2293     __ BIND(L_do_card_marks);
2294     __ addptr(end_to, -heapOopSize);         // make an inclusive end pointer
2295     gen_write_ref_array_post_barrier(to, end_to, rscratch1);
2296     __ xorptr(rax, rax);                  // return 0 on success
2297 
2298     // Common exit point (success or failure).
2299     __ BIND(L_done);
2300     __ movptr(r13, Address(rsp, saved_r13_offset * wordSize));
2301     __ movptr(r14, Address(rsp, saved_r14_offset * wordSize));
2302     inc_counter_np(SharedRuntime::_checkcast_array_copy_ctr);
2303     restore_arg_regs();
2304     __ leave(); // required for proper stackwalking of RuntimeStub frame
2305     __ ret(0);
2306 
2307     return start;
2308   }
2309 
2310   //
2311   //  Generate 'unsafe' array copy stub
2312   //  Though just as safe as the other stubs, it takes an unscaled
2313   //  size_t argument instead of an element count.
2314   //
2315   //  Input:
2316   //    c_rarg0   - source array address
2317   //    c_rarg1   - destination array address
2318   //    c_rarg2   - byte count, treated as ssize_t, can be zero
2319   //
2320   // Examines the alignment of the operands and dispatches
2321   // to a long, int, short, or byte copy loop.
2322   //
2323   address generate_unsafe_copy(const char *name,
2324                                address byte_copy_entry, address short_copy_entry,
2325                                address int_copy_entry, address long_copy_entry) {
2326 
2327     Label L_long_aligned, L_int_aligned, L_short_aligned;
2328 
2329     // Input registers (before setup_arg_regs)
2330     const Register from        = c_rarg0;  // source array address
2331     const Register to          = c_rarg1;  // destination array address
2332     const Register size        = c_rarg2;  // byte count (size_t)
2333 
2334     // Register used as a temp
2335     const Register bits        = rax;      // test copy of low bits
2336 
2337     __ align(CodeEntryAlignment);
2338     StubCodeMark mark(this, "StubRoutines", name);
2339     address start = __ pc();
2340 
2341     __ enter(); // required for proper stackwalking of RuntimeStub frame
2342 
2343     // bump this on entry, not on exit:
2344     inc_counter_np(SharedRuntime::_unsafe_array_copy_ctr);
2345 
2346     __ mov(bits, from);
2347     __ orptr(bits, to);
2348     __ orptr(bits, size);
2349 
2350     __ testb(bits, BytesPerLong-1);
2351     __ jccb(Assembler::zero, L_long_aligned);
2352 
2353     __ testb(bits, BytesPerInt-1);
2354     __ jccb(Assembler::zero, L_int_aligned);
2355 
2356     __ testb(bits, BytesPerShort-1);
2357     __ jump_cc(Assembler::notZero, RuntimeAddress(byte_copy_entry));
2358 
2359     __ BIND(L_short_aligned);
2360     __ shrptr(size, LogBytesPerShort); // size => short_count
2361     __ jump(RuntimeAddress(short_copy_entry));
2362 
2363     __ BIND(L_int_aligned);
2364     __ shrptr(size, LogBytesPerInt); // size => int_count
2365     __ jump(RuntimeAddress(int_copy_entry));
2366 
2367     __ BIND(L_long_aligned);
2368     __ shrptr(size, LogBytesPerLong); // size => qword_count
2369     __ jump(RuntimeAddress(long_copy_entry));
2370 
2371     return start;
2372   }
2373 
2374   // Perform range checks on the proposed arraycopy.
2375   // Kills temp, but nothing else.
2376   // Also, clean the sign bits of src_pos and dst_pos.
2377   void arraycopy_range_checks(Register src,     // source array oop (c_rarg0)
2378                               Register src_pos, // source position (c_rarg1)
2379                               Register dst,     // destination array oo (c_rarg2)
2380                               Register dst_pos, // destination position (c_rarg3)
2381                               Register length,
2382                               Register temp,
2383                               Label& L_failed) {
2384     BLOCK_COMMENT("arraycopy_range_checks:");
2385 
2386     //  if (src_pos + length > arrayOop(src)->length())  FAIL;
2387     __ movl(temp, length);
2388     __ addl(temp, src_pos);             // src_pos + length
2389     __ cmpl(temp, Address(src, arrayOopDesc::length_offset_in_bytes()));
2390     __ jcc(Assembler::above, L_failed);
2391 
2392     //  if (dst_pos + length > arrayOop(dst)->length())  FAIL;
2393     __ movl(temp, length);
2394     __ addl(temp, dst_pos);             // dst_pos + length
2395     __ cmpl(temp, Address(dst, arrayOopDesc::length_offset_in_bytes()));
2396     __ jcc(Assembler::above, L_failed);
2397 
2398     // Have to clean up high 32-bits of 'src_pos' and 'dst_pos'.
2399     // Move with sign extension can be used since they are positive.
2400     __ movslq(src_pos, src_pos);
2401     __ movslq(dst_pos, dst_pos);
2402 
2403     BLOCK_COMMENT("arraycopy_range_checks done");
2404   }
2405 
2406   //
2407   //  Generate generic array copy stubs
2408   //
2409   //  Input:
2410   //    c_rarg0    -  src oop
2411   //    c_rarg1    -  src_pos (32-bits)
2412   //    c_rarg2    -  dst oop
2413   //    c_rarg3    -  dst_pos (32-bits)
2414   // not Win64
2415   //    c_rarg4    -  element count (32-bits)
2416   // Win64
2417   //    rsp+40     -  element count (32-bits)
2418   //
2419   //  Output:
2420   //    rax ==  0  -  success
2421   //    rax == -1^K - failure, where K is partial transfer count
2422   //
2423   address generate_generic_copy(const char *name,
2424                                 address byte_copy_entry, address short_copy_entry,
2425                                 address int_copy_entry, address long_copy_entry,
2426                                 address oop_copy_entry, address checkcast_copy_entry) {
2427 
2428     Label L_failed, L_failed_0, L_objArray;
2429     Label L_copy_bytes, L_copy_shorts, L_copy_ints, L_copy_longs;
2430 
2431     // Input registers
2432     const Register src        = c_rarg0;  // source array oop
2433     const Register src_pos    = c_rarg1;  // source position
2434     const Register dst        = c_rarg2;  // destination array oop
2435     const Register dst_pos    = c_rarg3;  // destination position
2436 #ifndef _WIN64
2437     const Register length     = c_rarg4;
2438 #else
2439     const Address  length(rsp, 6 * wordSize);  // elements count is on stack on Win64
2440 #endif
2441 
2442     { int modulus = CodeEntryAlignment;
2443       int target  = modulus - 5; // 5 = sizeof jmp(L_failed)
2444       int advance = target - (__ offset() % modulus);
2445       if (advance < 0)  advance += modulus;
2446       if (advance > 0)  __ nop(advance);
2447     }
2448     StubCodeMark mark(this, "StubRoutines", name);
2449 
2450     // Short-hop target to L_failed.  Makes for denser prologue code.
2451     __ BIND(L_failed_0);
2452     __ jmp(L_failed);
2453     assert(__ offset() % CodeEntryAlignment == 0, "no further alignment needed");
2454 
2455     __ align(CodeEntryAlignment);
2456     address start = __ pc();
2457 
2458     __ enter(); // required for proper stackwalking of RuntimeStub frame
2459 
2460     // bump this on entry, not on exit:
2461     inc_counter_np(SharedRuntime::_generic_array_copy_ctr);
2462 
2463     //-----------------------------------------------------------------------
2464     // Assembler stub will be used for this call to arraycopy
2465     // if the following conditions are met:
2466     //
2467     // (1) src and dst must not be null.
2468     // (2) src_pos must not be negative.
2469     // (3) dst_pos must not be negative.
2470     // (4) length  must not be negative.
2471     // (5) src klass and dst klass should be the same and not NULL.
2472     // (6) src and dst should be arrays.
2473     // (7) src_pos + length must not exceed length of src.
2474     // (8) dst_pos + length must not exceed length of dst.
2475     //
2476 
2477     //  if (src == NULL) return -1;
2478     __ testptr(src, src);         // src oop
2479     size_t j1off = __ offset();
2480     __ jccb(Assembler::zero, L_failed_0);
2481 
2482     //  if (src_pos < 0) return -1;
2483     __ testl(src_pos, src_pos); // src_pos (32-bits)
2484     __ jccb(Assembler::negative, L_failed_0);
2485 
2486     //  if (dst == NULL) return -1;
2487     __ testptr(dst, dst);         // dst oop
2488     __ jccb(Assembler::zero, L_failed_0);
2489 
2490     //  if (dst_pos < 0) return -1;
2491     __ testl(dst_pos, dst_pos); // dst_pos (32-bits)
2492     size_t j4off = __ offset();
2493     __ jccb(Assembler::negative, L_failed_0);
2494 
2495     // The first four tests are very dense code,
2496     // but not quite dense enough to put four
2497     // jumps in a 16-byte instruction fetch buffer.
2498     // That's good, because some branch predicters
2499     // do not like jumps so close together.
2500     // Make sure of this.
2501     guarantee(((j1off ^ j4off) & ~15) != 0, "I$ line of 1st & 4th jumps");
2502 
2503     // registers used as temp
2504     const Register r11_length    = r11; // elements count to copy
2505     const Register r10_src_klass = r10; // array klass
2506 
2507     //  if (length < 0) return -1;
2508     __ movl(r11_length, length);        // length (elements count, 32-bits value)
2509     __ testl(r11_length, r11_length);
2510     __ jccb(Assembler::negative, L_failed_0);
2511 
2512     __ load_klass(r10_src_klass, src);
2513 #ifdef ASSERT
2514     //  assert(src->klass() != NULL);
2515     {
2516       BLOCK_COMMENT("assert klasses not null {");
2517       Label L1, L2;
2518       __ testptr(r10_src_klass, r10_src_klass);
2519       __ jcc(Assembler::notZero, L2);   // it is broken if klass is NULL
2520       __ bind(L1);
2521       __ stop("broken null klass");
2522       __ bind(L2);
2523       __ load_klass(rax, dst);
2524       __ cmpq(rax, 0);
2525       __ jcc(Assembler::equal, L1);     // this would be broken also
2526       BLOCK_COMMENT("} assert klasses not null done");
2527     }
2528 #endif
2529 
2530     // Load layout helper (32-bits)
2531     //
2532     //  |array_tag|     | header_size | element_type |     |log2_element_size|
2533     // 32        30    24            16              8     2                 0
2534     //
2535     //   array_tag: typeArray = 0x3, objArray = 0x2, non-array = 0x0
2536     //
2537 
2538     const int lh_offset = klassOopDesc::header_size() * HeapWordSize +
2539                           Klass::layout_helper_offset_in_bytes();
2540 
2541     // Handle objArrays completely differently...
2542     const jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
2543     __ cmpl(Address(r10_src_klass, lh_offset), objArray_lh);
2544     __ jcc(Assembler::equal, L_objArray);
2545 
2546     //  if (src->klass() != dst->klass()) return -1;
2547     __ load_klass(rax, dst);
2548     __ cmpq(r10_src_klass, rax);
2549     __ jcc(Assembler::notEqual, L_failed);
2550 
2551     const Register rax_lh = rax;  // layout helper
2552     __ movl(rax_lh, Address(r10_src_klass, lh_offset));
2553 
2554     //  if (!src->is_Array()) return -1;
2555     __ cmpl(rax_lh, Klass::_lh_neutral_value);
2556     __ jcc(Assembler::greaterEqual, L_failed);
2557 
2558     // At this point, it is known to be a typeArray (array_tag 0x3).
2559 #ifdef ASSERT
2560     {
2561       BLOCK_COMMENT("assert primitive array {");
2562       Label L;
2563       __ cmpl(rax_lh, (Klass::_lh_array_tag_type_value << Klass::_lh_array_tag_shift));
2564       __ jcc(Assembler::greaterEqual, L);
2565       __ stop("must be a primitive array");
2566       __ bind(L);
2567       BLOCK_COMMENT("} assert primitive array done");
2568     }
2569 #endif
2570 
2571     arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
2572                            r10, L_failed);
2573 
2574     // typeArrayKlass
2575     //
2576     // src_addr = (src + array_header_in_bytes()) + (src_pos << log2elemsize);
2577     // dst_addr = (dst + array_header_in_bytes()) + (dst_pos << log2elemsize);
2578     //
2579 
2580     const Register r10_offset = r10;    // array offset
2581     const Register rax_elsize = rax_lh; // element size
2582 
2583     __ movl(r10_offset, rax_lh);
2584     __ shrl(r10_offset, Klass::_lh_header_size_shift);
2585     __ andptr(r10_offset, Klass::_lh_header_size_mask);   // array_offset
2586     __ addptr(src, r10_offset);           // src array offset
2587     __ addptr(dst, r10_offset);           // dst array offset
2588     BLOCK_COMMENT("choose copy loop based on element size");
2589     __ andl(rax_lh, Klass::_lh_log2_element_size_mask); // rax_lh -> rax_elsize
2590 
2591     // next registers should be set before the jump to corresponding stub
2592     const Register from     = c_rarg0;  // source array address
2593     const Register to       = c_rarg1;  // destination array address
2594     const Register count    = c_rarg2;  // elements count
2595 
2596     // 'from', 'to', 'count' registers should be set in such order
2597     // since they are the same as 'src', 'src_pos', 'dst'.
2598 
2599   __ BIND(L_copy_bytes);
2600     __ cmpl(rax_elsize, 0);
2601     __ jccb(Assembler::notEqual, L_copy_shorts);
2602     __ lea(from, Address(src, src_pos, Address::times_1, 0));// src_addr
2603     __ lea(to,   Address(dst, dst_pos, Address::times_1, 0));// dst_addr
2604     __ movl2ptr(count, r11_length); // length
2605     __ jump(RuntimeAddress(byte_copy_entry));
2606 
2607   __ BIND(L_copy_shorts);
2608     __ cmpl(rax_elsize, LogBytesPerShort);
2609     __ jccb(Assembler::notEqual, L_copy_ints);
2610     __ lea(from, Address(src, src_pos, Address::times_2, 0));// src_addr
2611     __ lea(to,   Address(dst, dst_pos, Address::times_2, 0));// dst_addr
2612     __ movl2ptr(count, r11_length); // length
2613     __ jump(RuntimeAddress(short_copy_entry));
2614 
2615   __ BIND(L_copy_ints);
2616     __ cmpl(rax_elsize, LogBytesPerInt);
2617     __ jccb(Assembler::notEqual, L_copy_longs);
2618     __ lea(from, Address(src, src_pos, Address::times_4, 0));// src_addr
2619     __ lea(to,   Address(dst, dst_pos, Address::times_4, 0));// dst_addr
2620     __ movl2ptr(count, r11_length); // length
2621     __ jump(RuntimeAddress(int_copy_entry));
2622 
2623   __ BIND(L_copy_longs);
2624 #ifdef ASSERT
2625     {
2626       BLOCK_COMMENT("assert long copy {");
2627       Label L;
2628       __ cmpl(rax_elsize, LogBytesPerLong);
2629       __ jcc(Assembler::equal, L);
2630       __ stop("must be long copy, but elsize is wrong");
2631       __ bind(L);
2632       BLOCK_COMMENT("} assert long copy done");
2633     }
2634 #endif
2635     __ lea(from, Address(src, src_pos, Address::times_8, 0));// src_addr
2636     __ lea(to,   Address(dst, dst_pos, Address::times_8, 0));// dst_addr
2637     __ movl2ptr(count, r11_length); // length
2638     __ jump(RuntimeAddress(long_copy_entry));
2639 
2640     // objArrayKlass
2641   __ BIND(L_objArray);
2642     // live at this point:  r10_src_klass, r11_length, src[_pos], dst[_pos]
2643 
2644     Label L_plain_copy, L_checkcast_copy;
2645     //  test array classes for subtyping
2646     __ load_klass(rax, dst);
2647     __ cmpq(r10_src_klass, rax); // usual case is exact equality
2648     __ jcc(Assembler::notEqual, L_checkcast_copy);
2649 
2650     // Identically typed arrays can be copied without element-wise checks.
2651     arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
2652                            r10, L_failed);
2653 
2654     __ lea(from, Address(src, src_pos, TIMES_OOP,
2655                  arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // src_addr
2656     __ lea(to,   Address(dst, dst_pos, TIMES_OOP,
2657                  arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // dst_addr
2658     __ movl2ptr(count, r11_length); // length
2659   __ BIND(L_plain_copy);
2660     __ jump(RuntimeAddress(oop_copy_entry));
2661 
2662   __ BIND(L_checkcast_copy);
2663     // live at this point:  r10_src_klass, r11_length, rax (dst_klass)
2664     {
2665       // Before looking at dst.length, make sure dst is also an objArray.
2666       __ cmpl(Address(rax, lh_offset), objArray_lh);
2667       __ jcc(Assembler::notEqual, L_failed);
2668 
2669       // It is safe to examine both src.length and dst.length.
2670       arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
2671                              rax, L_failed);
2672 
2673       const Register r11_dst_klass = r11;
2674       __ load_klass(r11_dst_klass, dst); // reload
2675 
2676       // Marshal the base address arguments now, freeing registers.
2677       __ lea(from, Address(src, src_pos, TIMES_OOP,
2678                    arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
2679       __ lea(to,   Address(dst, dst_pos, TIMES_OOP,
2680                    arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
2681       __ movl(count, length);           // length (reloaded)
2682       Register sco_temp = c_rarg3;      // this register is free now
2683       assert_different_registers(from, to, count, sco_temp,
2684                                  r11_dst_klass, r10_src_klass);
2685       assert_clean_int(count, sco_temp);
2686 
2687       // Generate the type check.
2688       const int sco_offset = (klassOopDesc::header_size() * HeapWordSize +
2689                               Klass::super_check_offset_offset_in_bytes());
2690       __ movl(sco_temp, Address(r11_dst_klass, sco_offset));
2691       assert_clean_int(sco_temp, rax);
2692       generate_type_check(r10_src_klass, sco_temp, r11_dst_klass, L_plain_copy);
2693 
2694       // Fetch destination element klass from the objArrayKlass header.
2695       int ek_offset = (klassOopDesc::header_size() * HeapWordSize +
2696                        objArrayKlass::element_klass_offset_in_bytes());
2697       __ movptr(r11_dst_klass, Address(r11_dst_klass, ek_offset));
2698       __ movl(  sco_temp,      Address(r11_dst_klass, sco_offset));
2699       assert_clean_int(sco_temp, rax);
2700 
2701       // the checkcast_copy loop needs two extra arguments:
2702       assert(c_rarg3 == sco_temp, "#3 already in place");
2703       // Set up arguments for checkcast_copy_entry.
2704       setup_arg_regs(4);
2705       __ movptr(r8, r11_dst_klass);  // dst.klass.element_klass, r8 is c_rarg4 on Linux/Solaris
2706       __ jump(RuntimeAddress(checkcast_copy_entry));
2707     }
2708 
2709   __ BIND(L_failed);
2710     __ xorptr(rax, rax);
2711     __ notptr(rax); // return -1
2712     __ leave();   // required for proper stackwalking of RuntimeStub frame
2713     __ ret(0);
2714 
2715     return start;
2716   }
2717 
2718   void generate_arraycopy_stubs() {
2719     address entry;
2720     address entry_jbyte_arraycopy;
2721     address entry_jshort_arraycopy;
2722     address entry_jint_arraycopy;
2723     address entry_oop_arraycopy;
2724     address entry_jlong_arraycopy;
2725     address entry_checkcast_arraycopy;
2726 
2727     StubRoutines::_jbyte_disjoint_arraycopy  = generate_disjoint_byte_copy(false, &entry,
2728                                                                            "jbyte_disjoint_arraycopy");
2729     StubRoutines::_jbyte_arraycopy           = generate_conjoint_byte_copy(false, entry, &entry_jbyte_arraycopy,
2730                                                                            "jbyte_arraycopy");
2731 
2732     StubRoutines::_jshort_disjoint_arraycopy = generate_disjoint_short_copy(false, &entry,
2733                                                                             "jshort_disjoint_arraycopy");
2734     StubRoutines::_jshort_arraycopy          = generate_conjoint_short_copy(false, entry, &entry_jshort_arraycopy,
2735                                                                             "jshort_arraycopy");
2736 
2737     StubRoutines::_jint_disjoint_arraycopy   = generate_disjoint_int_oop_copy(false, false, &entry,
2738                                                                               "jint_disjoint_arraycopy");
2739     StubRoutines::_jint_arraycopy            = generate_conjoint_int_oop_copy(false, false, entry,
2740                                                                               &entry_jint_arraycopy, "jint_arraycopy");
2741 
2742     StubRoutines::_jlong_disjoint_arraycopy  = generate_disjoint_long_oop_copy(false, false, &entry,
2743                                                                                "jlong_disjoint_arraycopy");
2744     StubRoutines::_jlong_arraycopy           = generate_conjoint_long_oop_copy(false, false, entry,
2745                                                                                &entry_jlong_arraycopy, "jlong_arraycopy");
2746 
2747 
2748     if (UseCompressedOops) {
2749       StubRoutines::_oop_disjoint_arraycopy  = generate_disjoint_int_oop_copy(false, true, &entry,
2750                                                                               "oop_disjoint_arraycopy");
2751       StubRoutines::_oop_arraycopy           = generate_conjoint_int_oop_copy(false, true, entry,
2752                                                                               &entry_oop_arraycopy, "oop_arraycopy");
2753     } else {
2754       StubRoutines::_oop_disjoint_arraycopy  = generate_disjoint_long_oop_copy(false, true, &entry,
2755                                                                                "oop_disjoint_arraycopy");
2756       StubRoutines::_oop_arraycopy           = generate_conjoint_long_oop_copy(false, true, entry,
2757                                                                                &entry_oop_arraycopy, "oop_arraycopy");
2758     }
2759 
2760     StubRoutines::_checkcast_arraycopy = generate_checkcast_copy("checkcast_arraycopy", &entry_checkcast_arraycopy);
2761     StubRoutines::_unsafe_arraycopy    = generate_unsafe_copy("unsafe_arraycopy",
2762                                                               entry_jbyte_arraycopy,
2763                                                               entry_jshort_arraycopy,
2764                                                               entry_jint_arraycopy,
2765                                                               entry_jlong_arraycopy);
2766     StubRoutines::_generic_arraycopy   = generate_generic_copy("generic_arraycopy",
2767                                                                entry_jbyte_arraycopy,
2768                                                                entry_jshort_arraycopy,
2769                                                                entry_jint_arraycopy,
2770                                                                entry_oop_arraycopy,
2771                                                                entry_jlong_arraycopy,
2772                                                                entry_checkcast_arraycopy);
2773 
2774     StubRoutines::_jbyte_fill = generate_fill(T_BYTE, false, "jbyte_fill");
2775     StubRoutines::_jshort_fill = generate_fill(T_SHORT, false, "jshort_fill");
2776     StubRoutines::_jint_fill = generate_fill(T_INT, false, "jint_fill");
2777     StubRoutines::_arrayof_jbyte_fill = generate_fill(T_BYTE, true, "arrayof_jbyte_fill");
2778     StubRoutines::_arrayof_jshort_fill = generate_fill(T_SHORT, true, "arrayof_jshort_fill");
2779     StubRoutines::_arrayof_jint_fill = generate_fill(T_INT, true, "arrayof_jint_fill");
2780 
2781     // We don't generate specialized code for HeapWord-aligned source
2782     // arrays, so just use the code we've already generated
2783     StubRoutines::_arrayof_jbyte_disjoint_arraycopy  = StubRoutines::_jbyte_disjoint_arraycopy;
2784     StubRoutines::_arrayof_jbyte_arraycopy           = StubRoutines::_jbyte_arraycopy;
2785 
2786     StubRoutines::_arrayof_jshort_disjoint_arraycopy = StubRoutines::_jshort_disjoint_arraycopy;
2787     StubRoutines::_arrayof_jshort_arraycopy          = StubRoutines::_jshort_arraycopy;
2788 
2789     StubRoutines::_arrayof_jint_disjoint_arraycopy   = StubRoutines::_jint_disjoint_arraycopy;
2790     StubRoutines::_arrayof_jint_arraycopy            = StubRoutines::_jint_arraycopy;
2791 
2792     StubRoutines::_arrayof_jlong_disjoint_arraycopy  = StubRoutines::_jlong_disjoint_arraycopy;
2793     StubRoutines::_arrayof_jlong_arraycopy           = StubRoutines::_jlong_arraycopy;
2794 
2795     StubRoutines::_arrayof_oop_disjoint_arraycopy    = StubRoutines::_oop_disjoint_arraycopy;
2796     StubRoutines::_arrayof_oop_arraycopy             = StubRoutines::_oop_arraycopy;
2797   }
2798 
2799   void generate_math_stubs() {
2800     {
2801       StubCodeMark mark(this, "StubRoutines", "log");
2802       StubRoutines::_intrinsic_log = (double (*)(double)) __ pc();
2803 
2804       __ subq(rsp, 8);
2805       __ movdbl(Address(rsp, 0), xmm0);
2806       __ fld_d(Address(rsp, 0));
2807       __ flog();
2808       __ fstp_d(Address(rsp, 0));
2809       __ movdbl(xmm0, Address(rsp, 0));
2810       __ addq(rsp, 8);
2811       __ ret(0);
2812     }
2813     {
2814       StubCodeMark mark(this, "StubRoutines", "log10");
2815       StubRoutines::_intrinsic_log10 = (double (*)(double)) __ pc();
2816 
2817       __ subq(rsp, 8);
2818       __ movdbl(Address(rsp, 0), xmm0);
2819       __ fld_d(Address(rsp, 0));
2820       __ flog10();
2821       __ fstp_d(Address(rsp, 0));
2822       __ movdbl(xmm0, Address(rsp, 0));
2823       __ addq(rsp, 8);
2824       __ ret(0);
2825     }
2826     {
2827       StubCodeMark mark(this, "StubRoutines", "sin");
2828       StubRoutines::_intrinsic_sin = (double (*)(double)) __ pc();
2829 
2830       __ subq(rsp, 8);
2831       __ movdbl(Address(rsp, 0), xmm0);
2832       __ fld_d(Address(rsp, 0));
2833       __ trigfunc('s');
2834       __ fstp_d(Address(rsp, 0));
2835       __ movdbl(xmm0, Address(rsp, 0));
2836       __ addq(rsp, 8);
2837       __ ret(0);
2838     }
2839     {
2840       StubCodeMark mark(this, "StubRoutines", "cos");
2841       StubRoutines::_intrinsic_cos = (double (*)(double)) __ pc();
2842 
2843       __ subq(rsp, 8);
2844       __ movdbl(Address(rsp, 0), xmm0);
2845       __ fld_d(Address(rsp, 0));
2846       __ trigfunc('c');
2847       __ fstp_d(Address(rsp, 0));
2848       __ movdbl(xmm0, Address(rsp, 0));
2849       __ addq(rsp, 8);
2850       __ ret(0);
2851     }
2852     {
2853       StubCodeMark mark(this, "StubRoutines", "tan");
2854       StubRoutines::_intrinsic_tan = (double (*)(double)) __ pc();
2855 
2856       __ subq(rsp, 8);
2857       __ movdbl(Address(rsp, 0), xmm0);
2858       __ fld_d(Address(rsp, 0));
2859       __ trigfunc('t');
2860       __ fstp_d(Address(rsp, 0));
2861       __ movdbl(xmm0, Address(rsp, 0));
2862       __ addq(rsp, 8);
2863       __ ret(0);
2864     }
2865 
2866     // The intrinsic version of these seem to return the same value as
2867     // the strict version.
2868     StubRoutines::_intrinsic_exp = SharedRuntime::dexp;
2869     StubRoutines::_intrinsic_pow = SharedRuntime::dpow;
2870   }
2871 
2872 #undef __
2873 #define __ masm->
2874 
2875   // Continuation point for throwing of implicit exceptions that are
2876   // not handled in the current activation. Fabricates an exception
2877   // oop and initiates normal exception dispatching in this
2878   // frame. Since we need to preserve callee-saved values (currently
2879   // only for C2, but done for C1 as well) we need a callee-saved oop
2880   // map and therefore have to make these stubs into RuntimeStubs
2881   // rather than BufferBlobs.  If the compiler needs all registers to
2882   // be preserved between the fault point and the exception handler
2883   // then it must assume responsibility for that in
2884   // AbstractCompiler::continuation_for_implicit_null_exception or
2885   // continuation_for_implicit_division_by_zero_exception. All other
2886   // implicit exceptions (e.g., NullPointerException or
2887   // AbstractMethodError on entry) are either at call sites or
2888   // otherwise assume that stack unwinding will be initiated, so
2889   // caller saved registers were assumed volatile in the compiler.
2890   address generate_throw_exception(const char* name,
2891                                    address runtime_entry,
2892                                    bool restore_saved_exception_pc) {
2893     // Information about frame layout at time of blocking runtime call.
2894     // Note that we only have to preserve callee-saved registers since
2895     // the compilers are responsible for supplying a continuation point
2896     // if they expect all registers to be preserved.
2897     enum layout {
2898       rbp_off = frame::arg_reg_save_area_bytes/BytesPerInt,
2899       rbp_off2,
2900       return_off,
2901       return_off2,
2902       framesize // inclusive of return address
2903     };
2904 
2905     int insts_size = 512;
2906     int locs_size  = 64;
2907 
2908     CodeBuffer code(name, insts_size, locs_size);
2909     OopMapSet* oop_maps  = new OopMapSet();
2910     MacroAssembler* masm = new MacroAssembler(&code);
2911 
2912     address start = __ pc();
2913 
2914     // This is an inlined and slightly modified version of call_VM
2915     // which has the ability to fetch the return PC out of
2916     // thread-local storage and also sets up last_Java_sp slightly
2917     // differently than the real call_VM
2918     if (restore_saved_exception_pc) {
2919       __ movptr(rax,
2920                 Address(r15_thread,
2921                         in_bytes(JavaThread::saved_exception_pc_offset())));
2922       __ push(rax);
2923     }
2924 
2925     __ enter(); // required for proper stackwalking of RuntimeStub frame
2926 
2927     assert(is_even(framesize/2), "sp not 16-byte aligned");
2928 
2929     // return address and rbp are already in place
2930     __ subptr(rsp, (framesize-4) << LogBytesPerInt); // prolog
2931 
2932     int frame_complete = __ pc() - start;
2933 
2934     // Set up last_Java_sp and last_Java_fp
2935     __ set_last_Java_frame(rsp, rbp, NULL);
2936 
2937     // Call runtime
2938     __ movptr(c_rarg0, r15_thread);
2939     BLOCK_COMMENT("call runtime_entry");
2940     __ call(RuntimeAddress(runtime_entry));
2941 
2942     // Generate oop map
2943     OopMap* map = new OopMap(framesize, 0);
2944 
2945     oop_maps->add_gc_map(__ pc() - start, map);
2946 
2947     __ reset_last_Java_frame(true, false);
2948 
2949     __ leave(); // required for proper stackwalking of RuntimeStub frame
2950 
2951     // check for pending exceptions
2952 #ifdef ASSERT
2953     Label L;
2954     __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()),
2955             (int32_t) NULL_WORD);
2956     __ jcc(Assembler::notEqual, L);
2957     __ should_not_reach_here();
2958     __ bind(L);
2959 #endif // ASSERT
2960     __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
2961 
2962 
2963     // codeBlob framesize is in words (not VMRegImpl::slot_size)
2964     RuntimeStub* stub =
2965       RuntimeStub::new_runtime_stub(name,
2966                                     &code,
2967                                     frame_complete,
2968                                     (framesize >> (LogBytesPerWord - LogBytesPerInt)),
2969                                     oop_maps, false);
2970     return stub->entry_point();
2971   }
2972 
2973   // Initialization
2974   void generate_initial() {
2975     // Generates all stubs and initializes the entry points
2976 
2977     // This platform-specific stub is needed by generate_call_stub()
2978     StubRoutines::x86::_mxcsr_std        = generate_fp_mask("mxcsr_std",        0x0000000000001F80);
2979 
2980     // entry points that exist in all platforms Note: This is code
2981     // that could be shared among different platforms - however the
2982     // benefit seems to be smaller than the disadvantage of having a
2983     // much more complicated generator structure. See also comment in
2984     // stubRoutines.hpp.
2985 
2986     StubRoutines::_forward_exception_entry = generate_forward_exception();
2987 
2988     StubRoutines::_call_stub_entry =
2989       generate_call_stub(StubRoutines::_call_stub_return_address);
2990 
2991     // is referenced by megamorphic call
2992     StubRoutines::_catch_exception_entry = generate_catch_exception();
2993 
2994     // atomic calls
2995     StubRoutines::_atomic_xchg_entry         = generate_atomic_xchg();
2996     StubRoutines::_atomic_xchg_ptr_entry     = generate_atomic_xchg_ptr();
2997     StubRoutines::_atomic_cmpxchg_entry      = generate_atomic_cmpxchg();
2998     StubRoutines::_atomic_cmpxchg_long_entry = generate_atomic_cmpxchg_long();
2999     StubRoutines::_atomic_add_entry          = generate_atomic_add();
3000     StubRoutines::_atomic_add_ptr_entry      = generate_atomic_add_ptr();
3001     StubRoutines::_fence_entry               = generate_orderaccess_fence();
3002 
3003     StubRoutines::_handler_for_unsafe_access_entry =
3004       generate_handler_for_unsafe_access();
3005 
3006     // platform dependent
3007     StubRoutines::x86::_get_previous_fp_entry = generate_get_previous_fp();
3008 
3009     StubRoutines::x86::_verify_mxcsr_entry    = generate_verify_mxcsr();
3010   }
3011 
3012   void generate_all() {
3013     // Generates all stubs and initializes the entry points
3014 
3015     // These entry points require SharedInfo::stack0 to be set up in
3016     // non-core builds and need to be relocatable, so they each
3017     // fabricate a RuntimeStub internally.
3018     StubRoutines::_throw_AbstractMethodError_entry =
3019       generate_throw_exception("AbstractMethodError throw_exception",
3020                                CAST_FROM_FN_PTR(address,
3021                                                 SharedRuntime::
3022                                                 throw_AbstractMethodError),
3023                                false);
3024 
3025     StubRoutines::_throw_IncompatibleClassChangeError_entry =
3026       generate_throw_exception("IncompatibleClassChangeError throw_exception",
3027                                CAST_FROM_FN_PTR(address,
3028                                                 SharedRuntime::
3029                                                 throw_IncompatibleClassChangeError),
3030                                false);
3031 
3032     StubRoutines::_throw_ArithmeticException_entry =
3033       generate_throw_exception("ArithmeticException throw_exception",
3034                                CAST_FROM_FN_PTR(address,
3035                                                 SharedRuntime::
3036                                                 throw_ArithmeticException),
3037                                true);
3038 
3039     StubRoutines::_throw_NullPointerException_entry =
3040       generate_throw_exception("NullPointerException throw_exception",
3041                                CAST_FROM_FN_PTR(address,
3042                                                 SharedRuntime::
3043                                                 throw_NullPointerException),
3044                                true);
3045 
3046     StubRoutines::_throw_NullPointerException_at_call_entry =
3047       generate_throw_exception("NullPointerException at call throw_exception",
3048                                CAST_FROM_FN_PTR(address,
3049                                                 SharedRuntime::
3050                                                 throw_NullPointerException_at_call),
3051                                false);
3052 
3053     StubRoutines::_throw_StackOverflowError_entry =
3054       generate_throw_exception("StackOverflowError throw_exception",
3055                                CAST_FROM_FN_PTR(address,
3056                                                 SharedRuntime::
3057                                                 throw_StackOverflowError),
3058                                false);
3059 
3060     // entry points that are platform specific
3061     StubRoutines::x86::_f2i_fixup = generate_f2i_fixup();
3062     StubRoutines::x86::_f2l_fixup = generate_f2l_fixup();
3063     StubRoutines::x86::_d2i_fixup = generate_d2i_fixup();
3064     StubRoutines::x86::_d2l_fixup = generate_d2l_fixup();
3065 
3066     StubRoutines::x86::_float_sign_mask  = generate_fp_mask("float_sign_mask",  0x7FFFFFFF7FFFFFFF);
3067     StubRoutines::x86::_float_sign_flip  = generate_fp_mask("float_sign_flip",  0x8000000080000000);
3068     StubRoutines::x86::_double_sign_mask = generate_fp_mask("double_sign_mask", 0x7FFFFFFFFFFFFFFF);
3069     StubRoutines::x86::_double_sign_flip = generate_fp_mask("double_sign_flip", 0x8000000000000000);
3070 
3071     // support for verify_oop (must happen after universe_init)
3072     StubRoutines::_verify_oop_subroutine_entry = generate_verify_oop();
3073 
3074     // arraycopy stubs used by compilers
3075     generate_arraycopy_stubs();
3076 
3077     generate_math_stubs();
3078   }
3079 
3080  public:
3081   StubGenerator(CodeBuffer* code, bool all) : StubCodeGenerator(code) {
3082     if (all) {
3083       generate_all();
3084     } else {
3085       generate_initial();
3086     }
3087   }
3088 }; // end class declaration
3089 
3090 void StubGenerator_generate(CodeBuffer* code, bool all) {
3091   StubGenerator g(code, all);
3092 }