< prev index next >

src/cpu/x86/vm/sharedRuntime_x86_64.cpp

Print this page




  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/debugInfoRec.hpp"
  32 #include "code/icBuffer.hpp"
  33 #include "code/vtableStubs.hpp"
  34 #include "interpreter/interpreter.hpp"
  35 #include "oops/compiledICHolder.hpp"
  36 #include "prims/jvmtiRedefineClassesTrace.hpp"
  37 #include "runtime/sharedRuntime.hpp"
  38 #include "runtime/vframeArray.hpp"
  39 #include "vmreg_x86.inline.hpp"
  40 #ifdef COMPILER1
  41 #include "c1/c1_Runtime1.hpp"
  42 #endif
  43 #ifdef COMPILER2
  44 #include "opto/runtime.hpp"
  45 #endif
  46 #if INCLUDE_JVMCI
  47 #include "jvmci/jvmciJavaClasses.hpp"
  48 #endif
  49 
  50 #define __ masm->


 578             vt--;
 579           }
 580         } while (vt != 0);
 581       } else {
 582         total_args_passed++;
 583       }
 584     }
 585   } else {
 586     total_args_passed = sig_extended.length();
 587   }
 588   return total_args_passed;
 589 }
 590 
 591 
 592 static void gen_c2i_adapter_helper(MacroAssembler* masm,
 593                                    BasicType bt,
 594                                    BasicType prev_bt,
 595                                    size_t size_in_bytes,
 596                                    const VMRegPair& reg_pair,
 597                                    const Address& to,
 598                                    int extraspace) {

 599   assert(bt != T_VALUETYPE || !ValueTypePassFieldsAsArgs, "no value type here");
 600   if (bt == T_VOID) {
 601     assert(prev_bt == T_LONG || prev_bt == T_DOUBLE, "missing half");
 602     return;
 603   }
 604 
 605   // Say 4 args:
 606   // i   st_off
 607   // 0   32 T_LONG
 608   // 1   24 T_VOID
 609   // 2   16 T_OBJECT
 610   // 3    8 T_BOOL
 611   // -    0 return address
 612   //
 613   // However to make thing extra confusing. Because we can fit a long/double in
 614   // a single slot on a 64 bt vm and it would be silly to break them up, the interpreter
 615   // leaves one slot empty and only stores to a single slot. In this case the
 616   // slot that is occupied is the T_VOID slot. See I said it was confusing.
 617 
 618   bool wide = (size_in_bytes == wordSize);
 619   VMReg r_1 = reg_pair.first();
 620   VMReg r_2 = reg_pair.second();
 621   assert(r_2->is_valid() == wide, "invalid size");
 622   if (!r_1->is_valid()) {
 623     assert(!r_2->is_valid(), "must be invalid");
 624     return;
 625   }
 626   if (r_1->is_stack()) {




 627     int ld_off = r_1->reg2stack() * VMRegImpl::stack_slot_size + extraspace;
 628     __ load_sized_value(rax, Address(rsp, ld_off), size_in_bytes, /* is_signed */ false);
 629     __ store_sized_value(to, rax, size_in_bytes);
 630   } else if (r_1->is_Register()) {
 631     __ store_sized_value(to, r_1->as_Register(), size_in_bytes);





 632   } else {
 633     if (wide) {
 634       __ movdbl(to, r_1->as_XMMRegister());
 635     } else {
 636       __ movflt(to, r_1->as_XMMRegister());
 637     }
 638   }
 639 }
 640 
 641 static void gen_c2i_adapter(MacroAssembler *masm,
 642                             const GrowableArray<SigEntry>& sig_extended,
 643                             const VMRegPair *regs,
 644                             Label& skip_fixup,
 645                             address start,
 646                             OopMapSet*& oop_maps,
 647                             int& frame_complete,
 648                             int& frame_size_in_words) {
 649   // Before we get into the guts of the C2I adapter, see if we should be here
 650   // at all.  We've come from compiled code and are attempting to jump to the
 651   // interpreter, which means the caller made a static call to get here
 652   // (vcalls always get a compiled target if there is one).  Check for a
 653   // compiled target.  If there is one, we need to patch the caller's call.
 654   patch_callers_callsite(masm);
 655 
 656   __ bind(skip_fixup);
 657 

 658   if (ValueTypePassFieldsAsArgs) {
 659     // Is there a value type arguments?
 660     int i = 0;
 661     for (; i < sig_extended.length() && sig_extended.at(i)._bt != T_VALUETYPE; i++);
 662 
 663     if (i < sig_extended.length()) {
 664       // There is at least a value type argument: we're coming from
 665       // compiled code so we have no buffers to back the value
 666       // types. Allocate the buffers here with a runtime call.
 667       oop_maps = new OopMapSet();
 668       OopMap* map = NULL;
 669 
 670       map = RegisterSaver::save_live_registers(masm, 0, &frame_size_in_words);
 671 
 672       frame_complete = __ offset();
 673 
 674       __ set_last_Java_frame(noreg, noreg, NULL);
 675 
 676       __ mov(c_rarg0, r15_thread);
 677       __ mov(c_rarg1, rbx);
 678 
 679       __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::allocate_value_types)));
 680 
 681       oop_maps->add_gc_map((int)(__ pc() - start), map);
 682       __ reset_last_Java_frame(false, false);
 683 
 684       RegisterSaver::restore_live_registers(masm);
 685 
 686       Label no_exception;
 687       __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 688       __ jcc(Assembler::equal, no_exception);
 689 
 690       __ movptr(Address(r15_thread, JavaThread::vm_result_offset()), (int)NULL_WORD);
 691       __ movptr(rax, Address(r15_thread, Thread::pending_exception_offset()));
 692       __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
 693 
 694       __ bind(no_exception);
 695 
 696       // We get an array of objects from the runtime call
 697       int offset_in_bytes = arrayOopDesc::base_offset_in_bytes(T_OBJECT);
 698       __ get_vm_result(r13, r15_thread);
 699       __ get_vm_result_2(rbx, r15_thread); // TODO: required to keep the callee Method live?
 700       __ addptr(r13, offset_in_bytes);
 701       __ mov(r10, r13);
 702     }
 703   }
 704 
 705 
 706   // Since all args are passed on the stack, total_args_passed *
 707   // Interpreter::stackElementSize is the space we need. Plus 1 because
 708   // we also account for the return address location since
 709   // we store it first rather than hold it in rax across all the shuffling
 710   int total_args_passed = compute_total_args_passed_int(sig_extended);
 711   int extraspace = (total_args_passed * Interpreter::stackElementSize) + wordSize;
 712 
 713   // stack is aligned, keep it that way
 714   extraspace = round_to(extraspace, 2*wordSize);
 715 
 716   // Get return address
 717   __ pop(rax);
 718 
 719   // set senderSP value
 720   __ mov(r13, rsp);
 721 
 722   __ subptr(rsp, extraspace);
 723 
 724   // Store the return address in the expected location
 725   __ movptr(Address(rsp, 0), rax);
 726 
 727   // Now write the args into the outgoing interpreter space
 728 
 729   // next_arg_comp is the next argument from the compiler point of
 730   // view (value type fields are passed in registers/on the stack). In
 731   // sig_extended, a value type argument starts with: T_VALUETYPE,
 732   // followed by the types of the fields of the value type and T_VOID
 733   // to mark the end of the value type. ignored counts the number of
 734   // T_VALUETYPE/T_VOID. next_vt_arg is the next value type argument:
 735   // used to get the buffer for that argument from the pool of buffers
 736   // we allocated above and want to pass to the
 737   // interpreter. next_arg_int is the next argument from the
 738   // interpreter point of view (value types are passed by reference).

 739   for (int next_arg_comp = 0, ignored = 0, next_vt_arg = 0, next_arg_int = 0;
 740        next_arg_comp < sig_extended.length(); next_arg_comp++) {
 741     assert(ignored <= next_arg_comp, "shouldn't skip over more slot than there are arguments");
 742     assert(next_arg_int < total_args_passed, "more arguments for the interpreter than expected?");
 743     BasicType bt = sig_extended.at(next_arg_comp)._bt;
 744     int st_off = (total_args_passed - next_arg_int) * Interpreter::stackElementSize;
 745     if (!ValueTypePassFieldsAsArgs || bt != T_VALUETYPE) {
 746       int next_off = st_off - Interpreter::stackElementSize;
 747       const int offset = (bt == T_LONG || bt == T_DOUBLE) ? next_off : st_off;
 748       const VMRegPair reg_pair = regs[next_arg_comp-ignored];
 749       size_t size_in_bytes = reg_pair.second()->is_valid() ? 8 : 4;
 750       gen_c2i_adapter_helper(masm, bt, next_arg_comp > 0 ? sig_extended.at(next_arg_comp-1)._bt : T_ILLEGAL,
 751                              size_in_bytes, reg_pair, Address(rsp, offset), extraspace);
 752       next_arg_int++;
 753 #ifdef ASSERT
 754       if (bt == T_LONG || bt == T_DOUBLE) {
 755         // Overwrite the unused slot with known junk
 756         __ mov64(rax, CONST64(0xdeadffffdeadaaaa));
 757         __ movptr(Address(rsp, st_off), rax);
 758       }
 759 #endif /* ASSERT */
 760     } else {
 761       ignored++;
 762       // get the buffer from the just allocated pool of buffers
 763       __ load_heap_oop(r11, Address(r10, next_vt_arg * type2aelembytes(T_VALUETYPE)));

 764       next_vt_arg++; next_arg_int++;
 765       int vt = 1;
 766       // write fields we get from compiled code in registers/stack
 767       // slots to the buffer: we know we are done with that value type
 768       // argument when we hit the T_VOID that acts as an end of value
 769       // type delimiter for this value type. Value types are flattened
 770       // so we might encounter embedded value types. Each entry in
 771       // sig_extended contains a field offset in the buffer.
 772       do {
 773         next_arg_comp++;
 774         BasicType bt = sig_extended.at(next_arg_comp)._bt;
 775         BasicType prev_bt = sig_extended.at(next_arg_comp-1)._bt;
 776         if (bt == T_VALUETYPE) {
 777           vt++;
 778           ignored++;
 779         } else if (bt == T_VOID &&
 780                    prev_bt != T_LONG &&
 781                    prev_bt != T_DOUBLE) {
 782           vt--;
 783           ignored++;
 784         } else {
 785           int off = sig_extended.at(next_arg_comp)._offset;
 786           assert(off > 0, "offset in object should be positive");
 787           size_t size_in_bytes = is_java_primitive(bt) ? type2aelembytes(bt) : wordSize;


 788           gen_c2i_adapter_helper(masm, bt, next_arg_comp > 0 ? sig_extended.at(next_arg_comp-1)._bt : T_ILLEGAL,
 789                                  size_in_bytes, regs[next_arg_comp-ignored], Address(r11, off), extraspace);
 790         }
 791       } while (vt != 0);
 792       // pass the buffer to the interpreter
 793       __ movptr(Address(rsp, st_off), r11);
 794     }
 795   }
 796 


















 797   // Schedule the branch target address early.
 798   __ movptr(rcx, Address(rbx, in_bytes(Method::interpreter_entry_offset())));
 799   __ jmp(rcx);
 800 }
 801 
 802 static void range_check(MacroAssembler* masm, Register pc_reg, Register temp_reg,
 803                         address code_start, address code_end,
 804                         Label& L_ok) {
 805   Label L_fail;
 806   __ lea(temp_reg, ExternalAddress(code_start));
 807   __ cmpptr(pc_reg, temp_reg);
 808   __ jcc(Assembler::belowEqual, L_fail);
 809   __ lea(temp_reg, ExternalAddress(code_end));
 810   __ cmpptr(pc_reg, temp_reg);
 811   __ jcc(Assembler::below, L_ok);
 812   __ bind(L_fail);
 813 }
 814 
 815 static void gen_i2c_adapter_helper(MacroAssembler* masm,
 816                                    BasicType bt,
 817                                    BasicType prev_bt,
 818                                    size_t size_in_bytes,
 819                                    const VMRegPair& reg_pair,
 820                                    const Address& from) {

 821   assert(bt != T_VALUETYPE || !ValueTypePassFieldsAsArgs, "no value type here");
 822   if (bt == T_VOID) {
 823     // Longs and doubles are passed in native word order, but misaligned
 824     // in the 32-bit build.
 825     assert(prev_bt == T_LONG || prev_bt == T_DOUBLE, "missing half");
 826     return;
 827   }
 828   assert(!reg_pair.second()->is_valid() || reg_pair.first()->next() == reg_pair.second(),
 829          "scrambled load targets?");
 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   bool is_signed = (bt != T_CHAR) && (bt != T_BOOLEAN);
 841   if (r_1->is_stack()) {
 842     // Convert stack slot to an SP offset (+ wordSize to account for return address)
 843     int st_off = reg_pair.first()->reg2stack() * VMRegImpl::stack_slot_size + wordSize;
 844     // We can use r13 as a temp here because compiled code doesn't need r13 as an input
 845     // and if we end up going thru a c2i because of a miss a reasonable value of r13
 846     // will be generated.
 847     __ load_sized_value(r13, from, size_in_bytes, is_signed);
 848     __ movq(Address(rsp, st_off), r13);
 849   } else if (r_1->is_Register()) {
 850     Register r = r_1->as_Register();
 851     assert(r != rax, "must be different");
 852     __ load_sized_value(r, from, size_in_bytes, is_signed);





 853   } else {
 854     if (wide) {
 855       __ movdbl(r_1->as_XMMRegister(), from);
 856     } else {
 857       __ movflt(r_1->as_XMMRegister(), from);
 858     }
 859   }
 860 }
 861 
 862 void SharedRuntime::gen_i2c_adapter(MacroAssembler *masm,
 863                                     int comp_args_on_stack,
 864                                     const GrowableArray<SigEntry>& sig_extended,
 865                                     const VMRegPair *regs) {
 866 
 867   // Note: r13 contains the senderSP on entry. We must preserve it since
 868   // we may do a i2c -> c2i transition if we lose a race where compiled
 869   // code goes non-entrant while we get args ready.
 870   // In addition we use r13 to locate all the interpreter args as
 871   // we must align the stack to 16 bytes on an i2c entry else we
 872   // lose alignment we expect in all compiled code and register


 974   // next_arg_comp is the next argument from the compiler point of
 975   // view (value type fields are passed in registers/on the stack). In
 976   // sig_extended, a value type argument starts with: T_VALUETYPE,
 977   // followed by the types of the fields of the value type and T_VOID
 978   // to mark the end of the value type. ignored counts the number of
 979   // T_VALUETYPE/T_VOID. next_arg_int is the next argument from the
 980   // interpreter point of view (value types are passed by reference).
 981   for (int next_arg_comp = 0, ignored = 0, next_arg_int = 0; next_arg_comp < sig_extended.length(); next_arg_comp++) {
 982     assert(ignored <= next_arg_comp, "shouldn't skip over more slot than there are arguments");
 983     assert(next_arg_int < total_args_passed, "more arguments from the interpreter than expected?");
 984     BasicType bt = sig_extended.at(next_arg_comp)._bt;
 985     int ld_off = (total_args_passed - next_arg_int)*Interpreter::stackElementSize;
 986     if (!ValueTypePassFieldsAsArgs || bt != T_VALUETYPE) {
 987       // Load in argument order going down.
 988       // Point to interpreter value (vs. tag)
 989       int next_off = ld_off - Interpreter::stackElementSize;
 990       int offset = (bt == T_LONG || bt == T_DOUBLE) ? next_off : ld_off;
 991       const VMRegPair reg_pair = regs[next_arg_comp-ignored];
 992       size_t size_in_bytes = reg_pair.second()->is_valid() ? 8 : 4;
 993       gen_i2c_adapter_helper(masm, bt, next_arg_comp > 0 ? sig_extended.at(next_arg_comp-1)._bt : T_ILLEGAL,
 994                              size_in_bytes, reg_pair, Address(saved_sp, offset));
 995       next_arg_int++;
 996     } else {
 997       next_arg_int++;
 998       ignored++;
 999       // get the buffer for that value type
1000       __ movptr(r10, Address(saved_sp, ld_off));
1001       int vt = 1;
1002       // load fields to registers/stack slots from the buffer: we know
1003       // we are done with that value type argument when we hit the
1004       // T_VOID that acts as an end of value type delimiter for this
1005       // value type. Value types are flattened so we might encounter
1006       // embedded value types. Each entry in sig_extended contains a
1007       // field offset in the buffer.
1008       do {
1009         next_arg_comp++;
1010         BasicType bt = sig_extended.at(next_arg_comp)._bt;
1011         BasicType prev_bt = sig_extended.at(next_arg_comp-1)._bt;
1012         if (bt == T_VALUETYPE) {
1013           vt++;
1014           ignored++;
1015         } else if (bt == T_VOID &&
1016                    prev_bt != T_LONG &&
1017                    prev_bt != T_DOUBLE) {
1018           vt--;
1019           ignored++;
1020         } else {
1021           int off = sig_extended.at(next_arg_comp)._offset;
1022           assert(off > 0, "offset in object should be positive");
1023           size_t size_in_bytes = is_java_primitive(bt) ? type2aelembytes(bt) : wordSize;
1024           gen_i2c_adapter_helper(masm, bt, prev_bt, size_in_bytes, regs[next_arg_comp - ignored], Address(r10, off));

1025         }
1026       } while (vt != 0);
1027     }
1028   }
1029 
1030   // 6243940 We might end up in handle_wrong_method if
1031   // the callee is deoptimized as we race thru here. If that
1032   // happens we don't want to take a safepoint because the
1033   // caller frame will look interpreted and arguments are now
1034   // "compiled" so it is much better to make this transition
1035   // invisible to the stack walking code. Unfortunately if
1036   // we try and find the callee by normal means a safepoint
1037   // is possible. So we stash the desired callee in the thread
1038   // and the vm will find there should this case occur.
1039 
1040   __ movptr(Address(r15_thread, JavaThread::callee_target_offset()), rbx);
1041 
1042   // put Method* where a c2i would expect should we end up there
1043   // only needed because of c2 resolve stubs return Method* as a result in
1044   // rax


1082     __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
1083 
1084     __ bind(ok);
1085     // Method might have been compiled since the call site was patched to
1086     // interpreted if that is the case treat it as a miss so we can get
1087     // the call site corrected.
1088     __ cmpptr(Address(rbx, in_bytes(Method::code_offset())), (int32_t)NULL_WORD);
1089     __ jcc(Assembler::equal, skip_fixup);
1090     __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
1091   }
1092 
1093   address c2i_entry = __ pc();
1094 
1095   OopMapSet* oop_maps = NULL;
1096   int frame_complete = CodeOffsets::frame_never_safe;
1097   int frame_size_in_words = 0;
1098   gen_c2i_adapter(masm, sig_extended, regs, skip_fixup, i2c_entry, oop_maps, frame_complete, frame_size_in_words);
1099 
1100   __ flush();
1101   new_adapter = AdapterBlob::create(masm->code(), frame_complete, frame_size_in_words, oop_maps);
1102   return AdapterHandlerLibrary::new_entry(fingerprint, i2c_entry, c2i_entry, c2i_unverified_entry);



































