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) 2008, 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 "memory/allocation.inline.hpp"
28 #include "prims/methodHandles.hpp"
30 #define __ _masm->
32 #ifdef PRODUCT
33 #define BLOCK_COMMENT(str) /* nothing */
34 #else
35 #define BLOCK_COMMENT(str) __ block_comment(str)
36 #endif
38 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
40 address MethodHandleEntry::start_compiled_entry(MacroAssembler* _masm,
41 address interpreted_entry) {
42 // Just before the actual machine code entry point, allocate space
43 // for a MethodHandleEntry::Data record, so that we can manage everything
44 // from one base pointer.
45 __ align(wordSize);
46 address target = __ pc() + sizeof(Data);
47 while (__ pc() < target) {
48 __ nop();
49 __ align(wordSize);
50 }
52 MethodHandleEntry* me = (MethodHandleEntry*) __ pc();
53 me->set_end_address(__ pc()); // set a temporary end_address
54 me->set_from_interpreted_entry(interpreted_entry);
55 me->set_type_checking_entry(NULL);
57 return (address) me;
58 }
60 MethodHandleEntry* MethodHandleEntry::finish_compiled_entry(MacroAssembler* _masm,
61 address start_addr) {
62 MethodHandleEntry* me = (MethodHandleEntry*) start_addr;
63 assert(me->end_address() == start_addr, "valid ME");
65 // Fill in the real end_address:
66 __ align(wordSize);
67 me->set_end_address(__ pc());
69 return me;
70 }
72 // stack walking support
74 frame MethodHandles::ricochet_frame_sender(const frame& fr, RegisterMap *map) {
75 //RicochetFrame* f = RicochetFrame::from_frame(fr);
76 // Cf. is_interpreted_frame path of frame::sender
77 intptr_t* younger_sp = fr.sp();
78 intptr_t* sp = fr.sender_sp();
79 map->make_integer_regs_unsaved();
80 map->shift_window(sp, younger_sp);
81 bool this_frame_adjusted_stack = true; // I5_savedSP is live in this RF
82 return frame(sp, younger_sp, this_frame_adjusted_stack);
83 }
85 void MethodHandles::ricochet_frame_oops_do(const frame& fr, OopClosure* blk, const RegisterMap* reg_map) {
86 ResourceMark rm;
87 RicochetFrame* f = RicochetFrame::from_frame(fr);
89 // pick up the argument type descriptor:
90 Thread* thread = Thread::current();
91 Handle cookie(thread, f->compute_saved_args_layout(true, true));
93 // process fixed part
94 blk->do_oop((oop*)f->saved_target_addr());
95 blk->do_oop((oop*)f->saved_args_layout_addr());
97 // process variable arguments:
98 if (cookie.is_null()) return; // no arguments to describe
100 // the cookie is actually the invokeExact method for my target
101 // his argument signature is what I'm interested in
102 assert(cookie->is_method(), "");
103 methodHandle invoker(thread, methodOop(cookie()));
104 assert(invoker->name() == vmSymbols::invokeExact_name(), "must be this kind of method");
105 assert(!invoker->is_static(), "must have MH argument");
106 int slot_count = invoker->size_of_parameters();
107 assert(slot_count >= 1, "must include 'this'");
108 intptr_t* base = f->saved_args_base();
109 intptr_t* retval = NULL;
110 if (f->has_return_value_slot())
111 retval = f->return_value_slot_addr();
112 int slot_num = slot_count - 1;
113 intptr_t* loc = &base[slot_num];
114 //blk->do_oop((oop*) loc); // original target, which is irrelevant
115 int arg_num = 0;
116 for (SignatureStream ss(invoker->signature()); !ss.is_done(); ss.next()) {
117 if (ss.at_return_type()) continue;
118 BasicType ptype = ss.type();
119 if (ptype == T_ARRAY) ptype = T_OBJECT; // fold all refs to T_OBJECT
120 assert(ptype >= T_BOOLEAN && ptype <= T_OBJECT, "not array or void");
121 slot_num -= type2size[ptype];
122 loc = &base[slot_num];
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 // Ricochet Frames
131 const Register MethodHandles::RicochetFrame::L1_continuation = L1;
132 const Register MethodHandles::RicochetFrame::L2_saved_target = L2;
133 const Register MethodHandles::RicochetFrame::L3_saved_args_layout = L3;
134 const Register MethodHandles::RicochetFrame::L4_saved_args_base = L4; // cf. Gargs = G4
135 const Register MethodHandles::RicochetFrame::L5_conversion = L5;
136 #ifdef ASSERT
137 const Register MethodHandles::RicochetFrame::L0_magic_number_1 = L0;
138 #endif //ASSERT
140 oop MethodHandles::RicochetFrame::compute_saved_args_layout(bool read_cache, bool write_cache) {
141 if (read_cache) {
142 oop cookie = saved_args_layout();
143 if (cookie != NULL) return cookie;
144 }
145 oop target = saved_target();
146 oop mtype = java_lang_invoke_MethodHandle::type(target);
147 oop mtform = java_lang_invoke_MethodType::form(mtype);
148 oop cookie = java_lang_invoke_MethodTypeForm::vmlayout(mtform);
149 if (write_cache) {
150 (*saved_args_layout_addr()) = cookie;
151 }
152 return cookie;
153 }
155 void MethodHandles::RicochetFrame::generate_ricochet_blob(MacroAssembler* _masm,
156 // output params:
157 int* bounce_offset,
158 int* exception_offset,
159 int* frame_size_in_words) {
160 (*frame_size_in_words) = RicochetFrame::frame_size_in_bytes() / wordSize;
162 address start = __ pc();
164 #ifdef ASSERT
165 __ illtrap(0); __ illtrap(0); __ illtrap(0);
166 // here's a hint of something special:
167 __ set(MAGIC_NUMBER_1, G0);
168 __ set(MAGIC_NUMBER_2, G0);
169 #endif //ASSERT
170 __ illtrap(0); // not reached
172 // Return values are in registers.
173 // L1_continuation contains a cleanup continuation we must return
174 // to.
176 (*bounce_offset) = __ pc() - start;
177 BLOCK_COMMENT("ricochet_blob.bounce");
179 if (VerifyMethodHandles) RicochetFrame::verify_clean(_masm);
180 trace_method_handle(_masm, "ricochet_blob.bounce");
182 __ JMP(L1_continuation, 0);
183 __ delayed()->nop();
184 __ illtrap(0);
186 DEBUG_ONLY(__ set(MAGIC_NUMBER_2, G0));
188 (*exception_offset) = __ pc() - start;
189 BLOCK_COMMENT("ricochet_blob.exception");
191 // compare this to Interpreter::rethrow_exception_entry, which is parallel code
192 // for example, see TemplateInterpreterGenerator::generate_throw_exception
193 // Live registers in:
194 // Oexception (O0): exception
195 // Oissuing_pc (O1): return address/pc that threw exception (ignored, always equal to bounce addr)
196 __ verify_oop(Oexception);
198 // Take down the frame.
200 // Cf. InterpreterMacroAssembler::remove_activation.
201 leave_ricochet_frame(_masm, /*recv_reg=*/ noreg, I5_savedSP, I7);
203 // We are done with this activation frame; find out where to go next.
204 // The continuation point will be an exception handler, which expects
205 // the following registers set up:
206 //
207 // Oexception: exception
208 // Oissuing_pc: the local call that threw exception
209 // Other On: garbage
210 // In/Ln: the contents of the caller's register window
211 //
212 // We do the required restore at the last possible moment, because we
213 // need to preserve some state across a runtime call.
214 // (Remember that the caller activation is unknown--it might not be
215 // interpreted, so things like Lscratch are useless in the caller.)
216 __ mov(Oexception, Oexception ->after_save()); // get exception in I0 so it will be on O0 after restore
217 __ add(I7, frame::pc_return_offset, Oissuing_pc->after_save()); // likewise set I1 to a value local to the caller
218 __ call_VM_leaf(L7_thread_cache,
219 CAST_FROM_FN_PTR(address, SharedRuntime::exception_handler_for_return_address),
220 G2_thread, Oissuing_pc->after_save());
222 // The caller's SP was adjusted upon method entry to accomodate
223 // the callee's non-argument locals. Undo that adjustment.
224 __ JMP(O0, 0); // return exception handler in caller
225 __ delayed()->restore(I5_savedSP, G0, SP);
227 // (same old exception object is already in Oexception; see above)
228 // Note that an "issuing PC" is actually the next PC after the call
229 }
231 void MethodHandles::RicochetFrame::enter_ricochet_frame(MacroAssembler* _masm,
232 Register recv_reg,
233 Register argv_reg,
234 address return_handler) {
235 // does not include the __ save()
236 assert(argv_reg == Gargs, "");
237 Address G3_mh_vmtarget( recv_reg, java_lang_invoke_MethodHandle::vmtarget_offset_in_bytes());
238 Address G3_amh_conversion(recv_reg, java_lang_invoke_AdapterMethodHandle::conversion_offset_in_bytes());
240 // Create the RicochetFrame.
241 // Unlike on x86 we can store all required information in local
242 // registers.
243 BLOCK_COMMENT("push RicochetFrame {");
244 __ set(ExternalAddress(return_handler), L1_continuation);
245 __ load_heap_oop(G3_mh_vmtarget, L2_saved_target);
246 __ mov(G0, L3_saved_args_layout);
247 __ mov(Gargs, L4_saved_args_base);
248 __ lduw(G3_amh_conversion, L5_conversion); // 32-bit field
249 // I5, I6, I7 are already set up
250 DEBUG_ONLY(__ set((int32_t) MAGIC_NUMBER_1, L0_magic_number_1));
251 BLOCK_COMMENT("} RicochetFrame");
252 }
254 void MethodHandles::RicochetFrame::leave_ricochet_frame(MacroAssembler* _masm,
255 Register recv_reg,
256 Register new_sp_reg,
257 Register sender_pc_reg) {
258 assert(new_sp_reg == I5_savedSP, "exact_sender_sp already in place");
259 assert(sender_pc_reg == I7, "in a fixed place");
260 // does not include the __ ret() & __ restore()
261 assert_different_registers(recv_reg, new_sp_reg, sender_pc_reg);
262 // Take down the frame.
263 // Cf. InterpreterMacroAssembler::remove_activation.
264 BLOCK_COMMENT("end_ricochet_frame {");
265 if (recv_reg->is_valid())
266 __ mov(L2_saved_target, recv_reg);
267 BLOCK_COMMENT("} end_ricochet_frame");
268 }
270 // Emit code to verify that FP is pointing at a valid ricochet frame.
271 #ifdef ASSERT
272 enum {
273 ARG_LIMIT = 255, SLOP = 45,
274 // use this parameter for checking for garbage stack movements:
275 UNREASONABLE_STACK_MOVE = (ARG_LIMIT + SLOP)
276 // the slop defends against false alarms due to fencepost errors
277 };
279 void MethodHandles::RicochetFrame::verify_clean(MacroAssembler* _masm) {
280 // The stack should look like this:
281 // ... keep1 | dest=42 | keep2 | magic | handler | magic | recursive args | [RF]
282 // Check various invariants.
284 Register O7_temp = O7, O5_temp = O5;
286 Label L_ok_1, L_ok_2, L_ok_3, L_ok_4;
287 BLOCK_COMMENT("verify_clean {");
288 // Magic numbers must check out:
289 __ set((int32_t) MAGIC_NUMBER_1, O7_temp);
290 __ cmp_and_br_short(O7_temp, L0_magic_number_1, Assembler::equal, Assembler::pt, L_ok_1);
291 __ stop("damaged ricochet frame: MAGIC_NUMBER_1 not found");
293 __ BIND(L_ok_1);
295 // Arguments pointer must look reasonable:
296 #ifdef _LP64
297 Register FP_temp = O5_temp;
298 __ add(FP, STACK_BIAS, FP_temp);
299 #else
300 Register FP_temp = FP;
301 #endif
302 __ cmp_and_brx_short(L4_saved_args_base, FP_temp, Assembler::greaterEqualUnsigned, Assembler::pt, L_ok_2);
303 __ stop("damaged ricochet frame: L4 < FP");
305 __ BIND(L_ok_2);
306 // Disable until we decide on it's fate
307 // __ sub(L4_saved_args_base, UNREASONABLE_STACK_MOVE * Interpreter::stackElementSize, O7_temp);
308 // __ cmp(O7_temp, FP_temp);
309 // __ br(Assembler::lessEqualUnsigned, false, Assembler::pt, L_ok_3);
310 // __ delayed()->nop();
311 // __ stop("damaged ricochet frame: (L4 - UNREASONABLE_STACK_MOVE) > FP");
313 __ BIND(L_ok_3);
314 extract_conversion_dest_type(_masm, L5_conversion, O7_temp);
315 __ cmp_and_br_short(O7_temp, T_VOID, Assembler::equal, Assembler::pt, L_ok_4);
316 extract_conversion_vminfo(_masm, L5_conversion, O5_temp);
317 __ ld_ptr(L4_saved_args_base, __ argument_offset(O5_temp, O5_temp), O7_temp);
318 assert(Assembler::is_simm13(RETURN_VALUE_PLACEHOLDER), "must be simm13");
319 __ cmp_and_brx_short(O7_temp, (int32_t) RETURN_VALUE_PLACEHOLDER, Assembler::equal, Assembler::pt, L_ok_4);
320 __ stop("damaged ricochet frame: RETURN_VALUE_PLACEHOLDER not found");
321 __ BIND(L_ok_4);
322 BLOCK_COMMENT("} verify_clean");
323 }
324 #endif //ASSERT
326 void MethodHandles::load_klass_from_Class(MacroAssembler* _masm, Register klass_reg, Register temp_reg, Register temp2_reg) {
327 if (VerifyMethodHandles)
328 verify_klass(_masm, klass_reg, SystemDictionaryHandles::Class_klass(), temp_reg, temp2_reg,
329 "AMH argument is a Class");
330 __ load_heap_oop(Address(klass_reg, java_lang_Class::klass_offset_in_bytes()), klass_reg);
331 }
333 void MethodHandles::load_conversion_vminfo(MacroAssembler* _masm, Address conversion_field_addr, Register reg) {
334 assert(CONV_VMINFO_SHIFT == 0, "preshifted");
335 assert(CONV_VMINFO_MASK == right_n_bits(BitsPerByte), "else change type of following load");
336 __ ldub(conversion_field_addr.plus_disp(BytesPerInt - 1), reg);
337 }
339 void MethodHandles::extract_conversion_vminfo(MacroAssembler* _masm, Register conversion_field_reg, Register reg) {
340 assert(CONV_VMINFO_SHIFT == 0, "preshifted");
341 __ and3(conversion_field_reg, CONV_VMINFO_MASK, reg);
342 }
344 void MethodHandles::extract_conversion_dest_type(MacroAssembler* _masm, Register conversion_field_reg, Register reg) {
345 __ srl(conversion_field_reg, CONV_DEST_TYPE_SHIFT, reg);
346 __ and3(reg, 0x0F, reg);
347 }
349 void MethodHandles::load_stack_move(MacroAssembler* _masm,
350 Address G3_amh_conversion,
351 Register stack_move_reg) {
352 BLOCK_COMMENT("load_stack_move {");
353 __ ldsw(G3_amh_conversion, stack_move_reg);
354 __ sra(stack_move_reg, CONV_STACK_MOVE_SHIFT, stack_move_reg);
355 #ifdef ASSERT
356 if (VerifyMethodHandles) {
357 Label L_ok, L_bad;
358 int32_t stack_move_limit = 0x0800; // extra-large
359 __ cmp_and_br_short(stack_move_reg, stack_move_limit, Assembler::greaterEqual, Assembler::pn, L_bad);
360 __ cmp(stack_move_reg, -stack_move_limit);
361 __ br(Assembler::greater, false, Assembler::pt, L_ok);
362 __ delayed()->nop();
363 __ BIND(L_bad);
364 __ stop("load_stack_move of garbage value");
365 __ BIND(L_ok);
366 }
367 #endif
368 BLOCK_COMMENT("} load_stack_move");
369 }
371 #ifdef ASSERT
372 void MethodHandles::RicochetFrame::verify() const {
373 assert(magic_number_1() == MAGIC_NUMBER_1, "");
374 if (!Universe::heap()->is_gc_active()) {
375 if (saved_args_layout() != NULL) {
376 assert(saved_args_layout()->is_method(), "must be valid oop");
377 }
378 if (saved_target() != NULL) {
379 assert(java_lang_invoke_MethodHandle::is_instance(saved_target()), "checking frame value");
380 }
381 }
382 int conv_op = adapter_conversion_op(conversion());
383 assert(conv_op == java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS ||
384 conv_op == java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS ||
385 conv_op == java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF,
386 "must be a sane conversion");
387 if (has_return_value_slot()) {
388 assert(*return_value_slot_addr() == RETURN_VALUE_PLACEHOLDER, "");
389 }
390 }
392 void MethodHandles::verify_argslot(MacroAssembler* _masm, Register argslot_reg, Register temp_reg, const char* error_message) {
393 // Verify that argslot lies within (Gargs, FP].
394 Label L_ok, L_bad;
395 BLOCK_COMMENT("verify_argslot {");
396 __ cmp_and_brx_short(Gargs, argslot_reg, Assembler::greaterUnsigned, Assembler::pn, L_bad);
397 __ add(FP, STACK_BIAS, temp_reg); // STACK_BIAS is zero on !_LP64
398 __ cmp_and_brx_short(argslot_reg, temp_reg, Assembler::lessEqualUnsigned, Assembler::pt, L_ok);
399 __ BIND(L_bad);
400 __ stop(error_message);
401 __ BIND(L_ok);
402 BLOCK_COMMENT("} verify_argslot");
403 }
405 void MethodHandles::verify_argslots(MacroAssembler* _masm,
406 RegisterOrConstant arg_slots,
407 Register arg_slot_base_reg,
408 Register temp_reg,
409 Register temp2_reg,
410 bool negate_argslots,
411 const char* error_message) {
412 // Verify that [argslot..argslot+size) lies within (Gargs, FP).
413 Label L_ok, L_bad;
414 BLOCK_COMMENT("verify_argslots {");
415 if (negate_argslots) {
416 if (arg_slots.is_constant()) {
417 arg_slots = -1 * arg_slots.as_constant();
418 } else {
419 __ neg(arg_slots.as_register(), temp_reg);
420 arg_slots = temp_reg;
421 }
422 }
423 __ add(arg_slot_base_reg, __ argument_offset(arg_slots, temp_reg), temp_reg);
424 __ add(FP, STACK_BIAS, temp2_reg); // STACK_BIAS is zero on !_LP64
425 __ cmp_and_brx_short(temp_reg, temp2_reg, Assembler::greaterUnsigned, Assembler::pn, L_bad);
426 // Gargs points to the first word so adjust by BytesPerWord
427 __ add(arg_slot_base_reg, BytesPerWord, temp_reg);
428 __ cmp_and_brx_short(Gargs, temp_reg, Assembler::lessEqualUnsigned, Assembler::pt, L_ok);
429 __ BIND(L_bad);
430 __ stop(error_message);
431 __ BIND(L_ok);
432 BLOCK_COMMENT("} verify_argslots");
433 }
435 // Make sure that arg_slots has the same sign as the given direction.
436 // If (and only if) arg_slots is a assembly-time constant, also allow it to be zero.
437 void MethodHandles::verify_stack_move(MacroAssembler* _masm,
438 RegisterOrConstant arg_slots, int direction) {
439 enum { UNREASONABLE_STACK_MOVE = 256 * 4 }; // limit of 255 arguments
440 bool allow_zero = arg_slots.is_constant();
441 if (direction == 0) { direction = +1; allow_zero = true; }
442 assert(stack_move_unit() == -1, "else add extra checks here");
443 if (arg_slots.is_register()) {
444 Label L_ok, L_bad;
445 BLOCK_COMMENT("verify_stack_move {");
446 // __ btst(-stack_move_unit() - 1, arg_slots.as_register()); // no need
447 // __ br(Assembler::notZero, false, Assembler::pn, L_bad);
448 // __ delayed()->nop();
449 __ cmp(arg_slots.as_register(), (int32_t) NULL_WORD);
450 if (direction > 0) {
451 __ br(allow_zero ? Assembler::less : Assembler::lessEqual, false, Assembler::pn, L_bad);
452 __ delayed()->nop();
453 __ cmp(arg_slots.as_register(), (int32_t) UNREASONABLE_STACK_MOVE);
454 __ br(Assembler::less, false, Assembler::pn, L_ok);
455 __ delayed()->nop();
456 } else {
457 __ br(allow_zero ? Assembler::greater : Assembler::greaterEqual, false, Assembler::pn, L_bad);
458 __ delayed()->nop();
459 __ cmp(arg_slots.as_register(), (int32_t) -UNREASONABLE_STACK_MOVE);
460 __ br(Assembler::greater, false, Assembler::pn, L_ok);
461 __ delayed()->nop();
462 }
463 __ BIND(L_bad);
464 if (direction > 0)
465 __ stop("assert arg_slots > 0");
466 else
467 __ stop("assert arg_slots < 0");
468 __ BIND(L_ok);
469 BLOCK_COMMENT("} verify_stack_move");
470 } else {
471 intptr_t size = arg_slots.as_constant();
472 if (direction < 0) size = -size;
473 assert(size >= 0, "correct direction of constant move");
474 assert(size < UNREASONABLE_STACK_MOVE, "reasonable size of constant move");
475 }
476 }
478 void MethodHandles::verify_klass(MacroAssembler* _masm,
479 Register obj_reg, KlassHandle klass,
480 Register temp_reg, Register temp2_reg,
481 const char* error_message) {
482 oop* klass_addr = klass.raw_value();
483 assert(klass_addr >= SystemDictionaryHandles::Object_klass().raw_value() &&
484 klass_addr <= SystemDictionaryHandles::Long_klass().raw_value(),
485 "must be one of the SystemDictionaryHandles");
486 Label L_ok, L_bad;
487 BLOCK_COMMENT("verify_klass {");
488 __ verify_oop(obj_reg);
489 __ br_null_short(obj_reg, Assembler::pn, L_bad);
490 __ load_klass(obj_reg, temp_reg);
491 __ set(ExternalAddress(klass_addr), temp2_reg);
492 __ ld_ptr(Address(temp2_reg, 0), temp2_reg);
493 __ cmp_and_brx_short(temp_reg, temp2_reg, Assembler::equal, Assembler::pt, L_ok);
494 intptr_t super_check_offset = klass->super_check_offset();
495 __ ld_ptr(Address(temp_reg, super_check_offset), temp_reg);
496 __ set(ExternalAddress(klass_addr), temp2_reg);
497 __ ld_ptr(Address(temp2_reg, 0), temp2_reg);
498 __ cmp_and_brx_short(temp_reg, temp2_reg, Assembler::equal, Assembler::pt, L_ok);
499 __ BIND(L_bad);
500 __ stop(error_message);
501 __ BIND(L_ok);
502 BLOCK_COMMENT("} verify_klass");
503 }
504 #endif // ASSERT
507 void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register target, Register temp) {
508 assert(method == G5_method, "interpreter calling convention");
509 __ verify_oop(method);
510 __ ld_ptr(G5_method, in_bytes(methodOopDesc::from_interpreted_offset()), target);
511 if (JvmtiExport::can_post_interpreter_events()) {
512 // JVMTI events, such as single-stepping, are implemented partly by avoiding running
513 // compiled code in threads for which the event is enabled. Check here for
514 // interp_only_mode if these events CAN be enabled.
515 __ verify_thread();
516 Label skip_compiled_code;
518 const Address interp_only(G2_thread, JavaThread::interp_only_mode_offset());
519 __ ld(interp_only, temp);
520 __ tst(temp);
521 __ br(Assembler::notZero, true, Assembler::pn, skip_compiled_code);
522 __ delayed()->ld_ptr(G5_method, in_bytes(methodOopDesc::interpreter_entry_offset()), target);
523 __ bind(skip_compiled_code);
524 }
525 __ jmp(target, 0);
526 __ delayed()->nop();
527 }
530 // Code generation
531 address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm) {
532 // I5_savedSP/O5_savedSP: sender SP (must preserve)
533 // G4 (Gargs): incoming argument list (must preserve)
534 // G5_method: invoke methodOop
535 // G3_method_handle: receiver method handle (must load from sp[MethodTypeForm.vmslots])
536 // O0, O1, O2, O3, O4: garbage temps, blown away
537 Register O0_mtype = O0;
538 Register O1_scratch = O1;
539 Register O2_scratch = O2;
540 Register O3_scratch = O3;
541 Register O4_argslot = O4;
542 Register O4_argbase = O4;
544 // emit WrongMethodType path first, to enable back-branch from main path
545 Label wrong_method_type;
546 __ bind(wrong_method_type);
547 Label invoke_generic_slow_path;
548 assert(methodOopDesc::intrinsic_id_size_in_bytes() == sizeof(u1), "");;
549 __ ldub(Address(G5_method, methodOopDesc::intrinsic_id_offset_in_bytes()), O1_scratch);
550 __ cmp(O1_scratch, (int) vmIntrinsics::_invokeExact);
551 __ brx(Assembler::notEqual, false, Assembler::pt, invoke_generic_slow_path);
552 __ delayed()->nop();
553 __ mov(O0_mtype, G5_method_type); // required by throw_WrongMethodType
554 __ mov(G3_method_handle, G3_method_handle); // already in this register
555 // O0 will be filled in with JavaThread in stub
556 __ jump_to(AddressLiteral(StubRoutines::throw_WrongMethodTypeException_entry()), O3_scratch);
557 __ delayed()->nop();
559 // here's where control starts out:
560 __ align(CodeEntryAlignment);
561 address entry_point = __ pc();
563 // fetch the MethodType from the method handle
564 // FIXME: Interpreter should transmit pre-popped stack pointer, to locate base of arg list.
565 // This would simplify several touchy bits of code.
566 // See 6984712: JSR 292 method handle calls need a clean argument base pointer
567 {
568 Register tem = G5_method;
569 for (jint* pchase = methodOopDesc::method_type_offsets_chain(); (*pchase) != -1; pchase++) {
570 __ ld_ptr(Address(tem, *pchase), O0_mtype);
571 tem = O0_mtype; // in case there is another indirection
572 }
573 }
575 // given the MethodType, find out where the MH argument is buried
576 __ load_heap_oop(Address(O0_mtype, __ delayed_value(java_lang_invoke_MethodType::form_offset_in_bytes, O1_scratch)), O4_argslot);
577 __ ldsw( Address(O4_argslot, __ delayed_value(java_lang_invoke_MethodTypeForm::vmslots_offset_in_bytes, O1_scratch)), O4_argslot);
578 __ add(__ argument_address(O4_argslot, O4_argslot, 1), O4_argbase);
579 // Note: argument_address uses its input as a scratch register!
580 Address mh_receiver_slot_addr(O4_argbase, -Interpreter::stackElementSize);
581 __ ld_ptr(mh_receiver_slot_addr, G3_method_handle);
583 trace_method_handle(_masm, "invokeExact");
585 __ check_method_handle_type(O0_mtype, G3_method_handle, O1_scratch, wrong_method_type);
587 // Nobody uses the MH receiver slot after this. Make sure.
588 DEBUG_ONLY(__ set((int32_t) 0x999999, O1_scratch); __ st_ptr(O1_scratch, mh_receiver_slot_addr));
590 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
592 // for invokeGeneric (only), apply argument and result conversions on the fly
593 __ bind(invoke_generic_slow_path);
594 #ifdef ASSERT
595 if (VerifyMethodHandles) {
596 Label L;
597 __ ldub(Address(G5_method, methodOopDesc::intrinsic_id_offset_in_bytes()), O1_scratch);
598 __ cmp(O1_scratch, (int) vmIntrinsics::_invokeGeneric);
599 __ brx(Assembler::equal, false, Assembler::pt, L);
600 __ delayed()->nop();
601 __ stop("bad methodOop::intrinsic_id");
602 __ bind(L);
603 }
604 #endif //ASSERT
606 // make room on the stack for another pointer:
607 insert_arg_slots(_masm, 2 * stack_move_unit(), O4_argbase, O1_scratch, O2_scratch, O3_scratch);
608 // load up an adapter from the calling type (Java weaves this)
609 Register O2_form = O2_scratch;
610 Register O3_adapter = O3_scratch;
611 __ load_heap_oop(Address(O0_mtype, __ delayed_value(java_lang_invoke_MethodType::form_offset_in_bytes, O1_scratch)), O2_form);
612 __ load_heap_oop(Address(O2_form, __ delayed_value(java_lang_invoke_MethodTypeForm::genericInvoker_offset_in_bytes, O1_scratch)), O3_adapter);
613 __ verify_oop(O3_adapter);
614 __ st_ptr(O3_adapter, Address(O4_argbase, 1 * Interpreter::stackElementSize));
615 // As a trusted first argument, pass the type being called, so the adapter knows
616 // the actual types of the arguments and return values.
617 // (Generic invokers are shared among form-families of method-type.)
618 __ st_ptr(O0_mtype, Address(O4_argbase, 0 * Interpreter::stackElementSize));
619 // FIXME: assert that O3_adapter is of the right method-type.
620 __ mov(O3_adapter, G3_method_handle);
621 trace_method_handle(_masm, "invokeGeneric");
622 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
624 return entry_point;
625 }
627 // Workaround for C++ overloading nastiness on '0' for RegisterOrConstant.
628 static RegisterOrConstant constant(int value) {
629 return RegisterOrConstant(value);
630 }
632 static void load_vmargslot(MacroAssembler* _masm, Address vmargslot_addr, Register result) {
633 __ ldsw(vmargslot_addr, result);
634 }
636 static RegisterOrConstant adjust_SP_and_Gargs_down_by_slots(MacroAssembler* _masm,
637 RegisterOrConstant arg_slots,
638 Register temp_reg, Register temp2_reg) {
639 // Keep the stack pointer 2*wordSize aligned.
640 const int TwoWordAlignmentMask = right_n_bits(LogBytesPerWord + 1);
641 if (arg_slots.is_constant()) {
642 const int offset = arg_slots.as_constant() << LogBytesPerWord;
643 const int masked_offset = round_to(offset, 2 * BytesPerWord);
644 const int masked_offset2 = (offset + 1*BytesPerWord) & ~TwoWordAlignmentMask;
645 assert(masked_offset == masked_offset2, "must agree");
646 __ sub(Gargs, offset, Gargs);
647 __ sub(SP, masked_offset, SP );
648 return offset;
649 } else {
650 #ifdef ASSERT
651 {
652 Label L_ok;
653 __ cmp_and_br_short(arg_slots.as_register(), 0, Assembler::greaterEqual, Assembler::pt, L_ok);
654 __ stop("negative arg_slots");
655 __ bind(L_ok);
656 }
657 #endif
658 __ sll_ptr(arg_slots.as_register(), LogBytesPerWord, temp_reg);
659 __ add( temp_reg, 1*BytesPerWord, temp2_reg);
660 __ andn(temp2_reg, TwoWordAlignmentMask, temp2_reg);
661 __ sub(Gargs, temp_reg, Gargs);
662 __ sub(SP, temp2_reg, SP );
663 return temp_reg;
664 }
665 }
667 static RegisterOrConstant adjust_SP_and_Gargs_up_by_slots(MacroAssembler* _masm,
668 RegisterOrConstant arg_slots,
669 Register temp_reg, Register temp2_reg) {
670 // Keep the stack pointer 2*wordSize aligned.
671 const int TwoWordAlignmentMask = right_n_bits(LogBytesPerWord + 1);
672 if (arg_slots.is_constant()) {
673 const int offset = arg_slots.as_constant() << LogBytesPerWord;
674 const int masked_offset = offset & ~TwoWordAlignmentMask;
675 __ add(Gargs, offset, Gargs);
676 __ add(SP, masked_offset, SP );
677 return offset;
678 } else {
679 __ sll_ptr(arg_slots.as_register(), LogBytesPerWord, temp_reg);
680 __ andn(temp_reg, TwoWordAlignmentMask, temp2_reg);
681 __ add(Gargs, temp_reg, Gargs);
682 __ add(SP, temp2_reg, SP );
683 return temp_reg;
684 }
685 }
687 // Helper to insert argument slots into the stack.
688 // arg_slots must be a multiple of stack_move_unit() and < 0
689 // argslot_reg is decremented to point to the new (shifted) location of the argslot
690 // But, temp_reg ends up holding the original value of argslot_reg.
691 void MethodHandles::insert_arg_slots(MacroAssembler* _masm,
692 RegisterOrConstant arg_slots,
693 Register argslot_reg,
694 Register temp_reg, Register temp2_reg, Register temp3_reg) {
695 // allow constant zero
696 if (arg_slots.is_constant() && arg_slots.as_constant() == 0)
697 return;
699 assert_different_registers(argslot_reg, temp_reg, temp2_reg, temp3_reg,
700 (!arg_slots.is_register() ? Gargs : arg_slots.as_register()));
702 BLOCK_COMMENT("insert_arg_slots {");
703 if (VerifyMethodHandles)
704 verify_argslot(_masm, argslot_reg, temp_reg, "insertion point must fall within current frame");
705 if (VerifyMethodHandles)
706 verify_stack_move(_masm, arg_slots, -1);
708 // Make space on the stack for the inserted argument(s).
709 // Then pull down everything shallower than argslot_reg.
710 // The stacked return address gets pulled down with everything else.
711 // That is, copy [sp, argslot) downward by -size words. In pseudo-code:
712 // sp -= size;
713 // for (temp = sp + size; temp < argslot; temp++)
714 // temp[-size] = temp[0]
715 // argslot -= size;
717 // offset is temp3_reg in case of arg_slots being a register.
718 RegisterOrConstant offset = adjust_SP_and_Gargs_up_by_slots(_masm, arg_slots, temp3_reg, temp_reg);
719 __ sub(Gargs, offset, temp_reg); // source pointer for copy
721 {
722 Label loop;
723 __ BIND(loop);
724 // pull one word down each time through the loop
725 __ ld_ptr( Address(temp_reg, 0 ), temp2_reg);
726 __ st_ptr(temp2_reg, Address(temp_reg, offset) );
727 __ add(temp_reg, wordSize, temp_reg);
728 __ cmp_and_brx_short(temp_reg, argslot_reg, Assembler::lessUnsigned, Assembler::pt, loop);
729 }
731 // Now move the argslot down, to point to the opened-up space.
732 __ add(argslot_reg, offset, argslot_reg);
733 BLOCK_COMMENT("} insert_arg_slots");
734 }
737 // Helper to remove argument slots from the stack.
738 // arg_slots must be a multiple of stack_move_unit() and > 0
739 void MethodHandles::remove_arg_slots(MacroAssembler* _masm,
740 RegisterOrConstant arg_slots,
741 Register argslot_reg,
742 Register temp_reg, Register temp2_reg, Register temp3_reg) {
743 // allow constant zero
744 if (arg_slots.is_constant() && arg_slots.as_constant() == 0)
745 return;
746 assert_different_registers(argslot_reg, temp_reg, temp2_reg, temp3_reg,
747 (!arg_slots.is_register() ? Gargs : arg_slots.as_register()));
749 BLOCK_COMMENT("remove_arg_slots {");
750 if (VerifyMethodHandles)
751 verify_argslots(_masm, arg_slots, argslot_reg, temp_reg, temp2_reg, false,
752 "deleted argument(s) must fall within current frame");
753 if (VerifyMethodHandles)
754 verify_stack_move(_masm, arg_slots, +1);
756 // Pull up everything shallower than argslot.
757 // Then remove the excess space on the stack.
758 // The stacked return address gets pulled up with everything else.
759 // That is, copy [sp, argslot) upward by size words. In pseudo-code:
760 // for (temp = argslot-1; temp >= sp; --temp)
761 // temp[size] = temp[0]
762 // argslot += size;
763 // sp += size;
765 RegisterOrConstant offset = __ regcon_sll_ptr(arg_slots, LogBytesPerWord, temp3_reg);
766 __ sub(argslot_reg, wordSize, temp_reg); // source pointer for copy
768 {
769 Label L_loop;
770 __ BIND(L_loop);
771 // pull one word up each time through the loop
772 __ ld_ptr( Address(temp_reg, 0 ), temp2_reg);
773 __ st_ptr(temp2_reg, Address(temp_reg, offset) );
774 __ sub(temp_reg, wordSize, temp_reg);
775 __ cmp_and_brx_short(temp_reg, Gargs, Assembler::greaterEqualUnsigned, Assembler::pt, L_loop);
776 }
778 // And adjust the argslot address to point at the deletion point.
779 __ add(argslot_reg, offset, argslot_reg);
781 // We don't need the offset at this point anymore, just adjust SP and Gargs.
782 (void) adjust_SP_and_Gargs_up_by_slots(_masm, arg_slots, temp3_reg, temp_reg);
784 BLOCK_COMMENT("} remove_arg_slots");
785 }
787 // Helper to copy argument slots to the top of the stack.
788 // The sequence starts with argslot_reg and is counted by slot_count
789 // slot_count must be a multiple of stack_move_unit() and >= 0
790 // This function blows the temps but does not change argslot_reg.
791 void MethodHandles::push_arg_slots(MacroAssembler* _masm,
792 Register argslot_reg,
793 RegisterOrConstant slot_count,
794 Register temp_reg, Register temp2_reg) {
795 // allow constant zero
796 if (slot_count.is_constant() && slot_count.as_constant() == 0)
797 return;
798 assert_different_registers(argslot_reg, temp_reg, temp2_reg,
799 (!slot_count.is_register() ? Gargs : slot_count.as_register()),
800 SP);
801 assert(Interpreter::stackElementSize == wordSize, "else change this code");
803 BLOCK_COMMENT("push_arg_slots {");
804 if (VerifyMethodHandles)
805 verify_stack_move(_masm, slot_count, 0);
807 RegisterOrConstant offset = adjust_SP_and_Gargs_down_by_slots(_masm, slot_count, temp2_reg, temp_reg);
809 if (slot_count.is_constant()) {
810 for (int i = slot_count.as_constant() - 1; i >= 0; i--) {
811 __ ld_ptr( Address(argslot_reg, i * wordSize), temp_reg);
812 __ st_ptr(temp_reg, Address(Gargs, i * wordSize));
813 }
814 } else {
815 Label L_plural, L_loop, L_break;
816 // Emit code to dynamically check for the common cases, zero and one slot.
817 __ cmp(slot_count.as_register(), (int32_t) 1);
818 __ br(Assembler::greater, false, Assembler::pn, L_plural);
819 __ delayed()->nop();
820 __ br(Assembler::less, false, Assembler::pn, L_break);
821 __ delayed()->nop();
822 __ ld_ptr( Address(argslot_reg, 0), temp_reg);
823 __ st_ptr(temp_reg, Address(Gargs, 0));
824 __ ba_short(L_break);
825 __ BIND(L_plural);
827 // Loop for 2 or more:
828 // top = &argslot[slot_count]
829 // while (top > argslot) *(--Gargs) = *(--top)
830 Register top_reg = temp_reg;
831 __ add(argslot_reg, offset, top_reg);
832 __ add(Gargs, offset, Gargs ); // move back up again so we can go down
833 __ BIND(L_loop);
834 __ sub(top_reg, wordSize, top_reg);
835 __ sub(Gargs, wordSize, Gargs );
836 __ ld_ptr( Address(top_reg, 0), temp2_reg);
837 __ st_ptr(temp2_reg, Address(Gargs, 0));
838 __ cmp_and_brx_short(top_reg, argslot_reg, Assembler::greaterUnsigned, Assembler::pt, L_loop);
839 __ BIND(L_break);
840 }
841 BLOCK_COMMENT("} push_arg_slots");
842 }
844 // in-place movement; no change to Gargs
845 // blows temp_reg, temp2_reg
846 void MethodHandles::move_arg_slots_up(MacroAssembler* _masm,
847 Register bottom_reg, // invariant
848 Address top_addr, // can use temp_reg
849 RegisterOrConstant positive_distance_in_slots, // destroyed if register
850 Register temp_reg, Register temp2_reg) {
851 assert_different_registers(bottom_reg,
852 temp_reg, temp2_reg,
853 positive_distance_in_slots.register_or_noreg());
854 BLOCK_COMMENT("move_arg_slots_up {");
855 Label L_loop, L_break;
856 Register top_reg = temp_reg;
857 if (!top_addr.is_same_address(Address(top_reg, 0))) {
858 __ add(top_addr, top_reg);
859 }
860 // Detect empty (or broken) loop:
861 #ifdef ASSERT
862 if (VerifyMethodHandles) {
863 // Verify that &bottom < &top (non-empty interval)
864 Label L_ok, L_bad;
865 if (positive_distance_in_slots.is_register()) {
866 __ cmp(positive_distance_in_slots.as_register(), (int32_t) 0);
867 __ br(Assembler::lessEqual, false, Assembler::pn, L_bad);
868 __ delayed()->nop();
869 }
870 __ cmp_and_brx_short(bottom_reg, top_reg, Assembler::lessUnsigned, Assembler::pt, L_ok);
871 __ BIND(L_bad);
872 __ stop("valid bounds (copy up)");
873 __ BIND(L_ok);
874 }
875 #endif
876 __ cmp_and_brx_short(bottom_reg, top_reg, Assembler::greaterEqualUnsigned, Assembler::pn, L_break);
877 // work top down to bottom, copying contiguous data upwards
878 // In pseudo-code:
879 // while (--top >= bottom) *(top + distance) = *(top + 0);
880 RegisterOrConstant offset = __ argument_offset(positive_distance_in_slots, positive_distance_in_slots.register_or_noreg());
881 __ BIND(L_loop);
882 __ sub(top_reg, wordSize, top_reg);
883 __ ld_ptr( Address(top_reg, 0 ), temp2_reg);
884 __ st_ptr(temp2_reg, Address(top_reg, offset) );
885 __ cmp_and_brx_short(top_reg, bottom_reg, Assembler::greaterUnsigned, Assembler::pt, L_loop);
886 assert(Interpreter::stackElementSize == wordSize, "else change loop");
887 __ BIND(L_break);
888 BLOCK_COMMENT("} move_arg_slots_up");
889 }
891 // in-place movement; no change to rsp
892 // blows temp_reg, temp2_reg
893 void MethodHandles::move_arg_slots_down(MacroAssembler* _masm,
894 Address bottom_addr, // can use temp_reg
895 Register top_reg, // invariant
896 RegisterOrConstant negative_distance_in_slots, // destroyed if register
897 Register temp_reg, Register temp2_reg) {
898 assert_different_registers(top_reg,
899 negative_distance_in_slots.register_or_noreg(),
900 temp_reg, temp2_reg);
901 BLOCK_COMMENT("move_arg_slots_down {");
902 Label L_loop, L_break;
903 Register bottom_reg = temp_reg;
904 if (!bottom_addr.is_same_address(Address(bottom_reg, 0))) {
905 __ add(bottom_addr, bottom_reg);
906 }
907 // Detect empty (or broken) loop:
908 #ifdef ASSERT
909 assert(!negative_distance_in_slots.is_constant() || negative_distance_in_slots.as_constant() < 0, "");
910 if (VerifyMethodHandles) {
911 // Verify that &bottom < &top (non-empty interval)
912 Label L_ok, L_bad;
913 if (negative_distance_in_slots.is_register()) {
914 __ cmp(negative_distance_in_slots.as_register(), (int32_t) 0);
915 __ br(Assembler::greaterEqual, false, Assembler::pn, L_bad);
916 __ delayed()->nop();
917 }
918 __ cmp_and_brx_short(bottom_reg, top_reg, Assembler::lessUnsigned, Assembler::pt, L_ok);
919 __ BIND(L_bad);
920 __ stop("valid bounds (copy down)");
921 __ BIND(L_ok);
922 }
923 #endif
924 __ cmp_and_brx_short(bottom_reg, top_reg, Assembler::greaterEqualUnsigned, Assembler::pn, L_break);
925 // work bottom up to top, copying contiguous data downwards
926 // In pseudo-code:
927 // while (bottom < top) *(bottom - distance) = *(bottom + 0), bottom++;
928 RegisterOrConstant offset = __ argument_offset(negative_distance_in_slots, negative_distance_in_slots.register_or_noreg());
929 __ BIND(L_loop);
930 __ ld_ptr( Address(bottom_reg, 0 ), temp2_reg);
931 __ st_ptr(temp2_reg, Address(bottom_reg, offset) );
932 __ add(bottom_reg, wordSize, bottom_reg);
933 __ cmp_and_brx_short(bottom_reg, top_reg, Assembler::lessUnsigned, Assembler::pt, L_loop);
934 assert(Interpreter::stackElementSize == wordSize, "else change loop");
935 __ BIND(L_break);
936 BLOCK_COMMENT("} move_arg_slots_down");
937 }
939 // Copy from a field or array element to a stacked argument slot.
940 // is_element (ignored) says whether caller is loading an array element instead of an instance field.
941 void MethodHandles::move_typed_arg(MacroAssembler* _masm,
942 BasicType type, bool is_element,
943 Address value_src, Address slot_dest,
944 Register temp_reg) {
945 assert(!slot_dest.uses(temp_reg), "must be different register");
946 BLOCK_COMMENT(!is_element ? "move_typed_arg {" : "move_typed_arg { (array element)");
947 if (type == T_OBJECT || type == T_ARRAY) {
948 __ load_heap_oop(value_src, temp_reg);
949 __ verify_oop(temp_reg);
950 __ st_ptr(temp_reg, slot_dest);
951 } else if (type != T_VOID) {
952 int arg_size = type2aelembytes(type);
953 bool arg_is_signed = is_signed_subword_type(type);
954 int slot_size = is_subword_type(type) ? type2aelembytes(T_INT) : arg_size; // store int sub-words as int
955 __ load_sized_value( value_src, temp_reg, arg_size, arg_is_signed);
956 __ store_sized_value(temp_reg, slot_dest, slot_size );
957 }
958 BLOCK_COMMENT("} move_typed_arg");
959 }
961 // Cf. TemplateInterpreterGenerator::generate_return_entry_for and
962 // InterpreterMacroAssembler::save_return_value
963 void MethodHandles::move_return_value(MacroAssembler* _masm, BasicType type,
964 Address return_slot) {
965 BLOCK_COMMENT("move_return_value {");
966 // Look at the type and pull the value out of the corresponding register.
967 if (type == T_VOID) {
968 // nothing to do
969 } else if (type == T_OBJECT) {
970 __ verify_oop(O0);
971 __ st_ptr(O0, return_slot);
972 } else if (type == T_INT || is_subword_type(type)) {
973 int type_size = type2aelembytes(T_INT);
974 __ store_sized_value(O0, return_slot, type_size);
975 } else if (type == T_LONG) {
976 // store the value by parts
977 // Note: We assume longs are continguous (if misaligned) on the interpreter stack.
978 #if !defined(_LP64) && defined(COMPILER2)
979 __ stx(G1, return_slot);
980 #else
981 #ifdef _LP64
982 __ stx(O0, return_slot);
983 #else
984 if (return_slot.has_disp()) {
985 // The displacement is a constant
986 __ st(O0, return_slot);
987 __ st(O1, return_slot.plus_disp(Interpreter::stackElementSize));
988 } else {
989 __ std(O0, return_slot);
990 }
991 #endif
992 #endif
993 } else if (type == T_FLOAT) {
994 __ stf(FloatRegisterImpl::S, Ftos_f, return_slot);
995 } else if (type == T_DOUBLE) {
996 __ stf(FloatRegisterImpl::D, Ftos_f, return_slot);
997 } else {
998 ShouldNotReachHere();
999 }
1000 BLOCK_COMMENT("} move_return_value");
1001 }
1003 #ifdef ASSERT
1004 void MethodHandles::RicochetFrame::describe(const frame* fr, FrameValues& values, int frame_no) {
1005 RicochetFrame* rf = new RicochetFrame(*fr);
1007 // ricochet slots (kept in registers for sparc)
1008 values.describe(frame_no, rf->register_addr(I5_savedSP), err_msg("exact_sender_sp reg for #%d", frame_no));
1009 values.describe(frame_no, rf->register_addr(L5_conversion), err_msg("conversion reg for #%d", frame_no));
1010 values.describe(frame_no, rf->register_addr(L4_saved_args_base), err_msg("saved_args_base reg for #%d", frame_no));
1011 values.describe(frame_no, rf->register_addr(L3_saved_args_layout), err_msg("saved_args_layout reg for #%d", frame_no));
1012 values.describe(frame_no, rf->register_addr(L2_saved_target), err_msg("saved_target reg for #%d", frame_no));
1013 values.describe(frame_no, rf->register_addr(L1_continuation), err_msg("continuation reg for #%d", frame_no));
1015 // relevant ricochet targets (in caller frame)
1016 values.describe(-1, rf->saved_args_base(), err_msg("*saved_args_base for #%d", frame_no));
1017 values.describe(-1, (intptr_t *)(STACK_BIAS+(uintptr_t)rf->exact_sender_sp()), err_msg("*exact_sender_sp+STACK_BIAS for #%d", frame_no));
1018 }
1019 #endif // ASSERT
1021 #ifndef PRODUCT
1022 extern "C" void print_method_handle(oop mh);
1023 void trace_method_handle_stub(const char* adaptername,
1024 oopDesc* mh,
1025 intptr_t* saved_sp) {
1026 bool has_mh = (strstr(adaptername, "return/") == NULL); // return adapters don't have mh
1027 tty->print_cr("MH %s mh="INTPTR_FORMAT " saved_sp=" INTPTR_FORMAT, adaptername, (intptr_t) mh, saved_sp);
1028 if (has_mh)
1029 print_method_handle(mh);
1030 }
1031 void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) {
1032 if (!TraceMethodHandles) return;
1033 BLOCK_COMMENT("trace_method_handle {");
1034 // save: Gargs, O5_savedSP
1035 __ save_frame(16);
1036 __ set((intptr_t) adaptername, O0);
1037 __ mov(G3_method_handle, O1);
1038 __ mov(I5_savedSP, O2);
1039 __ mov(G3_method_handle, L3);
1040 __ mov(Gargs, L4);
1041 __ mov(G5_method_type, L5);
1042 __ call_VM_leaf(L7, CAST_FROM_FN_PTR(address, trace_method_handle_stub));
1044 __ mov(L3, G3_method_handle);
1045 __ mov(L4, Gargs);
1046 __ mov(L5, G5_method_type);
1047 __ restore();
1048 BLOCK_COMMENT("} trace_method_handle");
1049 }
1050 #endif // PRODUCT
1052 // which conversion op types are implemented here?
1053 int MethodHandles::adapter_conversion_ops_supported_mask() {
1054 return ((1<<java_lang_invoke_AdapterMethodHandle::OP_RETYPE_ONLY)
1055 |(1<<java_lang_invoke_AdapterMethodHandle::OP_RETYPE_RAW)
1056 |(1<<java_lang_invoke_AdapterMethodHandle::OP_CHECK_CAST)
1057 |(1<<java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_PRIM)
1058 |(1<<java_lang_invoke_AdapterMethodHandle::OP_REF_TO_PRIM)
1059 // OP_PRIM_TO_REF is below...
1060 |(1<<java_lang_invoke_AdapterMethodHandle::OP_SWAP_ARGS)
1061 |(1<<java_lang_invoke_AdapterMethodHandle::OP_ROT_ARGS)
1062 |(1<<java_lang_invoke_AdapterMethodHandle::OP_DUP_ARGS)
1063 |(1<<java_lang_invoke_AdapterMethodHandle::OP_DROP_ARGS)
1064 // OP_COLLECT_ARGS is below...
1065 |(1<<java_lang_invoke_AdapterMethodHandle::OP_SPREAD_ARGS)
1066 |(
1067 java_lang_invoke_MethodTypeForm::vmlayout_offset_in_bytes() <= 0 ? 0 :
1068 ((1<<java_lang_invoke_AdapterMethodHandle::OP_PRIM_TO_REF)
1069 |(1<<java_lang_invoke_AdapterMethodHandle::OP_COLLECT_ARGS)
1070 |(1<<java_lang_invoke_AdapterMethodHandle::OP_FOLD_ARGS)
1071 )
1072 )
1073 );
1074 }
1076 //------------------------------------------------------------------------------
1077 // MethodHandles::generate_method_handle_stub
1078 //
1079 // Generate an "entry" field for a method handle.
1080 // This determines how the method handle will respond to calls.
1081 void MethodHandles::generate_method_handle_stub(MacroAssembler* _masm, MethodHandles::EntryKind ek) {
1082 MethodHandles::EntryKind ek_orig = ek_original_kind(ek);
1084 // Here is the register state during an interpreted call,
1085 // as set up by generate_method_handle_interpreter_entry():
1086 // - G5: garbage temp (was MethodHandle.invoke methodOop, unused)
1087 // - G3: receiver method handle
1088 // - O5_savedSP: sender SP (must preserve)
1090 const Register O0_scratch = O0;
1091 const Register O1_scratch = O1;
1092 const Register O2_scratch = O2;
1093 const Register O3_scratch = O3;
1094 const Register O4_scratch = O4;
1095 const Register G5_scratch = G5;
1097 // Often used names:
1098 const Register O0_argslot = O0;
1100 // Argument registers for _raise_exception:
1101 const Register O0_code = O0;
1102 const Register O1_actual = O1;
1103 const Register O2_required = O2;
1105 guarantee(java_lang_invoke_MethodHandle::vmentry_offset_in_bytes() != 0, "must have offsets");
1107 // Some handy addresses:
1108 Address G3_mh_vmtarget( G3_method_handle, java_lang_invoke_MethodHandle::vmtarget_offset_in_bytes());
1110 Address G3_dmh_vmindex( G3_method_handle, java_lang_invoke_DirectMethodHandle::vmindex_offset_in_bytes());
1112 Address G3_bmh_vmargslot( G3_method_handle, java_lang_invoke_BoundMethodHandle::vmargslot_offset_in_bytes());
1113 Address G3_bmh_argument( G3_method_handle, java_lang_invoke_BoundMethodHandle::argument_offset_in_bytes());
1115 Address G3_amh_vmargslot( G3_method_handle, java_lang_invoke_AdapterMethodHandle::vmargslot_offset_in_bytes());
1116 Address G3_amh_argument ( G3_method_handle, java_lang_invoke_AdapterMethodHandle::argument_offset_in_bytes());
1117 Address G3_amh_conversion(G3_method_handle, java_lang_invoke_AdapterMethodHandle::conversion_offset_in_bytes());
1119 const int java_mirror_offset = in_bytes(Klass::java_mirror_offset());
1121 if (have_entry(ek)) {
1122 __ nop(); // empty stubs make SG sick
1123 return;
1124 }
1126 address interp_entry = __ pc();
1128 trace_method_handle(_masm, entry_name(ek));
1130 BLOCK_COMMENT(err_msg("Entry %s {", entry_name(ek)));
1132 switch ((int) ek) {
1133 case _raise_exception:
1134 {
1135 // Not a real MH entry, but rather shared code for raising an
1136 // exception. For sharing purposes the arguments are passed into registers
1137 // and then placed in the intepreter calling convention here.
1138 assert(raise_exception_method(), "must be set");
1139 assert(raise_exception_method()->from_compiled_entry(), "method must be linked");
1141 __ set(AddressLiteral((address) &_raise_exception_method), G5_method);
1142 __ ld_ptr(Address(G5_method, 0), G5_method);
1144 const int jobject_oop_offset = 0;
1145 __ ld_ptr(Address(G5_method, jobject_oop_offset), G5_method);
1147 adjust_SP_and_Gargs_down_by_slots(_masm, 3, noreg, noreg);
1149 __ st (O0_code, __ argument_address(constant(2), noreg, 0));
1150 __ st_ptr(O1_actual, __ argument_address(constant(1), noreg, 0));
1151 __ st_ptr(O2_required, __ argument_address(constant(0), noreg, 0));
1152 jump_from_method_handle(_masm, G5_method, O1_scratch, O2_scratch);
1153 }
1154 break;
1156 case _invokestatic_mh:
1157 case _invokespecial_mh:
1158 {
1159 __ load_heap_oop(G3_mh_vmtarget, G5_method); // target is a methodOop
1160 // Same as TemplateTable::invokestatic or invokespecial,
1161 // minus the CP setup and profiling:
1162 if (ek == _invokespecial_mh) {
1163 // Must load & check the first argument before entering the target method.
1164 __ load_method_handle_vmslots(O0_argslot, G3_method_handle, O1_scratch);
1165 __ ld_ptr(__ argument_address(O0_argslot, O0_argslot, -1), G3_method_handle);
1166 __ null_check(G3_method_handle);
1167 __ verify_oop(G3_method_handle);
1168 }
1169 jump_from_method_handle(_masm, G5_method, O1_scratch, O2_scratch);
1170 }
1171 break;
1173 case _invokevirtual_mh:
1174 {
1175 // Same as TemplateTable::invokevirtual,
1176 // minus the CP setup and profiling:
1178 // Pick out the vtable index and receiver offset from the MH,
1179 // and then we can discard it:
1180 Register O2_index = O2_scratch;
1181 __ load_method_handle_vmslots(O0_argslot, G3_method_handle, O1_scratch);
1182 __ ldsw(G3_dmh_vmindex, O2_index);
1183 // Note: The verifier allows us to ignore G3_mh_vmtarget.
1184 __ ld_ptr(__ argument_address(O0_argslot, O0_argslot, -1), G3_method_handle);
1185 __ null_check(G3_method_handle, oopDesc::klass_offset_in_bytes());
1187 // Get receiver klass:
1188 Register O0_klass = O0_argslot;
1189 __ load_klass(G3_method_handle, O0_klass);
1190 __ verify_oop(O0_klass);
1192 // Get target methodOop & entry point:
1193 const int base = instanceKlass::vtable_start_offset() * wordSize;
1194 assert(vtableEntry::size() * wordSize == wordSize, "adjust the scaling in the code below");
1196 __ sll_ptr(O2_index, LogBytesPerWord, O2_index);
1197 __ add(O0_klass, O2_index, O0_klass);
1198 Address vtable_entry_addr(O0_klass, base + vtableEntry::method_offset_in_bytes());
1199 __ ld_ptr(vtable_entry_addr, G5_method);
1201 jump_from_method_handle(_masm, G5_method, O1_scratch, O2_scratch);
1202 }
1203 break;
1205 case _invokeinterface_mh:
1206 {
1207 // Same as TemplateTable::invokeinterface,
1208 // minus the CP setup and profiling:
1209 __ load_method_handle_vmslots(O0_argslot, G3_method_handle, O1_scratch);
1210 Register O1_intf = O1_scratch;
1211 Register G5_index = G5_scratch;
1212 __ load_heap_oop(G3_mh_vmtarget, O1_intf);
1213 __ ldsw(G3_dmh_vmindex, G5_index);
1214 __ ld_ptr(__ argument_address(O0_argslot, O0_argslot, -1), G3_method_handle);
1215 __ null_check(G3_method_handle, oopDesc::klass_offset_in_bytes());
1217 // Get receiver klass:
1218 Register O0_klass = O0_argslot;
1219 __ load_klass(G3_method_handle, O0_klass);
1220 __ verify_oop(O0_klass);
1222 // Get interface:
1223 Label no_such_interface;
1224 __ verify_oop(O1_intf);
1225 __ lookup_interface_method(O0_klass, O1_intf,
1226 // Note: next two args must be the same:
1227 G5_index, G5_method,
1228 O2_scratch,
1229 O3_scratch,
1230 no_such_interface);
1232 jump_from_method_handle(_masm, G5_method, O1_scratch, O2_scratch);
1234 __ bind(no_such_interface);
1235 // Throw an exception.
1236 // For historical reasons, it will be IncompatibleClassChangeError.
1237 __ unimplemented("not tested yet");
1238 __ ld_ptr(Address(O1_intf, java_mirror_offset), O2_required); // required interface
1239 __ mov( O0_klass, O1_actual); // bad receiver
1240 __ jump_to(AddressLiteral(from_interpreted_entry(_raise_exception)), O3_scratch);
1241 __ delayed()->mov(Bytecodes::_invokeinterface, O0_code); // who is complaining?
1242 }
1243 break;
1245 case _bound_ref_mh:
1246 case _bound_int_mh:
1247 case _bound_long_mh:
1248 case _bound_ref_direct_mh:
1249 case _bound_int_direct_mh:
1250 case _bound_long_direct_mh:
1251 {
1252 const bool direct_to_method = (ek >= _bound_ref_direct_mh);
1253 BasicType arg_type = ek_bound_mh_arg_type(ek);
1254 int arg_slots = type2size[arg_type];
1256 // Make room for the new argument:
1257 load_vmargslot(_masm, G3_bmh_vmargslot, O0_argslot);
1258 __ add(__ argument_address(O0_argslot, O0_argslot), O0_argslot);
1260 insert_arg_slots(_masm, arg_slots * stack_move_unit(), O0_argslot, O1_scratch, O2_scratch, O3_scratch);
1262 // Store bound argument into the new stack slot:
1263 __ load_heap_oop(G3_bmh_argument, O1_scratch);
1264 if (arg_type == T_OBJECT) {
1265 __ st_ptr(O1_scratch, Address(O0_argslot, 0));
1266 } else {
1267 Address prim_value_addr(O1_scratch, java_lang_boxing_object::value_offset_in_bytes(arg_type));
1268 move_typed_arg(_masm, arg_type, false,
1269 prim_value_addr,
1270 Address(O0_argslot, 0),
1271 O2_scratch); // must be an even register for !_LP64 long moves (uses O2/O3)
1272 }
1274 if (direct_to_method) {
1275 __ load_heap_oop(G3_mh_vmtarget, G5_method); // target is a methodOop
1276 jump_from_method_handle(_masm, G5_method, O1_scratch, O2_scratch);
1277 } else {
1278 __ load_heap_oop(G3_mh_vmtarget, G3_method_handle); // target is a methodOop
1279 __ verify_oop(G3_method_handle);
1280 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1281 }
1282 }
1283 break;
1285 case _adapter_opt_profiling:
1286 if (java_lang_invoke_CountingMethodHandle::vmcount_offset_in_bytes() != 0) {
1287 Address G3_mh_vmcount(G3_method_handle, java_lang_invoke_CountingMethodHandle::vmcount_offset_in_bytes());
1288 __ ld(G3_mh_vmcount, O1_scratch);
1289 __ add(O1_scratch, 1, O1_scratch);
1290 __ st(O1_scratch, G3_mh_vmcount);
1291 }
1292 // fall through
1294 case _adapter_retype_only:
1295 case _adapter_retype_raw:
1296 // Immediately jump to the next MH layer:
1297 __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1298 __ verify_oop(G3_method_handle);
1299 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1300 // This is OK when all parameter types widen.
1301 // It is also OK when a return type narrows.
1302 break;
1304 case _adapter_check_cast:
1305 {
1306 // Check a reference argument before jumping to the next layer of MH:
1307 load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
1308 Address vmarg = __ argument_address(O0_argslot, O0_argslot);
1310 // What class are we casting to?
1311 Register O1_klass = O1_scratch; // Interesting AMH data.
1312 __ load_heap_oop(G3_amh_argument, O1_klass); // This is a Class object!
1313 load_klass_from_Class(_masm, O1_klass, O2_scratch, O3_scratch);
1315 Label L_done;
1316 __ ld_ptr(vmarg, O2_scratch);
1317 __ br_null_short(O2_scratch, Assembler::pn, L_done); // No cast if null.
1318 __ load_klass(O2_scratch, O2_scratch);
1320 // Live at this point:
1321 // - O0_argslot : argslot index in vmarg; may be required in the failing path
1322 // - O1_klass : klass required by the target method
1323 // - O2_scratch : argument klass to test
1324 // - G3_method_handle: adapter method handle
1325 __ check_klass_subtype(O2_scratch, O1_klass, O3_scratch, O4_scratch, L_done);
1327 // If we get here, the type check failed!
1328 __ load_heap_oop(G3_amh_argument, O2_required); // required class
1329 __ ld_ptr( vmarg, O1_actual); // bad object
1330 __ jump_to(AddressLiteral(from_interpreted_entry(_raise_exception)), O3_scratch);
1331 __ delayed()->mov(Bytecodes::_checkcast, O0_code); // who is complaining?
1333 __ BIND(L_done);
1334 // Get the new MH:
1335 __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1336 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1337 }
1338 break;
1340 case _adapter_prim_to_prim:
1341 case _adapter_ref_to_prim:
1342 // Handled completely by optimized cases.
1343 __ stop("init_AdapterMethodHandle should not issue this");
1344 break;
1346 case _adapter_opt_i2i: // optimized subcase of adapt_prim_to_prim
1347 //case _adapter_opt_f2i: // optimized subcase of adapt_prim_to_prim
1348 case _adapter_opt_l2i: // optimized subcase of adapt_prim_to_prim
1349 case _adapter_opt_unboxi: // optimized subcase of adapt_ref_to_prim
1350 {
1351 // Perform an in-place conversion to int or an int subword.
1352 load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
1353 Address value;
1354 Address vmarg;
1355 bool value_left_justified = false;
1357 switch (ek) {
1358 case _adapter_opt_i2i:
1359 value = vmarg = __ argument_address(O0_argslot, O0_argslot);
1360 break;
1361 case _adapter_opt_l2i:
1362 {
1363 // just delete the extra slot
1364 #ifdef _LP64
1365 // In V9, longs are given 2 64-bit slots in the interpreter, but the
1366 // data is passed in only 1 slot.
1367 // Keep the second slot.
1368 __ add(__ argument_address(O0_argslot, O0_argslot, -1), O0_argslot);
1369 remove_arg_slots(_masm, -stack_move_unit(), O0_argslot, O1_scratch, O2_scratch, O3_scratch);
1370 value = Address(O0_argslot, 4); // Get least-significant 32-bit of 64-bit value.
1371 vmarg = Address(O0_argslot, Interpreter::stackElementSize);
1372 #else
1373 // Keep the first slot.
1374 __ add(__ argument_address(O0_argslot, O0_argslot), O0_argslot);
1375 remove_arg_slots(_masm, -stack_move_unit(), O0_argslot, O1_scratch, O2_scratch, O3_scratch);
1376 value = Address(O0_argslot, 0);
1377 vmarg = value;
1378 #endif
1379 }
1380 break;
1381 case _adapter_opt_unboxi:
1382 {
1383 vmarg = __ argument_address(O0_argslot, O0_argslot);
1384 // Load the value up from the heap.
1385 __ ld_ptr(vmarg, O1_scratch);
1386 int value_offset = java_lang_boxing_object::value_offset_in_bytes(T_INT);
1387 #ifdef ASSERT
1388 for (int bt = T_BOOLEAN; bt < T_INT; bt++) {
1389 if (is_subword_type(BasicType(bt)))
1390 assert(value_offset == java_lang_boxing_object::value_offset_in_bytes(BasicType(bt)), "");
1391 }
1392 #endif
1393 __ null_check(O1_scratch, value_offset);
1394 value = Address(O1_scratch, value_offset);
1395 #ifdef _BIG_ENDIAN
1396 // Values stored in objects are packed.
1397 value_left_justified = true;
1398 #endif
1399 }
1400 break;
1401 default:
1402 ShouldNotReachHere();
1403 }
1405 // This check is required on _BIG_ENDIAN
1406 Register G5_vminfo = G5_scratch;
1407 __ ldsw(G3_amh_conversion, G5_vminfo);
1408 assert(CONV_VMINFO_SHIFT == 0, "preshifted");
1410 // Original 32-bit vmdata word must be of this form:
1411 // | MBZ:6 | signBitCount:8 | srcDstTypes:8 | conversionOp:8 |
1412 __ lduw(value, O1_scratch);
1413 if (!value_left_justified)
1414 __ sll(O1_scratch, G5_vminfo, O1_scratch);
1415 Label zero_extend, done;
1416 __ btst(CONV_VMINFO_SIGN_FLAG, G5_vminfo);
1417 __ br(Assembler::zero, false, Assembler::pn, zero_extend);
1418 __ delayed()->nop();
1420 // this path is taken for int->byte, int->short
1421 __ sra(O1_scratch, G5_vminfo, O1_scratch);
1422 __ ba_short(done);
1424 __ bind(zero_extend);
1425 // this is taken for int->char
1426 __ srl(O1_scratch, G5_vminfo, O1_scratch);
1428 __ bind(done);
1429 __ st(O1_scratch, vmarg);
1431 // Get the new MH:
1432 __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1433 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1434 }
1435 break;
1437 case _adapter_opt_i2l: // optimized subcase of adapt_prim_to_prim
1438 case _adapter_opt_unboxl: // optimized subcase of adapt_ref_to_prim
1439 {
1440 // Perform an in-place int-to-long or ref-to-long conversion.
1441 load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
1443 // On big-endian machine we duplicate the slot and store the MSW
1444 // in the first slot.
1445 __ add(__ argument_address(O0_argslot, O0_argslot, 1), O0_argslot);
1447 insert_arg_slots(_masm, stack_move_unit(), O0_argslot, O1_scratch, O2_scratch, O3_scratch);
1449 Address arg_lsw(O0_argslot, 0);
1450 Address arg_msw(O0_argslot, -Interpreter::stackElementSize);
1452 switch (ek) {
1453 case _adapter_opt_i2l:
1454 {
1455 #ifdef _LP64
1456 __ ldsw(arg_lsw, O2_scratch); // Load LSW sign-extended
1457 #else
1458 __ ldsw(arg_lsw, O3_scratch); // Load LSW sign-extended
1459 __ srlx(O3_scratch, BitsPerInt, O2_scratch); // Move MSW value to lower 32-bits for std
1460 #endif
1461 __ st_long(O2_scratch, arg_msw); // Uses O2/O3 on !_LP64
1462 }
1463 break;
1464 case _adapter_opt_unboxl:
1465 {
1466 // Load the value up from the heap.
1467 __ ld_ptr(arg_lsw, O1_scratch);
1468 int value_offset = java_lang_boxing_object::value_offset_in_bytes(T_LONG);
1469 assert(value_offset == java_lang_boxing_object::value_offset_in_bytes(T_DOUBLE), "");
1470 __ null_check(O1_scratch, value_offset);
1471 __ ld_long(Address(O1_scratch, value_offset), O2_scratch); // Uses O2/O3 on !_LP64
1472 __ st_long(O2_scratch, arg_msw);
1473 }
1474 break;
1475 default:
1476 ShouldNotReachHere();
1477 }
1479 __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1480 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1481 }
1482 break;
1484 case _adapter_opt_f2d: // optimized subcase of adapt_prim_to_prim
1485 case _adapter_opt_d2f: // optimized subcase of adapt_prim_to_prim
1486 {
1487 // perform an in-place floating primitive conversion
1488 __ unimplemented(entry_name(ek));
1489 }
1490 break;
1492 case _adapter_prim_to_ref:
1493 __ unimplemented(entry_name(ek)); // %%% FIXME: NYI
1494 break;
1496 case _adapter_swap_args:
1497 case _adapter_rot_args:
1498 // handled completely by optimized cases
1499 __ stop("init_AdapterMethodHandle should not issue this");
1500 break;
1502 case _adapter_opt_swap_1:
1503 case _adapter_opt_swap_2:
1504 case _adapter_opt_rot_1_up:
1505 case _adapter_opt_rot_1_down:
1506 case _adapter_opt_rot_2_up:
1507 case _adapter_opt_rot_2_down:
1508 {
1509 int swap_slots = ek_adapter_opt_swap_slots(ek);
1510 int rotate = ek_adapter_opt_swap_mode(ek);
1512 // 'argslot' is the position of the first argument to swap.
1513 load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
1514 __ add(__ argument_address(O0_argslot, O0_argslot), O0_argslot);
1515 if (VerifyMethodHandles)
1516 verify_argslot(_masm, O0_argslot, O2_scratch, "swap point must fall within current frame");
1518 // 'vminfo' is the second.
1519 Register O1_destslot = O1_scratch;
1520 load_conversion_vminfo(_masm, G3_amh_conversion, O1_destslot);
1521 __ add(__ argument_address(O1_destslot, O1_destslot), O1_destslot);
1522 if (VerifyMethodHandles)
1523 verify_argslot(_masm, O1_destslot, O2_scratch, "swap point must fall within current frame");
1525 assert(Interpreter::stackElementSize == wordSize, "else rethink use of wordSize here");
1526 if (!rotate) {
1527 // simple swap
1528 for (int i = 0; i < swap_slots; i++) {
1529 __ ld_ptr( Address(O0_argslot, i * wordSize), O2_scratch);
1530 __ ld_ptr( Address(O1_destslot, i * wordSize), O3_scratch);
1531 __ st_ptr(O3_scratch, Address(O0_argslot, i * wordSize));
1532 __ st_ptr(O2_scratch, Address(O1_destslot, i * wordSize));
1533 }
1534 } else {
1535 // A rotate is actually pair of moves, with an "odd slot" (or pair)
1536 // changing place with a series of other slots.
1537 // First, push the "odd slot", which is going to get overwritten
1538 switch (swap_slots) {
1539 case 2 : __ ld_ptr(Address(O0_argslot, 1 * wordSize), O4_scratch); // fall-thru
1540 case 1 : __ ld_ptr(Address(O0_argslot, 0 * wordSize), O3_scratch); break;
1541 default: ShouldNotReachHere();
1542 }
1543 if (rotate > 0) {
1544 // Here is rotate > 0:
1545 // (low mem) (high mem)
1546 // | dest: more_slots... | arg: odd_slot :arg+1 |
1547 // =>
1548 // | dest: odd_slot | dest+1: more_slots... :arg+1 |
1549 // work argslot down to destslot, copying contiguous data upwards
1550 // pseudo-code:
1551 // argslot = src_addr - swap_bytes
1552 // destslot = dest_addr
1553 // while (argslot >= destslot) *(argslot + swap_bytes) = *(argslot + 0), argslot--;
1554 move_arg_slots_up(_masm,
1555 O1_destslot,
1556 Address(O0_argslot, 0),
1557 swap_slots,
1558 O0_argslot, O2_scratch);
1559 } else {
1560 // Here is the other direction, rotate < 0:
1561 // (low mem) (high mem)
1562 // | arg: odd_slot | arg+1: more_slots... :dest+1 |
1563 // =>
1564 // | arg: more_slots... | dest: odd_slot :dest+1 |
1565 // work argslot up to destslot, copying contiguous data downwards
1566 // pseudo-code:
1567 // argslot = src_addr + swap_bytes
1568 // destslot = dest_addr
1569 // while (argslot <= destslot) *(argslot - swap_bytes) = *(argslot + 0), argslot++;
1570 // dest_slot denotes an exclusive upper limit
1571 int limit_bias = OP_ROT_ARGS_DOWN_LIMIT_BIAS;
1572 if (limit_bias != 0)
1573 __ add(O1_destslot, - limit_bias * wordSize, O1_destslot);
1574 move_arg_slots_down(_masm,
1575 Address(O0_argslot, swap_slots * wordSize),
1576 O1_destslot,
1577 -swap_slots,
1578 O0_argslot, O2_scratch);
1580 __ sub(O1_destslot, swap_slots * wordSize, O1_destslot);
1581 }
1582 // pop the original first chunk into the destination slot, now free
1583 switch (swap_slots) {
1584 case 2 : __ st_ptr(O4_scratch, Address(O1_destslot, 1 * wordSize)); // fall-thru
1585 case 1 : __ st_ptr(O3_scratch, Address(O1_destslot, 0 * wordSize)); break;
1586 default: ShouldNotReachHere();
1587 }
1588 }
1590 __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1591 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1592 }
1593 break;
1595 case _adapter_dup_args:
1596 {
1597 // 'argslot' is the position of the first argument to duplicate.
1598 load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
1599 __ add(__ argument_address(O0_argslot, O0_argslot), O0_argslot);
1601 // 'stack_move' is negative number of words to duplicate.
1602 Register O1_stack_move = O1_scratch;
1603 load_stack_move(_masm, G3_amh_conversion, O1_stack_move);
1605 if (VerifyMethodHandles) {
1606 verify_argslots(_masm, O1_stack_move, O0_argslot, O2_scratch, O3_scratch, true,
1607 "copied argument(s) must fall within current frame");
1608 }
1610 // insert location is always the bottom of the argument list:
1611 __ neg(O1_stack_move);
1612 push_arg_slots(_masm, O0_argslot, O1_stack_move, O2_scratch, O3_scratch);
1614 __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1615 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1616 }
1617 break;
1619 case _adapter_drop_args:
1620 {
1621 // 'argslot' is the position of the first argument to nuke.
1622 load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
1623 __ add(__ argument_address(O0_argslot, O0_argslot), O0_argslot);
1625 // 'stack_move' is number of words to drop.
1626 Register O1_stack_move = O1_scratch;
1627 load_stack_move(_masm, G3_amh_conversion, O1_stack_move);
1629 remove_arg_slots(_masm, O1_stack_move, O0_argslot, O2_scratch, O3_scratch, O4_scratch);
1631 __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
1632 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
1633 }
1634 break;
1636 case _adapter_collect_args:
1637 case _adapter_fold_args:
1638 case _adapter_spread_args:
1639 // Handled completely by optimized cases.
1640 __ stop("init_AdapterMethodHandle should not issue this");
1641 break;
1643 case _adapter_opt_collect_ref:
1644 case _adapter_opt_collect_int:
1645 case _adapter_opt_collect_long:
1646 case _adapter_opt_collect_float:
1647 case _adapter_opt_collect_double:
1648 case _adapter_opt_collect_void:
1649 case _adapter_opt_collect_0_ref:
1650 case _adapter_opt_collect_1_ref:
1651 case _adapter_opt_collect_2_ref:
1652 case _adapter_opt_collect_3_ref:
1653 case _adapter_opt_collect_4_ref:
1654 case _adapter_opt_collect_5_ref:
1655 case _adapter_opt_filter_S0_ref:
1656 case _adapter_opt_filter_S1_ref:
1657 case _adapter_opt_filter_S2_ref:
1658 case _adapter_opt_filter_S3_ref:
1659 case _adapter_opt_filter_S4_ref:
1660 case _adapter_opt_filter_S5_ref:
1661 case _adapter_opt_collect_2_S0_ref:
1662 case _adapter_opt_collect_2_S1_ref:
1663 case _adapter_opt_collect_2_S2_ref:
1664 case _adapter_opt_collect_2_S3_ref:
1665 case _adapter_opt_collect_2_S4_ref:
1666 case _adapter_opt_collect_2_S5_ref:
1667 case _adapter_opt_fold_ref:
1668 case _adapter_opt_fold_int:
1669 case _adapter_opt_fold_long:
1670 case _adapter_opt_fold_float:
1671 case _adapter_opt_fold_double:
1672 case _adapter_opt_fold_void:
1673 case _adapter_opt_fold_1_ref:
1674 case _adapter_opt_fold_2_ref:
1675 case _adapter_opt_fold_3_ref:
1676 case _adapter_opt_fold_4_ref:
1677 case _adapter_opt_fold_5_ref:
1678 {
1679 // Given a fresh incoming stack frame, build a new ricochet frame.
1680 // On entry, TOS points at a return PC, and FP is the callers frame ptr.
1681 // RSI/R13 has the caller's exact stack pointer, which we must also preserve.
1682 // RCX contains an AdapterMethodHandle of the indicated kind.
1684 // Relevant AMH fields:
1685 // amh.vmargslot:
1686 // points to the trailing edge of the arguments
1687 // to filter, collect, or fold. For a boxing operation,
1688 // it points just after the single primitive value.
1689 // amh.argument:
1690 // recursively called MH, on |collect| arguments
1691 // amh.vmtarget:
1692 // final destination MH, on return value, etc.
1693 // amh.conversion.dest:
1694 // tells what is the type of the return value
1695 // (not needed here, since dest is also derived from ek)
1696 // amh.conversion.vminfo:
1697 // points to the trailing edge of the return value
1698 // when the vmtarget is to be called; this is
1699 // equal to vmargslot + (retained ? |collect| : 0)
1701 // Pass 0 or more argument slots to the recursive target.
1702 int collect_count_constant = ek_adapter_opt_collect_count(ek);
1704 // The collected arguments are copied from the saved argument list:
1705 int collect_slot_constant = ek_adapter_opt_collect_slot(ek);
1707 assert(ek_orig == _adapter_collect_args ||
1708 ek_orig == _adapter_fold_args, "");
1709 bool retain_original_args = (ek_orig == _adapter_fold_args);
1711 // The return value is replaced (or inserted) at the 'vminfo' argslot.
1712 // Sometimes we can compute this statically.
1713 int dest_slot_constant = -1;
1714 if (!retain_original_args)
1715 dest_slot_constant = collect_slot_constant;
1716 else if (collect_slot_constant >= 0 && collect_count_constant >= 0)
1717 // We are preserving all the arguments, and the return value is prepended,
1718 // so the return slot is to the left (above) the |collect| sequence.
1719 dest_slot_constant = collect_slot_constant + collect_count_constant;
1721 // Replace all those slots by the result of the recursive call.
1722 // The result type can be one of ref, int, long, float, double, void.
1723 // In the case of void, nothing is pushed on the stack after return.
1724 BasicType dest = ek_adapter_opt_collect_type(ek);
1725 assert(dest == type2wfield[dest], "dest is a stack slot type");
1726 int dest_count = type2size[dest];
1727 assert(dest_count == 1 || dest_count == 2 || (dest_count == 0 && dest == T_VOID), "dest has a size");
1729 // Choose a return continuation.
1730 EntryKind ek_ret = _adapter_opt_return_any;
1731 if (dest != T_CONFLICT && OptimizeMethodHandles) {
1732 switch (dest) {
1733 case T_INT : ek_ret = _adapter_opt_return_int; break;
1734 case T_LONG : ek_ret = _adapter_opt_return_long; break;
1735 case T_FLOAT : ek_ret = _adapter_opt_return_float; break;
1736 case T_DOUBLE : ek_ret = _adapter_opt_return_double; break;
1737 case T_OBJECT : ek_ret = _adapter_opt_return_ref; break;
1738 case T_VOID : ek_ret = _adapter_opt_return_void; break;
1739 default : ShouldNotReachHere();
1740 }
1741 if (dest == T_OBJECT && dest_slot_constant >= 0) {
1742 EntryKind ek_try = EntryKind(_adapter_opt_return_S0_ref + dest_slot_constant);
1743 if (ek_try <= _adapter_opt_return_LAST &&
1744 ek_adapter_opt_return_slot(ek_try) == dest_slot_constant) {
1745 ek_ret = ek_try;
1746 }
1747 }
1748 assert(ek_adapter_opt_return_type(ek_ret) == dest, "");
1749 }
1751 // Already pushed: ... keep1 | collect | keep2 |
1753 // Push a few extra argument words, if we need them to store the return value.
1754 {
1755 int extra_slots = 0;
1756 if (retain_original_args) {
1757 extra_slots = dest_count;
1758 } else if (collect_count_constant == -1) {
1759 extra_slots = dest_count; // collect_count might be zero; be generous
1760 } else if (dest_count > collect_count_constant) {
1761 extra_slots = (dest_count - collect_count_constant);
1762 } else {
1763 // else we know we have enough dead space in |collect| to repurpose for return values
1764 }
1765 if (extra_slots != 0) {
1766 __ sub(SP, round_to(extra_slots, 2) * Interpreter::stackElementSize, SP);
1767 }
1768 }
1770 // Set up Ricochet Frame.
1771 __ mov(SP, O5_savedSP); // record SP for the callee
1773 // One extra (empty) slot for outgoing target MH (see Gargs computation below).
1774 __ save_frame(2); // Note: we need to add 2 slots since frame::memory_parameter_word_sp_offset is 23.
1776 // Note: Gargs is live throughout the following, until we make our recursive call.
1777 // And the RF saves a copy in L4_saved_args_base.
1779 RicochetFrame::enter_ricochet_frame(_masm, G3_method_handle, Gargs,
1780 entry(ek_ret)->from_interpreted_entry());
1782 // Compute argument base:
1783 // Set up Gargs for current frame, extra (empty) slot is for outgoing target MH (space reserved by save_frame above).
1784 __ add(FP, STACK_BIAS - (1 * Interpreter::stackElementSize), Gargs);
1786 // Now pushed: ... keep1 | collect | keep2 | extra | [RF]
1788 #ifdef ASSERT
1789 if (VerifyMethodHandles && dest != T_CONFLICT) {
1790 BLOCK_COMMENT("verify AMH.conv.dest {");
1791 extract_conversion_dest_type(_masm, RicochetFrame::L5_conversion, O1_scratch);
1792 Label L_dest_ok;
1793 __ cmp(O1_scratch, (int) dest);
1794 __ br(Assembler::equal, false, Assembler::pt, L_dest_ok);
1795 __ delayed()->nop();
1796 if (dest == T_INT) {
1797 for (int bt = T_BOOLEAN; bt < T_INT; bt++) {
1798 if (is_subword_type(BasicType(bt))) {
1799 __ cmp(O1_scratch, (int) bt);
1800 __ br(Assembler::equal, false, Assembler::pt, L_dest_ok);
1801 __ delayed()->nop();
1802 }
1803 }
1804 }
1805 __ stop("bad dest in AMH.conv");
1806 __ BIND(L_dest_ok);
1807 BLOCK_COMMENT("} verify AMH.conv.dest");
1808 }
1809 #endif //ASSERT
1811 // Find out where the original copy of the recursive argument sequence begins.
1812 Register O0_coll = O0_scratch;
1813 {
1814 RegisterOrConstant collect_slot = collect_slot_constant;
1815 if (collect_slot_constant == -1) {
1816 load_vmargslot(_masm, G3_amh_vmargslot, O1_scratch);
1817 collect_slot = O1_scratch;
1818 }
1819 // collect_slot might be 0, but we need the move anyway.
1820 __ add(RicochetFrame::L4_saved_args_base, __ argument_offset(collect_slot, collect_slot.register_or_noreg()), O0_coll);
1821 // O0_coll now points at the trailing edge of |collect| and leading edge of |keep2|
1822 }
1824 // Replace the old AMH with the recursive MH. (No going back now.)
1825 // In the case of a boxing call, the recursive call is to a 'boxer' method,
1826 // such as Integer.valueOf or Long.valueOf. In the case of a filter
1827 // or collect call, it will take one or more arguments, transform them,
1828 // and return some result, to store back into argument_base[vminfo].
1829 __ load_heap_oop(G3_amh_argument, G3_method_handle);
1830 if (VerifyMethodHandles) verify_method_handle(_masm, G3_method_handle, O1_scratch, O2_scratch);
1832 // Calculate |collect|, the number of arguments we are collecting.
1833 Register O1_collect_count = O1_scratch;
1834 RegisterOrConstant collect_count;
1835 if (collect_count_constant < 0) {
1836 __ load_method_handle_vmslots(O1_collect_count, G3_method_handle, O2_scratch);
1837 collect_count = O1_collect_count;
1838 } else {
1839 collect_count = collect_count_constant;
1840 #ifdef ASSERT
1841 if (VerifyMethodHandles) {
1842 BLOCK_COMMENT("verify collect_count_constant {");
1843 __ load_method_handle_vmslots(O3_scratch, G3_method_handle, O2_scratch);
1844 Label L_count_ok;
1845 __ cmp_and_br_short(O3_scratch, collect_count_constant, Assembler::equal, Assembler::pt, L_count_ok);
1846 __ stop("bad vminfo in AMH.conv");
1847 __ BIND(L_count_ok);
1848 BLOCK_COMMENT("} verify collect_count_constant");
1849 }
1850 #endif //ASSERT
1851 }
1853 // copy |collect| slots directly to TOS:
1854 push_arg_slots(_masm, O0_coll, collect_count, O2_scratch, O3_scratch);
1855 // Now pushed: ... keep1 | collect | keep2 | RF... | collect |
1856 // O0_coll still points at the trailing edge of |collect| and leading edge of |keep2|
1858 // If necessary, adjust the saved arguments to make room for the eventual return value.
1859 // Normal adjustment: ... keep1 | +dest+ | -collect- | keep2 | RF... | collect |
1860 // If retaining args: ... keep1 | +dest+ | collect | keep2 | RF... | collect |
1861 // In the non-retaining case, this might move keep2 either up or down.
1862 // We don't have to copy the whole | RF... collect | complex,
1863 // but we must adjust RF.saved_args_base.
1864 // Also, from now on, we will forget about the original copy of |collect|.
1865 // If we are retaining it, we will treat it as part of |keep2|.
1866 // For clarity we will define |keep3| = |collect|keep2| or |keep2|.
1868 BLOCK_COMMENT("adjust trailing arguments {");
1869 // Compare the sizes of |+dest+| and |-collect-|, which are opposed opening and closing movements.
1870 int open_count = dest_count;
1871 RegisterOrConstant close_count = collect_count_constant;
1872 Register O1_close_count = O1_collect_count;
1873 if (retain_original_args) {
1874 close_count = constant(0);
1875 } else if (collect_count_constant == -1) {
1876 close_count = O1_collect_count;
1877 }
1879 // How many slots need moving? This is simply dest_slot (0 => no |keep3|).
1880 RegisterOrConstant keep3_count;
1881 Register O2_keep3_count = O2_scratch;
1882 if (dest_slot_constant < 0) {
1883 extract_conversion_vminfo(_masm, RicochetFrame::L5_conversion, O2_keep3_count);
1884 keep3_count = O2_keep3_count;
1885 } else {
1886 keep3_count = dest_slot_constant;
1887 #ifdef ASSERT
1888 if (VerifyMethodHandles && dest_slot_constant < 0) {
1889 BLOCK_COMMENT("verify dest_slot_constant {");
1890 extract_conversion_vminfo(_masm, RicochetFrame::L5_conversion, O3_scratch);
1891 Label L_vminfo_ok;
1892 __ cmp_and_br_short(O3_scratch, dest_slot_constant, Assembler::equal, Assembler::pt, L_vminfo_ok);
1893 __ stop("bad vminfo in AMH.conv");
1894 __ BIND(L_vminfo_ok);
1895 BLOCK_COMMENT("} verify dest_slot_constant");
1896 }
1897 #endif //ASSERT
1898 }
1900 // tasks remaining:
1901 bool move_keep3 = (!keep3_count.is_constant() || keep3_count.as_constant() != 0);
1902 bool stomp_dest = (NOT_DEBUG(dest == T_OBJECT) DEBUG_ONLY(dest_count != 0));
1903 bool fix_arg_base = (!close_count.is_constant() || open_count != close_count.as_constant());
1905 // Old and new argument locations (based at slot 0).
1906 // Net shift (&new_argv - &old_argv) is (close_count - open_count).
1907 bool zero_open_count = (open_count == 0); // remember this bit of info
1908 if (move_keep3 && fix_arg_base) {
1909 // It will be easier to have everything in one register:
1910 if (close_count.is_register()) {
1911 // Deduct open_count from close_count register to get a clean +/- value.
1912 __ sub(close_count.as_register(), open_count, close_count.as_register());
1913 } else {
1914 close_count = close_count.as_constant() - open_count;
1915 }
1916 open_count = 0;
1917 }
1918 Register L4_old_argv = RicochetFrame::L4_saved_args_base;
1919 Register O3_new_argv = O3_scratch;
1920 if (fix_arg_base) {
1921 __ add(L4_old_argv, __ argument_offset(close_count, O4_scratch), O3_new_argv,
1922 -(open_count * Interpreter::stackElementSize));
1923 }
1925 // First decide if any actual data are to be moved.
1926 // We can skip if (a) |keep3| is empty, or (b) the argument list size didn't change.
1927 // (As it happens, all movements involve an argument list size change.)
1929 // If there are variable parameters, use dynamic checks to skip around the whole mess.
1930 Label L_done;
1931 if (keep3_count.is_register()) {
1932 __ cmp_and_br_short(keep3_count.as_register(), 0, Assembler::equal, Assembler::pn, L_done);
1933 }
1934 if (close_count.is_register()) {
1935 __ cmp_and_br_short(close_count.as_register(), open_count, Assembler::equal, Assembler::pn, L_done);
1936 }
1938 if (move_keep3 && fix_arg_base) {
1939 bool emit_move_down = false, emit_move_up = false, emit_guard = false;
1940 if (!close_count.is_constant()) {
1941 emit_move_down = emit_guard = !zero_open_count;
1942 emit_move_up = true;
1943 } else if (open_count != close_count.as_constant()) {
1944 emit_move_down = (open_count > close_count.as_constant());
1945 emit_move_up = !emit_move_down;
1946 }
1947 Label L_move_up;
1948 if (emit_guard) {
1949 __ cmp(close_count.as_register(), open_count);
1950 __ br(Assembler::greater, false, Assembler::pn, L_move_up);
1951 __ delayed()->nop();
1952 }
1954 if (emit_move_down) {
1955 // Move arguments down if |+dest+| > |-collect-|
1956 // (This is rare, except when arguments are retained.)
1957 // This opens space for the return value.
1958 if (keep3_count.is_constant()) {
1959 for (int i = 0; i < keep3_count.as_constant(); i++) {
1960 __ ld_ptr( Address(L4_old_argv, i * Interpreter::stackElementSize), O4_scratch);
1961 __ st_ptr(O4_scratch, Address(O3_new_argv, i * Interpreter::stackElementSize) );
1962 }
1963 } else {
1964 // Live: O1_close_count, O2_keep3_count, O3_new_argv
1965 Register argv_top = O0_scratch;
1966 __ add(L4_old_argv, __ argument_offset(keep3_count, O4_scratch), argv_top);
1967 move_arg_slots_down(_masm,
1968 Address(L4_old_argv, 0), // beginning of old argv
1969 argv_top, // end of old argv
1970 close_count, // distance to move down (must be negative)
1971 O4_scratch, G5_scratch);
1972 }
1973 }
1975 if (emit_guard) {
1976 __ ba_short(L_done); // assumes emit_move_up is true also
1977 __ BIND(L_move_up);
1978 }
1980 if (emit_move_up) {
1981 // Move arguments up if |+dest+| < |-collect-|
1982 // (This is usual, except when |keep3| is empty.)
1983 // This closes up the space occupied by the now-deleted collect values.
1984 if (keep3_count.is_constant()) {
1985 for (int i = keep3_count.as_constant() - 1; i >= 0; i--) {
1986 __ ld_ptr( Address(L4_old_argv, i * Interpreter::stackElementSize), O4_scratch);
1987 __ st_ptr(O4_scratch, Address(O3_new_argv, i * Interpreter::stackElementSize) );
1988 }
1989 } else {
1990 Address argv_top(L4_old_argv, __ argument_offset(keep3_count, O4_scratch));
1991 // Live: O1_close_count, O2_keep3_count, O3_new_argv
1992 move_arg_slots_up(_masm,
1993 L4_old_argv, // beginning of old argv
1994 argv_top, // end of old argv
1995 close_count, // distance to move up (must be positive)
1996 O4_scratch, G5_scratch);
1997 }
1998 }
1999 }
2000 __ BIND(L_done);
2002 if (fix_arg_base) {
2003 // adjust RF.saved_args_base
2004 __ mov(O3_new_argv, RicochetFrame::L4_saved_args_base);
2005 }
2007 if (stomp_dest) {
2008 // Stomp the return slot, so it doesn't hold garbage.
2009 // This isn't strictly necessary, but it may help detect bugs.
2010 __ set(RicochetFrame::RETURN_VALUE_PLACEHOLDER, O4_scratch);
2011 __ st_ptr(O4_scratch, Address(RicochetFrame::L4_saved_args_base,
2012 __ argument_offset(keep3_count, keep3_count.register_or_noreg()))); // uses O2_keep3_count
2013 }
2014 BLOCK_COMMENT("} adjust trailing arguments");
2016 BLOCK_COMMENT("do_recursive_call");
2017 __ mov(SP, O5_savedSP); // record SP for the callee
2018 __ set(ExternalAddress(SharedRuntime::ricochet_blob()->bounce_addr() - frame::pc_return_offset), O7);
2019 // The globally unique bounce address has two purposes:
2020 // 1. It helps the JVM recognize this frame (frame::is_ricochet_frame).
2021 // 2. When returned to, it cuts back the stack and redirects control flow
2022 // to the return handler.
2023 // The return handler will further cut back the stack when it takes
2024 // down the RF. Perhaps there is a way to streamline this further.
2026 // State during recursive call:
2027 // ... keep1 | dest | dest=42 | keep3 | RF... | collect | bounce_pc |
2028 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
2029 }
2030 break;
2032 case _adapter_opt_return_ref:
2033 case _adapter_opt_return_int:
2034 case _adapter_opt_return_long:
2035 case _adapter_opt_return_float:
2036 case _adapter_opt_return_double:
2037 case _adapter_opt_return_void:
2038 case _adapter_opt_return_S0_ref:
2039 case _adapter_opt_return_S1_ref:
2040 case _adapter_opt_return_S2_ref:
2041 case _adapter_opt_return_S3_ref:
2042 case _adapter_opt_return_S4_ref:
2043 case _adapter_opt_return_S5_ref:
2044 {
2045 BasicType dest_type_constant = ek_adapter_opt_return_type(ek);
2046 int dest_slot_constant = ek_adapter_opt_return_slot(ek);
2048 if (VerifyMethodHandles) RicochetFrame::verify_clean(_masm);
2050 if (dest_slot_constant == -1) {
2051 // The current stub is a general handler for this dest_type.
2052 // It can be called from _adapter_opt_return_any below.
2053 // Stash the address in a little table.
2054 assert((dest_type_constant & CONV_TYPE_MASK) == dest_type_constant, "oob");
2055 address return_handler = __ pc();
2056 _adapter_return_handlers[dest_type_constant] = return_handler;
2057 if (dest_type_constant == T_INT) {
2058 // do the subword types too
2059 for (int bt = T_BOOLEAN; bt < T_INT; bt++) {
2060 if (is_subword_type(BasicType(bt)) &&
2061 _adapter_return_handlers[bt] == NULL) {
2062 _adapter_return_handlers[bt] = return_handler;
2063 }
2064 }
2065 }
2066 }
2068 // On entry to this continuation handler, make Gargs live again.
2069 __ mov(RicochetFrame::L4_saved_args_base, Gargs);
2071 Register O7_temp = O7;
2072 Register O5_vminfo = O5;
2074 RegisterOrConstant dest_slot = dest_slot_constant;
2075 if (dest_slot_constant == -1) {
2076 extract_conversion_vminfo(_masm, RicochetFrame::L5_conversion, O5_vminfo);
2077 dest_slot = O5_vminfo;
2078 }
2079 // Store the result back into the argslot.
2080 // This code uses the interpreter calling sequence, in which the return value
2081 // is usually left in the TOS register, as defined by InterpreterMacroAssembler::pop.
2082 // There are certain irregularities with floating point values, which can be seen
2083 // in TemplateInterpreterGenerator::generate_return_entry_for.
2084 move_return_value(_masm, dest_type_constant, __ argument_address(dest_slot, O7_temp));
2086 RicochetFrame::leave_ricochet_frame(_masm, G3_method_handle, I5_savedSP, I7);
2088 // Load the final target and go.
2089 if (VerifyMethodHandles) verify_method_handle(_masm, G3_method_handle, O0_scratch, O1_scratch);
2090 __ restore(I5_savedSP, G0, SP);
2091 __ jump_to_method_handle_entry(G3_method_handle, O0_scratch);
2092 __ illtrap(0);
2093 }
2094 break;
2096 case _adapter_opt_return_any:
2097 {
2098 Register O7_temp = O7;
2099 Register O5_dest_type = O5;
2101 if (VerifyMethodHandles) RicochetFrame::verify_clean(_masm);
2102 extract_conversion_dest_type(_masm, RicochetFrame::L5_conversion, O5_dest_type);
2103 __ set(ExternalAddress((address) &_adapter_return_handlers[0]), O7_temp);
2104 __ sll_ptr(O5_dest_type, LogBytesPerWord, O5_dest_type);
2105 __ ld_ptr(O7_temp, O5_dest_type, O7_temp);
2107 #ifdef ASSERT
2108 { Label L_ok;
2109 __ br_notnull_short(O7_temp, Assembler::pt, L_ok);
2110 __ stop("bad method handle return");
2111 __ BIND(L_ok);
2112 }
2113 #endif //ASSERT
2114 __ JMP(O7_temp, 0);
2115 __ delayed()->nop();
2116 }
2117 break;
2119 case _adapter_opt_spread_0:
2120 case _adapter_opt_spread_1_ref:
2121 case _adapter_opt_spread_2_ref:
2122 case _adapter_opt_spread_3_ref:
2123 case _adapter_opt_spread_4_ref:
2124 case _adapter_opt_spread_5_ref:
2125 case _adapter_opt_spread_ref:
2126 case _adapter_opt_spread_byte:
2127 case _adapter_opt_spread_char:
2128 case _adapter_opt_spread_short:
2129 case _adapter_opt_spread_int:
2130 case _adapter_opt_spread_long:
2131 case _adapter_opt_spread_float:
2132 case _adapter_opt_spread_double:
2133 {
2134 // spread an array out into a group of arguments
2135 int length_constant = ek_adapter_opt_spread_count(ek);
2136 bool length_can_be_zero = (length_constant == 0);
2137 if (length_constant < 0) {
2138 // some adapters with variable length must handle the zero case
2139 if (!OptimizeMethodHandles ||
2140 ek_adapter_opt_spread_type(ek) != T_OBJECT)
2141 length_can_be_zero = true;
2142 }
2144 // find the address of the array argument
2145 load_vmargslot(_masm, G3_amh_vmargslot, O0_argslot);
2146 __ add(__ argument_address(O0_argslot, O0_argslot), O0_argslot);
2148 // O0_argslot points both to the array and to the first output arg
2149 Address vmarg = Address(O0_argslot, 0);
2151 // Get the array value.
2152 Register O1_array = O1_scratch;
2153 Register O2_array_klass = O2_scratch;
2154 BasicType elem_type = ek_adapter_opt_spread_type(ek);
2155 int elem_slots = type2size[elem_type]; // 1 or 2
2156 int array_slots = 1; // array is always a T_OBJECT
2157 int length_offset = arrayOopDesc::length_offset_in_bytes();
2158 int elem0_offset = arrayOopDesc::base_offset_in_bytes(elem_type);
2159 __ ld_ptr(vmarg, O1_array);
2161 Label L_array_is_empty, L_insert_arg_space, L_copy_args, L_args_done;
2162 if (length_can_be_zero) {
2163 // handle the null pointer case, if zero is allowed
2164 Label L_skip;
2165 if (length_constant < 0) {
2166 load_conversion_vminfo(_masm, G3_amh_conversion, O3_scratch);
2167 __ cmp_zero_and_br(Assembler::notZero, O3_scratch, L_skip);
2168 __ delayed()->nop(); // to avoid back-to-back cbcond instructions
2169 }
2170 __ br_null_short(O1_array, Assembler::pn, L_array_is_empty);
2171 __ BIND(L_skip);
2172 }
2173 __ null_check(O1_array, oopDesc::klass_offset_in_bytes());
2174 __ load_klass(O1_array, O2_array_klass);
2176 // Check the array type.
2177 Register O3_klass = O3_scratch;
2178 __ load_heap_oop(G3_amh_argument, O3_klass); // this is a Class object!
2179 load_klass_from_Class(_masm, O3_klass, O4_scratch, G5_scratch);
2181 Label L_ok_array_klass, L_bad_array_klass, L_bad_array_length;
2182 __ check_klass_subtype(O2_array_klass, O3_klass, O4_scratch, G5_scratch, L_ok_array_klass);
2183 // If we get here, the type check failed!
2184 __ ba_short(L_bad_array_klass);
2185 __ BIND(L_ok_array_klass);
2187 // Check length.
2188 if (length_constant >= 0) {
2189 __ ldsw(Address(O1_array, length_offset), O4_scratch);
2190 __ cmp(O4_scratch, length_constant);
2191 } else {
2192 Register O3_vminfo = O3_scratch;
2193 load_conversion_vminfo(_masm, G3_amh_conversion, O3_vminfo);
2194 __ ldsw(Address(O1_array, length_offset), O4_scratch);
2195 __ cmp(O3_vminfo, O4_scratch);
2196 }
2197 __ br(Assembler::notEqual, false, Assembler::pn, L_bad_array_length);
2198 __ delayed()->nop();
2200 Register O2_argslot_limit = O2_scratch;
2202 // Array length checks out. Now insert any required stack slots.
2203 if (length_constant == -1) {
2204 // Form a pointer to the end of the affected region.
2205 __ add(O0_argslot, Interpreter::stackElementSize, O2_argslot_limit);
2206 // 'stack_move' is negative number of words to insert
2207 // This number already accounts for elem_slots.
2208 Register O3_stack_move = O3_scratch;
2209 load_stack_move(_masm, G3_amh_conversion, O3_stack_move);
2210 __ cmp(O3_stack_move, 0);
2211 assert(stack_move_unit() < 0, "else change this comparison");
2212 __ br(Assembler::less, false, Assembler::pn, L_insert_arg_space);
2213 __ delayed()->nop();
2214 __ br(Assembler::equal, false, Assembler::pn, L_copy_args);
2215 __ delayed()->nop();
2216 // single argument case, with no array movement
2217 __ BIND(L_array_is_empty);
2218 remove_arg_slots(_masm, -stack_move_unit() * array_slots,
2219 O0_argslot, O1_scratch, O2_scratch, O3_scratch);
2220 __ ba_short(L_args_done); // no spreading to do
2221 __ BIND(L_insert_arg_space);
2222 // come here in the usual case, stack_move < 0 (2 or more spread arguments)
2223 // Live: O1_array, O2_argslot_limit, O3_stack_move
2224 insert_arg_slots(_masm, O3_stack_move,
2225 O0_argslot, O4_scratch, G5_scratch, O1_scratch);
2226 // reload from rdx_argslot_limit since rax_argslot is now decremented
2227 __ ld_ptr(Address(O2_argslot_limit, -Interpreter::stackElementSize), O1_array);
2228 } else if (length_constant >= 1) {
2229 int new_slots = (length_constant * elem_slots) - array_slots;
2230 insert_arg_slots(_masm, new_slots * stack_move_unit(),
2231 O0_argslot, O2_scratch, O3_scratch, O4_scratch);
2232 } else if (length_constant == 0) {
2233 __ BIND(L_array_is_empty);
2234 remove_arg_slots(_masm, -stack_move_unit() * array_slots,
2235 O0_argslot, O1_scratch, O2_scratch, O3_scratch);
2236 } else {
2237 ShouldNotReachHere();
2238 }
2240 // Copy from the array to the new slots.
2241 // Note: Stack change code preserves integrity of O0_argslot pointer.
2242 // So even after slot insertions, O0_argslot still points to first argument.
2243 // Beware: Arguments that are shallow on the stack are deep in the array,
2244 // and vice versa. So a downward-growing stack (the usual) has to be copied
2245 // elementwise in reverse order from the source array.
2246 __ BIND(L_copy_args);
2247 if (length_constant == -1) {
2248 // [O0_argslot, O2_argslot_limit) is the area we are inserting into.
2249 // Array element [0] goes at O0_argslot_limit[-wordSize].
2250 Register O1_source = O1_array;
2251 __ add(Address(O1_array, elem0_offset), O1_source);
2252 Register O4_fill_ptr = O4_scratch;
2253 __ mov(O2_argslot_limit, O4_fill_ptr);
2254 Label L_loop;
2255 __ BIND(L_loop);
2256 __ add(O4_fill_ptr, -Interpreter::stackElementSize * elem_slots, O4_fill_ptr);
2257 move_typed_arg(_masm, elem_type, true,
2258 Address(O1_source, 0), Address(O4_fill_ptr, 0),
2259 O2_scratch); // must be an even register for !_LP64 long moves (uses O2/O3)
2260 __ add(O1_source, type2aelembytes(elem_type), O1_source);
2261 __ cmp_and_brx_short(O4_fill_ptr, O0_argslot, Assembler::greaterUnsigned, Assembler::pt, L_loop);
2262 } else if (length_constant == 0) {
2263 // nothing to copy
2264 } else {
2265 int elem_offset = elem0_offset;
2266 int slot_offset = length_constant * Interpreter::stackElementSize;
2267 for (int index = 0; index < length_constant; index++) {
2268 slot_offset -= Interpreter::stackElementSize * elem_slots; // fill backward
2269 move_typed_arg(_masm, elem_type, true,
2270 Address(O1_array, elem_offset), Address(O0_argslot, slot_offset),
2271 O2_scratch); // must be an even register for !_LP64 long moves (uses O2/O3)
2272 elem_offset += type2aelembytes(elem_type);
2273 }
2274 }
2275 __ BIND(L_args_done);
2277 // Arguments are spread. Move to next method handle.
2278 __ load_heap_oop(G3_mh_vmtarget, G3_method_handle);
2279 __ jump_to_method_handle_entry(G3_method_handle, O1_scratch);
2281 __ BIND(L_bad_array_klass);
2282 assert(!vmarg.uses(O2_required), "must be different registers");
2283 __ load_heap_oop(Address(O2_array_klass, java_mirror_offset), O2_required); // required class
2284 __ ld_ptr( vmarg, O1_actual); // bad object
2285 __ jump_to(AddressLiteral(from_interpreted_entry(_raise_exception)), O3_scratch);
2286 __ delayed()->mov(Bytecodes::_aaload, O0_code); // who is complaining?
2288 __ bind(L_bad_array_length);
2289 assert(!vmarg.uses(O2_required), "must be different registers");
2290 __ mov( G3_method_handle, O2_required); // required class
2291 __ ld_ptr(vmarg, O1_actual); // bad object
2292 __ jump_to(AddressLiteral(from_interpreted_entry(_raise_exception)), O3_scratch);
2293 __ delayed()->mov(Bytecodes::_arraylength, O0_code); // who is complaining?
2294 }
2295 break;
2297 default:
2298 DEBUG_ONLY(tty->print_cr("bad ek=%d (%s)", (int)ek, entry_name(ek)));
2299 ShouldNotReachHere();
2300 }
2301 BLOCK_COMMENT(err_msg("} Entry %s", entry_name(ek)));
2303 address me_cookie = MethodHandleEntry::start_compiled_entry(_masm, interp_entry);
2304 __ unimplemented(entry_name(ek)); // %%% FIXME: NYI
2306 init_entry(ek, MethodHandleEntry::finish_compiled_entry(_masm, me_cookie));
2307 }