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 #ifndef SHARE_VM_UTILITIES_GLOBALDEFINITIONS_HPP
  26 #define SHARE_VM_UTILITIES_GLOBALDEFINITIONS_HPP
  27 
  28 #include "utilities/compilerWarnings.hpp"
  29 #include "utilities/debug.hpp"
  30 #include "utilities/macros.hpp"
  31 
  32 #include COMPILER_HEADER(utilities/globalDefinitions)
  33 
  34 // Defaults for macros that might be defined per compiler.
  35 #ifndef NOINLINE
  36 #define NOINLINE
  37 #endif
  38 #ifndef ALWAYSINLINE
  39 #define ALWAYSINLINE inline
  40 #endif
  41 
  42 // This file holds all globally used constants & types, class (forward)
  43 // declarations and a few frequently used utility functions.
  44 
  45 //----------------------------------------------------------------------------------------------------
  46 // Printf-style formatters for fixed- and variable-width types as pointers and
  47 // integers.  These are derived from the definitions in inttypes.h.  If the platform
  48 // doesn't provide appropriate definitions, they should be provided in
  49 // the compiler-specific definitions file (e.g., globalDefinitions_gcc.hpp)
  50 
  51 #define BOOL_TO_STR(_b_) ((_b_) ? "true" : "false")
  52 
  53 // Format 32-bit quantities.
  54 #define INT32_FORMAT           "%" PRId32
  55 #define UINT32_FORMAT          "%" PRIu32
  56 #define INT32_FORMAT_W(width)  "%" #width PRId32
  57 #define UINT32_FORMAT_W(width) "%" #width PRIu32
  58 
  59 #define PTR32_FORMAT           "0x%08" PRIx32
  60 #define PTR32_FORMAT_W(width)  "0x%" #width PRIx32
  61 
  62 // Format 64-bit quantities.
  63 #define INT64_FORMAT           "%" PRId64
  64 #define UINT64_FORMAT          "%" PRIu64
  65 #define UINT64_FORMAT_X        "%" PRIx64
  66 #define INT64_FORMAT_W(width)  "%" #width PRId64
  67 #define UINT64_FORMAT_W(width) "%" #width PRIu64
  68 
  69 #define PTR64_FORMAT           "0x%016" PRIx64
  70 
  71 // Format jlong, if necessary
  72 #ifndef JLONG_FORMAT
  73 #define JLONG_FORMAT           INT64_FORMAT
  74 #endif
  75 #ifndef JULONG_FORMAT
  76 #define JULONG_FORMAT          UINT64_FORMAT
  77 #endif
  78 #ifndef JULONG_FORMAT_X
  79 #define JULONG_FORMAT_X        UINT64_FORMAT_X
  80 #endif
  81 
  82 // Format pointers which change size between 32- and 64-bit.
  83 #ifdef  _LP64
  84 #define INTPTR_FORMAT "0x%016" PRIxPTR
  85 #define PTR_FORMAT    "0x%016" PRIxPTR
  86 #else   // !_LP64
  87 #define INTPTR_FORMAT "0x%08"  PRIxPTR
  88 #define PTR_FORMAT    "0x%08"  PRIxPTR
  89 #endif  // _LP64
  90 
  91 #define INTPTR_FORMAT_W(width)   "%" #width PRIxPTR
  92 
  93 #define SSIZE_FORMAT             "%"   PRIdPTR
  94 #define SIZE_FORMAT              "%"   PRIuPTR
  95 #define SIZE_FORMAT_HEX          "0x%" PRIxPTR
  96 #define SSIZE_FORMAT_W(width)    "%"   #width PRIdPTR
  97 #define SIZE_FORMAT_W(width)     "%"   #width PRIuPTR
  98 #define SIZE_FORMAT_HEX_W(width) "0x%" #width PRIxPTR
  99 
 100 #define INTX_FORMAT           "%" PRIdPTR
 101 #define UINTX_FORMAT          "%" PRIuPTR
 102 #define INTX_FORMAT_W(width)  "%" #width PRIdPTR
 103 #define UINTX_FORMAT_W(width) "%" #width PRIuPTR
 104 
 105 //----------------------------------------------------------------------------------------------------
 106 // Constants
 107 
 108 const int LogBytesPerShort   = 1;
 109 const int LogBytesPerInt     = 2;
 110 #ifdef _LP64
 111 const int LogBytesPerWord    = 3;
 112 #else
 113 const int LogBytesPerWord    = 2;
 114 #endif
 115 const int LogBytesPerLong    = 3;
 116 
 117 const int BytesPerShort      = 1 << LogBytesPerShort;
 118 const int BytesPerInt        = 1 << LogBytesPerInt;
 119 const int BytesPerWord       = 1 << LogBytesPerWord;
 120 const int BytesPerLong       = 1 << LogBytesPerLong;
 121 
 122 const int LogBitsPerByte     = 3;
 123 const int LogBitsPerShort    = LogBitsPerByte + LogBytesPerShort;
 124 const int LogBitsPerInt      = LogBitsPerByte + LogBytesPerInt;
 125 const int LogBitsPerWord     = LogBitsPerByte + LogBytesPerWord;
 126 const int LogBitsPerLong     = LogBitsPerByte + LogBytesPerLong;
 127 
 128 const int BitsPerByte        = 1 << LogBitsPerByte;
 129 const int BitsPerShort       = 1 << LogBitsPerShort;
 130 const int BitsPerInt         = 1 << LogBitsPerInt;
 131 const int BitsPerWord        = 1 << LogBitsPerWord;
 132 const int BitsPerLong        = 1 << LogBitsPerLong;
 133 
 134 const int WordAlignmentMask  = (1 << LogBytesPerWord) - 1;
 135 const int LongAlignmentMask  = (1 << LogBytesPerLong) - 1;
 136 
 137 const int WordsPerLong       = 2;       // Number of stack entries for longs
 138 
 139 const int oopSize            = sizeof(char*); // Full-width oop
 140 extern int heapOopSize;                       // Oop within a java object
 141 const int wordSize           = sizeof(char*);
 142 const int longSize           = sizeof(jlong);
 143 const int jintSize           = sizeof(jint);
 144 const int size_tSize         = sizeof(size_t);
 145 
 146 const int BytesPerOop        = BytesPerWord;  // Full-width oop
 147 
 148 extern int LogBytesPerHeapOop;                // Oop within a java object
 149 extern int LogBitsPerHeapOop;
 150 extern int BytesPerHeapOop;
 151 extern int BitsPerHeapOop;
 152 
 153 const int BitsPerJavaInteger = 32;
 154 const int BitsPerJavaLong    = 64;
 155 const int BitsPerSize_t      = size_tSize * BitsPerByte;
 156 
 157 // Size of a char[] needed to represent a jint as a string in decimal.
 158 const int jintAsStringSize = 12;
 159 
 160 // In fact this should be
 161 // log2_intptr(sizeof(class JavaThread)) - log2_intptr(64);
 162 // see os::set_memory_serialize_page()
 163 #ifdef _LP64
 164 const int SerializePageShiftCount = 4;
 165 #else
 166 const int SerializePageShiftCount = 3;
 167 #endif
 168 
 169 // An opaque struct of heap-word width, so that HeapWord* can be a generic
 170 // pointer into the heap.  We require that object sizes be measured in
 171 // units of heap words, so that that
 172 //   HeapWord* hw;
 173 //   hw += oop(hw)->foo();
 174 // works, where foo is a method (like size or scavenge) that returns the
 175 // object size.
 176 class HeapWord {
 177   friend class VMStructs;
 178  private:
 179   char* i;
 180 #ifndef PRODUCT
 181  public:
 182   char* value() { return i; }
 183 #endif
 184 };
 185 
 186 // Analogous opaque struct for metadata allocated from
 187 // metaspaces.
 188 class MetaWord {
 189  private:
 190   char* i;
 191 };
 192 
 193 // HeapWordSize must be 2^LogHeapWordSize.
 194 const int HeapWordSize        = sizeof(HeapWord);
 195 #ifdef _LP64
 196 const int LogHeapWordSize     = 3;
 197 #else
 198 const int LogHeapWordSize     = 2;
 199 #endif
 200 const int HeapWordsPerLong    = BytesPerLong / HeapWordSize;
 201 const int LogHeapWordsPerLong = LogBytesPerLong - LogHeapWordSize;
 202 
 203 // The larger HeapWordSize for 64bit requires larger heaps
 204 // for the same application running in 64bit.  See bug 4967770.
 205 // The minimum alignment to a heap word size is done.  Other
 206 // parts of the memory system may require additional alignment
 207 // and are responsible for those alignments.
 208 #ifdef _LP64
 209 #define ScaleForWordSize(x) align_size_down_((x) * 13 / 10, HeapWordSize)
 210 #else
 211 #define ScaleForWordSize(x) (x)
 212 #endif
 213 
 214 // The minimum number of native machine words necessary to contain "byte_size"
 215 // bytes.
 216 inline size_t heap_word_size(size_t byte_size) {
 217   return (byte_size + (HeapWordSize-1)) >> LogHeapWordSize;
 218 }
 219 
 220 //-------------------------------------------
 221 // Constant for jlong (standardized by C++11)
 222 
 223 // Build a 64bit integer constant
 224 #define CONST64(x)  (x ## LL)
 225 #define UCONST64(x) (x ## ULL)
 226 
 227 const jlong min_jlong = CONST64(0x8000000000000000);
 228 const jlong max_jlong = CONST64(0x7fffffffffffffff);
 229 
 230 const size_t K                  = 1024;
 231 const size_t M                  = K*K;
 232 const size_t G                  = M*K;
 233 const size_t HWperKB            = K / sizeof(HeapWord);
 234 
 235 // Constants for converting from a base unit to milli-base units.  For
 236 // example from seconds to milliseconds and microseconds
 237 
 238 const int MILLIUNITS    = 1000;         // milli units per base unit
 239 const int MICROUNITS    = 1000000;      // micro units per base unit
 240 const int NANOUNITS     = 1000000000;   // nano units per base unit
 241 
 242 const jlong NANOSECS_PER_SEC      = CONST64(1000000000);
 243 const jint  NANOSECS_PER_MILLISEC = 1000000;
 244 
 245 inline const char* proper_unit_for_byte_size(size_t s) {
 246 #ifdef _LP64
 247   if (s >= 10*G) {
 248     return "G";
 249   }
 250 #endif
 251   if (s >= 10*M) {
 252     return "M";
 253   } else if (s >= 10*K) {
 254     return "K";
 255   } else {
 256     return "B";
 257   }
 258 }
 259 
 260 template <class T>
 261 inline T byte_size_in_proper_unit(T s) {
 262 #ifdef _LP64
 263   if (s >= 10*G) {
 264     return (T)(s/G);
 265   }
 266 #endif
 267   if (s >= 10*M) {
 268     return (T)(s/M);
 269   } else if (s >= 10*K) {
 270     return (T)(s/K);
 271   } else {
 272     return s;
 273   }
 274 }
 275 
 276 inline const char* exact_unit_for_byte_size(size_t s) {
 277 #ifdef _LP64
 278   if (s >= G && (s % G) == 0) {
 279     return "G";
 280   }
 281 #endif
 282   if (s >= M && (s % M) == 0) {
 283     return "M";
 284   }
 285   if (s >= K && (s % K) == 0) {
 286     return "K";
 287   }
 288   return "B";
 289 }
 290 
 291 inline size_t byte_size_in_exact_unit(size_t s) {
 292 #ifdef _LP64
 293   if (s >= G && (s % G) == 0) {
 294     return s / G;
 295   }
 296 #endif
 297   if (s >= M && (s % M) == 0) {
 298     return s / M;
 299   }
 300   if (s >= K && (s % K) == 0) {
 301     return s / K;
 302   }
 303   return s;
 304 }
 305 
 306 //----------------------------------------------------------------------------------------------------
 307 // VM type definitions
 308 
 309 // intx and uintx are the 'extended' int and 'extended' unsigned int types;
 310 // they are 32bit wide on a 32-bit platform, and 64bit wide on a 64bit platform.
 311 
 312 typedef intptr_t  intx;
 313 typedef uintptr_t uintx;
 314 
 315 const intx  min_intx  = (intx)1 << (sizeof(intx)*BitsPerByte-1);
 316 const intx  max_intx  = (uintx)min_intx - 1;
 317 const uintx max_uintx = (uintx)-1;
 318 
 319 // Table of values:
 320 //      sizeof intx         4               8
 321 // min_intx             0x80000000      0x8000000000000000
 322 // max_intx             0x7FFFFFFF      0x7FFFFFFFFFFFFFFF
 323 // max_uintx            0xFFFFFFFF      0xFFFFFFFFFFFFFFFF
 324 
 325 typedef unsigned int uint;   NEEDS_CLEANUP
 326 
 327 
 328 //----------------------------------------------------------------------------------------------------
 329 // Java type definitions
 330 
 331 // All kinds of 'plain' byte addresses
 332 typedef   signed char s_char;
 333 typedef unsigned char u_char;
 334 typedef u_char*       address;
 335 typedef uintptr_t     address_word; // unsigned integer which will hold a pointer
 336                                     // except for some implementations of a C++
 337                                     // linkage pointer to function. Should never
 338                                     // need one of those to be placed in this
 339                                     // type anyway.
 340 
 341 //  Utility functions to "portably" (?) bit twiddle pointers
 342 //  Where portable means keep ANSI C++ compilers quiet
 343 
 344 inline address       set_address_bits(address x, int m)       { return address(intptr_t(x) | m); }
 345 inline address       clear_address_bits(address x, int m)     { return address(intptr_t(x) & ~m); }
 346 
 347 //  Utility functions to "portably" make cast to/from function pointers.
 348 
 349 inline address_word  mask_address_bits(address x, int m)      { return address_word(x) & m; }
 350 inline address_word  castable_address(address x)              { return address_word(x) ; }
 351 inline address_word  castable_address(void* x)                { return address_word(x) ; }
 352 
 353 // Pointer subtraction.
 354 // The idea here is to avoid ptrdiff_t, which is signed and so doesn't have
 355 // the range we might need to find differences from one end of the heap
 356 // to the other.
 357 // A typical use might be:
 358 //     if (pointer_delta(end(), top()) >= size) {
 359 //       // enough room for an object of size
 360 //       ...
 361 // and then additions like
 362 //       ... top() + size ...
 363 // are safe because we know that top() is at least size below end().
 364 inline size_t pointer_delta(const volatile void* left,
 365                             const volatile void* right,
 366                             size_t element_size) {
 367   return (((uintptr_t) left) - ((uintptr_t) right)) / element_size;
 368 }
 369 
 370 // A version specialized for HeapWord*'s.
 371 inline size_t pointer_delta(const HeapWord* left, const HeapWord* right) {
 372   return pointer_delta(left, right, sizeof(HeapWord));
 373 }
 374 // A version specialized for MetaWord*'s.
 375 inline size_t pointer_delta(const MetaWord* left, const MetaWord* right) {
 376   return pointer_delta(left, right, sizeof(MetaWord));
 377 }
 378 
 379 //
 380 // ANSI C++ does not allow casting from one pointer type to a function pointer
 381 // directly without at best a warning. This macro accomplishes it silently
 382 // In every case that is present at this point the value be cast is a pointer
 383 // to a C linkage function. In some case the type used for the cast reflects
 384 // that linkage and a picky compiler would not complain. In other cases because
 385 // there is no convenient place to place a typedef with extern C linkage (i.e
 386 // a platform dependent header file) it doesn't. At this point no compiler seems
 387 // picky enough to catch these instances (which are few). It is possible that
 388 // using templates could fix these for all cases. This use of templates is likely
 389 // so far from the middle of the road that it is likely to be problematic in
 390 // many C++ compilers.
 391 //
 392 #define CAST_TO_FN_PTR(func_type, value) (reinterpret_cast<func_type>(value))
 393 #define CAST_FROM_FN_PTR(new_type, func_ptr) ((new_type)((address_word)(func_ptr)))
 394 
 395 // Unsigned byte types for os and stream.hpp
 396 
 397 // Unsigned one, two, four and eigth byte quantities used for describing
 398 // the .class file format. See JVM book chapter 4.
 399 
 400 typedef jubyte  u1;
 401 typedef jushort u2;
 402 typedef juint   u4;
 403 typedef julong  u8;
 404 
 405 const jubyte  max_jubyte  = (jubyte)-1;  // 0xFF       largest jubyte
 406 const jushort max_jushort = (jushort)-1; // 0xFFFF     largest jushort
 407 const juint   max_juint   = (juint)-1;   // 0xFFFFFFFF largest juint
 408 const julong  max_julong  = (julong)-1;  // 0xFF....FF largest julong
 409 
 410 typedef jbyte  s1;
 411 typedef jshort s2;
 412 typedef jint   s4;
 413 typedef jlong  s8;
 414 
 415 const jbyte min_jbyte = -(1 << 7);       // smallest jbyte
 416 const jbyte max_jbyte = (1 << 7) - 1;    // largest jbyte
 417 const jshort min_jshort = -(1 << 15);    // smallest jshort
 418 const jshort max_jshort = (1 << 15) - 1; // largest jshort
 419 
 420 const jint min_jint = (jint)1 << (sizeof(jint)*BitsPerByte-1); // 0x80000000 == smallest jint
 421 const jint max_jint = (juint)min_jint - 1;                     // 0x7FFFFFFF == largest jint
 422 
 423 //----------------------------------------------------------------------------------------------------
 424 // JVM spec restrictions
 425 
 426 const int max_method_code_size = 64*K - 1;  // JVM spec, 2nd ed. section 4.8.1 (p.134)
 427 
 428 // Default ProtectionDomainCacheSize values
 429 
 430 const int defaultProtectionDomainCacheSize = NOT_LP64(137) LP64_ONLY(2017);
 431 
 432 //----------------------------------------------------------------------------------------------------
 433 // Default and minimum StringTableSize values
 434 
 435 const int defaultStringTableSize = NOT_LP64(1009) LP64_ONLY(60013);
 436 const int minimumStringTableSize = 1009;
 437 
 438 const int defaultSymbolTableSize = 20011;
 439 const int minimumSymbolTableSize = 1009;
 440 
 441 
 442 //----------------------------------------------------------------------------------------------------
 443 // HotSwap - for JVMTI   aka Class File Replacement and PopFrame
 444 //
 445 // Determines whether on-the-fly class replacement and frame popping are enabled.
 446 
 447 #define HOTSWAP
 448 
 449 //----------------------------------------------------------------------------------------------------
 450 // Object alignment, in units of HeapWords.
 451 //
 452 // Minimum is max(BytesPerLong, BytesPerDouble, BytesPerOop) / HeapWordSize, so jlong, jdouble and
 453 // reference fields can be naturally aligned.
 454 
 455 extern int MinObjAlignment;
 456 extern int MinObjAlignmentInBytes;
 457 extern int MinObjAlignmentInBytesMask;
 458 
 459 extern int LogMinObjAlignment;
 460 extern int LogMinObjAlignmentInBytes;
 461 
 462 const int LogKlassAlignmentInBytes = 3;
 463 const int LogKlassAlignment        = LogKlassAlignmentInBytes - LogHeapWordSize;
 464 const int KlassAlignmentInBytes    = 1 << LogKlassAlignmentInBytes;
 465 const int KlassAlignment           = KlassAlignmentInBytes / HeapWordSize;
 466 
 467 // Maximal size of heap where unscaled compression can be used. Also upper bound
 468 // for heap placement: 4GB.
 469 const  uint64_t UnscaledOopHeapMax = (uint64_t(max_juint) + 1);
 470 // Maximal size of heap where compressed oops can be used. Also upper bound for heap
 471 // placement for zero based compression algorithm: UnscaledOopHeapMax << LogMinObjAlignmentInBytes.
 472 extern uint64_t OopEncodingHeapMax;
 473 
 474 // Maximal size of compressed class space. Above this limit compression is not possible.
 475 // Also upper bound for placement of zero based class space. (Class space is further limited
 476 // to be < 3G, see arguments.cpp.)
 477 const  uint64_t KlassEncodingMetaspaceMax = (uint64_t(max_juint) + 1) << LogKlassAlignmentInBytes;
 478 
 479 // Machine dependent stuff
 480 
 481 // The maximum size of the code cache.  Can be overridden by targets.
 482 #define CODE_CACHE_SIZE_LIMIT (2*G)
 483 // Allow targets to reduce the default size of the code cache.
 484 #define CODE_CACHE_DEFAULT_LIMIT CODE_CACHE_SIZE_LIMIT
 485 
 486 #include CPU_HEADER(globalDefinitions)
 487 
 488 // To assure the IRIW property on processors that are not multiple copy
 489 // atomic, sync instructions must be issued between volatile reads to
 490 // assure their ordering, instead of after volatile stores.
 491 // (See "A Tutorial Introduction to the ARM and POWER Relaxed Memory Models"
 492 // by Luc Maranget, Susmit Sarkar and Peter Sewell, INRIA/Cambridge)
 493 #ifdef CPU_NOT_MULTIPLE_COPY_ATOMIC
 494 const bool support_IRIW_for_not_multiple_copy_atomic_cpu = true;
 495 #else
 496 const bool support_IRIW_for_not_multiple_copy_atomic_cpu = false;
 497 #endif
 498 
 499 // The byte alignment to be used by Arena::Amalloc.  See bugid 4169348.
 500 // Note: this value must be a power of 2
 501 
 502 #define ARENA_AMALLOC_ALIGNMENT (2*BytesPerWord)
 503 
 504 // Signed variants of alignment helpers.  There are two versions of each, a macro
 505 // for use in places like enum definitions that require compile-time constant
 506 // expressions and a function for all other places so as to get type checking.
 507 
 508 #define align_size_up_(size, alignment) (((size) + ((alignment) - 1)) & ~((alignment) - 1))
 509 
 510 inline bool is_size_aligned(size_t size, size_t alignment) {
 511   return align_size_up_(size, alignment) == size;
 512 }
 513 
 514 inline bool is_ptr_aligned(const void* ptr, size_t alignment) {
 515   return align_size_up_((intptr_t)ptr, (intptr_t)alignment) == (intptr_t)ptr;
 516 }
 517 
 518 inline intptr_t align_size_up(intptr_t size, intptr_t alignment) {
 519   return align_size_up_(size, alignment);
 520 }
 521 
 522 #define align_size_down_(size, alignment) ((size) & ~((alignment) - 1))
 523 
 524 inline intptr_t align_size_down(intptr_t size, intptr_t alignment) {
 525   return align_size_down_(size, alignment);
 526 }
 527 
 528 #define is_size_aligned_(size, alignment) ((size) == (align_size_up_(size, alignment)))
 529 
 530 inline void* align_ptr_up(const void* ptr, size_t alignment) {
 531   return (void*)align_size_up((intptr_t)ptr, (intptr_t)alignment);
 532 }
 533 
 534 inline void* align_ptr_down(void* ptr, size_t alignment) {
 535   return (void*)align_size_down((intptr_t)ptr, (intptr_t)alignment);
 536 }
 537 
 538 inline volatile void* align_ptr_down(volatile void* ptr, size_t alignment) {
 539   return (volatile void*)align_size_down((intptr_t)ptr, (intptr_t)alignment);
 540 }
 541 
 542 // Align metaspace objects by rounding up to natural word boundary
 543 
 544 inline intptr_t align_metadata_size(intptr_t size) {
 545   return align_size_up(size, 1);
 546 }
 547 
 548 // Align objects in the Java Heap by rounding up their size, in HeapWord units.
 549 // Since the size is given in words this is somewhat of a nop, but
 550 // distinguishes it from align_object_size.
 551 inline intptr_t align_object_size(intptr_t size) {
 552   return align_size_up(size, MinObjAlignment);
 553 }
 554 
 555 inline bool is_object_aligned(intptr_t addr) {
 556   return addr == align_object_size(addr);
 557 }
 558 
 559 inline bool is_ptr_object_aligned(const void* addr) {
 560   return is_ptr_aligned(addr, MinObjAlignmentInBytes);
 561 }
 562 
 563 // Pad out certain offsets to jlong alignment, in HeapWord units.
 564 
 565 inline intptr_t align_object_offset(intptr_t offset) {
 566   return align_size_up(offset, HeapWordsPerLong);
 567 }
 568 
 569 // Align down with a lower bound. If the aligning results in 0, return 'alignment'.
 570 
 571 inline size_t align_size_down_bounded(size_t size, size_t alignment) {
 572   size_t aligned_size = align_size_down_(size, alignment);
 573   return aligned_size > 0 ? aligned_size : alignment;
 574 }
 575 
 576 // Clamp an address to be within a specific page
 577 // 1. If addr is on the page it is returned as is
 578 // 2. If addr is above the page_address the start of the *next* page will be returned
 579 // 3. Otherwise, if addr is below the page_address the start of the page will be returned
 580 inline address clamp_address_in_page(address addr, address page_address, intptr_t page_size) {
 581   if (align_size_down(intptr_t(addr), page_size) == align_size_down(intptr_t(page_address), page_size)) {
 582     // address is in the specified page, just return it as is
 583     return addr;
 584   } else if (addr > page_address) {
 585     // address is above specified page, return start of next page
 586     return (address)align_size_down(intptr_t(page_address), page_size) + page_size;
 587   } else {
 588     // address is below specified page, return start of page
 589     return (address)align_size_down(intptr_t(page_address), page_size);
 590   }
 591 }
 592 
 593 
 594 // The expected size in bytes of a cache line, used to pad data structures.
 595 #ifndef DEFAULT_CACHE_LINE_SIZE
 596   #define DEFAULT_CACHE_LINE_SIZE 64
 597 #endif
 598 
 599 
 600 //----------------------------------------------------------------------------------------------------
 601 // Utility macros for compilers
 602 // used to silence compiler warnings
 603 
 604 #define Unused_Variable(var) var
 605 
 606 
 607 //----------------------------------------------------------------------------------------------------
 608 // Miscellaneous
 609 
 610 // 6302670 Eliminate Hotspot __fabsf dependency
 611 // All fabs() callers should call this function instead, which will implicitly
 612 // convert the operand to double, avoiding a dependency on __fabsf which
 613 // doesn't exist in early versions of Solaris 8.
 614 inline double fabsd(double value) {
 615   return fabs(value);
 616 }
 617 
 618 // Returns numerator/denominator as percentage value from 0 to 100. If denominator
 619 // is zero, return 0.0.
 620 template<typename T>
 621 inline double percent_of(T numerator, T denominator) {
 622   return denominator != 0 ? (double)numerator / denominator * 100.0 : 0.0;
 623 }
 624 
 625 //----------------------------------------------------------------------------------------------------
 626 // Special casts
 627 // Cast floats into same-size integers and vice-versa w/o changing bit-pattern
 628 typedef union {
 629   jfloat f;
 630   jint i;
 631 } FloatIntConv;
 632 
 633 typedef union {
 634   jdouble d;
 635   jlong l;
 636   julong ul;
 637 } DoubleLongConv;
 638 
 639 inline jint    jint_cast    (jfloat  x)  { return ((FloatIntConv*)&x)->i; }
 640 inline jfloat  jfloat_cast  (jint    x)  { return ((FloatIntConv*)&x)->f; }
 641 
 642 inline jlong   jlong_cast   (jdouble x)  { return ((DoubleLongConv*)&x)->l;  }
 643 inline julong  julong_cast  (jdouble x)  { return ((DoubleLongConv*)&x)->ul; }
 644 inline jdouble jdouble_cast (jlong   x)  { return ((DoubleLongConv*)&x)->d;  }
 645 
 646 inline jint low (jlong value)                    { return jint(value); }
 647 inline jint high(jlong value)                    { return jint(value >> 32); }
 648 
 649 // the fancy casts are a hopefully portable way
 650 // to do unsigned 32 to 64 bit type conversion
 651 inline void set_low (jlong* value, jint low )    { *value &= (jlong)0xffffffff << 32;
 652                                                    *value |= (jlong)(julong)(juint)low; }
 653 
 654 inline void set_high(jlong* value, jint high)    { *value &= (jlong)(julong)(juint)0xffffffff;
 655                                                    *value |= (jlong)high       << 32; }
 656 
 657 inline jlong jlong_from(jint h, jint l) {
 658   jlong result = 0; // initialization to avoid warning
 659   set_high(&result, h);
 660   set_low(&result,  l);
 661   return result;
 662 }
 663 
 664 union jlong_accessor {
 665   jint  words[2];
 666   jlong long_value;
 667 };
 668 
 669 void basic_types_init(); // cannot define here; uses assert
 670 
 671 
 672 // NOTE: replicated in SA in vm/agent/sun/jvm/hotspot/runtime/BasicType.java
 673 enum BasicType {
 674   T_BOOLEAN     =  4,
 675   T_CHAR        =  5,
 676   T_FLOAT       =  6,
 677   T_DOUBLE      =  7,
 678   T_BYTE        =  8,
 679   T_SHORT       =  9,
 680   T_INT         = 10,
 681   T_LONG        = 11,
 682   T_OBJECT      = 12,
 683   T_ARRAY       = 13,
 684   T_VOID        = 14,
 685   T_ADDRESS     = 15,
 686   T_NARROWOOP   = 16,
 687   T_METADATA    = 17,
 688   T_NARROWKLASS = 18,
 689   T_CONFLICT    = 19, // for stack value type with conflicting contents
 690   T_ILLEGAL     = 99
 691 };
 692 
 693 inline bool is_java_primitive(BasicType t) {
 694   return T_BOOLEAN <= t && t <= T_LONG;
 695 }
 696 
 697 inline bool is_subword_type(BasicType t) {
 698   // these guys are processed exactly like T_INT in calling sequences:
 699   return (t == T_BOOLEAN || t == T_CHAR || t == T_BYTE || t == T_SHORT);
 700 }
 701 
 702 inline bool is_signed_subword_type(BasicType t) {
 703   return (t == T_BYTE || t == T_SHORT);
 704 }
 705 
 706 // Convert a char from a classfile signature to a BasicType
 707 inline BasicType char2type(char c) {
 708   switch( c ) {
 709   case 'B': return T_BYTE;
 710   case 'C': return T_CHAR;
 711   case 'D': return T_DOUBLE;
 712   case 'F': return T_FLOAT;
 713   case 'I': return T_INT;
 714   case 'J': return T_LONG;
 715   case 'S': return T_SHORT;
 716   case 'Z': return T_BOOLEAN;
 717   case 'V': return T_VOID;
 718   case 'L': return T_OBJECT;
 719   case '[': return T_ARRAY;
 720   }
 721   return T_ILLEGAL;
 722 }
 723 
 724 extern char type2char_tab[T_CONFLICT+1];     // Map a BasicType to a jchar
 725 inline char type2char(BasicType t) { return (uint)t < T_CONFLICT+1 ? type2char_tab[t] : 0; }
 726 extern int type2size[T_CONFLICT+1];         // Map BasicType to result stack elements
 727 extern const char* type2name_tab[T_CONFLICT+1];     // Map a BasicType to a jchar
 728 inline const char* type2name(BasicType t) { return (uint)t < T_CONFLICT+1 ? type2name_tab[t] : NULL; }
 729 extern BasicType name2type(const char* name);
 730 
 731 // Auxiliary math routines
 732 // least common multiple
 733 extern size_t lcm(size_t a, size_t b);
 734 
 735 
 736 // NOTE: replicated in SA in vm/agent/sun/jvm/hotspot/runtime/BasicType.java
 737 enum BasicTypeSize {
 738   T_BOOLEAN_size     = 1,
 739   T_CHAR_size        = 1,
 740   T_FLOAT_size       = 1,
 741   T_DOUBLE_size      = 2,
 742   T_BYTE_size        = 1,
 743   T_SHORT_size       = 1,
 744   T_INT_size         = 1,
 745   T_LONG_size        = 2,
 746   T_OBJECT_size      = 1,
 747   T_ARRAY_size       = 1,
 748   T_NARROWOOP_size   = 1,
 749   T_NARROWKLASS_size = 1,
 750   T_VOID_size        = 0
 751 };
 752 
 753 
 754 // maps a BasicType to its instance field storage type:
 755 // all sub-word integral types are widened to T_INT
 756 extern BasicType type2field[T_CONFLICT+1];
 757 extern BasicType type2wfield[T_CONFLICT+1];
 758 
 759 
 760 // size in bytes
 761 enum ArrayElementSize {
 762   T_BOOLEAN_aelem_bytes     = 1,
 763   T_CHAR_aelem_bytes        = 2,
 764   T_FLOAT_aelem_bytes       = 4,
 765   T_DOUBLE_aelem_bytes      = 8,
 766   T_BYTE_aelem_bytes        = 1,
 767   T_SHORT_aelem_bytes       = 2,
 768   T_INT_aelem_bytes         = 4,
 769   T_LONG_aelem_bytes        = 8,
 770 #ifdef _LP64
 771   T_OBJECT_aelem_bytes      = 8,
 772   T_ARRAY_aelem_bytes       = 8,
 773 #else
 774   T_OBJECT_aelem_bytes      = 4,
 775   T_ARRAY_aelem_bytes       = 4,
 776 #endif
 777   T_NARROWOOP_aelem_bytes   = 4,
 778   T_NARROWKLASS_aelem_bytes = 4,
 779   T_VOID_aelem_bytes        = 0
 780 };
 781 
 782 extern int _type2aelembytes[T_CONFLICT+1]; // maps a BasicType to nof bytes used by its array element
 783 #ifdef ASSERT
 784 extern int type2aelembytes(BasicType t, bool allow_address = false); // asserts
 785 #else
 786 inline int type2aelembytes(BasicType t, bool allow_address = false) { return _type2aelembytes[t]; }
 787 #endif
 788 
 789 
 790 // JavaValue serves as a container for arbitrary Java values.
 791 
 792 class JavaValue {
 793 
 794  public:
 795   typedef union JavaCallValue {
 796     jfloat   f;
 797     jdouble  d;
 798     jint     i;
 799     jlong    l;
 800     jobject  h;
 801   } JavaCallValue;
 802 
 803  private:
 804   BasicType _type;
 805   JavaCallValue _value;
 806 
 807  public:
 808   JavaValue(BasicType t = T_ILLEGAL) { _type = t; }
 809 
 810   JavaValue(jfloat value) {
 811     _type    = T_FLOAT;
 812     _value.f = value;
 813   }
 814 
 815   JavaValue(jdouble value) {
 816     _type    = T_DOUBLE;
 817     _value.d = value;
 818   }
 819 
 820  jfloat get_jfloat() const { return _value.f; }
 821  jdouble get_jdouble() const { return _value.d; }
 822  jint get_jint() const { return _value.i; }
 823  jlong get_jlong() const { return _value.l; }
 824  jobject get_jobject() const { return _value.h; }
 825  JavaCallValue* get_value_addr() { return &_value; }
 826  BasicType get_type() const { return _type; }
 827 
 828  void set_jfloat(jfloat f) { _value.f = f;}
 829  void set_jdouble(jdouble d) { _value.d = d;}
 830  void set_jint(jint i) { _value.i = i;}
 831  void set_jlong(jlong l) { _value.l = l;}
 832  void set_jobject(jobject h) { _value.h = h;}
 833  void set_type(BasicType t) { _type = t; }
 834 
 835  jboolean get_jboolean() const { return (jboolean) (_value.i);}
 836  jbyte get_jbyte() const { return (jbyte) (_value.i);}
 837  jchar get_jchar() const { return (jchar) (_value.i);}
 838  jshort get_jshort() const { return (jshort) (_value.i);}
 839 
 840 };
 841 
 842 
 843 #define STACK_BIAS      0
 844 // V9 Sparc CPU's running in 64 Bit mode use a stack bias of 7ff
 845 // in order to extend the reach of the stack pointer.
 846 #if defined(SPARC) && defined(_LP64)
 847 #undef STACK_BIAS
 848 #define STACK_BIAS      0x7ff
 849 #endif
 850 
 851 
 852 // TosState describes the top-of-stack state before and after the execution of
 853 // a bytecode or method. The top-of-stack value may be cached in one or more CPU
 854 // registers. The TosState corresponds to the 'machine representation' of this cached
 855 // value. There's 4 states corresponding to the JAVA types int, long, float & double
 856 // as well as a 5th state in case the top-of-stack value is actually on the top
 857 // of stack (in memory) and thus not cached. The atos state corresponds to the itos
 858 // state when it comes to machine representation but is used separately for (oop)
 859 // type specific operations (e.g. verification code).
 860 
 861 enum TosState {         // describes the tos cache contents
 862   btos = 0,             // byte, bool tos cached
 863   ztos = 1,             // byte, bool tos cached
 864   ctos = 2,             // char tos cached
 865   stos = 3,             // short tos cached
 866   itos = 4,             // int tos cached
 867   ltos = 5,             // long tos cached
 868   ftos = 6,             // float tos cached
 869   dtos = 7,             // double tos cached
 870   atos = 8,             // object cached
 871   vtos = 9,             // tos not cached
 872   number_of_states,
 873   ilgl                  // illegal state: should not occur
 874 };
 875 
 876 
 877 inline TosState as_TosState(BasicType type) {
 878   switch (type) {
 879     case T_BYTE   : return btos;
 880     case T_BOOLEAN: return ztos;
 881     case T_CHAR   : return ctos;
 882     case T_SHORT  : return stos;
 883     case T_INT    : return itos;
 884     case T_LONG   : return ltos;
 885     case T_FLOAT  : return ftos;
 886     case T_DOUBLE : return dtos;
 887     case T_VOID   : return vtos;
 888     case T_ARRAY  : // fall through
 889     case T_OBJECT : return atos;
 890   }
 891   return ilgl;
 892 }
 893 
 894 inline BasicType as_BasicType(TosState state) {
 895   switch (state) {
 896     case btos : return T_BYTE;
 897     case ztos : return T_BOOLEAN;
 898     case ctos : return T_CHAR;
 899     case stos : return T_SHORT;
 900     case itos : return T_INT;
 901     case ltos : return T_LONG;
 902     case ftos : return T_FLOAT;
 903     case dtos : return T_DOUBLE;
 904     case atos : return T_OBJECT;
 905     case vtos : return T_VOID;
 906   }
 907   return T_ILLEGAL;
 908 }
 909 
 910 
 911 // Helper function to convert BasicType info into TosState
 912 // Note: Cannot define here as it uses global constant at the time being.
 913 TosState as_TosState(BasicType type);
 914 
 915 
 916 // JavaThreadState keeps track of which part of the code a thread is executing in. This
 917 // information is needed by the safepoint code.
 918 //
 919 // There are 4 essential states:
 920 //
 921 //  _thread_new         : Just started, but not executed init. code yet (most likely still in OS init code)
 922 //  _thread_in_native   : In native code. This is a safepoint region, since all oops will be in jobject handles
 923 //  _thread_in_vm       : Executing in the vm
 924 //  _thread_in_Java     : Executing either interpreted or compiled Java code (or could be in a stub)
 925 //
 926 // Each state has an associated xxxx_trans state, which is an intermediate state used when a thread is in
 927 // a transition from one state to another. These extra states makes it possible for the safepoint code to
 928 // handle certain thread_states without having to suspend the thread - making the safepoint code faster.
 929 //
 930 // Given a state, the xxxx_trans state can always be found by adding 1.
 931 //
 932 enum JavaThreadState {
 933   _thread_uninitialized     =  0, // should never happen (missing initialization)
 934   _thread_new               =  2, // just starting up, i.e., in process of being initialized
 935   _thread_new_trans         =  3, // corresponding transition state (not used, included for completness)
 936   _thread_in_native         =  4, // running in native code
 937   _thread_in_native_trans   =  5, // corresponding transition state
 938   _thread_in_vm             =  6, // running in VM
 939   _thread_in_vm_trans       =  7, // corresponding transition state
 940   _thread_in_Java           =  8, // running in Java or in stub code
 941   _thread_in_Java_trans     =  9, // corresponding transition state (not used, included for completness)
 942   _thread_blocked           = 10, // blocked in vm
 943   _thread_blocked_trans     = 11, // corresponding transition state
 944   _thread_max_state         = 12  // maximum thread state+1 - used for statistics allocation
 945 };
 946 
 947 
 948 
 949 //----------------------------------------------------------------------------------------------------
 950 // 'Forward' declarations of frequently used classes
 951 // (in order to reduce interface dependencies & reduce
 952 // number of unnecessary compilations after changes)
 953 
 954 class ClassFileStream;
 955 
 956 class Event;
 957 
 958 class Thread;
 959 class  VMThread;
 960 class  JavaThread;
 961 class Threads;
 962 
 963 class VM_Operation;
 964 class VMOperationQueue;
 965 
 966 class CodeBlob;
 967 class  CompiledMethod;
 968 class   nmethod;
 969 class RuntimeBlob;
 970 class  OSRAdapter;
 971 class  I2CAdapter;
 972 class  C2IAdapter;
 973 class CompiledIC;
 974 class relocInfo;
 975 class ScopeDesc;
 976 class PcDesc;
 977 
 978 class Recompiler;
 979 class Recompilee;
 980 class RecompilationPolicy;
 981 class RFrame;
 982 class  CompiledRFrame;
 983 class  InterpretedRFrame;
 984 
 985 class vframe;
 986 class   javaVFrame;
 987 class     interpretedVFrame;
 988 class     compiledVFrame;
 989 class     deoptimizedVFrame;
 990 class   externalVFrame;
 991 class     entryVFrame;
 992 
 993 class RegisterMap;
 994 
 995 class Mutex;
 996 class Monitor;
 997 class BasicLock;
 998 class BasicObjectLock;
 999 
