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