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 | 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 // Start adapter sharing verification only after the VM is booted. 2410 if (VerifyAdapterSharing && (entry != NULL) && entry->contains_all_checks()) { 2411 shared_entry = entry; 2412 entry = NULL; 2413 } 2414 #endif 2415 2416 if (entry != NULL) { 2417 // Re-use generated adapter only if VM is booted (StubRoutines::code2() != NULL 2418 // or VerifyAdapterCalls is false. The reason is that gen_i2c_adapter() generates 2419 // code based on the existence of StubRoutines::code1() and StubRoutines::code2(). 2420 // Since these fields can be initialized after adapters are generated, VerifyAdapterCalls 2421 // potentially reports a false error and/or VerifyAdapterSharing will fail. 2422 if (!entry->contains_all_checks()) { 2423 _adapters->free_entry(entry); 2424 } else { 2425 return entry; 2426 } 2427 } 2428 2429 // Get a description of the compiled java calling convention and the largest used (VMReg) stack slot usage 2430 int comp_args_on_stack = SharedRuntime::java_calling_convention(sig_bt, regs, total_args_passed, false); 2431 2432 // Make a C heap allocated version of the fingerprint to store in the adapter 2433 fingerprint = new AdapterFingerPrint(total_args_passed, sig_bt); 2434 2435 // Create I2C & C2I handlers 2436 2437 BufferBlob* buf = buffer_blob(); // the temporary code buffer in CodeCache 2438 if (buf != NULL) { 2439 CodeBuffer buffer(buf); 2440 short buffer_locs[20]; 2441 buffer.insts()->initialize_shared_locs((relocInfo*)buffer_locs, 2442 sizeof(buffer_locs)/sizeof(relocInfo)); 2443 2444 bool contains_all_checks = (StubRoutines::code2() != NULL) || !VerifyAdapterCalls; 2445 MacroAssembler _masm(&buffer); 2446 entry = SharedRuntime::generate_i2c2i_adapters(&_masm, 2447 total_args_passed, 2448 comp_args_on_stack, 2449 sig_bt, 2450 regs, 2451 fingerprint); 2452 entry->set_contains_all_checks(contains_all_checks); 2453 #ifdef ASSERT 2454 if (VerifyAdapterSharing) { 2455 if (shared_entry != NULL) { 2456 assert(shared_entry->compare_code(buf->code_begin(), buffer.insts_size()), "code must match"); 2457 // Release the one just created and return the original 2458 _adapters->free_entry(entry); 2459 return shared_entry; 2460 } else { 2461 entry->save_code(buf->code_begin(), buffer.insts_size()); 2462 } 2463 } 2464 #endif 2465 2466 B = AdapterBlob::create(&buffer); 2467 NOT_PRODUCT(insts_size = buffer.insts_size()); 2468 } 2469 if (B == NULL) { 2470 // CodeCache is full, disable compilation 2471 // Ought to log this but compile log is only per compile thread 2472 // and we're some non descript Java thread. 2473 MutexUnlocker mu(AdapterHandlerLibrary_lock); 2474 CompileBroker::handle_full_code_cache(); 2475 return NULL; // Out of CodeCache space 2476 } 2477 entry->relocate(B->content_begin()); 2478 #ifndef PRODUCT 2479 // debugging suppport 2480 if (PrintAdapterHandlers || PrintStubCode) { 2481 ttyLocker ttyl; 2523 } 2524 2525 void AdapterHandlerEntry::relocate(address new_base) { 2526 address old_base = base_address(); 2527 assert(old_base != NULL, ""); 2528 ptrdiff_t delta = new_base - old_base; 2529 if (_i2c_entry != NULL) 2530 _i2c_entry += delta; 2531 if (_c2i_entry != NULL) 2532 _c2i_entry += delta; 2533 if (_c2i_unverified_entry != NULL) 2534 _c2i_unverified_entry += delta; 2535 assert(base_address() == new_base, ""); 2536 } 2537 2538 2539 void AdapterHandlerEntry::deallocate() { 2540 delete _fingerprint; 2541 #ifdef ASSERT 2542 if (_saved_code) FREE_C_HEAP_ARRAY(unsigned char, _saved_code, mtCode); 2543 #endif 2544 } 2545 2546 2547 #ifdef ASSERT 2548 // Capture the code before relocation so that it can be compared 2549 // against other versions. If the code is captured after relocation 2550 // then relative instructions won't be equivalent. 2551 void AdapterHandlerEntry::save_code(unsigned char* buffer, int length) { 2552 _saved_code = NEW_C_HEAP_ARRAY(unsigned char, length, mtCode); 2553 _saved_code_length = length; 2554 memcpy(_saved_code, buffer, length); 2555 } 2556 2557 2558 bool AdapterHandlerEntry::compare_code(unsigned char* buffer, int length) { 2559 if (length != _saved_code_length) { 2560 return false; 2561 } 2562 2563 return (memcmp(buffer, _saved_code, length) == 0) ? true : false; 2564 } 2565 #endif 2566 2567 2568 // Create a native wrapper for this native method. The wrapper converts the 2569 // java compiled calling convention to the native convention, handlizes 2570 // arguments, and transitions to native. On return from the native we transition 2571 // back to java blocking if a safepoint is in progress. 2572 nmethod *AdapterHandlerLibrary::create_native_wrapper(methodHandle method, int compile_id) { 2573 ResourceMark rm; 2574 nmethod* nm = NULL; 2575 2576 assert(method->is_native(), "must be native"); 2577 assert(method->is_method_handle_intrinsic() || 2578 method->has_native_function(), "must have something valid to call!"); 2579 2580 { 2581 // perform the work while holding the lock, but perform any printing outside the lock 2582 MutexLocker mu(AdapterHandlerLibrary_lock); 2583 // See if somebody beat us to it |