methodHandles_aarch64.cpp revision 7880:647c8b619d80
1/*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2014, Red Hat Inc. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26#include "precompiled.hpp"
27#include "asm/macroAssembler.hpp"
28#include "interpreter/interpreter.hpp"
29#include "interpreter/interpreterRuntime.hpp"
30#include "memory/allocation.inline.hpp"
31#include "prims/methodHandles.hpp"
32
33#define __ _masm->
34
35#ifdef PRODUCT
36#define BLOCK_COMMENT(str) /* nothing */
37#else
38#define BLOCK_COMMENT(str) __ block_comment(str)
39#endif
40
41#define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
42
43void MethodHandles::load_klass_from_Class(MacroAssembler* _masm, Register klass_reg) {
44  if (VerifyMethodHandles)
45    verify_klass(_masm, klass_reg, SystemDictionary::WK_KLASS_ENUM_NAME(java_lang_Class),
46                 "MH argument is a Class");
47  __ ldr(klass_reg, Address(klass_reg, java_lang_Class::klass_offset_in_bytes()));
48}
49
50#ifdef ASSERT
51static int check_nonzero(const char* xname, int x) {
52  assert(x != 0, err_msg("%s should be nonzero", xname));
53  return x;
54}
55#define NONZERO(x) check_nonzero(#x, x)
56#else //ASSERT
57#define NONZERO(x) (x)
58#endif //PRODUCT
59
60#ifdef ASSERT
61void MethodHandles::verify_klass(MacroAssembler* _masm,
62                                 Register obj, SystemDictionary::WKID klass_id,
63                                 const char* error_message) {
64  Klass** klass_addr = SystemDictionary::well_known_klass_addr(klass_id);
65  KlassHandle klass = SystemDictionary::well_known_klass(klass_id);
66  Register temp = rscratch2;
67  Register temp2 = rscratch1; // used by MacroAssembler::cmpptr
68  Label L_ok, L_bad;
69  BLOCK_COMMENT("verify_klass {");
70  __ verify_oop(obj);
71  __ cbz(obj, L_bad);
72  __ push(RegSet::of(temp, temp2), sp);
73  __ load_klass(temp, obj);
74  __ cmpptr(temp, ExternalAddress((address) klass_addr));
75  __ br(Assembler::EQ, L_ok);
76  intptr_t super_check_offset = klass->super_check_offset();
77  __ ldr(temp, Address(temp, super_check_offset));
78  __ cmpptr(temp, ExternalAddress((address) klass_addr));
79  __ br(Assembler::EQ, L_ok);
80  __ pop(RegSet::of(temp, temp2), sp);
81  __ bind(L_bad);
82  __ stop(error_message);
83  __ BIND(L_ok);
84  __ pop(RegSet::of(temp, temp2), sp);
85  BLOCK_COMMENT("} verify_klass");
86}
87
88void MethodHandles::verify_ref_kind(MacroAssembler* _masm, int ref_kind, Register member_reg, Register temp) {  }
89
90#endif //ASSERT
91
92void MethodHandles::jump_from_method_handle(MacroAssembler* _masm, Register method, Register temp,
93                                            bool for_compiler_entry) {
94  assert(method == rmethod, "interpreter calling convention");
95  Label L_no_such_method;
96  __ cbz(rmethod, L_no_such_method);
97  __ verify_method_ptr(method);
98
99  if (!for_compiler_entry && JvmtiExport::can_post_interpreter_events()) {
100    Label run_compiled_code;
101    // JVMTI events, such as single-stepping, are implemented partly by avoiding running
102    // compiled code in threads for which the event is enabled.  Check here for
103    // interp_only_mode if these events CAN be enabled.
104
105    __ ldrb(rscratch1, Address(rthread, JavaThread::interp_only_mode_offset()));
106    __ cbnz(rscratch1, run_compiled_code);
107    __ ldr(rscratch1, Address(method, Method::interpreter_entry_offset()));
108    __ br(rscratch1);
109    __ BIND(run_compiled_code);
110  }
111
112  const ByteSize entry_offset = for_compiler_entry ? Method::from_compiled_offset() :
113                                                     Method::from_interpreted_offset();
114  __ ldr(rscratch1,Address(method, entry_offset));
115  __ br(rscratch1);
116  __ bind(L_no_such_method);
117  __ far_jump(RuntimeAddress(StubRoutines::throw_AbstractMethodError_entry()));
118}
119
120void MethodHandles::jump_to_lambda_form(MacroAssembler* _masm,
121                                        Register recv, Register method_temp,
122                                        Register temp2,
123                                        bool for_compiler_entry) {
124  BLOCK_COMMENT("jump_to_lambda_form {");
125  // This is the initial entry point of a lazy method handle.
126  // After type checking, it picks up the invoker from the LambdaForm.
127  assert_different_registers(recv, method_temp, temp2);
128  assert(recv != noreg, "required register");
129  assert(method_temp == rmethod, "required register for loading method");
130
131  //NOT_PRODUCT({ FlagSetting fs(TraceMethodHandles, true); trace_method_handle(_masm, "LZMH"); });
132
133  // Load the invoker, as MH -> MH.form -> LF.vmentry
134  __ verify_oop(recv);
135  __ load_heap_oop(method_temp, Address(recv, NONZERO(java_lang_invoke_MethodHandle::form_offset_in_bytes())));
136  __ verify_oop(method_temp);
137  __ load_heap_oop(method_temp, Address(method_temp, NONZERO(java_lang_invoke_LambdaForm::vmentry_offset_in_bytes())));
138  __ verify_oop(method_temp);
139  // the following assumes that a Method* is normally compressed in the vmtarget field:
140  __ ldr(method_temp, Address(method_temp, NONZERO(java_lang_invoke_MemberName::vmtarget_offset_in_bytes())));
141
142  if (VerifyMethodHandles && !for_compiler_entry) {
143    // make sure recv is already on stack
144    __ ldr(temp2, Address(method_temp, Method::const_offset()));
145    __ load_sized_value(temp2,
146                        Address(temp2, ConstMethod::size_of_parameters_offset()),
147                        sizeof(u2), /*is_signed*/ false);
148    // assert(sizeof(u2) == sizeof(Method::_size_of_parameters), "");
149    Label L;
150    __ ldr(rscratch1, __ argument_address(temp2, -1));
151    __ cmp(recv, rscratch1);
152    __ br(Assembler::EQ, L);
153    __ ldr(r0, __ argument_address(temp2, -1));
154    __ hlt(0);
155    __ BIND(L);
156  }
157
158  jump_from_method_handle(_masm, method_temp, temp2, for_compiler_entry);
159  BLOCK_COMMENT("} jump_to_lambda_form");
160}
161
162// Code generation
163address MethodHandles::generate_method_handle_interpreter_entry(MacroAssembler* _masm,
164                                                                vmIntrinsics::ID iid) {
165  const bool not_for_compiler_entry = false;  // this is the interpreter entry
166  assert(is_signature_polymorphic(iid), "expected invoke iid");
167  if (iid == vmIntrinsics::_invokeGeneric ||
168      iid == vmIntrinsics::_compiledLambdaForm) {
169    // Perhaps surprisingly, the symbolic references visible to Java are not directly used.
170    // They are linked to Java-generated adapters via MethodHandleNatives.linkMethod.
171    // They all allow an appendix argument.
172    __ hlt(0);           // empty stubs make SG sick
173    return NULL;
174  }
175
176  // r13: sender SP (must preserve; see prepare_to_jump_from_interpreted)
177  // rmethod: Method*
178  // r3: argument locator (parameter slot count, added to rsp)
179  // r1: used as temp to hold mh or receiver
180  // r0, r11: garbage temps, blown away
181  Register argp   = r3;   // argument list ptr, live on error paths
182  Register temp   = r0;
183  Register mh     = r1;   // MH receiver; dies quickly and is recycled
184
185  // here's where control starts out:
186  __ align(CodeEntryAlignment);
187  address entry_point = __ pc();
188
189  if (VerifyMethodHandles) {
190    Label L;
191    BLOCK_COMMENT("verify_intrinsic_id {");
192    __ ldrb(rscratch1, Address(rmethod, Method::intrinsic_id_offset_in_bytes()));
193    __ cmp(rscratch1, (int) iid);
194    __ br(Assembler::EQ, L);
195    if (iid == vmIntrinsics::_linkToVirtual ||
196        iid == vmIntrinsics::_linkToSpecial) {
197      // could do this for all kinds, but would explode assembly code size
198      trace_method_handle(_masm, "bad Method*::intrinsic_id");
199    }
200    __ hlt(0);
201    __ bind(L);
202    BLOCK_COMMENT("} verify_intrinsic_id");
203  }
204
205  // First task:  Find out how big the argument list is.
206  Address r3_first_arg_addr;
207  int ref_kind = signature_polymorphic_intrinsic_ref_kind(iid);
208  assert(ref_kind != 0 || iid == vmIntrinsics::_invokeBasic, "must be _invokeBasic or a linkTo intrinsic");
209  if (ref_kind == 0 || MethodHandles::ref_kind_has_receiver(ref_kind)) {
210    __ ldr(argp, Address(rmethod, Method::const_offset()));
211    __ load_sized_value(argp,
212                        Address(argp, ConstMethod::size_of_parameters_offset()),
213                        sizeof(u2), /*is_signed*/ false);
214    // assert(sizeof(u2) == sizeof(Method::_size_of_parameters), "");
215    r3_first_arg_addr = __ argument_address(argp, -1);
216  } else {
217    DEBUG_ONLY(argp = noreg);
218  }
219
220  if (!is_signature_polymorphic_static(iid)) {
221    __ ldr(mh, r3_first_arg_addr);
222    DEBUG_ONLY(argp = noreg);
223  }
224
225  // r3_first_arg_addr is live!
226
227  trace_method_handle_interpreter_entry(_masm, iid);
228  if (iid == vmIntrinsics::_invokeBasic) {
229    generate_method_handle_dispatch(_masm, iid, mh, noreg, not_for_compiler_entry);
230
231  } else {
232    // Adjust argument list by popping the trailing MemberName argument.
233    Register recv = noreg;
234    if (MethodHandles::ref_kind_has_receiver(ref_kind)) {
235      // Load the receiver (not the MH; the actual MemberName's receiver) up from the interpreter stack.
236      __ ldr(recv = r2, r3_first_arg_addr);
237    }
238    DEBUG_ONLY(argp = noreg);
239    Register rmember = rmethod;  // MemberName ptr; incoming method ptr is dead now
240    __ pop(rmember);             // extract last argument
241    generate_method_handle_dispatch(_masm, iid, recv, rmember, not_for_compiler_entry);
242  }
243
244  return entry_point;
245}
246
247
248void MethodHandles::generate_method_handle_dispatch(MacroAssembler* _masm,
249                                                    vmIntrinsics::ID iid,
250                                                    Register receiver_reg,
251                                                    Register member_reg,
252                                                    bool for_compiler_entry) {
253  assert(is_signature_polymorphic(iid), "expected invoke iid");
254  // temps used in this code are not used in *either* compiled or interpreted calling sequences
255  Register temp1 = r10;
256  Register temp2 = r11;
257  Register temp3 = r14;  // r13 is live by this point: it contains the sender SP
258  if (for_compiler_entry) {
259    assert(receiver_reg == (iid == vmIntrinsics::_linkToStatic ? noreg : j_rarg0), "only valid assignment");
260    assert_different_registers(temp1,        j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5, j_rarg6, j_rarg7);
261    assert_different_registers(temp2,        j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5, j_rarg6, j_rarg7);
262    assert_different_registers(temp3,        j_rarg0, j_rarg1, j_rarg2, j_rarg3, j_rarg4, j_rarg5, j_rarg6, j_rarg7);
263  }
264
265  assert_different_registers(temp1, temp2, temp3, receiver_reg);
266  assert_different_registers(temp1, temp2, temp3, member_reg);
267
268  if (iid == vmIntrinsics::_invokeBasic) {
269    // indirect through MH.form.vmentry.vmtarget
270    jump_to_lambda_form(_masm, receiver_reg, rmethod, temp1, for_compiler_entry);
271
272  } else {
273    // The method is a member invoker used by direct method handles.
274    if (VerifyMethodHandles) {
275      // make sure the trailing argument really is a MemberName (caller responsibility)
276      verify_klass(_masm, member_reg, SystemDictionary::WK_KLASS_ENUM_NAME(java_lang_invoke_MemberName),
277                   "MemberName required for invokeVirtual etc.");
278    }
279
280    Address member_clazz(    member_reg, NONZERO(java_lang_invoke_MemberName::clazz_offset_in_bytes()));
281    Address member_vmindex(  member_reg, NONZERO(java_lang_invoke_MemberName::vmindex_offset_in_bytes()));
282    Address member_vmtarget( member_reg, NONZERO(java_lang_invoke_MemberName::vmtarget_offset_in_bytes()));
283
284    Register temp1_recv_klass = temp1;
285    if (iid != vmIntrinsics::_linkToStatic) {
286      __ verify_oop(receiver_reg);
287      if (iid == vmIntrinsics::_linkToSpecial) {
288        // Don't actually load the klass; just null-check the receiver.
289        __ null_check(receiver_reg);
290      } else {
291        // load receiver klass itself
292        __ null_check(receiver_reg, oopDesc::klass_offset_in_bytes());
293        __ load_klass(temp1_recv_klass, receiver_reg);
294        __ verify_klass_ptr(temp1_recv_klass);
295      }
296      BLOCK_COMMENT("check_receiver {");
297      // The receiver for the MemberName must be in receiver_reg.
298      // Check the receiver against the MemberName.clazz
299      if (VerifyMethodHandles && iid == vmIntrinsics::_linkToSpecial) {
300        // Did not load it above...
301        __ load_klass(temp1_recv_klass, receiver_reg);
302        __ verify_klass_ptr(temp1_recv_klass);
303      }
304      if (VerifyMethodHandles && iid != vmIntrinsics::_linkToInterface) {
305        Label L_ok;
306        Register temp2_defc = temp2;
307        __ load_heap_oop(temp2_defc, member_clazz);
308        load_klass_from_Class(_masm, temp2_defc);
309        __ verify_klass_ptr(temp2_defc);
310        __ check_klass_subtype(temp1_recv_klass, temp2_defc, temp3, L_ok);
311        // If we get here, the type check failed!
312        __ hlt(0);
313        // __ STOP("receiver class disagrees with MemberName.clazz");
314        __ bind(L_ok);
315      }
316      BLOCK_COMMENT("} check_receiver");
317    }
318    if (iid == vmIntrinsics::_linkToSpecial ||
319        iid == vmIntrinsics::_linkToStatic) {
320      DEBUG_ONLY(temp1_recv_klass = noreg);  // these guys didn't load the recv_klass
321    }
322
323    // Live registers at this point:
324    //  member_reg - MemberName that was the trailing argument
325    //  temp1_recv_klass - klass of stacked receiver, if needed
326    //  r13 - interpreter linkage (if interpreted)  ??? FIXME
327    //  r1 ... r0 - compiler arguments (if compiled)
328
329    Label L_incompatible_class_change_error;
330    switch (iid) {
331    case vmIntrinsics::_linkToSpecial:
332      if (VerifyMethodHandles) {
333        verify_ref_kind(_masm, JVM_REF_invokeSpecial, member_reg, temp3);
334      }
335      __ ldr(rmethod, member_vmtarget);
336      break;
337
338    case vmIntrinsics::_linkToStatic:
339      if (VerifyMethodHandles) {
340        verify_ref_kind(_masm, JVM_REF_invokeStatic, member_reg, temp3);
341      }
342      __ ldr(rmethod, member_vmtarget);
343      break;
344
345    case vmIntrinsics::_linkToVirtual:
346    {
347      // same as TemplateTable::invokevirtual,
348      // minus the CP setup and profiling:
349
350      if (VerifyMethodHandles) {
351        verify_ref_kind(_masm, JVM_REF_invokeVirtual, member_reg, temp3);
352      }
353
354      // pick out the vtable index from the MemberName, and then we can discard it:
355      Register temp2_index = temp2;
356      __ ldr(temp2_index, member_vmindex);
357
358      if (VerifyMethodHandles) {
359        Label L_index_ok;
360        __ cmpw(temp2_index, 0U);
361        __ br(Assembler::GE, L_index_ok);
362        __ hlt(0);
363        __ BIND(L_index_ok);
364      }
365
366      // Note:  The verifier invariants allow us to ignore MemberName.clazz and vmtarget
367      // at this point.  And VerifyMethodHandles has already checked clazz, if needed.
368
369      // get target Method* & entry point
370      __ lookup_virtual_method(temp1_recv_klass, temp2_index, rmethod);
371      break;
372    }
373
374    case vmIntrinsics::_linkToInterface:
375    {
376      // same as TemplateTable::invokeinterface
377      // (minus the CP setup and profiling, with different argument motion)
378      if (VerifyMethodHandles) {
379        verify_ref_kind(_masm, JVM_REF_invokeInterface, member_reg, temp3);
380      }
381
382      Register temp3_intf = temp3;
383      __ load_heap_oop(temp3_intf, member_clazz);
384      load_klass_from_Class(_masm, temp3_intf);
385      __ verify_klass_ptr(temp3_intf);
386
387      Register rindex = rmethod;
388      __ ldr(rindex, member_vmindex);
389      if (VerifyMethodHandles) {
390        Label L;
391        __ cmpw(rindex, 0U);
392        __ br(Assembler::GE, L);
393        __ hlt(0);
394        __ bind(L);
395      }
396
397      // given intf, index, and recv klass, dispatch to the implementation method
398      __ lookup_interface_method(temp1_recv_klass, temp3_intf,
399                                 // note: next two args must be the same:
400                                 rindex, rmethod,
401                                 temp2,
402                                 L_incompatible_class_change_error);
403      break;
404    }
405
406    default:
407      fatal(err_msg_res("unexpected intrinsic %d: %s", iid, vmIntrinsics::name_at(iid)));
408      break;
409    }
410
411    // live at this point:  rmethod, r13 (if interpreted)
412
413    // After figuring out which concrete method to call, jump into it.
414    // Note that this works in the interpreter with no data motion.
415    // But the compiled version will require that r2_recv be shifted out.
416    __ verify_method_ptr(rmethod);
417    jump_from_method_handle(_masm, rmethod, temp1, for_compiler_entry);
418    if (iid == vmIntrinsics::_linkToInterface) {
419      __ bind(L_incompatible_class_change_error);
420      __ far_jump(RuntimeAddress(StubRoutines::throw_IncompatibleClassChangeError_entry()));
421    }
422  }
423}
424
425#ifndef PRODUCT
426void trace_method_handle_stub(const char* adaptername,
427                              oop mh,
428                              intptr_t* saved_regs,
429                              intptr_t* entry_sp) {  }
430
431// The stub wraps the arguments in a struct on the stack to avoid
432// dealing with the different calling conventions for passing 6
433// arguments.
434struct MethodHandleStubArguments {
435  const char* adaptername;
436  oopDesc* mh;
437  intptr_t* saved_regs;
438  intptr_t* entry_sp;
439};
440void trace_method_handle_stub_wrapper(MethodHandleStubArguments* args) {  }
441
442void MethodHandles::trace_method_handle(MacroAssembler* _masm, const char* adaptername) {  }
443#endif //PRODUCT
444