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