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