1000 class PeriodicTask;
1001 
1002 class JavaCallWrapper;
1003 
1004 class   oopDesc;
1005 class   metaDataOopDesc;
1006 
1007 class NativeCall;
1008 
1009 class zone;
1010 
1011 class StubQueue;
1012 
1013 class outputStream;
1014 
1015 class ResourceArea;
1016 
1017 class DebugInformationRecorder;
1018 class ScopeValue;
1019 class CompressedStream;
1020 class   DebugInfoReadStream;
1021 class   DebugInfoWriteStream;
1022 class LocationValue;
1023 class ConstantValue;
1024 class IllegalValue;
1025 
1026 class PrivilegedElement;
1027 class MonitorArray;
1028 
1029 class MonitorInfo;
1030 
1031 class OffsetClosure;
1032 class OopMapCache;
1033 class InterpreterOopMap;
1034 class OopMapCacheEntry;
1035 class OSThread;
1036 
1037 typedef int (*OSThreadStartFunc)(void*);
1038 
1039 class Space;
1040 
1041 class JavaValue;
1042 class methodHandle;
1043 class JavaCallArguments;
1044 
1045 // Basic support for errors.
1046 extern void basic_fatal(const char* msg);
1047 
1048 //----------------------------------------------------------------------------------------------------
1049 // Special constants for debugging
1050 
1051 const jint     badInt           = -3;                       // generic "bad int" value
1052 const long     badAddressVal    = -2;                       // generic "bad address" value
1053 const long     badOopVal        = -1;                       // generic "bad oop" value
1054 const intptr_t badHeapOopVal    = (intptr_t) CONST64(0x2BAD4B0BBAADBABE); // value used to zap heap after GC
1055 const int      badHandleValue   = 0xBC;                     // value used to zap vm handle area
1056 const int      badResourceValue = 0xAB;                     // value used to zap resource area
1057 const int      freeBlockPad     = 0xBA;                     // value used to pad freed blocks.
1058 const int      uninitBlockPad   = 0xF1;                     // value used to zap newly malloc'd blocks.
1059 const juint    uninitMetaWordVal= 0xf7f7f7f7;               // value used to zap newly allocated metachunk
1060 const intptr_t badJNIHandleVal  = (intptr_t) UCONST64(0xFEFEFEFEFEFEFEFE); // value used to zap jni handle area
1061 const juint    badHeapWordVal   = 0xBAADBABE;               // value used to zap heap after GC
1062 const juint    badMetaWordVal   = 0xBAADFADE;               // value used to zap metadata heap after GC
1063 const int      badCodeHeapNewVal= 0xCC;                     // value used to zap Code heap at allocation
1064 const int      badCodeHeapFreeVal = 0xDD;                   // value used to zap Code heap at deallocation
1065 
1066 
1067 // (These must be implemented as #defines because C++ compilers are
1068 // not obligated to inline non-integral constants!)
1069 #define       badAddress        ((address)::badAddressVal)
1070 #define       badOop            (cast_to_oop(::badOopVal))
1071 #define       badHeapWord       (::badHeapWordVal)
1072 #define       badJNIHandle      (cast_to_oop(::badJNIHandleVal))
1073 
1074 // Default TaskQueue size is 16K (32-bit) or 128K (64-bit)
1075 #define TASKQUEUE_SIZE (NOT_LP64(1<<14) LP64_ONLY(1<<17))
1076 
1077 //----------------------------------------------------------------------------------------------------
1078 // Utility functions for bitfield manipulations
1079 
1080 const intptr_t AllBits    = ~0; // all bits set in a word
1081 const intptr_t NoBits     =  0; // no bits set in a word
1082 const jlong    NoLongBits =  0; // no bits set in a long
1083 const intptr_t OneBit     =  1; // only right_most bit set in a word
1084 
1085 // get a word with the n.th or the right-most or left-most n bits set
1086 // (note: #define used only so that they can be used in enum constant definitions)
1087 #define nth_bit(n)        (((n) >= BitsPerWord) ? 0 : (OneBit << (n)))
1088 #define right_n_bits(n)   (nth_bit(n) - 1)
1089 #define left_n_bits(n)    (right_n_bits(n) << (((n) >= BitsPerWord) ? 0 : (BitsPerWord - (n))))
1090 
1091 // bit-operations using a mask m
1092 inline void   set_bits    (intptr_t& x, intptr_t m) { x |= m; }
1093 inline void clear_bits    (intptr_t& x, intptr_t m) { x &= ~m; }
1094 inline intptr_t mask_bits      (intptr_t  x, intptr_t m) { return x & m; }
1095 inline jlong    mask_long_bits (jlong     x, jlong    m) { return x & m; }
1096 inline bool mask_bits_are_true (intptr_t flags, intptr_t mask) { return (flags & mask) == mask; }
1097 
1098 // bit-operations using the n.th bit
1099 inline void    set_nth_bit(intptr_t& x, int n) { set_bits  (x, nth_bit(n)); }
1100 inline void  clear_nth_bit(intptr_t& x, int n) { clear_bits(x, nth_bit(n)); }
1101 inline bool is_set_nth_bit(intptr_t  x, int n) { return mask_bits (x, nth_bit(n)) != NoBits; }
1102 
1103 // returns the bitfield of x starting at start_bit_no with length field_length (no sign-extension!)
1104 inline intptr_t bitfield(intptr_t x, int start_bit_no, int field_length) {
1105   return mask_bits(x >> start_bit_no, right_n_bits(field_length));
1106 }
1107 
1108 
1109 //----------------------------------------------------------------------------------------------------
1110 // Utility functions for integers
1111 
1112 // Avoid use of global min/max macros which may cause unwanted double
1113 // evaluation of arguments.
1114 #ifdef max
1115 #undef max
1116 #endif
1117 
1118 #ifdef min
1119 #undef min
1120 #endif
1121 
1122 // The following defines serve the purpose of preventing use of accidentally
1123 // included min max macros from compiling, while continuing to allow innocent
1124 // min and max identifiers in the code to compile as intended.
1125 #define max max
1126 #define min min
1127 
1128 // It is necessary to use templates here. Having normal overloaded
1129 // functions does not work because it is necessary to provide both 32-
1130 // and 64-bit overloaded functions, which does not work, and having
1131 // explicitly-typed versions of these routines (i.e., MAX2I, MAX2L)
1132 // will be even more error-prone than macros.
1133 template<class T> inline T MAX2(T a, T b)           { return (a > b) ? a : b; }
1134 template<class T> inline T MIN2(T a, T b)           { return (a < b) ? a : b; }
1135 template<class T> inline T MAX3(T a, T b, T c)      { return MAX2(MAX2(a, b), c); }
1136 template<class T> inline T MIN3(T a, T b, T c)      { return MIN2(MIN2(a, b), c); }
1137 template<class T> inline T MAX4(T a, T b, T c, T d) { return MAX2(MAX3(a, b, c), d); }
1138 template<class T> inline T MIN4(T a, T b, T c, T d) { return MIN2(MIN3(a, b, c), d); }
1139 
1140 template<class T> inline T ABS(T x)                 { return (x > 0) ? x : -x; }
1141 
1142 // true if x is a power of 2, false otherwise
1143 inline bool is_power_of_2(intptr_t x) {
1144   return ((x != NoBits) && (mask_bits(x, x - 1) == NoBits));
1145 }
1146 
1147 // long version of is_power_of_2
1148 inline bool is_power_of_2_long(jlong x) {
1149   return ((x != NoLongBits) && (mask_long_bits(x, x - 1) == NoLongBits));
1150 }
1151 
1152 // Returns largest i such that 2^i <= x.
1153 // If x < 0, the function returns 31 on a 32-bit machine and 63 on a 64-bit machine.
1154 // If x == 0, the function returns -1.
1155 inline int log2_intptr(intptr_t x) {
1156   int i = -1;
1157   uintptr_t p = 1;
1158   while (p != 0 && p <= (uintptr_t)x) {
1159     // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x)
1160     i++; p *= 2;
1161   }
1162   // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1))
1163   // If p = 0, overflow has occurred and i = 31 or i = 63 (depending on the machine word size).
1164   return i;
1165 }
1166 
1167 //* largest i such that 2^i <= x
1168 //  A negative value of 'x' will return '63'
1169 inline int log2_long(jlong x) {
1170   int i = -1;
1171   julong p =  1;
1172   while (p != 0 && p <= (julong)x) {
1173     // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x)
1174     i++; p *= 2;
1175   }
1176   // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1))
1177   // (if p = 0 then overflow occurred and i = 63)
1178   return i;
1179 }
1180 
1181 //* the argument must be exactly a power of 2
1182 inline int exact_log2(intptr_t x) {
1183   assert(is_power_of_2(x), "x must be a power of 2: " INTPTR_FORMAT, x);
1184   return log2_intptr(x);
1185 }
1186 
1187 //* the argument must be exactly a power of 2
1188 inline int exact_log2_long(jlong x) {
1189   assert(is_power_of_2_long(x), "x must be a power of 2: " JLONG_FORMAT, x);
1190   return log2_long(x);
1191 }
1192 
1193 
1194 // returns integer round-up to the nearest multiple of s (s must be a power of two)
1195 inline intptr_t round_to(intptr_t x, uintx s) {
1196   assert(is_power_of_2(s), "s must be a power of 2: " UINTX_FORMAT, s);
1197   const uintx m = s - 1;
1198   return mask_bits(x + m, ~m);
1199 }
1200 
1201 // returns integer round-down to the nearest multiple of s (s must be a power of two)
1202 inline intptr_t round_down(intptr_t x, uintx s) {
1203   assert(is_power_of_2(s), "s must be a power of 2: " UINTX_FORMAT, s);
1204   const uintx m = s - 1;
1205   return mask_bits(x, ~m);
1206 }
1207 
1208 
1209 inline bool is_odd (intx x) { return x & 1;      }
1210 inline bool is_even(intx x) { return !is_odd(x); }
1211 
1212 // "to" should be greater than "from."
1213 inline intx byte_size(void* from, void* to) {
1214   return (address)to - (address)from;
1215 }
1216 
1217 //----------------------------------------------------------------------------------------------------
1218 // Avoid non-portable casts with these routines (DEPRECATED)
1219 
1220 // NOTE: USE Bytes class INSTEAD WHERE POSSIBLE
1221 //       Bytes is optimized machine-specifically and may be much faster then the portable routines below.
1222 
1223 // Given sequence of four bytes, build into a 32-bit word
1224 // following the conventions used in class files.
1225 // On the 386, this could be realized with a simple address cast.
1226 //
1227 
1228 // This routine takes eight bytes:
1229 inline u8 build_u8_from( u1 c1, u1 c2, u1 c3, u1 c4, u1 c5, u1 c6, u1 c7, u1 c8 ) {
1230   return  (( u8(c1) << 56 )  &  ( u8(0xff) << 56 ))
1231        |  (( u8(c2) << 48 )  &  ( u8(0xff) << 48 ))
1232        |  (( u8(c3) << 40 )  &  ( u8(0xff) << 40 ))
1233        |  (( u8(c4) << 32 )  &  ( u8(0xff) << 32 ))
1234        |  (( u8(c5) << 24 )  &  ( u8(0xff) << 24 ))
1235        |  (( u8(c6) << 16 )  &  ( u8(0xff) << 16 ))
1236        |  (( u8(c7) <<  8 )  &  ( u8(0xff) <<  8 ))
1237        |  (( u8(c8) <<  0 )  &  ( u8(0xff) <<  0 ));
1238 }
1239 
1240 // This routine takes four bytes:
1241 inline u4 build_u4_from( u1 c1, u1 c2, u1 c3, u1 c4 ) {
1242   return  (( u4(c1) << 24 )  &  0xff000000)
1243        |  (( u4(c2) << 16 )  &  0x00ff0000)
1244        |  (( u4(c3) <<  8 )  &  0x0000ff00)
1245        |  (( u4(c4) <<  0 )  &  0x000000ff);
1246 }
1247 
1248 // And this one works if the four bytes are contiguous in memory:
1249 inline u4 build_u4_from( u1* p ) {
1250   return  build_u4_from( p[0], p[1], p[2], p[3] );
1251 }
1252 
1253 // Ditto for two-byte ints:
1254 inline u2 build_u2_from( u1 c1, u1 c2 ) {
1255   return  u2((( u2(c1) <<  8 )  &  0xff00)
1256           |  (( u2(c2) <<  0 )  &  0x00ff));
1257 }
1258 
1259 // And this one works if the two bytes are contiguous in memory:
1260 inline u2 build_u2_from( u1* p ) {
1261   return  build_u2_from( p[0], p[1] );
1262 }
1263 
1264 // Ditto for floats:
1265 inline jfloat build_float_from( u1 c1, u1 c2, u1 c3, u1 c4 ) {
1266   u4 u = build_u4_from( c1, c2, c3, c4 );
1267   return  *(jfloat*)&u;
1268 }
1269 
1270 inline jfloat build_float_from( u1* p ) {
1271   u4 u = build_u4_from( p );
1272   return  *(jfloat*)&u;
1273 }
1274 
1275 
1276 // now (64-bit) longs
1277 
1278 inline jlong build_long_from( u1 c1, u1 c2, u1 c3, u1 c4, u1 c5, u1 c6, u1 c7, u1 c8 ) {
1279   return  (( jlong(c1) << 56 )  &  ( jlong(0xff) << 56 ))
1280        |  (( jlong(c2) << 48 )  &  ( jlong(0xff) << 48 ))
1281        |  (( jlong(c3) << 40 )  &  ( jlong(0xff) << 40 ))
1282        |  (( jlong(c4) << 32 )  &  ( jlong(0xff) << 32 ))
1283        |  (( jlong(c5) << 24 )  &  ( jlong(0xff) << 24 ))
1284        |  (( jlong(c6) << 16 )  &  ( jlong(0xff) << 16 ))
1285        |  (( jlong(c7) <<  8 )  &  ( jlong(0xff) <<  8 ))
1286        |  (( jlong(c8) <<  0 )  &  ( jlong(0xff) <<  0 ));
1287 }
1288 
1289 inline jlong build_long_from( u1* p ) {
1290   return  build_long_from( p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7] );
1291 }
1292 
1293 
1294 // Doubles, too!
1295 inline jdouble build_double_from( u1 c1, u1 c2, u1 c3, u1 c4, u1 c5, u1 c6, u1 c7, u1 c8 ) {
1296   jlong u = build_long_from( c1, c2, c3, c4, c5, c6, c7, c8 );
1297   return  *(jdouble*)&u;
1298 }
1299 
1300 inline jdouble build_double_from( u1* p ) {
1301   jlong u = build_long_from( p );
1302   return  *(jdouble*)&u;
1303 }
1304 
1305 
1306 // Portable routines to go the other way:
1307 
1308 inline void explode_short_to( u2 x, u1& c1, u1& c2 ) {
1309   c1 = u1(x >> 8);
1310   c2 = u1(x);
1311 }
1312 
1313 inline void explode_short_to( u2 x, u1* p ) {
1314   explode_short_to( x, p[0], p[1]);
1315 }
1316 
1317 inline void explode_int_to( u4 x, u1& c1, u1& c2, u1& c3, u1& c4 ) {
1318   c1 = u1(x >> 24);
1319   c2 = u1(x >> 16);
1320   c3 = u1(x >>  8);
1321   c4 = u1(x);
1322 }
1323 
1324 inline void explode_int_to( u4 x, u1* p ) {
1325   explode_int_to( x, p[0], p[1], p[2], p[3]);
1326 }
1327 
1328 
1329 // Pack and extract shorts to/from ints:
1330 
1331 inline int extract_low_short_from_int(jint x) {
1332   return x & 0xffff;
1333 }
1334 
1335 inline int extract_high_short_from_int(jint x) {
1336   return (x >> 16) & 0xffff;
1337 }
1338 
1339 inline int build_int_from_shorts( jushort low, jushort high ) {
1340   return ((int)((unsigned int)high << 16) | (unsigned int)low);
1341 }
1342 
1343 // Convert pointer to intptr_t, for use in printing pointers.
1344 inline intptr_t p2i(const void * p) {
1345   return (intptr_t) p;
1346 }
1347 
1348 // swap a & b
1349 template<class T> static void swap(T& a, T& b) {
1350   T tmp = a;
1351   a = b;
1352   b = tmp;
1353 }
1354 
1355 #define ARRAY_SIZE(array) (sizeof(array)/sizeof((array)[0]))
1356 
1357 //----------------------------------------------------------------------------------------------------
1358 // Sum and product which can never overflow: they wrap, just like the
1359 // Java operations.  Note that we don't intend these to be used for
1360 // general-purpose arithmetic: their purpose is to emulate Java
1361 // operations.
1362 
1363 // The goal of this code to avoid undefined or implementation-defined
1364 // behavior.  The use of an lvalue to reference cast is explicitly
1365 // permitted by Lvalues and rvalues [basic.lval].  [Section 3.10 Para
1366 // 15 in C++03]
1367 #define JAVA_INTEGER_OP(OP, NAME, TYPE, UNSIGNED_TYPE)  \
1368 inline TYPE NAME (TYPE in1, TYPE in2) {                 \
1369   UNSIGNED_TYPE ures = static_cast<UNSIGNED_TYPE>(in1); \
1370   ures OP ## = static_cast<UNSIGNED_TYPE>(in2);         \
1371   return reinterpret_cast<TYPE&>(ures);                 \
1372 }
1373 
1374 JAVA_INTEGER_OP(+, java_add, jint, juint)
1375 JAVA_INTEGER_OP(-, java_subtract, jint, juint)
1376 JAVA_INTEGER_OP(*, java_multiply, jint, juint)
1377 JAVA_INTEGER_OP(+, java_add, jlong, julong)
1378 JAVA_INTEGER_OP(-, java_subtract, jlong, julong)
1379 JAVA_INTEGER_OP(*, java_multiply, jlong, julong)
1380 
1381 #undef JAVA_INTEGER_OP
1382 
1383 // Dereference vptr
1384 // All C++ compilers that we know of have the vtbl pointer in the first
1385 // word.  If there are exceptions, this function needs to be made compiler
1386 // specific.
1387 static inline void* dereference_vptr(const void* addr) {
1388   return *(void**)addr;
1389 }
1390 
1391 #endif // SHARE_VM_UTILITIES_GLOBALDEFINITIONS_HPP