Repository navigation
Expand file tree
/
Copy pathAddress.java
More file actions
389 lines (331 loc) · 10.9 KB
/
Copy pathAddress.java
File metadata and controls
389 lines (331 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
*
* Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product
* that is described in this document. In particular, and without limitation, these intellectual property
* rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or
* more additional patents or pending patent applications in the U.S. and in other countries.
*
* U.S. Government Rights - Commercial software. Government users are subject to the Sun
* Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its
* supplements.
*
* Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or
* registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks
* are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the
* U.S. and other countries.
*
* UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open
* Company, Ltd.
*/
package com.sun.max.unsafe;
import java.math.*;
import com.sun.max.annotate.*;
import com.sun.max.lang.*;
import com.sun.max.program.*;
/**
* A machine word interpreted as a linear address.
* An Address is unsigned and arithmetic is supported.
*
* @author Bernd Mathiske
* @author Paul Caprioli
*/
public abstract class Address extends Word {
protected Address() {
}
@INLINE
public static Address zero() {
return fromInt(0);
}
@INLINE
public static Address max() {
return fromLong(-1L);
}
/**
* Creates an Address value from a given int value. Note that unlike {@link #fromInt(int)},
* the given int value is not sign extended. Also note that on 32-bit platforms, this operation
* is effectively a no-op.
*
* @param value the value to be converted to an Address
*/
@INLINE
public static Address fromUnsignedInt(int value) {
if (Word.width() == 64) {
final long longValue = value;
final long n = longValue & 0xffffffffL;
return UnsafeCast.asAddress(n);
}
return UnsafeCast.asAddress(value);
}
/**
* Creates an Address value from a given int value. Note that unlike {@link #fromUnsignedInt(int)},
* the given int value is sign extended first. Also note that on 32-bit platforms, this operation
* is effectively a no-op.
*
* @param value the value to be converted to an Address
*/
@INLINE
public static Address fromInt(int value) {
if (Word.width() == 64) {
final long n = value;
return UnsafeCast.asAddress(n);
}
return UnsafeCast.asAddress(value);
}
@INLINE
public static Address fromLong(long value) {
if (Word.width() == 64) {
return UnsafeCast.asAddress(value);
}
final int n = (int) value;
return UnsafeCast.asAddress(n);
}
@Override
public String toString() {
return "@" + toHexString();
}
public String toUnsignedString(int radix) {
if (radix == 16) {
if (Word.width() == 64) {
return Long.toHexString(toLong());
}
assert Word.width() == 32;
return Integer.toHexString(toInt());
}
if (radix == 8) {
if (Word.width() == 64) {
return Long.toOctalString(toLong());
}
assert Word.width() == 32;
return Integer.toOctalString(toInt());
}
if (radix == 2) {
if (Word.width() == 64) {
return Long.toBinaryString(toLong());
}
assert Word.width() == 32;
return Integer.toBinaryString(toInt());
}
assert radix == 10;
final long n = toLong();
if (Word.width() == 32) {
if (n <= Integer.MAX_VALUE && n >= 0) {
return Integer.toString(toInt());
}
return Long.toString(n & 0xffffffffL);
}
final long low = n & 0xffffffffL;
final long high = n >>> 32;
return BigInteger.valueOf(high).shiftLeft(32).or(BigInteger.valueOf(low)).toString();
}
public static Address parse(String s, int radix) {
Address result = Address.zero();
for (int i = 0; i < s.length(); i++) {
result = result.times(radix);
result = result.plus(Integer.parseInt(String.valueOf(s.charAt(i)), radix));
}
return result;
}
@INLINE
public final int toInt() {
if (Word.width() == 64) {
final long n = UnsafeCast.asLong(this);
return (int) n;
}
return UnsafeCast.asInt(this);
}
@INLINE
public final long toLong() {
if (Word.width() == 64) {
return UnsafeCast.asLong(this);
}
return 0xffffffffL & UnsafeCast.asInt(this);
}
@INLINE
public final boolean equals(int other) {
return fromInt(other) == this;
}
@INLINE(override = true)
public Address plus(Address addend) {
return asOffset().plus(addend.asOffset()).asAddress();
}
@INLINE(override = true)
public Address plus(Offset offset) {
return asOffset().plus(offset).asAddress();
}
@INLINE(override = true)
public Address plus(int addend) {
return asOffset().plus(addend).asAddress();
}
@INLINE(override = true)
public Address plus(long addend) {
return asOffset().plus(addend).asAddress();
}
@INLINE(override = true)
public Address minus(Address subtrahend) {
return asOffset().minus(subtrahend.asOffset()).asAddress();
}
@INLINE(override = true)
public Address minus(Offset offset) {
return asOffset().minus(offset).asAddress();
}
@INLINE(override = true)
public Address minus(int subtrahend) {
return asOffset().minus(subtrahend).asAddress();
}
@INLINE(override = true)
public Address minus(long subtrahend) {
return asOffset().minus(subtrahend).asAddress();
}
@INLINE(override = true)
public Address times(Address factor) {
return asOffset().times(factor.asOffset()).asAddress();
}
@INLINE(override = true)
public Address times(int factor) {
return asOffset().times(factor).asAddress();
}
protected abstract Address dividedByAddress(Address divisor);
@INLINE(override = true)
public Address dividedBy(Address divisor) {
return dividedByAddress(divisor);
}
protected abstract Address dividedByInt(int divisor);
@INLINE(override = true)
public Address dividedBy(int divisor) {
return dividedByInt(divisor);
}
protected abstract Address remainderByAddress(Address divisor);
@INLINE(override = true)
public Address remainder(Address divisor) {
return remainderByAddress(divisor);
}
protected abstract int remainderByInt(int divisor);
@INLINE(override = true)
public final int remainder(int divisor) {
return remainderByInt(divisor);
}
@INLINE(override = true)
public final boolean isRoundedBy(Address nBytes) {
return remainder(nBytes).isZero();
}
@INLINE(override = true)
public final boolean isRoundedBy(int nBytes) {
return remainder(nBytes) == 0;
}
@INLINE(override = true)
public Address roundedUpBy(Address nBytes) {
if (isRoundedBy(nBytes)) {
return this;
}
return plus(nBytes.minus(remainder(nBytes)));
}
@INLINE(override = true)
public Address roundedUpBy(int nBytes) {
if (isRoundedBy(nBytes)) {
return this;
}
return plus(nBytes - remainder(nBytes));
}
@INLINE(override = true)
public Address roundedDownBy(int nBytes) {
return minus(remainder(nBytes));
}
@INLINE(override = true)
public Address wordAligned() {
final int n = Word.size();
return plus(n - 1).and(Address.fromInt(n - 1).not());
}
@INLINE(override = true)
public Address aligned(int alignment) {
return plus(alignment - 1).and(Address.fromInt(alignment - 1).not());
}
@INLINE(override = true)
public boolean isWordAligned() {
final int n = Word.size();
return and(n - 1).equals(Address.zero());
}
@INLINE(override = true)
public boolean isAligned(int alignment) {
return and(alignment - 1).equals(Address.zero());
}
@INLINE(override = true)
public final boolean isBitSet(int index) {
return (toLong() & (1L << index)) != 0;
}
@INLINE(override = true)
public Address bitSet(int index) {
return fromLong(toLong() | (1L << index));
}
@INLINE(override = true)
public Address bitClear(int index) {
return fromLong(toLong() & ~(1L << index));
}
@INLINE(override = true)
public Address and(Address operand) {
if (Word.width() == 64) {
return fromLong(toLong() & operand.toLong());
}
return fromInt(toInt() & operand.toInt());
}
@INLINE(override = true)
public Address and(int operand) {
return and(fromInt(operand));
}
@INLINE(override = true)
public Address and(long operand) {
return and(fromLong(operand));
}
@INLINE(override = true)
public Address or(Address operand) {
if (Word.width() == 64) {
return fromLong(toLong() | operand.toLong());
}
return fromInt(toInt() | operand.toInt());
}
@INLINE(override = true)
public Address or(int operand) {
return or(fromInt(operand));
}
@INLINE(override = true)
public Address or(long operand) {
return or(fromLong(operand));
}
@INLINE(override = true)
public Address not() {
if (Word.width() == 64) {
return fromLong(~toLong());
}
return fromInt(~toInt());
}
@INLINE(override = true)
public Address shiftedLeft(int nBits) {
if (Word.width() == 64) {
return fromLong(toLong() << nBits);
}
return fromInt(toInt() << nBits);
}
@INLINE(override = true)
public Address unsignedShiftedRight(int nBits) {
if (Word.width() == 64) {
return fromLong(toLong() >>> nBits);
}
return fromInt(toInt() >>> nBits);
}
@INLINE(override = true)
public final int numberOfEffectiveBits() {
if (Word.width() == 64) {
return 64 - Long.numberOfLeadingZeros(toLong());
}
return 32 - Integer.numberOfLeadingZeros(toInt());
}
public final WordWidth effectiveWidth() {
final int bit = numberOfEffectiveBits();
for (WordWidth width : WordWidth.VALUES) {
if (bit < width.numberOfBits) {
return width;
}
}
throw ProgramError.unexpected();
}
}