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