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