1103 }
1104 
1105 int SharedRuntime::c_calling_convention(const BasicType *sig_bt,
1106                                          VMRegPair *regs,
1107                                          VMRegPair *regs2,
1108                                          int total_args_passed) {
1109   assert(regs2 == NULL, "not needed on x86");
1110 // We return the amount of VMRegImpl stack slots we need to reserve for all
1111 // the arguments NOT counting out_preserve_stack_slots.
1112 
1113 // NOTE: These arrays will have to change when c1 is ported
1114 #ifdef _WIN64
1115     static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {
1116       c_rarg0, c_rarg1, c_rarg2, c_rarg3
1117     };
1118     static const XMMRegister FP_ArgReg[Argument::n_float_register_parameters_c] = {
1119       c_farg0, c_farg1, c_farg2, c_farg3
1120     };
1121 #else
1122     static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {




  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/debugInfoRec.hpp"
  33 #include "code/icBuffer.hpp"
  34 #include "code/vtableStubs.hpp"
  35 #include "interpreter/interpreter.hpp"
  36 #include "oops/compiledICHolder.hpp"
  37 #include "prims/jvmtiRedefineClassesTrace.hpp"
  38 #include "runtime/sharedRuntime.hpp"
  39 #include "runtime/vframeArray.hpp"
  40 #include "vmreg_x86.inline.hpp"
  41 #ifdef COMPILER1
  42 #include "c1/c1_Runtime1.hpp"
  43 #endif
  44 #ifdef COMPILER2
  45 #include "opto/runtime.hpp"
  46 #endif
  47 #if INCLUDE_JVMCI
  48 #include "jvmci/jvmciJavaClasses.hpp"
  49 #endif
  50 
  51 #define __ masm->


 579             vt--;
 580           }
 581         } while (vt != 0);
 582       } else {
 583         total_args_passed++;
 584       }
 585     }
 586   } else {
 587     total_args_passed = sig_extended.length();
 588   }
 589   return total_args_passed;
 590 }
 591 
 592 
 593 static void gen_c2i_adapter_helper(MacroAssembler* masm,
 594                                    BasicType bt,
 595                                    BasicType prev_bt,
 596                                    size_t size_in_bytes,
 597                                    const VMRegPair& reg_pair,
 598                                    const Address& to,
 599                                    int extraspace,
 600                                    bool is_oop) {
 601   assert(bt != T_VALUETYPE || !ValueTypePassFieldsAsArgs, "no value type here");
 602   if (bt == T_VOID) {
 603     assert(prev_bt == T_LONG || prev_bt == T_DOUBLE, "missing half");
 604     return;
 605   }
 606 
 607   // Say 4 args:
 608   // i   st_off
 609   // 0   32 T_LONG
 610   // 1   24 T_VOID
 611   // 2   16 T_OBJECT
 612   // 3    8 T_BOOL
 613   // -    0 return address
 614   //
 615   // However to make thing extra confusing. Because we can fit a long/double in
 616   // a single slot on a 64 bt vm and it would be silly to break them up, the interpreter
 617   // leaves one slot empty and only stores to a single slot. In this case the
 618   // slot that is occupied is the T_VOID slot. See I said it was confusing.
 619 
 620   bool wide = (size_in_bytes == wordSize);
 621   VMReg r_1 = reg_pair.first();
 622   VMReg r_2 = reg_pair.second();
 623   assert(r_2->is_valid() == wide, "invalid size");
 624   if (!r_1->is_valid()) {
 625     assert(!r_2->is_valid(), "must be invalid");
 626     return;
 627   }
 628 
 629   if (!r_1->is_XMMRegister()) {
 630     Register val = rax;
 631     assert_different_registers(to.base(), val);
 632     if(r_1->is_stack()) {
 633       int ld_off = r_1->reg2stack() * VMRegImpl::stack_slot_size + extraspace;
 634       __ load_sized_value(val, Address(rsp, ld_off), size_in_bytes, /* is_signed */ false);
 635     } else {
 636       val = r_1->as_Register();
 637     }
 638     if (is_oop) {
 639       __ store_heap_oop(to, val);
 640     } else {
 641       __ store_sized_value(to, val, size_in_bytes);
 642     }
 643   } else {
 644     if (wide) {
 645       __ movdbl(to, r_1->as_XMMRegister());
 646     } else {
 647       __ movflt(to, r_1->as_XMMRegister());
 648     }
 649   }
 650 }
 651 
 652 static void gen_c2i_adapter(MacroAssembler *masm,
 653                             const GrowableArray<SigEntry>& sig_extended,
 654                             const VMRegPair *regs,
 655                             Label& skip_fixup,
 656                             address start,
 657                             OopMapSet*& oop_maps,
 658                             int& frame_complete,
 659                             int& frame_size_in_words) {
 660   // Before we get into the guts of the C2I adapter, see if we should be here
 661   // at all.  We've come from compiled code and are attempting to jump to the
 662   // interpreter, which means the caller made a static call to get here
 663   // (vcalls always get a compiled target if there is one).  Check for a
 664   // compiled target.  If there is one, we need to patch the caller's call.
 665   patch_callers_callsite(masm);
 666 
 667   __ bind(skip_fixup);
 668 
 669   bool has_value_argument = false;
 670   if (ValueTypePassFieldsAsArgs) {
 671     // Is there a value type argument?
 672     for (int i = 0; i < sig_extended.length() && !has_value_argument; i++) {
 673       has_value_argument = (sig_extended.at(i)._bt == T_VALUETYPE);
 674     }
 675     if (has_value_argument) {
 676       // There is at least a value type argument: we're coming from
 677       // compiled code so we have no buffers to back the value
 678       // types. Allocate the buffers here with a runtime call.
 679       oop_maps = new OopMapSet();
 680       OopMap* map = NULL;
 681 
 682       map = RegisterSaver::save_live_registers(masm, 0, &frame_size_in_words);
 683 
 684       frame_complete = __ offset();
 685 
 686       __ set_last_Java_frame(noreg, noreg, NULL);
 687 
 688       __ mov(c_rarg0, r15_thread);
 689       __ mov(c_rarg1, rbx);
 690 
 691       __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::allocate_value_types)));
 692 
 693       oop_maps->add_gc_map((int)(__ pc() - start), map);
 694       __ reset_last_Java_frame(false, false);
 695 
 696       RegisterSaver::restore_live_registers(masm);
 697 
 698       Label no_exception;
 699       __ cmpptr(Address(r15_thread, Thread::pending_exception_offset()), (int32_t)NULL_WORD);
 700       __ jcc(Assembler::equal, no_exception);
 701 
 702       __ movptr(Address(r15_thread, JavaThread::vm_result_offset()), (int)NULL_WORD);
 703       __ movptr(rax, Address(r15_thread, Thread::pending_exception_offset()));
 704       __ jump(RuntimeAddress(StubRoutines::forward_exception_entry()));
 705 
 706       __ bind(no_exception);
 707 
 708       // We get an array of objects from the runtime call

 709       __ get_vm_result(r13, r15_thread);
 710       __ get_vm_result_2(rbx, r15_thread); // TODO: required to keep the callee Method live?
 711       __ mov(r10, r13); // Cannot use r10 above because it's trashed by movptr()

 712     }
 713   }
 714 

 715   // Since all args are passed on the stack, total_args_passed *
 716   // Interpreter::stackElementSize is the space we need. Plus 1 because
 717   // we also account for the return address location since
 718   // we store it first rather than hold it in rax across all the shuffling
 719   int total_args_passed = compute_total_args_passed_int(sig_extended);
 720   int extraspace = (total_args_passed * Interpreter::stackElementSize) + wordSize;
 721 
 722   // stack is aligned, keep it that way
 723   extraspace = round_to(extraspace, 2*wordSize);
 724 
 725   // Get return address
 726   __ pop(rax);
 727 
 728   // set senderSP value
 729   __ mov(r13, rsp);
 730 
 731   __ subptr(rsp, extraspace);
 732 
 733   // Store the return address in the expected location
 734   __ movptr(Address(rsp, 0), rax);
 735 
 736   // Now write the args into the outgoing interpreter space
 737 
 738   // next_arg_comp is the next argument from the compiler point of
 739   // view (value type fields are passed in registers/on the stack). In
 740   // sig_extended, a value type argument starts with: T_VALUETYPE,
 741   // followed by the types of the fields of the value type and T_VOID
 742   // to mark the end of the value type. ignored counts the number of
 743   // T_VALUETYPE/T_VOID. next_vt_arg is the next value type argument:
 744   // used to get the buffer for that argument from the pool of buffers
 745   // we allocated above and want to pass to the
 746   // interpreter. next_arg_int is the next argument from the
 747   // interpreter point of view (value types are passed by reference).
 748   bool has_oop_field = false;
 749   for (int next_arg_comp = 0, ignored = 0, next_vt_arg = 0, next_arg_int = 0;
 750        next_arg_comp < sig_extended.length(); next_arg_comp++) {
 751     assert(ignored <= next_arg_comp, "shouldn't skip over more slot than there are arguments");
 752     assert(next_arg_int < total_args_passed, "more arguments for the interpreter than expected?");
 753     BasicType bt = sig_extended.at(next_arg_comp)._bt;
 754     int st_off = (total_args_passed - next_arg_int) * Interpreter::stackElementSize;
 755     if (!ValueTypePassFieldsAsArgs || bt != T_VALUETYPE) {
 756       int next_off = st_off - Interpreter::stackElementSize;
 757       const int offset = (bt == T_LONG || bt == T_DOUBLE) ? next_off : st_off;
 758       const VMRegPair reg_pair = regs[next_arg_comp-ignored];
 759       size_t size_in_bytes = reg_pair.second()->is_valid() ? 8 : 4;
 760       gen_c2i_adapter_helper(masm, bt, next_arg_comp > 0 ? sig_extended.at(next_arg_comp-1)._bt : T_ILLEGAL,
 761                              size_in_bytes, reg_pair, Address(rsp, offset), extraspace, false);
 762       next_arg_int++;
 763 #ifdef ASSERT
 764       if (bt == T_LONG || bt == T_DOUBLE) {
 765         // Overwrite the unused slot with known junk
 766         __ mov64(rax, CONST64(0xdeadffffdeadaaaa));
 767         __ movptr(Address(rsp, st_off), rax);
 768       }
 769 #endif /* ASSERT */
 770     } else {
 771       ignored++;
 772       // get the buffer from the just allocated pool of buffers
 773       int index = arrayOopDesc::base_offset_in_bytes(T_OBJECT) + next_vt_arg * type2aelembytes(T_VALUETYPE);
 774       __ load_heap_oop(r11, Address(r10, index));
 775       next_vt_arg++; next_arg_int++;
 776       int vt = 1;
 777       // write fields we get from compiled code in registers/stack
 778       // slots to the buffer: we know we are done with that value type
 779       // argument when we hit the T_VOID that acts as an end of value
 780       // type delimiter for this value type. Value types are flattened
 781       // so we might encounter embedded value types. Each entry in
 782       // sig_extended contains a field offset in the buffer.
 783       do {
 784         next_arg_comp++;
 785         BasicType bt = sig_extended.at(next_arg_comp)._bt;
 786         BasicType prev_bt = sig_extended.at(next_arg_comp-1)._bt;
 787         if (bt == T_VALUETYPE) {
 788           vt++;
 789           ignored++;
 790         } else if (bt == T_VOID &&
 791                    prev_bt != T_LONG &&
 792                    prev_bt != T_DOUBLE) {
 793           vt--;
 794           ignored++;
 795         } else {
 796           int off = sig_extended.at(next_arg_comp)._offset;
 797           assert(off > 0, "offset in object should be positive");
 798           size_t size_in_bytes = is_java_primitive(bt) ? type2aelembytes(bt) : wordSize;
 799           bool is_oop = (bt == T_OBJECT || bt == T_ARRAY);
 800           has_oop_field = has_oop_field || is_oop;
 801           gen_c2i_adapter_helper(masm, bt, next_arg_comp > 0 ? sig_extended.at(next_arg_comp-1)._bt : T_ILLEGAL,
 802                                  size_in_bytes, regs[next_arg_comp-ignored], Address(r11, off), extraspace, is_oop);
 803         }
 804       } while (vt != 0);
 805       // pass the buffer to the interpreter
 806       __ movptr(Address(rsp, st_off), r11);
 807     }
 808   }
 809 
 810   // If a value type was allocated and initialized, apply post barrier to all oop fields
 811   if (has_value_argument && has_oop_field) {
 812     __ push_CPU_state();
 813 
 814     // Allocate argument register save area
 815     if (frame::arg_reg_save_area_bytes != 0) {
 816       __ subptr(rsp, frame::arg_reg_save_area_bytes);
 817     }
 818     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::apply_post_barriers), r15_thread, r10, rbx);
 819     // De-allocate argument register save area
 820     if (frame::arg_reg_save_area_bytes != 0) {
 821       __ addptr(rsp, frame::arg_reg_save_area_bytes);
 822     }
 823 
 824     __ pop_CPU_state();
 825     __ get_vm_result_2(rbx, r15_thread); // TODO: required to keep the callee Method live?
 826   }
 827 
 828   // Schedule the branch target address early.
 829   __ movptr(rcx, Address(rbx, in_bytes(Method::interpreter_entry_offset())));
 830   __ jmp(rcx);
 831 }
 832 
 833 static void range_check(MacroAssembler* masm, Register pc_reg, Register temp_reg,
 834                         address code_start, address code_end,
 835                         Label& L_ok) {
 836   Label L_fail;
 837   __ lea(temp_reg, ExternalAddress(code_start));
 838   __ cmpptr(pc_reg, temp_reg);
 839   __ jcc(Assembler::belowEqual, L_fail);
 840   __ lea(temp_reg, ExternalAddress(code_end));
 841   __ cmpptr(pc_reg, temp_reg);
 842   __ jcc(Assembler::below, L_ok);
 843   __ bind(L_fail);
 844 }
 845 
 846 static void gen_i2c_adapter_helper(MacroAssembler* masm,
 847                                    BasicType bt,
 848                                    BasicType prev_bt,
 849                                    size_t size_in_bytes,
 850                                    const VMRegPair& reg_pair,
 851                                    const Address& from,
 852                                    bool is_oop) {
 853   assert(bt != T_VALUETYPE || !ValueTypePassFieldsAsArgs, "no value type here");
 854   if (bt == T_VOID) {
 855     // Longs and doubles are passed in native word order, but misaligned
 856     // in the 32-bit build.
 857     assert(prev_bt == T_LONG || prev_bt == T_DOUBLE, "missing half");
 858     return;
 859   }
 860   assert(!reg_pair.second()->is_valid() || reg_pair.first()->next() == reg_pair.second(),
 861          "scrambled load targets?");
 862 
 863   bool wide = (size_in_bytes == wordSize);
 864   VMReg r_1 = reg_pair.first();
 865   VMReg r_2 = reg_pair.second();
 866   assert(r_2->is_valid() == wide, "invalid size");
 867   if (!r_1->is_valid()) {
 868     assert(!r_2->is_valid(), "must be invalid");
 869     return;
 870   }
 871 
 872   bool is_signed = (bt != T_CHAR) && (bt != T_BOOLEAN);
 873   if (!r_1->is_XMMRegister()) {


 874     // We can use r13 as a temp here because compiled code doesn't need r13 as an input
 875     // and if we end up going thru a c2i because of a miss a reasonable value of r13
 876     // will be generated.
 877     Register dst = r_1->is_stack() ? r13 : r_1->as_Register();
 878     if (is_oop) {
 879       __ load_heap_oop(dst, from);
 880     } else {
 881       __ load_sized_value(dst, from, size_in_bytes, is_signed);
 882     }
 883     if (r_1->is_stack()) {
 884       // Convert stack slot to an SP offset (+ wordSize to account for return address)
 885       int st_off = reg_pair.first()->reg2stack() * VMRegImpl::stack_slot_size + wordSize;
 886       __ movq(Address(rsp, st_off), dst);
 887     }
 888   } else {
 889     if (wide) {
 890       __ movdbl(r_1->as_XMMRegister(), from);
 891     } else {
 892       __ movflt(r_1->as_XMMRegister(), from);
 893     }
 894   }
 895 }
 896 
 897 void SharedRuntime::gen_i2c_adapter(MacroAssembler *masm,
 898                                     int comp_args_on_stack,
 899                                     const GrowableArray<SigEntry>& sig_extended,
 900                                     const VMRegPair *regs) {
 901 
 902   // Note: r13 contains the senderSP on entry. We must preserve it since
 903   // we may do a i2c -> c2i transition if we lose a race where compiled
 904   // code goes non-entrant while we get args ready.
 905   // In addition we use r13 to locate all the interpreter args as
 906   // we must align the stack to 16 bytes on an i2c entry else we
 907   // lose alignment we expect in all compiled code and register


1009   // next_arg_comp is the next argument from the compiler point of
1010   // view (value type fields are passed in registers/on the stack). In
1011   // sig_extended, a value type argument starts with: T_VALUETYPE,
1012   // followed by the types of the fields of the value type and T_VOID
1013   // to mark the end of the value type. ignored counts the number of
1014   // T_VALUETYPE/T_VOID. next_arg_int is the next argument from the
1015   // interpreter point of view (value types are passed by reference).
1016   for (int next_arg_comp = 0, ignored = 0, next_arg_int = 0; next_arg_comp < sig_extended.length(); next_arg_comp++) {
1017     assert(ignored <= next_arg_comp, "shouldn't skip over more slot than there are arguments");
1018     assert(next_arg_int < total_args_passed, "more arguments from the interpreter than expected?");
1019     BasicType bt = sig_extended.at(next_arg_comp)._bt;
1020     int ld_off = (total_args_passed - next_arg_int)*Interpreter::stackElementSize;
1021     if (!ValueTypePassFieldsAsArgs || bt != T_VALUETYPE) {
1022       // Load in argument order going down.
1023       // Point to interpreter value (vs. tag)
1024       int next_off = ld_off - Interpreter::stackElementSize;
1025       int offset = (bt == T_LONG || bt == T_DOUBLE) ? next_off : ld_off;
1026       const VMRegPair reg_pair = regs[next_arg_comp-ignored];
1027       size_t size_in_bytes = reg_pair.second()->is_valid() ? 8 : 4;
1028       gen_i2c_adapter_helper(masm, bt, next_arg_comp > 0 ? sig_extended.at(next_arg_comp-1)._bt : T_ILLEGAL,
1029                              size_in_bytes, reg_pair, Address(saved_sp, offset), false);
1030       next_arg_int++;
1031     } else {
1032       next_arg_int++;
1033       ignored++;
1034       // get the buffer for that value type
1035       __ movptr(r10, Address(saved_sp, ld_off));
1036       int vt = 1;
1037       // load fields to registers/stack slots from the buffer: we know
1038       // we are done with that value type argument when we hit the
1039       // T_VOID that acts as an end of value type delimiter for this
1040       // value type. Value types are flattened so we might encounter
1041       // embedded value types. Each entry in sig_extended contains a
1042       // field offset in the buffer.
1043       do {
1044         next_arg_comp++;
1045         BasicType bt = sig_extended.at(next_arg_comp)._bt;
1046         BasicType prev_bt = sig_extended.at(next_arg_comp-1)._bt;
1047         if (bt == T_VALUETYPE) {
1048           vt++;
1049           ignored++;
1050         } else if (bt == T_VOID &&
1051                    prev_bt != T_LONG &&
1052                    prev_bt != T_DOUBLE) {
1053           vt--;
1054           ignored++;
1055         } else {
1056           int off = sig_extended.at(next_arg_comp)._offset;
1057           assert(off > 0, "offset in object should be positive");
1058           size_t size_in_bytes = is_java_primitive(bt) ? type2aelembytes(bt) : wordSize;
1059           bool is_oop = (bt == T_OBJECT || bt == T_ARRAY);
1060           gen_i2c_adapter_helper(masm, bt, prev_bt, size_in_bytes, regs[next_arg_comp - ignored], Address(r10, off), is_oop);
1061         }
1062       } while (vt != 0);
1063     }
1064   }
1065 
1066   // 6243940 We might end up in handle_wrong_method if
1067   // the callee is deoptimized as we race thru here. If that
1068   // happens we don't want to take a safepoint because the
1069   // caller frame will look interpreted and arguments are now
1070   // "compiled" so it is much better to make this transition
1071   // invisible to the stack walking code. Unfortunately if
1072   // we try and find the callee by normal means a safepoint
1073   // is possible. So we stash the desired callee in the thread
1074   // and the vm will find there should this case occur.
1075 
1076   __ movptr(Address(r15_thread, JavaThread::callee_target_offset()), rbx);
1077 
1078   // put Method* where a c2i would expect should we end up there
1079   // only needed because of c2 resolve stubs return Method* as a result in
1080   // rax


1118     __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
1119 
1120     __ bind(ok);
1121     // Method might have been compiled since the call site was patched to
1122     // interpreted if that is the case treat it as a miss so we can get
1123     // the call site corrected.
1124     __ cmpptr(Address(rbx, in_bytes(Method::code_offset())), (int32_t)NULL_WORD);
1125     __ jcc(Assembler::equal, skip_fixup);
1126     __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
1127   }
1128 
1129   address c2i_entry = __ pc();
1130 
1131   OopMapSet* oop_maps = NULL;
1132   int frame_complete = CodeOffsets::frame_never_safe;
1133   int frame_size_in_words = 0;
1134   gen_c2i_adapter(masm, sig_extended, regs, skip_fixup, i2c_entry, oop_maps, frame_complete, frame_size_in_words);
1135 
1136   __ flush();
1137   new_adapter = AdapterBlob::create(masm->code(), frame_complete, frame_size_in_words, oop_maps);
1138 
1139   // If value types are passed as fields, save the extended signature as symbol in
1140   // the AdapterHandlerEntry to be used by nmethod::preserve_callee_argument_oops().
1141   Symbol* extended_signature = NULL;
1142   if (ValueTypePassFieldsAsArgs) {
1143     bool has_value_argument = false;
1144     Thread* THREAD = Thread::current();
1145     ResourceMark rm(THREAD);
1146     int length = sig_extended.length();
1147     // TODO is 256 enough? Can we determine the required length?
1148     char* sig_str = NEW_RESOURCE_ARRAY(char, 256);
1149     int idx = 0;
1150     sig_str[idx++] = '(';
1151     for (int index = 0; index < length; index++ ) {
1152       BasicType bt = sig_extended.at(index)._bt;
1153       if (bt == T_VALUETYPE || bt == T_VOID) {
1154         has_value_argument = true;
1155         continue; // Ignore wrapper types
1156       }
1157       sig_str[idx++] = type2char(bt);
1158       if (bt == T_OBJECT) {
1159         sig_str[idx++] = ';';
1160       } else if (bt == T_ARRAY) {
1161         // We don't know the array element type, put void as placeholder
1162         sig_str[idx++] = 'V';
1163       }
1164     }
1165     sig_str[idx++] = ')';
1166     sig_str[idx++] = '\0';
1167     if (has_value_argument) {
1168       // Extended signature is only required if a value type argument is passed
1169       extended_signature = SymbolTable::new_permanent_symbol(sig_str, THREAD);
1170     }
1171   }
1172 
1173   return AdapterHandlerLibrary::new_entry(fingerprint, i2c_entry, c2i_entry, c2i_unverified_entry, extended_signature);
1174 }
1175 
1176 int SharedRuntime::c_calling_convention(const BasicType *sig_bt,
1177                                          VMRegPair *regs,
1178                                          VMRegPair *regs2,
1179                                          int total_args_passed) {
1180   assert(regs2 == NULL, "not needed on x86");
1181 // We return the amount of VMRegImpl stack slots we need to reserve for all
1182 // the arguments NOT counting out_preserve_stack_slots.
1183 
1184 // NOTE: These arrays will have to change when c1 is ported
1185 #ifdef _WIN64
1186     static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {
1187       c_rarg0, c_rarg1, c_rarg2, c_rarg3
1188     };
1189     static const XMMRegister FP_ArgReg[Argument::n_float_register_parameters_c] = {
1190       c_farg0, c_farg1, c_farg2, c_farg3
1191     };
1192 #else
1193     static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {


< prev index next >