270 // Emit code to verify that FP is pointing at a valid ricochet frame.
271 #ifdef ASSERT
272 enum {
273 ARG_LIMIT = 255, SLOP = 45,
274 // use this parameter for checking for garbage stack movements:
275 UNREASONABLE_STACK_MOVE = (ARG_LIMIT + SLOP)
276 // the slop defends against false alarms due to fencepost errors
277 };
278
279 void MethodHandles::RicochetFrame::verify_clean(MacroAssembler* _masm) {
280 // The stack should look like this:
281 // ... keep1 | dest=42 | keep2 | magic | handler | magic | recursive args | [RF]
282 // Check various invariants.
283
284 Register O7_temp = O7, O5_temp = O5;
285
286 Label L_ok_1, L_ok_2, L_ok_3, L_ok_4;
287 BLOCK_COMMENT("verify_clean {");
288 // Magic numbers must check out:
289 __ set((int32_t) MAGIC_NUMBER_1, O7_temp);
290 __ cmp(O7_temp, L0_magic_number_1);
291 __ br(Assembler::equal, false, Assembler::pt, L_ok_1);
292 __ delayed()->nop();
293 __ stop("damaged ricochet frame: MAGIC_NUMBER_1 not found");
294
295 __ BIND(L_ok_1);
296
297 // Arguments pointer must look reasonable:
298 #ifdef _LP64
299 Register FP_temp = O5_temp;
300 __ add(FP, STACK_BIAS, FP_temp);
301 #else
302 Register FP_temp = FP;
303 #endif
304 __ cmp(L4_saved_args_base, FP_temp);
305 __ br(Assembler::greaterEqualUnsigned, false, Assembler::pt, L_ok_2);
306 __ delayed()->nop();
307 __ stop("damaged ricochet frame: L4 < FP");
308
309 __ BIND(L_ok_2);
310 // Disable until we decide on it's fate
311 // __ sub(L4_saved_args_base, UNREASONABLE_STACK_MOVE * Interpreter::stackElementSize, O7_temp);
312 // __ cmp(O7_temp, FP_temp);
313 // __ br(Assembler::lessEqualUnsigned, false, Assembler::pt, L_ok_3);
314 // __ delayed()->nop();
315 // __ stop("damaged ricochet frame: (L4 - UNREASONABLE_STACK_MOVE) > FP");
316
317 __ BIND(L_ok_3);
318 extract_conversion_dest_type(_masm, L5_conversion, O7_temp);
319 __ cmp(O7_temp, T_VOID);
320 __ br(Assembler::equal, false, Assembler::pt, L_ok_4);
321 __ delayed()->nop();
322 extract_conversion_vminfo(_masm, L5_conversion, O5_temp);
323 __ ld_ptr(L4_saved_args_base, __ argument_offset(O5_temp, O5_temp), O7_temp);
324 assert(__ is_simm13(RETURN_VALUE_PLACEHOLDER), "must be simm13");
325 __ cmp(O7_temp, (int32_t) RETURN_VALUE_PLACEHOLDER);
326 __ brx(Assembler::equal, false, Assembler::pt, L_ok_4);
327 __ delayed()->nop();
328 __ stop("damaged ricochet frame: RETURN_VALUE_PLACEHOLDER not found");
329 __ BIND(L_ok_4);
330 BLOCK_COMMENT("} verify_clean");
331 }
332 #endif //ASSERT
333
334 void MethodHandles::load_klass_from_Class(MacroAssembler* _masm, Register klass_reg, Register temp_reg, Register temp2_reg) {
335 if (VerifyMethodHandles)
336 verify_klass(_masm, klass_reg, SystemDictionaryHandles::Class_klass(), temp_reg, temp2_reg,
337 "AMH argument is a Class");
338 __ load_heap_oop(Address(klass_reg, java_lang_Class::klass_offset_in_bytes()), klass_reg);
339 }
340
341 void MethodHandles::load_conversion_vminfo(MacroAssembler* _masm, Address conversion_field_addr, Register reg) {
342 assert(CONV_VMINFO_SHIFT == 0, "preshifted");
343 assert(CONV_VMINFO_MASK == right_n_bits(BitsPerByte), "else change type of following load");
344 __ ldub(conversion_field_addr.plus_disp(BytesPerInt - 1), reg);
345 }
346
347 void MethodHandles::extract_conversion_vminfo(MacroAssembler* _masm, Register conversion_field_reg, Register reg) {
348 assert(CONV_VMINFO_SHIFT == 0, "preshifted");
349 __ and3(conversion_field_reg, CONV_VMINFO_MASK, reg);
350 }
351
352 void MethodHandles::extract_conversion_dest_type(MacroAssembler* _masm, Register conversion_field_reg, Register reg) {
353 __ srl(conversion_field_reg, CONV_DEST_TYPE_SHIFT, reg);
354 __ and3(reg, 0x0F, reg);
355 }
356
357 void MethodHandles::load_stack_move(MacroAssembler* _masm,
358 Address G3_amh_conversion,
359 Register stack_move_reg) {
360 BLOCK_COMMENT("load_stack_move {");
361 __ ldsw(G3_amh_conversion, stack_move_reg);
362 __ sra(stack_move_reg, CONV_STACK_MOVE_SHIFT, stack_move_reg);
363 if (VerifyMethodHandles) {
364 Label L_ok, L_bad;
365 int32_t stack_move_limit = 0x0800; // extra-large
366 __ cmp(stack_move_reg, stack_move_limit);
367 __ br(Assembler::greaterEqual, false, Assembler::pn, L_bad);
368 __ delayed()->nop();
369 __ cmp(stack_move_reg, -stack_move_limit);
370 __ br(Assembler::greater, false, Assembler::pt, L_ok);
371 __ delayed()->nop();
372 __ BIND(L_bad);
373 __ stop("load_stack_move of garbage value");
374 __ BIND(L_ok);
375 }
376 BLOCK_COMMENT("} load_stack_move");
377 }
378
379 #ifdef ASSERT
380 void MethodHandles::RicochetFrame::verify() const {
381 assert(magic_number_1() == MAGIC_NUMBER_1, "");
382 if (!Universe::heap()->is_gc_active()) {
383 if (saved_args_layout() != NULL) {
384 assert(saved_args_layout()->is_method(), "must be valid oop");
385 }
386 if (saved_target() != NULL) {
387 assert(java_lang_invoke_MethodHandle::is_instance(saved_target()), "checking frame value");
388 }
389 }
390 int conv_op = adapter_conversion_op(conversion());
391 assert(conv_op == java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS ||
392 conv_op == java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS ||
393 conv_op == java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF,
394 "must be a sane conversion");
395 if (has_return_value_slot()) {
396 assert(*return_value_slot_addr() == RETURN_VALUE_PLACEHOLDER, "");
397 }
398 }
399
400 void MethodHandles::verify_argslot(MacroAssembler* _masm, Register argslot_reg, Register temp_reg, const char* error_message) {
401 // Verify that argslot lies within (Gargs, FP].
402 Label L_ok, L_bad;
403 BLOCK_COMMENT("verify_argslot {");
404 __ add(FP, STACK_BIAS, temp_reg); // STACK_BIAS is zero on !_LP64
405 __ cmp(argslot_reg, temp_reg);
406 __ brx(Assembler::greaterUnsigned, false, Assembler::pn, L_bad);
407 __ delayed()->nop();
408 __ cmp(Gargs, argslot_reg);
409 __ brx(Assembler::lessEqualUnsigned, false, Assembler::pt, L_ok);
410 __ delayed()->nop();
411 __ BIND(L_bad);
412 __ stop(error_message);
413 __ BIND(L_ok);
414 BLOCK_COMMENT("} verify_argslot");
415 }
416
417 void MethodHandles::verify_argslots(MacroAssembler* _masm,
418 RegisterOrConstant arg_slots,
419 Register arg_slot_base_reg,
420 Register temp_reg,
421 Register temp2_reg,
422 bool negate_argslots,
423 const char* error_message) {
424 // Verify that [argslot..argslot+size) lies within (Gargs, FP).
425 Label L_ok, L_bad;
426 BLOCK_COMMENT("verify_argslots {");
427 if (negate_argslots) {
428 if (arg_slots.is_constant()) {
429 arg_slots = -1 * arg_slots.as_constant();
430 } else {
431 __ neg(arg_slots.as_register(), temp_reg);
432 arg_slots = temp_reg;
433 }
434 }
435 __ add(arg_slot_base_reg, __ argument_offset(arg_slots, temp_reg), temp_reg);
436 __ add(FP, STACK_BIAS, temp2_reg); // STACK_BIAS is zero on !_LP64
437 __ cmp(temp_reg, temp2_reg);
438 __ brx(Assembler::greaterUnsigned, false, Assembler::pn, L_bad);
439 __ delayed()->nop();
440 // Gargs points to the first word so adjust by BytesPerWord
441 __ add(arg_slot_base_reg, BytesPerWord, temp_reg);
442 __ cmp(Gargs, temp_reg);
443 __ brx(Assembler::lessEqualUnsigned, false, Assembler::pt, L_ok);
444 __ delayed()->nop();
445 __ BIND(L_bad);
446 __ stop(error_message);
447 __ BIND(L_ok);
448 BLOCK_COMMENT("} verify_argslots");
449 }
450
451 // Make sure that arg_slots has the same sign as the given direction.
452 // If (and only if) arg_slots is a assembly-time constant, also allow it to be zero.
453 void MethodHandles::verify_stack_move(MacroAssembler* _masm,
454 RegisterOrConstant arg_slots, int direction) {
455 enum { UNREASONABLE_STACK_MOVE = 256 * 4 }; // limit of 255 arguments
456 bool allow_zero = arg_slots.is_constant();
457 if (direction == 0) { direction = +1; allow_zero = true; }
458 assert(stack_move_unit() == -1, "else add extra checks here");
459 if (arg_slots.is_register()) {
460 Label L_ok, L_bad;
461 BLOCK_COMMENT("verify_stack_move {");
462 // __ btst(-stack_move_unit() - 1, arg_slots.as_register()); // no need
463 // __ br(Assembler::notZero, false, Assembler::pn, L_bad);
464 // __ delayed()->nop();
485 BLOCK_COMMENT("} verify_stack_move");
486 } else {
487 intptr_t size = arg_slots.as_constant();
488 if (direction < 0) size = -size;
489 assert(size >= 0, "correct direction of constant move");
490 assert(size < UNREASONABLE_STACK_MOVE, "reasonable size of constant move");
491 }
492 }
493
494 void MethodHandles::verify_klass(MacroAssembler* _masm,
495 Register obj_reg, KlassHandle klass,
496 Register temp_reg, Register temp2_reg,
497 const char* error_message) {
498 oop* klass_addr = klass.raw_value();
499 assert(klass_addr >= SystemDictionaryHandles::Object_klass().raw_value() &&
500 klass_addr <= SystemDictionaryHandles::Long_klass().raw_value(),
501 "must be one of the SystemDictionaryHandles");
502 Label L_ok, L_bad;
503 BLOCK_COMMENT("verify_klass {");
504 __ verify_oop(obj_reg);
505 __ br_null(obj_reg, false, Assembler::pn, L_bad);
506 __ delayed()->nop();
507 __ load_klass(obj_reg, temp_reg);
508 __ set(ExternalAddress(klass_addr), temp2_reg);
509 __ ld_ptr(Address(temp2_reg, 0), temp2_reg);
510 __ cmp(temp_reg, temp2_reg);
511 __ brx(Assembler::equal, false, Assembler::pt, L_ok);
512 __ delayed()->nop();
513 intptr_t super_check_offset = klass->super_check_offset();
514 __ ld_ptr(Address(temp_reg, super_check_offset), temp_reg);
515 __ set(ExternalAddress(klass_addr), temp2_reg);
516 __ ld_ptr(Address(temp2_reg, 0), temp2_reg);
517 __ cmp(temp_reg, temp2_reg);
518 __ brx(Assembler::equal, false, Assembler::pt, L_ok);
519 __ delayed()->nop();
520 __ BIND(L_bad);
521 __ stop(error_message);
522 __ BIND(L_ok);
523 BLOCK_COMMENT("} verify_klass");
524 }
525 #endif // ASSERT
526
527
528 void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register target, Register temp) {
529 assert(method == G5_method, "interpreter calling convention");
530 __ verify_oop(method);
531 __ ld_ptr(G5_method, in_bytes(methodOopDesc::from_interpreted_offset()), target);
532 if (JvmtiExport::can_post_interpreter_events()) {
533 // JVMTI events, such as single-stepping, are implemented partly by avoiding running
534 // compiled code in threads for which the event is enabled. Check here for
535 // interp_only_mode if these events CAN be enabled.
536 __ verify_thread();
537 Label skip_compiled_code;
538
539 const Address interp_only(G2_thread, JavaThread::interp_only_mode_offset());
654 __ ldsw(vmargslot_addr, result);
655 }
656
657 static RegisterOrConstant adjust_SP_and_Gargs_down_by_slots(MacroAssembler* _masm,
658 RegisterOrConstant arg_slots,
659 Register temp_reg, Register temp2_reg) {
660 // Keep the stack pointer 2*wordSize aligned.
661 const int TwoWordAlignmentMask = right_n_bits(LogBytesPerWord + 1);
662 if (arg_slots.is_constant()) {
663 const int offset = arg_slots.as_constant() << LogBytesPerWord;
664 const int masked_offset = round_to(offset, 2 * BytesPerWord);
665 const int masked_offset2 = (offset + 1*BytesPerWord) & ~TwoWordAlignmentMask;
666 assert(masked_offset == masked_offset2, "must agree");
667 __ sub(Gargs, offset, Gargs);
668 __ sub(SP, masked_offset, SP );
669 return offset;
670 } else {
671 #ifdef ASSERT
672 {
673 Label L_ok;
674 __ cmp(arg_slots.as_register(), 0);
675 __ br(Assembler::greaterEqual, false, Assembler::pt, L_ok);
676 __ delayed()->nop();
677 __ stop("negative arg_slots");
678 __ bind(L_ok);
679 }
680 #endif
681 __ sll_ptr(arg_slots.as_register(), LogBytesPerWord, temp_reg);
682 __ add( temp_reg, 1*BytesPerWord, temp2_reg);
683 __ andn(temp2_reg, TwoWordAlignmentMask, temp2_reg);
684 __ sub(Gargs, temp_reg, Gargs);
685 __ sub(SP, temp2_reg, SP );
686 return temp_reg;
687 }
688 }
689
690 static RegisterOrConstant adjust_SP_and_Gargs_up_by_slots(MacroAssembler* _masm,
691 RegisterOrConstant arg_slots,
692 Register temp_reg, Register temp2_reg) {
693 // Keep the stack pointer 2*wordSize aligned.
694 const int TwoWordAlignmentMask = right_n_bits(LogBytesPerWord + 1);
695 if (arg_slots.is_constant()) {
696 const int offset = arg_slots.as_constant() << LogBytesPerWord;
731 // Make space on the stack for the inserted argument(s).
732 // Then pull down everything shallower than argslot_reg.
733 // The stacked return address gets pulled down with everything else.
734 // That is, copy [sp, argslot) downward by -size words. In pseudo-code:
735 // sp -= size;
736 // for (temp = sp + size; temp < argslot; temp++)
737 // temp[-size] = temp[0]
738 // argslot -= size;
739
740 // offset is temp3_reg in case of arg_slots being a register.
741 RegisterOrConstant offset = adjust_SP_and_Gargs_up_by_slots(_masm, arg_slots, temp3_reg, temp_reg);
742 __ sub(Gargs, offset, temp_reg); // source pointer for copy
743
744 {
745 Label loop;
746 __ BIND(loop);
747 // pull one word down each time through the loop
748 __ ld_ptr( Address(temp_reg, 0 ), temp2_reg);
749 __ st_ptr(temp2_reg, Address(temp_reg, offset) );
750 __ add(temp_reg, wordSize, temp_reg);
751 __ cmp(temp_reg, argslot_reg);
752 __ brx(Assembler::lessUnsigned, false, Assembler::pt, loop);
753 __ delayed()->nop(); // FILLME
754 }
755
756 // Now move the argslot down, to point to the opened-up space.
757 __ add(argslot_reg, offset, argslot_reg);
758 BLOCK_COMMENT("} insert_arg_slots");
759 }
760
761
762 // Helper to remove argument slots from the stack.
763 // arg_slots must be a multiple of stack_move_unit() and > 0
764 void MethodHandles::remove_arg_slots(MacroAssembler* _masm,
765 RegisterOrConstant arg_slots,
766 Register argslot_reg,
767 Register temp_reg, Register temp2_reg, Register temp3_reg) {
768 // allow constant zero
769 if (arg_slots.is_constant() && arg_slots.as_constant() == 0)
770 return;
771 assert_different_registers(argslot_reg, temp_reg, temp2_reg, temp3_reg,
772 (!arg_slots.is_register() ? Gargs : arg_slots.as_register()));
773
780
781 // Pull up everything shallower than argslot.
782 // Then remove the excess space on the stack.
783 // The stacked return address gets pulled up with everything else.
784 // That is, copy [sp, argslot) upward by size words. In pseudo-code:
785 // for (temp = argslot-1; temp >= sp; --temp)
786 // temp[size] = temp[0]
787 // argslot += size;
788 // sp += size;
789
790 RegisterOrConstant offset = __ regcon_sll_ptr(arg_slots, LogBytesPerWord, temp3_reg);
791 __ sub(argslot_reg, wordSize, temp_reg); // source pointer for copy
792
793 {
794 Label L_loop;
795 __ BIND(L_loop);
796 // pull one word up each time through the loop
797 __ ld_ptr( Address(temp_reg, 0 ), temp2_reg);
798 __ st_ptr(temp2_reg, Address(temp_reg, offset) );
799 __ sub(temp_reg, wordSize, temp_reg);
800 __ cmp(temp_reg, Gargs);
801 __ brx(Assembler::greaterEqualUnsigned, false, Assembler::pt, L_loop);
802 __ delayed()->nop(); // FILLME
803 }
804
805 // And adjust the argslot address to point at the deletion point.
806 __ add(argslot_reg, offset, argslot_reg);
807
808 // We don't need the offset at this point anymore, just adjust SP and Gargs.
809 (void) adjust_SP_and_Gargs_up_by_slots(_masm, arg_slots, temp3_reg, temp_reg);
810
811 BLOCK_COMMENT("} remove_arg_slots");
812 }
813
814 // Helper to copy argument slots to the top of the stack.
815 // The sequence starts with argslot_reg and is counted by slot_count
816 // slot_count must be a multiple of stack_move_unit() and >= 0
817 // This function blows the temps but does not change argslot_reg.
818 void MethodHandles::push_arg_slots(MacroAssembler* _masm,
819 Register argslot_reg,
820 RegisterOrConstant slot_count,
821 Register temp_reg, Register temp2_reg) {
822 // allow constant zero
831 if (VerifyMethodHandles)
832 verify_stack_move(_masm, slot_count, 0);
833
834 RegisterOrConstant offset = adjust_SP_and_Gargs_down_by_slots(_masm, slot_count, temp2_reg, temp_reg);
835
836 if (slot_count.is_constant()) {
837 for (int i = slot_count.as_constant() - 1; i >= 0; i--) {
838 __ ld_ptr( Address(argslot_reg, i * wordSize), temp_reg);
839 __ st_ptr(temp_reg, Address(Gargs, i * wordSize));
840 }
841 } else {
842 Label L_plural, L_loop, L_break;
843 // Emit code to dynamically check for the common cases, zero and one slot.
844 __ cmp(slot_count.as_register(), (int32_t) 1);
845 __ br(Assembler::greater, false, Assembler::pn, L_plural);
846 __ delayed()->nop();
847 __ br(Assembler::less, false, Assembler::pn, L_break);
848 __ delayed()->nop();
849 __ ld_ptr( Address(argslot_reg, 0), temp_reg);
850 __ st_ptr(temp_reg, Address(Gargs, 0));
851 __ ba(false, L_break);
852 __ delayed()->nop(); // FILLME
853 __ BIND(L_plural);
854
855 // Loop for 2 or more:
856 // top = &argslot[slot_count]
857 // while (top > argslot) *(--Gargs) = *(--top)
858 Register top_reg = temp_reg;
859 __ add(argslot_reg, offset, top_reg);
860 __ add(Gargs, offset, Gargs ); // move back up again so we can go down
861 __ BIND(L_loop);
862 __ sub(top_reg, wordSize, top_reg);
863 __ sub(Gargs, wordSize, Gargs );
864 __ ld_ptr( Address(top_reg, 0), temp2_reg);
865 __ st_ptr(temp2_reg, Address(Gargs, 0));
866 __ cmp(top_reg, argslot_reg);
867 __ brx(Assembler::greaterUnsigned, false, Assembler::pt, L_loop);
868 __ delayed()->nop(); // FILLME
869 __ BIND(L_break);
870 }
871 BLOCK_COMMENT("} push_arg_slots");
872 }
873
874 // in-place movement; no change to Gargs
875 // blows temp_reg, temp2_reg
876 void MethodHandles::move_arg_slots_up(MacroAssembler* _masm,
877 Register bottom_reg, // invariant
878 Address top_addr, // can use temp_reg
879 RegisterOrConstant positive_distance_in_slots, // destroyed if register
880 Register temp_reg, Register temp2_reg) {
881 assert_different_registers(bottom_reg,
882 temp_reg, temp2_reg,
883 positive_distance_in_slots.register_or_noreg());
884 BLOCK_COMMENT("move_arg_slots_up {");
885 Label L_loop, L_break;
886 Register top_reg = temp_reg;
887 if (!top_addr.is_same_address(Address(top_reg, 0))) {
888 __ add(top_addr, top_reg);
889 }
890 // Detect empty (or broken) loop:
891 #ifdef ASSERT
892 if (VerifyMethodHandles) {
893 // Verify that &bottom < &top (non-empty interval)
894 Label L_ok, L_bad;
895 if (positive_distance_in_slots.is_register()) {
896 __ cmp(positive_distance_in_slots.as_register(), (int32_t) 0);
897 __ br(Assembler::lessEqual, false, Assembler::pn, L_bad);
898 __ delayed()->nop();
899 }
900 __ cmp(bottom_reg, top_reg);
901 __ brx(Assembler::lessUnsigned, false, Assembler::pt, L_ok);
902 __ delayed()->nop();
903 __ BIND(L_bad);
904 __ stop("valid bounds (copy up)");
905 __ BIND(L_ok);
906 }
907 #endif
908 __ cmp(bottom_reg, top_reg);
909 __ brx(Assembler::greaterEqualUnsigned, false, Assembler::pn, L_break);
910 __ delayed()->nop();
911 // work top down to bottom, copying contiguous data upwards
912 // In pseudo-code:
913 // while (--top >= bottom) *(top + distance) = *(top + 0);
914 RegisterOrConstant offset = __ argument_offset(positive_distance_in_slots, positive_distance_in_slots.register_or_noreg());
915 __ BIND(L_loop);
916 __ sub(top_reg, wordSize, top_reg);
917 __ ld_ptr( Address(top_reg, 0 ), temp2_reg);
918 __ st_ptr(temp2_reg, Address(top_reg, offset) );
919 __ cmp(top_reg, bottom_reg);
920 __ brx(Assembler::greaterUnsigned, false, Assembler::pt, L_loop);
921 __ delayed()->nop(); // FILLME
922 assert(Interpreter::stackElementSize == wordSize, "else change loop");
923 __ BIND(L_break);
924 BLOCK_COMMENT("} move_arg_slots_up");
925 }
926
927 // in-place movement; no change to rsp
928 // blows temp_reg, temp2_reg
929 void MethodHandles::move_arg_slots_down(MacroAssembler* _masm,
930 Address bottom_addr, // can use temp_reg
931 Register top_reg, // invariant
932 RegisterOrConstant negative_distance_in_slots, // destroyed if register
933 Register temp_reg, Register temp2_reg) {
934 assert_different_registers(top_reg,
935 negative_distance_in_slots.register_or_noreg(),
936 temp_reg, temp2_reg);
937 BLOCK_COMMENT("move_arg_slots_down {");
938 Label L_loop, L_break;
939 Register bottom_reg = temp_reg;
940 if (!bottom_addr.is_same_address(Address(bottom_reg, 0))) {
941 __ add(bottom_addr, bottom_reg);
942 }
943 // Detect empty (or broken) loop:
944 #ifdef ASSERT
945 assert(!negative_distance_in_slots.is_constant() || negative_distance_in_slots.as_constant() < 0, "");
946 if (VerifyMethodHandles) {
947 // Verify that &bottom < &top (non-empty interval)
948 Label L_ok, L_bad;
949 if (negative_distance_in_slots.is_register()) {
950 __ cmp(negative_distance_in_slots.as_register(), (int32_t) 0);
951 __ br(Assembler::greaterEqual, false, Assembler::pn, L_bad);
952 __ delayed()->nop();
953 }
954 __ cmp(bottom_reg, top_reg);
955 __ brx(Assembler::lessUnsigned, false, Assembler::pt, L_ok);
956 __ delayed()->nop();
957 __ BIND(L_bad);
958 __ stop("valid bounds (copy down)");
959 __ BIND(L_ok);
960 }
961 #endif
962 __ cmp(bottom_reg, top_reg);
963 __ brx(Assembler::greaterEqualUnsigned, false, Assembler::pn, L_break);
964 __ delayed()->nop();
965 // work bottom up to top, copying contiguous data downwards
966 // In pseudo-code:
967 // while (bottom < top) *(bottom - distance) = *(bottom + 0), bottom++;
968 RegisterOrConstant offset = __ argument_offset(negative_distance_in_slots, negative_distance_in_slots.register_or_noreg());
969 __ BIND(L_loop);
970 __ ld_ptr( Address(bottom_reg, 0 ), temp2_reg);
971 __ st_ptr(temp2_reg, Address(bottom_reg, offset) );
972 __ add(bottom_reg, wordSize, bottom_reg);
973 __ cmp(bottom_reg, top_reg);
974 __ brx(Assembler::lessUnsigned, false, Assembler::pt, L_loop);
975 __ delayed()->nop(); // FILLME
976 assert(Interpreter::stackElementSize == wordSize, "else change loop");
977 __ BIND(L_break);
978 BLOCK_COMMENT("} move_arg_slots_down");
979 }
980
981 // Copy from a field or array element to a stacked argument slot.
982 // is_element (ignored) says whether caller is loading an array element instead of an instance field.
983 void MethodHandles::move_typed_arg(MacroAssembler* _masm,
984 BasicType type, bool is_element,
985 Address value_src, Address slot_dest,
986 Register temp_reg) {
987 assert(!slot_dest.uses(temp_reg), "must be different register");
988 BLOCK_COMMENT(!is_element ? "move_typed_arg {" : "move_typed_arg { (array element)");
989 if (type == T_OBJECT || type == T_ARRAY) {
990 __ load_heap_oop(value_src, temp_reg);
991 __ verify_oop(temp_reg);
992 __ st_ptr(temp_reg, slot_dest);
993 } else if (type != T_VOID) {
994 int arg_size = type2aelembytes(type);
995 bool arg_is_signed = is_signed_subword_type(type);
1312 __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1313 __ verify_oop(G3_method_handle);
1314 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1315 // This is OK when all parameter types widen.
1316 // It is also OK when a return type narrows.
1317 break;
1318
1319 case _adapter_check_cast:
1320 {
1321 // Check a reference argument before jumping to the next layer of MH:
1322 load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
1323 Address vmarg = __ argument_address(O0_argslot, O0_argslot);
1324
1325 // What class are we casting to?
1326 Register O1_klass = O1_scratch; // Interesting AMH data.
1327 __ load_heap_oop(G3_amh_argument, O1_klass); // This is a Class object!
1328 load_klass_from_Class(_masm, O1_klass, O2_scratch, O3_scratch);
1329
1330 Label L_done;
1331 __ ld_ptr(vmarg, O2_scratch);
1332 __ tst(O2_scratch);
1333 __ brx(Assembler::zero, false, Assembler::pn, L_done); // No cast if null.
1334 __ delayed()->nop();
1335 __ load_klass(O2_scratch, O2_scratch);
1336
1337 // Live at this point:
1338 // - O0_argslot : argslot index in vmarg; may be required in the failing path
1339 // - O1_klass : klass required by the target method
1340 // - O2_scratch : argument klass to test
1341 // - G3_method_handle: adapter method handle
1342 __ check_klass_subtype(O2_scratch, O1_klass, O3_scratch, O4_scratch, L_done);
1343
1344 // If we get here, the type check failed!
1345 __ load_heap_oop(G3_amh_argument, O2_required); // required class
1346 __ ld_ptr( vmarg, O1_actual); // bad object
1347 __ jump_to(AddressLiteral(from_interpreted_entry(_raise_exception)), O3_scratch);
1348 __ delayed()->mov(Bytecodes::_checkcast, O0_code); // who is complaining?
1349
1350 __ BIND(L_done);
1351 // Get the new MH:
1352 __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1353 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1354 }
1419 ShouldNotReachHere();
1420 }
1421
1422 // This check is required on _BIG_ENDIAN
1423 Register G5_vminfo = G5_scratch;
1424 __ ldsw(G3_amh_conversion, G5_vminfo);
1425 assert(CONV_VMINFO_SHIFT == 0, "preshifted");
1426
1427 // Original 32-bit vmdata word must be of this form:
1428 // | MBZ:6 | signBitCount:8 | srcDstTypes:8 | conversionOp:8 |
1429 __ lduw(value, O1_scratch);
1430 if (!value_left_justified)
1431 __ sll(O1_scratch, G5_vminfo, O1_scratch);
1432 Label zero_extend, done;
1433 __ btst(CONV_VMINFO_SIGN_FLAG, G5_vminfo);
1434 __ br(Assembler::zero, false, Assembler::pn, zero_extend);
1435 __ delayed()->nop();
1436
1437 // this path is taken for int->byte, int->short
1438 __ sra(O1_scratch, G5_vminfo, O1_scratch);
1439 __ ba(false, done);
1440 __ delayed()->nop();
1441
1442 __ bind(zero_extend);
1443 // this is taken for int->char
1444 __ srl(O1_scratch, G5_vminfo, O1_scratch);
1445
1446 __ bind(done);
1447 __ st(O1_scratch, vmarg);
1448
1449 // Get the new MH:
1450 __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1451 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1452 }
1453 break;
1454
1455 case _adapter_opt_i2l: // optimized subcase of adapt_prim_to_prim
1456 case _adapter_opt_unboxl: // optimized subcase of adapt_ref_to_prim
1457 {
1458 // Perform an in-place int-to-long or ref-to-long conversion.
1459 load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
1460
1843 // In the case of a boxing call, the recursive call is to a 'boxer' method,
1844 // such as Integer.valueOf or Long.valueOf. In the case of a filter
1845 // or collect call, it will take one or more arguments, transform them,
1846 // and return some result, to store back into argument_base[vminfo].
1847 __ load_heap_oop(G3_amh_argument, G3_method_handle);
1848 if (VerifyMethodHandles) verify_method_handle(_masm, G3_method_handle, O1_scratch, O2_scratch);
1849
1850 // Calculate |collect|, the number of arguments we are collecting.
1851 Register O1_collect_count = O1_scratch;
1852 RegisterOrConstant collect_count;
1853 if (collect_count_constant < 0) {
1854 __ load_method_handle_vmslots(O1_collect_count, G3_method_handle, O2_scratch);
1855 collect_count = O1_collect_count;
1856 } else {
1857 collect_count = collect_count_constant;
1858 #ifdef ASSERT
1859 if (VerifyMethodHandles) {
1860 BLOCK_COMMENT("verify collect_count_constant {");
1861 __ load_method_handle_vmslots(O3_scratch, G3_method_handle, O2_scratch);
1862 Label L_count_ok;
1863 __ cmp(O3_scratch, collect_count_constant);
1864 __ br(Assembler::equal, false, Assembler::pt, L_count_ok);
1865 __ delayed()->nop();
1866 __ stop("bad vminfo in AMH.conv");
1867 __ BIND(L_count_ok);
1868 BLOCK_COMMENT("} verify collect_count_constant");
1869 }
1870 #endif //ASSERT
1871 }
1872
1873 // copy |collect| slots directly to TOS:
1874 push_arg_slots(_masm, O0_coll, collect_count, O2_scratch, O3_scratch);
1875 // Now pushed: ... keep1 | collect | keep2 | RF... | collect |
1876 // O0_coll still points at the trailing edge of |collect| and leading edge of |keep2|
1877
1878 // If necessary, adjust the saved arguments to make room for the eventual return value.
1879 // Normal adjustment: ... keep1 | +dest+ | -collect- | keep2 | RF... | collect |
1880 // If retaining args: ... keep1 | +dest+ | collect | keep2 | RF... | collect |
1881 // In the non-retaining case, this might move keep2 either up or down.
1882 // We don't have to copy the whole | RF... collect | complex,
1883 // but we must adjust RF.saved_args_base.
1884 // Also, from now on, we will forget about the original copy of |collect|.
1885 // If we are retaining it, we will treat it as part of |keep2|.
1892 Register O1_close_count = O1_collect_count;
1893 if (retain_original_args) {
1894 close_count = constant(0);
1895 } else if (collect_count_constant == -1) {
1896 close_count = O1_collect_count;
1897 }
1898
1899 // How many slots need moving? This is simply dest_slot (0 => no |keep3|).
1900 RegisterOrConstant keep3_count;
1901 Register O2_keep3_count = O2_scratch;
1902 if (dest_slot_constant < 0) {
1903 extract_conversion_vminfo(_masm, RicochetFrame::L5_conversion, O2_keep3_count);
1904 keep3_count = O2_keep3_count;
1905 } else {
1906 keep3_count = dest_slot_constant;
1907 #ifdef ASSERT
1908 if (VerifyMethodHandles && dest_slot_constant < 0) {
1909 BLOCK_COMMENT("verify dest_slot_constant {");
1910 extract_conversion_vminfo(_masm, RicochetFrame::L5_conversion, O3_scratch);
1911 Label L_vminfo_ok;
1912 __ cmp(O3_scratch, dest_slot_constant);
1913 __ br(Assembler::equal, false, Assembler::pt, L_vminfo_ok);
1914 __ delayed()->nop();
1915 __ stop("bad vminfo in AMH.conv");
1916 __ BIND(L_vminfo_ok);
1917 BLOCK_COMMENT("} verify dest_slot_constant");
1918 }
1919 #endif //ASSERT
1920 }
1921
1922 // tasks remaining:
1923 bool move_keep3 = (!keep3_count.is_constant() || keep3_count.as_constant() != 0);
1924 bool stomp_dest = (NOT_DEBUG(dest == T_OBJECT) DEBUG_ONLY(dest_count != 0));
1925 bool fix_arg_base = (!close_count.is_constant() || open_count != close_count.as_constant());
1926
1927 // Old and new argument locations (based at slot 0).
1928 // Net shift (&new_argv - &old_argv) is (close_count - open_count).
1929 bool zero_open_count = (open_count == 0); // remember this bit of info
1930 if (move_keep3 && fix_arg_base) {
1931 // It will be easier to have everything in one register:
1932 if (close_count.is_register()) {
1933 // Deduct open_count from close_count register to get a clean +/- value.
1934 __ sub(close_count.as_register(), open_count, close_count.as_register());
1935 } else {
1936 close_count = close_count.as_constant() - open_count;
1937 }
1938 open_count = 0;
1939 }
1940 Register L4_old_argv = RicochetFrame::L4_saved_args_base;
1941 Register O3_new_argv = O3_scratch;
1942 if (fix_arg_base) {
1943 __ add(L4_old_argv, __ argument_offset(close_count, O4_scratch), O3_new_argv,
1944 -(open_count * Interpreter::stackElementSize));
1945 }
1946
1947 // First decide if any actual data are to be moved.
1948 // We can skip if (a) |keep3| is empty, or (b) the argument list size didn't change.
1949 // (As it happens, all movements involve an argument list size change.)
1950
1951 // If there are variable parameters, use dynamic checks to skip around the whole mess.
1952 Label L_done;
1953 if (keep3_count.is_register()) {
1954 __ tst(keep3_count.as_register());
1955 __ br(Assembler::zero, false, Assembler::pn, L_done);
1956 __ delayed()->nop();
1957 }
1958 if (close_count.is_register()) {
1959 __ cmp(close_count.as_register(), open_count);
1960 __ br(Assembler::equal, false, Assembler::pn, L_done);
1961 __ delayed()->nop();
1962 }
1963
1964 if (move_keep3 && fix_arg_base) {
1965 bool emit_move_down = false, emit_move_up = false, emit_guard = false;
1966 if (!close_count.is_constant()) {
1967 emit_move_down = emit_guard = !zero_open_count;
1968 emit_move_up = true;
1969 } else if (open_count != close_count.as_constant()) {
1970 emit_move_down = (open_count > close_count.as_constant());
1971 emit_move_up = !emit_move_down;
1972 }
1973 Label L_move_up;
1974 if (emit_guard) {
1975 __ cmp(close_count.as_register(), open_count);
1976 __ br(Assembler::greater, false, Assembler::pn, L_move_up);
1977 __ delayed()->nop();
1978 }
1979
1980 if (emit_move_down) {
1981 // Move arguments down if |+dest+| > |-collect-|
1982 // (This is rare, except when arguments are retained.)
1983 // This opens space for the return value.
1984 if (keep3_count.is_constant()) {
1985 for (int i = 0; i < keep3_count.as_constant(); i++) {
1986 __ ld_ptr( Address(L4_old_argv, i * Interpreter::stackElementSize), O4_scratch);
1987 __ st_ptr(O4_scratch, Address(O3_new_argv, i * Interpreter::stackElementSize) );
1988 }
1989 } else {
1990 // Live: O1_close_count, O2_keep3_count, O3_new_argv
1991 Register argv_top = O0_scratch;
1992 __ add(L4_old_argv, __ argument_offset(keep3_count, O4_scratch), argv_top);
1993 move_arg_slots_down(_masm,
1994 Address(L4_old_argv, 0), // beginning of old argv
1995 argv_top, // end of old argv
1996 close_count, // distance to move down (must be negative)
1997 O4_scratch, G5_scratch);
1998 }
1999 }
2000
2001 if (emit_guard) {
2002 __ ba(false, L_done); // assumes emit_move_up is true also
2003 __ delayed()->nop();
2004 __ BIND(L_move_up);
2005 }
2006
2007 if (emit_move_up) {
2008 // Move arguments up if |+dest+| < |-collect-|
2009 // (This is usual, except when |keep3| is empty.)
2010 // This closes up the space occupied by the now-deleted collect values.
2011 if (keep3_count.is_constant()) {
2012 for (int i = keep3_count.as_constant() - 1; i >= 0; i--) {
2013 __ ld_ptr( Address(L4_old_argv, i * Interpreter::stackElementSize), O4_scratch);
2014 __ st_ptr(O4_scratch, Address(O3_new_argv, i * Interpreter::stackElementSize) );
2015 }
2016 } else {
2017 Address argv_top(L4_old_argv, __ argument_offset(keep3_count, O4_scratch));
2018 // Live: O1_close_count, O2_keep3_count, O3_new_argv
2019 move_arg_slots_up(_masm,
2020 L4_old_argv, // beginning of old argv
2021 argv_top, // end of old argv
2022 close_count, // distance to move up (must be positive)
2023 O4_scratch, G5_scratch);
2116 if (VerifyMethodHandles) verify_method_handle(_masm, G3_method_handle, O0_scratch, O1_scratch);
2117 __ restore(I5_savedSP, G0, SP);
2118 __ jump_to_method_handle_entry(G3_method_handle, O0_scratch);
2119 __ illtrap(0);
2120 }
2121 break;
2122
2123 case _adapter_opt_return_any:
2124 {
2125 Register O7_temp = O7;
2126 Register O5_dest_type = O5;
2127
2128 if (VerifyMethodHandles) RicochetFrame::verify_clean(_masm);
2129 extract_conversion_dest_type(_masm, RicochetFrame::L5_conversion, O5_dest_type);
2130 __ set(ExternalAddress((address) &_adapter_return_handlers[0]), O7_temp);
2131 __ sll_ptr(O5_dest_type, LogBytesPerWord, O5_dest_type);
2132 __ ld_ptr(O7_temp, O5_dest_type, O7_temp);
2133
2134 #ifdef ASSERT
2135 { Label L_ok;
2136 __ br_notnull(O7_temp, false, Assembler::pt, L_ok);
2137 __ delayed()->nop();
2138 __ stop("bad method handle return");
2139 __ BIND(L_ok);
2140 }
2141 #endif //ASSERT
2142 __ JMP(O7_temp, 0);
2143 __ delayed()->nop();
2144 }
2145 break;
2146
2147 case _adapter_opt_spread_0:
2148 case _adapter_opt_spread_1_ref:
2149 case _adapter_opt_spread_2_ref:
2150 case _adapter_opt_spread_3_ref:
2151 case _adapter_opt_spread_4_ref:
2152 case _adapter_opt_spread_5_ref:
2153 case _adapter_opt_spread_ref:
2154 case _adapter_opt_spread_byte:
2155 case _adapter_opt_spread_char:
2156 case _adapter_opt_spread_short:
2157 case _adapter_opt_spread_int:
2175
2176 // O0_argslot points both to the array and to the first output arg
2177 Address vmarg = Address(O0_argslot, 0);
2178
2179 // Get the array value.
2180 Register O1_array = O1_scratch;
2181 Register O2_array_klass = O2_scratch;
2182 BasicType elem_type = ek_adapter_opt_spread_type(ek);
2183 int elem_slots = type2size[elem_type]; // 1 or 2
2184 int array_slots = 1; // array is always a T_OBJECT
2185 int length_offset = arrayOopDesc::length_offset_in_bytes();
2186 int elem0_offset = arrayOopDesc::base_offset_in_bytes(elem_type);
2187 __ ld_ptr(vmarg, O1_array);
2188
2189 Label L_array_is_empty, L_insert_arg_space, L_copy_args, L_args_done;
2190 if (length_can_be_zero) {
2191 // handle the null pointer case, if zero is allowed
2192 Label L_skip;
2193 if (length_constant < 0) {
2194 load_conversion_vminfo(_masm, G3_amh_conversion, O3_scratch);
2195 __ br_zero(Assembler::notZero, false, Assembler::pn, O3_scratch, L_skip);
2196 __ delayed()->nop();
2197 }
2198 __ br_null(O1_array, false, Assembler::pn, L_array_is_empty);
2199 __ delayed()->nop();
2200 __ BIND(L_skip);
2201 }
2202 __ null_check(O1_array, oopDesc::klass_offset_in_bytes());
2203 __ load_klass(O1_array, O2_array_klass);
2204
2205 // Check the array type.
2206 Register O3_klass = O3_scratch;
2207 __ load_heap_oop(G3_amh_argument, O3_klass); // this is a Class object!
2208 load_klass_from_Class(_masm, O3_klass, O4_scratch, G5_scratch);
2209
2210 Label L_ok_array_klass, L_bad_array_klass, L_bad_array_length;
2211 __ check_klass_subtype(O2_array_klass, O3_klass, O4_scratch, G5_scratch, L_ok_array_klass);
2212 // If we get here, the type check failed!
2213 __ ba(false, L_bad_array_klass);
2214 __ delayed()->nop();
2215 __ BIND(L_ok_array_klass);
2216
2217 // Check length.
2218 if (length_constant >= 0) {
2219 __ ldsw(Address(O1_array, length_offset), O4_scratch);
2220 __ cmp(O4_scratch, length_constant);
2221 } else {
2222 Register O3_vminfo = O3_scratch;
2223 load_conversion_vminfo(_masm, G3_amh_conversion, O3_vminfo);
2224 __ ldsw(Address(O1_array, length_offset), O4_scratch);
2225 __ cmp(O3_vminfo, O4_scratch);
2226 }
2227 __ br(Assembler::notEqual, false, Assembler::pn, L_bad_array_length);
2228 __ delayed()->nop();
2229
2230 Register O2_argslot_limit = O2_scratch;
2231
2232 // Array length checks out. Now insert any required stack slots.
2233 if (length_constant == -1) {
2234 // Form a pointer to the end of the affected region.
2235 __ add(O0_argslot, Interpreter::stackElementSize, O2_argslot_limit);
2236 // 'stack_move' is negative number of words to insert
2237 // This number already accounts for elem_slots.
2238 Register O3_stack_move = O3_scratch;
2239 load_stack_move(_masm, G3_amh_conversion, O3_stack_move);
2240 __ cmp(O3_stack_move, 0);
2241 assert(stack_move_unit() < 0, "else change this comparison");
2242 __ br(Assembler::less, false, Assembler::pn, L_insert_arg_space);
2243 __ delayed()->nop();
2244 __ br(Assembler::equal, false, Assembler::pn, L_copy_args);
2245 __ delayed()->nop();
2246 // single argument case, with no array movement
2247 __ BIND(L_array_is_empty);
2248 remove_arg_slots(_masm, -stack_move_unit() * array_slots,
2249 O0_argslot, O1_scratch, O2_scratch, O3_scratch);
2250 __ ba(false, L_args_done); // no spreading to do
2251 __ delayed()->nop();
2252 __ BIND(L_insert_arg_space);
2253 // come here in the usual case, stack_move < 0 (2 or more spread arguments)
2254 // Live: O1_array, O2_argslot_limit, O3_stack_move
2255 insert_arg_slots(_masm, O3_stack_move,
2256 O0_argslot, O4_scratch, G5_scratch, O1_scratch);
2257 // reload from rdx_argslot_limit since rax_argslot is now decremented
2258 __ ld_ptr(Address(O2_argslot_limit, -Interpreter::stackElementSize), O1_array);
2259 } else if (length_constant >= 1) {
2260 int new_slots = (length_constant * elem_slots) - array_slots;
2261 insert_arg_slots(_masm, new_slots * stack_move_unit(),
2262 O0_argslot, O2_scratch, O3_scratch, O4_scratch);
2263 } else if (length_constant == 0) {
2264 __ BIND(L_array_is_empty);
2265 remove_arg_slots(_masm, -stack_move_unit() * array_slots,
2266 O0_argslot, O1_scratch, O2_scratch, O3_scratch);
2267 } else {
2268 ShouldNotReachHere();
2269 }
2270
2271 // Copy from the array to the new slots.
2272 // Note: Stack change code preserves integrity of O0_argslot pointer.
2273 // So even after slot insertions, O0_argslot still points to first argument.
2274 // Beware: Arguments that are shallow on the stack are deep in the array,
2275 // and vice versa. So a downward-growing stack (the usual) has to be copied
2276 // elementwise in reverse order from the source array.
2277 __ BIND(L_copy_args);
2278 if (length_constant == -1) {
2279 // [O0_argslot, O2_argslot_limit) is the area we are inserting into.
2280 // Array element [0] goes at O0_argslot_limit[-wordSize].
2281 Register O1_source = O1_array;
2282 __ add(Address(O1_array, elem0_offset), O1_source);
2283 Register O4_fill_ptr = O4_scratch;
2284 __ mov(O2_argslot_limit, O4_fill_ptr);
2285 Label L_loop;
2286 __ BIND(L_loop);
2287 __ add(O4_fill_ptr, -Interpreter::stackElementSize * elem_slots, O4_fill_ptr);
2288 move_typed_arg(_masm, elem_type, true,
2289 Address(O1_source, 0), Address(O4_fill_ptr, 0),
2290 O2_scratch); // must be an even register for !_LP64 long moves (uses O2/O3)
2291 __ add(O1_source, type2aelembytes(elem_type), O1_source);
2292 __ cmp(O4_fill_ptr, O0_argslot);
2293 __ brx(Assembler::greaterUnsigned, false, Assembler::pt, L_loop);
2294 __ delayed()->nop(); // FILLME
2295 } else if (length_constant == 0) {
2296 // nothing to copy
2297 } else {
2298 int elem_offset = elem0_offset;
2299 int slot_offset = length_constant * Interpreter::stackElementSize;
2300 for (int index = 0; index < length_constant; index++) {
2301 slot_offset -= Interpreter::stackElementSize * elem_slots; // fill backward
2302 move_typed_arg(_masm, elem_type, true,
2303 Address(O1_array, elem_offset), Address(O0_argslot, slot_offset),
2304 O2_scratch); // must be an even register for !_LP64 long moves (uses O2/O3)
2305 elem_offset += type2aelembytes(elem_type);
2306 }
2307 }
2308 __ BIND(L_args_done);
2309
2310 // Arguments are spread. Move to next method handle.
2311 __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
2312 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
2313
2314 __ BIND(L_bad_array_klass);
|
270 // Emit code to verify that FP is pointing at a valid ricochet frame.
271 #ifdef ASSERT
272 enum {
273 ARG_LIMIT = 255, SLOP = 45,
274 // use this parameter for checking for garbage stack movements:
275 UNREASONABLE_STACK_MOVE = (ARG_LIMIT + SLOP)
276 // the slop defends against false alarms due to fencepost errors
277 };
278
279 void MethodHandles::RicochetFrame::verify_clean(MacroAssembler* _masm) {
280 // The stack should look like this:
281 // ... keep1 | dest=42 | keep2 | magic | handler | magic | recursive args | [RF]
282 // Check various invariants.
283
284 Register O7_temp = O7, O5_temp = O5;
285
286 Label L_ok_1, L_ok_2, L_ok_3, L_ok_4;
287 BLOCK_COMMENT("verify_clean {");
288 // Magic numbers must check out:
289 __ set((int32_t) MAGIC_NUMBER_1, O7_temp);
290 __ cmp_and_br_short(O7_temp, L0_magic_number_1, Assembler::equal, Assembler::pt, L_ok_1);
291 __ stop("damaged ricochet frame: MAGIC_NUMBER_1 not found");
292
293 __ BIND(L_ok_1);
294
295 // Arguments pointer must look reasonable:
296 #ifdef _LP64
297 Register FP_temp = O5_temp;
298 __ add(FP, STACK_BIAS, FP_temp);
299 #else
300 Register FP_temp = FP;
301 #endif
302 __ cmp_and_brx_short(L4_saved_args_base, FP_temp, Assembler::greaterEqualUnsigned, Assembler::pt, L_ok_2);
303 __ stop("damaged ricochet frame: L4 < FP");
304
305 __ BIND(L_ok_2);
306 // Disable until we decide on it's fate
307 // __ sub(L4_saved_args_base, UNREASONABLE_STACK_MOVE * Interpreter::stackElementSize, O7_temp);
308 // __ cmp(O7_temp, FP_temp);
309 // __ br(Assembler::lessEqualUnsigned, false, Assembler::pt, L_ok_3);
310 // __ delayed()->nop();
311 // __ stop("damaged ricochet frame: (L4 - UNREASONABLE_STACK_MOVE) > FP");
312
313 __ BIND(L_ok_3);
314 extract_conversion_dest_type(_masm, L5_conversion, O7_temp);
315 __ cmp_and_br_short(O7_temp, T_VOID, Assembler::equal, Assembler::pt, L_ok_4);
316 extract_conversion_vminfo(_masm, L5_conversion, O5_temp);
317 __ ld_ptr(L4_saved_args_base, __ argument_offset(O5_temp, O5_temp), O7_temp);
318 assert(__ is_simm13(RETURN_VALUE_PLACEHOLDER), "must be simm13");
319 __ cmp_and_brx_short(O7_temp, (int32_t) RETURN_VALUE_PLACEHOLDER, Assembler::equal, Assembler::pt, L_ok_4);
320 __ stop("damaged ricochet frame: RETURN_VALUE_PLACEHOLDER not found");
321 __ BIND(L_ok_4);
322 BLOCK_COMMENT("} verify_clean");
323 }
324 #endif //ASSERT
325
326 void MethodHandles::load_klass_from_Class(MacroAssembler* _masm, Register klass_reg, Register temp_reg, Register temp2_reg) {
327 if (VerifyMethodHandles)
328 verify_klass(_masm, klass_reg, SystemDictionaryHandles::Class_klass(), temp_reg, temp2_reg,
329 "AMH argument is a Class");
330 __ load_heap_oop(Address(klass_reg, java_lang_Class::klass_offset_in_bytes()), klass_reg);
331 }
332
333 void MethodHandles::load_conversion_vminfo(MacroAssembler* _masm, Address conversion_field_addr, Register reg) {
334 assert(CONV_VMINFO_SHIFT == 0, "preshifted");
335 assert(CONV_VMINFO_MASK == right_n_bits(BitsPerByte), "else change type of following load");
336 __ ldub(conversion_field_addr.plus_disp(BytesPerInt - 1), reg);
337 }
338
339 void MethodHandles::extract_conversion_vminfo(MacroAssembler* _masm, Register conversion_field_reg, Register reg) {
340 assert(CONV_VMINFO_SHIFT == 0, "preshifted");
341 __ and3(conversion_field_reg, CONV_VMINFO_MASK, reg);
342 }
343
344 void MethodHandles::extract_conversion_dest_type(MacroAssembler* _masm, Register conversion_field_reg, Register reg) {
345 __ srl(conversion_field_reg, CONV_DEST_TYPE_SHIFT, reg);
346 __ and3(reg, 0x0F, reg);
347 }
348
349 void MethodHandles::load_stack_move(MacroAssembler* _masm,
350 Address G3_amh_conversion,
351 Register stack_move_reg) {
352 BLOCK_COMMENT("load_stack_move {");
353 __ ldsw(G3_amh_conversion, stack_move_reg);
354 __ sra(stack_move_reg, CONV_STACK_MOVE_SHIFT, stack_move_reg);
355 if (VerifyMethodHandles) {
356 Label L_ok, L_bad;
357 int32_t stack_move_limit = 0x0800; // extra-large
358 __ cmp_and_br_short(stack_move_reg, stack_move_limit, Assembler::greaterEqual, Assembler::pn, L_bad);
359 __ cmp(stack_move_reg, -stack_move_limit);
360 __ br(Assembler::greater, false, Assembler::pt, L_ok);
361 __ delayed()->nop();
362 __ BIND(L_bad);
363 __ stop("load_stack_move of garbage value");
364 __ BIND(L_ok);
365 }
366 BLOCK_COMMENT("} load_stack_move");
367 }
368
369 #ifdef ASSERT
370 void MethodHandles::RicochetFrame::verify() const {
371 assert(magic_number_1() == MAGIC_NUMBER_1, "");
372 if (!Universe::heap()->is_gc_active()) {
373 if (saved_args_layout() != NULL) {
374 assert(saved_args_layout()->is_method(), "must be valid oop");
375 }
376 if (saved_target() != NULL) {
377 assert(java_lang_invoke_MethodHandle::is_instance(saved_target()), "checking frame value");
378 }
379 }
380 int conv_op = adapter_conversion_op(conversion());
381 assert(conv_op == java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS ||
382 conv_op == java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS ||
383 conv_op == java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF,
384 "must be a sane conversion");
385 if (has_return_value_slot()) {
386 assert(*return_value_slot_addr() == RETURN_VALUE_PLACEHOLDER, "");
387 }
388 }
389
390 void MethodHandles::verify_argslot(MacroAssembler* _masm, Register argslot_reg, Register temp_reg, const char* error_message) {
391 // Verify that argslot lies within (Gargs, FP].
392 Label L_ok, L_bad;
393 BLOCK_COMMENT("verify_argslot {");
394 __ cmp_and_brx_short(Gargs, argslot_reg, Assembler::greaterUnsigned, Assembler::pn, L_bad);
395 __ add(FP, STACK_BIAS, temp_reg); // STACK_BIAS is zero on !_LP64
396 __ cmp_and_brx_short(argslot_reg, temp_reg, Assembler::lessEqualUnsigned, Assembler::pt, L_ok);
397 __ BIND(L_bad);
398 __ stop(error_message);
399 __ BIND(L_ok);
400 BLOCK_COMMENT("} verify_argslot");
401 }
402
403 void MethodHandles::verify_argslots(MacroAssembler* _masm,
404 RegisterOrConstant arg_slots,
405 Register arg_slot_base_reg,
406 Register temp_reg,
407 Register temp2_reg,
408 bool negate_argslots,
409 const char* error_message) {
410 // Verify that [argslot..argslot+size) lies within (Gargs, FP).
411 Label L_ok, L_bad;
412 BLOCK_COMMENT("verify_argslots {");
413 if (negate_argslots) {
414 if (arg_slots.is_constant()) {
415 arg_slots = -1 * arg_slots.as_constant();
416 } else {
417 __ neg(arg_slots.as_register(), temp_reg);
418 arg_slots = temp_reg;
419 }
420 }
421 __ add(arg_slot_base_reg, __ argument_offset(arg_slots, temp_reg), temp_reg);
422 __ add(FP, STACK_BIAS, temp2_reg); // STACK_BIAS is zero on !_LP64
423 __ cmp_and_brx_short(temp_reg, temp2_reg, Assembler::greaterUnsigned, Assembler::pn, L_bad);
424 // Gargs points to the first word so adjust by BytesPerWord
425 __ add(arg_slot_base_reg, BytesPerWord, temp_reg);
426 __ cmp_and_brx_short(Gargs, temp_reg, Assembler::lessEqualUnsigned, Assembler::pt, L_ok);
427 __ BIND(L_bad);
428 __ stop(error_message);
429 __ BIND(L_ok);
430 BLOCK_COMMENT("} verify_argslots");
431 }
432
433 // Make sure that arg_slots has the same sign as the given direction.
434 // If (and only if) arg_slots is a assembly-time constant, also allow it to be zero.
435 void MethodHandles::verify_stack_move(MacroAssembler* _masm,
436 RegisterOrConstant arg_slots, int direction) {
437 enum { UNREASONABLE_STACK_MOVE = 256 * 4 }; // limit of 255 arguments
438 bool allow_zero = arg_slots.is_constant();
439 if (direction == 0) { direction = +1; allow_zero = true; }
440 assert(stack_move_unit() == -1, "else add extra checks here");
441 if (arg_slots.is_register()) {
442 Label L_ok, L_bad;
443 BLOCK_COMMENT("verify_stack_move {");
444 // __ btst(-stack_move_unit() - 1, arg_slots.as_register()); // no need
445 // __ br(Assembler::notZero, false, Assembler::pn, L_bad);
446 // __ delayed()->nop();
467 BLOCK_COMMENT("} verify_stack_move");
468 } else {
469 intptr_t size = arg_slots.as_constant();
470 if (direction < 0) size = -size;
471 assert(size >= 0, "correct direction of constant move");
472 assert(size < UNREASONABLE_STACK_MOVE, "reasonable size of constant move");
473 }
474 }
475
476 void MethodHandles::verify_klass(MacroAssembler* _masm,
477 Register obj_reg, KlassHandle klass,
478 Register temp_reg, Register temp2_reg,
479 const char* error_message) {
480 oop* klass_addr = klass.raw_value();
481 assert(klass_addr >= SystemDictionaryHandles::Object_klass().raw_value() &&
482 klass_addr <= SystemDictionaryHandles::Long_klass().raw_value(),
483 "must be one of the SystemDictionaryHandles");
484 Label L_ok, L_bad;
485 BLOCK_COMMENT("verify_klass {");
486 __ verify_oop(obj_reg);
487 __ br_null_short(obj_reg, Assembler::pn, L_bad);
488 __ load_klass(obj_reg, temp_reg);
489 __ set(ExternalAddress(klass_addr), temp2_reg);
490 __ ld_ptr(Address(temp2_reg, 0), temp2_reg);
491 __ cmp_and_brx_short(temp_reg, temp2_reg, Assembler::equal, Assembler::pt, L_ok);
492 intptr_t super_check_offset = klass->super_check_offset();
493 __ ld_ptr(Address(temp_reg, super_check_offset), temp_reg);
494 __ set(ExternalAddress(klass_addr), temp2_reg);
495 __ ld_ptr(Address(temp2_reg, 0), temp2_reg);
496 __ cmp_and_brx_short(temp_reg, temp2_reg, Assembler::equal, Assembler::pt, L_ok);
497 __ BIND(L_bad);
498 __ stop(error_message);
499 __ BIND(L_ok);
500 BLOCK_COMMENT("} verify_klass");
501 }
502 #endif // ASSERT
503
504
505 void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register target, Register temp) {
506 assert(method == G5_method, "interpreter calling convention");
507 __ verify_oop(method);
508 __ ld_ptr(G5_method, in_bytes(methodOopDesc::from_interpreted_offset()), target);
509 if (JvmtiExport::can_post_interpreter_events()) {
510 // JVMTI events, such as single-stepping, are implemented partly by avoiding running
511 // compiled code in threads for which the event is enabled. Check here for
512 // interp_only_mode if these events CAN be enabled.
513 __ verify_thread();
514 Label skip_compiled_code;
515
516 const Address interp_only(G2_thread, JavaThread::interp_only_mode_offset());
631 __ ldsw(vmargslot_addr, result);
632 }
633
634 static RegisterOrConstant adjust_SP_and_Gargs_down_by_slots(MacroAssembler* _masm,
635 RegisterOrConstant arg_slots,
636 Register temp_reg, Register temp2_reg) {
637 // Keep the stack pointer 2*wordSize aligned.
638 const int TwoWordAlignmentMask = right_n_bits(LogBytesPerWord + 1);
639 if (arg_slots.is_constant()) {
640 const int offset = arg_slots.as_constant() << LogBytesPerWord;
641 const int masked_offset = round_to(offset, 2 * BytesPerWord);
642 const int masked_offset2 = (offset + 1*BytesPerWord) & ~TwoWordAlignmentMask;
643 assert(masked_offset == masked_offset2, "must agree");
644 __ sub(Gargs, offset, Gargs);
645 __ sub(SP, masked_offset, SP );
646 return offset;
647 } else {
648 #ifdef ASSERT
649 {
650 Label L_ok;
651 __ cmp_and_br_short(arg_slots.as_register(), 0, Assembler::greaterEqual, Assembler::pt, L_ok);
652 __ stop("negative arg_slots");
653 __ bind(L_ok);
654 }
655 #endif
656 __ sll_ptr(arg_slots.as_register(), LogBytesPerWord, temp_reg);
657 __ add( temp_reg, 1*BytesPerWord, temp2_reg);
658 __ andn(temp2_reg, TwoWordAlignmentMask, temp2_reg);
659 __ sub(Gargs, temp_reg, Gargs);
660 __ sub(SP, temp2_reg, SP );
661 return temp_reg;
662 }
663 }
664
665 static RegisterOrConstant adjust_SP_and_Gargs_up_by_slots(MacroAssembler* _masm,
666 RegisterOrConstant arg_slots,
667 Register temp_reg, Register temp2_reg) {
668 // Keep the stack pointer 2*wordSize aligned.
669 const int TwoWordAlignmentMask = right_n_bits(LogBytesPerWord + 1);
670 if (arg_slots.is_constant()) {
671 const int offset = arg_slots.as_constant() << LogBytesPerWord;
706 // Make space on the stack for the inserted argument(s).
707 // Then pull down everything shallower than argslot_reg.
708 // The stacked return address gets pulled down with everything else.
709 // That is, copy [sp, argslot) downward by -size words. In pseudo-code:
710 // sp -= size;
711 // for (temp = sp + size; temp < argslot; temp++)
712 // temp[-size] = temp[0]
713 // argslot -= size;
714
715 // offset is temp3_reg in case of arg_slots being a register.
716 RegisterOrConstant offset = adjust_SP_and_Gargs_up_by_slots(_masm, arg_slots, temp3_reg, temp_reg);
717 __ sub(Gargs, offset, temp_reg); // source pointer for copy
718
719 {
720 Label loop;
721 __ BIND(loop);
722 // pull one word down each time through the loop
723 __ ld_ptr( Address(temp_reg, 0 ), temp2_reg);
724 __ st_ptr(temp2_reg, Address(temp_reg, offset) );
725 __ add(temp_reg, wordSize, temp_reg);
726 __ cmp_and_brx_short(temp_reg, argslot_reg, Assembler::lessUnsigned, Assembler::pt, loop);
727 }
728
729 // Now move the argslot down, to point to the opened-up space.
730 __ add(argslot_reg, offset, argslot_reg);
731 BLOCK_COMMENT("} insert_arg_slots");
732 }
733
734
735 // Helper to remove argument slots from the stack.
736 // arg_slots must be a multiple of stack_move_unit() and > 0
737 void MethodHandles::remove_arg_slots(MacroAssembler* _masm,
738 RegisterOrConstant arg_slots,
739 Register argslot_reg,
740 Register temp_reg, Register temp2_reg, Register temp3_reg) {
741 // allow constant zero
742 if (arg_slots.is_constant() && arg_slots.as_constant() == 0)
743 return;
744 assert_different_registers(argslot_reg, temp_reg, temp2_reg, temp3_reg,
745 (!arg_slots.is_register() ? Gargs : arg_slots.as_register()));
746
753
754 // Pull up everything shallower than argslot.
755 // Then remove the excess space on the stack.
756 // The stacked return address gets pulled up with everything else.
757 // That is, copy [sp, argslot) upward by size words. In pseudo-code:
758 // for (temp = argslot-1; temp >= sp; --temp)
759 // temp[size] = temp[0]
760 // argslot += size;
761 // sp += size;
762
763 RegisterOrConstant offset = __ regcon_sll_ptr(arg_slots, LogBytesPerWord, temp3_reg);
764 __ sub(argslot_reg, wordSize, temp_reg); // source pointer for copy
765
766 {
767 Label L_loop;
768 __ BIND(L_loop);
769 // pull one word up each time through the loop
770 __ ld_ptr( Address(temp_reg, 0 ), temp2_reg);
771 __ st_ptr(temp2_reg, Address(temp_reg, offset) );
772 __ sub(temp_reg, wordSize, temp_reg);
773 __ cmp_and_brx_short(temp_reg, Gargs, Assembler::greaterEqualUnsigned, Assembler::pt, L_loop);
774 }
775
776 // And adjust the argslot address to point at the deletion point.
777 __ add(argslot_reg, offset, argslot_reg);
778
779 // We don't need the offset at this point anymore, just adjust SP and Gargs.
780 (void) adjust_SP_and_Gargs_up_by_slots(_masm, arg_slots, temp3_reg, temp_reg);
781
782 BLOCK_COMMENT("} remove_arg_slots");
783 }
784
785 // Helper to copy argument slots to the top of the stack.
786 // The sequence starts with argslot_reg and is counted by slot_count
787 // slot_count must be a multiple of stack_move_unit() and >= 0
788 // This function blows the temps but does not change argslot_reg.
789 void MethodHandles::push_arg_slots(MacroAssembler* _masm,
790 Register argslot_reg,
791 RegisterOrConstant slot_count,
792 Register temp_reg, Register temp2_reg) {
793 // allow constant zero
802 if (VerifyMethodHandles)
803 verify_stack_move(_masm, slot_count, 0);
804
805 RegisterOrConstant offset = adjust_SP_and_Gargs_down_by_slots(_masm, slot_count, temp2_reg, temp_reg);
806
807 if (slot_count.is_constant()) {
808 for (int i = slot_count.as_constant() - 1; i >= 0; i--) {
809 __ ld_ptr( Address(argslot_reg, i * wordSize), temp_reg);
810 __ st_ptr(temp_reg, Address(Gargs, i * wordSize));
811 }
812 } else {
813 Label L_plural, L_loop, L_break;
814 // Emit code to dynamically check for the common cases, zero and one slot.
815 __ cmp(slot_count.as_register(), (int32_t) 1);
816 __ br(Assembler::greater, false, Assembler::pn, L_plural);
817 __ delayed()->nop();
818 __ br(Assembler::less, false, Assembler::pn, L_break);
819 __ delayed()->nop();
820 __ ld_ptr( Address(argslot_reg, 0), temp_reg);
821 __ st_ptr(temp_reg, Address(Gargs, 0));
822 __ ba_short(L_break);
823 __ BIND(L_plural);
824
825 // Loop for 2 or more:
826 // top = &argslot[slot_count]
827 // while (top > argslot) *(--Gargs) = *(--top)
828 Register top_reg = temp_reg;
829 __ add(argslot_reg, offset, top_reg);
830 __ add(Gargs, offset, Gargs ); // move back up again so we can go down
831 __ BIND(L_loop);
832 __ sub(top_reg, wordSize, top_reg);
833 __ sub(Gargs, wordSize, Gargs );
834 __ ld_ptr( Address(top_reg, 0), temp2_reg);
835 __ st_ptr(temp2_reg, Address(Gargs, 0));
836 __ cmp_and_brx_short(top_reg, argslot_reg, Assembler::greaterUnsigned, Assembler::pt, L_loop);
837 __ BIND(L_break);
838 }
839 BLOCK_COMMENT("} push_arg_slots");
840 }
841
842 // in-place movement; no change to Gargs
843 // blows temp_reg, temp2_reg
844 void MethodHandles::move_arg_slots_up(MacroAssembler* _masm,
845 Register bottom_reg, // invariant
846 Address top_addr, // can use temp_reg
847 RegisterOrConstant positive_distance_in_slots, // destroyed if register
848 Register temp_reg, Register temp2_reg) {
849 assert_different_registers(bottom_reg,
850 temp_reg, temp2_reg,
851 positive_distance_in_slots.register_or_noreg());
852 BLOCK_COMMENT("move_arg_slots_up {");
853 Label L_loop, L_break;
854 Register top_reg = temp_reg;
855 if (!top_addr.is_same_address(Address(top_reg, 0))) {
856 __ add(top_addr, top_reg);
857 }
858 // Detect empty (or broken) loop:
859 #ifdef ASSERT
860 if (VerifyMethodHandles) {
861 // Verify that &bottom < &top (non-empty interval)
862 Label L_ok, L_bad;
863 if (positive_distance_in_slots.is_register()) {
864 __ cmp(positive_distance_in_slots.as_register(), (int32_t) 0);
865 __ br(Assembler::lessEqual, false, Assembler::pn, L_bad);
866 __ delayed()->nop();
867 }
868 __ cmp_and_brx_short(bottom_reg, top_reg, Assembler::lessUnsigned, Assembler::pt, L_ok);
869 __ BIND(L_bad);
870 __ stop("valid bounds (copy up)");
871 __ BIND(L_ok);
872 }
873 #endif
874 __ cmp_and_brx_short(bottom_reg, top_reg, Assembler::greaterEqualUnsigned, Assembler::pn, L_break);
875 // work top down to bottom, copying contiguous data upwards
876 // In pseudo-code:
877 // while (--top >= bottom) *(top + distance) = *(top + 0);
878 RegisterOrConstant offset = __ argument_offset(positive_distance_in_slots, positive_distance_in_slots.register_or_noreg());
879 __ BIND(L_loop);
880 __ sub(top_reg, wordSize, top_reg);
881 __ ld_ptr( Address(top_reg, 0 ), temp2_reg);
882 __ st_ptr(temp2_reg, Address(top_reg, offset) );
883 __ cmp_and_brx_short(top_reg, bottom_reg, Assembler::greaterUnsigned, Assembler::pt, L_loop);
884 assert(Interpreter::stackElementSize == wordSize, "else change loop");
885 __ BIND(L_break);
886 BLOCK_COMMENT("} move_arg_slots_up");
887 }
888
889 // in-place movement; no change to rsp
890 // blows temp_reg, temp2_reg
891 void MethodHandles::move_arg_slots_down(MacroAssembler* _masm,
892 Address bottom_addr, // can use temp_reg
893 Register top_reg, // invariant
894 RegisterOrConstant negative_distance_in_slots, // destroyed if register
895 Register temp_reg, Register temp2_reg) {
896 assert_different_registers(top_reg,
897 negative_distance_in_slots.register_or_noreg(),
898 temp_reg, temp2_reg);
899 BLOCK_COMMENT("move_arg_slots_down {");
900 Label L_loop, L_break;
901 Register bottom_reg = temp_reg;
902 if (!bottom_addr.is_same_address(Address(bottom_reg, 0))) {
903 __ add(bottom_addr, bottom_reg);
904 }
905 // Detect empty (or broken) loop:
906 #ifdef ASSERT
907 assert(!negative_distance_in_slots.is_constant() || negative_distance_in_slots.as_constant() < 0, "");
908 if (VerifyMethodHandles) {
909 // Verify that &bottom < &top (non-empty interval)
910 Label L_ok, L_bad;
911 if (negative_distance_in_slots.is_register()) {
912 __ cmp(negative_distance_in_slots.as_register(), (int32_t) 0);
913 __ br(Assembler::greaterEqual, false, Assembler::pn, L_bad);
914 __ delayed()->nop();
915 }
916 __ cmp_and_brx_short(bottom_reg, top_reg, Assembler::lessUnsigned, Assembler::pt, L_ok);
917 __ BIND(L_bad);
918 __ stop("valid bounds (copy down)");
919 __ BIND(L_ok);
920 }
921 #endif
922 __ cmp_and_brx_short(bottom_reg, top_reg, Assembler::greaterEqualUnsigned, Assembler::pn, L_break);
923 // work bottom up to top, copying contiguous data downwards
924 // In pseudo-code:
925 // while (bottom < top) *(bottom - distance) = *(bottom + 0), bottom++;
926 RegisterOrConstant offset = __ argument_offset(negative_distance_in_slots, negative_distance_in_slots.register_or_noreg());
927 __ BIND(L_loop);
928 __ ld_ptr( Address(bottom_reg, 0 ), temp2_reg);
929 __ st_ptr(temp2_reg, Address(bottom_reg, offset) );
930 __ add(bottom_reg, wordSize, bottom_reg);
931 __ cmp_and_brx_short(bottom_reg, top_reg, Assembler::lessUnsigned, Assembler::pt, L_loop);
932 assert(Interpreter::stackElementSize == wordSize, "else change loop");
933 __ BIND(L_break);
934 BLOCK_COMMENT("} move_arg_slots_down");
935 }
936
937 // Copy from a field or array element to a stacked argument slot.
938 // is_element (ignored) says whether caller is loading an array element instead of an instance field.
939 void MethodHandles::move_typed_arg(MacroAssembler* _masm,
940 BasicType type, bool is_element,
941 Address value_src, Address slot_dest,
942 Register temp_reg) {
943 assert(!slot_dest.uses(temp_reg), "must be different register");
944 BLOCK_COMMENT(!is_element ? "move_typed_arg {" : "move_typed_arg { (array element)");
945 if (type == T_OBJECT || type == T_ARRAY) {
946 __ load_heap_oop(value_src, temp_reg);
947 __ verify_oop(temp_reg);
948 __ st_ptr(temp_reg, slot_dest);
949 } else if (type != T_VOID) {
950 int arg_size = type2aelembytes(type);
951 bool arg_is_signed = is_signed_subword_type(type);
1268 __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1269 __ verify_oop(G3_method_handle);
1270 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1271 // This is OK when all parameter types widen.
1272 // It is also OK when a return type narrows.
1273 break;
1274
1275 case _adapter_check_cast:
1276 {
1277 // Check a reference argument before jumping to the next layer of MH:
1278 load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
1279 Address vmarg = __ argument_address(O0_argslot, O0_argslot);
1280
1281 // What class are we casting to?
1282 Register O1_klass = O1_scratch; // Interesting AMH data.
1283 __ load_heap_oop(G3_amh_argument, O1_klass); // This is a Class object!
1284 load_klass_from_Class(_masm, O1_klass, O2_scratch, O3_scratch);
1285
1286 Label L_done;
1287 __ ld_ptr(vmarg, O2_scratch);
1288 __ br_null_short(O2_scratch, Assembler::pn, L_done); // No cast if null.
1289 __ load_klass(O2_scratch, O2_scratch);
1290
1291 // Live at this point:
1292 // - O0_argslot : argslot index in vmarg; may be required in the failing path
1293 // - O1_klass : klass required by the target method
1294 // - O2_scratch : argument klass to test
1295 // - G3_method_handle: adapter method handle
1296 __ check_klass_subtype(O2_scratch, O1_klass, O3_scratch, O4_scratch, L_done);
1297
1298 // If we get here, the type check failed!
1299 __ load_heap_oop(G3_amh_argument, O2_required); // required class
1300 __ ld_ptr( vmarg, O1_actual); // bad object
1301 __ jump_to(AddressLiteral(from_interpreted_entry(_raise_exception)), O3_scratch);
1302 __ delayed()->mov(Bytecodes::_checkcast, O0_code); // who is complaining?
1303
1304 __ BIND(L_done);
1305 // Get the new MH:
1306 __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1307 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1308 }
1373 ShouldNotReachHere();
1374 }
1375
1376 // This check is required on _BIG_ENDIAN
1377 Register G5_vminfo = G5_scratch;
1378 __ ldsw(G3_amh_conversion, G5_vminfo);
1379 assert(CONV_VMINFO_SHIFT == 0, "preshifted");
1380
1381 // Original 32-bit vmdata word must be of this form:
1382 // | MBZ:6 | signBitCount:8 | srcDstTypes:8 | conversionOp:8 |
1383 __ lduw(value, O1_scratch);
1384 if (!value_left_justified)
1385 __ sll(O1_scratch, G5_vminfo, O1_scratch);
1386 Label zero_extend, done;
1387 __ btst(CONV_VMINFO_SIGN_FLAG, G5_vminfo);
1388 __ br(Assembler::zero, false, Assembler::pn, zero_extend);
1389 __ delayed()->nop();
1390
1391 // this path is taken for int->byte, int->short
1392 __ sra(O1_scratch, G5_vminfo, O1_scratch);
1393 __ ba_short(done);
1394
1395 __ bind(zero_extend);
1396 // this is taken for int->char
1397 __ srl(O1_scratch, G5_vminfo, O1_scratch);
1398
1399 __ bind(done);
1400 __ st(O1_scratch, vmarg);
1401
1402 // Get the new MH:
1403 __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1404 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1405 }
1406 break;
1407
1408 case _adapter_opt_i2l: // optimized subcase of adapt_prim_to_prim
1409 case _adapter_opt_unboxl: // optimized subcase of adapt_ref_to_prim
1410 {
1411 // Perform an in-place int-to-long or ref-to-long conversion.
1412 load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
1413
1796 // In the case of a boxing call, the recursive call is to a 'boxer' method,
1797 // such as Integer.valueOf or Long.valueOf. In the case of a filter
1798 // or collect call, it will take one or more arguments, transform them,
1799 // and return some result, to store back into argument_base[vminfo].
1800 __ load_heap_oop(G3_amh_argument, G3_method_handle);
1801 if (VerifyMethodHandles) verify_method_handle(_masm, G3_method_handle, O1_scratch, O2_scratch);
1802
1803 // Calculate |collect|, the number of arguments we are collecting.
1804 Register O1_collect_count = O1_scratch;
1805 RegisterOrConstant collect_count;
1806 if (collect_count_constant < 0) {
1807 __ load_method_handle_vmslots(O1_collect_count, G3_method_handle, O2_scratch);
1808 collect_count = O1_collect_count;
1809 } else {
1810 collect_count = collect_count_constant;
1811 #ifdef ASSERT
1812 if (VerifyMethodHandles) {
1813 BLOCK_COMMENT("verify collect_count_constant {");
1814 __ load_method_handle_vmslots(O3_scratch, G3_method_handle, O2_scratch);
1815 Label L_count_ok;
1816 __ cmp_and_br_short(O3_scratch, collect_count_constant, Assembler::equal, Assembler::pt, L_count_ok);
1817 __ stop("bad vminfo in AMH.conv");
1818 __ BIND(L_count_ok);
1819 BLOCK_COMMENT("} verify collect_count_constant");
1820 }
1821 #endif //ASSERT
1822 }
1823
1824 // copy |collect| slots directly to TOS:
1825 push_arg_slots(_masm, O0_coll, collect_count, O2_scratch, O3_scratch);
1826 // Now pushed: ... keep1 | collect | keep2 | RF... | collect |
1827 // O0_coll still points at the trailing edge of |collect| and leading edge of |keep2|
1828
1829 // If necessary, adjust the saved arguments to make room for the eventual return value.
1830 // Normal adjustment: ... keep1 | +dest+ | -collect- | keep2 | RF... | collect |
1831 // If retaining args: ... keep1 | +dest+ | collect | keep2 | RF... | collect |
1832 // In the non-retaining case, this might move keep2 either up or down.
1833 // We don't have to copy the whole | RF... collect | complex,
1834 // but we must adjust RF.saved_args_base.
1835 // Also, from now on, we will forget about the original copy of |collect|.
1836 // If we are retaining it, we will treat it as part of |keep2|.
1843 Register O1_close_count = O1_collect_count;
1844 if (retain_original_args) {
1845 close_count = constant(0);
1846 } else if (collect_count_constant == -1) {
1847 close_count = O1_collect_count;
1848 }
1849
1850 // How many slots need moving? This is simply dest_slot (0 => no |keep3|).
1851 RegisterOrConstant keep3_count;
1852 Register O2_keep3_count = O2_scratch;
1853 if (dest_slot_constant < 0) {
1854 extract_conversion_vminfo(_masm, RicochetFrame::L5_conversion, O2_keep3_count);
1855 keep3_count = O2_keep3_count;
1856 } else {
1857 keep3_count = dest_slot_constant;
1858 #ifdef ASSERT
1859 if (VerifyMethodHandles && dest_slot_constant < 0) {
1860 BLOCK_COMMENT("verify dest_slot_constant {");
1861 extract_conversion_vminfo(_masm, RicochetFrame::L5_conversion, O3_scratch);
1862 Label L_vminfo_ok;
1863 __ cmp_and_br_short(O3_scratch, dest_slot_constant, Assembler::equal, Assembler::pt, L_vminfo_ok);
1864 __ stop("bad vminfo in AMH.conv");
1865 __ BIND(L_vminfo_ok);
1866 BLOCK_COMMENT("} verify dest_slot_constant");
1867 }
1868 #endif //ASSERT
1869 }
1870
1871 // tasks remaining:
1872 bool move_keep3 = (!keep3_count.is_constant() || keep3_count.as_constant() != 0);
1873 bool stomp_dest = (NOT_DEBUG(dest == T_OBJECT) DEBUG_ONLY(dest_count != 0));
1874 bool fix_arg_base = (!close_count.is_constant() || open_count != close_count.as_constant());
1875
1876 // Old and new argument locations (based at slot 0).
1877 // Net shift (&new_argv - &old_argv) is (close_count - open_count).
1878 bool zero_open_count = (open_count == 0); // remember this bit of info
1879 if (move_keep3 && fix_arg_base) {
1880 // It will be easier to have everything in one register:
1881 if (close_count.is_register()) {
1882 // Deduct open_count from close_count register to get a clean +/- value.
1883 __ sub(close_count.as_register(), open_count, close_count.as_register());
1884 } else {
1885 close_count = close_count.as_constant() - open_count;
1886 }
1887 open_count = 0;
1888 }
1889 Register L4_old_argv = RicochetFrame::L4_saved_args_base;
1890 Register O3_new_argv = O3_scratch;
1891 if (fix_arg_base) {
1892 __ add(L4_old_argv, __ argument_offset(close_count, O4_scratch), O3_new_argv,
1893 -(open_count * Interpreter::stackElementSize));
1894 }
1895
1896 // First decide if any actual data are to be moved.
1897 // We can skip if (a) |keep3| is empty, or (b) the argument list size didn't change.
1898 // (As it happens, all movements involve an argument list size change.)
1899
1900 // If there are variable parameters, use dynamic checks to skip around the whole mess.
1901 Label L_done;
1902 if (keep3_count.is_register()) {
1903 __ cmp_and_br_short(keep3_count.as_register(), 0, Assembler::equal, Assembler::pn, L_done);
1904 }
1905 if (close_count.is_register()) {
1906 __ cmp_and_br_short(close_count.as_register(), open_count, Assembler::equal, Assembler::pn, L_done);
1907 }
1908
1909 if (move_keep3 && fix_arg_base) {
1910 bool emit_move_down = false, emit_move_up = false, emit_guard = false;
1911 if (!close_count.is_constant()) {
1912 emit_move_down = emit_guard = !zero_open_count;
1913 emit_move_up = true;
1914 } else if (open_count != close_count.as_constant()) {
1915 emit_move_down = (open_count > close_count.as_constant());
1916 emit_move_up = !emit_move_down;
1917 }
1918 Label L_move_up;
1919 if (emit_guard) {
1920 __ cmp(close_count.as_register(), open_count);
1921 __ br(Assembler::greater, false, Assembler::pn, L_move_up);
1922 __ delayed()->nop();
1923 }
1924
1925 if (emit_move_down) {
1926 // Move arguments down if |+dest+| > |-collect-|
1927 // (This is rare, except when arguments are retained.)
1928 // This opens space for the return value.
1929 if (keep3_count.is_constant()) {
1930 for (int i = 0; i < keep3_count.as_constant(); i++) {
1931 __ ld_ptr( Address(L4_old_argv, i * Interpreter::stackElementSize), O4_scratch);
1932 __ st_ptr(O4_scratch, Address(O3_new_argv, i * Interpreter::stackElementSize) );
1933 }
1934 } else {
1935 // Live: O1_close_count, O2_keep3_count, O3_new_argv
1936 Register argv_top = O0_scratch;
1937 __ add(L4_old_argv, __ argument_offset(keep3_count, O4_scratch), argv_top);
1938 move_arg_slots_down(_masm,
1939 Address(L4_old_argv, 0), // beginning of old argv
1940 argv_top, // end of old argv
1941 close_count, // distance to move down (must be negative)
1942 O4_scratch, G5_scratch);
1943 }
1944 }
1945
1946 if (emit_guard) {
1947 __ ba_short(L_done); // assumes emit_move_up is true also
1948 __ BIND(L_move_up);
1949 }
1950
1951 if (emit_move_up) {
1952 // Move arguments up if |+dest+| < |-collect-|
1953 // (This is usual, except when |keep3| is empty.)
1954 // This closes up the space occupied by the now-deleted collect values.
1955 if (keep3_count.is_constant()) {
1956 for (int i = keep3_count.as_constant() - 1; i >= 0; i--) {
1957 __ ld_ptr( Address(L4_old_argv, i * Interpreter::stackElementSize), O4_scratch);
1958 __ st_ptr(O4_scratch, Address(O3_new_argv, i * Interpreter::stackElementSize) );
1959 }
1960 } else {
1961 Address argv_top(L4_old_argv, __ argument_offset(keep3_count, O4_scratch));
1962 // Live: O1_close_count, O2_keep3_count, O3_new_argv
1963 move_arg_slots_up(_masm,
1964 L4_old_argv, // beginning of old argv
1965 argv_top, // end of old argv
1966 close_count, // distance to move up (must be positive)
1967 O4_scratch, G5_scratch);
2060 if (VerifyMethodHandles) verify_method_handle(_masm, G3_method_handle, O0_scratch, O1_scratch);
2061 __ restore(I5_savedSP, G0, SP);
2062 __ jump_to_method_handle_entry(G3_method_handle, O0_scratch);
2063 __ illtrap(0);
2064 }
2065 break;
2066
2067 case _adapter_opt_return_any:
2068 {
2069 Register O7_temp = O7;
2070 Register O5_dest_type = O5;
2071
2072 if (VerifyMethodHandles) RicochetFrame::verify_clean(_masm);
2073 extract_conversion_dest_type(_masm, RicochetFrame::L5_conversion, O5_dest_type);
2074 __ set(ExternalAddress((address) &_adapter_return_handlers[0]), O7_temp);
2075 __ sll_ptr(O5_dest_type, LogBytesPerWord, O5_dest_type);
2076 __ ld_ptr(O7_temp, O5_dest_type, O7_temp);
2077
2078 #ifdef ASSERT
2079 { Label L_ok;
2080 __ br_notnull_short(O7_temp, Assembler::pt, L_ok);
2081 __ stop("bad method handle return");
2082 __ BIND(L_ok);
2083 }
2084 #endif //ASSERT
2085 __ JMP(O7_temp, 0);
2086 __ delayed()->nop();
2087 }
2088 break;
2089
2090 case _adapter_opt_spread_0:
2091 case _adapter_opt_spread_1_ref:
2092 case _adapter_opt_spread_2_ref:
2093 case _adapter_opt_spread_3_ref:
2094 case _adapter_opt_spread_4_ref:
2095 case _adapter_opt_spread_5_ref:
2096 case _adapter_opt_spread_ref:
2097 case _adapter_opt_spread_byte:
2098 case _adapter_opt_spread_char:
2099 case _adapter_opt_spread_short:
2100 case _adapter_opt_spread_int:
2118
2119 // O0_argslot points both to the array and to the first output arg
2120 Address vmarg = Address(O0_argslot, 0);
2121
2122 // Get the array value.
2123 Register O1_array = O1_scratch;
2124 Register O2_array_klass = O2_scratch;
2125 BasicType elem_type = ek_adapter_opt_spread_type(ek);
2126 int elem_slots = type2size[elem_type]; // 1 or 2
2127 int array_slots = 1; // array is always a T_OBJECT
2128 int length_offset = arrayOopDesc::length_offset_in_bytes();
2129 int elem0_offset = arrayOopDesc::base_offset_in_bytes(elem_type);
2130 __ ld_ptr(vmarg, O1_array);
2131
2132 Label L_array_is_empty, L_insert_arg_space, L_copy_args, L_args_done;
2133 if (length_can_be_zero) {
2134 // handle the null pointer case, if zero is allowed
2135 Label L_skip;
2136 if (length_constant < 0) {
2137 load_conversion_vminfo(_masm, G3_amh_conversion, O3_scratch);
2138 __ cmp_zero_and_br(Assembler::notZero, O3_scratch, L_skip);
2139 __ delayed()->nop(); // to avoid back-to-back cbcond instructions
2140 }
2141 __ br_null_short(O1_array, Assembler::pn, L_array_is_empty);
2142 __ BIND(L_skip);
2143 }
2144 __ null_check(O1_array, oopDesc::klass_offset_in_bytes());
2145 __ load_klass(O1_array, O2_array_klass);
2146
2147 // Check the array type.
2148 Register O3_klass = O3_scratch;
2149 __ load_heap_oop(G3_amh_argument, O3_klass); // this is a Class object!
2150 load_klass_from_Class(_masm, O3_klass, O4_scratch, G5_scratch);
2151
2152 Label L_ok_array_klass, L_bad_array_klass, L_bad_array_length;
2153 __ check_klass_subtype(O2_array_klass, O3_klass, O4_scratch, G5_scratch, L_ok_array_klass);
2154 // If we get here, the type check failed!
2155 __ ba_short(L_bad_array_klass);
2156 __ BIND(L_ok_array_klass);
2157
2158 // Check length.
2159 if (length_constant >= 0) {
2160 __ ldsw(Address(O1_array, length_offset), O4_scratch);
2161 __ cmp(O4_scratch, length_constant);
2162 } else {
2163 Register O3_vminfo = O3_scratch;
2164 load_conversion_vminfo(_masm, G3_amh_conversion, O3_vminfo);
2165 __ ldsw(Address(O1_array, length_offset), O4_scratch);
2166 __ cmp(O3_vminfo, O4_scratch);
2167 }
2168 __ br(Assembler::notEqual, false, Assembler::pn, L_bad_array_length);
2169 __ delayed()->nop();
2170
2171 Register O2_argslot_limit = O2_scratch;
2172
2173 // Array length checks out. Now insert any required stack slots.
2174 if (length_constant == -1) {
2175 // Form a pointer to the end of the affected region.
2176 __ add(O0_argslot, Interpreter::stackElementSize, O2_argslot_limit);
2177 // 'stack_move' is negative number of words to insert
2178 // This number already accounts for elem_slots.
2179 Register O3_stack_move = O3_scratch;
2180 load_stack_move(_masm, G3_amh_conversion, O3_stack_move);
2181 __ cmp(O3_stack_move, 0);
2182 assert(stack_move_unit() < 0, "else change this comparison");
2183 __ br(Assembler::less, false, Assembler::pn, L_insert_arg_space);
2184 __ delayed()->nop();
2185 __ br(Assembler::equal, false, Assembler::pn, L_copy_args);
2186 __ delayed()->nop();
2187 // single argument case, with no array movement
2188 __ BIND(L_array_is_empty);
2189 remove_arg_slots(_masm, -stack_move_unit() * array_slots,
2190 O0_argslot, O1_scratch, O2_scratch, O3_scratch);
2191 __ ba_short(L_args_done); // no spreading to do
2192 __ BIND(L_insert_arg_space);
2193 // come here in the usual case, stack_move < 0 (2 or more spread arguments)
2194 // Live: O1_array, O2_argslot_limit, O3_stack_move
2195 insert_arg_slots(_masm, O3_stack_move,
2196 O0_argslot, O4_scratch, G5_scratch, O1_scratch);
2197 // reload from rdx_argslot_limit since rax_argslot is now decremented
2198 __ ld_ptr(Address(O2_argslot_limit, -Interpreter::stackElementSize), O1_array);
2199 } else if (length_constant >= 1) {
2200 int new_slots = (length_constant * elem_slots) - array_slots;
2201 insert_arg_slots(_masm, new_slots * stack_move_unit(),
2202 O0_argslot, O2_scratch, O3_scratch, O4_scratch);
2203 } else if (length_constant == 0) {
2204 __ BIND(L_array_is_empty);
2205 remove_arg_slots(_masm, -stack_move_unit() * array_slots,
2206 O0_argslot, O1_scratch, O2_scratch, O3_scratch);
2207 } else {
2208 ShouldNotReachHere();
2209 }
2210
2211 // Copy from the array to the new slots.
2212 // Note: Stack change code preserves integrity of O0_argslot pointer.
2213 // So even after slot insertions, O0_argslot still points to first argument.
2214 // Beware: Arguments that are shallow on the stack are deep in the array,
2215 // and vice versa. So a downward-growing stack (the usual) has to be copied
2216 // elementwise in reverse order from the source array.
2217 __ BIND(L_copy_args);
2218 if (length_constant == -1) {
2219 // [O0_argslot, O2_argslot_limit) is the area we are inserting into.
2220 // Array element [0] goes at O0_argslot_limit[-wordSize].
2221 Register O1_source = O1_array;
2222 __ add(Address(O1_array, elem0_offset), O1_source);
2223 Register O4_fill_ptr = O4_scratch;
2224 __ mov(O2_argslot_limit, O4_fill_ptr);
2225 Label L_loop;
2226 __ BIND(L_loop);
2227 __ add(O4_fill_ptr, -Interpreter::stackElementSize * elem_slots, O4_fill_ptr);
2228 move_typed_arg(_masm, elem_type, true,
2229 Address(O1_source, 0), Address(O4_fill_ptr, 0),
2230 O2_scratch); // must be an even register for !_LP64 long moves (uses O2/O3)
2231 __ add(O1_source, type2aelembytes(elem_type), O1_source);
2232 __ cmp_and_brx_short(O4_fill_ptr, O0_argslot, Assembler::greaterUnsigned, Assembler::pt, L_loop);
2233 } else if (length_constant == 0) {
2234 // nothing to copy
2235 } else {
2236 int elem_offset = elem0_offset;
2237 int slot_offset = length_constant * Interpreter::stackElementSize;
2238 for (int index = 0; index < length_constant; index++) {
2239 slot_offset -= Interpreter::stackElementSize * elem_slots; // fill backward
2240 move_typed_arg(_masm, elem_type, true,
2241 Address(O1_array, elem_offset), Address(O0_argslot, slot_offset),
2242 O2_scratch); // must be an even register for !_LP64 long moves (uses O2/O3)
2243 elem_offset += type2aelembytes(elem_type);
2244 }
2245 }
2246 __ BIND(L_args_done);
2247
2248 // Arguments are spread. Move to next method handle.
2249 __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
2250 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
2251
2252 __ BIND(L_bad_array_klass);
|