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