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