1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2012, 2015 SAP AG. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "interpreter/interpreter.hpp"
  29 #include "nativeInst_ppc.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 "utilities/top.hpp"
  41 #include "runtime/thread.inline.hpp"
  42 
  43 #define __ _masm->
  44 
  45 #ifdef PRODUCT
  46 #define BLOCK_COMMENT(str) // nothing
  47 #else
  48 #define BLOCK_COMMENT(str) __ block_comment(str)
  49 #endif
  50 
  51 #if defined(ABI_ELFv2)
  52 #define STUB_ENTRY(name) StubRoutines::name()
  53 #else
  54 #define STUB_ENTRY(name) ((FunctionDescriptor*)StubRoutines::name())->entry()
  55 #endif
  56 
  57 class StubGenerator: public StubCodeGenerator {
  58  private:
  59 
  60   // Call stubs are used to call Java from C
  61   //
  62   // Arguments:
  63   //
  64   //   R3  - call wrapper address     : address
  65   //   R4  - result                   : intptr_t*
  66   //   R5  - result type              : BasicType
  67   //   R6  - method                   : Method
  68   //   R7  - frame mgr entry point    : address
  69   //   R8  - parameter block          : intptr_t*
  70   //   R9  - parameter count in words : int
  71   //   R10 - thread                   : Thread*
  72   //
  73   address generate_call_stub(address& return_address) {
  74     // Setup a new c frame, copy java arguments, call frame manager or
  75     // native_entry, and process result.
  76 
  77     StubCodeMark mark(this, "StubRoutines", "call_stub");
  78 
  79     address start = __ function_entry();
  80 
  81     // some sanity checks
  82     assert((sizeof(frame::abi_minframe) % 16) == 0,           "unaligned");
  83     assert((sizeof(frame::abi_reg_args) % 16) == 0,           "unaligned");
  84     assert((sizeof(frame::spill_nonvolatiles) % 16) == 0,     "unaligned");
  85     assert((sizeof(frame::parent_ijava_frame_abi) % 16) == 0, "unaligned");
  86     assert((sizeof(frame::entry_frame_locals) % 16) == 0,     "unaligned");
  87 
  88     Register r_arg_call_wrapper_addr        = R3;
  89     Register r_arg_result_addr              = R4;
  90     Register r_arg_result_type              = R5;
  91     Register r_arg_method                   = R6;
  92     Register r_arg_entry                    = R7;
  93     Register r_arg_thread                   = R10;
  94 
  95     Register r_temp                         = R24;
  96     Register r_top_of_arguments_addr        = R25;
  97     Register r_entryframe_fp                = R26;
  98 
  99     {
 100       // Stack on entry to call_stub:
 101       //
 102       //      F1      [C_FRAME]
 103       //              ...
 104 
 105       Register r_arg_argument_addr          = R8;
 106       Register r_arg_argument_count         = R9;
 107       Register r_frame_alignment_in_bytes   = R27;
 108       Register r_argument_addr              = R28;
 109       Register r_argumentcopy_addr          = R29;
 110       Register r_argument_size_in_bytes     = R30;
 111       Register r_frame_size                 = R23;
 112 
 113       Label arguments_copied;
 114 
 115       // Save LR/CR to caller's C_FRAME.
 116       __ save_LR_CR(R0);
 117 
 118       // Zero extend arg_argument_count.
 119       __ clrldi(r_arg_argument_count, r_arg_argument_count, 32);
 120 
 121       // Save non-volatiles GPRs to ENTRY_FRAME (not yet pushed, but it's safe).
 122       __ save_nonvolatile_gprs(R1_SP, _spill_nonvolatiles_neg(r14));
 123 
 124       // Keep copy of our frame pointer (caller's SP).
 125       __ mr(r_entryframe_fp, R1_SP);
 126 
 127       BLOCK_COMMENT("Push ENTRY_FRAME including arguments");
 128       // Push ENTRY_FRAME including arguments:
 129       //
 130       //      F0      [TOP_IJAVA_FRAME_ABI]
 131       //              alignment (optional)
 132       //              [outgoing Java arguments]
 133       //              [ENTRY_FRAME_LOCALS]
 134       //      F1      [C_FRAME]
 135       //              ...
 136 
 137       // calculate frame size
 138 
 139       // unaligned size of arguments
 140       __ sldi(r_argument_size_in_bytes,
 141                   r_arg_argument_count, Interpreter::logStackElementSize);
 142       // arguments alignment (max 1 slot)
 143       // FIXME: use round_to() here
 144       __ andi_(r_frame_alignment_in_bytes, r_arg_argument_count, 1);
 145       __ sldi(r_frame_alignment_in_bytes,
 146               r_frame_alignment_in_bytes, Interpreter::logStackElementSize);
 147 
 148       // size = unaligned size of arguments + top abi's size
 149       __ addi(r_frame_size, r_argument_size_in_bytes,
 150               frame::top_ijava_frame_abi_size);
 151       // size += arguments alignment
 152       __ add(r_frame_size,
 153              r_frame_size, r_frame_alignment_in_bytes);
 154       // size += size of call_stub locals
 155       __ addi(r_frame_size,
 156               r_frame_size, frame::entry_frame_locals_size);
 157 
 158       // push ENTRY_FRAME
 159       __ push_frame(r_frame_size, r_temp);
 160 
 161       // initialize call_stub locals (step 1)
 162       __ std(r_arg_call_wrapper_addr,
 163              _entry_frame_locals_neg(call_wrapper_address), r_entryframe_fp);
 164       __ std(r_arg_result_addr,
 165              _entry_frame_locals_neg(result_address), r_entryframe_fp);
 166       __ std(r_arg_result_type,
 167              _entry_frame_locals_neg(result_type), r_entryframe_fp);
 168       // we will save arguments_tos_address later
 169 
 170 
 171       BLOCK_COMMENT("Copy Java arguments");
 172       // copy Java arguments
 173 
 174       // Calculate top_of_arguments_addr which will be R17_tos (not prepushed) later.
 175       // FIXME: why not simply use SP+frame::top_ijava_frame_size?
 176       __ addi(r_top_of_arguments_addr,
 177               R1_SP, frame::top_ijava_frame_abi_size);
 178       __ add(r_top_of_arguments_addr,
 179              r_top_of_arguments_addr, r_frame_alignment_in_bytes);
 180 
 181       // any arguments to copy?
 182       __ cmpdi(CCR0, r_arg_argument_count, 0);
 183       __ beq(CCR0, arguments_copied);
 184 
 185       // prepare loop and copy arguments in reverse order
 186       {
 187         // init CTR with arg_argument_count
 188         __ mtctr(r_arg_argument_count);
 189 
 190         // let r_argumentcopy_addr point to last outgoing Java arguments P
 191         __ mr(r_argumentcopy_addr, r_top_of_arguments_addr);
 192 
 193         // let r_argument_addr point to last incoming java argument
 194         __ add(r_argument_addr,
 195                    r_arg_argument_addr, r_argument_size_in_bytes);
 196         __ addi(r_argument_addr, r_argument_addr, -BytesPerWord);
 197 
 198         // now loop while CTR > 0 and copy arguments
 199         {
 200           Label next_argument;
 201           __ bind(next_argument);
 202 
 203           __ ld(r_temp, 0, r_argument_addr);
 204           // argument_addr--;
 205           __ addi(r_argument_addr, r_argument_addr, -BytesPerWord);
 206           __ std(r_temp, 0, r_argumentcopy_addr);
 207           // argumentcopy_addr++;
 208           __ addi(r_argumentcopy_addr, r_argumentcopy_addr, BytesPerWord);
 209 
 210           __ bdnz(next_argument);
 211         }
 212       }
 213 
 214       // Arguments copied, continue.
 215       __ bind(arguments_copied);
 216     }
 217 
 218     {
 219       BLOCK_COMMENT("Call frame manager or native entry.");
 220       // Call frame manager or native entry.
 221       Register r_new_arg_entry = R14;
 222       assert_different_registers(r_new_arg_entry, r_top_of_arguments_addr,
 223                                  r_arg_method, r_arg_thread);
 224 
 225       __ mr(r_new_arg_entry, r_arg_entry);
 226 
 227       // Register state on entry to frame manager / native entry:
 228       //
 229       //   tos         -  intptr_t*    sender tos (prepushed) Lesp = (SP) + copied_arguments_offset - 8
 230       //   R19_method  -  Method
 231       //   R16_thread  -  JavaThread*
 232 
 233       // Tos must point to last argument - element_size.
 234 #ifdef CC_INTERP
 235       const Register tos = R17_tos;
 236 #else
 237       const Register tos = R15_esp;
 238 #endif
 239       __ addi(tos, r_top_of_arguments_addr, -Interpreter::stackElementSize);
 240 
 241       // initialize call_stub locals (step 2)
 242       // now save tos as arguments_tos_address
 243       __ std(tos, _entry_frame_locals_neg(arguments_tos_address), r_entryframe_fp);
 244 
 245       // load argument registers for call
 246       __ mr(R19_method, r_arg_method);
 247       __ mr(R16_thread, r_arg_thread);
 248       assert(tos != r_arg_method, "trashed r_arg_method");
 249       assert(tos != r_arg_thread && R19_method != r_arg_thread, "trashed r_arg_thread");
 250 
 251       // Set R15_prev_state to 0 for simplifying checks in callee.
 252 #ifdef CC_INTERP
 253       __ li(R15_prev_state, 0);
 254 #else
 255       __ load_const_optimized(R25_templateTableBase, (address)Interpreter::dispatch_table((TosState)0), R11_scratch1);
 256 #endif
 257       // Stack on entry to frame manager / native entry:
 258       //
 259       //      F0      [TOP_IJAVA_FRAME_ABI]
 260       //              alignment (optional)
 261       //              [outgoing Java arguments]
 262       //              [ENTRY_FRAME_LOCALS]
 263       //      F1      [C_FRAME]
 264       //              ...
 265       //
 266 
 267       // global toc register
 268       __ load_const_optimized(R29_TOC, MacroAssembler::global_toc(), R11_scratch1);
 269       // Remember the senderSP so we interpreter can pop c2i arguments off of the stack
 270       // when called via a c2i.
 271 
 272       // Pass initial_caller_sp to framemanager.
 273       __ mr(R21_tmp1, R1_SP);
 274 
 275       // Do a light-weight C-call here, r_new_arg_entry holds the address
 276       // of the interpreter entry point (frame manager or native entry)
 277       // and save runtime-value of LR in return_address.
 278       assert(r_new_arg_entry != tos && r_new_arg_entry != R19_method && r_new_arg_entry != R16_thread,
 279              "trashed r_new_arg_entry");
 280       return_address = __ call_stub(r_new_arg_entry);
 281     }
 282 
 283     {
 284       BLOCK_COMMENT("Returned from frame manager or native entry.");
 285       // Returned from frame manager or native entry.
 286       // Now pop frame, process result, and return to caller.
 287 
 288       // Stack on exit from frame manager / native entry:
 289       //
 290       //      F0      [ABI]
 291       //              ...
 292       //              [ENTRY_FRAME_LOCALS]
 293       //      F1      [C_FRAME]
 294       //              ...
 295       //
 296       // Just pop the topmost frame ...
 297       //
 298 
 299       Label ret_is_object;
 300       Label ret_is_long;
 301       Label ret_is_float;
 302       Label ret_is_double;
 303 
 304       Register r_entryframe_fp = R30;
 305       Register r_lr            = R7_ARG5;
 306       Register r_cr            = R8_ARG6;
 307 
 308       // Reload some volatile registers which we've spilled before the call
 309       // to frame manager / native entry.
 310       // Access all locals via frame pointer, because we know nothing about
 311       // the topmost frame's size.
 312       __ ld(r_entryframe_fp, _abi(callers_sp), R1_SP);
 313       assert_different_registers(r_entryframe_fp, R3_RET, r_arg_result_addr, r_arg_result_type, r_cr, r_lr);
 314       __ ld(r_arg_result_addr,
 315             _entry_frame_locals_neg(result_address), r_entryframe_fp);
 316       __ ld(r_arg_result_type,
 317             _entry_frame_locals_neg(result_type), r_entryframe_fp);
 318       __ ld(r_cr, _abi(cr), r_entryframe_fp);
 319       __ ld(r_lr, _abi(lr), r_entryframe_fp);
 320 
 321       // pop frame and restore non-volatiles, LR and CR
 322       __ mr(R1_SP, r_entryframe_fp);
 323       __ mtcr(r_cr);
 324       __ mtlr(r_lr);
 325 
 326       // Store result depending on type. Everything that is not
 327       // T_OBJECT, T_LONG, T_FLOAT, or T_DOUBLE is treated as T_INT.
 328       __ cmpwi(CCR0, r_arg_result_type, T_OBJECT);
 329       __ cmpwi(CCR1, r_arg_result_type, T_LONG);
 330       __ cmpwi(CCR5, r_arg_result_type, T_FLOAT);
 331       __ cmpwi(CCR6, r_arg_result_type, T_DOUBLE);
 332 
 333       // restore non-volatile registers
 334       __ restore_nonvolatile_gprs(R1_SP, _spill_nonvolatiles_neg(r14));
 335 
 336 
 337       // Stack on exit from call_stub:
 338       //
 339       //      0       [C_FRAME]
 340       //              ...
 341       //
 342       //  no call_stub frames left.
 343 
 344       // All non-volatiles have been restored at this point!!
 345       assert(R3_RET == R3, "R3_RET should be R3");
 346 
 347       __ beq(CCR0, ret_is_object);
 348       __ beq(CCR1, ret_is_long);
 349       __ beq(CCR5, ret_is_float);
 350       __ beq(CCR6, ret_is_double);
 351 
 352       // default:
 353       __ stw(R3_RET, 0, r_arg_result_addr);
 354       __ blr(); // return to caller
 355 
 356       // case T_OBJECT:
 357       __ bind(ret_is_object);
 358       __ std(R3_RET, 0, r_arg_result_addr);
 359       __ blr(); // return to caller
 360 
 361       // case T_LONG:
 362       __ bind(ret_is_long);
 363       __ std(R3_RET, 0, r_arg_result_addr);
 364       __ blr(); // return to caller
 365 
 366       // case T_FLOAT:
 367       __ bind(ret_is_float);
 368       __ stfs(F1_RET, 0, r_arg_result_addr);
 369       __ blr(); // return to caller
 370 
 371       // case T_DOUBLE:
 372       __ bind(ret_is_double);
 373       __ stfd(F1_RET, 0, r_arg_result_addr);
 374       __ blr(); // return to caller
 375     }
 376 
 377     return start;
 378   }
 379 
 380   // Return point for a Java call if there's an exception thrown in
 381   // Java code.  The exception is caught and transformed into a
 382   // pending exception stored in JavaThread that can be tested from
 383   // within the VM.
 384   //
 385   address generate_catch_exception() {
 386     StubCodeMark mark(this, "StubRoutines", "catch_exception");
 387 
 388     address start = __ pc();
 389 
 390     // Registers alive
 391     //
 392     //  R16_thread
 393     //  R3_ARG1 - address of pending exception
 394     //  R4_ARG2 - return address in call stub
 395 
 396     const Register exception_file = R21_tmp1;
 397     const Register exception_line = R22_tmp2;
 398 
 399     __ load_const(exception_file, (void*)__FILE__);
 400     __ load_const(exception_line, (void*)__LINE__);
 401 
 402     __ std(R3_ARG1, in_bytes(JavaThread::pending_exception_offset()), R16_thread);
 403     // store into `char *'
 404     __ std(exception_file, in_bytes(JavaThread::exception_file_offset()), R16_thread);
 405     // store into `int'
 406     __ stw(exception_line, in_bytes(JavaThread::exception_line_offset()), R16_thread);
 407 
 408     // complete return to VM
 409     assert(StubRoutines::_call_stub_return_address != NULL, "must have been generated before");
 410 
 411     __ mtlr(R4_ARG2);
 412     // continue in call stub
 413     __ blr();
 414 
 415     return start;
 416   }
 417 
 418   // Continuation point for runtime calls returning with a pending
 419   // exception.  The pending exception check happened in the runtime
 420   // or native call stub.  The pending exception in Thread is
 421   // converted into a Java-level exception.
 422   //
 423   // Read:
 424   //
 425   //   LR:     The pc the runtime library callee wants to return to.
 426   //           Since the exception occurred in the callee, the return pc
 427   //           from the point of view of Java is the exception pc.
 428   //   thread: Needed for method handles.
 429   //
 430   // Invalidate:
 431   //
 432   //   volatile registers (except below).
 433   //
 434   // Update:
 435   //
 436   //   R4_ARG2: exception
 437   //
 438   // (LR is unchanged and is live out).
 439   //
 440   address generate_forward_exception() {
 441     StubCodeMark mark(this, "StubRoutines", "forward_exception");
 442     address start = __ pc();
 443 
 444 #if !defined(PRODUCT)
 445     if (VerifyOops) {
 446       // Get pending exception oop.
 447       __ ld(R3_ARG1,
 448                 in_bytes(Thread::pending_exception_offset()),
 449                 R16_thread);
 450       // Make sure that this code is only executed if there is a pending exception.
 451       {
 452         Label L;
 453         __ cmpdi(CCR0, R3_ARG1, 0);
 454         __ bne(CCR0, L);
 455         __ stop("StubRoutines::forward exception: no pending exception (1)");
 456         __ bind(L);
 457       }
 458       __ verify_oop(R3_ARG1, "StubRoutines::forward exception: not an oop");
 459     }
 460 #endif
 461 
 462     // Save LR/CR and copy exception pc (LR) into R4_ARG2.
 463     __ save_LR_CR(R4_ARG2);
 464     __ push_frame_reg_args(0, R0);
 465     // Find exception handler.
 466     __ call_VM_leaf(CAST_FROM_FN_PTR(address,
 467                      SharedRuntime::exception_handler_for_return_address),
 468                     R16_thread,
 469                     R4_ARG2);
 470     // Copy handler's address.
 471     __ mtctr(R3_RET);
 472     __ pop_frame();
 473     __ restore_LR_CR(R0);
 474 
 475     // Set up the arguments for the exception handler:
 476     //  - R3_ARG1: exception oop
 477     //  - R4_ARG2: exception pc.
 478 
 479     // Load pending exception oop.
 480     __ ld(R3_ARG1,
 481               in_bytes(Thread::pending_exception_offset()),
 482               R16_thread);
 483 
 484     // The exception pc is the return address in the caller.
 485     // Must load it into R4_ARG2.
 486     __ mflr(R4_ARG2);
 487 
 488 #ifdef ASSERT
 489     // Make sure exception is set.
 490     {
 491       Label L;
 492       __ cmpdi(CCR0, R3_ARG1, 0);
 493       __ bne(CCR0, L);
 494       __ stop("StubRoutines::forward exception: no pending exception (2)");
 495       __ bind(L);
 496     }
 497 #endif
 498 
 499     // Clear the pending exception.
 500     __ li(R0, 0);
 501     __ std(R0,
 502                in_bytes(Thread::pending_exception_offset()),
 503                R16_thread);
 504     // Jump to exception handler.
 505     __ bctr();
 506 
 507     return start;
 508   }
 509 
 510 #undef __
 511 #define __ masm->
 512   // Continuation point for throwing of implicit exceptions that are
 513   // not handled in the current activation. Fabricates an exception
 514   // oop and initiates normal exception dispatching in this
 515   // frame. Only callee-saved registers are preserved (through the
 516   // normal register window / RegisterMap handling).  If the compiler
 517   // needs all registers to be preserved between the fault point and
 518   // the exception handler then it must assume responsibility for that
 519   // in AbstractCompiler::continuation_for_implicit_null_exception or
 520   // continuation_for_implicit_division_by_zero_exception. All other
 521   // implicit exceptions (e.g., NullPointerException or
 522   // AbstractMethodError on entry) are either at call sites or
 523   // otherwise assume that stack unwinding will be initiated, so
 524   // caller saved registers were assumed volatile in the compiler.
 525   //
 526   // Note that we generate only this stub into a RuntimeStub, because
 527   // it needs to be properly traversed and ignored during GC, so we
 528   // change the meaning of the "__" macro within this method.
 529   //
 530   // Note: the routine set_pc_not_at_call_for_caller in
 531   // SharedRuntime.cpp requires that this code be generated into a
 532   // RuntimeStub.
 533   address generate_throw_exception(const char* name, address runtime_entry, bool restore_saved_exception_pc,
 534                                    Register arg1 = noreg, Register arg2 = noreg) {
 535     CodeBuffer code(name, 1024 DEBUG_ONLY(+ 512), 0);
 536     MacroAssembler* masm = new MacroAssembler(&code);
 537 
 538     OopMapSet* oop_maps  = new OopMapSet();
 539     int frame_size_in_bytes = frame::abi_reg_args_size;
 540     OopMap* map = new OopMap(frame_size_in_bytes / sizeof(jint), 0);
 541 
 542     address start = __ pc();
 543 
 544     __ save_LR_CR(R11_scratch1);
 545 
 546     // Push a frame.
 547     __ push_frame_reg_args(0, R11_scratch1);
 548 
 549     address frame_complete_pc = __ pc();
 550 
 551     if (restore_saved_exception_pc) {
 552       __ unimplemented("StubGenerator::throw_exception with restore_saved_exception_pc", 74);
 553     }
 554 
 555     // Note that we always have a runtime stub frame on the top of
 556     // stack by this point. Remember the offset of the instruction
 557     // whose address will be moved to R11_scratch1.
 558     address gc_map_pc = __ get_PC_trash_LR(R11_scratch1);
 559 
 560     __ set_last_Java_frame(/*sp*/R1_SP, /*pc*/R11_scratch1);
 561 
 562     __ mr(R3_ARG1, R16_thread);
 563     if (arg1 != noreg) {
 564       __ mr(R4_ARG2, arg1);
 565     }
 566     if (arg2 != noreg) {
 567       __ mr(R5_ARG3, arg2);
 568     }
 569 #if defined(ABI_ELFv2)
 570     __ call_c(runtime_entry, relocInfo::none);
 571 #else
 572     __ call_c(CAST_FROM_FN_PTR(FunctionDescriptor*, runtime_entry), relocInfo::none);
 573 #endif
 574 
 575     // Set an oopmap for the call site.
 576     oop_maps->add_gc_map((int)(gc_map_pc - start), map);
 577 
 578     __ reset_last_Java_frame();
 579 
 580 #ifdef ASSERT
 581     // Make sure that this code is only executed if there is a pending
 582     // exception.
 583     {
 584       Label L;
 585       __ ld(R0,
 586                 in_bytes(Thread::pending_exception_offset()),
 587                 R16_thread);
 588       __ cmpdi(CCR0, R0, 0);
 589       __ bne(CCR0, L);
 590       __ stop("StubRoutines::throw_exception: no pending exception");
 591       __ bind(L);
 592     }
 593 #endif
 594 
 595     // Pop frame.
 596     __ pop_frame();
 597 
 598     __ restore_LR_CR(R11_scratch1);
 599 
 600     __ load_const(R11_scratch1, StubRoutines::forward_exception_entry());
 601     __ mtctr(R11_scratch1);
 602     __ bctr();
 603 
 604     // Create runtime stub with OopMap.
 605     RuntimeStub* stub =
 606       RuntimeStub::new_runtime_stub(name, &code,
 607                                     /*frame_complete=*/ (int)(frame_complete_pc - start),
 608                                     frame_size_in_bytes/wordSize,
 609                                     oop_maps,
 610                                     false);
 611     return stub->entry_point();
 612   }
 613 #undef __
 614 #define __ _masm->
 615 
 616   //  Generate G1 pre-write barrier for array.
 617   //
 618   //  Input:
 619   //     from     - register containing src address (only needed for spilling)
 620   //     to       - register containing starting address
 621   //     count    - register containing element count
 622   //     tmp      - scratch register
 623   //
 624   //  Kills:
 625   //     nothing
 626   //
 627   void gen_write_ref_array_pre_barrier(Register from, Register to, Register count, bool dest_uninitialized, Register Rtmp1,
 628                                        Register preserve1 = noreg, Register preserve2 = noreg) {
 629     BarrierSet* const bs = Universe::heap()->barrier_set();
 630     switch (bs->kind()) {
 631       case BarrierSet::G1SATBCTLogging:
 632         // With G1, don't generate the call if we statically know that the target in uninitialized
 633         if (!dest_uninitialized) {
 634           int spill_slots = 3;
 635           if (preserve1 != noreg) { spill_slots++; }
 636           if (preserve2 != noreg) { spill_slots++; }
 637           const int frame_size = align_size_up(frame::abi_reg_args_size + spill_slots * BytesPerWord, frame::alignment_in_bytes);
 638           Label filtered;
 639 
 640           // Is marking active?
 641           if (in_bytes(SATBMarkQueue::byte_width_of_active()) == 4) {
 642             __ lwz(Rtmp1, in_bytes(JavaThread::satb_mark_queue_offset() + SATBMarkQueue::byte_offset_of_active()), R16_thread);
 643           } else {
 644             guarantee(in_bytes(SATBMarkQueue::byte_width_of_active()) == 1, "Assumption");
 645             __ lbz(Rtmp1, in_bytes(JavaThread::satb_mark_queue_offset() + SATBMarkQueue::byte_offset_of_active()), R16_thread);
 646           }
 647           __ cmpdi(CCR0, Rtmp1, 0);
 648           __ beq(CCR0, filtered);
 649 
 650           __ save_LR_CR(R0);
 651           __ push_frame(frame_size, R0);
 652           int slot_nr = 0;
 653           __ std(from,  frame_size - (++slot_nr) * wordSize, R1_SP);
 654           __ std(to,    frame_size - (++slot_nr) * wordSize, R1_SP);
 655           __ std(count, frame_size - (++slot_nr) * wordSize, R1_SP);
 656           if (preserve1 != noreg) { __ std(preserve1, frame_size - (++slot_nr) * wordSize, R1_SP); }
 657           if (preserve2 != noreg) { __ std(preserve2, frame_size - (++slot_nr) * wordSize, R1_SP); }
 658 
 659           __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_pre), to, count);
 660 
 661           slot_nr = 0;
 662           __ ld(from,  frame_size - (++slot_nr) * wordSize, R1_SP);
 663           __ ld(to,    frame_size - (++slot_nr) * wordSize, R1_SP);
 664           __ ld(count, frame_size - (++slot_nr) * wordSize, R1_SP);
 665           if (preserve1 != noreg) { __ ld(preserve1, frame_size - (++slot_nr) * wordSize, R1_SP); }
 666           if (preserve2 != noreg) { __ ld(preserve2, frame_size - (++slot_nr) * wordSize, R1_SP); }
 667           __ addi(R1_SP, R1_SP, frame_size); // pop_frame()
 668           __ restore_LR_CR(R0);
 669 
 670           __ bind(filtered);
 671         }
 672         break;
 673       case BarrierSet::CardTableForRS:
 674       case BarrierSet::CardTableExtension:
 675       case BarrierSet::ModRef:
 676         break;
 677       default:
 678         ShouldNotReachHere();
 679     }
 680   }
 681 
 682   //  Generate CMS/G1 post-write barrier for array.
 683   //
 684   //  Input:
 685   //     addr     - register containing starting address
 686   //     count    - register containing element count
 687   //     tmp      - scratch register
 688   //
 689   //  The input registers and R0 are overwritten.
 690   //
 691   void gen_write_ref_array_post_barrier(Register addr, Register count, Register tmp, Register preserve = noreg) {
 692     BarrierSet* const bs = Universe::heap()->barrier_set();
 693 
 694     switch (bs->kind()) {
 695       case BarrierSet::G1SATBCTLogging:
 696         {
 697           int spill_slots = (preserve != noreg) ? 1 : 0;
 698           const int frame_size = align_size_up(frame::abi_reg_args_size + spill_slots * BytesPerWord, frame::alignment_in_bytes);
 699 
 700           __ save_LR_CR(R0);
 701           __ push_frame(frame_size, R0);
 702           if (preserve != noreg) { __ std(preserve, frame_size - 1 * wordSize, R1_SP); }
 703           __ call_VM_leaf(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_post), addr, count);
 704           if (preserve != noreg) { __ ld(preserve, frame_size - 1 * wordSize, R1_SP); }
 705           __ addi(R1_SP, R1_SP, frame_size); // pop_frame();
 706           __ restore_LR_CR(R0);
 707         }
 708         break;
 709       case BarrierSet::CardTableForRS:
 710       case BarrierSet::CardTableExtension:
 711         {
 712           Label Lskip_loop, Lstore_loop;
 713           if (UseConcMarkSweepGC) {
 714             // TODO PPC port: contribute optimization / requires shared changes
 715             __ release();
 716           }
 717 
 718           CardTableModRefBS* const ct = barrier_set_cast<CardTableModRefBS>(bs);
 719           assert(sizeof(*ct->byte_map_base) == sizeof(jbyte), "adjust this code");
 720           assert_different_registers(addr, count, tmp);
 721 
 722           __ sldi(count, count, LogBytesPerHeapOop);
 723           __ addi(count, count, -BytesPerHeapOop);
 724           __ add(count, addr, count);
 725           // Use two shifts to clear out those low order two bits! (Cannot opt. into 1.)
 726           __ srdi(addr, addr, CardTableModRefBS::card_shift);
 727           __ srdi(count, count, CardTableModRefBS::card_shift);
 728           __ subf(count, addr, count);
 729           assert_different_registers(R0, addr, count, tmp);
 730           __ load_const(tmp, (address)ct->byte_map_base);
 731           __ addic_(count, count, 1);
 732           __ beq(CCR0, Lskip_loop);
 733           __ li(R0, 0);
 734           __ mtctr(count);
 735           // Byte store loop
 736           __ bind(Lstore_loop);
 737           __ stbx(R0, tmp, addr);
 738           __ addi(addr, addr, 1);
 739           __ bdnz(Lstore_loop);
 740           __ bind(Lskip_loop);
 741         }
 742       break;
 743       case BarrierSet::ModRef:
 744         break;
 745       default:
 746         ShouldNotReachHere();
 747     }
 748   }
 749 
 750   // Support for void zero_words_aligned8(HeapWord* to, size_t count)
 751   //
 752   // Arguments:
 753   //   to:
 754   //   count:
 755   //
 756   // Destroys:
 757   //
 758   address generate_zero_words_aligned8() {
 759     StubCodeMark mark(this, "StubRoutines", "zero_words_aligned8");
 760 
 761     // Implemented as in ClearArray.
 762     address start = __ function_entry();
 763 
 764     Register base_ptr_reg   = R3_ARG1; // tohw (needs to be 8b aligned)
 765     Register cnt_dwords_reg = R4_ARG2; // count (in dwords)
 766     Register tmp1_reg       = R5_ARG3;
 767     Register tmp2_reg       = R6_ARG4;
 768     Register zero_reg       = R7_ARG5;
 769 
 770     // Procedure for large arrays (uses data cache block zero instruction).
 771     Label dwloop, fast, fastloop, restloop, lastdword, done;
 772     int cl_size = VM_Version::L1_data_cache_line_size();
 773     int cl_dwords = cl_size >> 3;
 774     int cl_dwordaddr_bits = exact_log2(cl_dwords);
 775     int min_dcbz = 2; // Needs to be positive, apply dcbz only to at least min_dcbz cache lines.
 776 
 777     // Clear up to 128byte boundary if long enough, dword_cnt=(16-(base>>3))%16.
 778     __ dcbtst(base_ptr_reg);                    // Indicate write access to first cache line ...
 779     __ andi(tmp2_reg, cnt_dwords_reg, 1);       // to check if number of dwords is even.
 780     __ srdi_(tmp1_reg, cnt_dwords_reg, 1);      // number of double dwords
 781     __ load_const_optimized(zero_reg, 0L);      // Use as zero register.
 782 
 783     __ cmpdi(CCR1, tmp2_reg, 0);                // cnt_dwords even?
 784     __ beq(CCR0, lastdword);                    // size <= 1
 785     __ mtctr(tmp1_reg);                         // Speculatively preload counter for rest loop (>0).
 786     __ cmpdi(CCR0, cnt_dwords_reg, (min_dcbz+1)*cl_dwords-1); // Big enough to ensure >=min_dcbz cache lines are included?
 787     __ neg(tmp1_reg, base_ptr_reg);             // bit 0..58: bogus, bit 57..60: (16-(base>>3))%16, bit 61..63: 000
 788 
 789     __ blt(CCR0, restloop);                     // Too small. (<31=(2*cl_dwords)-1 is sufficient, but bigger performs better.)
 790     __ rldicl_(tmp1_reg, tmp1_reg, 64-3, 64-cl_dwordaddr_bits); // Extract number of dwords to 128byte boundary=(16-(base>>3))%16.
 791 
 792     __ beq(CCR0, fast);                         // already 128byte aligned
 793     __ mtctr(tmp1_reg);                         // Set ctr to hit 128byte boundary (0<ctr<cnt).
 794     __ subf(cnt_dwords_reg, tmp1_reg, cnt_dwords_reg); // rest (>0 since size>=256-8)
 795 
 796     // Clear in first cache line dword-by-dword if not already 128byte aligned.
 797     __ bind(dwloop);
 798       __ std(zero_reg, 0, base_ptr_reg);        // Clear 8byte aligned block.
 799       __ addi(base_ptr_reg, base_ptr_reg, 8);
 800     __ bdnz(dwloop);
 801 
 802     // clear 128byte blocks
 803     __ bind(fast);
 804     __ srdi(tmp1_reg, cnt_dwords_reg, cl_dwordaddr_bits); // loop count for 128byte loop (>0 since size>=256-8)
 805     __ andi(tmp2_reg, cnt_dwords_reg, 1);       // to check if rest even
 806 
 807     __ mtctr(tmp1_reg);                         // load counter
 808     __ cmpdi(CCR1, tmp2_reg, 0);                // rest even?
 809     __ rldicl_(tmp1_reg, cnt_dwords_reg, 63, 65-cl_dwordaddr_bits); // rest in double dwords
 810 
 811     __ bind(fastloop);
 812       __ dcbz(base_ptr_reg);                    // Clear 128byte aligned block.
 813       __ addi(base_ptr_reg, base_ptr_reg, cl_size);
 814     __ bdnz(fastloop);
 815 
 816     //__ dcbtst(base_ptr_reg);                  // Indicate write access to last cache line.
 817     __ beq(CCR0, lastdword);                    // rest<=1
 818     __ mtctr(tmp1_reg);                         // load counter
 819 
 820     // Clear rest.
 821     __ bind(restloop);
 822       __ std(zero_reg, 0, base_ptr_reg);        // Clear 8byte aligned block.
 823       __ std(zero_reg, 8, base_ptr_reg);        // Clear 8byte aligned block.
 824       __ addi(base_ptr_reg, base_ptr_reg, 16);
 825     __ bdnz(restloop);
 826 
 827     __ bind(lastdword);
 828     __ beq(CCR1, done);
 829     __ std(zero_reg, 0, base_ptr_reg);
 830     __ bind(done);
 831     __ blr();                                   // return
 832 
 833     return start;
 834   }
 835 
 836   // The following routine generates a subroutine to throw an asynchronous
 837   // UnknownError when an unsafe access gets a fault that could not be
 838   // reasonably prevented by the programmer.  (Example: SIGBUS/OBJERR.)
 839   //
 840   address generate_handler_for_unsafe_access() {
 841     StubCodeMark mark(this, "StubRoutines", "handler_for_unsafe_access");
 842     address start = __ function_entry();
 843     __ unimplemented("StubRoutines::handler_for_unsafe_access", 93);
 844     return start;
 845   }
 846 
 847 #if !defined(PRODUCT)
 848   // Wrapper which calls oopDesc::is_oop_or_null()
 849   // Only called by MacroAssembler::verify_oop
 850   static void verify_oop_helper(const char* message, oop o) {
 851     if (!o->is_oop_or_null()) {
 852       fatal("%s", message);
 853     }
 854     ++ StubRoutines::_verify_oop_count;
 855   }
 856 #endif
 857 
 858   // Return address of code to be called from code generated by
 859   // MacroAssembler::verify_oop.
 860   //
 861   // Don't generate, rather use C++ code.
 862   address generate_verify_oop() {
 863     // this is actually a `FunctionDescriptor*'.
 864     address start = 0;
 865 
 866 #if !defined(PRODUCT)
 867     start = CAST_FROM_FN_PTR(address, verify_oop_helper);
 868 #endif
 869 
 870     return start;
 871   }
 872 
 873   // Fairer handling of safepoints for native methods.
 874   //
 875   // Generate code which reads from the polling page. This special handling is needed as the
 876   // linux-ppc64 kernel before 2.6.6 doesn't set si_addr on some segfaults in 64bit mode
 877   // (cf. http://www.kernel.org/pub/linux/kernel/v2.6/ChangeLog-2.6.6), especially when we try
 878   // to read from the safepoint polling page.
 879   address generate_load_from_poll() {
 880     StubCodeMark mark(this, "StubRoutines", "generate_load_from_poll");
 881     address start = __ function_entry();
 882     __ unimplemented("StubRoutines::verify_oop", 95);  // TODO PPC port
 883     return start;
 884   }
 885 
 886   // -XX:+OptimizeFill : convert fill/copy loops into intrinsic
 887   //
 888   // The code is implemented(ported from sparc) as we believe it benefits JVM98, however
 889   // tracing(-XX:+TraceOptimizeFill) shows the intrinsic replacement doesn't happen at all!
 890   //
 891   // Source code in function is_range_check_if() shows that OptimizeFill relaxed the condition
 892   // for turning on loop predication optimization, and hence the behavior of "array range check"
 893   // and "loop invariant check" could be influenced, which potentially boosted JVM98.
 894   //
 895   // Generate stub for disjoint short fill. If "aligned" is true, the
 896   // "to" address is assumed to be heapword aligned.
 897   //
 898   // Arguments for generated stub:
 899   //   to:    R3_ARG1
 900   //   value: R4_ARG2
 901   //   count: R5_ARG3 treated as signed
 902   //
 903   address generate_fill(BasicType t, bool aligned, const char* name) {
 904     StubCodeMark mark(this, "StubRoutines", name);
 905     address start = __ function_entry();
 906 
 907     const Register to    = R3_ARG1;   // source array address
 908     const Register value = R4_ARG2;   // fill value
 909     const Register count = R5_ARG3;   // elements count
 910     const Register temp  = R6_ARG4;   // temp register
 911 
 912     //assert_clean_int(count, O3);    // Make sure 'count' is clean int.
 913 
 914     Label L_exit, L_skip_align1, L_skip_align2, L_fill_byte;
 915     Label L_fill_2_bytes, L_fill_4_bytes, L_fill_elements, L_fill_32_bytes;
 916 
 917     int shift = -1;
 918     switch (t) {
 919        case T_BYTE:
 920         shift = 2;
 921         // Clone bytes (zero extend not needed because store instructions below ignore high order bytes).
 922         __ rldimi(value, value, 8, 48);     // 8 bit -> 16 bit
 923         __ cmpdi(CCR0, count, 2<<shift);    // Short arrays (< 8 bytes) fill by element.
 924         __ blt(CCR0, L_fill_elements);
 925         __ rldimi(value, value, 16, 32);    // 16 bit -> 32 bit
 926         break;
 927        case T_SHORT:
 928         shift = 1;
 929         // Clone bytes (zero extend not needed because store instructions below ignore high order bytes).
 930         __ rldimi(value, value, 16, 32);    // 16 bit -> 32 bit
 931         __ cmpdi(CCR0, count, 2<<shift);    // Short arrays (< 8 bytes) fill by element.
 932         __ blt(CCR0, L_fill_elements);
 933         break;
 934       case T_INT:
 935         shift = 0;
 936         __ cmpdi(CCR0, count, 2<<shift);    // Short arrays (< 8 bytes) fill by element.
 937         __ blt(CCR0, L_fill_4_bytes);
 938         break;
 939       default: ShouldNotReachHere();
 940     }
 941 
 942     if (!aligned && (t == T_BYTE || t == T_SHORT)) {
 943       // Align source address at 4 bytes address boundary.
 944       if (t == T_BYTE) {
 945         // One byte misalignment happens only for byte arrays.
 946         __ andi_(temp, to, 1);
 947         __ beq(CCR0, L_skip_align1);
 948         __ stb(value, 0, to);
 949         __ addi(to, to, 1);
 950         __ addi(count, count, -1);
 951         __ bind(L_skip_align1);
 952       }
 953       // Two bytes misalignment happens only for byte and short (char) arrays.
 954       __ andi_(temp, to, 2);
 955       __ beq(CCR0, L_skip_align2);
 956       __ sth(value, 0, to);
 957       __ addi(to, to, 2);
 958       __ addi(count, count, -(1 << (shift - 1)));
 959       __ bind(L_skip_align2);
 960     }
 961 
 962     if (!aligned) {
 963       // Align to 8 bytes, we know we are 4 byte aligned to start.
 964       __ andi_(temp, to, 7);
 965       __ beq(CCR0, L_fill_32_bytes);
 966       __ stw(value, 0, to);
 967       __ addi(to, to, 4);
 968       __ addi(count, count, -(1 << shift));
 969       __ bind(L_fill_32_bytes);
 970     }
 971 
 972     __ li(temp, 8<<shift);                  // Prepare for 32 byte loop.
 973     // Clone bytes int->long as above.
 974     __ rldimi(value, value, 32, 0);         // 32 bit -> 64 bit
 975 
 976     Label L_check_fill_8_bytes;
 977     // Fill 32-byte chunks.
 978     __ subf_(count, temp, count);
 979     __ blt(CCR0, L_check_fill_8_bytes);
 980 
 981     Label L_fill_32_bytes_loop;
 982     __ align(32);
 983     __ bind(L_fill_32_bytes_loop);
 984 
 985     __ std(value, 0, to);
 986     __ std(value, 8, to);
 987     __ subf_(count, temp, count);           // Update count.
 988     __ std(value, 16, to);
 989     __ std(value, 24, to);
 990 
 991     __ addi(to, to, 32);
 992     __ bge(CCR0, L_fill_32_bytes_loop);
 993 
 994     __ bind(L_check_fill_8_bytes);
 995     __ add_(count, temp, count);
 996     __ beq(CCR0, L_exit);
 997     __ addic_(count, count, -(2 << shift));
 998     __ blt(CCR0, L_fill_4_bytes);
 999 
1000     //
1001     // Length is too short, just fill 8 bytes at a time.
1002     //
1003     Label L_fill_8_bytes_loop;
1004     __ bind(L_fill_8_bytes_loop);
1005     __ std(value, 0, to);
1006     __ addic_(count, count, -(2 << shift));
1007     __ addi(to, to, 8);
1008     __ bge(CCR0, L_fill_8_bytes_loop);
1009 
1010     // Fill trailing 4 bytes.
1011     __ bind(L_fill_4_bytes);
1012     __ andi_(temp, count, 1<<shift);
1013     __ beq(CCR0, L_fill_2_bytes);
1014 
1015     __ stw(value, 0, to);
1016     if (t == T_BYTE || t == T_SHORT) {
1017       __ addi(to, to, 4);
1018       // Fill trailing 2 bytes.
1019       __ bind(L_fill_2_bytes);
1020       __ andi_(temp, count, 1<<(shift-1));
1021       __ beq(CCR0, L_fill_byte);
1022       __ sth(value, 0, to);
1023       if (t == T_BYTE) {
1024         __ addi(to, to, 2);
1025         // Fill trailing byte.
1026         __ bind(L_fill_byte);
1027         __ andi_(count, count, 1);
1028         __ beq(CCR0, L_exit);
1029         __ stb(value, 0, to);
1030       } else {
1031         __ bind(L_fill_byte);
1032       }
1033     } else {
1034       __ bind(L_fill_2_bytes);
1035     }
1036     __ bind(L_exit);
1037     __ blr();
1038 
1039     // Handle copies less than 8 bytes. Int is handled elsewhere.
1040     if (t == T_BYTE) {
1041       __ bind(L_fill_elements);
1042       Label L_fill_2, L_fill_4;
1043       __ andi_(temp, count, 1);
1044       __ beq(CCR0, L_fill_2);
1045       __ stb(value, 0, to);
1046       __ addi(to, to, 1);
1047       __ bind(L_fill_2);
1048       __ andi_(temp, count, 2);
1049       __ beq(CCR0, L_fill_4);
1050       __ stb(value, 0, to);
1051       __ stb(value, 0, to);
1052       __ addi(to, to, 2);
1053       __ bind(L_fill_4);
1054       __ andi_(temp, count, 4);
1055       __ beq(CCR0, L_exit);
1056       __ stb(value, 0, to);
1057       __ stb(value, 1, to);
1058       __ stb(value, 2, to);
1059       __ stb(value, 3, to);
1060       __ blr();
1061     }
1062 
1063     if (t == T_SHORT) {
1064       Label L_fill_2;
1065       __ bind(L_fill_elements);
1066       __ andi_(temp, count, 1);
1067       __ beq(CCR0, L_fill_2);
1068       __ sth(value, 0, to);
1069       __ addi(to, to, 2);
1070       __ bind(L_fill_2);
1071       __ andi_(temp, count, 2);
1072       __ beq(CCR0, L_exit);
1073       __ sth(value, 0, to);
1074       __ sth(value, 2, to);
1075       __ blr();
1076     }
1077     return start;
1078   }
1079 
1080 
1081   // Generate overlap test for array copy stubs.
1082   //
1083   // Input:
1084   //   R3_ARG1    -  from
1085   //   R4_ARG2    -  to
1086   //   R5_ARG3    -  element count
1087   //
1088   void array_overlap_test(address no_overlap_target, int log2_elem_size) {
1089     Register tmp1 = R6_ARG4;
1090     Register tmp2 = R7_ARG5;
1091 
1092 #ifdef ASSERT
1093     __ srdi_(tmp2, R5_ARG3, 31);
1094     __ asm_assert_eq("missing zero extend", 0xAFFE);
1095 #endif
1096 
1097     __ subf(tmp1, R3_ARG1, R4_ARG2); // distance in bytes
1098     __ sldi(tmp2, R5_ARG3, log2_elem_size); // size in bytes
1099     __ cmpld(CCR0, R3_ARG1, R4_ARG2); // Use unsigned comparison!
1100     __ cmpld(CCR1, tmp1, tmp2);
1101     __ crnand(CCR0, Assembler::less, CCR1, Assembler::less);
1102     // Overlaps if Src before dst and distance smaller than size.
1103     // Branch to forward copy routine otherwise (within range of 32kB).
1104     __ bc(Assembler::bcondCRbiIs1, Assembler::bi0(CCR0, Assembler::less), no_overlap_target);
1105 
1106     // need to copy backwards
1107   }
1108 
1109   // The guideline in the implementations of generate_disjoint_xxx_copy
1110   // (xxx=byte,short,int,long,oop) is to copy as many elements as possible with
1111   // single instructions, but to avoid alignment interrupts (see subsequent
1112   // comment). Furthermore, we try to minimize misaligned access, even
1113   // though they cause no alignment interrupt.
1114   //
1115   // In Big-Endian mode, the PowerPC architecture requires implementations to
1116   // handle automatically misaligned integer halfword and word accesses,
1117   // word-aligned integer doubleword accesses, and word-aligned floating-point
1118   // accesses. Other accesses may or may not generate an Alignment interrupt
1119   // depending on the implementation.
1120   // Alignment interrupt handling may require on the order of hundreds of cycles,
1121   // so every effort should be made to avoid misaligned memory values.
1122   //
1123   //
1124   // Generate stub for disjoint byte copy.  If "aligned" is true, the
1125   // "from" and "to" addresses are assumed to be heapword aligned.
1126   //
1127   // Arguments for generated stub:
1128   //      from:  R3_ARG1
1129   //      to:    R4_ARG2
1130   //      count: R5_ARG3 treated as signed
1131   //
1132   address generate_disjoint_byte_copy(bool aligned, const char * name) {
1133     StubCodeMark mark(this, "StubRoutines", name);
1134     address start = __ function_entry();
1135 
1136     Register tmp1 = R6_ARG4;
1137     Register tmp2 = R7_ARG5;
1138     Register tmp3 = R8_ARG6;
1139     Register tmp4 = R9_ARG7;
1140 
1141 
1142     Label l_1, l_2, l_3, l_4, l_5, l_6, l_7, l_8, l_9;
1143     // Don't try anything fancy if arrays don't have many elements.
1144     __ li(tmp3, 0);
1145     __ cmpwi(CCR0, R5_ARG3, 17);
1146     __ ble(CCR0, l_6); // copy 4 at a time
1147 
1148     if (!aligned) {
1149       __ xorr(tmp1, R3_ARG1, R4_ARG2);
1150       __ andi_(tmp1, tmp1, 3);
1151       __ bne(CCR0, l_6); // If arrays don't have the same alignment mod 4, do 4 element copy.
1152 
1153       // Copy elements if necessary to align to 4 bytes.
1154       __ neg(tmp1, R3_ARG1); // Compute distance to alignment boundary.
1155       __ andi_(tmp1, tmp1, 3);
1156       __ beq(CCR0, l_2);
1157 
1158       __ subf(R5_ARG3, tmp1, R5_ARG3);
1159       __ bind(l_9);
1160       __ lbz(tmp2, 0, R3_ARG1);
1161       __ addic_(tmp1, tmp1, -1);
1162       __ stb(tmp2, 0, R4_ARG2);
1163       __ addi(R3_ARG1, R3_ARG1, 1);
1164       __ addi(R4_ARG2, R4_ARG2, 1);
1165       __ bne(CCR0, l_9);
1166 
1167       __ bind(l_2);
1168     }
1169 
1170     // copy 8 elements at a time
1171     __ xorr(tmp2, R3_ARG1, R4_ARG2); // skip if src & dest have differing alignment mod 8
1172     __ andi_(tmp1, tmp2, 7);
1173     __ bne(CCR0, l_7); // not same alignment -> to or from is aligned -> copy 8
1174 
1175     // copy a 2-element word if necessary to align to 8 bytes
1176     __ andi_(R0, R3_ARG1, 7);
1177     __ beq(CCR0, l_7);
1178 
1179     __ lwzx(tmp2, R3_ARG1, tmp3);
1180     __ addi(R5_ARG3, R5_ARG3, -4);
1181     __ stwx(tmp2, R4_ARG2, tmp3);
1182     { // FasterArrayCopy
1183       __ addi(R3_ARG1, R3_ARG1, 4);
1184       __ addi(R4_ARG2, R4_ARG2, 4);
1185     }
1186     __ bind(l_7);
1187 
1188     { // FasterArrayCopy
1189       __ cmpwi(CCR0, R5_ARG3, 31);
1190       __ ble(CCR0, l_6); // copy 2 at a time if less than 32 elements remain
1191 
1192       __ srdi(tmp1, R5_ARG3, 5);
1193       __ andi_(R5_ARG3, R5_ARG3, 31);
1194       __ mtctr(tmp1);
1195 
1196       __ bind(l_8);
1197       // Use unrolled version for mass copying (copy 32 elements a time)
1198       // Load feeding store gets zero latency on Power6, however not on Power5.
1199       // Therefore, the following sequence is made for the good of both.
1200       __ ld(tmp1, 0, R3_ARG1);
1201       __ ld(tmp2, 8, R3_ARG1);
1202       __ ld(tmp3, 16, R3_ARG1);
1203       __ ld(tmp4, 24, R3_ARG1);
1204       __ std(tmp1, 0, R4_ARG2);
1205       __ std(tmp2, 8, R4_ARG2);
1206       __ std(tmp3, 16, R4_ARG2);
1207       __ std(tmp4, 24, R4_ARG2);
1208       __ addi(R3_ARG1, R3_ARG1, 32);
1209       __ addi(R4_ARG2, R4_ARG2, 32);
1210       __ bdnz(l_8);
1211     }
1212 
1213     __ bind(l_6);
1214 
1215     // copy 4 elements at a time
1216     __ cmpwi(CCR0, R5_ARG3, 4);
1217     __ blt(CCR0, l_1);
1218     __ srdi(tmp1, R5_ARG3, 2);
1219     __ mtctr(tmp1); // is > 0
1220     __ andi_(R5_ARG3, R5_ARG3, 3);
1221 
1222     { // FasterArrayCopy
1223       __ addi(R3_ARG1, R3_ARG1, -4);
1224       __ addi(R4_ARG2, R4_ARG2, -4);
1225       __ bind(l_3);
1226       __ lwzu(tmp2, 4, R3_ARG1);
1227       __ stwu(tmp2, 4, R4_ARG2);
1228       __ bdnz(l_3);
1229       __ addi(R3_ARG1, R3_ARG1, 4);
1230       __ addi(R4_ARG2, R4_ARG2, 4);
1231     }
1232 
1233     // do single element copy
1234     __ bind(l_1);
1235     __ cmpwi(CCR0, R5_ARG3, 0);
1236     __ beq(CCR0, l_4);
1237 
1238     { // FasterArrayCopy
1239       __ mtctr(R5_ARG3);
1240       __ addi(R3_ARG1, R3_ARG1, -1);
1241       __ addi(R4_ARG2, R4_ARG2, -1);
1242 
1243       __ bind(l_5);
1244       __ lbzu(tmp2, 1, R3_ARG1);
1245       __ stbu(tmp2, 1, R4_ARG2);
1246       __ bdnz(l_5);
1247     }
1248 
1249     __ bind(l_4);
1250     __ li(R3_RET, 0); // return 0
1251     __ blr();
1252 
1253     return start;
1254   }
1255 
1256   // Generate stub for conjoint byte copy.  If "aligned" is true, the
1257   // "from" and "to" addresses are assumed to be heapword aligned.
1258   //
1259   // Arguments for generated stub:
1260   //      from:  R3_ARG1
1261   //      to:    R4_ARG2
1262   //      count: R5_ARG3 treated as signed
1263   //
1264   address generate_conjoint_byte_copy(bool aligned, const char * name) {
1265     StubCodeMark mark(this, "StubRoutines", name);
1266     address start = __ function_entry();
1267 
1268     Register tmp1 = R6_ARG4;
1269     Register tmp2 = R7_ARG5;
1270     Register tmp3 = R8_ARG6;
1271 
1272     address nooverlap_target = aligned ?
1273       STUB_ENTRY(arrayof_jbyte_disjoint_arraycopy) :
1274       STUB_ENTRY(jbyte_disjoint_arraycopy);
1275 
1276     array_overlap_test(nooverlap_target, 0);
1277     // Do reverse copy. We assume the case of actual overlap is rare enough
1278     // that we don't have to optimize it.
1279     Label l_1, l_2;
1280 
1281     __ b(l_2);
1282     __ bind(l_1);
1283     __ stbx(tmp1, R4_ARG2, R5_ARG3);
1284     __ bind(l_2);
1285     __ addic_(R5_ARG3, R5_ARG3, -1);
1286     __ lbzx(tmp1, R3_ARG1, R5_ARG3);
1287     __ bge(CCR0, l_1);
1288 
1289     __ li(R3_RET, 0); // return 0
1290     __ blr();
1291 
1292     return start;
1293   }
1294 
1295   // Generate stub for disjoint short copy.  If "aligned" is true, the
1296   // "from" and "to" addresses are assumed to be heapword aligned.
1297   //
1298   // Arguments for generated stub:
1299   //      from:  R3_ARG1
1300   //      to:    R4_ARG2
1301   //  elm.count: R5_ARG3 treated as signed
1302   //
1303   // Strategy for aligned==true:
1304   //
1305   //  If length <= 9:
1306   //     1. copy 2 elements at a time (l_6)
1307   //     2. copy last element if original element count was odd (l_1)
1308   //
1309   //  If length > 9:
1310   //     1. copy 4 elements at a time until less than 4 elements are left (l_7)
1311   //     2. copy 2 elements at a time until less than 2 elements are left (l_6)
1312   //     3. copy last element if one was left in step 2. (l_1)
1313   //
1314   //
1315   // Strategy for aligned==false:
1316   //
1317   //  If length <= 9: same as aligned==true case, but NOTE: load/stores
1318   //                  can be unaligned (see comment below)
1319   //
1320   //  If length > 9:
1321   //     1. continue with step 6. if the alignment of from and to mod 4
1322   //        is different.
1323   //     2. align from and to to 4 bytes by copying 1 element if necessary
1324   //     3. at l_2 from and to are 4 byte aligned; continue with
1325   //        5. if they cannot be aligned to 8 bytes because they have
1326   //        got different alignment mod 8.
1327   //     4. at this point we know that both, from and to, have the same
1328   //        alignment mod 8, now copy one element if necessary to get
1329   //        8 byte alignment of from and to.
1330   //     5. copy 4 elements at a time until less than 4 elements are
1331   //        left; depending on step 3. all load/stores are aligned or
1332   //        either all loads or all stores are unaligned.
1333   //     6. copy 2 elements at a time until less than 2 elements are
1334   //        left (l_6); arriving here from step 1., there is a chance
1335   //        that all accesses are unaligned.
1336   //     7. copy last element if one was left in step 6. (l_1)
1337   //
1338   //  There are unaligned data accesses using integer load/store
1339   //  instructions in this stub. POWER allows such accesses.
1340   //
1341   //  According to the manuals (PowerISA_V2.06_PUBLIC, Book II,
1342   //  Chapter 2: Effect of Operand Placement on Performance) unaligned
1343   //  integer load/stores have good performance. Only unaligned
1344   //  floating point load/stores can have poor performance.
1345   //
1346   //  TODO:
1347   //
1348   //  1. check if aligning the backbranch target of loops is beneficial
1349   //
1350   address generate_disjoint_short_copy(bool aligned, const char * name) {
1351     StubCodeMark mark(this, "StubRoutines", name);
1352 
1353     Register tmp1 = R6_ARG4;
1354     Register tmp2 = R7_ARG5;
1355     Register tmp3 = R8_ARG6;
1356     Register tmp4 = R9_ARG7;
1357 
1358     address start = __ function_entry();
1359 
1360       Label l_1, l_2, l_3, l_4, l_5, l_6, l_7, l_8;
1361     // don't try anything fancy if arrays don't have many elements
1362     __ li(tmp3, 0);
1363     __ cmpwi(CCR0, R5_ARG3, 9);
1364     __ ble(CCR0, l_6); // copy 2 at a time
1365 
1366     if (!aligned) {
1367       __ xorr(tmp1, R3_ARG1, R4_ARG2);
1368       __ andi_(tmp1, tmp1, 3);
1369       __ bne(CCR0, l_6); // if arrays don't have the same alignment mod 4, do 2 element copy
1370 
1371       // At this point it is guaranteed that both, from and to have the same alignment mod 4.
1372 
1373       // Copy 1 element if necessary to align to 4 bytes.
1374       __ andi_(tmp1, R3_ARG1, 3);
1375       __ beq(CCR0, l_2);
1376 
1377       __ lhz(tmp2, 0, R3_ARG1);
1378       __ addi(R3_ARG1, R3_ARG1, 2);
1379       __ sth(tmp2, 0, R4_ARG2);
1380       __ addi(R4_ARG2, R4_ARG2, 2);
1381       __ addi(R5_ARG3, R5_ARG3, -1);
1382       __ bind(l_2);
1383 
1384       // At this point the positions of both, from and to, are at least 4 byte aligned.
1385 
1386       // Copy 4 elements at a time.
1387       // Align to 8 bytes, but only if both, from and to, have same alignment mod 8.
1388       __ xorr(tmp2, R3_ARG1, R4_ARG2);
1389       __ andi_(tmp1, tmp2, 7);
1390       __ bne(CCR0, l_7); // not same alignment mod 8 -> copy 4, either from or to will be unaligned
1391 
1392       // Copy a 2-element word if necessary to align to 8 bytes.
1393       __ andi_(R0, R3_ARG1, 7);
1394       __ beq(CCR0, l_7);
1395 
1396       __ lwzx(tmp2, R3_ARG1, tmp3);
1397       __ addi(R5_ARG3, R5_ARG3, -2);
1398       __ stwx(tmp2, R4_ARG2, tmp3);
1399       { // FasterArrayCopy
1400         __ addi(R3_ARG1, R3_ARG1, 4);
1401         __ addi(R4_ARG2, R4_ARG2, 4);
1402       }
1403     }
1404 
1405     __ bind(l_7);
1406 
1407     // Copy 4 elements at a time; either the loads or the stores can
1408     // be unaligned if aligned == false.
1409 
1410     { // FasterArrayCopy
1411       __ cmpwi(CCR0, R5_ARG3, 15);
1412       __ ble(CCR0, l_6); // copy 2 at a time if less than 16 elements remain
1413 
1414       __ srdi(tmp1, R5_ARG3, 4);
1415       __ andi_(R5_ARG3, R5_ARG3, 15);
1416       __ mtctr(tmp1);
1417 
1418       __ bind(l_8);
1419       // Use unrolled version for mass copying (copy 16 elements a time).
1420       // Load feeding store gets zero latency on Power6, however not on Power5.
1421       // Therefore, the following sequence is made for the good of both.
1422       __ ld(tmp1, 0, R3_ARG1);
1423       __ ld(tmp2, 8, R3_ARG1);
1424       __ ld(tmp3, 16, R3_ARG1);
1425       __ ld(tmp4, 24, R3_ARG1);
1426       __ std(tmp1, 0, R4_ARG2);
1427       __ std(tmp2, 8, R4_ARG2);
1428       __ std(tmp3, 16, R4_ARG2);
1429       __ std(tmp4, 24, R4_ARG2);
1430       __ addi(R3_ARG1, R3_ARG1, 32);
1431       __ addi(R4_ARG2, R4_ARG2, 32);
1432       __ bdnz(l_8);
1433     }
1434     __ bind(l_6);
1435 
1436     // copy 2 elements at a time
1437     { // FasterArrayCopy
1438       __ cmpwi(CCR0, R5_ARG3, 2);
1439       __ blt(CCR0, l_1);
1440       __ srdi(tmp1, R5_ARG3, 1);
1441       __ andi_(R5_ARG3, R5_ARG3, 1);
1442 
1443       __ addi(R3_ARG1, R3_ARG1, -4);
1444       __ addi(R4_ARG2, R4_ARG2, -4);
1445       __ mtctr(tmp1);
1446 
1447       __ bind(l_3);
1448       __ lwzu(tmp2, 4, R3_ARG1);
1449       __ stwu(tmp2, 4, R4_ARG2);
1450       __ bdnz(l_3);
1451 
1452       __ addi(R3_ARG1, R3_ARG1, 4);
1453       __ addi(R4_ARG2, R4_ARG2, 4);
1454     }
1455 
1456     // do single element copy
1457     __ bind(l_1);
1458     __ cmpwi(CCR0, R5_ARG3, 0);
1459     __ beq(CCR0, l_4);
1460 
1461     { // FasterArrayCopy
1462       __ mtctr(R5_ARG3);
1463       __ addi(R3_ARG1, R3_ARG1, -2);
1464       __ addi(R4_ARG2, R4_ARG2, -2);
1465 
1466       __ bind(l_5);
1467       __ lhzu(tmp2, 2, R3_ARG1);
1468       __ sthu(tmp2, 2, R4_ARG2);
1469       __ bdnz(l_5);
1470     }
1471     __ bind(l_4);
1472     __ li(R3_RET, 0); // return 0
1473     __ blr();
1474 
1475     return start;
1476   }
1477 
1478   // Generate stub for conjoint short copy.  If "aligned" is true, the
1479   // "from" and "to" addresses are assumed to be heapword aligned.
1480   //
1481   // Arguments for generated stub:
1482   //      from:  R3_ARG1
1483   //      to:    R4_ARG2
1484   //      count: R5_ARG3 treated as signed
1485   //
1486   address generate_conjoint_short_copy(bool aligned, const char * name) {
1487     StubCodeMark mark(this, "StubRoutines", name);
1488     address start = __ function_entry();
1489 
1490     Register tmp1 = R6_ARG4;
1491     Register tmp2 = R7_ARG5;
1492     Register tmp3 = R8_ARG6;
1493 
1494     address nooverlap_target = aligned ?
1495       STUB_ENTRY(arrayof_jshort_disjoint_arraycopy) :
1496       STUB_ENTRY(jshort_disjoint_arraycopy);
1497 
1498     array_overlap_test(nooverlap_target, 1);
1499 
1500     Label l_1, l_2;
1501     __ sldi(tmp1, R5_ARG3, 1);
1502     __ b(l_2);
1503     __ bind(l_1);
1504     __ sthx(tmp2, R4_ARG2, tmp1);
1505     __ bind(l_2);
1506     __ addic_(tmp1, tmp1, -2);
1507     __ lhzx(tmp2, R3_ARG1, tmp1);
1508     __ bge(CCR0, l_1);
1509 
1510     __ li(R3_RET, 0); // return 0
1511     __ blr();
1512 
1513     return start;
1514   }
1515 
1516   // Generate core code for disjoint int copy (and oop copy on 32-bit).  If "aligned"
1517   // is true, the "from" and "to" addresses are assumed to be heapword aligned.
1518   //
1519   // Arguments:
1520   //      from:  R3_ARG1
1521   //      to:    R4_ARG2
1522   //      count: R5_ARG3 treated as signed
1523   //
1524   void generate_disjoint_int_copy_core(bool aligned) {
1525     Register tmp1 = R6_ARG4;
1526     Register tmp2 = R7_ARG5;
1527     Register tmp3 = R8_ARG6;
1528     Register tmp4 = R0;
1529 
1530     Label l_1, l_2, l_3, l_4, l_5, l_6;
1531     // for short arrays, just do single element copy
1532     __ li(tmp3, 0);
1533     __ cmpwi(CCR0, R5_ARG3, 5);
1534     __ ble(CCR0, l_2);
1535 
1536     if (!aligned) {
1537         // check if arrays have same alignment mod 8.
1538         __ xorr(tmp1, R3_ARG1, R4_ARG2);
1539         __ andi_(R0, tmp1, 7);
1540         // Not the same alignment, but ld and std just need to be 4 byte aligned.
1541         __ bne(CCR0, l_4); // to OR from is 8 byte aligned -> copy 2 at a time
1542 
1543         // copy 1 element to align to and from on an 8 byte boundary
1544         __ andi_(R0, R3_ARG1, 7);
1545         __ beq(CCR0, l_4);
1546 
1547         __ lwzx(tmp2, R3_ARG1, tmp3);
1548         __ addi(R5_ARG3, R5_ARG3, -1);
1549         __ stwx(tmp2, R4_ARG2, tmp3);
1550         { // FasterArrayCopy
1551           __ addi(R3_ARG1, R3_ARG1, 4);
1552           __ addi(R4_ARG2, R4_ARG2, 4);
1553         }
1554         __ bind(l_4);
1555       }
1556 
1557     { // FasterArrayCopy
1558       __ cmpwi(CCR0, R5_ARG3, 7);
1559       __ ble(CCR0, l_2); // copy 1 at a time if less than 8 elements remain
1560 
1561       __ srdi(tmp1, R5_ARG3, 3);
1562       __ andi_(R5_ARG3, R5_ARG3, 7);
1563       __ mtctr(tmp1);
1564 
1565       __ bind(l_6);
1566       // Use unrolled version for mass copying (copy 8 elements a time).
1567       // Load feeding store gets zero latency on power6, however not on power 5.
1568       // Therefore, the following sequence is made for the good of both.
1569       __ ld(tmp1, 0, R3_ARG1);
1570       __ ld(tmp2, 8, R3_ARG1);
1571       __ ld(tmp3, 16, R3_ARG1);
1572       __ ld(tmp4, 24, R3_ARG1);
1573       __ std(tmp1, 0, R4_ARG2);
1574       __ std(tmp2, 8, R4_ARG2);
1575       __ std(tmp3, 16, R4_ARG2);
1576       __ std(tmp4, 24, R4_ARG2);
1577       __ addi(R3_ARG1, R3_ARG1, 32);
1578       __ addi(R4_ARG2, R4_ARG2, 32);
1579       __ bdnz(l_6);
1580     }
1581 
1582     // copy 1 element at a time
1583     __ bind(l_2);
1584     __ cmpwi(CCR0, R5_ARG3, 0);
1585     __ beq(CCR0, l_1);
1586 
1587     { // FasterArrayCopy
1588       __ mtctr(R5_ARG3);
1589       __ addi(R3_ARG1, R3_ARG1, -4);
1590       __ addi(R4_ARG2, R4_ARG2, -4);
1591 
1592       __ bind(l_3);
1593       __ lwzu(tmp2, 4, R3_ARG1);
1594       __ stwu(tmp2, 4, R4_ARG2);
1595       __ bdnz(l_3);
1596     }
1597 
1598     __ bind(l_1);
1599     return;
1600   }
1601 
1602   // Generate stub for disjoint int copy.  If "aligned" is true, the
1603   // "from" and "to" addresses are assumed to be heapword aligned.
1604   //
1605   // Arguments for generated stub:
1606   //      from:  R3_ARG1
1607   //      to:    R4_ARG2
1608   //      count: R5_ARG3 treated as signed
1609   //
1610   address generate_disjoint_int_copy(bool aligned, const char * name) {
1611     StubCodeMark mark(this, "StubRoutines", name);
1612     address start = __ function_entry();
1613     generate_disjoint_int_copy_core(aligned);
1614     __ li(R3_RET, 0); // return 0
1615     __ blr();
1616     return start;
1617   }
1618 
1619   // Generate core code for conjoint int copy (and oop copy on
1620   // 32-bit).  If "aligned" is true, the "from" and "to" addresses
1621   // are assumed to be heapword aligned.
1622   //
1623   // Arguments:
1624   //      from:  R3_ARG1
1625   //      to:    R4_ARG2
1626   //      count: R5_ARG3 treated as signed
1627   //
1628   void generate_conjoint_int_copy_core(bool aligned) {
1629     // Do reverse copy.  We assume the case of actual overlap is rare enough
1630     // that we don't have to optimize it.
1631 
1632     Label l_1, l_2, l_3, l_4, l_5, l_6;
1633 
1634     Register tmp1 = R6_ARG4;
1635     Register tmp2 = R7_ARG5;
1636     Register tmp3 = R8_ARG6;
1637     Register tmp4 = R0;
1638 
1639     { // FasterArrayCopy
1640       __ cmpwi(CCR0, R5_ARG3, 0);
1641       __ beq(CCR0, l_6);
1642 
1643       __ sldi(R5_ARG3, R5_ARG3, 2);
1644       __ add(R3_ARG1, R3_ARG1, R5_ARG3);
1645       __ add(R4_ARG2, R4_ARG2, R5_ARG3);
1646       __ srdi(R5_ARG3, R5_ARG3, 2);
1647 
1648       __ cmpwi(CCR0, R5_ARG3, 7);
1649       __ ble(CCR0, l_5); // copy 1 at a time if less than 8 elements remain
1650 
1651       __ srdi(tmp1, R5_ARG3, 3);
1652       __ andi(R5_ARG3, R5_ARG3, 7);
1653       __ mtctr(tmp1);
1654 
1655       __ bind(l_4);
1656       // Use unrolled version for mass copying (copy 4 elements a time).
1657       // Load feeding store gets zero latency on Power6, however not on Power5.
1658       // Therefore, the following sequence is made for the good of both.
1659       __ addi(R3_ARG1, R3_ARG1, -32);
1660       __ addi(R4_ARG2, R4_ARG2, -32);
1661       __ ld(tmp4, 24, R3_ARG1);
1662       __ ld(tmp3, 16, R3_ARG1);
1663       __ ld(tmp2, 8, R3_ARG1);
1664       __ ld(tmp1, 0, R3_ARG1);
1665       __ std(tmp4, 24, R4_ARG2);
1666       __ std(tmp3, 16, R4_ARG2);
1667       __ std(tmp2, 8, R4_ARG2);
1668       __ std(tmp1, 0, R4_ARG2);
1669       __ bdnz(l_4);
1670 
1671       __ cmpwi(CCR0, R5_ARG3, 0);
1672       __ beq(CCR0, l_6);
1673 
1674       __ bind(l_5);
1675       __ mtctr(R5_ARG3);
1676       __ bind(l_3);
1677       __ lwz(R0, -4, R3_ARG1);
1678       __ stw(R0, -4, R4_ARG2);
1679       __ addi(R3_ARG1, R3_ARG1, -4);
1680       __ addi(R4_ARG2, R4_ARG2, -4);
1681       __ bdnz(l_3);
1682 
1683       __ bind(l_6);
1684     }
1685   }
1686 
1687   // Generate stub for conjoint int copy.  If "aligned" is true, the
1688   // "from" and "to" addresses are assumed to be heapword aligned.
1689   //
1690   // Arguments for generated stub:
1691   //      from:  R3_ARG1
1692   //      to:    R4_ARG2
1693   //      count: R5_ARG3 treated as signed
1694   //
1695   address generate_conjoint_int_copy(bool aligned, const char * name) {
1696     StubCodeMark mark(this, "StubRoutines", name);
1697     address start = __ function_entry();
1698 
1699     address nooverlap_target = aligned ?
1700       STUB_ENTRY(arrayof_jint_disjoint_arraycopy) :
1701       STUB_ENTRY(jint_disjoint_arraycopy);
1702 
1703     array_overlap_test(nooverlap_target, 2);
1704 
1705     generate_conjoint_int_copy_core(aligned);
1706 
1707     __ li(R3_RET, 0); // return 0
1708     __ blr();
1709 
1710     return start;
1711   }
1712 
1713   // Generate core code for disjoint long copy (and oop copy on
1714   // 64-bit).  If "aligned" is true, the "from" and "to" addresses
1715   // are assumed to be heapword aligned.
1716   //
1717   // Arguments:
1718   //      from:  R3_ARG1
1719   //      to:    R4_ARG2
1720   //      count: R5_ARG3 treated as signed
1721   //
1722   void generate_disjoint_long_copy_core(bool aligned) {
1723     Register tmp1 = R6_ARG4;
1724     Register tmp2 = R7_ARG5;
1725     Register tmp3 = R8_ARG6;
1726     Register tmp4 = R0;
1727 
1728     Label l_1, l_2, l_3, l_4;
1729 
1730     { // FasterArrayCopy
1731       __ cmpwi(CCR0, R5_ARG3, 3);
1732       __ ble(CCR0, l_3); // copy 1 at a time if less than 4 elements remain
1733 
1734       __ srdi(tmp1, R5_ARG3, 2);
1735       __ andi_(R5_ARG3, R5_ARG3, 3);
1736       __ mtctr(tmp1);
1737 
1738       __ bind(l_4);
1739       // Use unrolled version for mass copying (copy 4 elements a time).
1740       // Load feeding store gets zero latency on Power6, however not on Power5.
1741       // Therefore, the following sequence is made for the good of both.
1742       __ ld(tmp1, 0, R3_ARG1);
1743       __ ld(tmp2, 8, R3_ARG1);
1744       __ ld(tmp3, 16, R3_ARG1);
1745       __ ld(tmp4, 24, R3_ARG1);
1746       __ std(tmp1, 0, R4_ARG2);
1747       __ std(tmp2, 8, R4_ARG2);
1748       __ std(tmp3, 16, R4_ARG2);
1749       __ std(tmp4, 24, R4_ARG2);
1750       __ addi(R3_ARG1, R3_ARG1, 32);
1751       __ addi(R4_ARG2, R4_ARG2, 32);
1752       __ bdnz(l_4);
1753     }
1754 
1755     // copy 1 element at a time
1756     __ bind(l_3);
1757     __ cmpwi(CCR0, R5_ARG3, 0);
1758     __ beq(CCR0, l_1);
1759 
1760     { // FasterArrayCopy
1761       __ mtctr(R5_ARG3);
1762       __ addi(R3_ARG1, R3_ARG1, -8);
1763       __ addi(R4_ARG2, R4_ARG2, -8);
1764 
1765       __ bind(l_2);
1766       __ ldu(R0, 8, R3_ARG1);
1767       __ stdu(R0, 8, R4_ARG2);
1768       __ bdnz(l_2);
1769 
1770     }
1771     __ bind(l_1);
1772   }
1773 
1774   // Generate stub for disjoint long copy.  If "aligned" is true, the
1775   // "from" and "to" addresses are assumed to be heapword aligned.
1776   //
1777   // Arguments for generated stub:
1778   //      from:  R3_ARG1
1779   //      to:    R4_ARG2
1780   //      count: R5_ARG3 treated as signed
1781   //
1782   address generate_disjoint_long_copy(bool aligned, const char * name) {
1783     StubCodeMark mark(this, "StubRoutines", name);
1784     address start = __ function_entry();
1785     generate_disjoint_long_copy_core(aligned);
1786     __ li(R3_RET, 0); // return 0
1787     __ blr();
1788 
1789     return start;
1790   }
1791 
1792   // Generate core code for conjoint long copy (and oop copy on
1793   // 64-bit).  If "aligned" is true, the "from" and "to" addresses
1794   // are assumed to be heapword aligned.
1795   //
1796   // Arguments:
1797   //      from:  R3_ARG1
1798   //      to:    R4_ARG2
1799   //      count: R5_ARG3 treated as signed
1800   //
1801   void generate_conjoint_long_copy_core(bool aligned) {
1802     Register tmp1 = R6_ARG4;
1803     Register tmp2 = R7_ARG5;
1804     Register tmp3 = R8_ARG6;
1805     Register tmp4 = R0;
1806 
1807     Label l_1, l_2, l_3, l_4, l_5;
1808 
1809     __ cmpwi(CCR0, R5_ARG3, 0);
1810     __ beq(CCR0, l_1);
1811 
1812     { // FasterArrayCopy
1813       __ sldi(R5_ARG3, R5_ARG3, 3);
1814       __ add(R3_ARG1, R3_ARG1, R5_ARG3);
1815       __ add(R4_ARG2, R4_ARG2, R5_ARG3);
1816       __ srdi(R5_ARG3, R5_ARG3, 3);
1817 
1818       __ cmpwi(CCR0, R5_ARG3, 3);
1819       __ ble(CCR0, l_5); // copy 1 at a time if less than 4 elements remain
1820 
1821       __ srdi(tmp1, R5_ARG3, 2);
1822       __ andi(R5_ARG3, R5_ARG3, 3);
1823       __ mtctr(tmp1);
1824 
1825       __ bind(l_4);
1826       // Use unrolled version for mass copying (copy 4 elements a time).
1827       // Load feeding store gets zero latency on Power6, however not on Power5.
1828       // Therefore, the following sequence is made for the good of both.
1829       __ addi(R3_ARG1, R3_ARG1, -32);
1830       __ addi(R4_ARG2, R4_ARG2, -32);
1831       __ ld(tmp4, 24, R3_ARG1);
1832       __ ld(tmp3, 16, R3_ARG1);
1833       __ ld(tmp2, 8, R3_ARG1);
1834       __ ld(tmp1, 0, R3_ARG1);
1835       __ std(tmp4, 24, R4_ARG2);
1836       __ std(tmp3, 16, R4_ARG2);
1837       __ std(tmp2, 8, R4_ARG2);
1838       __ std(tmp1, 0, R4_ARG2);
1839       __ bdnz(l_4);
1840 
1841       __ cmpwi(CCR0, R5_ARG3, 0);
1842       __ beq(CCR0, l_1);
1843 
1844       __ bind(l_5);
1845       __ mtctr(R5_ARG3);
1846       __ bind(l_3);
1847       __ ld(R0, -8, R3_ARG1);
1848       __ std(R0, -8, R4_ARG2);
1849       __ addi(R3_ARG1, R3_ARG1, -8);
1850       __ addi(R4_ARG2, R4_ARG2, -8);
1851       __ bdnz(l_3);
1852 
1853     }
1854     __ bind(l_1);
1855   }
1856 
1857   // Generate stub for conjoint long copy.  If "aligned" is true, the
1858   // "from" and "to" addresses are assumed to be heapword aligned.
1859   //
1860   // Arguments for generated stub:
1861   //      from:  R3_ARG1
1862   //      to:    R4_ARG2
1863   //      count: R5_ARG3 treated as signed
1864   //
1865   address generate_conjoint_long_copy(bool aligned, const char * name) {
1866     StubCodeMark mark(this, "StubRoutines", name);
1867     address start = __ function_entry();
1868 
1869     address nooverlap_target = aligned ?
1870       STUB_ENTRY(arrayof_jlong_disjoint_arraycopy) :
1871       STUB_ENTRY(jlong_disjoint_arraycopy);
1872 
1873     array_overlap_test(nooverlap_target, 3);
1874     generate_conjoint_long_copy_core(aligned);
1875 
1876     __ li(R3_RET, 0); // return 0
1877     __ blr();
1878 
1879     return start;
1880   }
1881 
1882   // Generate stub for conjoint oop copy.  If "aligned" is true, the
1883   // "from" and "to" addresses are assumed to be heapword aligned.
1884   //
1885   // Arguments for generated stub:
1886   //      from:  R3_ARG1
1887   //      to:    R4_ARG2
1888   //      count: R5_ARG3 treated as signed
1889   //      dest_uninitialized: G1 support
1890   //
1891   address generate_conjoint_oop_copy(bool aligned, const char * name, bool dest_uninitialized) {
1892     StubCodeMark mark(this, "StubRoutines", name);
1893 
1894     address start = __ function_entry();
1895 
1896     address nooverlap_target = aligned ?
1897       STUB_ENTRY(arrayof_oop_disjoint_arraycopy) :
1898       STUB_ENTRY(oop_disjoint_arraycopy);
1899 
1900     gen_write_ref_array_pre_barrier(R3_ARG1, R4_ARG2, R5_ARG3, dest_uninitialized, R9_ARG7);
1901 
1902     // Save arguments.
1903     __ mr(R9_ARG7, R4_ARG2);
1904     __ mr(R10_ARG8, R5_ARG3);
1905 
1906     if (UseCompressedOops) {
1907       array_overlap_test(nooverlap_target, 2);
1908       generate_conjoint_int_copy_core(aligned);
1909     } else {
1910       array_overlap_test(nooverlap_target, 3);
1911       generate_conjoint_long_copy_core(aligned);
1912     }
1913 
1914     gen_write_ref_array_post_barrier(R9_ARG7, R10_ARG8, R11_scratch1);
1915     __ li(R3_RET, 0); // return 0
1916     __ blr();
1917     return start;
1918   }
1919 
1920   // Generate stub for disjoint oop copy.  If "aligned" is true, the
1921   // "from" and "to" addresses are assumed to be heapword aligned.
1922   //
1923   // Arguments for generated stub:
1924   //      from:  R3_ARG1
1925   //      to:    R4_ARG2
1926   //      count: R5_ARG3 treated as signed
1927   //      dest_uninitialized: G1 support
1928   //
1929   address generate_disjoint_oop_copy(bool aligned, const char * name, bool dest_uninitialized) {
1930     StubCodeMark mark(this, "StubRoutines", name);
1931     address start = __ function_entry();
1932 
1933     gen_write_ref_array_pre_barrier(R3_ARG1, R4_ARG2, R5_ARG3, dest_uninitialized, R9_ARG7);
1934 
1935     // save some arguments, disjoint_long_copy_core destroys them.
1936     // needed for post barrier
1937     __ mr(R9_ARG7, R4_ARG2);
1938     __ mr(R10_ARG8, R5_ARG3);
1939 
1940     if (UseCompressedOops) {
1941       generate_disjoint_int_copy_core(aligned);
1942     } else {
1943       generate_disjoint_long_copy_core(aligned);
1944     }
1945 
1946     gen_write_ref_array_post_barrier(R9_ARG7, R10_ARG8, R11_scratch1);
1947     __ li(R3_RET, 0); // return 0
1948     __ blr();
1949 
1950     return start;
1951   }
1952 
1953 
1954   // Helper for generating a dynamic type check.
1955   // Smashes only the given temp registers.
1956   void generate_type_check(Register sub_klass,
1957                            Register super_check_offset,
1958                            Register super_klass,
1959                            Register temp,
1960                            Label& L_success) {
1961     assert_different_registers(sub_klass, super_check_offset, super_klass);
1962 
1963     BLOCK_COMMENT("type_check:");
1964 
1965     Label L_miss;
1966 
1967     __ check_klass_subtype_fast_path(sub_klass, super_klass, temp, R0, &L_success, &L_miss, NULL,
1968                                      super_check_offset);
1969     __ check_klass_subtype_slow_path(sub_klass, super_klass, temp, R0, &L_success, NULL);
1970 
1971     // Fall through on failure!
1972     __ bind(L_miss);
1973   }
1974 
1975 
1976   //  Generate stub for checked oop copy.
1977   //
1978   // Arguments for generated stub:
1979   //      from:  R3
1980   //      to:    R4
1981   //      count: R5 treated as signed
1982   //      ckoff: R6 (super_check_offset)
1983   //      ckval: R7 (super_klass)
1984   //      ret:   R3 zero for success; (-1^K) where K is partial transfer count
1985   //
1986   address generate_checkcast_copy(const char *name, bool dest_uninitialized) {
1987 
1988     const Register R3_from   = R3_ARG1;      // source array address
1989     const Register R4_to     = R4_ARG2;      // destination array address
1990     const Register R5_count  = R5_ARG3;      // elements count
1991     const Register R6_ckoff  = R6_ARG4;      // super_check_offset
1992     const Register R7_ckval  = R7_ARG5;      // super_klass
1993 
1994     const Register R8_offset = R8_ARG6;      // loop var, with stride wordSize
1995     const Register R9_remain = R9_ARG7;      // loop var, with stride -1
1996     const Register R10_oop   = R10_ARG8;     // actual oop copied
1997     const Register R11_klass = R11_scratch1; // oop._klass
1998     const Register R12_tmp   = R12_scratch2;
1999 
2000     const Register R2_minus1 = R2;
2001 
2002     //__ align(CodeEntryAlignment);
2003     StubCodeMark mark(this, "StubRoutines", name);
2004     address start = __ function_entry();
2005 
2006     // TODO: Assert that int is 64 bit sign extended and arrays are not conjoint.
2007 
2008     gen_write_ref_array_pre_barrier(R3_from, R4_to, R5_count, dest_uninitialized, R12_tmp, /* preserve: */ R6_ckoff, R7_ckval);
2009 
2010     //inc_counter_np(SharedRuntime::_checkcast_array_copy_ctr, R12_tmp, R3_RET);
2011 
2012     Label load_element, store_element, store_null, success, do_card_marks;
2013     __ or_(R9_remain, R5_count, R5_count); // Initialize loop index, and test it.
2014     __ li(R8_offset, 0);                   // Offset from start of arrays.
2015     __ li(R2_minus1, -1);
2016     __ bne(CCR0, load_element);
2017 
2018     // Empty array: Nothing to do.
2019     __ li(R3_RET, 0);           // Return 0 on (trivial) success.
2020     __ blr();
2021 
2022     // ======== begin loop ========
2023     // (Entry is load_element.)
2024     __ align(OptoLoopAlignment);
2025     __ bind(store_element);
2026     if (UseCompressedOops) {
2027       __ encode_heap_oop_not_null(R10_oop);
2028       __ bind(store_null);
2029       __ stw(R10_oop, R8_offset, R4_to);
2030     } else {
2031       __ bind(store_null);
2032       __ std(R10_oop, R8_offset, R4_to);
2033     }
2034 
2035     __ addi(R8_offset, R8_offset, heapOopSize);   // Step to next offset.
2036     __ add_(R9_remain, R2_minus1, R9_remain);     // Decrement the count.
2037     __ beq(CCR0, success);
2038 
2039     // ======== loop entry is here ========
2040     __ bind(load_element);
2041     __ load_heap_oop(R10_oop, R8_offset, R3_from, &store_null);  // Load the oop.
2042 
2043     __ load_klass(R11_klass, R10_oop); // Query the object klass.
2044 
2045     generate_type_check(R11_klass, R6_ckoff, R7_ckval, R12_tmp,
2046                         // Branch to this on success:
2047                         store_element);
2048     // ======== end loop ========
2049 
2050     // It was a real error; we must depend on the caller to finish the job.
2051     // Register R9_remain has number of *remaining* oops, R5_count number of *total* oops.
2052     // Emit GC store barriers for the oops we have copied (R5_count minus R9_remain),
2053     // and report their number to the caller.
2054     __ subf_(R5_count, R9_remain, R5_count);
2055     __ nand(R3_RET, R5_count, R5_count);   // report (-1^K) to caller
2056     __ bne(CCR0, do_card_marks);
2057     __ blr();
2058 
2059     __ bind(success);
2060     __ li(R3_RET, 0);
2061 
2062     __ bind(do_card_marks);
2063     // Store check on R4_to[0..R5_count-1].
2064     gen_write_ref_array_post_barrier(R4_to, R5_count, R12_tmp, /* preserve: */ R3_RET);
2065     __ blr();
2066     return start;
2067   }
2068 
2069 
2070   //  Generate 'unsafe' array copy stub.
2071   //  Though just as safe as the other stubs, it takes an unscaled
2072   //  size_t argument instead of an element count.
2073   //
2074   // Arguments for generated stub:
2075   //      from:  R3
2076   //      to:    R4
2077   //      count: R5 byte count, treated as ssize_t, can be zero
2078   //
2079   // Examines the alignment of the operands and dispatches
2080   // to a long, int, short, or byte copy loop.
2081   //
2082   address generate_unsafe_copy(const char* name,
2083                                address byte_copy_entry,
2084                                address short_copy_entry,
2085                                address int_copy_entry,
2086                                address long_copy_entry) {
2087 
2088     const Register R3_from   = R3_ARG1;      // source array address
2089     const Register R4_to     = R4_ARG2;      // destination array address
2090     const Register R5_count  = R5_ARG3;      // elements count (as long on PPC64)
2091 
2092     const Register R6_bits   = R6_ARG4;      // test copy of low bits
2093     const Register R7_tmp    = R7_ARG5;
2094 
2095     //__ align(CodeEntryAlignment);
2096     StubCodeMark mark(this, "StubRoutines", name);
2097     address start = __ function_entry();
2098 
2099     // Bump this on entry, not on exit:
2100     //inc_counter_np(SharedRuntime::_unsafe_array_copy_ctr, R6_bits, R7_tmp);
2101 
2102     Label short_copy, int_copy, long_copy;
2103 
2104     __ orr(R6_bits, R3_from, R4_to);
2105     __ orr(R6_bits, R6_bits, R5_count);
2106     __ andi_(R0, R6_bits, (BytesPerLong-1));
2107     __ beq(CCR0, long_copy);
2108 
2109     __ andi_(R0, R6_bits, (BytesPerInt-1));
2110     __ beq(CCR0, int_copy);
2111 
2112     __ andi_(R0, R6_bits, (BytesPerShort-1));
2113     __ beq(CCR0, short_copy);
2114 
2115     // byte_copy:
2116     __ b(byte_copy_entry);
2117 
2118     __ bind(short_copy);
2119     __ srwi(R5_count, R5_count, LogBytesPerShort);
2120     __ b(short_copy_entry);
2121 
2122     __ bind(int_copy);
2123     __ srwi(R5_count, R5_count, LogBytesPerInt);
2124     __ b(int_copy_entry);
2125 
2126     __ bind(long_copy);
2127     __ srwi(R5_count, R5_count, LogBytesPerLong);
2128     __ b(long_copy_entry);
2129 
2130     return start;
2131   }
2132 
2133 
2134   // Perform range checks on the proposed arraycopy.
2135   // Kills the two temps, but nothing else.
2136   // Also, clean the sign bits of src_pos and dst_pos.
2137   void arraycopy_range_checks(Register src,     // source array oop
2138                               Register src_pos, // source position
2139                               Register dst,     // destination array oop
2140                               Register dst_pos, // destination position
2141                               Register length,  // length of copy
2142                               Register temp1, Register temp2,
2143                               Label& L_failed) {
2144     BLOCK_COMMENT("arraycopy_range_checks:");
2145 
2146     const Register array_length = temp1;  // scratch
2147     const Register end_pos      = temp2;  // scratch
2148 
2149     //  if (src_pos + length > arrayOop(src)->length() ) FAIL;
2150     __ lwa(array_length, arrayOopDesc::length_offset_in_bytes(), src);
2151     __ add(end_pos, src_pos, length);  // src_pos + length
2152     __ cmpd(CCR0, end_pos, array_length);
2153     __ bgt(CCR0, L_failed);
2154 
2155     //  if (dst_pos + length > arrayOop(dst)->length() ) FAIL;
2156     __ lwa(array_length, arrayOopDesc::length_offset_in_bytes(), dst);
2157     __ add(end_pos, dst_pos, length);  // src_pos + length
2158     __ cmpd(CCR0, end_pos, array_length);
2159     __ bgt(CCR0, L_failed);
2160 
2161     BLOCK_COMMENT("arraycopy_range_checks done");
2162   }
2163 
2164 
2165   //
2166   //  Generate generic array copy stubs
2167   //
2168   //  Input:
2169   //    R3    -  src oop
2170   //    R4    -  src_pos
2171   //    R5    -  dst oop
2172   //    R6    -  dst_pos
2173   //    R7    -  element count
2174   //
2175   //  Output:
2176   //    R3 ==  0  -  success
2177   //    R3 == -1  -  need to call System.arraycopy
2178   //
2179   address generate_generic_copy(const char *name,
2180                                 address entry_jbyte_arraycopy,
2181                                 address entry_jshort_arraycopy,
2182                                 address entry_jint_arraycopy,
2183                                 address entry_oop_arraycopy,
2184                                 address entry_disjoint_oop_arraycopy,
2185                                 address entry_jlong_arraycopy,
2186                                 address entry_checkcast_arraycopy) {
2187     Label L_failed, L_objArray;
2188 
2189     // Input registers
2190     const Register src       = R3_ARG1;  // source array oop
2191     const Register src_pos   = R4_ARG2;  // source position
2192     const Register dst       = R5_ARG3;  // destination array oop
2193     const Register dst_pos   = R6_ARG4;  // destination position
2194     const Register length    = R7_ARG5;  // elements count
2195 
2196     // registers used as temp
2197     const Register src_klass = R8_ARG6;  // source array klass
2198     const Register dst_klass = R9_ARG7;  // destination array klass
2199     const Register lh        = R10_ARG8; // layout handler
2200     const Register temp      = R2;
2201 
2202     //__ align(CodeEntryAlignment);
2203     StubCodeMark mark(this, "StubRoutines", name);
2204     address start = __ function_entry();
2205 
2206     // Bump this on entry, not on exit:
2207     //inc_counter_np(SharedRuntime::_generic_array_copy_ctr, lh, temp);
2208 
2209     // In principle, the int arguments could be dirty.
2210 
2211     //-----------------------------------------------------------------------
2212     // Assembler stubs will be used for this call to arraycopy
2213     // if the following conditions are met:
2214     //
2215     // (1) src and dst must not be null.
2216     // (2) src_pos must not be negative.
2217     // (3) dst_pos must not be negative.
2218     // (4) length  must not be negative.
2219     // (5) src klass and dst klass should be the same and not NULL.
2220     // (6) src and dst should be arrays.
2221     // (7) src_pos + length must not exceed length of src.
2222     // (8) dst_pos + length must not exceed length of dst.
2223     BLOCK_COMMENT("arraycopy initial argument checks");
2224 
2225     __ cmpdi(CCR1, src, 0);      // if (src == NULL) return -1;
2226     __ extsw_(src_pos, src_pos); // if (src_pos < 0) return -1;
2227     __ cmpdi(CCR5, dst, 0);      // if (dst == NULL) return -1;
2228     __ cror(CCR1, Assembler::equal, CCR0, Assembler::less);
2229     __ extsw_(dst_pos, dst_pos); // if (src_pos < 0) return -1;
2230     __ cror(CCR5, Assembler::equal, CCR0, Assembler::less);
2231     __ extsw_(length, length);   // if (length < 0) return -1;
2232     __ cror(CCR1, Assembler::equal, CCR5, Assembler::equal);
2233     __ cror(CCR1, Assembler::equal, CCR0, Assembler::less);
2234     __ beq(CCR1, L_failed);
2235 
2236     BLOCK_COMMENT("arraycopy argument klass checks");
2237     __ load_klass(src_klass, src);
2238     __ load_klass(dst_klass, dst);
2239 
2240     // Load layout helper
2241     //
2242     //  |array_tag|     | header_size | element_type |     |log2_element_size|
2243     // 32        30    24            16              8     2                 0
2244     //
2245     //   array_tag: typeArray = 0x3, objArray = 0x2, non-array = 0x0
2246     //
2247 
2248     int lh_offset = in_bytes(Klass::layout_helper_offset());
2249 
2250     // Load 32-bits signed value. Use br() instruction with it to check icc.
2251     __ lwz(lh, lh_offset, src_klass);
2252 
2253     // Handle objArrays completely differently...
2254     jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
2255     __ load_const_optimized(temp, objArray_lh, R0);
2256     __ cmpw(CCR0, lh, temp);
2257     __ beq(CCR0, L_objArray);
2258 
2259     __ cmpd(CCR5, src_klass, dst_klass);          // if (src->klass() != dst->klass()) return -1;
2260     __ cmpwi(CCR6, lh, Klass::_lh_neutral_value); // if (!src->is_Array()) return -1;
2261 
2262     __ crnand(CCR5, Assembler::equal, CCR6, Assembler::less);
2263     __ beq(CCR5, L_failed);
2264 
2265     // At this point, it is known to be a typeArray (array_tag 0x3).
2266 #ifdef ASSERT
2267     { Label L;
2268       jint lh_prim_tag_in_place = (Klass::_lh_array_tag_type_value << Klass::_lh_array_tag_shift);
2269       __ load_const_optimized(temp, lh_prim_tag_in_place, R0);
2270       __ cmpw(CCR0, lh, temp);
2271       __ bge(CCR0, L);
2272       __ stop("must be a primitive array");
2273       __ bind(L);
2274     }
2275 #endif
2276 
2277     arraycopy_range_checks(src, src_pos, dst, dst_pos, length,
2278                            temp, dst_klass, L_failed);
2279 
2280     // TypeArrayKlass
2281     //
2282     // src_addr = (src + array_header_in_bytes()) + (src_pos << log2elemsize);
2283     // dst_addr = (dst + array_header_in_bytes()) + (dst_pos << log2elemsize);
2284     //
2285 
2286     const Register offset = dst_klass;    // array offset
2287     const Register elsize = src_klass;    // log2 element size
2288 
2289     __ rldicl(offset, lh, 64 - Klass::_lh_header_size_shift, 64 - exact_log2(Klass::_lh_header_size_mask + 1));
2290     __ andi(elsize, lh, Klass::_lh_log2_element_size_mask);
2291     __ add(src, offset, src);       // src array offset
2292     __ add(dst, offset, dst);       // dst array offset
2293 
2294     // Next registers should be set before the jump to corresponding stub.
2295     const Register from     = R3_ARG1;  // source array address
2296     const Register to       = R4_ARG2;  // destination array address
2297     const Register count    = R5_ARG3;  // elements count
2298 
2299     // 'from', 'to', 'count' registers should be set in this order
2300     // since they are the same as 'src', 'src_pos', 'dst'.
2301 
2302     BLOCK_COMMENT("scale indexes to element size");
2303     __ sld(src_pos, src_pos, elsize);
2304     __ sld(dst_pos, dst_pos, elsize);
2305     __ add(from, src_pos, src);  // src_addr
2306     __ add(to, dst_pos, dst);    // dst_addr
2307     __ mr(count, length);        // length
2308 
2309     BLOCK_COMMENT("choose copy loop based on element size");
2310     // Using conditional branches with range 32kB.
2311     const int bo = Assembler::bcondCRbiIs1, bi = Assembler::bi0(CCR0, Assembler::equal);
2312     __ cmpwi(CCR0, elsize, 0);
2313     __ bc(bo, bi, entry_jbyte_arraycopy);
2314     __ cmpwi(CCR0, elsize, LogBytesPerShort);
2315     __ bc(bo, bi, entry_jshort_arraycopy);
2316     __ cmpwi(CCR0, elsize, LogBytesPerInt);
2317     __ bc(bo, bi, entry_jint_arraycopy);
2318 #ifdef ASSERT
2319     { Label L;
2320       __ cmpwi(CCR0, elsize, LogBytesPerLong);
2321       __ beq(CCR0, L);
2322       __ stop("must be long copy, but elsize is wrong");
2323       __ bind(L);
2324     }
2325 #endif
2326     __ b(entry_jlong_arraycopy);
2327 
2328     // ObjArrayKlass
2329   __ bind(L_objArray);
2330     // live at this point:  src_klass, dst_klass, src[_pos], dst[_pos], length
2331 
2332     Label L_disjoint_plain_copy, L_checkcast_copy;
2333     //  test array classes for subtyping
2334     __ cmpd(CCR0, src_klass, dst_klass);         // usual case is exact equality
2335     __ bne(CCR0, L_checkcast_copy);
2336 
2337     // Identically typed arrays can be copied without element-wise checks.
2338     arraycopy_range_checks(src, src_pos, dst, dst_pos, length,
2339                            temp, lh, L_failed);
2340 
2341     __ addi(src, src, arrayOopDesc::base_offset_in_bytes(T_OBJECT)); //src offset
2342     __ addi(dst, dst, arrayOopDesc::base_offset_in_bytes(T_OBJECT)); //dst offset
2343     __ sldi(src_pos, src_pos, LogBytesPerHeapOop);
2344     __ sldi(dst_pos, dst_pos, LogBytesPerHeapOop);
2345     __ add(from, src_pos, src);  // src_addr
2346     __ add(to, dst_pos, dst);    // dst_addr
2347     __ mr(count, length);        // length
2348     __ b(entry_oop_arraycopy);
2349 
2350   __ bind(L_checkcast_copy);
2351     // live at this point:  src_klass, dst_klass
2352     {
2353       // Before looking at dst.length, make sure dst is also an objArray.
2354       __ lwz(temp, lh_offset, dst_klass);
2355       __ cmpw(CCR0, lh, temp);
2356       __ bne(CCR0, L_failed);
2357 
2358       // It is safe to examine both src.length and dst.length.
2359       arraycopy_range_checks(src, src_pos, dst, dst_pos, length,
2360                              temp, lh, L_failed);
2361 
2362       // Marshal the base address arguments now, freeing registers.
2363       __ addi(src, src, arrayOopDesc::base_offset_in_bytes(T_OBJECT)); //src offset
2364       __ addi(dst, dst, arrayOopDesc::base_offset_in_bytes(T_OBJECT)); //dst offset
2365       __ sldi(src_pos, src_pos, LogBytesPerHeapOop);
2366       __ sldi(dst_pos, dst_pos, LogBytesPerHeapOop);
2367       __ add(from, src_pos, src);  // src_addr
2368       __ add(to, dst_pos, dst);    // dst_addr
2369       __ mr(count, length);        // length
2370 
2371       Register sco_temp = R6_ARG4;             // This register is free now.
2372       assert_different_registers(from, to, count, sco_temp,
2373                                  dst_klass, src_klass);
2374 
2375       // Generate the type check.
2376       int sco_offset = in_bytes(Klass::super_check_offset_offset());
2377       __ lwz(sco_temp, sco_offset, dst_klass);
2378       generate_type_check(src_klass, sco_temp, dst_klass,
2379                           temp, L_disjoint_plain_copy);
2380 
2381       // Fetch destination element klass from the ObjArrayKlass header.
2382       int ek_offset = in_bytes(ObjArrayKlass::element_klass_offset());
2383 
2384       // The checkcast_copy loop needs two extra arguments:
2385       __ ld(R7_ARG5, ek_offset, dst_klass);   // dest elem klass
2386       __ lwz(R6_ARG4, sco_offset, R7_ARG5);   // sco of elem klass
2387       __ b(entry_checkcast_arraycopy);
2388     }
2389 
2390     __ bind(L_disjoint_plain_copy);
2391     __ b(entry_disjoint_oop_arraycopy);
2392 
2393   __ bind(L_failed);
2394     __ li(R3_RET, -1); // return -1
2395     __ blr();
2396     return start;
2397   }
2398 
2399 
2400   void generate_arraycopy_stubs() {
2401     // Note: the disjoint stubs must be generated first, some of
2402     // the conjoint stubs use them.
2403 
2404     // non-aligned disjoint versions
2405     StubRoutines::_jbyte_disjoint_arraycopy       = generate_disjoint_byte_copy(false, "jbyte_disjoint_arraycopy");
2406     StubRoutines::_jshort_disjoint_arraycopy      = generate_disjoint_short_copy(false, "jshort_disjoint_arraycopy");
2407     StubRoutines::_jint_disjoint_arraycopy        = generate_disjoint_int_copy(false, "jint_disjoint_arraycopy");
2408     StubRoutines::_jlong_disjoint_arraycopy       = generate_disjoint_long_copy(false, "jlong_disjoint_arraycopy");
2409     StubRoutines::_oop_disjoint_arraycopy         = generate_disjoint_oop_copy(false, "oop_disjoint_arraycopy", false);
2410     StubRoutines::_oop_disjoint_arraycopy_uninit  = generate_disjoint_oop_copy(false, "oop_disjoint_arraycopy_uninit", true);
2411 
2412     // aligned disjoint versions
2413     StubRoutines::_arrayof_jbyte_disjoint_arraycopy      = generate_disjoint_byte_copy(true, "arrayof_jbyte_disjoint_arraycopy");
2414     StubRoutines::_arrayof_jshort_disjoint_arraycopy     = generate_disjoint_short_copy(true, "arrayof_jshort_disjoint_arraycopy");
2415     StubRoutines::_arrayof_jint_disjoint_arraycopy       = generate_disjoint_int_copy(true, "arrayof_jint_disjoint_arraycopy");
2416     StubRoutines::_arrayof_jlong_disjoint_arraycopy      = generate_disjoint_long_copy(true, "arrayof_jlong_disjoint_arraycopy");
2417     StubRoutines::_arrayof_oop_disjoint_arraycopy        = generate_disjoint_oop_copy(true, "arrayof_oop_disjoint_arraycopy", false);
2418     StubRoutines::_arrayof_oop_disjoint_arraycopy_uninit = generate_disjoint_oop_copy(true, "oop_disjoint_arraycopy_uninit", true);
2419 
2420     // non-aligned conjoint versions
2421     StubRoutines::_jbyte_arraycopy      = generate_conjoint_byte_copy(false, "jbyte_arraycopy");
2422     StubRoutines::_jshort_arraycopy     = generate_conjoint_short_copy(false, "jshort_arraycopy");
2423     StubRoutines::_jint_arraycopy       = generate_conjoint_int_copy(false, "jint_arraycopy");
2424     StubRoutines::_jlong_arraycopy      = generate_conjoint_long_copy(false, "jlong_arraycopy");
2425     StubRoutines::_oop_arraycopy        = generate_conjoint_oop_copy(false, "oop_arraycopy", false);
2426     StubRoutines::_oop_arraycopy_uninit = generate_conjoint_oop_copy(false, "oop_arraycopy_uninit", true);
2427 
2428     // aligned conjoint versions
2429     StubRoutines::_arrayof_jbyte_arraycopy      = generate_conjoint_byte_copy(true, "arrayof_jbyte_arraycopy");
2430     StubRoutines::_arrayof_jshort_arraycopy     = generate_conjoint_short_copy(true, "arrayof_jshort_arraycopy");
2431     StubRoutines::_arrayof_jint_arraycopy       = generate_conjoint_int_copy(true, "arrayof_jint_arraycopy");
2432     StubRoutines::_arrayof_jlong_arraycopy      = generate_conjoint_long_copy(true, "arrayof_jlong_arraycopy");
2433     StubRoutines::_arrayof_oop_arraycopy        = generate_conjoint_oop_copy(true, "arrayof_oop_arraycopy", false);
2434     StubRoutines::_arrayof_oop_arraycopy_uninit = generate_conjoint_oop_copy(true, "arrayof_oop_arraycopy", true);
2435 
2436     // special/generic versions
2437     StubRoutines::_checkcast_arraycopy        = generate_checkcast_copy("checkcast_arraycopy", false);
2438     StubRoutines::_checkcast_arraycopy_uninit = generate_checkcast_copy("checkcast_arraycopy_uninit", true);
2439 
2440     StubRoutines::_unsafe_arraycopy  = generate_unsafe_copy("unsafe_arraycopy",
2441                                                             STUB_ENTRY(jbyte_arraycopy),
2442                                                             STUB_ENTRY(jshort_arraycopy),
2443                                                             STUB_ENTRY(jint_arraycopy),
2444                                                             STUB_ENTRY(jlong_arraycopy));
2445     StubRoutines::_generic_arraycopy = generate_generic_copy("generic_arraycopy",
2446                                                              STUB_ENTRY(jbyte_arraycopy),
2447                                                              STUB_ENTRY(jshort_arraycopy),
2448                                                              STUB_ENTRY(jint_arraycopy),
2449                                                              STUB_ENTRY(oop_arraycopy),
2450                                                              STUB_ENTRY(oop_disjoint_arraycopy),
2451                                                              STUB_ENTRY(jlong_arraycopy),
2452                                                              STUB_ENTRY(checkcast_arraycopy));
2453 
2454     // fill routines
2455     StubRoutines::_jbyte_fill          = generate_fill(T_BYTE,  false, "jbyte_fill");
2456     StubRoutines::_jshort_fill         = generate_fill(T_SHORT, false, "jshort_fill");
2457     StubRoutines::_jint_fill           = generate_fill(T_INT,   false, "jint_fill");
2458     StubRoutines::_arrayof_jbyte_fill  = generate_fill(T_BYTE,  true, "arrayof_jbyte_fill");
2459     StubRoutines::_arrayof_jshort_fill = generate_fill(T_SHORT, true, "arrayof_jshort_fill");
2460     StubRoutines::_arrayof_jint_fill   = generate_fill(T_INT,   true, "arrayof_jint_fill");
2461   }
2462 
2463   // Safefetch stubs.
2464   void generate_safefetch(const char* name, int size, address* entry, address* fault_pc, address* continuation_pc) {
2465     // safefetch signatures:
2466     //   int      SafeFetch32(int*      adr, int      errValue);
2467     //   intptr_t SafeFetchN (intptr_t* adr, intptr_t errValue);
2468     //
2469     // arguments:
2470     //   R3_ARG1 = adr
2471     //   R4_ARG2 = errValue
2472     //
2473     // result:
2474     //   R3_RET  = *adr or errValue
2475 
2476     StubCodeMark mark(this, "StubRoutines", name);
2477 
2478     // Entry point, pc or function descriptor.
2479     *entry = __ function_entry();
2480 
2481     // Load *adr into R4_ARG2, may fault.
2482     *fault_pc = __ pc();
2483     switch (size) {
2484       case 4:
2485         // int32_t, signed extended
2486         __ lwa(R4_ARG2, 0, R3_ARG1);
2487         break;
2488       case 8:
2489         // int64_t
2490         __ ld(R4_ARG2, 0, R3_ARG1);
2491         break;
2492       default:
2493         ShouldNotReachHere();
2494     }
2495 
2496     // return errValue or *adr
2497     *continuation_pc = __ pc();
2498     __ mr(R3_RET, R4_ARG2);
2499     __ blr();
2500   }
2501 
2502   // Stub for BigInteger::multiplyToLen()
2503   //
2504   //  Arguments:
2505   //
2506   //  Input:
2507   //    R3 - x address
2508   //    R4 - x length
2509   //    R5 - y address
2510   //    R6 - y length
2511   //    R7 - z address
2512   //    R8 - z length
2513   //
2514   address generate_multiplyToLen() {
2515 
2516     StubCodeMark mark(this, "StubRoutines", "multiplyToLen");
2517 
2518     address start = __ function_entry();
2519 
2520     const Register x     = R3;
2521     const Register xlen  = R4;
2522     const Register y     = R5;
2523     const Register ylen  = R6;
2524     const Register z     = R7;
2525     const Register zlen  = R8;
2526 
2527     const Register tmp1  = R2; // TOC not used.
2528     const Register tmp2  = R9;
2529     const Register tmp3  = R10;
2530     const Register tmp4  = R11;
2531     const Register tmp5  = R12;
2532 
2533     // non-volatile regs
2534     const Register tmp6  = R31;
2535     const Register tmp7  = R30;
2536     const Register tmp8  = R29;
2537     const Register tmp9  = R28;
2538     const Register tmp10 = R27;
2539     const Register tmp11 = R26;
2540     const Register tmp12 = R25;
2541     const Register tmp13 = R24;
2542 
2543     BLOCK_COMMENT("Entry:");
2544 
2545     // Save non-volatile regs (frameless).
2546     int current_offs = 8;
2547     __ std(R24, -current_offs, R1_SP); current_offs += 8;
2548     __ std(R25, -current_offs, R1_SP); current_offs += 8;
2549     __ std(R26, -current_offs, R1_SP); current_offs += 8;
2550     __ std(R27, -current_offs, R1_SP); current_offs += 8;
2551     __ std(R28, -current_offs, R1_SP); current_offs += 8;
2552     __ std(R29, -current_offs, R1_SP); current_offs += 8;
2553     __ std(R30, -current_offs, R1_SP); current_offs += 8;
2554     __ std(R31, -current_offs, R1_SP);
2555 
2556     __ multiply_to_len(x, xlen, y, ylen, z, zlen, tmp1, tmp2, tmp3, tmp4, tmp5,
2557                        tmp6, tmp7, tmp8, tmp9, tmp10, tmp11, tmp12, tmp13);
2558 
2559     // Restore non-volatile regs.
2560     current_offs = 8;
2561     __ ld(R24, -current_offs, R1_SP); current_offs += 8;
2562     __ ld(R25, -current_offs, R1_SP); current_offs += 8;
2563     __ ld(R26, -current_offs, R1_SP); current_offs += 8;
2564     __ ld(R27, -current_offs, R1_SP); current_offs += 8;
2565     __ ld(R28, -current_offs, R1_SP); current_offs += 8;
2566     __ ld(R29, -current_offs, R1_SP); current_offs += 8;
2567     __ ld(R30, -current_offs, R1_SP); current_offs += 8;
2568     __ ld(R31, -current_offs, R1_SP);
2569 
2570     __ blr();  // Return to caller.
2571 
2572     return start;
2573   }
2574 
2575   /**
2576    * Arguments:
2577    *
2578    * Inputs:
2579    *   R3_ARG1    - int   crc
2580    *   R4_ARG2    - byte* buf
2581    *   R5_ARG3    - int   length (of buffer)
2582    *
2583    * scratch:
2584    *   R6_ARG4    - crc table address
2585    *   R7_ARG5    - tmp1
2586    *   R8_ARG6    - tmp2
2587    *
2588    * Ouput:
2589    *   R3_RET     - int   crc result
2590    */
2591   // Compute CRC32 function.
2592   address generate_CRC32_updateBytes(const char* name) {
2593     __ align(CodeEntryAlignment);
2594     StubCodeMark mark(this, "StubRoutines", name);
2595     address start = __ function_entry();  // Remember stub start address (is rtn value).
2596 
2597     // arguments to kernel_crc32:
2598     Register       crc     = R3_ARG1;  // Current checksum, preset by caller or result from previous call.
2599     Register       data    = R4_ARG2;  // source byte array
2600     Register       dataLen = R5_ARG3;  // #bytes to process
2601     Register       table   = R6_ARG4;  // crc table address
2602 
2603     Register       t0      = R9;       // work reg for kernel* emitters
2604     Register       t1      = R10;      // work reg for kernel* emitters
2605     Register       t2      = R11;      // work reg for kernel* emitters
2606     Register       t3      = R12;      // work reg for kernel* emitters
2607 
2608     BLOCK_COMMENT("Stub body {");
2609     assert_different_registers(crc, data, dataLen, table);
2610 
2611     StubRoutines::ppc64::generate_load_crc_table_addr(_masm, table);
2612 
2613     __ kernel_crc32_1byte(crc, data, dataLen, table, t0, t1, t2, t3);
2614 
2615     BLOCK_COMMENT("return");
2616     __ mr_if_needed(R3_RET, crc);      // Updated crc is function result. No copying required (R3_ARG1 == R3_RET).
2617     __ blr();
2618 
2619     BLOCK_COMMENT("} Stub body");
2620     return start;
2621   }
2622 
2623   // Initialization
2624   void generate_initial() {
2625     // Generates all stubs and initializes the entry points
2626 
2627     // Entry points that exist in all platforms.
2628     // Note: This is code that could be shared among different platforms - however the
2629     // benefit seems to be smaller than the disadvantage of having a
2630     // much more complicated generator structure. See also comment in
2631     // stubRoutines.hpp.
2632 
2633     StubRoutines::_forward_exception_entry          = generate_forward_exception();
2634     StubRoutines::_call_stub_entry                  = generate_call_stub(StubRoutines::_call_stub_return_address);
2635     StubRoutines::_catch_exception_entry            = generate_catch_exception();
2636 
2637     // Build this early so it's available for the interpreter.
2638     StubRoutines::_throw_StackOverflowError_entry   =
2639       generate_throw_exception("StackOverflowError throw_exception",
2640                                CAST_FROM_FN_PTR(address, SharedRuntime::throw_StackOverflowError), false);
2641 
2642     // CRC32 Intrinsics.
2643     if (UseCRC32Intrinsics) {
2644       StubRoutines::_crc_table_adr    = (address)StubRoutines::ppc64::_crc_table;
2645       StubRoutines::_updateBytesCRC32 = generate_CRC32_updateBytes("CRC32_updateBytes");
2646     }
2647   }
2648 
2649   void generate_all() {
2650     // Generates all stubs and initializes the entry points
2651 
2652     // These entry points require SharedInfo::stack0 to be set up in
2653     // non-core builds
2654     StubRoutines::_throw_AbstractMethodError_entry         = generate_throw_exception("AbstractMethodError throw_exception",          CAST_FROM_FN_PTR(address, SharedRuntime::throw_AbstractMethodError),  false);
2655     // Handle IncompatibleClassChangeError in itable stubs.
2656     StubRoutines::_throw_IncompatibleClassChangeError_entry= generate_throw_exception("IncompatibleClassChangeError throw_exception", CAST_FROM_FN_PTR(address, SharedRuntime::throw_IncompatibleClassChangeError),  false);
2657     StubRoutines::_throw_NullPointerException_at_call_entry= generate_throw_exception("NullPointerException at call throw_exception", CAST_FROM_FN_PTR(address, SharedRuntime::throw_NullPointerException_at_call), false);
2658 
2659     StubRoutines::_handler_for_unsafe_access_entry         = generate_handler_for_unsafe_access();
2660 
2661     // support for verify_oop (must happen after universe_init)
2662     StubRoutines::_verify_oop_subroutine_entry             = generate_verify_oop();
2663 
2664     // arraycopy stubs used by compilers
2665     generate_arraycopy_stubs();
2666 
2667     if (UseAESIntrinsics) {
2668       guarantee(!UseAESIntrinsics, "not yet implemented.");
2669     }
2670 
2671     // Safefetch stubs.
2672     generate_safefetch("SafeFetch32", sizeof(int),     &StubRoutines::_safefetch32_entry,
2673                                                        &StubRoutines::_safefetch32_fault_pc,
2674                                                        &StubRoutines::_safefetch32_continuation_pc);
2675     generate_safefetch("SafeFetchN", sizeof(intptr_t), &StubRoutines::_safefetchN_entry,
2676                                                        &StubRoutines::_safefetchN_fault_pc,
2677                                                        &StubRoutines::_safefetchN_continuation_pc);
2678 
2679 #ifdef COMPILER2
2680     if (UseMultiplyToLenIntrinsic) {
2681       StubRoutines::_multiplyToLen = generate_multiplyToLen();
2682     }
2683 #endif
2684 
2685     if (UseMontgomeryMultiplyIntrinsic) {
2686       StubRoutines::_montgomeryMultiply
2687         = CAST_FROM_FN_PTR(address, SharedRuntime::montgomery_multiply);
2688     }
2689     if (UseMontgomerySquareIntrinsic) {
2690       StubRoutines::_montgomerySquare
2691         = CAST_FROM_FN_PTR(address, SharedRuntime::montgomery_square);
2692     }
2693   }
2694 
2695  public:
2696   StubGenerator(CodeBuffer* code, bool all) : StubCodeGenerator(code) {
2697     // replace the standard masm with a special one:
2698     _masm = new MacroAssembler(code);
2699     if (all) {
2700       generate_all();
2701     } else {
2702       generate_initial();
2703     }
2704   }
2705 };
2706 
2707 void StubGenerator_generate(CodeBuffer* code, bool all) {
2708   StubGenerator g(code, all);
2709 }