1 /* 2 * Copyright (c) 1997, 2025, 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 #include "runtime/globals.hpp" 26 #include "runtime/os.hpp" 27 #include "runtime/signature.hpp" 28 #include "utilities/globalDefinitions.hpp" 29 #include "utilities/powerOfTwo.hpp" 30 31 // Basic error support 32 33 // Info for oops within a java object. Defaults are zero so 34 // things will break badly if incorrectly initialized. 35 int heapOopSize = 0; 36 int LogBytesPerHeapOop = 0; 37 int LogBitsPerHeapOop = 0; 38 int BytesPerHeapOop = 0; 39 int BitsPerHeapOop = 0; 40 41 // Old CDS options 42 bool RequireSharedSpaces; 43 extern "C" { 44 JNIEXPORT jboolean UseSharedSpaces = true; 45 } 46 47 // Object alignment, in units of HeapWords. 48 // Defaults are -1 so things will break badly if incorrectly initialized. 49 int MinObjAlignment = -1; 50 int MinObjAlignmentInBytes = -1; 51 int MinObjAlignmentInBytesMask = 0; 52 53 int LogMinObjAlignment = -1; 54 int LogMinObjAlignmentInBytes = -1; 55 56 // Oop encoding heap max 57 uint64_t OopEncodingHeapMax = 0; 58 59 // Something to help porters sleep at night 60 61 #ifdef ASSERT 62 static BasicType char2type(int ch) { 63 switch (ch) { 64 #define EACH_SIG(ch, bt, ignore) \ 65 case ch: return bt; 66 SIGNATURE_TYPES_DO(EACH_SIG, ignore) 67 #undef EACH_SIG 68 } 69 return T_ILLEGAL; 70 } 71 72 extern bool signature_constants_sane(); 73 #endif //ASSERT 74 75 void basic_types_init() { 76 #ifdef ASSERT 77 #ifdef _LP64 78 static_assert(min_intx == (intx)CONST64(0x8000000000000000), "correct constant"); 79 static_assert(max_intx == CONST64(0x7FFFFFFFFFFFFFFF), "correct constant"); 80 static_assert(max_uintx == CONST64(0xFFFFFFFFFFFFFFFF), "correct constant"); 81 static_assert( 8 == sizeof( intx), "wrong size for basic type"); 82 static_assert( 8 == sizeof( jobject), "wrong size for basic type"); 83 #else 84 static_assert(min_intx == (intx)0x80000000, "correct constant"); 85 static_assert(max_intx == 0x7FFFFFFF, "correct constant"); 86 static_assert(max_uintx == 0xFFFFFFFF, "correct constant"); 87 static_assert( 4 == sizeof( intx), "wrong size for basic type"); 88 static_assert( 4 == sizeof( jobject), "wrong size for basic type"); 89 #endif 90 static_assert( (~max_juint) == 0, "max_juint has all its bits"); 91 static_assert( (~max_uintx) == 0, "max_uintx has all its bits"); 92 static_assert( (~max_julong) == 0, "max_julong has all its bits"); 93 static_assert( 1 == sizeof( jbyte), "wrong size for basic type"); 94 static_assert( 2 == sizeof( jchar), "wrong size for basic type"); 95 static_assert( 2 == sizeof( jshort), "wrong size for basic type"); 96 static_assert( 4 == sizeof( juint), "wrong size for basic type"); 97 static_assert( 4 == sizeof( jint), "wrong size for basic type"); 98 static_assert( 1 == sizeof( jboolean), "wrong size for basic type"); 99 static_assert( 8 == sizeof( jlong), "wrong size for basic type"); 100 static_assert( 4 == sizeof( jfloat), "wrong size for basic type"); 101 static_assert( 8 == sizeof( jdouble), "wrong size for basic type"); 102 static_assert( 1 == sizeof( u1), "wrong size for basic type"); 103 static_assert( 2 == sizeof( u2), "wrong size for basic type"); 104 static_assert( 4 == sizeof( u4), "wrong size for basic type"); 105 static_assert(wordSize == BytesPerWord, "should be the same since they're used interchangeably"); 106 static_assert(wordSize == HeapWordSize, "should be the same since they're also used interchangeably"); 107 108 assert(signature_constants_sane(), ""); 109 110 int num_type_chars = 0; 111 for (int i = 0; i < 99; i++) { 112 if (type2char((BasicType)i) != 0) { 113 assert(char2type(type2char((BasicType)i)) == i, "proper inverses"); 114 assert(Signature::basic_type(type2char((BasicType)i)) == i, "proper inverses"); 115 num_type_chars++; 116 } 117 } 118 assert(num_type_chars == 11, "must have tested the right number of mappings"); 119 assert(char2type(0) == T_ILLEGAL, "correct illegality"); 120 121 { 122 for (int i = T_BOOLEAN; i <= T_CONFLICT; i++) { 123 BasicType vt = (BasicType)i; 124 BasicType ft = type2field[vt]; 125 switch (vt) { 126 // the following types might plausibly show up in memory layouts: 127 case T_BOOLEAN: 128 case T_BYTE: 129 case T_CHAR: 130 case T_SHORT: 131 case T_INT: 132 case T_FLOAT: 133 case T_DOUBLE: 134 case T_LONG: 135 case T_OBJECT: 136 case T_ADDRESS: // random raw pointer 137 case T_METADATA: // metadata pointer 138 case T_NARROWOOP: // compressed pointer 139 case T_NARROWKLASS: // compressed klass pointer 140 case T_CONFLICT: // might as well support a bottom type 141 case T_VOID: // padding or other unaddressed word 142 // layout type must map to itself 143 assert(vt == ft, ""); 144 break; 145 default: 146 // non-layout type must map to a (different) layout type 147 assert(vt != ft, ""); 148 assert(ft == type2field[ft], ""); 149 } 150 // every type must map to same-sized layout type: 151 assert(type2size[vt] == type2size[ft], ""); 152 } 153 } 154 // These are assumed, e.g., when filling HeapWords with juints. 155 static_assert(is_power_of_2(sizeof(juint)), "juint must be power of 2"); 156 static_assert(is_power_of_2(HeapWordSize), "HeapWordSize must be power of 2"); 157 static_assert((size_t)HeapWordSize >= sizeof(juint), 158 "HeapWord should be at least as large as juint"); 159 #endif 160 161 if( JavaPriority1_To_OSPriority != -1 ) 162 os::java_to_os_priority[1] = JavaPriority1_To_OSPriority; 163 if( JavaPriority2_To_OSPriority != -1 ) 164 os::java_to_os_priority[2] = JavaPriority2_To_OSPriority; 165 if( JavaPriority3_To_OSPriority != -1 ) 166 os::java_to_os_priority[3] = JavaPriority3_To_OSPriority; 167 if( JavaPriority4_To_OSPriority != -1 ) 168 os::java_to_os_priority[4] = JavaPriority4_To_OSPriority; 169 if( JavaPriority5_To_OSPriority != -1 ) 170 os::java_to_os_priority[5] = JavaPriority5_To_OSPriority; 171 if( JavaPriority6_To_OSPriority != -1 ) 172 os::java_to_os_priority[6] = JavaPriority6_To_OSPriority; 173 if( JavaPriority7_To_OSPriority != -1 ) 174 os::java_to_os_priority[7] = JavaPriority7_To_OSPriority; 175 if( JavaPriority8_To_OSPriority != -1 ) 176 os::java_to_os_priority[8] = JavaPriority8_To_OSPriority; 177 if( JavaPriority9_To_OSPriority != -1 ) 178 os::java_to_os_priority[9] = JavaPriority9_To_OSPriority; 179 if(JavaPriority10_To_OSPriority != -1 ) 180 os::java_to_os_priority[10] = JavaPriority10_To_OSPriority; 181 182 // Set the size of basic types here (after argument parsing but before 183 // stub generation). 184 if (UseCompressedOops) { 185 // Size info for oops within java objects is fixed 186 heapOopSize = jintSize; 187 LogBytesPerHeapOop = LogBytesPerInt; 188 LogBitsPerHeapOop = LogBitsPerInt; 189 BytesPerHeapOop = BytesPerInt; 190 BitsPerHeapOop = BitsPerInt; 191 } else { 192 heapOopSize = oopSize; 193 LogBytesPerHeapOop = LogBytesPerWord; 194 LogBitsPerHeapOop = LogBitsPerWord; 195 BytesPerHeapOop = BytesPerWord; 196 BitsPerHeapOop = BitsPerWord; 197 } 198 _type2aelembytes[T_OBJECT] = heapOopSize; 199 _type2aelembytes[T_ARRAY] = heapOopSize; 200 } 201 202 203 // Map BasicType to signature character 204 char type2char_tab[T_CONFLICT+1] = { 205 0, 0, 0, 0, 206 JVM_SIGNATURE_BOOLEAN, JVM_SIGNATURE_CHAR, 207 JVM_SIGNATURE_FLOAT, JVM_SIGNATURE_DOUBLE, 208 JVM_SIGNATURE_BYTE, JVM_SIGNATURE_SHORT, 209 JVM_SIGNATURE_INT, JVM_SIGNATURE_LONG, 210 JVM_SIGNATURE_CLASS, JVM_SIGNATURE_ARRAY, 211 JVM_SIGNATURE_VOID, 0, 212 0, 0, 0, 0 213 }; 214 215 // Map BasicType to Java type name 216 const char* type2name_tab[T_CONFLICT+1] = { 217 nullptr, nullptr, nullptr, nullptr, 218 "boolean", 219 "char", 220 "float", 221 "double", 222 "byte", 223 "short", 224 "int", 225 "long", 226 "object", 227 "array", 228 "void", 229 "*address*", 230 "*narrowoop*", 231 "*metadata*", 232 "*narrowklass*", 233 "*conflict*" 234 }; 235 const char* type2name(BasicType t) { 236 if (t < ARRAY_SIZE(type2name_tab)) { 237 return type2name_tab[t]; 238 } else if (t == T_ILLEGAL) { 239 return "*illegal*"; 240 } else { 241 fatal("invalid type %d", t); 242 return "invalid type"; 243 } 244 } 245 246 247 248 BasicType name2type(const char* name) { 249 for (int i = T_BOOLEAN; i <= T_VOID; i++) { 250 BasicType t = (BasicType)i; 251 if (type2name_tab[t] != nullptr && 0 == strcmp(type2name_tab[t], name)) 252 return t; 253 } 254 return T_ILLEGAL; 255 } 256 257 // Map BasicType to size in words 258 int type2size[T_CONFLICT+1]={ -1, 0, 0, 0, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 0, 1, 1, 1, 1, -1}; 259 260 BasicType type2field[T_CONFLICT+1] = { 261 (BasicType)0, // 0, 262 (BasicType)0, // 1, 263 (BasicType)0, // 2, 264 (BasicType)0, // 3, 265 T_BOOLEAN, // T_BOOLEAN = 4, 266 T_CHAR, // T_CHAR = 5, 267 T_FLOAT, // T_FLOAT = 6, 268 T_DOUBLE, // T_DOUBLE = 7, 269 T_BYTE, // T_BYTE = 8, 270 T_SHORT, // T_SHORT = 9, 271 T_INT, // T_INT = 10, 272 T_LONG, // T_LONG = 11, 273 T_OBJECT, // T_OBJECT = 12, 274 T_OBJECT, // T_ARRAY = 13, 275 T_VOID, // T_VOID = 14, 276 T_ADDRESS, // T_ADDRESS = 15, 277 T_NARROWOOP, // T_NARROWOOP= 16, 278 T_METADATA, // T_METADATA = 17, 279 T_NARROWKLASS, // T_NARROWKLASS = 18, 280 T_CONFLICT // T_CONFLICT = 19, 281 }; 282 283 284 BasicType type2wfield[T_CONFLICT+1] = { 285 (BasicType)0, // 0, 286 (BasicType)0, // 1, 287 (BasicType)0, // 2, 288 (BasicType)0, // 3, 289 T_INT, // T_BOOLEAN = 4, 290 T_INT, // T_CHAR = 5, 291 T_FLOAT, // T_FLOAT = 6, 292 T_DOUBLE, // T_DOUBLE = 7, 293 T_INT, // T_BYTE = 8, 294 T_INT, // T_SHORT = 9, 295 T_INT, // T_INT = 10, 296 T_LONG, // T_LONG = 11, 297 T_OBJECT, // T_OBJECT = 12, 298 T_OBJECT, // T_ARRAY = 13, 299 T_VOID, // T_VOID = 14, 300 T_ADDRESS, // T_ADDRESS = 15, 301 T_NARROWOOP, // T_NARROWOOP = 16, 302 T_METADATA, // T_METADATA = 17, 303 T_NARROWKLASS, // T_NARROWKLASS = 18, 304 T_CONFLICT // T_CONFLICT = 19, 305 }; 306 307 308 int _type2aelembytes[T_CONFLICT+1] = { 309 0, // 0 310 0, // 1 311 0, // 2 312 0, // 3 313 T_BOOLEAN_aelem_bytes, // T_BOOLEAN = 4, 314 T_CHAR_aelem_bytes, // T_CHAR = 5, 315 T_FLOAT_aelem_bytes, // T_FLOAT = 6, 316 T_DOUBLE_aelem_bytes, // T_DOUBLE = 7, 317 T_BYTE_aelem_bytes, // T_BYTE = 8, 318 T_SHORT_aelem_bytes, // T_SHORT = 9, 319 T_INT_aelem_bytes, // T_INT = 10, 320 T_LONG_aelem_bytes, // T_LONG = 11, 321 T_OBJECT_aelem_bytes, // T_OBJECT = 12, 322 T_ARRAY_aelem_bytes, // T_ARRAY = 13, 323 0, // T_VOID = 14, 324 T_OBJECT_aelem_bytes, // T_ADDRESS = 15, 325 T_NARROWOOP_aelem_bytes, // T_NARROWOOP= 16, 326 T_OBJECT_aelem_bytes, // T_METADATA = 17, 327 T_NARROWKLASS_aelem_bytes, // T_NARROWKLASS= 18, 328 0 // T_CONFLICT = 19, 329 }; 330 331 #ifdef ASSERT 332 int type2aelembytes(BasicType t, bool allow_address) { 333 assert((allow_address || t != T_ADDRESS) && t <= T_CONFLICT, "unexpected basic type"); 334 return _type2aelembytes[t]; 335 } 336 #endif 337 338 // Support for 64-bit integer arithmetic 339 340 // The following code is mostly taken from JVM typedefs_md.h and system_md.c 341 342 static const jlong high_bit = (jlong)1 << (jlong)63; 343 static const jlong other_bits = ~high_bit; 344 345 jlong float2long(jfloat f) { 346 jlong tmp = (jlong) f; 347 if (tmp != high_bit) { 348 return tmp; 349 } else { 350 if (g_isnan((jdouble)f)) { 351 return 0; 352 } 353 if (f < 0) { 354 return high_bit; 355 } else { 356 return other_bits; 357 } 358 } 359 } 360 361 362 jlong double2long(jdouble f) { 363 jlong tmp = (jlong) f; 364 if (tmp != high_bit) { 365 return tmp; 366 } else { 367 if (g_isnan(f)) { 368 return 0; 369 } 370 if (f < 0) { 371 return high_bit; 372 } else { 373 return other_bits; 374 } 375 } 376 } 377 378 // least common multiple 379 size_t lcm(size_t a, size_t b) { 380 size_t cur, div, next; 381 382 cur = MAX2(a, b); 383 div = MIN2(a, b); 384 385 assert(div != 0, "lcm requires positive arguments"); 386 387 388 while ((next = cur % div) != 0) { 389 cur = div; div = next; 390 } 391 392 393 julong result = julong(a) * b / div; 394 assert(result <= (size_t)max_uintx, "Integer overflow in lcm"); 395 396 return size_t(result); 397 } 398 399 400 // Test that nth_bit macro and friends behave as 401 // expected, even with low-precedence operators. 402 403 STATIC_ASSERT(nth_bit(3) == 0x8); 404 STATIC_ASSERT(nth_bit(1|2) == 0x8); 405 406 STATIC_ASSERT(right_n_bits(3) == 0x7); 407 STATIC_ASSERT(right_n_bits(1|2) == 0x7); 408 409 // Check for Flush-To-Zero mode 410 411 // On some processors faster execution can be achieved by setting a 412 // mode to return zero for extremely small results, rather than an 413 // IEEE-754 subnormal number. This mode is not compatible with the 414 // Java Language Standard. 415 416 // We need the addition of _large_subnormal and _small_subnormal to be 417 // performed at runtime. _small_subnormal is volatile so that 418 // expressions involving it cannot be evaluated at compile time. 419 static const double large_subnormal_double 420 = jdouble_cast(0x0030000000000000); // 0x1.0p-1020; 421 static const volatile double small_subnormal_double 422 = jdouble_cast(0x0000000000000003); // 0x0.0000000000003p-1022; 423 424 // Quickly test to make sure IEEE-754 subnormal numbers are correctly 425 // handled. 426 bool IEEE_subnormal_handling_OK() { 427 // _small_subnormal is the smallest subnormal number that has two 428 // bits set. _large_subnormal is a number such that, when 429 // _small_subnormal is added to it, must be rounded according to the 430 // mode. These two tests detect the rounding mode in use. If 431 // subnormals are turned off (i.e. subnormals-are-zero) flush-to- 432 // zero mode is in use. 433 434 return (large_subnormal_double + small_subnormal_double > large_subnormal_double 435 && -large_subnormal_double - small_subnormal_double < -large_subnormal_double); 436 }