< prev index next >

src/hotspot/cpu/x86/sharedRuntime_x86_64.cpp

Print this page




 475     case T_SHORT:
 476     case T_INT:
 477       if (int_args < Argument::n_int_register_parameters_j) {
 478         regs[i].set1(INT_ArgReg[int_args++]->as_VMReg());
 479       } else {
 480         regs[i].set1(VMRegImpl::stack2reg(stk_args));
 481         stk_args += 2;
 482       }
 483       break;
 484     case T_VOID:
 485       // halves of T_LONG or T_DOUBLE
 486       assert(i != 0 && (sig_bt[i - 1] == T_LONG || sig_bt[i - 1] == T_DOUBLE), "expecting half");
 487       regs[i].set_bad();
 488       break;
 489     case T_LONG:
 490       assert((i + 1) < total_args_passed && sig_bt[i + 1] == T_VOID, "expecting half");
 491       // fall through
 492     case T_OBJECT:
 493     case T_ARRAY:
 494     case T_ADDRESS:
 495     case T_VALUETYPE:
 496     case T_VALUETYPEPTR:
 497       if (int_args < Argument::n_int_register_parameters_j) {
 498         regs[i].set2(INT_ArgReg[int_args++]->as_VMReg());
 499       } else {
 500         regs[i].set2(VMRegImpl::stack2reg(stk_args));
 501         stk_args += 2;
 502       }
 503       break;
 504     case T_FLOAT:
 505       if (fp_args < Argument::n_float_register_parameters_j) {
 506         regs[i].set1(FP_ArgReg[fp_args++]->as_VMReg());
 507       } else {
 508         regs[i].set1(VMRegImpl::stack2reg(stk_args));
 509         stk_args += 2;
 510       }
 511       break;
 512     case T_DOUBLE:
 513       assert((i + 1) < total_args_passed && sig_bt[i + 1] == T_VOID, "expecting half");
 514       if (fp_args < Argument::n_float_register_parameters_j) {
 515         regs[i].set2(FP_ArgReg[fp_args++]->as_VMReg());
 516       } else {


 559       if (int_args < Argument::n_int_register_parameters_j+1) {
 560         regs[i].set1(INT_ArgReg[int_args]->as_VMReg());
 561         int_args++;
 562       } else {
 563         return -1;
 564       }
 565       break;
 566     case T_VOID:
 567       // halves of T_LONG or T_DOUBLE
 568       assert(i != 0 && (sig_bt[i - 1] == T_LONG || sig_bt[i - 1] == T_DOUBLE), "expecting half");
 569       regs[i].set_bad();
 570       break;
 571     case T_LONG:
 572       assert(sig_bt[i + 1] == T_VOID, "expecting half");
 573       // fall through
 574     case T_OBJECT:
 575     case T_VALUETYPE:
 576     case T_ARRAY:
 577     case T_ADDRESS:
 578     case T_METADATA:
 579     case T_VALUETYPEPTR:
 580       if (int_args < Argument::n_int_register_parameters_j+1) {
 581         regs[i].set2(INT_ArgReg[int_args]->as_VMReg());
 582         int_args++;
 583       } else {
 584         return -1;
 585       }
 586       break;
 587     case T_FLOAT:
 588       if (fp_args < Argument::n_float_register_parameters_j) {
 589         regs[i].set1(FP_ArgReg[fp_args]->as_VMReg());
 590         fp_args++;
 591       } else {
 592         return -1;
 593       }
 594       break;
 595     case T_DOUBLE:
 596       assert(sig_bt[i + 1] == T_VOID, "expecting half");
 597       if (fp_args < Argument::n_float_register_parameters_j) {
 598         regs[i].set2(FP_ArgReg[fp_args]->as_VMReg());
 599         fp_args++;


 639   __ mov(c_rarg0, rbx);
 640   __ mov(c_rarg1, rax);
 641   __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::fixup_callers_callsite)));
 642 
 643   // De-allocate argument register save area
 644   if (frame::arg_reg_save_area_bytes != 0) {
 645     __ addptr(rsp, frame::arg_reg_save_area_bytes);
 646   }
 647 
 648   __ vzeroupper();
 649   __ pop_CPU_state();
 650   // restore sp
 651   __ mov(rsp, r13);
 652   __ bind(L);
 653 }
 654 
 655 // For each value type argument, sig includes the list of fields of
 656 // the value type. This utility function computes the number of
 657 // arguments for the call if value types are passed by reference (the
 658 // calling convention the interpreter expects).
 659 static int compute_total_args_passed_int(const GrowableArray<SigEntry>& sig_extended) {
 660   int total_args_passed = 0;
 661   if (ValueTypePassFieldsAsArgs) {
 662     for (int i = 0; i < sig_extended.length(); i++) {
 663       BasicType bt = sig_extended.at(i)._bt;
 664       if (bt == T_VALUETYPE) {


 665         // In sig_extended, a value type argument starts with:
 666         // T_VALUETYPE, followed by the types of the fields of the
 667         // value type and T_VOID to mark the end of the value
 668         // type. Value types are flattened so, for instance, in the
 669         // case of a value type with an int field and a value type
 670         // field that itself has 2 fields, an int and a long:
 671         // T_VALUETYPE T_INT T_VALUETYPE T_INT T_LONG T_VOID (second
 672         // slot for the T_LONG) T_VOID (inner T_VALUETYPE) T_VOID
 673         // (outer T_VALUETYPE)
 674         total_args_passed++;
 675         int vt = 1;
 676         do {
 677           i++;
 678           BasicType bt = sig_extended.at(i)._bt;
 679           BasicType prev_bt = sig_extended.at(i-1)._bt;
 680           if (bt == T_VALUETYPE) {
 681             vt++;
 682           } else if (bt == T_VOID &&
 683                      prev_bt != T_LONG &&
 684                      prev_bt != T_DOUBLE) {
 685             vt--;
 686           }
 687         } while (vt != 0);
 688       } else {
 689         total_args_passed++;
 690       }
 691     }
 692   } else {
 693     total_args_passed = sig_extended.length();
 694   }
 695   return total_args_passed;
 696 }
 697 
 698 
 699 static void gen_c2i_adapter_helper(MacroAssembler* masm,
 700                                    BasicType bt,
 701                                    BasicType prev_bt,
 702                                    size_t size_in_bytes,
 703                                    const VMRegPair& reg_pair,
 704                                    const Address& to,
 705                                    int extraspace,
 706                                    bool is_oop) {
 707   assert(bt != T_VALUETYPE || !ValueTypePassFieldsAsArgs, "no value type here");
 708   if (bt == T_VOID) {
 709     assert(prev_bt == T_LONG || prev_bt == T_DOUBLE, "missing half");
 710     return;
 711   }
 712 
 713   // Say 4 args:


 725 
 726   bool wide = (size_in_bytes == wordSize);
 727   VMReg r_1 = reg_pair.first();
 728   VMReg r_2 = reg_pair.second();
 729   assert(r_2->is_valid() == wide, "invalid size");
 730   if (!r_1->is_valid()) {
 731     assert(!r_2->is_valid(), "must be invalid");
 732     return;
 733   }
 734 
 735   if (!r_1->is_XMMRegister()) {
 736     Register val = rax;
 737     assert_different_registers(to.base(), val);
 738     if(r_1->is_stack()) {
 739       int ld_off = r_1->reg2stack() * VMRegImpl::stack_slot_size + extraspace;
 740       __ load_sized_value(val, Address(rsp, ld_off), size_in_bytes, /* is_signed */ false);
 741     } else {
 742       val = r_1->as_Register();
 743     }
 744     if (is_oop) {
 745       __ store_heap_oop(to, val);







 746     } else {
 747       __ store_sized_value(to, val, size_in_bytes);
 748     }
 749   } else {
 750     if (wide) {
 751       __ movdbl(to, r_1->as_XMMRegister());
 752     } else {
 753       __ movflt(to, r_1->as_XMMRegister());
 754     }
 755   }
 756 }
 757 
 758 static void gen_c2i_adapter(MacroAssembler *masm,
 759                             const GrowableArray<SigEntry>& sig_extended,
 760                             const VMRegPair *regs,
 761                             Label& skip_fixup,
 762                             address start,
 763                             OopMapSet*& oop_maps,
 764                             int& frame_complete,
 765                             int& frame_size_in_words) {
 766   // Before we get into the guts of the C2I adapter, see if we should be here
 767   // at all.  We've come from compiled code and are attempting to jump to the
 768   // interpreter, which means the caller made a static call to get here
 769   // (vcalls always get a compiled target if there is one).  Check for a
 770   // compiled target.  If there is one, we need to patch the caller's call.
 771   patch_callers_callsite(masm);
 772 
 773   __ bind(skip_fixup);
 774 
 775   bool has_value_argument = false;
 776   if (ValueTypePassFieldsAsArgs) {
 777     // Is there a value type argument?
 778     for (int i = 0; i < sig_extended.length() && !has_value_argument; i++) {
 779       has_value_argument = (sig_extended.at(i)._bt == T_VALUETYPE);
 780     }
 781     if (has_value_argument) {
 782       // There is at least a value type argument: we're coming from
 783       // compiled code so we have no buffers to back the value
 784       // types. Allocate the buffers here with a runtime call.
 785       oop_maps = new OopMapSet();
 786       OopMap* map = NULL;
 787 
 788       map = RegisterSaver::save_live_registers(masm, 0, &frame_size_in_words);
 789 
 790       frame_complete = __ offset();
 791 
 792       __ set_last_Java_frame(noreg, noreg, NULL);
 793 
 794       __ mov(c_rarg0, r15_thread);
 795       __ mov(c_rarg1, rbx);
 796 
 797       __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::allocate_value_types)));
 798 
 799       oop_maps->add_gc_map((int)(__ pc() - start), map);


 836 
 837   __ subptr(rsp, extraspace);
 838 
 839   // Store the return address in the expected location
 840   __ movptr(Address(rsp, 0), rax);
 841 
 842   // Now write the args into the outgoing interpreter space
 843 
 844   // next_arg_comp is the next argument from the compiler point of
 845   // view (value type fields are passed in registers/on the stack). In
 846   // sig_extended, a value type argument starts with: T_VALUETYPE,
 847   // followed by the types of the fields of the value type and T_VOID
 848   // to mark the end of the value type. ignored counts the number of
 849   // T_VALUETYPE/T_VOID. next_vt_arg is the next value type argument:
 850   // used to get the buffer for that argument from the pool of buffers
 851   // we allocated above and want to pass to the
 852   // interpreter. next_arg_int is the next argument from the
 853   // interpreter point of view (value types are passed by reference).
 854   bool has_oop_field = false;
 855   for (int next_arg_comp = 0, ignored = 0, next_vt_arg = 0, next_arg_int = 0;
 856        next_arg_comp < sig_extended.length(); next_arg_comp++) {
 857     assert(ignored <= next_arg_comp, "shouldn't skip over more slot than there are arguments");
 858     assert(next_arg_int < total_args_passed, "more arguments for the interpreter than expected?");
 859     BasicType bt = sig_extended.at(next_arg_comp)._bt;
 860     int st_off = (total_args_passed - next_arg_int) * Interpreter::stackElementSize;
 861     if (!ValueTypePassFieldsAsArgs || bt != T_VALUETYPE) {



 862       int next_off = st_off - Interpreter::stackElementSize;
 863       const int offset = (bt == T_LONG || bt == T_DOUBLE) ? next_off : st_off;
 864       const VMRegPair reg_pair = regs[next_arg_comp-ignored];
 865       size_t size_in_bytes = reg_pair.second()->is_valid() ? 8 : 4;
 866       gen_c2i_adapter_helper(masm, bt, next_arg_comp > 0 ? sig_extended.at(next_arg_comp-1)._bt : T_ILLEGAL,
 867                              size_in_bytes, reg_pair, Address(rsp, offset), extraspace, false);
 868       next_arg_int++;
 869 #ifdef ASSERT
 870       if (bt == T_LONG || bt == T_DOUBLE) {
 871         // Overwrite the unused slot with known junk
 872         __ mov64(rax, CONST64(0xdeadffffdeadaaaa));
 873         __ movptr(Address(rsp, st_off), rax);
 874       }
 875 #endif /* ASSERT */
 876     } else {
 877       ignored++;
 878       // get the buffer from the just allocated pool of buffers
 879       int index = arrayOopDesc::base_offset_in_bytes(T_OBJECT) + next_vt_arg * type2aelembytes(T_VALUETYPE);
 880       __ load_heap_oop(r11, Address(r10, index));
 881       next_vt_arg++; next_arg_int++;
 882       int vt = 1;
 883       // write fields we get from compiled code in registers/stack
 884       // slots to the buffer: we know we are done with that value type
 885       // argument when we hit the T_VOID that acts as an end of value
 886       // type delimiter for this value type. Value types are flattened
 887       // so we might encounter embedded value types. Each entry in
 888       // sig_extended contains a field offset in the buffer.
 889       do {
 890         next_arg_comp++;
 891         BasicType bt = sig_extended.at(next_arg_comp)._bt;
 892         BasicType prev_bt = sig_extended.at(next_arg_comp-1)._bt;
 893         if (bt == T_VALUETYPE) {
 894           vt++;
 895           ignored++;
 896         } else if (bt == T_VOID &&
 897                    prev_bt != T_LONG &&
 898                    prev_bt != T_DOUBLE) {
 899           vt--;
 900           ignored++;


 901         } else {
 902           int off = sig_extended.at(next_arg_comp)._offset;
 903           assert(off > 0, "offset in object should be positive");
 904           size_t size_in_bytes = is_java_primitive(bt) ? type2aelembytes(bt) : wordSize;
 905           bool is_oop = (bt == T_OBJECT || bt == T_VALUETYPEPTR || bt == T_ARRAY);
 906           has_oop_field = has_oop_field || is_oop;
 907           gen_c2i_adapter_helper(masm, bt, next_arg_comp > 0 ? sig_extended.at(next_arg_comp-1)._bt : T_ILLEGAL,
 908                                  size_in_bytes, regs[next_arg_comp-ignored], Address(r11, off), extraspace, is_oop);
 909         }
 910       } while (vt != 0);
 911       // pass the buffer to the interpreter
 912       __ movptr(Address(rsp, st_off), r11);
 913     }
 914   }
 915 
 916   // If a value type was allocated and initialized, apply post barrier to all oop fields
 917   if (has_value_argument && has_oop_field) {
 918     __ push(r13); // save senderSP
 919     __ push(rbx); // save callee
 920     // Allocate argument register save area
 921     if (frame::arg_reg_save_area_bytes != 0) {
 922       __ subptr(rsp, frame::arg_reg_save_area_bytes);
 923     }
 924     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::apply_post_barriers), r15_thread, r10);
 925     // De-allocate argument register save area
 926     if (frame::arg_reg_save_area_bytes != 0) {
 927       __ addptr(rsp, frame::arg_reg_save_area_bytes);


 984       __ load_heap_oop(dst, from);
 985     } else {
 986       __ load_sized_value(dst, from, size_in_bytes, is_signed);
 987     }
 988     if (r_1->is_stack()) {
 989       // Convert stack slot to an SP offset (+ wordSize to account for return address)
 990       int st_off = r_1->reg2stack() * VMRegImpl::stack_slot_size + wordSize;
 991       __ movq(Address(rsp, st_off), dst);
 992     }
 993   } else {
 994     if (wide) {
 995       __ movdbl(r_1->as_XMMRegister(), from);
 996     } else {
 997       __ movflt(r_1->as_XMMRegister(), from);
 998     }
 999   }
