Skip to content
This repository was archived by the owner on Aug 27, 2022. It is now read-only.
/ lanai Public archive

Commit 1f5a033

Browse files
committedSep 25, 2020
8253555: Make ByteSize and WordSize typed scoped enums
Reviewed-by: kbarrett, tschatzl
1 parent f62eefc commit 1f5a033

File tree

9 files changed

+74
-177
lines changed

9 files changed

+74
-177
lines changed
 

‎src/hotspot/cpu/aarch64/assembler_aarch64.hpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,8 @@ class Address {
401401
: _base(r), _index(noreg), _offset(o), _mode(base_plus_offset), _target(0) { }
402402
Address(Register r, unsigned long long o)
403403
: _base(r), _index(noreg), _offset(o), _mode(base_plus_offset), _target(0) { }
404-
#ifdef ASSERT
405404
Address(Register r, ByteSize disp)
406-
: _base(r), _index(noreg), _offset(in_bytes(disp)), _mode(base_plus_offset), _target(0) { }
407-
#endif
405+
: Address(r, in_bytes(disp)) { }
408406
Address(Register r, Register r1, extend ext = lsl())
409407
: _base(r), _index(r1), _offset(0), _mode(base_plus_offset_reg),
410408
_ext(ext), _target(0) { }

‎src/hotspot/cpu/arm/assembler_arm.hpp

+2-11
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,8 @@ class Address {
8989
_offset_op = add_offset;
9090
}
9191

92-
#ifdef ASSERT
93-
Address(Register rn, ByteSize offset, AsmOffset mode = basic_offset) {
94-
_base = rn;
95-
_index = noreg;
96-
_disp = in_bytes(offset);
97-
_mode = mode;
98-
_shift_imm = 0;
99-
_shift = lsl;
100-
_offset_op = add_offset;
101-
}
102-
#endif
92+
Address(Register rn, ByteSize offset, AsmOffset mode = basic_offset) :
93+
Address(rn, in_bytes(offset), mode) {}
10394

10495
Address(Register rn, Register rm, AsmShift shift = lsl,
10596
int shift_imm = 0, AsmOffset mode = basic_offset,

‎src/hotspot/cpu/arm/assembler_arm_32.hpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,8 @@ class AsmOperand {
5555
encode(imm_8);
5656
}
5757

58-
#ifdef ASSERT
59-
AsmOperand(ByteSize bytesize_8) {
60-
const int imm_8 = in_bytes(bytesize_8);
61-
encode(imm_8);
62-
}
63-
#endif // ASSERT
58+
AsmOperand(ByteSize bytesize_8) :
59+
AsmOperand(in_bytes(bytesize_8)) {}
6460

6561
AsmOperand(Register rm, AsmShift shift, int shift_imm) {
6662
encode(rm,shift,shift_imm);

‎src/hotspot/cpu/ppc/assembler_ppc.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1639,7 +1639,7 @@ class Assembler : public AbstractAssembler {
16391639

16401640
// For convenience. Load pointer into d from b+s1.
16411641
inline void ld_ptr(Register d, int b, Register s1);
1642-
DEBUG_ONLY(inline void ld_ptr(Register d, ByteSize b, Register s1);)
1642+
inline void ld_ptr(Register d, ByteSize b, Register s1);
16431643

16441644
// PPC 1, section 3.3.3 Fixed-Point Store Instructions
16451645
inline void stwx( Register d, Register s1, Register s2);
@@ -1663,7 +1663,7 @@ class Assembler : public AbstractAssembler {
16631663
inline void stdbrx( Register d, Register s1, Register s2);
16641664

16651665
inline void st_ptr(Register d, int si16, Register s1);
1666-
DEBUG_ONLY(inline void st_ptr(Register d, ByteSize b, Register s1);)
1666+
inline void st_ptr(Register d, ByteSize b, Register s1);
16671667

16681668
// PPC 1, section 3.3.13 Move To/From System Register Instructions
16691669
inline void mtlr( Register s1);

‎src/hotspot/cpu/ppc/assembler_ppc.inline.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ inline void Assembler::ldu( Register d, int si16, Register s1) { assert(d !=
342342
inline void Assembler::ldbrx( Register d, Register s1, Register s2) { emit_int32(LDBRX_OPCODE | rt(d) | ra0mem(s1) | rb(s2));}
343343

344344
inline void Assembler::ld_ptr(Register d, int b, Register s1) { ld(d, b, s1); }
345-
DEBUG_ONLY(inline void Assembler::ld_ptr(Register d, ByteSize b, Register s1) { ld(d, in_bytes(b), s1); })
345+
inline void Assembler::ld_ptr(Register d, ByteSize b, Register s1) { ld(d, in_bytes(b), s1); }
346346

347347
// PPC 1, section 3.3.3 Fixed-Point Store Instructions
348348
inline void Assembler::stwx( Register d, Register s1, Register s2) { emit_int32(STWX_OPCODE | rs(d) | ra0mem(s1) | rb(s2));}
@@ -366,7 +366,7 @@ inline void Assembler::stdux(Register s, Register a, Register b) { emit_int32(
366366
inline void Assembler::stdbrx( Register d, Register s1, Register s2) { emit_int32(STDBRX_OPCODE | rs(d) | ra0mem(s1) | rb(s2));}
367367

368368
inline void Assembler::st_ptr(Register d, int b, Register s1) { std(d, b, s1); }
369-
DEBUG_ONLY(inline void Assembler::st_ptr(Register d, ByteSize b, Register s1) { std(d, in_bytes(b), s1); })
369+
inline void Assembler::st_ptr(Register d, ByteSize b, Register s1) { std(d, in_bytes(b), s1); }
370370

371371
// PPC 1, section 3.3.13 Move To/From System Register Instructions
372372
inline void Assembler::mtlr( Register s1) { emit_int32(MTLR_OPCODE | rs(s1)); }

‎src/hotspot/cpu/s390/assembler_s390.hpp

+2-9
Original file line numberDiff line numberDiff line change
@@ -206,18 +206,11 @@ class Address {
206206
if (roc.is_constant()) _disp += roc.as_constant(); else _index = roc.as_register();
207207
}
208208

209-
#ifdef ASSERT
210-
// ByteSize is only a class when ASSERT is defined, otherwise it's an int.
211209
Address(Register base, ByteSize disp) :
212-
_base(base),
213-
_index(noreg),
214-
_disp(in_bytes(disp)) {}
210+
Address(base, in_bytes(disp)) {}
215211

216212
Address(Register base, Register index, ByteSize disp) :
217-
_base(base),
218-
_index(index),
219-
_disp(in_bytes(disp)) {}
220-
#endif
213+
Address(base, index, in_bytes(disp)) {}
221214

222215
// Aborts if disp is a register and base and index are set already.
223216
Address plus_disp(RegisterOrConstant disp) const {

‎src/hotspot/cpu/x86/assembler_x86.hpp

+13-45
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,19 @@ class Address {
256256
"inconsistent address");
257257
}
258258

259+
// The following overloads are used in connection with the
260+
// ByteSize type (see sizes.hpp). They simplify the use of
261+
// ByteSize'd arguments in assembly code.
262+
263+
Address(Register base, ByteSize disp)
264+
: Address(base, in_bytes(disp)) {}
265+
266+
Address(Register base, Register index, ScaleFactor scale, ByteSize disp)
267+
: Address(base, index, scale, in_bytes(disp)) {}
268+
269+
Address(Register base, RegisterOrConstant index, ScaleFactor scale, ByteSize disp)
270+
: Address(base, index, scale, in_bytes(disp)) {}
271+
259272
Address plus_disp(int disp) const {
260273
Address a = (*this);
261274
a._disp += disp;
@@ -276,51 +289,6 @@ class Address {
276289
return _base == a._base && _disp == a._disp && _index == a._index && _scale == a._scale;
277290
}
278291

279-
// The following two overloads are used in connection with the
280-
// ByteSize type (see sizes.hpp). They simplify the use of
281-
// ByteSize'd arguments in assembly code. Note that their equivalent
282-
// for the optimized build are the member functions with int disp
283-
// argument since ByteSize is mapped to an int type in that case.
284-
//
285-
// Note: DO NOT introduce similar overloaded functions for WordSize
286-
// arguments as in the optimized mode, both ByteSize and WordSize
287-
// are mapped to the same type and thus the compiler cannot make a
288-
// distinction anymore (=> compiler errors).
289-
290-
#ifdef ASSERT
291-
Address(Register base, ByteSize disp)
292-
: _base(base),
293-
_index(noreg),
294-
_xmmindex(xnoreg),
295-
_scale(no_scale),
296-
_disp(in_bytes(disp)),
297-
_isxmmindex(false){
298-
}
299-
300-
Address(Register base, Register index, ScaleFactor scale, ByteSize disp)
301-
: _base(base),
302-
_index(index),
303-
_xmmindex(xnoreg),
304-
_scale(scale),
305-
_disp(in_bytes(disp)),
306-
_isxmmindex(false){
307-
assert(!index->is_valid() == (scale == Address::no_scale),
308-
"inconsistent address");
309-
}
310-
Address(Register base, RegisterOrConstant index, ScaleFactor scale, ByteSize disp)
311-
: _base (base),
312-
_index(index.register_or_noreg()),
313-
_xmmindex(xnoreg),
314-
_scale(scale),
315-
_disp (in_bytes(disp) + (index.constant_or_zero() * scale_size(scale))),
316-
_isxmmindex(false) {
317-
if (!index.is_register()) scale = Address::no_scale;
318-
assert(!_index->is_valid() == (scale == Address::no_scale),
319-
"inconsistent address");
320-
}
321-
322-
#endif // ASSERT
323-
324292
// accessors
325293
bool uses(Register reg) const { return _base == reg || _index == reg; }
326294
Register base() const { return _base; }

‎src/hotspot/share/utilities/sizes.hpp

+9-99
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,6 @@
3232
// WordSize is used for sizes measured in machine words (i.e., 32bit or 64bit words
3333
// depending on platform).
3434
//
35-
// The classes are defined with friend functions operating on them instead of member
36-
// functions so that they (the classes) can be re-#define'd to int types in optimized
37-
// mode. This allows full type checking and maximum safety in debug mode, and full
38-
// optimizations (constant folding) and zero overhead (time and space wise) in the
39-
// optimized build (some compilers do not optimize one-element value classes but
40-
// instead create an object in memory - thus the overhead may be significant).
41-
//
42-
// Note: 1) DO NOT add new overloaded friend functions that do not have a unique function
43-
// function name but require signature types for resolution. This will not work
44-
// in optimized mode as both, ByteSize and WordSize are mapped to the same type
45-
// and thus the distinction would not be possible anymore (=> compiler errors).
46-
//
47-
// 2) DO NOT add non-static member functions as they cannot be mapped so something
48-
// compilable in the optimized build. Static member functions could be added
49-
// but require a corresponding class definition in the optimized build.
50-
//
5135
// These classes should help doing a transition from (currently) word-size based offsets
5236
// to byte-size based offsets in the VM (this will be important if we desire to pack
5337
// objects more densely in the VM for 64bit machines). Such a transition should proceed
@@ -56,93 +40,19 @@
5640
// a) first transition the whole VM into a form where all sizes are strongly typed
5741
// b) change all WordSize's to ByteSize's where desired and fix the compilation errors
5842

43+
enum class WordSize : int {};
5944

60-
#ifdef ASSERT
61-
62-
class ByteSize {
63-
private:
64-
int _size;
65-
66-
// Note: This constructor must be private to avoid implicit conversions!
67-
ByteSize(int size) { _size = size; }
68-
69-
public:
70-
// constructors
71-
inline friend ByteSize in_ByteSize(int size);
72-
73-
// accessors
74-
inline friend int in_bytes(ByteSize x);
75-
76-
// operators
77-
friend ByteSize operator + (ByteSize x, ByteSize y) { return ByteSize(in_bytes(x) + in_bytes(y)); }
78-
friend ByteSize operator - (ByteSize x, ByteSize y) { return ByteSize(in_bytes(x) - in_bytes(y)); }
79-
friend ByteSize operator * (ByteSize x, int y) { return ByteSize(in_bytes(x) * y ); }
80-
81-
// comparison
82-
friend bool operator == (ByteSize x, ByteSize y) { return in_bytes(x) == in_bytes(y); }
83-
friend bool operator != (ByteSize x, ByteSize y) { return in_bytes(x) != in_bytes(y); }
84-
friend bool operator < (ByteSize x, ByteSize y) { return in_bytes(x) < in_bytes(y); }
85-
friend bool operator <= (ByteSize x, ByteSize y) { return in_bytes(x) <= in_bytes(y); }
86-
friend bool operator > (ByteSize x, ByteSize y) { return in_bytes(x) > in_bytes(y); }
87-
friend bool operator >= (ByteSize x, ByteSize y) { return in_bytes(x) >= in_bytes(y); }
88-
};
89-
90-
inline ByteSize in_ByteSize(int size) { return ByteSize(size); }
91-
inline int in_bytes(ByteSize x) { return x._size; }
92-
93-
94-
class WordSize {
95-
private:
96-
int _size;
97-
98-
// Note: This constructor must be private to avoid implicit conversions!
99-
WordSize(int size) { _size = size; }
100-
101-
public:
102-
// constructors
103-
inline friend WordSize in_WordSize(int size);
104-
105-
// accessors
106-
inline friend int in_words(WordSize x);
107-
108-
// operators
109-
friend WordSize operator + (WordSize x, WordSize y) { return WordSize(in_words(x) + in_words(y)); }
110-
friend WordSize operator - (WordSize x, WordSize y) { return WordSize(in_words(x) - in_words(y)); }
111-
friend WordSize operator * (WordSize x, int y) { return WordSize(in_words(x) * y ); }
112-
113-
// comparison
114-
friend bool operator == (WordSize x, WordSize y) { return in_words(x) == in_words(y); }
115-
friend bool operator != (WordSize x, WordSize y) { return in_words(x) != in_words(y); }
116-
friend bool operator < (WordSize x, WordSize y) { return in_words(x) < in_words(y); }
117-
friend bool operator <= (WordSize x, WordSize y) { return in_words(x) <= in_words(y); }
118-
friend bool operator > (WordSize x, WordSize y) { return in_words(x) > in_words(y); }
119-
friend bool operator >= (WordSize x, WordSize y) { return in_words(x) >= in_words(y); }
120-
};
121-
122-
inline WordSize in_WordSize(int size) { return WordSize(size); }
123-
inline int in_words(WordSize x) { return x._size; }
124-
125-
126-
#else // ASSERT
127-
128-
// The following definitions must match the corresponding friend declarations
129-
// in the Byte/WordSize classes if they are typedef'ed to be int. This will
130-
// be the case in optimized mode to ensure zero overhead for these types.
131-
//
132-
// Note: If a compiler does not inline these function calls away, one may
133-
// want to use #define's to make sure full optimization (constant
134-
// folding in particular) is possible.
135-
136-
typedef int ByteSize;
137-
inline ByteSize in_ByteSize(int size) { return size; }
138-
inline int in_bytes (ByteSize x) { return x; }
45+
constexpr WordSize in_WordSize(int size) { return static_cast<WordSize>(size); }
46+
constexpr int in_words(WordSize x) { return static_cast<int>(x); }
13947

140-
typedef int WordSize;
141-
inline WordSize in_WordSize(int size) { return size; }
142-
inline int in_words (WordSize x) { return x; }
48+
enum class ByteSize : int {};
14349

144-
#endif // ASSERT
50+
constexpr ByteSize in_ByteSize(int size) { return static_cast<ByteSize>(size); }
51+
constexpr int in_bytes(ByteSize x) { return static_cast<int>(x); }
14552

53+
constexpr ByteSize operator + (ByteSize x, ByteSize y) { return in_ByteSize(in_bytes(x) + in_bytes(y)); }
54+
constexpr ByteSize operator - (ByteSize x, ByteSize y) { return in_ByteSize(in_bytes(x) - in_bytes(y)); }
55+
constexpr ByteSize operator * (ByteSize x, int y) { return in_ByteSize(in_bytes(x) * y ); }
14656

14757
// Use the following #define to get C++ field member offsets
14858

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2020, 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+
#include "precompiled.hpp"
25+
#include "utilities/sizes.hpp"
26+
#include "unittest.hpp"
27+
28+
TEST(ByteSize, constructors) {
29+
EXPECT_EQ(in_bytes(in_ByteSize(10)), 10);
30+
}
31+
32+
TEST(ByteSize, operators) {
33+
ByteSize s = in_ByteSize(7);
34+
ASSERT_EQ(in_bytes(s + in_ByteSize(3)), 10);
35+
ASSERT_EQ(in_bytes(s - in_ByteSize(3)), 4);
36+
ASSERT_EQ(in_bytes(s * 3), 21);
37+
}
38+
39+
TEST(WordSize, constructors) {
40+
EXPECT_EQ(in_words(in_WordSize(10)), 10);
41+
}

0 commit comments

Comments
 (0)
This repository has been archived.