src/cpu/x86/vm/stubGenerator_x86_64.cpp

Wed, 19 Dec 2012 15:40:35 -0800

author
kvn
date
Wed, 19 Dec 2012 15:40:35 -0800
changeset 4363
2c7f594145dc
parent 4325
d2f8c38e543d
child 4411
e2e6bf86682c
permissions
-rw-r--r--

8004835: Improve AES intrinsics on x86
Summary: Enable AES intrinsics on non-AVX cpus, group together aes instructions in crypto stubs.
Reviewed-by: roland, twisti

     1 /*
     2  * Copyright (c) 2003, 2012, 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  */
    25 #include "precompiled.hpp"
    26 #include "asm/macroAssembler.hpp"
    27 #include "asm/macroAssembler.inline.hpp"
    28 #include "interpreter/interpreter.hpp"
    29 #include "nativeInst_x86.hpp"
    30 #include "oops/instanceOop.hpp"
    31 #include "oops/method.hpp"
    32 #include "oops/objArrayKlass.hpp"
    33 #include "oops/oop.inline.hpp"
    34 #include "prims/methodHandles.hpp"
    35 #include "runtime/frame.inline.hpp"
    36 #include "runtime/handles.inline.hpp"
    37 #include "runtime/sharedRuntime.hpp"
    38 #include "runtime/stubCodeGenerator.hpp"
    39 #include "runtime/stubRoutines.hpp"
    40 #include "runtime/thread.inline.hpp"
    41 #include "utilities/top.hpp"
    42 #ifdef COMPILER2
    43 #include "opto/runtime.hpp"
    44 #endif
    46 // Declaration and definition of StubGenerator (no .hpp file).
    47 // For a more detailed description of the stub routine structure
    48 // see the comment in stubRoutines.hpp
    50 #define __ _masm->
    51 #define TIMES_OOP (UseCompressedOops ? Address::times_4 : Address::times_8)
    52 #define a__ ((Assembler*)_masm)->
    54 #ifdef PRODUCT
    55 #define BLOCK_COMMENT(str) /* nothing */
    56 #else
    57 #define BLOCK_COMMENT(str) __ block_comment(str)
    58 #endif
    60 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
    61 const int MXCSR_MASK = 0xFFC0;  // Mask out any pending exceptions
    63 // Stub Code definitions
    65 static address handle_unsafe_access() {
    66   JavaThread* thread = JavaThread::current();
    67   address pc = thread->saved_exception_pc();
    68   // pc is the instruction which we must emulate
    69   // doing a no-op is fine:  return garbage from the load
    70   // therefore, compute npc
    71   address npc = Assembler::locate_next_instruction(pc);
    73   // request an async exception
    74   thread->set_pending_unsafe_access_error();
    76   // return address of next instruction to execute
    77   return npc;
    78 }
    80 class StubGenerator: public StubCodeGenerator {
    81  private:
    83 #ifdef PRODUCT
    84 #define inc_counter_np(counter) (0)
    85 #else
    86   void inc_counter_np_(int& counter) {
    87     // This can destroy rscratch1 if counter is far from the code cache
    88     __ incrementl(ExternalAddress((address)&counter));
    89   }
    90 #define inc_counter_np(counter) \
    91   BLOCK_COMMENT("inc_counter " #counter); \
    92   inc_counter_np_(counter);
    93 #endif
    95   // Call stubs are used to call Java from C
    96   //
    97   // Linux Arguments:
    98   //    c_rarg0:   call wrapper address                   address
    99   //    c_rarg1:   result                                 address
   100   //    c_rarg2:   result type                            BasicType
   101   //    c_rarg3:   method                                 Method*
   102   //    c_rarg4:   (interpreter) entry point              address
   103   //    c_rarg5:   parameters                             intptr_t*
   104   //    16(rbp): parameter size (in words)              int
   105   //    24(rbp): thread                                 Thread*
   106   //
   107   //     [ return_from_Java     ] <--- rsp
   108   //     [ argument word n      ]
   109   //      ...
   110   // -12 [ argument word 1      ]
   111   // -11 [ saved r15            ] <--- rsp_after_call
   112   // -10 [ saved r14            ]
   113   //  -9 [ saved r13            ]
   114   //  -8 [ saved r12            ]
   115   //  -7 [ saved rbx            ]
   116   //  -6 [ call wrapper         ]
   117   //  -5 [ result               ]
   118   //  -4 [ result type          ]
   119   //  -3 [ method               ]
   120   //  -2 [ entry point          ]
   121   //  -1 [ parameters           ]
   122   //   0 [ saved rbp            ] <--- rbp
   123   //   1 [ return address       ]
   124   //   2 [ parameter size       ]
   125   //   3 [ thread               ]
   126   //
   127   // Windows Arguments:
   128   //    c_rarg0:   call wrapper address                   address
   129   //    c_rarg1:   result                                 address
   130   //    c_rarg2:   result type                            BasicType
   131   //    c_rarg3:   method                                 Method*
   132   //    48(rbp): (interpreter) entry point              address
   133   //    56(rbp): parameters                             intptr_t*
   134   //    64(rbp): parameter size (in words)              int
   135   //    72(rbp): thread                                 Thread*
   136   //
   137   //     [ return_from_Java     ] <--- rsp
   138   //     [ argument word n      ]
   139   //      ...
   140   // -28 [ argument word 1      ]
   141   // -27 [ saved xmm15          ] <--- rsp_after_call
   142   //     [ saved xmm7-xmm14     ]
   143   //  -9 [ saved xmm6           ] (each xmm register takes 2 slots)
   144   //  -7 [ saved r15            ]
   145   //  -6 [ saved r14            ]
   146   //  -5 [ saved r13            ]
   147   //  -4 [ saved r12            ]
   148   //  -3 [ saved rdi            ]
   149   //  -2 [ saved rsi            ]
   150   //  -1 [ saved rbx            ]
   151   //   0 [ saved rbp            ] <--- rbp
   152   //   1 [ return address       ]
   153   //   2 [ call wrapper         ]
   154   //   3 [ result               ]
   155   //   4 [ result type          ]
   156   //   5 [ method               ]
   157   //   6 [ entry point          ]
   158   //   7 [ parameters           ]
   159   //   8 [ parameter size       ]
   160   //   9 [ thread               ]
   161   //
   162   //    Windows reserves the callers stack space for arguments 1-4.
   163   //    We spill c_rarg0-c_rarg3 to this space.
   165   // Call stub stack layout word offsets from rbp
   166   enum call_stub_layout {
   167 #ifdef _WIN64
   168     xmm_save_first     = 6,  // save from xmm6
   169     xmm_save_last      = 15, // to xmm15
   170     xmm_save_base      = -9,
   171     rsp_after_call_off = xmm_save_base - 2 * (xmm_save_last - xmm_save_first), // -27
   172     r15_off            = -7,
   173     r14_off            = -6,
   174     r13_off            = -5,
   175     r12_off            = -4,
   176     rdi_off            = -3,
   177     rsi_off            = -2,
   178     rbx_off            = -1,
   179     rbp_off            =  0,
   180     retaddr_off        =  1,
   181     call_wrapper_off   =  2,
   182     result_off         =  3,
   183     result_type_off    =  4,
   184     method_off         =  5,
   185     entry_point_off    =  6,
   186     parameters_off     =  7,
   187     parameter_size_off =  8,
   188     thread_off         =  9
   189 #else
   190     rsp_after_call_off = -12,
   191     mxcsr_off          = rsp_after_call_off,
   192     r15_off            = -11,
   193     r14_off            = -10,
   194     r13_off            = -9,
   195     r12_off            = -8,
   196     rbx_off            = -7,
   197     call_wrapper_off   = -6,
   198     result_off         = -5,
   199     result_type_off    = -4,
   200     method_off         = -3,
   201     entry_point_off    = -2,
   202     parameters_off     = -1,
   203     rbp_off            =  0,
   204     retaddr_off        =  1,
   205     parameter_size_off =  2,
   206     thread_off         =  3
   207 #endif
   208   };
   210 #ifdef _WIN64
   211   Address xmm_save(int reg) {
   212     assert(reg >= xmm_save_first && reg <= xmm_save_last, "XMM register number out of range");
   213     return Address(rbp, (xmm_save_base - (reg - xmm_save_first) * 2) * wordSize);
   214   }
   215 #endif
   217   address generate_call_stub(address& return_address) {
   218     assert((int)frame::entry_frame_after_call_words == -(int)rsp_after_call_off + 1 &&
   219            (int)frame::entry_frame_call_wrapper_offset == (int)call_wrapper_off,
   220            "adjust this code");
   221     StubCodeMark mark(this, "StubRoutines", "call_stub");
   222     address start = __ pc();
   224     // same as in generate_catch_exception()!
   225     const Address rsp_after_call(rbp, rsp_after_call_off * wordSize);
   227     const Address call_wrapper  (rbp, call_wrapper_off   * wordSize);
   228     const Address result        (rbp, result_off         * wordSize);
   229     const Address result_type   (rbp, result_type_off    * wordSize);
   230     const Address method        (rbp, method_off         * wordSize);
   231     const Address entry_point   (rbp, entry_point_off    * wordSize);
   232     const Address parameters    (rbp, parameters_off     * wordSize);
   233     const Address parameter_size(rbp, parameter_size_off * wordSize);
   235     // same as in generate_catch_exception()!
   236     const Address thread        (rbp, thread_off         * wordSize);
   238     const Address r15_save(rbp, r15_off * wordSize);
   239     const Address r14_save(rbp, r14_off * wordSize);
   240     const Address r13_save(rbp, r13_off * wordSize);
   241     const Address r12_save(rbp, r12_off * wordSize);
   242     const Address rbx_save(rbp, rbx_off * wordSize);
   244     // stub code
   245     __ enter();
   246     __ subptr(rsp, -rsp_after_call_off * wordSize);
   248     // save register parameters
   249 #ifndef _WIN64
   250     __ movptr(parameters,   c_rarg5); // parameters
   251     __ movptr(entry_point,  c_rarg4); // entry_point
   252 #endif
   254     __ movptr(method,       c_rarg3); // method
   255     __ movl(result_type,  c_rarg2);   // result type
   256     __ movptr(result,       c_rarg1); // result
   257     __ movptr(call_wrapper, c_rarg0); // call wrapper
   259     // save regs belonging to calling function
   260     __ movptr(rbx_save, rbx);
   261     __ movptr(r12_save, r12);
   262     __ movptr(r13_save, r13);
   263     __ movptr(r14_save, r14);
   264     __ movptr(r15_save, r15);
   265 #ifdef _WIN64
   266     for (int i = 6; i <= 15; i++) {
   267       __ movdqu(xmm_save(i), as_XMMRegister(i));
   268     }
   270     const Address rdi_save(rbp, rdi_off * wordSize);
   271     const Address rsi_save(rbp, rsi_off * wordSize);
   273     __ movptr(rsi_save, rsi);
   274     __ movptr(rdi_save, rdi);
   275 #else
   276     const Address mxcsr_save(rbp, mxcsr_off * wordSize);
   277     {
   278       Label skip_ldmx;
   279       __ stmxcsr(mxcsr_save);
   280       __ movl(rax, mxcsr_save);
   281       __ andl(rax, MXCSR_MASK);    // Only check control and mask bits
   282       ExternalAddress mxcsr_std(StubRoutines::x86::mxcsr_std());
   283       __ cmp32(rax, mxcsr_std);
   284       __ jcc(Assembler::equal, skip_ldmx);
   285       __ ldmxcsr(mxcsr_std);
   286       __ bind(skip_ldmx);
   287     }
   288 #endif
   290     // Load up thread register
   291     __ movptr(r15_thread, thread);
   292     __ reinit_heapbase();
   294 #ifdef ASSERT
   295     // make sure we have no pending exceptions
   296     {
   297       Label L;
   298       __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
   299       __ jcc(Assembler::equal, L);
   300       __ stop("StubRoutines::call_stub: entered with pending exception");
   301       __ bind(L);
   302     }
   303 #endif
   305     // pass parameters if any
   306     BLOCK_COMMENT("pass parameters if any");
   307     Label parameters_done;
   308     __ movl(c_rarg3, parameter_size);
   309     __ testl(c_rarg3, c_rarg3);
   310     __ jcc(Assembler::zero, parameters_done);
   312     Label loop;
   313     __ movptr(c_rarg2, parameters);       // parameter pointer
   314     __ movl(c_rarg1, c_rarg3);            // parameter counter is in c_rarg1
   315     __ BIND(loop);
   316     __ movptr(rax, Address(c_rarg2, 0));// get parameter
   317     __ addptr(c_rarg2, wordSize);       // advance to next parameter
   318     __ decrementl(c_rarg1);             // decrement counter
   319     __ push(rax);                       // pass parameter
   320     __ jcc(Assembler::notZero, loop);
   322     // call Java function
   323     __ BIND(parameters_done);
   324     __ movptr(rbx, method);             // get Method*
   325     __ movptr(c_rarg1, entry_point);    // get entry_point
   326     __ mov(r13, rsp);                   // set sender sp
   327     BLOCK_COMMENT("call Java function");
   328     __ call(c_rarg1);
   330     BLOCK_COMMENT("call_stub_return_address:");
   331     return_address = __ pc();
   333     // store result depending on type (everything that is not
   334     // T_OBJECT, T_LONG, T_FLOAT or T_DOUBLE is treated as T_INT)
   335     __ movptr(c_rarg0, result);
   336     Label is_long, is_float, is_double, exit;
   337     __ movl(c_rarg1, result_type);
   338     __ cmpl(c_rarg1, T_OBJECT);
   339     __ jcc(Assembler::equal, is_long);
   340     __ cmpl(c_rarg1, T_LONG);
   341     __ jcc(Assembler::equal, is_long);
   342     __ cmpl(c_rarg1, T_FLOAT);
   343     __ jcc(Assembler::equal, is_float);
   344     __ cmpl(c_rarg1, T_DOUBLE);
   345     __ jcc(Assembler::equal, is_double);
   347     // handle T_INT case
   348     __ movl(Address(c_rarg0, 0), rax);
   350     __ BIND(exit);
   352     // pop parameters
   353     __ lea(rsp, rsp_after_call);
   355 #ifdef ASSERT
   356     // verify that threads correspond
   357     {
   358       Label L, S;
   359       __ cmpptr(r15_thread, thread);
   360       __ jcc(Assembler::notEqual, S);
   361       __ get_thread(rbx);
   362       __ cmpptr(r15_thread, rbx);
   363       __ jcc(Assembler::equal, L);
   364       __ bind(S);
   365       __ jcc(Assembler::equal, L);
   366       __ stop("StubRoutines::call_stub: threads must correspond");
   367       __ bind(L);
   368     }
   369 #endif
   371     // restore regs belonging to calling function
   372 #ifdef _WIN64
   373     for (int i = 15; i >= 6; i--) {
   374       __ movdqu(as_XMMRegister(i), xmm_save(i));
   375     }
   376 #endif
   377     __ movptr(r15, r15_save);
   378     __ movptr(r14, r14_save);
   379     __ movptr(r13, r13_save);
   380     __ movptr(r12, r12_save);
   381     __ movptr(rbx, rbx_save);
   383 #ifdef _WIN64
   384     __ movptr(rdi, rdi_save);
   385     __ movptr(rsi, rsi_save);
   386 #else
   387     __ ldmxcsr(mxcsr_save);
   388 #endif
   390     // restore rsp
   391     __ addptr(rsp, -rsp_after_call_off * wordSize);
   393     // return
   394     __ pop(rbp);
   395     __ ret(0);
   397     // handle return types different from T_INT
   398     __ BIND(is_long);
   399     __ movq(Address(c_rarg0, 0), rax);
   400     __ jmp(exit);
   402     __ BIND(is_float);
   403     __ movflt(Address(c_rarg0, 0), xmm0);
   404     __ jmp(exit);
   406     __ BIND(is_double);
   407     __ movdbl(Address(c_rarg0, 0), xmm0);
   408     __ jmp(exit);
   410     return start;
   411   }
   413   // Return point for a Java call if there's an exception thrown in
   414   // Java code.  The exception is caught and transformed into a
   415   // pending exception stored in JavaThread that can be tested from
   416   // within the VM.
   417   //
   418   // Note: Usually the parameters are removed by the callee. In case
   419   // of an exception crossing an activation frame boundary, that is
   420   // not the case if the callee is compiled code => need to setup the
   421   // rsp.
   422   //
   423   // rax: exception oop
   425   address generate_catch_exception() {
   426     StubCodeMark mark(this, "StubRoutines", "catch_exception");
   427     address start = __ pc();
   429     // same as in generate_call_stub():
   430     const Address rsp_after_call(rbp, rsp_after_call_off * wordSize);
   431     const Address thread        (rbp, thread_off         * wordSize);
   433 #ifdef ASSERT
   434     // verify that threads correspond
   435     {
   436       Label L, S;
   437       __ cmpptr(r15_thread, thread);
   438       __ jcc(Assembler::notEqual, S);
   439       __ get_thread(rbx);
   440       __ cmpptr(r15_thread, rbx);
   441       __ jcc(Assembler::equal, L);
   442       __ bind(S);
   443       __ stop("StubRoutines::catch_exception: threads must correspond");
   444       __ bind(L);
   445     }
   446 #endif
   448     // set pending exception
   449     __ verify_oop(rax);
   451     __ movptr(Address(r15_thread, Thread::pending_exception_offset()), rax);
   452     __ lea(rscratch1, ExternalAddress((address)__FILE__));
   453     __ movptr(Address(r15_thread, Thread::exception_file_offset()), rscratch1);
   454     __ movl(Address(r15_thread, Thread::exception_line_offset()), (int)  __LINE__);
   456     // complete return to VM
   457     assert(StubRoutines::_call_stub_return_address != NULL,
   458            "_call_stub_return_address must have been generated before");
   459     __ jump(RuntimeAddress(StubRoutines::_call_stub_return_address));
   461     return start;
   462   }
   464   // Continuation point for runtime calls returning with a pending
   465   // exception.  The pending exception check happened in the runtime
   466   // or native call stub.  The pending exception in Thread is
   467   // converted into a Java-level exception.
   468   //
   469   // Contract with Java-level exception handlers:
   470   // rax: exception
   471   // rdx: throwing pc
   472   //
   473   // NOTE: At entry of this stub, exception-pc must be on stack !!
   475   address generate_forward_exception() {
   476     StubCodeMark mark(this, "StubRoutines", "forward exception");
   477     address start = __ pc();
   479     // Upon entry, the sp points to the return address returning into
   480     // Java (interpreted or compiled) code; i.e., the return address
   481     // becomes the throwing pc.
   482     //
   483     // Arguments pushed before the runtime call are still on the stack
   484     // but the exception handler will reset the stack pointer ->
   485     // ignore them.  A potential result in registers can be ignored as
   486     // well.
   488 #ifdef ASSERT
   489     // make sure this code is only executed if there is a pending exception
   490     {
   491       Label L;
   492       __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t) NULL);
   493       __ jcc(Assembler::notEqual, L);
   494       __ stop("StubRoutines::forward exception: no pending exception (1)");
   495       __ bind(L);
   496     }
   497 #endif
   499     // compute exception handler into rbx
   500     __ movptr(c_rarg0, Address(rsp, 0));
   501     BLOCK_COMMENT("call exception_handler_for_return_address");
   502     __ call_VM_leaf(CAST_FROM_FN_PTR(address,
   503                          SharedRuntime::exception_handler_for_return_address),
   504                     r15_thread, c_rarg0);
   505     __ mov(rbx, rax);
   507     // setup rax & rdx, remove return address & clear pending exception
   508     __ pop(rdx);
   509     __ movptr(rax, Address(r15_thread, Thread::pending_exception_offset()));
   510     __ movptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
   512 #ifdef ASSERT
   513     // make sure exception is set
   514     {
   515       Label L;
   516       __ testptr(rax, rax);
   517       __ jcc(Assembler::notEqual, L);
   518       __ stop("StubRoutines::forward exception: no pending exception (2)");
   519       __ bind(L);
   520     }
   521 #endif
   523     // continue at exception handler (return address removed)
   524     // rax: exception
   525     // rbx: exception handler
   526     // rdx: throwing pc
   527     __ verify_oop(rax);
   528     __ jmp(rbx);
   530     return start;
   531   }
   533   // Support for jint atomic::xchg(jint exchange_value, volatile jint* dest)
   534   //
   535   // Arguments :
   536   //    c_rarg0: exchange_value
   537   //    c_rarg0: dest
   538   //
   539   // Result:
   540   //    *dest <- ex, return (orig *dest)
   541   address generate_atomic_xchg() {
   542     StubCodeMark mark(this, "StubRoutines", "atomic_xchg");
   543     address start = __ pc();
   545     __ movl(rax, c_rarg0); // Copy to eax we need a return value anyhow
   546     __ xchgl(rax, Address(c_rarg1, 0)); // automatic LOCK
   547     __ ret(0);
   549     return start;
   550   }
   552   // Support for intptr_t atomic::xchg_ptr(intptr_t exchange_value, volatile intptr_t* dest)
   553   //
   554   // Arguments :
   555   //    c_rarg0: exchange_value
   556   //    c_rarg1: dest
   557   //
   558   // Result:
   559   //    *dest <- ex, return (orig *dest)
   560   address generate_atomic_xchg_ptr() {
   561     StubCodeMark mark(this, "StubRoutines", "atomic_xchg_ptr");
   562     address start = __ pc();
   564     __ movptr(rax, c_rarg0); // Copy to eax we need a return value anyhow
   565     __ xchgptr(rax, Address(c_rarg1, 0)); // automatic LOCK
   566     __ ret(0);
   568     return start;
   569   }
   571   // Support for jint atomic::atomic_cmpxchg(jint exchange_value, volatile jint* dest,
   572   //                                         jint compare_value)
   573   //
   574   // Arguments :
   575   //    c_rarg0: exchange_value
   576   //    c_rarg1: dest
   577   //    c_rarg2: compare_value
   578   //
   579   // Result:
   580   //    if ( compare_value == *dest ) {
   581   //       *dest = exchange_value
   582   //       return compare_value;
   583   //    else
   584   //       return *dest;
   585   address generate_atomic_cmpxchg() {
   586     StubCodeMark mark(this, "StubRoutines", "atomic_cmpxchg");
   587     address start = __ pc();
   589     __ movl(rax, c_rarg2);
   590    if ( os::is_MP() ) __ lock();
   591     __ cmpxchgl(c_rarg0, Address(c_rarg1, 0));
   592     __ ret(0);
   594     return start;
   595   }
   597   // Support for jint atomic::atomic_cmpxchg_long(jlong exchange_value,
   598   //                                             volatile jlong* dest,
   599   //                                             jlong compare_value)
   600   // Arguments :
   601   //    c_rarg0: exchange_value
   602   //    c_rarg1: dest
   603   //    c_rarg2: compare_value
   604   //
   605   // Result:
   606   //    if ( compare_value == *dest ) {
   607   //       *dest = exchange_value
   608   //       return compare_value;
   609   //    else
   610   //       return *dest;
   611   address generate_atomic_cmpxchg_long() {
   612     StubCodeMark mark(this, "StubRoutines", "atomic_cmpxchg_long");
   613     address start = __ pc();
   615     __ movq(rax, c_rarg2);
   616    if ( os::is_MP() ) __ lock();
   617     __ cmpxchgq(c_rarg0, Address(c_rarg1, 0));
   618     __ ret(0);
   620     return start;
   621   }
   623   // Support for jint atomic::add(jint add_value, volatile jint* dest)
   624   //
   625   // Arguments :
   626   //    c_rarg0: add_value
   627   //    c_rarg1: dest
   628   //
   629   // Result:
   630   //    *dest += add_value
   631   //    return *dest;
   632   address generate_atomic_add() {
   633     StubCodeMark mark(this, "StubRoutines", "atomic_add");
   634     address start = __ pc();
   636     __ movl(rax, c_rarg0);
   637    if ( os::is_MP() ) __ lock();
   638     __ xaddl(Address(c_rarg1, 0), c_rarg0);
   639     __ addl(rax, c_rarg0);
   640     __ ret(0);
   642     return start;
   643   }
   645   // Support for intptr_t atomic::add_ptr(intptr_t add_value, volatile intptr_t* dest)
   646   //
   647   // Arguments :
   648   //    c_rarg0: add_value
   649   //    c_rarg1: dest
   650   //
   651   // Result:
   652   //    *dest += add_value
   653   //    return *dest;
   654   address generate_atomic_add_ptr() {
   655     StubCodeMark mark(this, "StubRoutines", "atomic_add_ptr");
   656     address start = __ pc();
   658     __ movptr(rax, c_rarg0); // Copy to eax we need a return value anyhow
   659    if ( os::is_MP() ) __ lock();
   660     __ xaddptr(Address(c_rarg1, 0), c_rarg0);
   661     __ addptr(rax, c_rarg0);
   662     __ ret(0);
   664     return start;
   665   }
   667   // Support for intptr_t OrderAccess::fence()
   668   //
   669   // Arguments :
   670   //
   671   // Result:
   672   address generate_orderaccess_fence() {
   673     StubCodeMark mark(this, "StubRoutines", "orderaccess_fence");
   674     address start = __ pc();
   675     __ membar(Assembler::StoreLoad);
   676     __ ret(0);
   678     return start;
   679   }
   681   // Support for intptr_t get_previous_fp()
   682   //
   683   // This routine is used to find the previous frame pointer for the
   684   // caller (current_frame_guess). This is used as part of debugging
   685   // ps() is seemingly lost trying to find frames.
   686   // This code assumes that caller current_frame_guess) has a frame.
   687   address generate_get_previous_fp() {
   688     StubCodeMark mark(this, "StubRoutines", "get_previous_fp");
   689     const Address old_fp(rbp, 0);
   690     const Address older_fp(rax, 0);
   691     address start = __ pc();
   693     __ enter();
   694     __ movptr(rax, old_fp); // callers fp
   695     __ movptr(rax, older_fp); // the frame for ps()
   696     __ pop(rbp);
   697     __ ret(0);
   699     return start;
   700   }
   702   // Support for intptr_t get_previous_sp()
   703   //
   704   // This routine is used to find the previous stack pointer for the
   705   // caller.
   706   address generate_get_previous_sp() {
   707     StubCodeMark mark(this, "StubRoutines", "get_previous_sp");
   708     address start = __ pc();
   710     __ movptr(rax, rsp);
   711     __ addptr(rax, 8); // return address is at the top of the stack.
   712     __ ret(0);
   714     return start;
   715   }
   717   //----------------------------------------------------------------------------------------------------
   718   // Support for void verify_mxcsr()
   719   //
   720   // This routine is used with -Xcheck:jni to verify that native
   721   // JNI code does not return to Java code without restoring the
   722   // MXCSR register to our expected state.
   724   address generate_verify_mxcsr() {
   725     StubCodeMark mark(this, "StubRoutines", "verify_mxcsr");
   726     address start = __ pc();
   728     const Address mxcsr_save(rsp, 0);
   730     if (CheckJNICalls) {
   731       Label ok_ret;
   732       __ push(rax);
   733       __ subptr(rsp, wordSize);      // allocate a temp location
   734       __ stmxcsr(mxcsr_save);
   735       __ movl(rax, mxcsr_save);
   736       __ andl(rax, MXCSR_MASK);    // Only check control and mask bits
   737       __ cmpl(rax, *(int *)(StubRoutines::x86::mxcsr_std()));
   738       __ jcc(Assembler::equal, ok_ret);
   740       __ warn("MXCSR changed by native JNI code, use -XX:+RestoreMXCSROnJNICall");
   742       __ ldmxcsr(ExternalAddress(StubRoutines::x86::mxcsr_std()));
   744       __ bind(ok_ret);
   745       __ addptr(rsp, wordSize);
   746       __ pop(rax);
   747     }
   749     __ ret(0);
   751     return start;
   752   }
   754   address generate_f2i_fixup() {
   755     StubCodeMark mark(this, "StubRoutines", "f2i_fixup");
   756     Address inout(rsp, 5 * wordSize); // return address + 4 saves
   758     address start = __ pc();
   760     Label L;
   762     __ push(rax);
   763     __ push(c_rarg3);
   764     __ push(c_rarg2);
   765     __ push(c_rarg1);
   767     __ movl(rax, 0x7f800000);
   768     __ xorl(c_rarg3, c_rarg3);
   769     __ movl(c_rarg2, inout);
   770     __ movl(c_rarg1, c_rarg2);
   771     __ andl(c_rarg1, 0x7fffffff);
   772     __ cmpl(rax, c_rarg1); // NaN? -> 0
   773     __ jcc(Assembler::negative, L);
   774     __ testl(c_rarg2, c_rarg2); // signed ? min_jint : max_jint
   775     __ movl(c_rarg3, 0x80000000);
   776     __ movl(rax, 0x7fffffff);
   777     __ cmovl(Assembler::positive, c_rarg3, rax);
   779     __ bind(L);
   780     __ movptr(inout, c_rarg3);
   782     __ pop(c_rarg1);
   783     __ pop(c_rarg2);
   784     __ pop(c_rarg3);
   785     __ pop(rax);
   787     __ ret(0);
   789     return start;
   790   }
   792   address generate_f2l_fixup() {
   793     StubCodeMark mark(this, "StubRoutines", "f2l_fixup");
   794     Address inout(rsp, 5 * wordSize); // return address + 4 saves
   795     address start = __ pc();
   797     Label L;
   799     __ push(rax);
   800     __ push(c_rarg3);
   801     __ push(c_rarg2);
   802     __ push(c_rarg1);
   804     __ movl(rax, 0x7f800000);
   805     __ xorl(c_rarg3, c_rarg3);
   806     __ movl(c_rarg2, inout);
   807     __ movl(c_rarg1, c_rarg2);
   808     __ andl(c_rarg1, 0x7fffffff);
   809     __ cmpl(rax, c_rarg1); // NaN? -> 0
   810     __ jcc(Assembler::negative, L);
   811     __ testl(c_rarg2, c_rarg2); // signed ? min_jlong : max_jlong
   812     __ mov64(c_rarg3, 0x8000000000000000);
   813     __ mov64(rax, 0x7fffffffffffffff);
   814     __ cmov(Assembler::positive, c_rarg3, rax);
   816     __ bind(L);
   817     __ movptr(inout, c_rarg3);
   819     __ pop(c_rarg1);
   820     __ pop(c_rarg2);
   821     __ pop(c_rarg3);
   822     __ pop(rax);
   824     __ ret(0);
   826     return start;
   827   }
   829   address generate_d2i_fixup() {
   830     StubCodeMark mark(this, "StubRoutines", "d2i_fixup");
   831     Address inout(rsp, 6 * wordSize); // return address + 5 saves
   833     address start = __ pc();
   835     Label L;
   837     __ push(rax);
   838     __ push(c_rarg3);
   839     __ push(c_rarg2);
   840     __ push(c_rarg1);
   841     __ push(c_rarg0);
   843     __ movl(rax, 0x7ff00000);
   844     __ movq(c_rarg2, inout);
   845     __ movl(c_rarg3, c_rarg2);
   846     __ mov(c_rarg1, c_rarg2);
   847     __ mov(c_rarg0, c_rarg2);
   848     __ negl(c_rarg3);
   849     __ shrptr(c_rarg1, 0x20);
   850     __ orl(c_rarg3, c_rarg2);
   851     __ andl(c_rarg1, 0x7fffffff);
   852     __ xorl(c_rarg2, c_rarg2);
   853     __ shrl(c_rarg3, 0x1f);
   854     __ orl(c_rarg1, c_rarg3);
   855     __ cmpl(rax, c_rarg1);
   856     __ jcc(Assembler::negative, L); // NaN -> 0
   857     __ testptr(c_rarg0, c_rarg0); // signed ? min_jint : max_jint
   858     __ movl(c_rarg2, 0x80000000);
   859     __ movl(rax, 0x7fffffff);
   860     __ cmov(Assembler::positive, c_rarg2, rax);
   862     __ bind(L);
   863     __ movptr(inout, c_rarg2);
   865     __ pop(c_rarg0);
   866     __ pop(c_rarg1);
   867     __ pop(c_rarg2);
   868     __ pop(c_rarg3);
   869     __ pop(rax);
   871     __ ret(0);
   873     return start;
   874   }
   876   address generate_d2l_fixup() {
   877     StubCodeMark mark(this, "StubRoutines", "d2l_fixup");
   878     Address inout(rsp, 6 * wordSize); // return address + 5 saves
   880     address start = __ pc();
   882     Label L;
   884     __ push(rax);
   885     __ push(c_rarg3);
   886     __ push(c_rarg2);
   887     __ push(c_rarg1);
   888     __ push(c_rarg0);
   890     __ movl(rax, 0x7ff00000);
   891     __ movq(c_rarg2, inout);
   892     __ movl(c_rarg3, c_rarg2);
   893     __ mov(c_rarg1, c_rarg2);
   894     __ mov(c_rarg0, c_rarg2);
   895     __ negl(c_rarg3);
   896     __ shrptr(c_rarg1, 0x20);
   897     __ orl(c_rarg3, c_rarg2);
   898     __ andl(c_rarg1, 0x7fffffff);
   899     __ xorl(c_rarg2, c_rarg2);
   900     __ shrl(c_rarg3, 0x1f);
   901     __ orl(c_rarg1, c_rarg3);
   902     __ cmpl(rax, c_rarg1);
   903     __ jcc(Assembler::negative, L); // NaN -> 0
   904     __ testq(c_rarg0, c_rarg0); // signed ? min_jlong : max_jlong
   905     __ mov64(c_rarg2, 0x8000000000000000);
   906     __ mov64(rax, 0x7fffffffffffffff);
   907     __ cmovq(Assembler::positive, c_rarg2, rax);
   909     __ bind(L);
   910     __ movq(inout, c_rarg2);
   912     __ pop(c_rarg0);
   913     __ pop(c_rarg1);
   914     __ pop(c_rarg2);
   915     __ pop(c_rarg3);
   916     __ pop(rax);
   918     __ ret(0);
   920     return start;
   921   }
   923   address generate_fp_mask(const char *stub_name, int64_t mask) {
   924     __ align(CodeEntryAlignment);
   925     StubCodeMark mark(this, "StubRoutines", stub_name);
   926     address start = __ pc();
   928     __ emit_data64( mask, relocInfo::none );
   929     __ emit_data64( mask, relocInfo::none );
   931     return start;
   932   }
   934   // The following routine generates a subroutine to throw an
   935   // asynchronous UnknownError when an unsafe access gets a fault that
   936   // could not be reasonably prevented by the programmer.  (Example:
   937   // SIGBUS/OBJERR.)
   938   address generate_handler_for_unsafe_access() {
   939     StubCodeMark mark(this, "StubRoutines", "handler_for_unsafe_access");
   940     address start = __ pc();
   942     __ push(0);                       // hole for return address-to-be
   943     __ pusha();                       // push registers
   944     Address next_pc(rsp, RegisterImpl::number_of_registers * BytesPerWord);
   946     // FIXME: this probably needs alignment logic
   948     __ subptr(rsp, frame::arg_reg_save_area_bytes);
   949     BLOCK_COMMENT("call handle_unsafe_access");
   950     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, handle_unsafe_access)));
   951     __ addptr(rsp, frame::arg_reg_save_area_bytes);
   953     __ movptr(next_pc, rax);          // stuff next address
   954     __ popa();
   955     __ ret(0);                        // jump to next address
   957     return start;
   958   }
   960   // Non-destructive plausibility checks for oops
   961   //
   962   // Arguments:
   963   //    all args on stack!
   964   //
   965   // Stack after saving c_rarg3:
   966   //    [tos + 0]: saved c_rarg3
   967   //    [tos + 1]: saved c_rarg2
   968   //    [tos + 2]: saved r12 (several TemplateTable methods use it)
   969   //    [tos + 3]: saved flags
   970   //    [tos + 4]: return address
   971   //  * [tos + 5]: error message (char*)
   972   //  * [tos + 6]: object to verify (oop)
   973   //  * [tos + 7]: saved rax - saved by caller and bashed
   974   //  * [tos + 8]: saved r10 (rscratch1) - saved by caller
   975   //  * = popped on exit
   976   address generate_verify_oop() {
   977     StubCodeMark mark(this, "StubRoutines", "verify_oop");
   978     address start = __ pc();
   980     Label exit, error;
   982     __ pushf();
   983     __ incrementl(ExternalAddress((address) StubRoutines::verify_oop_count_addr()));
   985     __ push(r12);
   987     // save c_rarg2 and c_rarg3
   988     __ push(c_rarg2);
   989     __ push(c_rarg3);
   991     enum {
   992            // After previous pushes.
   993            oop_to_verify = 6 * wordSize,
   994            saved_rax     = 7 * wordSize,
   995            saved_r10     = 8 * wordSize,
   997            // Before the call to MacroAssembler::debug(), see below.
   998            return_addr   = 16 * wordSize,
   999            error_msg     = 17 * wordSize
  1000     };
  1002     // get object
  1003     __ movptr(rax, Address(rsp, oop_to_verify));
  1005     // make sure object is 'reasonable'
  1006     __ testptr(rax, rax);
  1007     __ jcc(Assembler::zero, exit); // if obj is NULL it is OK
  1008     // Check if the oop is in the right area of memory
  1009     __ movptr(c_rarg2, rax);
  1010     __ movptr(c_rarg3, (intptr_t) Universe::verify_oop_mask());
  1011     __ andptr(c_rarg2, c_rarg3);
  1012     __ movptr(c_rarg3, (intptr_t) Universe::verify_oop_bits());
  1013     __ cmpptr(c_rarg2, c_rarg3);
  1014     __ jcc(Assembler::notZero, error);
  1016     // set r12 to heapbase for load_klass()
  1017     __ reinit_heapbase();
  1019     // make sure klass is 'reasonable', which is not zero.
  1020     __ load_klass(rax, rax);  // get klass
  1021     __ testptr(rax, rax);
  1022     __ jcc(Assembler::zero, error); // if klass is NULL it is broken
  1023     // TODO: Future assert that klass is lower 4g memory for UseCompressedKlassPointers
  1025     // return if everything seems ok
  1026     __ bind(exit);
  1027     __ movptr(rax, Address(rsp, saved_rax));     // get saved rax back
  1028     __ movptr(rscratch1, Address(rsp, saved_r10)); // get saved r10 back
  1029     __ pop(c_rarg3);                             // restore c_rarg3
  1030     __ pop(c_rarg2);                             // restore c_rarg2
  1031     __ pop(r12);                                 // restore r12
  1032     __ popf();                                   // restore flags
  1033     __ ret(4 * wordSize);                        // pop caller saved stuff
  1035     // handle errors
  1036     __ bind(error);
  1037     __ movptr(rax, Address(rsp, saved_rax));     // get saved rax back
  1038     __ movptr(rscratch1, Address(rsp, saved_r10)); // get saved r10 back
  1039     __ pop(c_rarg3);                             // get saved c_rarg3 back
  1040     __ pop(c_rarg2);                             // get saved c_rarg2 back
  1041     __ pop(r12);                                 // get saved r12 back
  1042     __ popf();                                   // get saved flags off stack --
  1043                                                  // will be ignored
  1045     __ pusha();                                  // push registers
  1046                                                  // (rip is already
  1047                                                  // already pushed)
  1048     // debug(char* msg, int64_t pc, int64_t regs[])
  1049     // We've popped the registers we'd saved (c_rarg3, c_rarg2 and flags), and
  1050     // pushed all the registers, so now the stack looks like:
  1051     //     [tos +  0] 16 saved registers
  1052     //     [tos + 16] return address
  1053     //   * [tos + 17] error message (char*)
  1054     //   * [tos + 18] object to verify (oop)
  1055     //   * [tos + 19] saved rax - saved by caller and bashed
  1056     //   * [tos + 20] saved r10 (rscratch1) - saved by caller
  1057     //   * = popped on exit
  1059     __ movptr(c_rarg0, Address(rsp, error_msg));    // pass address of error message
  1060     __ movptr(c_rarg1, Address(rsp, return_addr));  // pass return address
  1061     __ movq(c_rarg2, rsp);                          // pass address of regs on stack
  1062     __ mov(r12, rsp);                               // remember rsp
  1063     __ subptr(rsp, frame::arg_reg_save_area_bytes); // windows
  1064     __ andptr(rsp, -16);                            // align stack as required by ABI
  1065     BLOCK_COMMENT("call MacroAssembler::debug");
  1066     __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, MacroAssembler::debug64)));
  1067     __ mov(rsp, r12);                               // restore rsp
  1068     __ popa();                                      // pop registers (includes r12)
  1069     __ ret(4 * wordSize);                           // pop caller saved stuff
  1071     return start;
  1074   //
  1075   // Verify that a register contains clean 32-bits positive value
  1076   // (high 32-bits are 0) so it could be used in 64-bits shifts.
  1077   //
  1078   //  Input:
  1079   //    Rint  -  32-bits value
  1080   //    Rtmp  -  scratch
  1081   //
  1082   void assert_clean_int(Register Rint, Register Rtmp) {
  1083 #ifdef ASSERT
  1084     Label L;
  1085     assert_different_registers(Rtmp, Rint);
  1086     __ movslq(Rtmp, Rint);
  1087     __ cmpq(Rtmp, Rint);
  1088     __ jcc(Assembler::equal, L);
  1089     __ stop("high 32-bits of int value are not 0");
  1090     __ bind(L);
  1091 #endif
  1094   //  Generate overlap test for array copy stubs
  1095   //
  1096   //  Input:
  1097   //     c_rarg0 - from
  1098   //     c_rarg1 - to
  1099   //     c_rarg2 - element count
  1100   //
  1101   //  Output:
  1102   //     rax   - &from[element count - 1]
  1103   //
  1104   void array_overlap_test(address no_overlap_target, Address::ScaleFactor sf) {
  1105     assert(no_overlap_target != NULL, "must be generated");
  1106     array_overlap_test(no_overlap_target, NULL, sf);
  1108   void array_overlap_test(Label& L_no_overlap, Address::ScaleFactor sf) {
  1109     array_overlap_test(NULL, &L_no_overlap, sf);
  1111   void array_overlap_test(address no_overlap_target, Label* NOLp, Address::ScaleFactor sf) {
  1112     const Register from     = c_rarg0;
  1113     const Register to       = c_rarg1;
  1114     const Register count    = c_rarg2;
  1115     const Register end_from = rax;
  1117     __ cmpptr(to, from);
  1118     __ lea(end_from, Address(from, count, sf, 0));
  1119     if (NOLp == NULL) {
  1120       ExternalAddress no_overlap(no_overlap_target);
  1121       __ jump_cc(Assembler::belowEqual, no_overlap);
  1122       __ cmpptr(to, end_from);
  1123       __ jump_cc(Assembler::aboveEqual, no_overlap);
  1124     } else {
  1125       __ jcc(Assembler::belowEqual, (*NOLp));
  1126       __ cmpptr(to, end_from);
  1127       __ jcc(Assembler::aboveEqual, (*NOLp));
  1131   // Shuffle first three arg regs on Windows into Linux/Solaris locations.
  1132   //
  1133   // Outputs:
  1134   //    rdi - rcx
  1135   //    rsi - rdx
  1136   //    rdx - r8
  1137   //    rcx - r9
  1138   //
  1139   // Registers r9 and r10 are used to save rdi and rsi on Windows, which latter
  1140   // are non-volatile.  r9 and r10 should not be used by the caller.
  1141   //
  1142   void setup_arg_regs(int nargs = 3) {
  1143     const Register saved_rdi = r9;
  1144     const Register saved_rsi = r10;
  1145     assert(nargs == 3 || nargs == 4, "else fix");
  1146 #ifdef _WIN64
  1147     assert(c_rarg0 == rcx && c_rarg1 == rdx && c_rarg2 == r8 && c_rarg3 == r9,
  1148            "unexpected argument registers");
  1149     if (nargs >= 4)
  1150       __ mov(rax, r9);  // r9 is also saved_rdi
  1151     __ movptr(saved_rdi, rdi);
  1152     __ movptr(saved_rsi, rsi);
  1153     __ mov(rdi, rcx); // c_rarg0
  1154     __ mov(rsi, rdx); // c_rarg1
  1155     __ mov(rdx, r8);  // c_rarg2
  1156     if (nargs >= 4)
  1157       __ mov(rcx, rax); // c_rarg3 (via rax)
  1158 #else
  1159     assert(c_rarg0 == rdi && c_rarg1 == rsi && c_rarg2 == rdx && c_rarg3 == rcx,
  1160            "unexpected argument registers");
  1161 #endif
  1164   void restore_arg_regs() {
  1165     const Register saved_rdi = r9;
  1166     const Register saved_rsi = r10;
  1167 #ifdef _WIN64
  1168     __ movptr(rdi, saved_rdi);
  1169     __ movptr(rsi, saved_rsi);
  1170 #endif
  1173   // Generate code for an array write pre barrier
  1174   //
  1175   //     addr    -  starting address
  1176   //     count   -  element count
  1177   //     tmp     - scratch register
  1178   //
  1179   //     Destroy no registers!
  1180   //
  1181   void  gen_write_ref_array_pre_barrier(Register addr, Register count, bool dest_uninitialized) {
  1182     BarrierSet* bs = Universe::heap()->barrier_set();
  1183     switch (bs->kind()) {
  1184       case BarrierSet::G1SATBCT:
  1185       case BarrierSet::G1SATBCTLogging:
  1186         // With G1, don't generate the call if we statically know that the target in uninitialized
  1187         if (!dest_uninitialized) {
  1188            __ pusha();                      // push registers
  1189            if (count == c_rarg0) {
  1190              if (addr == c_rarg1) {
  1191                // exactly backwards!!
  1192                __ xchgptr(c_rarg1, c_rarg0);
  1193              } else {
  1194                __ movptr(c_rarg1, count);
  1195                __ movptr(c_rarg0, addr);
  1197            } else {
  1198              __ movptr(c_rarg0, addr);
  1199              __ movptr(c_rarg1, count);
  1201            __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_pre), 2);
  1202            __ popa();
  1204          break;
  1205       case BarrierSet::CardTableModRef:
  1206       case BarrierSet::CardTableExtension:
  1207       case BarrierSet::ModRef:
  1208         break;
  1209       default:
  1210         ShouldNotReachHere();
  1215   //
  1216   // Generate code for an array write post barrier
  1217   //
  1218   //  Input:
  1219   //     start    - register containing starting address of destination array
  1220   //     end      - register containing ending address of destination array
  1221   //     scratch  - scratch register
  1222   //
  1223   //  The input registers are overwritten.
  1224   //  The ending address is inclusive.
  1225   void  gen_write_ref_array_post_barrier(Register start, Register end, Register scratch) {
  1226     assert_different_registers(start, end, scratch);
  1227     BarrierSet* bs = Universe::heap()->barrier_set();
  1228     switch (bs->kind()) {
  1229       case BarrierSet::G1SATBCT:
  1230       case BarrierSet::G1SATBCTLogging:
  1233           __ pusha();                      // push registers (overkill)
  1234           // must compute element count unless barrier set interface is changed (other platforms supply count)
  1235           assert_different_registers(start, end, scratch);
  1236           __ lea(scratch, Address(end, BytesPerHeapOop));
  1237           __ subptr(scratch, start);               // subtract start to get #bytes
  1238           __ shrptr(scratch, LogBytesPerHeapOop);  // convert to element count
  1239           __ mov(c_rarg0, start);
  1240           __ mov(c_rarg1, scratch);
  1241           __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_post), 2);
  1242           __ popa();
  1244         break;
  1245       case BarrierSet::CardTableModRef:
  1246       case BarrierSet::CardTableExtension:
  1248           CardTableModRefBS* ct = (CardTableModRefBS*)bs;
  1249           assert(sizeof(*ct->byte_map_base) == sizeof(jbyte), "adjust this code");
  1251           Label L_loop;
  1253            __ shrptr(start, CardTableModRefBS::card_shift);
  1254            __ addptr(end, BytesPerHeapOop);
  1255            __ shrptr(end, CardTableModRefBS::card_shift);
  1256            __ subptr(end, start); // number of bytes to copy
  1258           intptr_t disp = (intptr_t) ct->byte_map_base;
  1259           if (Assembler::is_simm32(disp)) {
  1260             Address cardtable(noreg, noreg, Address::no_scale, disp);
  1261             __ lea(scratch, cardtable);
  1262           } else {
  1263             ExternalAddress cardtable((address)disp);
  1264             __ lea(scratch, cardtable);
  1267           const Register count = end; // 'end' register contains bytes count now
  1268           __ addptr(start, scratch);
  1269         __ BIND(L_loop);
  1270           __ movb(Address(start, count, Address::times_1), 0);
  1271           __ decrement(count);
  1272           __ jcc(Assembler::greaterEqual, L_loop);
  1274         break;
  1275       default:
  1276         ShouldNotReachHere();
  1282   // Copy big chunks forward
  1283   //
  1284   // Inputs:
  1285   //   end_from     - source arrays end address
  1286   //   end_to       - destination array end address
  1287   //   qword_count  - 64-bits element count, negative
  1288   //   to           - scratch
  1289   //   L_copy_32_bytes - entry label
  1290   //   L_copy_8_bytes  - exit  label
  1291   //
  1292   void copy_32_bytes_forward(Register end_from, Register end_to,
  1293                              Register qword_count, Register to,
  1294                              Label& L_copy_32_bytes, Label& L_copy_8_bytes) {
  1295     DEBUG_ONLY(__ stop("enter at entry label, not here"));
  1296     Label L_loop;
  1297     __ align(OptoLoopAlignment);
  1298   __ BIND(L_loop);
  1299     if(UseUnalignedLoadStores) {
  1300       __ movdqu(xmm0, Address(end_from, qword_count, Address::times_8, -24));
  1301       __ movdqu(Address(end_to, qword_count, Address::times_8, -24), xmm0);
  1302       __ movdqu(xmm1, Address(end_from, qword_count, Address::times_8, - 8));
  1303       __ movdqu(Address(end_to, qword_count, Address::times_8, - 8), xmm1);
  1305     } else {
  1306       __ movq(to, Address(end_from, qword_count, Address::times_8, -24));
  1307       __ movq(Address(end_to, qword_count, Address::times_8, -24), to);
  1308       __ movq(to, Address(end_from, qword_count, Address::times_8, -16));
  1309       __ movq(Address(end_to, qword_count, Address::times_8, -16), to);
  1310       __ movq(to, Address(end_from, qword_count, Address::times_8, - 8));
  1311       __ movq(Address(end_to, qword_count, Address::times_8, - 8), to);
  1312       __ movq(to, Address(end_from, qword_count, Address::times_8, - 0));
  1313       __ movq(Address(end_to, qword_count, Address::times_8, - 0), to);
  1315   __ BIND(L_copy_32_bytes);
  1316     __ addptr(qword_count, 4);
  1317     __ jcc(Assembler::lessEqual, L_loop);
  1318     __ subptr(qword_count, 4);
  1319     __ jcc(Assembler::less, L_copy_8_bytes); // Copy trailing qwords
  1323   // Copy big chunks backward
  1324   //
  1325   // Inputs:
  1326   //   from         - source arrays address
  1327   //   dest         - destination array address
  1328   //   qword_count  - 64-bits element count
  1329   //   to           - scratch
  1330   //   L_copy_32_bytes - entry label
  1331   //   L_copy_8_bytes  - exit  label
  1332   //
  1333   void copy_32_bytes_backward(Register from, Register dest,
  1334                               Register qword_count, Register to,
  1335                               Label& L_copy_32_bytes, Label& L_copy_8_bytes) {
  1336     DEBUG_ONLY(__ stop("enter at entry label, not here"));
  1337     Label L_loop;
  1338     __ align(OptoLoopAlignment);
  1339   __ BIND(L_loop);
  1340     if(UseUnalignedLoadStores) {
  1341       __ movdqu(xmm0, Address(from, qword_count, Address::times_8, 16));
  1342       __ movdqu(Address(dest, qword_count, Address::times_8, 16), xmm0);
  1343       __ movdqu(xmm1, Address(from, qword_count, Address::times_8,  0));
  1344       __ movdqu(Address(dest, qword_count, Address::times_8,  0), xmm1);
  1346     } else {
  1347       __ movq(to, Address(from, qword_count, Address::times_8, 24));
  1348       __ movq(Address(dest, qword_count, Address::times_8, 24), to);
  1349       __ movq(to, Address(from, qword_count, Address::times_8, 16));
  1350       __ movq(Address(dest, qword_count, Address::times_8, 16), to);
  1351       __ movq(to, Address(from, qword_count, Address::times_8,  8));
  1352       __ movq(Address(dest, qword_count, Address::times_8,  8), to);
  1353       __ movq(to, Address(from, qword_count, Address::times_8,  0));
  1354       __ movq(Address(dest, qword_count, Address::times_8,  0), to);
  1356   __ BIND(L_copy_32_bytes);
  1357     __ subptr(qword_count, 4);
  1358     __ jcc(Assembler::greaterEqual, L_loop);
  1359     __ addptr(qword_count, 4);
  1360     __ jcc(Assembler::greater, L_copy_8_bytes); // Copy trailing qwords
  1364   // Arguments:
  1365   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
  1366   //             ignored
  1367   //   name    - stub name string
  1368   //
  1369   // Inputs:
  1370   //   c_rarg0   - source array address
  1371   //   c_rarg1   - destination array address
  1372   //   c_rarg2   - element count, treated as ssize_t, can be zero
  1373   //
  1374   // If 'from' and/or 'to' are aligned on 4-, 2-, or 1-byte boundaries,
  1375   // we let the hardware handle it.  The one to eight bytes within words,
  1376   // dwords or qwords that span cache line boundaries will still be loaded
  1377   // and stored atomically.
  1378   //
  1379   // Side Effects:
  1380   //   disjoint_byte_copy_entry is set to the no-overlap entry point
  1381   //   used by generate_conjoint_byte_copy().
  1382   //
  1383   address generate_disjoint_byte_copy(bool aligned, address* entry, const char *name) {
  1384     __ align(CodeEntryAlignment);
  1385     StubCodeMark mark(this, "StubRoutines", name);
  1386     address start = __ pc();
  1388     Label L_copy_32_bytes, L_copy_8_bytes, L_copy_4_bytes, L_copy_2_bytes;
  1389     Label L_copy_byte, L_exit;
  1390     const Register from        = rdi;  // source array address
  1391     const Register to          = rsi;  // destination array address
  1392     const Register count       = rdx;  // elements count
  1393     const Register byte_count  = rcx;
  1394     const Register qword_count = count;
  1395     const Register end_from    = from; // source array end address
  1396     const Register end_to      = to;   // destination array end address
  1397     // End pointers are inclusive, and if count is not zero they point
  1398     // to the last unit copied:  end_to[0] := end_from[0]
  1400     __ enter(); // required for proper stackwalking of RuntimeStub frame
  1401     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
  1403     if (entry != NULL) {
  1404       *entry = __ pc();
  1405        // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
  1406       BLOCK_COMMENT("Entry:");
  1409     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
  1410                       // r9 and r10 may be used to save non-volatile registers
  1412     // 'from', 'to' and 'count' are now valid
  1413     __ movptr(byte_count, count);
  1414     __ shrptr(count, 3); // count => qword_count
  1416     // Copy from low to high addresses.  Use 'to' as scratch.
  1417     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
  1418     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
  1419     __ negptr(qword_count); // make the count negative
  1420     __ jmp(L_copy_32_bytes);
  1422     // Copy trailing qwords
  1423   __ BIND(L_copy_8_bytes);
  1424     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
  1425     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
  1426     __ increment(qword_count);
  1427     __ jcc(Assembler::notZero, L_copy_8_bytes);
  1429     // Check for and copy trailing dword
  1430   __ BIND(L_copy_4_bytes);
  1431     __ testl(byte_count, 4);
  1432     __ jccb(Assembler::zero, L_copy_2_bytes);
  1433     __ movl(rax, Address(end_from, 8));
  1434     __ movl(Address(end_to, 8), rax);
  1436     __ addptr(end_from, 4);
  1437     __ addptr(end_to, 4);
  1439     // Check for and copy trailing word
  1440   __ BIND(L_copy_2_bytes);
  1441     __ testl(byte_count, 2);
  1442     __ jccb(Assembler::zero, L_copy_byte);
  1443     __ movw(rax, Address(end_from, 8));
  1444     __ movw(Address(end_to, 8), rax);
  1446     __ addptr(end_from, 2);
  1447     __ addptr(end_to, 2);
  1449     // Check for and copy trailing byte
  1450   __ BIND(L_copy_byte);
  1451     __ testl(byte_count, 1);
  1452     __ jccb(Assembler::zero, L_exit);
  1453     __ movb(rax, Address(end_from, 8));
  1454     __ movb(Address(end_to, 8), rax);
  1456   __ BIND(L_exit);
  1457     restore_arg_regs();
  1458     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); // Update counter after rscratch1 is free
  1459     __ xorptr(rax, rax); // return 0
  1460     __ leave(); // required for proper stackwalking of RuntimeStub frame
  1461     __ ret(0);
  1463     // Copy in 32-bytes chunks
  1464     copy_32_bytes_forward(end_from, end_to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
  1465     __ jmp(L_copy_4_bytes);
  1467     return start;
  1470   // Arguments:
  1471   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
  1472   //             ignored
  1473   //   name    - stub name string
  1474   //
  1475   // Inputs:
  1476   //   c_rarg0   - source array address
  1477   //   c_rarg1   - destination array address
  1478   //   c_rarg2   - element count, treated as ssize_t, can be zero
  1479   //
  1480   // If 'from' and/or 'to' are aligned on 4-, 2-, or 1-byte boundaries,
  1481   // we let the hardware handle it.  The one to eight bytes within words,
  1482   // dwords or qwords that span cache line boundaries will still be loaded
  1483   // and stored atomically.
  1484   //
  1485   address generate_conjoint_byte_copy(bool aligned, address nooverlap_target,
  1486                                       address* entry, const char *name) {
  1487     __ align(CodeEntryAlignment);
  1488     StubCodeMark mark(this, "StubRoutines", name);
  1489     address start = __ pc();
  1491     Label L_copy_32_bytes, L_copy_8_bytes, L_copy_4_bytes, L_copy_2_bytes;
  1492     const Register from        = rdi;  // source array address
  1493     const Register to          = rsi;  // destination array address
  1494     const Register count       = rdx;  // elements count
  1495     const Register byte_count  = rcx;
  1496     const Register qword_count = count;
  1498     __ enter(); // required for proper stackwalking of RuntimeStub frame
  1499     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
  1501     if (entry != NULL) {
  1502       *entry = __ pc();
  1503       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
  1504       BLOCK_COMMENT("Entry:");
  1507     array_overlap_test(nooverlap_target, Address::times_1);
  1508     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
  1509                       // r9 and r10 may be used to save non-volatile registers
  1511     // 'from', 'to' and 'count' are now valid
  1512     __ movptr(byte_count, count);
  1513     __ shrptr(count, 3);   // count => qword_count
  1515     // Copy from high to low addresses.
  1517     // Check for and copy trailing byte
  1518     __ testl(byte_count, 1);
  1519     __ jcc(Assembler::zero, L_copy_2_bytes);
  1520     __ movb(rax, Address(from, byte_count, Address::times_1, -1));
  1521     __ movb(Address(to, byte_count, Address::times_1, -1), rax);
  1522     __ decrement(byte_count); // Adjust for possible trailing word
  1524     // Check for and copy trailing word
  1525   __ BIND(L_copy_2_bytes);
  1526     __ testl(byte_count, 2);
  1527     __ jcc(Assembler::zero, L_copy_4_bytes);
  1528     __ movw(rax, Address(from, byte_count, Address::times_1, -2));
  1529     __ movw(Address(to, byte_count, Address::times_1, -2), rax);
  1531     // Check for and copy trailing dword
  1532   __ BIND(L_copy_4_bytes);
  1533     __ testl(byte_count, 4);
  1534     __ jcc(Assembler::zero, L_copy_32_bytes);
  1535     __ movl(rax, Address(from, qword_count, Address::times_8));
  1536     __ movl(Address(to, qword_count, Address::times_8), rax);
  1537     __ jmp(L_copy_32_bytes);
  1539     // Copy trailing qwords
  1540   __ BIND(L_copy_8_bytes);
  1541     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
  1542     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
  1543     __ decrement(qword_count);
  1544     __ jcc(Assembler::notZero, L_copy_8_bytes);
  1546     restore_arg_regs();
  1547     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); // Update counter after rscratch1 is free
  1548     __ xorptr(rax, rax); // return 0
  1549     __ leave(); // required for proper stackwalking of RuntimeStub frame
  1550     __ ret(0);
  1552     // Copy in 32-bytes chunks
  1553     copy_32_bytes_backward(from, to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
  1555     restore_arg_regs();
  1556     inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); // Update counter after rscratch1 is free
  1557     __ xorptr(rax, rax); // return 0
  1558     __ leave(); // required for proper stackwalking of RuntimeStub frame
  1559     __ ret(0);
  1561     return start;
  1564   // Arguments:
  1565   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
  1566   //             ignored
  1567   //   name    - stub name string
  1568   //
  1569   // Inputs:
  1570   //   c_rarg0   - source array address
  1571   //   c_rarg1   - destination array address
  1572   //   c_rarg2   - element count, treated as ssize_t, can be zero
  1573   //
  1574   // If 'from' and/or 'to' are aligned on 4- or 2-byte boundaries, we
  1575   // let the hardware handle it.  The two or four words within dwords
  1576   // or qwords that span cache line boundaries will still be loaded
  1577   // and stored atomically.
  1578   //
  1579   // Side Effects:
  1580   //   disjoint_short_copy_entry is set to the no-overlap entry point
  1581   //   used by generate_conjoint_short_copy().
  1582   //
  1583   address generate_disjoint_short_copy(bool aligned, address *entry, const char *name) {
  1584     __ align(CodeEntryAlignment);
  1585     StubCodeMark mark(this, "StubRoutines", name);
  1586     address start = __ pc();
  1588     Label L_copy_32_bytes, L_copy_8_bytes, L_copy_4_bytes,L_copy_2_bytes,L_exit;
  1589     const Register from        = rdi;  // source array address
  1590     const Register to          = rsi;  // destination array address
  1591     const Register count       = rdx;  // elements count
  1592     const Register word_count  = rcx;
  1593     const Register qword_count = count;
  1594     const Register end_from    = from; // source array end address
  1595     const Register end_to      = to;   // destination array end address
  1596     // End pointers are inclusive, and if count is not zero they point
  1597     // to the last unit copied:  end_to[0] := end_from[0]
  1599     __ enter(); // required for proper stackwalking of RuntimeStub frame
  1600     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
  1602     if (entry != NULL) {
  1603       *entry = __ pc();
  1604       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
  1605       BLOCK_COMMENT("Entry:");
  1608     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
  1609                       // r9 and r10 may be used to save non-volatile registers
  1611     // 'from', 'to' and 'count' are now valid
  1612     __ movptr(word_count, count);
  1613     __ shrptr(count, 2); // count => qword_count
  1615     // Copy from low to high addresses.  Use 'to' as scratch.
  1616     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
  1617     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
  1618     __ negptr(qword_count);
  1619     __ jmp(L_copy_32_bytes);
  1621     // Copy trailing qwords
  1622   __ BIND(L_copy_8_bytes);
  1623     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
  1624     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
  1625     __ increment(qword_count);
  1626     __ jcc(Assembler::notZero, L_copy_8_bytes);
  1628     // Original 'dest' is trashed, so we can't use it as a
  1629     // base register for a possible trailing word copy
  1631     // Check for and copy trailing dword
  1632   __ BIND(L_copy_4_bytes);
  1633     __ testl(word_count, 2);
  1634     __ jccb(Assembler::zero, L_copy_2_bytes);
  1635     __ movl(rax, Address(end_from, 8));
  1636     __ movl(Address(end_to, 8), rax);
  1638     __ addptr(end_from, 4);
  1639     __ addptr(end_to, 4);
  1641     // Check for and copy trailing word
  1642   __ BIND(L_copy_2_bytes);
  1643     __ testl(word_count, 1);
  1644     __ jccb(Assembler::zero, L_exit);
  1645     __ movw(rax, Address(end_from, 8));
  1646     __ movw(Address(end_to, 8), rax);
  1648   __ BIND(L_exit);
  1649     restore_arg_regs();
  1650     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); // Update counter after rscratch1 is free
  1651     __ xorptr(rax, rax); // return 0
  1652     __ leave(); // required for proper stackwalking of RuntimeStub frame
  1653     __ ret(0);
  1655     // Copy in 32-bytes chunks
  1656     copy_32_bytes_forward(end_from, end_to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
  1657     __ jmp(L_copy_4_bytes);
  1659     return start;
  1662   address generate_fill(BasicType t, bool aligned, const char *name) {
  1663     __ align(CodeEntryAlignment);
  1664     StubCodeMark mark(this, "StubRoutines", name);
  1665     address start = __ pc();
  1667     BLOCK_COMMENT("Entry:");
  1669     const Register to       = c_rarg0;  // source array address
  1670     const Register value    = c_rarg1;  // value
  1671     const Register count    = c_rarg2;  // elements count
  1673     __ enter(); // required for proper stackwalking of RuntimeStub frame
  1675     __ generate_fill(t, aligned, to, value, count, rax, xmm0);
  1677     __ leave(); // required for proper stackwalking of RuntimeStub frame
  1678     __ ret(0);
  1679     return start;
  1682   // Arguments:
  1683   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
  1684   //             ignored
  1685   //   name    - stub name string
  1686   //
  1687   // Inputs:
  1688   //   c_rarg0   - source array address
  1689   //   c_rarg1   - destination array address
  1690   //   c_rarg2   - element count, treated as ssize_t, can be zero
  1691   //
  1692   // If 'from' and/or 'to' are aligned on 4- or 2-byte boundaries, we
  1693   // let the hardware handle it.  The two or four words within dwords
  1694   // or qwords that span cache line boundaries will still be loaded
  1695   // and stored atomically.
  1696   //
  1697   address generate_conjoint_short_copy(bool aligned, address nooverlap_target,
  1698                                        address *entry, const char *name) {
  1699     __ align(CodeEntryAlignment);
  1700     StubCodeMark mark(this, "StubRoutines", name);
  1701     address start = __ pc();
  1703     Label L_copy_32_bytes, L_copy_8_bytes, L_copy_4_bytes;
  1704     const Register from        = rdi;  // source array address
  1705     const Register to          = rsi;  // destination array address
  1706     const Register count       = rdx;  // elements count
  1707     const Register word_count  = rcx;
  1708     const Register qword_count = count;
  1710     __ enter(); // required for proper stackwalking of RuntimeStub frame
  1711     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
  1713     if (entry != NULL) {
  1714       *entry = __ pc();
  1715       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
  1716       BLOCK_COMMENT("Entry:");
  1719     array_overlap_test(nooverlap_target, Address::times_2);
  1720     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
  1721                       // r9 and r10 may be used to save non-volatile registers
  1723     // 'from', 'to' and 'count' are now valid
  1724     __ movptr(word_count, count);
  1725     __ shrptr(count, 2); // count => qword_count
  1727     // Copy from high to low addresses.  Use 'to' as scratch.
  1729     // Check for and copy trailing word
  1730     __ testl(word_count, 1);
  1731     __ jccb(Assembler::zero, L_copy_4_bytes);
  1732     __ movw(rax, Address(from, word_count, Address::times_2, -2));
  1733     __ movw(Address(to, word_count, Address::times_2, -2), rax);
  1735     // Check for and copy trailing dword
  1736   __ BIND(L_copy_4_bytes);
  1737     __ testl(word_count, 2);
  1738     __ jcc(Assembler::zero, L_copy_32_bytes);
  1739     __ movl(rax, Address(from, qword_count, Address::times_8));
  1740     __ movl(Address(to, qword_count, Address::times_8), rax);
  1741     __ jmp(L_copy_32_bytes);
  1743     // Copy trailing qwords
  1744   __ BIND(L_copy_8_bytes);
  1745     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
  1746     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
  1747     __ decrement(qword_count);
  1748     __ jcc(Assembler::notZero, L_copy_8_bytes);
  1750     restore_arg_regs();
  1751     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); // Update counter after rscratch1 is free
  1752     __ xorptr(rax, rax); // return 0
  1753     __ leave(); // required for proper stackwalking of RuntimeStub frame
  1754     __ ret(0);
  1756     // Copy in 32-bytes chunks
  1757     copy_32_bytes_backward(from, to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
  1759     restore_arg_regs();
  1760     inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); // Update counter after rscratch1 is free
  1761     __ xorptr(rax, rax); // return 0
  1762     __ leave(); // required for proper stackwalking of RuntimeStub frame
  1763     __ ret(0);
  1765     return start;
  1768   // Arguments:
  1769   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
  1770   //             ignored
  1771   //   is_oop  - true => oop array, so generate store check code
  1772   //   name    - stub name string
  1773   //
  1774   // Inputs:
  1775   //   c_rarg0   - source array address
  1776   //   c_rarg1   - destination array address
  1777   //   c_rarg2   - element count, treated as ssize_t, can be zero
  1778   //
  1779   // If 'from' and/or 'to' are aligned on 4-byte boundaries, we let
  1780   // the hardware handle it.  The two dwords within qwords that span
  1781   // cache line boundaries will still be loaded and stored atomicly.
  1782   //
  1783   // Side Effects:
  1784   //   disjoint_int_copy_entry is set to the no-overlap entry point
  1785   //   used by generate_conjoint_int_oop_copy().
  1786   //
  1787   address generate_disjoint_int_oop_copy(bool aligned, bool is_oop, address* entry,
  1788                                          const char *name, bool dest_uninitialized = false) {
  1789     __ align(CodeEntryAlignment);
  1790     StubCodeMark mark(this, "StubRoutines", name);
  1791     address start = __ pc();
  1793     Label L_copy_32_bytes, L_copy_8_bytes, L_copy_4_bytes, L_exit;
  1794     const Register from        = rdi;  // source array address
  1795     const Register to          = rsi;  // destination array address
  1796     const Register count       = rdx;  // elements count
  1797     const Register dword_count = rcx;
  1798     const Register qword_count = count;
  1799     const Register end_from    = from; // source array end address
  1800     const Register end_to      = to;   // destination array end address
  1801     const Register saved_to    = r11;  // saved destination array address
  1802     // End pointers are inclusive, and if count is not zero they point
  1803     // to the last unit copied:  end_to[0] := end_from[0]
  1805     __ enter(); // required for proper stackwalking of RuntimeStub frame
  1806     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
  1808     if (entry != NULL) {
  1809       *entry = __ pc();
  1810       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
  1811       BLOCK_COMMENT("Entry:");
  1814     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
  1815                       // r9 and r10 may be used to save non-volatile registers
  1816     if (is_oop) {
  1817       __ movq(saved_to, to);
  1818       gen_write_ref_array_pre_barrier(to, count, dest_uninitialized);
  1821     // 'from', 'to' and 'count' are now valid
  1822     __ movptr(dword_count, count);
  1823     __ shrptr(count, 1); // count => qword_count
  1825     // Copy from low to high addresses.  Use 'to' as scratch.
  1826     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
  1827     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
  1828     __ negptr(qword_count);
  1829     __ jmp(L_copy_32_bytes);
  1831     // Copy trailing qwords
  1832   __ BIND(L_copy_8_bytes);
  1833     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
  1834     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
  1835     __ increment(qword_count);
  1836     __ jcc(Assembler::notZero, L_copy_8_bytes);
  1838     // Check for and copy trailing dword
  1839   __ BIND(L_copy_4_bytes);
  1840     __ testl(dword_count, 1); // Only byte test since the value is 0 or 1
  1841     __ jccb(Assembler::zero, L_exit);
  1842     __ movl(rax, Address(end_from, 8));
  1843     __ movl(Address(end_to, 8), rax);
  1845   __ BIND(L_exit);
  1846     if (is_oop) {
  1847       __ leaq(end_to, Address(saved_to, dword_count, Address::times_4, -4));
  1848       gen_write_ref_array_post_barrier(saved_to, end_to, rax);
  1850     restore_arg_regs();
  1851     inc_counter_np(SharedRuntime::_jint_array_copy_ctr); // Update counter after rscratch1 is free
  1852     __ xorptr(rax, rax); // return 0
  1853     __ leave(); // required for proper stackwalking of RuntimeStub frame
  1854     __ ret(0);
  1856     // Copy 32-bytes chunks
  1857     copy_32_bytes_forward(end_from, end_to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
  1858     __ jmp(L_copy_4_bytes);
  1860     return start;
  1863   // Arguments:
  1864   //   aligned - true => Input and output aligned on a HeapWord == 8-byte boundary
  1865   //             ignored
  1866   //   is_oop  - true => oop array, so generate store check code
  1867   //   name    - stub name string
  1868   //
  1869   // Inputs:
  1870   //   c_rarg0   - source array address
  1871   //   c_rarg1   - destination array address
  1872   //   c_rarg2   - element count, treated as ssize_t, can be zero
  1873   //
  1874   // If 'from' and/or 'to' are aligned on 4-byte boundaries, we let
  1875   // the hardware handle it.  The two dwords within qwords that span
  1876   // cache line boundaries will still be loaded and stored atomicly.
  1877   //
  1878   address generate_conjoint_int_oop_copy(bool aligned, bool is_oop, address nooverlap_target,
  1879                                          address *entry, const char *name,
  1880                                          bool dest_uninitialized = false) {
  1881     __ align(CodeEntryAlignment);
  1882     StubCodeMark mark(this, "StubRoutines", name);
  1883     address start = __ pc();
  1885     Label L_copy_32_bytes, L_copy_8_bytes, L_copy_2_bytes, L_exit;
  1886     const Register from        = rdi;  // source array address
  1887     const Register to          = rsi;  // destination array address
  1888     const Register count       = rdx;  // elements count
  1889     const Register dword_count = rcx;
  1890     const Register qword_count = count;
  1892     __ enter(); // required for proper stackwalking of RuntimeStub frame
  1893     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
  1895     if (entry != NULL) {
  1896       *entry = __ pc();
  1897        // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
  1898       BLOCK_COMMENT("Entry:");
  1901     array_overlap_test(nooverlap_target, Address::times_4);
  1902     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
  1903                       // r9 and r10 may be used to save non-volatile registers
  1905     if (is_oop) {
  1906       // no registers are destroyed by this call
  1907       gen_write_ref_array_pre_barrier(to, count, dest_uninitialized);
  1910     assert_clean_int(count, rax); // Make sure 'count' is clean int.
  1911     // 'from', 'to' and 'count' are now valid
  1912     __ movptr(dword_count, count);
  1913     __ shrptr(count, 1); // count => qword_count
  1915     // Copy from high to low addresses.  Use 'to' as scratch.
  1917     // Check for and copy trailing dword
  1918     __ testl(dword_count, 1);
  1919     __ jcc(Assembler::zero, L_copy_32_bytes);
  1920     __ movl(rax, Address(from, dword_count, Address::times_4, -4));
  1921     __ movl(Address(to, dword_count, Address::times_4, -4), rax);
  1922     __ jmp(L_copy_32_bytes);
  1924     // Copy trailing qwords
  1925   __ BIND(L_copy_8_bytes);
  1926     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
  1927     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
  1928     __ decrement(qword_count);
  1929     __ jcc(Assembler::notZero, L_copy_8_bytes);
  1931     if (is_oop) {
  1932       __ jmp(L_exit);
  1934     restore_arg_regs();
  1935     inc_counter_np(SharedRuntime::_jint_array_copy_ctr); // Update counter after rscratch1 is free
  1936     __ xorptr(rax, rax); // return 0
  1937     __ leave(); // required for proper stackwalking of RuntimeStub frame
  1938     __ ret(0);
  1940     // Copy in 32-bytes chunks
  1941     copy_32_bytes_backward(from, to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
  1943    __ bind(L_exit);
  1944      if (is_oop) {
  1945        Register end_to = rdx;
  1946        __ leaq(end_to, Address(to, dword_count, Address::times_4, -4));
  1947        gen_write_ref_array_post_barrier(to, end_to, rax);
  1949     restore_arg_regs();
  1950     inc_counter_np(SharedRuntime::_jint_array_copy_ctr); // Update counter after rscratch1 is free
  1951     __ xorptr(rax, rax); // return 0
  1952     __ leave(); // required for proper stackwalking of RuntimeStub frame
  1953     __ ret(0);
  1955     return start;
  1958   // Arguments:
  1959   //   aligned - true => Input and output aligned on a HeapWord boundary == 8 bytes
  1960   //             ignored
  1961   //   is_oop  - true => oop array, so generate store check code
  1962   //   name    - stub name string
  1963   //
  1964   // Inputs:
  1965   //   c_rarg0   - source array address
  1966   //   c_rarg1   - destination array address
  1967   //   c_rarg2   - element count, treated as ssize_t, can be zero
  1968   //
  1969  // Side Effects:
  1970   //   disjoint_oop_copy_entry or disjoint_long_copy_entry is set to the
  1971   //   no-overlap entry point used by generate_conjoint_long_oop_copy().
  1972   //
  1973   address generate_disjoint_long_oop_copy(bool aligned, bool is_oop, address *entry,
  1974                                           const char *name, bool dest_uninitialized = false) {
  1975     __ align(CodeEntryAlignment);
  1976     StubCodeMark mark(this, "StubRoutines", name);
  1977     address start = __ pc();
  1979     Label L_copy_32_bytes, L_copy_8_bytes, L_exit;
  1980     const Register from        = rdi;  // source array address
  1981     const Register to          = rsi;  // destination array address
  1982     const Register qword_count = rdx;  // elements count
  1983     const Register end_from    = from; // source array end address
  1984     const Register end_to      = rcx;  // destination array end address
  1985     const Register saved_to    = to;
  1986     // End pointers are inclusive, and if count is not zero they point
  1987     // to the last unit copied:  end_to[0] := end_from[0]
  1989     __ enter(); // required for proper stackwalking of RuntimeStub frame
  1990     // Save no-overlap entry point for generate_conjoint_long_oop_copy()
  1991     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
  1993     if (entry != NULL) {
  1994       *entry = __ pc();
  1995       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
  1996       BLOCK_COMMENT("Entry:");
  1999     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
  2000                       // r9 and r10 may be used to save non-volatile registers
  2001     // 'from', 'to' and 'qword_count' are now valid
  2002     if (is_oop) {
  2003       // no registers are destroyed by this call
  2004       gen_write_ref_array_pre_barrier(to, qword_count, dest_uninitialized);
  2007     // Copy from low to high addresses.  Use 'to' as scratch.
  2008     __ lea(end_from, Address(from, qword_count, Address::times_8, -8));
  2009     __ lea(end_to,   Address(to,   qword_count, Address::times_8, -8));
  2010     __ negptr(qword_count);
  2011     __ jmp(L_copy_32_bytes);
  2013     // Copy trailing qwords
  2014   __ BIND(L_copy_8_bytes);
  2015     __ movq(rax, Address(end_from, qword_count, Address::times_8, 8));
  2016     __ movq(Address(end_to, qword_count, Address::times_8, 8), rax);
  2017     __ increment(qword_count);
  2018     __ jcc(Assembler::notZero, L_copy_8_bytes);
  2020     if (is_oop) {
  2021       __ jmp(L_exit);
  2022     } else {
  2023       restore_arg_regs();
  2024       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
  2025       __ xorptr(rax, rax); // return 0
  2026       __ leave(); // required for proper stackwalking of RuntimeStub frame
  2027       __ ret(0);
  2030     // Copy 64-byte chunks
  2031     copy_32_bytes_forward(end_from, end_to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
  2033     if (is_oop) {
  2034     __ BIND(L_exit);
  2035       gen_write_ref_array_post_barrier(saved_to, end_to, rax);
  2037     restore_arg_regs();
  2038     if (is_oop) {
  2039       inc_counter_np(SharedRuntime::_oop_array_copy_ctr); // Update counter after rscratch1 is free
  2040     } else {
  2041       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
  2043     __ xorptr(rax, rax); // return 0
  2044     __ leave(); // required for proper stackwalking of RuntimeStub frame
  2045     __ ret(0);
  2047     return start;
  2050   // Arguments:
  2051   //   aligned - true => Input and output aligned on a HeapWord boundary == 8 bytes
  2052   //             ignored
  2053   //   is_oop  - true => oop array, so generate store check code
  2054   //   name    - stub name string
  2055   //
  2056   // Inputs:
  2057   //   c_rarg0   - source array address
  2058   //   c_rarg1   - destination array address
  2059   //   c_rarg2   - element count, treated as ssize_t, can be zero
  2060   //
  2061   address generate_conjoint_long_oop_copy(bool aligned, bool is_oop,
  2062                                           address nooverlap_target, address *entry,
  2063                                           const char *name, bool dest_uninitialized = false) {
  2064     __ align(CodeEntryAlignment);
  2065     StubCodeMark mark(this, "StubRoutines", name);
  2066     address start = __ pc();
  2068     Label L_copy_32_bytes, L_copy_8_bytes, L_exit;
  2069     const Register from        = rdi;  // source array address
  2070     const Register to          = rsi;  // destination array address
  2071     const Register qword_count = rdx;  // elements count
  2072     const Register saved_count = rcx;
  2074     __ enter(); // required for proper stackwalking of RuntimeStub frame
  2075     assert_clean_int(c_rarg2, rax);    // Make sure 'count' is clean int.
  2077     if (entry != NULL) {
  2078       *entry = __ pc();
  2079       // caller can pass a 64-bit byte count here (from Unsafe.copyMemory)
  2080       BLOCK_COMMENT("Entry:");
  2083     array_overlap_test(nooverlap_target, Address::times_8);
  2084     setup_arg_regs(); // from => rdi, to => rsi, count => rdx
  2085                       // r9 and r10 may be used to save non-volatile registers
  2086     // 'from', 'to' and 'qword_count' are now valid
  2087     if (is_oop) {
  2088       // Save to and count for store barrier
  2089       __ movptr(saved_count, qword_count);
  2090       // No registers are destroyed by this call
  2091       gen_write_ref_array_pre_barrier(to, saved_count, dest_uninitialized);
  2094     __ jmp(L_copy_32_bytes);
  2096     // Copy trailing qwords
  2097   __ BIND(L_copy_8_bytes);
  2098     __ movq(rax, Address(from, qword_count, Address::times_8, -8));
  2099     __ movq(Address(to, qword_count, Address::times_8, -8), rax);
  2100     __ decrement(qword_count);
  2101     __ jcc(Assembler::notZero, L_copy_8_bytes);
  2103     if (is_oop) {
  2104       __ jmp(L_exit);
  2105     } else {
  2106       restore_arg_regs();
  2107       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
  2108       __ xorptr(rax, rax); // return 0
  2109       __ leave(); // required for proper stackwalking of RuntimeStub frame
  2110       __ ret(0);
  2113     // Copy in 32-bytes chunks
  2114     copy_32_bytes_backward(from, to, qword_count, rax, L_copy_32_bytes, L_copy_8_bytes);
  2116     if (is_oop) {
  2117     __ BIND(L_exit);
  2118       __ lea(rcx, Address(to, saved_count, Address::times_8, -8));
  2119       gen_write_ref_array_post_barrier(to, rcx, rax);
  2121     restore_arg_regs();
  2122     if (is_oop) {
  2123       inc_counter_np(SharedRuntime::_oop_array_copy_ctr); // Update counter after rscratch1 is free
  2124     } else {
  2125       inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); // Update counter after rscratch1 is free
  2127     __ xorptr(rax, rax); // return 0
  2128     __ leave(); // required for proper stackwalking of RuntimeStub frame
  2129     __ ret(0);
  2131     return start;
  2135   // Helper for generating a dynamic type check.
  2136   // Smashes no registers.
  2137   void generate_type_check(Register sub_klass,
  2138                            Register super_check_offset,
  2139                            Register super_klass,
  2140                            Label& L_success) {
  2141     assert_different_registers(sub_klass, super_check_offset, super_klass);
  2143     BLOCK_COMMENT("type_check:");
  2145     Label L_miss;
  2147     __ check_klass_subtype_fast_path(sub_klass, super_klass, noreg,        &L_success, &L_miss, NULL,
  2148                                      super_check_offset);
  2149     __ check_klass_subtype_slow_path(sub_klass, super_klass, noreg, noreg, &L_success, NULL);
  2151     // Fall through on failure!
  2152     __ BIND(L_miss);
  2155   //
  2156   //  Generate checkcasting array copy stub
  2157   //
  2158   //  Input:
  2159   //    c_rarg0   - source array address
  2160   //    c_rarg1   - destination array address
  2161   //    c_rarg2   - element count, treated as ssize_t, can be zero
  2162   //    c_rarg3   - size_t ckoff (super_check_offset)
  2163   // not Win64
  2164   //    c_rarg4   - oop ckval (super_klass)
  2165   // Win64
  2166   //    rsp+40    - oop ckval (super_klass)
  2167   //
  2168   //  Output:
  2169   //    rax ==  0  -  success
  2170   //    rax == -1^K - failure, where K is partial transfer count
  2171   //
  2172   address generate_checkcast_copy(const char *name, address *entry,
  2173                                   bool dest_uninitialized = false) {
  2175     Label L_load_element, L_store_element, L_do_card_marks, L_done;
  2177     // Input registers (after setup_arg_regs)
  2178     const Register from        = rdi;   // source array address
  2179     const Register to          = rsi;   // destination array address
  2180     const Register length      = rdx;   // elements count
  2181     const Register ckoff       = rcx;   // super_check_offset
  2182     const Register ckval       = r8;    // super_klass
  2184     // Registers used as temps (r13, r14 are save-on-entry)
  2185     const Register end_from    = from;  // source array end address
  2186     const Register end_to      = r13;   // destination array end address
  2187     const Register count       = rdx;   // -(count_remaining)
  2188     const Register r14_length  = r14;   // saved copy of length
  2189     // End pointers are inclusive, and if length is not zero they point
  2190     // to the last unit copied:  end_to[0] := end_from[0]
  2192     const Register rax_oop    = rax;    // actual oop copied
  2193     const Register r11_klass  = r11;    // oop._klass
  2195     //---------------------------------------------------------------
  2196     // Assembler stub will be used for this call to arraycopy
  2197     // if the two arrays are subtypes of Object[] but the
  2198     // destination array type is not equal to or a supertype
  2199     // of the source type.  Each element must be separately
  2200     // checked.
  2202     __ align(CodeEntryAlignment);
  2203     StubCodeMark mark(this, "StubRoutines", name);
  2204     address start = __ pc();
  2206     __ enter(); // required for proper stackwalking of RuntimeStub frame
  2208 #ifdef ASSERT
  2209     // caller guarantees that the arrays really are different
  2210     // otherwise, we would have to make conjoint checks
  2211     { Label L;
  2212       array_overlap_test(L, TIMES_OOP);
  2213       __ stop("checkcast_copy within a single array");
  2214       __ bind(L);
  2216 #endif //ASSERT
  2218     setup_arg_regs(4); // from => rdi, to => rsi, length => rdx
  2219                        // ckoff => rcx, ckval => r8
  2220                        // r9 and r10 may be used to save non-volatile registers
  2221 #ifdef _WIN64
  2222     // last argument (#4) is on stack on Win64
  2223     __ movptr(ckval, Address(rsp, 6 * wordSize));
  2224 #endif
  2226     // Caller of this entry point must set up the argument registers.
  2227     if (entry != NULL) {
  2228       *entry = __ pc();
  2229       BLOCK_COMMENT("Entry:");
  2232     // allocate spill slots for r13, r14
  2233     enum {
  2234       saved_r13_offset,
  2235       saved_r14_offset,
  2236       saved_rbp_offset
  2237     };
  2238     __ subptr(rsp, saved_rbp_offset * wordSize);
  2239     __ movptr(Address(rsp, saved_r13_offset * wordSize), r13);
  2240     __ movptr(Address(rsp, saved_r14_offset * wordSize), r14);
  2242     // check that int operands are properly extended to size_t
  2243     assert_clean_int(length, rax);
  2244     assert_clean_int(ckoff, rax);
  2246 #ifdef ASSERT
  2247     BLOCK_COMMENT("assert consistent ckoff/ckval");
  2248     // The ckoff and ckval must be mutually consistent,
  2249     // even though caller generates both.
  2250     { Label L;
  2251       int sco_offset = in_bytes(Klass::super_check_offset_offset());
  2252       __ cmpl(ckoff, Address(ckval, sco_offset));
  2253       __ jcc(Assembler::equal, L);
  2254       __ stop("super_check_offset inconsistent");
  2255       __ bind(L);
  2257 #endif //ASSERT
  2259     // Loop-invariant addresses.  They are exclusive end pointers.
  2260     Address end_from_addr(from, length, TIMES_OOP, 0);
  2261     Address   end_to_addr(to,   length, TIMES_OOP, 0);
  2262     // Loop-variant addresses.  They assume post-incremented count < 0.
  2263     Address from_element_addr(end_from, count, TIMES_OOP, 0);
  2264     Address   to_element_addr(end_to,   count, TIMES_OOP, 0);
  2266     gen_write_ref_array_pre_barrier(to, count, dest_uninitialized);
  2268     // Copy from low to high addresses, indexed from the end of each array.
  2269     __ lea(end_from, end_from_addr);
  2270     __ lea(end_to,   end_to_addr);
  2271     __ movptr(r14_length, length);        // save a copy of the length
  2272     assert(length == count, "");          // else fix next line:
  2273     __ negptr(count);                     // negate and test the length
  2274     __ jcc(Assembler::notZero, L_load_element);
  2276     // Empty array:  Nothing to do.
  2277     __ xorptr(rax, rax);                  // return 0 on (trivial) success
  2278     __ jmp(L_done);
  2280     // ======== begin loop ========
  2281     // (Loop is rotated; its entry is L_load_element.)
  2282     // Loop control:
  2283     //   for (count = -count; count != 0; count++)
  2284     // Base pointers src, dst are biased by 8*(count-1),to last element.
  2285     __ align(OptoLoopAlignment);
  2287     __ BIND(L_store_element);
  2288     __ store_heap_oop(to_element_addr, rax_oop);  // store the oop
  2289     __ increment(count);               // increment the count toward zero
  2290     __ jcc(Assembler::zero, L_do_card_marks);
  2292     // ======== loop entry is here ========
  2293     __ BIND(L_load_element);
  2294     __ load_heap_oop(rax_oop, from_element_addr); // load the oop
  2295     __ testptr(rax_oop, rax_oop);
  2296     __ jcc(Assembler::zero, L_store_element);
  2298     __ load_klass(r11_klass, rax_oop);// query the object klass
  2299     generate_type_check(r11_klass, ckoff, ckval, L_store_element);
  2300     // ======== end loop ========
  2302     // It was a real error; we must depend on the caller to finish the job.
  2303     // Register rdx = -1 * number of *remaining* oops, r14 = *total* oops.
  2304     // Emit GC store barriers for the oops we have copied (r14 + rdx),
  2305     // and report their number to the caller.
  2306     assert_different_registers(rax, r14_length, count, to, end_to, rcx);
  2307     __ lea(end_to, to_element_addr);
  2308     __ addptr(end_to, -heapOopSize);      // make an inclusive end pointer
  2309     gen_write_ref_array_post_barrier(to, end_to, rscratch1);
  2310     __ movptr(rax, r14_length);           // original oops
  2311     __ addptr(rax, count);                // K = (original - remaining) oops
  2312     __ notptr(rax);                       // report (-1^K) to caller
  2313     __ jmp(L_done);
  2315     // Come here on success only.
  2316     __ BIND(L_do_card_marks);
  2317     __ addptr(end_to, -heapOopSize);         // make an inclusive end pointer
  2318     gen_write_ref_array_post_barrier(to, end_to, rscratch1);
  2319     __ xorptr(rax, rax);                  // return 0 on success
  2321     // Common exit point (success or failure).
  2322     __ BIND(L_done);
  2323     __ movptr(r13, Address(rsp, saved_r13_offset * wordSize));
  2324     __ movptr(r14, Address(rsp, saved_r14_offset * wordSize));
  2325     restore_arg_regs();
  2326     inc_counter_np(SharedRuntime::_checkcast_array_copy_ctr); // Update counter after rscratch1 is free
  2327     __ leave(); // required for proper stackwalking of RuntimeStub frame
  2328     __ ret(0);
  2330     return start;
  2333   //
  2334   //  Generate 'unsafe' array copy stub
  2335   //  Though just as safe as the other stubs, it takes an unscaled
  2336   //  size_t argument instead of an element count.
  2337   //
  2338   //  Input:
  2339   //    c_rarg0   - source array address
  2340   //    c_rarg1   - destination array address
  2341   //    c_rarg2   - byte count, treated as ssize_t, can be zero
  2342   //
  2343   // Examines the alignment of the operands and dispatches
  2344   // to a long, int, short, or byte copy loop.
  2345   //
  2346   address generate_unsafe_copy(const char *name,
  2347                                address byte_copy_entry, address short_copy_entry,
  2348                                address int_copy_entry, address long_copy_entry) {
  2350     Label L_long_aligned, L_int_aligned, L_short_aligned;
  2352     // Input registers (before setup_arg_regs)
  2353     const Register from        = c_rarg0;  // source array address
  2354     const Register to          = c_rarg1;  // destination array address
  2355     const Register size        = c_rarg2;  // byte count (size_t)
  2357     // Register used as a temp
  2358     const Register bits        = rax;      // test copy of low bits
  2360     __ align(CodeEntryAlignment);
  2361     StubCodeMark mark(this, "StubRoutines", name);
  2362     address start = __ pc();
  2364     __ enter(); // required for proper stackwalking of RuntimeStub frame
  2366     // bump this on entry, not on exit:
  2367     inc_counter_np(SharedRuntime::_unsafe_array_copy_ctr);
  2369     __ mov(bits, from);
  2370     __ orptr(bits, to);
  2371     __ orptr(bits, size);
  2373     __ testb(bits, BytesPerLong-1);
  2374     __ jccb(Assembler::zero, L_long_aligned);
  2376     __ testb(bits, BytesPerInt-1);
  2377     __ jccb(Assembler::zero, L_int_aligned);
  2379     __ testb(bits, BytesPerShort-1);
  2380     __ jump_cc(Assembler::notZero, RuntimeAddress(byte_copy_entry));
  2382     __ BIND(L_short_aligned);
  2383     __ shrptr(size, LogBytesPerShort); // size => short_count
  2384     __ jump(RuntimeAddress(short_copy_entry));
  2386     __ BIND(L_int_aligned);
  2387     __ shrptr(size, LogBytesPerInt); // size => int_count
  2388     __ jump(RuntimeAddress(int_copy_entry));
  2390     __ BIND(L_long_aligned);
  2391     __ shrptr(size, LogBytesPerLong); // size => qword_count
  2392     __ jump(RuntimeAddress(long_copy_entry));
  2394     return start;
  2397   // Perform range checks on the proposed arraycopy.
  2398   // Kills temp, but nothing else.
  2399   // Also, clean the sign bits of src_pos and dst_pos.
  2400   void arraycopy_range_checks(Register src,     // source array oop (c_rarg0)
  2401                               Register src_pos, // source position (c_rarg1)
  2402                               Register dst,     // destination array oo (c_rarg2)
  2403                               Register dst_pos, // destination position (c_rarg3)
  2404                               Register length,
  2405                               Register temp,
  2406                               Label& L_failed) {
  2407     BLOCK_COMMENT("arraycopy_range_checks:");
  2409     //  if (src_pos + length > arrayOop(src)->length())  FAIL;
  2410     __ movl(temp, length);
  2411     __ addl(temp, src_pos);             // src_pos + length
  2412     __ cmpl(temp, Address(src, arrayOopDesc::length_offset_in_bytes()));
  2413     __ jcc(Assembler::above, L_failed);
  2415     //  if (dst_pos + length > arrayOop(dst)->length())  FAIL;
  2416     __ movl(temp, length);
  2417     __ addl(temp, dst_pos);             // dst_pos + length
  2418     __ cmpl(temp, Address(dst, arrayOopDesc::length_offset_in_bytes()));
  2419     __ jcc(Assembler::above, L_failed);
  2421     // Have to clean up high 32-bits of 'src_pos' and 'dst_pos'.
  2422     // Move with sign extension can be used since they are positive.
  2423     __ movslq(src_pos, src_pos);
  2424     __ movslq(dst_pos, dst_pos);
  2426     BLOCK_COMMENT("arraycopy_range_checks done");
  2429   //
  2430   //  Generate generic array copy stubs
  2431   //
  2432   //  Input:
  2433   //    c_rarg0    -  src oop
  2434   //    c_rarg1    -  src_pos (32-bits)
  2435   //    c_rarg2    -  dst oop
  2436   //    c_rarg3    -  dst_pos (32-bits)
  2437   // not Win64
  2438   //    c_rarg4    -  element count (32-bits)
  2439   // Win64
  2440   //    rsp+40     -  element count (32-bits)
  2441   //
  2442   //  Output:
  2443   //    rax ==  0  -  success
  2444   //    rax == -1^K - failure, where K is partial transfer count
  2445   //
  2446   address generate_generic_copy(const char *name,
  2447                                 address byte_copy_entry, address short_copy_entry,
  2448                                 address int_copy_entry, address oop_copy_entry,
  2449                                 address long_copy_entry, address checkcast_copy_entry) {
  2451     Label L_failed, L_failed_0, L_objArray;
  2452     Label L_copy_bytes, L_copy_shorts, L_copy_ints, L_copy_longs;
  2454     // Input registers
  2455     const Register src        = c_rarg0;  // source array oop
  2456     const Register src_pos    = c_rarg1;  // source position
  2457     const Register dst        = c_rarg2;  // destination array oop
  2458     const Register dst_pos    = c_rarg3;  // destination position
  2459 #ifndef _WIN64
  2460     const Register length     = c_rarg4;
  2461 #else
  2462     const Address  length(rsp, 6 * wordSize);  // elements count is on stack on Win64
  2463 #endif
  2465     { int modulus = CodeEntryAlignment;
  2466       int target  = modulus - 5; // 5 = sizeof jmp(L_failed)
  2467       int advance = target - (__ offset() % modulus);
  2468       if (advance < 0)  advance += modulus;
  2469       if (advance > 0)  __ nop(advance);
  2471     StubCodeMark mark(this, "StubRoutines", name);
  2473     // Short-hop target to L_failed.  Makes for denser prologue code.
  2474     __ BIND(L_failed_0);
  2475     __ jmp(L_failed);
  2476     assert(__ offset() % CodeEntryAlignment == 0, "no further alignment needed");
  2478     __ align(CodeEntryAlignment);
  2479     address start = __ pc();
  2481     __ enter(); // required for proper stackwalking of RuntimeStub frame
  2483     // bump this on entry, not on exit:
  2484     inc_counter_np(SharedRuntime::_generic_array_copy_ctr);
  2486     //-----------------------------------------------------------------------
  2487     // Assembler stub will be used for this call to arraycopy
  2488     // if the following conditions are met:
  2489     //
  2490     // (1) src and dst must not be null.
  2491     // (2) src_pos must not be negative.
  2492     // (3) dst_pos must not be negative.
  2493     // (4) length  must not be negative.
  2494     // (5) src klass and dst klass should be the same and not NULL.
  2495     // (6) src and dst should be arrays.
  2496     // (7) src_pos + length must not exceed length of src.
  2497     // (8) dst_pos + length must not exceed length of dst.
  2498     //
  2500     //  if (src == NULL) return -1;
  2501     __ testptr(src, src);         // src oop
  2502     size_t j1off = __ offset();
  2503     __ jccb(Assembler::zero, L_failed_0);
  2505     //  if (src_pos < 0) return -1;
  2506     __ testl(src_pos, src_pos); // src_pos (32-bits)
  2507     __ jccb(Assembler::negative, L_failed_0);
  2509     //  if (dst == NULL) return -1;
  2510     __ testptr(dst, dst);         // dst oop
  2511     __ jccb(Assembler::zero, L_failed_0);
  2513     //  if (dst_pos < 0) return -1;
  2514     __ testl(dst_pos, dst_pos); // dst_pos (32-bits)
  2515     size_t j4off = __ offset();
  2516     __ jccb(Assembler::negative, L_failed_0);
  2518     // The first four tests are very dense code,
  2519     // but not quite dense enough to put four
  2520     // jumps in a 16-byte instruction fetch buffer.
  2521     // That's good, because some branch predicters
  2522     // do not like jumps so close together.
  2523     // Make sure of this.
  2524     guarantee(((j1off ^ j4off) & ~15) != 0, "I$ line of 1st & 4th jumps");
  2526     // registers used as temp
  2527     const Register r11_length    = r11; // elements count to copy
  2528     const Register r10_src_klass = r10; // array klass
  2530     //  if (length < 0) return -1;
  2531     __ movl(r11_length, length);        // length (elements count, 32-bits value)
  2532     __ testl(r11_length, r11_length);
  2533     __ jccb(Assembler::negative, L_failed_0);
  2535     __ load_klass(r10_src_klass, src);
  2536 #ifdef ASSERT
  2537     //  assert(src->klass() != NULL);
  2539       BLOCK_COMMENT("assert klasses not null {");
  2540       Label L1, L2;
  2541       __ testptr(r10_src_klass, r10_src_klass);
  2542       __ jcc(Assembler::notZero, L2);   // it is broken if klass is NULL
  2543       __ bind(L1);
  2544       __ stop("broken null klass");
  2545       __ bind(L2);
  2546       __ load_klass(rax, dst);
  2547       __ cmpq(rax, 0);
  2548       __ jcc(Assembler::equal, L1);     // this would be broken also
  2549       BLOCK_COMMENT("} assert klasses not null done");
  2551 #endif
  2553     // Load layout helper (32-bits)
  2554     //
  2555     //  |array_tag|     | header_size | element_type |     |log2_element_size|
  2556     // 32        30    24            16              8     2                 0
  2557     //
  2558     //   array_tag: typeArray = 0x3, objArray = 0x2, non-array = 0x0
  2559     //
  2561     const int lh_offset = in_bytes(Klass::layout_helper_offset());
  2563     // Handle objArrays completely differently...
  2564     const jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
  2565     __ cmpl(Address(r10_src_klass, lh_offset), objArray_lh);
  2566     __ jcc(Assembler::equal, L_objArray);
  2568     //  if (src->klass() != dst->klass()) return -1;
  2569     __ load_klass(rax, dst);
  2570     __ cmpq(r10_src_klass, rax);
  2571     __ jcc(Assembler::notEqual, L_failed);
  2573     const Register rax_lh = rax;  // layout helper
  2574     __ movl(rax_lh, Address(r10_src_klass, lh_offset));
  2576     //  if (!src->is_Array()) return -1;
  2577     __ cmpl(rax_lh, Klass::_lh_neutral_value);
  2578     __ jcc(Assembler::greaterEqual, L_failed);
  2580     // At this point, it is known to be a typeArray (array_tag 0x3).
  2581 #ifdef ASSERT
  2583       BLOCK_COMMENT("assert primitive array {");
  2584       Label L;
  2585       __ cmpl(rax_lh, (Klass::_lh_array_tag_type_value << Klass::_lh_array_tag_shift));
  2586       __ jcc(Assembler::greaterEqual, L);
  2587       __ stop("must be a primitive array");
  2588       __ bind(L);
  2589       BLOCK_COMMENT("} assert primitive array done");
  2591 #endif
  2593     arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
  2594                            r10, L_failed);
  2596     // TypeArrayKlass
  2597     //
  2598     // src_addr = (src + array_header_in_bytes()) + (src_pos << log2elemsize);
  2599     // dst_addr = (dst + array_header_in_bytes()) + (dst_pos << log2elemsize);
  2600     //
  2602     const Register r10_offset = r10;    // array offset
  2603     const Register rax_elsize = rax_lh; // element size
  2605     __ movl(r10_offset, rax_lh);
  2606     __ shrl(r10_offset, Klass::_lh_header_size_shift);
  2607     __ andptr(r10_offset, Klass::_lh_header_size_mask);   // array_offset
  2608     __ addptr(src, r10_offset);           // src array offset
  2609     __ addptr(dst, r10_offset);           // dst array offset
  2610     BLOCK_COMMENT("choose copy loop based on element size");
  2611     __ andl(rax_lh, Klass::_lh_log2_element_size_mask); // rax_lh -> rax_elsize
  2613     // next registers should be set before the jump to corresponding stub
  2614     const Register from     = c_rarg0;  // source array address
  2615     const Register to       = c_rarg1;  // destination array address
  2616     const Register count    = c_rarg2;  // elements count
  2618     // 'from', 'to', 'count' registers should be set in such order
  2619     // since they are the same as 'src', 'src_pos', 'dst'.
  2621   __ BIND(L_copy_bytes);
  2622     __ cmpl(rax_elsize, 0);
  2623     __ jccb(Assembler::notEqual, L_copy_shorts);
  2624     __ lea(from, Address(src, src_pos, Address::times_1, 0));// src_addr
  2625     __ lea(to,   Address(dst, dst_pos, Address::times_1, 0));// dst_addr
  2626     __ movl2ptr(count, r11_length); // length
  2627     __ jump(RuntimeAddress(byte_copy_entry));
  2629   __ BIND(L_copy_shorts);
  2630     __ cmpl(rax_elsize, LogBytesPerShort);
  2631     __ jccb(Assembler::notEqual, L_copy_ints);
  2632     __ lea(from, Address(src, src_pos, Address::times_2, 0));// src_addr
  2633     __ lea(to,   Address(dst, dst_pos, Address::times_2, 0));// dst_addr
  2634     __ movl2ptr(count, r11_length); // length
  2635     __ jump(RuntimeAddress(short_copy_entry));
  2637   __ BIND(L_copy_ints);
  2638     __ cmpl(rax_elsize, LogBytesPerInt);
  2639     __ jccb(Assembler::notEqual, L_copy_longs);
  2640     __ lea(from, Address(src, src_pos, Address::times_4, 0));// src_addr
  2641     __ lea(to,   Address(dst, dst_pos, Address::times_4, 0));// dst_addr
  2642     __ movl2ptr(count, r11_length); // length
  2643     __ jump(RuntimeAddress(int_copy_entry));
  2645   __ BIND(L_copy_longs);
  2646 #ifdef ASSERT
  2648       BLOCK_COMMENT("assert long copy {");
  2649       Label L;
  2650       __ cmpl(rax_elsize, LogBytesPerLong);
  2651       __ jcc(Assembler::equal, L);
  2652       __ stop("must be long copy, but elsize is wrong");
  2653       __ bind(L);
  2654       BLOCK_COMMENT("} assert long copy done");
  2656 #endif
  2657     __ lea(from, Address(src, src_pos, Address::times_8, 0));// src_addr
  2658     __ lea(to,   Address(dst, dst_pos, Address::times_8, 0));// dst_addr
  2659     __ movl2ptr(count, r11_length); // length
  2660     __ jump(RuntimeAddress(long_copy_entry));
  2662     // ObjArrayKlass
  2663   __ BIND(L_objArray);
  2664     // live at this point:  r10_src_klass, r11_length, src[_pos], dst[_pos]
  2666     Label L_plain_copy, L_checkcast_copy;
  2667     //  test array classes for subtyping
  2668     __ load_klass(rax, dst);
  2669     __ cmpq(r10_src_klass, rax); // usual case is exact equality
  2670     __ jcc(Assembler::notEqual, L_checkcast_copy);
  2672     // Identically typed arrays can be copied without element-wise checks.
  2673     arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
  2674                            r10, L_failed);
  2676     __ lea(from, Address(src, src_pos, TIMES_OOP,
  2677                  arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // src_addr
  2678     __ lea(to,   Address(dst, dst_pos, TIMES_OOP,
  2679                  arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // dst_addr
  2680     __ movl2ptr(count, r11_length); // length
  2681   __ BIND(L_plain_copy);
  2682     __ jump(RuntimeAddress(oop_copy_entry));
  2684   __ BIND(L_checkcast_copy);
  2685     // live at this point:  r10_src_klass, r11_length, rax (dst_klass)
  2687       // Before looking at dst.length, make sure dst is also an objArray.
  2688       __ cmpl(Address(rax, lh_offset), objArray_lh);
  2689       __ jcc(Assembler::notEqual, L_failed);
  2691       // It is safe to examine both src.length and dst.length.
  2692       arraycopy_range_checks(src, src_pos, dst, dst_pos, r11_length,
  2693                              rax, L_failed);
  2695       const Register r11_dst_klass = r11;
  2696       __ load_klass(r11_dst_klass, dst); // reload
  2698       // Marshal the base address arguments now, freeing registers.
  2699       __ lea(from, Address(src, src_pos, TIMES_OOP,
  2700                    arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
  2701       __ lea(to,   Address(dst, dst_pos, TIMES_OOP,
  2702                    arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
  2703       __ movl(count, length);           // length (reloaded)
  2704       Register sco_temp = c_rarg3;      // this register is free now
  2705       assert_different_registers(from, to, count, sco_temp,
  2706                                  r11_dst_klass, r10_src_klass);
  2707       assert_clean_int(count, sco_temp);
  2709       // Generate the type check.
  2710       const int sco_offset = in_bytes(Klass::super_check_offset_offset());
  2711       __ movl(sco_temp, Address(r11_dst_klass, sco_offset));
  2712       assert_clean_int(sco_temp, rax);
  2713       generate_type_check(r10_src_klass, sco_temp, r11_dst_klass, L_plain_copy);
  2715       // Fetch destination element klass from the ObjArrayKlass header.
  2716       int ek_offset = in_bytes(ObjArrayKlass::element_klass_offset());
  2717       __ movptr(r11_dst_klass, Address(r11_dst_klass, ek_offset));
  2718       __ movl(  sco_temp,      Address(r11_dst_klass, sco_offset));
  2719       assert_clean_int(sco_temp, rax);
  2721       // the checkcast_copy loop needs two extra arguments:
  2722       assert(c_rarg3 == sco_temp, "#3 already in place");
  2723       // Set up arguments for checkcast_copy_entry.
  2724       setup_arg_regs(4);
  2725       __ movptr(r8, r11_dst_klass);  // dst.klass.element_klass, r8 is c_rarg4 on Linux/Solaris
  2726       __ jump(RuntimeAddress(checkcast_copy_entry));
  2729   __ BIND(L_failed);
  2730     __ xorptr(rax, rax);
  2731     __ notptr(rax); // return -1
  2732     __ leave();   // required for proper stackwalking of RuntimeStub frame
  2733     __ ret(0);
  2735     return start;
  2738   void generate_arraycopy_stubs() {
  2739     address entry;
  2740     address entry_jbyte_arraycopy;
  2741     address entry_jshort_arraycopy;
  2742     address entry_jint_arraycopy;
  2743     address entry_oop_arraycopy;
  2744     address entry_jlong_arraycopy;
  2745     address entry_checkcast_arraycopy;
  2747     StubRoutines::_jbyte_disjoint_arraycopy  = generate_disjoint_byte_copy(false, &entry,
  2748                                                                            "jbyte_disjoint_arraycopy");
  2749     StubRoutines::_jbyte_arraycopy           = generate_conjoint_byte_copy(false, entry, &entry_jbyte_arraycopy,
  2750                                                                            "jbyte_arraycopy");
  2752     StubRoutines::_jshort_disjoint_arraycopy = generate_disjoint_short_copy(false, &entry,
  2753                                                                             "jshort_disjoint_arraycopy");
  2754     StubRoutines::_jshort_arraycopy          = generate_conjoint_short_copy(false, entry, &entry_jshort_arraycopy,
  2755                                                                             "jshort_arraycopy");
  2757     StubRoutines::_jint_disjoint_arraycopy   = generate_disjoint_int_oop_copy(false, false, &entry,
  2758                                                                               "jint_disjoint_arraycopy");
  2759     StubRoutines::_jint_arraycopy            = generate_conjoint_int_oop_copy(false, false, entry,
  2760                                                                               &entry_jint_arraycopy, "jint_arraycopy");
  2762     StubRoutines::_jlong_disjoint_arraycopy  = generate_disjoint_long_oop_copy(false, false, &entry,
  2763                                                                                "jlong_disjoint_arraycopy");
  2764     StubRoutines::_jlong_arraycopy           = generate_conjoint_long_oop_copy(false, false, entry,
  2765                                                                                &entry_jlong_arraycopy, "jlong_arraycopy");
  2768     if (UseCompressedOops) {
  2769       StubRoutines::_oop_disjoint_arraycopy  = generate_disjoint_int_oop_copy(false, true, &entry,
  2770                                                                               "oop_disjoint_arraycopy");
  2771       StubRoutines::_oop_arraycopy           = generate_conjoint_int_oop_copy(false, true, entry,
  2772                                                                               &entry_oop_arraycopy, "oop_arraycopy");
  2773       StubRoutines::_oop_disjoint_arraycopy_uninit  = generate_disjoint_int_oop_copy(false, true, &entry,
  2774                                                                                      "oop_disjoint_arraycopy_uninit",
  2775                                                                                      /*dest_uninitialized*/true);
  2776       StubRoutines::_oop_arraycopy_uninit           = generate_conjoint_int_oop_copy(false, true, entry,
  2777                                                                                      NULL, "oop_arraycopy_uninit",
  2778                                                                                      /*dest_uninitialized*/true);
  2779     } else {
  2780       StubRoutines::_oop_disjoint_arraycopy  = generate_disjoint_long_oop_copy(false, true, &entry,
  2781                                                                                "oop_disjoint_arraycopy");
  2782       StubRoutines::_oop_arraycopy           = generate_conjoint_long_oop_copy(false, true, entry,
  2783                                                                                &entry_oop_arraycopy, "oop_arraycopy");
  2784       StubRoutines::_oop_disjoint_arraycopy_uninit  = generate_disjoint_long_oop_copy(false, true, &entry,
  2785                                                                                       "oop_disjoint_arraycopy_uninit",
  2786                                                                                       /*dest_uninitialized*/true);
  2787       StubRoutines::_oop_arraycopy_uninit           = generate_conjoint_long_oop_copy(false, true, entry,
  2788                                                                                       NULL, "oop_arraycopy_uninit",
  2789                                                                                       /*dest_uninitialized*/true);
  2792     StubRoutines::_checkcast_arraycopy        = generate_checkcast_copy("checkcast_arraycopy", &entry_checkcast_arraycopy);
  2793     StubRoutines::_checkcast_arraycopy_uninit = generate_checkcast_copy("checkcast_arraycopy_uninit", NULL,
  2794                                                                         /*dest_uninitialized*/true);
  2796     StubRoutines::_unsafe_arraycopy    = generate_unsafe_copy("unsafe_arraycopy",
  2797                                                               entry_jbyte_arraycopy,
  2798                                                               entry_jshort_arraycopy,
  2799                                                               entry_jint_arraycopy,
  2800                                                               entry_jlong_arraycopy);
  2801     StubRoutines::_generic_arraycopy   = generate_generic_copy("generic_arraycopy",
  2802                                                                entry_jbyte_arraycopy,
  2803                                                                entry_jshort_arraycopy,
  2804                                                                entry_jint_arraycopy,
  2805                                                                entry_oop_arraycopy,
  2806                                                                entry_jlong_arraycopy,
  2807                                                                entry_checkcast_arraycopy);
  2809     StubRoutines::_jbyte_fill = generate_fill(T_BYTE, false, "jbyte_fill");
  2810     StubRoutines::_jshort_fill = generate_fill(T_SHORT, false, "jshort_fill");
  2811     StubRoutines::_jint_fill = generate_fill(T_INT, false, "jint_fill");
  2812     StubRoutines::_arrayof_jbyte_fill = generate_fill(T_BYTE, true, "arrayof_jbyte_fill");
  2813     StubRoutines::_arrayof_jshort_fill = generate_fill(T_SHORT, true, "arrayof_jshort_fill");
  2814     StubRoutines::_arrayof_jint_fill = generate_fill(T_INT, true, "arrayof_jint_fill");
  2816     // We don't generate specialized code for HeapWord-aligned source
  2817     // arrays, so just use the code we've already generated
  2818     StubRoutines::_arrayof_jbyte_disjoint_arraycopy  = StubRoutines::_jbyte_disjoint_arraycopy;
  2819     StubRoutines::_arrayof_jbyte_arraycopy           = StubRoutines::_jbyte_arraycopy;
  2821     StubRoutines::_arrayof_jshort_disjoint_arraycopy = StubRoutines::_jshort_disjoint_arraycopy;
  2822     StubRoutines::_arrayof_jshort_arraycopy          = StubRoutines::_jshort_arraycopy;
  2824     StubRoutines::_arrayof_jint_disjoint_arraycopy   = StubRoutines::_jint_disjoint_arraycopy;
  2825     StubRoutines::_arrayof_jint_arraycopy            = StubRoutines::_jint_arraycopy;
  2827     StubRoutines::_arrayof_jlong_disjoint_arraycopy  = StubRoutines::_jlong_disjoint_arraycopy;
  2828     StubRoutines::_arrayof_jlong_arraycopy           = StubRoutines::_jlong_arraycopy;
  2830     StubRoutines::_arrayof_oop_disjoint_arraycopy    = StubRoutines::_oop_disjoint_arraycopy;
  2831     StubRoutines::_arrayof_oop_arraycopy             = StubRoutines::_oop_arraycopy;
  2833     StubRoutines::_arrayof_oop_disjoint_arraycopy_uninit    = StubRoutines::_oop_disjoint_arraycopy_uninit;
  2834     StubRoutines::_arrayof_oop_arraycopy_uninit             = StubRoutines::_oop_arraycopy_uninit;
  2837   void generate_math_stubs() {
  2839       StubCodeMark mark(this, "StubRoutines", "log");
  2840       StubRoutines::_intrinsic_log = (double (*)(double)) __ pc();
  2842       __ subq(rsp, 8);
  2843       __ movdbl(Address(rsp, 0), xmm0);
  2844       __ fld_d(Address(rsp, 0));
  2845       __ flog();
  2846       __ fstp_d(Address(rsp, 0));
  2847       __ movdbl(xmm0, Address(rsp, 0));
  2848       __ addq(rsp, 8);
  2849       __ ret(0);
  2852       StubCodeMark mark(this, "StubRoutines", "log10");
  2853       StubRoutines::_intrinsic_log10 = (double (*)(double)) __ pc();
  2855       __ subq(rsp, 8);
  2856       __ movdbl(Address(rsp, 0), xmm0);
  2857       __ fld_d(Address(rsp, 0));
  2858       __ flog10();
  2859       __ fstp_d(Address(rsp, 0));
  2860       __ movdbl(xmm0, Address(rsp, 0));
  2861       __ addq(rsp, 8);
  2862       __ ret(0);
  2865       StubCodeMark mark(this, "StubRoutines", "sin");
  2866       StubRoutines::_intrinsic_sin = (double (*)(double)) __ pc();
  2868       __ subq(rsp, 8);
  2869       __ movdbl(Address(rsp, 0), xmm0);
  2870       __ fld_d(Address(rsp, 0));
  2871       __ trigfunc('s');
  2872       __ fstp_d(Address(rsp, 0));
  2873       __ movdbl(xmm0, Address(rsp, 0));
  2874       __ addq(rsp, 8);
  2875       __ ret(0);
  2878       StubCodeMark mark(this, "StubRoutines", "cos");
  2879       StubRoutines::_intrinsic_cos = (double (*)(double)) __ pc();
  2881       __ subq(rsp, 8);
  2882       __ movdbl(Address(rsp, 0), xmm0);
  2883       __ fld_d(Address(rsp, 0));
  2884       __ trigfunc('c');
  2885       __ fstp_d(Address(rsp, 0));
  2886       __ movdbl(xmm0, Address(rsp, 0));
  2887       __ addq(rsp, 8);
  2888       __ ret(0);
  2891       StubCodeMark mark(this, "StubRoutines", "tan");
  2892       StubRoutines::_intrinsic_tan = (double (*)(double)) __ pc();
  2894       __ subq(rsp, 8);
  2895       __ movdbl(Address(rsp, 0), xmm0);
  2896       __ fld_d(Address(rsp, 0));
  2897       __ trigfunc('t');
  2898       __ fstp_d(Address(rsp, 0));
  2899       __ movdbl(xmm0, Address(rsp, 0));
  2900       __ addq(rsp, 8);
  2901       __ ret(0);
  2904       StubCodeMark mark(this, "StubRoutines", "exp");
  2905       StubRoutines::_intrinsic_exp = (double (*)(double)) __ pc();
  2907       __ subq(rsp, 8);
  2908       __ movdbl(Address(rsp, 0), xmm0);
  2909       __ fld_d(Address(rsp, 0));
  2910       __ exp_with_fallback(0);
  2911       __ fstp_d(Address(rsp, 0));
  2912       __ movdbl(xmm0, Address(rsp, 0));
  2913       __ addq(rsp, 8);
  2914       __ ret(0);
  2917       StubCodeMark mark(this, "StubRoutines", "pow");
  2918       StubRoutines::_intrinsic_pow = (double (*)(double,double)) __ pc();
  2920       __ subq(rsp, 8);
  2921       __ movdbl(Address(rsp, 0), xmm1);
  2922       __ fld_d(Address(rsp, 0));
  2923       __ movdbl(Address(rsp, 0), xmm0);
  2924       __ fld_d(Address(rsp, 0));
  2925       __ pow_with_fallback(0);
  2926       __ fstp_d(Address(rsp, 0));
  2927       __ movdbl(xmm0, Address(rsp, 0));
  2928       __ addq(rsp, 8);
  2929       __ ret(0);
  2933   // AES intrinsic stubs
  2934   enum {AESBlockSize = 16};
  2936   address generate_key_shuffle_mask() {
  2937     __ align(16);
  2938     StubCodeMark mark(this, "StubRoutines", "key_shuffle_mask");
  2939     address start = __ pc();
  2940     __ emit_data64( 0x0405060700010203, relocInfo::none );
  2941     __ emit_data64( 0x0c0d0e0f08090a0b, relocInfo::none );
  2942     return start;
  2945   // Utility routine for loading a 128-bit key word in little endian format
  2946   // can optionally specify that the shuffle mask is already in an xmmregister
  2947   void load_key(XMMRegister xmmdst, Register key, int offset, XMMRegister xmm_shuf_mask=NULL) {
  2948     __ movdqu(xmmdst, Address(key, offset));
  2949     if (xmm_shuf_mask != NULL) {
  2950       __ pshufb(xmmdst, xmm_shuf_mask);
  2951     } else {
  2952       __ pshufb(xmmdst, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
  2956   // Arguments:
  2957   //
  2958   // Inputs:
  2959   //   c_rarg0   - source byte array address
  2960   //   c_rarg1   - destination byte array address
  2961   //   c_rarg2   - K (key) in little endian int array
  2962   //
  2963   address generate_aescrypt_encryptBlock() {
  2964     assert(UseAES, "need AES instructions and misaligned SSE support");
  2965     __ align(CodeEntryAlignment);
  2966     StubCodeMark mark(this, "StubRoutines", "aescrypt_encryptBlock");
  2967     Label L_doLast;
  2968     address start = __ pc();
  2970     const Register from        = c_rarg0;  // source array address
  2971     const Register to          = c_rarg1;  // destination array address
  2972     const Register key         = c_rarg2;  // key array address
  2973     const Register keylen      = rax;
  2975     const XMMRegister xmm_result = xmm0;
  2976     const XMMRegister xmm_key_shuf_mask = xmm1;
  2977     // On win64 xmm6-xmm15 must be preserved so don't use them.
  2978     const XMMRegister xmm_temp1  = xmm2;
  2979     const XMMRegister xmm_temp2  = xmm3;
  2980     const XMMRegister xmm_temp3  = xmm4;
  2981     const XMMRegister xmm_temp4  = xmm5;
  2983     __ enter(); // required for proper stackwalking of RuntimeStub frame
  2985     // keylen could be only {11, 13, 15} * 4 = {44, 52, 60}
  2986     __ movl(keylen, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
  2988     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
  2989     __ movdqu(xmm_result, Address(from, 0));  // get 16 bytes of input
  2991     // For encryption, the java expanded key ordering is just what we need
  2992     // we don't know if the key is aligned, hence not using load-execute form
  2994     load_key(xmm_temp1, key, 0x00, xmm_key_shuf_mask);
  2995     __ pxor(xmm_result, xmm_temp1);
  2997     load_key(xmm_temp1, key, 0x10, xmm_key_shuf_mask);
  2998     load_key(xmm_temp2, key, 0x20, xmm_key_shuf_mask);
  2999     load_key(xmm_temp3, key, 0x30, xmm_key_shuf_mask);
  3000     load_key(xmm_temp4, key, 0x40, xmm_key_shuf_mask);
  3002     __ aesenc(xmm_result, xmm_temp1);
  3003     __ aesenc(xmm_result, xmm_temp2);
  3004     __ aesenc(xmm_result, xmm_temp3);
  3005     __ aesenc(xmm_result, xmm_temp4);
  3007     load_key(xmm_temp1, key, 0x50, xmm_key_shuf_mask);
  3008     load_key(xmm_temp2, key, 0x60, xmm_key_shuf_mask);
  3009     load_key(xmm_temp3, key, 0x70, xmm_key_shuf_mask);
  3010     load_key(xmm_temp4, key, 0x80, xmm_key_shuf_mask);
  3012     __ aesenc(xmm_result, xmm_temp1);
  3013     __ aesenc(xmm_result, xmm_temp2);
  3014     __ aesenc(xmm_result, xmm_temp3);
  3015     __ aesenc(xmm_result, xmm_temp4);
  3017     load_key(xmm_temp1, key, 0x90, xmm_key_shuf_mask);
  3018     load_key(xmm_temp2, key, 0xa0, xmm_key_shuf_mask);
  3020     __ cmpl(keylen, 44);
  3021     __ jccb(Assembler::equal, L_doLast);
  3023     __ aesenc(xmm_result, xmm_temp1);
  3024     __ aesenc(xmm_result, xmm_temp2);
  3026     load_key(xmm_temp1, key, 0xb0, xmm_key_shuf_mask);
  3027     load_key(xmm_temp2, key, 0xc0, xmm_key_shuf_mask);
  3029     __ cmpl(keylen, 52);
  3030     __ jccb(Assembler::equal, L_doLast);
  3032     __ aesenc(xmm_result, xmm_temp1);
  3033     __ aesenc(xmm_result, xmm_temp2);
  3035     load_key(xmm_temp1, key, 0xd0, xmm_key_shuf_mask);
  3036     load_key(xmm_temp2, key, 0xe0, xmm_key_shuf_mask);
  3038     __ BIND(L_doLast);
  3039     __ aesenc(xmm_result, xmm_temp1);
  3040     __ aesenclast(xmm_result, xmm_temp2);
  3041     __ movdqu(Address(to, 0), xmm_result);        // store the result
  3042     __ xorptr(rax, rax); // return 0
  3043     __ leave(); // required for proper stackwalking of RuntimeStub frame
  3044     __ ret(0);
  3046     return start;
  3050   // Arguments:
  3051   //
  3052   // Inputs:
  3053   //   c_rarg0   - source byte array address
  3054   //   c_rarg1   - destination byte array address
  3055   //   c_rarg2   - K (key) in little endian int array
  3056   //
  3057   address generate_aescrypt_decryptBlock() {
  3058     assert(UseAES, "need AES instructions and misaligned SSE support");
  3059     __ align(CodeEntryAlignment);
  3060     StubCodeMark mark(this, "StubRoutines", "aescrypt_decryptBlock");
  3061     Label L_doLast;
  3062     address start = __ pc();
  3064     const Register from        = c_rarg0;  // source array address
  3065     const Register to          = c_rarg1;  // destination array address
  3066     const Register key         = c_rarg2;  // key array address
  3067     const Register keylen      = rax;
  3069     const XMMRegister xmm_result = xmm0;
  3070     const XMMRegister xmm_key_shuf_mask = xmm1;
  3071     // On win64 xmm6-xmm15 must be preserved so don't use them.
  3072     const XMMRegister xmm_temp1  = xmm2;
  3073     const XMMRegister xmm_temp2  = xmm3;
  3074     const XMMRegister xmm_temp3  = xmm4;
  3075     const XMMRegister xmm_temp4  = xmm5;
  3077     __ enter(); // required for proper stackwalking of RuntimeStub frame
  3079     // keylen could be only {11, 13, 15} * 4 = {44, 52, 60}
  3080     __ movl(keylen, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
  3082     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
  3083     __ movdqu(xmm_result, Address(from, 0));
  3085     // for decryption java expanded key ordering is rotated one position from what we want
  3086     // so we start from 0x10 here and hit 0x00 last
  3087     // we don't know if the key is aligned, hence not using load-execute form
  3088     load_key(xmm_temp1, key, 0x10, xmm_key_shuf_mask);
  3089     load_key(xmm_temp2, key, 0x20, xmm_key_shuf_mask);
  3090     load_key(xmm_temp3, key, 0x30, xmm_key_shuf_mask);
  3091     load_key(xmm_temp4, key, 0x40, xmm_key_shuf_mask);
  3093     __ pxor  (xmm_result, xmm_temp1);
  3094     __ aesdec(xmm_result, xmm_temp2);
  3095     __ aesdec(xmm_result, xmm_temp3);
  3096     __ aesdec(xmm_result, xmm_temp4);
  3098     load_key(xmm_temp1, key, 0x50, xmm_key_shuf_mask);
  3099     load_key(xmm_temp2, key, 0x60, xmm_key_shuf_mask);
  3100     load_key(xmm_temp3, key, 0x70, xmm_key_shuf_mask);
  3101     load_key(xmm_temp4, key, 0x80, xmm_key_shuf_mask);
  3103     __ aesdec(xmm_result, xmm_temp1);
  3104     __ aesdec(xmm_result, xmm_temp2);
  3105     __ aesdec(xmm_result, xmm_temp3);
  3106     __ aesdec(xmm_result, xmm_temp4);
  3108     load_key(xmm_temp1, key, 0x90, xmm_key_shuf_mask);
  3109     load_key(xmm_temp2, key, 0xa0, xmm_key_shuf_mask);
  3110     load_key(xmm_temp3, key, 0x00, xmm_key_shuf_mask);
  3112     __ cmpl(keylen, 44);
  3113     __ jccb(Assembler::equal, L_doLast);
  3115     __ aesdec(xmm_result, xmm_temp1);
  3116     __ aesdec(xmm_result, xmm_temp2);
  3118     load_key(xmm_temp1, key, 0xb0, xmm_key_shuf_mask);
  3119     load_key(xmm_temp2, key, 0xc0, xmm_key_shuf_mask);
  3121     __ cmpl(keylen, 52);
  3122     __ jccb(Assembler::equal, L_doLast);
  3124     __ aesdec(xmm_result, xmm_temp1);
  3125     __ aesdec(xmm_result, xmm_temp2);
  3127     load_key(xmm_temp1, key, 0xd0, xmm_key_shuf_mask);
  3128     load_key(xmm_temp2, key, 0xe0, xmm_key_shuf_mask);
  3130     __ BIND(L_doLast);
  3131     __ aesdec(xmm_result, xmm_temp1);
  3132     __ aesdec(xmm_result, xmm_temp2);
  3134     // for decryption the aesdeclast operation is always on key+0x00
  3135     __ aesdeclast(xmm_result, xmm_temp3);
  3136     __ movdqu(Address(to, 0), xmm_result);  // store the result
  3137     __ xorptr(rax, rax); // return 0
  3138     __ leave(); // required for proper stackwalking of RuntimeStub frame
  3139     __ ret(0);
  3141     return start;
  3145   // Arguments:
  3146   //
  3147   // Inputs:
  3148   //   c_rarg0   - source byte array address
  3149   //   c_rarg1   - destination byte array address
  3150   //   c_rarg2   - K (key) in little endian int array
  3151   //   c_rarg3   - r vector byte array address
  3152   //   c_rarg4   - input length
  3153   //
  3154   address generate_cipherBlockChaining_encryptAESCrypt() {
  3155     assert(UseAES, "need AES instructions and misaligned SSE support");
  3156     __ align(CodeEntryAlignment);
  3157     StubCodeMark mark(this, "StubRoutines", "cipherBlockChaining_encryptAESCrypt");
  3158     address start = __ pc();
  3160     Label L_exit, L_key_192_256, L_key_256, L_loopTop_128, L_loopTop_192, L_loopTop_256;
  3161     const Register from        = c_rarg0;  // source array address
  3162     const Register to          = c_rarg1;  // destination array address
  3163     const Register key         = c_rarg2;  // key array address
  3164     const Register rvec        = c_rarg3;  // r byte array initialized from initvector array address
  3165                                            // and left with the results of the last encryption block
  3166 #ifndef _WIN64
  3167     const Register len_reg     = c_rarg4;  // src len (must be multiple of blocksize 16)
  3168 #else
  3169     const Address  len_mem(rsp, 6 * wordSize);  // length is on stack on Win64
  3170     const Register len_reg     = r10;      // pick the first volatile windows register
  3171 #endif
  3172     const Register pos         = rax;
  3174     // xmm register assignments for the loops below
  3175     const XMMRegister xmm_result = xmm0;
  3176     const XMMRegister xmm_temp   = xmm1;
  3177     // keys 0-10 preloaded into xmm2-xmm12
  3178     const int XMM_REG_NUM_KEY_FIRST = 2;
  3179     const int XMM_REG_NUM_KEY_LAST  = 15;
  3180     const XMMRegister xmm_key0   = as_XMMRegister(XMM_REG_NUM_KEY_FIRST);
  3181     const XMMRegister xmm_key10  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+10);
  3182     const XMMRegister xmm_key11  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+11);
  3183     const XMMRegister xmm_key12  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+12);
  3184     const XMMRegister xmm_key13  = as_XMMRegister(XMM_REG_NUM_KEY_FIRST+13);
  3186     __ enter(); // required for proper stackwalking of RuntimeStub frame
  3188 #ifdef _WIN64
  3189     // on win64, fill len_reg from stack position
  3190     __ movl(len_reg, len_mem);
  3191     // save the xmm registers which must be preserved 6-15
  3192     __ subptr(rsp, -rsp_after_call_off * wordSize);
  3193     for (int i = 6; i <= XMM_REG_NUM_KEY_LAST; i++) {
  3194       __ movdqu(xmm_save(i), as_XMMRegister(i));
  3196 #endif
  3198     const XMMRegister xmm_key_shuf_mask = xmm_temp;  // used temporarily to swap key bytes up front
  3199     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
  3200     // load up xmm regs xmm2 thru xmm12 with key 0x00 - 0xa0
  3201     for (int rnum = XMM_REG_NUM_KEY_FIRST, offset = 0x00; rnum <= XMM_REG_NUM_KEY_FIRST+10; rnum++) {
  3202       load_key(as_XMMRegister(rnum), key, offset, xmm_key_shuf_mask);
  3203       offset += 0x10;
  3205     __ movdqu(xmm_result, Address(rvec, 0x00));   // initialize xmm_result with r vec
  3207     // now split to different paths depending on the keylen (len in ints of AESCrypt.KLE array (52=192, or 60=256))
  3208     __ movl(rax, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
  3209     __ cmpl(rax, 44);
  3210     __ jcc(Assembler::notEqual, L_key_192_256);
  3212     // 128 bit code follows here
  3213     __ movptr(pos, 0);
  3214     __ align(OptoLoopAlignment);
  3216     __ BIND(L_loopTop_128);
  3217     __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of input
  3218     __ pxor  (xmm_result, xmm_temp);               // xor with the current r vector
  3219     __ pxor  (xmm_result, xmm_key0);               // do the aes rounds
  3220     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_FIRST + 9; rnum++) {
  3221       __ aesenc(xmm_result, as_XMMRegister(rnum));
  3223     __ aesenclast(xmm_result, xmm_key10);
  3224     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);     // store into the next 16 bytes of output
  3225     // no need to store r to memory until we exit
  3226     __ addptr(pos, AESBlockSize);
  3227     __ subptr(len_reg, AESBlockSize);
  3228     __ jcc(Assembler::notEqual, L_loopTop_128);
  3230     __ BIND(L_exit);
  3231     __ movdqu(Address(rvec, 0), xmm_result);     // final value of r stored in rvec of CipherBlockChaining object
  3233 #ifdef _WIN64
  3234     // restore xmm regs belonging to calling function
  3235     for (int i = 6; i <= XMM_REG_NUM_KEY_LAST; i++) {
  3236       __ movdqu(as_XMMRegister(i), xmm_save(i));
  3238 #endif
  3239     __ movl(rax, 0); // return 0 (why?)
  3240     __ leave(); // required for proper stackwalking of RuntimeStub frame
  3241     __ ret(0);
  3243     __ BIND(L_key_192_256);
  3244     // here rax = len in ints of AESCrypt.KLE array (52=192, or 60=256)
  3245     load_key(xmm_key11, key, 0xb0, xmm_key_shuf_mask);
  3246     load_key(xmm_key12, key, 0xc0, xmm_key_shuf_mask);
  3247     __ cmpl(rax, 52);
  3248     __ jcc(Assembler::notEqual, L_key_256);
  3250     // 192-bit code follows here (could be changed to use more xmm registers)
  3251     __ movptr(pos, 0);
  3252     __ align(OptoLoopAlignment);
  3254     __ BIND(L_loopTop_192);
  3255     __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of input
  3256     __ pxor  (xmm_result, xmm_temp);               // xor with the current r vector
  3257     __ pxor  (xmm_result, xmm_key0);               // do the aes rounds
  3258     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum  <= XMM_REG_NUM_KEY_FIRST + 11; rnum++) {
  3259       __ aesenc(xmm_result, as_XMMRegister(rnum));
  3261     __ aesenclast(xmm_result, xmm_key12);
  3262     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);     // store into the next 16 bytes of output
  3263     // no need to store r to memory until we exit
  3264     __ addptr(pos, AESBlockSize);
  3265     __ subptr(len_reg, AESBlockSize);
  3266     __ jcc(Assembler::notEqual, L_loopTop_192);
  3267     __ jmp(L_exit);
  3269     __ BIND(L_key_256);
  3270     // 256-bit code follows here (could be changed to use more xmm registers)
  3271     load_key(xmm_key13, key, 0xd0, xmm_key_shuf_mask);
  3272     __ movptr(pos, 0);
  3273     __ align(OptoLoopAlignment);
  3275     __ BIND(L_loopTop_256);
  3276     __ movdqu(xmm_temp, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of input
  3277     __ pxor  (xmm_result, xmm_temp);               // xor with the current r vector
  3278     __ pxor  (xmm_result, xmm_key0);               // do the aes rounds
  3279     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum  <= XMM_REG_NUM_KEY_FIRST + 13; rnum++) {
  3280       __ aesenc(xmm_result, as_XMMRegister(rnum));
  3282     load_key(xmm_temp, key, 0xe0);
  3283     __ aesenclast(xmm_result, xmm_temp);
  3284     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);     // store into the next 16 bytes of output
  3285     // no need to store r to memory until we exit
  3286     __ addptr(pos, AESBlockSize);
  3287     __ subptr(len_reg, AESBlockSize);
  3288     __ jcc(Assembler::notEqual, L_loopTop_256);
  3289     __ jmp(L_exit);
  3291     return start;
  3296   // This is a version of CBC/AES Decrypt which does 4 blocks in a loop at a time
  3297   // to hide instruction latency
  3298   //
  3299   // Arguments:
  3300   //
  3301   // Inputs:
  3302   //   c_rarg0   - source byte array address
  3303   //   c_rarg1   - destination byte array address
  3304   //   c_rarg2   - K (key) in little endian int array
  3305   //   c_rarg3   - r vector byte array address
  3306   //   c_rarg4   - input length
  3307   //
  3309   address generate_cipherBlockChaining_decryptAESCrypt_Parallel() {
  3310     assert(UseAES, "need AES instructions and misaligned SSE support");
  3311     __ align(CodeEntryAlignment);
  3312     StubCodeMark mark(this, "StubRoutines", "cipherBlockChaining_decryptAESCrypt");
  3313     address start = __ pc();
  3315     Label L_exit, L_key_192_256, L_key_256;
  3316     Label L_singleBlock_loopTop_128, L_multiBlock_loopTop_128;
  3317     Label L_singleBlock_loopTop_192, L_singleBlock_loopTop_256;
  3318     const Register from        = c_rarg0;  // source array address
  3319     const Register to          = c_rarg1;  // destination array address
  3320     const Register key         = c_rarg2;  // key array address
  3321     const Register rvec        = c_rarg3;  // r byte array initialized from initvector array address
  3322                                            // and left with the results of the last encryption block
  3323 #ifndef _WIN64
  3324     const Register len_reg     = c_rarg4;  // src len (must be multiple of blocksize 16)
  3325 #else
  3326     const Address  len_mem(rsp, 6 * wordSize);  // length is on stack on Win64
  3327     const Register len_reg     = r10;      // pick the first volatile windows register
  3328 #endif
  3329     const Register pos         = rax;
  3331     // keys 0-10 preloaded into xmm2-xmm12
  3332     const int XMM_REG_NUM_KEY_FIRST = 5;
  3333     const int XMM_REG_NUM_KEY_LAST  = 15;
  3334     const XMMRegister xmm_key_first = as_XMMRegister(XMM_REG_NUM_KEY_FIRST);
  3335     const XMMRegister xmm_key_last  = as_XMMRegister(XMM_REG_NUM_KEY_LAST);
  3337     __ enter(); // required for proper stackwalking of RuntimeStub frame
  3339 #ifdef _WIN64
  3340     // on win64, fill len_reg from stack position
  3341     __ movl(len_reg, len_mem);
  3342     // save the xmm registers which must be preserved 6-15
  3343     __ subptr(rsp, -rsp_after_call_off * wordSize);
  3344     for (int i = 6; i <= XMM_REG_NUM_KEY_LAST; i++) {
  3345       __ movdqu(xmm_save(i), as_XMMRegister(i));
  3347 #endif
  3348     // the java expanded key ordering is rotated one position from what we want
  3349     // so we start from 0x10 here and hit 0x00 last
  3350     const XMMRegister xmm_key_shuf_mask = xmm1;  // used temporarily to swap key bytes up front
  3351     __ movdqu(xmm_key_shuf_mask, ExternalAddress(StubRoutines::x86::key_shuffle_mask_addr()));
  3352     // load up xmm regs 5 thru 15 with key 0x10 - 0xa0 - 0x00
  3353     for (int rnum = XMM_REG_NUM_KEY_FIRST, offset = 0x10; rnum < XMM_REG_NUM_KEY_LAST; rnum++) {
  3354       load_key(as_XMMRegister(rnum), key, offset, xmm_key_shuf_mask);
  3355       offset += 0x10;
  3357     load_key(xmm_key_last, key, 0x00, xmm_key_shuf_mask);
  3359     const XMMRegister xmm_prev_block_cipher = xmm1;  // holds cipher of previous block
  3361     // registers holding the four results in the parallelized loop
  3362     const XMMRegister xmm_result0 = xmm0;
  3363     const XMMRegister xmm_result1 = xmm2;
  3364     const XMMRegister xmm_result2 = xmm3;
  3365     const XMMRegister xmm_result3 = xmm4;
  3367     __ movdqu(xmm_prev_block_cipher, Address(rvec, 0x00));   // initialize with initial rvec
  3369     // now split to different paths depending on the keylen (len in ints of AESCrypt.KLE array (52=192, or 60=256))
  3370     __ movl(rax, Address(key, arrayOopDesc::length_offset_in_bytes() - arrayOopDesc::base_offset_in_bytes(T_INT)));
  3371     __ cmpl(rax, 44);
  3372     __ jcc(Assembler::notEqual, L_key_192_256);
  3375     // 128-bit code follows here, parallelized
  3376     __ movptr(pos, 0);
  3377     __ align(OptoLoopAlignment);
  3378     __ BIND(L_multiBlock_loopTop_128);
  3379     __ cmpptr(len_reg, 4*AESBlockSize);           // see if at least 4 blocks left
  3380     __ jcc(Assembler::less, L_singleBlock_loopTop_128);
  3382     __ movdqu(xmm_result0, Address(from, pos, Address::times_1, 0*AESBlockSize));   // get next 4 blocks into xmmresult registers
  3383     __ movdqu(xmm_result1, Address(from, pos, Address::times_1, 1*AESBlockSize));
  3384     __ movdqu(xmm_result2, Address(from, pos, Address::times_1, 2*AESBlockSize));
  3385     __ movdqu(xmm_result3, Address(from, pos, Address::times_1, 3*AESBlockSize));
  3387 #define DoFour(opc, src_reg)                    \
  3388     __ opc(xmm_result0, src_reg);               \
  3389     __ opc(xmm_result1, src_reg);               \
  3390     __ opc(xmm_result2, src_reg);               \
  3391     __ opc(xmm_result3, src_reg);
  3393     DoFour(pxor, xmm_key_first);
  3394     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum  <= XMM_REG_NUM_KEY_LAST - 1; rnum++) {
  3395       DoFour(aesdec, as_XMMRegister(rnum));
  3397     DoFour(aesdeclast, xmm_key_last);
  3398     // for each result, xor with the r vector of previous cipher block
  3399     __ pxor(xmm_result0, xmm_prev_block_cipher);
  3400     __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 0*AESBlockSize));
  3401     __ pxor(xmm_result1, xmm_prev_block_cipher);
  3402     __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 1*AESBlockSize));
  3403     __ pxor(xmm_result2, xmm_prev_block_cipher);
  3404     __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 2*AESBlockSize));
  3405     __ pxor(xmm_result3, xmm_prev_block_cipher);
  3406     __ movdqu(xmm_prev_block_cipher, Address(from, pos, Address::times_1, 3*AESBlockSize));   // this will carry over to next set of blocks
  3408     __ movdqu(Address(to, pos, Address::times_1, 0*AESBlockSize), xmm_result0);     // store 4 results into the next 64 bytes of output
  3409     __ movdqu(Address(to, pos, Address::times_1, 1*AESBlockSize), xmm_result1);
  3410     __ movdqu(Address(to, pos, Address::times_1, 2*AESBlockSize), xmm_result2);
  3411     __ movdqu(Address(to, pos, Address::times_1, 3*AESBlockSize), xmm_result3);
  3413     __ addptr(pos, 4*AESBlockSize);
  3414     __ subptr(len_reg, 4*AESBlockSize);
  3415     __ jmp(L_multiBlock_loopTop_128);
  3417     // registers used in the non-parallelized loops
  3418     // xmm register assignments for the loops below
  3419     const XMMRegister xmm_result = xmm0;
  3420     const XMMRegister xmm_prev_block_cipher_save = xmm2;
  3421     const XMMRegister xmm_key11 = xmm3;
  3422     const XMMRegister xmm_key12 = xmm4;
  3423     const XMMRegister xmm_temp  = xmm4;
  3425     __ align(OptoLoopAlignment);
  3426     __ BIND(L_singleBlock_loopTop_128);
  3427     __ cmpptr(len_reg, 0);           // any blocks left??
  3428     __ jcc(Assembler::equal, L_exit);
  3429     __ movdqu(xmm_result, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of cipher input
  3430     __ movdqa(xmm_prev_block_cipher_save, xmm_result);              // save for next r vector
  3431     __ pxor  (xmm_result, xmm_key_first);               // do the aes dec rounds
  3432     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum  <= XMM_REG_NUM_KEY_LAST - 1; rnum++) {
  3433       __ aesdec(xmm_result, as_XMMRegister(rnum));
  3435     __ aesdeclast(xmm_result, xmm_key_last);
  3436     __ pxor  (xmm_result, xmm_prev_block_cipher);               // xor with the current r vector
  3437     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);     // store into the next 16 bytes of output
  3438     // no need to store r to memory until we exit
  3439     __ movdqa(xmm_prev_block_cipher, xmm_prev_block_cipher_save);              // set up next r vector with cipher input from this block
  3441     __ addptr(pos, AESBlockSize);
  3442     __ subptr(len_reg, AESBlockSize);
  3443     __ jmp(L_singleBlock_loopTop_128);
  3446     __ BIND(L_exit);
  3447     __ movdqu(Address(rvec, 0), xmm_prev_block_cipher);     // final value of r stored in rvec of CipherBlockChaining object
  3448 #ifdef _WIN64
  3449     // restore regs belonging to calling function
  3450     for (int i = 6; i <= XMM_REG_NUM_KEY_LAST; i++) {
  3451       __ movdqu(as_XMMRegister(i), xmm_save(i));
  3453 #endif
  3454     __ movl(rax, 0); // return 0 (why?)
  3455     __ leave(); // required for proper stackwalking of RuntimeStub frame
  3456     __ ret(0);
  3459     __ BIND(L_key_192_256);
  3460     // here rax = len in ints of AESCrypt.KLE array (52=192, or 60=256)
  3461     load_key(xmm_key11, key, 0xb0);
  3462     __ cmpl(rax, 52);
  3463     __ jcc(Assembler::notEqual, L_key_256);
  3465     // 192-bit code follows here (could be optimized to use parallelism)
  3466     load_key(xmm_key12, key, 0xc0);     // 192-bit key goes up to c0
  3467     __ movptr(pos, 0);
  3468     __ align(OptoLoopAlignment);
  3470     __ BIND(L_singleBlock_loopTop_192);
  3471     __ movdqu(xmm_result, Address(from, pos, Address::times_1, 0));   // get next 16 bytes of cipher input
  3472     __ movdqa(xmm_prev_block_cipher_save, xmm_result);              // save for next r vector
  3473     __ pxor  (xmm_result, xmm_key_first);               // do the aes dec rounds
  3474     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_LAST - 1; rnum++) {
  3475       __ aesdec(xmm_result, as_XMMRegister(rnum));
  3477     __ aesdec(xmm_result, xmm_key11);
  3478     __ aesdec(xmm_result, xmm_key12);
  3479     __ aesdeclast(xmm_result, xmm_key_last);                    // xmm15 always came from key+0
  3480     __ pxor  (xmm_result, xmm_prev_block_cipher);               // xor with the current r vector
  3481     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);  // store into the next 16 bytes of output
  3482     // no need to store r to memory until we exit
  3483     __ movdqa(xmm_prev_block_cipher, xmm_prev_block_cipher_save);  // set up next r vector with cipher input from this block
  3484     __ addptr(pos, AESBlockSize);
  3485     __ subptr(len_reg, AESBlockSize);
  3486     __ jcc(Assembler::notEqual,L_singleBlock_loopTop_192);
  3487     __ jmp(L_exit);
  3489     __ BIND(L_key_256);
  3490     // 256-bit code follows here (could be optimized to use parallelism)
  3491     __ movptr(pos, 0);
  3492     __ align(OptoLoopAlignment);
  3494     __ BIND(L_singleBlock_loopTop_256);
  3495     __ movdqu(xmm_result, Address(from, pos, Address::times_1, 0)); // get next 16 bytes of cipher input
  3496     __ movdqa(xmm_prev_block_cipher_save, xmm_result);              // save for next r vector
  3497     __ pxor  (xmm_result, xmm_key_first);               // do the aes dec rounds
  3498     for (int rnum = XMM_REG_NUM_KEY_FIRST + 1; rnum <= XMM_REG_NUM_KEY_LAST - 1; rnum++) {
  3499       __ aesdec(xmm_result, as_XMMRegister(rnum));
  3501     __ aesdec(xmm_result, xmm_key11);
  3502     load_key(xmm_temp, key, 0xc0);
  3503     __ aesdec(xmm_result, xmm_temp);
  3504     load_key(xmm_temp, key, 0xd0);
  3505     __ aesdec(xmm_result, xmm_temp);
  3506     load_key(xmm_temp, key, 0xe0);     // 256-bit key goes up to e0
  3507     __ aesdec(xmm_result, xmm_temp);
  3508     __ aesdeclast(xmm_result, xmm_key_last);          // xmm15 came from key+0
  3509     __ pxor  (xmm_result, xmm_prev_block_cipher);               // xor with the current r vector
  3510     __ movdqu(Address(to, pos, Address::times_1, 0), xmm_result);  // store into the next 16 bytes of output
  3511     // no need to store r to memory until we exit
  3512     __ movdqa(xmm_prev_block_cipher, xmm_prev_block_cipher_save);  // set up next r vector with cipher input from this block
  3513     __ addptr(pos, AESBlockSize);
  3514     __ subptr(len_reg, AESBlockSize);
  3515     __ jcc(Assembler::notEqual,L_singleBlock_loopTop_256);
  3516     __ jmp(L_exit);
  3518     return start;
  3523 #undef __
  3524 #define __ masm->
  3526   // Continuation point for throwing of implicit exceptions that are
  3527   // not handled in the current activation. Fabricates an exception
  3528   // oop and initiates normal exception dispatching in this
  3529   // frame. Since we need to preserve callee-saved values (currently
  3530   // only for C2, but done for C1 as well) we need a callee-saved oop
  3531   // map and therefore have to make these stubs into RuntimeStubs
  3532   // rather than BufferBlobs.  If the compiler needs all registers to
  3533   // be preserved between the fault point and the exception handler
  3534   // then it must assume responsibility for that in
  3535   // AbstractCompiler::continuation_for_implicit_null_exception or
  3536   // continuation_for_implicit_division_by_zero_exception. All other
  3537   // implicit exceptions (e.g., NullPointerException or
  3538   // AbstractMethodError on entry) are either at call sites or
  3539   // otherwise assume that stack unwinding will be initiated, so
  3540   // caller saved registers were assumed volatile in the compiler.
  3541   address generate_throw_exception(const char* name,
  3542                                    address runtime_entry,
  3543                                    Register arg1 = noreg,
  3544                                    Register arg2 = noreg) {
  3545     // Information about frame layout at time of blocking runtime call.
  3546     // Note that we only have to preserve callee-saved registers since
  3547     // the compilers are responsible for supplying a continuation point
  3548     // if they expect all registers to be preserved.
  3549     enum layout {
  3550       rbp_off = frame::arg_reg_save_area_bytes/BytesPerInt,
  3551       rbp_off2,
  3552       return_off,
  3553       return_off2,
  3554       framesize // inclusive of return address
  3555     };
  3557     int insts_size = 512;
  3558     int locs_size  = 64;
  3560     CodeBuffer code(name, insts_size, locs_size);
  3561     OopMapSet* oop_maps  = new OopMapSet();
  3562     MacroAssembler* masm = new MacroAssembler(&code);
  3564     address start = __ pc();
  3566     // This is an inlined and slightly modified version of call_VM
  3567     // which has the ability to fetch the return PC out of
  3568     // thread-local storage and also sets up last_Java_sp slightly
  3569     // differently than the real call_VM
  3571     __ enter(); // required for proper stackwalking of RuntimeStub frame
  3573     assert(is_even(framesize/2), "sp not 16-byte aligned");
  3575     // return address and rbp are already in place
  3576     __ subptr(rsp, (framesize-4) << LogBytesPerInt); // prolog
  3578     int frame_complete = __ pc() - start;
  3580     // Set up last_Java_sp and last_Java_fp
  3581     address the_pc = __ pc();
  3582     __ set_last_Java_frame(rsp, rbp, the_pc);
  3583     __ andptr(rsp, -(StackAlignmentInBytes));    // Align stack
  3585     // Call runtime
  3586     if (arg1 != noreg) {
  3587       assert(arg2 != c_rarg1, "clobbered");
  3588       __ movptr(c_rarg1, arg1);
  3590     if (arg2 != noreg) {
  3591       __ movptr(c_rarg2, arg2);
  3593     __ movptr(c_rarg0, r15_thread);
  3594     BLOCK_COMMENT("call runtime_entry");
  3595     __ call(RuntimeAddress(runtime_entry));
  3597     // Generate oop map
  3598     OopMap* map = new OopMap(framesize, 0);
  3600     oop_maps->add_gc_map(the_pc - start, map);
  3602     __ reset_last_Java_frame(true, true);
  3604     __ leave(); // required for proper stackwalking of RuntimeStub frame
  3606     // check for pending exceptions
  3607 #ifdef ASSERT
  3608     Label L;
  3609     __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()),
  3610             (int32_t) NULL_WORD);
  3611     __ jcc(Assembler::notEqual, L);
  3612     __ should_not_reach_here();
  3613     __ bind(L);
  3614 #endif // ASSERT
  3615     __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
  3618     // codeBlob framesize is in words (not VMRegImpl::slot_size)
  3619     RuntimeStub* stub =
  3620       RuntimeStub::new_runtime_stub(name,
  3621                                     &code,
  3622                                     frame_complete,
  3623                                     (framesize >> (LogBytesPerWord - LogBytesPerInt)),
  3624                                     oop_maps, false);
  3625     return stub->entry_point();
  3628   // Initialization
  3629   void generate_initial() {
  3630     // Generates all stubs and initializes the entry points
  3632     // This platform-specific stub is needed by generate_call_stub()
  3633     StubRoutines::x86::_mxcsr_std        = generate_fp_mask("mxcsr_std",        0x0000000000001F80);
  3635     // entry points that exist in all platforms Note: This is code
  3636     // that could be shared among different platforms - however the
  3637     // benefit seems to be smaller than the disadvantage of having a
  3638     // much more complicated generator structure. See also comment in
  3639     // stubRoutines.hpp.
  3641     StubRoutines::_forward_exception_entry = generate_forward_exception();
  3643     StubRoutines::_call_stub_entry =
  3644       generate_call_stub(StubRoutines::_call_stub_return_address);
  3646     // is referenced by megamorphic call
  3647     StubRoutines::_catch_exception_entry = generate_catch_exception();
  3649     // atomic calls
  3650     StubRoutines::_atomic_xchg_entry         = generate_atomic_xchg();
  3651     StubRoutines::_atomic_xchg_ptr_entry     = generate_atomic_xchg_ptr();
  3652     StubRoutines::_atomic_cmpxchg_entry      = generate_atomic_cmpxchg();
  3653     StubRoutines::_atomic_cmpxchg_long_entry = generate_atomic_cmpxchg_long();
  3654     StubRoutines::_atomic_add_entry          = generate_atomic_add();
  3655     StubRoutines::_atomic_add_ptr_entry      = generate_atomic_add_ptr();
  3656     StubRoutines::_fence_entry               = generate_orderaccess_fence();
  3658     StubRoutines::_handler_for_unsafe_access_entry =
  3659       generate_handler_for_unsafe_access();
  3661     // platform dependent
  3662     StubRoutines::x86::_get_previous_fp_entry = generate_get_previous_fp();
  3663     StubRoutines::x86::_get_previous_sp_entry = generate_get_previous_sp();
  3665     StubRoutines::x86::_verify_mxcsr_entry    = generate_verify_mxcsr();
  3667     // Build this early so it's available for the interpreter.
  3668     StubRoutines::_throw_StackOverflowError_entry =
  3669       generate_throw_exception("StackOverflowError throw_exception",
  3670                                CAST_FROM_FN_PTR(address,
  3671                                                 SharedRuntime::
  3672                                                 throw_StackOverflowError));
  3675   void generate_all() {
  3676     // Generates all stubs and initializes the entry points
  3678     // These entry points require SharedInfo::stack0 to be set up in
  3679     // non-core builds and need to be relocatable, so they each
  3680     // fabricate a RuntimeStub internally.
  3681     StubRoutines::_throw_AbstractMethodError_entry =
  3682       generate_throw_exception("AbstractMethodError throw_exception",
  3683                                CAST_FROM_FN_PTR(address,
  3684                                                 SharedRuntime::
  3685                                                 throw_AbstractMethodError));
  3687     StubRoutines::_throw_IncompatibleClassChangeError_entry =
  3688       generate_throw_exception("IncompatibleClassChangeError throw_exception",
  3689                                CAST_FROM_FN_PTR(address,
  3690                                                 SharedRuntime::
  3691                                                 throw_IncompatibleClassChangeError));
  3693     StubRoutines::_throw_NullPointerException_at_call_entry =
  3694       generate_throw_exception("NullPointerException at call throw_exception",
  3695                                CAST_FROM_FN_PTR(address,
  3696                                                 SharedRuntime::
  3697                                                 throw_NullPointerException_at_call));
  3699     // entry points that are platform specific
  3700     StubRoutines::x86::_f2i_fixup = generate_f2i_fixup();
  3701     StubRoutines::x86::_f2l_fixup = generate_f2l_fixup();
  3702     StubRoutines::x86::_d2i_fixup = generate_d2i_fixup();
  3703     StubRoutines::x86::_d2l_fixup = generate_d2l_fixup();
  3705     StubRoutines::x86::_float_sign_mask  = generate_fp_mask("float_sign_mask",  0x7FFFFFFF7FFFFFFF);
  3706     StubRoutines::x86::_float_sign_flip  = generate_fp_mask("float_sign_flip",  0x8000000080000000);
  3707     StubRoutines::x86::_double_sign_mask = generate_fp_mask("double_sign_mask", 0x7FFFFFFFFFFFFFFF);
  3708     StubRoutines::x86::_double_sign_flip = generate_fp_mask("double_sign_flip", 0x8000000000000000);
  3710     // support for verify_oop (must happen after universe_init)
  3711     StubRoutines::_verify_oop_subroutine_entry = generate_verify_oop();
  3713     // arraycopy stubs used by compilers
  3714     generate_arraycopy_stubs();
  3716     generate_math_stubs();
  3718     // don't bother generating these AES intrinsic stubs unless global flag is set
  3719     if (UseAESIntrinsics) {
  3720       StubRoutines::x86::_key_shuffle_mask_addr = generate_key_shuffle_mask();  // needed by the others
  3722       StubRoutines::_aescrypt_encryptBlock = generate_aescrypt_encryptBlock();
  3723       StubRoutines::_aescrypt_decryptBlock = generate_aescrypt_decryptBlock();
  3724       StubRoutines::_cipherBlockChaining_encryptAESCrypt = generate_cipherBlockChaining_encryptAESCrypt();
  3725       StubRoutines::_cipherBlockChaining_decryptAESCrypt = generate_cipherBlockChaining_decryptAESCrypt_Parallel();
  3729  public:
  3730   StubGenerator(CodeBuffer* code, bool all) : StubCodeGenerator(code) {
  3731     if (all) {
  3732       generate_all();
  3733     } else {
  3734       generate_initial();
  3737 }; // end class declaration
  3739 void StubGenerator_generate(CodeBuffer* code, bool all) {
  3740   StubGenerator g(code, all);

mercurial