Thu, 02 Feb 2012 09:14:38 -0800
7141637: JSR 292: MH spread invoker crashes with NULL argument on x86_32
Reviewed-by: twisti
Contributed-by: Volker Simonis <volker.simonis@gmail.com>
1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 #include "precompiled.hpp"
26 #include "interpreter/interpreter.hpp"
27 #include "interpreter/interpreterRuntime.hpp"
28 #include "memory/allocation.inline.hpp"
29 #include "prims/methodHandles.hpp"
31 #define __ _masm->
33 #ifdef PRODUCT
34 #define BLOCK_COMMENT(str) /* nothing */
35 #else
36 #define BLOCK_COMMENT(str) __ block_comment(str)
37 #endif
39 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
41 // Workaround for C++ overloading nastiness on '0' for RegisterOrConstant.
42 static RegisterOrConstant constant(int value) {
43 return RegisterOrConstant(value);
44 }
46 address MethodHandleEntry::start_compiled_entry(MacroAssembler* _masm,
47 address interpreted_entry) {
48 // Just before the actual machine code entry point, allocate space
49 // for a MethodHandleEntry::Data record, so that we can manage everything
50 // from one base pointer.
51 __ align(wordSize);
52 address target = __ pc() + sizeof(Data);
53 while (__ pc() < target) {
54 __ nop();
55 __ align(wordSize);
56 }
58 MethodHandleEntry* me = (MethodHandleEntry*) __ pc();
59 me->set_end_address(__ pc()); // set a temporary end_address
60 me->set_from_interpreted_entry(interpreted_entry);
61 me->set_type_checking_entry(NULL);
63 return (address) me;
64 }
66 MethodHandleEntry* MethodHandleEntry::finish_compiled_entry(MacroAssembler* _masm,
67 address start_addr) {
68 MethodHandleEntry* me = (MethodHandleEntry*) start_addr;
69 assert(me->end_address() == start_addr, "valid ME");
71 // Fill in the real end_address:
72 __ align(wordSize);
73 me->set_end_address(__ pc());
75 return me;
76 }
78 // stack walking support
80 frame MethodHandles::ricochet_frame_sender(const frame& fr, RegisterMap *map) {
81 RicochetFrame* f = RicochetFrame::from_frame(fr);
82 if (map->update_map())
83 frame::update_map_with_saved_link(map, &f->_sender_link);
84 return frame(f->extended_sender_sp(), f->exact_sender_sp(), f->sender_link(), f->sender_pc());
85 }
87 void MethodHandles::ricochet_frame_oops_do(const frame& fr, OopClosure* blk, const RegisterMap* reg_map) {
88 RicochetFrame* f = RicochetFrame::from_frame(fr);
90 // pick up the argument type descriptor:
91 Thread* thread = Thread::current();
92 Handle cookie(thread, f->compute_saved_args_layout(true, true));
94 // process fixed part
95 blk->do_oop((oop*)f->saved_target_addr());
96 blk->do_oop((oop*)f->saved_args_layout_addr());
98 // process variable arguments:
99 if (cookie.is_null()) return; // no arguments to describe
101 // the cookie is actually the invokeExact method for my target
102 // his argument signature is what I'm interested in
103 assert(cookie->is_method(), "");
104 methodHandle invoker(thread, methodOop(cookie()));
105 assert(invoker->name() == vmSymbols::invokeExact_name(), "must be this kind of method");
106 assert(!invoker->is_static(), "must have MH argument");
107 int slot_count = invoker->size_of_parameters();
108 assert(slot_count >= 1, "must include 'this'");
109 intptr_t* base = f->saved_args_base();
110 intptr_t* retval = NULL;
111 if (f->has_return_value_slot())
112 retval = f->return_value_slot_addr();
113 int slot_num = slot_count;
114 intptr_t* loc = &base[slot_num -= 1];
115 //blk->do_oop((oop*) loc); // original target, which is irrelevant
116 int arg_num = 0;
117 for (SignatureStream ss(invoker->signature()); !ss.is_done(); ss.next()) {
118 if (ss.at_return_type()) continue;
119 BasicType ptype = ss.type();
120 if (ptype == T_ARRAY) ptype = T_OBJECT; // fold all refs to T_OBJECT
121 assert(ptype >= T_BOOLEAN && ptype <= T_OBJECT, "not array or void");
122 loc = &base[slot_num -= type2size[ptype]];
123 bool is_oop = (ptype == T_OBJECT && loc != retval);
124 if (is_oop) blk->do_oop((oop*)loc);
125 arg_num += 1;
126 }
127 assert(slot_num == 0, "must have processed all the arguments");
128 }
130 oop MethodHandles::RicochetFrame::compute_saved_args_layout(bool read_cache, bool write_cache) {
131 oop cookie = NULL;
132 if (read_cache) {
133 cookie = saved_args_layout();
134 if (cookie != NULL) return cookie;
135 }
136 oop target = saved_target();
137 oop mtype = java_lang_invoke_MethodHandle::type(target);
138 oop mtform = java_lang_invoke_MethodType::form(mtype);
139 cookie = java_lang_invoke_MethodTypeForm::vmlayout(mtform);
140 if (write_cache) {
141 (*saved_args_layout_addr()) = cookie;
142 }
143 return cookie;
144 }
146 void MethodHandles::RicochetFrame::generate_ricochet_blob(MacroAssembler* _masm,
147 // output params:
148 int* bounce_offset,
149 int* exception_offset,
150 int* frame_size_in_words) {
151 (*frame_size_in_words) = RicochetFrame::frame_size_in_bytes() / wordSize;
153 address start = __ pc();
155 #ifdef ASSERT
156 __ hlt(); __ hlt(); __ hlt();
157 // here's a hint of something special:
158 __ push(MAGIC_NUMBER_1);
159 __ push(MAGIC_NUMBER_2);
160 #endif //ASSERT
161 __ hlt(); // not reached
163 // A return PC has just been popped from the stack.
164 // Return values are in registers.
165 // The ebp points into the RicochetFrame, which contains
166 // a cleanup continuation we must return to.
168 (*bounce_offset) = __ pc() - start;
169 BLOCK_COMMENT("ricochet_blob.bounce");
171 if (VerifyMethodHandles) RicochetFrame::verify_clean(_masm);
172 trace_method_handle(_masm, "return/ricochet_blob.bounce");
174 __ jmp(frame_address(continuation_offset_in_bytes()));
175 __ hlt();
176 DEBUG_ONLY(__ push(MAGIC_NUMBER_2));
178 (*exception_offset) = __ pc() - start;
179 BLOCK_COMMENT("ricochet_blob.exception");
181 // compare this to Interpreter::rethrow_exception_entry, which is parallel code
182 // for example, see TemplateInterpreterGenerator::generate_throw_exception
183 // Live registers in:
184 // rax: exception
185 // rdx: return address/pc that threw exception (ignored, always equal to bounce addr)
186 __ verify_oop(rax);
188 // no need to empty_FPU_stack or reinit_heapbase, since caller frame will do the same if needed
190 // Take down the frame.
192 // Cf. InterpreterMacroAssembler::remove_activation.
193 leave_ricochet_frame(_masm, /*rcx_recv=*/ noreg,
194 saved_last_sp_register(),
195 /*sender_pc_reg=*/ rdx);
197 // In between activations - previous activation type unknown yet
198 // compute continuation point - the continuation point expects the
199 // following registers set up:
200 //
201 // rax: exception
202 // rdx: return address/pc that threw exception
203 // rsp: expression stack of caller
204 // rbp: ebp of caller
205 __ push(rax); // save exception
206 __ push(rdx); // save return address
207 Register thread_reg = LP64_ONLY(r15_thread) NOT_LP64(rdi);
208 NOT_LP64(__ get_thread(thread_reg));
209 __ call_VM_leaf(CAST_FROM_FN_PTR(address,
210 SharedRuntime::exception_handler_for_return_address),
211 thread_reg, rdx);
212 __ mov(rbx, rax); // save exception handler
213 __ pop(rdx); // restore return address
214 __ pop(rax); // restore exception
215 __ jmp(rbx); // jump to exception
216 // handler of caller
217 }
219 void MethodHandles::RicochetFrame::enter_ricochet_frame(MacroAssembler* _masm,
220 Register rcx_recv,
221 Register rax_argv,
222 address return_handler,
223 Register rbx_temp) {
224 const Register saved_last_sp = saved_last_sp_register();
225 Address rcx_mh_vmtarget( rcx_recv, java_lang_invoke_MethodHandle::vmtarget_offset_in_bytes() );
226 Address rcx_amh_conversion( rcx_recv, java_lang_invoke_AdapterMethodHandle::conversion_offset_in_bytes() );
228 // Push the RicochetFrame a word at a time.
229 // This creates something similar to an interpreter frame.
230 // Cf. TemplateInterpreterGenerator::generate_fixed_frame.
231 BLOCK_COMMENT("push RicochetFrame {");
232 DEBUG_ONLY(int rfo = (int) sizeof(RicochetFrame));
233 assert((rfo -= wordSize) == RicochetFrame::sender_pc_offset_in_bytes(), "");
234 #define RF_FIELD(push_value, name) \
235 { push_value; \
236 assert((rfo -= wordSize) == RicochetFrame::name##_offset_in_bytes(), ""); }
237 RF_FIELD(__ push(rbp), sender_link);
238 RF_FIELD(__ push(saved_last_sp), exact_sender_sp); // rsi/r13
239 RF_FIELD(__ pushptr(rcx_amh_conversion), conversion);
240 RF_FIELD(__ push(rax_argv), saved_args_base); // can be updated if args are shifted
241 RF_FIELD(__ push((int32_t) NULL_WORD), saved_args_layout); // cache for GC layout cookie
242 if (UseCompressedOops) {
243 __ load_heap_oop(rbx_temp, rcx_mh_vmtarget);
244 RF_FIELD(__ push(rbx_temp), saved_target);
245 } else {
246 RF_FIELD(__ pushptr(rcx_mh_vmtarget), saved_target);
247 }
248 __ lea(rbx_temp, ExternalAddress(return_handler));
249 RF_FIELD(__ push(rbx_temp), continuation);
250 #undef RF_FIELD
251 assert(rfo == 0, "fully initialized the RicochetFrame");
252 // compute new frame pointer:
253 __ lea(rbp, Address(rsp, RicochetFrame::sender_link_offset_in_bytes()));
254 // Push guard word #1 in debug mode.
255 DEBUG_ONLY(__ push((int32_t) RicochetFrame::MAGIC_NUMBER_1));
256 // For debugging, leave behind an indication of which stub built this frame.
257 DEBUG_ONLY({ Label L; __ call(L, relocInfo::none); __ bind(L); });
258 BLOCK_COMMENT("} RicochetFrame");
259 }
261 void MethodHandles::RicochetFrame::leave_ricochet_frame(MacroAssembler* _masm,
262 Register rcx_recv,
263 Register new_sp_reg,
264 Register sender_pc_reg) {
265 assert_different_registers(rcx_recv, new_sp_reg, sender_pc_reg);
266 const Register saved_last_sp = saved_last_sp_register();
267 // Take down the frame.
268 // Cf. InterpreterMacroAssembler::remove_activation.
269 BLOCK_COMMENT("end_ricochet_frame {");
270 // TO DO: If (exact_sender_sp - extended_sender_sp) > THRESH, compact the frame down.
271 // This will keep stack in bounds even with unlimited tailcalls, each with an adapter.
272 if (rcx_recv->is_valid())
273 __ movptr(rcx_recv, RicochetFrame::frame_address(RicochetFrame::saved_target_offset_in_bytes()));
274 __ movptr(sender_pc_reg, RicochetFrame::frame_address(RicochetFrame::sender_pc_offset_in_bytes()));
275 __ movptr(saved_last_sp, RicochetFrame::frame_address(RicochetFrame::exact_sender_sp_offset_in_bytes()));
276 __ movptr(rbp, RicochetFrame::frame_address(RicochetFrame::sender_link_offset_in_bytes()));
277 __ mov(rsp, new_sp_reg);
278 BLOCK_COMMENT("} end_ricochet_frame");
279 }
281 // Emit code to verify that RBP is pointing at a valid ricochet frame.
282 #ifndef PRODUCT
283 enum {
284 ARG_LIMIT = 255, SLOP = 4,
285 // use this parameter for checking for garbage stack movements:
286 UNREASONABLE_STACK_MOVE = (ARG_LIMIT + SLOP)
287 // the slop defends against false alarms due to fencepost errors
288 };
289 #endif
291 #ifdef ASSERT
292 void MethodHandles::RicochetFrame::verify_clean(MacroAssembler* _masm) {
293 // The stack should look like this:
294 // ... keep1 | dest=42 | keep2 | RF | magic | handler | magic | recursive args |
295 // Check various invariants.
296 verify_offsets();
298 Register rdi_temp = rdi;
299 Register rcx_temp = rcx;
300 { __ push(rdi_temp); __ push(rcx_temp); }
301 #define UNPUSH_TEMPS \
302 { __ pop(rcx_temp); __ pop(rdi_temp); }
304 Address magic_number_1_addr = RicochetFrame::frame_address(RicochetFrame::magic_number_1_offset_in_bytes());
305 Address magic_number_2_addr = RicochetFrame::frame_address(RicochetFrame::magic_number_2_offset_in_bytes());
306 Address continuation_addr = RicochetFrame::frame_address(RicochetFrame::continuation_offset_in_bytes());
307 Address conversion_addr = RicochetFrame::frame_address(RicochetFrame::conversion_offset_in_bytes());
308 Address saved_args_base_addr = RicochetFrame::frame_address(RicochetFrame::saved_args_base_offset_in_bytes());
310 Label L_bad, L_ok;
311 BLOCK_COMMENT("verify_clean {");
312 // Magic numbers must check out:
313 __ cmpptr(magic_number_1_addr, (int32_t) MAGIC_NUMBER_1);
314 __ jcc(Assembler::notEqual, L_bad);
315 __ cmpptr(magic_number_2_addr, (int32_t) MAGIC_NUMBER_2);
316 __ jcc(Assembler::notEqual, L_bad);
318 // Arguments pointer must look reasonable:
319 __ movptr(rcx_temp, saved_args_base_addr);
320 __ cmpptr(rcx_temp, rbp);
321 __ jcc(Assembler::below, L_bad);
322 __ subptr(rcx_temp, UNREASONABLE_STACK_MOVE * Interpreter::stackElementSize);
323 __ cmpptr(rcx_temp, rbp);
324 __ jcc(Assembler::above, L_bad);
326 load_conversion_dest_type(_masm, rdi_temp, conversion_addr);
327 __ cmpl(rdi_temp, T_VOID);
328 __ jcc(Assembler::equal, L_ok);
329 __ movptr(rcx_temp, saved_args_base_addr);
330 load_conversion_vminfo(_masm, rdi_temp, conversion_addr);
331 __ cmpptr(Address(rcx_temp, rdi_temp, Interpreter::stackElementScale()),
332 (int32_t) RETURN_VALUE_PLACEHOLDER);
333 __ jcc(Assembler::equal, L_ok);
334 __ BIND(L_bad);
335 UNPUSH_TEMPS;
336 __ stop("damaged ricochet frame");
337 __ BIND(L_ok);
338 UNPUSH_TEMPS;
339 BLOCK_COMMENT("} verify_clean");
341 #undef UNPUSH_TEMPS
343 }
344 #endif //ASSERT
346 void MethodHandles::load_klass_from_Class(MacroAssembler* _masm, Register klass_reg) {
347 if (VerifyMethodHandles)
348 verify_klass(_masm, klass_reg, SystemDictionaryHandles::Class_klass(),
349 "AMH argument is a Class");
350 __ load_heap_oop(klass_reg, Address(klass_reg, java_lang_Class::klass_offset_in_bytes()));
351 }
353 void MethodHandles::load_conversion_vminfo(MacroAssembler* _masm, Register reg, Address conversion_field_addr) {
354 int bits = BitsPerByte;
355 int offset = (CONV_VMINFO_SHIFT / bits);
356 int shift = (CONV_VMINFO_SHIFT % bits);
357 __ load_unsigned_byte(reg, conversion_field_addr.plus_disp(offset));
358 assert(CONV_VMINFO_MASK == right_n_bits(bits - shift), "else change type of previous load");
359 assert(shift == 0, "no shift needed");
360 }
362 void MethodHandles::load_conversion_dest_type(MacroAssembler* _masm, Register reg, Address conversion_field_addr) {
363 int bits = BitsPerByte;
364 int offset = (CONV_DEST_TYPE_SHIFT / bits);
365 int shift = (CONV_DEST_TYPE_SHIFT % bits);
366 __ load_unsigned_byte(reg, conversion_field_addr.plus_disp(offset));
367 assert(CONV_TYPE_MASK == right_n_bits(bits - shift), "else change type of previous load");
368 __ shrl(reg, shift);
369 DEBUG_ONLY(int conv_type_bits = (int) exact_log2(CONV_TYPE_MASK+1));
370 assert((shift + conv_type_bits) == bits, "left justified in byte");
371 }
373 void MethodHandles::load_stack_move(MacroAssembler* _masm,
374 Register rdi_stack_move,
375 Register rcx_amh,
376 bool might_be_negative) {
377 BLOCK_COMMENT("load_stack_move {");
378 Address rcx_amh_conversion(rcx_amh, java_lang_invoke_AdapterMethodHandle::conversion_offset_in_bytes());
379 __ movl(rdi_stack_move, rcx_amh_conversion);
380 __ sarl(rdi_stack_move, CONV_STACK_MOVE_SHIFT);
381 #ifdef _LP64
382 if (might_be_negative) {
383 // clean high bits of stack motion register (was loaded as an int)
384 __ movslq(rdi_stack_move, rdi_stack_move);
385 }
386 #endif //_LP64
387 #ifdef ASSERT
388 if (VerifyMethodHandles) {
389 Label L_ok, L_bad;
390 int32_t stack_move_limit = 0x4000; // extra-large
391 __ cmpptr(rdi_stack_move, stack_move_limit);
392 __ jcc(Assembler::greaterEqual, L_bad);
393 __ cmpptr(rdi_stack_move, -stack_move_limit);
394 __ jcc(Assembler::greater, L_ok);
395 __ bind(L_bad);
396 __ stop("load_stack_move of garbage value");
397 __ BIND(L_ok);
398 }
399 #endif
400 BLOCK_COMMENT("} load_stack_move");
401 }
403 #ifdef ASSERT
404 void MethodHandles::RicochetFrame::verify_offsets() {
405 // Check compatibility of this struct with the more generally used offsets of class frame:
406 int ebp_off = sender_link_offset_in_bytes(); // offset from struct base to local rbp value
407 assert(ebp_off + wordSize*frame::interpreter_frame_method_offset == saved_args_base_offset_in_bytes(), "");
408 assert(ebp_off + wordSize*frame::interpreter_frame_last_sp_offset == conversion_offset_in_bytes(), "");
409 assert(ebp_off + wordSize*frame::interpreter_frame_sender_sp_offset == exact_sender_sp_offset_in_bytes(), "");
410 // These last two have to be exact:
411 assert(ebp_off + wordSize*frame::link_offset == sender_link_offset_in_bytes(), "");
412 assert(ebp_off + wordSize*frame::return_addr_offset == sender_pc_offset_in_bytes(), "");
413 }
415 void MethodHandles::RicochetFrame::verify() const {
416 verify_offsets();
417 assert(magic_number_1() == MAGIC_NUMBER_1, err_msg(PTR_FORMAT " == " PTR_FORMAT, magic_number_1(), MAGIC_NUMBER_1));
418 assert(magic_number_2() == MAGIC_NUMBER_2, err_msg(PTR_FORMAT " == " PTR_FORMAT, magic_number_2(), MAGIC_NUMBER_2));
419 if (!Universe::heap()->is_gc_active()) {
420 if (saved_args_layout() != NULL) {
421 assert(saved_args_layout()->is_method(), "must be valid oop");
422 }
423 if (saved_target() != NULL) {
424 assert(java_lang_invoke_MethodHandle::is_instance(saved_target()), "checking frame value");
425 }
426 }
427 int conv_op = adapter_conversion_op(conversion());
428 assert(conv_op == java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS ||
429 conv_op == java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS ||
430 conv_op == java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF,
431 "must be a sane conversion");
432 if (has_return_value_slot()) {
433 assert(*return_value_slot_addr() == RETURN_VALUE_PLACEHOLDER, "");
434 }
435 }
436 #endif //PRODUCT
438 #ifdef ASSERT
439 void MethodHandles::verify_argslot(MacroAssembler* _masm,
440 Register argslot_reg,
441 const char* error_message) {
442 // Verify that argslot lies within (rsp, rbp].
443 Label L_ok, L_bad;
444 BLOCK_COMMENT("verify_argslot {");
445 __ cmpptr(argslot_reg, rbp);
446 __ jccb(Assembler::above, L_bad);
447 __ cmpptr(rsp, argslot_reg);
448 __ jccb(Assembler::below, L_ok);
449 __ bind(L_bad);
450 __ stop(error_message);
451 __ BIND(L_ok);
452 BLOCK_COMMENT("} verify_argslot");
453 }
455 void MethodHandles::verify_argslots(MacroAssembler* _masm,
456 RegisterOrConstant arg_slots,
457 Register arg_slot_base_reg,
458 bool negate_argslots,
459 const char* error_message) {
460 // Verify that [argslot..argslot+size) lies within (rsp, rbp).
461 Label L_ok, L_bad;
462 Register rdi_temp = rdi;
463 BLOCK_COMMENT("verify_argslots {");
464 __ push(rdi_temp);
465 if (negate_argslots) {
466 if (arg_slots.is_constant()) {
467 arg_slots = -1 * arg_slots.as_constant();
468 } else {
469 __ movptr(rdi_temp, arg_slots);
470 __ negptr(rdi_temp);
471 arg_slots = rdi_temp;
472 }
473 }
474 __ lea(rdi_temp, Address(arg_slot_base_reg, arg_slots, Interpreter::stackElementScale()));
475 __ cmpptr(rdi_temp, rbp);
476 __ pop(rdi_temp);
477 __ jcc(Assembler::above, L_bad);
478 __ cmpptr(rsp, arg_slot_base_reg);
479 __ jcc(Assembler::below, L_ok);
480 __ bind(L_bad);
481 __ stop(error_message);
482 __ BIND(L_ok);
483 BLOCK_COMMENT("} verify_argslots");
484 }
486 // Make sure that arg_slots has the same sign as the given direction.
487 // If (and only if) arg_slots is a assembly-time constant, also allow it to be zero.
488 void MethodHandles::verify_stack_move(MacroAssembler* _masm,
489 RegisterOrConstant arg_slots, int direction) {
490 bool allow_zero = arg_slots.is_constant();
491 if (direction == 0) { direction = +1; allow_zero = true; }
492 assert(stack_move_unit() == -1, "else add extra checks here");
493 if (arg_slots.is_register()) {
494 Label L_ok, L_bad;
495 BLOCK_COMMENT("verify_stack_move {");
496 // testl(arg_slots.as_register(), -stack_move_unit() - 1); // no need
497 // jcc(Assembler::notZero, L_bad);
498 __ cmpptr(arg_slots.as_register(), (int32_t) NULL_WORD);
499 if (direction > 0) {
500 __ jcc(allow_zero ? Assembler::less : Assembler::lessEqual, L_bad);
501 __ cmpptr(arg_slots.as_register(), (int32_t) UNREASONABLE_STACK_MOVE);
502 __ jcc(Assembler::less, L_ok);
503 } else {
504 __ jcc(allow_zero ? Assembler::greater : Assembler::greaterEqual, L_bad);
505 __ cmpptr(arg_slots.as_register(), (int32_t) -UNREASONABLE_STACK_MOVE);
506 __ jcc(Assembler::greater, L_ok);
507 }
508 __ bind(L_bad);
509 if (direction > 0)
510 __ stop("assert arg_slots > 0");
511 else
512 __ stop("assert arg_slots < 0");
513 __ BIND(L_ok);
514 BLOCK_COMMENT("} verify_stack_move");
515 } else {
516 intptr_t size = arg_slots.as_constant();
517 if (direction < 0) size = -size;
518 assert(size >= 0, "correct direction of constant move");
519 assert(size < UNREASONABLE_STACK_MOVE, "reasonable size of constant move");
520 }
521 }
523 void MethodHandles::verify_klass(MacroAssembler* _masm,
524 Register obj, KlassHandle klass,
525 const char* error_message) {
526 oop* klass_addr = klass.raw_value();
527 assert(klass_addr >= SystemDictionaryHandles::Object_klass().raw_value() &&
528 klass_addr <= SystemDictionaryHandles::Long_klass().raw_value(),
529 "must be one of the SystemDictionaryHandles");
530 Register temp = rdi;
531 Label L_ok, L_bad;
532 BLOCK_COMMENT("verify_klass {");
533 __ verify_oop(obj);
534 __ testptr(obj, obj);
535 __ jcc(Assembler::zero, L_bad);
536 __ push(temp);
537 __ load_klass(temp, obj);
538 __ cmpptr(temp, ExternalAddress((address) klass_addr));
539 __ jcc(Assembler::equal, L_ok);
540 intptr_t super_check_offset = klass->super_check_offset();
541 __ movptr(temp, Address(temp, super_check_offset));
542 __ cmpptr(temp, ExternalAddress((address) klass_addr));
543 __ jcc(Assembler::equal, L_ok);
544 __ pop(temp);
545 __ bind(L_bad);
546 __ stop(error_message);
547 __ BIND(L_ok);
548 __ pop(temp);
549 BLOCK_COMMENT("} verify_klass");
550 }
551 #endif //ASSERT
553 void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register temp) {
554 if (JvmtiExport::can_post_interpreter_events()) {
555 Label run_compiled_code;
556 // JVMTI events, such as single-stepping, are implemented partly by avoiding running
557 // compiled code in threads for which the event is enabled. Check here for
558 // interp_only_mode if these events CAN be enabled.
559 #ifdef _LP64
560 Register rthread = r15_thread;
561 #else
562 Register rthread = temp;
563 __ get_thread(rthread);
564 #endif
565 // interp_only is an int, on little endian it is sufficient to test the byte only
566 // Is a cmpl faster?
567 __ cmpb(Address(rthread, JavaThread::interp_only_mode_offset()), 0);
568 __ jccb(Assembler::zero, run_compiled_code);
569 __ jmp(Address(method, methodOopDesc::interpreter_entry_offset()));
570 __ bind(run_compiled_code);
571 }
572 __ jmp(Address(method, methodOopDesc::from_interpreted_offset()));
573 }
575 // Code generation
576 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm) {
577 // rbx: methodOop
578 // rcx: receiver method handle (must load from sp[MethodTypeForm.vmslots])
579 // rsi/r13: sender SP (must preserve; see prepare_to_jump_from_interpreted)
580 // rdx, rdi: garbage temp, blown away
582 Register rbx_method = rbx;
583 Register rcx_recv = rcx;
584 Register rax_mtype = rax;
585 Register rdx_temp = rdx;
586 Register rdi_temp = rdi;
588 // emit WrongMethodType path first, to enable jccb back-branch from main path
589 Label wrong_method_type;
590 __ bind(wrong_method_type);
591 Label invoke_generic_slow_path, invoke_exact_error_path;
592 assert(methodOopDesc::intrinsic_id_size_in_bytes() == sizeof(u1), "");;
593 __ cmpb(Address(rbx_method, methodOopDesc::intrinsic_id_offset_in_bytes()), (int) vmIntrinsics::_invokeExact);
594 __ jcc(Assembler::notEqual, invoke_generic_slow_path);
595 __ jmp(invoke_exact_error_path);
597 // here's where control starts out:
598 __ align(CodeEntryAlignment);
599 address entry_point = __ pc();
601 // fetch the MethodType from the method handle into rax (the 'check' register)
602 // FIXME: Interpreter should transmit pre-popped stack pointer, to locate base of arg list.
603 // This would simplify several touchy bits of code.
604 // See 6984712: JSR 292 method handle calls need a clean argument base pointer
605 {
606 Register tem = rbx_method;
607 for (jint* pchase = methodOopDesc::method_type_offsets_chain(); (*pchase) != -1; pchase++) {
608 __ movptr(rax_mtype, Address(tem, *pchase));
609 tem = rax_mtype; // in case there is another indirection
610 }
611 }
613 // given the MethodType, find out where the MH argument is buried
614 __ load_heap_oop(rdx_temp, Address(rax_mtype, __ delayed_value(java_lang_invoke_MethodType::form_offset_in_bytes, rdi_temp)));
615 Register rdx_vmslots = rdx_temp;
616 __ movl(rdx_vmslots, Address(rdx_temp, __ delayed_value(java_lang_invoke_MethodTypeForm::vmslots_offset_in_bytes, rdi_temp)));
617 Address mh_receiver_slot_addr = __ argument_address(rdx_vmslots);
618 __ movptr(rcx_recv, mh_receiver_slot_addr);
620 trace_method_handle(_masm, "invokeExact");
622 __ check_method_handle_type(rax_mtype, rcx_recv, rdi_temp, wrong_method_type);
624 // Nobody uses the MH receiver slot after this. Make sure.
625 DEBUG_ONLY(__ movptr(mh_receiver_slot_addr, (int32_t)0x999999));
627 __ jump_to_method_handle_entry(rcx_recv, rdi_temp);
629 // error path for invokeExact (only)
630 __ bind(invoke_exact_error_path);
631 // ensure that the top of stack is properly aligned.
632 __ mov(rdi, rsp);
633 __ andptr(rsp, -StackAlignmentInBytes); // Align the stack for the ABI
634 __ pushptr(Address(rdi, 0)); // Pick up the return address
636 // Stub wants expected type in rax and the actual type in rcx
637 __ jump(ExternalAddress(StubRoutines::throw_WrongMethodTypeException_entry()));
639 // for invokeGeneric (only), apply argument and result conversions on the fly
640 __ bind(invoke_generic_slow_path);
641 #ifdef ASSERT
642 if (VerifyMethodHandles) {
643 Label L;
644 __ cmpb(Address(rbx_method, methodOopDesc::intrinsic_id_offset_in_bytes()), (int) vmIntrinsics::_invokeGeneric);
645 __ jcc(Assembler::equal, L);
646 __ stop("bad methodOop::intrinsic_id");
647 __ bind(L);
648 }
649 #endif //ASSERT
650 Register rbx_temp = rbx_method; // don't need it now
652 // make room on the stack for another pointer:
653 Register rcx_argslot = rcx_recv;
654 __ lea(rcx_argslot, __ argument_address(rdx_vmslots, 1));
655 insert_arg_slots(_masm, 2 * stack_move_unit(),
656 rcx_argslot, rbx_temp, rdx_temp);
658 // load up an adapter from the calling type (Java weaves this)
659 Register rdx_adapter = rdx_temp;
660 __ load_heap_oop(rdx_temp, Address(rax_mtype, __ delayed_value(java_lang_invoke_MethodType::form_offset_in_bytes, rdi_temp)));
661 __ load_heap_oop(rdx_adapter, Address(rdx_temp, __ delayed_value(java_lang_invoke_MethodTypeForm::genericInvoker_offset_in_bytes, rdi_temp)));
662 __ verify_oop(rdx_adapter);
663 __ movptr(Address(rcx_argslot, 1 * Interpreter::stackElementSize), rdx_adapter);
664 // As a trusted first argument, pass the type being called, so the adapter knows
665 // the actual types of the arguments and return values.
666 // (Generic invokers are shared among form-families of method-type.)
667 __ movptr(Address(rcx_argslot, 0 * Interpreter::stackElementSize), rax_mtype);
668 // FIXME: assert that rdx_adapter is of the right method-type.
669 __ mov(rcx, rdx_adapter);
670 trace_method_handle(_masm, "invokeGeneric");
671 __ jump_to_method_handle_entry(rcx, rdi_temp);
673 return entry_point;
674 }
676 // Helper to insert argument slots into the stack.
677 // arg_slots must be a multiple of stack_move_unit() and < 0
678 // rax_argslot is decremented to point to the new (shifted) location of the argslot
679 // But, rdx_temp ends up holding the original value of rax_argslot.
680 void MethodHandles::insert_arg_slots(MacroAssembler* _masm,
681 RegisterOrConstant arg_slots,
682 Register rax_argslot,
683 Register rbx_temp, Register rdx_temp) {
684 // allow constant zero
685 if (arg_slots.is_constant() && arg_slots.as_constant() == 0)
686 return;
687 assert_different_registers(rax_argslot, rbx_temp, rdx_temp,
688 (!arg_slots.is_register() ? rsp : arg_slots.as_register()));
689 if (VerifyMethodHandles)
690 verify_argslot(_masm, rax_argslot, "insertion point must fall within current frame");
691 if (VerifyMethodHandles)
692 verify_stack_move(_masm, arg_slots, -1);
694 // Make space on the stack for the inserted argument(s).
695 // Then pull down everything shallower than rax_argslot.
696 // The stacked return address gets pulled down with everything else.
697 // That is, copy [rsp, argslot) downward by -size words. In pseudo-code:
698 // rsp -= size;
699 // for (rdx = rsp + size; rdx < argslot; rdx++)
700 // rdx[-size] = rdx[0]
701 // argslot -= size;
702 BLOCK_COMMENT("insert_arg_slots {");
703 __ mov(rdx_temp, rsp); // source pointer for copy
704 __ lea(rsp, Address(rsp, arg_slots, Interpreter::stackElementScale()));
705 {
706 Label loop;
707 __ BIND(loop);
708 // pull one word down each time through the loop
709 __ movptr(rbx_temp, Address(rdx_temp, 0));
710 __ movptr(Address(rdx_temp, arg_slots, Interpreter::stackElementScale()), rbx_temp);
711 __ addptr(rdx_temp, wordSize);
712 __ cmpptr(rdx_temp, rax_argslot);
713 __ jcc(Assembler::below, loop);
714 }
716 // Now move the argslot down, to point to the opened-up space.
717 __ lea(rax_argslot, Address(rax_argslot, arg_slots, Interpreter::stackElementScale()));
718 BLOCK_COMMENT("} insert_arg_slots");
719 }
721 // Helper to remove argument slots from the stack.
722 // arg_slots must be a multiple of stack_move_unit() and > 0
723 void MethodHandles::remove_arg_slots(MacroAssembler* _masm,
724 RegisterOrConstant arg_slots,
725 Register rax_argslot,
726 Register rbx_temp, Register rdx_temp) {
727 // allow constant zero
728 if (arg_slots.is_constant() && arg_slots.as_constant() == 0)
729 return;
730 assert_different_registers(rax_argslot, rbx_temp, rdx_temp,
731 (!arg_slots.is_register() ? rsp : arg_slots.as_register()));
732 if (VerifyMethodHandles)
733 verify_argslots(_masm, arg_slots, rax_argslot, false,
734 "deleted argument(s) must fall within current frame");
735 if (VerifyMethodHandles)
736 verify_stack_move(_masm, arg_slots, +1);
738 BLOCK_COMMENT("remove_arg_slots {");
739 // Pull up everything shallower than rax_argslot.
740 // Then remove the excess space on the stack.
741 // The stacked return address gets pulled up with everything else.
742 // That is, copy [rsp, argslot) upward by size words. In pseudo-code:
743 // for (rdx = argslot-1; rdx >= rsp; --rdx)
744 // rdx[size] = rdx[0]
745 // argslot += size;
746 // rsp += size;
747 __ lea(rdx_temp, Address(rax_argslot, -wordSize)); // source pointer for copy
748 {
749 Label loop;
750 __ BIND(loop);
751 // pull one word up each time through the loop
752 __ movptr(rbx_temp, Address(rdx_temp, 0));
753 __ movptr(Address(rdx_temp, arg_slots, Interpreter::stackElementScale()), rbx_temp);
754 __ addptr(rdx_temp, -wordSize);
755 __ cmpptr(rdx_temp, rsp);
756 __ jcc(Assembler::aboveEqual, loop);
757 }
759 // Now move the argslot up, to point to the just-copied block.
760 __ lea(rsp, Address(rsp, arg_slots, Interpreter::stackElementScale()));
761 // And adjust the argslot address to point at the deletion point.
762 __ lea(rax_argslot, Address(rax_argslot, arg_slots, Interpreter::stackElementScale()));
763 BLOCK_COMMENT("} remove_arg_slots");
764 }
766 // Helper to copy argument slots to the top of the stack.
767 // The sequence starts with rax_argslot and is counted by slot_count
768 // slot_count must be a multiple of stack_move_unit() and >= 0
769 // This function blows the temps but does not change rax_argslot.
770 void MethodHandles::push_arg_slots(MacroAssembler* _masm,
771 Register rax_argslot,
772 RegisterOrConstant slot_count,
773 int skip_words_count,
774 Register rbx_temp, Register rdx_temp) {
775 assert_different_registers(rax_argslot, rbx_temp, rdx_temp,
776 (!slot_count.is_register() ? rbp : slot_count.as_register()),
777 rsp);
778 assert(Interpreter::stackElementSize == wordSize, "else change this code");
780 if (VerifyMethodHandles)
781 verify_stack_move(_masm, slot_count, 0);
783 // allow constant zero
784 if (slot_count.is_constant() && slot_count.as_constant() == 0)
785 return;
787 BLOCK_COMMENT("push_arg_slots {");
789 Register rbx_top = rbx_temp;
791 // There is at most 1 word to carry down with the TOS.
792 switch (skip_words_count) {
793 case 1: __ pop(rdx_temp); break;
794 case 0: break;
795 default: ShouldNotReachHere();
796 }
798 if (slot_count.is_constant()) {
799 for (int i = slot_count.as_constant() - 1; i >= 0; i--) {
800 __ pushptr(Address(rax_argslot, i * wordSize));
801 }
802 } else {
803 Label L_plural, L_loop, L_break;
804 // Emit code to dynamically check for the common cases, zero and one slot.
805 __ cmpl(slot_count.as_register(), (int32_t) 1);
806 __ jccb(Assembler::greater, L_plural);
807 __ jccb(Assembler::less, L_break);
808 __ pushptr(Address(rax_argslot, 0));
809 __ jmpb(L_break);
810 __ BIND(L_plural);
812 // Loop for 2 or more:
813 // rbx = &rax[slot_count]
814 // while (rbx > rax) *(--rsp) = *(--rbx)
815 __ lea(rbx_top, Address(rax_argslot, slot_count, Address::times_ptr));
816 __ BIND(L_loop);
817 __ subptr(rbx_top, wordSize);
818 __ pushptr(Address(rbx_top, 0));
819 __ cmpptr(rbx_top, rax_argslot);
820 __ jcc(Assembler::above, L_loop);
821 __ bind(L_break);
822 }
823 switch (skip_words_count) {
824 case 1: __ push(rdx_temp); break;
825 case 0: break;
826 default: ShouldNotReachHere();
827 }
828 BLOCK_COMMENT("} push_arg_slots");
829 }
831 // in-place movement; no change to rsp
832 // blows rax_temp, rdx_temp
833 void MethodHandles::move_arg_slots_up(MacroAssembler* _masm,
834 Register rbx_bottom, // invariant
835 Address top_addr, // can use rax_temp
836 RegisterOrConstant positive_distance_in_slots,
837 Register rax_temp, Register rdx_temp) {
838 BLOCK_COMMENT("move_arg_slots_up {");
839 assert_different_registers(rbx_bottom,
840 rax_temp, rdx_temp,
841 positive_distance_in_slots.register_or_noreg());
842 Label L_loop, L_break;
843 Register rax_top = rax_temp;
844 if (!top_addr.is_same_address(Address(rax_top, 0)))
845 __ lea(rax_top, top_addr);
846 // Detect empty (or broken) loop:
847 #ifdef ASSERT
848 if (VerifyMethodHandles) {
849 // Verify that &bottom < &top (non-empty interval)
850 Label L_ok, L_bad;
851 if (positive_distance_in_slots.is_register()) {
852 __ cmpptr(positive_distance_in_slots.as_register(), (int32_t) 0);
853 __ jcc(Assembler::lessEqual, L_bad);
854 }
855 __ cmpptr(rbx_bottom, rax_top);
856 __ jcc(Assembler::below, L_ok);
857 __ bind(L_bad);
858 __ stop("valid bounds (copy up)");
859 __ BIND(L_ok);
860 }
861 #endif
862 __ cmpptr(rbx_bottom, rax_top);
863 __ jccb(Assembler::aboveEqual, L_break);
864 // work rax down to rbx, copying contiguous data upwards
865 // In pseudo-code:
866 // [rbx, rax) = &[bottom, top)
867 // while (--rax >= rbx) *(rax + distance) = *(rax + 0), rax--;
868 __ BIND(L_loop);
869 __ subptr(rax_top, wordSize);
870 __ movptr(rdx_temp, Address(rax_top, 0));
871 __ movptr( Address(rax_top, positive_distance_in_slots, Address::times_ptr), rdx_temp);
872 __ cmpptr(rax_top, rbx_bottom);
873 __ jcc(Assembler::above, L_loop);
874 assert(Interpreter::stackElementSize == wordSize, "else change loop");
875 __ bind(L_break);
876 BLOCK_COMMENT("} move_arg_slots_up");
877 }
879 // in-place movement; no change to rsp
880 // blows rax_temp, rdx_temp
881 void MethodHandles::move_arg_slots_down(MacroAssembler* _masm,
882 Address bottom_addr, // can use rax_temp
883 Register rbx_top, // invariant
884 RegisterOrConstant negative_distance_in_slots,
885 Register rax_temp, Register rdx_temp) {
886 BLOCK_COMMENT("move_arg_slots_down {");
887 assert_different_registers(rbx_top,
888 negative_distance_in_slots.register_or_noreg(),
889 rax_temp, rdx_temp);
890 Label L_loop, L_break;
891 Register rax_bottom = rax_temp;
892 if (!bottom_addr.is_same_address(Address(rax_bottom, 0)))
893 __ lea(rax_bottom, bottom_addr);
894 // Detect empty (or broken) loop:
895 #ifdef ASSERT
896 assert(!negative_distance_in_slots.is_constant() || negative_distance_in_slots.as_constant() < 0, "");
897 if (VerifyMethodHandles) {
898 // Verify that &bottom < &top (non-empty interval)
899 Label L_ok, L_bad;
900 if (negative_distance_in_slots.is_register()) {
901 __ cmpptr(negative_distance_in_slots.as_register(), (int32_t) 0);
902 __ jcc(Assembler::greaterEqual, L_bad);
903 }
904 __ cmpptr(rax_bottom, rbx_top);
905 __ jcc(Assembler::below, L_ok);
906 __ bind(L_bad);
907 __ stop("valid bounds (copy down)");
908 __ BIND(L_ok);
909 }
910 #endif
911 __ cmpptr(rax_bottom, rbx_top);
912 __ jccb(Assembler::aboveEqual, L_break);
913 // work rax up to rbx, copying contiguous data downwards
914 // In pseudo-code:
915 // [rax, rbx) = &[bottom, top)
916 // while (rax < rbx) *(rax - distance) = *(rax + 0), rax++;
917 __ BIND(L_loop);
918 __ movptr(rdx_temp, Address(rax_bottom, 0));
919 __ movptr( Address(rax_bottom, negative_distance_in_slots, Address::times_ptr), rdx_temp);
920 __ addptr(rax_bottom, wordSize);
921 __ cmpptr(rax_bottom, rbx_top);
922 __ jcc(Assembler::below, L_loop);
923 assert(Interpreter::stackElementSize == wordSize, "else change loop");
924 __ bind(L_break);
925 BLOCK_COMMENT("} move_arg_slots_down");
926 }
928 // Copy from a field or array element to a stacked argument slot.
929 // is_element (ignored) says whether caller is loading an array element instead of an instance field.
930 void MethodHandles::move_typed_arg(MacroAssembler* _masm,
931 BasicType type, bool is_element,
932 Address slot_dest, Address value_src,
933 Register rbx_temp, Register rdx_temp) {
934 BLOCK_COMMENT(!is_element ? "move_typed_arg {" : "move_typed_arg { (array element)");
935 if (type == T_OBJECT || type == T_ARRAY) {
936 __ load_heap_oop(rbx_temp, value_src);
937 __ movptr(slot_dest, rbx_temp);
938 } else if (type != T_VOID) {
939 int arg_size = type2aelembytes(type);
940 bool arg_is_signed = is_signed_subword_type(type);
941 int slot_size = (arg_size > wordSize) ? arg_size : wordSize;
942 __ load_sized_value( rdx_temp, value_src, arg_size, arg_is_signed, rbx_temp);
943 __ store_sized_value( slot_dest, rdx_temp, slot_size, rbx_temp);
944 }
945 BLOCK_COMMENT("} move_typed_arg");
946 }
948 void MethodHandles::move_return_value(MacroAssembler* _masm, BasicType type,
949 Address return_slot) {
950 BLOCK_COMMENT("move_return_value {");
951 // Old versions of the JVM must clean the FPU stack after every return.
952 #ifndef _LP64
953 #ifdef COMPILER2
954 // The FPU stack is clean if UseSSE >= 2 but must be cleaned in other cases
955 if ((type == T_FLOAT && UseSSE < 1) || (type == T_DOUBLE && UseSSE < 2)) {
956 for (int i = 1; i < 8; i++) {
957 __ ffree(i);
958 }
959 } else if (UseSSE < 2) {
960 __ empty_FPU_stack();
961 }
962 #endif //COMPILER2
963 #endif //!_LP64
965 // Look at the type and pull the value out of the corresponding register.
966 if (type == T_VOID) {
967 // nothing to do
968 } else if (type == T_OBJECT) {
969 __ movptr(return_slot, rax);
970 } else if (type == T_INT || is_subword_type(type)) {
971 // write the whole word, even if only 32 bits is significant
972 __ movptr(return_slot, rax);
973 } else if (type == T_LONG) {
974 // store the value by parts
975 // Note: We assume longs are continguous (if misaligned) on the interpreter stack.
976 __ store_sized_value(return_slot, rax, BytesPerLong, rdx);
977 } else if (NOT_LP64((type == T_FLOAT && UseSSE < 1) ||
978 (type == T_DOUBLE && UseSSE < 2) ||)
979 false) {
980 // Use old x86 FPU registers:
981 if (type == T_FLOAT)
982 __ fstp_s(return_slot);
983 else
984 __ fstp_d(return_slot);
985 } else if (type == T_FLOAT) {
986 __ movflt(return_slot, xmm0);
987 } else if (type == T_DOUBLE) {
988 __ movdbl(return_slot, xmm0);
989 } else {
990 ShouldNotReachHere();
991 }
992 BLOCK_COMMENT("} move_return_value");
993 }
995 #ifndef PRODUCT
996 #define DESCRIBE_RICOCHET_OFFSET(rf, name) \
997 values.describe(frame_no, (intptr_t *) (((uintptr_t)rf) + MethodHandles::RicochetFrame::name##_offset_in_bytes()), #name)
999 void MethodHandles::RicochetFrame::describe(const frame* fr, FrameValues& values, int frame_no) {
1000 address bp = (address) fr->fp();
1001 RicochetFrame* rf = (RicochetFrame*)(bp - sender_link_offset_in_bytes());
1003 // ricochet slots
1004 DESCRIBE_RICOCHET_OFFSET(rf, exact_sender_sp);
1005 DESCRIBE_RICOCHET_OFFSET(rf, conversion);
1006 DESCRIBE_RICOCHET_OFFSET(rf, saved_args_base);
1007 DESCRIBE_RICOCHET_OFFSET(rf, saved_args_layout);
1008 DESCRIBE_RICOCHET_OFFSET(rf, saved_target);
1009 DESCRIBE_RICOCHET_OFFSET(rf, continuation);
1011 // relevant ricochet targets (in caller frame)
1012 values.describe(-1, rf->saved_args_base(), err_msg("*saved_args_base for #%d", frame_no));
1013 }
1014 #endif // ASSERT
1016 #ifndef PRODUCT
1017 extern "C" void print_method_handle(oop mh);
1018 void trace_method_handle_stub(const char* adaptername,
1019 oop mh,
1020 intptr_t* saved_regs,
1021 intptr_t* entry_sp,
1022 intptr_t* saved_sp,
1023 intptr_t* saved_bp) {
1024 // called as a leaf from native code: do not block the JVM!
1025 bool has_mh = (strstr(adaptername, "return/") == NULL); // return adapters don't have rcx_mh
1027 intptr_t* last_sp = (intptr_t*) saved_bp[frame::interpreter_frame_last_sp_offset];
1028 intptr_t* base_sp = last_sp;
1029 typedef MethodHandles::RicochetFrame RicochetFrame;
1030 RicochetFrame* rfp = (RicochetFrame*)((address)saved_bp - RicochetFrame::sender_link_offset_in_bytes());
1031 if (Universe::heap()->is_in((address) rfp->saved_args_base())) {
1032 // Probably an interpreter frame.
1033 base_sp = (intptr_t*) saved_bp[frame::interpreter_frame_monitor_block_top_offset];
1034 }
1035 intptr_t mh_reg = (intptr_t)mh;
1036 const char* mh_reg_name = "rcx_mh";
1037 if (!has_mh) mh_reg_name = "rcx";
1038 tty->print_cr("MH %s %s="PTR_FORMAT" sp=("PTR_FORMAT"+"INTX_FORMAT") stack_size="INTX_FORMAT" bp="PTR_FORMAT,
1039 adaptername, mh_reg_name, mh_reg,
1040 (intptr_t)entry_sp, (intptr_t)(saved_sp - entry_sp), (intptr_t)(base_sp - last_sp), (intptr_t)saved_bp);
1041 if (Verbose) {
1042 tty->print(" reg dump: ");
1043 int saved_regs_count = (entry_sp-1) - saved_regs;
1044 // 32 bit: rdi rsi rbp rsp; rbx rdx rcx (*) rax
1045 int i;
1046 for (i = 0; i <= saved_regs_count; i++) {
1047 if (i > 0 && i % 4 == 0 && i != saved_regs_count) {
1048 tty->cr();
1049 tty->print(" + dump: ");
1050 }
1051 tty->print(" %d: "PTR_FORMAT, i, saved_regs[i]);
1052 }
1053 tty->cr();
1054 if (last_sp != saved_sp && last_sp != NULL)
1055 tty->print_cr("*** last_sp="PTR_FORMAT, (intptr_t)last_sp);
1057 {
1058 // dumping last frame with frame::describe
1060 JavaThread* p = JavaThread::active();
1062 ResourceMark rm;
1063 PRESERVE_EXCEPTION_MARK; // may not be needed by safer and unexpensive here
1064 FrameValues values;
1066 // Note: We want to allow trace_method_handle from any call site.
1067 // While trace_method_handle creates a frame, it may be entered
1068 // without a PC on the stack top (e.g. not just after a call).
1069 // Walking that frame could lead to failures due to that invalid PC.
1070 // => carefully detect that frame when doing the stack walking
1072 // Current C frame
1073 frame cur_frame = os::current_frame();
1075 // Robust search of trace_calling_frame (independant of inlining).
1076 // Assumes saved_regs comes from a pusha in the trace_calling_frame.
1077 assert(cur_frame.sp() < saved_regs, "registers not saved on stack ?");
1078 frame trace_calling_frame = os::get_sender_for_C_frame(&cur_frame);
1079 while (trace_calling_frame.fp() < saved_regs) {
1080 trace_calling_frame = os::get_sender_for_C_frame(&trace_calling_frame);
1081 }
1083 // safely create a frame and call frame::describe
1084 intptr_t *dump_sp = trace_calling_frame.sender_sp();
1085 intptr_t *dump_fp = trace_calling_frame.link();
1087 bool walkable = has_mh; // whether the traced frame shoud be walkable
1089 if (walkable) {
1090 // The previous definition of walkable may have to be refined
1091 // if new call sites cause the next frame constructor to start
1092 // failing. Alternatively, frame constructors could be
1093 // modified to support the current or future non walkable
1094 // frames (but this is more intrusive and is not considered as
1095 // part of this RFE, which will instead use a simpler output).
1096 frame dump_frame = frame(dump_sp, dump_fp);
1097 dump_frame.describe(values, 1);
1098 } else {
1099 // Stack may not be walkable (invalid PC above FP):
1100 // Add descriptions without building a Java frame to avoid issues
1101 values.describe(-1, dump_fp, "fp for #1 <not parsed, cannot trust pc>");
1102 values.describe(-1, dump_sp, "sp for #1");
1103 }
1105 // mark saved_sp if seems valid
1106 if (has_mh) {
1107 if ((saved_sp >= dump_sp - UNREASONABLE_STACK_MOVE) && (saved_sp < dump_fp)) {
1108 values.describe(-1, saved_sp, "*saved_sp");
1109 }
1110 }
1112 tty->print_cr(" stack layout:");
1113 values.print(p);
1114 }
1115 if (has_mh)
1116 print_method_handle(mh);
1117 }
1118 }
1120 // The stub wraps the arguments in a struct on the stack to avoid
1121 // dealing with the different calling conventions for passing 6
1122 // arguments.
1123 struct MethodHandleStubArguments {
1124 const char* adaptername;
1125 oopDesc* mh;
1126 intptr_t* saved_regs;
1127 intptr_t* entry_sp;
1128 intptr_t* saved_sp;
1129 intptr_t* saved_bp;
1130 };
1131 void trace_method_handle_stub_wrapper(MethodHandleStubArguments* args) {
1132 trace_method_handle_stub(args->adaptername,
1133 args->mh,
1134 args->saved_regs,
1135 args->entry_sp,
1136 args->saved_sp,
1137 args->saved_bp);
1138 }
1140 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) {
1141 if (!TraceMethodHandles) return;
1142 BLOCK_COMMENT("trace_method_handle {");
1143 __ enter();
1144 __ andptr(rsp, -16); // align stack if needed for FPU state
1145 __ pusha();
1146 __ mov(rbx, rsp); // for retreiving saved_regs
1147 // Note: saved_regs must be in the entered frame for the
1148 // robust stack walking implemented in trace_method_handle_stub.
1150 // save FP result, valid at some call sites (adapter_opt_return_float, ...)
1151 __ increment(rsp, -2 * wordSize);
1152 if (UseSSE >= 2) {
1153 __ movdbl(Address(rsp, 0), xmm0);
1154 } else if (UseSSE == 1) {
1155 __ movflt(Address(rsp, 0), xmm0);
1156 } else {
1157 __ fst_d(Address(rsp, 0));
1158 }
1160 // incoming state:
1161 // rcx: method handle
1162 // r13 or rsi: saved sp
1163 // To avoid calling convention issues, build a record on the stack and pass the pointer to that instead.
1164 // Note: fix the increment below if pushing more arguments
1165 __ push(rbp); // saved_bp
1166 __ push(saved_last_sp_register()); // saved_sp
1167 __ push(rbp); // entry_sp (with extra align space)
1168 __ push(rbx); // pusha saved_regs
1169 __ push(rcx); // mh
1170 __ push(rcx); // slot for adaptername
1171 __ movptr(Address(rsp, 0), (intptr_t) adaptername);
1172 __ super_call_VM_leaf(CAST_FROM_FN_PTR(address, trace_method_handle_stub_wrapper), rsp);
1173 __ increment(rsp, 6 * wordSize); // MethodHandleStubArguments
1175 if (UseSSE >= 2) {
1176 __ movdbl(xmm0, Address(rsp, 0));
1177 } else if (UseSSE == 1) {
1178 __ movflt(xmm0, Address(rsp, 0));
1179 } else {
1180 __ fld_d(Address(rsp, 0));
1181 }
1182 __ increment(rsp, 2 * wordSize);
1184 __ popa();
1185 __ leave();
1186 BLOCK_COMMENT("} trace_method_handle");
1187 }
1188 #endif //PRODUCT
1190 // which conversion op types are implemented here?
1191 int MethodHandles::adapter_conversion_ops_supported_mask() {
1192 return ((1<<java_lang_invoke_AdapterMethodHandle::OP_RETYPE_ONLY)
1193 |(1<<java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW)
1194 |(1<<java_lang_invoke_AdapterMethodHandle::OP_CHECK_CAST)
1195 |(1<<java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_PRIM)
1196 |(1<<java_lang_invoke_AdapterMethodHandle::OP_REF_TO_PRIM)
1197 //OP_PRIM_TO_REF is below...
1198 |(1<<java_lang_invoke_AdapterMethodHandle::OP_SWAP_ARGS)
1199 |(1<<java_lang_invoke_AdapterMethodHandle::OP_ROT_ARGS)
1200 |(1<<java_lang_invoke_AdapterMethodHandle::OP_DUP_ARGS)
1201 |(1<<java_lang_invoke_AdapterMethodHandle::OP_DROP_ARGS)
1202 //OP_COLLECT_ARGS is below...
1203 |(1<<java_lang_invoke_AdapterMethodHandle::OP_SPREAD_ARGS)
1204 |(
1205 java_lang_invoke_MethodTypeForm::vmlayout_offset_in_bytes() <= 0 ? 0 :
1206 ((1<<java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF)
1207 |(1<<java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS)
1208 |(1<<java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS)
1209 ))
1210 );
1211 }
1213 //------------------------------------------------------------------------------
1214 // MethodHandles::generate_method_handle_stub
1215 //
1216 // Generate an "entry" field for a method handle.
1217 // This determines how the method handle will respond to calls.
1218 void MethodHandles::generate_method_handle_stub(MacroAssembler* _masm, MethodHandles::EntryKind ek) {
1219 MethodHandles::EntryKind ek_orig = ek_original_kind(ek);
1221 // Here is the register state during an interpreted call,
1222 // as set up by generate_method_handle_interpreter_entry():
1223 // - rbx: garbage temp (was MethodHandle.invoke methodOop, unused)
1224 // - rcx: receiver method handle
1225 // - rax: method handle type (only used by the check_mtype entry point)
1226 // - rsi/r13: sender SP (must preserve; see prepare_to_jump_from_interpreted)
1227 // - rdx: garbage temp, can blow away
1229 const Register rcx_recv = rcx;
1230 const Register rax_argslot = rax;
1231 const Register rbx_temp = rbx;
1232 const Register rdx_temp = rdx;
1233 const Register rdi_temp = rdi;
1235 // This guy is set up by prepare_to_jump_from_interpreted (from interpreted calls)
1236 // and gen_c2i_adapter (from compiled calls):
1237 const Register saved_last_sp = saved_last_sp_register();
1239 // Argument registers for _raise_exception.
1240 // 32-bit: Pass first two oop/int args in registers ECX and EDX.
1241 const Register rarg0_code = LP64_ONLY(j_rarg0) NOT_LP64(rcx);
1242 const Register rarg1_actual = LP64_ONLY(j_rarg1) NOT_LP64(rdx);
1243 const Register rarg2_required = LP64_ONLY(j_rarg2) NOT_LP64(rdi);
1244 assert_different_registers(rarg0_code, rarg1_actual, rarg2_required, saved_last_sp);
1246 guarantee(java_lang_invoke_MethodHandle::vmentry_offset_in_bytes() != 0, "must have offsets");
1248 // some handy addresses
1249 Address rcx_mh_vmtarget( rcx_recv, java_lang_invoke_MethodHandle::vmtarget_offset_in_bytes() );
1250 Address rcx_dmh_vmindex( rcx_recv, java_lang_invoke_DirectMethodHandle::vmindex_offset_in_bytes() );
1252 Address rcx_bmh_vmargslot( rcx_recv, java_lang_invoke_BoundMethodHandle::vmargslot_offset_in_bytes() );
1253 Address rcx_bmh_argument( rcx_recv, java_lang_invoke_BoundMethodHandle::argument_offset_in_bytes() );
1255 Address rcx_amh_vmargslot( rcx_recv, java_lang_invoke_AdapterMethodHandle::vmargslot_offset_in_bytes() );
1256 Address rcx_amh_argument( rcx_recv, java_lang_invoke_AdapterMethodHandle::argument_offset_in_bytes() );
1257 Address rcx_amh_conversion( rcx_recv, java_lang_invoke_AdapterMethodHandle::conversion_offset_in_bytes() );
1258 Address vmarg; // __ argument_address(vmargslot)
1260 const int java_mirror_offset = in_bytes(Klass::java_mirror_offset());
1262 if (have_entry(ek)) {
1263 __ nop(); // empty stubs make SG sick
1264 return;
1265 }
1267 #ifdef ASSERT
1268 __ push((int32_t) 0xEEEEEEEE);
1269 __ push((int32_t) (intptr_t) entry_name(ek));
1270 LP64_ONLY(__ push((int32_t) high((intptr_t) entry_name(ek))));
1271 __ push((int32_t) 0x33333333);
1272 #endif //ASSERT
1274 address interp_entry = __ pc();
1276 trace_method_handle(_masm, entry_name(ek));
1278 BLOCK_COMMENT(err_msg("Entry %s {", entry_name(ek)));
1280 switch ((int) ek) {
1281 case _raise_exception:
1282 {
1283 // Not a real MH entry, but rather shared code for raising an
1284 // exception. Since we use the compiled entry, arguments are
1285 // expected in compiler argument registers.
1286 assert(raise_exception_method(), "must be set");
1287 assert(raise_exception_method()->from_compiled_entry(), "method must be linked");
1289 const Register rax_pc = rax;
1290 __ pop(rax_pc); // caller PC
1291 __ mov(rsp, saved_last_sp); // cut the stack back to where the caller started
1293 Register rbx_method = rbx_temp;
1294 __ movptr(rbx_method, ExternalAddress((address) &_raise_exception_method));
1296 const int jobject_oop_offset = 0;
1297 __ movptr(rbx_method, Address(rbx_method, jobject_oop_offset)); // dereference the jobject
1299 __ movptr(saved_last_sp, rsp);
1300 __ subptr(rsp, 3 * wordSize);
1301 __ push(rax_pc); // restore caller PC
1303 __ movl (__ argument_address(constant(2)), rarg0_code);
1304 __ movptr(__ argument_address(constant(1)), rarg1_actual);
1305 __ movptr(__ argument_address(constant(0)), rarg2_required);
1306 jump_from_method_handle(_masm, rbx_method, rax);
1307 }
1308 break;
1310 case _invokestatic_mh:
1311 case _invokespecial_mh:
1312 {
1313 Register rbx_method = rbx_temp;
1314 __ load_heap_oop(rbx_method, rcx_mh_vmtarget); // target is a methodOop
1315 __ verify_oop(rbx_method);
1316 // same as TemplateTable::invokestatic or invokespecial,
1317 // minus the CP setup and profiling:
1318 if (ek == _invokespecial_mh) {
1319 // Must load & check the first argument before entering the target method.
1320 __ load_method_handle_vmslots(rax_argslot, rcx_recv, rdx_temp);
1321 __ movptr(rcx_recv, __ argument_address(rax_argslot, -1));
1322 __ null_check(rcx_recv);
1323 __ verify_oop(rcx_recv);
1324 }
1325 jump_from_method_handle(_masm, rbx_method, rax);
1326 }
1327 break;
1329 case _invokevirtual_mh:
1330 {
1331 // same as TemplateTable::invokevirtual,
1332 // minus the CP setup and profiling:
1334 // pick out the vtable index and receiver offset from the MH,
1335 // and then we can discard it:
1336 __ load_method_handle_vmslots(rax_argslot, rcx_recv, rdx_temp);
1337 Register rbx_index = rbx_temp;
1338 __ movl(rbx_index, rcx_dmh_vmindex);
1339 // Note: The verifier allows us to ignore rcx_mh_vmtarget.
1340 __ movptr(rcx_recv, __ argument_address(rax_argslot, -1));
1341 __ null_check(rcx_recv, oopDesc::klass_offset_in_bytes());
1343 // get receiver klass
1344 Register rax_klass = rax_argslot;
1345 __ load_klass(rax_klass, rcx_recv);
1346 __ verify_oop(rax_klass);
1348 // get target methodOop & entry point
1349 const int base = instanceKlass::vtable_start_offset() * wordSize;
1350 assert(vtableEntry::size() * wordSize == wordSize, "adjust the scaling in the code below");
1351 Address vtable_entry_addr(rax_klass,
1352 rbx_index, Address::times_ptr,
1353 base + vtableEntry::method_offset_in_bytes());
1354 Register rbx_method = rbx_temp;
1355 __ movptr(rbx_method, vtable_entry_addr);
1357 __ verify_oop(rbx_method);
1358 jump_from_method_handle(_masm, rbx_method, rax);
1359 }
1360 break;
1362 case _invokeinterface_mh:
1363 {
1364 // same as TemplateTable::invokeinterface,
1365 // minus the CP setup and profiling:
1367 // pick out the interface and itable index from the MH.
1368 __ load_method_handle_vmslots(rax_argslot, rcx_recv, rdx_temp);
1369 Register rdx_intf = rdx_temp;
1370 Register rbx_index = rbx_temp;
1371 __ load_heap_oop(rdx_intf, rcx_mh_vmtarget);
1372 __ movl(rbx_index, rcx_dmh_vmindex);
1373 __ movptr(rcx_recv, __ argument_address(rax_argslot, -1));
1374 __ null_check(rcx_recv, oopDesc::klass_offset_in_bytes());
1376 // get receiver klass
1377 Register rax_klass = rax_argslot;
1378 __ load_klass(rax_klass, rcx_recv);
1379 __ verify_oop(rax_klass);
1381 Register rbx_method = rbx_index;
1383 // get interface klass
1384 Label no_such_interface;
1385 __ verify_oop(rdx_intf);
1386 __ lookup_interface_method(rax_klass, rdx_intf,
1387 // note: next two args must be the same:
1388 rbx_index, rbx_method,
1389 rdi_temp,
1390 no_such_interface);
1392 __ verify_oop(rbx_method);
1393 jump_from_method_handle(_masm, rbx_method, rax);
1394 __ hlt();
1396 __ bind(no_such_interface);
1397 // Throw an exception.
1398 // For historical reasons, it will be IncompatibleClassChangeError.
1399 __ mov(rbx_temp, rcx_recv); // rarg2_required might be RCX
1400 assert_different_registers(rarg2_required, rbx_temp);
1401 __ movptr(rarg2_required, Address(rdx_intf, java_mirror_offset)); // required interface
1402 __ mov( rarg1_actual, rbx_temp); // bad receiver
1403 __ movl( rarg0_code, (int) Bytecodes::_invokeinterface); // who is complaining?
1404 __ jump(ExternalAddress(from_interpreted_entry(_raise_exception)));
1405 }
1406 break;
1408 case _bound_ref_mh:
1409 case _bound_int_mh:
1410 case _bound_long_mh:
1411 case _bound_ref_direct_mh:
1412 case _bound_int_direct_mh:
1413 case _bound_long_direct_mh:
1414 {
1415 const bool direct_to_method = (ek >= _bound_ref_direct_mh);
1416 BasicType arg_type = ek_bound_mh_arg_type(ek);
1417 int arg_slots = type2size[arg_type];
1419 // make room for the new argument:
1420 __ movl(rax_argslot, rcx_bmh_vmargslot);
1421 __ lea(rax_argslot, __ argument_address(rax_argslot));
1423 insert_arg_slots(_masm, arg_slots * stack_move_unit(), rax_argslot, rbx_temp, rdx_temp);
1425 // store bound argument into the new stack slot:
1426 __ load_heap_oop(rbx_temp, rcx_bmh_argument);
1427 if (arg_type == T_OBJECT) {
1428 __ movptr(Address(rax_argslot, 0), rbx_temp);
1429 } else {
1430 Address prim_value_addr(rbx_temp, java_lang_boxing_object::value_offset_in_bytes(arg_type));
1431 move_typed_arg(_masm, arg_type, false,
1432 Address(rax_argslot, 0),
1433 prim_value_addr,
1434 rbx_temp, rdx_temp);
1435 }
1437 if (direct_to_method) {
1438 Register rbx_method = rbx_temp;
1439 __ load_heap_oop(rbx_method, rcx_mh_vmtarget);
1440 __ verify_oop(rbx_method);
1441 jump_from_method_handle(_masm, rbx_method, rax);
1442 } else {
1443 __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
1444 __ verify_oop(rcx_recv);
1445 __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
1446 }
1447 }
1448 break;
1450 case _adapter_opt_profiling:
1451 if (java_lang_invoke_CountingMethodHandle::vmcount_offset_in_bytes() != 0) {
1452 Address rcx_mh_vmcount(rcx_recv, java_lang_invoke_CountingMethodHandle::vmcount_offset_in_bytes());
1453 __ incrementl(rcx_mh_vmcount);
1454 }
1455 // fall through
1457 case _adapter_retype_only:
1458 case _adapter_retype_raw:
1459 // immediately jump to the next MH layer:
1460 __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
1461 __ verify_oop(rcx_recv);
1462 __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
1463 // This is OK when all parameter types widen.
1464 // It is also OK when a return type narrows.
1465 break;
1467 case _adapter_check_cast:
1468 {
1469 // temps:
1470 Register rbx_klass = rbx_temp; // interesting AMH data
1472 // check a reference argument before jumping to the next layer of MH:
1473 __ movl(rax_argslot, rcx_amh_vmargslot);
1474 vmarg = __ argument_address(rax_argslot);
1476 // What class are we casting to?
1477 __ load_heap_oop(rbx_klass, rcx_amh_argument); // this is a Class object!
1478 load_klass_from_Class(_masm, rbx_klass);
1480 Label done;
1481 __ movptr(rdx_temp, vmarg);
1482 __ testptr(rdx_temp, rdx_temp);
1483 __ jcc(Assembler::zero, done); // no cast if null
1484 __ load_klass(rdx_temp, rdx_temp);
1486 // live at this point:
1487 // - rbx_klass: klass required by the target method
1488 // - rdx_temp: argument klass to test
1489 // - rcx_recv: adapter method handle
1490 __ check_klass_subtype(rdx_temp, rbx_klass, rax_argslot, done);
1492 // If we get here, the type check failed!
1493 // Call the wrong_method_type stub, passing the failing argument type in rax.
1494 Register rax_mtype = rax_argslot;
1495 __ movl(rax_argslot, rcx_amh_vmargslot); // reload argslot field
1496 __ movptr(rdx_temp, vmarg);
1498 assert_different_registers(rarg2_required, rdx_temp);
1499 __ load_heap_oop(rarg2_required, rcx_amh_argument); // required class
1500 __ mov( rarg1_actual, rdx_temp); // bad object
1501 __ movl( rarg0_code, (int) Bytecodes::_checkcast); // who is complaining?
1502 __ jump(ExternalAddress(from_interpreted_entry(_raise_exception)));
1504 __ bind(done);
1505 // get the new MH:
1506 __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
1507 __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
1508 }
1509 break;
1511 case _adapter_prim_to_prim:
1512 case _adapter_ref_to_prim:
1513 case _adapter_prim_to_ref:
1514 // handled completely by optimized cases
1515 __ stop("init_AdapterMethodHandle should not issue this");
1516 break;
1518 case _adapter_opt_i2i: // optimized subcase of adapt_prim_to_prim
1519 //case _adapter_opt_f2i: // optimized subcase of adapt_prim_to_prim
1520 case _adapter_opt_l2i: // optimized subcase of adapt_prim_to_prim
1521 case _adapter_opt_unboxi: // optimized subcase of adapt_ref_to_prim
1522 {
1523 // perform an in-place conversion to int or an int subword
1524 __ movl(rax_argslot, rcx_amh_vmargslot);
1525 vmarg = __ argument_address(rax_argslot);
1527 switch (ek) {
1528 case _adapter_opt_i2i:
1529 __ movl(rdx_temp, vmarg);
1530 break;
1531 case _adapter_opt_l2i:
1532 {
1533 // just delete the extra slot; on a little-endian machine we keep the first
1534 __ lea(rax_argslot, __ argument_address(rax_argslot, 1));
1535 remove_arg_slots(_masm, -stack_move_unit(),
1536 rax_argslot, rbx_temp, rdx_temp);
1537 vmarg = Address(rax_argslot, -Interpreter::stackElementSize);
1538 __ movl(rdx_temp, vmarg);
1539 }
1540 break;
1541 case _adapter_opt_unboxi:
1542 {
1543 // Load the value up from the heap.
1544 __ movptr(rdx_temp, vmarg);
1545 int value_offset = java_lang_boxing_object::value_offset_in_bytes(T_INT);
1546 #ifdef ASSERT
1547 for (int bt = T_BOOLEAN; bt < T_INT; bt++) {
1548 if (is_subword_type(BasicType(bt)))
1549 assert(value_offset == java_lang_boxing_object::value_offset_in_bytes(BasicType(bt)), "");
1550 }
1551 #endif
1552 __ null_check(rdx_temp, value_offset);
1553 __ movl(rdx_temp, Address(rdx_temp, value_offset));
1554 // We load this as a word. Because we are little-endian,
1555 // the low bits will be correct, but the high bits may need cleaning.
1556 // The vminfo will guide us to clean those bits.
1557 }
1558 break;
1559 default:
1560 ShouldNotReachHere();
1561 }
1563 // Do the requested conversion and store the value.
1564 Register rbx_vminfo = rbx_temp;
1565 load_conversion_vminfo(_masm, rbx_vminfo, rcx_amh_conversion);
1567 // get the new MH:
1568 __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
1569 // (now we are done with the old MH)
1571 // original 32-bit vmdata word must be of this form:
1572 // | MBZ:6 | signBitCount:8 | srcDstTypes:8 | conversionOp:8 |
1573 __ xchgptr(rcx, rbx_vminfo); // free rcx for shifts
1574 __ shll(rdx_temp /*, rcx*/);
1575 Label zero_extend, done;
1576 __ testl(rcx, CONV_VMINFO_SIGN_FLAG);
1577 __ jccb(Assembler::zero, zero_extend);
1579 // this path is taken for int->byte, int->short
1580 __ sarl(rdx_temp /*, rcx*/);
1581 __ jmpb(done);
1583 __ bind(zero_extend);
1584 // this is taken for int->char
1585 __ shrl(rdx_temp /*, rcx*/);
1587 __ bind(done);
1588 __ movl(vmarg, rdx_temp); // Store the value.
1589 __ xchgptr(rcx, rbx_vminfo); // restore rcx_recv
1591 __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
1592 }
1593 break;
1595 case _adapter_opt_i2l: // optimized subcase of adapt_prim_to_prim
1596 case _adapter_opt_unboxl: // optimized subcase of adapt_ref_to_prim
1597 {
1598 // perform an in-place int-to-long or ref-to-long conversion
1599 __ movl(rax_argslot, rcx_amh_vmargslot);
1601 // on a little-endian machine we keep the first slot and add another after
1602 __ lea(rax_argslot, __ argument_address(rax_argslot, 1));
1603 insert_arg_slots(_masm, stack_move_unit(),
1604 rax_argslot, rbx_temp, rdx_temp);
1605 Address vmarg1(rax_argslot, -Interpreter::stackElementSize);
1606 Address vmarg2 = vmarg1.plus_disp(Interpreter::stackElementSize);
1608 switch (ek) {
1609 case _adapter_opt_i2l:
1610 {
1611 #ifdef _LP64
1612 __ movslq(rdx_temp, vmarg1); // Load sign-extended
1613 __ movq(vmarg1, rdx_temp); // Store into first slot
1614 #else
1615 __ movl(rdx_temp, vmarg1);
1616 __ sarl(rdx_temp, BitsPerInt - 1); // __ extend_sign()
1617 __ movl(vmarg2, rdx_temp); // store second word
1618 #endif
1619 }
1620 break;
1621 case _adapter_opt_unboxl:
1622 {
1623 // Load the value up from the heap.
1624 __ movptr(rdx_temp, vmarg1);
1625 int value_offset = java_lang_boxing_object::value_offset_in_bytes(T_LONG);
1626 assert(value_offset == java_lang_boxing_object::value_offset_in_bytes(T_DOUBLE), "");
1627 __ null_check(rdx_temp, value_offset);
1628 #ifdef _LP64
1629 __ movq(rbx_temp, Address(rdx_temp, value_offset));
1630 __ movq(vmarg1, rbx_temp);
1631 #else
1632 __ movl(rbx_temp, Address(rdx_temp, value_offset + 0*BytesPerInt));
1633 __ movl(rdx_temp, Address(rdx_temp, value_offset + 1*BytesPerInt));
1634 __ movl(vmarg1, rbx_temp);
1635 __ movl(vmarg2, rdx_temp);
1636 #endif
1637 }
1638 break;
1639 default:
1640 ShouldNotReachHere();
1641 }
1643 __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
1644 __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
1645 }
1646 break;
1648 case _adapter_opt_f2d: // optimized subcase of adapt_prim_to_prim
1649 case _adapter_opt_d2f: // optimized subcase of adapt_prim_to_prim
1650 {
1651 // perform an in-place floating primitive conversion
1652 __ movl(rax_argslot, rcx_amh_vmargslot);
1653 __ lea(rax_argslot, __ argument_address(rax_argslot, 1));
1654 if (ek == _adapter_opt_f2d) {
1655 insert_arg_slots(_masm, stack_move_unit(),
1656 rax_argslot, rbx_temp, rdx_temp);
1657 }
1658 Address vmarg(rax_argslot, -Interpreter::stackElementSize);
1660 #ifdef _LP64
1661 if (ek == _adapter_opt_f2d) {
1662 __ movflt(xmm0, vmarg);
1663 __ cvtss2sd(xmm0, xmm0);
1664 __ movdbl(vmarg, xmm0);
1665 } else {
1666 __ movdbl(xmm0, vmarg);
1667 __ cvtsd2ss(xmm0, xmm0);
1668 __ movflt(vmarg, xmm0);
1669 }
1670 #else //_LP64
1671 if (ek == _adapter_opt_f2d) {
1672 __ fld_s(vmarg); // load float to ST0
1673 __ fstp_d(vmarg); // store double
1674 } else {
1675 __ fld_d(vmarg); // load double to ST0
1676 __ fstp_s(vmarg); // store single
1677 }
1678 #endif //_LP64
1680 if (ek == _adapter_opt_d2f) {
1681 remove_arg_slots(_masm, -stack_move_unit(),
1682 rax_argslot, rbx_temp, rdx_temp);
1683 }
1685 __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
1686 __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
1687 }
1688 break;
1690 case _adapter_swap_args:
1691 case _adapter_rot_args:
1692 // handled completely by optimized cases
1693 __ stop("init_AdapterMethodHandle should not issue this");
1694 break;
1696 case _adapter_opt_swap_1:
1697 case _adapter_opt_swap_2:
1698 case _adapter_opt_rot_1_up:
1699 case _adapter_opt_rot_1_down:
1700 case _adapter_opt_rot_2_up:
1701 case _adapter_opt_rot_2_down:
1702 {
1703 int swap_slots = ek_adapter_opt_swap_slots(ek);
1704 int rotate = ek_adapter_opt_swap_mode(ek);
1706 // 'argslot' is the position of the first argument to swap
1707 __ movl(rax_argslot, rcx_amh_vmargslot);
1708 __ lea(rax_argslot, __ argument_address(rax_argslot));
1710 // 'vminfo' is the second
1711 Register rbx_destslot = rbx_temp;
1712 load_conversion_vminfo(_masm, rbx_destslot, rcx_amh_conversion);
1713 __ lea(rbx_destslot, __ argument_address(rbx_destslot));
1714 if (VerifyMethodHandles)
1715 verify_argslot(_masm, rbx_destslot, "swap point must fall within current frame");
1717 assert(Interpreter::stackElementSize == wordSize, "else rethink use of wordSize here");
1718 if (!rotate) {
1719 // simple swap
1720 for (int i = 0; i < swap_slots; i++) {
1721 __ movptr(rdi_temp, Address(rax_argslot, i * wordSize));
1722 __ movptr(rdx_temp, Address(rbx_destslot, i * wordSize));
1723 __ movptr(Address(rax_argslot, i * wordSize), rdx_temp);
1724 __ movptr(Address(rbx_destslot, i * wordSize), rdi_temp);
1725 }
1726 } else {
1727 // A rotate is actually pair of moves, with an "odd slot" (or pair)
1728 // changing place with a series of other slots.
1729 // First, push the "odd slot", which is going to get overwritten
1730 for (int i = swap_slots - 1; i >= 0; i--) {
1731 // handle one with rdi_temp instead of a push:
1732 if (i == 0) __ movptr(rdi_temp, Address(rax_argslot, i * wordSize));
1733 else __ pushptr( Address(rax_argslot, i * wordSize));
1734 }
1735 if (rotate > 0) {
1736 // Here is rotate > 0:
1737 // (low mem) (high mem)
1738 // | dest: more_slots... | arg: odd_slot :arg+1 |
1739 // =>
1740 // | dest: odd_slot | dest+1: more_slots... :arg+1 |
1741 // work argslot down to destslot, copying contiguous data upwards
1742 // pseudo-code:
1743 // rax = src_addr - swap_bytes
1744 // rbx = dest_addr
1745 // while (rax >= rbx) *(rax + swap_bytes) = *(rax + 0), rax--;
1746 move_arg_slots_up(_masm,
1747 rbx_destslot,
1748 Address(rax_argslot, 0),
1749 swap_slots,
1750 rax_argslot, rdx_temp);
1751 } else {
1752 // Here is the other direction, rotate < 0:
1753 // (low mem) (high mem)
1754 // | arg: odd_slot | arg+1: more_slots... :dest+1 |
1755 // =>
1756 // | arg: more_slots... | dest: odd_slot :dest+1 |
1757 // work argslot up to destslot, copying contiguous data downwards
1758 // pseudo-code:
1759 // rax = src_addr + swap_bytes
1760 // rbx = dest_addr
1761 // while (rax <= rbx) *(rax - swap_bytes) = *(rax + 0), rax++;
1762 // dest_slot denotes an exclusive upper limit
1763 int limit_bias = OP_ROT_ARGS_DOWN_LIMIT_BIAS;
1764 if (limit_bias != 0)
1765 __ addptr(rbx_destslot, - limit_bias * wordSize);
1766 move_arg_slots_down(_masm,
1767 Address(rax_argslot, swap_slots * wordSize),
1768 rbx_destslot,
1769 -swap_slots,
1770 rax_argslot, rdx_temp);
1771 __ subptr(rbx_destslot, swap_slots * wordSize);
1772 }
1773 // pop the original first chunk into the destination slot, now free
1774 for (int i = 0; i < swap_slots; i++) {
1775 if (i == 0) __ movptr(Address(rbx_destslot, i * wordSize), rdi_temp);
1776 else __ popptr(Address(rbx_destslot, i * wordSize));
1777 }
1778 }
1780 __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
1781 __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
1782 }
1783 break;
1785 case _adapter_dup_args:
1786 {
1787 // 'argslot' is the position of the first argument to duplicate
1788 __ movl(rax_argslot, rcx_amh_vmargslot);
1789 __ lea(rax_argslot, __ argument_address(rax_argslot));
1791 // 'stack_move' is negative number of words to duplicate
1792 Register rdi_stack_move = rdi_temp;
1793 load_stack_move(_masm, rdi_stack_move, rcx_recv, true);
1795 if (VerifyMethodHandles) {
1796 verify_argslots(_masm, rdi_stack_move, rax_argslot, true,
1797 "copied argument(s) must fall within current frame");
1798 }
1800 // insert location is always the bottom of the argument list:
1801 Address insert_location = __ argument_address(constant(0));
1802 int pre_arg_words = insert_location.disp() / wordSize; // return PC is pushed
1803 assert(insert_location.base() == rsp, "");
1805 __ negl(rdi_stack_move);
1806 push_arg_slots(_masm, rax_argslot, rdi_stack_move,
1807 pre_arg_words, rbx_temp, rdx_temp);
1809 __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
1810 __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
1811 }
1812 break;
1814 case _adapter_drop_args:
1815 {
1816 // 'argslot' is the position of the first argument to nuke
1817 __ movl(rax_argslot, rcx_amh_vmargslot);
1818 __ lea(rax_argslot, __ argument_address(rax_argslot));
1820 // (must do previous push after argslot address is taken)
1822 // 'stack_move' is number of words to drop
1823 Register rdi_stack_move = rdi_temp;
1824 load_stack_move(_masm, rdi_stack_move, rcx_recv, false);
1825 remove_arg_slots(_masm, rdi_stack_move,
1826 rax_argslot, rbx_temp, rdx_temp);
1828 __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
1829 __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
1830 }
1831 break;
1833 case _adapter_collect_args:
1834 case _adapter_fold_args:
1835 case _adapter_spread_args:
1836 // handled completely by optimized cases
1837 __ stop("init_AdapterMethodHandle should not issue this");
1838 break;
1840 case _adapter_opt_collect_ref:
1841 case _adapter_opt_collect_int:
1842 case _adapter_opt_collect_long:
1843 case _adapter_opt_collect_float:
1844 case _adapter_opt_collect_double:
1845 case _adapter_opt_collect_void:
1846 case _adapter_opt_collect_0_ref:
1847 case _adapter_opt_collect_1_ref:
1848 case _adapter_opt_collect_2_ref:
1849 case _adapter_opt_collect_3_ref:
1850 case _adapter_opt_collect_4_ref:
1851 case _adapter_opt_collect_5_ref:
1852 case _adapter_opt_filter_S0_ref:
1853 case _adapter_opt_filter_S1_ref:
1854 case _adapter_opt_filter_S2_ref:
1855 case _adapter_opt_filter_S3_ref:
1856 case _adapter_opt_filter_S4_ref:
1857 case _adapter_opt_filter_S5_ref:
1858 case _adapter_opt_collect_2_S0_ref:
1859 case _adapter_opt_collect_2_S1_ref:
1860 case _adapter_opt_collect_2_S2_ref:
1861 case _adapter_opt_collect_2_S3_ref:
1862 case _adapter_opt_collect_2_S4_ref:
1863 case _adapter_opt_collect_2_S5_ref:
1864 case _adapter_opt_fold_ref:
1865 case _adapter_opt_fold_int:
1866 case _adapter_opt_fold_long:
1867 case _adapter_opt_fold_float:
1868 case _adapter_opt_fold_double:
1869 case _adapter_opt_fold_void:
1870 case _adapter_opt_fold_1_ref:
1871 case _adapter_opt_fold_2_ref:
1872 case _adapter_opt_fold_3_ref:
1873 case _adapter_opt_fold_4_ref:
1874 case _adapter_opt_fold_5_ref:
1875 {
1876 // Given a fresh incoming stack frame, build a new ricochet frame.
1877 // On entry, TOS points at a return PC, and RBP is the callers frame ptr.
1878 // RSI/R13 has the caller's exact stack pointer, which we must also preserve.
1879 // RCX contains an AdapterMethodHandle of the indicated kind.
1881 // Relevant AMH fields:
1882 // amh.vmargslot:
1883 // points to the trailing edge of the arguments
1884 // to filter, collect, or fold. For a boxing operation,
1885 // it points just after the single primitive value.
1886 // amh.argument:
1887 // recursively called MH, on |collect| arguments
1888 // amh.vmtarget:
1889 // final destination MH, on return value, etc.
1890 // amh.conversion.dest:
1891 // tells what is the type of the return value
1892 // (not needed here, since dest is also derived from ek)
1893 // amh.conversion.vminfo:
1894 // points to the trailing edge of the return value
1895 // when the vmtarget is to be called; this is
1896 // equal to vmargslot + (retained ? |collect| : 0)
1898 // Pass 0 or more argument slots to the recursive target.
1899 int collect_count_constant = ek_adapter_opt_collect_count(ek);
1901 // The collected arguments are copied from the saved argument list:
1902 int collect_slot_constant = ek_adapter_opt_collect_slot(ek);
1904 assert(ek_orig == _adapter_collect_args ||
1905 ek_orig == _adapter_fold_args, "");
1906 bool retain_original_args = (ek_orig == _adapter_fold_args);
1908 // The return value is replaced (or inserted) at the 'vminfo' argslot.
1909 // Sometimes we can compute this statically.
1910 int dest_slot_constant = -1;
1911 if (!retain_original_args)
1912 dest_slot_constant = collect_slot_constant;
1913 else if (collect_slot_constant >= 0 && collect_count_constant >= 0)
1914 // We are preserving all the arguments, and the return value is prepended,
1915 // so the return slot is to the left (above) the |collect| sequence.
1916 dest_slot_constant = collect_slot_constant + collect_count_constant;
1918 // Replace all those slots by the result of the recursive call.
1919 // The result type can be one of ref, int, long, float, double, void.
1920 // In the case of void, nothing is pushed on the stack after return.
1921 BasicType dest = ek_adapter_opt_collect_type(ek);
1922 assert(dest == type2wfield[dest], "dest is a stack slot type");
1923 int dest_count = type2size[dest];
1924 assert(dest_count == 1 || dest_count == 2 || (dest_count == 0 && dest == T_VOID), "dest has a size");
1926 // Choose a return continuation.
1927 EntryKind ek_ret = _adapter_opt_return_any;
1928 if (dest != T_CONFLICT && OptimizeMethodHandles) {
1929 switch (dest) {
1930 case T_INT : ek_ret = _adapter_opt_return_int; break;
1931 case T_LONG : ek_ret = _adapter_opt_return_long; break;
1932 case T_FLOAT : ek_ret = _adapter_opt_return_float; break;
1933 case T_DOUBLE : ek_ret = _adapter_opt_return_double; break;
1934 case T_OBJECT : ek_ret = _adapter_opt_return_ref; break;
1935 case T_VOID : ek_ret = _adapter_opt_return_void; break;
1936 default : ShouldNotReachHere();
1937 }
1938 if (dest == T_OBJECT && dest_slot_constant >= 0) {
1939 EntryKind ek_try = EntryKind(_adapter_opt_return_S0_ref + dest_slot_constant);
1940 if (ek_try <= _adapter_opt_return_LAST &&
1941 ek_adapter_opt_return_slot(ek_try) == dest_slot_constant) {
1942 ek_ret = ek_try;
1943 }
1944 }
1945 assert(ek_adapter_opt_return_type(ek_ret) == dest, "");
1946 }
1948 // Already pushed: ... keep1 | collect | keep2 | sender_pc |
1949 // push(sender_pc);
1951 // Compute argument base:
1952 Register rax_argv = rax_argslot;
1953 __ lea(rax_argv, __ argument_address(constant(0)));
1955 // Push a few extra argument words, if we need them to store the return value.
1956 {
1957 int extra_slots = 0;
1958 if (retain_original_args) {
1959 extra_slots = dest_count;
1960 } else if (collect_count_constant == -1) {
1961 extra_slots = dest_count; // collect_count might be zero; be generous
1962 } else if (dest_count > collect_count_constant) {
1963 extra_slots = (dest_count - collect_count_constant);
1964 } else {
1965 // else we know we have enough dead space in |collect| to repurpose for return values
1966 }
1967 DEBUG_ONLY(extra_slots += 1);
1968 if (extra_slots > 0) {
1969 __ pop(rbx_temp); // return value
1970 __ subptr(rsp, (extra_slots * Interpreter::stackElementSize));
1971 // Push guard word #2 in debug mode.
1972 DEBUG_ONLY(__ movptr(Address(rsp, 0), (int32_t) RicochetFrame::MAGIC_NUMBER_2));
1973 __ push(rbx_temp);
1974 }
1975 }
1977 RicochetFrame::enter_ricochet_frame(_masm, rcx_recv, rax_argv,
1978 entry(ek_ret)->from_interpreted_entry(), rbx_temp);
1980 // Now pushed: ... keep1 | collect | keep2 | RF |
1981 // some handy frame slots:
1982 Address exact_sender_sp_addr = RicochetFrame::frame_address(RicochetFrame::exact_sender_sp_offset_in_bytes());
1983 Address conversion_addr = RicochetFrame::frame_address(RicochetFrame::conversion_offset_in_bytes());
1984 Address saved_args_base_addr = RicochetFrame::frame_address(RicochetFrame::saved_args_base_offset_in_bytes());
1986 #ifdef ASSERT
1987 if (VerifyMethodHandles && dest != T_CONFLICT) {
1988 BLOCK_COMMENT("verify AMH.conv.dest");
1989 load_conversion_dest_type(_masm, rbx_temp, conversion_addr);
1990 Label L_dest_ok;
1991 __ cmpl(rbx_temp, (int) dest);
1992 __ jcc(Assembler::equal, L_dest_ok);
1993 if (dest == T_INT) {
1994 for (int bt = T_BOOLEAN; bt < T_INT; bt++) {
1995 if (is_subword_type(BasicType(bt))) {
1996 __ cmpl(rbx_temp, (int) bt);
1997 __ jcc(Assembler::equal, L_dest_ok);
1998 }
1999 }
2000 }
2001 __ stop("bad dest in AMH.conv");
2002 __ BIND(L_dest_ok);
2003 }
2004 #endif //ASSERT
2006 // Find out where the original copy of the recursive argument sequence begins.
2007 Register rax_coll = rax_argv;
2008 {
2009 RegisterOrConstant collect_slot = collect_slot_constant;
2010 if (collect_slot_constant == -1) {
2011 __ movl(rdi_temp, rcx_amh_vmargslot);
2012 collect_slot = rdi_temp;
2013 }
2014 if (collect_slot_constant != 0)
2015 __ lea(rax_coll, Address(rax_argv, collect_slot, Interpreter::stackElementScale()));
2016 // rax_coll now points at the trailing edge of |collect| and leading edge of |keep2|
2017 }
2019 // Replace the old AMH with the recursive MH. (No going back now.)
2020 // In the case of a boxing call, the recursive call is to a 'boxer' method,
2021 // such as Integer.valueOf or Long.valueOf. In the case of a filter
2022 // or collect call, it will take one or more arguments, transform them,
2023 // and return some result, to store back into argument_base[vminfo].
2024 __ load_heap_oop(rcx_recv, rcx_amh_argument);
2025 if (VerifyMethodHandles) verify_method_handle(_masm, rcx_recv);
2027 // Push a space for the recursively called MH first:
2028 __ push((int32_t)NULL_WORD);
2030 // Calculate |collect|, the number of arguments we are collecting.
2031 Register rdi_collect_count = rdi_temp;
2032 RegisterOrConstant collect_count;
2033 if (collect_count_constant >= 0) {
2034 collect_count = collect_count_constant;
2035 } else {
2036 __ load_method_handle_vmslots(rdi_collect_count, rcx_recv, rdx_temp);
2037 collect_count = rdi_collect_count;
2038 }
2039 #ifdef ASSERT
2040 if (VerifyMethodHandles && collect_count_constant >= 0) {
2041 __ load_method_handle_vmslots(rbx_temp, rcx_recv, rdx_temp);
2042 Label L_count_ok;
2043 __ cmpl(rbx_temp, collect_count_constant);
2044 __ jcc(Assembler::equal, L_count_ok);
2045 __ stop("bad vminfo in AMH.conv");
2046 __ BIND(L_count_ok);
2047 }
2048 #endif //ASSERT
2050 // copy |collect| slots directly to TOS:
2051 push_arg_slots(_masm, rax_coll, collect_count, 0, rbx_temp, rdx_temp);
2052 // Now pushed: ... keep1 | collect | keep2 | RF... | collect |
2053 // rax_coll still points at the trailing edge of |collect| and leading edge of |keep2|
2055 // If necessary, adjust the saved arguments to make room for the eventual return value.
2056 // Normal adjustment: ... keep1 | +dest+ | -collect- | keep2 | RF... | collect |
2057 // If retaining args: ... keep1 | +dest+ | collect | keep2 | RF... | collect |
2058 // In the non-retaining case, this might move keep2 either up or down.
2059 // We don't have to copy the whole | RF... collect | complex,
2060 // but we must adjust RF.saved_args_base.
2061 // Also, from now on, we will forget about the original copy of |collect|.
2062 // If we are retaining it, we will treat it as part of |keep2|.
2063 // For clarity we will define |keep3| = |collect|keep2| or |keep2|.
2065 BLOCK_COMMENT("adjust trailing arguments {");
2066 // Compare the sizes of |+dest+| and |-collect-|, which are opposed opening and closing movements.
2067 int open_count = dest_count;
2068 RegisterOrConstant close_count = collect_count_constant;
2069 Register rdi_close_count = rdi_collect_count;
2070 if (retain_original_args) {
2071 close_count = constant(0);
2072 } else if (collect_count_constant == -1) {
2073 close_count = rdi_collect_count;
2074 }
2076 // How many slots need moving? This is simply dest_slot (0 => no |keep3|).
2077 RegisterOrConstant keep3_count;
2078 Register rsi_keep3_count = rsi; // can repair from RF.exact_sender_sp
2079 if (dest_slot_constant >= 0) {
2080 keep3_count = dest_slot_constant;
2081 } else {
2082 load_conversion_vminfo(_masm, rsi_keep3_count, conversion_addr);
2083 keep3_count = rsi_keep3_count;
2084 }
2085 #ifdef ASSERT
2086 if (VerifyMethodHandles && dest_slot_constant >= 0) {
2087 load_conversion_vminfo(_masm, rbx_temp, conversion_addr);
2088 Label L_vminfo_ok;
2089 __ cmpl(rbx_temp, dest_slot_constant);
2090 __ jcc(Assembler::equal, L_vminfo_ok);
2091 __ stop("bad vminfo in AMH.conv");
2092 __ BIND(L_vminfo_ok);
2093 }
2094 #endif //ASSERT
2096 // tasks remaining:
2097 bool move_keep3 = (!keep3_count.is_constant() || keep3_count.as_constant() != 0);
2098 bool stomp_dest = (NOT_DEBUG(dest == T_OBJECT) DEBUG_ONLY(dest_count != 0));
2099 bool fix_arg_base = (!close_count.is_constant() || open_count != close_count.as_constant());
2101 if (stomp_dest | fix_arg_base) {
2102 // we will probably need an updated rax_argv value
2103 if (collect_slot_constant >= 0) {
2104 // rax_coll already holds the leading edge of |keep2|, so tweak it
2105 assert(rax_coll == rax_argv, "elided a move");
2106 if (collect_slot_constant != 0)
2107 __ subptr(rax_argv, collect_slot_constant * Interpreter::stackElementSize);
2108 } else {
2109 // Just reload from RF.saved_args_base.
2110 __ movptr(rax_argv, saved_args_base_addr);
2111 }
2112 }
2114 // Old and new argument locations (based at slot 0).
2115 // Net shift (&new_argv - &old_argv) is (close_count - open_count).
2116 bool zero_open_count = (open_count == 0); // remember this bit of info
2117 if (move_keep3 && fix_arg_base) {
2118 // It will be easier to have everything in one register:
2119 if (close_count.is_register()) {
2120 // Deduct open_count from close_count register to get a clean +/- value.
2121 __ subptr(close_count.as_register(), open_count);
2122 } else {
2123 close_count = close_count.as_constant() - open_count;
2124 }
2125 open_count = 0;
2126 }
2127 Address old_argv(rax_argv, 0);
2128 Address new_argv(rax_argv, close_count, Interpreter::stackElementScale(),
2129 - open_count * Interpreter::stackElementSize);
2131 // First decide if any actual data are to be moved.
2132 // We can skip if (a) |keep3| is empty, or (b) the argument list size didn't change.
2133 // (As it happens, all movements involve an argument list size change.)
2135 // If there are variable parameters, use dynamic checks to skip around the whole mess.
2136 Label L_done;
2137 if (!keep3_count.is_constant()) {
2138 __ testl(keep3_count.as_register(), keep3_count.as_register());
2139 __ jcc(Assembler::zero, L_done);
2140 }
2141 if (!close_count.is_constant()) {
2142 __ cmpl(close_count.as_register(), open_count);
2143 __ jcc(Assembler::equal, L_done);
2144 }
2146 if (move_keep3 && fix_arg_base) {
2147 bool emit_move_down = false, emit_move_up = false, emit_guard = false;
2148 if (!close_count.is_constant()) {
2149 emit_move_down = emit_guard = !zero_open_count;
2150 emit_move_up = true;
2151 } else if (open_count != close_count.as_constant()) {
2152 emit_move_down = (open_count > close_count.as_constant());
2153 emit_move_up = !emit_move_down;
2154 }
2155 Label L_move_up;
2156 if (emit_guard) {
2157 __ cmpl(close_count.as_register(), open_count);
2158 __ jcc(Assembler::greater, L_move_up);
2159 }
2161 if (emit_move_down) {
2162 // Move arguments down if |+dest+| > |-collect-|
2163 // (This is rare, except when arguments are retained.)
2164 // This opens space for the return value.
2165 if (keep3_count.is_constant()) {
2166 for (int i = 0; i < keep3_count.as_constant(); i++) {
2167 __ movptr(rdx_temp, old_argv.plus_disp(i * Interpreter::stackElementSize));
2168 __ movptr( new_argv.plus_disp(i * Interpreter::stackElementSize), rdx_temp);
2169 }
2170 } else {
2171 Register rbx_argv_top = rbx_temp;
2172 __ lea(rbx_argv_top, old_argv.plus_disp(keep3_count, Interpreter::stackElementScale()));
2173 move_arg_slots_down(_masm,
2174 old_argv, // beginning of old argv
2175 rbx_argv_top, // end of old argv
2176 close_count, // distance to move down (must be negative)
2177 rax_argv, rdx_temp);
2178 // Used argv as an iteration variable; reload from RF.saved_args_base.
2179 __ movptr(rax_argv, saved_args_base_addr);
2180 }
2181 }
2183 if (emit_guard) {
2184 __ jmp(L_done); // assumes emit_move_up is true also
2185 __ BIND(L_move_up);
2186 }
2188 if (emit_move_up) {
2190 // Move arguments up if |+dest+| < |-collect-|
2191 // (This is usual, except when |keep3| is empty.)
2192 // This closes up the space occupied by the now-deleted collect values.
2193 if (keep3_count.is_constant()) {
2194 for (int i = keep3_count.as_constant() - 1; i >= 0; i--) {
2195 __ movptr(rdx_temp, old_argv.plus_disp(i * Interpreter::stackElementSize));
2196 __ movptr( new_argv.plus_disp(i * Interpreter::stackElementSize), rdx_temp);
2197 }
2198 } else {
2199 Address argv_top = old_argv.plus_disp(keep3_count, Interpreter::stackElementScale());
2200 move_arg_slots_up(_masm,
2201 rax_argv, // beginning of old argv
2202 argv_top, // end of old argv
2203 close_count, // distance to move up (must be positive)
2204 rbx_temp, rdx_temp);
2205 }
2206 }
2207 }
2208 __ BIND(L_done);
2210 if (fix_arg_base) {
2211 // adjust RF.saved_args_base by adding (close_count - open_count)
2212 if (!new_argv.is_same_address(Address(rax_argv, 0)))
2213 __ lea(rax_argv, new_argv);
2214 __ movptr(saved_args_base_addr, rax_argv);
2215 }
2217 if (stomp_dest) {
2218 // Stomp the return slot, so it doesn't hold garbage.
2219 // This isn't strictly necessary, but it may help detect bugs.
2220 int forty_two = RicochetFrame::RETURN_VALUE_PLACEHOLDER;
2221 __ movptr(Address(rax_argv, keep3_count, Address::times_ptr),
2222 (int32_t) forty_two);
2223 // uses rsi_keep3_count
2224 }
2225 BLOCK_COMMENT("} adjust trailing arguments");
2227 BLOCK_COMMENT("do_recursive_call");
2228 __ mov(saved_last_sp, rsp); // set rsi/r13 for callee
2229 __ pushptr(ExternalAddress(SharedRuntime::ricochet_blob()->bounce_addr()).addr());
2230 // The globally unique bounce address has two purposes:
2231 // 1. It helps the JVM recognize this frame (frame::is_ricochet_frame).
2232 // 2. When returned to, it cuts back the stack and redirects control flow
2233 // to the return handler.
2234 // The return handler will further cut back the stack when it takes
2235 // down the RF. Perhaps there is a way to streamline this further.
2237 // State during recursive call:
2238 // ... keep1 | dest | dest=42 | keep3 | RF... | collect | bounce_pc |
2239 __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
2241 break;
2242 }
2244 case _adapter_opt_return_ref:
2245 case _adapter_opt_return_int:
2246 case _adapter_opt_return_long:
2247 case _adapter_opt_return_float:
2248 case _adapter_opt_return_double:
2249 case _adapter_opt_return_void:
2250 case _adapter_opt_return_S0_ref:
2251 case _adapter_opt_return_S1_ref:
2252 case _adapter_opt_return_S2_ref:
2253 case _adapter_opt_return_S3_ref:
2254 case _adapter_opt_return_S4_ref:
2255 case _adapter_opt_return_S5_ref:
2256 {
2257 BasicType dest_type_constant = ek_adapter_opt_return_type(ek);
2258 int dest_slot_constant = ek_adapter_opt_return_slot(ek);
2260 if (VerifyMethodHandles) RicochetFrame::verify_clean(_masm);
2262 if (dest_slot_constant == -1) {
2263 // The current stub is a general handler for this dest_type.
2264 // It can be called from _adapter_opt_return_any below.
2265 // Stash the address in a little table.
2266 assert((dest_type_constant & CONV_TYPE_MASK) == dest_type_constant, "oob");
2267 address return_handler = __ pc();
2268 _adapter_return_handlers[dest_type_constant] = return_handler;
2269 if (dest_type_constant == T_INT) {
2270 // do the subword types too
2271 for (int bt = T_BOOLEAN; bt < T_INT; bt++) {
2272 if (is_subword_type(BasicType(bt)) &&
2273 _adapter_return_handlers[bt] == NULL) {
2274 _adapter_return_handlers[bt] = return_handler;
2275 }
2276 }
2277 }
2278 }
2280 Register rbx_arg_base = rbx_temp;
2281 assert_different_registers(rax, rdx, // possibly live return value registers
2282 rdi_temp, rbx_arg_base);
2284 Address conversion_addr = RicochetFrame::frame_address(RicochetFrame::conversion_offset_in_bytes());
2285 Address saved_args_base_addr = RicochetFrame::frame_address(RicochetFrame::saved_args_base_offset_in_bytes());
2287 __ movptr(rbx_arg_base, saved_args_base_addr);
2288 RegisterOrConstant dest_slot = dest_slot_constant;
2289 if (dest_slot_constant == -1) {
2290 load_conversion_vminfo(_masm, rdi_temp, conversion_addr);
2291 dest_slot = rdi_temp;
2292 }
2293 // Store the result back into the argslot.
2294 // This code uses the interpreter calling sequence, in which the return value
2295 // is usually left in the TOS register, as defined by InterpreterMacroAssembler::pop.
2296 // There are certain irregularities with floating point values, which can be seen
2297 // in TemplateInterpreterGenerator::generate_return_entry_for.
2298 move_return_value(_masm, dest_type_constant, Address(rbx_arg_base, dest_slot, Interpreter::stackElementScale()));
2300 RicochetFrame::leave_ricochet_frame(_masm, rcx_recv, rbx_arg_base, rdx_temp);
2301 __ push(rdx_temp); // repush the return PC
2303 // Load the final target and go.
2304 if (VerifyMethodHandles) verify_method_handle(_masm, rcx_recv);
2305 __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
2306 __ hlt(); // --------------------
2307 break;
2308 }
2310 case _adapter_opt_return_any:
2311 {
2312 if (VerifyMethodHandles) RicochetFrame::verify_clean(_masm);
2313 Register rdi_conv = rdi_temp;
2314 assert_different_registers(rax, rdx, // possibly live return value registers
2315 rdi_conv, rbx_temp);
2317 Address conversion_addr = RicochetFrame::frame_address(RicochetFrame::conversion_offset_in_bytes());
2318 load_conversion_dest_type(_masm, rdi_conv, conversion_addr);
2319 __ lea(rbx_temp, ExternalAddress((address) &_adapter_return_handlers[0]));
2320 __ movptr(rbx_temp, Address(rbx_temp, rdi_conv, Address::times_ptr));
2322 #ifdef ASSERT
2323 { Label L_badconv;
2324 __ testptr(rbx_temp, rbx_temp);
2325 __ jccb(Assembler::zero, L_badconv);
2326 __ jmp(rbx_temp);
2327 __ bind(L_badconv);
2328 __ stop("bad method handle return");
2329 }
2330 #else //ASSERT
2331 __ jmp(rbx_temp);
2332 #endif //ASSERT
2333 break;
2334 }
2336 case _adapter_opt_spread_0:
2337 case _adapter_opt_spread_1_ref:
2338 case _adapter_opt_spread_2_ref:
2339 case _adapter_opt_spread_3_ref:
2340 case _adapter_opt_spread_4_ref:
2341 case _adapter_opt_spread_5_ref:
2342 case _adapter_opt_spread_ref:
2343 case _adapter_opt_spread_byte:
2344 case _adapter_opt_spread_char:
2345 case _adapter_opt_spread_short:
2346 case _adapter_opt_spread_int:
2347 case _adapter_opt_spread_long:
2348 case _adapter_opt_spread_float:
2349 case _adapter_opt_spread_double:
2350 {
2351 // spread an array out into a group of arguments
2352 int length_constant = ek_adapter_opt_spread_count(ek);
2353 bool length_can_be_zero = (length_constant == 0);
2354 if (length_constant < 0) {
2355 // some adapters with variable length must handle the zero case
2356 if (!OptimizeMethodHandles ||
2357 ek_adapter_opt_spread_type(ek) != T_OBJECT)
2358 length_can_be_zero = true;
2359 }
2361 // find the address of the array argument
2362 __ movl(rax_argslot, rcx_amh_vmargslot);
2363 __ lea(rax_argslot, __ argument_address(rax_argslot));
2365 // grab another temp
2366 Register rsi_temp = rsi;
2368 // arx_argslot points both to the array and to the first output arg
2369 vmarg = Address(rax_argslot, 0);
2371 // Get the array value.
2372 Register rdi_array = rdi_temp;
2373 Register rdx_array_klass = rdx_temp;
2374 BasicType elem_type = ek_adapter_opt_spread_type(ek);
2375 int elem_slots = type2size[elem_type]; // 1 or 2
2376 int array_slots = 1; // array is always a T_OBJECT
2377 int length_offset = arrayOopDesc::length_offset_in_bytes();
2378 int elem0_offset = arrayOopDesc::base_offset_in_bytes(elem_type);
2379 __ movptr(rdi_array, vmarg);
2381 Label L_array_is_empty, L_insert_arg_space, L_copy_args, L_args_done;
2382 if (length_can_be_zero) {
2383 // handle the null pointer case, if zero is allowed
2384 Label L_skip;
2385 if (length_constant < 0) {
2386 load_conversion_vminfo(_masm, rbx_temp, rcx_amh_conversion);
2387 __ testl(rbx_temp, rbx_temp);
2388 __ jcc(Assembler::notZero, L_skip);
2389 }
2390 __ testptr(rdi_array, rdi_array);
2391 __ jcc(Assembler::notZero, L_skip);
2393 // If 'rsi' contains the 'saved_last_sp' (this is only the
2394 // case in a 32-bit version of the VM) we have to save 'rsi'
2395 // on the stack because later on (at 'L_array_is_empty') 'rsi'
2396 // will be overwritten.
2397 { if (rsi_temp == saved_last_sp) __ push(saved_last_sp); }
2398 // Also prepare a handy macro which restores 'rsi' if required.
2399 #define UNPUSH_RSI \
2400 { if (rsi_temp == saved_last_sp) __ pop(saved_last_sp); }
2402 __ jmp(L_array_is_empty);
2403 __ bind(L_skip);
2404 }
2405 __ null_check(rdi_array, oopDesc::klass_offset_in_bytes());
2406 __ load_klass(rdx_array_klass, rdi_array);
2408 // Save 'rsi' if required (see comment above). Do this only
2409 // after the null check such that the exception handler which is
2410 // called in the case of a null pointer exception will not be
2411 // confused by the extra value on the stack (it expects the
2412 // return pointer on top of the stack)
2413 { if (rsi_temp == saved_last_sp) __ push(saved_last_sp); }
2415 // Check the array type.
2416 Register rbx_klass = rbx_temp;
2417 __ load_heap_oop(rbx_klass, rcx_amh_argument); // this is a Class object!
2418 load_klass_from_Class(_masm, rbx_klass);
2420 Label ok_array_klass, bad_array_klass, bad_array_length;
2421 __ check_klass_subtype(rdx_array_klass, rbx_klass, rsi_temp, ok_array_klass);
2422 // If we get here, the type check failed!
2423 __ jmp(bad_array_klass);
2424 __ BIND(ok_array_klass);
2426 // Check length.
2427 if (length_constant >= 0) {
2428 __ cmpl(Address(rdi_array, length_offset), length_constant);
2429 } else {
2430 Register rbx_vminfo = rbx_temp;
2431 load_conversion_vminfo(_masm, rbx_vminfo, rcx_amh_conversion);
2432 __ cmpl(rbx_vminfo, Address(rdi_array, length_offset));
2433 }
2434 __ jcc(Assembler::notEqual, bad_array_length);
2436 Register rdx_argslot_limit = rdx_temp;
2438 // Array length checks out. Now insert any required stack slots.
2439 if (length_constant == -1) {
2440 // Form a pointer to the end of the affected region.
2441 __ lea(rdx_argslot_limit, Address(rax_argslot, Interpreter::stackElementSize));
2442 // 'stack_move' is negative number of words to insert
2443 // This number already accounts for elem_slots.
2444 Register rsi_stack_move = rsi_temp;
2445 load_stack_move(_masm, rsi_stack_move, rcx_recv, true);
2446 __ cmpptr(rsi_stack_move, 0);
2447 assert(stack_move_unit() < 0, "else change this comparison");
2448 __ jcc(Assembler::less, L_insert_arg_space);
2449 __ jcc(Assembler::equal, L_copy_args);
2450 // single argument case, with no array movement
2451 __ BIND(L_array_is_empty);
2452 remove_arg_slots(_masm, -stack_move_unit() * array_slots,
2453 rax_argslot, rbx_temp, rdx_temp);
2454 __ jmp(L_args_done); // no spreading to do
2455 __ BIND(L_insert_arg_space);
2456 // come here in the usual case, stack_move < 0 (2 or more spread arguments)
2457 Register rdi_temp = rdi_array; // spill this
2458 insert_arg_slots(_masm, rsi_stack_move,
2459 rax_argslot, rbx_temp, rdi_temp);
2460 // reload the array since rsi was killed
2461 // reload from rdx_argslot_limit since rax_argslot is now decremented
2462 __ movptr(rdi_array, Address(rdx_argslot_limit, -Interpreter::stackElementSize));
2463 } else if (length_constant >= 1) {
2464 int new_slots = (length_constant * elem_slots) - array_slots;
2465 insert_arg_slots(_masm, new_slots * stack_move_unit(),
2466 rax_argslot, rbx_temp, rdx_temp);
2467 } else if (length_constant == 0) {
2468 __ BIND(L_array_is_empty);
2469 remove_arg_slots(_masm, -stack_move_unit() * array_slots,
2470 rax_argslot, rbx_temp, rdx_temp);
2471 } else {
2472 ShouldNotReachHere();
2473 }
2475 // Copy from the array to the new slots.
2476 // Note: Stack change code preserves integrity of rax_argslot pointer.
2477 // So even after slot insertions, rax_argslot still points to first argument.
2478 // Beware: Arguments that are shallow on the stack are deep in the array,
2479 // and vice versa. So a downward-growing stack (the usual) has to be copied
2480 // elementwise in reverse order from the source array.
2481 __ BIND(L_copy_args);
2482 if (length_constant == -1) {
2483 // [rax_argslot, rdx_argslot_limit) is the area we are inserting into.
2484 // Array element [0] goes at rdx_argslot_limit[-wordSize].
2485 Register rdi_source = rdi_array;
2486 __ lea(rdi_source, Address(rdi_array, elem0_offset));
2487 Register rdx_fill_ptr = rdx_argslot_limit;
2488 Label loop;
2489 __ BIND(loop);
2490 __ addptr(rdx_fill_ptr, -Interpreter::stackElementSize * elem_slots);
2491 move_typed_arg(_masm, elem_type, true,
2492 Address(rdx_fill_ptr, 0), Address(rdi_source, 0),
2493 rbx_temp, rsi_temp);
2494 __ addptr(rdi_source, type2aelembytes(elem_type));
2495 __ cmpptr(rdx_fill_ptr, rax_argslot);
2496 __ jcc(Assembler::above, loop);
2497 } else if (length_constant == 0) {
2498 // nothing to copy
2499 } else {
2500 int elem_offset = elem0_offset;
2501 int slot_offset = length_constant * Interpreter::stackElementSize;
2502 for (int index = 0; index < length_constant; index++) {
2503 slot_offset -= Interpreter::stackElementSize * elem_slots; // fill backward
2504 move_typed_arg(_masm, elem_type, true,
2505 Address(rax_argslot, slot_offset), Address(rdi_array, elem_offset),
2506 rbx_temp, rsi_temp);
2507 elem_offset += type2aelembytes(elem_type);
2508 }
2509 }
2510 __ BIND(L_args_done);
2512 // Arguments are spread. Move to next method handle.
2513 UNPUSH_RSI;
2514 __ load_heap_oop(rcx_recv, rcx_mh_vmtarget);
2515 __ jump_to_method_handle_entry(rcx_recv, rdx_temp);
2517 __ bind(bad_array_klass);
2518 UNPUSH_RSI;
2519 assert(!vmarg.uses(rarg2_required), "must be different registers");
2520 __ load_heap_oop( rarg2_required, Address(rdx_array_klass, java_mirror_offset)); // required type
2521 __ movptr( rarg1_actual, vmarg); // bad array
2522 __ movl( rarg0_code, (int) Bytecodes::_aaload); // who is complaining?
2523 __ jump(ExternalAddress(from_interpreted_entry(_raise_exception)));
2525 __ bind(bad_array_length);
2526 UNPUSH_RSI;
2527 assert(!vmarg.uses(rarg2_required), "must be different registers");
2528 __ mov( rarg2_required, rcx_recv); // AMH requiring a certain length
2529 __ movptr( rarg1_actual, vmarg); // bad array
2530 __ movl( rarg0_code, (int) Bytecodes::_arraylength); // who is complaining?
2531 __ jump(ExternalAddress(from_interpreted_entry(_raise_exception)));
2532 #undef UNPUSH_RSI
2534 break;
2535 }
2537 default:
2538 // do not require all platforms to recognize all adapter types
2539 __ nop();
2540 return;
2541 }
2542 BLOCK_COMMENT(err_msg("} Entry %s", entry_name(ek)));
2543 __ hlt();
2545 address me_cookie = MethodHandleEntry::start_compiled_entry(_masm, interp_entry);
2546 __ unimplemented(entry_name(ek)); // %%% FIXME: NYI
2548 init_entry(ek, MethodHandleEntry::finish_compiled_entry(_masm, me_cookie));
2549 }