11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #ifndef _WINDOWS
27 #include "alloca.h"
28 #endif
29 #include "asm/macroAssembler.hpp"
30 #include "asm/macroAssembler.inline.hpp"
31 #include "code/compiledIC.hpp"
32 #include "code/debugInfoRec.hpp"
33 #include "code/icBuffer.hpp"
34 #include "code/nativeInst.hpp"
35 #include "code/vtableStubs.hpp"
36 #include "compiler/oopMap.hpp"
37 #include "gc/shared/collectedHeap.hpp"
38 #include "gc/shared/gcLocker.hpp"
39 #include "gc/shared/barrierSet.hpp"
40 #include "gc/shared/barrierSetAssembler.hpp"
41 #include "interpreter/interpreter.hpp"
42 #include "logging/log.hpp"
43 #include "memory/resourceArea.hpp"
44 #include "memory/universe.hpp"
45 #include "oops/compiledICHolder.hpp"
46 #include "oops/klass.inline.hpp"
47 #include "oops/method.inline.hpp"
48 #include "prims/methodHandles.hpp"
49 #include "runtime/continuation.hpp"
50 #include "runtime/continuationEntry.inline.hpp"
507 case T_SHORT:
508 case T_INT:
509 if (int_args < Argument::n_int_register_parameters_j) {
510 regs[i].set1(INT_ArgReg[int_args++]->as_VMReg());
511 } else {
512 regs[i].set1(VMRegImpl::stack2reg(stk_args));
513 stk_args += 2;
514 }
515 break;
516 case T_VOID:
517 // halves of T_LONG or T_DOUBLE
518 assert(i != 0 && (sig_bt[i - 1] == T_LONG || sig_bt[i - 1] == T_DOUBLE), "expecting half");
519 regs[i].set_bad();
520 break;
521 case T_LONG:
522 assert((i + 1) < total_args_passed && sig_bt[i + 1] == T_VOID, "expecting half");
523 // fall through
524 case T_OBJECT:
525 case T_ARRAY:
526 case T_ADDRESS:
527 if (int_args < Argument::n_int_register_parameters_j) {
528 regs[i].set2(INT_ArgReg[int_args++]->as_VMReg());
529 } else {
530 regs[i].set2(VMRegImpl::stack2reg(stk_args));
531 stk_args += 2;
532 }
533 break;
534 case T_FLOAT:
535 if (fp_args < Argument::n_float_register_parameters_j) {
536 regs[i].set1(FP_ArgReg[fp_args++]->as_VMReg());
537 } else {
538 regs[i].set1(VMRegImpl::stack2reg(stk_args));
539 stk_args += 2;
540 }
541 break;
542 case T_DOUBLE:
543 assert((i + 1) < total_args_passed && sig_bt[i + 1] == T_VOID, "expecting half");
544 if (fp_args < Argument::n_float_register_parameters_j) {
545 regs[i].set2(FP_ArgReg[fp_args++]->as_VMReg());
546 } else {
547 regs[i].set2(VMRegImpl::stack2reg(stk_args));
548 stk_args += 2;
549 }
550 break;
551 default:
552 ShouldNotReachHere();
553 break;
554 }
555 }
556
557 return align_up(stk_args, 2);
558 }
559
560 // Patch the callers callsite with entry to compiled code if it exists.
561 static void patch_callers_callsite(MacroAssembler *masm) {
562 Label L;
563 __ cmpptr(Address(rbx, in_bytes(Method::code_offset())), NULL_WORD);
564 __ jcc(Assembler::equal, L);
565
566 // Save the current stack pointer
567 __ mov(r13, rsp);
568 // Schedule the branch target address early.
569 // Call into the VM to patch the caller, then jump to compiled callee
570 // rax isn't live so capture return address while we easily can
571 __ movptr(rax, Address(rsp, 0));
572
573 // align stack so push_CPU_state doesn't fault
574 __ andptr(rsp, -(StackAlignmentInBytes));
575 __ push_CPU_state();
576 __ vzeroupper();
577 // VM needs caller's callsite
578 // VM needs target method
579 // This needs to be a long call since we will relocate this adapter to
582 // Allocate argument register save area
583 if (frame::arg_reg_save_area_bytes != 0) {
584 __ subptr(rsp, frame::arg_reg_save_area_bytes);
585 }
586 __ mov(c_rarg0, rbx);
587 __ mov(c_rarg1, rax);
588 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::fixup_callers_callsite)));
589
590 // De-allocate argument register save area
591 if (frame::arg_reg_save_area_bytes != 0) {
592 __ addptr(rsp, frame::arg_reg_save_area_bytes);
593 }
594
595 __ vzeroupper();
596 __ pop_CPU_state();
597 // restore sp
598 __ mov(rsp, r13);
599 __ bind(L);
600 }
601
602
603 static void gen_c2i_adapter(MacroAssembler *masm,
604 int total_args_passed,
605 int comp_args_on_stack,
606 const BasicType *sig_bt,
607 const VMRegPair *regs,
608 Label& skip_fixup) {
609 // Before we get into the guts of the C2I adapter, see if we should be here
610 // at all. We've come from compiled code and are attempting to jump to the
611 // interpreter, which means the caller made a static call to get here
612 // (vcalls always get a compiled target if there is one). Check for a
613 // compiled target. If there is one, we need to patch the caller's call.
614 patch_callers_callsite(masm);
615
616 __ bind(skip_fixup);
617
618 // Since all args are passed on the stack, total_args_passed *
619 // Interpreter::stackElementSize is the space we need.
620
621 assert(total_args_passed >= 0, "total_args_passed is %d", total_args_passed);
622
623 int extraspace = (total_args_passed * Interpreter::stackElementSize);
624
625 // stack is aligned, keep it that way
626 // This is not currently needed or enforced by the interpreter, but
627 // we might as well conform to the ABI.
628 extraspace = align_up(extraspace, 2*wordSize);
629
630 // set senderSP value
631 __ lea(r13, Address(rsp, wordSize));
632
633 #ifdef ASSERT
634 __ check_stack_alignment(r13, "sender stack not aligned");
635 #endif
636 if (extraspace > 0) {
637 // Pop the return address
638 __ pop(rax);
639
640 __ subptr(rsp, extraspace);
641
642 // Push the return address
643 __ push(rax);
644
645 // Account for the return address location since we store it first rather
646 // than hold it in a register across all the shuffling
647 extraspace += wordSize;
648 }
649
650 #ifdef ASSERT
651 __ check_stack_alignment(rsp, "callee stack not aligned", wordSize, rax);
652 #endif
653
654 // Now write the args into the outgoing interpreter space
655 for (int i = 0; i < total_args_passed; i++) {
656 if (sig_bt[i] == T_VOID) {
657 assert(i > 0 && (sig_bt[i-1] == T_LONG || sig_bt[i-1] == T_DOUBLE), "missing half");
658 continue;
659 }
660
661 // offset to start parameters
662 int st_off = (total_args_passed - i) * Interpreter::stackElementSize;
663 int next_off = st_off - Interpreter::stackElementSize;
664
665 // Say 4 args:
666 // i st_off
667 // 0 32 T_LONG
668 // 1 24 T_VOID
669 // 2 16 T_OBJECT
670 // 3 8 T_BOOL
671 // - 0 return address
672 //
673 // However to make thing extra confusing. Because we can fit a long/double in
674 // a single slot on a 64 bt vm and it would be silly to break them up, the interpreter
675 // leaves one slot empty and only stores to a single slot. In this case the
676 // slot that is occupied is the T_VOID slot. See I said it was confusing.
677
678 VMReg r_1 = regs[i].first();
679 VMReg r_2 = regs[i].second();
680 if (!r_1->is_valid()) {
681 assert(!r_2->is_valid(), "");
682 continue;
683 }
684 if (r_1->is_stack()) {
685 // memory to memory use rax
686 int ld_off = r_1->reg2stack() * VMRegImpl::stack_slot_size + extraspace;
687 if (!r_2->is_valid()) {
688 // sign extend??
689 __ movl(rax, Address(rsp, ld_off));
690 __ movptr(Address(rsp, st_off), rax);
691
692 } else {
693
694 __ movq(rax, Address(rsp, ld_off));
695
696 // Two VMREgs|OptoRegs can be T_OBJECT, T_ADDRESS, T_DOUBLE, T_LONG
697 // T_DOUBLE and T_LONG use two slots in the interpreter
698 if ( sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) {
699 // ld_off == LSW, ld_off+wordSize == MSW
700 // st_off == MSW, next_off == LSW
701 __ movq(Address(rsp, next_off), rax);
702 #ifdef ASSERT
703 // Overwrite the unused slot with known junk
704 __ mov64(rax, CONST64(0xdeadffffdeadaaaa));
705 __ movptr(Address(rsp, st_off), rax);
706 #endif /* ASSERT */
707 } else {
708 __ movq(Address(rsp, st_off), rax);
709 }
710 }
711 } else if (r_1->is_Register()) {
712 Register r = r_1->as_Register();
713 if (!r_2->is_valid()) {
714 // must be only an int (or less ) so move only 32bits to slot
715 // why not sign extend??
716 __ movl(Address(rsp, st_off), r);
717 } else {
718 // Two VMREgs|OptoRegs can be T_OBJECT, T_ADDRESS, T_DOUBLE, T_LONG
719 // T_DOUBLE and T_LONG use two slots in the interpreter
720 if ( sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) {
721 // long/double in gpr
722 #ifdef ASSERT
723 // Overwrite the unused slot with known junk
724 __ mov64(rax, CONST64(0xdeadffffdeadaaab));
725 __ movptr(Address(rsp, st_off), rax);
726 #endif /* ASSERT */
727 __ movq(Address(rsp, next_off), r);
728 } else {
729 __ movptr(Address(rsp, st_off), r);
730 }
731 }
732 } else {
733 assert(r_1->is_XMMRegister(), "");
734 if (!r_2->is_valid()) {
735 // only a float use just part of the slot
736 __ movflt(Address(rsp, st_off), r_1->as_XMMRegister());
737 } else {
738 #ifdef ASSERT
739 // Overwrite the unused slot with known junk
740 __ mov64(rax, CONST64(0xdeadffffdeadaaac));
741 __ movptr(Address(rsp, st_off), rax);
742 #endif /* ASSERT */
743 __ movdbl(Address(rsp, next_off), r_1->as_XMMRegister());
744 }
745 }
746 }
747
748 // Schedule the branch target address early.
749 __ movptr(rcx, Address(rbx, in_bytes(Method::interpreter_entry_offset())));
750 __ jmp(rcx);
751 }
752
753 static void range_check(MacroAssembler* masm, Register pc_reg, Register temp_reg,
754 address code_start, address code_end,
755 Label& L_ok) {
756 Label L_fail;
757 __ lea(temp_reg, ExternalAddress(code_start));
758 __ cmpptr(pc_reg, temp_reg);
759 __ jcc(Assembler::belowEqual, L_fail);
760 __ lea(temp_reg, ExternalAddress(code_end));
761 __ cmpptr(pc_reg, temp_reg);
762 __ jcc(Assembler::below, L_ok);
763 __ bind(L_fail);
764 }
765
766 void SharedRuntime::gen_i2c_adapter(MacroAssembler *masm,
767 int total_args_passed,
768 int comp_args_on_stack,
769 const BasicType *sig_bt,
770 const VMRegPair *regs) {
771
772 // Note: r13 contains the senderSP on entry. We must preserve it since
773 // we may do a i2c -> c2i transition if we lose a race where compiled
774 // code goes non-entrant while we get args ready.
775 // In addition we use r13 to locate all the interpreter args as
776 // we must align the stack to 16 bytes on an i2c entry else we
777 // lose alignment we expect in all compiled code and register
778 // save code can segv when fxsave instructions find improperly
779 // aligned stack pointer.
780
781 // Adapters can be frameless because they do not require the caller
782 // to perform additional cleanup work, such as correcting the stack pointer.
783 // An i2c adapter is frameless because the *caller* frame, which is interpreted,
784 // routinely repairs its own stack pointer (from interpreter_frame_last_sp),
785 // even if a callee has modified the stack pointer.
786 // A c2i adapter is frameless because the *callee* frame, which is interpreted,
787 // routinely repairs its caller's stack pointer (from sender_sp, which is set
788 // up via the senderSP register).
789 // In other words, if *either* the caller or callee is interpreted, we can
840 // Convert 4-byte c2 stack slots to words.
841 int comp_words_on_stack = align_up(comp_args_on_stack*VMRegImpl::stack_slot_size, wordSize)>>LogBytesPerWord;
842
843 if (comp_args_on_stack) {
844 __ subptr(rsp, comp_words_on_stack * wordSize);
845 }
846
847 // Ensure compiled code always sees stack at proper alignment
848 __ andptr(rsp, -16);
849
850 // push the return address and misalign the stack that youngest frame always sees
851 // as far as the placement of the call instruction
852 __ push(rax);
853
854 // Put saved SP in another register
855 const Register saved_sp = rax;
856 __ movptr(saved_sp, r11);
857
858 // Will jump to the compiled code just as if compiled code was doing it.
859 // Pre-load the register-jump target early, to schedule it better.
860 __ movptr(r11, Address(rbx, in_bytes(Method::from_compiled_offset())));
861
862 #if INCLUDE_JVMCI
863 if (EnableJVMCI) {
864 // check if this call should be routed towards a specific entry point
865 __ cmpptr(Address(r15_thread, in_bytes(JavaThread::jvmci_alternate_call_target_offset())), 0);
866 Label no_alternative_target;
867 __ jcc(Assembler::equal, no_alternative_target);
868 __ movptr(r11, Address(r15_thread, in_bytes(JavaThread::jvmci_alternate_call_target_offset())));
869 __ movptr(Address(r15_thread, in_bytes(JavaThread::jvmci_alternate_call_target_offset())), 0);
870 __ bind(no_alternative_target);
871 }
872 #endif // INCLUDE_JVMCI
873
874 // Now generate the shuffle code. Pick up all register args and move the
875 // rest through the floating point stack top.
876 for (int i = 0; i < total_args_passed; i++) {
877 if (sig_bt[i] == T_VOID) {
878 // Longs and doubles are passed in native word order, but misaligned
879 // in the 32-bit build.
880 assert(i > 0 && (sig_bt[i-1] == T_LONG || sig_bt[i-1] == T_DOUBLE), "missing half");
881 continue;
882 }
883
884 // Pick up 0, 1 or 2 words from SP+offset.
885
886 assert(!regs[i].second()->is_valid() || regs[i].first()->next() == regs[i].second(),
887 "scrambled load targets?");
888 // Load in argument order going down.
889 int ld_off = (total_args_passed - i)*Interpreter::stackElementSize;
890 // Point to interpreter value (vs. tag)
891 int next_off = ld_off - Interpreter::stackElementSize;
892 //
893 //
894 //
895 VMReg r_1 = regs[i].first();
896 VMReg r_2 = regs[i].second();
897 if (!r_1->is_valid()) {
898 assert(!r_2->is_valid(), "");
899 continue;
900 }
902 // Convert stack slot to an SP offset (+ wordSize to account for return address )
903 int st_off = regs[i].first()->reg2stack()*VMRegImpl::stack_slot_size + wordSize;
904
905 // We can use r13 as a temp here because compiled code doesn't need r13 as an input
906 // and if we end up going thru a c2i because of a miss a reasonable value of r13
907 // will be generated.
908 if (!r_2->is_valid()) {
909 // sign extend???
910 __ movl(r13, Address(saved_sp, ld_off));
911 __ movptr(Address(rsp, st_off), r13);
912 } else {
913 //
914 // We are using two optoregs. This can be either T_OBJECT, T_ADDRESS, T_LONG, or T_DOUBLE
915 // the interpreter allocates two slots but only uses one for thr T_LONG or T_DOUBLE case
916 // So we must adjust where to pick up the data to match the interpreter.
917 //
918 // Interpreter local[n] == MSW, local[n+1] == LSW however locals
919 // are accessed as negative so LSW is at LOW address
920
921 // ld_off is MSW so get LSW
922 const int offset = (sig_bt[i]==T_LONG||sig_bt[i]==T_DOUBLE)?
923 next_off : ld_off;
924 __ movq(r13, Address(saved_sp, offset));
925 // st_off is LSW (i.e. reg.first())
926 __ movq(Address(rsp, st_off), r13);
927 }
928 } else if (r_1->is_Register()) { // Register argument
929 Register r = r_1->as_Register();
930 assert(r != rax, "must be different");
931 if (r_2->is_valid()) {
932 //
933 // We are using two VMRegs. This can be either T_OBJECT, T_ADDRESS, T_LONG, or T_DOUBLE
934 // the interpreter allocates two slots but only uses one for thr T_LONG or T_DOUBLE case
935 // So we must adjust where to pick up the data to match the interpreter.
936
937 const int offset = (sig_bt[i]==T_LONG||sig_bt[i]==T_DOUBLE)?
938 next_off : ld_off;
939
940 // this can be a misaligned move
941 __ movq(r, Address(saved_sp, offset));
942 } else {
943 // sign extend and use a full word?
944 __ movl(r, Address(saved_sp, ld_off));
945 }
946 } else {
947 if (!r_2->is_valid()) {
948 __ movflt(r_1->as_XMMRegister(), Address(saved_sp, ld_off));
949 } else {
950 __ movdbl(r_1->as_XMMRegister(), Address(saved_sp, next_off));
951 }
952 }
953 }
954
955 __ push_cont_fastpath(); // Set JavaThread::_cont_fastpath to the sp of the oldest interpreted frame we know about
956
957 // 6243940 We might end up in handle_wrong_method if
958 // the callee is deoptimized as we race thru here. If that
959 // happens we don't want to take a safepoint because the
960 // caller frame will look interpreted and arguments are now
961 // "compiled" so it is much better to make this transition
962 // invisible to the stack walking code. Unfortunately if
963 // we try and find the callee by normal means a safepoint
964 // is possible. So we stash the desired callee in the thread
965 // and the vm will find there should this case occur.
966
967 __ movptr(Address(r15_thread, JavaThread::callee_target_offset()), rbx);
968
969 // put Method* where a c2i would expect should we end up there
970 // only needed because eof c2 resolve stubs return Method* as a result in
971 // rax
972 __ mov(rax, rbx);
973 __ jmp(r11);
974 }
975
976 // ---------------------------------------------------------------
977 AdapterHandlerEntry* SharedRuntime::generate_i2c2i_adapters(MacroAssembler *masm,
978 int total_args_passed,
979 int comp_args_on_stack,
980 const BasicType *sig_bt,
981 const VMRegPair *regs,
982 AdapterFingerPrint* fingerprint) {
983 address i2c_entry = __ pc();
984
985 gen_i2c_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs);
986
987 // -------------------------------------------------------------------------
988 // Generate a C2I adapter. On entry we know rbx holds the Method* during calls
989 // to the interpreter. The args start out packed in the compiled layout. They
990 // need to be unpacked into the interpreter layout. This will almost always
991 // require some stack space. We grow the current (compiled) stack, then repack
992 // the args. We finally end in a jump to the generic interpreter entry point.
993 // On exit from the interpreter, the interpreter will restore our SP (lest the
994 // compiled code, which relies solely on SP and not RBP, get sick).
995
996 address c2i_unverified_entry = __ pc();
997 Label skip_fixup;
998 Label ok;
999
1000 Register holder = rax;
1001 Register receiver = j_rarg0;
1002 Register temp = rbx;
1003
1004 {
1005 __ load_klass(temp, receiver, rscratch1);
1006 __ cmpptr(temp, Address(holder, CompiledICHolder::holder_klass_offset()));
1007 __ movptr(rbx, Address(holder, CompiledICHolder::holder_metadata_offset()));
1008 __ jcc(Assembler::equal, ok);
1009 __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
1010
1011 __ bind(ok);
1012 // Method might have been compiled since the call site was patched to
1013 // interpreted if that is the case treat it as a miss so we can get
1014 // the call site corrected.
1015 __ cmpptr(Address(rbx, in_bytes(Method::code_offset())), NULL_WORD);
1016 __ jcc(Assembler::equal, skip_fixup);
1017 __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
1018 }
1019
1020 address c2i_entry = __ pc();
1021
1022 // Class initialization barrier for static methods
1023 address c2i_no_clinit_check_entry = nullptr;
1024 if (VM_Version::supports_fast_class_init_checks()) {
1025 Label L_skip_barrier;
1026 Register method = rbx;
1027
1028 { // Bypass the barrier for non-static methods
1029 Register flags = rscratch1;
1030 __ movl(flags, Address(method, Method::access_flags_offset()));
1031 __ testl(flags, JVM_ACC_STATIC);
1032 __ jcc(Assembler::zero, L_skip_barrier); // non-static
1033 }
1034
1035 Register klass = rscratch1;
1036 __ load_method_holder(klass, method);
1037 __ clinit_barrier(klass, r15_thread, &L_skip_barrier /*L_fast_path*/);
1038
1039 __ jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub())); // slow path
1040
1041 __ bind(L_skip_barrier);
1042 c2i_no_clinit_check_entry = __ pc();
1043 }
1044
1045 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
1046 bs->c2i_entry_barrier(masm);
1047
1048 gen_c2i_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs, skip_fixup);
1049
1050 return AdapterHandlerLibrary::new_entry(fingerprint, i2c_entry, c2i_entry, c2i_unverified_entry, c2i_no_clinit_check_entry);
1051 }
1052
1053 int SharedRuntime::c_calling_convention(const BasicType *sig_bt,
1054 VMRegPair *regs,
1055 VMRegPair *regs2,
1056 int total_args_passed) {
1057 assert(regs2 == nullptr, "not needed on x86");
1058 // We return the amount of VMRegImpl stack slots we need to reserve for all
1059 // the arguments NOT counting out_preserve_stack_slots.
1060
1061 // NOTE: These arrays will have to change when c1 is ported
1062 #ifdef _WIN64
1063 static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {
1064 c_rarg0, c_rarg1, c_rarg2, c_rarg3
1065 };
1066 static const XMMRegister FP_ArgReg[Argument::n_float_register_parameters_c] = {
1067 c_farg0, c_farg1, c_farg2, c_farg3
1068 };
1069 #else
1070 static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {
1088 case T_BYTE:
1089 case T_SHORT:
1090 case T_INT:
1091 if (int_args < Argument::n_int_register_parameters_c) {
1092 regs[i].set1(INT_ArgReg[int_args++]->as_VMReg());
1093 #ifdef _WIN64
1094 fp_args++;
1095 // Allocate slots for callee to stuff register args the stack.
1096 stk_args += 2;
1097 #endif
1098 } else {
1099 regs[i].set1(VMRegImpl::stack2reg(stk_args));
1100 stk_args += 2;
1101 }
1102 break;
1103 case T_LONG:
1104 assert((i + 1) < total_args_passed && sig_bt[i + 1] == T_VOID, "expecting half");
1105 // fall through
1106 case T_OBJECT:
1107 case T_ARRAY:
1108 case T_ADDRESS:
1109 case T_METADATA:
1110 if (int_args < Argument::n_int_register_parameters_c) {
1111 regs[i].set2(INT_ArgReg[int_args++]->as_VMReg());
1112 #ifdef _WIN64
1113 fp_args++;
1114 stk_args += 2;
1115 #endif
1116 } else {
1117 regs[i].set2(VMRegImpl::stack2reg(stk_args));
1118 stk_args += 2;
1119 }
1120 break;
1121 case T_FLOAT:
1122 if (fp_args < Argument::n_float_register_parameters_c) {
1123 regs[i].set1(FP_ArgReg[fp_args++]->as_VMReg());
1124 #ifdef _WIN64
1125 int_args++;
1126 // Allocate slots for callee to stuff register args the stack.
1127 stk_args += 2;
2014
2015 int temploc = -1;
2016 for (int ai = 0; ai < arg_order.length(); ai += 2) {
2017 int i = arg_order.at(ai);
2018 int c_arg = arg_order.at(ai + 1);
2019 __ block_comment(err_msg("move %d -> %d", i, c_arg));
2020 #ifdef ASSERT
2021 if (in_regs[i].first()->is_Register()) {
2022 assert(!reg_destroyed[in_regs[i].first()->as_Register()->encoding()], "destroyed reg!");
2023 } else if (in_regs[i].first()->is_XMMRegister()) {
2024 assert(!freg_destroyed[in_regs[i].first()->as_XMMRegister()->encoding()], "destroyed reg!");
2025 }
2026 if (out_regs[c_arg].first()->is_Register()) {
2027 reg_destroyed[out_regs[c_arg].first()->as_Register()->encoding()] = true;
2028 } else if (out_regs[c_arg].first()->is_XMMRegister()) {
2029 freg_destroyed[out_regs[c_arg].first()->as_XMMRegister()->encoding()] = true;
2030 }
2031 #endif /* ASSERT */
2032 switch (in_sig_bt[i]) {
2033 case T_ARRAY:
2034 case T_OBJECT:
2035 __ object_move(map, oop_handle_offset, stack_slots, in_regs[i], out_regs[c_arg],
2036 ((i == 0) && (!is_static)),
2037 &receiver_offset);
2038 break;
2039 case T_VOID:
2040 break;
2041
2042 case T_FLOAT:
2043 __ float_move(in_regs[i], out_regs[c_arg]);
2044 break;
2045
2046 case T_DOUBLE:
2047 assert( i + 1 < total_in_args &&
2048 in_sig_bt[i + 1] == T_VOID &&
2049 out_sig_bt[c_arg+1] == T_VOID, "bad arg list");
2050 __ double_move(in_regs[i], out_regs[c_arg]);
2051 break;
2052
2053 case T_LONG :
2140 const int mark_word_offset = BasicLock::displaced_header_offset_in_bytes();
2141
2142 // Get the handle (the 2nd argument)
2143 __ mov(oop_handle_reg, c_rarg1);
2144
2145 // Get address of the box
2146
2147 __ lea(lock_reg, Address(rsp, lock_slot_offset * VMRegImpl::stack_slot_size));
2148
2149 // Load the oop from the handle
2150 __ movptr(obj_reg, Address(oop_handle_reg, 0));
2151
2152 if (LockingMode == LM_MONITOR) {
2153 __ jmp(slow_path_lock);
2154 } else if (LockingMode == LM_LEGACY) {
2155 // Load immediate 1 into swap_reg %rax
2156 __ movl(swap_reg, 1);
2157
2158 // Load (object->mark() | 1) into swap_reg %rax
2159 __ orptr(swap_reg, Address(obj_reg, oopDesc::mark_offset_in_bytes()));
2160
2161 // Save (object->mark() | 1) into BasicLock's displaced header
2162 __ movptr(Address(lock_reg, mark_word_offset), swap_reg);
2163
2164 // src -> dest iff dest == rax else rax <- dest
2165 __ lock();
2166 __ cmpxchgptr(lock_reg, Address(obj_reg, oopDesc::mark_offset_in_bytes()));
2167 __ jcc(Assembler::equal, count_mon);
2168
2169 // Hmm should this move to the slow path code area???
2170
2171 // Test if the oopMark is an obvious stack pointer, i.e.,
2172 // 1) (mark & 3) == 0, and
2173 // 2) rsp <= mark < mark + os::pagesize()
2174 // These 3 tests can be done by evaluating the following
2175 // expression: ((mark - rsp) & (3 - os::vm_page_size())),
2176 // assuming both stack pointer and pagesize have their
2177 // least significant 2 bits clear.
2178 // NOTE: the oopMark is in swap_reg %rax as the result of cmpxchg
2179
2204 // Now set thread in native
2205 __ movl(Address(r15_thread, JavaThread::thread_state_offset()), _thread_in_native);
2206
2207 __ call(RuntimeAddress(native_func));
2208
2209 // Verify or restore cpu control state after JNI call
2210 __ restore_cpu_control_state_after_jni(rscratch1);
2211
2212 // Unpack native results.
2213 switch (ret_type) {
2214 case T_BOOLEAN: __ c2bool(rax); break;
2215 case T_CHAR : __ movzwl(rax, rax); break;
2216 case T_BYTE : __ sign_extend_byte (rax); break;
2217 case T_SHORT : __ sign_extend_short(rax); break;
2218 case T_INT : /* nothing to do */ break;
2219 case T_DOUBLE :
2220 case T_FLOAT :
2221 // Result is in xmm0 we'll save as needed
2222 break;
2223 case T_ARRAY: // Really a handle
2224 case T_OBJECT: // Really a handle
2225 break; // can't de-handlize until after safepoint check
2226 case T_VOID: break;
2227 case T_LONG: break;
2228 default : ShouldNotReachHere();
2229 }
2230
2231 Label after_transition;
2232
2233 // Switch thread to "native transition" state before reading the synchronization state.
2234 // This additional state is necessary because reading and testing the synchronization
2235 // state is not atomic w.r.t. GC, as this scenario demonstrates:
2236 // Java thread A, in _thread_in_native state, loads _not_synchronized and is preempted.
2237 // VM thread changes sync state to synchronizing and suspends threads for GC.
2238 // Thread A is resumed to finish this native method, but doesn't block here since it
2239 // didn't see any synchronization is progress, and escapes.
2240 __ movl(Address(r15_thread, JavaThread::thread_state_offset()), _thread_in_native_trans);
2241
2242 // Force this write out before the read below
2243 if (!UseSystemMemoryBarrier) {
3701 __ movptr(Address(r15_thread, JavaThread::exception_pc_offset()), NULL_WORD);
3702 #endif
3703 // Clear the exception oop so GC no longer processes it as a root.
3704 __ movptr(Address(r15_thread, JavaThread::exception_oop_offset()), NULL_WORD);
3705
3706 // rax: exception oop
3707 // r8: exception handler
3708 // rdx: exception pc
3709 // Jump to handler
3710
3711 __ jmp(r8);
3712
3713 // Make sure all code is generated
3714 masm->flush();
3715
3716 // Set exception blob
3717 _exception_blob = ExceptionBlob::create(&buffer, oop_maps, SimpleRuntimeFrame::framesize >> 1);
3718 }
3719 #endif // COMPILER2
3720
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #ifndef _WINDOWS
27 #include "alloca.h"
28 #endif
29 #include "asm/macroAssembler.hpp"
30 #include "asm/macroAssembler.inline.hpp"
31 #include "classfile/symbolTable.hpp"
32 #include "code/compiledIC.hpp"
33 #include "code/debugInfoRec.hpp"
34 #include "code/icBuffer.hpp"
35 #include "code/nativeInst.hpp"
36 #include "code/vtableStubs.hpp"
37 #include "compiler/oopMap.hpp"
38 #include "gc/shared/collectedHeap.hpp"
39 #include "gc/shared/gcLocker.hpp"
40 #include "gc/shared/barrierSet.hpp"
41 #include "gc/shared/barrierSetAssembler.hpp"
42 #include "interpreter/interpreter.hpp"
43 #include "logging/log.hpp"
44 #include "memory/resourceArea.hpp"
45 #include "memory/universe.hpp"
46 #include "oops/compiledICHolder.hpp"
47 #include "oops/klass.inline.hpp"
48 #include "oops/method.inline.hpp"
49 #include "prims/methodHandles.hpp"
50 #include "runtime/continuation.hpp"
51 #include "runtime/continuationEntry.inline.hpp"
508 case T_SHORT:
509 case T_INT:
510 if (int_args < Argument::n_int_register_parameters_j) {
511 regs[i].set1(INT_ArgReg[int_args++]->as_VMReg());
512 } else {
513 regs[i].set1(VMRegImpl::stack2reg(stk_args));
514 stk_args += 2;
515 }
516 break;
517 case T_VOID:
518 // halves of T_LONG or T_DOUBLE
519 assert(i != 0 && (sig_bt[i - 1] == T_LONG || sig_bt[i - 1] == T_DOUBLE), "expecting half");
520 regs[i].set_bad();
521 break;
522 case T_LONG:
523 assert((i + 1) < total_args_passed && sig_bt[i + 1] == T_VOID, "expecting half");
524 // fall through
525 case T_OBJECT:
526 case T_ARRAY:
527 case T_ADDRESS:
528 case T_PRIMITIVE_OBJECT:
529 if (int_args < Argument::n_int_register_parameters_j) {
530 regs[i].set2(INT_ArgReg[int_args++]->as_VMReg());
531 } else {
532 regs[i].set2(VMRegImpl::stack2reg(stk_args));
533 stk_args += 2;
534 }
535 break;
536 case T_FLOAT:
537 if (fp_args < Argument::n_float_register_parameters_j) {
538 regs[i].set1(FP_ArgReg[fp_args++]->as_VMReg());
539 } else {
540 regs[i].set1(VMRegImpl::stack2reg(stk_args));
541 stk_args += 2;
542 }
543 break;
544 case T_DOUBLE:
545 assert((i + 1) < total_args_passed && sig_bt[i + 1] == T_VOID, "expecting half");
546 if (fp_args < Argument::n_float_register_parameters_j) {
547 regs[i].set2(FP_ArgReg[fp_args++]->as_VMReg());
548 } else {
549 regs[i].set2(VMRegImpl::stack2reg(stk_args));
550 stk_args += 2;
551 }
552 break;
553 default:
554 ShouldNotReachHere();
555 break;
556 }
557 }
558
559 return align_up(stk_args, 2);
560 }
561
562 // Same as java_calling_convention() but for multiple return
563 // values. There's no way to store them on the stack so if we don't
564 // have enough registers, multiple values can't be returned.
565 const uint SharedRuntime::java_return_convention_max_int = Argument::n_int_register_parameters_j+1;
566 const uint SharedRuntime::java_return_convention_max_float = Argument::n_float_register_parameters_j;
567 int SharedRuntime::java_return_convention(const BasicType *sig_bt,
568 VMRegPair *regs,
569 int total_args_passed) {
570 // Create the mapping between argument positions and
571 // registers.
572 static const Register INT_ArgReg[java_return_convention_max_int] = {
573 rax, j_rarg5, j_rarg4, j_rarg3, j_rarg2, j_rarg1, j_rarg0
574 };
575 static const XMMRegister FP_ArgReg[java_return_convention_max_float] = {
576 j_farg0, j_farg1, j_farg2, j_farg3,
577 j_farg4, j_farg5, j_farg6, j_farg7
578 };
579
580
581 uint int_args = 0;
582 uint fp_args = 0;
583
584 for (int i = 0; i < total_args_passed; i++) {
585 switch (sig_bt[i]) {
586 case T_BOOLEAN:
587 case T_CHAR:
588 case T_BYTE:
589 case T_SHORT:
590 case T_INT:
591 if (int_args < Argument::n_int_register_parameters_j+1) {
592 regs[i].set1(INT_ArgReg[int_args]->as_VMReg());
593 int_args++;
594 } else {
595 return -1;
596 }
597 break;
598 case T_VOID:
599 // halves of T_LONG or T_DOUBLE
600 assert(i != 0 && (sig_bt[i - 1] == T_LONG || sig_bt[i - 1] == T_DOUBLE), "expecting half");
601 regs[i].set_bad();
602 break;
603 case T_LONG:
604 assert(sig_bt[i + 1] == T_VOID, "expecting half");
605 // fall through
606 case T_OBJECT:
607 case T_PRIMITIVE_OBJECT:
608 case T_ARRAY:
609 case T_ADDRESS:
610 case T_METADATA:
611 if (int_args < Argument::n_int_register_parameters_j+1) {
612 regs[i].set2(INT_ArgReg[int_args]->as_VMReg());
613 int_args++;
614 } else {
615 return -1;
616 }
617 break;
618 case T_FLOAT:
619 if (fp_args < Argument::n_float_register_parameters_j) {
620 regs[i].set1(FP_ArgReg[fp_args]->as_VMReg());
621 fp_args++;
622 } else {
623 return -1;
624 }
625 break;
626 case T_DOUBLE:
627 assert(sig_bt[i + 1] == T_VOID, "expecting half");
628 if (fp_args < Argument::n_float_register_parameters_j) {
629 regs[i].set2(FP_ArgReg[fp_args]->as_VMReg());
630 fp_args++;
631 } else {
632 return -1;
633 }
634 break;
635 default:
636 ShouldNotReachHere();
637 break;
638 }
639 }
640
641 return int_args + fp_args;
642 }
643
644 // Patch the callers callsite with entry to compiled code if it exists.
645 static void patch_callers_callsite(MacroAssembler *masm) {
646 Label L;
647 __ cmpptr(Address(rbx, in_bytes(Method::code_offset())), NULL_WORD);
648 __ jcc(Assembler::equal, L);
649
650 // Save the current stack pointer
651 __ mov(r13, rsp);
652 // Schedule the branch target address early.
653 // Call into the VM to patch the caller, then jump to compiled callee
654 // rax isn't live so capture return address while we easily can
655 __ movptr(rax, Address(rsp, 0));
656
657 // align stack so push_CPU_state doesn't fault
658 __ andptr(rsp, -(StackAlignmentInBytes));
659 __ push_CPU_state();
660 __ vzeroupper();
661 // VM needs caller's callsite
662 // VM needs target method
663 // This needs to be a long call since we will relocate this adapter to
666 // Allocate argument register save area
667 if (frame::arg_reg_save_area_bytes != 0) {
668 __ subptr(rsp, frame::arg_reg_save_area_bytes);
669 }
670 __ mov(c_rarg0, rbx);
671 __ mov(c_rarg1, rax);
672 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::fixup_callers_callsite)));
673
674 // De-allocate argument register save area
675 if (frame::arg_reg_save_area_bytes != 0) {
676 __ addptr(rsp, frame::arg_reg_save_area_bytes);
677 }
678
679 __ vzeroupper();
680 __ pop_CPU_state();
681 // restore sp
682 __ mov(rsp, r13);
683 __ bind(L);
684 }
685
686 // For each inline type argument, sig includes the list of fields of
687 // the inline type. This utility function computes the number of
688 // arguments for the call if inline types are passed by reference (the
689 // calling convention the interpreter expects).
690 static int compute_total_args_passed_int(const GrowableArray<SigEntry>* sig_extended) {
691 int total_args_passed = 0;
692 if (InlineTypePassFieldsAsArgs) {
693 for (int i = 0; i < sig_extended->length(); i++) {
694 BasicType bt = sig_extended->at(i)._bt;
695 if (bt == T_PRIMITIVE_OBJECT) {
696 // In sig_extended, an inline type argument starts with:
697 // T_PRIMITIVE_OBJECT, followed by the types of the fields of the
698 // inline type and T_VOID to mark the end of the value
699 // type. Inline types are flattened so, for instance, in the
700 // case of an inline type with an int field and an inline type
701 // field that itself has 2 fields, an int and a long:
702 // T_PRIMITIVE_OBJECT T_INT T_PRIMITIVE_OBJECT T_INT T_LONG T_VOID (second
703 // slot for the T_LONG) T_VOID (inner T_PRIMITIVE_OBJECT) T_VOID
704 // (outer T_PRIMITIVE_OBJECT)
705 total_args_passed++;
706 int vt = 1;
707 do {
708 i++;
709 BasicType bt = sig_extended->at(i)._bt;
710 BasicType prev_bt = sig_extended->at(i-1)._bt;
711 if (bt == T_PRIMITIVE_OBJECT) {
712 vt++;
713 } else if (bt == T_VOID &&
714 prev_bt != T_LONG &&
715 prev_bt != T_DOUBLE) {
716 vt--;
717 }
718 } while (vt != 0);
719 } else {
720 total_args_passed++;
721 }
722 }
723 } else {
724 total_args_passed = sig_extended->length();
725 }
726 return total_args_passed;
727 }
728
729
730 static void gen_c2i_adapter_helper(MacroAssembler* masm,
731 BasicType bt,
732 BasicType prev_bt,
733 size_t size_in_bytes,
734 const VMRegPair& reg_pair,
735 const Address& to,
736 int extraspace,
737 bool is_oop) {
738 assert(bt != T_PRIMITIVE_OBJECT || !InlineTypePassFieldsAsArgs, "no inline type here");
739 if (bt == T_VOID) {
740 assert(prev_bt == T_LONG || prev_bt == T_DOUBLE, "missing half");
741 return;
742 }
743
744 // Say 4 args:
745 // i st_off
746 // 0 32 T_LONG
747 // 1 24 T_VOID
748 // 2 16 T_OBJECT
749 // 3 8 T_BOOL
750 // - 0 return address
751 //
752 // However to make thing extra confusing. Because we can fit a long/double in
753 // a single slot on a 64 bt vm and it would be silly to break them up, the interpreter
754 // leaves one slot empty and only stores to a single slot. In this case the
755 // slot that is occupied is the T_VOID slot. See I said it was confusing.
756
757 bool wide = (size_in_bytes == wordSize);
758 VMReg r_1 = reg_pair.first();
759 VMReg r_2 = reg_pair.second();
760 assert(r_2->is_valid() == wide, "invalid size");
761 if (!r_1->is_valid()) {
762 assert(!r_2->is_valid(), "must be invalid");
763 return;
764 }
765
766 if (!r_1->is_XMMRegister()) {
767 Register val = rax;
768 if (r_1->is_stack()) {
769 int ld_off = r_1->reg2stack() * VMRegImpl::stack_slot_size + extraspace;
770 __ load_sized_value(val, Address(rsp, ld_off), size_in_bytes, /* is_signed */ false);
771 } else {
772 val = r_1->as_Register();
773 }
774 assert_different_registers(to.base(), val, rscratch1);
775 if (is_oop) {
776 __ push(r13);
777 __ push(rbx);
778 __ store_heap_oop(to, val, rscratch1, r13, rbx, IN_HEAP | ACCESS_WRITE | IS_DEST_UNINITIALIZED);
779 __ pop(rbx);
780 __ pop(r13);
781 } else {
782 __ store_sized_value(to, val, size_in_bytes);
783 }
784 } else {
785 if (wide) {
786 __ movdbl(to, r_1->as_XMMRegister());
787 } else {
788 __ movflt(to, r_1->as_XMMRegister());
789 }
790 }
791 }
792
793 static void gen_c2i_adapter(MacroAssembler *masm,
794 const GrowableArray<SigEntry>* sig_extended,
795 const VMRegPair *regs,
796 bool requires_clinit_barrier,
797 address& c2i_no_clinit_check_entry,
798 Label& skip_fixup,
799 address start,
800 OopMapSet* oop_maps,
801 int& frame_complete,
802 int& frame_size_in_words,
803 bool alloc_inline_receiver) {
804 if (requires_clinit_barrier && VM_Version::supports_fast_class_init_checks()) {
805 Label L_skip_barrier;
806 Register method = rbx;
807
808 { // Bypass the barrier for non-static methods
809 Register flags = rscratch1;
810 __ movl(flags, Address(method, Method::access_flags_offset()));
811 __ testl(flags, JVM_ACC_STATIC);
812 __ jcc(Assembler::zero, L_skip_barrier); // non-static
813 }
814
815 Register klass = rscratch1;
816 __ load_method_holder(klass, method);
817 __ clinit_barrier(klass, r15_thread, &L_skip_barrier /*L_fast_path*/);
818
819 __ jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub())); // slow path
820
821 __ bind(L_skip_barrier);
822 c2i_no_clinit_check_entry = __ pc();
823 }
824
825 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
826 bs->c2i_entry_barrier(masm);
827
828 // Before we get into the guts of the C2I adapter, see if we should be here
829 // at all. We've come from compiled code and are attempting to jump to the
830 // interpreter, which means the caller made a static call to get here
831 // (vcalls always get a compiled target if there is one). Check for a
832 // compiled target. If there is one, we need to patch the caller's call.
833 patch_callers_callsite(masm);
834
835 __ bind(skip_fixup);
836
837 if (InlineTypePassFieldsAsArgs) {
838 // Is there an inline type argument?
839 bool has_inline_argument = false;
840 for (int i = 0; i < sig_extended->length() && !has_inline_argument; i++) {
841 has_inline_argument = (sig_extended->at(i)._bt == T_PRIMITIVE_OBJECT);
842 }
843 if (has_inline_argument) {
844 // There is at least an inline type argument: we're coming from
845 // compiled code so we have no buffers to back the inline types.
846 // Allocate the buffers here with a runtime call.
847 OopMap* map = RegisterSaver::save_live_registers(masm, 0, &frame_size_in_words, /*save_vectors*/ false);
848
849 frame_complete = __ offset();
850
851 __ set_last_Java_frame(noreg, noreg, nullptr, rscratch1);
852
853 __ mov(c_rarg0, r15_thread);
854 __ mov(c_rarg1, rbx);
855 __ mov64(c_rarg2, (int64_t)alloc_inline_receiver);
856 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::allocate_inline_types)));
857
858 oop_maps->add_gc_map((int)(__ pc() - start), map);
859 __ reset_last_Java_frame(false);
860
861 RegisterSaver::restore_live_registers(masm);
862
863 Label no_exception;
864 __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), NULL_WORD);
865 __ jcc(Assembler::equal, no_exception);
866
867 __ movptr(Address(r15_thread, JavaThread::vm_result_offset()), NULL_WORD);
868 __ movptr(rax, Address(r15_thread, Thread::pending_exception_offset()));
869 __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
870
871 __ bind(no_exception);
872
873 // We get an array of objects from the runtime call
874 __ get_vm_result(rscratch2, r15_thread); // Use rscratch2 (r11) as temporary because rscratch1 (r10) is trashed by movptr()
875 __ get_vm_result_2(rbx, r15_thread); // TODO: required to keep the callee Method live?
876 }
877 }
878
879 // Since all args are passed on the stack, total_args_passed *
880 // Interpreter::stackElementSize is the space we need.
881 int total_args_passed = compute_total_args_passed_int(sig_extended);
882 assert(total_args_passed >= 0, "total_args_passed is %d", total_args_passed);
883
884 int extraspace = (total_args_passed * Interpreter::stackElementSize);
885
886 // stack is aligned, keep it that way
887 // This is not currently needed or enforced by the interpreter, but
888 // we might as well conform to the ABI.
889 extraspace = align_up(extraspace, 2*wordSize);
890
891 // set senderSP value
892 __ lea(r13, Address(rsp, wordSize));
893
894 #ifdef ASSERT
895 __ check_stack_alignment(r13, "sender stack not aligned");
896 #endif
897 if (extraspace > 0) {
898 // Pop the return address
899 __ pop(rax);
900
901 __ subptr(rsp, extraspace);
902
903 // Push the return address
904 __ push(rax);
905
906 // Account for the return address location since we store it first rather
907 // than hold it in a register across all the shuffling
908 extraspace += wordSize;
909 }
910
911 #ifdef ASSERT
912 __ check_stack_alignment(rsp, "callee stack not aligned", wordSize, rax);
913 #endif
914
915 // Now write the args into the outgoing interpreter space
916
917 // next_arg_comp is the next argument from the compiler point of
918 // view (inline type fields are passed in registers/on the stack). In
919 // sig_extended, an inline type argument starts with: T_PRIMITIVE_OBJECT,
920 // followed by the types of the fields of the inline type and T_VOID
921 // to mark the end of the inline type. ignored counts the number of
922 // T_PRIMITIVE_OBJECT/T_VOID. next_vt_arg is the next inline type argument:
923 // used to get the buffer for that argument from the pool of buffers
924 // we allocated above and want to pass to the
925 // interpreter. next_arg_int is the next argument from the
926 // interpreter point of view (inline types are passed by reference).
927 for (int next_arg_comp = 0, ignored = 0, next_vt_arg = 0, next_arg_int = 0;
928 next_arg_comp < sig_extended->length(); next_arg_comp++) {
929 assert(ignored <= next_arg_comp, "shouldn't skip over more slots than there are arguments");
930 assert(next_arg_int <= total_args_passed, "more arguments for the interpreter than expected?");
931 BasicType bt = sig_extended->at(next_arg_comp)._bt;
932 int st_off = (total_args_passed - next_arg_int) * Interpreter::stackElementSize;
933 if (!InlineTypePassFieldsAsArgs || bt != T_PRIMITIVE_OBJECT) {
934 int next_off = st_off - Interpreter::stackElementSize;
935 const int offset = (bt == T_LONG || bt == T_DOUBLE) ? next_off : st_off;
936 const VMRegPair reg_pair = regs[next_arg_comp-ignored];
937 size_t size_in_bytes = reg_pair.second()->is_valid() ? 8 : 4;
938 gen_c2i_adapter_helper(masm, bt, next_arg_comp > 0 ? sig_extended->at(next_arg_comp-1)._bt : T_ILLEGAL,
939 size_in_bytes, reg_pair, Address(rsp, offset), extraspace, false);
940 next_arg_int++;
941 #ifdef ASSERT
942 if (bt == T_LONG || bt == T_DOUBLE) {
943 // Overwrite the unused slot with known junk
944 __ mov64(rax, CONST64(0xdeadffffdeadaaaa));
945 __ movptr(Address(rsp, st_off), rax);
946 }
947 #endif /* ASSERT */
948 } else {
949 ignored++;
950 // get the buffer from the just allocated pool of buffers
951 int index = arrayOopDesc::base_offset_in_bytes(T_OBJECT) + next_vt_arg * type2aelembytes(T_PRIMITIVE_OBJECT);
952 __ load_heap_oop(r14, Address(rscratch2, index));
953 next_vt_arg++; next_arg_int++;
954 int vt = 1;
955 // write fields we get from compiled code in registers/stack
956 // slots to the buffer: we know we are done with that inline type
957 // argument when we hit the T_VOID that acts as an end of inline
958 // type delimiter for this inline type. Inline types are flattened
959 // so we might encounter embedded inline types. Each entry in
960 // sig_extended contains a field offset in the buffer.
961 Label L_null;
962 do {
963 next_arg_comp++;
964 BasicType bt = sig_extended->at(next_arg_comp)._bt;
965 BasicType prev_bt = sig_extended->at(next_arg_comp-1)._bt;
966 if (bt == T_PRIMITIVE_OBJECT) {
967 vt++;
968 ignored++;
969 } else if (bt == T_VOID &&
970 prev_bt != T_LONG &&
971 prev_bt != T_DOUBLE) {
972 vt--;
973 ignored++;
974 } else {
975 int off = sig_extended->at(next_arg_comp)._offset;
976 if (off == -1) {
977 // Nullable inline type argument, emit null check
978 VMReg reg = regs[next_arg_comp-ignored].first();
979 Label L_notNull;
980 if (reg->is_stack()) {
981 int ld_off = reg->reg2stack() * VMRegImpl::stack_slot_size + extraspace;
982 __ testb(Address(rsp, ld_off), 1);
983 } else {
984 __ testb(reg->as_Register(), 1);
985 }
986 __ jcc(Assembler::notZero, L_notNull);
987 __ movptr(Address(rsp, st_off), 0);
988 __ jmp(L_null);
989 __ bind(L_notNull);
990 continue;
991 }
992 assert(off > 0, "offset in object should be positive");
993 size_t size_in_bytes = is_java_primitive(bt) ? type2aelembytes(bt) : wordSize;
994 bool is_oop = is_reference_type(bt);
995 gen_c2i_adapter_helper(masm, bt, next_arg_comp > 0 ? sig_extended->at(next_arg_comp-1)._bt : T_ILLEGAL,
996 size_in_bytes, regs[next_arg_comp-ignored], Address(r14, off), extraspace, is_oop);
997 }
998 } while (vt != 0);
999 // pass the buffer to the interpreter
1000 __ movptr(Address(rsp, st_off), r14);
1001 __ bind(L_null);
1002 }
1003 }
1004
1005 // Schedule the branch target address early.
1006 __ movptr(rcx, Address(rbx, in_bytes(Method::interpreter_entry_offset())));
1007 __ jmp(rcx);
1008 }
1009
1010 static void range_check(MacroAssembler* masm, Register pc_reg, Register temp_reg,
1011 address code_start, address code_end,
1012 Label& L_ok) {
1013 Label L_fail;
1014 __ lea(temp_reg, ExternalAddress(code_start));
1015 __ cmpptr(pc_reg, temp_reg);
1016 __ jcc(Assembler::belowEqual, L_fail);
1017 __ lea(temp_reg, ExternalAddress(code_end));
1018 __ cmpptr(pc_reg, temp_reg);
1019 __ jcc(Assembler::below, L_ok);
1020 __ bind(L_fail);
1021 }
1022
1023 void SharedRuntime::gen_i2c_adapter(MacroAssembler *masm,
1024 int comp_args_on_stack,
1025 const GrowableArray<SigEntry>* sig,
1026 const VMRegPair *regs) {
1027
1028 // Note: r13 contains the senderSP on entry. We must preserve it since
1029 // we may do a i2c -> c2i transition if we lose a race where compiled
1030 // code goes non-entrant while we get args ready.
1031 // In addition we use r13 to locate all the interpreter args as
1032 // we must align the stack to 16 bytes on an i2c entry else we
1033 // lose alignment we expect in all compiled code and register
1034 // save code can segv when fxsave instructions find improperly
1035 // aligned stack pointer.
1036
1037 // Adapters can be frameless because they do not require the caller
1038 // to perform additional cleanup work, such as correcting the stack pointer.
1039 // An i2c adapter is frameless because the *caller* frame, which is interpreted,
1040 // routinely repairs its own stack pointer (from interpreter_frame_last_sp),
1041 // even if a callee has modified the stack pointer.
1042 // A c2i adapter is frameless because the *callee* frame, which is interpreted,
1043 // routinely repairs its caller's stack pointer (from sender_sp, which is set
1044 // up via the senderSP register).
1045 // In other words, if *either* the caller or callee is interpreted, we can
1096 // Convert 4-byte c2 stack slots to words.
1097 int comp_words_on_stack = align_up(comp_args_on_stack*VMRegImpl::stack_slot_size, wordSize)>>LogBytesPerWord;
1098
1099 if (comp_args_on_stack) {
1100 __ subptr(rsp, comp_words_on_stack * wordSize);
1101 }
1102
1103 // Ensure compiled code always sees stack at proper alignment
1104 __ andptr(rsp, -16);
1105
1106 // push the return address and misalign the stack that youngest frame always sees
1107 // as far as the placement of the call instruction
1108 __ push(rax);
1109
1110 // Put saved SP in another register
1111 const Register saved_sp = rax;
1112 __ movptr(saved_sp, r11);
1113
1114 // Will jump to the compiled code just as if compiled code was doing it.
1115 // Pre-load the register-jump target early, to schedule it better.
1116 __ movptr(r11, Address(rbx, in_bytes(Method::from_compiled_inline_offset())));
1117
1118 #if INCLUDE_JVMCI
1119 if (EnableJVMCI) {
1120 // check if this call should be routed towards a specific entry point
1121 __ cmpptr(Address(r15_thread, in_bytes(JavaThread::jvmci_alternate_call_target_offset())), 0);
1122 Label no_alternative_target;
1123 __ jcc(Assembler::equal, no_alternative_target);
1124 __ movptr(r11, Address(r15_thread, in_bytes(JavaThread::jvmci_alternate_call_target_offset())));
1125 __ movptr(Address(r15_thread, in_bytes(JavaThread::jvmci_alternate_call_target_offset())), 0);
1126 __ bind(no_alternative_target);
1127 }
1128 #endif // INCLUDE_JVMCI
1129
1130 int total_args_passed = sig->length();
1131
1132 // Now generate the shuffle code. Pick up all register args and move the
1133 // rest through the floating point stack top.
1134 for (int i = 0; i < total_args_passed; i++) {
1135 BasicType bt = sig->at(i)._bt;
1136 assert(bt != T_PRIMITIVE_OBJECT, "i2c adapter doesn't unpack inline type args");
1137 if (bt == T_VOID) {
1138 // Longs and doubles are passed in native word order, but misaligned
1139 // in the 32-bit build.
1140 BasicType prev_bt = (i > 0) ? sig->at(i-1)._bt : T_ILLEGAL;
1141 assert(i > 0 && (prev_bt == T_LONG || prev_bt == T_DOUBLE), "missing half");
1142 continue;
1143 }
1144
1145 // Pick up 0, 1 or 2 words from SP+offset.
1146
1147 assert(!regs[i].second()->is_valid() || regs[i].first()->next() == regs[i].second(),
1148 "scrambled load targets?");
1149 // Load in argument order going down.
1150 int ld_off = (total_args_passed - i)*Interpreter::stackElementSize;
1151 // Point to interpreter value (vs. tag)
1152 int next_off = ld_off - Interpreter::stackElementSize;
1153 //
1154 //
1155 //
1156 VMReg r_1 = regs[i].first();
1157 VMReg r_2 = regs[i].second();
1158 if (!r_1->is_valid()) {
1159 assert(!r_2->is_valid(), "");
1160 continue;
1161 }
1163 // Convert stack slot to an SP offset (+ wordSize to account for return address )
1164 int st_off = regs[i].first()->reg2stack()*VMRegImpl::stack_slot_size + wordSize;
1165
1166 // We can use r13 as a temp here because compiled code doesn't need r13 as an input
1167 // and if we end up going thru a c2i because of a miss a reasonable value of r13
1168 // will be generated.
1169 if (!r_2->is_valid()) {
1170 // sign extend???
1171 __ movl(r13, Address(saved_sp, ld_off));
1172 __ movptr(Address(rsp, st_off), r13);
1173 } else {
1174 //
1175 // We are using two optoregs. This can be either T_OBJECT, T_ADDRESS, T_LONG, or T_DOUBLE
1176 // the interpreter allocates two slots but only uses one for thr T_LONG or T_DOUBLE case
1177 // So we must adjust where to pick up the data to match the interpreter.
1178 //
1179 // Interpreter local[n] == MSW, local[n+1] == LSW however locals
1180 // are accessed as negative so LSW is at LOW address
1181
1182 // ld_off is MSW so get LSW
1183 const int offset = (bt==T_LONG||bt==T_DOUBLE)?
1184 next_off : ld_off;
1185 __ movq(r13, Address(saved_sp, offset));
1186 // st_off is LSW (i.e. reg.first())
1187 __ movq(Address(rsp, st_off), r13);
1188 }
1189 } else if (r_1->is_Register()) { // Register argument
1190 Register r = r_1->as_Register();
1191 assert(r != rax, "must be different");
1192 if (r_2->is_valid()) {
1193 //
1194 // We are using two VMRegs. This can be either T_OBJECT, T_ADDRESS, T_LONG, or T_DOUBLE
1195 // the interpreter allocates two slots but only uses one for thr T_LONG or T_DOUBLE case
1196 // So we must adjust where to pick up the data to match the interpreter.
1197
1198 const int offset = (bt==T_LONG||bt==T_DOUBLE)?
1199 next_off : ld_off;
1200
1201 // this can be a misaligned move
1202 __ movq(r, Address(saved_sp, offset));
1203 } else {
1204 // sign extend and use a full word?
1205 __ movl(r, Address(saved_sp, ld_off));
1206 }
1207 } else {
1208 if (!r_2->is_valid()) {
1209 __ movflt(r_1->as_XMMRegister(), Address(saved_sp, ld_off));
1210 } else {
1211 __ movdbl(r_1->as_XMMRegister(), Address(saved_sp, next_off));
1212 }
1213 }
1214 }
1215
1216 __ push_cont_fastpath(); // Set JavaThread::_cont_fastpath to the sp of the oldest interpreted frame we know about
1217
1218 // 6243940 We might end up in handle_wrong_method if
1219 // the callee is deoptimized as we race thru here. If that
1220 // happens we don't want to take a safepoint because the
1221 // caller frame will look interpreted and arguments are now
1222 // "compiled" so it is much better to make this transition
1223 // invisible to the stack walking code. Unfortunately if
1224 // we try and find the callee by normal means a safepoint
1225 // is possible. So we stash the desired callee in the thread
1226 // and the vm will find there should this case occur.
1227
1228 __ movptr(Address(r15_thread, JavaThread::callee_target_offset()), rbx);
1229
1230 // put Method* where a c2i would expect should we end up there
1231 // only needed because of c2 resolve stubs return Method* as a result in
1232 // rax
1233 __ mov(rax, rbx);
1234 __ jmp(r11);
1235 }
1236
1237 static void gen_inline_cache_check(MacroAssembler *masm, Label& skip_fixup) {
1238 Label ok;
1239
1240 Register holder = rax;
1241 Register receiver = j_rarg0;
1242 Register temp = rbx;
1243
1244 __ load_klass(temp, receiver, rscratch1);
1245 __ cmpptr(temp, Address(holder, CompiledICHolder::holder_klass_offset()));
1246 __ movptr(rbx, Address(holder, CompiledICHolder::holder_metadata_offset()));
1247 __ jcc(Assembler::equal, ok);
1248 __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
1249
1250 __ bind(ok);
1251 // Method might have been compiled since the call site was patched to
1252 // interpreted if that is the case treat it as a miss so we can get
1253 // the call site corrected.
1254 __ cmpptr(Address(rbx, in_bytes(Method::code_offset())), NULL_WORD);
1255 __ jcc(Assembler::equal, skip_fixup);
1256 __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
1257 }
1258
1259 // ---------------------------------------------------------------
1260 AdapterHandlerEntry* SharedRuntime::generate_i2c2i_adapters(MacroAssembler* masm,
1261 int comp_args_on_stack,
1262 const GrowableArray<SigEntry>* sig,
1263 const VMRegPair* regs,
1264 const GrowableArray<SigEntry>* sig_cc,
1265 const VMRegPair* regs_cc,
1266 const GrowableArray<SigEntry>* sig_cc_ro,
1267 const VMRegPair* regs_cc_ro,
1268 AdapterFingerPrint* fingerprint,
1269 AdapterBlob*& new_adapter,
1270 bool allocate_code_blob) {
1271 address i2c_entry = __ pc();
1272 gen_i2c_adapter(masm, comp_args_on_stack, sig, regs);
1273
1274 // -------------------------------------------------------------------------
1275 // Generate a C2I adapter. On entry we know rbx holds the Method* during calls
1276 // to the interpreter. The args start out packed in the compiled layout. They
1277 // need to be unpacked into the interpreter layout. This will almost always
1278 // require some stack space. We grow the current (compiled) stack, then repack
1279 // the args. We finally end in a jump to the generic interpreter entry point.
1280 // On exit from the interpreter, the interpreter will restore our SP (lest the
1281 // compiled code, which relies solely on SP and not RBP, get sick).
1282
1283 address c2i_unverified_entry = __ pc();
1284 address c2i_unverified_inline_entry = __ pc();
1285 Label skip_fixup;
1286
1287 gen_inline_cache_check(masm, skip_fixup);
1288
1289 OopMapSet* oop_maps = new OopMapSet();
1290 int frame_complete = CodeOffsets::frame_never_safe;
1291 int frame_size_in_words = 0;
1292
1293 // Scalarized c2i adapter with non-scalarized receiver (i.e., don't pack receiver)
1294 address c2i_no_clinit_check_entry = nullptr;
1295 address c2i_inline_ro_entry = __ pc();
1296 if (regs_cc != regs_cc_ro) {
1297 // No class init barrier needed because method is guaranteed to be non-static
1298 gen_c2i_adapter(masm, sig_cc_ro, regs_cc_ro, /* requires_clinit_barrier = */ false, c2i_no_clinit_check_entry,
1299 skip_fixup, i2c_entry, oop_maps, frame_complete, frame_size_in_words, /* alloc_inline_receiver = */ false);
1300 skip_fixup.reset();
1301 }
1302
1303 // Scalarized c2i adapter
1304 address c2i_entry = __ pc();
1305 address c2i_inline_entry = __ pc();
1306 gen_c2i_adapter(masm, sig_cc, regs_cc, /* requires_clinit_barrier = */ true, c2i_no_clinit_check_entry,
1307 skip_fixup, i2c_entry, oop_maps, frame_complete, frame_size_in_words, /* alloc_inline_receiver = */ true);
1308
1309 // Non-scalarized c2i adapter
1310 if (regs != regs_cc) {
1311 c2i_unverified_inline_entry = __ pc();
1312 Label inline_entry_skip_fixup;
1313 gen_inline_cache_check(masm, inline_entry_skip_fixup);
1314
1315 c2i_inline_entry = __ pc();
1316 gen_c2i_adapter(masm, sig, regs, /* requires_clinit_barrier = */ true, c2i_no_clinit_check_entry,
1317 inline_entry_skip_fixup, i2c_entry, oop_maps, frame_complete, frame_size_in_words, /* alloc_inline_receiver = */ false);
1318 }
1319
1320
1321 // The c2i adapters might safepoint and trigger a GC. The caller must make sure that
1322 // the GC knows about the location of oop argument locations passed to the c2i adapter.
1323 if (allocate_code_blob) {
1324 bool caller_must_gc_arguments = (regs != regs_cc);
1325 new_adapter = AdapterBlob::create(masm->code(), frame_complete, frame_size_in_words, oop_maps, caller_must_gc_arguments);
1326 }
1327
1328 return AdapterHandlerLibrary::new_entry(fingerprint, i2c_entry, c2i_entry, c2i_inline_entry, c2i_inline_ro_entry, c2i_unverified_entry, c2i_unverified_inline_entry, c2i_no_clinit_check_entry);
1329 }
1330
1331 int SharedRuntime::c_calling_convention(const BasicType *sig_bt,
1332 VMRegPair *regs,
1333 VMRegPair *regs2,
1334 int total_args_passed) {
1335 assert(regs2 == nullptr, "not needed on x86");
1336 // We return the amount of VMRegImpl stack slots we need to reserve for all
1337 // the arguments NOT counting out_preserve_stack_slots.
1338
1339 // NOTE: These arrays will have to change when c1 is ported
1340 #ifdef _WIN64
1341 static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {
1342 c_rarg0, c_rarg1, c_rarg2, c_rarg3
1343 };
1344 static const XMMRegister FP_ArgReg[Argument::n_float_register_parameters_c] = {
1345 c_farg0, c_farg1, c_farg2, c_farg3
1346 };
1347 #else
1348 static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {
1366 case T_BYTE:
1367 case T_SHORT:
1368 case T_INT:
1369 if (int_args < Argument::n_int_register_parameters_c) {
1370 regs[i].set1(INT_ArgReg[int_args++]->as_VMReg());
1371 #ifdef _WIN64
1372 fp_args++;
1373 // Allocate slots for callee to stuff register args the stack.
1374 stk_args += 2;
1375 #endif
1376 } else {
1377 regs[i].set1(VMRegImpl::stack2reg(stk_args));
1378 stk_args += 2;
1379 }
1380 break;
1381 case T_LONG:
1382 assert((i + 1) < total_args_passed && sig_bt[i + 1] == T_VOID, "expecting half");
1383 // fall through
1384 case T_OBJECT:
1385 case T_ARRAY:
1386 case T_PRIMITIVE_OBJECT:
1387 case T_ADDRESS:
1388 case T_METADATA:
1389 if (int_args < Argument::n_int_register_parameters_c) {
1390 regs[i].set2(INT_ArgReg[int_args++]->as_VMReg());
1391 #ifdef _WIN64
1392 fp_args++;
1393 stk_args += 2;
1394 #endif
1395 } else {
1396 regs[i].set2(VMRegImpl::stack2reg(stk_args));
1397 stk_args += 2;
1398 }
1399 break;
1400 case T_FLOAT:
1401 if (fp_args < Argument::n_float_register_parameters_c) {
1402 regs[i].set1(FP_ArgReg[fp_args++]->as_VMReg());
1403 #ifdef _WIN64
1404 int_args++;
1405 // Allocate slots for callee to stuff register args the stack.
1406 stk_args += 2;
2293
2294 int temploc = -1;
2295 for (int ai = 0; ai < arg_order.length(); ai += 2) {
2296 int i = arg_order.at(ai);
2297 int c_arg = arg_order.at(ai + 1);
2298 __ block_comment(err_msg("move %d -> %d", i, c_arg));
2299 #ifdef ASSERT
2300 if (in_regs[i].first()->is_Register()) {
2301 assert(!reg_destroyed[in_regs[i].first()->as_Register()->encoding()], "destroyed reg!");
2302 } else if (in_regs[i].first()->is_XMMRegister()) {
2303 assert(!freg_destroyed[in_regs[i].first()->as_XMMRegister()->encoding()], "destroyed reg!");
2304 }
2305 if (out_regs[c_arg].first()->is_Register()) {
2306 reg_destroyed[out_regs[c_arg].first()->as_Register()->encoding()] = true;
2307 } else if (out_regs[c_arg].first()->is_XMMRegister()) {
2308 freg_destroyed[out_regs[c_arg].first()->as_XMMRegister()->encoding()] = true;
2309 }
2310 #endif /* ASSERT */
2311 switch (in_sig_bt[i]) {
2312 case T_ARRAY:
2313 case T_PRIMITIVE_OBJECT:
2314 case T_OBJECT:
2315 __ object_move(map, oop_handle_offset, stack_slots, in_regs[i], out_regs[c_arg],
2316 ((i == 0) && (!is_static)),
2317 &receiver_offset);
2318 break;
2319 case T_VOID:
2320 break;
2321
2322 case T_FLOAT:
2323 __ float_move(in_regs[i], out_regs[c_arg]);
2324 break;
2325
2326 case T_DOUBLE:
2327 assert( i + 1 < total_in_args &&
2328 in_sig_bt[i + 1] == T_VOID &&
2329 out_sig_bt[c_arg+1] == T_VOID, "bad arg list");
2330 __ double_move(in_regs[i], out_regs[c_arg]);
2331 break;
2332
2333 case T_LONG :
2420 const int mark_word_offset = BasicLock::displaced_header_offset_in_bytes();
2421
2422 // Get the handle (the 2nd argument)
2423 __ mov(oop_handle_reg, c_rarg1);
2424
2425 // Get address of the box
2426
2427 __ lea(lock_reg, Address(rsp, lock_slot_offset * VMRegImpl::stack_slot_size));
2428
2429 // Load the oop from the handle
2430 __ movptr(obj_reg, Address(oop_handle_reg, 0));
2431
2432 if (LockingMode == LM_MONITOR) {
2433 __ jmp(slow_path_lock);
2434 } else if (LockingMode == LM_LEGACY) {
2435 // Load immediate 1 into swap_reg %rax
2436 __ movl(swap_reg, 1);
2437
2438 // Load (object->mark() | 1) into swap_reg %rax
2439 __ orptr(swap_reg, Address(obj_reg, oopDesc::mark_offset_in_bytes()));
2440 if (EnableValhalla) {
2441 // Mask inline_type bit such that we go to the slow path if object is an inline type
2442 __ andptr(swap_reg, ~((int) markWord::inline_type_bit_in_place));
2443 }
2444
2445 // Save (object->mark() | 1) into BasicLock's displaced header
2446 __ movptr(Address(lock_reg, mark_word_offset), swap_reg);
2447
2448 // src -> dest iff dest == rax else rax <- dest
2449 __ lock();
2450 __ cmpxchgptr(lock_reg, Address(obj_reg, oopDesc::mark_offset_in_bytes()));
2451 __ jcc(Assembler::equal, count_mon);
2452
2453 // Hmm should this move to the slow path code area???
2454
2455 // Test if the oopMark is an obvious stack pointer, i.e.,
2456 // 1) (mark & 3) == 0, and
2457 // 2) rsp <= mark < mark + os::pagesize()
2458 // These 3 tests can be done by evaluating the following
2459 // expression: ((mark - rsp) & (3 - os::vm_page_size())),
2460 // assuming both stack pointer and pagesize have their
2461 // least significant 2 bits clear.
2462 // NOTE: the oopMark is in swap_reg %rax as the result of cmpxchg
2463
2488 // Now set thread in native
2489 __ movl(Address(r15_thread, JavaThread::thread_state_offset()), _thread_in_native);
2490
2491 __ call(RuntimeAddress(native_func));
2492
2493 // Verify or restore cpu control state after JNI call
2494 __ restore_cpu_control_state_after_jni(rscratch1);
2495
2496 // Unpack native results.
2497 switch (ret_type) {
2498 case T_BOOLEAN: __ c2bool(rax); break;
2499 case T_CHAR : __ movzwl(rax, rax); break;
2500 case T_BYTE : __ sign_extend_byte (rax); break;
2501 case T_SHORT : __ sign_extend_short(rax); break;
2502 case T_INT : /* nothing to do */ break;
2503 case T_DOUBLE :
2504 case T_FLOAT :
2505 // Result is in xmm0 we'll save as needed
2506 break;
2507 case T_ARRAY: // Really a handle
2508 case T_PRIMITIVE_OBJECT: // Really a handle
2509 case T_OBJECT: // Really a handle
2510 break; // can't de-handlize until after safepoint check
2511 case T_VOID: break;
2512 case T_LONG: break;
2513 default : ShouldNotReachHere();
2514 }
2515
2516 Label after_transition;
2517
2518 // Switch thread to "native transition" state before reading the synchronization state.
2519 // This additional state is necessary because reading and testing the synchronization
2520 // state is not atomic w.r.t. GC, as this scenario demonstrates:
2521 // Java thread A, in _thread_in_native state, loads _not_synchronized and is preempted.
2522 // VM thread changes sync state to synchronizing and suspends threads for GC.
2523 // Thread A is resumed to finish this native method, but doesn't block here since it
2524 // didn't see any synchronization is progress, and escapes.
2525 __ movl(Address(r15_thread, JavaThread::thread_state_offset()), _thread_in_native_trans);
2526
2527 // Force this write out before the read below
2528 if (!UseSystemMemoryBarrier) {
3986 __ movptr(Address(r15_thread, JavaThread::exception_pc_offset()), NULL_WORD);
3987 #endif
3988 // Clear the exception oop so GC no longer processes it as a root.
3989 __ movptr(Address(r15_thread, JavaThread::exception_oop_offset()), NULL_WORD);
3990
3991 // rax: exception oop
3992 // r8: exception handler
3993 // rdx: exception pc
3994 // Jump to handler
3995
3996 __ jmp(r8);
3997
3998 // Make sure all code is generated
3999 masm->flush();
4000
4001 // Set exception blob
4002 _exception_blob = ExceptionBlob::create(&buffer, oop_maps, SimpleRuntimeFrame::framesize >> 1);
4003 }
4004 #endif // COMPILER2
4005
4006 BufferedInlineTypeBlob* SharedRuntime::generate_buffered_inline_type_adapter(const InlineKlass* vk) {
4007 BufferBlob* buf = BufferBlob::create("inline types pack/unpack", 16 * K);
4008 CodeBuffer buffer(buf);
4009 short buffer_locs[20];
4010 buffer.insts()->initialize_shared_locs((relocInfo*)buffer_locs,
4011 sizeof(buffer_locs)/sizeof(relocInfo));
4012
4013 MacroAssembler* masm = new MacroAssembler(&buffer);
4014
4015 const Array<SigEntry>* sig_vk = vk->extended_sig();
4016 const Array<VMRegPair>* regs = vk->return_regs();
4017
4018 int pack_fields_jobject_off = __ offset();
4019 // Resolve pre-allocated buffer from JNI handle.
4020 // We cannot do this in generate_call_stub() because it requires GC code to be initialized.
4021 __ movptr(rax, Address(r13, 0));
4022 __ resolve_jobject(rax /* value */,
4023 r15_thread /* thread */,
4024 r12 /* tmp */);
4025 __ movptr(Address(r13, 0), rax);
4026
4027 int pack_fields_off = __ offset();
4028
4029 int j = 1;
4030 for (int i = 0; i < sig_vk->length(); i++) {
4031 BasicType bt = sig_vk->at(i)._bt;
4032 if (bt == T_PRIMITIVE_OBJECT) {
4033 continue;
4034 }
4035 if (bt == T_VOID) {
4036 if (sig_vk->at(i-1)._bt == T_LONG ||
4037 sig_vk->at(i-1)._bt == T_DOUBLE) {
4038 j++;
4039 }
4040 continue;
4041 }
4042 int off = sig_vk->at(i)._offset;
4043 assert(off > 0, "offset in object should be positive");
4044 VMRegPair pair = regs->at(j);
4045 VMReg r_1 = pair.first();
4046 VMReg r_2 = pair.second();
4047 Address to(rax, off);
4048 if (bt == T_FLOAT) {
4049 __ movflt(to, r_1->as_XMMRegister());
4050 } else if (bt == T_DOUBLE) {
4051 __ movdbl(to, r_1->as_XMMRegister());
4052 } else {
4053 Register val = r_1->as_Register();
4054 assert_different_registers(to.base(), val, r14, r13, rbx, rscratch1);
4055 if (is_reference_type(bt)) {
4056 __ store_heap_oop(to, val, r14, r13, rbx, IN_HEAP | ACCESS_WRITE | IS_DEST_UNINITIALIZED);
4057 } else {
4058 __ store_sized_value(to, r_1->as_Register(), type2aelembytes(bt));
4059 }
4060 }
4061 j++;
4062 }
4063 assert(j == regs->length(), "missed a field?");
4064
4065 __ ret(0);
4066
4067 int unpack_fields_off = __ offset();
4068
4069 Label skip;
4070 __ testptr(rax, rax);
4071 __ jcc(Assembler::zero, skip);
4072
4073 j = 1;
4074 for (int i = 0; i < sig_vk->length(); i++) {
4075 BasicType bt = sig_vk->at(i)._bt;
4076 if (bt == T_PRIMITIVE_OBJECT) {
4077 continue;
4078 }
4079 if (bt == T_VOID) {
4080 if (sig_vk->at(i-1)._bt == T_LONG ||
4081 sig_vk->at(i-1)._bt == T_DOUBLE) {
4082 j++;
4083 }
4084 continue;
4085 }
4086 int off = sig_vk->at(i)._offset;
4087 assert(off > 0, "offset in object should be positive");
4088 VMRegPair pair = regs->at(j);
4089 VMReg r_1 = pair.first();
4090 VMReg r_2 = pair.second();
4091 Address from(rax, off);
4092 if (bt == T_FLOAT) {
4093 __ movflt(r_1->as_XMMRegister(), from);
4094 } else if (bt == T_DOUBLE) {
4095 __ movdbl(r_1->as_XMMRegister(), from);
4096 } else if (bt == T_OBJECT || bt == T_ARRAY) {
4097 assert_different_registers(rax, r_1->as_Register());
4098 __ load_heap_oop(r_1->as_Register(), from);
4099 } else {
4100 assert(is_java_primitive(bt), "unexpected basic type");
4101 assert_different_registers(rax, r_1->as_Register());
4102 size_t size_in_bytes = type2aelembytes(bt);
4103 __ load_sized_value(r_1->as_Register(), from, size_in_bytes, bt != T_CHAR && bt != T_BOOLEAN);
4104 }
4105 j++;
4106 }
4107 assert(j == regs->length(), "missed a field?");
4108
4109 __ bind(skip);
4110 __ ret(0);
4111
4112 __ flush();
4113
4114 return BufferedInlineTypeBlob::create(&buffer, pack_fields_off, pack_fields_jobject_off, unpack_fields_off);
4115 }
|