1134 bool has_receiver = bc != Bytecodes::_invokestatic &&
1135 bc != Bytecodes::_invokedynamic &&
1136 bc != Bytecodes::_invokehandle;
1137
1138 // Find receiver for non-static call
1139 if (has_receiver) {
1140 // This register map must be update since we need to find the receiver for
1141 // compiled frames. The receiver might be in a register.
1142 RegisterMap reg_map2(thread);
1143 frame stubFrame = thread->last_frame();
1144 // Caller-frame is a compiled frame
1145 frame callerFrame = stubFrame.sender(®_map2);
1146
1147 methodHandle callee = attached_method;
1148 if (callee.is_null()) {
1149 callee = bytecode.static_target(CHECK_NH);
1150 if (callee.is_null()) {
1151 THROW_(vmSymbols::java_lang_NoSuchMethodException(), nullHandle);
1152 }
1153 }
1154 if (ValueTypePassFieldsAsArgs && callee->method_holder()->is_value()) {
1155 // If the receiver is a value type that is passed as fields, no oop is available.
1156 // Resolve the call without receiver null checking.
1157 assert(bc == Bytecodes::_invokevirtual, "only allowed with invokevirtual");
1158 assert(!attached_method.is_null(), "must have attached method");
1159 constantPoolHandle constants(THREAD, caller->constants());
1160 LinkInfo link_info(attached_method->method_holder(), attached_method->name(), attached_method->signature());
1161 LinkResolver::resolve_virtual_call(callinfo, receiver, NULL, link_info, /*check_null_or_abstract*/ false, CHECK_NH);
1162 return receiver; // is null
1163 } else {
1164 // Retrieve from a compiled argument list
1165 receiver = Handle(THREAD, callerFrame.retrieve_receiver(®_map2));
1166
1167 if (receiver.is_null()) {
1168 THROW_(vmSymbols::java_lang_NullPointerException(), nullHandle);
1169 }
1170 }
1171 }
1172
1173 // Resolve method
1174 if (attached_method.not_null()) {
2279 case T_BOOLEAN:
2280 case T_BYTE:
2281 case T_SHORT:
2282 case T_CHAR: {
2283 if (is_valuetype) {
2284 // Do not widen value type field types
2285 assert(ValueTypePassFieldsAsArgs, "must be enabled");
2286 return in;
2287 } else {
2288 // They are all promoted to T_INT in the calling convention
2289 return T_INT;
2290 }
2291 }
2292
2293 case T_VALUETYPE: {
2294 // If value types are passed as fields, return 'in' to differentiate
2295 // between a T_VALUETYPE and a T_OBJECT in the signature.
2296 return ValueTypePassFieldsAsArgs ? in : adapter_encoding(T_OBJECT, false);
2297 }
2298
2299 case T_VALUETYPEPTR:
2300 return T_VALUETYPE; // TODO hack because we don't have enough bits to represent T_VALUETYPEPTR.
2301
2302 case T_OBJECT:
2303 case T_ARRAY:
2304 // In other words, we assume that any register good enough for
2305 // an int or long is good enough for a managed pointer.
2306 #ifdef _LP64
2307 return T_LONG;
2308 #else
2309 return T_INT;
2310 #endif
2311
2312 case T_INT:
2313 case T_LONG:
2314 case T_FLOAT:
2315 case T_DOUBLE:
2316 case T_VOID:
2317 return in;
2318
2319 default:
2320 ShouldNotReachHere();
2321 return T_CONFLICT;
2322 }
2323 }
2324
2325 public:
2326 AdapterFingerPrint(int total_args_passed, BasicType* sig_bt) {
2327 // The fingerprint is based on the BasicType signature encoded
2328 // into an array of ints with eight entries per int.
2329 int* ptr;
2330 int len = (total_args_passed + (_basic_types_per_int-1)) / _basic_types_per_int;
2331 if (len <= _compact_int_count) {
2332 assert(_compact_int_count == 3, "else change next line");
2333 _value._compact[0] = _value._compact[1] = _value._compact[2] = 0;
2334 // Storing the signature encoded as signed chars hits about 98%
2335 // of the time.
2336 _length = -len;
2337 ptr = _value._compact;
2338 } else {
2339 _length = len;
2340 _value._fingerprint = NEW_C_HEAP_ARRAY(int, _length, mtCode);
2341 ptr = _value._fingerprint;
2342 }
2343
2344 // Now pack the BasicTypes with 8 per int
2345 int sig_index = 0;
2346 BasicType prev_sbt = T_ILLEGAL;
2347 int vt_count = 0;
2348 for (int index = 0; index < len; index++) {
2349 int value = 0;
2350 for (int byte = 0; byte < _basic_types_per_int; byte++) {
2351 int bt = 0;
2352 if (sig_index < total_args_passed) {
2353 BasicType sbt = sig_bt[sig_index++];
2354 if (ValueTypePassFieldsAsArgs && sbt == T_VALUETYPE) {
2355 // Found start of value type in signature
2356 vt_count++;
2357 } else if (ValueTypePassFieldsAsArgs && sbt == T_VOID &&
2358 prev_sbt != T_LONG && prev_sbt != T_DOUBLE) {
2359 // Found end of value type in signature
2360 vt_count--;
2361 assert(vt_count >= 0, "invalid vt_count");
2362 }
2363 bt = adapter_encoding(sbt, vt_count > 0);
2364 prev_sbt = sbt;
2365 }
2366 assert((bt & _basic_type_mask) == bt, "must fit in 4 bits");
2367 value = (value << _basic_type_bits) | bt;
2368 }
2369 ptr[index] = value;
2370 }
2371 assert(vt_count == 0, "invalid vt_count");
2372 }
2373
2437
2438 private:
2439
2440 #ifndef PRODUCT
2441 static int _lookups; // number of calls to lookup
2442 static int _buckets; // number of buckets checked
2443 static int _equals; // number of buckets checked with matching hash
2444 static int _hits; // number of successful lookups
2445 static int _compact; // number of equals calls with compact signature
2446 #endif
2447
2448 AdapterHandlerEntry* bucket(int i) {
2449 return (AdapterHandlerEntry*)BasicHashtable<mtCode>::bucket(i);
2450 }
2451
2452 public:
2453 AdapterHandlerTable()
2454 : BasicHashtable<mtCode>(293, (DumpSharedSpaces ? sizeof(CDSAdapterHandlerEntry) : sizeof(AdapterHandlerEntry))) { }
2455
2456 // Create a new entry suitable for insertion in the table
2457 AdapterHandlerEntry* new_entry(AdapterFingerPrint* fingerprint, address i2c_entry, address c2i_entry, address c2i_unverified_entry, Symbol* sig_extended) {
2458 AdapterHandlerEntry* entry = (AdapterHandlerEntry*)BasicHashtable<mtCode>::new_entry(fingerprint->compute_hash());
2459 entry->init(fingerprint, i2c_entry, c2i_entry, c2i_unverified_entry, sig_extended);
2460 if (DumpSharedSpaces) {
2461 ((CDSAdapterHandlerEntry*)entry)->init();
2462 }
2463 return entry;
2464 }
2465
2466 // Insert an entry into the table
2467 void add(AdapterHandlerEntry* entry) {
2468 int index = hash_to_index(entry->hash());
2469 add_entry(index, entry);
2470 }
2471
2472 void free_entry(AdapterHandlerEntry* entry) {
2473 entry->deallocate();
2474 BasicHashtable<mtCode>::free_entry(entry);
2475 }
2476
2477 // Find a entry with the same fingerprint if it exists
2478 AdapterHandlerEntry* lookup(int total_args_passed, BasicType* sig_bt) {
2479 NOT_PRODUCT(_lookups++);
2480 AdapterFingerPrint fp(total_args_passed, sig_bt);
2481 unsigned int hash = fp.compute_hash();
2482 int index = hash_to_index(hash);
2483 for (AdapterHandlerEntry* e = bucket(index); e != NULL; e = e->next()) {
2484 NOT_PRODUCT(_buckets++);
2485 if (e->hash() == hash) {
2486 NOT_PRODUCT(_equals++);
2487 if (fp.equals(e->fingerprint())) {
2488 #ifndef PRODUCT
2489 if (fp.is_compact()) _compact++;
2490 _hits++;
2491 #endif
2492 return e;
2493 }
2494 }
2495 }
2496 return NULL;
2497 }
2498
2499 #ifndef PRODUCT
2500 void print_statistics() {
2582 _buffer = BufferBlob::create("adapters", AdapterHandlerLibrary_size);
2583 return _buffer;
2584 }
2585
2586 extern "C" void unexpected_adapter_call() {
2587 ShouldNotCallThis();
2588 }
2589
2590 void AdapterHandlerLibrary::initialize() {
2591 if (_adapters != NULL) return;
2592 _adapters = new AdapterHandlerTable();
2593
2594 // Create a special handler for abstract methods. Abstract methods
2595 // are never compiled so an i2c entry is somewhat meaningless, but
2596 // throw AbstractMethodError just in case.
2597 // Pass wrong_method_abstract for the c2i transitions to return
2598 // AbstractMethodError for invalid invocations.
2599 address wrong_method_abstract = SharedRuntime::get_handle_wrong_method_abstract_stub();
2600 _abstract_method_handler = AdapterHandlerLibrary::new_entry(new AdapterFingerPrint(0, NULL),
2601 StubRoutines::throw_AbstractMethodError_entry(),
2602 wrong_method_abstract, wrong_method_abstract);
2603 }
2604
2605 AdapterHandlerEntry* AdapterHandlerLibrary::new_entry(AdapterFingerPrint* fingerprint,
2606 address i2c_entry,
2607 address c2i_entry,
2608 address c2i_unverified_entry,
2609 Symbol* sig_extended) {
2610 return _adapters->new_entry(fingerprint, i2c_entry, c2i_entry, c2i_unverified_entry, sig_extended);
2611 }
2612
2613 AdapterHandlerEntry* AdapterHandlerLibrary::get_adapter(const methodHandle& method, TRAPS) {
2614 AdapterHandlerEntry* entry = get_adapter0(method, CHECK_NULL);
2615 if (method->is_shared()) {
2616 // See comments around Method::link_method()
2617 MutexLocker mu(AdapterHandlerLibrary_lock);
2618 if (method->adapter() == NULL) {
2619 method->update_adapter_trampoline(entry);
2620 }
2621 address trampoline = method->from_compiled_entry();
2622 if (*(int*)trampoline == 0) {
2623 CodeBuffer buffer(trampoline, (int)SharedRuntime::trampoline_size());
2624 MacroAssembler _masm(&buffer);
2625 SharedRuntime::generate_trampoline(&_masm, entry->get_c2i_entry());
2626 assert(*(int*)trampoline != 0, "Instruction(s) for trampoline must not be encoded as zeros.");
2627
2628 if (PrintInterpreter) {
2629 Disassembler::decode(buffer.insts_begin(), buffer.insts_end());
2630 }
2631 }
2632 }
2633
2634 return entry;
2635 }
2636
2637 AdapterHandlerEntry* AdapterHandlerLibrary::get_adapter0(const methodHandle& method, TRAPS) {
2638 // Use customized signature handler. Need to lock around updates to
2639 // the AdapterHandlerTable (it is not safe for concurrent readers
2640 // and a single writer: this could be fixed if it becomes a
2641 // problem).
2642
2643 ResourceMark rm;
2644
2645 NOT_PRODUCT(int insts_size = 0);
2646 AdapterBlob* new_adapter = NULL;
2647 AdapterHandlerEntry* entry = NULL;
2648 AdapterFingerPrint* fingerprint = NULL;
2649 {
2650 MutexLocker mu(AdapterHandlerLibrary_lock);
2651 // make sure data structure is initialized
2652 initialize();
2653
2654 if (method->is_abstract()) {
2655 return _abstract_method_handler;
2656 }
2657
2658 // Fill in the signature array, for the calling-convention call.
2659 GrowableArray<SigEntry> sig_extended;
2660 {
2661 MutexUnlocker mul(AdapterHandlerLibrary_lock);
2662 Thread* THREAD = Thread::current();
2663 Klass* holder = method->method_holder();
2664 GrowableArray<BasicType> sig_bt_tmp;
2665
2666 int i = 0;
2667 if (!method->is_static()) { // Pass in receiver first
2668 if (holder->is_value()) {
2669 ValueKlass* vk = ValueKlass::cast(holder);
2670 if (!ValueTypePassFieldsAsArgs) {
2671 // If we don't pass value types as arguments or if the holder of
2672 // the method is __Value, we must pass a reference.
2673 sig_extended.push(SigEntry(T_VALUETYPEPTR));
2674 } else {
2675 const Array<SigEntry>* sig_vk = vk->extended_sig();
2676 sig_extended.appendAll(sig_vk);
2677 }
2678 } else {
2679 sig_extended.push(SigEntry(T_OBJECT));
2680 }
2681 }
2682 for (SignatureStream ss(method->signature()); !ss.at_return_type(); ss.next()) {
2683 Symbol* sym = ss.as_symbol_or_null();
2684 if (sym != NULL && sym->is_Q_signature()) {
2685 if (!ValueTypePassFieldsAsArgs) {
2686 sig_extended.push(SigEntry(T_VALUETYPEPTR));
2687 } else {
2688 // Method handle intrinsics with a __Value argument may be created during
2689 // compilation. Only do a full system dictionary lookup if the argument name
2690 // is not __Value, to avoid lookups from the compiler thread.
2691 Klass* k = ss.as_klass(Handle(THREAD, holder->class_loader()),
2692 Handle(THREAD, holder->protection_domain()),
2693 SignatureStream::ReturnNull, CHECK_NULL);
2694 const Array<SigEntry>* sig_vk = ValueKlass::cast(k)->extended_sig();
2695 sig_extended.appendAll(sig_vk);
2696 }
2697 } else {
2698 sig_extended.push(SigEntry(ss.type()));
2699 if (ss.type() == T_LONG || ss.type() == T_DOUBLE) {
2700 sig_extended.push(SigEntry(T_VOID));
2701 }
2702 }
2703 }
2704 }
2705
2706 int total_args_passed_cc = ValueTypePassFieldsAsArgs ? SigEntry::count_fields(sig_extended) : sig_extended.length();
2707 BasicType* sig_bt_cc = NEW_RESOURCE_ARRAY(BasicType, total_args_passed_cc);
2708 SigEntry::fill_sig_bt(sig_extended, sig_bt_cc, total_args_passed_cc, ValueTypePassFieldsAsArgs);
2709
2710 int total_args_passed_fp = sig_extended.length();
2711 BasicType* sig_bt_fp = NEW_RESOURCE_ARRAY(BasicType, total_args_passed_fp);
2712 for (int i = 0; i < sig_extended.length(); i++) {
2713 sig_bt_fp[i] = sig_extended.at(i)._bt;
2714 }
2715
2716 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, total_args_passed_cc);
2717
2718 // Lookup method signature's fingerprint
2719 entry = _adapters->lookup(total_args_passed_fp, sig_bt_fp);
2720
2721 #ifdef ASSERT
2722 AdapterHandlerEntry* shared_entry = NULL;
2723 // Start adapter sharing verification only after the VM is booted.
2724 if (VerifyAdapterSharing && (entry != NULL)) {
2725 shared_entry = entry;
2726 entry = NULL;
2727 }
2728 #endif
2729
2730 if (entry != NULL) {
2731 return entry;
2732 }
2733
2734 // Get a description of the compiled java calling convention and the largest used (VMReg) stack slot usage
2735 int comp_args_on_stack = SharedRuntime::java_calling_convention(sig_bt_cc, regs, total_args_passed_cc, false);
2736
2737 // Make a C heap allocated version of the fingerprint to store in the adapter
2738 fingerprint = new AdapterFingerPrint(total_args_passed_fp, sig_bt_fp);
2739
2740 // StubRoutines::code2() is initialized after this function can be called. As a result,
2741 // VerifyAdapterCalls and VerifyAdapterSharing can fail if we re-use code that generated
2742 // prior to StubRoutines::code2() being set. Checks refer to checks generated in an I2C
2743 // stub that ensure that an I2C stub is called from an interpreter frame.
2744 bool contains_all_checks = StubRoutines::code2() != NULL;
2745
2746 // Create I2C & C2I handlers
2747 BufferBlob* buf = buffer_blob(); // the temporary code buffer in CodeCache
2748 if (buf != NULL) {
2749 CodeBuffer buffer(buf);
2750 short buffer_locs[20];
2751 buffer.insts()->initialize_shared_locs((relocInfo*)buffer_locs,
2752 sizeof(buffer_locs)/sizeof(relocInfo));
2753
2754 MacroAssembler _masm(&buffer);
2755 entry = SharedRuntime::generate_i2c2i_adapters(&_masm,
2756 comp_args_on_stack,
2757 sig_extended,
2758 regs,
2759 fingerprint,
2760 new_adapter);
2761 #ifdef ASSERT
2762 if (VerifyAdapterSharing) {
2763 if (shared_entry != NULL) {
2764 assert(shared_entry->compare_code(buf->code_begin(), buffer.insts_size()), "code must match");
2765 // Release the one just created and return the original
2766 _adapters->free_entry(entry);
2767 return shared_entry;
2768 } else {
2769 entry->save_code(buf->code_begin(), buffer.insts_size());
2770 }
2771 }
2772 #endif
2773
2774 NOT_PRODUCT(insts_size = buffer.insts_size());
2775 }
2776 if (new_adapter == NULL) {
2777 // CodeCache is full, disable compilation
2778 // Ought to log this but compile log is only per compile thread
2779 // and we're some non descript Java thread.
2780 return NULL; // Out of CodeCache space
2809 char blob_id[256];
2810 jio_snprintf(blob_id,
2811 sizeof(blob_id),
2812 "%s(%s)@" PTR_FORMAT,
2813 new_adapter->name(),
2814 fingerprint->as_string(),
2815 new_adapter->content_begin());
2816 Forte::register_stub(blob_id, new_adapter->content_begin(), new_adapter->content_end());
2817
2818 if (JvmtiExport::should_post_dynamic_code_generated()) {
2819 JvmtiExport::post_dynamic_code_generated(blob_id, new_adapter->content_begin(), new_adapter->content_end());
2820 }
2821 }
2822 return entry;
2823 }
2824
2825 address AdapterHandlerEntry::base_address() {
2826 address base = _i2c_entry;
2827 if (base == NULL) base = _c2i_entry;
2828 assert(base <= _c2i_entry || _c2i_entry == NULL, "");
2829 assert(base <= _c2i_unverified_entry || _c2i_unverified_entry == NULL, "");
2830 return base;
2831 }
2832
2833 void AdapterHandlerEntry::relocate(address new_base) {
2834 address old_base = base_address();
2835 assert(old_base != NULL, "");
2836 ptrdiff_t delta = new_base - old_base;
2837 if (_i2c_entry != NULL)
2838 _i2c_entry += delta;
2839 if (_c2i_entry != NULL)
2840 _c2i_entry += delta;
2841 if (_c2i_unverified_entry != NULL)
2842 _c2i_unverified_entry += delta;
2843 assert(base_address() == new_base, "");
2844 }
2845
2846
2847 void AdapterHandlerEntry::deallocate() {
2848 delete _fingerprint;
2849 #ifdef ASSERT
2850 if (_saved_code) FREE_C_HEAP_ARRAY(unsigned char, _saved_code);
2851 #endif
2852 }
2853
2854
2855 #ifdef ASSERT
2856 // Capture the code before relocation so that it can be compared
2857 // against other versions. If the code is captured after relocation
2858 // then relative instructions won't be equivalent.
2859 void AdapterHandlerEntry::save_code(unsigned char* buffer, int length) {
2860 _saved_code = NEW_C_HEAP_ARRAY(unsigned char, length, mtCode);
2861 _saved_code_length = length;
2862 memcpy(_saved_code, buffer, length);
2863 }
2864
2865
2866 bool AdapterHandlerEntry::compare_code(unsigned char* buffer, int length) {
2867 if (length != _saved_code_length) {
2868 return false;
2901
2902 ResourceMark rm;
2903 BufferBlob* buf = buffer_blob(); // the temporary code buffer in CodeCache
2904 if (buf != NULL) {
2905 CodeBuffer buffer(buf);
2906 double locs_buf[20];
2907 buffer.insts()->initialize_shared_locs((relocInfo*)locs_buf, sizeof(locs_buf) / sizeof(relocInfo));
2908 MacroAssembler _masm(&buffer);
2909
2910 // Fill in the signature array, for the calling-convention call.
2911 const int total_args_passed = method->size_of_parameters();
2912
2913 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, total_args_passed);
2914 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, total_args_passed);
2915 int i=0;
2916 if (!method->is_static()) // Pass in receiver first
2917 sig_bt[i++] = T_OBJECT;
2918 SignatureStream ss(method->signature());
2919 for (; !ss.at_return_type(); ss.next()) {
2920 BasicType bt = ss.type();
2921 if (bt == T_VALUETYPE) {
2922 #ifdef ASSERT
2923 Thread* THREAD = Thread::current();
2924 // Avoid class loading from compiler thread
2925 if (THREAD->can_call_java()) {
2926 Handle class_loader(THREAD, method->method_holder()->class_loader());
2927 Handle protection_domain(THREAD, method->method_holder()->protection_domain());
2928 Klass* k = ss.as_klass(class_loader, protection_domain, SignatureStream::ReturnNull, THREAD);
2929 assert(k != NULL && !HAS_PENDING_EXCEPTION, "can't resolve klass");
2930 }
2931 #endif
2932 bt = T_VALUETYPEPTR;
2933 }
2934 sig_bt[i++] = bt; // Collect remaining bits of signature
2935 if (ss.type() == T_LONG || ss.type() == T_DOUBLE)
2936 sig_bt[i++] = T_VOID; // Longs & doubles take 2 Java slots
2937 }
2938 assert(i == total_args_passed, "");
2939 BasicType ret_type = ss.type();
2940
2941 // Now get the compiled-Java layout as input (or output) arguments.
2942 // NOTE: Stubs for compiled entry points of method handle intrinsics
2943 // are just trampolines so the argument registers must be outgoing ones.
2944 const bool is_outgoing = method->is_method_handle_intrinsic();
2945 int comp_args_on_stack = SharedRuntime::java_calling_convention(sig_bt, regs, total_args_passed, is_outgoing);
2946
2947 // Generate the compiled-to-native wrapper code
2948 nm = SharedRuntime::generate_native_wrapper(&_masm, method, compile_id, sig_bt, regs, ret_type);
2949
2950 if (nm != NULL) {
2951 method->set_code(method, nm);
2952
2953 DirectiveSet* directive = DirectivesStack::getDefaultDirective(CompileBroker::compiler(CompLevel_simple));
3179 AdapterHandlerEntry* a = iter.next();
3180 if (b == CodeCache::find_blob(a->get_i2c_entry())) return true;
3181 }
3182 return false;
3183 }
3184
3185 void AdapterHandlerLibrary::print_handler_on(outputStream* st, const CodeBlob* b) {
3186 AdapterHandlerTableIterator iter(_adapters);
3187 while (iter.has_next()) {
3188 AdapterHandlerEntry* a = iter.next();
3189 if (b == CodeCache::find_blob(a->get_i2c_entry())) {
3190 st->print("Adapter for signature: ");
3191 a->print_adapter_on(tty);
3192 return;
3193 }
3194 }
3195 assert(false, "Should have found handler");
3196 }
3197
3198 void AdapterHandlerEntry::print_adapter_on(outputStream* st) const {
3199 st->print_cr("AHE@" INTPTR_FORMAT ": %s i2c: " INTPTR_FORMAT " c2i: " INTPTR_FORMAT " c2iUV: " INTPTR_FORMAT,
3200 p2i(this), fingerprint()->as_string(),
3201 p2i(get_i2c_entry()), p2i(get_c2i_entry()), p2i(get_c2i_unverified_entry()));
3202
3203 }
3204
3205 #if INCLUDE_CDS
3206
3207 void CDSAdapterHandlerEntry::init() {
3208 assert(DumpSharedSpaces, "used during dump time only");
3209 _c2i_entry_trampoline = (address)MetaspaceShared::misc_code_space_alloc(SharedRuntime::trampoline_size());
3210 _adapter_trampoline = (AdapterHandlerEntry**)MetaspaceShared::misc_code_space_alloc(sizeof(AdapterHandlerEntry*));
3211 };
3212
3213 #endif // INCLUDE_CDS
3214
3215
3216 #ifndef PRODUCT
3217
3218 void AdapterHandlerLibrary::print_statistics() {
3219 _adapters->print_statistics();
3220 }
3221
3288
3289 oop new_obj = thread->vm_result();
3290 if (new_obj == NULL) return;
3291
3292 BarrierSet *bs = BarrierSet::barrier_set();
3293 bs->on_slowpath_allocation_exit(thread, new_obj);
3294 }
3295
3296 // We are at a compiled code to interpreter call. We need backing
3297 // buffers for all value type arguments. Allocate an object array to
3298 // hold them (convenient because once we're done with it we don't have
3299 // to worry about freeing it).
3300 JRT_ENTRY(void, SharedRuntime::allocate_value_types(JavaThread* thread, Method* callee_method))
3301 {
3302 assert(ValueTypePassFieldsAsArgs, "no reason to call this");
3303 ResourceMark rm;
3304 JavaThread* THREAD = thread;
3305 methodHandle callee(callee_method);
3306
3307 int nb_slots = 0;
3308 bool has_value_receiver = !callee->is_static() && callee->method_holder()->is_value();
3309 if (has_value_receiver) {
3310 nb_slots++;
3311 }
3312 Handle class_loader(THREAD, callee->method_holder()->class_loader());
3313 Handle protection_domain(THREAD, callee->method_holder()->protection_domain());
3314 for (SignatureStream ss(callee->signature()); !ss.at_return_type(); ss.next()) {
3315 if (ss.type() == T_VALUETYPE) {
3316 nb_slots++;
3317 }
3318 }
3319 objArrayOop array_oop = oopFactory::new_objectArray(nb_slots, CHECK);
3320 objArrayHandle array(THREAD, array_oop);
3321 int i = 0;
3322 if (has_value_receiver) {
3323 ValueKlass* vk = ValueKlass::cast(callee->method_holder());
3324 oop res = vk->allocate_instance(CHECK);
3325 array->obj_at_put(i, res);
3326 i++;
3327 }
3328 for (SignatureStream ss(callee->signature()); !ss.at_return_type(); ss.next()) {
3329 if (ss.type() == T_VALUETYPE) {
3330 Klass* k = ss.as_klass(class_loader, protection_domain, SignatureStream::ReturnNull, THREAD);
3331 assert(k != NULL && !HAS_PENDING_EXCEPTION, "can't resolve klass");
3332 ValueKlass* vk = ValueKlass::cast(k);
3333 oop res = vk->allocate_instance(CHECK);
3334 array->obj_at_put(i, res);
3335 i++;
3336 }
3337 }
3338 thread->set_vm_result(array());
3339 thread->set_vm_result_2(callee()); // TODO: required to keep callee live?
3340 }
3341 JRT_END
3342
3343 // Iterate of the array of heap allocated value types and apply the GC post barrier to all reference fields.
3360 map++;
3361 }
3362 }
3363 }
3364 }
3365 JRT_END
3366
3367 // We're returning from an interpreted method: load each field into a
3368 // register following the calling convention
3369 JRT_LEAF(void, SharedRuntime::load_value_type_fields_in_regs(JavaThread* thread, oopDesc* res))
3370 {
3371 assert(res->klass()->is_value(), "only value types here");
3372 ResourceMark rm;
3373 RegisterMap reg_map(thread);
3374 frame stubFrame = thread->last_frame();
3375 frame callerFrame = stubFrame.sender(®_map);
3376 assert(callerFrame.is_interpreted_frame(), "should be coming from interpreter");
3377
3378 ValueKlass* vk = ValueKlass::cast(res->klass());
3379
3380 const Array<SigEntry>* sig_vk = vk->extended_sig() ;
3381 const Array<VMRegPair>* regs = vk->return_regs();
3382
3383 if (regs == NULL) {
3384 // The fields of the value klass don't fit in registers, bail out
3385 return;
3386 }
3387
3388 int j = 1;
3389 for (int i = 0; i < sig_vk->length(); i++) {
3390 BasicType bt = sig_vk->at(i)._bt;
3391 if (bt == T_VALUETYPE) {
3392 continue;
3393 }
3394 if (bt == T_VOID) {
3395 if (sig_vk->at(i-1)._bt == T_LONG ||
3396 sig_vk->at(i-1)._bt == T_DOUBLE) {
3397 j++;
3398 }
3399 continue;
3400 }
3401 int off = sig_vk->at(i)._offset;
3402 VMRegPair pair = regs->at(j);
3403 address loc = reg_map.location(pair.first());
3404 switch(bt) {
3405 case T_BOOLEAN:
3406 *(intptr_t*)loc = *(jboolean*)((address)res + off);
3407 break;
3408 case T_CHAR:
3409 *(intptr_t*)loc = *(jchar*)((address)res + off);
3410 break;
3411 case T_BYTE:
3412 *(intptr_t*)loc = *(jbyte*)((address)res + off);
3413 break;
3414 case T_SHORT:
3415 *(intptr_t*)loc = *(jshort*)((address)res + off);
3416 break;
3417 case T_INT: {
3418 jint v = *(jint*)((address)res + off);
3419 *(intptr_t*)loc = v;
3420 break;
3421 }
3422 case T_LONG:
3423 #ifdef _LP64
3424 *(intptr_t*)loc = *(jlong*)((address)res + off);
3425 #else
3426 Unimplemented();
3427 #endif
3428 break;
3429 case T_OBJECT:
3430 case T_ARRAY: {
3431 oop v = HeapAccess<>::oop_load_at(res, off);
3432 *(oop*)loc = v;
3433 break;
3434 }
3435 case T_FLOAT:
3436 *(jfloat*)loc = *(jfloat*)((address)res + off);
3437 break;
3438 case T_DOUBLE:
3439 *(jdouble*)loc = *(jdouble*)((address)res + off);
3440 break;
3441 default:
3442 ShouldNotReachHere();
3443 }
3444 j++;
3445 }
3446 assert(j == regs->length(), "missed a field?");
3447
3448 #ifdef ASSERT
3449 VMRegPair pair = regs->at(0);
3450 address loc = reg_map.location(pair.first());
3451 assert(*(oopDesc**)loc == res, "overwritten object");
3452 #endif
3453
3454 thread->set_vm_result(res);
3455 }
3456 JRT_END
3457
3458 // We've returned to an interpreted method, the interpreter needs a
3459 // reference to a value type instance. Allocate it and initialize it
3466 frame callerFrame = stubFrame.sender(®_map);
3467
3468 #ifdef ASSERT
3469 ValueKlass* verif_vk = ValueKlass::returned_value_klass(reg_map);
3470 #endif
3471
3472 if (!is_set_nth_bit(res, 0)) {
3473 // We're not returning with value type fields in registers (the
3474 // calling convention didn't allow it for this value klass)
3475 assert(!Metaspace::contains((void*)res), "should be oop or pointer in buffer area");
3476 thread->set_vm_result((oopDesc*)res);
3477 assert(verif_vk == NULL, "broken calling convention");
3478 return;
3479 }
3480
3481 clear_nth_bit(res, 0);
3482 ValueKlass* vk = (ValueKlass*)res;
3483 assert(verif_vk == vk, "broken calling convention");
3484 assert(Metaspace::contains((void*)res), "should be klass");
3485
3486 // Allocate handles for every oop fields so they are safe in case of
3487 // a safepoint when allocating
3488 GrowableArray<Handle> handles;
3489 vk->save_oop_fields(reg_map, handles);
3490
3491 // It's unsafe to safepoint until we are here
3492
3493 Handle new_vt;
3494 JRT_BLOCK;
3495 {
3496 Thread* THREAD = thread;
3497 oop vt = vk->realloc_result(reg_map, handles, CHECK);
3498 new_vt = Handle(thread, vt);
3499
3500 #ifdef ASSERT
3501 javaVFrame* vf = javaVFrame::cast(vframe::new_vframe(&callerFrame, ®_map, thread));
3502 Method* m = vf->method();
3503 int bci = vf->bci();
3504 Bytecode_invoke inv(m, bci);
3505
3506 methodHandle callee = inv.static_target(thread);
3507 assert(!thread->has_pending_exception(), "call resolution should work");
3508 ValueKlass* verif_vk2 = callee->returned_value_type(thread);
3509 assert(verif_vk == verif_vk2, "Bad value klass");
3510 #endif
3511 }
3512 JRT_BLOCK_END;
3513
3514 thread->set_vm_result(new_vt());
3515 }
3516 JRT_END
3517
|
1134 bool has_receiver = bc != Bytecodes::_invokestatic &&
1135 bc != Bytecodes::_invokedynamic &&
1136 bc != Bytecodes::_invokehandle;
1137
1138 // Find receiver for non-static call
1139 if (has_receiver) {
1140 // This register map must be update since we need to find the receiver for
1141 // compiled frames. The receiver might be in a register.
1142 RegisterMap reg_map2(thread);
1143 frame stubFrame = thread->last_frame();
1144 // Caller-frame is a compiled frame
1145 frame callerFrame = stubFrame.sender(®_map2);
1146
1147 methodHandle callee = attached_method;
1148 if (callee.is_null()) {
1149 callee = bytecode.static_target(CHECK_NH);
1150 if (callee.is_null()) {
1151 THROW_(vmSymbols::java_lang_NoSuchMethodException(), nullHandle);
1152 }
1153 }
1154 // TODO for now, don't scalarize value type receivers because of interface calls
1155 if (ValueTypePassFieldsAsArgs && callee->method_holder()->is_value() && false) {
1156 // If the receiver is a value type that is passed as fields, no oop is available.
1157 // Resolve the call without receiver null checking.
1158 assert(bc == Bytecodes::_invokevirtual, "only allowed with invokevirtual");
1159 assert(!attached_method.is_null(), "must have attached method");
1160 constantPoolHandle constants(THREAD, caller->constants());
1161 LinkInfo link_info(attached_method->method_holder(), attached_method->name(), attached_method->signature());
1162 LinkResolver::resolve_virtual_call(callinfo, receiver, NULL, link_info, /*check_null_or_abstract*/ false, CHECK_NH);
1163 return receiver; // is null
1164 } else {
1165 // Retrieve from a compiled argument list
1166 receiver = Handle(THREAD, callerFrame.retrieve_receiver(®_map2));
1167
1168 if (receiver.is_null()) {
1169 THROW_(vmSymbols::java_lang_NullPointerException(), nullHandle);
1170 }
1171 }
1172 }
1173
1174 // Resolve method
1175 if (attached_method.not_null()) {
2280 case T_BOOLEAN:
2281 case T_BYTE:
2282 case T_SHORT:
2283 case T_CHAR: {
2284 if (is_valuetype) {
2285 // Do not widen value type field types
2286 assert(ValueTypePassFieldsAsArgs, "must be enabled");
2287 return in;
2288 } else {
2289 // They are all promoted to T_INT in the calling convention
2290 return T_INT;
2291 }
2292 }
2293
2294 case T_VALUETYPE: {
2295 // If value types are passed as fields, return 'in' to differentiate
2296 // between a T_VALUETYPE and a T_OBJECT in the signature.
2297 return ValueTypePassFieldsAsArgs ? in : adapter_encoding(T_OBJECT, false);
2298 }
2299
2300 case T_OBJECT:
2301 case T_ARRAY:
2302 // In other words, we assume that any register good enough for
2303 // an int or long is good enough for a managed pointer.
2304 #ifdef _LP64
2305 return T_LONG;
2306 #else
2307 return T_INT;
2308 #endif
2309
2310 case T_INT:
2311 case T_LONG:
2312 case T_FLOAT:
2313 case T_DOUBLE:
2314 case T_VOID:
2315 return in;
2316
2317 default:
2318 ShouldNotReachHere();
2319 return T_CONFLICT;
2320 }
2321 }
2322
2323 public:
2324 AdapterFingerPrint(int total_args_passed, const GrowableArray<SigEntry>* sig) {
2325 // The fingerprint is based on the BasicType signature encoded
2326 // into an array of ints with eight entries per int.
2327 int* ptr;
2328 int len = (total_args_passed + (_basic_types_per_int-1)) / _basic_types_per_int;
2329 if (len <= _compact_int_count) {
2330 assert(_compact_int_count == 3, "else change next line");
2331 _value._compact[0] = _value._compact[1] = _value._compact[2] = 0;
2332 // Storing the signature encoded as signed chars hits about 98%
2333 // of the time.
2334 _length = -len;
2335 ptr = _value._compact;
2336 } else {
2337 _length = len;
2338 _value._fingerprint = NEW_C_HEAP_ARRAY(int, _length, mtCode);
2339 ptr = _value._fingerprint;
2340 }
2341
2342 // Now pack the BasicTypes with 8 per int
2343 int sig_index = 0;
2344 BasicType prev_sbt = T_ILLEGAL;
2345 int vt_count = 0;
2346 for (int index = 0; index < len; index++) {
2347 int value = 0;
2348 for (int byte = 0; byte < _basic_types_per_int; byte++) {
2349 int bt = 0;
2350 if (sig_index < total_args_passed) {
2351 BasicType sbt = sig->at(sig_index++)._bt;
2352 if (ValueTypePassFieldsAsArgs && sbt == T_VALUETYPE) {
2353 // Found start of value type in signature
2354 vt_count++;
2355 } else if (ValueTypePassFieldsAsArgs && sbt == T_VOID &&
2356 prev_sbt != T_LONG && prev_sbt != T_DOUBLE) {
2357 // Found end of value type in signature
2358 vt_count--;
2359 assert(vt_count >= 0, "invalid vt_count");
2360 }
2361 bt = adapter_encoding(sbt, vt_count > 0);
2362 prev_sbt = sbt;
2363 }
2364 assert((bt & _basic_type_mask) == bt, "must fit in 4 bits");
2365 value = (value << _basic_type_bits) | bt;
2366 }
2367 ptr[index] = value;
2368 }
2369 assert(vt_count == 0, "invalid vt_count");
2370 }
2371
2435
2436 private:
2437
2438 #ifndef PRODUCT
2439 static int _lookups; // number of calls to lookup
2440 static int _buckets; // number of buckets checked
2441 static int _equals; // number of buckets checked with matching hash
2442 static int _hits; // number of successful lookups
2443 static int _compact; // number of equals calls with compact signature
2444 #endif
2445
2446 AdapterHandlerEntry* bucket(int i) {
2447 return (AdapterHandlerEntry*)BasicHashtable<mtCode>::bucket(i);
2448 }
2449
2450 public:
2451 AdapterHandlerTable()
2452 : BasicHashtable<mtCode>(293, (DumpSharedSpaces ? sizeof(CDSAdapterHandlerEntry) : sizeof(AdapterHandlerEntry))) { }
2453
2454 // Create a new entry suitable for insertion in the table
2455 AdapterHandlerEntry* new_entry(AdapterFingerPrint* fingerprint, address i2c_entry, address c2i_entry, address c2i_value_entry, address c2i_unverified_entry) {
2456 AdapterHandlerEntry* entry = (AdapterHandlerEntry*)BasicHashtable<mtCode>::new_entry(fingerprint->compute_hash());
2457 entry->init(fingerprint, i2c_entry, c2i_entry, c2i_value_entry, c2i_unverified_entry);
2458 if (DumpSharedSpaces) {
2459 ((CDSAdapterHandlerEntry*)entry)->init();
2460 }
2461 return entry;
2462 }
2463
2464 // Insert an entry into the table
2465 void add(AdapterHandlerEntry* entry) {
2466 int index = hash_to_index(entry->hash());
2467 add_entry(index, entry);
2468 }
2469
2470 void free_entry(AdapterHandlerEntry* entry) {
2471 entry->deallocate();
2472 BasicHashtable<mtCode>::free_entry(entry);
2473 }
2474
2475 // Find a entry with the same fingerprint if it exists
2476 AdapterHandlerEntry* lookup(int total_args_passed, const GrowableArray<SigEntry>* sig) {
2477 NOT_PRODUCT(_lookups++);
2478 AdapterFingerPrint fp(total_args_passed, sig);
2479 unsigned int hash = fp.compute_hash();
2480 int index = hash_to_index(hash);
2481 for (AdapterHandlerEntry* e = bucket(index); e != NULL; e = e->next()) {
2482 NOT_PRODUCT(_buckets++);
2483 if (e->hash() == hash) {
2484 NOT_PRODUCT(_equals++);
2485 if (fp.equals(e->fingerprint())) {
2486 #ifndef PRODUCT
2487 if (fp.is_compact()) _compact++;
2488 _hits++;
2489 #endif
2490 return e;
2491 }
2492 }
2493 }
2494 return NULL;
2495 }
2496
2497 #ifndef PRODUCT
2498 void print_statistics() {
2580 _buffer = BufferBlob::create("adapters", AdapterHandlerLibrary_size);
2581 return _buffer;
2582 }
2583
2584 extern "C" void unexpected_adapter_call() {
2585 ShouldNotCallThis();
2586 }
2587
2588 void AdapterHandlerLibrary::initialize() {
2589 if (_adapters != NULL) return;
2590 _adapters = new AdapterHandlerTable();
2591
2592 // Create a special handler for abstract methods. Abstract methods
2593 // are never compiled so an i2c entry is somewhat meaningless, but
2594 // throw AbstractMethodError just in case.
2595 // Pass wrong_method_abstract for the c2i transitions to return
2596 // AbstractMethodError for invalid invocations.
2597 address wrong_method_abstract = SharedRuntime::get_handle_wrong_method_abstract_stub();
2598 _abstract_method_handler = AdapterHandlerLibrary::new_entry(new AdapterFingerPrint(0, NULL),
2599 StubRoutines::throw_AbstractMethodError_entry(),
2600 wrong_method_abstract, wrong_method_abstract, wrong_method_abstract);
2601 }
2602
2603 AdapterHandlerEntry* AdapterHandlerLibrary::new_entry(AdapterFingerPrint* fingerprint,
2604 address i2c_entry,
2605 address c2i_entry,
2606 address c2i_value_entry,
2607 address c2i_unverified_entry) {
2608 return _adapters->new_entry(fingerprint, i2c_entry, c2i_entry, c2i_value_entry, c2i_unverified_entry);
2609 }
2610
2611 AdapterHandlerEntry* AdapterHandlerLibrary::get_adapter(const methodHandle& method) {
2612 AdapterHandlerEntry* entry = get_adapter0(method);
2613 if (method->is_shared()) {
2614 // See comments around Method::link_method()
2615 MutexLocker mu(AdapterHandlerLibrary_lock);
2616 if (method->adapter() == NULL) {
2617 method->update_adapter_trampoline(entry);
2618 }
2619 address trampoline = method->from_compiled_entry();
2620 if (*(int*)trampoline == 0) {
2621 CodeBuffer buffer(trampoline, (int)SharedRuntime::trampoline_size());
2622 MacroAssembler _masm(&buffer);
2623 SharedRuntime::generate_trampoline(&_masm, entry->get_c2i_entry());
2624 assert(*(int*)trampoline != 0, "Instruction(s) for trampoline must not be encoded as zeros.");
2625
2626 if (PrintInterpreter) {
2627 Disassembler::decode(buffer.insts_begin(), buffer.insts_end());
2628 }
2629 }
2630 }
2631
2632 return entry;
2633 }
2634
2635 AdapterHandlerEntry* AdapterHandlerLibrary::get_adapter0(const methodHandle& method) {
2636 // Use customized signature handler. Need to lock around updates to
2637 // the AdapterHandlerTable (it is not safe for concurrent readers
2638 // and a single writer: this could be fixed if it becomes a
2639 // problem).
2640
2641 ResourceMark rm;
2642
2643 NOT_PRODUCT(int insts_size = 0);
2644 AdapterBlob* new_adapter = NULL;
2645 AdapterHandlerEntry* entry = NULL;
2646 AdapterFingerPrint* fingerprint = NULL;
2647 {
2648 MutexLocker mu(AdapterHandlerLibrary_lock);
2649 // make sure data structure is initialized
2650 initialize();
2651
2652 if (method->is_abstract()) {
2653 return _abstract_method_handler;
2654 }
2655
2656 bool has_value_arg = false;
2657 GrowableArray<SigEntry> sig(method->size_of_parameters());
2658 if (!method->is_static()) {
2659 // TODO for now, don't scalarize value type receivers because of interface calls
2660 //has_value_arg |= method->method_holder()->is_value();
2661 SigEntry::add_entry(&sig, T_OBJECT);
2662 }
2663 for (SignatureStream ss(method->signature()); !ss.at_return_type(); ss.next()) {
2664 BasicType bt = ss.type();
2665 if (bt == T_VALUETYPE) {
2666 has_value_arg = true;
2667 bt = T_OBJECT;
2668 }
2669 SigEntry::add_entry(&sig, bt);
2670 }
2671
2672 // Get a description of the compiled java calling convention and the largest used (VMReg) stack slot usage
2673 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sig.length());
2674 int args_on_stack = SharedRuntime::java_calling_convention(&sig, regs);
2675
2676 // Now compute the scalarized calling convention if there are value types in the signature
2677 GrowableArray<SigEntry> sig_cc = sig;
2678 VMRegPair* regs_cc = regs;
2679 SigEntry reserved_entry;
2680 int args_on_stack_cc = args_on_stack;
2681
2682 if (ValueTypePassFieldsAsArgs && has_value_arg) {
2683 MutexUnlocker mul(AdapterHandlerLibrary_lock);
2684 InstanceKlass* holder = method->method_holder();
2685
2686 sig_cc = GrowableArray<SigEntry>(method->size_of_parameters());
2687 if (!method->is_static()) {
2688 // TODO for now, don't scalarize value type receivers because of interface calls
2689 if (false && holder->is_value()) {
2690 sig_cc.appendAll(ValueKlass::cast(holder)->extended_sig());
2691 } else {
2692 SigEntry::add_entry(&sig_cc, T_OBJECT);
2693 }
2694 }
2695 Thread* THREAD = Thread::current();
2696 for (SignatureStream ss(method->signature()); !ss.at_return_type(); ss.next()) {
2697 if (ss.type() == T_VALUETYPE) {
2698 Klass* k = ss.as_klass(Handle(THREAD, holder->class_loader()),
2699 Handle(THREAD, holder->protection_domain()),
2700 SignatureStream::ReturnNull, THREAD);
2701 assert(k != NULL && !HAS_PENDING_EXCEPTION, "value klass should have been pre-loaded");
2702 sig_cc.appendAll(ValueKlass::cast(k)->extended_sig());
2703 } else {
2704 SigEntry::add_entry(&sig_cc, ss.type());
2705 }
2706 }
2707 regs_cc = NEW_RESOURCE_ARRAY(VMRegPair, sig_cc.length() + 2);
2708 args_on_stack_cc = SharedRuntime::java_calling_convention(&sig_cc, regs_cc);
2709
2710 // This stack slot is occupied by the return address with the unscalarized calling
2711 // convention. Don't use it for argument with the scalarized calling convention.
2712 int ret_addr_slot = args_on_stack_cc - args_on_stack;
2713 if (ret_addr_slot > 0) {
2714 // Make sure stack of the scalarized calling convention with
2715 // the reserved entry (2 slots) is 16-byte (4 slots) aligned.
2716 int alignment = StackAlignmentInBytes/VMRegImpl::stack_slot_size;
2717 ret_addr_slot = align_up(ret_addr_slot + 2, alignment) - 2;
2718 // Find index in signature that belongs to return address slot
2719 reserved_entry._offset = 0;
2720 int sig_idx = 0;
2721 for (; sig_idx < sig_cc.length(); ++sig_idx) {
2722 if (SigEntry::skip_value_delimiters(&sig_cc, sig_idx)) {
2723 VMReg first = regs_cc[reserved_entry._offset].first();
2724 if (first->is_stack()) {
2725 // Select a type for the reserved entry that will end up on the stack
2726 reserved_entry._bt = sig_cc.at(sig_idx)._bt;
2727 if ((int)first->reg2stack() == ret_addr_slot) {
2728 break;
2729 }
2730 }
2731 reserved_entry._offset++;
2732 }
2733 }
2734 // Insert reserved entry and re-compute calling convention
2735 SigEntry::insert_reserved_entry(&sig_cc, sig_idx, reserved_entry._bt);
2736 args_on_stack_cc = SharedRuntime::java_calling_convention(&sig_cc, regs_cc);
2737 }
2738 // Upper bound on stack arguments to avoid hitting the argument limit and
2739 // bailing out of compilation ("unsupported incoming calling sequence").
2740 // TODO we need a reasonable limit (flag?) here
2741 if (args_on_stack_cc > 50) {
2742 // Don't scalarize value type arguments
2743 sig_cc = sig;
2744 regs_cc = regs;
2745 args_on_stack_cc = args_on_stack;
2746 }
2747 }
2748
2749 // Lookup method signature's fingerprint
2750 entry = _adapters->lookup(sig_cc.length(), &sig_cc);
2751
2752 #ifdef ASSERT
2753 AdapterHandlerEntry* shared_entry = NULL;
2754 // Start adapter sharing verification only after the VM is booted.
2755 if (VerifyAdapterSharing && (entry != NULL)) {
2756 shared_entry = entry;
2757 entry = NULL;
2758 }
2759 #endif
2760
2761 if (entry != NULL) {
2762 return entry;
2763 }
2764
2765 // Make a C heap allocated version of the fingerprint to store in the adapter
2766 fingerprint = new AdapterFingerPrint(sig_cc.length(), &sig_cc);
2767
2768 // StubRoutines::code2() is initialized after this function can be called. As a result,
2769 // VerifyAdapterCalls and VerifyAdapterSharing can fail if we re-use code that generated
2770 // prior to StubRoutines::code2() being set. Checks refer to checks generated in an I2C
2771 // stub that ensure that an I2C stub is called from an interpreter frame.
2772 bool contains_all_checks = StubRoutines::code2() != NULL;
2773
2774 // Create I2C & C2I handlers
2775 BufferBlob* buf = buffer_blob(); // the temporary code buffer in CodeCache
2776 if (buf != NULL) {
2777 CodeBuffer buffer(buf);
2778 short buffer_locs[20];
2779 buffer.insts()->initialize_shared_locs((relocInfo*)buffer_locs,
2780 sizeof(buffer_locs)/sizeof(relocInfo));
2781
2782 MacroAssembler _masm(&buffer);
2783 entry = SharedRuntime::generate_i2c2i_adapters(&_masm,
2784 args_on_stack,
2785 args_on_stack_cc,
2786 &sig,
2787 regs,
2788 &sig_cc,
2789 regs_cc,
2790 fingerprint,
2791 new_adapter);
2792
2793 if (regs != regs_cc) {
2794 // Save a C heap allocated version of the scalarized signature and store it in the adapter
2795 GrowableArray<SigEntry>* heap_sig = new (ResourceObj::C_HEAP, mtInternal)GrowableArray<SigEntry>(method->size_of_parameters(), true);
2796 heap_sig->appendAll(&sig_cc);
2797 entry->set_sig_cc(heap_sig);
2798 entry->set_res_entry(reserved_entry);
2799 }
2800
2801 #ifdef ASSERT
2802 if (VerifyAdapterSharing) {
2803 if (shared_entry != NULL) {
2804 assert(shared_entry->compare_code(buf->code_begin(), buffer.insts_size()), "code must match");
2805 // Release the one just created and return the original
2806 _adapters->free_entry(entry);
2807 return shared_entry;
2808 } else {
2809 entry->save_code(buf->code_begin(), buffer.insts_size());
2810 }
2811 }
2812 #endif
2813
2814 NOT_PRODUCT(insts_size = buffer.insts_size());
2815 }
2816 if (new_adapter == NULL) {
2817 // CodeCache is full, disable compilation
2818 // Ought to log this but compile log is only per compile thread
2819 // and we're some non descript Java thread.
2820 return NULL; // Out of CodeCache space
2849 char blob_id[256];
2850 jio_snprintf(blob_id,
2851 sizeof(blob_id),
2852 "%s(%s)@" PTR_FORMAT,
2853 new_adapter->name(),
2854 fingerprint->as_string(),
2855 new_adapter->content_begin());
2856 Forte::register_stub(blob_id, new_adapter->content_begin(), new_adapter->content_end());
2857
2858 if (JvmtiExport::should_post_dynamic_code_generated()) {
2859 JvmtiExport::post_dynamic_code_generated(blob_id, new_adapter->content_begin(), new_adapter->content_end());
2860 }
2861 }
2862 return entry;
2863 }
2864
2865 address AdapterHandlerEntry::base_address() {
2866 address base = _i2c_entry;
2867 if (base == NULL) base = _c2i_entry;
2868 assert(base <= _c2i_entry || _c2i_entry == NULL, "");
2869 assert(base <= _c2i_value_entry || _c2i_value_entry == NULL, "");
2870 assert(base <= _c2i_unverified_entry || _c2i_unverified_entry == NULL, "");
2871 return base;
2872 }
2873
2874 void AdapterHandlerEntry::relocate(address new_base) {
2875 address old_base = base_address();
2876 assert(old_base != NULL, "");
2877 ptrdiff_t delta = new_base - old_base;
2878 if (_i2c_entry != NULL)
2879 _i2c_entry += delta;
2880 if (_c2i_entry != NULL)
2881 _c2i_entry += delta;
2882 if (_c2i_value_entry != NULL)
2883 _c2i_value_entry += delta;
2884 if (_c2i_unverified_entry != NULL)
2885 _c2i_unverified_entry += delta;
2886 assert(base_address() == new_base, "");
2887 }
2888
2889
2890 void AdapterHandlerEntry::deallocate() {
2891 delete _fingerprint;
2892 if (_sig_cc != NULL) {
2893 delete _sig_cc;
2894 }
2895 #ifdef ASSERT
2896 if (_saved_code) FREE_C_HEAP_ARRAY(unsigned char, _saved_code);
2897 #endif
2898 }
2899
2900
2901 #ifdef ASSERT
2902 // Capture the code before relocation so that it can be compared
2903 // against other versions. If the code is captured after relocation
2904 // then relative instructions won't be equivalent.
2905 void AdapterHandlerEntry::save_code(unsigned char* buffer, int length) {
2906 _saved_code = NEW_C_HEAP_ARRAY(unsigned char, length, mtCode);
2907 _saved_code_length = length;
2908 memcpy(_saved_code, buffer, length);
2909 }
2910
2911
2912 bool AdapterHandlerEntry::compare_code(unsigned char* buffer, int length) {
2913 if (length != _saved_code_length) {
2914 return false;
2947
2948 ResourceMark rm;
2949 BufferBlob* buf = buffer_blob(); // the temporary code buffer in CodeCache
2950 if (buf != NULL) {
2951 CodeBuffer buffer(buf);
2952 double locs_buf[20];
2953 buffer.insts()->initialize_shared_locs((relocInfo*)locs_buf, sizeof(locs_buf) / sizeof(relocInfo));
2954 MacroAssembler _masm(&buffer);
2955
2956 // Fill in the signature array, for the calling-convention call.
2957 const int total_args_passed = method->size_of_parameters();
2958
2959 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, total_args_passed);
2960 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, total_args_passed);
2961 int i=0;
2962 if (!method->is_static()) // Pass in receiver first
2963 sig_bt[i++] = T_OBJECT;
2964 SignatureStream ss(method->signature());
2965 for (; !ss.at_return_type(); ss.next()) {
2966 BasicType bt = ss.type();
2967 sig_bt[i++] = bt; // Collect remaining bits of signature
2968 if (ss.type() == T_LONG || ss.type() == T_DOUBLE)
2969 sig_bt[i++] = T_VOID; // Longs & doubles take 2 Java slots
2970 }
2971 assert(i == total_args_passed, "");
2972 BasicType ret_type = ss.type();
2973
2974 // Now get the compiled-Java layout as input (or output) arguments.
2975 // NOTE: Stubs for compiled entry points of method handle intrinsics
2976 // are just trampolines so the argument registers must be outgoing ones.
2977 const bool is_outgoing = method->is_method_handle_intrinsic();
2978 int comp_args_on_stack = SharedRuntime::java_calling_convention(sig_bt, regs, total_args_passed, is_outgoing);
2979
2980 // Generate the compiled-to-native wrapper code
2981 nm = SharedRuntime::generate_native_wrapper(&_masm, method, compile_id, sig_bt, regs, ret_type);
2982
2983 if (nm != NULL) {
2984 method->set_code(method, nm);
2985
2986 DirectiveSet* directive = DirectivesStack::getDefaultDirective(CompileBroker::compiler(CompLevel_simple));
3212 AdapterHandlerEntry* a = iter.next();
3213 if (b == CodeCache::find_blob(a->get_i2c_entry())) return true;
3214 }
3215 return false;
3216 }
3217
3218 void AdapterHandlerLibrary::print_handler_on(outputStream* st, const CodeBlob* b) {
3219 AdapterHandlerTableIterator iter(_adapters);
3220 while (iter.has_next()) {
3221 AdapterHandlerEntry* a = iter.next();
3222 if (b == CodeCache::find_blob(a->get_i2c_entry())) {
3223 st->print("Adapter for signature: ");
3224 a->print_adapter_on(tty);
3225 return;
3226 }
3227 }
3228 assert(false, "Should have found handler");
3229 }
3230
3231 void AdapterHandlerEntry::print_adapter_on(outputStream* st) const {
3232 st->print_cr("AHE@" INTPTR_FORMAT ": %s i2c: " INTPTR_FORMAT " c2i: " INTPTR_FORMAT " c2iMH: " INTPTR_FORMAT " c2iUV: " INTPTR_FORMAT,
3233 p2i(this), fingerprint()->as_string(),
3234 p2i(get_i2c_entry()), p2i(get_c2i_entry()), p2i(get_c2i_value_entry()), p2i(get_c2i_unverified_entry()));
3235
3236 }
3237
3238 #if INCLUDE_CDS
3239
3240 void CDSAdapterHandlerEntry::init() {
3241 assert(DumpSharedSpaces, "used during dump time only");
3242 _c2i_entry_trampoline = (address)MetaspaceShared::misc_code_space_alloc(SharedRuntime::trampoline_size());
3243 _adapter_trampoline = (AdapterHandlerEntry**)MetaspaceShared::misc_code_space_alloc(sizeof(AdapterHandlerEntry*));
3244 };
3245
3246 #endif // INCLUDE_CDS
3247
3248
3249 #ifndef PRODUCT
3250
3251 void AdapterHandlerLibrary::print_statistics() {
3252 _adapters->print_statistics();
3253 }
3254
3321
3322 oop new_obj = thread->vm_result();
3323 if (new_obj == NULL) return;
3324
3325 BarrierSet *bs = BarrierSet::barrier_set();
3326 bs->on_slowpath_allocation_exit(thread, new_obj);
3327 }
3328
3329 // We are at a compiled code to interpreter call. We need backing
3330 // buffers for all value type arguments. Allocate an object array to
3331 // hold them (convenient because once we're done with it we don't have
3332 // to worry about freeing it).
3333 JRT_ENTRY(void, SharedRuntime::allocate_value_types(JavaThread* thread, Method* callee_method))
3334 {
3335 assert(ValueTypePassFieldsAsArgs, "no reason to call this");
3336 ResourceMark rm;
3337 JavaThread* THREAD = thread;
3338 methodHandle callee(callee_method);
3339
3340 int nb_slots = 0;
3341 InstanceKlass* holder = callee->method_holder();
3342 // TODO for now, don't scalarize value type receivers because of interface calls
3343 //bool has_value_receiver = !callee->is_static() && holder->is_value();
3344 bool has_value_receiver = false;
3345 if (has_value_receiver) {
3346 nb_slots++;
3347 }
3348 Handle class_loader(THREAD, holder->class_loader());
3349 Handle protection_domain(THREAD, holder->protection_domain());
3350 for (SignatureStream ss(callee->signature()); !ss.at_return_type(); ss.next()) {
3351 if (ss.type() == T_VALUETYPE) {
3352 nb_slots++;
3353 }
3354 }
3355 objArrayOop array_oop = oopFactory::new_objectArray(nb_slots, CHECK);
3356 objArrayHandle array(THREAD, array_oop);
3357 int i = 0;
3358 if (has_value_receiver) {
3359 ValueKlass* vk = ValueKlass::cast(holder);
3360 oop res = vk->allocate_instance(CHECK);
3361 array->obj_at_put(i, res);
3362 i++;
3363 }
3364 for (SignatureStream ss(callee->signature()); !ss.at_return_type(); ss.next()) {
3365 if (ss.type() == T_VALUETYPE) {
3366 Klass* k = ss.as_klass(class_loader, protection_domain, SignatureStream::ReturnNull, THREAD);
3367 assert(k != NULL && !HAS_PENDING_EXCEPTION, "can't resolve klass");
3368 ValueKlass* vk = ValueKlass::cast(k);
3369 oop res = vk->allocate_instance(CHECK);
3370 array->obj_at_put(i, res);
3371 i++;
3372 }
3373 }
3374 thread->set_vm_result(array());
3375 thread->set_vm_result_2(callee()); // TODO: required to keep callee live?
3376 }
3377 JRT_END
3378
3379 // Iterate of the array of heap allocated value types and apply the GC post barrier to all reference fields.
3396 map++;
3397 }
3398 }
3399 }
3400 }
3401 JRT_END
3402
3403 // We're returning from an interpreted method: load each field into a
3404 // register following the calling convention
3405 JRT_LEAF(void, SharedRuntime::load_value_type_fields_in_regs(JavaThread* thread, oopDesc* res))
3406 {
3407 assert(res->klass()->is_value(), "only value types here");
3408 ResourceMark rm;
3409 RegisterMap reg_map(thread);
3410 frame stubFrame = thread->last_frame();
3411 frame callerFrame = stubFrame.sender(®_map);
3412 assert(callerFrame.is_interpreted_frame(), "should be coming from interpreter");
3413
3414 ValueKlass* vk = ValueKlass::cast(res->klass());
3415
3416 const Array<SigEntry>* sig_vk = vk->extended_sig();
3417 const Array<VMRegPair>* regs = vk->return_regs();
3418
3419 if (regs == NULL) {
3420 // The fields of the value klass don't fit in registers, bail out
3421 return;
3422 }
3423
3424 int j = 1;
3425 for (int i = 0; i < sig_vk->length(); i++) {
3426 BasicType bt = sig_vk->at(i)._bt;
3427 if (bt == T_VALUETYPE) {
3428 continue;
3429 }
3430 if (bt == T_VOID) {
3431 if (sig_vk->at(i-1)._bt == T_LONG ||
3432 sig_vk->at(i-1)._bt == T_DOUBLE) {
3433 j++;
3434 }
3435 continue;
3436 }
3437 int off = sig_vk->at(i)._offset;
3438 assert(off > 0, "offset in object should be positive");
3439 VMRegPair pair = regs->at(j);
3440 address loc = reg_map.location(pair.first());
3441 switch(bt) {
3442 case T_BOOLEAN:
3443 *(jboolean*)loc = res->bool_field(off);
3444 break;
3445 case T_CHAR:
3446 *(jchar*)loc = res->char_field(off);
3447 break;
3448 case T_BYTE:
3449 *(jbyte*)loc = res->byte_field(off);
3450 break;
3451 case T_SHORT:
3452 *(jshort*)loc = res->short_field(off);
3453 break;
3454 case T_INT: {
3455 *(jint*)loc = res->int_field(off);
3456 break;
3457 }
3458 case T_LONG:
3459 #ifdef _LP64
3460 *(intptr_t*)loc = res->long_field(off);
3461 #else
3462 Unimplemented();
3463 #endif
3464 break;
3465 case T_OBJECT:
3466 case T_ARRAY: {
3467 *(oop*)loc = res->obj_field(off);
3468 break;
3469 }
3470 case T_FLOAT:
3471 *(jfloat*)loc = res->float_field(off);
3472 break;
3473 case T_DOUBLE:
3474 *(jdouble*)loc = res->double_field(off);
3475 break;
3476 default:
3477 ShouldNotReachHere();
3478 }
3479 j++;
3480 }
3481 assert(j == regs->length(), "missed a field?");
3482
3483 #ifdef ASSERT
3484 VMRegPair pair = regs->at(0);
3485 address loc = reg_map.location(pair.first());
3486 assert(*(oopDesc**)loc == res, "overwritten object");
3487 #endif
3488
3489 thread->set_vm_result(res);
3490 }
3491 JRT_END
3492
3493 // We've returned to an interpreted method, the interpreter needs a
3494 // reference to a value type instance. Allocate it and initialize it
3501 frame callerFrame = stubFrame.sender(®_map);
3502
3503 #ifdef ASSERT
3504 ValueKlass* verif_vk = ValueKlass::returned_value_klass(reg_map);
3505 #endif
3506
3507 if (!is_set_nth_bit(res, 0)) {
3508 // We're not returning with value type fields in registers (the
3509 // calling convention didn't allow it for this value klass)
3510 assert(!Metaspace::contains((void*)res), "should be oop or pointer in buffer area");
3511 thread->set_vm_result((oopDesc*)res);
3512 assert(verif_vk == NULL, "broken calling convention");
3513 return;
3514 }
3515
3516 clear_nth_bit(res, 0);
3517 ValueKlass* vk = (ValueKlass*)res;
3518 assert(verif_vk == vk, "broken calling convention");
3519 assert(Metaspace::contains((void*)res), "should be klass");
3520
3521 // Allocate handles for every oop field so they are safe in case of
3522 // a safepoint when allocating
3523 GrowableArray<Handle> handles;
3524 vk->save_oop_fields(reg_map, handles);
3525
3526 // It's unsafe to safepoint until we are here
3527 JRT_BLOCK;
3528 {
3529 Thread* THREAD = thread;
3530 oop vt = vk->realloc_result(reg_map, handles, CHECK);
3531 thread->set_vm_result(vt);
3532 }
3533 JRT_BLOCK_END;
3534 }
3535 JRT_END
3536
|