1 /*
   2  * Copyright 1997-2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 // This file holds all globally used constants & types, class (forward)
  26 // declarations and a few frequently used utility functions.
  27 
  28 //----------------------------------------------------------------------------------------------------
  29 // Constants
  30 
  31 const int LogBytesPerShort   = 1;
  32 const int LogBytesPerInt     = 2;
  33 #ifdef _LP64
  34 const int LogBytesPerWord    = 3;
  35 #else
  36 const int LogBytesPerWord    = 2;
  37 #endif
  38 const int LogBytesPerLong    = 3;
  39 
  40 const int BytesPerShort      = 1 << LogBytesPerShort;
  41 const int BytesPerInt        = 1 << LogBytesPerInt;
  42 const int BytesPerWord       = 1 << LogBytesPerWord;
  43 const int BytesPerLong       = 1 << LogBytesPerLong;
  44 
  45 const int LogBitsPerByte     = 3;
  46 const int LogBitsPerShort    = LogBitsPerByte + LogBytesPerShort;
  47 const int LogBitsPerInt      = LogBitsPerByte + LogBytesPerInt;
  48 const int LogBitsPerWord     = LogBitsPerByte + LogBytesPerWord;
  49 const int LogBitsPerLong     = LogBitsPerByte + LogBytesPerLong;
  50 
  51 const int BitsPerByte        = 1 << LogBitsPerByte;
  52 const int BitsPerShort       = 1 << LogBitsPerShort;
  53 const int BitsPerInt         = 1 << LogBitsPerInt;
  54 const int BitsPerWord        = 1 << LogBitsPerWord;
  55 const int BitsPerLong        = 1 << LogBitsPerLong;
  56 
  57 const int WordAlignmentMask  = (1 << LogBytesPerWord) - 1;
  58 const int LongAlignmentMask  = (1 << LogBytesPerLong) - 1;
  59 
  60 const int WordsPerLong       = 2;       // Number of stack entries for longs
  61 
  62 const int oopSize            = sizeof(char*); // Full-width oop
  63 extern int heapOopSize;                       // Oop within a java object
  64 const int wordSize           = sizeof(char*);
  65 const int longSize           = sizeof(jlong);
  66 const int jintSize           = sizeof(jint);
  67 const int size_tSize         = sizeof(size_t);
  68 
  69 const int BytesPerOop        = BytesPerWord;  // Full-width oop
  70 
  71 extern int LogBytesPerHeapOop;                // Oop within a java object
  72 extern int LogBitsPerHeapOop;
  73 extern int BytesPerHeapOop;
  74 extern int BitsPerHeapOop;
  75 
  76 const int BitsPerJavaInteger = 32;
  77 const int BitsPerSize_t      = size_tSize * BitsPerByte;
  78 
  79 // Size of a char[] needed to represent a jint as a string in decimal.
  80 const int jintAsStringSize = 12;
  81 
  82 // In fact this should be
  83 // log2_intptr(sizeof(class JavaThread)) - log2_intptr(64);
  84 // see os::set_memory_serialize_page()
  85 #ifdef _LP64
  86 const int SerializePageShiftCount = 4;
  87 #else
  88 const int SerializePageShiftCount = 3;
  89 #endif
  90 
  91 // An opaque struct of heap-word width, so that HeapWord* can be a generic
  92 // pointer into the heap.  We require that object sizes be measured in
  93 // units of heap words, so that that
  94 //   HeapWord* hw;
  95 //   hw += oop(hw)->foo();
  96 // works, where foo is a method (like size or scavenge) that returns the
  97 // object size.
  98 class HeapWord {
  99   friend class VMStructs;
 100  private:
 101   char* i;
 102 #ifndef PRODUCT
 103  public:
 104   char* value() { return i; }
 105 #endif
 106 };
 107 
 108 // HeapWordSize must be 2^LogHeapWordSize.
 109 const int HeapWordSize        = sizeof(HeapWord);
 110 #ifdef _LP64
 111 const int LogHeapWordSize     = 3;
 112 #else
 113 const int LogHeapWordSize     = 2;
 114 #endif
 115 const int HeapWordsPerLong    = BytesPerLong / HeapWordSize;
 116 const int LogHeapWordsPerLong = LogBytesPerLong - LogHeapWordSize;
 117 
 118 // The larger HeapWordSize for 64bit requires larger heaps
 119 // for the same application running in 64bit.  See bug 4967770.
 120 // The minimum alignment to a heap word size is done.  Other
 121 // parts of the memory system may required additional alignment
 122 // and are responsible for those alignments.
 123 #ifdef _LP64
 124 #define ScaleForWordSize(x) align_size_down_((x) * 13 / 10, HeapWordSize)
 125 #else
 126 #define ScaleForWordSize(x) (x)
 127 #endif
 128 
 129 // The minimum number of native machine words necessary to contain "byte_size"
 130 // bytes.
 131 inline size_t heap_word_size(size_t byte_size) {
 132   return (byte_size + (HeapWordSize-1)) >> LogHeapWordSize;
 133 }
 134 
 135 
 136 const size_t K                  = 1024;
 137 const size_t M                  = K*K;
 138 const size_t G                  = M*K;
 139 const size_t HWperKB            = K / sizeof(HeapWord);
 140 
 141 const jint min_jint = (jint)1 << (sizeof(jint)*BitsPerByte-1); // 0x80000000 == smallest jint
 142 const jint max_jint = (juint)min_jint - 1;                     // 0x7FFFFFFF == largest jint
 143 
 144 // Constants for converting from a base unit to milli-base units.  For
 145 // example from seconds to milliseconds and microseconds
 146 
 147 const int MILLIUNITS    = 1000;         // milli units per base unit
 148 const int MICROUNITS    = 1000000;      // micro units per base unit
 149 const int NANOUNITS     = 1000000000;   // nano units per base unit
 150 
 151 inline const char* proper_unit_for_byte_size(size_t s) {
 152   if (s >= 10*M) {
 153     return "M";
 154   } else if (s >= 10*K) {
 155     return "K";
 156   } else {
 157     return "B";
 158   }
 159 }
 160 
 161 inline size_t byte_size_in_proper_unit(size_t s) {
 162   if (s >= 10*M) {
 163     return s/M;
 164   } else if (s >= 10*K) {
 165     return s/K;
 166   } else {
 167     return s;
 168   }
 169 }
 170 
 171 
 172 //----------------------------------------------------------------------------------------------------
 173 // VM type definitions
 174 
 175 // intx and uintx are the 'extended' int and 'extended' unsigned int types;
 176 // they are 32bit wide on a 32-bit platform, and 64bit wide on a 64bit platform.
 177 
 178 typedef intptr_t  intx;
 179 typedef uintptr_t uintx;
 180 
 181 const intx  min_intx  = (intx)1 << (sizeof(intx)*BitsPerByte-1);
 182 const intx  max_intx  = (uintx)min_intx - 1;
 183 const uintx max_uintx = (uintx)-1;
 184 
 185 // Table of values:
 186 //      sizeof intx         4               8
 187 // min_intx             0x80000000      0x8000000000000000
 188 // max_intx             0x7FFFFFFF      0x7FFFFFFFFFFFFFFF
 189 // max_uintx            0xFFFFFFFF      0xFFFFFFFFFFFFFFFF
 190 
 191 typedef unsigned int uint;   NEEDS_CLEANUP
 192 
 193 
 194 //----------------------------------------------------------------------------------------------------
 195 // Java type definitions
 196 
 197 // All kinds of 'plain' byte addresses
 198 typedef   signed char s_char;
 199 typedef unsigned char u_char;
 200 typedef u_char*       address;
 201 typedef uintptr_t     address_word; // unsigned integer which will hold a pointer
 202                                     // except for some implementations of a C++
 203                                     // linkage pointer to function. Should never
 204                                     // need one of those to be placed in this
 205                                     // type anyway.
 206 
 207 //  Utility functions to "portably" (?) bit twiddle pointers
 208 //  Where portable means keep ANSI C++ compilers quiet
 209 
 210 inline address       set_address_bits(address x, int m)       { return address(intptr_t(x) | m); }
 211 inline address       clear_address_bits(address x, int m)     { return address(intptr_t(x) & ~m); }
 212 
 213 //  Utility functions to "portably" make cast to/from function pointers.
 214 
 215 inline address_word  mask_address_bits(address x, int m)      { return address_word(x) & m; }
 216 inline address_word  castable_address(address x)              { return address_word(x) ; }
 217 inline address_word  castable_address(void* x)                { return address_word(x) ; }
 218 
 219 // Pointer subtraction.
 220 // The idea here is to avoid ptrdiff_t, which is signed and so doesn't have
 221 // the range we might need to find differences from one end of the heap
 222 // to the other.
 223 // A typical use might be:
 224 //     if (pointer_delta(end(), top()) >= size) {
 225 //       // enough room for an object of size
 226 //       ...
 227 // and then additions like
 228 //       ... top() + size ...
 229 // are safe because we know that top() is at least size below end().
 230 inline size_t pointer_delta(const void* left,
 231                             const void* right,
 232                             size_t element_size) {
 233   return (((uintptr_t) left) - ((uintptr_t) right)) / element_size;
 234 }
 235 // A version specialized for HeapWord*'s.
 236 inline size_t pointer_delta(const HeapWord* left, const HeapWord* right) {
 237   return pointer_delta(left, right, sizeof(HeapWord));
 238 }
 239 
 240 //
 241 // ANSI C++ does not allow casting from one pointer type to a function pointer
 242 // directly without at best a warning. This macro accomplishes it silently
 243 // In every case that is present at this point the value be cast is a pointer
 244 // to a C linkage function. In somecase the type used for the cast reflects
 245 // that linkage and a picky compiler would not complain. In other cases because
 246 // there is no convenient place to place a typedef with extern C linkage (i.e
 247 // a platform dependent header file) it doesn't. At this point no compiler seems
 248 // picky enough to catch these instances (which are few). It is possible that
 249 // using templates could fix these for all cases. This use of templates is likely
 250 // so far from the middle of the road that it is likely to be problematic in
 251 // many C++ compilers.
 252 //
 253 #define CAST_TO_FN_PTR(func_type, value) ((func_type)(castable_address(value)))
 254 #define CAST_FROM_FN_PTR(new_type, func_ptr) ((new_type)((address_word)(func_ptr)))
 255 
 256 // Unsigned byte types for os and stream.hpp
 257 
 258 // Unsigned one, two, four and eigth byte quantities used for describing
 259 // the .class file format. See JVM book chapter 4.
 260 
 261 typedef jubyte  u1;
 262 typedef jushort u2;
 263 typedef juint   u4;
 264 typedef julong  u8;
 265 
 266 const jubyte  max_jubyte  = (jubyte)-1;  // 0xFF       largest jubyte
 267 const jushort max_jushort = (jushort)-1; // 0xFFFF     largest jushort
 268 const juint   max_juint   = (juint)-1;   // 0xFFFFFFFF largest juint
 269 const julong  max_julong  = (julong)-1;  // 0xFF....FF largest julong
 270 
 271 //----------------------------------------------------------------------------------------------------
 272 // JVM spec restrictions
 273 
 274 const int max_method_code_size = 64*K - 1;  // JVM spec, 2nd ed. section 4.8.1 (p.134)
 275 
 276 
 277 //----------------------------------------------------------------------------------------------------
 278 // HotSwap - for JVMTI   aka Class File Replacement and PopFrame
 279 //
 280 // Determines whether on-the-fly class replacement and frame popping are enabled.
 281 
 282 #define HOTSWAP
 283 
 284 //----------------------------------------------------------------------------------------------------
 285 // Object alignment, in units of HeapWords.
 286 //
 287 // Minimum is max(BytesPerLong, BytesPerDouble, BytesPerOop) / HeapWordSize, so jlong, jdouble and
 288 // reference fields can be naturally aligned.
 289 
 290 const int MinObjAlignment            = HeapWordsPerLong;
 291 const int MinObjAlignmentInBytes     = MinObjAlignment * HeapWordSize;
 292 const int MinObjAlignmentInBytesMask = MinObjAlignmentInBytes - 1;
 293 
 294 const int LogMinObjAlignment         = LogHeapWordsPerLong;
 295 const int LogMinObjAlignmentInBytes  = LogMinObjAlignment + LogHeapWordSize;
 296 
 297 // Machine dependent stuff
 298 
 299 #include "incls/_globalDefinitions_pd.hpp.incl"
 300 
 301 // The byte alignment to be used by Arena::Amalloc.  See bugid 4169348.
 302 // Note: this value must be a power of 2
 303 
 304 #define ARENA_AMALLOC_ALIGNMENT (2*BytesPerWord)
 305 
 306 // Signed variants of alignment helpers.  There are two versions of each, a macro
 307 // for use in places like enum definitions that require compile-time constant
 308 // expressions and a function for all other places so as to get type checking.
 309 
 310 #define align_size_up_(size, alignment) (((size) + ((alignment) - 1)) & ~((alignment) - 1))
 311 
 312 inline intptr_t align_size_up(intptr_t size, intptr_t alignment) {
 313   return align_size_up_(size, alignment);
 314 }
 315 
 316 #define align_size_down_(size, alignment) ((size) & ~((alignment) - 1))
 317 
 318 inline intptr_t align_size_down(intptr_t size, intptr_t alignment) {
 319   return align_size_down_(size, alignment);
 320 }
 321 
 322 // Align objects by rounding up their size, in HeapWord units.
 323 
 324 #define align_object_size_(size) align_size_up_(size, MinObjAlignment)
 325 
 326 inline intptr_t align_object_size(intptr_t size) {
 327   return align_size_up(size, MinObjAlignment);
 328 }
 329 
 330 // Pad out certain offsets to jlong alignment, in HeapWord units.
 331 
 332 #define align_object_offset_(offset) align_size_up_(offset, HeapWordsPerLong)
 333 
 334 inline intptr_t align_object_offset(intptr_t offset) {
 335   return align_size_up(offset, HeapWordsPerLong);
 336 }
 337 
 338 inline bool is_object_aligned(intptr_t offset) {
 339   return offset == align_object_offset(offset);
 340 }
 341 
 342 
 343 //----------------------------------------------------------------------------------------------------
 344 // Utility macros for compilers
 345 // used to silence compiler warnings
 346 
 347 #define Unused_Variable(var) var
 348 
 349 
 350 //----------------------------------------------------------------------------------------------------
 351 // Miscellaneous
 352 
 353 // 6302670 Eliminate Hotspot __fabsf dependency
 354 // All fabs() callers should call this function instead, which will implicitly
 355 // convert the operand to double, avoiding a dependency on __fabsf which
 356 // doesn't exist in early versions of Solaris 8.
 357 inline double fabsd(double value) {
 358   return fabs(value);
 359 }
 360 
 361 inline jint low (jlong value)                    { return jint(value); }
 362 inline jint high(jlong value)                    { return jint(value >> 32); }
 363 
 364 // the fancy casts are a hopefully portable way
 365 // to do unsigned 32 to 64 bit type conversion
 366 inline void set_low (jlong* value, jint low )    { *value &= (jlong)0xffffffff << 32;
 367                                                    *value |= (jlong)(julong)(juint)low; }
 368 
 369 inline void set_high(jlong* value, jint high)    { *value &= (jlong)(julong)(juint)0xffffffff;
 370                                                    *value |= (jlong)high       << 32; }
 371 
 372 inline jlong jlong_from(jint h, jint l) {
 373   jlong result = 0; // initialization to avoid warning
 374   set_high(&result, h);
 375   set_low(&result,  l);
 376   return result;
 377 }
 378 
 379 union jlong_accessor {
 380   jint  words[2];
 381   jlong long_value;
 382 };
 383 
 384 void basic_types_init(); // cannot define here; uses assert
 385 
 386 
 387 // NOTE: replicated in SA in vm/agent/sun/jvm/hotspot/runtime/BasicType.java
 388 enum BasicType {
 389   T_BOOLEAN  =  4,
 390   T_CHAR     =  5,
 391   T_FLOAT    =  6,
 392   T_DOUBLE   =  7,
 393   T_BYTE     =  8,
 394   T_SHORT    =  9,
 395   T_INT      = 10,
 396   T_LONG     = 11,
 397   T_OBJECT   = 12,
 398   T_ARRAY    = 13,
 399   T_VOID     = 14,
 400   T_ADDRESS  = 15,
 401   T_NARROWOOP= 16,
 402   T_CONFLICT = 17, // for stack value type with conflicting contents
 403   T_ILLEGAL  = 99
 404 };
 405 
 406 inline bool is_java_primitive(BasicType t) {
 407   return T_BOOLEAN <= t && t <= T_LONG;
 408 }
 409 
 410 // Convert a char from a classfile signature to a BasicType
 411 inline BasicType char2type(char c) {
 412   switch( c ) {
 413   case 'B': return T_BYTE;
 414   case 'C': return T_CHAR;
 415   case 'D': return T_DOUBLE;
 416   case 'F': return T_FLOAT;
 417   case 'I': return T_INT;
 418   case 'J': return T_LONG;
 419   case 'S': return T_SHORT;
 420   case 'Z': return T_BOOLEAN;
 421   case 'V': return T_VOID;
 422   case 'L': return T_OBJECT;
 423   case '[': return T_ARRAY;
 424   }
 425   return T_ILLEGAL;
 426 }
 427 
 428 extern char type2char_tab[T_CONFLICT+1];     // Map a BasicType to a jchar
 429 inline char type2char(BasicType t) { return (uint)t < T_CONFLICT+1 ? type2char_tab[t] : 0; }
 430 extern int type2size[T_CONFLICT+1];         // Map BasicType to result stack elements
 431 extern const char* type2name_tab[T_CONFLICT+1];     // Map a BasicType to a jchar
 432 inline const char* type2name(BasicType t) { return (uint)t < T_CONFLICT+1 ? type2name_tab[t] : NULL; }
 433 extern BasicType name2type(const char* name);
 434 
 435 // Auxilary math routines
 436 // least common multiple
 437 extern size_t lcm(size_t a, size_t b);
 438 
 439 
 440 // NOTE: replicated in SA in vm/agent/sun/jvm/hotspot/runtime/BasicType.java
 441 enum BasicTypeSize {
 442   T_BOOLEAN_size = 1,
 443   T_CHAR_size    = 1,
 444   T_FLOAT_size   = 1,
 445   T_DOUBLE_size  = 2,
 446   T_BYTE_size    = 1,
 447   T_SHORT_size   = 1,
 448   T_INT_size     = 1,
 449   T_LONG_size    = 2,
 450   T_OBJECT_size  = 1,
 451   T_ARRAY_size   = 1,
 452   T_NARROWOOP_size = 1,
 453   T_VOID_size    = 0
 454 };
 455 
 456 
 457 // maps a BasicType to its instance field storage type:
 458 // all sub-word integral types are widened to T_INT
 459 extern BasicType type2field[T_CONFLICT+1];
 460 extern BasicType type2wfield[T_CONFLICT+1];
 461 
 462 
 463 // size in bytes
 464 enum ArrayElementSize {
 465   T_BOOLEAN_aelem_bytes = 1,
 466   T_CHAR_aelem_bytes    = 2,
 467   T_FLOAT_aelem_bytes   = 4,
 468   T_DOUBLE_aelem_bytes  = 8,
 469   T_BYTE_aelem_bytes    = 1,
 470   T_SHORT_aelem_bytes   = 2,
 471   T_INT_aelem_bytes     = 4,
 472   T_LONG_aelem_bytes    = 8,
 473 #ifdef _LP64
 474   T_OBJECT_aelem_bytes  = 8,
 475   T_ARRAY_aelem_bytes   = 8,
 476 #else
 477   T_OBJECT_aelem_bytes  = 4,
 478   T_ARRAY_aelem_bytes   = 4,
 479 #endif
 480   T_NARROWOOP_aelem_bytes = 4,
 481   T_VOID_aelem_bytes    = 0
 482 };
 483 
 484 extern int _type2aelembytes[T_CONFLICT+1]; // maps a BasicType to nof bytes used by its array element
 485 #ifdef ASSERT
 486 extern int type2aelembytes(BasicType t, bool allow_address = false); // asserts
 487 #else
 488 inline int type2aelembytes(BasicType t) { return _type2aelembytes[t]; }
 489 #endif
 490 
 491 
 492 // JavaValue serves as a container for arbitrary Java values.
 493 
 494 class JavaValue {
 495 
 496  public:
 497   typedef union JavaCallValue {
 498     jfloat   f;
 499     jdouble  d;
 500     jint     i;
 501     jlong    l;
 502     jobject  h;
 503   } JavaCallValue;
 504 
 505  private:
 506   BasicType _type;
 507   JavaCallValue _value;
 508 
 509  public:
 510   JavaValue(BasicType t = T_ILLEGAL) { _type = t; }
 511 
 512   JavaValue(jfloat value) {
 513     _type    = T_FLOAT;
 514     _value.f = value;
 515   }
 516 
 517   JavaValue(jdouble value) {
 518     _type    = T_DOUBLE;
 519     _value.d = value;
 520   }
 521 
 522  jfloat get_jfloat() const { return _value.f; }
 523  jdouble get_jdouble() const { return _value.d; }
 524  jint get_jint() const { return _value.i; }
 525  jlong get_jlong() const { return _value.l; }
 526  jobject get_jobject() const { return _value.h; }
 527  JavaCallValue* get_value_addr() { return &_value; }
 528  BasicType get_type() const { return _type; }
 529 
 530  void set_jfloat(jfloat f) { _value.f = f;}
 531  void set_jdouble(jdouble d) { _value.d = d;}
 532  void set_jint(jint i) { _value.i = i;}
 533  void set_jlong(jlong l) { _value.l = l;}
 534  void set_jobject(jobject h) { _value.h = h;}
 535  void set_type(BasicType t) { _type = t; }
 536 
 537  jboolean get_jboolean() const { return (jboolean) (_value.i);}
 538  jbyte get_jbyte() const { return (jbyte) (_value.i);}
 539  jchar get_jchar() const { return (jchar) (_value.i);}
 540  jshort get_jshort() const { return (jshort) (_value.i);}
 541 
 542 };
 543 
 544 
 545 #define STACK_BIAS      0
 546 // V9 Sparc CPU's running in 64 Bit mode use a stack bias of 7ff
 547 // in order to extend the reach of the stack pointer.
 548 #if defined(SPARC) && defined(_LP64)
 549 #undef STACK_BIAS
 550 #define STACK_BIAS      0x7ff
 551 #endif
 552 
 553 
 554 // TosState describes the top-of-stack state before and after the execution of
 555 // a bytecode or method. The top-of-stack value may be cached in one or more CPU
 556 // registers. The TosState corresponds to the 'machine represention' of this cached
 557 // value. There's 4 states corresponding to the JAVA types int, long, float & double
 558 // as well as a 5th state in case the top-of-stack value is actually on the top
 559 // of stack (in memory) and thus not cached. The atos state corresponds to the itos
 560 // state when it comes to machine representation but is used separately for (oop)
 561 // type specific operations (e.g. verification code).
 562 
 563 enum TosState {         // describes the tos cache contents
 564   btos = 0,             // byte, bool tos cached
 565   ctos = 1,             // short, char tos cached
 566   stos = 2,             // short, char tos cached
 567   itos = 3,             // int tos cached
 568   ltos = 4,             // long tos cached
 569   ftos = 5,             // float tos cached
 570   dtos = 6,             // double tos cached
 571   atos = 7,             // object cached
 572   vtos = 8,             // tos not cached
 573   number_of_states,
 574   ilgl                  // illegal state: should not occur
 575 };
 576 
 577 
 578 inline TosState as_TosState(BasicType type) {
 579   switch (type) {
 580     case T_BYTE   : return btos;
 581     case T_BOOLEAN: return btos;
 582     case T_CHAR   : return ctos;
 583     case T_SHORT  : return stos;
 584     case T_INT    : return itos;
 585     case T_LONG   : return ltos;
 586     case T_FLOAT  : return ftos;
 587     case T_DOUBLE : return dtos;
 588     case T_VOID   : return vtos;
 589     case T_ARRAY  : // fall through
 590     case T_OBJECT : return atos;
 591   }
 592   return ilgl;
 593 }
 594 
 595 
 596 // Helper function to convert BasicType info into TosState
 597 // Note: Cannot define here as it uses global constant at the time being.
 598 TosState as_TosState(BasicType type);
 599 
 600 
 601 // ReferenceType is used to distinguish between java/lang/ref/Reference subclasses
 602 
 603 enum ReferenceType {
 604  REF_NONE,      // Regular class
 605  REF_OTHER,     // Subclass of java/lang/ref/Reference, but not subclass of one of the classes below
 606  REF_SOFT,      // Subclass of java/lang/ref/SoftReference
 607  REF_WEAK,      // Subclass of java/lang/ref/WeakReference
 608  REF_FINAL,     // Subclass of java/lang/ref/FinalReference
 609  REF_PHANTOM    // Subclass of java/lang/ref/PhantomReference
 610 };
 611 
 612 
 613 // JavaThreadState keeps track of which part of the code a thread is executing in. This
 614 // information is needed by the safepoint code.
 615 //
 616 // There are 4 essential states:
 617 //
 618 //  _thread_new         : Just started, but not executed init. code yet (most likely still in OS init code)
 619 //  _thread_in_native   : In native code. This is a safepoint region, since all oops will be in jobject handles
 620 //  _thread_in_vm       : Executing in the vm
 621 //  _thread_in_Java     : Executing either interpreted or compiled Java code (or could be in a stub)
 622 //
 623 // Each state has an associated xxxx_trans state, which is an intermediate state used when a thread is in
 624 // a transition from one state to another. These extra states makes it possible for the safepoint code to
 625 // handle certain thread_states without having to suspend the thread - making the safepoint code faster.
 626 //
 627 // Given a state, the xxx_trans state can always be found by adding 1.
 628 //
 629 enum JavaThreadState {
 630   _thread_uninitialized     =  0, // should never happen (missing initialization)
 631   _thread_new               =  2, // just starting up, i.e., in process of being initialized
 632   _thread_new_trans         =  3, // corresponding transition state (not used, included for completness)
 633   _thread_in_native         =  4, // running in native code
 634   _thread_in_native_trans   =  5, // corresponding transition state
 635   _thread_in_vm             =  6, // running in VM
 636   _thread_in_vm_trans       =  7, // corresponding transition state
 637   _thread_in_Java           =  8, // running in Java or in stub code
 638   _thread_in_Java_trans     =  9, // corresponding transition state (not used, included for completness)
 639   _thread_blocked           = 10, // blocked in vm
 640   _thread_blocked_trans     = 11, // corresponding transition state
 641   _thread_max_state         = 12  // maximum thread state+1 - used for statistics allocation
 642 };
 643 
 644 
 645 // Handy constants for deciding which compiler mode to use.
 646 enum MethodCompilation {
 647   InvocationEntryBci = -1,     // i.e., not a on-stack replacement compilation
 648   InvalidOSREntryBci = -2
 649 };
 650 
 651 // Enumeration to distinguish tiers of compilation
 652 enum CompLevel {
 653   CompLevel_none              = 0,
 654   CompLevel_fast_compile      = 1,
 655   CompLevel_full_optimization = 2,
 656 
 657   CompLevel_highest_tier      = CompLevel_full_optimization,
 658 #ifdef TIERED
 659   CompLevel_initial_compile   = CompLevel_fast_compile
 660 #else
 661   CompLevel_initial_compile   = CompLevel_full_optimization
 662 #endif // TIERED
 663 };
 664 
 665 inline bool is_tier1_compile(int comp_level) {
 666   return comp_level == CompLevel_fast_compile;
 667 }
 668 inline bool is_tier2_compile(int comp_level) {
 669   return comp_level == CompLevel_full_optimization;
 670 }
 671 inline bool is_highest_tier_compile(int comp_level) {
 672   return comp_level == CompLevel_highest_tier;
 673 }
 674 
 675 //----------------------------------------------------------------------------------------------------
 676 // 'Forward' declarations of frequently used classes
 677 // (in order to reduce interface dependencies & reduce
 678 // number of unnecessary compilations after changes)
 679 
 680 class symbolTable;
 681 class ClassFileStream;
 682 
 683 class Event;
 684 
 685 class Thread;
 686 class  VMThread;
 687 class  JavaThread;
 688 class Threads;
 689 
 690 class VM_Operation;
 691 class VMOperationQueue;
 692 
 693 class CodeBlob;
 694 class  nmethod;
 695 class  OSRAdapter;
 696 class  I2CAdapter;
 697 class  C2IAdapter;
 698 class CompiledIC;
 699 class relocInfo;
 700 class ScopeDesc;
 701 class PcDesc;
 702 
 703 class Recompiler;
 704 class Recompilee;
 705 class RecompilationPolicy;
 706 class RFrame;
 707 class  CompiledRFrame;
 708 class  InterpretedRFrame;
 709 
 710 class frame;
 711 
 712 class vframe;
 713 class   javaVFrame;
 714 class     interpretedVFrame;
 715 class     compiledVFrame;
 716 class     deoptimizedVFrame;
 717 class   externalVFrame;
 718 class     entryVFrame;
 719 
 720 class RegisterMap;
 721 
 722 class Mutex;
 723 class Monitor;
 724 class BasicLock;
 725 class BasicObjectLock;
 726 
 727 class PeriodicTask;
 728 
 729 class JavaCallWrapper;
 730 
 731 class   oopDesc;
 732 
 733 class NativeCall;
 734 
 735 class zone;
 736 
 737 class StubQueue;
 738 
 739 class outputStream;
 740 
 741 class ResourceArea;
 742 
 743 class DebugInformationRecorder;
 744 class ScopeValue;
 745 class CompressedStream;
 746 class   DebugInfoReadStream;
 747 class   DebugInfoWriteStream;
 748 class LocationValue;
 749 class ConstantValue;
 750 class IllegalValue;
 751 
 752 class PrivilegedElement;
 753 class MonitorArray;
 754 
 755 class MonitorInfo;
 756 
 757 class OffsetClosure;
 758 class OopMapCache;
 759 class InterpreterOopMap;
 760 class OopMapCacheEntry;
 761 class OSThread;
 762 
 763 typedef int (*OSThreadStartFunc)(void*);
 764 
 765 class Space;
 766 
 767 class JavaValue;
 768 class methodHandle;
 769 class JavaCallArguments;
 770 
 771 // Basic support for errors (general debug facilities not defined at this point fo the include phase)
 772 
 773 extern void basic_fatal(const char* msg);
 774 
 775 
 776 //----------------------------------------------------------------------------------------------------
 777 // Special constants for debugging
 778 
 779 const jint     badInt           = -3;                       // generic "bad int" value
 780 const long     badAddressVal    = -2;                       // generic "bad address" value
 781 const long     badOopVal        = -1;                       // generic "bad oop" value
 782 const intptr_t badHeapOopVal    = (intptr_t) CONST64(0x2BAD4B0BBAADBABE); // value used to zap heap after GC
 783 const int      badHandleValue   = 0xBC;                     // value used to zap vm handle area
 784 const int      badResourceValue = 0xAB;                     // value used to zap resource area
 785 const int      freeBlockPad     = 0xBA;                     // value used to pad freed blocks.
 786 const int      uninitBlockPad   = 0xF1;                     // value used to zap newly malloc'd blocks.
 787 const intptr_t badJNIHandleVal  = (intptr_t) CONST64(0xFEFEFEFEFEFEFEFE); // value used to zap jni handle area
 788 const juint    badHeapWordVal   = 0xBAADBABE;               // value used to zap heap after GC
 789 const int      badCodeHeapNewVal= 0xCC;                     // value used to zap Code heap at allocation
 790 const int      badCodeHeapFreeVal = 0xDD;                   // value used to zap Code heap at deallocation
 791 
 792 
 793 // (These must be implemented as #defines because C++ compilers are
 794 // not obligated to inline non-integral constants!)
 795 #define       badAddress        ((address)::badAddressVal)
 796 #define       badOop            ((oop)::badOopVal)
 797 #define       badHeapWord       (::badHeapWordVal)
 798 #define       badJNIHandle      ((oop)::badJNIHandleVal)
 799 
 800 
 801 //----------------------------------------------------------------------------------------------------
 802 // Utility functions for bitfield manipulations
 803 
 804 const intptr_t AllBits    = ~0; // all bits set in a word
 805 const intptr_t NoBits     =  0; // no bits set in a word
 806 const jlong    NoLongBits =  0; // no bits set in a long
 807 const intptr_t OneBit     =  1; // only right_most bit set in a word
 808 
 809 // get a word with the n.th or the right-most or left-most n bits set
 810 // (note: #define used only so that they can be used in enum constant definitions)
 811 #define nth_bit(n)        (n >= BitsPerWord ? 0 : OneBit << (n))
 812 #define right_n_bits(n)   (nth_bit(n) - 1)
 813 #define left_n_bits(n)    (right_n_bits(n) << (n >= BitsPerWord ? 0 : (BitsPerWord - n)))
 814 
 815 // bit-operations using a mask m
 816 inline void   set_bits    (intptr_t& x, intptr_t m) { x |= m; }
 817 inline void clear_bits    (intptr_t& x, intptr_t m) { x &= ~m; }
 818 inline intptr_t mask_bits      (intptr_t  x, intptr_t m) { return x & m; }
 819 inline jlong    mask_long_bits (jlong     x, jlong    m) { return x & m; }
 820 inline bool mask_bits_are_true (intptr_t flags, intptr_t mask) { return (flags & mask) == mask; }
 821 
 822 // bit-operations using the n.th bit
 823 inline void    set_nth_bit(intptr_t& x, int n) { set_bits  (x, nth_bit(n)); }
 824 inline void  clear_nth_bit(intptr_t& x, int n) { clear_bits(x, nth_bit(n)); }
 825 inline bool is_set_nth_bit(intptr_t  x, int n) { return mask_bits (x, nth_bit(n)) != NoBits; }
 826 
 827 // returns the bitfield of x starting at start_bit_no with length field_length (no sign-extension!)
 828 inline intptr_t bitfield(intptr_t x, int start_bit_no, int field_length) {
 829   return mask_bits(x >> start_bit_no, right_n_bits(field_length));
 830 }
 831 
 832 
 833 //----------------------------------------------------------------------------------------------------
 834 // Utility functions for integers
 835 
 836 // Avoid use of global min/max macros which may cause unwanted double
 837 // evaluation of arguments.
 838 #ifdef max
 839 #undef max
 840 #endif
 841 
 842 #ifdef min
 843 #undef min
 844 #endif
 845 
 846 #define max(a,b) Do_not_use_max_use_MAX2_instead
 847 #define min(a,b) Do_not_use_min_use_MIN2_instead
 848 
 849 // It is necessary to use templates here. Having normal overloaded
 850 // functions does not work because it is necessary to provide both 32-
 851 // and 64-bit overloaded functions, which does not work, and having
 852 // explicitly-typed versions of these routines (i.e., MAX2I, MAX2L)
 853 // will be even more error-prone than macros.
 854 template<class T> inline T MAX2(T a, T b)           { return (a > b) ? a : b; }
 855 template<class T> inline T MIN2(T a, T b)           { return (a < b) ? a : b; }
 856 template<class T> inline T MAX3(T a, T b, T c)      { return MAX2(MAX2(a, b), c); }
 857 template<class T> inline T MIN3(T a, T b, T c)      { return MIN2(MIN2(a, b), c); }
 858 template<class T> inline T MAX4(T a, T b, T c, T d) { return MAX2(MAX3(a, b, c), d); }
 859 template<class T> inline T MIN4(T a, T b, T c, T d) { return MIN2(MIN3(a, b, c), d); }
 860 
 861 template<class T> inline T ABS(T x)                 { return (x > 0) ? x : -x; }
 862 
 863 // true if x is a power of 2, false otherwise
 864 inline bool is_power_of_2(intptr_t x) {
 865   return ((x != NoBits) && (mask_bits(x, x - 1) == NoBits));
 866 }
 867 
 868 // long version of is_power_of_2
 869 inline bool is_power_of_2_long(jlong x) {
 870   return ((x != NoLongBits) && (mask_long_bits(x, x - 1) == NoLongBits));
 871 }
 872 
 873 //* largest i such that 2^i <= x
 874 //  A negative value of 'x' will return '31'
 875 inline int log2_intptr(intptr_t x) {
 876   int i = -1;
 877   uintptr_t p =  1;
 878   while (p != 0 && p <= (uintptr_t)x) {
 879     // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x)
 880     i++; p *= 2;
 881   }
 882   // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1))
 883   // (if p = 0 then overflow occured and i = 31)
 884   return i;
 885 }
 886 
 887 //* largest i such that 2^i <= x
 888 //  A negative value of 'x' will return '63'
 889 inline int log2_long(jlong x) {
 890   int i = -1;
 891   julong p =  1;
 892   while (p != 0 && p <= (julong)x) {
 893     // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x)
 894     i++; p *= 2;
 895   }
 896   // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1))
 897   // (if p = 0 then overflow occured and i = 63)
 898   return i;
 899 }
 900 
 901 //* the argument must be exactly a power of 2
 902 inline int exact_log2(intptr_t x) {
 903   #ifdef ASSERT
 904     if (!is_power_of_2(x)) basic_fatal("x must be a power of 2");
 905   #endif
 906   return log2_intptr(x);
 907 }
 908 
 909 
 910 // returns integer round-up to the nearest multiple of s (s must be a power of two)
 911 inline intptr_t round_to(intptr_t x, uintx s) {
 912   #ifdef ASSERT
 913     if (!is_power_of_2(s)) basic_fatal("s must be a power of 2");
 914   #endif
 915   const uintx m = s - 1;
 916   return mask_bits(x + m, ~m);
 917 }
 918 
 919 // returns integer round-down to the nearest multiple of s (s must be a power of two)
 920 inline intptr_t round_down(intptr_t x, uintx s) {
 921   #ifdef ASSERT
 922     if (!is_power_of_2(s)) basic_fatal("s must be a power of 2");
 923   #endif
 924   const uintx m = s - 1;
 925   return mask_bits(x, ~m);
 926 }
 927 
 928 
 929 inline bool is_odd (intx x) { return x & 1;      }
 930 inline bool is_even(intx x) { return !is_odd(x); }
 931 
 932 // "to" should be greater than "from."
 933 inline intx byte_size(void* from, void* to) {
 934   return (address)to - (address)from;
 935 }
 936 
 937 //----------------------------------------------------------------------------------------------------
 938 // Avoid non-portable casts with these routines (DEPRECATED)
 939 
 940 // NOTE: USE Bytes class INSTEAD WHERE POSSIBLE
 941 //       Bytes is optimized machine-specifically and may be much faster then the portable routines below.
 942 
 943 // Given sequence of four bytes, build into a 32-bit word
 944 // following the conventions used in class files.
 945 // On the 386, this could be realized with a simple address cast.
 946 //
 947 
 948 // This routine takes eight bytes:
 949 inline u8 build_u8_from( u1 c1, u1 c2, u1 c3, u1 c4, u1 c5, u1 c6, u1 c7, u1 c8 ) {
 950   return  ( u8(c1) << 56 )  &  ( u8(0xff) << 56 )
 951        |  ( u8(c2) << 48 )  &  ( u8(0xff) << 48 )
 952        |  ( u8(c3) << 40 )  &  ( u8(0xff) << 40 )
 953        |  ( u8(c4) << 32 )  &  ( u8(0xff) << 32 )
 954        |  ( u8(c5) << 24 )  &  ( u8(0xff) << 24 )
 955        |  ( u8(c6) << 16 )  &  ( u8(0xff) << 16 )
 956        |  ( u8(c7) <<  8 )  &  ( u8(0xff) <<  8 )
 957        |  ( u8(c8) <<  0 )  &  ( u8(0xff) <<  0 );
 958 }
 959 
 960 // This routine takes four bytes:
 961 inline u4 build_u4_from( u1 c1, u1 c2, u1 c3, u1 c4 ) {
 962   return  ( u4(c1) << 24 )  &  0xff000000
 963        |  ( u4(c2) << 16 )  &  0x00ff0000
 964        |  ( u4(c3) <<  8 )  &  0x0000ff00
 965        |  ( u4(c4) <<  0 )  &  0x000000ff;
 966 }
 967 
 968 // And this one works if the four bytes are contiguous in memory:
 969 inline u4 build_u4_from( u1* p ) {
 970   return  build_u4_from( p[0], p[1], p[2], p[3] );
 971 }
 972 
 973 // Ditto for two-byte ints:
 974 inline u2 build_u2_from( u1 c1, u1 c2 ) {
 975   return  u2(( u2(c1) <<  8 )  &  0xff00
 976           |  ( u2(c2) <<  0 )  &  0x00ff);
 977 }
 978 
 979 // And this one works if the two bytes are contiguous in memory:
 980 inline u2 build_u2_from( u1* p ) {
 981   return  build_u2_from( p[0], p[1] );
 982 }
 983 
 984 // Ditto for floats:
 985 inline jfloat build_float_from( u1 c1, u1 c2, u1 c3, u1 c4 ) {
 986   u4 u = build_u4_from( c1, c2, c3, c4 );
 987   return  *(jfloat*)&u;
 988 }
 989 
 990 inline jfloat build_float_from( u1* p ) {
 991   u4 u = build_u4_from( p );
 992   return  *(jfloat*)&u;
 993 }
 994 
 995 
 996 // now (64-bit) longs
 997 
 998 inline jlong build_long_from( u1 c1, u1 c2, u1 c3, u1 c4, u1 c5, u1 c6, u1 c7, u1 c8 ) {
 999   return  ( jlong(c1) << 56 )  &  ( jlong(0xff) << 56 )
1000        |  ( jlong(c2) << 48 )  &  ( jlong(0xff) << 48 )
1001        |  ( jlong(c3) << 40 )  &  ( jlong(0xff) << 40 )
1002        |  ( jlong(c4) << 32 )  &  ( jlong(0xff) << 32 )
1003        |  ( jlong(c5) << 24 )  &  ( jlong(0xff) << 24 )
1004        |  ( jlong(c6) << 16 )  &  ( jlong(0xff) << 16 )
1005        |  ( jlong(c7) <<  8 )  &  ( jlong(0xff) <<  8 )
1006        |  ( jlong(c8) <<  0 )  &  ( jlong(0xff) <<  0 );
1007 }
1008 
1009 inline jlong build_long_from( u1* p ) {
1010   return  build_long_from( p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7] );
1011 }
1012 
1013 
1014 // Doubles, too!
1015 inline jdouble build_double_from( u1 c1, u1 c2, u1 c3, u1 c4, u1 c5, u1 c6, u1 c7, u1 c8 ) {
1016   jlong u = build_long_from( c1, c2, c3, c4, c5, c6, c7, c8 );
1017   return  *(jdouble*)&u;
1018 }
1019 
1020 inline jdouble build_double_from( u1* p ) {
1021   jlong u = build_long_from( p );
1022   return  *(jdouble*)&u;
1023 }
1024 
1025 
1026 // Portable routines to go the other way:
1027 
1028 inline void explode_short_to( u2 x, u1& c1, u1& c2 ) {
1029   c1 = u1(x >> 8);
1030   c2 = u1(x);
1031 }
1032 
1033 inline void explode_short_to( u2 x, u1* p ) {
1034   explode_short_to( x, p[0], p[1]);
1035 }
1036 
1037 inline void explode_int_to( u4 x, u1& c1, u1& c2, u1& c3, u1& c4 ) {
1038   c1 = u1(x >> 24);
1039   c2 = u1(x >> 16);
1040   c3 = u1(x >>  8);
1041   c4 = u1(x);
1042 }
1043 
1044 inline void explode_int_to( u4 x, u1* p ) {
1045   explode_int_to( x, p[0], p[1], p[2], p[3]);
1046 }
1047 
1048 
1049 // Pack and extract shorts to/from ints:
1050 
1051 inline int extract_low_short_from_int(jint x) {
1052   return x & 0xffff;
1053 }
1054 
1055 inline int extract_high_short_from_int(jint x) {
1056   return (x >> 16) & 0xffff;
1057 }
1058 
1059 inline int build_int_from_shorts( jushort low, jushort high ) {
1060   return ((int)((unsigned int)high << 16) | (unsigned int)low);
1061 }
1062 
1063 // Printf-style formatters for fixed- and variable-width types as pointers and
1064 // integers.
1065 //
1066 // Each compiler-specific definitions file (e.g., globalDefinitions_gcc.hpp)
1067 // must define the macro FORMAT64_MODIFIER, which is the modifier for '%x' or
1068 // '%d' formats to indicate a 64-bit quantity; commonly "l" (in LP64) or "ll"
1069 // (in ILP32).
1070 
1071 // Format 32-bit quantities.
1072 #define INT32_FORMAT  "%d"
1073 #define UINT32_FORMAT "%u"
1074 #define INT32_FORMAT_W(width)   "%" #width "d"
1075 #define UINT32_FORMAT_W(width)  "%" #width "u"
1076 
1077 #define PTR32_FORMAT  "0x%08x"
1078 
1079 // Format 64-bit quantities.
1080 #define INT64_FORMAT  "%" FORMAT64_MODIFIER "d"
1081 #define UINT64_FORMAT "%" FORMAT64_MODIFIER "u"
1082 #define PTR64_FORMAT  "0x%016" FORMAT64_MODIFIER "x"
1083 
1084 #define INT64_FORMAT_W(width)  "%" #width FORMAT64_MODIFIER "d"
1085 #define UINT64_FORMAT_W(width) "%" #width FORMAT64_MODIFIER "u"
1086 
1087 // Format macros that allow the field width to be specified.  The width must be
1088 // a string literal (e.g., "8") or a macro that evaluates to one.
1089 #ifdef _LP64
1090 #define SSIZE_FORMAT_W(width)   INT64_FORMAT_W(width)
1091 #define SIZE_FORMAT_W(width)    UINT64_FORMAT_W(width)
1092 #else
1093 #define SSIZE_FORMAT_W(width)   INT32_FORMAT_W(width)
1094 #define SIZE_FORMAT_W(width)    UINT32_FORMAT_W(width)
1095 #endif // _LP64
1096 
1097 // Format pointers and size_t (or size_t-like integer types) which change size
1098 // between 32- and 64-bit.
1099 #ifdef  _LP64
1100 #define PTR_FORMAT    PTR64_FORMAT
1101 #define UINTX_FORMAT  UINT64_FORMAT
1102 #define INTX_FORMAT   INT64_FORMAT
1103 #define SIZE_FORMAT   UINT64_FORMAT
1104 #define SSIZE_FORMAT  INT64_FORMAT
1105 #else   // !_LP64
1106 #define PTR_FORMAT    PTR32_FORMAT
1107 #define UINTX_FORMAT  UINT32_FORMAT
1108 #define INTX_FORMAT   INT32_FORMAT
1109 #define SIZE_FORMAT   UINT32_FORMAT
1110 #define SSIZE_FORMAT  INT32_FORMAT
1111 #endif  // _LP64
1112 
1113 #define INTPTR_FORMAT PTR_FORMAT
1114 
1115 // Enable zap-a-lot if in debug version.
1116 
1117 # ifdef ASSERT
1118 # ifdef COMPILER2
1119 #   define ENABLE_ZAP_DEAD_LOCALS
1120 #endif /* COMPILER2 */
1121 # endif /* ASSERT */
1122 
1123 #define ARRAY_SIZE(array) (sizeof(array)/sizeof((array)[0]))