1000 }
1001 
1002 void SharedRuntime::gen_i2c_adapter(MacroAssembler *masm,
1003                                     int comp_args_on_stack,
1004                                     const GrowableArray<SigEntry>& sig_extended,
1005                                     const VMRegPair *regs) {
1006 
1007   // Note: r13 contains the senderSP on entry. We must preserve it since
1008   // we may do a i2c -> c2i transition if we lose a race where compiled
1009   // code goes non-entrant while we get args ready.
1010   // In addition we use r13 to locate all the interpreter args as
1011   // we must align the stack to 16 bytes on an i2c entry else we
1012   // lose alignment we expect in all compiled code and register
1013   // save code can segv when fxsave instructions find improperly
1014   // aligned stack pointer.
1015 
1016   // Adapters can be frameless because they do not require the caller
1017   // to perform additional cleanup work, such as correcting the stack pointer.
1018   // An i2c adapter is frameless because the *caller* frame, which is interpreted,
1019   // routinely repairs its own stack pointer (from interpreter_frame_last_sp),
1020   // even if a callee has modified the stack pointer.
1021   // A c2i adapter is frameless because the *callee* frame, which is interpreted,
1022   // routinely repairs its caller's stack pointer (from sender_sp, which is set
1023   // up via the senderSP register).
1024   // In other words, if *either* the caller or callee is interpreted, we can


1062 
1063   // Must preserve original SP for loading incoming arguments because
1064   // we need to align the outgoing SP for compiled code.
1065   __ movptr(r11, rsp);
1066 
1067   // Cut-out for having no stack args.  Since up to 2 int/oop args are passed
1068   // in registers, we will occasionally have no stack args.
1069   int comp_words_on_stack = 0;
1070   if (comp_args_on_stack) {
1071     // Sig words on the stack are greater-than VMRegImpl::stack0.  Those in
1072     // registers are below.  By subtracting stack0, we either get a negative
1073     // number (all values in registers) or the maximum stack slot accessed.
1074 
1075     // Convert 4-byte c2 stack slots to words.
1076     comp_words_on_stack = align_up(comp_args_on_stack*VMRegImpl::stack_slot_size, wordSize)>>LogBytesPerWord;
1077     // Round up to miminum stack alignment, in wordSize
1078     comp_words_on_stack = align_up(comp_words_on_stack, 2);
1079     __ subptr(rsp, comp_words_on_stack * wordSize);
1080   }
1081 
1082 
1083   // Ensure compiled code always sees stack at proper alignment
1084   __ andptr(rsp, -16);
1085 
1086   // push the return address and misalign the stack that youngest frame always sees
1087   // as far as the placement of the call instruction
1088   __ push(rax);
1089 
1090   // Put saved SP in another register
1091   const Register saved_sp = rax;
1092   __ movptr(saved_sp, r11);
1093 
1094   // Will jump to the compiled code just as if compiled code was doing it.
1095   // Pre-load the register-jump target early, to schedule it better.





1096   __ movptr(r11, Address(rbx, in_bytes(Method::from_compiled_offset())));

1097 
1098 #if INCLUDE_JVMCI
1099   if (EnableJVMCI || UseAOT) {
1100     // check if this call should be routed towards a specific entry point
1101     __ cmpptr(Address(r15_thread, in_bytes(JavaThread::jvmci_alternate_call_target_offset())), 0);
1102     Label no_alternative_target;
1103     __ jcc(Assembler::equal, no_alternative_target);
1104     __ movptr(r11, Address(r15_thread, in_bytes(JavaThread::jvmci_alternate_call_target_offset())));
1105     __ movptr(Address(r15_thread, in_bytes(JavaThread::jvmci_alternate_call_target_offset())), 0);
1106     __ bind(no_alternative_target);
1107   }
1108 #endif // INCLUDE_JVMCI
1109 
1110   int total_args_passed = compute_total_args_passed_int(sig_extended);
1111   // Now generate the shuffle code.  Pick up all register args and move the
1112   // rest through the floating point stack top.
1113 
1114   // next_arg_comp is the next argument from the compiler point of
1115   // view (value type fields are passed in registers/on the stack). In
1116   // sig_extended, a value type argument starts with: T_VALUETYPE,
1117   // followed by the types of the fields of the value type and T_VOID
1118   // to mark the end of the value type. ignored counts the number of
1119   // T_VALUETYPE/T_VOID. next_arg_int is the next argument from the
1120   // interpreter point of view (value types are passed by reference).
1121   for (int next_arg_comp = 0, ignored = 0, next_arg_int = 0; next_arg_comp < sig_extended.length(); next_arg_comp++) {
1122     assert(ignored <= next_arg_comp, "shouldn't skip over more slot than there are arguments");
1123     assert(next_arg_int < total_args_passed, "more arguments from the interpreter than expected?");
1124     BasicType bt = sig_extended.at(next_arg_comp)._bt;
1125     int ld_off = (total_args_passed - next_arg_int)*Interpreter::stackElementSize;
1126     if (!ValueTypePassFieldsAsArgs || bt != T_VALUETYPE) {
1127       // Load in argument order going down.
1128       // Point to interpreter value (vs. tag)



1129       int next_off = ld_off - Interpreter::stackElementSize;
1130       int offset = (bt == T_LONG || bt == T_DOUBLE) ? next_off : ld_off;
1131       const VMRegPair reg_pair = regs[next_arg_comp-ignored];
1132       size_t size_in_bytes = reg_pair.second()->is_valid() ? 8 : 4;
1133       gen_i2c_adapter_helper(masm, bt, next_arg_comp > 0 ? sig_extended.at(next_arg_comp-1)._bt : T_ILLEGAL,
1134                              size_in_bytes, reg_pair, Address(saved_sp, offset), false);
1135       next_arg_int++;
1136     } else {
1137       next_arg_int++;
1138       ignored++;
1139       // get the buffer for that value type
1140       __ movptr(r10, Address(saved_sp, ld_off));
1141       int vt = 1;
1142       // load fields to registers/stack slots from the buffer: we know
1143       // we are done with that value type argument when we hit the
1144       // T_VOID that acts as an end of value type delimiter for this
1145       // value type. Value types are flattened so we might encounter
1146       // embedded value types. Each entry in sig_extended contains a
1147       // field offset in the buffer.
1148       do {
1149         next_arg_comp++;
1150         BasicType bt = sig_extended.at(next_arg_comp)._bt;
1151         BasicType prev_bt = sig_extended.at(next_arg_comp-1)._bt;
1152         if (bt == T_VALUETYPE) {
1153           vt++;
1154           ignored++;
1155         } else if (bt == T_VOID &&
1156                    prev_bt != T_LONG &&
1157                    prev_bt != T_DOUBLE) {
1158           vt--;
1159           ignored++;


1160         } else {
1161           int off = sig_extended.at(next_arg_comp)._offset;
1162           assert(off > 0, "offset in object should be positive");
1163           size_t size_in_bytes = is_java_primitive(bt) ? type2aelembytes(bt) : wordSize;
1164           bool is_oop = (bt == T_OBJECT || bt == T_VALUETYPEPTR || bt == T_ARRAY);
1165           gen_i2c_adapter_helper(masm, bt, prev_bt, size_in_bytes, regs[next_arg_comp - ignored], Address(r10, off), is_oop);
1166         }
1167       } while (vt != 0);
1168     }
1169   }
1170 
1171   // 6243940 We might end up in handle_wrong_method if
1172   // the callee is deoptimized as we race thru here. If that
1173   // happens we don't want to take a safepoint because the
1174   // caller frame will look interpreted and arguments are now
1175   // "compiled" so it is much better to make this transition
1176   // invisible to the stack walking code. Unfortunately if
1177   // we try and find the callee by normal means a safepoint
1178   // is possible. So we stash the desired callee in the thread
1179   // and the vm will find there should this case occur.
1180 
1181   __ movptr(Address(r15_thread, JavaThread::callee_target_offset()), rbx);
1182 
1183   // put Method* where a c2i would expect should we end up there
1184   // only needed because of c2 resolve stubs return Method* as a result in
1185   // rax
1186   __ mov(rax, rbx);
1187   __ jmp(r11);
1188 }
1189 
1190 // ---------------------------------------------------------------
1191 AdapterHandlerEntry* SharedRuntime::generate_i2c2i_adapters(MacroAssembler *masm,
1192                                                             int comp_args_on_stack,
1193                                                             const GrowableArray<SigEntry>& sig_extended,
1194                                                             const VMRegPair *regs,



1195                                                             AdapterFingerPrint* fingerprint,
1196                                                             AdapterBlob*& new_adapter) {
1197   address i2c_entry = __ pc();
1198 
1199   gen_i2c_adapter(masm, comp_args_on_stack, sig_extended, regs);






1200 
1201   // -------------------------------------------------------------------------
1202   // Generate a C2I adapter.  On entry we know rbx holds the Method* during calls
1203   // to the interpreter.  The args start out packed in the compiled layout.  They
1204   // need to be unpacked into the interpreter layout.  This will almost always
1205   // require some stack space.  We grow the current (compiled) stack, then repack
1206   // the args.  We  finally end in a jump to the generic interpreter entry point.
1207   // On exit from the interpreter, the interpreter will restore our SP (lest the
1208   // compiled code, which relys solely on SP and not RBP, get sick).
1209 
1210   address c2i_unverified_entry = __ pc();
1211   Label skip_fixup;
1212   Label ok;
1213 
1214   Register holder = rax;
1215   Register receiver = j_rarg0;
1216   Register temp = rbx;
1217 
1218   {
1219     __ load_klass(temp, receiver);
1220     __ cmpptr(temp, Address(holder, CompiledICHolder::holder_klass_offset()));
1221     __ movptr(rbx, Address(holder, CompiledICHolder::holder_metadata_offset()));
1222     __ jcc(Assembler::equal, ok);
1223     __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
1224 
1225     __ bind(ok);
1226     // Method might have been compiled since the call site was patched to
1227     // interpreted if that is the case treat it as a miss so we can get
1228     // the call site corrected.
1229     __ cmpptr(Address(rbx, in_bytes(Method::code_offset())), (int32_t)NULL_WORD);
1230     __ jcc(Assembler::equal, skip_fixup);
1231     __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
1232   }
1233 
1234   address c2i_entry = __ pc();

1235 
1236   OopMapSet* oop_maps = NULL;
1237   int frame_complete = CodeOffsets::frame_never_safe;
1238   int frame_size_in_words = 0;
1239   gen_c2i_adapter(masm, sig_extended, regs, skip_fixup, i2c_entry, oop_maps, frame_complete, frame_size_in_words);







1240 
1241   __ flush();
1242   new_adapter = AdapterBlob::create(masm->code(), frame_complete, frame_size_in_words, oop_maps);
1243 
1244   // If the method has value types arguments, save the extended signature as symbol in
1245   // the AdapterHandlerEntry to be used for scalarization of value type arguments.
1246   Symbol* extended_signature = NULL;
1247   bool has_value_argument = false;
1248   Thread* THREAD = Thread::current();
1249   ResourceMark rm(THREAD);
1250   int length = sig_extended.length();
1251   char* sig_str = NEW_RESOURCE_ARRAY(char, 2*length + 3);
1252   int idx = 0;
1253   sig_str[idx++] = '(';
1254   for (int index = 0; index < length; index++) {
1255     BasicType bt = sig_extended.at(index)._bt;
1256     if (bt == T_VALUETYPE) {
1257       has_value_argument = true;
1258     } else if (bt == T_VALUETYPEPTR) {
1259       has_value_argument = true;
1260       // non-flattened value type field
1261       sig_str[idx++] = type2char(T_VALUETYPE);
1262       sig_str[idx++] = ';';
1263     } else if (bt == T_VOID) {
1264       // Ignore
1265     } else {
1266       if (bt == T_ARRAY) {
1267         bt = T_OBJECT; // We don't know the element type, treat as Object
1268       }
1269       sig_str[idx++] = type2char(bt);
1270       if (bt == T_OBJECT) {
1271         sig_str[idx++] = ';';
1272       }
1273     }
1274   }
1275   sig_str[idx++] = ')';
1276   sig_str[idx++] = '\0';
1277   if (has_value_argument) {
1278     // Extended signature is only required if a value type argument is passed
1279     extended_signature = SymbolTable::new_permanent_symbol(sig_str, THREAD);
1280   }
1281 
1282   return AdapterHandlerLibrary::new_entry(fingerprint, i2c_entry, c2i_entry, c2i_unverified_entry, extended_signature);
1283 }
1284 
1285 int SharedRuntime::c_calling_convention(const BasicType *sig_bt,
1286                                          VMRegPair *regs,
1287                                          VMRegPair *regs2,
1288                                          int total_args_passed) {
1289   assert(regs2 == NULL, "not needed on x86");
1290 // We return the amount of VMRegImpl stack slots we need to reserve for all
1291 // the arguments NOT counting out_preserve_stack_slots.
1292 
1293 // NOTE: These arrays will have to change when c1 is ported
1294 #ifdef _WIN64
1295     static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {
1296       c_rarg0, c_rarg1, c_rarg2, c_rarg3
1297     };
1298     static const XMMRegister FP_ArgReg[Argument::n_float_register_parameters_c] = {
1299       c_farg0, c_farg1, c_farg2, c_farg3
1300     };
1301 #else
1302     static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {


4331   // rdx: exception pc
4332   // Jump to handler
4333 
4334   __ jmp(r8);
4335 
4336   // Make sure all code is generated
4337   masm->flush();
4338 
4339   // Set exception blob
4340   _exception_blob =  ExceptionBlob::create(&buffer, oop_maps, SimpleRuntimeFrame::framesize >> 1);
4341 }
4342 #endif // COMPILER2
4343 
4344 BufferedValueTypeBlob* SharedRuntime::generate_buffered_value_type_adapter(const ValueKlass* vk) {
4345   BufferBlob* buf = BufferBlob::create("value types pack/unpack", 16 * K);
4346   CodeBuffer buffer(buf);
4347   short buffer_locs[20];
4348   buffer.insts()->initialize_shared_locs((relocInfo*)buffer_locs,
4349                                          sizeof(buffer_locs)/sizeof(relocInfo));
4350 
4351   MacroAssembler _masm(&buffer);
4352   MacroAssembler* masm = &_masm;
4353 
4354   const Array<SigEntry>* sig_vk = vk->extended_sig();
4355   const Array<VMRegPair>* regs = vk->return_regs();
4356 
4357   int pack_fields_off = __ offset();
4358 
4359   int j = 1;
4360   for (int i = 0; i < sig_vk->length(); i++) {
4361     BasicType bt = sig_vk->at(i)._bt;
4362     if (bt == T_VALUETYPE) {
4363       continue;
4364     }
4365     if (bt == T_VOID) {
4366       if (sig_vk->at(i-1)._bt == T_LONG ||
4367           sig_vk->at(i-1)._bt == T_DOUBLE) {
4368         j++;
4369       }
4370       continue;
4371     }
4372     int off = sig_vk->at(i)._offset;

4373     VMRegPair pair = regs->at(j);
4374     VMReg r_1 = pair.first();
4375     VMReg r_2 = pair.second();
4376     Address to(rax, off);
4377     if (bt == T_FLOAT) {
4378       __ movflt(to, r_1->as_XMMRegister());
4379     } else if (bt == T_DOUBLE) {
4380       __ movdbl(to, r_1->as_XMMRegister());
4381     } else if (bt == T_OBJECT || bt == T_VALUETYPEPTR || bt == T_ARRAY) {
4382       __ store_heap_oop(to, r_1->as_Register());










4383     } else {
4384       assert(is_java_primitive(bt), "unexpected basic type");

4385       size_t size_in_bytes = type2aelembytes(bt);
4386       __ store_sized_value(to, r_1->as_Register(), size_in_bytes);
4387     }
4388     j++;
4389   }
4390   assert(j == regs->length(), "missed a field?");
4391 
4392   __ ret(0);
4393 
4394   int unpack_fields_off = __ offset();
4395 
4396   j = 1;
4397   for (int i = 0; i < sig_vk->length(); i++) {
4398     BasicType bt = sig_vk->at(i)._bt;
4399     if (bt == T_VALUETYPE) {
4400       continue;
4401     }
4402     if (bt == T_VOID) {
4403       if (sig_vk->at(i-1)._bt == T_LONG ||
4404           sig_vk->at(i-1)._bt == T_DOUBLE) {
4405         j++;
4406       }
4407       continue;
4408     }
4409     int off = sig_vk->at(i)._offset;

4410     VMRegPair pair = regs->at(j);
4411     VMReg r_1 = pair.first();
4412     VMReg r_2 = pair.second();
4413     Address from(rax, off);
4414     if (bt == T_FLOAT) {
4415       __ movflt(r_1->as_XMMRegister(), from);
4416     } else if (bt == T_DOUBLE) {
4417       __ movdbl(r_1->as_XMMRegister(), from);
4418     } else if (bt == T_OBJECT || bt == T_VALUETYPEPTR || bt == T_ARRAY) {

4419       __ load_heap_oop(r_1->as_Register(), from);
4420     } else {
4421       assert(is_java_primitive(bt), "unexpected basic type");

4422       size_t size_in_bytes = type2aelembytes(bt);
4423       __ load_sized_value(r_1->as_Register(), from, size_in_bytes, bt != T_CHAR && bt != T_BOOLEAN);
4424     }
4425     j++;
4426   }
4427   assert(j == regs->length(), "missed a field?");
4428 
4429   if (StressValueTypeReturnedAsFields) {
4430     __ load_klass(rax, rax);
4431     __ orptr(rax, 1);
4432   }
4433 
4434   __ ret(0);
4435 
4436   __ flush();
4437 
4438   return BufferedValueTypeBlob::create(&buffer, pack_fields_off, unpack_fields_off);
4439 }


 475     case T_SHORT:
 476     case T_INT:
 477       if (int_args < Argument::n_int_register_parameters_j) {
 478         regs[i].set1(INT_ArgReg[int_args++]->as_VMReg());
 479       } else {
 480         regs[i].set1(VMRegImpl::stack2reg(stk_args));
 481         stk_args += 2;
 482       }
 483       break;
 484     case T_VOID:
 485       // halves of T_LONG or T_DOUBLE
 486       assert(i != 0 && (sig_bt[i - 1] == T_LONG || sig_bt[i - 1] == T_DOUBLE), "expecting half");
 487       regs[i].set_bad();
 488       break;
 489     case T_LONG:
 490       assert((i + 1) < total_args_passed && sig_bt[i + 1] == T_VOID, "expecting half");
 491       // fall through
 492     case T_OBJECT:
 493     case T_ARRAY:
 494     case T_ADDRESS:


 495       if (int_args < Argument::n_int_register_parameters_j) {
 496         regs[i].set2(INT_ArgReg[int_args++]->as_VMReg());
 497       } else {
 498         regs[i].set2(VMRegImpl::stack2reg(stk_args));
 499         stk_args += 2;
 500       }
 501       break;
 502     case T_FLOAT:
 503       if (fp_args < Argument::n_float_register_parameters_j) {
 504         regs[i].set1(FP_ArgReg[fp_args++]->as_VMReg());
 505       } else {
 506         regs[i].set1(VMRegImpl::stack2reg(stk_args));
 507         stk_args += 2;
 508       }
 509       break;
 510     case T_DOUBLE:
 511       assert((i + 1) < total_args_passed && sig_bt[i + 1] == T_VOID, "expecting half");
 512       if (fp_args < Argument::n_float_register_parameters_j) {
 513         regs[i].set2(FP_ArgReg[fp_args++]->as_VMReg());
 514       } else {


 557       if (int_args < Argument::n_int_register_parameters_j+1) {
 558         regs[i].set1(INT_ArgReg[int_args]->as_VMReg());
 559         int_args++;
 560       } else {
 561         return -1;
 562       }
 563       break;
 564     case T_VOID:
 565       // halves of T_LONG or T_DOUBLE
 566       assert(i != 0 && (sig_bt[i - 1] == T_LONG || sig_bt[i - 1] == T_DOUBLE), "expecting half");
 567       regs[i].set_bad();
 568       break;
 569     case T_LONG:
 570       assert(sig_bt[i + 1] == T_VOID, "expecting half");
 571       // fall through
 572     case T_OBJECT:
 573     case T_VALUETYPE:
 574     case T_ARRAY:
 575     case T_ADDRESS:
 576     case T_METADATA:

 577       if (int_args < Argument::n_int_register_parameters_j+1) {
 578         regs[i].set2(INT_ArgReg[int_args]->as_VMReg());
 579         int_args++;
 580       } else {
 581         return -1;
 582       }
 583       break;
 584     case T_FLOAT:
 585       if (fp_args < Argument::n_float_register_parameters_j) {
 586         regs[i].set1(FP_ArgReg[fp_args]->as_VMReg());
 587         fp_args++;
 588       } else {
 589         return -1;
 590       }
 591       break;
 592     case T_DOUBLE:
 593       assert(sig_bt[i + 1] == T_VOID, "expecting half");
 594       if (fp_args < Argument::n_float_register_parameters_j) {
 595         regs[i].set2(FP_ArgReg[fp_args]->as_VMReg());
 596         fp_args++;


 636   __ mov(c_rarg0, rbx);
 637   __ mov(c_rarg1, rax);
 638   __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::fixup_callers_callsite)));
 639 
 640   // De-allocate argument register save area
 641   if (frame::arg_reg_save_area_bytes != 0) {
 642     __ addptr(rsp, frame::arg_reg_save_area_bytes);
 643   }
 644 
 645   __ vzeroupper();
 646   __ pop_CPU_state();
 647   // restore sp
 648   __ mov(rsp, r13);
 649   __ bind(L);
 650 }
 651 
 652 // For each value type argument, sig includes the list of fields of
 653 // the value type. This utility function computes the number of
 654 // arguments for the call if value types are passed by reference (the
 655 // calling convention the interpreter expects).
 656 static int compute_total_args_passed_int(const GrowableArray<SigEntry>* sig_extended) {
 657   int total_args_passed = 0;
 658   if (ValueTypePassFieldsAsArgs) {
 659     for (int i = 0; i < sig_extended->length(); i++) {
 660       BasicType bt = sig_extended->at(i)._bt;
 661       if (SigEntry::is_reserved_entry(sig_extended, i)) {
 662         // Ignore reserved entry
 663       } else if (bt == T_VALUETYPE) {
 664         // In sig_extended, a value type argument starts with:
 665         // T_VALUETYPE, followed by the types of the fields of the
 666         // value type and T_VOID to mark the end of the value
 667         // type. Value types are flattened so, for instance, in the
 668         // case of a value type with an int field and a value type
 669         // field that itself has 2 fields, an int and a long:
 670         // T_VALUETYPE T_INT T_VALUETYPE T_INT T_LONG T_VOID (second
 671         // slot for the T_LONG) T_VOID (inner T_VALUETYPE) T_VOID
 672         // (outer T_VALUETYPE)
 673         total_args_passed++;
 674         int vt = 1;
 675         do {
 676           i++;
 677           BasicType bt = sig_extended->at(i)._bt;
 678           BasicType prev_bt = sig_extended->at(i-1)._bt;
 679           if (bt == T_VALUETYPE) {
 680             vt++;
 681           } else if (bt == T_VOID &&
 682                      prev_bt != T_LONG &&
 683                      prev_bt != T_DOUBLE) {
 684             vt--;
 685           }
 686         } while (vt != 0);
 687       } else {
 688         total_args_passed++;
 689       }
 690     }
 691   } else {
 692     total_args_passed = sig_extended->length();
 693   }
 694   return total_args_passed;
 695 }
 696 
 697 
 698 static void gen_c2i_adapter_helper(MacroAssembler* masm,
 699                                    BasicType bt,
 700                                    BasicType prev_bt,
 701                                    size_t size_in_bytes,
 702                                    const VMRegPair& reg_pair,
 703                                    const Address& to,
 704                                    int extraspace,
 705                                    bool is_oop) {
 706   assert(bt != T_VALUETYPE || !ValueTypePassFieldsAsArgs, "no value type here");
 707   if (bt == T_VOID) {
 708     assert(prev_bt == T_LONG || prev_bt == T_DOUBLE, "missing half");
 709     return;
 710   }
 711 
 712   // Say 4 args:


 724 
 725   bool wide = (size_in_bytes == wordSize);
 726   VMReg r_1 = reg_pair.first();
 727   VMReg r_2 = reg_pair.second();
 728   assert(r_2->is_valid() == wide, "invalid size");
 729   if (!r_1->is_valid()) {
 730     assert(!r_2->is_valid(), "must be invalid");
 731     return;
 732   }
 733 
 734   if (!r_1->is_XMMRegister()) {
 735     Register val = rax;
 736     assert_different_registers(to.base(), val);
 737     if(r_1->is_stack()) {
 738       int ld_off = r_1->reg2stack() * VMRegImpl::stack_slot_size + extraspace;
 739       __ load_sized_value(val, Address(rsp, ld_off), size_in_bytes, /* is_signed */ false);
 740     } else {
 741       val = r_1->as_Register();
 742     }
 743     if (is_oop) {
 744       // We don't need barriers because the destination is a newly allocated object.
 745       // Also, we cannot use store_heap_oop(to, val) because it uses r8 as tmp.
 746       if (UseCompressedOops) {
 747         __ encode_heap_oop(val);
 748         __ movl(to, val);
 749       } else {
 750         __ movptr(to, val);
 751       }
 752     } else {
 753       __ store_sized_value(to, val, size_in_bytes);
 754     }
 755   } else {
 756     if (wide) {
 757       __ movdbl(to, r_1->as_XMMRegister());
 758     } else {
 759       __ movflt(to, r_1->as_XMMRegister());
 760     }
 761   }
 762 }
 763 
 764 static void gen_c2i_adapter(MacroAssembler *masm,
 765                             const GrowableArray<SigEntry>* sig_extended,
 766                             const VMRegPair *regs,
 767                             Label& skip_fixup,
 768                             address start,
 769                             OopMapSet*& oop_maps,
 770                             int& frame_complete,
 771                             int& frame_size_in_words) {
 772   // Before we get into the guts of the C2I adapter, see if we should be here
 773   // at all.  We've come from compiled code and are attempting to jump to the
 774   // interpreter, which means the caller made a static call to get here
 775   // (vcalls always get a compiled target if there is one).  Check for a
 776   // compiled target.  If there is one, we need to patch the caller's call.
 777   patch_callers_callsite(masm);
 778 
 779   __ bind(skip_fixup);
 780 
 781   bool has_value_argument = false;
 782   if (ValueTypePassFieldsAsArgs) {
 783     // Is there a value type argument?
 784     for (int i = 0; i < sig_extended->length() && !has_value_argument; i++) {
 785       has_value_argument = (sig_extended->at(i)._bt == T_VALUETYPE);
 786     }
 787     if (has_value_argument) {
 788       // There is at least a value type argument: we're coming from
 789       // compiled code so we have no buffers to back the value
 790       // types. Allocate the buffers here with a runtime call.
 791       oop_maps = new OopMapSet();
 792       OopMap* map = NULL;
 793 
 794       map = RegisterSaver::save_live_registers(masm, 0, &frame_size_in_words);
 795 
 796       frame_complete = __ offset();
 797 
 798       __ set_last_Java_frame(noreg, noreg, NULL);
 799 
 800       __ mov(c_rarg0, r15_thread);
 801       __ mov(c_rarg1, rbx);
 802 
 803       __ call(RuntimeAddress(CAST_FROM_FN_PTR(address, SharedRuntime::allocate_value_types)));
 804 
 805       oop_maps->add_gc_map((int)(__ pc() - start), map);


 842 
 843   __ subptr(rsp, extraspace);
 844 
 845   // Store the return address in the expected location
 846   __ movptr(Address(rsp, 0), rax);
 847 
 848   // Now write the args into the outgoing interpreter space
 849 
 850   // next_arg_comp is the next argument from the compiler point of
 851   // view (value type fields are passed in registers/on the stack). In
 852   // sig_extended, a value type argument starts with: T_VALUETYPE,
 853   // followed by the types of the fields of the value type and T_VOID
 854   // to mark the end of the value type. ignored counts the number of
 855   // T_VALUETYPE/T_VOID. next_vt_arg is the next value type argument:
 856   // used to get the buffer for that argument from the pool of buffers
 857   // we allocated above and want to pass to the
 858   // interpreter. next_arg_int is the next argument from the
 859   // interpreter point of view (value types are passed by reference).
 860   bool has_oop_field = false;
 861   for (int next_arg_comp = 0, ignored = 0, next_vt_arg = 0, next_arg_int = 0;
 862        next_arg_comp < sig_extended->length(); next_arg_comp++) {
 863     assert(ignored <= next_arg_comp, "shouldn't skip over more slots than there are arguments");
 864     assert(next_arg_int <= total_args_passed, "more arguments for the interpreter than expected?");
 865     BasicType bt = sig_extended->at(next_arg_comp)._bt;
 866     int st_off = (total_args_passed - next_arg_int) * Interpreter::stackElementSize;
 867     if (!ValueTypePassFieldsAsArgs || bt != T_VALUETYPE) {
 868       if (SigEntry::is_reserved_entry(sig_extended, next_arg_comp)) {
 869         continue; // Ignore reserved entry
 870       }
 871       int next_off = st_off - Interpreter::stackElementSize;
 872       const int offset = (bt == T_LONG || bt == T_DOUBLE) ? next_off : st_off;
 873       const VMRegPair reg_pair = regs[next_arg_comp-ignored];
 874       size_t size_in_bytes = reg_pair.second()->is_valid() ? 8 : 4;
 875       gen_c2i_adapter_helper(masm, bt, next_arg_comp > 0 ? sig_extended->at(next_arg_comp-1)._bt : T_ILLEGAL,
 876                              size_in_bytes, reg_pair, Address(rsp, offset), extraspace, false);
 877       next_arg_int++;
 878 #ifdef ASSERT
 879       if (bt == T_LONG || bt == T_DOUBLE) {
 880         // Overwrite the unused slot with known junk
 881         __ mov64(rax, CONST64(0xdeadffffdeadaaaa));
 882         __ movptr(Address(rsp, st_off), rax);
 883       }
 884 #endif /* ASSERT */
 885     } else {
 886       ignored++;
 887       // get the buffer from the just allocated pool of buffers
 888       int index = arrayOopDesc::base_offset_in_bytes(T_OBJECT) + next_vt_arg * type2aelembytes(T_VALUETYPE);
 889       __ load_heap_oop(r11, Address(r10, index));
 890       next_vt_arg++; next_arg_int++;
 891       int vt = 1;
 892       // write fields we get from compiled code in registers/stack
 893       // slots to the buffer: we know we are done with that value type
 894       // argument when we hit the T_VOID that acts as an end of value
 895       // type delimiter for this value type. Value types are flattened
 896       // so we might encounter embedded value types. Each entry in
 897       // sig_extended contains a field offset in the buffer.
 898       do {
 899         next_arg_comp++;
 900         BasicType bt = sig_extended->at(next_arg_comp)._bt;
 901         BasicType prev_bt = sig_extended->at(next_arg_comp-1)._bt;
 902         if (bt == T_VALUETYPE) {
 903           vt++;
 904           ignored++;
 905         } else if (bt == T_VOID &&
 906                    prev_bt != T_LONG &&
 907                    prev_bt != T_DOUBLE) {
 908           vt--;
 909           ignored++;
 910         } else if (SigEntry::is_reserved_entry(sig_extended, next_arg_comp)) {
 911           // Ignore reserved entry
 912         } else {
 913           int off = sig_extended->at(next_arg_comp)._offset;
 914           assert(off > 0, "offset in object should be positive");
 915           size_t size_in_bytes = is_java_primitive(bt) ? type2aelembytes(bt) : wordSize;
 916           bool is_oop = (bt == T_OBJECT || bt == T_ARRAY);
 917           has_oop_field = has_oop_field || is_oop;
 918           gen_c2i_adapter_helper(masm, bt, next_arg_comp > 0 ? sig_extended->at(next_arg_comp-1)._bt : T_ILLEGAL,
 919                                  size_in_bytes, regs[next_arg_comp-ignored], Address(r11, off), extraspace, is_oop);
 920         }
 921       } while (vt != 0);
 922       // pass the buffer to the interpreter
 923       __ movptr(Address(rsp, st_off), r11);
 924     }
 925   }
 926 
 927   // If a value type was allocated and initialized, apply post barrier to all oop fields
 928   if (has_value_argument && has_oop_field) {
 929     __ push(r13); // save senderSP
 930     __ push(rbx); // save callee
 931     // Allocate argument register save area
 932     if (frame::arg_reg_save_area_bytes != 0) {
 933       __ subptr(rsp, frame::arg_reg_save_area_bytes);
 934     }
 935     __ call_VM_leaf(CAST_FROM_FN_PTR(address, SharedRuntime::apply_post_barriers), r15_thread, r10);
 936     // De-allocate argument register save area
 937     if (frame::arg_reg_save_area_bytes != 0) {
 938       __ addptr(rsp, frame::arg_reg_save_area_bytes);


 995       __ load_heap_oop(dst, from);
 996     } else {
 997       __ load_sized_value(dst, from, size_in_bytes, is_signed);
 998     }
 999     if (r_1->is_stack()) {
1000       // Convert stack slot to an SP offset (+ wordSize to account for return address)
1001       int st_off = r_1->reg2stack() * VMRegImpl::stack_slot_size + wordSize;
1002       __ movq(Address(rsp, st_off), dst);
1003     }
1004   } else {
1005     if (wide) {
1006       __ movdbl(r_1->as_XMMRegister(), from);
1007     } else {
1008       __ movflt(r_1->as_XMMRegister(), from);
1009     }
1010   }
1011 }
1012 
1013 void SharedRuntime::gen_i2c_adapter(MacroAssembler *masm,
1014                                     int comp_args_on_stack,
1015                                     const GrowableArray<SigEntry>* sig,
1016                                     const VMRegPair *regs) {
1017 
1018   // Note: r13 contains the senderSP on entry. We must preserve it since
1019   // we may do a i2c -> c2i transition if we lose a race where compiled
1020   // code goes non-entrant while we get args ready.
1021   // In addition we use r13 to locate all the interpreter args as
1022   // we must align the stack to 16 bytes on an i2c entry else we
1023   // lose alignment we expect in all compiled code and register
1024   // save code can segv when fxsave instructions find improperly
1025   // aligned stack pointer.
1026 
1027   // Adapters can be frameless because they do not require the caller
1028   // to perform additional cleanup work, such as correcting the stack pointer.
1029   // An i2c adapter is frameless because the *caller* frame, which is interpreted,
1030   // routinely repairs its own stack pointer (from interpreter_frame_last_sp),
1031   // even if a callee has modified the stack pointer.
1032   // A c2i adapter is frameless because the *callee* frame, which is interpreted,
1033   // routinely repairs its caller's stack pointer (from sender_sp, which is set
1034   // up via the senderSP register).
1035   // In other words, if *either* the caller or callee is interpreted, we can


1073 
1074   // Must preserve original SP for loading incoming arguments because
1075   // we need to align the outgoing SP for compiled code.
1076   __ movptr(r11, rsp);
1077 
1078   // Cut-out for having no stack args.  Since up to 2 int/oop args are passed
1079   // in registers, we will occasionally have no stack args.
1080   int comp_words_on_stack = 0;
1081   if (comp_args_on_stack) {
1082     // Sig words on the stack are greater-than VMRegImpl::stack0.  Those in
1083     // registers are below.  By subtracting stack0, we either get a negative
1084     // number (all values in registers) or the maximum stack slot accessed.
1085 
1086     // Convert 4-byte c2 stack slots to words.
1087     comp_words_on_stack = align_up(comp_args_on_stack*VMRegImpl::stack_slot_size, wordSize)>>LogBytesPerWord;
1088     // Round up to miminum stack alignment, in wordSize
1089     comp_words_on_stack = align_up(comp_words_on_stack, 2);
1090     __ subptr(rsp, comp_words_on_stack * wordSize);
1091   }
1092 

1093   // Ensure compiled code always sees stack at proper alignment
1094   __ andptr(rsp, -16);
1095 
1096   // push the return address and misalign the stack that youngest frame always sees
1097   // as far as the placement of the call instruction
1098   __ push(rax);
1099 
1100   // Put saved SP in another register
1101   const Register saved_sp = rax;
1102   __ movptr(saved_sp, r11);
1103 
1104   // Will jump to the compiled code just as if compiled code was doing it.
1105   // Pre-load the register-jump target early, to schedule it better.
1106   if (StressValueTypePassFieldsAsArgs) {
1107     // For stress testing, don't unpack value types in the i2c adapter but
1108     // call the value type entry point and let it take care of unpacking.
1109     __ movptr(r11, Address(rbx, in_bytes(Method::from_compiled_value_offset())));
1110   } else {
1111     __ movptr(r11, Address(rbx, in_bytes(Method::from_compiled_offset())));
1112   }
1113 
1114 #if INCLUDE_JVMCI
1115   if (EnableJVMCI || UseAOT) {
1116     // check if this call should be routed towards a specific entry point
1117     __ cmpptr(Address(r15_thread, in_bytes(JavaThread::jvmci_alternate_call_target_offset())), 0);
1118     Label no_alternative_target;
1119     __ jcc(Assembler::equal, no_alternative_target);
1120     __ movptr(r11, Address(r15_thread, in_bytes(JavaThread::jvmci_alternate_call_target_offset())));
1121     __ movptr(Address(r15_thread, in_bytes(JavaThread::jvmci_alternate_call_target_offset())), 0);
1122     __ bind(no_alternative_target);
1123   }
1124 #endif // INCLUDE_JVMCI
1125 
1126   int total_args_passed = compute_total_args_passed_int(sig);
1127   // Now generate the shuffle code.  Pick up all register args and move the
1128   // rest through the floating point stack top.
1129 
1130   // next_arg_comp is the next argument from the compiler point of
1131   // view (value type fields are passed in registers/on the stack). In
1132   // sig_extended, a value type argument starts with: T_VALUETYPE,
1133   // followed by the types of the fields of the value type and T_VOID
1134   // to mark the end of the value type. ignored counts the number of
1135   // T_VALUETYPE/T_VOID. next_arg_int is the next argument from the
1136   // interpreter point of view (value types are passed by reference).
1137   for (int next_arg_comp = 0, ignored = 0, next_arg_int = 0; next_arg_comp < sig->length(); next_arg_comp++) {
1138     assert(ignored <= next_arg_comp, "shouldn't skip over more slots than there are arguments");
1139     assert(next_arg_int <= total_args_passed, "more arguments from the interpreter than expected?");
1140     BasicType bt = sig->at(next_arg_comp)._bt;
1141     int ld_off = (total_args_passed - next_arg_int)*Interpreter::stackElementSize;
1142     if (!ValueTypePassFieldsAsArgs || bt != T_VALUETYPE) {
1143       // Load in argument order going down.
1144       // Point to interpreter value (vs. tag)
1145       if (SigEntry::is_reserved_entry(sig, next_arg_comp)) {
1146         continue; // Ignore reserved entry
1147       }
1148       int next_off = ld_off - Interpreter::stackElementSize;
1149       int offset = (bt == T_LONG || bt == T_DOUBLE) ? next_off : ld_off;
1150       const VMRegPair reg_pair = regs[next_arg_comp-ignored];
1151       size_t size_in_bytes = reg_pair.second()->is_valid() ? 8 : 4;
1152       gen_i2c_adapter_helper(masm, bt, next_arg_comp > 0 ? sig->at(next_arg_comp-1)._bt : T_ILLEGAL,
1153                              size_in_bytes, reg_pair, Address(saved_sp, offset), false);
1154       next_arg_int++;
1155     } else {
1156       next_arg_int++;
1157       ignored++;
1158       // get the buffer for that value type
1159       __ movptr(r10, Address(saved_sp, ld_off));
1160       int vt = 1;
1161       // load fields to registers/stack slots from the buffer: we know
1162       // we are done with that value type argument when we hit the
1163       // T_VOID that acts as an end of value type delimiter for this
1164       // value type. Value types are flattened so we might encounter
1165       // embedded value types. Each entry in sig_extended contains a
1166       // field offset in the buffer.
1167       do {
1168         next_arg_comp++;
1169         BasicType bt = sig->at(next_arg_comp)._bt;
1170         BasicType prev_bt = sig->at(next_arg_comp-1)._bt;
1171         if (bt == T_VALUETYPE) {
1172           vt++;
1173           ignored++;
1174         } else if (bt == T_VOID &&
1175                    prev_bt != T_LONG &&
1176                    prev_bt != T_DOUBLE) {
1177           vt--;
1178           ignored++;
1179         } else if (SigEntry::is_reserved_entry(sig, next_arg_comp)) {
1180           // Ignore reserved entry
1181         } else {
1182           int off = sig->at(next_arg_comp)._offset;
1183           assert(off > 0, "offset in object should be positive");
1184           size_t size_in_bytes = is_java_primitive(bt) ? type2aelembytes(bt) : wordSize;
1185           bool is_oop = (bt == T_OBJECT || bt == T_ARRAY);
1186           gen_i2c_adapter_helper(masm, bt, prev_bt, size_in_bytes, regs[next_arg_comp - ignored], Address(r10, off), is_oop);
1187         }
1188       } while (vt != 0);
1189     }
1190   }
1191 
1192   // 6243940 We might end up in handle_wrong_method if
1193   // the callee is deoptimized as we race thru here. If that
1194   // happens we don't want to take a safepoint because the
1195   // caller frame will look interpreted and arguments are now
1196   // "compiled" so it is much better to make this transition
1197   // invisible to the stack walking code. Unfortunately if
1198   // we try and find the callee by normal means a safepoint
1199   // is possible. So we stash the desired callee in the thread
1200   // and the vm will find there should this case occur.
1201 
1202   __ movptr(Address(r15_thread, JavaThread::callee_target_offset()), rbx);
1203 
1204   // put Method* where a c2i would expect should we end up there
1205   // only needed because of c2 resolve stubs return Method* as a result in
1206   // rax
1207   __ mov(rax, rbx);
1208   __ jmp(r11);
1209 }
1210 
1211 // ---------------------------------------------------------------
1212 AdapterHandlerEntry* SharedRuntime::generate_i2c2i_adapters(MacroAssembler *masm,
1213                                                             int comp_args_on_stack,
1214                                                             int comp_args_on_stack_cc,
1215                                                             const GrowableArray<SigEntry>* sig,
1216                                                             const VMRegPair* regs,
1217                                                             const GrowableArray<SigEntry>* sig_cc,
1218                                                             const VMRegPair* regs_cc,
1219                                                             AdapterFingerPrint* fingerprint,
1220                                                             AdapterBlob*& new_adapter) {
1221   address i2c_entry = __ pc();
1222 
1223   if (StressValueTypePassFieldsAsArgs) {
1224     // For stress testing, don't unpack value types in the i2c adapter but
1225     // call the value type entry point and let it take care of unpacking.
1226     gen_i2c_adapter(masm, comp_args_on_stack, sig, regs);
1227   } else {
1228     gen_i2c_adapter(masm, comp_args_on_stack_cc, sig_cc, regs_cc);
1229   }
1230 
1231   // -------------------------------------------------------------------------
1232   // Generate a C2I adapter.  On entry we know rbx holds the Method* during calls
1233   // to the interpreter.  The args start out packed in the compiled layout.  They
1234   // need to be unpacked into the interpreter layout.  This will almost always
1235   // require some stack space.  We grow the current (compiled) stack, then repack
1236   // the args.  We  finally end in a jump to the generic interpreter entry point.
1237   // On exit from the interpreter, the interpreter will restore our SP (lest the
1238   // compiled code, which relys solely on SP and not RBP, get sick).
1239 
1240   address c2i_unverified_entry = __ pc();
1241   Label skip_fixup;
1242   Label ok;
1243 
1244   Register holder = rax;
1245   Register receiver = j_rarg0;
1246   Register temp = rbx;
1247 
1248   {
1249     __ load_klass(temp, receiver);
1250     __ cmpptr(temp, Address(holder, CompiledICHolder::holder_klass_offset()));
1251     __ movptr(rbx, Address(holder, CompiledICHolder::holder_metadata_offset()));
1252     __ jcc(Assembler::equal, ok);
1253     __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
1254 
1255     __ bind(ok);
1256     // Method might have been compiled since the call site was patched to
1257     // interpreted if that is the case treat it as a miss so we can get
1258     // the call site corrected.
1259     __ cmpptr(Address(rbx, in_bytes(Method::code_offset())), (int32_t)NULL_WORD);
1260     __ jcc(Assembler::equal, skip_fixup);
1261     __ jump(RuntimeAddress(SharedRuntime::get_ic_miss_stub()));
1262   }
1263 
1264   address c2i_entry = __ pc();
1265   address c2i_value_entry = c2i_entry;
1266 
1267   OopMapSet* oop_maps = NULL;
1268   int frame_complete = CodeOffsets::frame_never_safe;
1269   int frame_size_in_words = 0;
1270   gen_c2i_adapter(masm, sig_cc, regs_cc, skip_fixup, i2c_entry, oop_maps, frame_complete, frame_size_in_words);
1271 
1272   if (regs != regs_cc) {
1273     // Non-scalarized c2i adapter
1274     c2i_value_entry = __ pc();
1275     Label unused;
1276     gen_c2i_adapter(masm, sig, regs, unused, i2c_entry, oop_maps, frame_complete, frame_size_in_words);
1277   }
1278 
1279   __ flush();

1280 
1281   // The c2i adapter might safepoint and trigger a GC. The caller must make sure that
1282   // the GC knows about the location of oop argument locations passed to the c2i adapter.
1283   bool caller_must_gc_arguments = (regs != regs_cc);
1284   new_adapter = AdapterBlob::create(masm->code(), frame_complete, frame_size_in_words, oop_maps, caller_must_gc_arguments);

































1285 
1286   return AdapterHandlerLibrary::new_entry(fingerprint, i2c_entry, c2i_entry, c2i_value_entry, c2i_unverified_entry);
1287 }
1288 
1289 int SharedRuntime::c_calling_convention(const BasicType *sig_bt,
1290                                          VMRegPair *regs,
1291                                          VMRegPair *regs2,
1292                                          int total_args_passed) {
1293   assert(regs2 == NULL, "not needed on x86");
1294 // We return the amount of VMRegImpl stack slots we need to reserve for all
1295 // the arguments NOT counting out_preserve_stack_slots.
1296 
1297 // NOTE: These arrays will have to change when c1 is ported
1298 #ifdef _WIN64
1299     static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {
1300       c_rarg0, c_rarg1, c_rarg2, c_rarg3
1301     };
1302     static const XMMRegister FP_ArgReg[Argument::n_float_register_parameters_c] = {
1303       c_farg0, c_farg1, c_farg2, c_farg3
1304     };
1305 #else
1306     static const Register INT_ArgReg[Argument::n_int_register_parameters_c] = {


4335   // rdx: exception pc
4336   // Jump to handler
4337 
4338   __ jmp(r8);
4339 
4340   // Make sure all code is generated
4341   masm->flush();
4342 
4343   // Set exception blob
4344   _exception_blob =  ExceptionBlob::create(&buffer, oop_maps, SimpleRuntimeFrame::framesize >> 1);
4345 }
4346 #endif // COMPILER2
4347 
4348 BufferedValueTypeBlob* SharedRuntime::generate_buffered_value_type_adapter(const ValueKlass* vk) {
4349   BufferBlob* buf = BufferBlob::create("value types pack/unpack", 16 * K);
4350   CodeBuffer buffer(buf);
4351   short buffer_locs[20];
4352   buffer.insts()->initialize_shared_locs((relocInfo*)buffer_locs,
4353                                          sizeof(buffer_locs)/sizeof(relocInfo));
4354 
4355   MacroAssembler* masm = new MacroAssembler(&buffer);

4356 
4357   const Array<SigEntry>* sig_vk = vk->extended_sig();
4358   const Array<VMRegPair>* regs = vk->return_regs();
4359 
4360   int pack_fields_off = __ offset();
4361 
4362   int j = 1;
4363   for (int i = 0; i < sig_vk->length(); i++) {
4364     BasicType bt = sig_vk->at(i)._bt;
4365     if (bt == T_VALUETYPE) {
4366       continue;
4367     }
4368     if (bt == T_VOID) {
4369       if (sig_vk->at(i-1)._bt == T_LONG ||
4370           sig_vk->at(i-1)._bt == T_DOUBLE) {
4371         j++;
4372       }
4373       continue;
4374     }
4375     int off = sig_vk->at(i)._offset;
4376     assert(off > 0, "offset in object should be positive");
4377     VMRegPair pair = regs->at(j);
4378     VMReg r_1 = pair.first();
4379     VMReg r_2 = pair.second();
4380     Address to(rax, off);
4381     if (bt == T_FLOAT) {
4382       __ movflt(to, r_1->as_XMMRegister());
4383     } else if (bt == T_DOUBLE) {
4384       __ movdbl(to, r_1->as_XMMRegister());
4385     } else if (bt == T_OBJECT || bt == T_ARRAY) {
4386       Register val = r_1->as_Register();
4387       assert_different_registers(rax, val);
4388       // We don't need barriers because the destination is a newly allocated object.
4389       // Also, we cannot use store_heap_oop(to, val) because it uses r8 as tmp.
4390       if (UseCompressedOops) {
4391         __ encode_heap_oop(val);
4392         __ movl(to, val);
4393       } else {
4394         __ movptr(to, val);
4395       }
4396 
4397     } else {
4398       assert(is_java_primitive(bt), "unexpected basic type");
4399       assert_different_registers(rax, r_1->as_Register());
4400       size_t size_in_bytes = type2aelembytes(bt);
4401       __ store_sized_value(to, r_1->as_Register(), size_in_bytes);
4402     }
4403     j++;
4404   }
4405   assert(j == regs->length(), "missed a field?");
4406 
4407   __ ret(0);
4408 
4409   int unpack_fields_off = __ offset();
4410 
4411   j = 1;
4412   for (int i = 0; i < sig_vk->length(); i++) {
4413     BasicType bt = sig_vk->at(i)._bt;
4414     if (bt == T_VALUETYPE) {
4415       continue;
4416     }
4417     if (bt == T_VOID) {
4418       if (sig_vk->at(i-1)._bt == T_LONG ||
4419           sig_vk->at(i-1)._bt == T_DOUBLE) {
4420         j++;
4421       }
4422       continue;
4423     }
4424     int off = sig_vk->at(i)._offset;
4425     assert(off > 0, "offset in object should be positive");
4426     VMRegPair pair = regs->at(j);
4427     VMReg r_1 = pair.first();
4428     VMReg r_2 = pair.second();
4429     Address from(rax, off);
4430     if (bt == T_FLOAT) {
4431       __ movflt(r_1->as_XMMRegister(), from);
4432     } else if (bt == T_DOUBLE) {
4433       __ movdbl(r_1->as_XMMRegister(), from);
4434     } else if (bt == T_OBJECT || bt == T_ARRAY) {
4435       assert_different_registers(rax, r_1->as_Register());
4436       __ load_heap_oop(r_1->as_Register(), from);
4437     } else {
4438       assert(is_java_primitive(bt), "unexpected basic type");
4439       assert_different_registers(rax, r_1->as_Register());
4440       size_t size_in_bytes = type2aelembytes(bt);
4441       __ load_sized_value(r_1->as_Register(), from, size_in_bytes, bt != T_CHAR && bt != T_BOOLEAN);
4442     }
4443     j++;
4444   }
4445   assert(j == regs->length(), "missed a field?");
4446 
4447   if (StressValueTypeReturnedAsFields) {
4448     __ load_klass(rax, rax);
4449     __ orptr(rax, 1);
4450   }
4451 
4452   __ ret(0);
4453 
4454   __ flush();
4455 
4456   return BufferedValueTypeBlob::create(&buffer, pack_fields_off, unpack_fields_off);
4457 }
< prev index next >