hotspot/src/share/vm/runtime/sharedRuntime.cpp
changeset 4755 eee57ea6d910
parent 4753 9e7bcf214f71
child 4891 7c8755dd5bb2
child 4764 d15bb22d4d39
equal deleted inserted replaced
4754:8aef16f24e16 4755:eee57ea6d910
  1804 // A simple wrapper class around the calling convention information
  1804 // A simple wrapper class around the calling convention information
  1805 // that allows sharing of adapters for the same calling convention.
  1805 // that allows sharing of adapters for the same calling convention.
  1806 class AdapterFingerPrint : public CHeapObj {
  1806 class AdapterFingerPrint : public CHeapObj {
  1807  private:
  1807  private:
  1808   union {
  1808   union {
  1809     signed char  _compact[12];
  1809     int  _compact[3];
  1810     int          _compact_int[3];
  1810     int* _fingerprint;
  1811     intptr_t*    _fingerprint;
       
  1812   } _value;
  1811   } _value;
  1813   int _length; // A negative length indicates that _value._fingerprint is the array.
  1812   int _length; // A negative length indicates the fingerprint is in the compact form,
  1814                // Otherwise it's in the compact form.
  1813                // Otherwise _value._fingerprint is the array.
       
  1814 
       
  1815   // Remap BasicTypes that are handled equivalently by the adapters.
       
  1816   // These are correct for the current system but someday it might be
       
  1817   // necessary to make this mapping platform dependent.
       
  1818   static BasicType adapter_encoding(BasicType in) {
       
  1819     assert((~0xf & in) == 0, "must fit in 4 bits");
       
  1820     switch(in) {
       
  1821       case T_BOOLEAN:
       
  1822       case T_BYTE:
       
  1823       case T_SHORT:
       
  1824       case T_CHAR:
       
  1825         // There are all promoted to T_INT in the calling convention
       
  1826         return T_INT;
       
  1827 
       
  1828       case T_OBJECT:
       
  1829       case T_ARRAY:
       
  1830         if (!TaggedStackInterpreter) {
       
  1831 #ifdef _LP64
       
  1832           return T_LONG;
       
  1833 #else
       
  1834           return T_INT;
       
  1835 #endif
       
  1836         }
       
  1837         return T_OBJECT;
       
  1838 
       
  1839       case T_INT:
       
  1840       case T_LONG:
       
  1841       case T_FLOAT:
       
  1842       case T_DOUBLE:
       
  1843       case T_VOID:
       
  1844         return in;
       
  1845 
       
  1846       default:
       
  1847         ShouldNotReachHere();
       
  1848         return T_CONFLICT;
       
  1849     }
       
  1850   }
  1815 
  1851 
  1816  public:
  1852  public:
  1817   AdapterFingerPrint(int total_args_passed, VMRegPair* regs) {
  1853   AdapterFingerPrint(int total_args_passed, BasicType* sig_bt) {
  1818     assert(sizeof(_value._compact) == sizeof(_value._compact_int), "must match");
  1854     // The fingerprint is based on the BasicType signature encoded
  1819     _length = total_args_passed * 2;
  1855     // into an array of ints with four entries per int.
  1820     if (_length < (int)sizeof(_value._compact)) {
  1856     int* ptr;
  1821       _value._compact_int[0] = _value._compact_int[1] = _value._compact_int[2] = 0;
  1857     int len = (total_args_passed + 3) >> 2;
       
  1858     if (len <= (int)(sizeof(_value._compact) / sizeof(int))) {
       
  1859       _value._compact[0] = _value._compact[1] = _value._compact[2] = 0;
  1822       // Storing the signature encoded as signed chars hits about 98%
  1860       // Storing the signature encoded as signed chars hits about 98%
  1823       // of the time.
  1861       // of the time.
  1824       signed char* ptr = _value._compact;
  1862       _length = -len;
  1825       int o = 0;
  1863       ptr = _value._compact;
  1826       for (int i = 0; i < total_args_passed; i++) {
  1864     } else {
  1827         VMRegPair pair = regs[i];
  1865       _length = len;
  1828         intptr_t v1 = pair.first()->value();
  1866       _value._fingerprint = NEW_C_HEAP_ARRAY(int, _length);
  1829         intptr_t v2 = pair.second()->value();
  1867       ptr = _value._fingerprint;
  1830         if (v1 == (signed char) v1 &&
  1868     }
  1831             v2 == (signed char) v2) {
  1869 
  1832           _value._compact[o++] = v1;
  1870     // Now pack the BasicTypes with 4 per int
  1833           _value._compact[o++] = v2;
  1871     int sig_index = 0;
  1834         } else {
  1872     for (int index = 0; index < len; index++) {
  1835           goto big;
  1873       int value = 0;
       
  1874       for (int byte = 0; byte < 4; byte++) {
       
  1875         if (sig_index < total_args_passed) {
       
  1876           value = (value << 4) | adapter_encoding(sig_bt[sig_index++]);
  1836         }
  1877         }
  1837       }
  1878       }
  1838       _length = -_length;
  1879       ptr[index] = value;
  1839       return;
  1880     }
  1840     }
       
  1841   big:
       
  1842     _value._fingerprint = NEW_C_HEAP_ARRAY(intptr_t, _length);
       
  1843     int o = 0;
       
  1844     for (int i = 0; i < total_args_passed; i++) {
       
  1845       VMRegPair pair = regs[i];
       
  1846       intptr_t v1 = pair.first()->value();
       
  1847       intptr_t v2 = pair.second()->value();
       
  1848       _value._fingerprint[o++] = v1;
       
  1849       _value._fingerprint[o++] = v2;
       
  1850     }
       
  1851   }
       
  1852 
       
  1853   AdapterFingerPrint(AdapterFingerPrint* orig) {
       
  1854     _length = orig->_length;
       
  1855     _value = orig->_value;
       
  1856     // take ownership of any storage by destroying the length
       
  1857     orig->_length = 0;
       
  1858   }
  1881   }
  1859 
  1882 
  1860   ~AdapterFingerPrint() {
  1883   ~AdapterFingerPrint() {
  1861     if (_length > 0) {
  1884     if (_length > 0) {
  1862       FREE_C_HEAP_ARRAY(int, _value._fingerprint);
  1885       FREE_C_HEAP_ARRAY(int, _value._fingerprint);
  1863     }
  1886     }
  1864   }
  1887   }
  1865 
  1888 
  1866   AdapterFingerPrint* allocate() {
  1889   int value(int index) {
  1867     return new AdapterFingerPrint(this);
       
  1868   }
       
  1869 
       
  1870   intptr_t value(int index) {
       
  1871     if (_length < 0) {
  1890     if (_length < 0) {
  1872       return _value._compact[index];
  1891       return _value._compact[index];
  1873     }
  1892     }
  1874     return _value._fingerprint[index];
  1893     return _value._fingerprint[index];
  1875   }
  1894   }
  1881   bool is_compact() {
  1900   bool is_compact() {
  1882     return _length <= 0;
  1901     return _length <= 0;
  1883   }
  1902   }
  1884 
  1903 
  1885   unsigned int compute_hash() {
  1904   unsigned int compute_hash() {
  1886     intptr_t hash = 0;
  1905     int hash = 0;
  1887     for (int i = 0; i < length(); i++) {
  1906     for (int i = 0; i < length(); i++) {
  1888       intptr_t v = value(i);
  1907       int v = value(i);
  1889       hash = (hash << 8) ^ v ^ (hash >> 5);
  1908       hash = (hash << 8) ^ v ^ (hash >> 5);
  1890     }
  1909     }
  1891     return (unsigned int)hash;
  1910     return (unsigned int)hash;
  1892   }
  1911   }
  1893 
  1912 
  1902   bool equals(AdapterFingerPrint* other) {
  1921   bool equals(AdapterFingerPrint* other) {
  1903     if (other->_length != _length) {
  1922     if (other->_length != _length) {
  1904       return false;
  1923       return false;
  1905     }
  1924     }
  1906     if (_length < 0) {
  1925     if (_length < 0) {
  1907       return _value._compact_int[0] == other->_value._compact_int[0] &&
  1926       return _value._compact[0] == other->_value._compact[0] &&
  1908              _value._compact_int[1] == other->_value._compact_int[1] &&
  1927              _value._compact[1] == other->_value._compact[1] &&
  1909              _value._compact_int[2] == other->_value._compact_int[2];
  1928              _value._compact[2] == other->_value._compact[2];
  1910     } else {
  1929     } else {
  1911       for (int i = 0; i < _length; i++) {
  1930       for (int i = 0; i < _length; i++) {
  1912         if (_value._fingerprint[i] != other->_value._fingerprint[i]) {
  1931         if (_value._fingerprint[i] != other->_value._fingerprint[i]) {
  1913           return false;
  1932           return false;
  1914         }
  1933         }
  1952   void add(AdapterHandlerEntry* entry) {
  1971   void add(AdapterHandlerEntry* entry) {
  1953     int index = hash_to_index(entry->hash());
  1972     int index = hash_to_index(entry->hash());
  1954     add_entry(index, entry);
  1973     add_entry(index, entry);
  1955   }
  1974   }
  1956 
  1975 
       
  1976   void free_entry(AdapterHandlerEntry* entry) {
       
  1977     entry->deallocate();
       
  1978     BasicHashtable::free_entry(entry);
       
  1979   }
       
  1980 
  1957   // Find a entry with the same fingerprint if it exists
  1981   // Find a entry with the same fingerprint if it exists
  1958   AdapterHandlerEntry* lookup(int total_args_passed, VMRegPair* regs) {
  1982   AdapterHandlerEntry* lookup(int total_args_passed, BasicType* sig_bt) {
  1959     debug_only(_lookups++);
  1983     debug_only(_lookups++);
  1960     AdapterFingerPrint fp(total_args_passed, regs);
  1984     AdapterFingerPrint fp(total_args_passed, sig_bt);
  1961     unsigned int hash = fp.compute_hash();
  1985     unsigned int hash = fp.compute_hash();
  1962     int index = hash_to_index(hash);
  1986     int index = hash_to_index(hash);
  1963     for (AdapterHandlerEntry* e = bucket(index); e != NULL; e = e->next()) {
  1987     for (AdapterHandlerEntry* e = bucket(index); e != NULL; e = e->next()) {
  1964       debug_only(_buckets++);
  1988       debug_only(_buckets++);
  1965       if (e->hash() == hash) {
  1989       if (e->hash() == hash) {
  2127       if (ss.type() == T_LONG || ss.type() == T_DOUBLE)
  2151       if (ss.type() == T_LONG || ss.type() == T_DOUBLE)
  2128         sig_bt[i++] = T_VOID;   // Longs & doubles take 2 Java slots
  2152         sig_bt[i++] = T_VOID;   // Longs & doubles take 2 Java slots
  2129     }
  2153     }
  2130     assert(i == total_args_passed, "");
  2154     assert(i == total_args_passed, "");
  2131 
  2155 
       
  2156     // Lookup method signature's fingerprint
       
  2157     entry = _adapters->lookup(total_args_passed, sig_bt);
       
  2158 
       
  2159 #ifdef ASSERT
       
  2160     AdapterHandlerEntry* shared_entry = NULL;
       
  2161     if (VerifyAdapterSharing && entry != NULL) {
       
  2162       shared_entry = entry;
       
  2163       entry = NULL;
       
  2164     }
       
  2165 #endif
       
  2166 
       
  2167     if (entry != NULL) {
       
  2168       return entry;
       
  2169     }
       
  2170 
  2132     // Get a description of the compiled java calling convention and the largest used (VMReg) stack slot usage
  2171     // Get a description of the compiled java calling convention and the largest used (VMReg) stack slot usage
  2133     int comp_args_on_stack = SharedRuntime::java_calling_convention(sig_bt, regs, total_args_passed, false);
  2172     int comp_args_on_stack = SharedRuntime::java_calling_convention(sig_bt, regs, total_args_passed, false);
  2134 
  2173 
  2135     // Lookup method signature's fingerprint
       
  2136     entry = _adapters->lookup(total_args_passed, regs);
       
  2137     if (entry != NULL) {
       
  2138       return entry;
       
  2139     }
       
  2140 
       
  2141     // Make a C heap allocated version of the fingerprint to store in the adapter
  2174     // Make a C heap allocated version of the fingerprint to store in the adapter
  2142     fingerprint = new AdapterFingerPrint(total_args_passed, regs);
  2175     fingerprint = new AdapterFingerPrint(total_args_passed, sig_bt);
  2143 
  2176 
  2144     // Create I2C & C2I handlers
  2177     // Create I2C & C2I handlers
  2145 
  2178 
  2146     BufferBlob*  buf = buffer_blob(); // the temporary code buffer in CodeCache
  2179     BufferBlob*  buf = buffer_blob(); // the temporary code buffer in CodeCache
  2147     if (buf != NULL) {
  2180     if (buf != NULL) {
  2155                                                      total_args_passed,
  2188                                                      total_args_passed,
  2156                                                      comp_args_on_stack,
  2189                                                      comp_args_on_stack,
  2157                                                      sig_bt,
  2190                                                      sig_bt,
  2158                                                      regs,
  2191                                                      regs,
  2159                                                      fingerprint);
  2192                                                      fingerprint);
       
  2193 
       
  2194 #ifdef ASSERT
       
  2195       if (VerifyAdapterSharing) {
       
  2196         if (shared_entry != NULL) {
       
  2197           assert(shared_entry->compare_code(buf->instructions_begin(), buffer.code_size(), total_args_passed, sig_bt),
       
  2198                  "code must match");
       
  2199           // Release the one just created and return the original
       
  2200           _adapters->free_entry(entry);
       
  2201           return shared_entry;
       
  2202         } else  {
       
  2203           entry->save_code(buf->instructions_begin(), buffer.code_size(), total_args_passed, sig_bt);
       
  2204         }
       
  2205       }
       
  2206 #endif
  2160 
  2207 
  2161       B = BufferBlob::create(AdapterHandlerEntry::name, &buffer);
  2208       B = BufferBlob::create(AdapterHandlerEntry::name, &buffer);
  2162       NOT_PRODUCT(code_size = buffer.code_size());
  2209       NOT_PRODUCT(code_size = buffer.code_size());
  2163     }
  2210     }
  2164     if (B == NULL) {
  2211     if (B == NULL) {
  2209     ptrdiff_t delta = new_base - _i2c_entry;
  2256     ptrdiff_t delta = new_base - _i2c_entry;
  2210     _i2c_entry += delta;
  2257     _i2c_entry += delta;
  2211     _c2i_entry += delta;
  2258     _c2i_entry += delta;
  2212     _c2i_unverified_entry += delta;
  2259     _c2i_unverified_entry += delta;
  2213 }
  2260 }
       
  2261 
       
  2262 
       
  2263 void AdapterHandlerEntry::deallocate() {
       
  2264   delete _fingerprint;
       
  2265 #ifdef ASSERT
       
  2266   if (_saved_code) FREE_C_HEAP_ARRAY(unsigned char, _saved_code);
       
  2267   if (_saved_sig)  FREE_C_HEAP_ARRAY(Basictype, _saved_sig);
       
  2268 #endif
       
  2269 }
       
  2270 
       
  2271 
       
  2272 #ifdef ASSERT
       
  2273 // Capture the code before relocation so that it can be compared
       
  2274 // against other versions.  If the code is captured after relocation
       
  2275 // then relative instructions won't be equivalent.
       
  2276 void AdapterHandlerEntry::save_code(unsigned char* buffer, int length, int total_args_passed, BasicType* sig_bt) {
       
  2277   _saved_code = NEW_C_HEAP_ARRAY(unsigned char, length);
       
  2278   _code_length = length;
       
  2279   memcpy(_saved_code, buffer, length);
       
  2280   _total_args_passed = total_args_passed;
       
  2281   _saved_sig = NEW_C_HEAP_ARRAY(BasicType, _total_args_passed);
       
  2282   memcpy(_saved_sig, sig_bt, _total_args_passed * sizeof(BasicType));
       
  2283 }
       
  2284 
       
  2285 
       
  2286 bool AdapterHandlerEntry::compare_code(unsigned char* buffer, int length, int total_args_passed, BasicType* sig_bt) {
       
  2287   if (length != _code_length) {
       
  2288     return false;
       
  2289   }
       
  2290   for (int i = 0; i < length; i++) {
       
  2291     if (buffer[i] != _saved_code[i]) {
       
  2292       return false;
       
  2293     }
       
  2294   }
       
  2295   return true;
       
  2296 }
       
  2297 #endif
       
  2298 
  2214 
  2299 
  2215 // Create a native wrapper for this native method.  The wrapper converts the
  2300 // Create a native wrapper for this native method.  The wrapper converts the
  2216 // java compiled calling convention to the native convention, handlizes
  2301 // java compiled calling convention to the native convention, handlizes
  2217 // arguments, and transitions to native.  On return from the native we transition
  2302 // arguments, and transitions to native.  On return from the native we transition
  2218 // back to java blocking if a safepoint is in progress.
  2303 // back to java blocking if a safepoint is in progress.