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