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