sharedRuntime.hpp revision 1601:126ea7725993
1100206Sdd/*
2330449Seadler * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3330449Seadler * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4100206Sdd *
5100206Sdd * This code is free software; you can redistribute it and/or modify it
6100206Sdd * under the terms of the GNU General Public License version 2 only, as
7100206Sdd * published by the Free Software Foundation.
8100206Sdd *
9100206Sdd * This code is distributed in the hope that it will be useful, but WITHOUT
10100206Sdd * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11100206Sdd * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12100206Sdd * version 2 for more details (a copy is included in the LICENSE file that
13100206Sdd * accompanied this code).
14100206Sdd *
15100206Sdd * You should have received a copy of the GNU General Public License version
16100206Sdd * 2 along with this work; if not, write to the Free Software Foundation,
17100206Sdd * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18100206Sdd *
19100206Sdd * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20100206Sdd * or visit www.oracle.com if you need additional information or have any
21100206Sdd * questions.
22100206Sdd *
23100206Sdd */
24100206Sdd
25100206Sddclass AdapterHandlerEntry;
26100206Sddclass AdapterHandlerTable;
27100206Sddclass AdapterFingerPrint;
28100206Sddclass vframeStream;
29100206Sdd
30100206Sdd// Runtime is the base class for various runtime interfaces
31100206Sdd// (InterpreterRuntime, CompilerRuntime, etc.). It provides
32100206Sdd// shared functionality such as exception forwarding (C++ to
33100206Sdd// Java exceptions), locking/unlocking mechanisms, statistical
34100206Sdd// information, etc.
35100206Sdd
36100206Sddclass SharedRuntime: AllStatic {
37100206Sdd private:
38100206Sdd  static methodHandle resolve_sub_helper(JavaThread *thread,
39100206Sdd                                     bool is_virtual,
40100799Sdd                                     bool is_optimized, TRAPS);
41100206Sdd
42100206Sdd  // Shared stub locations
43100206Sdd
44100206Sdd  static RuntimeStub* _wrong_method_blob;
45100206Sdd  static RuntimeStub* _ic_miss_blob;
46100206Sdd  static RuntimeStub* _resolve_opt_virtual_call_blob;
47100206Sdd  static RuntimeStub* _resolve_virtual_call_blob;
48100206Sdd  static RuntimeStub* _resolve_static_call_blob;
49100206Sdd
50100206Sdd  static SafepointBlob* _polling_page_safepoint_handler_blob;
51100206Sdd  static SafepointBlob* _polling_page_return_handler_blob;
52229778Suqs#ifdef COMPILER2
53100799Sdd  static ExceptionBlob*       _exception_blob;
54100799Sdd  static UncommonTrapBlob*    _uncommon_trap_blob;
55100206Sdd#endif // COMPILER2
56100206Sdd
57100206Sdd#ifndef PRODUCT
58100206Sdd
59100206Sdd  // Counters
60100206Sdd  static int     _nof_megamorphic_calls;         // total # of megamorphic calls (through vtable)
61100206Sdd
62100206Sdd#endif // !PRODUCT
63100206Sdd public:
64100206Sdd
65100206Sdd  // max bytes for each dtrace string parameter
66100206Sdd  enum { max_dtrace_string_size = 256 };
67100206Sdd
68100206Sdd  // The following arithmetic routines are used on platforms that do
69100206Sdd  // not have machine instructions to implement their functionality.
70100206Sdd  // Do not remove these.
71100206Sdd
72100206Sdd  // long arithmetics
73100206Sdd  static jlong   lmul(jlong y, jlong x);
74100206Sdd  static jlong   ldiv(jlong y, jlong x);
75100206Sdd  static jlong   lrem(jlong y, jlong x);
76100206Sdd
77100206Sdd  // float and double remainder
78100206Sdd  static jfloat  frem(jfloat  x, jfloat  y);
79100206Sdd  static jdouble drem(jdouble x, jdouble y);
80100206Sdd
81227081Sed#ifdef __SOFTFP__
82100206Sdd  static jfloat  fadd(jfloat x, jfloat y);
83100206Sdd  static jfloat  fsub(jfloat x, jfloat y);
84100206Sdd  static jfloat  fmul(jfloat x, jfloat y);
85100206Sdd  static jfloat  fdiv(jfloat x, jfloat y);
86100206Sdd
87124830Sgrehan  static jdouble dadd(jdouble x, jdouble y);
88100206Sdd  static jdouble dsub(jdouble x, jdouble y);
89100206Sdd  static jdouble dmul(jdouble x, jdouble y);
90100206Sdd  static jdouble ddiv(jdouble x, jdouble y);
91100206Sdd#endif // __SOFTFP__
92100206Sdd
93100206Sdd  // float conversion (needs to set appropriate rounding mode)
94100206Sdd  static jint    f2i (jfloat  x);
95100206Sdd  static jlong   f2l (jfloat  x);
96100206Sdd  static jint    d2i (jdouble x);
97100206Sdd  static jlong   d2l (jdouble x);
98100206Sdd  static jfloat  d2f (jdouble x);
99100206Sdd  static jfloat  l2f (jlong   x);
100100206Sdd  static jdouble l2d (jlong   x);
101100206Sdd
102100206Sdd#ifdef __SOFTFP__
103100206Sdd  static jfloat  i2f (jint    x);
104100206Sdd  static jdouble i2d (jint    x);
105100206Sdd  static jdouble f2d (jfloat  x);
106100206Sdd#endif // __SOFTFP__
107100206Sdd
108100206Sdd  // double trigonometrics and transcendentals
109100206Sdd  static jdouble dsin(jdouble x);
110100206Sdd  static jdouble dcos(jdouble x);
111100206Sdd  static jdouble dtan(jdouble x);
112100206Sdd  static jdouble dlog(jdouble x);
113100206Sdd  static jdouble dlog10(jdouble x);
114100206Sdd  static jdouble dexp(jdouble x);
115100206Sdd  static jdouble dpow(jdouble x, jdouble y);
116100206Sdd
117100206Sdd#if defined(__SOFTFP__) || defined(E500V2)
118100799Sdd  static double dabs(double f);
119100799Sdd  static double dsqrt(double f);
120100799Sdd#endif
121100799Sdd
122100799Sdd#ifdef __SOFTFP__
123100799Sdd  // C++ compiler generates soft float instructions as well as passing
124100799Sdd  // float and double in registers.
125100799Sdd  static int  fcmpl(float x, float y);
126100206Sdd  static int  fcmpg(float x, float y);
127100206Sdd  static int  dcmpl(double x, double y);
128100206Sdd  static int  dcmpg(double x, double y);
129100206Sdd
130100206Sdd  static int unordered_fcmplt(float x, float y);
131100206Sdd  static int unordered_dcmplt(double x, double y);
132100206Sdd  static int unordered_fcmple(float x, float y);
133100206Sdd  static int unordered_dcmple(double x, double y);
134100206Sdd  static int unordered_fcmpge(float x, float y);
135100206Sdd  static int unordered_dcmpge(double x, double y);
136100206Sdd  static int unordered_fcmpgt(float x, float y);
137100206Sdd  static int unordered_dcmpgt(double x, double y);
138100206Sdd
139100206Sdd  static float  fneg(float f);
140100799Sdd  static double dneg(double f);
141100799Sdd#endif
142100799Sdd
143100799Sdd  // exception handling across interpreter/compiler boundaries
144100799Sdd  static address raw_exception_handler_for_return_address(JavaThread* thread, address return_address);
145100799Sdd  static address exception_handler_for_return_address(JavaThread* thread, address return_address);
146100799Sdd
147100799Sdd#ifndef SERIALGC
148100206Sdd  // G1 write barriers
149100206Sdd  static void g1_wb_pre(oopDesc* orig, JavaThread *thread);
150100206Sdd  static void g1_wb_post(void* card_addr, JavaThread* thread);
151100206Sdd#endif // !SERIALGC
152100206Sdd
153100206Sdd  // exception handling and implicit exceptions
154100206Sdd  static address compute_compiled_exc_handler(nmethod* nm, address ret_pc, Handle& exception,
155100206Sdd                                              bool force_unwind, bool top_frame_only);
156100206Sdd  enum ImplicitExceptionKind {
157100206Sdd    IMPLICIT_NULL,
158100206Sdd    IMPLICIT_DIVIDE_BY_ZERO,
159100206Sdd    STACK_OVERFLOW
160100206Sdd  };
161100206Sdd  static void    throw_AbstractMethodError(JavaThread* thread);
162100206Sdd  static void    throw_IncompatibleClassChangeError(JavaThread* thread);
163100206Sdd  static void    throw_ArithmeticException(JavaThread* thread);
164100206Sdd  static void    throw_NullPointerException(JavaThread* thread);
165100206Sdd  static void    throw_NullPointerException_at_call(JavaThread* thread);
166100206Sdd  static void    throw_StackOverflowError(JavaThread* thread);
167100206Sdd  static address continuation_for_implicit_exception(JavaThread* thread,
168100206Sdd                                                     address faulting_pc,
169100206Sdd                                                     ImplicitExceptionKind exception_kind);
170100206Sdd
171100206Sdd  // Shared stub locations
172100206Sdd  static address get_poll_stub(address pc);
173100206Sdd
174100206Sdd  static address get_ic_miss_stub() {
175100206Sdd    assert(_ic_miss_blob!= NULL, "oops");
176100206Sdd    return _ic_miss_blob->instructions_begin();
177100206Sdd  }
178100206Sdd
179100206Sdd  static address get_handle_wrong_method_stub() {
180100206Sdd    assert(_wrong_method_blob!= NULL, "oops");
181100206Sdd    return _wrong_method_blob->instructions_begin();
182100206Sdd  }
183100206Sdd
184100206Sdd#ifdef COMPILER2
185100206Sdd  static void generate_uncommon_trap_blob(void);
186100206Sdd  static UncommonTrapBlob* uncommon_trap_blob()                  { return _uncommon_trap_blob; }
187100206Sdd#endif // COMPILER2
188100206Sdd
189100206Sdd  static address get_resolve_opt_virtual_call_stub(){
190100206Sdd    assert(_resolve_opt_virtual_call_blob != NULL, "oops");
191100206Sdd    return _resolve_opt_virtual_call_blob->instructions_begin();
192100206Sdd  }
193100206Sdd  static address get_resolve_virtual_call_stub() {
194100206Sdd    assert(_resolve_virtual_call_blob != NULL, "oops");
195100206Sdd    return _resolve_virtual_call_blob->instructions_begin();
196100206Sdd  }
197100206Sdd  static address get_resolve_static_call_stub() {
198100206Sdd    assert(_resolve_static_call_blob != NULL, "oops");
199100206Sdd    return _resolve_static_call_blob->instructions_begin();
200100206Sdd  }
201100206Sdd
202100206Sdd  static SafepointBlob* polling_page_return_handler_blob()     { return _polling_page_return_handler_blob; }
203100206Sdd  static SafepointBlob* polling_page_safepoint_handler_blob()  { return _polling_page_safepoint_handler_blob; }
204100206Sdd
205100206Sdd  // Counters
206100206Sdd#ifndef PRODUCT
207100206Sdd  static address nof_megamorphic_calls_addr() { return (address)&_nof_megamorphic_calls; }
208100206Sdd#endif // PRODUCT
209100206Sdd
210100206Sdd  // Helper routine for full-speed JVMTI exception throwing support
211100206Sdd  static void throw_and_post_jvmti_exception(JavaThread *thread, Handle h_exception);
212100206Sdd  static void throw_and_post_jvmti_exception(JavaThread *thread, symbolOop name, const char *message = NULL);
213100206Sdd
214100206Sdd  // RedefineClasses() tracing support for obsolete method entry
215100206Sdd  static int rc_trace_method_entry(JavaThread* thread, methodOopDesc* m);
216100206Sdd
217100206Sdd  // To be used as the entry point for unresolved native methods.
218100206Sdd  static address native_method_throw_unsatisfied_link_error_entry();
219100206Sdd
220100206Sdd  // bytecode tracing is only used by the TraceBytecodes
221100206Sdd  static intptr_t trace_bytecode(JavaThread* thread, intptr_t preserve_this_value, intptr_t tos, intptr_t tos2) PRODUCT_RETURN0;
222100206Sdd
223100206Sdd  // Used to back off a spin lock that is under heavy contention
224100206Sdd  static void yield_all(JavaThread* thread, int attempts = 0);
225100206Sdd
226100206Sdd  static oop retrieve_receiver( symbolHandle sig, frame caller );
227100206Sdd
228100206Sdd  static void register_finalizer(JavaThread* thread, oopDesc* obj);
229100206Sdd
230100206Sdd  // dtrace notifications
231100805Sdd  static int dtrace_object_alloc(oopDesc* o);
232100206Sdd  static int dtrace_object_alloc_base(Thread* thread, oopDesc* o);
233100206Sdd  static int dtrace_method_entry(JavaThread* thread, methodOopDesc* m);
234100206Sdd  static int dtrace_method_exit(JavaThread* thread, methodOopDesc* m);
235100206Sdd
236100206Sdd  // Utility method for retrieving the Java thread id, returns 0 if the
237100206Sdd  // thread is not a well formed Java thread.
238100206Sdd  static jlong get_java_tid(Thread* thread);
239100206Sdd
240100206Sdd
241100206Sdd  // used by native wrappers to reenable yellow if overflow happened in native code
242100206Sdd  static void reguard_yellow_pages();
243100206Sdd
244100206Sdd  /**
245100206Sdd   * Fill in the "X cannot be cast to a Y" message for ClassCastException
246100206Sdd   *
247100206Sdd   * @param thr the current thread
248100206Sdd   * @param name the name of the class of the object attempted to be cast
249100206Sdd   * @return the dynamically allocated exception message (must be freed
250100206Sdd   * by the caller using a resource mark)
251100206Sdd   *
252100206Sdd   * BCP must refer to the current 'checkcast' opcode for the frame
253100206Sdd   * on top of the stack.
254100206Sdd   * The caller (or one of it's callers) must use a ResourceMark
255100206Sdd   * in order to correctly free the result.
256100206Sdd   */
257100206Sdd  static char* generate_class_cast_message(JavaThread* thr, const char* name);
258100206Sdd
259100206Sdd  /**
260100206Sdd   * Fill in the message for a WrongMethodTypeException
261100206Sdd   *
262100206Sdd   * @param thr the current thread
263100206Sdd   * @param mtype (optional) expected method type (or argument class)
264100206Sdd   * @param mhandle (optional) actual method handle (or argument)
265100206Sdd   * @return the dynamically allocated exception message
266100799Sdd   *
267100799Sdd   * BCP for the frame on top of the stack must refer to an
268100799Sdd   * 'invokevirtual' op for a method handle, or an 'invokedyamic' op.
269100799Sdd   * The caller (or one of its callers) must use a ResourceMark
270100799Sdd   * in order to correctly free the result.
271100799Sdd   */
272100799Sdd  static char* generate_wrong_method_type_message(JavaThread* thr,
273100803Sdd                                                  oopDesc* mtype = NULL,
274100799Sdd                                                  oopDesc* mhandle = NULL);
275100799Sdd
276100799Sdd  /** Return non-null if the mtype is a klass or Class, not a MethodType. */
277100799Sdd  static oop wrong_method_type_is_for_single_argument(JavaThread* thr,
278100799Sdd                                                      oopDesc* mtype);
279100799Sdd
280100799Sdd  /**
281100799Sdd   * Fill in the "X cannot be cast to a Y" message for ClassCastException
282100803Sdd   *
283100799Sdd   * @param name the name of the class of the object attempted to be cast
284100799Sdd   * @param klass the name of the target klass attempt
285100799Sdd   * @param gripe the specific kind of problem being reported
286100799Sdd   * @return the dynamically allocated exception message (must be freed
287100799Sdd   * by the caller using a resource mark)
288100799Sdd   *
289100799Sdd   * This version does not require access the frame, so it can be called
290100799Sdd   * from interpreted code
291100799Sdd   * The caller (or one of it's callers) must use a ResourceMark
292100799Sdd   * in order to correctly free the result.
293100799Sdd   */
294100799Sdd  static char* generate_class_cast_message(const char* name, const char* klass,
295100799Sdd                                           const char* gripe = " cannot be cast to ");
296100799Sdd
297100799Sdd  // Resolves a call site- may patch in the destination of the call into the
298100799Sdd  // compiled code.
299100799Sdd  static methodHandle resolve_helper(JavaThread *thread,
300100799Sdd                                     bool is_virtual,
301100799Sdd                                     bool is_optimized, TRAPS);
302100799Sdd
303100799Sdd  static void generate_stubs(void);
304100799Sdd
305100799Sdd  private:
306100799Sdd  // deopt blob
307100799Sdd  static void generate_deopt_blob(void);
308100799Sdd  static DeoptimizationBlob* _deopt_blob;
309100206Sdd
310100206Sdd  public:
311100206Sdd  static DeoptimizationBlob* deopt_blob(void)      { return _deopt_blob; }
312100206Sdd
313100206Sdd  // Resets a call-site in compiled code so it will get resolved again.
314100206Sdd  static methodHandle reresolve_call_site(JavaThread *thread, TRAPS);
315100206Sdd
316100206Sdd  // In the code prolog, if the klass comparison fails, the inline cache
317100206Sdd  // misses and the call site is patched to megamorphic
318100206Sdd  static methodHandle handle_ic_miss_helper(JavaThread* thread, TRAPS);
319137303Sdd
320100206Sdd  // Find the method that called us.
321100206Sdd  static methodHandle find_callee_method(JavaThread* thread, TRAPS);
322100206Sdd
323100206Sdd
324100206Sdd private:
325100206Sdd  static Handle find_callee_info(JavaThread* thread,
326100206Sdd                                 Bytecodes::Code& bc,
327100206Sdd                                 CallInfo& callinfo, TRAPS);
328100206Sdd  static Handle find_callee_info_helper(JavaThread* thread,
329100206Sdd                                        vframeStream& vfst,
330100206Sdd                                        Bytecodes::Code& bc,
331100206Sdd                                        CallInfo& callinfo, TRAPS);
332100206Sdd
333100206Sdd  static address clean_virtual_call_entry();
334100206Sdd  static address clean_opt_virtual_call_entry();
335100206Sdd  static address clean_static_call_entry();
336100206Sdd
337100206Sdd public:
338100206Sdd
339100206Sdd  // Read the array of BasicTypes from a Java signature, and compute where
340100206Sdd  // compiled Java code would like to put the results.  Values in reg_lo and
341100206Sdd  // reg_hi refer to 4-byte quantities.  Values less than SharedInfo::stack0 are
342100206Sdd  // registers, those above refer to 4-byte stack slots.  All stack slots are
343100206Sdd  // based off of the window top.  SharedInfo::stack0 refers to the first usable
344100206Sdd  // slot in the bottom of the frame. SharedInfo::stack0+1 refers to the memory word
345100206Sdd  // 4-bytes higher. So for sparc because the register window save area is at
346100206Sdd  // the bottom of the frame the first 16 words will be skipped and SharedInfo::stack0
347100206Sdd  // will be just above it. (
348100206Sdd  // return value is the maximum number of VMReg stack slots the convention will use.
349100206Sdd  static int java_calling_convention(const BasicType *sig_bt, VMRegPair *regs, int total_args_passed, int is_outgoing);
350100206Sdd
351100206Sdd  // Ditto except for calling C
352100206Sdd  static int c_calling_convention(const BasicType *sig_bt, VMRegPair *regs, int total_args_passed);
353100206Sdd
354100206Sdd  // Generate I2C and C2I adapters. These adapters are simple argument marshalling
355100206Sdd  // blobs. Unlike adapters in the tiger and earlier releases the code in these
356100206Sdd  // blobs does not create a new frame and are therefore virtually invisible
357100206Sdd  // to the stack walking code. In general these blobs extend the callers stack
358100206Sdd  // as needed for the conversion of argument locations.
359100206Sdd
360100206Sdd  // When calling a c2i blob the code will always call the interpreter even if
361100206Sdd  // by the time we reach the blob there is compiled code available. This allows
362100206Sdd  // the blob to pass the incoming stack pointer (the sender sp) in a known
363100206Sdd  // location for the interpreter to record. This is used by the frame code
364100206Sdd  // to correct the sender code to match up with the stack pointer when the
365137303Sdd  // thread left the compiled code. In addition it allows the interpreter
366137303Sdd  // to remove the space the c2i adapter allocated to do it argument conversion.
367100206Sdd
368100206Sdd  // Although a c2i blob will always run interpreted even if compiled code is
369100206Sdd  // present if we see that compiled code is present the compiled call site
370100206Sdd  // will be patched/re-resolved so that later calls will run compiled.
371100206Sdd
372100206Sdd  // Aditionally a c2i blob need to have a unverified entry because it can be reached
373100206Sdd  // in situations where the call site is an inlined cache site and may go megamorphic.
374100206Sdd
375100206Sdd  // A i2c adapter is simpler than the c2i adapter. This is because it is assumed
376100206Sdd  // that the interpreter before it does any call dispatch will record the current
377100206Sdd  // stack pointer in the interpreter frame. On return it will restore the stack
378100206Sdd  // pointer as needed. This means the i2c adapter code doesn't need any special
379100206Sdd  // handshaking path with compiled code to keep the stack walking correct.
380100206Sdd
381100206Sdd  static AdapterHandlerEntry* generate_i2c2i_adapters(MacroAssembler *_masm,
382100206Sdd                                                      int total_args_passed,
383100206Sdd                                                      int max_arg,
384100206Sdd                                                      const BasicType *sig_bt,
385100206Sdd                                                      const VMRegPair *regs,
386100206Sdd                                                      AdapterFingerPrint* fingerprint);
387100206Sdd
388100206Sdd  // OSR support
389100206Sdd
390100206Sdd  // OSR_migration_begin will extract the jvm state from an interpreter
391100206Sdd  // frame (locals, monitors) and store the data in a piece of C heap
392100206Sdd  // storage. This then allows the interpreter frame to be removed from the
393100206Sdd  // stack and the OSR nmethod to be called. That method is called with a
394100206Sdd  // pointer to the C heap storage. This pointer is the return value from
395100206Sdd  // OSR_migration_begin.
396100206Sdd
397100206Sdd  static intptr_t* OSR_migration_begin( JavaThread *thread);
398137303Sdd
399137303Sdd  // OSR_migration_end is a trivial routine. It is called after the compiled
400154053Smaxim  // method has extracted the jvm state from the C heap that OSR_migration_begin
401137303Sdd  // created. It's entire job is to simply free this storage.
402100206Sdd  static void      OSR_migration_end  ( intptr_t* buf);
403100206Sdd
404100206Sdd  // Convert a sig into a calling convention register layout
405100206Sdd  // and find interesting things about it.
406100206Sdd  static VMRegPair* find_callee_arguments(symbolOop sig, bool has_receiver, int *arg_size);
407100206Sdd  static VMReg     name_for_receiver();
408100206Sdd
409100206Sdd  // "Top of Stack" slots that may be unused by the calling convention but must
410100206Sdd  // otherwise be preserved.
411100206Sdd  // On Intel these are not necessary and the value can be zero.
412100206Sdd  // On Sparc this describes the words reserved for storing a register window
413100206Sdd  // when an interrupt occurs.
414100206Sdd  static uint out_preserve_stack_slots();
415100206Sdd
416100206Sdd  // Save and restore a native result
417100206Sdd  static void    save_native_result(MacroAssembler *_masm, BasicType ret_type, int frame_slots );
418100206Sdd  static void restore_native_result(MacroAssembler *_masm, BasicType ret_type, int frame_slots );
419100206Sdd
420100206Sdd  // Generate a native wrapper for a given method.  The method takes arguments
421100206Sdd  // in the Java compiled code convention, marshals them to the native
422100206Sdd  // convention (handlizes oops, etc), transitions to native, makes the call,
423100206Sdd  // returns to java state (possibly blocking), unhandlizes any result and
424100206Sdd  // returns.
425100206Sdd  static nmethod *generate_native_wrapper(MacroAssembler* masm,
426100206Sdd                                          methodHandle method,
427100206Sdd                                          int total_args_passed,
428100206Sdd                                          int max_arg,
429100206Sdd                                          BasicType *sig_bt,
430100206Sdd                                          VMRegPair *regs,
431100206Sdd                                          BasicType ret_type );
432100206Sdd
433100206Sdd#ifdef HAVE_DTRACE_H
434100206Sdd  // Generate a dtrace wrapper for a given method.  The method takes arguments
435100206Sdd  // in the Java compiled code convention, marshals them to the native
436100206Sdd  // convention (handlizes oops, etc), transitions to native, makes the call,
437100206Sdd  // returns to java state (possibly blocking), unhandlizes any result and
438100206Sdd  // returns.
439100206Sdd  static nmethod *generate_dtrace_nmethod(MacroAssembler* masm,
440100206Sdd                                          methodHandle method);
441100206Sdd
442100206Sdd  // dtrace support to convert a Java string to utf8
443100206Sdd  static void get_utf(oopDesc* src, address dst);
444100206Sdd#endif // def HAVE_DTRACE_H
445100206Sdd
446100206Sdd  // A compiled caller has just called the interpreter, but compiled code
447100206Sdd  // exists.  Patch the caller so he no longer calls into the interpreter.
448100206Sdd  static void fixup_callers_callsite(methodOopDesc* moop, address ret_pc);
449100206Sdd
450100206Sdd  // Slow-path Locking and Unlocking
451100206Sdd  static void complete_monitor_locking_C(oopDesc* obj, BasicLock* lock, JavaThread* thread);
452100206Sdd  static void complete_monitor_unlocking_C(oopDesc* obj, BasicLock* lock);
453100206Sdd
454100206Sdd  // Resolving of calls
455100206Sdd  static address resolve_static_call_C     (JavaThread *thread);
456100206Sdd  static address resolve_virtual_call_C    (JavaThread *thread);
457100206Sdd  static address resolve_opt_virtual_call_C(JavaThread *thread);
458100206Sdd
459100206Sdd  // arraycopy, the non-leaf version.  (See StubRoutines for all the leaf calls.)
460100206Sdd  static void slow_arraycopy_C(oopDesc* src,  jint src_pos,
461100206Sdd                               oopDesc* dest, jint dest_pos,
462100206Sdd                               jint length, JavaThread* thread);
463100206Sdd
464100206Sdd  // handle ic miss with caller being compiled code
465  // wrong method handling (inline cache misses, zombie methods)
466  static address handle_wrong_method(JavaThread* thread);
467  static address handle_wrong_method_ic_miss(JavaThread* thread);
468
469#ifndef PRODUCT
470
471  // Collect and print inline cache miss statistics
472 private:
473  enum { maxICmiss_count = 100 };
474  static int     _ICmiss_index;                  // length of IC miss histogram
475  static int     _ICmiss_count[maxICmiss_count]; // miss counts
476  static address _ICmiss_at[maxICmiss_count];    // miss addresses
477  static void trace_ic_miss(address at);
478
479 public:
480  static int _monitor_enter_ctr;                 // monitor enter slow
481  static int _monitor_exit_ctr;                  // monitor exit slow
482  static int _throw_null_ctr;                    // throwing a null-pointer exception
483  static int _ic_miss_ctr;                       // total # of IC misses
484  static int _wrong_method_ctr;
485  static int _resolve_static_ctr;
486  static int _resolve_virtual_ctr;
487  static int _resolve_opt_virtual_ctr;
488  static int _implicit_null_throws;
489  static int _implicit_div0_throws;
490
491  static int _jbyte_array_copy_ctr;        // Slow-path byte array copy
492  static int _jshort_array_copy_ctr;       // Slow-path short array copy
493  static int _jint_array_copy_ctr;         // Slow-path int array copy
494  static int _jlong_array_copy_ctr;        // Slow-path long array copy
495  static int _oop_array_copy_ctr;          // Slow-path oop array copy
496  static int _checkcast_array_copy_ctr;    // Slow-path oop array copy, with cast
497  static int _unsafe_array_copy_ctr;       // Slow-path includes alignment checks
498  static int _generic_array_copy_ctr;      // Slow-path includes type decoding
499  static int _slow_array_copy_ctr;         // Slow-path failed out to a method call
500
501  static int _new_instance_ctr;            // 'new' object requires GC
502  static int _new_array_ctr;               // 'new' array requires GC
503  static int _multi1_ctr, _multi2_ctr, _multi3_ctr, _multi4_ctr, _multi5_ctr;
504  static int _find_handler_ctr;            // find exception handler
505  static int _rethrow_ctr;                 // rethrow exception
506  static int _mon_enter_stub_ctr;          // monitor enter stub
507  static int _mon_exit_stub_ctr;           // monitor exit stub
508  static int _mon_enter_ctr;               // monitor enter slow
509  static int _mon_exit_ctr;                // monitor exit slow
510  static int _partial_subtype_ctr;         // SubRoutines::partial_subtype_check
511
512  // Statistics code
513  // stats for "normal" compiled calls (non-interface)
514  static int     _nof_normal_calls;              // total # of calls
515  static int     _nof_optimized_calls;           // total # of statically-bound calls
516  static int     _nof_inlined_calls;             // total # of inlined normal calls
517  static int     _nof_static_calls;              // total # of calls to static methods or super methods (invokespecial)
518  static int     _nof_inlined_static_calls;      // total # of inlined static calls
519  // stats for compiled interface calls
520  static int     _nof_interface_calls;           // total # of compiled calls
521  static int     _nof_optimized_interface_calls; // total # of statically-bound interface calls
522  static int     _nof_inlined_interface_calls;   // total # of inlined interface calls
523  static int     _nof_megamorphic_interface_calls;// total # of megamorphic interface calls
524  // stats for runtime exceptions
525  static int     _nof_removable_exceptions;      // total # of exceptions that could be replaced by branches due to inlining
526
527 public: // for compiler
528  static address nof_normal_calls_addr()                { return (address)&_nof_normal_calls; }
529  static address nof_optimized_calls_addr()             { return (address)&_nof_optimized_calls; }
530  static address nof_inlined_calls_addr()               { return (address)&_nof_inlined_calls; }
531  static address nof_static_calls_addr()                { return (address)&_nof_static_calls; }
532  static address nof_inlined_static_calls_addr()        { return (address)&_nof_inlined_static_calls; }
533  static address nof_interface_calls_addr()             { return (address)&_nof_interface_calls; }
534  static address nof_optimized_interface_calls_addr()   { return (address)&_nof_optimized_interface_calls; }
535  static address nof_inlined_interface_calls_addr()     { return (address)&_nof_inlined_interface_calls; }
536  static address nof_megamorphic_interface_calls_addr() { return (address)&_nof_megamorphic_interface_calls; }
537  static void print_call_statistics(int comp_total);
538  static void print_statistics();
539  static void print_ic_miss_histogram();
540
541#endif // PRODUCT
542};
543
544
545// ---------------------------------------------------------------------------
546// Implementation of AdapterHandlerLibrary
547//
548// This library manages argument marshaling adapters and native wrappers.
549// There are 2 flavors of adapters: I2C and C2I.
550//
551// The I2C flavor takes a stock interpreted call setup, marshals the arguments
552// for a Java-compiled call, and jumps to Rmethod-> code()->
553// instructions_begin().  It is broken to call it without an nmethod assigned.
554// The usual behavior is to lift any register arguments up out of the stack
555// and possibly re-pack the extra arguments to be contigious.  I2C adapters
556// will save what the interpreter's stack pointer will be after arguments are
557// popped, then adjust the interpreter's frame size to force alignment and
558// possibly to repack the arguments.  After re-packing, it jumps to the
559// compiled code start.  There are no safepoints in this adapter code and a GC
560// cannot happen while marshaling is in progress.
561//
562// The C2I flavor takes a stock compiled call setup plus the target method in
563// Rmethod, marshals the arguments for an interpreted call and jumps to
564// Rmethod->_i2i_entry.  On entry, the interpreted frame has not yet been
565// setup.  Compiled frames are fixed-size and the args are likely not in the
566// right place.  Hence all the args will likely be copied into the
567// interpreter's frame, forcing that frame to grow.  The compiled frame's
568// outgoing stack args will be dead after the copy.
569//
570// Native wrappers, like adapters, marshal arguments.  Unlike adapters they
571// also perform an offical frame push & pop.  They have a call to the native
572// routine in their middles and end in a return (instead of ending in a jump).
573// The native wrappers are stored in real nmethods instead of the BufferBlobs
574// used by the adapters.  The code generation happens here because it's very
575// similar to what the adapters have to do.
576
577class AdapterHandlerEntry : public BasicHashtableEntry {
578  friend class AdapterHandlerTable;
579
580 private:
581  AdapterFingerPrint* _fingerprint;
582  address _i2c_entry;
583  address _c2i_entry;
584  address _c2i_unverified_entry;
585
586#ifdef ASSERT
587  // Captures code and signature used to generate this adapter when
588  // verifing adapter equivalence.
589  unsigned char* _saved_code;
590  int            _code_length;
591  BasicType*     _saved_sig;
592  int            _total_args_passed;
593#endif
594
595  void init(AdapterFingerPrint* fingerprint, address i2c_entry, address c2i_entry, address c2i_unverified_entry) {
596    _fingerprint = fingerprint;
597    _i2c_entry = i2c_entry;
598    _c2i_entry = c2i_entry;
599    _c2i_unverified_entry = c2i_unverified_entry;
600#ifdef ASSERT
601    _saved_code = NULL;
602    _code_length = 0;
603    _saved_sig = NULL;
604    _total_args_passed = 0;
605#endif
606  }
607
608  void deallocate();
609
610  // should never be used
611  AdapterHandlerEntry();
612
613 public:
614  address get_i2c_entry()            { return _i2c_entry; }
615  address get_c2i_entry()            { return _c2i_entry; }
616  address get_c2i_unverified_entry() { return _c2i_unverified_entry; }
617
618  void relocate(address new_base);
619
620  AdapterFingerPrint* fingerprint()  { return _fingerprint; }
621
622  AdapterHandlerEntry* next() {
623    return (AdapterHandlerEntry*)BasicHashtableEntry::next();
624  }
625
626#ifdef ASSERT
627  // Used to verify that code generated for shared adapters is equivalent
628  void save_code(unsigned char* code, int length, int total_args_passed, BasicType* sig_bt);
629  bool compare_code(unsigned char* code, int length, int total_args_passed, BasicType* sig_bt);
630#endif
631
632  void print();
633};
634
635class AdapterHandlerLibrary: public AllStatic {
636 private:
637  static BufferBlob* _buffer; // the temporary code buffer in CodeCache
638  static AdapterHandlerTable* _adapters;
639  static AdapterHandlerEntry* _abstract_method_handler;
640  static BufferBlob* buffer_blob();
641  static void initialize();
642
643 public:
644
645  static AdapterHandlerEntry* new_entry(AdapterFingerPrint* fingerprint,
646                                        address i2c_entry, address c2i_entry, address c2i_unverified_entry);
647  static nmethod* create_native_wrapper(methodHandle method);
648  static AdapterHandlerEntry* get_adapter(methodHandle method);
649
650#ifdef HAVE_DTRACE_H
651  static nmethod* create_dtrace_nmethod (methodHandle method);
652#endif // HAVE_DTRACE_H
653
654  static void print_handler(CodeBlob* b) { print_handler_on(tty, b); }
655  static void print_handler_on(outputStream* st, CodeBlob* b);
656  static bool contains(CodeBlob* b);
657#ifndef PRODUCT
658  static void print_statistics();
659#endif /* PRODUCT */
660
661};
662