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