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 // Prototyping 475 // "Code Missing Here" macro, un-define when integrating back from prototyping stage and break 476 // compilation on purpose (i.e. "forget me not") 477 #define PROTOTYPE 478 #ifdef PROTOTYPE 479 #define CMH(m) 480 #endif 481 482 //---------------------------------------------------------------------------------------------------- 483 // Miscellaneous 484 485 // 6302670 Eliminate Hotspot __fabsf dependency 486 // All fabs() callers should call this function instead, which will implicitly 487 // convert the operand to double, avoiding a dependency on __fabsf which 488 // doesn't exist in early versions of Solaris 8. 489 inline double fabsd(double value) { 490 return fabs(value); 491 } 492 493 // Returns numerator/denominator as percentage value from 0 to 100. If denominator 494 // is zero, return 0.0. 495 template<typename T> 496 inline double percent_of(T numerator, T denominator) { 497 return denominator != 0 ? (double)numerator / denominator * 100.0 : 0.0; 498 } 499 500 //---------------------------------------------------------------------------------------------------- 501 // Special casts 502 // Cast floats into same-size integers and vice-versa w/o changing bit-pattern 503 typedef union { 504 jfloat f; 505 jint i; 506 } FloatIntConv; 507 508 typedef union { 509 jdouble d; 510 jlong l; 511 julong ul; 512 } DoubleLongConv; 513 514 inline jint jint_cast (jfloat x) { return ((FloatIntConv*)&x)->i; } 515 inline jfloat jfloat_cast (jint x) { return ((FloatIntConv*)&x)->f; } 516 517 inline jlong jlong_cast (jdouble x) { return ((DoubleLongConv*)&x)->l; } 518 inline julong julong_cast (jdouble x) { return ((DoubleLongConv*)&x)->ul; } 519 inline jdouble jdouble_cast (jlong x) { return ((DoubleLongConv*)&x)->d; } 520 521 inline jint low (jlong value) { return jint(value); } 522 inline jint high(jlong value) { return jint(value >> 32); } 523 524 // the fancy casts are a hopefully portable way 525 // to do unsigned 32 to 64 bit type conversion 526 inline void set_low (jlong* value, jint low ) { *value &= (jlong)0xffffffff << 32; 527 *value |= (jlong)(julong)(juint)low; } 528 529 inline void set_high(jlong* value, jint high) { *value &= (jlong)(julong)(juint)0xffffffff; 530 *value |= (jlong)high << 32; } 531 532 inline jlong jlong_from(jint h, jint l) { 533 jlong result = 0; // initialization to avoid warning 534 set_high(&result, h); 535 set_low(&result, l); 536 return result; 537 } 538 539 union jlong_accessor { 540 jint words[2]; 541 jlong long_value; 542 }; 543 544 void basic_types_init(); // cannot define here; uses assert 545 546 547 // NOTE: replicated in SA in vm/agent/sun/jvm/hotspot/runtime/BasicType.java 548 enum BasicType { 549 T_BOOLEAN = 4, 550 T_CHAR = 5, 551 T_FLOAT = 6, 552 T_DOUBLE = 7, 553 T_BYTE = 8, 554 T_SHORT = 9, 555 T_INT = 10, 556 T_LONG = 11, 557 T_OBJECT = 12, 558 T_ARRAY = 13, 559 T_VALUETYPE = 14, 560 T_VOID = 15, 561 T_ADDRESS = 16, 562 T_NARROWOOP = 17, 563 T_METADATA = 18, 564 T_NARROWKLASS = 19, 565 T_CONFLICT = 20, // for stack value type with conflicting contents 566 T_ILLEGAL = 99 567 }; 568 569 inline bool is_java_primitive(BasicType t) { 570 return T_BOOLEAN <= t && t <= T_LONG; 571 } 572 573 inline bool is_subword_type(BasicType t) { 574 // these guys are processed exactly like T_INT in calling sequences: 575 return (t == T_BOOLEAN || t == T_CHAR || t == T_BYTE || t == T_SHORT); 576 } 577 578 inline bool is_signed_subword_type(BasicType t) { 579 return (t == T_BYTE || t == T_SHORT); 580 } 581 582 inline bool is_reference_type(BasicType t) { 583 return (t == T_OBJECT || t == T_ARRAY || t == T_VALUETYPE); 584 } 585 586 // Convert a char from a classfile signature to a BasicType 587 inline BasicType char2type(char c) { 588 switch( c ) { 589 case 'B': return T_BYTE; 590 case 'C': return T_CHAR; 591 case 'D': return T_DOUBLE; 592 case 'F': return T_FLOAT; 593 case 'I': return T_INT; 594 case 'J': return T_LONG; 595 case 'S': return T_SHORT; 596 case 'Z': return T_BOOLEAN; 597 case 'V': return T_VOID; 598 case 'L': return T_OBJECT; 599 case '[': return T_ARRAY; 600 case 'Q': return T_VALUETYPE; 601 } 602 return T_ILLEGAL; 603 } 604 605 extern char type2char_tab[T_CONFLICT+1]; // Map a BasicType to a jchar 606 inline char type2char(BasicType t) { return (uint)t < T_CONFLICT+1 ? type2char_tab[t] : 0; } 607 extern int type2size[T_CONFLICT+1]; // Map BasicType to result stack elements 608 extern const char* type2name_tab[T_CONFLICT+1]; // Map a BasicType to a jchar 609 inline const char* type2name(BasicType t) { return (uint)t < T_CONFLICT+1 ? type2name_tab[t] : NULL; } 610 extern BasicType name2type(const char* name); 611 612 // Auxiliary math routines 613 // least common multiple 614 extern size_t lcm(size_t a, size_t b); 615 616 617 // NOTE: replicated in SA in vm/agent/sun/jvm/hotspot/runtime/BasicType.java 618 enum BasicTypeSize { 619 T_BOOLEAN_size = 1, 620 T_CHAR_size = 1, 621 T_FLOAT_size = 1, 622 T_DOUBLE_size = 2, 623 T_BYTE_size = 1, 624 T_SHORT_size = 1, 625 T_INT_size = 1, 626 T_LONG_size = 2, 627 T_OBJECT_size = 1, 628 T_ARRAY_size = 1, 629 T_NARROWOOP_size = 1, 630 T_NARROWKLASS_size = 1, 631 T_VOID_size = 0, 632 T_VALUETYPE_size = 1 633 }; 634 635 636 // maps a BasicType to its instance field storage type: 637 // all sub-word integral types are widened to T_INT 638 extern BasicType type2field[T_CONFLICT+1]; 639 extern BasicType type2wfield[T_CONFLICT+1]; 640 641 642 // size in bytes 643 enum ArrayElementSize { 644 T_BOOLEAN_aelem_bytes = 1, 645 T_CHAR_aelem_bytes = 2, 646 T_FLOAT_aelem_bytes = 4, 647 T_DOUBLE_aelem_bytes = 8, 648 T_BYTE_aelem_bytes = 1, 649 T_SHORT_aelem_bytes = 2, 650 T_INT_aelem_bytes = 4, 651 T_LONG_aelem_bytes = 8, 652 #ifdef _LP64 653 T_OBJECT_aelem_bytes = 8, 654 T_ARRAY_aelem_bytes = 8, 655 T_VALUETYPE_aelem_bytes = 8, 656 #else 657 T_OBJECT_aelem_bytes = 4, 658 T_ARRAY_aelem_bytes = 4, 659 T_VALUETYPE_aelem_bytes = 4, 660 #endif 661 T_NARROWOOP_aelem_bytes = 4, 662 T_NARROWKLASS_aelem_bytes = 4, 663 T_VOID_aelem_bytes = 0 664 }; 665 666 extern int _type2aelembytes[T_CONFLICT+1]; // maps a BasicType to nof bytes used by its array element 667 #ifdef ASSERT 668 extern int type2aelembytes(BasicType t, bool allow_address = false); // asserts 669 #else 670 inline int type2aelembytes(BasicType t, bool allow_address = false) { return _type2aelembytes[t]; } 671 #endif 672 673 674 // JavaValue serves as a container for arbitrary Java values. 675 676 class JavaValue { 677 678 public: 679 typedef union JavaCallValue { 680 jfloat f; 681 jdouble d; 682 jint i; 683 jlong l; 684 jobject h; 685 } JavaCallValue; 686 687 private: 688 BasicType _type; 689 JavaCallValue _value; 690 691 public: 692 JavaValue(BasicType t = T_ILLEGAL) { _type = t; } 693 694 JavaValue(jfloat value) { 695 _type = T_FLOAT; 696 _value.f = value; 697 } 698 699 JavaValue(jdouble value) { 700 _type = T_DOUBLE; 701 _value.d = value; 702 } 703 704 jfloat get_jfloat() const { return _value.f; } 705 jdouble get_jdouble() const { return _value.d; } 706 jint get_jint() const { return _value.i; } 707 jlong get_jlong() const { return _value.l; } 708 jobject get_jobject() const { return _value.h; } 709 JavaCallValue* get_value_addr() { return &_value; } 710 BasicType get_type() const { return _type; } 711 712 void set_jfloat(jfloat f) { _value.f = f;} 713 void set_jdouble(jdouble d) { _value.d = d;} 714 void set_jint(jint i) { _value.i = i;} 715 void set_jlong(jlong l) { _value.l = l;} 716 void set_jobject(jobject h) { _value.h = h;} 717 void set_type(BasicType t) { _type = t; } 718 719 jboolean get_jboolean() const { return (jboolean) (_value.i);} 720 jbyte get_jbyte() const { return (jbyte) (_value.i);} 721 jchar get_jchar() const { return (jchar) (_value.i);} 722 jshort get_jshort() const { return (jshort) (_value.i);} 723 724 }; 725 726 727 #define STACK_BIAS 0 728 // V9 Sparc CPU's running in 64 Bit mode use a stack bias of 7ff 729 // in order to extend the reach of the stack pointer. 730 #if defined(SPARC) && defined(_LP64) 731 #undef STACK_BIAS 732 #define STACK_BIAS 0x7ff 733 #endif 734 735 736 // TosState describes the top-of-stack state before and after the execution of 737 // a bytecode or method. The top-of-stack value may be cached in one or more CPU 738 // registers. The TosState corresponds to the 'machine representation' of this cached 739 // value. There's 4 states corresponding to the JAVA types int, long, float & double 740 // as well as a 5th state in case the top-of-stack value is actually on the top 741 // of stack (in memory) and thus not cached. The atos state corresponds to the itos 742 // state when it comes to machine representation but is used separately for (oop) 743 // type specific operations (e.g. verification code). 744 745 enum TosState { // describes the tos cache contents 746 btos = 0, // byte, bool tos cached 747 ztos = 1, // byte, bool tos cached 748 ctos = 2, // char tos cached 749 stos = 3, // short tos cached 750 itos = 4, // int tos cached 751 ltos = 5, // long tos cached 752 ftos = 6, // float tos cached 753 dtos = 7, // double tos cached 754 atos = 8, // object cached 755 vtos = 9, // tos not cached, 756 number_of_states, 757 ilgl // illegal state: should not occur 758 }; 759 760 761 inline TosState as_TosState(BasicType type) { 762 switch (type) { 763 case T_BYTE : return btos; 764 case T_BOOLEAN: return ztos; 765 case T_CHAR : return ctos; 766 case T_SHORT : return stos; 767 case T_INT : return itos; 768 case T_LONG : return ltos; 769 case T_FLOAT : return ftos; 770 case T_DOUBLE : return dtos; 771 case T_VOID : return vtos; 772 case T_VALUETYPE: // fall through 773 case T_ARRAY : // fall through 774 case T_OBJECT : return atos; 775 default : return ilgl; 776 } 777 } 778 779 inline BasicType as_BasicType(TosState state) { 780 switch (state) { 781 case btos : return T_BYTE; 782 case ztos : return T_BOOLEAN; 783 case ctos : return T_CHAR; 784 case stos : return T_SHORT; 785 case itos : return T_INT; 786 case ltos : return T_LONG; 787 case ftos : return T_FLOAT; 788 case dtos : return T_DOUBLE; 789 case atos : return T_OBJECT; 790 case vtos : return T_VOID; 791 default : return T_ILLEGAL; 792 } 793 } 794 795 796 // Helper function to convert BasicType info into TosState 797 // Note: Cannot define here as it uses global constant at the time being. 798 TosState as_TosState(BasicType type); 799 800 801 // JavaThreadState keeps track of which part of the code a thread is executing in. This 802 // information is needed by the safepoint code. 803 // 804 // There are 4 essential states: 805 // 806 // _thread_new : Just started, but not executed init. code yet (most likely still in OS init code) 807 // _thread_in_native : In native code. This is a safepoint region, since all oops will be in jobject handles 808 // _thread_in_vm : Executing in the vm 809 // _thread_in_Java : Executing either interpreted or compiled Java code (or could be in a stub) 810 // 811 // Each state has an associated xxxx_trans state, which is an intermediate state used when a thread is in 812 // a transition from one state to another. These extra states makes it possible for the safepoint code to 813 // handle certain thread_states without having to suspend the thread - making the safepoint code faster. 814 // 815 // Given a state, the xxxx_trans state can always be found by adding 1. 816 // 817 enum JavaThreadState { 818 _thread_uninitialized = 0, // should never happen (missing initialization) 819 _thread_new = 2, // just starting up, i.e., in process of being initialized 820 _thread_new_trans = 3, // corresponding transition state (not used, included for completness) 821 _thread_in_native = 4, // running in native code 822 _thread_in_native_trans = 5, // corresponding transition state 823 _thread_in_vm = 6, // running in VM 824 _thread_in_vm_trans = 7, // corresponding transition state 825 _thread_in_Java = 8, // running in Java or in stub code 826 _thread_in_Java_trans = 9, // corresponding transition state (not used, included for completness) 827 _thread_blocked = 10, // blocked in vm 828 _thread_blocked_trans = 11, // corresponding transition state 829 _thread_max_state = 12 // maximum thread state+1 - used for statistics allocation 830 }; 831 832 //---------------------------------------------------------------------------------------------------- 833 // Special constants for debugging 834 835 const jint badInt = -3; // generic "bad int" value 836 const intptr_t badAddressVal = -2; // generic "bad address" value 837 const intptr_t badOopVal = -1; // generic "bad oop" value 838 const intptr_t badHeapOopVal = (intptr_t) CONST64(0x2BAD4B0BBAADBABE); // value used to zap heap after GC 839 const int badStackSegVal = 0xCA; // value used to zap stack segments 840 const int badHandleValue = 0xBC; // value used to zap vm handle area 841 const int badResourceValue = 0xAB; // value used to zap resource area 842 const int freeBlockPad = 0xBA; // value used to pad freed blocks. 843 const int uninitBlockPad = 0xF1; // value used to zap newly malloc'd blocks. 844 const juint uninitMetaWordVal= 0xf7f7f7f7; // value used to zap newly allocated metachunk 845 const juint badHeapWordVal = 0xBAADBABE; // value used to zap heap after GC 846 const juint badMetaWordVal = 0xBAADFADE; // value used to zap metadata heap after GC 847 const int badCodeHeapNewVal= 0xCC; // value used to zap Code heap at allocation 848 const int badCodeHeapFreeVal = 0xDD; // value used to zap Code heap at deallocation 849 850 851 // (These must be implemented as #defines because C++ compilers are 852 // not obligated to inline non-integral constants!) 853 #define badAddress ((address)::badAddressVal) 854 #define badOop (cast_to_oop(::badOopVal)) 855 #define badHeapWord (::badHeapWordVal) 856 857 // Default TaskQueue size is 16K (32-bit) or 128K (64-bit) 858 #define TASKQUEUE_SIZE (NOT_LP64(1<<14) LP64_ONLY(1<<17)) 859 860 //---------------------------------------------------------------------------------------------------- 861 // Utility functions for bitfield manipulations 862 863 const intptr_t AllBits = ~0; // all bits set in a word 864 const intptr_t NoBits = 0; // no bits set in a word 865 const jlong NoLongBits = 0; // no bits set in a long 866 const intptr_t OneBit = 1; // only right_most bit set in a word 867 868 // get a word with the n.th or the right-most or left-most n bits set 869 // (note: #define used only so that they can be used in enum constant definitions) 870 #define nth_bit(n) (((n) >= BitsPerWord) ? 0 : (OneBit << (n))) 871 #define right_n_bits(n) (nth_bit(n) - 1) 872 #define left_n_bits(n) (right_n_bits(n) << (((n) >= BitsPerWord) ? 0 : (BitsPerWord - (n)))) 873 874 // bit-operations using a mask m 875 inline void set_bits (intptr_t& x, intptr_t m) { x |= m; } 876 inline void clear_bits (intptr_t& x, intptr_t m) { x &= ~m; } 877 inline intptr_t mask_bits (intptr_t x, intptr_t m) { return x & m; } 878 inline jlong mask_long_bits (jlong x, jlong m) { return x & m; } 879 inline bool mask_bits_are_true (intptr_t flags, intptr_t mask) { return (flags & mask) == mask; } 880 881 // bit-operations using the n.th bit 882 inline void set_nth_bit(intptr_t& x, int n) { set_bits (x, nth_bit(n)); } 883 inline void clear_nth_bit(intptr_t& x, int n) { clear_bits(x, nth_bit(n)); } 884 inline bool is_set_nth_bit(intptr_t x, int n) { return mask_bits (x, nth_bit(n)) != NoBits; } 885 886 // returns the bitfield of x starting at start_bit_no with length field_length (no sign-extension!) 887 inline intptr_t bitfield(intptr_t x, int start_bit_no, int field_length) { 888 return mask_bits(x >> start_bit_no, right_n_bits(field_length)); 889 } 890 891 892 //---------------------------------------------------------------------------------------------------- 893 // Utility functions for integers 894 895 // Avoid use of global min/max macros which may cause unwanted double 896 // evaluation of arguments. 897 #ifdef max 898 #undef max 899 #endif 900 901 #ifdef min 902 #undef min 903 #endif 904 905 // It is necessary to use templates here. Having normal overloaded 906 // functions does not work because it is necessary to provide both 32- 907 // and 64-bit overloaded functions, which does not work, and having 908 // explicitly-typed versions of these routines (i.e., MAX2I, MAX2L) 909 // will be even more error-prone than macros. 910 template<class T> inline T MAX2(T a, T b) { return (a > b) ? a : b; } 911 template<class T> inline T MIN2(T a, T b) { return (a < b) ? a : b; } 912 template<class T> inline T MAX3(T a, T b, T c) { return MAX2(MAX2(a, b), c); } 913 template<class T> inline T MIN3(T a, T b, T c) { return MIN2(MIN2(a, b), c); } 914 template<class T> inline T MAX4(T a, T b, T c, T d) { return MAX2(MAX3(a, b, c), d); } 915 template<class T> inline T MIN4(T a, T b, T c, T d) { return MIN2(MIN3(a, b, c), d); } 916 917 template<class T> inline T ABS(T x) { return (x > 0) ? x : -x; } 918 919 // true if x is a power of 2, false otherwise 920 inline bool is_power_of_2(intptr_t x) { 921 return ((x != NoBits) && (mask_bits(x, x - 1) == NoBits)); 922 } 923 924 // long version of is_power_of_2 925 inline bool is_power_of_2_long(jlong x) { 926 return ((x != NoLongBits) && (mask_long_bits(x, x - 1) == NoLongBits)); 927 } 928 929 // Returns largest i such that 2^i <= x. 930 // If x == 0, the function returns -1. 931 inline int log2_intptr(uintptr_t x) { 932 int i = -1; 933 uintptr_t p = 1; 934 while (p != 0 && p <= x) { 935 // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) 936 i++; p *= 2; 937 } 938 // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1)) 939 // If p = 0, overflow has occurred and i = 31 or i = 63 (depending on the machine word size). 940 return i; 941 } 942 943 //* largest i such that 2^i <= x 944 inline int log2_long(julong x) { 945 int i = -1; 946 julong p = 1; 947 while (p != 0 && p <= x) { 948 // p = 2^(i+1) && p <= x (i.e., 2^(i+1) <= x) 949 i++; p *= 2; 950 } 951 // p = 2^(i+1) && x < p (i.e., 2^i <= x < 2^(i+1)) 952 // (if p = 0 then overflow occurred and i = 63) 953 return i; 954 } 955 956 // If x < 0, the function returns 31 on a 32-bit machine and 63 on a 64-bit machine. 957 inline int log2_intptr(intptr_t x) { 958 return log2_intptr((uintptr_t)x); 959 } 960 961 inline int log2_int(int x) { 962 STATIC_ASSERT(sizeof(int) <= sizeof(uintptr_t)); 963 return log2_intptr((uintptr_t)x); 964 } 965 966 inline int log2_jint(jint x) { 967 STATIC_ASSERT(sizeof(jint) <= sizeof(uintptr_t)); 968 return log2_intptr((uintptr_t)x); 969 } 970 971 inline int log2_uint(uint x) { 972 STATIC_ASSERT(sizeof(uint) <= sizeof(uintptr_t)); 973 return log2_intptr((uintptr_t)x); 974 } 975 976 // A negative value of 'x' will return '63' 977 inline int log2_jlong(jlong x) { 978 STATIC_ASSERT(sizeof(jlong) <= sizeof(julong)); 979 return log2_long((julong)x); 980 } 981 982 //* the argument must be exactly a power of 2 983 inline int exact_log2(intptr_t x) { 984 assert(is_power_of_2(x), "x must be a power of 2: " INTPTR_FORMAT, x); 985 return log2_intptr(x); 986 } 987 988 // the argument doesn't need to be a power of two 989 inline int upper_log2(intptr_t x) { 990 int shift = log2_intptr(x); 991 intptr_t y = 1ULL << shift; 992 if (y < x) { 993 shift++; 994 } 995 return shift; 996 } 997 998 //* the argument must be exactly a power of 2 999 inline int exact_log2_long(jlong x) { 1000 assert(is_power_of_2_long(x), "x must be a power of 2: " JLONG_FORMAT, x); 1001 return log2_long(x); 1002 } 1003 1004 inline bool is_odd (intx x) { return x & 1; } 1005 inline bool is_even(intx x) { return !is_odd(x); } 1006 1007 // abs methods which cannot overflow and so are well-defined across 1008 // the entire domain of integer types. 1009 static inline unsigned int uabs(unsigned int n) { 1010 union { 1011 unsigned int result; 1012 int value; 1013 }; 1014 result = n; 1015 if (value < 0) result = 0-result; 1016 return result; 1017 } 1018 static inline julong uabs(julong n) { 1019 union { 1020 julong result; 1021 jlong value; 1022 }; 1023 result = n; 1024 if (value < 0) result = 0-result; 1025 return result; 1026 } 1027 static inline julong uabs(jlong n) { return uabs((julong)n); } 1028 static inline unsigned int uabs(int n) { return uabs((unsigned int)n); } 1029 1030 // "to" should be greater than "from." 1031 inline intx byte_size(void* from, void* to) { 1032 return (address)to - (address)from; 1033 } 1034 1035 1036 // Pack and extract shorts to/from ints: 1037 1038 inline int extract_low_short_from_int(jint x) { 1039 return x & 0xffff; 1040 } 1041 1042 inline int extract_high_short_from_int(jint x) { 1043 return (x >> 16) & 0xffff; 1044 } 1045 1046 inline int build_int_from_shorts( jushort low, jushort high ) { 1047 return ((int)((unsigned int)high << 16) | (unsigned int)low); 1048 } 1049 1050 // Convert pointer to intptr_t, for use in printing pointers. 1051 inline intptr_t p2i(const void * p) { 1052 return (intptr_t) p; 1053 } 1054 1055 // swap a & b 1056 template<class T> static void swap(T& a, T& b) { 1057 T tmp = a; 1058 a = b; 1059 b = tmp; 1060 } 1061 1062 #define ARRAY_SIZE(array) (sizeof(array)/sizeof((array)[0])) 1063 1064 //---------------------------------------------------------------------------------------------------- 1065 // Sum and product which can never overflow: they wrap, just like the 1066 // Java operations. Note that we don't intend these to be used for 1067 // general-purpose arithmetic: their purpose is to emulate Java 1068 // operations. 1069 1070 // The goal of this code to avoid undefined or implementation-defined 1071 // behavior. The use of an lvalue to reference cast is explicitly 1072 // permitted by Lvalues and rvalues [basic.lval]. [Section 3.10 Para 1073 // 15 in C++03] 1074 #define JAVA_INTEGER_OP(OP, NAME, TYPE, UNSIGNED_TYPE) \ 1075 inline TYPE NAME (TYPE in1, TYPE in2) { \ 1076 UNSIGNED_TYPE ures = static_cast<UNSIGNED_TYPE>(in1); \ 1077 ures OP ## = static_cast<UNSIGNED_TYPE>(in2); \ 1078 return reinterpret_cast<TYPE&>(ures); \ 1079 } 1080 1081 JAVA_INTEGER_OP(+, java_add, jint, juint) 1082 JAVA_INTEGER_OP(-, java_subtract, jint, juint) 1083 JAVA_INTEGER_OP(*, java_multiply, jint, juint) 1084 JAVA_INTEGER_OP(+, java_add, jlong, julong) 1085 JAVA_INTEGER_OP(-, java_subtract, jlong, julong) 1086 JAVA_INTEGER_OP(*, java_multiply, jlong, julong) 1087 1088 #undef JAVA_INTEGER_OP 1089 1090 // Dereference vptr 1091 // All C++ compilers that we know of have the vtbl pointer in the first 1092 // word. If there are exceptions, this function needs to be made compiler 1093 // specific. 1094 static inline void* dereference_vptr(const void* addr) { 1095 return *(void**)addr; 1096 } 1097 1098 //---------------------------------------------------------------------------------------------------- 1099 // String type aliases used by command line flag declarations and 1100 // processing utilities. 1101 1102 typedef const char* ccstr; 1103 typedef const char* ccstrlist; // represents string arguments which accumulate 1104 1105 //---------------------------------------------------------------------------------------------------- 1106 // Default hash/equals functions used by ResourceHashtable and KVHashtable 1107 1108 template<typename K> unsigned primitive_hash(const K& k) { 1109 unsigned hash = (unsigned)((uintptr_t)k); 1110 return hash ^ (hash >> 3); // just in case we're dealing with aligned ptrs 1111 } 1112 1113 template<typename K> bool primitive_equals(const K& k0, const K& k1) { 1114 return k0 == k1; 1115 } 1116 1117 1118 #endif // SHARE_UTILITIES_GLOBALDEFINITIONS_HPP