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