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