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