/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "interpreter/bytecodeHistogram.hpp"
#include "interpreter/bytecodeTracer.hpp"
#include "interpreter/linkResolver.hpp"
#include "memory/allocation.hpp"
#include "memory/resourceArea.hpp"
#include "runtime/threadLocalStorage.hpp"
#include "utilities/hashtable.hpp"
class AdapterHandlerEntry;
class AdapterHandlerTable;
class AdapterFingerPrint;
class vframeStream;
// Runtime is the base class for various runtime interfaces
// (InterpreterRuntime, CompilerRuntime, etc.). It provides
// shared functionality such as exception forwarding (C++ to
// information, etc.
friend class VMStructs;
private:
bool is_virtual,
bool is_optimized, TRAPS);
// Shared stub locations
#ifdef COMPILER2
#endif // COMPILER2
#ifndef PRODUCT
// Counters
#endif // !PRODUCT
private:
public:
static void generate_stubs(void);
// max bytes for each dtrace string parameter
// The following arithmetic routines are used on platforms that do
// not have machine instructions to implement their functionality.
// Do not remove these.
// long arithmetics
// float and double remainder
#ifdef __SOFTFP__
#endif // __SOFTFP__
// float conversion (needs to set appropriate rounding mode)
#ifdef __SOFTFP__
#endif // __SOFTFP__
// double trigonometrics and transcendentals
#if defined(__SOFTFP__) || defined(E500V2)
static double dabs(double f);
#endif
#if defined(__SOFTFP__) || defined(PPC)
static double dsqrt(double f);
#endif
#ifdef __SOFTFP__
// C++ compiler generates soft float instructions as well as passing
// float and double in registers.
static int fcmpl(float x, float y);
static int fcmpg(float x, float y);
static int dcmpl(double x, double y);
static int dcmpg(double x, double y);
static int unordered_fcmplt(float x, float y);
static int unordered_dcmplt(double x, double y);
static int unordered_fcmple(float x, float y);
static int unordered_dcmple(double x, double y);
static int unordered_fcmpge(float x, float y);
static int unordered_dcmpge(double x, double y);
static int unordered_fcmpgt(float x, float y);
static int unordered_dcmpgt(double x, double y);
static float fneg(float f);
static double dneg(double f);
#endif
// exception handling across interpreter/compiler boundaries
static address raw_exception_handler_for_return_address(JavaThread* thread, address return_address);
#ifndef SERIALGC
// G1 write barriers
#endif // !SERIALGC
// exception handling and implicit exceptions
bool force_unwind, bool top_frame_only);
enum ImplicitExceptionKind {
};
// Shared stub locations
return _ic_miss_blob->entry_point();
}
return _wrong_method_blob->entry_point();
}
#ifdef COMPILER2
static void generate_uncommon_trap_blob(void);
#endif // COMPILER2
return _resolve_opt_virtual_call_blob->entry_point();
}
return _resolve_virtual_call_blob->entry_point();
}
return _resolve_static_call_blob->entry_point();
}
static SafepointBlob* polling_page_return_handler_blob() { return _polling_page_return_handler_blob; }
static SafepointBlob* polling_page_safepoint_handler_blob() { return _polling_page_safepoint_handler_blob; }
static SafepointBlob* polling_page_vectors_safepoint_handler_blob() { return _polling_page_vectors_safepoint_handler_blob; }
// Counters
#ifndef PRODUCT
#endif // PRODUCT
// Helper routine for full-speed JVMTI exception throwing support
static void throw_and_post_jvmti_exception(JavaThread *thread, Symbol* name, const char *message = NULL);
// RedefineClasses() tracing support for obsolete method entry
// To be used as the entry point for unresolved native methods.
// bytecode tracing is only used by the TraceBytecodes
static intptr_t trace_bytecode(JavaThread* thread, intptr_t preserve_this_value, intptr_t tos, intptr_t tos2) PRODUCT_RETURN0;
// Used to back off a spin lock that is under heavy contention
// dtrace notifications
static int dtrace_object_alloc(oopDesc* o);
// Utility method for retrieving the Java thread id, returns 0 if the
// thread is not a well formed Java thread.
// used by native wrappers to reenable yellow if overflow happened in native code
static void reguard_yellow_pages();
/**
* Fill in the "X cannot be cast to a Y" message for ClassCastException
*
* @param thr the current thread
* @param name the name of the class of the object attempted to be cast
* @return the dynamically allocated exception message (must be freed
* by the caller using a resource mark)
*
* BCP must refer to the current 'checkcast' opcode for the frame
* on top of the stack.
* The caller (or one of it's callers) must use a ResourceMark
* in order to correctly free the result.
*/
/**
* Fill in the "X cannot be cast to a Y" message for ClassCastException
*
* @param name the name of the class of the object attempted to be cast
* @param klass the name of the target klass attempt
* @param gripe the specific kind of problem being reported
* @return the dynamically allocated exception message (must be freed
* by the caller using a resource mark)
*
* This version does not require access the frame, so it can be called
* from interpreted code
* The caller (or one of it's callers) must use a ResourceMark
* in order to correctly free the result.
*/
const char* gripe = " cannot be cast to ");
// Resolves a call site- may patch in the destination of the call into the
// compiled code.
bool is_virtual,
bool is_optimized, TRAPS);
private:
// deopt blob
static void generate_deopt_blob(void);
public:
// Resets a call-site in compiled code so it will get resolved again.
// In the code prolog, if the klass comparison fails, the inline cache
// misses and the call site is patched to megamorphic
// Find the method that called us.
private:
static address clean_virtual_call_entry();
static address clean_opt_virtual_call_entry();
static address clean_static_call_entry();
public:
// Read the array of BasicTypes from a Java signature, and compute where
// compiled Java code would like to put the results. Values in reg_lo and
// reg_hi refer to 4-byte quantities. Values less than SharedInfo::stack0 are
// registers, those above refer to 4-byte stack slots. All stack slots are
// based off of the window top. SharedInfo::stack0 refers to the first usable
// slot in the bottom of the frame. SharedInfo::stack0+1 refers to the memory word
// 4-bytes higher. So for sparc because the register window save area is at
// the bottom of the frame the first 16 words will be skipped and SharedInfo::stack0
// will be just above it. (
// return value is the maximum number of VMReg stack slots the convention will use.
static int java_calling_convention(const BasicType* sig_bt, VMRegPair* regs, int total_args_passed, int is_outgoing);
// Ditto except for calling C
// Generate I2C and C2I adapters. These adapters are simple argument marshalling
// blobs. Unlike adapters in the tiger and earlier releases the code in these
// blobs does not create a new frame and are therefore virtually invisible
// to the stack walking code. In general these blobs extend the callers stack
// as needed for the conversion of argument locations.
// When calling a c2i blob the code will always call the interpreter even if
// by the time we reach the blob there is compiled code available. This allows
// the blob to pass the incoming stack pointer (the sender sp) in a known
// location for the interpreter to record. This is used by the frame code
// to correct the sender code to match up with the stack pointer when the
// thread left the compiled code. In addition it allows the interpreter
// to remove the space the c2i adapter allocated to do it argument conversion.
// Although a c2i blob will always run interpreted even if compiled code is
// present if we see that compiled code is present the compiled call site
// will be patched/re-resolved so that later calls will run compiled.
// Aditionally a c2i blob need to have a unverified entry because it can be reached
// in situations where the call site is an inlined cache site and may go megamorphic.
// A i2c adapter is simpler than the c2i adapter. This is because it is assumed
// that the interpreter before it does any call dispatch will record the current
// stack pointer in the interpreter frame. On return it will restore the stack
// pointer as needed. This means the i2c adapter code doesn't need any special
// handshaking path with compiled code to keep the stack walking correct.
int total_args_passed,
int max_arg,
// OSR support
// OSR_migration_begin will extract the jvm state from an interpreter
// frame (locals, monitors) and store the data in a piece of C heap
// storage. This then allows the interpreter frame to be removed from the
// stack and the OSR nmethod to be called. That method is called with a
// pointer to the C heap storage. This pointer is the return value from
// OSR_migration_begin.
// OSR_migration_end is a trivial routine. It is called after the compiled
// method has extracted the jvm state from the C heap that OSR_migration_begin
// created. It's entire job is to simply free this storage.
// Convert a sig into a calling convention register layout
// and find interesting things about it.
static VMReg name_for_receiver();
// "Top of Stack" slots that may be unused by the calling convention but must
// otherwise be preserved.
// On Intel these are not necessary and the value can be zero.
// On Sparc this describes the words reserved for storing a register window
// when an interrupt occurs.
static uint out_preserve_stack_slots();
// Is vector's size (in bytes) bigger than a size saved by default?
// For example, on x86 16 bytes XMM registers are saved by default.
static bool is_wide_vector(int size);
// Save and restore a native result
// Generate a native wrapper for a given method. The method takes arguments
// in the Java compiled code convention, marshals them to the native
// convention (handlizes oops, etc), transitions to native, makes the call,
// returns to java state (possibly blocking), unhandlizes any result and
// returns.
//
// The wrapper may contain special-case code if the given method
// is a JNI critical method, or a compiled method handle adapter,
// such as _invokeBasic, _linkToVirtual, etc.
int compile_id,
// Block before entering a JNI critical method
#ifdef HAVE_DTRACE_H
// Generate a dtrace wrapper for a given method. The method takes arguments
// in the Java compiled code convention, marshals them to the native
// convention (handlizes oops, etc), transitions to native, makes the call,
// returns to java state (possibly blocking), unhandlizes any result and
// returns.
// dtrace support to convert a Java string to utf8
#endif // def HAVE_DTRACE_H
// A compiled caller has just called the interpreter, but compiled code
// exists. Patch the caller so he no longer calls into the interpreter.
// Slow-path Locking and Unlocking
// Resolving of calls
// arraycopy, the non-leaf version. (See StubRoutines for all the leaf calls.)
// handle ic miss with caller being compiled code
// wrong method handling (inline cache misses, zombie methods)
#ifndef PRODUCT
// Collect and print inline cache miss statistics
private:
public:
static int _wrong_method_ctr;
static int _resolve_static_ctr;
static int _resolve_virtual_ctr;
static int _resolve_opt_virtual_ctr;
static int _implicit_null_throws;
static int _implicit_div0_throws;
// Statistics code
// stats for "normal" compiled calls (non-interface)
static int _nof_static_calls; // total # of calls to static methods or super methods (invokespecial)
// stats for compiled interface calls
// stats for runtime exceptions
static int _nof_removable_exceptions; // total # of exceptions that could be replaced by branches due to inlining
public: // for compiler
static address nof_optimized_interface_calls_addr() { return (address)&_nof_optimized_interface_calls; }
static address nof_inlined_interface_calls_addr() { return (address)&_nof_inlined_interface_calls; }
static address nof_megamorphic_interface_calls_addr() { return (address)&_nof_megamorphic_interface_calls; }
static void print_call_statistics(int comp_total);
static void print_statistics();
static void print_ic_miss_histogram();
#endif // PRODUCT
};
// ---------------------------------------------------------------------------
// Implementation of AdapterHandlerLibrary
//
// This library manages argument marshaling adapters and native wrappers.
// There are 2 flavors of adapters: I2C and C2I.
//
// The I2C flavor takes a stock interpreted call setup, marshals the
// arguments for a Java-compiled call, and jumps to Rmethod-> code()->
// code_begin(). It is broken to call it without an nmethod assigned.
// The usual behavior is to lift any register arguments up out of the
// stack and possibly re-pack the extra arguments to be contigious.
// I2C adapters will save what the interpreter's stack pointer will be
// after arguments are popped, then adjust the interpreter's frame
// size to force alignment and possibly to repack the arguments.
// After re-packing, it jumps to the compiled code start. There are
// no safepoints in this adapter code and a GC cannot happen while
// marshaling is in progress.
//
// The C2I flavor takes a stock compiled call setup plus the target method in
// Rmethod, marshals the arguments for an interpreted call and jumps to
// Rmethod->_i2i_entry. On entry, the interpreted frame has not yet been
// setup. Compiled frames are fixed-size and the args are likely not in the
// right place. Hence all the args will likely be copied into the
// interpreter's frame, forcing that frame to grow. The compiled frame's
// outgoing stack args will be dead after the copy.
//
// Native wrappers, like adapters, marshal arguments. Unlike adapters they
// also perform an offical frame push & pop. They have a call to the native
// routine in their middles and end in a return (instead of ending in a jump).
// The native wrappers are stored in real nmethods instead of the BufferBlobs
// used by the adapters. The code generation happens here because it's very
// similar to what the adapters have to do.
friend class AdapterHandlerTable;
private:
#ifdef ASSERT
// Captures code and signature used to generate this adapter when
// verifing adapter equivalence.
unsigned char* _saved_code;
int _code_length;
int _total_args_passed;
#endif
void init(AdapterFingerPrint* fingerprint, address i2c_entry, address c2i_entry, address c2i_unverified_entry) {
#ifdef ASSERT
_saved_code = NULL;
_code_length = 0;
_saved_sig = NULL;
_total_args_passed = 0;
#endif
}
void deallocate();
// should never be used
public:
}
#ifdef ASSERT
// Used to verify that code generated for shared adapters is equivalent
#endif
//virtual void print_on(outputStream* st) const; DO NOT USE
};
private:
static BufferBlob* buffer_blob();
static void initialize();
public:
#ifdef HAVE_DTRACE_H
#endif // HAVE_DTRACE_H
#ifndef PRODUCT
static void print_statistics();
#endif /* PRODUCT */
};
#endif // SHARE_VM_RUNTIME_SHAREDRUNTIME_HPP