src/share/vm/runtime/sharedRuntime.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File 8026478 Sdiff src/share/vm/runtime

src/share/vm/runtime/sharedRuntime.cpp

Print this page




2205   }
2206 
2207  public:
2208   AdapterHandlerTable()
2209     : BasicHashtable<mtCode>(293, sizeof(AdapterHandlerEntry)) { }
2210 
2211   // Create a new entry suitable for insertion in the table
2212   AdapterHandlerEntry* new_entry(AdapterFingerPrint* fingerprint, address i2c_entry, address c2i_entry, address c2i_unverified_entry) {
2213     AdapterHandlerEntry* entry = (AdapterHandlerEntry*)BasicHashtable<mtCode>::new_entry(fingerprint->compute_hash());
2214     entry->init(fingerprint, i2c_entry, c2i_entry, c2i_unverified_entry);
2215     return entry;
2216   }
2217 
2218   // Insert an entry into the table
2219   void add(AdapterHandlerEntry* entry) {
2220     int index = hash_to_index(entry->hash());
2221     add_entry(index, entry);
2222   }
2223 
2224   void free_entry(AdapterHandlerEntry* entry) {




















2225     entry->deallocate();
2226     BasicHashtable<mtCode>::free_entry(entry);
2227   }
2228 
2229   // Find a entry with the same fingerprint if it exists
2230   AdapterHandlerEntry* lookup(int total_args_passed, BasicType* sig_bt) {
2231     NOT_PRODUCT(_lookups++);
2232     AdapterFingerPrint fp(total_args_passed, sig_bt);
2233     unsigned int hash = fp.compute_hash();
2234     int index = hash_to_index(hash);
2235     for (AdapterHandlerEntry* e = bucket(index); e != NULL; e = e->next()) {
2236       NOT_PRODUCT(_buckets++);
2237       if (e->hash() == hash) {
2238         NOT_PRODUCT(_equals++);
2239         if (fp.equals(e->fingerprint())) {
2240 #ifndef PRODUCT
2241           if (fp.is_compact()) _compact++;
2242           _hits++;
2243 #endif
2244           return e;


2389     // Fill in the signature array, for the calling-convention call.
2390     int total_args_passed = method->size_of_parameters(); // All args on stack
2391 
2392     BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, total_args_passed);
2393     VMRegPair* regs   = NEW_RESOURCE_ARRAY(VMRegPair, total_args_passed);
2394     int i = 0;
2395     if (!method->is_static())  // Pass in receiver first
2396       sig_bt[i++] = T_OBJECT;
2397     for (SignatureStream ss(method->signature()); !ss.at_return_type(); ss.next()) {
2398       sig_bt[i++] = ss.type();  // Collect remaining bits of signature
2399       if (ss.type() == T_LONG || ss.type() == T_DOUBLE)
2400         sig_bt[i++] = T_VOID;   // Longs & doubles take 2 Java slots
2401     }
2402     assert(i == total_args_passed, "");
2403 
2404     // Lookup method signature's fingerprint
2405     entry = _adapters->lookup(total_args_passed, sig_bt);
2406 
2407 #ifdef ASSERT
2408     AdapterHandlerEntry* shared_entry = NULL;
2409     if (VerifyAdapterSharing && entry != NULL) {

2410       shared_entry = entry;
2411       entry = NULL;
2412     }
2413 #endif
2414 
2415     if (entry != NULL) {








2416       return entry;
2417     }

2418 
2419     // Get a description of the compiled java calling convention and the largest used (VMReg) stack slot usage
2420     int comp_args_on_stack = SharedRuntime::java_calling_convention(sig_bt, regs, total_args_passed, false);
2421 
2422     // Make a C heap allocated version of the fingerprint to store in the adapter
2423     fingerprint = new AdapterFingerPrint(total_args_passed, sig_bt);
2424 
2425     // Create I2C & C2I handlers
2426 
2427     BufferBlob* buf = buffer_blob(); // the temporary code buffer in CodeCache
2428     if (buf != NULL) {
2429       CodeBuffer buffer(buf);
2430       short buffer_locs[20];
2431       buffer.insts()->initialize_shared_locs((relocInfo*)buffer_locs,
2432                                              sizeof(buffer_locs)/sizeof(relocInfo));
2433       MacroAssembler _masm(&buffer);
2434 


2435       entry = SharedRuntime::generate_i2c2i_adapters(&_masm,
2436                                                      total_args_passed,
2437                                                      comp_args_on_stack,
2438                                                      sig_bt,
2439                                                      regs,
2440                                                      fingerprint);
2441 
2442 #ifdef ASSERT
2443       if (VerifyAdapterSharing) {
2444         if (shared_entry != NULL) {
2445           assert(shared_entry->compare_code(buf->code_begin(), buffer.insts_size(), total_args_passed, sig_bt),
2446                  "code must match");
2447           // Release the one just created and return the original
2448           _adapters->free_entry(entry);
2449           return shared_entry;
2450         } else  {
2451           entry->save_code(buf->code_begin(), buffer.insts_size(), total_args_passed, sig_bt);
2452         }
2453       }
2454 #endif
2455 
2456       B = AdapterBlob::create(&buffer);
2457       NOT_PRODUCT(insts_size = buffer.insts_size());
2458     }
2459     if (B == NULL) {
2460       // CodeCache is full, disable compilation
2461       // Ought to log this but compile log is only per compile thread
2462       // and we're some non descript Java thread.
2463       MutexUnlocker mu(AdapterHandlerLibrary_lock);
2464       CompileBroker::handle_full_code_cache();
2465       return NULL; // Out of CodeCache space
2466     }
2467     entry->relocate(B->content_begin());
2468 #ifndef PRODUCT
2469     // debugging suppport
2470     if (PrintAdapterHandlers || PrintStubCode) {
2471       ttyLocker ttyl;


2513 }
2514 
2515 void AdapterHandlerEntry::relocate(address new_base) {
2516   address old_base = base_address();
2517   assert(old_base != NULL, "");
2518   ptrdiff_t delta = new_base - old_base;
2519   if (_i2c_entry != NULL)
2520     _i2c_entry += delta;
2521   if (_c2i_entry != NULL)
2522     _c2i_entry += delta;
2523   if (_c2i_unverified_entry != NULL)
2524     _c2i_unverified_entry += delta;
2525   assert(base_address() == new_base, "");
2526 }
2527 
2528 
2529 void AdapterHandlerEntry::deallocate() {
2530   delete _fingerprint;
2531 #ifdef ASSERT
2532   if (_saved_code) FREE_C_HEAP_ARRAY(unsigned char, _saved_code, mtCode);
2533   if (_saved_sig)  FREE_C_HEAP_ARRAY(Basictype, _saved_sig, mtCode);
2534 #endif
2535 }
2536 
2537 
2538 #ifdef ASSERT
2539 // Capture the code before relocation so that it can be compared
2540 // against other versions.  If the code is captured after relocation
2541 // then relative instructions won't be equivalent.
2542 void AdapterHandlerEntry::save_code(unsigned char* buffer, int length, int total_args_passed, BasicType* sig_bt) {
2543   _saved_code = NEW_C_HEAP_ARRAY(unsigned char, length, mtCode);
2544   _code_length = length;
2545   memcpy(_saved_code, buffer, length);
2546   _total_args_passed = total_args_passed;
2547   _saved_sig = NEW_C_HEAP_ARRAY(BasicType, _total_args_passed, mtCode);
2548   memcpy(_saved_sig, sig_bt, _total_args_passed * sizeof(BasicType));
2549 }
2550 
2551 
2552 bool AdapterHandlerEntry::compare_code(unsigned char* buffer, int length, int total_args_passed, BasicType* sig_bt) {
2553   if (length != _code_length) {
2554     return false;
2555   }
2556   for (int i = 0; i < length; i++) {
2557     if (buffer[i] != _saved_code[i]) {
2558       return false;
2559     }
2560   }
2561   return true;
2562 }
2563 #endif
2564 
2565 
2566 // Create a native wrapper for this native method.  The wrapper converts the
2567 // java compiled calling convention to the native convention, handlizes
2568 // arguments, and transitions to native.  On return from the native we transition
2569 // back to java blocking if a safepoint is in progress.
2570 nmethod *AdapterHandlerLibrary::create_native_wrapper(methodHandle method, int compile_id) {
2571   ResourceMark rm;
2572   nmethod* nm = NULL;
2573 
2574   assert(method->is_native(), "must be native");
2575   assert(method->is_method_handle_intrinsic() ||
2576          method->has_native_function(), "must have something valid to call!");
2577 
2578   {
2579     // perform the work while holding the lock, but perform any printing outside the lock
2580     MutexLocker mu(AdapterHandlerLibrary_lock);
2581     // See if somebody beat us to it




2205   }
2206 
2207  public:
2208   AdapterHandlerTable()
2209     : BasicHashtable<mtCode>(293, sizeof(AdapterHandlerEntry)) { }
2210 
2211   // Create a new entry suitable for insertion in the table
2212   AdapterHandlerEntry* new_entry(AdapterFingerPrint* fingerprint, address i2c_entry, address c2i_entry, address c2i_unverified_entry) {
2213     AdapterHandlerEntry* entry = (AdapterHandlerEntry*)BasicHashtable<mtCode>::new_entry(fingerprint->compute_hash());
2214     entry->init(fingerprint, i2c_entry, c2i_entry, c2i_unverified_entry);
2215     return entry;
2216   }
2217 
2218   // Insert an entry into the table
2219   void add(AdapterHandlerEntry* entry) {
2220     int index = hash_to_index(entry->hash());
2221     add_entry(index, entry);
2222   }
2223 
2224   void free_entry(AdapterHandlerEntry* entry) {
2225     AdapterFingerPrint* finger_print = entry->fingerprint();
2226     unsigned int hash = entry->fingerprint()->compute_hash();
2227     int index = hash_to_index(hash);
2228     AdapterHandlerEntry* previous = NULL;
2229 
2230     // Loop over entries of the bucket and update linked list
2231     for (AdapterHandlerEntry* e = bucket(index); e != NULL; e = e->next()) {
2232       if (e->hash() == hash) {
2233         if (finger_print->equals(e->fingerprint())) {
2234           if (previous != NULL) {
2235             previous->set_next(e->next());
2236           } else {
2237             set_entry(index, e->next());
2238           }
2239         }
2240       }
2241       previous = e;
2242     }
2243 
2244     // Do the actual de-allocation
2245     entry->deallocate();
2246     BasicHashtable<mtCode>::free_entry(entry);
2247   }
2248 
2249   // Find a entry with the same fingerprint if it exists
2250   AdapterHandlerEntry* lookup(int total_args_passed, BasicType* sig_bt) {
2251     NOT_PRODUCT(_lookups++);
2252     AdapterFingerPrint fp(total_args_passed, sig_bt);
2253     unsigned int hash = fp.compute_hash();
2254     int index = hash_to_index(hash);
2255     for (AdapterHandlerEntry* e = bucket(index); e != NULL; e = e->next()) {
2256       NOT_PRODUCT(_buckets++);
2257       if (e->hash() == hash) {
2258         NOT_PRODUCT(_equals++);
2259         if (fp.equals(e->fingerprint())) {
2260 #ifndef PRODUCT
2261           if (fp.is_compact()) _compact++;
2262           _hits++;
2263 #endif
2264           return e;


2409     // Fill in the signature array, for the calling-convention call.
2410     int total_args_passed = method->size_of_parameters(); // All args on stack
2411 
2412     BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, total_args_passed);
2413     VMRegPair* regs   = NEW_RESOURCE_ARRAY(VMRegPair, total_args_passed);
2414     int i = 0;
2415     if (!method->is_static())  // Pass in receiver first
2416       sig_bt[i++] = T_OBJECT;
2417     for (SignatureStream ss(method->signature()); !ss.at_return_type(); ss.next()) {
2418       sig_bt[i++] = ss.type();  // Collect remaining bits of signature
2419       if (ss.type() == T_LONG || ss.type() == T_DOUBLE)
2420         sig_bt[i++] = T_VOID;   // Longs & doubles take 2 Java slots
2421     }
2422     assert(i == total_args_passed, "");
2423 
2424     // Lookup method signature's fingerprint
2425     entry = _adapters->lookup(total_args_passed, sig_bt);
2426 
2427 #ifdef ASSERT
2428     AdapterHandlerEntry* shared_entry = NULL;
2429     // Start adapter sharing verification only after the VM is booted.
2430     if (VerifyAdapterSharing && (entry != NULL) && entry->contains_all_checks()) {
2431       shared_entry = entry;
2432       entry = NULL;
2433     }
2434 #endif
2435 
2436     if (entry != NULL) {
2437       // Re-use generated adapter only if VM is booted (StubRoutines::code2() != NULL
2438       // or VerifyAdapterCalls is false. The reason is that gen_i2c_adapter() generates
2439       // code based on the existence of StubRoutines::code1() and StubRoutines::code2().
2440       // Since these fields can be initialized after adapters are generated, VerifyAdapterCalls
2441       // potentially reports a false error and/or VerifyAdapterSharing will fail.
2442       if (!entry->contains_all_checks()) {
2443         _adapters->free_entry(entry);
2444       } else {
2445         return entry;
2446       }
2447     }
2448 
2449     // Get a description of the compiled java calling convention and the largest used (VMReg) stack slot usage
2450     int comp_args_on_stack = SharedRuntime::java_calling_convention(sig_bt, regs, total_args_passed, false);
2451 
2452     // Make a C heap allocated version of the fingerprint to store in the adapter
2453     fingerprint = new AdapterFingerPrint(total_args_passed, sig_bt);
2454 
2455     // Create I2C & C2I handlers

2456     BufferBlob* buf = buffer_blob(); // the temporary code buffer in CodeCache
2457     if (buf != NULL) {
2458       CodeBuffer buffer(buf);
2459       short buffer_locs[20];
2460       buffer.insts()->initialize_shared_locs((relocInfo*)buffer_locs,
2461                                              sizeof(buffer_locs)/sizeof(relocInfo));

2462 
2463       bool contains_all_checks = (StubRoutines::code2() != NULL) || !VerifyAdapterCalls;
2464       MacroAssembler _masm(&buffer);
2465       entry = SharedRuntime::generate_i2c2i_adapters(&_masm,
2466                                                      total_args_passed,
2467                                                      comp_args_on_stack,
2468                                                      sig_bt,
2469                                                      regs,
2470                                                      fingerprint);
2471       entry->set_contains_all_checks(contains_all_checks);
2472 #ifdef ASSERT
2473       if (VerifyAdapterSharing) {
2474         if (shared_entry != NULL) {
2475           assert(shared_entry->compare_code(buf->code_begin(), buffer.insts_size()), "code must match");

2476           // Release the one just created and return the original
2477           _adapters->free_entry(entry);
2478           return shared_entry;
2479         } else  {
2480           entry->save_code(buf->code_begin(), buffer.insts_size());
2481         }
2482       }
2483 #endif
2484 
2485       B = AdapterBlob::create(&buffer);
2486       NOT_PRODUCT(insts_size = buffer.insts_size());
2487     }
2488     if (B == NULL) {
2489       // CodeCache is full, disable compilation
2490       // Ought to log this but compile log is only per compile thread
2491       // and we're some non descript Java thread.
2492       MutexUnlocker mu(AdapterHandlerLibrary_lock);
2493       CompileBroker::handle_full_code_cache();
2494       return NULL; // Out of CodeCache space
2495     }
2496     entry->relocate(B->content_begin());
2497 #ifndef PRODUCT
2498     // debugging suppport
2499     if (PrintAdapterHandlers || PrintStubCode) {
2500       ttyLocker ttyl;


2542 }
2543 
2544 void AdapterHandlerEntry::relocate(address new_base) {
2545   address old_base = base_address();
2546   assert(old_base != NULL, "");
2547   ptrdiff_t delta = new_base - old_base;
2548   if (_i2c_entry != NULL)
2549     _i2c_entry += delta;
2550   if (_c2i_entry != NULL)
2551     _c2i_entry += delta;
2552   if (_c2i_unverified_entry != NULL)
2553     _c2i_unverified_entry += delta;
2554   assert(base_address() == new_base, "");
2555 }
2556 
2557 
2558 void AdapterHandlerEntry::deallocate() {
2559   delete _fingerprint;
2560 #ifdef ASSERT
2561   if (_saved_code) FREE_C_HEAP_ARRAY(unsigned char, _saved_code, mtCode);

2562 #endif
2563 }
2564 
2565 
2566 #ifdef ASSERT
2567 // Capture the code before relocation so that it can be compared
2568 // against other versions.  If the code is captured after relocation
2569 // then relative instructions won't be equivalent.
2570 void AdapterHandlerEntry::save_code(unsigned char* buffer, int length) {
2571   _saved_code = NEW_C_HEAP_ARRAY(unsigned char, length, mtCode);
2572   _saved_code_length = length;
2573   memcpy(_saved_code, buffer, length);



2574 }
2575 
2576 
2577 bool AdapterHandlerEntry::compare_code(unsigned char* buffer, int length) {
2578   if (length != _saved_code_length) {




2579     return false;
2580   }
2581 
2582   return (memcmp(buffer, _saved_code, length) == 0) ? true : false;
2583 }
2584 #endif
2585 
2586 
2587 // Create a native wrapper for this native method.  The wrapper converts the
2588 // java compiled calling convention to the native convention, handlizes
2589 // arguments, and transitions to native.  On return from the native we transition
2590 // back to java blocking if a safepoint is in progress.
2591 nmethod *AdapterHandlerLibrary::create_native_wrapper(methodHandle method, int compile_id) {
2592   ResourceMark rm;
2593   nmethod* nm = NULL;
2594 
2595   assert(method->is_native(), "must be native");
2596   assert(method->is_method_handle_intrinsic() ||
2597          method->has_native_function(), "must have something valid to call!");
2598 
2599   {
2600     // perform the work while holding the lock, but perform any printing outside the lock
2601     MutexLocker mu(AdapterHandlerLibrary_lock);
2602     // See if somebody beat us to it


src/share/vm/runtime/sharedRuntime.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File