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