sharedRuntime.hpp revision 9737:e286c9ccd58d
171088Sjasone/*
271088Sjasone * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
371088Sjasone * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
471088Sjasone *
571088Sjasone * This code is free software; you can redistribute it and/or modify it
671088Sjasone * under the terms of the GNU General Public License version 2 only, as
771088Sjasone * published by the Free Software Foundation.
871088Sjasone *
971088Sjasone * This code is distributed in the hope that it will be useful, but WITHOUT
1071088Sjasone * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1171088Sjasone * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1271088Sjasone * version 2 for more details (a copy is included in the LICENSE file that
1371088Sjasone * accompanied this code).
1471088Sjasone *
1571088Sjasone * You should have received a copy of the GNU General Public License version
1671088Sjasone * 2 along with this work; if not, write to the Free Software Foundation,
1771088Sjasone * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1871088Sjasone *
1971088Sjasone * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2071088Sjasone * or visit www.oracle.com if you need additional information or have any
2171088Sjasone * questions.
2271088Sjasone *
2371088Sjasone */
2471088Sjasone
2571088Sjasone#ifndef SHARE_VM_RUNTIME_SHAREDRUNTIME_HPP
2671088Sjasone#define SHARE_VM_RUNTIME_SHAREDRUNTIME_HPP
27116182Sobrien
28116182Sobrien#include "interpreter/bytecodeHistogram.hpp"
29116182Sobrien#include "interpreter/bytecodeTracer.hpp"
3071088Sjasone#include "interpreter/linkResolver.hpp"
3171088Sjasone#include "memory/allocation.hpp"
3271088Sjasone#include "memory/resourceArea.hpp"
3371088Sjasone#include "utilities/hashtable.hpp"
3476166Smarkm#include "utilities/macros.hpp"
3576166Smarkm
3671088Sjasoneclass AdapterHandlerEntry;
3771088Sjasoneclass AdapterHandlerTable;
3871088Sjasoneclass AdapterFingerPrint;
3971088Sjasoneclass vframeStream;
40109862Sjeff
4171088Sjasone// Runtime is the base class for various runtime interfaces
42126326Sjhb// (InterpreterRuntime, CompilerRuntime, etc.). It provides
4371088Sjasone// shared functionality such as exception forwarding (C++ to
4471088Sjasone// Java exceptions), locking/unlocking mechanisms, statistical
4571088Sjasone// information, etc.
4671088Sjasone
4771088Sjasoneclass SharedRuntime: AllStatic {
4871088Sjasone  friend class VMStructs;
4971088Sjasone
5071088Sjasone private:
5171088Sjasone  static methodHandle resolve_sub_helper(JavaThread *thread,
5283366Sjulian                                         bool is_virtual,
5387594Sobrien                                         bool is_optimized, TRAPS);
54103216Sjulian
5587594Sobrien  // Shared stub locations
5687594Sobrien
5771088Sjasone  static RuntimeStub*        _wrong_method_blob;
5871088Sjasone  static RuntimeStub*        _wrong_method_abstract_blob;
5971088Sjasone  static RuntimeStub*        _ic_miss_blob;
6071088Sjasone  static RuntimeStub*        _resolve_opt_virtual_call_blob;
6171088Sjasone  static RuntimeStub*        _resolve_virtual_call_blob;
6271088Sjasone  static RuntimeStub*        _resolve_static_call_blob;
6371088Sjasone
6471088Sjasone  static DeoptimizationBlob* _deopt_blob;
6571088Sjasone
6671088Sjasone  static SafepointBlob*      _polling_page_vectors_safepoint_handler_blob;
6771088Sjasone  static SafepointBlob*      _polling_page_safepoint_handler_blob;
6871088Sjasone  static SafepointBlob*      _polling_page_return_handler_blob;
6971088Sjasone
7071088Sjasone#ifdef COMPILER2
7171088Sjasone  static UncommonTrapBlob*   _uncommon_trap_blob;
7271088Sjasone#endif // COMPILER2
7371088Sjasone
7471088Sjasone#ifndef PRODUCT
7571088Sjasone  // Counters
7671088Sjasone  static int     _nof_megamorphic_calls;         // total # of megamorphic calls (through vtable)
77126326Sjhb#endif // !PRODUCT
78126326Sjhb
7971088Sjasone private:
80126326Sjhb  enum { POLL_AT_RETURN,  POLL_AT_LOOP, POLL_AT_VECTOR_LOOP };
81126326Sjhb  static SafepointBlob* generate_handler_blob(address call_ptr, int poll_type);
82126326Sjhb  static RuntimeStub*   generate_resolve_blob(address destination, const char* name);
83126326Sjhb
8471088Sjasone public:
8571088Sjasone  static void generate_stubs(void);
8671088Sjasone
8783366Sjulian  // max bytes for each dtrace string parameter
8871088Sjasone  enum { max_dtrace_string_size = 256 };
8983366Sjulian
9071088Sjasone  // The following arithmetic routines are used on platforms that do
9171088Sjasone  // not have machine instructions to implement their functionality.
9271088Sjasone  // Do not remove these.
9371088Sjasone
9471088Sjasone  // long arithmetics
9571088Sjasone  static jlong   lmul(jlong y, jlong x);
96126326Sjhb  static jlong   ldiv(jlong y, jlong x);
9783366Sjulian  static jlong   lrem(jlong y, jlong x);
9871088Sjasone
9971088Sjasone  // float and double remainder
10083366Sjulian  static jfloat  frem(jfloat  x, jfloat  y);
10171088Sjasone  static jdouble drem(jdouble x, jdouble y);
10297995Sjhb
10397995Sjhb
10471088Sjasone#ifdef _WIN64
10583366Sjulian  // Workaround for fmod issue in the Windows x64 CRT
106111883Sjhb  static double fmod_winx64(double x, double y);
107111883Sjhb#endif
10874912Sjhb
10971088Sjasone#ifdef __SOFTFP__
110126326Sjhb  static jfloat  fadd(jfloat x, jfloat y);
11171088Sjasone  static jfloat  fsub(jfloat x, jfloat y);
112100209Sgallatin  static jfloat  fmul(jfloat x, jfloat y);
113100209Sgallatin  static jfloat  fdiv(jfloat x, jfloat y);
114100209Sgallatin
115100209Sgallatin  static jdouble dadd(jdouble x, jdouble y);
11671088Sjasone  static jdouble dsub(jdouble x, jdouble y);
11771088Sjasone  static jdouble dmul(jdouble x, jdouble y);
11871088Sjasone  static jdouble ddiv(jdouble x, jdouble y);
11995322Shsu#endif // __SOFTFP__
120126326Sjhb
12195322Shsu  // float conversion (needs to set appropriate rounding mode)
12288900Sjhb  static jint    f2i (jfloat  x);
12388900Sjhb  static jlong   f2l (jfloat  x);
12471088Sjasone  static jint    d2i (jdouble x);
125126326Sjhb  static jlong   d2l (jdouble x);
126126326Sjhb  static jfloat  d2f (jdouble x);
12771088Sjasone  static jfloat  l2f (jlong   x);
12871088Sjasone  static jdouble l2d (jlong   x);
12997995Sjhb
13097995Sjhb#ifdef __SOFTFP__
13171088Sjasone  static jfloat  i2f (jint    x);
13271088Sjasone  static jdouble i2d (jint    x);
13372200Sbmilekic  static jdouble f2d (jfloat  x);
13474912Sjhb#endif // __SOFTFP__
13571088Sjasone
13671088Sjasone  // double trigonometrics and transcendentals
13771088Sjasone  static jdouble dsin(jdouble x);
13871088Sjasone  static jdouble dcos(jdouble x);
13983366Sjulian  static jdouble dtan(jdouble x);
14071088Sjasone  static jdouble dlog(jdouble x);
14171088Sjasone  static jdouble dlog10(jdouble x);
14271088Sjasone  static jdouble dexp(jdouble x);
14371088Sjasone  static jdouble dpow(jdouble x, jdouble y);
14471088Sjasone
14571088Sjasone#if defined(__SOFTFP__) || defined(E500V2)
146126326Sjhb  static double dabs(double f);
14783366Sjulian#endif
14883658Speter
149126326Sjhb#if defined(__SOFTFP__) || defined(PPC)
15071088Sjasone  static double dsqrt(double f);
15171088Sjasone#endif
15283366Sjulian
15383650Sjhb  // Montgomery multiplication
15471088Sjasone  static void montgomery_multiply(jint *a_ints, jint *b_ints, jint *n_ints,
15571088Sjasone                                  jint len, jlong inv, jint *m_ints);
15697995Sjhb  static void montgomery_square(jint *a_ints, jint *n_ints,
15797995Sjhb                                jint len, jlong inv, jint *m_ints);
15871088Sjasone
15983366Sjulian#ifdef __SOFTFP__
160111883Sjhb  // C++ compiler generates soft float instructions as well as passing
161111883Sjhb  // float and double in registers.
16274912Sjhb  static int  fcmpl(float x, float y);
16371088Sjasone  static int  fcmpg(float x, float y);
16471088Sjasone  static int  dcmpl(double x, double y);
16571088Sjasone  static int  dcmpg(double x, double y);
16671088Sjasone
16771088Sjasone  static int unordered_fcmplt(float x, float y);
16871088Sjasone  static int unordered_dcmplt(double x, double y);
16971088Sjasone  static int unordered_fcmple(float x, float y);
17071088Sjasone  static int unordered_dcmple(double x, double y);
17171088Sjasone  static int unordered_fcmpge(float x, float y);
17271088Sjasone  static int unordered_dcmpge(double x, double y);
17395322Shsu  static int unordered_fcmpgt(float x, float y);
174126326Sjhb  static int unordered_dcmpgt(double x, double y);
17595322Shsu
176126326Sjhb  static float  fneg(float f);
17771088Sjasone  static double dneg(double f);
17888900Sjhb#endif
17988900Sjhb
18071088Sjasone  // exception handling across interpreter/compiler boundaries
181126326Sjhb  static address raw_exception_handler_for_return_address(JavaThread* thread, address return_address);
182126326Sjhb  static address exception_handler_for_return_address(JavaThread* thread, address return_address);
183126326Sjhb
184126326Sjhb#if INCLUDE_ALL_GCS
185126326Sjhb  // G1 write barriers
186126326Sjhb  static void g1_wb_pre(oopDesc* orig, JavaThread *thread);
187126326Sjhb  static void g1_wb_post(void* card_addr, JavaThread* thread);
188126326Sjhb#endif // INCLUDE_ALL_GCS
189126326Sjhb
19071088Sjasone  // exception handling and implicit exceptions
191126326Sjhb  static address compute_compiled_exc_handler(nmethod* nm, address ret_pc, Handle& exception,
19283650Sjhb                                              bool force_unwind, bool top_frame_only);
19399072Sjulian  enum ImplicitExceptionKind {
19499072Sjulian    IMPLICIT_NULL,
195113625Sjhb    IMPLICIT_DIVIDE_BY_ZERO,
19671088Sjasone    STACK_OVERFLOW
19771088Sjasone  };
19897995Sjhb  static void    throw_AbstractMethodError(JavaThread* thread);
19997995Sjhb  static void    throw_IncompatibleClassChangeError(JavaThread* thread);
20071088Sjasone  static void    throw_ArithmeticException(JavaThread* thread);
20197995Sjhb  static void    throw_NullPointerException(JavaThread* thread);
20272200Sbmilekic  static void    throw_NullPointerException_at_call(JavaThread* thread);
20374912Sjhb  static void    throw_StackOverflowError(JavaThread* thread);
20471088Sjasone  static void    throw_delayed_StackOverflowError(JavaThread* thread);
20571088Sjasone  static void    throw_StackOverflowError_common(JavaThread* thread, bool delayed);
20671088Sjasone  static address continuation_for_implicit_exception(JavaThread* thread,
20771088Sjasone                                                     address faulting_pc,
20871088Sjasone                                                     ImplicitExceptionKind exception_kind);
20971088Sjasone#if INCLUDE_JVMCI
21071088Sjasone  static address deoptimize_for_implicit_exception(JavaThread* thread, address pc, nmethod* nm, int deopt_reason);
21171088Sjasone#endif
21271088Sjasone
21371088Sjasone  static void enable_stack_reserved_zone(JavaThread* thread);
21471088Sjasone  static frame look_for_reserved_stack_annotated_method(JavaThread* thread, frame fr);
21571088Sjasone
216126326Sjhb  // Shared stub locations
21783366Sjulian  static address get_poll_stub(address pc);
21871088Sjasone
21971088Sjasone  static address get_ic_miss_stub() {
22071088Sjasone    assert(_ic_miss_blob!= NULL, "oops");
22183366Sjulian    return _ic_miss_blob->entry_point();
22271088Sjasone  }
22371088Sjasone
22497995Sjhb  static address get_handle_wrong_method_stub() {
22597995Sjhb    assert(_wrong_method_blob!= NULL, "oops");
22671088Sjasone    return _wrong_method_blob->entry_point();
22783366Sjulian  }
228111883Sjhb
229111883Sjhb  static address get_handle_wrong_method_abstract_stub() {
23074912Sjhb    assert(_wrong_method_abstract_blob!= NULL, "oops");
23171088Sjasone    return _wrong_method_abstract_blob->entry_point();
23271088Sjasone  }
23371088Sjasone
23471088Sjasone#ifdef COMPILER2
23571088Sjasone  static void generate_uncommon_trap_blob(void);
23683366Sjulian  static UncommonTrapBlob* uncommon_trap_blob()                  { return _uncommon_trap_blob; }
23771088Sjasone#endif // COMPILER2
23871088Sjasone
23971088Sjasone  static address get_resolve_opt_virtual_call_stub() {
24071088Sjasone    assert(_resolve_opt_virtual_call_blob != NULL, "oops");
24195322Shsu    return _resolve_opt_virtual_call_blob->entry_point();
242126326Sjhb  }
24395322Shsu  static address get_resolve_virtual_call_stub() {
24488900Sjhb    assert(_resolve_virtual_call_blob != NULL, "oops");
24588900Sjhb    return _resolve_virtual_call_blob->entry_point();
24671088Sjasone  }
247126326Sjhb  static address get_resolve_static_call_stub() {
248126885Sjhb    assert(_resolve_static_call_blob != NULL, "oops");
249126326Sjhb    return _resolve_static_call_blob->entry_point();
25071088Sjasone  }
25171088Sjasone
25297995Sjhb  static SafepointBlob* polling_page_return_handler_blob()     { return _polling_page_return_handler_blob; }
25397995Sjhb  static SafepointBlob* polling_page_safepoint_handler_blob()  { return _polling_page_safepoint_handler_blob; }
25471088Sjasone  static SafepointBlob* polling_page_vectors_safepoint_handler_blob()  { return _polling_page_vectors_safepoint_handler_blob; }
25571088Sjasone
25672200Sbmilekic  // Counters
25774912Sjhb#ifndef PRODUCT
25871088Sjasone  static address nof_megamorphic_calls_addr() { return (address)&_nof_megamorphic_calls; }
25971088Sjasone#endif // PRODUCT
26071088Sjasone
26171088Sjasone  // Helper routine for full-speed JVMTI exception throwing support
26271088Sjasone  static void throw_and_post_jvmti_exception(JavaThread *thread, Handle h_exception);
26371088Sjasone  static void throw_and_post_jvmti_exception(JavaThread *thread, Symbol* name, const char *message = NULL);
26483366Sjulian
26571088Sjasone  // RedefineClasses() tracing support for obsolete method entry
26671088Sjasone  static int rc_trace_method_entry(JavaThread* thread, Method* m);
26771088Sjasone
26871088Sjasone  // To be used as the entry point for unresolved native methods.
26971088Sjasone  static address native_method_throw_unsatisfied_link_error_entry();
27071088Sjasone  static address native_method_throw_unsupported_operation_exception_entry();
271126326Sjhb
27283366Sjulian  // bytecode tracing is only used by the TraceBytecodes
27383650Sjhb  static intptr_t trace_bytecode(JavaThread* thread, intptr_t preserve_this_value, intptr_t tos, intptr_t tos2) PRODUCT_RETURN0;
27471088Sjasone
27571088Sjasone  static oop retrieve_receiver(Symbol* sig, frame caller);
27671088Sjasone
27771088Sjasone  static void register_finalizer(JavaThread* thread, oopDesc* obj);
27883366Sjulian
27983650Sjhb  // dtrace notifications
28071088Sjasone  static int dtrace_object_alloc(oopDesc* o, int size);
28171088Sjasone  static int dtrace_object_alloc_base(Thread* thread, oopDesc* o, int size);
28297995Sjhb  static int dtrace_method_entry(JavaThread* thread, Method* m);
28397995Sjhb  static int dtrace_method_exit(JavaThread* thread, Method* m);
28471088Sjasone
28583366Sjulian  // Utility method for retrieving the Java thread id, returns 0 if the
286111883Sjhb  // thread is not a well formed Java thread.
287111883Sjhb  static jlong get_java_tid(Thread* thread);
28874912Sjhb
28971088Sjasone
29071088Sjasone  // used by native wrappers to reenable yellow if overflow happened in native code
29171088Sjasone  static void reguard_yellow_pages();
29271088Sjasone
29371088Sjasone  // Fill in the "X cannot be cast to a Y" message for ClassCastException
29483366Sjulian  //
29571088Sjasone  // @param thr the current thread
29671088Sjasone  // @param name the name of the class of the object attempted to be cast
29771088Sjasone  // @return the dynamically allocated exception message (must be freed
29871088Sjasone  // by the caller using a resource mark)
29995322Shsu  //
300126326Sjhb  // BCP must refer to the current 'checkcast' opcode for the frame
30195322Shsu  // on top of the stack.
30288900Sjhb  // The caller (or one of it's callers) must use a ResourceMark
30388900Sjhb  // in order to correctly free the result.
30471088Sjasone  //
305126326Sjhb  static char* generate_class_cast_message(JavaThread* thr, const char* name);
306126885Sjhb
307126326Sjhb  // Fill in the "X cannot be cast to a Y" message for ClassCastException
308126326Sjhb  //
309126326Sjhb  // @param name the name of the class of the object attempted to be cast
310126326Sjhb  // @param klass the name of the target klass attempt
311126326Sjhb  // @param gripe the specific kind of problem being reported
312126326Sjhb  // @return the dynamically allocated exception message (must be freed
313126326Sjhb  // by the caller using a resource mark)
314126326Sjhb  //
31571088Sjasone  // This version does not require access the frame, so it can be called
316126326Sjhb  // from interpreted code
31783650Sjhb  // The caller (or one of it's callers) must use a ResourceMark
31899072Sjulian  // in order to correctly free the result.
31999072Sjulian  //
320113625Sjhb  static char* generate_class_cast_message(const char* name, const char* klass,
32199072Sjulian                                           const char* gripe = " cannot be cast to ");
32271088Sjasone
32397995Sjhb  // Resolves a call site- may patch in the destination of the call into the
32497995Sjhb  // compiled code.
32571088Sjasone  static methodHandle resolve_helper(JavaThread *thread,
32697995Sjhb                                     bool is_virtual,
32772200Sbmilekic                                     bool is_optimized, TRAPS);
32874912Sjhb
32971088Sjasone private:
33071088Sjasone  // deopt blob
33171088Sjasone  static void generate_deopt_blob(void);
33271088Sjasone
33371088Sjasone public:
33483366Sjulian  static DeoptimizationBlob* deopt_blob(void)      { return _deopt_blob; }
33571088Sjasone
33683366Sjulian  // Resets a call-site in compiled code so it will get resolved again.
33771088Sjasone  static methodHandle reresolve_call_site(JavaThread *thread, TRAPS);
33871088Sjasone
33971088Sjasone  // In the code prolog, if the klass comparison fails, the inline cache
34071088Sjasone  // misses and the call site is patched to megamorphic
34171088Sjasone  static methodHandle handle_ic_miss_helper(JavaThread* thread, TRAPS);
34271088Sjasone
34371088Sjasone  // Find the method that called us.
344126326Sjhb  static methodHandle find_callee_method(JavaThread* thread, TRAPS);
34571088Sjasone
34671088Sjasone
34771088Sjasone private:
34883366Sjulian  static Handle find_callee_info(JavaThread* thread,
34971088Sjasone                                 Bytecodes::Code& bc,
35071088Sjasone                                 CallInfo& callinfo, TRAPS);
35171088Sjasone  static Handle find_callee_info_helper(JavaThread* thread,
352122352Stanimura                                        vframeStream& vfst,
35371088Sjasone                                        Bytecodes::Code& bc,
35471088Sjasone                                        CallInfo& callinfo, TRAPS);
355126326Sjhb
35671088Sjasone  static address clean_virtual_call_entry();
357  static address clean_opt_virtual_call_entry();
358  static address clean_static_call_entry();
359
360 public:
361
362  // Read the array of BasicTypes from a Java signature, and compute where
363  // compiled Java code would like to put the results.  Values in reg_lo and
364  // reg_hi refer to 4-byte quantities.  Values less than SharedInfo::stack0 are
365  // registers, those above refer to 4-byte stack slots.  All stack slots are
366  // based off of the window top.  SharedInfo::stack0 refers to the first usable
367  // slot in the bottom of the frame. SharedInfo::stack0+1 refers to the memory word
368  // 4-bytes higher. So for sparc because the register window save area is at
369  // the bottom of the frame the first 16 words will be skipped and SharedInfo::stack0
370  // will be just above it. (
371  // return value is the maximum number of VMReg stack slots the convention will use.
372  static int java_calling_convention(const BasicType* sig_bt, VMRegPair* regs, int total_args_passed, int is_outgoing);
373
374  static void check_member_name_argument_is_last_argument(const methodHandle& method,
375                                                          const BasicType* sig_bt,
376                                                          const VMRegPair* regs) NOT_DEBUG_RETURN;
377
378  // Ditto except for calling C
379  //
380  // C argument in register AND stack slot.
381  // Some architectures require that an argument must be passed in a register
382  // AND in a stack slot. These architectures provide a second VMRegPair array
383  // to be filled by the c_calling_convention method. On other architectures,
384  // NULL is being passed as the second VMRegPair array, so arguments are either
385  // passed in a register OR in a stack slot.
386  static int c_calling_convention(const BasicType *sig_bt, VMRegPair *regs, VMRegPair *regs2,
387                                  int total_args_passed);
388
389  // Compute the new number of arguments in the signature if 32 bit ints
390  // must be converted to longs. Needed if CCallingConventionRequiresIntsAsLongs
391  // is true.
392  static int  convert_ints_to_longints_argcnt(int in_args_count, BasicType* in_sig_bt);
393  // Adapt a method's signature if it contains 32 bit integers that must
394  // be converted to longs. Needed if CCallingConventionRequiresIntsAsLongs
395  // is true.
396  static void convert_ints_to_longints(int i2l_argcnt, int& in_args_count,
397                                       BasicType*& in_sig_bt, VMRegPair*& in_regs);
398
399  // Generate I2C and C2I adapters. These adapters are simple argument marshalling
400  // blobs. Unlike adapters in the tiger and earlier releases the code in these
401  // blobs does not create a new frame and are therefore virtually invisible
402  // to the stack walking code. In general these blobs extend the callers stack
403  // as needed for the conversion of argument locations.
404
405  // When calling a c2i blob the code will always call the interpreter even if
406  // by the time we reach the blob there is compiled code available. This allows
407  // the blob to pass the incoming stack pointer (the sender sp) in a known
408  // location for the interpreter to record. This is used by the frame code
409  // to correct the sender code to match up with the stack pointer when the
410  // thread left the compiled code. In addition it allows the interpreter
411  // to remove the space the c2i adapter allocated to do its argument conversion.
412
413  // Although a c2i blob will always run interpreted even if compiled code is
414  // present if we see that compiled code is present the compiled call site
415  // will be patched/re-resolved so that later calls will run compiled.
416
417  // Additionally a c2i blob need to have a unverified entry because it can be reached
418  // in situations where the call site is an inlined cache site and may go megamorphic.
419
420  // A i2c adapter is simpler than the c2i adapter. This is because it is assumed
421  // that the interpreter before it does any call dispatch will record the current
422  // stack pointer in the interpreter frame. On return it will restore the stack
423  // pointer as needed. This means the i2c adapter code doesn't need any special
424  // handshaking path with compiled code to keep the stack walking correct.
425
426  static AdapterHandlerEntry* generate_i2c2i_adapters(MacroAssembler *_masm,
427                                                      int total_args_passed,
428                                                      int max_arg,
429                                                      const BasicType *sig_bt,
430                                                      const VMRegPair *regs,
431                                                      AdapterFingerPrint* fingerprint);
432
433  static void gen_i2c_adapter(MacroAssembler *_masm,
434                              int total_args_passed,
435                              int comp_args_on_stack,
436                              const BasicType *sig_bt,
437                              const VMRegPair *regs);
438
439  // OSR support
440
441  // OSR_migration_begin will extract the jvm state from an interpreter
442  // frame (locals, monitors) and store the data in a piece of C heap
443  // storage. This then allows the interpreter frame to be removed from the
444  // stack and the OSR nmethod to be called. That method is called with a
445  // pointer to the C heap storage. This pointer is the return value from
446  // OSR_migration_begin.
447
448  static intptr_t* OSR_migration_begin(JavaThread *thread);
449
450  // OSR_migration_end is a trivial routine. It is called after the compiled
451  // method has extracted the jvm state from the C heap that OSR_migration_begin
452  // created. It's entire job is to simply free this storage.
453  static void OSR_migration_end(intptr_t* buf);
454
455  // Convert a sig into a calling convention register layout
456  // and find interesting things about it.
457  static VMRegPair* find_callee_arguments(Symbol* sig, bool has_receiver, bool has_appendix, int *arg_size);
458  static VMReg name_for_receiver();
459
460  // "Top of Stack" slots that may be unused by the calling convention but must
461  // otherwise be preserved.
462  // On Intel these are not necessary and the value can be zero.
463  // On Sparc this describes the words reserved for storing a register window
464  // when an interrupt occurs.
465  static uint out_preserve_stack_slots();
466
467  // Is vector's size (in bytes) bigger than a size saved by default?
468  // For example, on x86 16 bytes XMM registers are saved by default.
469  static bool is_wide_vector(int size);
470
471  // Save and restore a native result
472  static void    save_native_result(MacroAssembler *_masm, BasicType ret_type, int frame_slots);
473  static void restore_native_result(MacroAssembler *_masm, BasicType ret_type, int frame_slots);
474
475  // Generate a native wrapper for a given method.  The method takes arguments
476  // in the Java compiled code convention, marshals them to the native
477  // convention (handlizes oops, etc), transitions to native, makes the call,
478  // returns to java state (possibly blocking), unhandlizes any result and
479  // returns.
480  //
481  // The wrapper may contain special-case code if the given method
482  // is a JNI critical method, or a compiled method handle adapter,
483  // such as _invokeBasic, _linkToVirtual, etc.
484  static nmethod* generate_native_wrapper(MacroAssembler* masm,
485                                          const methodHandle& method,
486                                          int compile_id,
487                                          BasicType* sig_bt,
488                                          VMRegPair* regs,
489                                          BasicType ret_type);
490
491  // Block before entering a JNI critical method
492  static void block_for_jni_critical(JavaThread* thread);
493
494  // A compiled caller has just called the interpreter, but compiled code
495  // exists.  Patch the caller so he no longer calls into the interpreter.
496  static void fixup_callers_callsite(Method* moop, address ret_pc);
497  static bool should_fixup_call_destination(address destination, address entry_point, address caller_pc, Method* moop, CodeBlob* cb);
498
499  // Slow-path Locking and Unlocking
500  static void complete_monitor_locking_C(oopDesc* obj, BasicLock* lock, JavaThread* thread);
501  static void complete_monitor_unlocking_C(oopDesc* obj, BasicLock* lock, JavaThread* thread);
502
503  // Resolving of calls
504  static address resolve_static_call_C     (JavaThread *thread);
505  static address resolve_virtual_call_C    (JavaThread *thread);
506  static address resolve_opt_virtual_call_C(JavaThread *thread);
507
508  // arraycopy, the non-leaf version.  (See StubRoutines for all the leaf calls.)
509  static void slow_arraycopy_C(oopDesc* src,  jint src_pos,
510                               oopDesc* dest, jint dest_pos,
511                               jint length, JavaThread* thread);
512
513  // handle ic miss with caller being compiled code
514  // wrong method handling (inline cache misses, zombie methods)
515  static address handle_wrong_method(JavaThread* thread);
516  static address handle_wrong_method_abstract(JavaThread* thread);
517  static address handle_wrong_method_ic_miss(JavaThread* thread);
518
519#ifndef PRODUCT
520
521  // Collect and print inline cache miss statistics
522 private:
523  enum { maxICmiss_count = 100 };
524  static int     _ICmiss_index;                  // length of IC miss histogram
525  static int     _ICmiss_count[maxICmiss_count]; // miss counts
526  static address _ICmiss_at[maxICmiss_count];    // miss addresses
527  static void trace_ic_miss(address at);
528
529 public:
530  static int _throw_null_ctr;                    // throwing a null-pointer exception
531  static int _ic_miss_ctr;                       // total # of IC misses
532  static int _wrong_method_ctr;
533  static int _resolve_static_ctr;
534  static int _resolve_virtual_ctr;
535  static int _resolve_opt_virtual_ctr;
536  static int _implicit_null_throws;
537  static int _implicit_div0_throws;
538
539  static int _jbyte_array_copy_ctr;        // Slow-path byte array copy
540  static int _jshort_array_copy_ctr;       // Slow-path short array copy
541  static int _jint_array_copy_ctr;         // Slow-path int array copy
542  static int _jlong_array_copy_ctr;        // Slow-path long array copy
543  static int _oop_array_copy_ctr;          // Slow-path oop array copy
544  static int _checkcast_array_copy_ctr;    // Slow-path oop array copy, with cast
545  static int _unsafe_array_copy_ctr;       // Slow-path includes alignment checks
546  static int _generic_array_copy_ctr;      // Slow-path includes type decoding
547  static int _slow_array_copy_ctr;         // Slow-path failed out to a method call
548
549  static int _new_instance_ctr;            // 'new' object requires GC
550  static int _new_array_ctr;               // 'new' array requires GC
551  static int _multi1_ctr, _multi2_ctr, _multi3_ctr, _multi4_ctr, _multi5_ctr;
552  static int _find_handler_ctr;            // find exception handler
553  static int _rethrow_ctr;                 // rethrow exception
554  static int _mon_enter_stub_ctr;          // monitor enter stub
555  static int _mon_exit_stub_ctr;           // monitor exit stub
556  static int _mon_enter_ctr;               // monitor enter slow
557  static int _mon_exit_ctr;                // monitor exit slow
558  static int _partial_subtype_ctr;         // SubRoutines::partial_subtype_check
559
560  // Statistics code
561  // stats for "normal" compiled calls (non-interface)
562  static int     _nof_normal_calls;              // total # of calls
563  static int     _nof_optimized_calls;           // total # of statically-bound calls
564  static int     _nof_inlined_calls;             // total # of inlined normal calls
565  static int     _nof_static_calls;              // total # of calls to static methods or super methods (invokespecial)
566  static int     _nof_inlined_static_calls;      // total # of inlined static calls
567  // stats for compiled interface calls
568  static int     _nof_interface_calls;           // total # of compiled calls
569  static int     _nof_optimized_interface_calls; // total # of statically-bound interface calls
570  static int     _nof_inlined_interface_calls;   // total # of inlined interface calls
571  static int     _nof_megamorphic_interface_calls;// total # of megamorphic interface calls
572  // stats for runtime exceptions
573  static int     _nof_removable_exceptions;      // total # of exceptions that could be replaced by branches due to inlining
574
575 public: // for compiler
576  static address nof_normal_calls_addr()                { return (address)&_nof_normal_calls; }
577  static address nof_optimized_calls_addr()             { return (address)&_nof_optimized_calls; }
578  static address nof_inlined_calls_addr()               { return (address)&_nof_inlined_calls; }
579  static address nof_static_calls_addr()                { return (address)&_nof_static_calls; }
580  static address nof_inlined_static_calls_addr()        { return (address)&_nof_inlined_static_calls; }
581  static address nof_interface_calls_addr()             { return (address)&_nof_interface_calls; }
582  static address nof_optimized_interface_calls_addr()   { return (address)&_nof_optimized_interface_calls; }
583  static address nof_inlined_interface_calls_addr()     { return (address)&_nof_inlined_interface_calls; }
584  static address nof_megamorphic_interface_calls_addr() { return (address)&_nof_megamorphic_interface_calls; }
585  static void print_call_statistics(int comp_total);
586  static void print_statistics();
587  static void print_ic_miss_histogram();
588
589#endif // PRODUCT
590};
591
592
593// ---------------------------------------------------------------------------
594// Implementation of AdapterHandlerLibrary
595//
596// This library manages argument marshaling adapters and native wrappers.
597// There are 2 flavors of adapters: I2C and C2I.
598//
599// The I2C flavor takes a stock interpreted call setup, marshals the
600// arguments for a Java-compiled call, and jumps to Rmethod-> code()->
601// code_begin().  It is broken to call it without an nmethod assigned.
602// The usual behavior is to lift any register arguments up out of the
603// stack and possibly re-pack the extra arguments to be contiguous.
604// I2C adapters will save what the interpreter's stack pointer will be
605// after arguments are popped, then adjust the interpreter's frame
606// size to force alignment and possibly to repack the arguments.
607// After re-packing, it jumps to the compiled code start.  There are
608// no safepoints in this adapter code and a GC cannot happen while
609// marshaling is in progress.
610//
611// The C2I flavor takes a stock compiled call setup plus the target method in
612// Rmethod, marshals the arguments for an interpreted call and jumps to
613// Rmethod->_i2i_entry.  On entry, the interpreted frame has not yet been
614// setup.  Compiled frames are fixed-size and the args are likely not in the
615// right place.  Hence all the args will likely be copied into the
616// interpreter's frame, forcing that frame to grow.  The compiled frame's
617// outgoing stack args will be dead after the copy.
618//
619// Native wrappers, like adapters, marshal arguments.  Unlike adapters they
620// also perform an official frame push & pop.  They have a call to the native
621// routine in their middles and end in a return (instead of ending in a jump).
622// The native wrappers are stored in real nmethods instead of the BufferBlobs
623// used by the adapters.  The code generation happens here because it's very
624// similar to what the adapters have to do.
625
626class AdapterHandlerEntry : public BasicHashtableEntry<mtCode> {
627  friend class AdapterHandlerTable;
628
629 private:
630  AdapterFingerPrint* _fingerprint;
631  address _i2c_entry;
632  address _c2i_entry;
633  address _c2i_unverified_entry;
634
635#ifdef ASSERT
636  // Captures code and signature used to generate this adapter when
637  // verifying adapter equivalence.
638  unsigned char* _saved_code;
639  int            _saved_code_length;
640#endif
641
642  void init(AdapterFingerPrint* fingerprint, address i2c_entry, address c2i_entry, address c2i_unverified_entry) {
643    _fingerprint = fingerprint;
644    _i2c_entry = i2c_entry;
645    _c2i_entry = c2i_entry;
646    _c2i_unverified_entry = c2i_unverified_entry;
647#ifdef ASSERT
648    _saved_code = NULL;
649    _saved_code_length = 0;
650#endif
651  }
652
653  void deallocate();
654
655  // should never be used
656  AdapterHandlerEntry();
657
658 public:
659  address get_i2c_entry()            const { return _i2c_entry; }
660  address get_c2i_entry()            const { return _c2i_entry; }
661  address get_c2i_unverified_entry() const { return _c2i_unverified_entry; }
662  address base_address();
663  void relocate(address new_base);
664
665  AdapterFingerPrint* fingerprint() const { return _fingerprint; }
666
667  AdapterHandlerEntry* next() {
668    return (AdapterHandlerEntry*)BasicHashtableEntry<mtCode>::next();
669  }
670
671#ifdef ASSERT
672  // Used to verify that code generated for shared adapters is equivalent
673  void save_code   (unsigned char* code, int length);
674  bool compare_code(unsigned char* code, int length);
675#endif
676
677  //virtual void print_on(outputStream* st) const;  DO NOT USE
678  void print_adapter_on(outputStream* st) const;
679};
680
681class AdapterHandlerLibrary: public AllStatic {
682 private:
683  static BufferBlob* _buffer; // the temporary code buffer in CodeCache
684  static AdapterHandlerTable* _adapters;
685  static AdapterHandlerEntry* _abstract_method_handler;
686  static BufferBlob* buffer_blob();
687  static void initialize();
688
689 public:
690
691  static AdapterHandlerEntry* new_entry(AdapterFingerPrint* fingerprint,
692                                        address i2c_entry, address c2i_entry, address c2i_unverified_entry);
693  static void create_native_wrapper(const methodHandle& method);
694  static AdapterHandlerEntry* get_adapter(const methodHandle& method);
695
696  static void print_handler(const CodeBlob* b) { print_handler_on(tty, b); }
697  static void print_handler_on(outputStream* st, const CodeBlob* b);
698  static bool contains(const CodeBlob* b);
699#ifndef PRODUCT
700  static void print_statistics();
701#endif // PRODUCT
702
703};
704
705#endif // SHARE_VM_RUNTIME_SHAREDRUNTIME_HPP
706