1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/vmSymbols.hpp"
  27 #include "memory/oopFactory.hpp"
  28 #include "oops/oop.inline.hpp"
  29 #include "runtime/handles.inline.hpp"
  30 #include "utilities/xmlstream.hpp"
  31 
  32 
  33 Symbol* vmSymbols::_symbols[vmSymbols::SID_LIMIT];
  34 
  35 Symbol* vmSymbols::_type_signatures[T_VOID+1] = { NULL /*, NULL...*/ };
  36 
  37 inline int compare_symbol(const Symbol* a, const Symbol* b) {
  38   if (a == b)  return 0;
  39   // follow the natural address order:
  40   return (address)a > (address)b ? +1 : -1;
  41 }
  42 
  43 static vmSymbols::SID vm_symbol_index[vmSymbols::SID_LIMIT];
  44 extern "C" {
  45   static int compare_vmsymbol_sid(const void* void_a, const void* void_b) {
  46     const Symbol* a = vmSymbols::symbol_at(*((vmSymbols::SID*) void_a));
  47     const Symbol* b = vmSymbols::symbol_at(*((vmSymbols::SID*) void_b));
  48     return compare_symbol(a, b);
  49   }
  50 }
  51 
  52 #ifdef ASSERT
  53 #define VM_SYMBOL_ENUM_NAME_BODY(name, string) #name "\0"
  54 static const char* vm_symbol_enum_names =
  55   VM_SYMBOLS_DO(VM_SYMBOL_ENUM_NAME_BODY, VM_ALIAS_IGNORE)
  56   "\0";
  57 static const char* vm_symbol_enum_name(vmSymbols::SID sid) {
  58   const char* string = &vm_symbol_enum_names[0];
  59   int skip = (int)sid - (int)vmSymbols::FIRST_SID;
  60   for (; skip != 0; skip--) {
  61     size_t skiplen = strlen(string);
  62     if (skiplen == 0)  return "<unknown>";  // overflow
  63     string += skiplen+1;
  64   }
  65   return string;
  66 }
  67 #endif //ASSERT
  68 
  69 // Put all the VM symbol strings in one place.
  70 // Makes for a more compact libjvm.
  71 #define VM_SYMBOL_BODY(name, string) string "\0"
  72 static const char* vm_symbol_bodies = VM_SYMBOLS_DO(VM_SYMBOL_BODY, VM_ALIAS_IGNORE);
  73 
  74 void vmSymbols::initialize(TRAPS) {
  75   assert((int)SID_LIMIT <= (1<<log2_SID_LIMIT), "must fit in this bitfield");
  76   assert((int)SID_LIMIT*5 > (1<<log2_SID_LIMIT), "make the bitfield smaller, please");
  77   assert(vmIntrinsics::FLAG_LIMIT <= (1 << vmIntrinsics::log2_FLAG_LIMIT), "must fit in this bitfield");
  78 
  79   if (!UseSharedSpaces) {
  80     const char* string = &vm_symbol_bodies[0];
  81     for (int index = (int)FIRST_SID; index < (int)SID_LIMIT; index++) {
  82       Symbol* sym = SymbolTable::new_permanent_symbol(string, CHECK);
  83       _symbols[index] = sym;
  84       string += strlen(string); // skip string body
  85       string += 1;              // skip trailing null
  86     }
  87 
  88     _type_signatures[T_BYTE]    = byte_signature();
  89     _type_signatures[T_CHAR]    = char_signature();
  90     _type_signatures[T_DOUBLE]  = double_signature();
  91     _type_signatures[T_FLOAT]   = float_signature();
  92     _type_signatures[T_INT]     = int_signature();
  93     _type_signatures[T_LONG]    = long_signature();
  94     _type_signatures[T_SHORT]   = short_signature();
  95     _type_signatures[T_BOOLEAN] = bool_signature();
  96     _type_signatures[T_VOID]    = void_signature();
  97     // no single signatures for T_OBJECT or T_ARRAY
  98   }
  99 
 100 #ifdef ASSERT
 101   // Check for duplicates:
 102   for (int i1 = (int)FIRST_SID; i1 < (int)SID_LIMIT; i1++) {
 103     Symbol* sym = symbol_at((SID)i1);
 104     for (int i2 = (int)FIRST_SID; i2 < i1; i2++) {
 105       if (symbol_at((SID)i2) == sym) {
 106         tty->print("*** Duplicate VM symbol SIDs %s(%d) and %s(%d): \"",
 107                    vm_symbol_enum_name((SID)i2), i2,
 108                    vm_symbol_enum_name((SID)i1), i1);
 109         sym->print_symbol_on(tty);
 110         tty->print_cr("\"");
 111       }
 112     }
 113   }
 114 #endif //ASSERT
 115 
 116   // Create an index for find_id:
 117   {
 118     for (int index = (int)FIRST_SID; index < (int)SID_LIMIT; index++) {
 119       vm_symbol_index[index] = (SID)index;
 120     }
 121     int num_sids = SID_LIMIT-FIRST_SID;
 122     qsort(&vm_symbol_index[FIRST_SID], num_sids, sizeof(vm_symbol_index[0]),
 123           compare_vmsymbol_sid);
 124   }
 125 
 126 #ifdef ASSERT
 127   {
 128     // Spot-check correspondence between strings, symbols, and enums:
 129     assert(_symbols[NO_SID] == NULL, "must be");
 130     const char* str = "java/lang/Object";
 131     TempNewSymbol jlo = SymbolTable::new_permanent_symbol(str, CHECK);
 132     assert(strncmp(str, (char*)jlo->base(), jlo->utf8_length()) == 0, "");
 133     assert(jlo == java_lang_Object(), "");
 134     SID sid = VM_SYMBOL_ENUM_NAME(java_lang_Object);
 135     assert(find_sid(jlo) == sid, "");
 136     assert(symbol_at(sid) == jlo, "");
 137 
 138     // Make sure find_sid produces the right answer in each case.
 139     for (int index = (int)FIRST_SID; index < (int)SID_LIMIT; index++) {
 140       Symbol* sym = symbol_at((SID)index);
 141       sid = find_sid(sym);
 142       assert(sid == (SID)index, "symbol index works");
 143       // Note:  If there are duplicates, this assert will fail.
 144       // A "Duplicate VM symbol" message will have already been printed.
 145     }
 146 
 147     // The string "format" happens (at the moment) not to be a vmSymbol,
 148     // though it is a method name in java.lang.String.
 149     str = "format";
 150     TempNewSymbol fmt = SymbolTable::new_permanent_symbol(str, CHECK);
 151     sid = find_sid(fmt);
 152     assert(sid == NO_SID, "symbol index works (negative test)");
 153   }
 154 #endif
 155 }
 156 
 157 
 158 #ifndef PRODUCT
 159 const char* vmSymbols::name_for(vmSymbols::SID sid) {
 160   if (sid == NO_SID)
 161     return "NO_SID";
 162   const char* string = &vm_symbol_bodies[0];
 163   for (int index = (int)FIRST_SID; index < (int)SID_LIMIT; index++) {
 164     if (index == (int)sid)
 165       return string;
 166     string += strlen(string); // skip string body
 167     string += 1;              // skip trailing null
 168   }
 169   return "BAD_SID";
 170 }
 171 #endif
 172 
 173 
 174 
 175 void vmSymbols::symbols_do(SymbolClosure* f) {
 176   for (int index = (int)FIRST_SID; index < (int)SID_LIMIT; index++) {
 177     f->do_symbol(&_symbols[index]);
 178   }
 179   for (int i = 0; i < T_VOID+1; i++) {
 180     f->do_symbol(&_type_signatures[i]);
 181   }
 182 }
 183 
 184 void vmSymbols::serialize(SerializeClosure* soc) {
 185   soc->do_region((u_char*)&_symbols[FIRST_SID],
 186                  (SID_LIMIT - FIRST_SID) * sizeof(_symbols[0]));
 187   soc->do_region((u_char*)_type_signatures, sizeof(_type_signatures));
 188 }
 189 
 190 
 191 BasicType vmSymbols::signature_type(const Symbol* s) {
 192   assert(s != NULL, "checking");
 193   for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
 194     if (s == _type_signatures[i]) {
 195       return (BasicType)i;
 196     }
 197   }
 198   return T_OBJECT;
 199 }
 200 
 201 
 202 static int mid_hint = (int)vmSymbols::FIRST_SID+1;
 203 
 204 #ifndef PRODUCT
 205 static int find_sid_calls, find_sid_probes;
 206 // (Typical counts are calls=7000 and probes=17000.)
 207 #endif
 208 
 209 vmSymbols::SID vmSymbols::find_sid(const Symbol* symbol) {
 210   // Handle the majority of misses by a bounds check.
 211   // Then, use a binary search over the index.
 212   // Expected trip count is less than log2_SID_LIMIT, about eight.
 213   // This is slow but acceptable, given that calls are not
 214   // dynamically common.  (Method*::intrinsic_id has a cache.)
 215   NOT_PRODUCT(find_sid_calls++);
 216   int min = (int)FIRST_SID, max = (int)SID_LIMIT - 1;
 217   SID sid = NO_SID, sid1;
 218   int cmp1;
 219   sid1 = vm_symbol_index[min];
 220   cmp1 = compare_symbol(symbol, symbol_at(sid1));
 221   if (cmp1 <= 0) {              // before the first
 222     if (cmp1 == 0)  sid = sid1;
 223   } else {
 224     sid1 = vm_symbol_index[max];
 225     cmp1 = compare_symbol(symbol, symbol_at(sid1));
 226     if (cmp1 >= 0) {            // after the last
 227       if (cmp1 == 0)  sid = sid1;
 228     } else {
 229       // After checking the extremes, do a binary search.
 230       ++min; --max;             // endpoints are done
 231       int mid = mid_hint;       // start at previous success
 232       while (max >= min) {
 233         assert(mid >= min && mid <= max, "");
 234         NOT_PRODUCT(find_sid_probes++);
 235         sid1 = vm_symbol_index[mid];
 236         cmp1 = compare_symbol(symbol, symbol_at(sid1));
 237         if (cmp1 == 0) {
 238           mid_hint = mid;
 239           sid = sid1;
 240           break;
 241         }
 242         if (cmp1 < 0)
 243           max = mid - 1;        // symbol < symbol_at(sid)
 244         else
 245           min = mid + 1;
 246 
 247         // Pick a new probe point:
 248         mid = (max + min) / 2;
 249       }
 250     }
 251   }
 252 
 253 #ifdef ASSERT
 254   // Perform the exhaustive self-check the first 1000 calls,
 255   // and every 100 calls thereafter.
 256   static int find_sid_check_count = -2000;
 257   if ((uint)++find_sid_check_count > (uint)100) {
 258     if (find_sid_check_count > 0)  find_sid_check_count = 0;
 259 
 260     // Make sure this is the right answer, using linear search.
 261     // (We have already proven that there are no duplicates in the list.)
 262     SID sid2 = NO_SID;
 263     for (int index = (int)FIRST_SID; index < (int)SID_LIMIT; index++) {
 264       Symbol* sym2 = symbol_at((SID)index);
 265       if (sym2 == symbol) {
 266         sid2 = (SID)index;
 267         break;
 268       }
 269     }
 270     // Unless it's a duplicate, assert that the sids are the same.
 271     if (_symbols[sid] != _symbols[sid2]) {
 272       assert(sid == sid2, "binary same as linear search");
 273     }
 274   }
 275 #endif //ASSERT
 276 
 277   return sid;
 278 }
 279 
 280 vmSymbols::SID vmSymbols::find_sid(const char* symbol_name) {
 281   Symbol* symbol = SymbolTable::probe(symbol_name, (int) strlen(symbol_name));
 282   if (symbol == NULL)  return NO_SID;
 283   return find_sid(symbol);
 284 }
 285 
 286 static vmIntrinsics::ID wrapper_intrinsic(BasicType type, bool unboxing) {
 287 #define TYPE2(type, unboxing) ((int)(type)*2 + ((unboxing) ? 1 : 0))
 288   switch (TYPE2(type, unboxing)) {
 289 #define BASIC_TYPE_CASE(type, box, unbox) \
 290     case TYPE2(type, false):  return vmIntrinsics::box; \
 291     case TYPE2(type, true):   return vmIntrinsics::unbox
 292     BASIC_TYPE_CASE(T_BOOLEAN, _Boolean_valueOf,   _booleanValue);
 293     BASIC_TYPE_CASE(T_BYTE,    _Byte_valueOf,      _byteValue);
 294     BASIC_TYPE_CASE(T_CHAR,    _Character_valueOf, _charValue);
 295     BASIC_TYPE_CASE(T_SHORT,   _Short_valueOf,     _shortValue);
 296     BASIC_TYPE_CASE(T_INT,     _Integer_valueOf,   _intValue);
 297     BASIC_TYPE_CASE(T_LONG,    _Long_valueOf,      _longValue);
 298     BASIC_TYPE_CASE(T_FLOAT,   _Float_valueOf,     _floatValue);
 299     BASIC_TYPE_CASE(T_DOUBLE,  _Double_valueOf,    _doubleValue);
 300 #undef BASIC_TYPE_CASE
 301   }
 302 #undef TYPE2
 303   return vmIntrinsics::_none;
 304 }
 305 
 306 vmIntrinsics::ID vmIntrinsics::for_boxing(BasicType type) {
 307   return wrapper_intrinsic(type, false);
 308 }
 309 vmIntrinsics::ID vmIntrinsics::for_unboxing(BasicType type) {
 310   return wrapper_intrinsic(type, true);
 311 }
 312 
 313 vmIntrinsics::ID vmIntrinsics::for_raw_conversion(BasicType src, BasicType dest) {
 314 #define SRC_DEST(s,d) (((int)(s) << 4) + (int)(d))
 315   switch (SRC_DEST(src, dest)) {
 316   case SRC_DEST(T_INT, T_FLOAT):   return vmIntrinsics::_intBitsToFloat;
 317   case SRC_DEST(T_FLOAT, T_INT):   return vmIntrinsics::_floatToRawIntBits;
 318 
 319   case SRC_DEST(T_LONG, T_DOUBLE): return vmIntrinsics::_longBitsToDouble;
 320   case SRC_DEST(T_DOUBLE, T_LONG): return vmIntrinsics::_doubleToRawLongBits;
 321   }
 322 #undef SRC_DEST
 323 
 324   return vmIntrinsics::_none;
 325 }
 326 
 327 bool vmIntrinsics::preserves_state(vmIntrinsics::ID id) {
 328   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
 329   switch(id) {
 330 #ifdef TRACE_HAVE_INTRINSICS
 331   case vmIntrinsics::_counterTime:
 332 #endif
 333   case vmIntrinsics::_currentTimeMillis:
 334   case vmIntrinsics::_nanoTime:
 335   case vmIntrinsics::_floatToRawIntBits:
 336   case vmIntrinsics::_intBitsToFloat:
 337   case vmIntrinsics::_doubleToRawLongBits:
 338   case vmIntrinsics::_longBitsToDouble:
 339   case vmIntrinsics::_getClass:
 340   case vmIntrinsics::_isInstance:
 341   case vmIntrinsics::_currentThread:
 342   case vmIntrinsics::_dabs:
 343   case vmIntrinsics::_dsqrt:
 344   case vmIntrinsics::_dsin:
 345   case vmIntrinsics::_dcos:
 346   case vmIntrinsics::_dtan:
 347   case vmIntrinsics::_dlog:
 348   case vmIntrinsics::_dlog10:
 349   case vmIntrinsics::_dexp:
 350   case vmIntrinsics::_dpow:
 351   case vmIntrinsics::_checkIndex:
 352   case vmIntrinsics::_Reference_get:
 353   case vmIntrinsics::_updateCRC32:
 354   case vmIntrinsics::_updateBytesCRC32:
 355   case vmIntrinsics::_updateByteBufferCRC32:
 356     return true;
 357   default:
 358     return false;
 359   }
 360 }
 361 
 362 bool vmIntrinsics::can_trap(vmIntrinsics::ID id) {
 363   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
 364   switch(id) {
 365 #ifdef TRACE_HAVE_INTRINSICS
 366   case vmIntrinsics::_counterTime:
 367 #endif
 368   case vmIntrinsics::_currentTimeMillis:
 369   case vmIntrinsics::_nanoTime:
 370   case vmIntrinsics::_floatToRawIntBits:
 371   case vmIntrinsics::_intBitsToFloat:
 372   case vmIntrinsics::_doubleToRawLongBits:
 373   case vmIntrinsics::_longBitsToDouble:
 374   case vmIntrinsics::_currentThread:
 375   case vmIntrinsics::_dabs:
 376   case vmIntrinsics::_dsqrt:
 377   case vmIntrinsics::_dsin:
 378   case vmIntrinsics::_dcos:
 379   case vmIntrinsics::_dtan:
 380   case vmIntrinsics::_dlog:
 381   case vmIntrinsics::_dlog10:
 382   case vmIntrinsics::_dexp:
 383   case vmIntrinsics::_dpow:
 384   case vmIntrinsics::_updateCRC32:
 385   case vmIntrinsics::_updateBytesCRC32:
 386   case vmIntrinsics::_updateByteBufferCRC32:
 387     return false;
 388   default:
 389     return true;
 390   }
 391 }
 392 
 393 bool vmIntrinsics::does_virtual_dispatch(vmIntrinsics::ID id) {
 394   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
 395   switch(id) {
 396   case vmIntrinsics::_hashCode:
 397   case vmIntrinsics::_clone:
 398     return true;
 399     break;
 400   default:
 401     return false;
 402   }
 403 }
 404 
 405 int vmIntrinsics::predicates_needed(vmIntrinsics::ID id) {
 406   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
 407   switch (id) {
 408   case vmIntrinsics::_cipherBlockChaining_encryptAESCrypt:
 409   case vmIntrinsics::_cipherBlockChaining_decryptAESCrypt:
 410   case vmIntrinsics::_counterMode_AESCrypt:
 411     return 1;
 412   case vmIntrinsics::_digestBase_implCompressMB:
 413     return 3;
 414   default:
 415     return 0;
 416   }
 417 }
 418 
 419 bool vmIntrinsics::is_intrinsic_available(vmIntrinsics::ID id) {
 420   return !vmIntrinsics::is_intrinsic_disabled(id) &&
 421     !vmIntrinsics::is_disabled_by_flags(id);
 422 }
 423 
 424 bool vmIntrinsics::is_intrinsic_disabled(vmIntrinsics::ID id) {
 425   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
 426 
 427   ResourceMark rm;
 428   ccstr DisableIntrinsicString = DirectiveSet::canonicalize_disableintrinsic(DisableIntrinsic);
 429   // Create a copy of the string that contains the list of disabled
 430   // intrinsics. The copy is created because the string
 431   // will be modified by strtok(). Then, the list is tokenized with
 432   // ',' as a separator.
 433   size_t length = strlen(DisableIntrinsicString);
 434   char* local_list = NEW_RESOURCE_ARRAY(char, length + 1);
 435   strncpy(local_list, DisableIntrinsicString, length + 1);
 436 
 437   char* token = strtok(local_list, ",");
 438   while (token != NULL) {
 439     if (strcmp(token, vmIntrinsics::name_at(id)) == 0) {
 440       return true;
 441     }
 442     else {
 443       token = strtok(NULL, ",");
 444     }
 445   }
 446 
 447   return false;
 448 }
 449 
 450 
 451 bool vmIntrinsics::is_disabled_by_flags(const methodHandle& method) {
 452   vmIntrinsics::ID id = method->intrinsic_id();
 453   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
 454   return is_disabled_by_flags(id);
 455 }
 456 
 457 bool vmIntrinsics::is_disabled_by_flags(vmIntrinsics::ID id) {
 458   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
 459 
 460   // -XX:-InlineNatives disables nearly all intrinsics except the ones listed in
 461   // the following switch statement.
 462   if (!InlineNatives) {
 463     switch (id) {
 464     case vmIntrinsics::_indexOfL:
 465     case vmIntrinsics::_indexOfU:
 466     case vmIntrinsics::_indexOfUL:
 467     case vmIntrinsics::_indexOfIL:
 468     case vmIntrinsics::_indexOfIU:
 469     case vmIntrinsics::_indexOfIUL:
 470     case vmIntrinsics::_indexOfU_char:
 471     case vmIntrinsics::_compareToL:
 472     case vmIntrinsics::_compareToU:
 473     case vmIntrinsics::_compareToLU:
 474     case vmIntrinsics::_compareToUL:
 475     case vmIntrinsics::_equalsL:
 476     case vmIntrinsics::_equalsU:
 477     case vmIntrinsics::_equalsC:
 478     case vmIntrinsics::_getCharStringU:
 479     case vmIntrinsics::_putCharStringU:
 480     case vmIntrinsics::_compressStringC:
 481     case vmIntrinsics::_compressStringB:
 482     case vmIntrinsics::_inflateStringC:
 483     case vmIntrinsics::_inflateStringB:
 484     case vmIntrinsics::_getAndAddInt:
 485     case vmIntrinsics::_getAndAddLong:
 486     case vmIntrinsics::_getAndSetInt:
 487     case vmIntrinsics::_getAndSetLong:
 488     case vmIntrinsics::_getAndSetObject:
 489     case vmIntrinsics::_loadFence:
 490     case vmIntrinsics::_storeFence:
 491     case vmIntrinsics::_fullFence:
 492     case vmIntrinsics::_hasNegatives:
 493     case vmIntrinsics::_Reference_get:
 494       break;
 495     default:
 496       return true;
 497     }
 498   }
 499 
 500   switch (id) {
 501   case vmIntrinsics::_isInstance:
 502   case vmIntrinsics::_isAssignableFrom:
 503   case vmIntrinsics::_getModifiers:
 504   case vmIntrinsics::_isInterface:
 505   case vmIntrinsics::_isArray:
 506   case vmIntrinsics::_isPrimitive:
 507   case vmIntrinsics::_getSuperclass:
 508   case vmIntrinsics::_Class_cast:
 509   case vmIntrinsics::_getLength:
 510   case vmIntrinsics::_newArray:
 511   case vmIntrinsics::_getClass:
 512     if (!InlineClassNatives) return true;
 513     break;
 514   case vmIntrinsics::_currentThread:
 515   case vmIntrinsics::_isInterrupted:
 516     if (!InlineThreadNatives) return true;
 517     break;
 518   case vmIntrinsics::_floatToRawIntBits:
 519   case vmIntrinsics::_intBitsToFloat:
 520   case vmIntrinsics::_doubleToRawLongBits:
 521   case vmIntrinsics::_longBitsToDouble:
 522   case vmIntrinsics::_dabs:
 523   case vmIntrinsics::_dsqrt:
 524   case vmIntrinsics::_dsin:
 525   case vmIntrinsics::_dcos:
 526   case vmIntrinsics::_dtan:
 527   case vmIntrinsics::_dlog:
 528   case vmIntrinsics::_dexp:
 529   case vmIntrinsics::_dpow:
 530   case vmIntrinsics::_dlog10:
 531   case vmIntrinsics::_datan2:
 532   case vmIntrinsics::_min:
 533   case vmIntrinsics::_max:
 534   case vmIntrinsics::_floatToIntBits:
 535   case vmIntrinsics::_doubleToLongBits:
 536     if (!InlineMathNatives) return true;
 537     break;
 538   case vmIntrinsics::_arraycopy:
 539     if (!InlineArrayCopy) return true;
 540     break;
 541   case vmIntrinsics::_updateCRC32:
 542   case vmIntrinsics::_updateBytesCRC32:
 543   case vmIntrinsics::_updateByteBufferCRC32:
 544     if (!UseCRC32Intrinsics) return true;
 545     break;
 546   case vmIntrinsics::_getObject:
 547   case vmIntrinsics::_getBoolean:
 548   case vmIntrinsics::_getByte:
 549   case vmIntrinsics::_getShort:
 550   case vmIntrinsics::_getChar:
 551   case vmIntrinsics::_getInt:
 552   case vmIntrinsics::_getLong:
 553   case vmIntrinsics::_getFloat:
 554   case vmIntrinsics::_getDouble:
 555   case vmIntrinsics::_putObject:
 556   case vmIntrinsics::_putBoolean:
 557   case vmIntrinsics::_putByte:
 558   case vmIntrinsics::_putShort:
 559   case vmIntrinsics::_putChar:
 560   case vmIntrinsics::_putInt:
 561   case vmIntrinsics::_putLong:
 562   case vmIntrinsics::_putFloat:
 563   case vmIntrinsics::_putDouble:
 564   case vmIntrinsics::_getObjectVolatile:
 565   case vmIntrinsics::_getBooleanVolatile:
 566   case vmIntrinsics::_getByteVolatile:
 567   case vmIntrinsics::_getShortVolatile:
 568   case vmIntrinsics::_getCharVolatile:
 569   case vmIntrinsics::_getIntVolatile:
 570   case vmIntrinsics::_getLongVolatile:
 571   case vmIntrinsics::_getFloatVolatile:
 572   case vmIntrinsics::_getDoubleVolatile:
 573   case vmIntrinsics::_putObjectVolatile:
 574   case vmIntrinsics::_putBooleanVolatile:
 575   case vmIntrinsics::_putByteVolatile:
 576   case vmIntrinsics::_putShortVolatile:
 577   case vmIntrinsics::_putCharVolatile:
 578   case vmIntrinsics::_putIntVolatile:
 579   case vmIntrinsics::_putLongVolatile:
 580   case vmIntrinsics::_putFloatVolatile:
 581   case vmIntrinsics::_putDoubleVolatile:
 582   case vmIntrinsics::_getObjectAcquire:
 583   case vmIntrinsics::_getBooleanAcquire:
 584   case vmIntrinsics::_getByteAcquire:
 585   case vmIntrinsics::_getShortAcquire:
 586   case vmIntrinsics::_getCharAcquire:
 587   case vmIntrinsics::_getIntAcquire:
 588   case vmIntrinsics::_getLongAcquire:
 589   case vmIntrinsics::_getFloatAcquire:
 590   case vmIntrinsics::_getDoubleAcquire:
 591   case vmIntrinsics::_putObjectRelease:
 592   case vmIntrinsics::_putBooleanRelease:
 593   case vmIntrinsics::_putByteRelease:
 594   case vmIntrinsics::_putShortRelease:
 595   case vmIntrinsics::_putCharRelease:
 596   case vmIntrinsics::_putIntRelease:
 597   case vmIntrinsics::_putLongRelease:
 598   case vmIntrinsics::_putFloatRelease:
 599   case vmIntrinsics::_putDoubleRelease:
 600   case vmIntrinsics::_getObjectOpaque:
 601   case vmIntrinsics::_getBooleanOpaque:
 602   case vmIntrinsics::_getByteOpaque:
 603   case vmIntrinsics::_getShortOpaque:
 604   case vmIntrinsics::_getCharOpaque:
 605   case vmIntrinsics::_getIntOpaque:
 606   case vmIntrinsics::_getLongOpaque:
 607   case vmIntrinsics::_getFloatOpaque:
 608   case vmIntrinsics::_getDoubleOpaque:
 609   case vmIntrinsics::_putObjectOpaque:
 610   case vmIntrinsics::_putBooleanOpaque:
 611   case vmIntrinsics::_putByteOpaque:
 612   case vmIntrinsics::_putShortOpaque:
 613   case vmIntrinsics::_putCharOpaque:
 614   case vmIntrinsics::_putIntOpaque:
 615   case vmIntrinsics::_putLongOpaque:
 616   case vmIntrinsics::_putFloatOpaque:
 617   case vmIntrinsics::_putDoubleOpaque:
 618   case vmIntrinsics::_getByte_raw:
 619   case vmIntrinsics::_getShort_raw:
 620   case vmIntrinsics::_getChar_raw:
 621   case vmIntrinsics::_getInt_raw:
 622   case vmIntrinsics::_getLong_raw:
 623   case vmIntrinsics::_getFloat_raw:
 624   case vmIntrinsics::_getDouble_raw:
 625   case vmIntrinsics::_putByte_raw:
 626   case vmIntrinsics::_putShort_raw:
 627   case vmIntrinsics::_putChar_raw:
 628   case vmIntrinsics::_putInt_raw:
 629   case vmIntrinsics::_putLong_raw:
 630   case vmIntrinsics::_putFloat_raw:
 631   case vmIntrinsics::_putDouble_raw:
 632   case vmIntrinsics::_getAndAddInt:
 633   case vmIntrinsics::_getAndAddLong:
 634   case vmIntrinsics::_getAndSetInt:
 635   case vmIntrinsics::_getAndSetLong:
 636   case vmIntrinsics::_getAndSetObject:
 637   case vmIntrinsics::_loadFence:
 638   case vmIntrinsics::_storeFence:
 639   case vmIntrinsics::_fullFence:
 640   case vmIntrinsics::_compareAndSwapLong:
 641   case vmIntrinsics::_weakCompareAndSwapLong:
 642   case vmIntrinsics::_weakCompareAndSwapLongAcquire:
 643   case vmIntrinsics::_weakCompareAndSwapLongRelease:
 644   case vmIntrinsics::_compareAndSwapInt:
 645   case vmIntrinsics::_weakCompareAndSwapInt:
 646   case vmIntrinsics::_weakCompareAndSwapIntAcquire:
 647   case vmIntrinsics::_weakCompareAndSwapIntRelease:
 648   case vmIntrinsics::_compareAndSwapObject:
 649   case vmIntrinsics::_weakCompareAndSwapObject:
 650   case vmIntrinsics::_weakCompareAndSwapObjectAcquire:
 651   case vmIntrinsics::_weakCompareAndSwapObjectRelease:
 652   case vmIntrinsics::_compareAndExchangeIntVolatile:
 653   case vmIntrinsics::_compareAndExchangeIntAcquire:
 654   case vmIntrinsics::_compareAndExchangeIntRelease:
 655   case vmIntrinsics::_compareAndExchangeLongVolatile:
 656   case vmIntrinsics::_compareAndExchangeLongAcquire:
 657   case vmIntrinsics::_compareAndExchangeLongRelease:
 658   case vmIntrinsics::_compareAndExchangeObjectVolatile:
 659   case vmIntrinsics::_compareAndExchangeObjectAcquire:
 660   case vmIntrinsics::_compareAndExchangeObjectRelease:
 661     if (!InlineUnsafeOps) return true;
 662     break;
 663   case vmIntrinsics::_getShortUnaligned:
 664   case vmIntrinsics::_getCharUnaligned:
 665   case vmIntrinsics::_getIntUnaligned:
 666   case vmIntrinsics::_getLongUnaligned:
 667   case vmIntrinsics::_putShortUnaligned:
 668   case vmIntrinsics::_putCharUnaligned:
 669   case vmIntrinsics::_putIntUnaligned:
 670   case vmIntrinsics::_putLongUnaligned:
 671   case vmIntrinsics::_allocateInstance:
 672   case vmIntrinsics::_getAddress_raw:
 673   case vmIntrinsics::_putAddress_raw:
 674     if (!InlineUnsafeOps || !UseUnalignedAccesses) return true;
 675     break;
 676   case vmIntrinsics::_hashCode:
 677     if (!InlineObjectHash) return true;
 678     break;
 679   case vmIntrinsics::_aescrypt_encryptBlock:
 680   case vmIntrinsics::_aescrypt_decryptBlock:
 681     if (!UseAESIntrinsics) return true;
 682     break;
 683   case vmIntrinsics::_cipherBlockChaining_encryptAESCrypt:
 684   case vmIntrinsics::_cipherBlockChaining_decryptAESCrypt:
 685     if (!UseAESIntrinsics) return true;
 686     break;
 687   case vmIntrinsics::_counterMode_AESCrypt:
 688     if (!UseAESCTRIntrinsics) return true;
 689     break;
 690   case vmIntrinsics::_sha_implCompress:
 691     if (!UseSHA1Intrinsics) return true;
 692     break;
 693   case vmIntrinsics::_sha2_implCompress:
 694     if (!UseSHA256Intrinsics) return true;
 695     break;
 696   case vmIntrinsics::_sha5_implCompress:
 697     if (!UseSHA512Intrinsics) return true;
 698     break;
 699   case vmIntrinsics::_digestBase_implCompressMB:
 700     if (!(UseSHA1Intrinsics || UseSHA256Intrinsics || UseSHA512Intrinsics)) return true;
 701     break;
 702   case vmIntrinsics::_ghash_processBlocks:
 703     if (!UseGHASHIntrinsics) return true;
 704     break;
 705   case vmIntrinsics::_updateBytesCRC32C:
 706   case vmIntrinsics::_updateDirectByteBufferCRC32C:
 707     if (!UseCRC32CIntrinsics) return true;
 708     break;
 709   case vmIntrinsics::_updateBytesAdler32:
 710   case vmIntrinsics::_updateByteBufferAdler32:
 711     if (!UseAdler32Intrinsics) return true;
 712     break;
 713   case vmIntrinsics::_copyMemory:
 714     if (!InlineArrayCopy || !InlineUnsafeOps) return true;
 715     break;
 716 #ifdef COMPILER1
 717   case vmIntrinsics::_checkIndex:
 718     if (!InlineNIOCheckIndex) return true;
 719     break;
 720 #endif // COMPILER1
 721 #ifdef COMPILER2
 722   case vmIntrinsics::_clone:
 723   case vmIntrinsics::_copyOf:
 724   case vmIntrinsics::_copyOfRange:
 725     // These intrinsics use both the objectcopy and the arraycopy
 726     // intrinsic mechanism.
 727     if (!InlineObjectCopy || !InlineArrayCopy) return true;
 728     break;
 729   case vmIntrinsics::_compareToL:
 730   case vmIntrinsics::_compareToU:
 731   case vmIntrinsics::_compareToLU:
 732   case vmIntrinsics::_compareToUL:
 733     if (!SpecialStringCompareTo) return true;
 734     break;
 735   case vmIntrinsics::_indexOfL:
 736   case vmIntrinsics::_indexOfU:
 737   case vmIntrinsics::_indexOfUL:
 738   case vmIntrinsics::_indexOfIL:
 739   case vmIntrinsics::_indexOfIU:
 740   case vmIntrinsics::_indexOfIUL:
 741   case vmIntrinsics::_indexOfU_char:
 742     if (!SpecialStringIndexOf) return true;
 743     break;
 744   case vmIntrinsics::_equalsL:
 745   case vmIntrinsics::_equalsU:
 746     if (!SpecialStringEquals) return true;
 747     break;
 748   case vmIntrinsics::_equalsB:
 749   case vmIntrinsics::_equalsC:
 750     if (!SpecialArraysEquals) return true;
 751     break;
 752   case vmIntrinsics::_encodeISOArray:
 753   case vmIntrinsics::_encodeByteISOArray:
 754     if (!SpecialEncodeISOArray) return true;
 755     break;
 756   case vmIntrinsics::_getCallerClass:
 757     if (!InlineReflectionGetCallerClass) return true;
 758     break;
 759   case vmIntrinsics::_multiplyToLen:
 760     if (!UseMultiplyToLenIntrinsic) return true;
 761     break;
 762   case vmIntrinsics::_squareToLen:
 763     if (!UseSquareToLenIntrinsic) return true;
 764     break;
 765   case vmIntrinsics::_mulAdd:
 766     if (!UseMulAddIntrinsic) return true;
 767     break;
 768   case vmIntrinsics::_montgomeryMultiply:
 769     if (!UseMontgomeryMultiplyIntrinsic) return true;
 770     break;
 771   case vmIntrinsics::_montgomerySquare:
 772     if (!UseMontgomerySquareIntrinsic) return true;
 773     break;
 774   case vmIntrinsics::_vectorizedMismatch:
 775     if (!UseVectorizedMismatchIntrinsic) return true;
 776     break;
 777   case vmIntrinsics::_addExactI:
 778   case vmIntrinsics::_addExactL:
 779   case vmIntrinsics::_decrementExactI:
 780   case vmIntrinsics::_decrementExactL:
 781   case vmIntrinsics::_incrementExactI:
 782   case vmIntrinsics::_incrementExactL:
 783   case vmIntrinsics::_multiplyExactI:
 784   case vmIntrinsics::_multiplyExactL:
 785   case vmIntrinsics::_negateExactI:
 786   case vmIntrinsics::_negateExactL:
 787   case vmIntrinsics::_subtractExactI:
 788   case vmIntrinsics::_subtractExactL:
 789     if (!UseMathExactIntrinsics || !InlineMathNatives) return true;
 790     break;
 791 #endif // COMPILER2
 792   default:
 793     return false;
 794   }
 795 
 796   return false;
 797 }
 798 
 799 #define VM_INTRINSIC_INITIALIZE(id, klass, name, sig, flags) #id "\0"
 800 static const char* vm_intrinsic_name_bodies =
 801   VM_INTRINSICS_DO(VM_INTRINSIC_INITIALIZE,
 802                    VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE);
 803 
 804 static const char* vm_intrinsic_name_table[vmIntrinsics::ID_LIMIT];
 805 
 806 const char* vmIntrinsics::name_at(vmIntrinsics::ID id) {
 807   const char** nt = &vm_intrinsic_name_table[0];
 808   if (nt[_none] == NULL) {
 809     char* string = (char*) &vm_intrinsic_name_bodies[0];
 810     for (int index = FIRST_ID; index < ID_LIMIT; index++) {
 811       nt[index] = string;
 812       string += strlen(string); // skip string body
 813       string += 1;              // skip trailing null
 814     }
 815     assert(!strcmp(nt[_hashCode], "_hashCode"), "lined up");
 816     nt[_none] = "_none";
 817   }
 818   if ((uint)id < (uint)ID_LIMIT)
 819     return vm_intrinsic_name_table[(uint)id];
 820   else
 821     return "(unknown intrinsic)";
 822 }
 823 
 824 // These are flag-matching functions:
 825 inline bool match_F_R(jshort flags) {
 826   const int req = 0;
 827   const int neg = JVM_ACC_STATIC | JVM_ACC_SYNCHRONIZED;
 828   return (flags & (req | neg)) == req;
 829 }
 830 inline bool match_F_Y(jshort flags) {
 831   const int req = JVM_ACC_SYNCHRONIZED;
 832   const int neg = JVM_ACC_STATIC;
 833   return (flags & (req | neg)) == req;
 834 }
 835 inline bool match_F_RN(jshort flags) {
 836   const int req = JVM_ACC_NATIVE;
 837   const int neg = JVM_ACC_STATIC | JVM_ACC_SYNCHRONIZED;
 838   return (flags & (req | neg)) == req;
 839 }
 840 inline bool match_F_S(jshort flags) {
 841   const int req = JVM_ACC_STATIC;
 842   const int neg = JVM_ACC_SYNCHRONIZED;
 843   return (flags & (req | neg)) == req;
 844 }
 845 inline bool match_F_SN(jshort flags) {
 846   const int req = JVM_ACC_STATIC | JVM_ACC_NATIVE;
 847   const int neg = JVM_ACC_SYNCHRONIZED;
 848   return (flags & (req | neg)) == req;
 849 }
 850 inline bool match_F_RNY(jshort flags) {
 851   const int req = JVM_ACC_NATIVE | JVM_ACC_SYNCHRONIZED;
 852   const int neg = JVM_ACC_STATIC;
 853   return (flags & (req | neg)) == req;
 854 }
 855 
 856 // These are for forming case labels:
 857 #define ID3(x, y, z) (( jlong)(z) +                                  \
 858                       ((jlong)(y) <<    vmSymbols::log2_SID_LIMIT) + \
 859                       ((jlong)(x) << (2*vmSymbols::log2_SID_LIMIT))  )
 860 #define SID_ENUM(n) vmSymbols::VM_SYMBOL_ENUM_NAME(n)
 861 
 862 vmIntrinsics::ID vmIntrinsics::find_id_impl(vmSymbols::SID holder,
 863                                             vmSymbols::SID name,
 864                                             vmSymbols::SID sig,
 865                                             jshort flags) {
 866   assert((int)vmSymbols::SID_LIMIT <= (1<<vmSymbols::log2_SID_LIMIT), "must fit");
 867 
 868   // Let the C compiler build the decision tree.
 869 
 870 #define VM_INTRINSIC_CASE(id, klass, name, sig, fcode) \
 871   case ID3(SID_ENUM(klass), SID_ENUM(name), SID_ENUM(sig)): \
 872     if (!match_##fcode(flags))  break; \
 873     return id;
 874 
 875   switch (ID3(holder, name, sig)) {
 876     VM_INTRINSICS_DO(VM_INTRINSIC_CASE,
 877                      VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE);
 878   }
 879   return vmIntrinsics::_none;
 880 
 881 #undef VM_INTRINSIC_CASE
 882 }
 883 
 884 
 885 const char* vmIntrinsics::short_name_as_C_string(vmIntrinsics::ID id, char* buf, int buflen) {
 886   const char* str = name_at(id);
 887 #ifndef PRODUCT
 888   const char* kname = vmSymbols::name_for(class_for(id));
 889   const char* mname = vmSymbols::name_for(name_for(id));
 890   const char* sname = vmSymbols::name_for(signature_for(id));
 891   const char* fname = "";
 892   switch (flags_for(id)) {
 893   case F_Y:  fname = "synchronized ";  break;
 894   case F_RN: fname = "native ";        break;
 895   case F_SN: fname = "native static "; break;
 896   case F_S:  fname = "static ";        break;
 897   case F_RNY:fname = "native synchronized "; break;
 898   }
 899   const char* kptr = strrchr(kname, '/');
 900   if (kptr != NULL)  kname = kptr + 1;
 901   int len = jio_snprintf(buf, buflen, "%s: %s%s.%s%s",
 902                          str, fname, kname, mname, sname);
 903   if (len < buflen)
 904     str = buf;
 905 #endif //PRODUCT
 906   return str;
 907 }
 908 
 909 
 910 // These are to get information about intrinsics.
 911 
 912 #define ID4(x, y, z, f) ((ID3(x, y, z) << vmIntrinsics::log2_FLAG_LIMIT) | (jlong) (f))
 913 
 914 static const jlong intrinsic_info_array[vmIntrinsics::ID_LIMIT+1] = {
 915 #define VM_INTRINSIC_INFO(ignore_id, klass, name, sig, fcode) \
 916   ID4(SID_ENUM(klass), SID_ENUM(name), SID_ENUM(sig), vmIntrinsics::fcode),
 917 
 918   0, VM_INTRINSICS_DO(VM_INTRINSIC_INFO,
 919                      VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE)
 920     0
 921 #undef VM_INTRINSIC_INFO
 922 };
 923 
 924 inline jlong intrinsic_info(vmIntrinsics::ID id) {
 925   return intrinsic_info_array[vmIntrinsics::ID_from((int)id)];
 926 }
 927 
 928 vmSymbols::SID vmIntrinsics::class_for(vmIntrinsics::ID id) {
 929   jlong info = intrinsic_info(id);
 930   int shift = 2*vmSymbols::log2_SID_LIMIT + log2_FLAG_LIMIT, mask = right_n_bits(vmSymbols::log2_SID_LIMIT);
 931   assert(((ID4(1021,1022,1023,15) >> shift) & mask) == 1021, "");
 932   return vmSymbols::SID( (info >> shift) & mask );
 933 }
 934 
 935 vmSymbols::SID vmIntrinsics::name_for(vmIntrinsics::ID id) {
 936   jlong info = intrinsic_info(id);
 937   int shift = vmSymbols::log2_SID_LIMIT + log2_FLAG_LIMIT, mask = right_n_bits(vmSymbols::log2_SID_LIMIT);
 938   assert(((ID4(1021,1022,1023,15) >> shift) & mask) == 1022, "");
 939   return vmSymbols::SID( (info >> shift) & mask );
 940 }
 941 
 942 vmSymbols::SID vmIntrinsics::signature_for(vmIntrinsics::ID id) {
 943   jlong info = intrinsic_info(id);
 944   int shift = log2_FLAG_LIMIT, mask = right_n_bits(vmSymbols::log2_SID_LIMIT);
 945   assert(((ID4(1021,1022,1023,15) >> shift) & mask) == 1023, "");
 946   return vmSymbols::SID( (info >> shift) & mask );
 947 }
 948 
 949 vmIntrinsics::Flags vmIntrinsics::flags_for(vmIntrinsics::ID id) {
 950   jlong info = intrinsic_info(id);
 951   int shift = 0, mask = right_n_bits(log2_FLAG_LIMIT);
 952   assert(((ID4(1021,1022,1023,15) >> shift) & mask) == 15, "");
 953   return Flags( (info >> shift) & mask );
 954 }
 955 
 956 
 957 #ifndef PRODUCT
 958 // verify_method performs an extra check on a matched intrinsic method
 959 
 960 static bool match_method(Method* m, Symbol* n, Symbol* s) {
 961   return (m->name() == n &&
 962           m->signature() == s);
 963 }
 964 
 965 static vmIntrinsics::ID match_method_with_klass(Method* m, Symbol* mk) {
 966 #define VM_INTRINSIC_MATCH(id, klassname, namepart, sigpart, flags) \
 967   { Symbol* k = vmSymbols::klassname(); \
 968     if (mk == k) { \
 969       Symbol* n = vmSymbols::namepart(); \
 970       Symbol* s = vmSymbols::sigpart(); \
 971       if (match_method(m, n, s)) \
 972         return vmIntrinsics::id; \
 973     } }
 974   VM_INTRINSICS_DO(VM_INTRINSIC_MATCH,
 975                    VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE);
 976   return vmIntrinsics::_none;
 977 #undef VM_INTRINSIC_MATCH
 978 }
 979 
 980 void vmIntrinsics::verify_method(ID actual_id, Method* m) {
 981   Symbol* mk = m->method_holder()->name();
 982   ID declared_id = match_method_with_klass(m, mk);
 983 
 984   if (declared_id == actual_id)  return; // success
 985 
 986   if (declared_id == _none && actual_id != _none && mk == vmSymbols::java_lang_StrictMath()) {
 987     // Here are a few special cases in StrictMath not declared in vmSymbols.hpp.
 988     switch (actual_id) {
 989     case _min:
 990     case _max:
 991     case _dsqrt:
 992       declared_id = match_method_with_klass(m, vmSymbols::java_lang_Math());
 993       if (declared_id == actual_id)  return; // acceptable alias
 994       break;
 995     }
 996   }
 997 
 998   const char* declared_name = name_at(declared_id);
 999   const char* actual_name   = name_at(actual_id);
1000   methodHandle mh = m;
1001   m = NULL;
1002   ttyLocker ttyl;
1003   if (xtty != NULL) {
1004     xtty->begin_elem("intrinsic_misdeclared actual='%s' declared='%s'",
1005                      actual_name, declared_name);
1006     xtty->method(mh);
1007     xtty->end_elem("%s", "");
1008   }
1009   if (PrintMiscellaneous && (WizardMode || Verbose)) {
1010     tty->print_cr("*** misidentified method; %s(%d) should be %s(%d):",
1011                   declared_name, declared_id, actual_name, actual_id);
1012     mh()->print_short_name(tty);
1013     tty->cr();
1014   }
1015 }
1016 #endif //PRODUCT