10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #ifndef _WINDOWS
26 #include "alloca.h"
27 #endif
28 #include "asm/macroAssembler.hpp"
29 #include "asm/macroAssembler.inline.hpp"
30 #include "code/aotCodeCache.hpp"
31 #include "code/compiledIC.hpp"
32 #include "code/debugInfoRec.hpp"
33 #include "code/nativeInst.hpp"
34 #include "code/vtableStubs.hpp"
35 #include "compiler/oopMap.hpp"
36 #include "gc/shared/collectedHeap.hpp"
37 #include "gc/shared/gcLocker.hpp"
38 #include "gc/shared/barrierSet.hpp"
39 #include "gc/shared/barrierSetAssembler.hpp"
40 #include "interpreter/interpreter.hpp"
41 #include "logging/log.hpp"
42 #include "memory/resourceArea.hpp"
43 #include "memory/universe.hpp"
44 #include "oops/klass.inline.hpp"
45 #include "oops/method.inline.hpp"
46 #include "prims/methodHandles.hpp"
47 #include "runtime/continuation.hpp"
48 #include "runtime/continuationEntry.inline.hpp"
49 #include "runtime/globals.hpp"
617 break;
618 case T_DOUBLE:
619 assert((i + 1) < total_args_passed && sig_bt[i + 1] == T_VOID, "expecting half");
620 if (fp_args < Argument::n_float_register_parameters_j) {
621 regs[i].set2(FP_ArgReg[fp_args++]->as_VMReg());
622 } else {
623 stk_args = align_up(stk_args, 2);
624 regs[i].set2(VMRegImpl::stack2reg(stk_args));
625 stk_args += 2;
626 }
627 break;
628 default:
629 ShouldNotReachHere();
630 break;
631 }
632 }
633
634 return stk_args;
635 }
636
637 // Patch the callers callsite with entry to compiled code if it exists.
638 static void patch_callers_callsite(MacroAssembler *masm) {
639 Label L;
640 __ cmpptr(Address(rbx, in_bytes(Method::code_offset())), NULL_WORD);
641 __ jcc(Assembler::equal, L);
642
643 // Save the current stack pointer
644 __ mov(r13, rsp);
645 // Schedule the branch target address early.
646 // Call into the VM to patch the caller, then jump to compiled callee
647 // rax isn't live so capture return address while we easily can
648 __ movptr(rax, Address(rsp, 0));
649
650 // align stack so push_CPU_state doesn't fault
651 __ andptr(rsp, -(StackAlignmentInBytes));
652 __ push_CPU_state();
653 __ vzeroupper();
654 // VM needs caller's callsite
655 // VM needs target method
656 // This needs to be a long call since we will relocate this adapter to
659 // Allocate argument register save area
660 if (frame::arg_reg_save_area_bytes != 0) {
661 __ subptr(rsp, frame::arg_reg_save_area_bytes);
662 }
663 __ mov(c_rarg0, rbx);
664 __ mov(c_rarg1, rax);
665 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::fixup_callers_callsite)));
666
667 // De-allocate argument register save area
668 if (frame::arg_reg_save_area_bytes != 0) {
669 __ addptr(rsp, frame::arg_reg_save_area_bytes);
670 }
671
672 __ vzeroupper();
673 __ pop_CPU_state();
674 // restore sp
675 __ mov(rsp, r13);
676 __ bind(L);
677 }
678
679 static void gen_c2i_adapter(MacroAssembler *masm,
680 int total_args_passed,
681 int comp_args_on_stack,
682 const BasicType *sig_bt,
683 const VMRegPair *regs,
684 Label& skip_fixup) {
685 // Before we get into the guts of the C2I adapter, see if we should be here
686 // at all. We've come from compiled code and are attempting to jump to the
687 // interpreter, which means the caller made a static call to get here
688 // (vcalls always get a compiled target if there is one). Check for a
689 // compiled target. If there is one, we need to patch the caller's call.
690 patch_callers_callsite(masm);
691
692 __ bind(skip_fixup);
693
694 // Since all args are passed on the stack, total_args_passed *
695 // Interpreter::stackElementSize is the space we need.
696
697 assert(total_args_passed >= 0, "total_args_passed is %d", total_args_passed);
698
699 int extraspace = (total_args_passed * Interpreter::stackElementSize);
700
701 // stack is aligned, keep it that way
702 // This is not currently needed or enforced by the interpreter, but
703 // we might as well conform to the ABI.
704 extraspace = align_up(extraspace, 2*wordSize);
705
706 // set senderSP value
707 __ lea(r13, Address(rsp, wordSize));
708
709 #ifdef ASSERT
710 __ check_stack_alignment(r13, "sender stack not aligned");
711 #endif
712 if (extraspace > 0) {
713 // Pop the return address
714 __ pop(rax);
715
716 __ subptr(rsp, extraspace);
717
718 // Push the return address
719 __ push(rax);
720
721 // Account for the return address location since we store it first rather
722 // than hold it in a register across all the shuffling
723 extraspace += wordSize;
724 }
725
726 #ifdef ASSERT
727 __ check_stack_alignment(rsp, "callee stack not aligned", wordSize, rax);
728 #endif
729
730 // Now write the args into the outgoing interpreter space
731 for (int i = 0; i < total_args_passed; i++) {
732 if (sig_bt[i] == T_VOID) {
733 assert(i > 0 && (sig_bt[i-1] == T_LONG || sig_bt[i-1] == T_DOUBLE), "missing half");
734 continue;
735 }
736
737 // offset to start parameters
738 int st_off = (total_args_passed - i) * Interpreter::stackElementSize;
739 int next_off = st_off - Interpreter::stackElementSize;
740
741 // Say 4 args:
742 // i st_off
743 // 0 32 T_LONG
744 // 1 24 T_VOID
745 // 2 16 T_OBJECT
746 // 3 8 T_BOOL
747 // - 0 return address
748 //
749 // However to make thing extra confusing. Because we can fit a long/double in
750 // a single slot on a 64 bt vm and it would be silly to break them up, the interpreter
751 // leaves one slot empty and only stores to a single slot. In this case the
752 // slot that is occupied is the T_VOID slot. See I said it was confusing.
753
754 VMReg r_1 = regs[i].first();
755 VMReg r_2 = regs[i].second();
756 if (!r_1->is_valid()) {
757 assert(!r_2->is_valid(), "");
758 continue;
759 }
760 if (r_1->is_stack()) {
761 // memory to memory use rax
762 int ld_off = r_1->reg2stack() * VMRegImpl::stack_slot_size + extraspace;
763 if (!r_2->is_valid()) {
764 // sign extend??
765 __ movl(rax, Address(rsp, ld_off));
766 __ movptr(Address(rsp, st_off), rax);
767
768 } else {
769
770 __ movq(rax, Address(rsp, ld_off));
771
772 // Two VMREgs|OptoRegs can be T_OBJECT, T_ADDRESS, T_DOUBLE, T_LONG
773 // T_DOUBLE and T_LONG use two slots in the interpreter
774 if ( sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) {
775 // ld_off == LSW, ld_off+wordSize == MSW
776 // st_off == MSW, next_off == LSW
777 __ movq(Address(rsp, next_off), rax);
778 #ifdef ASSERT
779 // Overwrite the unused slot with known junk
780 __ mov64(rax, CONST64(0xdeadffffdeadaaaa));
781 __ movptr(Address(rsp, st_off), rax);
782 #endif /* ASSERT */
783 } else {
784 __ movq(Address(rsp, st_off), rax);
785 }
786 }
787 } else if (r_1->is_Register()) {
788 Register r = r_1->as_Register();
789 if (!r_2->is_valid()) {
790 // must be only an int (or less ) so move only 32bits to slot
791 // why not sign extend??
792 __ movl(Address(rsp, st_off), r);
793 } else {
794 // Two VMREgs|OptoRegs can be T_OBJECT, T_ADDRESS, T_DOUBLE, T_LONG
795 // T_DOUBLE and T_LONG use two slots in the interpreter
796 if ( sig_bt[i] == T_LONG || sig_bt[i] == T_DOUBLE) {
797 // long/double in gpr
798 #ifdef ASSERT
799 // Overwrite the unused slot with known junk
800 __ mov64(rax, CONST64(0xdeadffffdeadaaab));
801 __ movptr(Address(rsp, st_off), rax);
802 #endif /* ASSERT */
803 __ movq(Address(rsp, next_off), r);
804 } else {
805 __ movptr(Address(rsp, st_off), r);
806 }
807 }
808 } else {
809 assert(r_1->is_XMMRegister(), "");
810 if (!r_2->is_valid()) {
811 // only a float use just part of the slot
812 __ movflt(Address(rsp, st_off), r_1->as_XMMRegister());
813 } else {
814 #ifdef ASSERT
815 // Overwrite the unused slot with known junk
816 __ mov64(rax, CONST64(0xdeadffffdeadaaac));
817 __ movptr(Address(rsp, st_off), rax);
818 #endif /* ASSERT */
819 __ movdbl(Address(rsp, next_off), r_1->as_XMMRegister());
820 }
821 }
822 }
823
824 // Schedule the branch target address early.
825 __ movptr(rcx, Address(rbx, in_bytes(Method::interpreter_entry_offset())));
826 __ jmp(rcx);
827 }
828
829 void SharedRuntime::gen_i2c_adapter(MacroAssembler *masm,
830 int total_args_passed,
831 int comp_args_on_stack,
832 const BasicType *sig_bt,
833 const VMRegPair *regs) {
834
835 // Note: r13 contains the senderSP on entry. We must preserve it since
836 // we may do a i2c -> c2i transition if we lose a race where compiled
837 // code goes non-entrant while we get args ready.
838 // In addition we use r13 to locate all the interpreter args as
839 // we must align the stack to 16 bytes on an i2c entry else we
840 // lose alignment we expect in all compiled code and register
841 // save code can segv when fxsave instructions find improperly
842 // aligned stack pointer.
843
844 // Adapters can be frameless because they do not require the caller
845 // to perform additional cleanup work, such as correcting the stack pointer.
846 // An i2c adapter is frameless because the *caller* frame, which is interpreted,
847 // routinely repairs its own stack pointer (from interpreter_frame_last_sp),
848 // even if a callee has modified the stack pointer.
849 // A c2i adapter is frameless because the *callee* frame, which is interpreted,
850 // routinely repairs its caller's stack pointer (from sender_sp, which is set
851 // up via the senderSP register).
852 // In other words, if *either* the caller or callee is interpreted, we can
868 // Convert 4-byte c2 stack slots to words.
869 int comp_words_on_stack = align_up(comp_args_on_stack*VMRegImpl::stack_slot_size, wordSize)>>LogBytesPerWord;
870
871 if (comp_args_on_stack) {
872 __ subptr(rsp, comp_words_on_stack * wordSize);
873 }
874
875 // Ensure compiled code always sees stack at proper alignment
876 __ andptr(rsp, -16);
877
878 // push the return address and misalign the stack that youngest frame always sees
879 // as far as the placement of the call instruction
880 __ push(rax);
881
882 // Put saved SP in another register
883 const Register saved_sp = rax;
884 __ movptr(saved_sp, r11);
885
886 // Will jump to the compiled code just as if compiled code was doing it.
887 // Pre-load the register-jump target early, to schedule it better.
888 __ movptr(r11, Address(rbx, in_bytes(Method::from_compiled_offset())));
889
890 #if INCLUDE_JVMCI
891 if (EnableJVMCI) {
892 // check if this call should be routed towards a specific entry point
893 __ cmpptr(Address(r15_thread, in_bytes(JavaThread::jvmci_alternate_call_target_offset())), 0);
894 Label no_alternative_target;
895 __ jcc(Assembler::equal, no_alternative_target);
896 __ movptr(r11, Address(r15_thread, in_bytes(JavaThread::jvmci_alternate_call_target_offset())));
897 __ movptr(Address(r15_thread, in_bytes(JavaThread::jvmci_alternate_call_target_offset())), 0);
898 __ bind(no_alternative_target);
899 }
900 #endif // INCLUDE_JVMCI
901
902 // Now generate the shuffle code. Pick up all register args and move the
903 // rest through the floating point stack top.
904 for (int i = 0; i < total_args_passed; i++) {
905 if (sig_bt[i] == T_VOID) {
906 // Longs and doubles are passed in native word order, but misaligned
907 // in the 32-bit build.
908 assert(i > 0 && (sig_bt[i-1] == T_LONG || sig_bt[i-1] == T_DOUBLE), "missing half");
909 continue;
910 }
911
912 // Pick up 0, 1 or 2 words from SP+offset.
913
914 assert(!regs[i].second()->is_valid() || regs[i].first()->next() == regs[i].second(),
915 "scrambled load targets?");
916 // Load in argument order going down.
917 int ld_off = (total_args_passed - i)*Interpreter::stackElementSize;
918 // Point to interpreter value (vs. tag)
919 int next_off = ld_off - Interpreter::stackElementSize;
920 //
921 //
922 //
923 VMReg r_1 = regs[i].first();
924 VMReg r_2 = regs[i].second();
925 if (!r_1->is_valid()) {
926 assert(!r_2->is_valid(), "");
927 continue;
928 }
930 // Convert stack slot to an SP offset (+ wordSize to account for return address )
931 int st_off = regs[i].first()->reg2stack()*VMRegImpl::stack_slot_size + wordSize;
932
933 // We can use r13 as a temp here because compiled code doesn't need r13 as an input
934 // and if we end up going thru a c2i because of a miss a reasonable value of r13
935 // will be generated.
936 if (!r_2->is_valid()) {
937 // sign extend???
938 __ movl(r13, Address(saved_sp, ld_off));
939 __ movptr(Address(rsp, st_off), r13);
940 } else {
941 //
942 // We are using two optoregs. This can be either T_OBJECT, T_ADDRESS, T_LONG, or T_DOUBLE
943 // the interpreter allocates two slots but only uses one for thr T_LONG or T_DOUBLE case
944 // So we must adjust where to pick up the data to match the interpreter.
945 //
946 // Interpreter local[n] == MSW, local[n+1] == LSW however locals
947 // are accessed as negative so LSW is at LOW address
948
949 // ld_off is MSW so get LSW
950 const int offset = (sig_bt[i]==T_LONG||sig_bt[i]==T_DOUBLE)?
951 next_off : ld_off;
952 __ movq(r13, Address(saved_sp, offset));
953 // st_off is LSW (i.e. reg.first())
954 __ movq(Address(rsp, st_off), r13);
955 }
956 } else if (r_1->is_Register()) { // Register argument
957 Register r = r_1->as_Register();
958 assert(r != rax, "must be different");
959 if (r_2->is_valid()) {
960 //
961 // We are using two VMRegs. This can be either T_OBJECT, T_ADDRESS, T_LONG, or T_DOUBLE
962 // the interpreter allocates two slots but only uses one for thr T_LONG or T_DOUBLE case
963 // So we must adjust where to pick up the data to match the interpreter.
964
965 const int offset = (sig_bt[i]==T_LONG||sig_bt[i]==T_DOUBLE)?
966 next_off : ld_off;
967
968 // this can be a misaligned move
969 __ movq(r, Address(saved_sp, offset));
970 } else {
971 // sign extend and use a full word?
972 __ movl(r, Address(saved_sp, ld_off));
973 }
974 } else {
975 if (!r_2->is_valid()) {
976 __ movflt(r_1->as_XMMRegister(), Address(saved_sp, ld_off));
977 } else {
978 __ movdbl(r_1->as_XMMRegister(), Address(saved_sp, next_off));
979 }
980 }
981 }
982
983 __ push_cont_fastpath(); // Set JavaThread::_cont_fastpath to the sp of the oldest interpreted frame we know about
984
985 // 6243940 We might end up in handle_wrong_method if
986 // the callee is deoptimized as we race thru here. If that
987 // happens we don't want to take a safepoint because the
988 // caller frame will look interpreted and arguments are now
989 // "compiled" so it is much better to make this transition
990 // invisible to the stack walking code. Unfortunately if
991 // we try and find the callee by normal means a safepoint
992 // is possible. So we stash the desired callee in the thread
993 // and the vm will find there should this case occur.
994
995 __ movptr(Address(r15_thread, JavaThread::callee_target_offset()), rbx);
996
997 // put Method* where a c2i would expect should we end up there
998 // only needed because eof c2 resolve stubs return Method* as a result in
999 // rax
1000 __ mov(rax, rbx);
1001 __ jmp(r11);
1002 }
1003
1004 // ---------------------------------------------------------------
1005 void SharedRuntime::generate_i2c2i_adapters(MacroAssembler *masm,
1006 int total_args_passed,
1007 int comp_args_on_stack,
1008 const BasicType *sig_bt,
1009 const VMRegPair *regs,
1010 address entry_address[AdapterBlob::ENTRY_COUNT]) {
1011 entry_address[AdapterBlob::I2C] = __ pc();
1012
1013 gen_i2c_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs);
1014
1015 // -------------------------------------------------------------------------
1016 // Generate a C2I adapter. On entry we know rbx holds the Method* during calls
1017 // to the interpreter. The args start out packed in the compiled layout. They
1018 // need to be unpacked into the interpreter layout. This will almost always
1019 // require some stack space. We grow the current (compiled) stack, then repack
1020 // the args. We finally end in a jump to the generic interpreter entry point.
1021 // On exit from the interpreter, the interpreter will restore our SP (lest the
1022 // compiled code, which relies solely on SP and not RBP, get sick).
1023
1024 entry_address[AdapterBlob::C2I_Unverified] = __ pc();
1025 Label skip_fixup;
1026
1027 Register data = rax;
1028 Register receiver = j_rarg0;
1029 Register temp = rbx;
1030
1031 {
1032 __ ic_check(1 /* end_alignment */);
1033 __ movptr(rbx, Address(data, CompiledICData::speculated_method_offset()));
1034 // Method might have been compiled since the call site was patched to
1035 // interpreted if that is the case treat it as a miss so we can get
1036 // the call site corrected.
1037 __ cmpptr(Address(rbx, in_bytes(Method::code_offset())), NULL_WORD);
1038 __ jcc(Assembler::equal, skip_fixup);
1039 __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
1040 }
1041
1042 entry_address[AdapterBlob::C2I] = __ pc();
1043
1044 // Class initialization barrier for static methods
1045 entry_address[AdapterBlob::C2I_No_Clinit_Check] = nullptr;
1046 assert(VM_Version::supports_fast_class_init_checks(), "sanity");
1047 Label L_skip_barrier;
1048 Register method = rbx;
1049
1050 // Bypass the barrier for non-static methods
1051 Register flags = rscratch1;
1052 __ load_unsigned_short(flags, Address(method, Method::access_flags_offset()));
1053 __ testl(flags, JVM_ACC_STATIC);
1054 __ jcc(Assembler::zero, L_skip_barrier); // non-static
1055
1056 Register klass = rscratch1;
1057 __ load_method_holder(klass, method);
1058 __ clinit_barrier(klass, &L_skip_barrier /*L_fast_path*/);
1059
1060 __ jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub())); // slow path
1061
1062 __ bind(L_skip_barrier);
1063 entry_address[AdapterBlob::C2I_No_Clinit_Check] = __ pc();
1064
1065 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
1066 bs->c2i_entry_barrier(masm);
1067
1068 gen_c2i_adapter(masm, total_args_passed, comp_args_on_stack, sig_bt, regs, skip_fixup);
1069 return;
1070 }
1071
1072 int SharedRuntime::c_calling_convention(const BasicType *sig_bt,
1073 VMRegPair *regs,
1074 int total_args_passed) {
1075
1076 // We return the amount of VMRegImpl stack slots we need to reserve for all
1077 // the arguments NOT counting out_preserve_stack_slots.
1078
1079 // NOTE: These arrays will have to change when c1 is ported
1080 #ifdef _WIN64
1081 static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {
1082 c_rarg0, c_rarg1, c_rarg2, c_rarg3
1083 };
1084 static const XMMRegister FP_ArgReg[Argument::n_float_register_parameters_c] = {
1085 c_farg0, c_farg1, c_farg2, c_farg3
1086 };
1087 #else
1088 static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {
1089 c_rarg0, c_rarg1, c_rarg2, c_rarg3, c_rarg4, c_rarg5
3492 julong *scratch = (julong *)alloca(total_allocation);
3493
3494 // Local scratch arrays
3495 julong
3496 *a = scratch + 0 * longwords,
3497 *n = scratch + 1 * longwords,
3498 *m = scratch + 2 * longwords;
3499
3500 reverse_words((julong *)a_ints, a, longwords);
3501 reverse_words((julong *)n_ints, n, longwords);
3502
3503 if (len >= MONTGOMERY_SQUARING_THRESHOLD) {
3504 ::montgomery_square(a, n, m, (julong)inv, longwords);
3505 } else {
3506 ::montgomery_multiply(a, a, n, m, (julong)inv, longwords);
3507 }
3508
3509 reverse_words(m, (julong *)m_ints, longwords);
3510 }
3511
3512 #if INCLUDE_JFR
3513
3514 // For c2: c_rarg0 is junk, call to runtime to write a checkpoint.
3515 // It returns a jobject handle to the event writer.
3516 // The handle is dereferenced and the return value is the event writer oop.
3517 RuntimeStub* SharedRuntime::generate_jfr_write_checkpoint() {
3518 enum layout {
3519 rbp_off,
3520 rbpH_off,
3521 return_off,
3522 return_off2,
3523 framesize // inclusive of return address
3524 };
3525
3526 const char* name = SharedRuntime::stub_name(StubId::shared_jfr_write_checkpoint_id);
3527 CodeBuffer code(name, 1024, 64);
3528 MacroAssembler* masm = new MacroAssembler(&code);
3529 address start = __ pc();
3530
3531 __ enter();
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #ifndef _WINDOWS
26 #include "alloca.h"
27 #endif
28 #include "asm/macroAssembler.hpp"
29 #include "asm/macroAssembler.inline.hpp"
30 #include "classfile/symbolTable.hpp"
31 #include "code/aotCodeCache.hpp"
32 #include "code/compiledIC.hpp"
33 #include "code/debugInfoRec.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/klass.inline.hpp"
46 #include "oops/method.inline.hpp"
47 #include "prims/methodHandles.hpp"
48 #include "runtime/continuation.hpp"
49 #include "runtime/continuationEntry.inline.hpp"
50 #include "runtime/globals.hpp"
618 break;
619 case T_DOUBLE:
620 assert((i + 1) < total_args_passed && sig_bt[i + 1] == T_VOID, "expecting half");
621 if (fp_args < Argument::n_float_register_parameters_j) {
622 regs[i].set2(FP_ArgReg[fp_args++]->as_VMReg());
623 } else {
624 stk_args = align_up(stk_args, 2);
625 regs[i].set2(VMRegImpl::stack2reg(stk_args));
626 stk_args += 2;
627 }
628 break;
629 default:
630 ShouldNotReachHere();
631 break;
632 }
633 }
634
635 return stk_args;
636 }
637
638 // Same as java_calling_convention() but for multiple return
639 // values. There's no way to store them on the stack so if we don't
640 // have enough registers, multiple values can't be returned.
641 const uint SharedRuntime::java_return_convention_max_int = Argument::n_int_register_parameters_j+1;
642 const uint SharedRuntime::java_return_convention_max_float = Argument::n_float_register_parameters_j;
643 int SharedRuntime::java_return_convention(const BasicType *sig_bt,
644 VMRegPair *regs,
645 int total_args_passed) {
646 // Create the mapping between argument positions and
647 // registers.
648 static const Register INT_ArgReg[java_return_convention_max_int] = {
649 rax, j_rarg5, j_rarg4, j_rarg3, j_rarg2, j_rarg1, j_rarg0
650 };
651 static const XMMRegister FP_ArgReg[java_return_convention_max_float] = {
652 j_farg0, j_farg1, j_farg2, j_farg3,
653 j_farg4, j_farg5, j_farg6, j_farg7
654 };
655
656
657 uint int_args = 0;
658 uint fp_args = 0;
659
660 for (int i = 0; i < total_args_passed; i++) {
661 switch (sig_bt[i]) {
662 case T_BOOLEAN:
663 case T_CHAR:
664 case T_BYTE:
665 case T_SHORT:
666 case T_INT:
667 if (int_args < Argument::n_int_register_parameters_j+1) {
668 regs[i].set1(INT_ArgReg[int_args]->as_VMReg());
669 int_args++;
670 } else {
671 return -1;
672 }
673 break;
674 case T_VOID:
675 // halves of T_LONG or T_DOUBLE
676 assert(i != 0 && (sig_bt[i - 1] == T_LONG || sig_bt[i - 1] == T_DOUBLE), "expecting half");
677 regs[i].set_bad();
678 break;
679 case T_LONG:
680 assert(sig_bt[i + 1] == T_VOID, "expecting half");
681 // fall through
682 case T_OBJECT:
683 case T_ARRAY:
684 case T_ADDRESS:
685 case T_METADATA:
686 if (int_args < Argument::n_int_register_parameters_j+1) {
687 regs[i].set2(INT_ArgReg[int_args]->as_VMReg());
688 int_args++;
689 } else {
690 return -1;
691 }
692 break;
693 case T_FLOAT:
694 if (fp_args < Argument::n_float_register_parameters_j) {
695 regs[i].set1(FP_ArgReg[fp_args]->as_VMReg());
696 fp_args++;
697 } else {
698 return -1;
699 }
700 break;
701 case T_DOUBLE:
702 assert(sig_bt[i + 1] == T_VOID, "expecting half");
703 if (fp_args < Argument::n_float_register_parameters_j) {
704 regs[i].set2(FP_ArgReg[fp_args]->as_VMReg());
705 fp_args++;
706 } else {
707 return -1;
708 }
709 break;
710 default:
711 ShouldNotReachHere();
712 break;
713 }
714 }
715
716 return int_args + fp_args;
717 }
718
719 // Patch the callers callsite with entry to compiled code if it exists.
720 static void patch_callers_callsite(MacroAssembler *masm) {
721 Label L;
722 __ cmpptr(Address(rbx, in_bytes(Method::code_offset())), NULL_WORD);
723 __ jcc(Assembler::equal, L);
724
725 // Save the current stack pointer
726 __ mov(r13, rsp);
727 // Schedule the branch target address early.
728 // Call into the VM to patch the caller, then jump to compiled callee
729 // rax isn't live so capture return address while we easily can
730 __ movptr(rax, Address(rsp, 0));
731
732 // align stack so push_CPU_state doesn't fault
733 __ andptr(rsp, -(StackAlignmentInBytes));
734 __ push_CPU_state();
735 __ vzeroupper();
736 // VM needs caller's callsite
737 // VM needs target method
738 // This needs to be a long call since we will relocate this adapter to
741 // Allocate argument register save area
742 if (frame::arg_reg_save_area_bytes != 0) {
743 __ subptr(rsp, frame::arg_reg_save_area_bytes);
744 }
745 __ mov(c_rarg0, rbx);
746 __ mov(c_rarg1, rax);
747 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::fixup_callers_callsite)));
748
749 // De-allocate argument register save area
750 if (frame::arg_reg_save_area_bytes != 0) {
751 __ addptr(rsp, frame::arg_reg_save_area_bytes);
752 }
753
754 __ vzeroupper();
755 __ pop_CPU_state();
756 // restore sp
757 __ mov(rsp, r13);
758 __ bind(L);
759 }
760
761 // For each inline type argument, sig includes the list of fields of
762 // the inline type. This utility function computes the number of
763 // arguments for the call if inline types are passed by reference (the
764 // calling convention the interpreter expects).
765 static int compute_total_args_passed_int(const GrowableArray<SigEntry>* sig_extended) {
766 int total_args_passed = 0;
767 if (InlineTypePassFieldsAsArgs) {
768 for (int i = 0; i < sig_extended->length(); i++) {
769 BasicType bt = sig_extended->at(i)._bt;
770 if (bt == T_METADATA) {
771 // In sig_extended, an inline type argument starts with:
772 // T_METADATA, followed by the types of the fields of the
773 // inline type and T_VOID to mark the end of the value
774 // type. Inline types are flattened so, for instance, in the
775 // case of an inline type with an int field and an inline type
776 // field that itself has 2 fields, an int and a long:
777 // T_METADATA T_INT T_METADATA T_INT T_LONG T_VOID (second
778 // slot for the T_LONG) T_VOID (inner inline type) T_VOID
779 // (outer inline type)
780 total_args_passed++;
781 int vt = 1;
782 do {
783 i++;
784 BasicType bt = sig_extended->at(i)._bt;
785 BasicType prev_bt = sig_extended->at(i-1)._bt;
786 if (bt == T_METADATA) {
787 vt++;
788 } else if (bt == T_VOID &&
789 prev_bt != T_LONG &&
790 prev_bt != T_DOUBLE) {
791 vt--;
792 }
793 } while (vt != 0);
794 } else {
795 total_args_passed++;
796 }
797 }
798 } else {
799 total_args_passed = sig_extended->length();
800 }
801 return total_args_passed;
802 }
803
804
805 static void gen_c2i_adapter_helper(MacroAssembler* masm,
806 BasicType bt,
807 BasicType prev_bt,
808 size_t size_in_bytes,
809 const VMRegPair& reg_pair,
810 const Address& to,
811 int extraspace,
812 bool is_oop) {
813 if (bt == T_VOID) {
814 assert(prev_bt == T_LONG || prev_bt == T_DOUBLE, "missing half");
815 return;
816 }
817
818 // Say 4 args:
819 // i st_off
820 // 0 32 T_LONG
821 // 1 24 T_VOID
822 // 2 16 T_OBJECT
823 // 3 8 T_BOOL
824 // - 0 return address
825 //
826 // However to make thing extra confusing. Because we can fit a long/double in
827 // a single slot on a 64 bt vm and it would be silly to break them up, the interpreter
828 // leaves one slot empty and only stores to a single slot. In this case the
829 // slot that is occupied is the T_VOID slot. See I said it was confusing.
830
831 bool wide = (size_in_bytes == wordSize);
832 VMReg r_1 = reg_pair.first();
833 VMReg r_2 = reg_pair.second();
834 assert(r_2->is_valid() == wide, "invalid size");
835 if (!r_1->is_valid()) {
836 assert(!r_2->is_valid(), "must be invalid");
837 return;
838 }
839
840 if (!r_1->is_XMMRegister()) {
841 Register val = rax;
842 if (r_1->is_stack()) {
843 int ld_off = r_1->reg2stack() * VMRegImpl::stack_slot_size + extraspace;
844 __ load_sized_value(val, Address(rsp, ld_off), size_in_bytes, /* is_signed */ false);
845 } else {
846 val = r_1->as_Register();
847 }
848 assert_different_registers(to.base(), val, rscratch1);
849 if (is_oop) {
850 __ push(r13);
851 __ push(rbx);
852 // store_heap_oop transitively calls oop_store_at which corrupts to.base(). We need to keep it valid.
853 __ push(to.base());
854 __ store_heap_oop(to, val, rscratch1, r13, rbx, IN_HEAP | ACCESS_WRITE | IS_DEST_UNINITIALIZED);
855 __ pop(to.base());
856 __ pop(rbx);
857 __ pop(r13);
858 } else {
859 __ store_sized_value(to, val, size_in_bytes);
860 }
861 } else {
862 if (wide) {
863 __ movdbl(to, r_1->as_XMMRegister());
864 } else {
865 __ movflt(to, r_1->as_XMMRegister());
866 }
867 }
868 }
869
870 static void gen_c2i_adapter(MacroAssembler *masm,
871 const GrowableArray<SigEntry>* sig_extended,
872 const VMRegPair *regs,
873 bool requires_clinit_barrier,
874 address& c2i_no_clinit_check_entry,
875 Label& skip_fixup,
876 address start,
877 OopMapSet* oop_maps,
878 int& frame_complete,
879 int& frame_size_in_words,
880 bool alloc_inline_receiver) {
881 if (requires_clinit_barrier) {
882 assert(VM_Version::supports_fast_class_init_checks(), "sanity");
883 Label L_skip_barrier;
884 Register method = rbx;
885
886 { // Bypass the barrier for non-static methods
887 Register flags = rscratch1;
888 __ load_unsigned_short(flags, Address(method, Method::access_flags_offset()));
889 __ testl(flags, JVM_ACC_STATIC);
890 __ jcc(Assembler::zero, L_skip_barrier); // non-static
891 }
892
893 Register klass = rscratch1;
894 __ load_method_holder(klass, method);
895 __ clinit_barrier(klass, &L_skip_barrier /*L_fast_path*/);
896
897 __ jump(RuntimeAddress(SharedRuntime::get_handle_wrong_method_stub())); // slow path
898
899 __ bind(L_skip_barrier);
900 c2i_no_clinit_check_entry = __ pc();
901 }
902
903 BarrierSetAssembler* bs = BarrierSet::barrier_set()->barrier_set_assembler();
904 bs->c2i_entry_barrier(masm);
905
906 // Before we get into the guts of the C2I adapter, see if we should be here
907 // at all. We've come from compiled code and are attempting to jump to the
908 // interpreter, which means the caller made a static call to get here
909 // (vcalls always get a compiled target if there is one). Check for a
910 // compiled target. If there is one, we need to patch the caller's call.
911 patch_callers_callsite(masm);
912
913 __ bind(skip_fixup);
914
915 if (InlineTypePassFieldsAsArgs) {
916 // Is there an inline type argument?
917 bool has_inline_argument = false;
918 for (int i = 0; i < sig_extended->length() && !has_inline_argument; i++) {
919 has_inline_argument = (sig_extended->at(i)._bt == T_METADATA);
920 }
921 if (has_inline_argument) {
922 // There is at least a value type argument: we're coming from
923 // compiled code so we may not have buffers to back the value
924 // objects. Allocate the buffers here with a runtime call for
925 // the value arguments that needs a buffer.
926 OopMap* map = RegisterSaver::save_live_registers(masm, 0, &frame_size_in_words, /*save_wide_vectors*/ false);
927
928 frame_complete = __ offset();
929
930 __ set_last_Java_frame(noreg, noreg, nullptr, rscratch1);
931
932 __ mov(c_rarg0, r15_thread);
933 __ mov(c_rarg1, rbx);
934 __ mov64(c_rarg2, (int64_t)alloc_inline_receiver);
935 __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::allocate_inline_types)));
936
937 oop_maps->add_gc_map((int)(__ pc() - start), map);
938 __ reset_last_Java_frame(false);
939
940 RegisterSaver::restore_live_registers(masm);
941
942 Label no_exception;
943 __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), NULL_WORD);
944 __ jcc(Assembler::equal, no_exception);
945
946 __ movptr(Address(r15_thread, JavaThread::vm_result_oop_offset()), NULL_WORD);
947 __ movptr(rax, Address(r15_thread, Thread::pending_exception_offset()));
948 __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
949
950 __ bind(no_exception);
951
952 // We get an array of objects from the runtime call
953 __ get_vm_result_oop(rscratch2); // Use rscratch2 (r11) as temporary because rscratch1 (r10) is trashed by movptr()
954 __ get_vm_result_metadata(rbx); // TODO: required to keep the callee Method live?
955 }
956 }
957
958 // Since all args are passed on the stack, total_args_passed *
959 // Interpreter::stackElementSize is the space we need.
960 int total_args_passed = compute_total_args_passed_int(sig_extended);
961 assert(total_args_passed >= 0, "total_args_passed is %d", total_args_passed);
962
963 int extraspace = (total_args_passed * Interpreter::stackElementSize);
964
965 // stack is aligned, keep it that way
966 // This is not currently needed or enforced by the interpreter, but
967 // we might as well conform to the ABI.
968 extraspace = align_up(extraspace, 2*wordSize);
969
970 // set senderSP value
971 __ lea(r13, Address(rsp, wordSize));
972
973 #ifdef ASSERT
974 __ check_stack_alignment(r13, "sender stack not aligned");
975 #endif
976 if (extraspace > 0) {
977 // Pop the return address
978 __ pop(rax);
979
980 __ subptr(rsp, extraspace);
981
982 // Push the return address
983 __ push(rax);
984
985 // Account for the return address location since we store it first rather
986 // than hold it in a register across all the shuffling
987 extraspace += wordSize;
988 }
989
990 #ifdef ASSERT
991 __ check_stack_alignment(rsp, "callee stack not aligned", wordSize, rax);
992 #endif
993
994 // Now write the args into the outgoing interpreter space
995
996 // next_arg_comp is the next argument from the compiler point of
997 // view (inline type fields are passed in registers/on the stack). In
998 // sig_extended, an inline type argument starts with: T_METADATA,
999 // followed by the types of the fields of the inline type and T_VOID
1000 // to mark the end of the inline type. ignored counts the number of
1001 // T_METADATA/T_VOID. next_vt_arg is the next inline type argument:
1002 // used to get the buffer for that argument from the pool of buffers
1003 // we allocated above and want to pass to the
1004 // interpreter. next_arg_int is the next argument from the
1005 // interpreter point of view (inline types are passed by reference).
1006 for (int next_arg_comp = 0, ignored = 0, next_vt_arg = 0, next_arg_int = 0;
1007 next_arg_comp < sig_extended->length(); next_arg_comp++) {
1008 assert(ignored <= next_arg_comp, "shouldn't skip over more slots than there are arguments");
1009 assert(next_arg_int <= total_args_passed, "more arguments for the interpreter than expected?");
1010 BasicType bt = sig_extended->at(next_arg_comp)._bt;
1011 int st_off = (total_args_passed - next_arg_int) * Interpreter::stackElementSize;
1012 if (!InlineTypePassFieldsAsArgs || bt != T_METADATA) {
1013 int next_off = st_off - Interpreter::stackElementSize;
1014 const int offset = (bt == T_LONG || bt == T_DOUBLE) ? next_off : st_off;
1015 const VMRegPair reg_pair = regs[next_arg_comp-ignored];
1016 size_t size_in_bytes = reg_pair.second()->is_valid() ? 8 : 4;
1017 gen_c2i_adapter_helper(masm, bt, next_arg_comp > 0 ? sig_extended->at(next_arg_comp-1)._bt : T_ILLEGAL,
1018 size_in_bytes, reg_pair, Address(rsp, offset), extraspace, false);
1019 next_arg_int++;
1020 #ifdef ASSERT
1021 if (bt == T_LONG || bt == T_DOUBLE) {
1022 // Overwrite the unused slot with known junk
1023 __ mov64(rax, CONST64(0xdeadffffdeadaaaa));
1024 __ movptr(Address(rsp, st_off), rax);
1025 }
1026 #endif /* ASSERT */
1027 } else {
1028 ignored++;
1029 next_arg_int++;
1030 int vt = 1;
1031 // write fields we get from compiled code in registers/stack
1032 // slots to the buffer: we know we are done with that inline type
1033 // argument when we hit the T_VOID that acts as an end of inline
1034 // type delimiter for this inline type. Inline types are flattened
1035 // so we might encounter embedded inline types. Each entry in
1036 // sig_extended contains a field offset in the buffer.
1037 Label L_null;
1038 Label not_null_buffer;
1039 do {
1040 next_arg_comp++;
1041 BasicType bt = sig_extended->at(next_arg_comp)._bt;
1042 BasicType prev_bt = sig_extended->at(next_arg_comp-1)._bt;
1043 if (bt == T_METADATA) {
1044 vt++;
1045 ignored++;
1046 } else if (bt == T_VOID &&
1047 prev_bt != T_LONG &&
1048 prev_bt != T_DOUBLE) {
1049 vt--;
1050 ignored++;
1051 } else if (sig_extended->at(next_arg_comp)._vt_oop) {
1052 // buffer argument: use if non null
1053 VMReg buffer = regs[next_arg_comp-ignored].first();
1054 if (buffer->is_stack()) {
1055 int ld_off = buffer->reg2stack() * VMRegImpl::stack_slot_size + extraspace;
1056 __ movptr(r14, Address(rsp, ld_off));
1057 } else {
1058 __ movptr(r14, buffer->as_Register());
1059 }
1060 __ testptr(r14, r14);
1061 __ jcc(Assembler::notEqual, not_null_buffer);
1062 // otherwise get the buffer from the just allocated pool of buffers
1063 int index = arrayOopDesc::base_offset_in_bytes(T_OBJECT) + next_vt_arg * type2aelembytes(T_OBJECT);
1064 __ load_heap_oop(r14, Address(rscratch2, index));
1065 next_vt_arg++;
1066 } else {
1067 int off = sig_extended->at(next_arg_comp)._offset;
1068 if (off == -1) {
1069 // Nullable inline type argument, emit null check
1070 VMReg reg = regs[next_arg_comp-ignored].first();
1071 Label L_notNull;
1072 if (reg->is_stack()) {
1073 int ld_off = reg->reg2stack() * VMRegImpl::stack_slot_size + extraspace;
1074 __ testb(Address(rsp, ld_off), 1);
1075 } else {
1076 __ testb(reg->as_Register(), 1);
1077 }
1078 __ jcc(Assembler::notZero, L_notNull);
1079 __ movptr(Address(rsp, st_off), 0);
1080 __ jmp(L_null);
1081 __ bind(L_notNull);
1082 continue;
1083 }
1084 assert(off > 0, "offset in object should be positive");
1085 size_t size_in_bytes = is_java_primitive(bt) ? type2aelembytes(bt) : wordSize;
1086 bool is_oop = is_reference_type(bt);
1087 gen_c2i_adapter_helper(masm, bt, next_arg_comp > 0 ? sig_extended->at(next_arg_comp-1)._bt : T_ILLEGAL,
1088 size_in_bytes, regs[next_arg_comp-ignored], Address(r14, off), extraspace, is_oop);
1089 }
1090 } while (vt != 0);
1091 // pass the buffer to the interpreter
1092 __ bind(not_null_buffer);
1093 __ movptr(Address(rsp, st_off), r14);
1094 __ bind(L_null);
1095 }
1096 }
1097
1098 // Schedule the branch target address early.
1099 __ movptr(rcx, Address(rbx, in_bytes(Method::interpreter_entry_offset())));
1100 __ jmp(rcx);
1101 }
1102
1103 void SharedRuntime::gen_i2c_adapter(MacroAssembler *masm,
1104 int comp_args_on_stack,
1105 const GrowableArray<SigEntry>* sig,
1106 const VMRegPair *regs) {
1107
1108 // Note: r13 contains the senderSP on entry. We must preserve it since
1109 // we may do a i2c -> c2i transition if we lose a race where compiled
1110 // code goes non-entrant while we get args ready.
1111 // In addition we use r13 to locate all the interpreter args as
1112 // we must align the stack to 16 bytes on an i2c entry else we
1113 // lose alignment we expect in all compiled code and register
1114 // save code can segv when fxsave instructions find improperly
1115 // aligned stack pointer.
1116
1117 // Adapters can be frameless because they do not require the caller
1118 // to perform additional cleanup work, such as correcting the stack pointer.
1119 // An i2c adapter is frameless because the *caller* frame, which is interpreted,
1120 // routinely repairs its own stack pointer (from interpreter_frame_last_sp),
1121 // even if a callee has modified the stack pointer.
1122 // A c2i adapter is frameless because the *callee* frame, which is interpreted,
1123 // routinely repairs its caller's stack pointer (from sender_sp, which is set
1124 // up via the senderSP register).
1125 // In other words, if *either* the caller or callee is interpreted, we can
1141 // Convert 4-byte c2 stack slots to words.
1142 int comp_words_on_stack = align_up(comp_args_on_stack*VMRegImpl::stack_slot_size, wordSize)>>LogBytesPerWord;
1143
1144 if (comp_args_on_stack) {
1145 __ subptr(rsp, comp_words_on_stack * wordSize);
1146 }
1147
1148 // Ensure compiled code always sees stack at proper alignment
1149 __ andptr(rsp, -16);
1150
1151 // push the return address and misalign the stack that youngest frame always sees
1152 // as far as the placement of the call instruction
1153 __ push(rax);
1154
1155 // Put saved SP in another register
1156 const Register saved_sp = rax;
1157 __ movptr(saved_sp, r11);
1158
1159 // Will jump to the compiled code just as if compiled code was doing it.
1160 // Pre-load the register-jump target early, to schedule it better.
1161 __ movptr(r11, Address(rbx, in_bytes(Method::from_compiled_inline_offset())));
1162
1163 #if INCLUDE_JVMCI
1164 if (EnableJVMCI) {
1165 // check if this call should be routed towards a specific entry point
1166 __ cmpptr(Address(r15_thread, in_bytes(JavaThread::jvmci_alternate_call_target_offset())), 0);
1167 Label no_alternative_target;
1168 __ jcc(Assembler::equal, no_alternative_target);
1169 __ movptr(r11, Address(r15_thread, in_bytes(JavaThread::jvmci_alternate_call_target_offset())));
1170 __ movptr(Address(r15_thread, in_bytes(JavaThread::jvmci_alternate_call_target_offset())), 0);
1171 __ bind(no_alternative_target);
1172 }
1173 #endif // INCLUDE_JVMCI
1174
1175 int total_args_passed = sig->length();
1176
1177 // Now generate the shuffle code. Pick up all register args and move the
1178 // rest through the floating point stack top.
1179 for (int i = 0; i < total_args_passed; i++) {
1180 BasicType bt = sig->at(i)._bt;
1181 if (bt == T_VOID) {
1182 // Longs and doubles are passed in native word order, but misaligned
1183 // in the 32-bit build.
1184 BasicType prev_bt = (i > 0) ? sig->at(i-1)._bt : T_ILLEGAL;
1185 assert(i > 0 && (prev_bt == T_LONG || prev_bt == T_DOUBLE), "missing half");
1186 continue;
1187 }
1188
1189 // Pick up 0, 1 or 2 words from SP+offset.
1190
1191 assert(!regs[i].second()->is_valid() || regs[i].first()->next() == regs[i].second(),
1192 "scrambled load targets?");
1193 // Load in argument order going down.
1194 int ld_off = (total_args_passed - i)*Interpreter::stackElementSize;
1195 // Point to interpreter value (vs. tag)
1196 int next_off = ld_off - Interpreter::stackElementSize;
1197 //
1198 //
1199 //
1200 VMReg r_1 = regs[i].first();
1201 VMReg r_2 = regs[i].second();
1202 if (!r_1->is_valid()) {
1203 assert(!r_2->is_valid(), "");
1204 continue;
1205 }
1207 // Convert stack slot to an SP offset (+ wordSize to account for return address )
1208 int st_off = regs[i].first()->reg2stack()*VMRegImpl::stack_slot_size + wordSize;
1209
1210 // We can use r13 as a temp here because compiled code doesn't need r13 as an input
1211 // and if we end up going thru a c2i because of a miss a reasonable value of r13
1212 // will be generated.
1213 if (!r_2->is_valid()) {
1214 // sign extend???
1215 __ movl(r13, Address(saved_sp, ld_off));
1216 __ movptr(Address(rsp, st_off), r13);
1217 } else {
1218 //
1219 // We are using two optoregs. This can be either T_OBJECT, T_ADDRESS, T_LONG, or T_DOUBLE
1220 // the interpreter allocates two slots but only uses one for thr T_LONG or T_DOUBLE case
1221 // So we must adjust where to pick up the data to match the interpreter.
1222 //
1223 // Interpreter local[n] == MSW, local[n+1] == LSW however locals
1224 // are accessed as negative so LSW is at LOW address
1225
1226 // ld_off is MSW so get LSW
1227 const int offset = (bt==T_LONG||bt==T_DOUBLE)?
1228 next_off : ld_off;
1229 __ movq(r13, Address(saved_sp, offset));
1230 // st_off is LSW (i.e. reg.first())
1231 __ movq(Address(rsp, st_off), r13);
1232 }
1233 } else if (r_1->is_Register()) { // Register argument
1234 Register r = r_1->as_Register();
1235 assert(r != rax, "must be different");
1236 if (r_2->is_valid()) {
1237 //
1238 // We are using two VMRegs. This can be either T_OBJECT, T_ADDRESS, T_LONG, or T_DOUBLE
1239 // the interpreter allocates two slots but only uses one for thr T_LONG or T_DOUBLE case
1240 // So we must adjust where to pick up the data to match the interpreter.
1241
1242 const int offset = (bt==T_LONG||bt==T_DOUBLE)?
1243 next_off : ld_off;
1244
1245 // this can be a misaligned move
1246 __ movq(r, Address(saved_sp, offset));
1247 } else {
1248 // sign extend and use a full word?
1249 __ movl(r, Address(saved_sp, ld_off));
1250 }
1251 } else {
1252 if (!r_2->is_valid()) {
1253 __ movflt(r_1->as_XMMRegister(), Address(saved_sp, ld_off));
1254 } else {
1255 __ movdbl(r_1->as_XMMRegister(), Address(saved_sp, next_off));
1256 }
1257 }
1258 }
1259
1260 __ push_cont_fastpath(); // Set JavaThread::_cont_fastpath to the sp of the oldest interpreted frame we know about
1261
1262 // 6243940 We might end up in handle_wrong_method if
1263 // the callee is deoptimized as we race thru here. If that
1264 // happens we don't want to take a safepoint because the
1265 // caller frame will look interpreted and arguments are now
1266 // "compiled" so it is much better to make this transition
1267 // invisible to the stack walking code. Unfortunately if
1268 // we try and find the callee by normal means a safepoint
1269 // is possible. So we stash the desired callee in the thread
1270 // and the vm will find there should this case occur.
1271
1272 __ movptr(Address(r15_thread, JavaThread::callee_target_offset()), rbx);
1273
1274 // put Method* where a c2i would expect should we end up there
1275 // only needed because of c2 resolve stubs return Method* as a result in
1276 // rax
1277 __ mov(rax, rbx);
1278 __ jmp(r11);
1279 }
1280
1281 static void gen_inline_cache_check(MacroAssembler *masm, Label& skip_fixup) {
1282 Register data = rax;
1283 __ ic_check(1 /* end_alignment */);
1284 __ movptr(rbx, Address(data, CompiledICData::speculated_method_offset()));
1285
1286 // Method might have been compiled since the call site was patched to
1287 // interpreted if that is the case treat it as a miss so we can get
1288 // the call site corrected.
1289 __ cmpptr(Address(rbx, in_bytes(Method::code_offset())), NULL_WORD);
1290 __ jcc(Assembler::equal, skip_fixup);
1291 __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
1292 }
1293
1294 // ---------------------------------------------------------------
1295 void SharedRuntime::generate_i2c2i_adapters(MacroAssembler* masm,
1296 int comp_args_on_stack,
1297 const GrowableArray<SigEntry>* sig,
1298 const VMRegPair* regs,
1299 const GrowableArray<SigEntry>* sig_cc,
1300 const VMRegPair* regs_cc,
1301 const GrowableArray<SigEntry>* sig_cc_ro,
1302 const VMRegPair* regs_cc_ro,
1303 address entry_address[AdapterBlob::ENTRY_COUNT],
1304 AdapterBlob*& new_adapter,
1305 bool allocate_code_blob) {
1306 entry_address[AdapterBlob::I2C] = __ pc();
1307 gen_i2c_adapter(masm, comp_args_on_stack, sig, regs);
1308
1309 // -------------------------------------------------------------------------
1310 // Generate a C2I adapter. On entry we know rbx holds the Method* during calls
1311 // to the interpreter. The args start out packed in the compiled layout. They
1312 // need to be unpacked into the interpreter layout. This will almost always
1313 // require some stack space. We grow the current (compiled) stack, then repack
1314 // the args. We finally end in a jump to the generic interpreter entry point.
1315 // On exit from the interpreter, the interpreter will restore our SP (lest the
1316 // compiled code, which relies solely on SP and not RBP, get sick).
1317
1318 entry_address[AdapterBlob::C2I_Unverified] = __ pc();
1319 entry_address[AdapterBlob::C2I_Unverified_Inline] = __ pc();
1320 Label skip_fixup;
1321
1322 gen_inline_cache_check(masm, skip_fixup);
1323
1324 OopMapSet* oop_maps = new OopMapSet();
1325 int frame_complete = CodeOffsets::frame_never_safe;
1326 int frame_size_in_words = 0;
1327
1328 // Scalarized c2i adapter with non-scalarized receiver (i.e., don't pack receiver)
1329 entry_address[AdapterBlob::C2I_No_Clinit_Check] = nullptr;
1330 entry_address[AdapterBlob::C2I_Inline_RO] = __ pc();
1331 if (regs_cc != regs_cc_ro) {
1332 // No class init barrier needed because method is guaranteed to be non-static
1333 gen_c2i_adapter(masm, sig_cc_ro, regs_cc_ro, /* requires_clinit_barrier = */ false, entry_address[AdapterBlob::C2I_No_Clinit_Check],
1334 skip_fixup, entry_address[AdapterBlob::I2C], oop_maps, frame_complete, frame_size_in_words, /* alloc_inline_receiver = */ false);
1335 skip_fixup.reset();
1336 }
1337
1338 // Scalarized c2i adapter
1339 entry_address[AdapterBlob::C2I] = __ pc();
1340 entry_address[AdapterBlob::C2I_Inline] = __ pc();
1341 gen_c2i_adapter(masm, sig_cc, regs_cc, /* requires_clinit_barrier = */ true, entry_address[AdapterBlob::C2I_No_Clinit_Check],
1342 skip_fixup, entry_address[AdapterBlob::I2C], oop_maps, frame_complete, frame_size_in_words, /* alloc_inline_receiver = */ true);
1343
1344 // Non-scalarized c2i adapter
1345 if (regs != regs_cc) {
1346 entry_address[AdapterBlob::C2I_Unverified_Inline] = __ pc();
1347 Label inline_entry_skip_fixup;
1348 gen_inline_cache_check(masm, inline_entry_skip_fixup);
1349
1350 entry_address[AdapterBlob::C2I_Inline] = __ pc();
1351 gen_c2i_adapter(masm, sig, regs, /* requires_clinit_barrier = */ true, entry_address[AdapterBlob::C2I_No_Clinit_Check],
1352 inline_entry_skip_fixup, entry_address[AdapterBlob::I2C], oop_maps, frame_complete, frame_size_in_words, /* alloc_inline_receiver = */ false);
1353 }
1354
1355 // The c2i adapters might safepoint and trigger a GC. The caller must make sure that
1356 // the GC knows about the location of oop argument locations passed to the c2i adapter.
1357 if (allocate_code_blob) {
1358 bool caller_must_gc_arguments = (regs != regs_cc);
1359 int entry_offset[AdapterHandlerEntry::ENTRIES_COUNT];
1360 assert(AdapterHandlerEntry::ENTRIES_COUNT == 7, "sanity");
1361 AdapterHandlerLibrary::address_to_offset(entry_address, entry_offset);
1362 new_adapter = AdapterBlob::create(masm->code(), entry_offset, frame_complete, frame_size_in_words, oop_maps, caller_must_gc_arguments);
1363 }
1364 }
1365
1366 int SharedRuntime::c_calling_convention(const BasicType *sig_bt,
1367 VMRegPair *regs,
1368 int total_args_passed) {
1369
1370 // We return the amount of VMRegImpl stack slots we need to reserve for all
1371 // the arguments NOT counting out_preserve_stack_slots.
1372
1373 // NOTE: These arrays will have to change when c1 is ported
1374 #ifdef _WIN64
1375 static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {
1376 c_rarg0, c_rarg1, c_rarg2, c_rarg3
1377 };
1378 static const XMMRegister FP_ArgReg[Argument::n_float_register_parameters_c] = {
1379 c_farg0, c_farg1, c_farg2, c_farg3
1380 };
1381 #else
1382 static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {
1383 c_rarg0, c_rarg1, c_rarg2, c_rarg3, c_rarg4, c_rarg5
3786 julong *scratch = (julong *)alloca(total_allocation);
3787
3788 // Local scratch arrays
3789 julong
3790 *a = scratch + 0 * longwords,
3791 *n = scratch + 1 * longwords,
3792 *m = scratch + 2 * longwords;
3793
3794 reverse_words((julong *)a_ints, a, longwords);
3795 reverse_words((julong *)n_ints, n, longwords);
3796
3797 if (len >= MONTGOMERY_SQUARING_THRESHOLD) {
3798 ::montgomery_square(a, n, m, (julong)inv, longwords);
3799 } else {
3800 ::montgomery_multiply(a, a, n, m, (julong)inv, longwords);
3801 }
3802
3803 reverse_words(m, (julong *)m_ints, longwords);
3804 }
3805
3806 BufferedInlineTypeBlob* SharedRuntime::generate_buffered_inline_type_adapter(const InlineKlass* vk) {
3807 BufferBlob* buf = BufferBlob::create("inline types pack/unpack", 16 * K);
3808 if (buf == nullptr) {
3809 return nullptr;
3810 }
3811 CodeBuffer buffer(buf);
3812 short buffer_locs[20];
3813 buffer.insts()->initialize_shared_locs((relocInfo*)buffer_locs,
3814 sizeof(buffer_locs)/sizeof(relocInfo));
3815
3816 MacroAssembler* masm = new MacroAssembler(&buffer);
3817
3818 const Array<SigEntry>* sig_vk = vk->extended_sig();
3819 const Array<VMRegPair>* regs = vk->return_regs();
3820
3821 int pack_fields_jobject_off = __ offset();
3822 // Resolve pre-allocated buffer from JNI handle.
3823 // We cannot do this in generate_call_stub() because it requires GC code to be initialized.
3824 __ movptr(rax, Address(r13, 0));
3825 __ resolve_jobject(rax /* value */,
3826 r12 /* tmp */);
3827 __ movptr(Address(r13, 0), rax);
3828
3829 int pack_fields_off = __ offset();
3830
3831 int j = 1;
3832 for (int i = 0; i < sig_vk->length(); i++) {
3833 BasicType bt = sig_vk->at(i)._bt;
3834 if (bt == T_METADATA) {
3835 continue;
3836 }
3837 if (bt == T_VOID) {
3838 if (sig_vk->at(i-1)._bt == T_LONG ||
3839 sig_vk->at(i-1)._bt == T_DOUBLE) {
3840 j++;
3841 }
3842 continue;
3843 }
3844 int off = sig_vk->at(i)._offset;
3845 assert(off > 0, "offset in object should be positive");
3846 VMRegPair pair = regs->at(j);
3847 VMReg r_1 = pair.first();
3848 Address to(rax, off);
3849 if (bt == T_FLOAT) {
3850 __ movflt(to, r_1->as_XMMRegister());
3851 } else if (bt == T_DOUBLE) {
3852 __ movdbl(to, r_1->as_XMMRegister());
3853 } else {
3854 Register val = r_1->as_Register();
3855 assert_different_registers(to.base(), val, r14, r13, rbx, rscratch1);
3856 if (is_reference_type(bt)) {
3857 // store_heap_oop transitively calls oop_store_at which corrupts to.base(). We need to keep rax valid.
3858 __ mov(rbx, rax);
3859 Address to_with_rbx(rbx, off);
3860 __ store_heap_oop(to_with_rbx, val, r14, r13, rbx, IN_HEAP | ACCESS_WRITE | IS_DEST_UNINITIALIZED);
3861 } else {
3862 __ store_sized_value(to, r_1->as_Register(), type2aelembytes(bt));
3863 }
3864 }
3865 j++;
3866 }
3867 assert(j == regs->length(), "missed a field?");
3868 if (vk->supports_nullable_layouts()) {
3869 // Set the null marker
3870 __ movb(Address(rax, vk->null_marker_offset()), 1);
3871 }
3872 __ ret(0);
3873
3874 int unpack_fields_off = __ offset();
3875
3876 Label skip;
3877 Label not_null;
3878 __ testptr(rax, rax);
3879 __ jcc(Assembler::notZero, not_null);
3880
3881 // Return value is null. Zero all registers because the runtime requires a canonical
3882 // representation of a flat null.
3883 j = 1;
3884 for (int i = 0; i < sig_vk->length(); i++) {
3885 BasicType bt = sig_vk->at(i)._bt;
3886 if (bt == T_METADATA) {
3887 continue;
3888 }
3889 if (bt == T_VOID) {
3890 if (sig_vk->at(i-1)._bt == T_LONG ||
3891 sig_vk->at(i-1)._bt == T_DOUBLE) {
3892 j++;
3893 }
3894 continue;
3895 }
3896
3897 VMRegPair pair = regs->at(j);
3898 VMReg r_1 = pair.first();
3899 if (r_1->is_XMMRegister()) {
3900 __ xorps(r_1->as_XMMRegister(), r_1->as_XMMRegister());
3901 } else {
3902 __ xorl(r_1->as_Register(), r_1->as_Register());
3903 }
3904 j++;
3905 }
3906 __ jmp(skip);
3907 __ bind(not_null);
3908
3909 j = 1;
3910 for (int i = 0; i < sig_vk->length(); i++) {
3911 BasicType bt = sig_vk->at(i)._bt;
3912 if (bt == T_METADATA) {
3913 continue;
3914 }
3915 if (bt == T_VOID) {
3916 if (sig_vk->at(i-1)._bt == T_LONG ||
3917 sig_vk->at(i-1)._bt == T_DOUBLE) {
3918 j++;
3919 }
3920 continue;
3921 }
3922 int off = sig_vk->at(i)._offset;
3923 assert(off > 0, "offset in object should be positive");
3924 VMRegPair pair = regs->at(j);
3925 VMReg r_1 = pair.first();
3926 VMReg r_2 = pair.second();
3927 Address from(rax, off);
3928 if (bt == T_FLOAT) {
3929 __ movflt(r_1->as_XMMRegister(), from);
3930 } else if (bt == T_DOUBLE) {
3931 __ movdbl(r_1->as_XMMRegister(), from);
3932 } else if (bt == T_OBJECT || bt == T_ARRAY) {
3933 assert_different_registers(rax, r_1->as_Register());
3934 __ load_heap_oop(r_1->as_Register(), from);
3935 } else {
3936 assert(is_java_primitive(bt), "unexpected basic type");
3937 assert_different_registers(rax, r_1->as_Register());
3938 size_t size_in_bytes = type2aelembytes(bt);
3939 __ load_sized_value(r_1->as_Register(), from, size_in_bytes, bt != T_CHAR && bt != T_BOOLEAN);
3940 }
3941 j++;
3942 }
3943 assert(j == regs->length(), "missed a field?");
3944
3945 __ bind(skip);
3946 __ ret(0);
3947
3948 __ flush();
3949
3950 return BufferedInlineTypeBlob::create(&buffer, pack_fields_off, pack_fields_jobject_off, unpack_fields_off);
3951 }
3952
3953 #if INCLUDE_JFR
3954
3955 // For c2: c_rarg0 is junk, call to runtime to write a checkpoint.
3956 // It returns a jobject handle to the event writer.
3957 // The handle is dereferenced and the return value is the event writer oop.
3958 RuntimeStub* SharedRuntime::generate_jfr_write_checkpoint() {
3959 enum layout {
3960 rbp_off,
3961 rbpH_off,
3962 return_off,
3963 return_off2,
3964 framesize // inclusive of return address
3965 };
3966
3967 const char* name = SharedRuntime::stub_name(StubId::shared_jfr_write_checkpoint_id);
3968 CodeBuffer code(name, 1024, 64);
3969 MacroAssembler* masm = new MacroAssembler(&code);
3970 address start = __ pc();
3971
3972 __ enter();
|