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