Fri, 15 Jan 2010 11:53:33 -0800
6849984: Value methods for platform dependent math functions constant fold incorrectly
Reviewed-by: kvn, twisti
1 /*
2 * Copyright 1999-2010 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 #include "incls/_precompiled.incl"
26 #include "incls/_stubGenerator_x86_32.cpp.incl"
28 // Declaration and definition of StubGenerator (no .hpp file).
29 // For a more detailed description of the stub routine structure
30 // see the comment in stubRoutines.hpp
32 #define __ _masm->
33 #define a__ ((Assembler*)_masm)->
35 #ifdef PRODUCT
36 #define BLOCK_COMMENT(str) /* nothing */
37 #else
38 #define BLOCK_COMMENT(str) __ block_comment(str)
39 #endif
41 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
43 const int MXCSR_MASK = 0xFFC0; // Mask out any pending exceptions
44 const int FPU_CNTRL_WRD_MASK = 0xFFFF;
46 // -------------------------------------------------------------------------------------------------------------------------
47 // Stub Code definitions
49 static address handle_unsafe_access() {
50 JavaThread* thread = JavaThread::current();
51 address pc = thread->saved_exception_pc();
52 // pc is the instruction which we must emulate
53 // doing a no-op is fine: return garbage from the load
54 // therefore, compute npc
55 address npc = Assembler::locate_next_instruction(pc);
57 // request an async exception
58 thread->set_pending_unsafe_access_error();
60 // return address of next instruction to execute
61 return npc;
62 }
64 class StubGenerator: public StubCodeGenerator {
65 private:
67 #ifdef PRODUCT
68 #define inc_counter_np(counter) (0)
69 #else
70 void inc_counter_np_(int& counter) {
71 __ incrementl(ExternalAddress((address)&counter));
72 }
73 #define inc_counter_np(counter) \
74 BLOCK_COMMENT("inc_counter " #counter); \
75 inc_counter_np_(counter);
76 #endif //PRODUCT
78 void inc_copy_counter_np(BasicType t) {
79 #ifndef PRODUCT
80 switch (t) {
81 case T_BYTE: inc_counter_np(SharedRuntime::_jbyte_array_copy_ctr); return;
82 case T_SHORT: inc_counter_np(SharedRuntime::_jshort_array_copy_ctr); return;
83 case T_INT: inc_counter_np(SharedRuntime::_jint_array_copy_ctr); return;
84 case T_LONG: inc_counter_np(SharedRuntime::_jlong_array_copy_ctr); return;
85 case T_OBJECT: inc_counter_np(SharedRuntime::_oop_array_copy_ctr); return;
86 }
87 ShouldNotReachHere();
88 #endif //PRODUCT
89 }
91 //------------------------------------------------------------------------------------------------------------------------
92 // Call stubs are used to call Java from C
93 //
94 // [ return_from_Java ] <--- rsp
95 // [ argument word n ]
96 // ...
97 // -N [ argument word 1 ]
98 // -7 [ Possible padding for stack alignment ]
99 // -6 [ Possible padding for stack alignment ]
100 // -5 [ Possible padding for stack alignment ]
101 // -4 [ mxcsr save ] <--- rsp_after_call
102 // -3 [ saved rbx, ]
103 // -2 [ saved rsi ]
104 // -1 [ saved rdi ]
105 // 0 [ saved rbp, ] <--- rbp,
106 // 1 [ return address ]
107 // 2 [ ptr. to call wrapper ]
108 // 3 [ result ]
109 // 4 [ result_type ]
110 // 5 [ method ]
111 // 6 [ entry_point ]
112 // 7 [ parameters ]
113 // 8 [ parameter_size ]
114 // 9 [ thread ]
117 address generate_call_stub(address& return_address) {
118 StubCodeMark mark(this, "StubRoutines", "call_stub");
119 address start = __ pc();
121 // stub code parameters / addresses
122 assert(frame::entry_frame_call_wrapper_offset == 2, "adjust this code");
123 bool sse_save = false;
124 const Address rsp_after_call(rbp, -4 * wordSize); // same as in generate_catch_exception()!
125 const int locals_count_in_bytes (4*wordSize);
126 const Address mxcsr_save (rbp, -4 * wordSize);
127 const Address saved_rbx (rbp, -3 * wordSize);
128 const Address saved_rsi (rbp, -2 * wordSize);
129 const Address saved_rdi (rbp, -1 * wordSize);
130 const Address result (rbp, 3 * wordSize);
131 const Address result_type (rbp, 4 * wordSize);
132 const Address method (rbp, 5 * wordSize);
133 const Address entry_point (rbp, 6 * wordSize);
134 const Address parameters (rbp, 7 * wordSize);
135 const Address parameter_size(rbp, 8 * wordSize);
136 const Address thread (rbp, 9 * wordSize); // same as in generate_catch_exception()!
137 sse_save = UseSSE > 0;
139 // stub code
140 __ enter();
141 __ movptr(rcx, parameter_size); // parameter counter
142 __ shlptr(rcx, Interpreter::logStackElementSize()); // convert parameter count to bytes
143 __ addptr(rcx, locals_count_in_bytes); // reserve space for register saves
144 __ subptr(rsp, rcx);
145 __ andptr(rsp, -(StackAlignmentInBytes)); // Align stack
147 // save rdi, rsi, & rbx, according to C calling conventions
148 __ movptr(saved_rdi, rdi);
149 __ movptr(saved_rsi, rsi);
150 __ movptr(saved_rbx, rbx);
151 // save and initialize %mxcsr
152 if (sse_save) {
153 Label skip_ldmx;
154 __ stmxcsr(mxcsr_save);
155 __ movl(rax, mxcsr_save);
156 __ andl(rax, MXCSR_MASK); // Only check control and mask bits
157 ExternalAddress mxcsr_std(StubRoutines::addr_mxcsr_std());
158 __ cmp32(rax, mxcsr_std);
159 __ jcc(Assembler::equal, skip_ldmx);
160 __ ldmxcsr(mxcsr_std);
161 __ bind(skip_ldmx);
162 }
164 // make sure the control word is correct.
165 __ fldcw(ExternalAddress(StubRoutines::addr_fpu_cntrl_wrd_std()));
167 #ifdef ASSERT
168 // make sure we have no pending exceptions
169 { Label L;
170 __ movptr(rcx, thread);
171 __ cmpptr(Address(rcx, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
172 __ jcc(Assembler::equal, L);
173 __ stop("StubRoutines::call_stub: entered with pending exception");
174 __ bind(L);
175 }
176 #endif
178 // pass parameters if any
179 BLOCK_COMMENT("pass parameters if any");
180 Label parameters_done;
181 __ movl(rcx, parameter_size); // parameter counter
182 __ testl(rcx, rcx);
183 __ jcc(Assembler::zero, parameters_done);
185 // parameter passing loop
187 Label loop;
188 // Copy Java parameters in reverse order (receiver last)
189 // Note that the argument order is inverted in the process
190 // source is rdx[rcx: N-1..0]
191 // dest is rsp[rbx: 0..N-1]
193 __ movptr(rdx, parameters); // parameter pointer
194 __ xorptr(rbx, rbx);
196 __ BIND(loop);
197 if (TaggedStackInterpreter) {
198 __ movptr(rax, Address(rdx, rcx, Interpreter::stackElementScale(),
199 -2*wordSize)); // get tag
200 __ movptr(Address(rsp, rbx, Interpreter::stackElementScale(),
201 Interpreter::expr_tag_offset_in_bytes(0)), rax); // store tag
202 }
204 // get parameter
205 __ movptr(rax, Address(rdx, rcx, Interpreter::stackElementScale(), -wordSize));
206 __ movptr(Address(rsp, rbx, Interpreter::stackElementScale(),
207 Interpreter::expr_offset_in_bytes(0)), rax); // store parameter
208 __ increment(rbx);
209 __ decrement(rcx);
210 __ jcc(Assembler::notZero, loop);
212 // call Java function
213 __ BIND(parameters_done);
214 __ movptr(rbx, method); // get methodOop
215 __ movptr(rax, entry_point); // get entry_point
216 __ mov(rsi, rsp); // set sender sp
217 BLOCK_COMMENT("call Java function");
218 __ call(rax);
220 BLOCK_COMMENT("call_stub_return_address:");
221 return_address = __ pc();
223 Label common_return;
225 __ BIND(common_return);
227 // store result depending on type
228 // (everything that is not T_LONG, T_FLOAT or T_DOUBLE is treated as T_INT)
229 __ movptr(rdi, result);
230 Label is_long, is_float, is_double, exit;
231 __ movl(rsi, result_type);
232 __ cmpl(rsi, T_LONG);
233 __ jcc(Assembler::equal, is_long);
234 __ cmpl(rsi, T_FLOAT);
235 __ jcc(Assembler::equal, is_float);
236 __ cmpl(rsi, T_DOUBLE);
237 __ jcc(Assembler::equal, is_double);
239 // handle T_INT case
240 __ movl(Address(rdi, 0), rax);
241 __ BIND(exit);
243 // check that FPU stack is empty
244 __ verify_FPU(0, "generate_call_stub");
246 // pop parameters
247 __ lea(rsp, rsp_after_call);
249 // restore %mxcsr
250 if (sse_save) {
251 __ ldmxcsr(mxcsr_save);
252 }
254 // restore rdi, rsi and rbx,
255 __ movptr(rbx, saved_rbx);
256 __ movptr(rsi, saved_rsi);
257 __ movptr(rdi, saved_rdi);
258 __ addptr(rsp, 4*wordSize);
260 // return
261 __ pop(rbp);
262 __ ret(0);
264 // handle return types different from T_INT
265 __ BIND(is_long);
266 __ movl(Address(rdi, 0 * wordSize), rax);
267 __ movl(Address(rdi, 1 * wordSize), rdx);
268 __ jmp(exit);
270 __ BIND(is_float);
271 // interpreter uses xmm0 for return values
272 if (UseSSE >= 1) {
273 __ movflt(Address(rdi, 0), xmm0);
274 } else {
275 __ fstp_s(Address(rdi, 0));
276 }
277 __ jmp(exit);
279 __ BIND(is_double);
280 // interpreter uses xmm0 for return values
281 if (UseSSE >= 2) {
282 __ movdbl(Address(rdi, 0), xmm0);
283 } else {
284 __ fstp_d(Address(rdi, 0));
285 }
286 __ jmp(exit);
288 // If we call compiled code directly from the call stub we will
289 // need to adjust the return back to the call stub to a specialized
290 // piece of code that can handle compiled results and cleaning the fpu
291 // stack. compiled code will be set to return here instead of the
292 // return above that handles interpreter returns.
294 BLOCK_COMMENT("call_stub_compiled_return:");
295 StubRoutines::x86::set_call_stub_compiled_return( __ pc());
297 #ifdef COMPILER2
298 if (UseSSE >= 2) {
299 __ verify_FPU(0, "call_stub_compiled_return");
300 } else {
301 for (int i = 1; i < 8; i++) {
302 __ ffree(i);
303 }
305 // UseSSE <= 1 so double result should be left on TOS
306 __ movl(rsi, result_type);
307 __ cmpl(rsi, T_DOUBLE);
308 __ jcc(Assembler::equal, common_return);
309 if (UseSSE == 0) {
310 // UseSSE == 0 so float result should be left on TOS
311 __ cmpl(rsi, T_FLOAT);
312 __ jcc(Assembler::equal, common_return);
313 }
314 __ ffree(0);
315 }
316 #endif /* COMPILER2 */
317 __ jmp(common_return);
319 return start;
320 }
323 //------------------------------------------------------------------------------------------------------------------------
324 // Return point for a Java call if there's an exception thrown in Java code.
325 // The exception is caught and transformed into a pending exception stored in
326 // JavaThread that can be tested from within the VM.
327 //
328 // Note: Usually the parameters are removed by the callee. In case of an exception
329 // crossing an activation frame boundary, that is not the case if the callee
330 // is compiled code => need to setup the rsp.
331 //
332 // rax,: exception oop
334 address generate_catch_exception() {
335 StubCodeMark mark(this, "StubRoutines", "catch_exception");
336 const Address rsp_after_call(rbp, -4 * wordSize); // same as in generate_call_stub()!
337 const Address thread (rbp, 9 * wordSize); // same as in generate_call_stub()!
338 address start = __ pc();
340 // get thread directly
341 __ movptr(rcx, thread);
342 #ifdef ASSERT
343 // verify that threads correspond
344 { Label L;
345 __ get_thread(rbx);
346 __ cmpptr(rbx, rcx);
347 __ jcc(Assembler::equal, L);
348 __ stop("StubRoutines::catch_exception: threads must correspond");
349 __ bind(L);
350 }
351 #endif
352 // set pending exception
353 __ verify_oop(rax);
354 __ movptr(Address(rcx, Thread::pending_exception_offset()), rax );
355 __ lea(Address(rcx, Thread::exception_file_offset ()),
356 ExternalAddress((address)__FILE__));
357 __ movl(Address(rcx, Thread::exception_line_offset ()), __LINE__ );
358 // complete return to VM
359 assert(StubRoutines::_call_stub_return_address != NULL, "_call_stub_return_address must have been generated before");
360 __ jump(RuntimeAddress(StubRoutines::_call_stub_return_address));
362 return start;
363 }
366 //------------------------------------------------------------------------------------------------------------------------
367 // Continuation point for runtime calls returning with a pending exception.
368 // The pending exception check happened in the runtime or native call stub.
369 // The pending exception in Thread is converted into a Java-level exception.
370 //
371 // Contract with Java-level exception handlers:
372 // rax,: exception
373 // rdx: throwing pc
374 //
375 // NOTE: At entry of this stub, exception-pc must be on stack !!
377 address generate_forward_exception() {
378 StubCodeMark mark(this, "StubRoutines", "forward exception");
379 address start = __ pc();
381 // Upon entry, the sp points to the return address returning into Java
382 // (interpreted or compiled) code; i.e., the return address becomes the
383 // throwing pc.
384 //
385 // Arguments pushed before the runtime call are still on the stack but
386 // the exception handler will reset the stack pointer -> ignore them.
387 // A potential result in registers can be ignored as well.
389 #ifdef ASSERT
390 // make sure this code is only executed if there is a pending exception
391 { Label L;
392 __ get_thread(rcx);
393 __ cmpptr(Address(rcx, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
394 __ jcc(Assembler::notEqual, L);
395 __ stop("StubRoutines::forward exception: no pending exception (1)");
396 __ bind(L);
397 }
398 #endif
400 // compute exception handler into rbx,
401 __ movptr(rax, Address(rsp, 0));
402 BLOCK_COMMENT("call exception_handler_for_return_address");
403 __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address), rax);
404 __ mov(rbx, rax);
406 // setup rax, & rdx, remove return address & clear pending exception
407 __ get_thread(rcx);
408 __ pop(rdx);
409 __ movptr(rax, Address(rcx, Thread::pending_exception_offset()));
410 __ movptr(Address(rcx, Thread::pending_exception_offset()), NULL_WORD);
412 #ifdef ASSERT
413 // make sure exception is set
414 { Label L;
415 __ testptr(rax, rax);
416 __ jcc(Assembler::notEqual, L);
417 __ stop("StubRoutines::forward exception: no pending exception (2)");
418 __ bind(L);
419 }
420 #endif
422 // continue at exception handler (return address removed)
423 // rax,: exception
424 // rbx,: exception handler
425 // rdx: throwing pc
426 __ verify_oop(rax);
427 __ jmp(rbx);
429 return start;
430 }
433 //----------------------------------------------------------------------------------------------------
434 // Support for jint Atomic::xchg(jint exchange_value, volatile jint* dest)
435 //
436 // xchg exists as far back as 8086, lock needed for MP only
437 // Stack layout immediately after call:
438 //
439 // 0 [ret addr ] <--- rsp
440 // 1 [ ex ]
441 // 2 [ dest ]
442 //
443 // Result: *dest <- ex, return (old *dest)
444 //
445 // Note: win32 does not currently use this code
447 address generate_atomic_xchg() {
448 StubCodeMark mark(this, "StubRoutines", "atomic_xchg");
449 address start = __ pc();
451 __ push(rdx);
452 Address exchange(rsp, 2 * wordSize);
453 Address dest_addr(rsp, 3 * wordSize);
454 __ movl(rax, exchange);
455 __ movptr(rdx, dest_addr);
456 __ xchgl(rax, Address(rdx, 0));
457 __ pop(rdx);
458 __ ret(0);
460 return start;
461 }
463 //----------------------------------------------------------------------------------------------------
464 // Support for void verify_mxcsr()
465 //
466 // This routine is used with -Xcheck:jni to verify that native
467 // JNI code does not return to Java code without restoring the
468 // MXCSR register to our expected state.
471 address generate_verify_mxcsr() {
472 StubCodeMark mark(this, "StubRoutines", "verify_mxcsr");
473 address start = __ pc();
475 const Address mxcsr_save(rsp, 0);
477 if (CheckJNICalls && UseSSE > 0 ) {
478 Label ok_ret;
479 ExternalAddress mxcsr_std(StubRoutines::addr_mxcsr_std());
480 __ push(rax);
481 __ subptr(rsp, wordSize); // allocate a temp location
482 __ stmxcsr(mxcsr_save);
483 __ movl(rax, mxcsr_save);
484 __ andl(rax, MXCSR_MASK);
485 __ cmp32(rax, mxcsr_std);
486 __ jcc(Assembler::equal, ok_ret);
488 __ warn("MXCSR changed by native JNI code.");
490 __ ldmxcsr(mxcsr_std);
492 __ bind(ok_ret);
493 __ addptr(rsp, wordSize);
494 __ pop(rax);
495 }
497 __ ret(0);
499 return start;
500 }
503 //---------------------------------------------------------------------------
504 // Support for void verify_fpu_cntrl_wrd()
505 //
506 // This routine is used with -Xcheck:jni to verify that native
507 // JNI code does not return to Java code without restoring the
508 // FP control word to our expected state.
510 address generate_verify_fpu_cntrl_wrd() {
511 StubCodeMark mark(this, "StubRoutines", "verify_spcw");
512 address start = __ pc();
514 const Address fpu_cntrl_wrd_save(rsp, 0);
516 if (CheckJNICalls) {
517 Label ok_ret;
518 __ push(rax);
519 __ subptr(rsp, wordSize); // allocate a temp location
520 __ fnstcw(fpu_cntrl_wrd_save);
521 __ movl(rax, fpu_cntrl_wrd_save);
522 __ andl(rax, FPU_CNTRL_WRD_MASK);
523 ExternalAddress fpu_std(StubRoutines::addr_fpu_cntrl_wrd_std());
524 __ cmp32(rax, fpu_std);
525 __ jcc(Assembler::equal, ok_ret);
527 __ warn("Floating point control word changed by native JNI code.");
529 __ fldcw(fpu_std);
531 __ bind(ok_ret);
532 __ addptr(rsp, wordSize);
533 __ pop(rax);
534 }
536 __ ret(0);
538 return start;
539 }
541 //---------------------------------------------------------------------------
542 // Wrapper for slow-case handling of double-to-integer conversion
543 // d2i or f2i fast case failed either because it is nan or because
544 // of under/overflow.
545 // Input: FPU TOS: float value
546 // Output: rax, (rdx): integer (long) result
548 address generate_d2i_wrapper(BasicType t, address fcn) {
549 StubCodeMark mark(this, "StubRoutines", "d2i_wrapper");
550 address start = __ pc();
552 // Capture info about frame layout
553 enum layout { FPUState_off = 0,
554 rbp_off = FPUStateSizeInWords,
555 rdi_off,
556 rsi_off,
557 rcx_off,
558 rbx_off,
559 saved_argument_off,
560 saved_argument_off2, // 2nd half of double
561 framesize
562 };
564 assert(FPUStateSizeInWords == 27, "update stack layout");
566 // Save outgoing argument to stack across push_FPU_state()
567 __ subptr(rsp, wordSize * 2);
568 __ fstp_d(Address(rsp, 0));
570 // Save CPU & FPU state
571 __ push(rbx);
572 __ push(rcx);
573 __ push(rsi);
574 __ push(rdi);
575 __ push(rbp);
576 __ push_FPU_state();
578 // push_FPU_state() resets the FP top of stack
579 // Load original double into FP top of stack
580 __ fld_d(Address(rsp, saved_argument_off * wordSize));
581 // Store double into stack as outgoing argument
582 __ subptr(rsp, wordSize*2);
583 __ fst_d(Address(rsp, 0));
585 // Prepare FPU for doing math in C-land
586 __ empty_FPU_stack();
587 // Call the C code to massage the double. Result in EAX
588 if (t == T_INT)
589 { BLOCK_COMMENT("SharedRuntime::d2i"); }
590 else if (t == T_LONG)
591 { BLOCK_COMMENT("SharedRuntime::d2l"); }
592 __ call_VM_leaf( fcn, 2 );
594 // Restore CPU & FPU state
595 __ pop_FPU_state();
596 __ pop(rbp);
597 __ pop(rdi);
598 __ pop(rsi);
599 __ pop(rcx);
600 __ pop(rbx);
601 __ addptr(rsp, wordSize * 2);
603 __ ret(0);
605 return start;
606 }
609 //---------------------------------------------------------------------------
610 // The following routine generates a subroutine to throw an asynchronous
611 // UnknownError when an unsafe access gets a fault that could not be
612 // reasonably prevented by the programmer. (Example: SIGBUS/OBJERR.)
613 address generate_handler_for_unsafe_access() {
614 StubCodeMark mark(this, "StubRoutines", "handler_for_unsafe_access");
615 address start = __ pc();
617 __ push(0); // hole for return address-to-be
618 __ pusha(); // push registers
619 Address next_pc(rsp, RegisterImpl::number_of_registers * BytesPerWord);
620 BLOCK_COMMENT("call handle_unsafe_access");
621 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, handle_unsafe_access)));
622 __ movptr(next_pc, rax); // stuff next address
623 __ popa();
624 __ ret(0); // jump to next address
626 return start;
627 }
630 //----------------------------------------------------------------------------------------------------
631 // Non-destructive plausibility checks for oops
633 address generate_verify_oop() {
634 StubCodeMark mark(this, "StubRoutines", "verify_oop");
635 address start = __ pc();
637 // Incoming arguments on stack after saving rax,:
638 //
639 // [tos ]: saved rdx
640 // [tos + 1]: saved EFLAGS
641 // [tos + 2]: return address
642 // [tos + 3]: char* error message
643 // [tos + 4]: oop object to verify
644 // [tos + 5]: saved rax, - saved by caller and bashed
646 Label exit, error;
647 __ pushf();
648 __ incrementl(ExternalAddress((address) StubRoutines::verify_oop_count_addr()));
649 __ push(rdx); // save rdx
650 // make sure object is 'reasonable'
651 __ movptr(rax, Address(rsp, 4 * wordSize)); // get object
652 __ testptr(rax, rax);
653 __ jcc(Assembler::zero, exit); // if obj is NULL it is ok
655 // Check if the oop is in the right area of memory
656 const int oop_mask = Universe::verify_oop_mask();
657 const int oop_bits = Universe::verify_oop_bits();
658 __ mov(rdx, rax);
659 __ andptr(rdx, oop_mask);
660 __ cmpptr(rdx, oop_bits);
661 __ jcc(Assembler::notZero, error);
663 // make sure klass is 'reasonable'
664 __ movptr(rax, Address(rax, oopDesc::klass_offset_in_bytes())); // get klass
665 __ testptr(rax, rax);
666 __ jcc(Assembler::zero, error); // if klass is NULL it is broken
668 // Check if the klass is in the right area of memory
669 const int klass_mask = Universe::verify_klass_mask();
670 const int klass_bits = Universe::verify_klass_bits();
671 __ mov(rdx, rax);
672 __ andptr(rdx, klass_mask);
673 __ cmpptr(rdx, klass_bits);
674 __ jcc(Assembler::notZero, error);
676 // make sure klass' klass is 'reasonable'
677 __ movptr(rax, Address(rax, oopDesc::klass_offset_in_bytes())); // get klass' klass
678 __ testptr(rax, rax);
679 __ jcc(Assembler::zero, error); // if klass' klass is NULL it is broken
681 __ mov(rdx, rax);
682 __ andptr(rdx, klass_mask);
683 __ cmpptr(rdx, klass_bits);
684 __ jcc(Assembler::notZero, error); // if klass not in right area
685 // of memory it is broken too.
687 // return if everything seems ok
688 __ bind(exit);
689 __ movptr(rax, Address(rsp, 5 * wordSize)); // get saved rax, back
690 __ pop(rdx); // restore rdx
691 __ popf(); // restore EFLAGS
692 __ ret(3 * wordSize); // pop arguments
694 // handle errors
695 __ bind(error);
696 __ movptr(rax, Address(rsp, 5 * wordSize)); // get saved rax, back
697 __ pop(rdx); // get saved rdx back
698 __ popf(); // get saved EFLAGS off stack -- will be ignored
699 __ pusha(); // push registers (eip = return address & msg are already pushed)
700 BLOCK_COMMENT("call MacroAssembler::debug");
701 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, MacroAssembler::debug32)));
702 __ popa();
703 __ ret(3 * wordSize); // pop arguments
704 return start;
705 }
707 //
708 // Generate pre-barrier for array stores
709 //
710 // Input:
711 // start - starting address
712 // count - element count
713 void gen_write_ref_array_pre_barrier(Register start, Register count) {
714 assert_different_registers(start, count);
715 BarrierSet* bs = Universe::heap()->barrier_set();
716 switch (bs->kind()) {
717 case BarrierSet::G1SATBCT:
718 case BarrierSet::G1SATBCTLogging:
719 {
720 __ pusha(); // push registers
721 __ push(count);
722 __ push(start);
723 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_pre)));
724 __ addptr(rsp, 2*wordSize);
725 __ popa();
726 }
727 break;
728 case BarrierSet::CardTableModRef:
729 case BarrierSet::CardTableExtension:
730 case BarrierSet::ModRef:
731 break;
732 default :
733 ShouldNotReachHere();
735 }
736 }
739 //
740 // Generate a post-barrier for an array store
741 //
742 // start - starting address
743 // count - element count
744 //
745 // The two input registers are overwritten.
746 //
747 void gen_write_ref_array_post_barrier(Register start, Register count) {
748 BarrierSet* bs = Universe::heap()->barrier_set();
749 assert_different_registers(start, count);
750 switch (bs->kind()) {
751 case BarrierSet::G1SATBCT:
752 case BarrierSet::G1SATBCTLogging:
753 {
754 __ pusha(); // push registers
755 __ push(count);
756 __ push(start);
757 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, BarrierSet::static_write_ref_array_post)));
758 __ addptr(rsp, 2*wordSize);
759 __ popa();
760 }
761 break;
763 case BarrierSet::CardTableModRef:
764 case BarrierSet::CardTableExtension:
765 {
766 CardTableModRefBS* ct = (CardTableModRefBS*)bs;
767 assert(sizeof(*ct->byte_map_base) == sizeof(jbyte), "adjust this code");
769 Label L_loop;
770 const Register end = count; // elements count; end == start+count-1
771 assert_different_registers(start, end);
773 __ lea(end, Address(start, count, Address::times_ptr, -wordSize));
774 __ shrptr(start, CardTableModRefBS::card_shift);
775 __ shrptr(end, CardTableModRefBS::card_shift);
776 __ subptr(end, start); // end --> count
777 __ BIND(L_loop);
778 intptr_t disp = (intptr_t) ct->byte_map_base;
779 Address cardtable(start, count, Address::times_1, disp);
780 __ movb(cardtable, 0);
781 __ decrement(count);
782 __ jcc(Assembler::greaterEqual, L_loop);
783 }
784 break;
785 case BarrierSet::ModRef:
786 break;
787 default :
788 ShouldNotReachHere();
790 }
791 }
794 // Copy 64 bytes chunks
795 //
796 // Inputs:
797 // from - source array address
798 // to_from - destination array address - from
799 // qword_count - 8-bytes element count, negative
800 //
801 void xmm_copy_forward(Register from, Register to_from, Register qword_count) {
802 assert( UseSSE >= 2, "supported cpu only" );
803 Label L_copy_64_bytes_loop, L_copy_64_bytes, L_copy_8_bytes, L_exit;
804 // Copy 64-byte chunks
805 __ jmpb(L_copy_64_bytes);
806 __ align(16);
807 __ BIND(L_copy_64_bytes_loop);
809 if(UseUnalignedLoadStores) {
810 __ movdqu(xmm0, Address(from, 0));
811 __ movdqu(Address(from, to_from, Address::times_1, 0), xmm0);
812 __ movdqu(xmm1, Address(from, 16));
813 __ movdqu(Address(from, to_from, Address::times_1, 16), xmm1);
814 __ movdqu(xmm2, Address(from, 32));
815 __ movdqu(Address(from, to_from, Address::times_1, 32), xmm2);
816 __ movdqu(xmm3, Address(from, 48));
817 __ movdqu(Address(from, to_from, Address::times_1, 48), xmm3);
819 } else {
820 __ movq(xmm0, Address(from, 0));
821 __ movq(Address(from, to_from, Address::times_1, 0), xmm0);
822 __ movq(xmm1, Address(from, 8));
823 __ movq(Address(from, to_from, Address::times_1, 8), xmm1);
824 __ movq(xmm2, Address(from, 16));
825 __ movq(Address(from, to_from, Address::times_1, 16), xmm2);
826 __ movq(xmm3, Address(from, 24));
827 __ movq(Address(from, to_from, Address::times_1, 24), xmm3);
828 __ movq(xmm4, Address(from, 32));
829 __ movq(Address(from, to_from, Address::times_1, 32), xmm4);
830 __ movq(xmm5, Address(from, 40));
831 __ movq(Address(from, to_from, Address::times_1, 40), xmm5);
832 __ movq(xmm6, Address(from, 48));
833 __ movq(Address(from, to_from, Address::times_1, 48), xmm6);
834 __ movq(xmm7, Address(from, 56));
835 __ movq(Address(from, to_from, Address::times_1, 56), xmm7);
836 }
838 __ addl(from, 64);
839 __ BIND(L_copy_64_bytes);
840 __ subl(qword_count, 8);
841 __ jcc(Assembler::greaterEqual, L_copy_64_bytes_loop);
842 __ addl(qword_count, 8);
843 __ jccb(Assembler::zero, L_exit);
844 //
845 // length is too short, just copy qwords
846 //
847 __ BIND(L_copy_8_bytes);
848 __ movq(xmm0, Address(from, 0));
849 __ movq(Address(from, to_from, Address::times_1), xmm0);
850 __ addl(from, 8);
851 __ decrement(qword_count);
852 __ jcc(Assembler::greater, L_copy_8_bytes);
853 __ BIND(L_exit);
854 }
856 // Copy 64 bytes chunks
857 //
858 // Inputs:
859 // from - source array address
860 // to_from - destination array address - from
861 // qword_count - 8-bytes element count, negative
862 //
863 void mmx_copy_forward(Register from, Register to_from, Register qword_count) {
864 assert( VM_Version::supports_mmx(), "supported cpu only" );
865 Label L_copy_64_bytes_loop, L_copy_64_bytes, L_copy_8_bytes, L_exit;
866 // Copy 64-byte chunks
867 __ jmpb(L_copy_64_bytes);
868 __ align(16);
869 __ BIND(L_copy_64_bytes_loop);
870 __ movq(mmx0, Address(from, 0));
871 __ movq(mmx1, Address(from, 8));
872 __ movq(mmx2, Address(from, 16));
873 __ movq(Address(from, to_from, Address::times_1, 0), mmx0);
874 __ movq(mmx3, Address(from, 24));
875 __ movq(Address(from, to_from, Address::times_1, 8), mmx1);
876 __ movq(mmx4, Address(from, 32));
877 __ movq(Address(from, to_from, Address::times_1, 16), mmx2);
878 __ movq(mmx5, Address(from, 40));
879 __ movq(Address(from, to_from, Address::times_1, 24), mmx3);
880 __ movq(mmx6, Address(from, 48));
881 __ movq(Address(from, to_from, Address::times_1, 32), mmx4);
882 __ movq(mmx7, Address(from, 56));
883 __ movq(Address(from, to_from, Address::times_1, 40), mmx5);
884 __ movq(Address(from, to_from, Address::times_1, 48), mmx6);
885 __ movq(Address(from, to_from, Address::times_1, 56), mmx7);
886 __ addptr(from, 64);
887 __ BIND(L_copy_64_bytes);
888 __ subl(qword_count, 8);
889 __ jcc(Assembler::greaterEqual, L_copy_64_bytes_loop);
890 __ addl(qword_count, 8);
891 __ jccb(Assembler::zero, L_exit);
892 //
893 // length is too short, just copy qwords
894 //
895 __ BIND(L_copy_8_bytes);
896 __ movq(mmx0, Address(from, 0));
897 __ movq(Address(from, to_from, Address::times_1), mmx0);
898 __ addptr(from, 8);
899 __ decrement(qword_count);
900 __ jcc(Assembler::greater, L_copy_8_bytes);
901 __ BIND(L_exit);
902 __ emms();
903 }
905 address generate_disjoint_copy(BasicType t, bool aligned,
906 Address::ScaleFactor sf,
907 address* entry, const char *name) {
908 __ align(CodeEntryAlignment);
909 StubCodeMark mark(this, "StubRoutines", name);
910 address start = __ pc();
912 Label L_0_count, L_exit, L_skip_align1, L_skip_align2, L_copy_byte;
913 Label L_copy_2_bytes, L_copy_4_bytes, L_copy_64_bytes;
915 int shift = Address::times_ptr - sf;
917 const Register from = rsi; // source array address
918 const Register to = rdi; // destination array address
919 const Register count = rcx; // elements count
920 const Register to_from = to; // (to - from)
921 const Register saved_to = rdx; // saved destination array address
923 __ enter(); // required for proper stackwalking of RuntimeStub frame
924 __ push(rsi);
925 __ push(rdi);
926 __ movptr(from , Address(rsp, 12+ 4));
927 __ movptr(to , Address(rsp, 12+ 8));
928 __ movl(count, Address(rsp, 12+ 12));
929 if (t == T_OBJECT) {
930 __ testl(count, count);
931 __ jcc(Assembler::zero, L_0_count);
932 gen_write_ref_array_pre_barrier(to, count);
933 __ mov(saved_to, to); // save 'to'
934 }
936 *entry = __ pc(); // Entry point from conjoint arraycopy stub.
937 BLOCK_COMMENT("Entry:");
939 __ subptr(to, from); // to --> to_from
940 __ cmpl(count, 2<<shift); // Short arrays (< 8 bytes) copy by element
941 __ jcc(Assembler::below, L_copy_4_bytes); // use unsigned cmp
942 if (!UseUnalignedLoadStores && !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 __ testl(from, 1);
947 __ jccb(Assembler::zero, L_skip_align1);
948 __ movb(rax, Address(from, 0));
949 __ movb(Address(from, to_from, Address::times_1, 0), rax);
950 __ increment(from);
951 __ decrement(count);
952 __ BIND(L_skip_align1);
953 }
954 // Two bytes misalignment happens only for byte and short (char) arrays
955 __ testl(from, 2);
956 __ jccb(Assembler::zero, L_skip_align2);
957 __ movw(rax, Address(from, 0));
958 __ movw(Address(from, to_from, Address::times_1, 0), rax);
959 __ addptr(from, 2);
960 __ subl(count, 1<<(shift-1));
961 __ BIND(L_skip_align2);
962 }
963 if (!VM_Version::supports_mmx()) {
964 __ mov(rax, count); // save 'count'
965 __ shrl(count, shift); // bytes count
966 __ addptr(to_from, from);// restore 'to'
967 __ rep_mov();
968 __ subptr(to_from, from);// restore 'to_from'
969 __ mov(count, rax); // restore 'count'
970 __ jmpb(L_copy_2_bytes); // all dwords were copied
971 } else {
972 if (!UseUnalignedLoadStores) {
973 // align to 8 bytes, we know we are 4 byte aligned to start
974 __ testptr(from, 4);
975 __ jccb(Assembler::zero, L_copy_64_bytes);
976 __ movl(rax, Address(from, 0));
977 __ movl(Address(from, to_from, Address::times_1, 0), rax);
978 __ addptr(from, 4);
979 __ subl(count, 1<<shift);
980 }
981 __ BIND(L_copy_64_bytes);
982 __ mov(rax, count);
983 __ shrl(rax, shift+1); // 8 bytes chunk count
984 //
985 // Copy 8-byte chunks through MMX registers, 8 per iteration of the loop
986 //
987 if (UseXMMForArrayCopy) {
988 xmm_copy_forward(from, to_from, rax);
989 } else {
990 mmx_copy_forward(from, to_from, rax);
991 }
992 }
993 // copy tailing dword
994 __ BIND(L_copy_4_bytes);
995 __ testl(count, 1<<shift);
996 __ jccb(Assembler::zero, L_copy_2_bytes);
997 __ movl(rax, Address(from, 0));
998 __ movl(Address(from, to_from, Address::times_1, 0), rax);
999 if (t == T_BYTE || t == T_SHORT) {
1000 __ addptr(from, 4);
1001 __ BIND(L_copy_2_bytes);
1002 // copy tailing word
1003 __ testl(count, 1<<(shift-1));
1004 __ jccb(Assembler::zero, L_copy_byte);
1005 __ movw(rax, Address(from, 0));
1006 __ movw(Address(from, to_from, Address::times_1, 0), rax);
1007 if (t == T_BYTE) {
1008 __ addptr(from, 2);
1009 __ BIND(L_copy_byte);
1010 // copy tailing byte
1011 __ testl(count, 1);
1012 __ jccb(Assembler::zero, L_exit);
1013 __ movb(rax, Address(from, 0));
1014 __ movb(Address(from, to_from, Address::times_1, 0), rax);
1015 __ BIND(L_exit);
1016 } else {
1017 __ BIND(L_copy_byte);
1018 }
1019 } else {
1020 __ BIND(L_copy_2_bytes);
1021 }
1023 if (t == T_OBJECT) {
1024 __ movl(count, Address(rsp, 12+12)); // reread 'count'
1025 __ mov(to, saved_to); // restore 'to'
1026 gen_write_ref_array_post_barrier(to, count);
1027 __ BIND(L_0_count);
1028 }
1029 inc_copy_counter_np(t);
1030 __ pop(rdi);
1031 __ pop(rsi);
1032 __ leave(); // required for proper stackwalking of RuntimeStub frame
1033 __ xorptr(rax, rax); // return 0
1034 __ ret(0);
1035 return start;
1036 }
1039 address generate_conjoint_copy(BasicType t, bool aligned,
1040 Address::ScaleFactor sf,
1041 address nooverlap_target,
1042 address* entry, const char *name) {
1043 __ align(CodeEntryAlignment);
1044 StubCodeMark mark(this, "StubRoutines", name);
1045 address start = __ pc();
1047 Label L_0_count, L_exit, L_skip_align1, L_skip_align2, L_copy_byte;
1048 Label L_copy_2_bytes, L_copy_4_bytes, L_copy_8_bytes, L_copy_8_bytes_loop;
1050 int shift = Address::times_ptr - sf;
1052 const Register src = rax; // source array address
1053 const Register dst = rdx; // destination array address
1054 const Register from = rsi; // source array address
1055 const Register to = rdi; // destination array address
1056 const Register count = rcx; // elements count
1057 const Register end = rax; // array end address
1059 __ enter(); // required for proper stackwalking of RuntimeStub frame
1060 __ push(rsi);
1061 __ push(rdi);
1062 __ movptr(src , Address(rsp, 12+ 4)); // from
1063 __ movptr(dst , Address(rsp, 12+ 8)); // to
1064 __ movl2ptr(count, Address(rsp, 12+12)); // count
1065 if (t == T_OBJECT) {
1066 gen_write_ref_array_pre_barrier(dst, count);
1067 }
1069 if (entry != NULL) {
1070 *entry = __ pc(); // Entry point from generic arraycopy stub.
1071 BLOCK_COMMENT("Entry:");
1072 }
1074 if (t == T_OBJECT) {
1075 __ testl(count, count);
1076 __ jcc(Assembler::zero, L_0_count);
1077 }
1078 __ mov(from, src);
1079 __ mov(to , dst);
1081 // arrays overlap test
1082 RuntimeAddress nooverlap(nooverlap_target);
1083 __ cmpptr(dst, src);
1084 __ lea(end, Address(src, count, sf, 0)); // src + count * elem_size
1085 __ jump_cc(Assembler::belowEqual, nooverlap);
1086 __ cmpptr(dst, end);
1087 __ jump_cc(Assembler::aboveEqual, nooverlap);
1089 // copy from high to low
1090 __ cmpl(count, 2<<shift); // Short arrays (< 8 bytes) copy by element
1091 __ jcc(Assembler::below, L_copy_4_bytes); // use unsigned cmp
1092 if (t == T_BYTE || t == T_SHORT) {
1093 // Align the end of destination array at 4 bytes address boundary
1094 __ lea(end, Address(dst, count, sf, 0));
1095 if (t == T_BYTE) {
1096 // One byte misalignment happens only for byte arrays
1097 __ testl(end, 1);
1098 __ jccb(Assembler::zero, L_skip_align1);
1099 __ decrement(count);
1100 __ movb(rdx, Address(from, count, sf, 0));
1101 __ movb(Address(to, count, sf, 0), rdx);
1102 __ BIND(L_skip_align1);
1103 }
1104 // Two bytes misalignment happens only for byte and short (char) arrays
1105 __ testl(end, 2);
1106 __ jccb(Assembler::zero, L_skip_align2);
1107 __ subptr(count, 1<<(shift-1));
1108 __ movw(rdx, Address(from, count, sf, 0));
1109 __ movw(Address(to, count, sf, 0), rdx);
1110 __ BIND(L_skip_align2);
1111 __ cmpl(count, 2<<shift); // Short arrays (< 8 bytes) copy by element
1112 __ jcc(Assembler::below, L_copy_4_bytes);
1113 }
1115 if (!VM_Version::supports_mmx()) {
1116 __ std();
1117 __ mov(rax, count); // Save 'count'
1118 __ mov(rdx, to); // Save 'to'
1119 __ lea(rsi, Address(from, count, sf, -4));
1120 __ lea(rdi, Address(to , count, sf, -4));
1121 __ shrptr(count, shift); // bytes count
1122 __ rep_mov();
1123 __ cld();
1124 __ mov(count, rax); // restore 'count'
1125 __ andl(count, (1<<shift)-1); // mask the number of rest elements
1126 __ movptr(from, Address(rsp, 12+4)); // reread 'from'
1127 __ mov(to, rdx); // restore 'to'
1128 __ jmpb(L_copy_2_bytes); // all dword were copied
1129 } else {
1130 // Align to 8 bytes the end of array. It is aligned to 4 bytes already.
1131 __ testptr(end, 4);
1132 __ jccb(Assembler::zero, L_copy_8_bytes);
1133 __ subl(count, 1<<shift);
1134 __ movl(rdx, Address(from, count, sf, 0));
1135 __ movl(Address(to, count, sf, 0), rdx);
1136 __ jmpb(L_copy_8_bytes);
1138 __ align(16);
1139 // Move 8 bytes
1140 __ BIND(L_copy_8_bytes_loop);
1141 if (UseXMMForArrayCopy) {
1142 __ movq(xmm0, Address(from, count, sf, 0));
1143 __ movq(Address(to, count, sf, 0), xmm0);
1144 } else {
1145 __ movq(mmx0, Address(from, count, sf, 0));
1146 __ movq(Address(to, count, sf, 0), mmx0);
1147 }
1148 __ BIND(L_copy_8_bytes);
1149 __ subl(count, 2<<shift);
1150 __ jcc(Assembler::greaterEqual, L_copy_8_bytes_loop);
1151 __ addl(count, 2<<shift);
1152 if (!UseXMMForArrayCopy) {
1153 __ emms();
1154 }
1155 }
1156 __ BIND(L_copy_4_bytes);
1157 // copy prefix qword
1158 __ testl(count, 1<<shift);
1159 __ jccb(Assembler::zero, L_copy_2_bytes);
1160 __ movl(rdx, Address(from, count, sf, -4));
1161 __ movl(Address(to, count, sf, -4), rdx);
1163 if (t == T_BYTE || t == T_SHORT) {
1164 __ subl(count, (1<<shift));
1165 __ BIND(L_copy_2_bytes);
1166 // copy prefix dword
1167 __ testl(count, 1<<(shift-1));
1168 __ jccb(Assembler::zero, L_copy_byte);
1169 __ movw(rdx, Address(from, count, sf, -2));
1170 __ movw(Address(to, count, sf, -2), rdx);
1171 if (t == T_BYTE) {
1172 __ subl(count, 1<<(shift-1));
1173 __ BIND(L_copy_byte);
1174 // copy prefix byte
1175 __ testl(count, 1);
1176 __ jccb(Assembler::zero, L_exit);
1177 __ movb(rdx, Address(from, 0));
1178 __ movb(Address(to, 0), rdx);
1179 __ BIND(L_exit);
1180 } else {
1181 __ BIND(L_copy_byte);
1182 }
1183 } else {
1184 __ BIND(L_copy_2_bytes);
1185 }
1186 if (t == T_OBJECT) {
1187 __ movl2ptr(count, Address(rsp, 12+12)); // reread count
1188 gen_write_ref_array_post_barrier(to, count);
1189 __ BIND(L_0_count);
1190 }
1191 inc_copy_counter_np(t);
1192 __ pop(rdi);
1193 __ pop(rsi);
1194 __ leave(); // required for proper stackwalking of RuntimeStub frame
1195 __ xorptr(rax, rax); // return 0
1196 __ ret(0);
1197 return start;
1198 }
1201 address generate_disjoint_long_copy(address* entry, const char *name) {
1202 __ align(CodeEntryAlignment);
1203 StubCodeMark mark(this, "StubRoutines", name);
1204 address start = __ pc();
1206 Label L_copy_8_bytes, L_copy_8_bytes_loop;
1207 const Register from = rax; // source array address
1208 const Register to = rdx; // destination array address
1209 const Register count = rcx; // elements count
1210 const Register to_from = rdx; // (to - from)
1212 __ enter(); // required for proper stackwalking of RuntimeStub frame
1213 __ movptr(from , Address(rsp, 8+0)); // from
1214 __ movptr(to , Address(rsp, 8+4)); // to
1215 __ movl2ptr(count, Address(rsp, 8+8)); // count
1217 *entry = __ pc(); // Entry point from conjoint arraycopy stub.
1218 BLOCK_COMMENT("Entry:");
1220 __ subptr(to, from); // to --> to_from
1221 if (VM_Version::supports_mmx()) {
1222 if (UseXMMForArrayCopy) {
1223 xmm_copy_forward(from, to_from, count);
1224 } else {
1225 mmx_copy_forward(from, to_from, count);
1226 }
1227 } else {
1228 __ jmpb(L_copy_8_bytes);
1229 __ align(16);
1230 __ BIND(L_copy_8_bytes_loop);
1231 __ fild_d(Address(from, 0));
1232 __ fistp_d(Address(from, to_from, Address::times_1));
1233 __ addptr(from, 8);
1234 __ BIND(L_copy_8_bytes);
1235 __ decrement(count);
1236 __ jcc(Assembler::greaterEqual, L_copy_8_bytes_loop);
1237 }
1238 inc_copy_counter_np(T_LONG);
1239 __ leave(); // required for proper stackwalking of RuntimeStub frame
1240 __ xorptr(rax, rax); // return 0
1241 __ ret(0);
1242 return start;
1243 }
1245 address generate_conjoint_long_copy(address nooverlap_target,
1246 address* entry, const char *name) {
1247 __ align(CodeEntryAlignment);
1248 StubCodeMark mark(this, "StubRoutines", name);
1249 address start = __ pc();
1251 Label L_copy_8_bytes, L_copy_8_bytes_loop;
1252 const Register from = rax; // source array address
1253 const Register to = rdx; // destination array address
1254 const Register count = rcx; // elements count
1255 const Register end_from = rax; // source array end address
1257 __ enter(); // required for proper stackwalking of RuntimeStub frame
1258 __ movptr(from , Address(rsp, 8+0)); // from
1259 __ movptr(to , Address(rsp, 8+4)); // to
1260 __ movl2ptr(count, Address(rsp, 8+8)); // count
1262 *entry = __ pc(); // Entry point from generic arraycopy stub.
1263 BLOCK_COMMENT("Entry:");
1265 // arrays overlap test
1266 __ cmpptr(to, from);
1267 RuntimeAddress nooverlap(nooverlap_target);
1268 __ jump_cc(Assembler::belowEqual, nooverlap);
1269 __ lea(end_from, Address(from, count, Address::times_8, 0));
1270 __ cmpptr(to, end_from);
1271 __ movptr(from, Address(rsp, 8)); // from
1272 __ jump_cc(Assembler::aboveEqual, nooverlap);
1274 __ jmpb(L_copy_8_bytes);
1276 __ align(16);
1277 __ BIND(L_copy_8_bytes_loop);
1278 if (VM_Version::supports_mmx()) {
1279 if (UseXMMForArrayCopy) {
1280 __ movq(xmm0, Address(from, count, Address::times_8));
1281 __ movq(Address(to, count, Address::times_8), xmm0);
1282 } else {
1283 __ movq(mmx0, Address(from, count, Address::times_8));
1284 __ movq(Address(to, count, Address::times_8), mmx0);
1285 }
1286 } else {
1287 __ fild_d(Address(from, count, Address::times_8));
1288 __ fistp_d(Address(to, count, Address::times_8));
1289 }
1290 __ BIND(L_copy_8_bytes);
1291 __ decrement(count);
1292 __ jcc(Assembler::greaterEqual, L_copy_8_bytes_loop);
1294 if (VM_Version::supports_mmx() && !UseXMMForArrayCopy) {
1295 __ emms();
1296 }
1297 inc_copy_counter_np(T_LONG);
1298 __ leave(); // required for proper stackwalking of RuntimeStub frame
1299 __ xorptr(rax, rax); // return 0
1300 __ ret(0);
1301 return start;
1302 }
1305 // Helper for generating a dynamic type check.
1306 // The sub_klass must be one of {rbx, rdx, rsi}.
1307 // The temp is killed.
1308 void generate_type_check(Register sub_klass,
1309 Address& super_check_offset_addr,
1310 Address& super_klass_addr,
1311 Register temp,
1312 Label* L_success, Label* L_failure) {
1313 BLOCK_COMMENT("type_check:");
1315 Label L_fallthrough;
1316 #define LOCAL_JCC(assembler_con, label_ptr) \
1317 if (label_ptr != NULL) __ jcc(assembler_con, *(label_ptr)); \
1318 else __ jcc(assembler_con, L_fallthrough) /*omit semi*/
1320 // The following is a strange variation of the fast path which requires
1321 // one less register, because needed values are on the argument stack.
1322 // __ check_klass_subtype_fast_path(sub_klass, *super_klass*, temp,
1323 // L_success, L_failure, NULL);
1324 assert_different_registers(sub_klass, temp);
1326 int sc_offset = (klassOopDesc::header_size() * HeapWordSize +
1327 Klass::secondary_super_cache_offset_in_bytes());
1329 // if the pointers are equal, we are done (e.g., String[] elements)
1330 __ cmpptr(sub_klass, super_klass_addr);
1331 LOCAL_JCC(Assembler::equal, L_success);
1333 // check the supertype display:
1334 __ movl2ptr(temp, super_check_offset_addr);
1335 Address super_check_addr(sub_klass, temp, Address::times_1, 0);
1336 __ movptr(temp, super_check_addr); // load displayed supertype
1337 __ cmpptr(temp, super_klass_addr); // test the super type
1338 LOCAL_JCC(Assembler::equal, L_success);
1340 // if it was a primary super, we can just fail immediately
1341 __ cmpl(super_check_offset_addr, sc_offset);
1342 LOCAL_JCC(Assembler::notEqual, L_failure);
1344 // The repne_scan instruction uses fixed registers, which will get spilled.
1345 // We happen to know this works best when super_klass is in rax.
1346 Register super_klass = temp;
1347 __ movptr(super_klass, super_klass_addr);
1348 __ check_klass_subtype_slow_path(sub_klass, super_klass, noreg, noreg,
1349 L_success, L_failure);
1351 __ bind(L_fallthrough);
1353 if (L_success == NULL) { BLOCK_COMMENT("L_success:"); }
1354 if (L_failure == NULL) { BLOCK_COMMENT("L_failure:"); }
1356 #undef LOCAL_JCC
1357 }
1359 //
1360 // Generate checkcasting array copy stub
1361 //
1362 // Input:
1363 // 4(rsp) - source array address
1364 // 8(rsp) - destination array address
1365 // 12(rsp) - element count, can be zero
1366 // 16(rsp) - size_t ckoff (super_check_offset)
1367 // 20(rsp) - oop ckval (super_klass)
1368 //
1369 // Output:
1370 // rax, == 0 - success
1371 // rax, == -1^K - failure, where K is partial transfer count
1372 //
1373 address generate_checkcast_copy(const char *name, address* entry) {
1374 __ align(CodeEntryAlignment);
1375 StubCodeMark mark(this, "StubRoutines", name);
1376 address start = __ pc();
1378 Label L_load_element, L_store_element, L_do_card_marks, L_done;
1380 // register use:
1381 // rax, rdx, rcx -- loop control (end_from, end_to, count)
1382 // rdi, rsi -- element access (oop, klass)
1383 // rbx, -- temp
1384 const Register from = rax; // source array address
1385 const Register to = rdx; // destination array address
1386 const Register length = rcx; // elements count
1387 const Register elem = rdi; // each oop copied
1388 const Register elem_klass = rsi; // each elem._klass (sub_klass)
1389 const Register temp = rbx; // lone remaining temp
1391 __ enter(); // required for proper stackwalking of RuntimeStub frame
1393 __ push(rsi);
1394 __ push(rdi);
1395 __ push(rbx);
1397 Address from_arg(rsp, 16+ 4); // from
1398 Address to_arg(rsp, 16+ 8); // to
1399 Address length_arg(rsp, 16+12); // elements count
1400 Address ckoff_arg(rsp, 16+16); // super_check_offset
1401 Address ckval_arg(rsp, 16+20); // super_klass
1403 // Load up:
1404 __ movptr(from, from_arg);
1405 __ movptr(to, to_arg);
1406 __ movl2ptr(length, length_arg);
1408 *entry = __ pc(); // Entry point from generic arraycopy stub.
1409 BLOCK_COMMENT("Entry:");
1411 //---------------------------------------------------------------
1412 // Assembler stub will be used for this call to arraycopy
1413 // if the two arrays are subtypes of Object[] but the
1414 // destination array type is not equal to or a supertype
1415 // of the source type. Each element must be separately
1416 // checked.
1418 // Loop-invariant addresses. They are exclusive end pointers.
1419 Address end_from_addr(from, length, Address::times_ptr, 0);
1420 Address end_to_addr(to, length, Address::times_ptr, 0);
1422 Register end_from = from; // re-use
1423 Register end_to = to; // re-use
1424 Register count = length; // re-use
1426 // Loop-variant addresses. They assume post-incremented count < 0.
1427 Address from_element_addr(end_from, count, Address::times_ptr, 0);
1428 Address to_element_addr(end_to, count, Address::times_ptr, 0);
1429 Address elem_klass_addr(elem, oopDesc::klass_offset_in_bytes());
1431 // Copy from low to high addresses, indexed from the end of each array.
1432 gen_write_ref_array_pre_barrier(to, count);
1433 __ lea(end_from, end_from_addr);
1434 __ lea(end_to, end_to_addr);
1435 assert(length == count, ""); // else fix next line:
1436 __ negptr(count); // negate and test the length
1437 __ jccb(Assembler::notZero, L_load_element);
1439 // Empty array: Nothing to do.
1440 __ xorptr(rax, rax); // return 0 on (trivial) success
1441 __ jmp(L_done);
1443 // ======== begin loop ========
1444 // (Loop is rotated; its entry is L_load_element.)
1445 // Loop control:
1446 // for (count = -count; count != 0; count++)
1447 // Base pointers src, dst are biased by 8*count,to last element.
1448 __ align(16);
1450 __ BIND(L_store_element);
1451 __ movptr(to_element_addr, elem); // store the oop
1452 __ increment(count); // increment the count toward zero
1453 __ jccb(Assembler::zero, L_do_card_marks);
1455 // ======== loop entry is here ========
1456 __ BIND(L_load_element);
1457 __ movptr(elem, from_element_addr); // load the oop
1458 __ testptr(elem, elem);
1459 __ jccb(Assembler::zero, L_store_element);
1461 // (Could do a trick here: Remember last successful non-null
1462 // element stored and make a quick oop equality check on it.)
1464 __ movptr(elem_klass, elem_klass_addr); // query the object klass
1465 generate_type_check(elem_klass, ckoff_arg, ckval_arg, temp,
1466 &L_store_element, NULL);
1467 // (On fall-through, we have failed the element type check.)
1468 // ======== end loop ========
1470 // It was a real error; we must depend on the caller to finish the job.
1471 // Register "count" = -1 * number of *remaining* oops, length_arg = *total* oops.
1472 // Emit GC store barriers for the oops we have copied (length_arg + count),
1473 // and report their number to the caller.
1474 __ addl(count, length_arg); // transfers = (length - remaining)
1475 __ movl2ptr(rax, count); // save the value
1476 __ notptr(rax); // report (-1^K) to caller
1477 __ movptr(to, to_arg); // reload
1478 assert_different_registers(to, count, rax);
1479 gen_write_ref_array_post_barrier(to, count);
1480 __ jmpb(L_done);
1482 // Come here on success only.
1483 __ BIND(L_do_card_marks);
1484 __ movl2ptr(count, length_arg);
1485 __ movptr(to, to_arg); // reload
1486 gen_write_ref_array_post_barrier(to, count);
1487 __ xorptr(rax, rax); // return 0 on success
1489 // Common exit point (success or failure).
1490 __ BIND(L_done);
1491 __ pop(rbx);
1492 __ pop(rdi);
1493 __ pop(rsi);
1494 inc_counter_np(SharedRuntime::_checkcast_array_copy_ctr);
1495 __ leave(); // required for proper stackwalking of RuntimeStub frame
1496 __ ret(0);
1498 return start;
1499 }
1501 //
1502 // Generate 'unsafe' array copy stub
1503 // Though just as safe as the other stubs, it takes an unscaled
1504 // size_t argument instead of an element count.
1505 //
1506 // Input:
1507 // 4(rsp) - source array address
1508 // 8(rsp) - destination array address
1509 // 12(rsp) - byte count, can be zero
1510 //
1511 // Output:
1512 // rax, == 0 - success
1513 // rax, == -1 - need to call System.arraycopy
1514 //
1515 // Examines the alignment of the operands and dispatches
1516 // to a long, int, short, or byte copy loop.
1517 //
1518 address generate_unsafe_copy(const char *name,
1519 address byte_copy_entry,
1520 address short_copy_entry,
1521 address int_copy_entry,
1522 address long_copy_entry) {
1524 Label L_long_aligned, L_int_aligned, L_short_aligned;
1526 __ align(CodeEntryAlignment);
1527 StubCodeMark mark(this, "StubRoutines", name);
1528 address start = __ pc();
1530 const Register from = rax; // source array address
1531 const Register to = rdx; // destination array address
1532 const Register count = rcx; // elements count
1534 __ enter(); // required for proper stackwalking of RuntimeStub frame
1535 __ push(rsi);
1536 __ push(rdi);
1537 Address from_arg(rsp, 12+ 4); // from
1538 Address to_arg(rsp, 12+ 8); // to
1539 Address count_arg(rsp, 12+12); // byte count
1541 // Load up:
1542 __ movptr(from , from_arg);
1543 __ movptr(to , to_arg);
1544 __ movl2ptr(count, count_arg);
1546 // bump this on entry, not on exit:
1547 inc_counter_np(SharedRuntime::_unsafe_array_copy_ctr);
1549 const Register bits = rsi;
1550 __ mov(bits, from);
1551 __ orptr(bits, to);
1552 __ orptr(bits, count);
1554 __ testl(bits, BytesPerLong-1);
1555 __ jccb(Assembler::zero, L_long_aligned);
1557 __ testl(bits, BytesPerInt-1);
1558 __ jccb(Assembler::zero, L_int_aligned);
1560 __ testl(bits, BytesPerShort-1);
1561 __ jump_cc(Assembler::notZero, RuntimeAddress(byte_copy_entry));
1563 __ BIND(L_short_aligned);
1564 __ shrptr(count, LogBytesPerShort); // size => short_count
1565 __ movl(count_arg, count); // update 'count'
1566 __ jump(RuntimeAddress(short_copy_entry));
1568 __ BIND(L_int_aligned);
1569 __ shrptr(count, LogBytesPerInt); // size => int_count
1570 __ movl(count_arg, count); // update 'count'
1571 __ jump(RuntimeAddress(int_copy_entry));
1573 __ BIND(L_long_aligned);
1574 __ shrptr(count, LogBytesPerLong); // size => qword_count
1575 __ movl(count_arg, count); // update 'count'
1576 __ pop(rdi); // Do pops here since jlong_arraycopy stub does not do it.
1577 __ pop(rsi);
1578 __ jump(RuntimeAddress(long_copy_entry));
1580 return start;
1581 }
1584 // Perform range checks on the proposed arraycopy.
1585 // Smashes src_pos and dst_pos. (Uses them up for temps.)
1586 void arraycopy_range_checks(Register src,
1587 Register src_pos,
1588 Register dst,
1589 Register dst_pos,
1590 Address& length,
1591 Label& L_failed) {
1592 BLOCK_COMMENT("arraycopy_range_checks:");
1593 const Register src_end = src_pos; // source array end position
1594 const Register dst_end = dst_pos; // destination array end position
1595 __ addl(src_end, length); // src_pos + length
1596 __ addl(dst_end, length); // dst_pos + length
1598 // if (src_pos + length > arrayOop(src)->length() ) FAIL;
1599 __ cmpl(src_end, Address(src, arrayOopDesc::length_offset_in_bytes()));
1600 __ jcc(Assembler::above, L_failed);
1602 // if (dst_pos + length > arrayOop(dst)->length() ) FAIL;
1603 __ cmpl(dst_end, Address(dst, arrayOopDesc::length_offset_in_bytes()));
1604 __ jcc(Assembler::above, L_failed);
1606 BLOCK_COMMENT("arraycopy_range_checks done");
1607 }
1610 //
1611 // Generate generic array copy stubs
1612 //
1613 // Input:
1614 // 4(rsp) - src oop
1615 // 8(rsp) - src_pos
1616 // 12(rsp) - dst oop
1617 // 16(rsp) - dst_pos
1618 // 20(rsp) - element count
1619 //
1620 // Output:
1621 // rax, == 0 - success
1622 // rax, == -1^K - failure, where K is partial transfer count
1623 //
1624 address generate_generic_copy(const char *name,
1625 address entry_jbyte_arraycopy,
1626 address entry_jshort_arraycopy,
1627 address entry_jint_arraycopy,
1628 address entry_oop_arraycopy,
1629 address entry_jlong_arraycopy,
1630 address entry_checkcast_arraycopy) {
1631 Label L_failed, L_failed_0, L_objArray;
1633 { int modulus = CodeEntryAlignment;
1634 int target = modulus - 5; // 5 = sizeof jmp(L_failed)
1635 int advance = target - (__ offset() % modulus);
1636 if (advance < 0) advance += modulus;
1637 if (advance > 0) __ nop(advance);
1638 }
1639 StubCodeMark mark(this, "StubRoutines", name);
1641 // Short-hop target to L_failed. Makes for denser prologue code.
1642 __ BIND(L_failed_0);
1643 __ jmp(L_failed);
1644 assert(__ offset() % CodeEntryAlignment == 0, "no further alignment needed");
1646 __ align(CodeEntryAlignment);
1647 address start = __ pc();
1649 __ enter(); // required for proper stackwalking of RuntimeStub frame
1650 __ push(rsi);
1651 __ push(rdi);
1653 // bump this on entry, not on exit:
1654 inc_counter_np(SharedRuntime::_generic_array_copy_ctr);
1656 // Input values
1657 Address SRC (rsp, 12+ 4);
1658 Address SRC_POS (rsp, 12+ 8);
1659 Address DST (rsp, 12+12);
1660 Address DST_POS (rsp, 12+16);
1661 Address LENGTH (rsp, 12+20);
1663 //-----------------------------------------------------------------------
1664 // Assembler stub will be used for this call to arraycopy
1665 // if the following conditions are met:
1666 //
1667 // (1) src and dst must not be null.
1668 // (2) src_pos must not be negative.
1669 // (3) dst_pos must not be negative.
1670 // (4) length must not be negative.
1671 // (5) src klass and dst klass should be the same and not NULL.
1672 // (6) src and dst should be arrays.
1673 // (7) src_pos + length must not exceed length of src.
1674 // (8) dst_pos + length must not exceed length of dst.
1675 //
1677 const Register src = rax; // source array oop
1678 const Register src_pos = rsi;
1679 const Register dst = rdx; // destination array oop
1680 const Register dst_pos = rdi;
1681 const Register length = rcx; // transfer count
1683 // if (src == NULL) return -1;
1684 __ movptr(src, SRC); // src oop
1685 __ testptr(src, src);
1686 __ jccb(Assembler::zero, L_failed_0);
1688 // if (src_pos < 0) return -1;
1689 __ movl2ptr(src_pos, SRC_POS); // src_pos
1690 __ testl(src_pos, src_pos);
1691 __ jccb(Assembler::negative, L_failed_0);
1693 // if (dst == NULL) return -1;
1694 __ movptr(dst, DST); // dst oop
1695 __ testptr(dst, dst);
1696 __ jccb(Assembler::zero, L_failed_0);
1698 // if (dst_pos < 0) return -1;
1699 __ movl2ptr(dst_pos, DST_POS); // dst_pos
1700 __ testl(dst_pos, dst_pos);
1701 __ jccb(Assembler::negative, L_failed_0);
1703 // if (length < 0) return -1;
1704 __ movl2ptr(length, LENGTH); // length
1705 __ testl(length, length);
1706 __ jccb(Assembler::negative, L_failed_0);
1708 // if (src->klass() == NULL) return -1;
1709 Address src_klass_addr(src, oopDesc::klass_offset_in_bytes());
1710 Address dst_klass_addr(dst, oopDesc::klass_offset_in_bytes());
1711 const Register rcx_src_klass = rcx; // array klass
1712 __ movptr(rcx_src_klass, Address(src, oopDesc::klass_offset_in_bytes()));
1714 #ifdef ASSERT
1715 // assert(src->klass() != NULL);
1716 BLOCK_COMMENT("assert klasses not null");
1717 { Label L1, L2;
1718 __ testptr(rcx_src_klass, rcx_src_klass);
1719 __ jccb(Assembler::notZero, L2); // it is broken if klass is NULL
1720 __ bind(L1);
1721 __ stop("broken null klass");
1722 __ bind(L2);
1723 __ cmpptr(dst_klass_addr, (int32_t)NULL_WORD);
1724 __ jccb(Assembler::equal, L1); // this would be broken also
1725 BLOCK_COMMENT("assert done");
1726 }
1727 #endif //ASSERT
1729 // Load layout helper (32-bits)
1730 //
1731 // |array_tag| | header_size | element_type | |log2_element_size|
1732 // 32 30 24 16 8 2 0
1733 //
1734 // array_tag: typeArray = 0x3, objArray = 0x2, non-array = 0x0
1735 //
1737 int lh_offset = klassOopDesc::header_size() * HeapWordSize +
1738 Klass::layout_helper_offset_in_bytes();
1739 Address src_klass_lh_addr(rcx_src_klass, lh_offset);
1741 // Handle objArrays completely differently...
1742 jint objArray_lh = Klass::array_layout_helper(T_OBJECT);
1743 __ cmpl(src_klass_lh_addr, objArray_lh);
1744 __ jcc(Assembler::equal, L_objArray);
1746 // if (src->klass() != dst->klass()) return -1;
1747 __ cmpptr(rcx_src_klass, dst_klass_addr);
1748 __ jccb(Assembler::notEqual, L_failed_0);
1750 const Register rcx_lh = rcx; // layout helper
1751 assert(rcx_lh == rcx_src_klass, "known alias");
1752 __ movl(rcx_lh, src_klass_lh_addr);
1754 // if (!src->is_Array()) return -1;
1755 __ cmpl(rcx_lh, Klass::_lh_neutral_value);
1756 __ jcc(Assembler::greaterEqual, L_failed_0); // signed cmp
1758 // At this point, it is known to be a typeArray (array_tag 0x3).
1759 #ifdef ASSERT
1760 { Label L;
1761 __ cmpl(rcx_lh, (Klass::_lh_array_tag_type_value << Klass::_lh_array_tag_shift));
1762 __ jcc(Assembler::greaterEqual, L); // signed cmp
1763 __ stop("must be a primitive array");
1764 __ bind(L);
1765 }
1766 #endif
1768 assert_different_registers(src, src_pos, dst, dst_pos, rcx_lh);
1769 arraycopy_range_checks(src, src_pos, dst, dst_pos, LENGTH, L_failed);
1771 // typeArrayKlass
1772 //
1773 // src_addr = (src + array_header_in_bytes()) + (src_pos << log2elemsize);
1774 // dst_addr = (dst + array_header_in_bytes()) + (dst_pos << log2elemsize);
1775 //
1776 const Register rsi_offset = rsi; // array offset
1777 const Register src_array = src; // src array offset
1778 const Register dst_array = dst; // dst array offset
1779 const Register rdi_elsize = rdi; // log2 element size
1781 __ mov(rsi_offset, rcx_lh);
1782 __ shrptr(rsi_offset, Klass::_lh_header_size_shift);
1783 __ andptr(rsi_offset, Klass::_lh_header_size_mask); // array_offset
1784 __ addptr(src_array, rsi_offset); // src array offset
1785 __ addptr(dst_array, rsi_offset); // dst array offset
1786 __ andptr(rcx_lh, Klass::_lh_log2_element_size_mask); // log2 elsize
1788 // next registers should be set before the jump to corresponding stub
1789 const Register from = src; // source array address
1790 const Register to = dst; // destination array address
1791 const Register count = rcx; // elements count
1792 // some of them should be duplicated on stack
1793 #define FROM Address(rsp, 12+ 4)
1794 #define TO Address(rsp, 12+ 8) // Not used now
1795 #define COUNT Address(rsp, 12+12) // Only for oop arraycopy
1797 BLOCK_COMMENT("scale indexes to element size");
1798 __ movl2ptr(rsi, SRC_POS); // src_pos
1799 __ shlptr(rsi); // src_pos << rcx (log2 elsize)
1800 assert(src_array == from, "");
1801 __ addptr(from, rsi); // from = src_array + SRC_POS << log2 elsize
1802 __ movl2ptr(rdi, DST_POS); // dst_pos
1803 __ shlptr(rdi); // dst_pos << rcx (log2 elsize)
1804 assert(dst_array == to, "");
1805 __ addptr(to, rdi); // to = dst_array + DST_POS << log2 elsize
1806 __ movptr(FROM, from); // src_addr
1807 __ mov(rdi_elsize, rcx_lh); // log2 elsize
1808 __ movl2ptr(count, LENGTH); // elements count
1810 BLOCK_COMMENT("choose copy loop based on element size");
1811 __ cmpl(rdi_elsize, 0);
1813 __ jump_cc(Assembler::equal, RuntimeAddress(entry_jbyte_arraycopy));
1814 __ cmpl(rdi_elsize, LogBytesPerShort);
1815 __ jump_cc(Assembler::equal, RuntimeAddress(entry_jshort_arraycopy));
1816 __ cmpl(rdi_elsize, LogBytesPerInt);
1817 __ jump_cc(Assembler::equal, RuntimeAddress(entry_jint_arraycopy));
1818 #ifdef ASSERT
1819 __ cmpl(rdi_elsize, LogBytesPerLong);
1820 __ jccb(Assembler::notEqual, L_failed);
1821 #endif
1822 __ pop(rdi); // Do pops here since jlong_arraycopy stub does not do it.
1823 __ pop(rsi);
1824 __ jump(RuntimeAddress(entry_jlong_arraycopy));
1826 __ BIND(L_failed);
1827 __ xorptr(rax, rax);
1828 __ notptr(rax); // return -1
1829 __ pop(rdi);
1830 __ pop(rsi);
1831 __ leave(); // required for proper stackwalking of RuntimeStub frame
1832 __ ret(0);
1834 // objArrayKlass
1835 __ BIND(L_objArray);
1836 // live at this point: rcx_src_klass, src[_pos], dst[_pos]
1838 Label L_plain_copy, L_checkcast_copy;
1839 // test array classes for subtyping
1840 __ cmpptr(rcx_src_klass, dst_klass_addr); // usual case is exact equality
1841 __ jccb(Assembler::notEqual, L_checkcast_copy);
1843 // Identically typed arrays can be copied without element-wise checks.
1844 assert_different_registers(src, src_pos, dst, dst_pos, rcx_src_klass);
1845 arraycopy_range_checks(src, src_pos, dst, dst_pos, LENGTH, L_failed);
1847 __ BIND(L_plain_copy);
1848 __ movl2ptr(count, LENGTH); // elements count
1849 __ movl2ptr(src_pos, SRC_POS); // reload src_pos
1850 __ lea(from, Address(src, src_pos, Address::times_ptr,
1851 arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // src_addr
1852 __ movl2ptr(dst_pos, DST_POS); // reload dst_pos
1853 __ lea(to, Address(dst, dst_pos, Address::times_ptr,
1854 arrayOopDesc::base_offset_in_bytes(T_OBJECT))); // dst_addr
1855 __ movptr(FROM, from); // src_addr
1856 __ movptr(TO, to); // dst_addr
1857 __ movl(COUNT, count); // count
1858 __ jump(RuntimeAddress(entry_oop_arraycopy));
1860 __ BIND(L_checkcast_copy);
1861 // live at this point: rcx_src_klass, dst[_pos], src[_pos]
1862 {
1863 // Handy offsets:
1864 int ek_offset = (klassOopDesc::header_size() * HeapWordSize +
1865 objArrayKlass::element_klass_offset_in_bytes());
1866 int sco_offset = (klassOopDesc::header_size() * HeapWordSize +
1867 Klass::super_check_offset_offset_in_bytes());
1869 Register rsi_dst_klass = rsi;
1870 Register rdi_temp = rdi;
1871 assert(rsi_dst_klass == src_pos, "expected alias w/ src_pos");
1872 assert(rdi_temp == dst_pos, "expected alias w/ dst_pos");
1873 Address dst_klass_lh_addr(rsi_dst_klass, lh_offset);
1875 // Before looking at dst.length, make sure dst is also an objArray.
1876 __ movptr(rsi_dst_klass, dst_klass_addr);
1877 __ cmpl(dst_klass_lh_addr, objArray_lh);
1878 __ jccb(Assembler::notEqual, L_failed);
1880 // It is safe to examine both src.length and dst.length.
1881 __ movl2ptr(src_pos, SRC_POS); // reload rsi
1882 arraycopy_range_checks(src, src_pos, dst, dst_pos, LENGTH, L_failed);
1883 // (Now src_pos and dst_pos are killed, but not src and dst.)
1885 // We'll need this temp (don't forget to pop it after the type check).
1886 __ push(rbx);
1887 Register rbx_src_klass = rbx;
1889 __ mov(rbx_src_klass, rcx_src_klass); // spill away from rcx
1890 __ movptr(rsi_dst_klass, dst_klass_addr);
1891 Address super_check_offset_addr(rsi_dst_klass, sco_offset);
1892 Label L_fail_array_check;
1893 generate_type_check(rbx_src_klass,
1894 super_check_offset_addr, dst_klass_addr,
1895 rdi_temp, NULL, &L_fail_array_check);
1896 // (On fall-through, we have passed the array type check.)
1897 __ pop(rbx);
1898 __ jmp(L_plain_copy);
1900 __ BIND(L_fail_array_check);
1901 // Reshuffle arguments so we can call checkcast_arraycopy:
1903 // match initial saves for checkcast_arraycopy
1904 // push(rsi); // already done; see above
1905 // push(rdi); // already done; see above
1906 // push(rbx); // already done; see above
1908 // Marshal outgoing arguments now, freeing registers.
1909 Address from_arg(rsp, 16+ 4); // from
1910 Address to_arg(rsp, 16+ 8); // to
1911 Address length_arg(rsp, 16+12); // elements count
1912 Address ckoff_arg(rsp, 16+16); // super_check_offset
1913 Address ckval_arg(rsp, 16+20); // super_klass
1915 Address SRC_POS_arg(rsp, 16+ 8);
1916 Address DST_POS_arg(rsp, 16+16);
1917 Address LENGTH_arg(rsp, 16+20);
1918 // push rbx, changed the incoming offsets (why not just use rbp,??)
1919 // assert(SRC_POS_arg.disp() == SRC_POS.disp() + 4, "");
1921 __ movptr(rbx, Address(rsi_dst_klass, ek_offset));
1922 __ movl2ptr(length, LENGTH_arg); // reload elements count
1923 __ movl2ptr(src_pos, SRC_POS_arg); // reload src_pos
1924 __ movl2ptr(dst_pos, DST_POS_arg); // reload dst_pos
1926 __ movptr(ckval_arg, rbx); // destination element type
1927 __ movl(rbx, Address(rbx, sco_offset));
1928 __ movl(ckoff_arg, rbx); // corresponding class check offset
1930 __ movl(length_arg, length); // outgoing length argument
1932 __ lea(from, Address(src, src_pos, Address::times_ptr,
1933 arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
1934 __ movptr(from_arg, from);
1936 __ lea(to, Address(dst, dst_pos, Address::times_ptr,
1937 arrayOopDesc::base_offset_in_bytes(T_OBJECT)));
1938 __ movptr(to_arg, to);
1939 __ jump(RuntimeAddress(entry_checkcast_arraycopy));
1940 }
1942 return start;
1943 }
1945 void generate_arraycopy_stubs() {
1946 address entry;
1947 address entry_jbyte_arraycopy;
1948 address entry_jshort_arraycopy;
1949 address entry_jint_arraycopy;
1950 address entry_oop_arraycopy;
1951 address entry_jlong_arraycopy;
1952 address entry_checkcast_arraycopy;
1954 StubRoutines::_arrayof_jbyte_disjoint_arraycopy =
1955 generate_disjoint_copy(T_BYTE, true, Address::times_1, &entry,
1956 "arrayof_jbyte_disjoint_arraycopy");
1957 StubRoutines::_arrayof_jbyte_arraycopy =
1958 generate_conjoint_copy(T_BYTE, true, Address::times_1, entry,
1959 NULL, "arrayof_jbyte_arraycopy");
1960 StubRoutines::_jbyte_disjoint_arraycopy =
1961 generate_disjoint_copy(T_BYTE, false, Address::times_1, &entry,
1962 "jbyte_disjoint_arraycopy");
1963 StubRoutines::_jbyte_arraycopy =
1964 generate_conjoint_copy(T_BYTE, false, Address::times_1, entry,
1965 &entry_jbyte_arraycopy, "jbyte_arraycopy");
1967 StubRoutines::_arrayof_jshort_disjoint_arraycopy =
1968 generate_disjoint_copy(T_SHORT, true, Address::times_2, &entry,
1969 "arrayof_jshort_disjoint_arraycopy");
1970 StubRoutines::_arrayof_jshort_arraycopy =
1971 generate_conjoint_copy(T_SHORT, true, Address::times_2, entry,
1972 NULL, "arrayof_jshort_arraycopy");
1973 StubRoutines::_jshort_disjoint_arraycopy =
1974 generate_disjoint_copy(T_SHORT, false, Address::times_2, &entry,
1975 "jshort_disjoint_arraycopy");
1976 StubRoutines::_jshort_arraycopy =
1977 generate_conjoint_copy(T_SHORT, false, Address::times_2, entry,
1978 &entry_jshort_arraycopy, "jshort_arraycopy");
1980 // Next arrays are always aligned on 4 bytes at least.
1981 StubRoutines::_jint_disjoint_arraycopy =
1982 generate_disjoint_copy(T_INT, true, Address::times_4, &entry,
1983 "jint_disjoint_arraycopy");
1984 StubRoutines::_jint_arraycopy =
1985 generate_conjoint_copy(T_INT, true, Address::times_4, entry,
1986 &entry_jint_arraycopy, "jint_arraycopy");
1988 StubRoutines::_oop_disjoint_arraycopy =
1989 generate_disjoint_copy(T_OBJECT, true, Address::times_ptr, &entry,
1990 "oop_disjoint_arraycopy");
1991 StubRoutines::_oop_arraycopy =
1992 generate_conjoint_copy(T_OBJECT, true, Address::times_ptr, entry,
1993 &entry_oop_arraycopy, "oop_arraycopy");
1995 StubRoutines::_jlong_disjoint_arraycopy =
1996 generate_disjoint_long_copy(&entry, "jlong_disjoint_arraycopy");
1997 StubRoutines::_jlong_arraycopy =
1998 generate_conjoint_long_copy(entry, &entry_jlong_arraycopy,
1999 "jlong_arraycopy");
2001 StubRoutines::_arrayof_jint_disjoint_arraycopy =
2002 StubRoutines::_jint_disjoint_arraycopy;
2003 StubRoutines::_arrayof_oop_disjoint_arraycopy =
2004 StubRoutines::_oop_disjoint_arraycopy;
2005 StubRoutines::_arrayof_jlong_disjoint_arraycopy =
2006 StubRoutines::_jlong_disjoint_arraycopy;
2008 StubRoutines::_arrayof_jint_arraycopy = StubRoutines::_jint_arraycopy;
2009 StubRoutines::_arrayof_oop_arraycopy = StubRoutines::_oop_arraycopy;
2010 StubRoutines::_arrayof_jlong_arraycopy = StubRoutines::_jlong_arraycopy;
2012 StubRoutines::_checkcast_arraycopy =
2013 generate_checkcast_copy("checkcast_arraycopy",
2014 &entry_checkcast_arraycopy);
2016 StubRoutines::_unsafe_arraycopy =
2017 generate_unsafe_copy("unsafe_arraycopy",
2018 entry_jbyte_arraycopy,
2019 entry_jshort_arraycopy,
2020 entry_jint_arraycopy,
2021 entry_jlong_arraycopy);
2023 StubRoutines::_generic_arraycopy =
2024 generate_generic_copy("generic_arraycopy",
2025 entry_jbyte_arraycopy,
2026 entry_jshort_arraycopy,
2027 entry_jint_arraycopy,
2028 entry_oop_arraycopy,
2029 entry_jlong_arraycopy,
2030 entry_checkcast_arraycopy);
2031 }
2033 void generate_math_stubs() {
2034 {
2035 StubCodeMark mark(this, "StubRoutines", "log");
2036 StubRoutines::_intrinsic_log = (double (*)(double)) __ pc();
2038 __ fld_d(Address(rsp, 4));
2039 __ flog();
2040 __ ret(0);
2041 }
2042 {
2043 StubCodeMark mark(this, "StubRoutines", "log10");
2044 StubRoutines::_intrinsic_log10 = (double (*)(double)) __ pc();
2046 __ fld_d(Address(rsp, 4));
2047 __ flog10();
2048 __ ret(0);
2049 }
2050 {
2051 StubCodeMark mark(this, "StubRoutines", "sin");
2052 StubRoutines::_intrinsic_sin = (double (*)(double)) __ pc();
2054 __ fld_d(Address(rsp, 4));
2055 __ trigfunc('s');
2056 __ ret(0);
2057 }
2058 {
2059 StubCodeMark mark(this, "StubRoutines", "cos");
2060 StubRoutines::_intrinsic_cos = (double (*)(double)) __ pc();
2062 __ fld_d(Address(rsp, 4));
2063 __ trigfunc('c');
2064 __ ret(0);
2065 }
2066 {
2067 StubCodeMark mark(this, "StubRoutines", "tan");
2068 StubRoutines::_intrinsic_tan = (double (*)(double)) __ pc();
2070 __ fld_d(Address(rsp, 4));
2071 __ trigfunc('t');
2072 __ ret(0);
2073 }
2075 // The intrinsic version of these seem to return the same value as
2076 // the strict version.
2077 StubRoutines::_intrinsic_exp = SharedRuntime::dexp;
2078 StubRoutines::_intrinsic_pow = SharedRuntime::dpow;
2079 }
2081 public:
2082 // Information about frame layout at time of blocking runtime call.
2083 // Note that we only have to preserve callee-saved registers since
2084 // the compilers are responsible for supplying a continuation point
2085 // if they expect all registers to be preserved.
2086 enum layout {
2087 thread_off, // last_java_sp
2088 rbp_off, // callee saved register
2089 ret_pc,
2090 framesize
2091 };
2093 private:
2095 #undef __
2096 #define __ masm->
2098 //------------------------------------------------------------------------------------------------------------------------
2099 // Continuation point for throwing of implicit exceptions that are not handled in
2100 // the current activation. Fabricates an exception oop and initiates normal
2101 // exception dispatching in this frame.
2102 //
2103 // Previously the compiler (c2) allowed for callee save registers on Java calls.
2104 // This is no longer true after adapter frames were removed but could possibly
2105 // be brought back in the future if the interpreter code was reworked and it
2106 // was deemed worthwhile. The comment below was left to describe what must
2107 // happen here if callee saves were resurrected. As it stands now this stub
2108 // could actually be a vanilla BufferBlob and have now oopMap at all.
2109 // Since it doesn't make much difference we've chosen to leave it the
2110 // way it was in the callee save days and keep the comment.
2112 // If we need to preserve callee-saved values we need a callee-saved oop map and
2113 // therefore have to make these stubs into RuntimeStubs rather than BufferBlobs.
2114 // If the compiler needs all registers to be preserved between the fault
2115 // point and the exception handler then it must assume responsibility for that in
2116 // AbstractCompiler::continuation_for_implicit_null_exception or
2117 // continuation_for_implicit_division_by_zero_exception. All other implicit
2118 // exceptions (e.g., NullPointerException or AbstractMethodError on entry) are
2119 // either at call sites or otherwise assume that stack unwinding will be initiated,
2120 // so caller saved registers were assumed volatile in the compiler.
2121 address generate_throw_exception(const char* name, address runtime_entry,
2122 bool restore_saved_exception_pc) {
2124 int insts_size = 256;
2125 int locs_size = 32;
2127 CodeBuffer code(name, insts_size, locs_size);
2128 OopMapSet* oop_maps = new OopMapSet();
2129 MacroAssembler* masm = new MacroAssembler(&code);
2131 address start = __ pc();
2133 // This is an inlined and slightly modified version of call_VM
2134 // which has the ability to fetch the return PC out of
2135 // thread-local storage and also sets up last_Java_sp slightly
2136 // differently than the real call_VM
2137 Register java_thread = rbx;
2138 __ get_thread(java_thread);
2139 if (restore_saved_exception_pc) {
2140 __ movptr(rax, Address(java_thread, in_bytes(JavaThread::saved_exception_pc_offset())));
2141 __ push(rax);
2142 }
2144 __ enter(); // required for proper stackwalking of RuntimeStub frame
2146 // pc and rbp, already pushed
2147 __ subptr(rsp, (framesize-2) * wordSize); // prolog
2149 // Frame is now completed as far as size and linkage.
2151 int frame_complete = __ pc() - start;
2153 // push java thread (becomes first argument of C function)
2154 __ movptr(Address(rsp, thread_off * wordSize), java_thread);
2156 // Set up last_Java_sp and last_Java_fp
2157 __ set_last_Java_frame(java_thread, rsp, rbp, NULL);
2159 // Call runtime
2160 BLOCK_COMMENT("call runtime_entry");
2161 __ call(RuntimeAddress(runtime_entry));
2162 // Generate oop map
2163 OopMap* map = new OopMap(framesize, 0);
2164 oop_maps->add_gc_map(__ pc() - start, map);
2166 // restore the thread (cannot use the pushed argument since arguments
2167 // may be overwritten by C code generated by an optimizing compiler);
2168 // however can use the register value directly if it is callee saved.
2169 __ get_thread(java_thread);
2171 __ reset_last_Java_frame(java_thread, true, false);
2173 __ leave(); // required for proper stackwalking of RuntimeStub frame
2175 // check for pending exceptions
2176 #ifdef ASSERT
2177 Label L;
2178 __ cmpptr(Address(java_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
2179 __ jcc(Assembler::notEqual, L);
2180 __ should_not_reach_here();
2181 __ bind(L);
2182 #endif /* ASSERT */
2183 __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
2186 RuntimeStub* stub = RuntimeStub::new_runtime_stub(name, &code, frame_complete, framesize, oop_maps, false);
2187 return stub->entry_point();
2188 }
2191 void create_control_words() {
2192 // Round to nearest, 53-bit mode, exceptions masked
2193 StubRoutines::_fpu_cntrl_wrd_std = 0x027F;
2194 // Round to zero, 53-bit mode, exception mased
2195 StubRoutines::_fpu_cntrl_wrd_trunc = 0x0D7F;
2196 // Round to nearest, 24-bit mode, exceptions masked
2197 StubRoutines::_fpu_cntrl_wrd_24 = 0x007F;
2198 // Round to nearest, 64-bit mode, exceptions masked
2199 StubRoutines::_fpu_cntrl_wrd_64 = 0x037F;
2200 // Round to nearest, 64-bit mode, exceptions masked
2201 StubRoutines::_mxcsr_std = 0x1F80;
2202 // Note: the following two constants are 80-bit values
2203 // layout is critical for correct loading by FPU.
2204 // Bias for strict fp multiply/divide
2205 StubRoutines::_fpu_subnormal_bias1[0]= 0x00000000; // 2^(-15360) == 0x03ff 8000 0000 0000 0000
2206 StubRoutines::_fpu_subnormal_bias1[1]= 0x80000000;
2207 StubRoutines::_fpu_subnormal_bias1[2]= 0x03ff;
2208 // Un-Bias for strict fp multiply/divide
2209 StubRoutines::_fpu_subnormal_bias2[0]= 0x00000000; // 2^(+15360) == 0x7bff 8000 0000 0000 0000
2210 StubRoutines::_fpu_subnormal_bias2[1]= 0x80000000;
2211 StubRoutines::_fpu_subnormal_bias2[2]= 0x7bff;
2212 }
2214 //---------------------------------------------------------------------------
2215 // Initialization
2217 void generate_initial() {
2218 // Generates all stubs and initializes the entry points
2220 //------------------------------------------------------------------------------------------------------------------------
2221 // entry points that exist in all platforms
2222 // Note: This is code that could be shared among different platforms - however the benefit seems to be smaller than
2223 // the disadvantage of having a much more complicated generator structure. See also comment in stubRoutines.hpp.
2224 StubRoutines::_forward_exception_entry = generate_forward_exception();
2226 StubRoutines::_call_stub_entry =
2227 generate_call_stub(StubRoutines::_call_stub_return_address);
2228 // is referenced by megamorphic call
2229 StubRoutines::_catch_exception_entry = generate_catch_exception();
2231 // These are currently used by Solaris/Intel
2232 StubRoutines::_atomic_xchg_entry = generate_atomic_xchg();
2234 StubRoutines::_handler_for_unsafe_access_entry =
2235 generate_handler_for_unsafe_access();
2237 // platform dependent
2238 create_control_words();
2240 StubRoutines::x86::_verify_mxcsr_entry = generate_verify_mxcsr();
2241 StubRoutines::x86::_verify_fpu_cntrl_wrd_entry = generate_verify_fpu_cntrl_wrd();
2242 StubRoutines::_d2i_wrapper = generate_d2i_wrapper(T_INT,
2243 CAST_FROM_FN_PTR(address, SharedRuntime::d2i));
2244 StubRoutines::_d2l_wrapper = generate_d2i_wrapper(T_LONG,
2245 CAST_FROM_FN_PTR(address, SharedRuntime::d2l));
2246 }
2249 void generate_all() {
2250 // Generates all stubs and initializes the entry points
2252 // These entry points require SharedInfo::stack0 to be set up in non-core builds
2253 // and need to be relocatable, so they each fabricate a RuntimeStub internally.
2254 StubRoutines::_throw_AbstractMethodError_entry = generate_throw_exception("AbstractMethodError throw_exception", CAST_FROM_FN_PTR(address, SharedRuntime::throw_AbstractMethodError), false);
2255 StubRoutines::_throw_IncompatibleClassChangeError_entry= generate_throw_exception("IncompatibleClassChangeError throw_exception", CAST_FROM_FN_PTR(address, SharedRuntime::throw_IncompatibleClassChangeError), false);
2256 StubRoutines::_throw_ArithmeticException_entry = generate_throw_exception("ArithmeticException throw_exception", CAST_FROM_FN_PTR(address, SharedRuntime::throw_ArithmeticException), true);
2257 StubRoutines::_throw_NullPointerException_entry = generate_throw_exception("NullPointerException throw_exception", CAST_FROM_FN_PTR(address, SharedRuntime::throw_NullPointerException), true);
2258 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);
2259 StubRoutines::_throw_StackOverflowError_entry = generate_throw_exception("StackOverflowError throw_exception", CAST_FROM_FN_PTR(address, SharedRuntime::throw_StackOverflowError), false);
2261 //------------------------------------------------------------------------------------------------------------------------
2262 // entry points that are platform specific
2264 // support for verify_oop (must happen after universe_init)
2265 StubRoutines::_verify_oop_subroutine_entry = generate_verify_oop();
2267 // arraycopy stubs used by compilers
2268 generate_arraycopy_stubs();
2270 // generic method handle stubs
2271 if (EnableMethodHandles && SystemDictionary::MethodHandle_klass() != NULL) {
2272 for (MethodHandles::EntryKind ek = MethodHandles::_EK_FIRST;
2273 ek < MethodHandles::_EK_LIMIT;
2274 ek = MethodHandles::EntryKind(1 + (int)ek)) {
2275 StubCodeMark mark(this, "MethodHandle", MethodHandles::entry_name(ek));
2276 MethodHandles::generate_method_handle_stub(_masm, ek);
2277 }
2278 }
2280 generate_math_stubs();
2281 }
2284 public:
2285 StubGenerator(CodeBuffer* code, bool all) : StubCodeGenerator(code) {
2286 if (all) {
2287 generate_all();
2288 } else {
2289 generate_initial();
2290 }
2291 }
2292 }; // end class declaration
2295 void StubGenerator_generate(CodeBuffer* code, bool all) {
2296 StubGenerator g(code, all);
2297 }