8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #ifndef SHARE_RUNTIME_SHAREDRUNTIME_HPP
26 #define SHARE_RUNTIME_SHAREDRUNTIME_HPP
27
28 #include "classfile/compactHashtable.hpp"
29 #include "code/codeBlob.hpp"
30 #include "code/vmreg.hpp"
31 #include "interpreter/linkResolver.hpp"
32 #include "memory/allStatic.hpp"
33 #include "memory/metaspaceClosure.hpp"
34 #include "memory/resourceArea.hpp"
35 #include "runtime/safepointVerifiers.hpp"
36 #include "runtime/stubInfo.hpp"
37 #include "utilities/macros.hpp"
38
39 class AdapterHandlerEntry;
40 class AdapterFingerPrint;
41 class vframeStream;
42
43 // Runtime is the base class for various runtime interfaces
44 // (InterpreterRuntime, CompilerRuntime, etc.). It provides
45 // shared functionality such as exception forwarding (C++ to
46 // Java exceptions), locking/unlocking mechanisms, statistical
47 // information, etc.
48
49 class SharedRuntime: AllStatic {
50 private:
51 // Declare shared stub fields
52 #define SHARED_STUB_FIELD_DECLARE(name, type) \
53 static type* BLOB_FIELD_NAME(name);
54 SHARED_STUBS_DO(SHARED_STUB_FIELD_DECLARE)
55 #undef SHARED_STUB_FIELD_DECLARE
56
57 #ifdef ASSERT
58 static bool is_resolve_id(StubId id) {
59 return (id == StubId::shared_wrong_method_id ||
60 id == StubId::shared_wrong_method_abstract_id ||
61 id == StubId::shared_ic_miss_id ||
359 // on top of the stack.
360 // The caller (or one of its callers) must use a ResourceMark
361 // in order to correctly free the result.
362 //
363 static char* generate_class_cast_message(JavaThread* thr, Klass* caster_klass);
364
365 // Fill in the "X cannot be cast to a Y" message for ClassCastException
366 //
367 // @param caster_klass the class of the object we are casting
368 // @param target_klass the target klass attempt
369 // @return the dynamically allocated exception message (must be freed
370 // by the caller using a resource mark)
371 //
372 // This version does not require access the frame, so it can be called
373 // from interpreted code
374 // The caller (or one of it's callers) must use a ResourceMark
375 // in order to correctly free the result.
376 //
377 static char* generate_class_cast_message(Klass* caster_klass, Klass* target_klass, Symbol* target_klass_name = nullptr);
378
379 // Resolves a call site- may patch in the destination of the call into the
380 // compiled code.
381 static methodHandle resolve_helper(bool is_virtual, bool is_optimized, TRAPS);
382
383 private:
384 // deopt blob
385 static void generate_deopt_blob(void);
386
387 public:
388 static DeoptimizationBlob* deopt_blob(void) { return _deopt_blob; }
389
390 // Resets a call-site in compiled code so it will get resolved again.
391 static methodHandle reresolve_call_site(TRAPS);
392
393 // In the code prolog, if the klass comparison fails, the inline cache
394 // misses and the call site is patched to megamorphic
395 static methodHandle handle_ic_miss_helper(TRAPS);
396
397 // Find the method that called us.
398 static methodHandle find_callee_method(TRAPS);
399
400 static void monitor_enter_helper(oopDesc* obj, BasicLock* lock, JavaThread* thread);
401
402 static void monitor_exit_helper(oopDesc* obj, BasicLock* lock, JavaThread* current);
403
404 private:
405 static Handle find_callee_info(Bytecodes::Code& bc, CallInfo& callinfo, TRAPS);
406 static Handle find_callee_info_helper(vframeStream& vfst, Bytecodes::Code& bc, CallInfo& callinfo, TRAPS);
407
408 static Method* extract_attached_method(vframeStream& vfst);
409
410 #if defined(X86) && defined(COMPILER1)
411 // For Object.hashCode, System.identityHashCode try to pull hashCode from object header if available.
412 static void inline_check_hashcode_from_object_header(MacroAssembler* masm, const methodHandle& method, Register obj_reg, Register result);
413 #endif // X86 && COMPILER1
414
415 public:
416
417 // Read the array of BasicTypes from a Java signature, and compute where
418 // compiled Java code would like to put the results. Values in reg_lo and
419 // reg_hi refer to 4-byte quantities. Values less than SharedInfo::stack0 are
420 // registers, those above refer to 4-byte stack slots. All stack slots are
421 // based off of the window top. SharedInfo::stack0 refers to the first usable
422 // slot in the bottom of the frame. SharedInfo::stack0+1 refers to the memory word
423 // 4-bytes higher.
424 // return value is the maximum number of VMReg stack slots the convention will use.
425 static int java_calling_convention(const BasicType* sig_bt, VMRegPair* regs, int total_args_passed);
426
427 static void check_member_name_argument_is_last_argument(const methodHandle& method,
428 const BasicType* sig_bt,
429 const VMRegPair* regs) NOT_DEBUG_RETURN;
430
431 // Ditto except for calling C
432 //
433 // C argument in register AND stack slot.
434 // Some architectures require that an argument must be passed in a register
435 // AND in a stack slot. These architectures provide a second VMRegPair array
436 // to be filled by the c_calling_convention method. On other architectures,
437 // null is being passed as the second VMRegPair array, so arguments are either
438 // passed in a register OR in a stack slot.
439 static int c_calling_convention(const BasicType *sig_bt, VMRegPair *regs, int total_args_passed);
440
441 static int vector_calling_convention(VMRegPair *regs,
442 uint num_bits,
443 uint total_args_passed);
444
445 // Generate I2C and C2I adapters. These adapters are simple argument marshalling
452 // by the time we reach the blob there is compiled code available. This allows
453 // the blob to pass the incoming stack pointer (the sender sp) in a known
454 // location for the interpreter to record. This is used by the frame code
455 // to correct the sender code to match up with the stack pointer when the
456 // thread left the compiled code. In addition it allows the interpreter
457 // to remove the space the c2i adapter allocated to do its argument conversion.
458
459 // Although a c2i blob will always run interpreted even if compiled code is
460 // present if we see that compiled code is present the compiled call site
461 // will be patched/re-resolved so that later calls will run compiled.
462
463 // Additionally a c2i blob need to have a unverified entry because it can be reached
464 // in situations where the call site is an inlined cache site and may go megamorphic.
465
466 // A i2c adapter is simpler than the c2i adapter. This is because it is assumed
467 // that the interpreter before it does any call dispatch will record the current
468 // stack pointer in the interpreter frame. On return it will restore the stack
469 // pointer as needed. This means the i2c adapter code doesn't need any special
470 // handshaking path with compiled code to keep the stack walking correct.
471
472 static void generate_i2c2i_adapters(MacroAssembler *_masm,
473 int total_args_passed,
474 int max_arg,
475 const BasicType *sig_bt,
476 const VMRegPair *regs,
477 address entry_address[AdapterBlob::ENTRY_COUNT]);
478
479 static void gen_i2c_adapter(MacroAssembler *_masm,
480 int total_args_passed,
481 int comp_args_on_stack,
482 const BasicType *sig_bt,
483 const VMRegPair *regs);
484
485 // OSR support
486
487 // OSR_migration_begin will extract the jvm state from an interpreter
488 // frame (locals, monitors) and store the data in a piece of C heap
489 // storage. This then allows the interpreter frame to be removed from the
490 // stack and the OSR nmethod to be called. That method is called with a
491 // pointer to the C heap storage. This pointer is the return value from
492 // OSR_migration_begin.
493
494 static intptr_t* OSR_migration_begin(JavaThread *thread);
495
496 // OSR_migration_end is a trivial routine. It is called after the compiled
497 // method has extracted the jvm state from the C heap that OSR_migration_begin
498 // created. It's entire job is to simply free this storage.
499 static void OSR_migration_end(intptr_t* buf);
500
501 // Convert a sig into a calling convention register layout
502 // and find interesting things about it.
534 // returns.
535 //
536 // The wrapper may contain special-case code if the given method
537 // is a compiled method handle adapter, such as _invokeBasic, _linkToVirtual, etc.
538 static nmethod* generate_native_wrapper(MacroAssembler* masm,
539 const methodHandle& method,
540 int compile_id,
541 BasicType* sig_bt,
542 VMRegPair* regs,
543 BasicType ret_type);
544
545 // A compiled caller has just called the interpreter, but compiled code
546 // exists. Patch the caller so he no longer calls into the interpreter.
547 static void fixup_callers_callsite(Method* moop, address ret_pc);
548
549 // Slow-path Locking and Unlocking
550 static void complete_monitor_locking_C(oopDesc* obj, BasicLock* lock, JavaThread* current);
551 static void complete_monitor_unlocking_C(oopDesc* obj, BasicLock* lock, JavaThread* current);
552
553 // Resolving of calls
554 static address get_resolved_entry (JavaThread* current, methodHandle callee_method);
555 static address resolve_static_call_C (JavaThread* current);
556 static address resolve_virtual_call_C (JavaThread* current);
557 static address resolve_opt_virtual_call_C(JavaThread* current);
558
559 // arraycopy, the non-leaf version. (See StubRoutines for all the leaf calls.)
560 static void slow_arraycopy_C(oopDesc* src, jint src_pos,
561 oopDesc* dest, jint dest_pos,
562 jint length, JavaThread* thread);
563
564 // handle ic miss with caller being compiled code
565 // wrong method handling (inline cache misses)
566 static address handle_wrong_method(JavaThread* current);
567 static address handle_wrong_method_abstract(JavaThread* current);
568 static address handle_wrong_method_ic_miss(JavaThread* current);
569
570 static address handle_unsafe_access(JavaThread* thread, address next_pc);
571
572 #ifndef PRODUCT
573
574 // Collect and print inline cache miss statistics
575 private:
576 enum { maxICmiss_count = 100 };
577 static int _ICmiss_index; // length of IC miss histogram
578 static int _ICmiss_count[maxICmiss_count]; // miss counts
579 static address _ICmiss_at[maxICmiss_count]; // miss addresses
580 static void trace_ic_miss(address at);
581
582 public:
583 static uint _ic_miss_ctr; // total # of IC misses
584 static uint _wrong_method_ctr;
585 static uint _resolve_static_ctr;
586 static uint _resolve_virtual_ctr;
587 static uint _resolve_opt_virtual_ctr;
588 static uint _implicit_null_throws;
589 static uint _implicit_div0_throws;
590
591 static uint _jbyte_array_copy_ctr; // Slow-path byte array copy
690 //
691 // The C2I flavor takes a stock compiled call setup plus the target method in
692 // Rmethod, marshals the arguments for an interpreted call and jumps to
693 // Rmethod->_i2i_entry. On entry, the interpreted frame has not yet been
694 // setup. Compiled frames are fixed-size and the args are likely not in the
695 // right place. Hence all the args will likely be copied into the
696 // interpreter's frame, forcing that frame to grow. The compiled frame's
697 // outgoing stack args will be dead after the copy.
698 //
699 // Native wrappers, like adapters, marshal arguments. Unlike adapters they
700 // also perform an official frame push & pop. They have a call to the native
701 // routine in their middles and end in a return (instead of ending in a jump).
702 // The native wrappers are stored in real nmethods instead of the BufferBlobs
703 // used by the adapters. The code generation happens here because it's very
704 // similar to what the adapters have to do.
705
706 class AdapterHandlerEntry : public MetaspaceObj {
707 friend class AdapterHandlerLibrary;
708
709 public:
710 static const int ENTRIES_COUNT = 4;
711
712 private:
713 AdapterFingerPrint* _fingerprint;
714 AdapterBlob* _adapter_blob;
715 uint _id;
716 bool _linked;
717
718 static const char *_entry_names[];
719
720 #ifdef ASSERT
721 // Captures code and signature used to generate this adapter when
722 // verifying adapter equivalence.
723 unsigned char* _saved_code;
724 int _saved_code_length;
725 #endif
726
727 AdapterHandlerEntry(int id, AdapterFingerPrint* fingerprint) :
728 _fingerprint(fingerprint),
729 _adapter_blob(nullptr),
730 _id(id),
731 _linked(false)
732 #ifdef ASSERT
733 , _saved_code(nullptr),
734 _saved_code_length(0)
735 #endif
736 { }
737
738 ~AdapterHandlerEntry();
739
740 // Allocate on CHeap instead of metaspace (see JDK-8331086).
741 // Dummy argument is used to avoid C++ warning about using
742 // deleted opearator MetaspaceObj::delete().
743 void* operator new(size_t size, size_t dummy) throw() {
744 assert(size == BytesPerWord * heap_word_size(sizeof(AdapterHandlerEntry)), "should match");
745 void* p = AllocateHeap(size, mtCode);
746 memset(p, 0, size);
747 return p;
748 }
749
750 public:
751 static AdapterHandlerEntry* allocate(uint id, AdapterFingerPrint* fingerprint) {
762 }
763
764 address get_i2c_entry() const {
765 #ifndef ZERO
766 assert(_adapter_blob != nullptr, "must be");
767 return _adapter_blob->i2c_entry();
768 #else
769 return nullptr;
770 #endif // ZERO
771 }
772
773 address get_c2i_entry() const {
774 #ifndef ZERO
775 assert(_adapter_blob != nullptr, "must be");
776 return _adapter_blob->c2i_entry();
777 #else
778 return nullptr;
779 #endif // ZERO
780 }
781
782 address get_c2i_unverified_entry() const {
783 #ifndef ZERO
784 assert(_adapter_blob != nullptr, "must be");
785 return _adapter_blob->c2i_unverified_entry();
786 #else
787 return nullptr;
788 #endif // ZERO
789 }
790
791 address get_c2i_no_clinit_check_entry() const {
792 #ifndef ZERO
793 assert(_adapter_blob != nullptr, "must be");
794 return _adapter_blob->c2i_no_clinit_check_entry();
795 #else
796 return nullptr;
797 #endif // ZERO
798 }
799
800 AdapterBlob* adapter_blob() const { return _adapter_blob; }
801 bool is_linked() const { return _linked; }
802
803 uint id() const { return _id; }
804 AdapterFingerPrint* fingerprint() const { return _fingerprint; }
805
806 #ifdef ASSERT
807 // Used to verify that code generated for shared adapters is equivalent
808 void save_code (unsigned char* code, int length);
809 bool compare_code(AdapterHandlerEntry* other);
810 #endif
811
812 //virtual void print_on(outputStream* st) const; DO NOT USE
813 void print_adapter_on(outputStream* st) const;
814
815 void metaspace_pointers_do(MetaspaceClosure* it);
816 int size() const {return (int)heap_word_size(sizeof(AdapterHandlerEntry)); }
817 MetaspaceObj::Type type() const { return AdapterHandlerEntryType; }
818
819 void remove_unshareable_info() NOT_CDS_RETURN;
820 void link() NOT_CDS_RETURN;
821 };
822
823 #if INCLUDE_CDS
824 class ArchivedAdapterTable;
825 #endif // INCLUDE_CDS
826
827 class AdapterHandlerLibrary: public AllStatic {
828 friend class SharedRuntime;
829 private:
830 static volatile uint _id_counter; // counter for generating unique adapter ids, range = [1,UINT_MAX]
831 static BufferBlob* _buffer; // the temporary code buffer in CodeCache
832 static AdapterHandlerEntry* _no_arg_handler;
833 static AdapterHandlerEntry* _int_arg_handler;
834 static AdapterHandlerEntry* _obj_arg_handler;
835 static AdapterHandlerEntry* _obj_int_arg_handler;
836 static AdapterHandlerEntry* _obj_obj_arg_handler;
837 #if INCLUDE_CDS
838 static ArchivedAdapterTable _aot_adapter_handler_table;
839 #endif // INCLUDE_CDS
840
841 static BufferBlob* buffer_blob();
842 static void initialize();
843 static AdapterHandlerEntry* get_simple_adapter(const methodHandle& method);
844 static void lookup_aot_cache(AdapterHandlerEntry* handler);
845 static AdapterHandlerEntry* create_adapter(int total_args_passed,
846 BasicType* sig_bt,
847 bool is_transient = false);
848 static void lookup_simple_adapters() NOT_CDS_RETURN;
849 #ifndef PRODUCT
850 static void print_adapter_handler_info(outputStream* st, AdapterHandlerEntry* handler);
851 #endif // PRODUCT
852 public:
853
854 static AdapterHandlerEntry* new_entry(AdapterFingerPrint* fingerprint);
855 static void create_native_wrapper(const methodHandle& method);
856 static AdapterHandlerEntry* get_adapter(const methodHandle& method);
857 static AdapterHandlerEntry* lookup(int total_args_passed, BasicType* sig_bt);
858 static bool generate_adapter_code(AdapterHandlerEntry* handler,
859 int total_args_passed,
860 BasicType* sig_bt,
861 bool is_transient);
862
863 #ifdef ASSERT
864 static void verify_adapter_sharing(int total_args_passed, BasicType* sig_bt, AdapterHandlerEntry* cached);
865 #endif // ASSERT
866
867 static void print_handler(const CodeBlob* b) { print_handler_on(tty, b); }
868 static void print_handler_on(outputStream* st, const CodeBlob* b);
869 static bool contains(const CodeBlob* b);
870 static const char* name(AdapterHandlerEntry* handler);
871 static uint32_t id(AdapterHandlerEntry* handler);
872 #ifndef PRODUCT
873 static void print_statistics();
874 #endif // PRODUCT
875
876 static void link_aot_adapter_handler(AdapterHandlerEntry* handler) NOT_CDS_RETURN;
877 static void dump_aot_adapter_table() NOT_CDS_RETURN;
878 static void serialize_shared_table_header(SerializeClosure* soc) NOT_CDS_RETURN;
879 static void link_aot_adapters() NOT_CDS_RETURN;
880 static void address_to_offset(address entry_address[AdapterBlob::ENTRY_COUNT], int entry_offset[AdapterBlob::ENTRY_COUNT]);
881 };
882
883 #endif // SHARE_RUNTIME_SHAREDRUNTIME_HPP
|
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #ifndef SHARE_RUNTIME_SHAREDRUNTIME_HPP
26 #define SHARE_RUNTIME_SHAREDRUNTIME_HPP
27
28 #include "asm/codeBuffer.hpp"
29 #include "classfile/compactHashtable.hpp"
30 #include "code/codeBlob.hpp"
31 #include "code/vmreg.hpp"
32 #include "interpreter/linkResolver.hpp"
33 #include "memory/allStatic.hpp"
34 #include "memory/metaspaceClosure.hpp"
35 #include "memory/resourceArea.hpp"
36 #include "runtime/safepointVerifiers.hpp"
37 #include "runtime/signature.hpp"
38 #include "runtime/stubInfo.hpp"
39 #include "utilities/macros.hpp"
40
41 class AdapterHandlerEntry;
42 class AdapterFingerPrint;
43 class vframeStream;
44 class SigEntry;
45
46 // Runtime is the base class for various runtime interfaces
47 // (InterpreterRuntime, CompilerRuntime, etc.). It provides
48 // shared functionality such as exception forwarding (C++ to
49 // Java exceptions), locking/unlocking mechanisms, statistical
50 // information, etc.
51
52 class SharedRuntime: AllStatic {
53 private:
54 // Declare shared stub fields
55 #define SHARED_STUB_FIELD_DECLARE(name, type) \
56 static type* BLOB_FIELD_NAME(name);
57 SHARED_STUBS_DO(SHARED_STUB_FIELD_DECLARE)
58 #undef SHARED_STUB_FIELD_DECLARE
59
60 #ifdef ASSERT
61 static bool is_resolve_id(StubId id) {
62 return (id == StubId::shared_wrong_method_id ||
63 id == StubId::shared_wrong_method_abstract_id ||
64 id == StubId::shared_ic_miss_id ||
362 // on top of the stack.
363 // The caller (or one of its callers) must use a ResourceMark
364 // in order to correctly free the result.
365 //
366 static char* generate_class_cast_message(JavaThread* thr, Klass* caster_klass);
367
368 // Fill in the "X cannot be cast to a Y" message for ClassCastException
369 //
370 // @param caster_klass the class of the object we are casting
371 // @param target_klass the target klass attempt
372 // @return the dynamically allocated exception message (must be freed
373 // by the caller using a resource mark)
374 //
375 // This version does not require access the frame, so it can be called
376 // from interpreted code
377 // The caller (or one of it's callers) must use a ResourceMark
378 // in order to correctly free the result.
379 //
380 static char* generate_class_cast_message(Klass* caster_klass, Klass* target_klass, Symbol* target_klass_name = nullptr);
381
382 static char* generate_identity_exception_message(JavaThread* thr, Klass* klass);
383
384 // Resolves a call site- may patch in the destination of the call into the
385 // compiled code.
386 static methodHandle resolve_helper(bool is_virtual, bool is_optimized, bool& caller_does_not_scalarize, TRAPS);
387
388 private:
389 // deopt blob
390 static void generate_deopt_blob(void);
391
392 public:
393 static DeoptimizationBlob* deopt_blob(void) { return _deopt_blob; }
394
395 // Resets a call-site in compiled code so it will get resolved again.
396 static methodHandle reresolve_call_site(bool& is_optimized, bool& caller_does_not_scalarize, TRAPS);
397
398 // In the code prolog, if the klass comparison fails, the inline cache
399 // misses and the call site is patched to megamorphic
400 static methodHandle handle_ic_miss_helper(bool& caller_does_not_scalarize, TRAPS);
401
402 // Find the method that called us.
403 static methodHandle find_callee_method(bool& caller_does_not_scalarize, TRAPS);
404
405 static void monitor_enter_helper(oopDesc* obj, BasicLock* lock, JavaThread* thread);
406
407 static void monitor_exit_helper(oopDesc* obj, BasicLock* lock, JavaThread* current);
408
409 private:
410 static Handle find_callee_info(Bytecodes::Code& bc, CallInfo& callinfo, TRAPS);
411 static Handle find_callee_info_helper(vframeStream& vfst, Bytecodes::Code& bc, CallInfo& callinfo, TRAPS);
412
413 static Method* extract_attached_method(vframeStream& vfst);
414
415 #if defined(X86) && defined(COMPILER1)
416 // For Object.hashCode, System.identityHashCode try to pull hashCode from object header if available.
417 static void inline_check_hashcode_from_object_header(MacroAssembler* masm, const methodHandle& method, Register obj_reg, Register result);
418 #endif // X86 && COMPILER1
419
420 public:
421
422 // Read the array of BasicTypes from a Java signature, and compute where
423 // compiled Java code would like to put the results. Values in reg_lo and
424 // reg_hi refer to 4-byte quantities. Values less than SharedInfo::stack0 are
425 // registers, those above refer to 4-byte stack slots. All stack slots are
426 // based off of the window top. SharedInfo::stack0 refers to the first usable
427 // slot in the bottom of the frame. SharedInfo::stack0+1 refers to the memory word
428 // 4-bytes higher.
429 // return value is the maximum number of VMReg stack slots the convention will use.
430 static int java_calling_convention(const BasicType* sig_bt, VMRegPair* regs, int total_args_passed);
431 static int java_calling_convention(const GrowableArray<SigEntry>* sig, VMRegPair* regs) {
432 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sig->length());
433 int total_args_passed = SigEntry::fill_sig_bt(sig, sig_bt);
434 return java_calling_convention(sig_bt, regs, total_args_passed);
435 }
436 static int java_return_convention(const BasicType* sig_bt, VMRegPair* regs, int total_args_passed);
437 static const uint java_return_convention_max_int;
438 static const uint java_return_convention_max_float;
439
440 static void check_member_name_argument_is_last_argument(const methodHandle& method,
441 const BasicType* sig_bt,
442 const VMRegPair* regs) NOT_DEBUG_RETURN;
443
444 // Ditto except for calling C
445 //
446 // C argument in register AND stack slot.
447 // Some architectures require that an argument must be passed in a register
448 // AND in a stack slot. These architectures provide a second VMRegPair array
449 // to be filled by the c_calling_convention method. On other architectures,
450 // null is being passed as the second VMRegPair array, so arguments are either
451 // passed in a register OR in a stack slot.
452 static int c_calling_convention(const BasicType *sig_bt, VMRegPair *regs, int total_args_passed);
453
454 static int vector_calling_convention(VMRegPair *regs,
455 uint num_bits,
456 uint total_args_passed);
457
458 // Generate I2C and C2I adapters. These adapters are simple argument marshalling
465 // by the time we reach the blob there is compiled code available. This allows
466 // the blob to pass the incoming stack pointer (the sender sp) in a known
467 // location for the interpreter to record. This is used by the frame code
468 // to correct the sender code to match up with the stack pointer when the
469 // thread left the compiled code. In addition it allows the interpreter
470 // to remove the space the c2i adapter allocated to do its argument conversion.
471
472 // Although a c2i blob will always run interpreted even if compiled code is
473 // present if we see that compiled code is present the compiled call site
474 // will be patched/re-resolved so that later calls will run compiled.
475
476 // Additionally a c2i blob need to have a unverified entry because it can be reached
477 // in situations where the call site is an inlined cache site and may go megamorphic.
478
479 // A i2c adapter is simpler than the c2i adapter. This is because it is assumed
480 // that the interpreter before it does any call dispatch will record the current
481 // stack pointer in the interpreter frame. On return it will restore the stack
482 // pointer as needed. This means the i2c adapter code doesn't need any special
483 // handshaking path with compiled code to keep the stack walking correct.
484
485 static void generate_i2c2i_adapters(MacroAssembler* _masm,
486 int total_args_passed,
487 const GrowableArray<SigEntry>* sig,
488 const VMRegPair* regs,
489 const GrowableArray<SigEntry>* sig_cc,
490 const VMRegPair* regs_cc,
491 const GrowableArray<SigEntry>* sig_cc_ro,
492 const VMRegPair* regs_cc_ro,
493 address entry_address[AdapterBlob::ENTRY_COUNT],
494 AdapterBlob*& new_adapter,
495 bool allocate_code_blob);
496
497 static void gen_i2c_adapter(MacroAssembler *_masm,
498 int comp_args_on_stack,
499 const GrowableArray<SigEntry>* sig,
500 const VMRegPair *regs);
501
502 // OSR support
503
504 // OSR_migration_begin will extract the jvm state from an interpreter
505 // frame (locals, monitors) and store the data in a piece of C heap
506 // storage. This then allows the interpreter frame to be removed from the
507 // stack and the OSR nmethod to be called. That method is called with a
508 // pointer to the C heap storage. This pointer is the return value from
509 // OSR_migration_begin.
510
511 static intptr_t* OSR_migration_begin(JavaThread *thread);
512
513 // OSR_migration_end is a trivial routine. It is called after the compiled
514 // method has extracted the jvm state from the C heap that OSR_migration_begin
515 // created. It's entire job is to simply free this storage.
516 static void OSR_migration_end(intptr_t* buf);
517
518 // Convert a sig into a calling convention register layout
519 // and find interesting things about it.
551 // returns.
552 //
553 // The wrapper may contain special-case code if the given method
554 // is a compiled method handle adapter, such as _invokeBasic, _linkToVirtual, etc.
555 static nmethod* generate_native_wrapper(MacroAssembler* masm,
556 const methodHandle& method,
557 int compile_id,
558 BasicType* sig_bt,
559 VMRegPair* regs,
560 BasicType ret_type);
561
562 // A compiled caller has just called the interpreter, but compiled code
563 // exists. Patch the caller so he no longer calls into the interpreter.
564 static void fixup_callers_callsite(Method* moop, address ret_pc);
565
566 // Slow-path Locking and Unlocking
567 static void complete_monitor_locking_C(oopDesc* obj, BasicLock* lock, JavaThread* current);
568 static void complete_monitor_unlocking_C(oopDesc* obj, BasicLock* lock, JavaThread* current);
569
570 // Resolving of calls
571 static address get_resolved_entry (JavaThread* current, methodHandle callee_method,
572 bool is_static_call, bool is_optimized, bool caller_does_not_scalarize);
573 static address resolve_static_call_C (JavaThread* current);
574 static address resolve_virtual_call_C (JavaThread* current);
575 static address resolve_opt_virtual_call_C(JavaThread* current);
576
577 static void load_inline_type_fields_in_regs(JavaThread* current, oopDesc* res);
578 static void store_inline_type_fields_to_buf(JavaThread* current, intptr_t res);
579
580 // arraycopy, the non-leaf version. (See StubRoutines for all the leaf calls.)
581 static void slow_arraycopy_C(oopDesc* src, jint src_pos,
582 oopDesc* dest, jint dest_pos,
583 jint length, JavaThread* thread);
584
585 // handle ic miss with caller being compiled code
586 // wrong method handling (inline cache misses)
587 static address handle_wrong_method(JavaThread* current);
588 static address handle_wrong_method_abstract(JavaThread* current);
589 static address handle_wrong_method_ic_miss(JavaThread* current);
590 static void allocate_inline_types(JavaThread* current, Method* callee, bool allocate_receiver);
591 static oop allocate_inline_types_impl(JavaThread* current, methodHandle callee, bool allocate_receiver, TRAPS);
592
593 static address handle_unsafe_access(JavaThread* thread, address next_pc);
594
595 static BufferedInlineTypeBlob* generate_buffered_inline_type_adapter(const InlineKlass* vk);
596 #ifndef PRODUCT
597
598 // Collect and print inline cache miss statistics
599 private:
600 enum { maxICmiss_count = 100 };
601 static int _ICmiss_index; // length of IC miss histogram
602 static int _ICmiss_count[maxICmiss_count]; // miss counts
603 static address _ICmiss_at[maxICmiss_count]; // miss addresses
604 static void trace_ic_miss(address at);
605
606 public:
607 static uint _ic_miss_ctr; // total # of IC misses
608 static uint _wrong_method_ctr;
609 static uint _resolve_static_ctr;
610 static uint _resolve_virtual_ctr;
611 static uint _resolve_opt_virtual_ctr;
612 static uint _implicit_null_throws;
613 static uint _implicit_div0_throws;
614
615 static uint _jbyte_array_copy_ctr; // Slow-path byte array copy
714 //
715 // The C2I flavor takes a stock compiled call setup plus the target method in
716 // Rmethod, marshals the arguments for an interpreted call and jumps to
717 // Rmethod->_i2i_entry. On entry, the interpreted frame has not yet been
718 // setup. Compiled frames are fixed-size and the args are likely not in the
719 // right place. Hence all the args will likely be copied into the
720 // interpreter's frame, forcing that frame to grow. The compiled frame's
721 // outgoing stack args will be dead after the copy.
722 //
723 // Native wrappers, like adapters, marshal arguments. Unlike adapters they
724 // also perform an official frame push & pop. They have a call to the native
725 // routine in their middles and end in a return (instead of ending in a jump).
726 // The native wrappers are stored in real nmethods instead of the BufferBlobs
727 // used by the adapters. The code generation happens here because it's very
728 // similar to what the adapters have to do.
729
730 class AdapterHandlerEntry : public MetaspaceObj {
731 friend class AdapterHandlerLibrary;
732
733 public:
734 static const int ENTRIES_COUNT = 7;
735
736 private:
737 AdapterFingerPrint* _fingerprint;
738 AdapterBlob* _adapter_blob;
739 uint _id;
740 bool _linked;
741
742 static const char *_entry_names[];
743
744 // Support for scalarized inline type calling convention
745 const GrowableArray<SigEntry>* _sig_cc;
746
747 #ifdef ASSERT
748 // Captures code and signature used to generate this adapter when
749 // verifying adapter equivalence.
750 unsigned char* _saved_code;
751 int _saved_code_length;
752 #endif
753
754 AdapterHandlerEntry(int id, AdapterFingerPrint* fingerprint) :
755 _fingerprint(fingerprint),
756 _adapter_blob(nullptr),
757 _id(id),
758 _linked(false),
759 _sig_cc(nullptr)
760 #ifdef ASSERT
761 , _saved_code(nullptr),
762 _saved_code_length(0)
763 #endif
764 { }
765
766 ~AdapterHandlerEntry();
767
768 // Allocate on CHeap instead of metaspace (see JDK-8331086).
769 // Dummy argument is used to avoid C++ warning about using
770 // deleted opearator MetaspaceObj::delete().
771 void* operator new(size_t size, size_t dummy) throw() {
772 assert(size == BytesPerWord * heap_word_size(sizeof(AdapterHandlerEntry)), "should match");
773 void* p = AllocateHeap(size, mtCode);
774 memset(p, 0, size);
775 return p;
776 }
777
778 public:
779 static AdapterHandlerEntry* allocate(uint id, AdapterFingerPrint* fingerprint) {
790 }
791
792 address get_i2c_entry() const {
793 #ifndef ZERO
794 assert(_adapter_blob != nullptr, "must be");
795 return _adapter_blob->i2c_entry();
796 #else
797 return nullptr;
798 #endif // ZERO
799 }
800
801 address get_c2i_entry() const {
802 #ifndef ZERO
803 assert(_adapter_blob != nullptr, "must be");
804 return _adapter_blob->c2i_entry();
805 #else
806 return nullptr;
807 #endif // ZERO
808 }
809
810 address get_c2i_inline_entry() const {
811 #ifndef ZERO
812 assert(_adapter_blob != nullptr, "must be");
813 return _adapter_blob->c2i_inline_entry();
814 #else
815 return nullptr;
816 #endif // ZERO
817 }
818
819 address get_c2i_inline_ro_entry() const {
820 #ifndef ZERO
821 assert(_adapter_blob != nullptr, "must be");
822 return _adapter_blob->c2i_inline_ro_entry();
823 #else
824 return nullptr;
825 #endif // ZERO
826 }
827
828 address get_c2i_unverified_entry() const {
829 #ifndef ZERO
830 assert(_adapter_blob != nullptr, "must be");
831 return _adapter_blob->c2i_unverified_entry();
832 #else
833 return nullptr;
834 #endif // ZERO
835 }
836
837 address get_c2i_unverified_inline_entry() const {
838 #ifndef ZERO
839 assert(_adapter_blob != nullptr, "must be");
840 return _adapter_blob->c2i_unverified_inline_entry();
841 #else
842 return nullptr;
843 #endif // ZERO
844 }
845
846 address get_c2i_no_clinit_check_entry() const {
847 #ifndef ZERO
848 assert(_adapter_blob != nullptr, "must be");
849 return _adapter_blob->c2i_no_clinit_check_entry();
850 #else
851 return nullptr;
852 #endif // ZERO
853 }
854
855 AdapterBlob* adapter_blob() const { return _adapter_blob; }
856 bool is_linked() const { return _linked; }
857
858 // Support for scalarized inline type calling convention
859 void set_sig_cc(const GrowableArray<SigEntry>* sig) { _sig_cc = sig; }
860 const GrowableArray<SigEntry>* get_sig_cc() const { return _sig_cc; }
861
862 uint id() const { return _id; }
863 AdapterFingerPrint* fingerprint() const { return _fingerprint; }
864
865 #ifdef ASSERT
866 // Used to verify that code generated for shared adapters is equivalent
867 void save_code (unsigned char* code, int length);
868 bool compare_code(AdapterHandlerEntry* other);
869 #endif
870
871 //virtual void print_on(outputStream* st) const; DO NOT USE
872 void print_adapter_on(outputStream* st) const;
873
874 void metaspace_pointers_do(MetaspaceClosure* it);
875 int size() const {return (int)heap_word_size(sizeof(AdapterHandlerEntry)); }
876 MetaspaceObj::Type type() const { return AdapterHandlerEntryType; }
877
878 void remove_unshareable_info() NOT_CDS_RETURN;
879 void link() NOT_CDS_RETURN;
880 };
881
882 #if INCLUDE_CDS
883 class ArchivedAdapterTable;
884 #endif // INCLUDE_CDS
885
886 class CompiledEntrySignature;
887
888 class AdapterHandlerLibrary: public AllStatic {
889 friend class SharedRuntime;
890 private:
891 static volatile uint _id_counter; // counter for generating unique adapter ids, range = [1,UINT_MAX]
892 static BufferBlob* _buffer; // the temporary code buffer in CodeCache
893 static AdapterHandlerEntry* _no_arg_handler;
894 static AdapterHandlerEntry* _int_arg_handler;
895 static AdapterHandlerEntry* _obj_arg_handler;
896 static AdapterHandlerEntry* _obj_int_arg_handler;
897 static AdapterHandlerEntry* _obj_obj_arg_handler;
898 #if INCLUDE_CDS
899 static ArchivedAdapterTable _aot_adapter_handler_table;
900 #endif // INCLUDE_CDS
901
902 static BufferBlob* buffer_blob();
903 static void initialize();
904 static AdapterHandlerEntry* get_simple_adapter(const methodHandle& method);
905 static void lookup_aot_cache(AdapterHandlerEntry* handler);
906 static AdapterHandlerEntry* create_adapter(CompiledEntrySignature& ces,
907 bool allocate_code_blob,
908 bool is_transient = false);
909 static void lookup_simple_adapters() NOT_CDS_RETURN;
910 #ifndef PRODUCT
911 static void print_adapter_handler_info(outputStream* st, AdapterHandlerEntry* handler);
912 #endif // PRODUCT
913 public:
914
915 static AdapterHandlerEntry* new_entry(AdapterFingerPrint* fingerprint);
916 static void create_native_wrapper(const methodHandle& method);
917 static AdapterHandlerEntry* get_adapter(const methodHandle& method);
918 static AdapterHandlerEntry* lookup(const GrowableArray<SigEntry>* sig, bool has_ro_adapter = false);
919 static bool generate_adapter_code(AdapterHandlerEntry* handler,
920 CompiledEntrySignature& ces,
921 bool allocate_code_blob,
922 bool is_transient);
923
924 #ifdef ASSERT
925 static void verify_adapter_sharing(CompiledEntrySignature& ces, AdapterHandlerEntry* cached_entry);
926 #endif // ASSERT
927
928 static void print_handler(const CodeBlob* b) { print_handler_on(tty, b); }
929 static void print_handler_on(outputStream* st, const CodeBlob* b);
930 static bool contains(const CodeBlob* b);
931 static const char* name(AdapterHandlerEntry* handler);
932 static uint32_t id(AdapterHandlerEntry* handler);
933 #ifndef PRODUCT
934 static void print_statistics();
935 #endif // PRODUCT
936
937 static void link_aot_adapter_handler(AdapterHandlerEntry* handler) NOT_CDS_RETURN;
938 static void dump_aot_adapter_table() NOT_CDS_RETURN;
939 static void serialize_shared_table_header(SerializeClosure* soc) NOT_CDS_RETURN;
940 static void link_aot_adapters() NOT_CDS_RETURN;
941 static void address_to_offset(address entry_address[AdapterBlob::ENTRY_COUNT], int entry_offset[AdapterBlob::ENTRY_COUNT]);
942 };
943
944 // Utility class for computing the calling convention of the 3 types
945 // of compiled method entries:
946 // Method::_from_compiled_entry - sig_cc
947 // Method::_from_compiled_inline_ro_entry - sig_cc_ro
948 // Method::_from_compiled_inline_entry - sig
949 class CompiledEntrySignature : public StackObj {
950 Method* _method;
951 int _num_inline_args;
952 bool _has_inline_recv;
953 GrowableArray<SigEntry>* _sig;
954 GrowableArray<SigEntry>* _sig_cc;
955 GrowableArray<SigEntry>* _sig_cc_ro;
956 VMRegPair* _regs;
957 VMRegPair* _regs_cc;
958 VMRegPair* _regs_cc_ro;
959
960 int _args_on_stack;
961 int _args_on_stack_cc;
962 int _args_on_stack_cc_ro;
963
964 bool _c1_needs_stack_repair;
965 bool _c2_needs_stack_repair;
966
967 GrowableArray<Method*>* _supers;
968
969 public:
970 Method* method() const { return _method; }
971
972 // Used by Method::_from_compiled_inline_entry
973 GrowableArray<SigEntry>* sig() const { return _sig; }
974
975 // Used by Method::_from_compiled_entry
976 GrowableArray<SigEntry>* sig_cc() const { return _sig_cc; }
977
978 // Used by Method::_from_compiled_inline_ro_entry
979 GrowableArray<SigEntry>* sig_cc_ro() const { return _sig_cc_ro; }
980
981 VMRegPair* regs() const { return _regs; }
982 VMRegPair* regs_cc() const { return _regs_cc; }
983 VMRegPair* regs_cc_ro() const { return _regs_cc_ro; }
984
985 int args_on_stack() const { return _args_on_stack; }
986 int args_on_stack_cc() const { return _args_on_stack_cc; }
987 int args_on_stack_cc_ro() const { return _args_on_stack_cc_ro; }
988
989 int num_inline_args() const { return _num_inline_args; }
990 bool has_inline_recv() const { return _has_inline_recv; }
991
992 bool has_scalarized_args() const { return _sig != _sig_cc; }
993 bool c1_needs_stack_repair() const { return _c1_needs_stack_repair; }
994 bool c2_needs_stack_repair() const { return _c2_needs_stack_repair; }
995 CodeOffsets::Entries c1_inline_ro_entry_type() const;
996
997 GrowableArray<Method*>* get_supers();
998
999 CompiledEntrySignature(Method* method = nullptr);
1000 void compute_calling_conventions(bool init = true);
1001 void initialize_from_fingerprint(AdapterFingerPrint* fingerprint);
1002 };
1003
1004 #endif // SHARE_RUNTIME_SHAREDRUNTIME_HPP
|