43972
|
1 |
/*
|
54601
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.core.common.util;
|
|
26 |
|
|
27 |
import static org.graalvm.compiler.core.common.util.TypeConversion.asS1;
|
|
28 |
import static org.graalvm.compiler.core.common.util.TypeConversion.asS2;
|
|
29 |
import static org.graalvm.compiler.core.common.util.TypeConversion.asS4;
|
|
30 |
import static org.graalvm.compiler.core.common.util.TypeConversion.asU1;
|
|
31 |
import static org.graalvm.compiler.core.common.util.TypeConversion.asU2;
|
|
32 |
import static org.graalvm.compiler.core.common.util.TypeConversion.asU4;
|
54328
|
33 |
import static org.graalvm.compiler.serviceprovider.GraalUnsafeAccess.getUnsafe;
|
|
34 |
|
57537
|
35 |
import java.nio.ByteBuffer;
|
|
36 |
|
54601
|
37 |
import org.graalvm.compiler.core.common.calc.UnsignedMath;
|
|
38 |
|
43972
|
39 |
import sun.misc.Unsafe;
|
|
40 |
|
|
41 |
/**
|
|
42 |
* Provides low-level sequential write access to a byte[] array for signed and unsigned values of
|
|
43 |
* size 1, 2, 4, and 8 bytes. To avoid copying an array when the buffer size is no longer
|
|
44 |
* sufficient, the buffer is split into chunks of a fixed size.
|
|
45 |
*
|
|
46 |
* The flag {@code supportsUnalignedMemoryAccess} must be set according to the capabilities of the
|
|
47 |
* hardware architecture: the value {@code true} allows more efficient memory access on
|
|
48 |
* architectures that support unaligned memory accesses; the value {@code false} is the safe
|
|
49 |
* fallback that works on every hardware.
|
|
50 |
*/
|
|
51 |
public abstract class UnsafeArrayTypeWriter implements TypeWriter {
|
54328
|
52 |
private static final Unsafe UNSAFE = getUnsafe();
|
43972
|
53 |
private static final int MIN_CHUNK_LENGTH = 200;
|
|
54 |
private static final int MAX_CHUNK_LENGTH = 16000;
|
|
55 |
|
54601
|
56 |
// Constants for UNSIGNED5 coding of Pack200
|
|
57 |
public static final long HIGH_WORD_SHIFT = 6;
|
|
58 |
public static final long NUM_HIGH_CODES = 1 << HIGH_WORD_SHIFT; // number of high codes (64)
|
|
59 |
public static final long NUM_LOW_CODES = (1 << Byte.SIZE) - NUM_HIGH_CODES;
|
|
60 |
public static final long MAX_BYTES = 11;
|
|
61 |
|
43972
|
62 |
static class Chunk {
|
|
63 |
protected final byte[] data;
|
|
64 |
protected int size;
|
|
65 |
protected Chunk next;
|
|
66 |
|
|
67 |
protected Chunk(int arrayLength) {
|
|
68 |
data = new byte[arrayLength];
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
protected final Chunk firstChunk;
|
|
73 |
protected Chunk writeChunk;
|
|
74 |
protected int totalSize;
|
|
75 |
|
|
76 |
public static UnsafeArrayTypeWriter create(boolean supportsUnalignedMemoryAccess) {
|
|
77 |
if (supportsUnalignedMemoryAccess) {
|
|
78 |
return new UnalignedUnsafeArrayTypeWriter();
|
|
79 |
} else {
|
|
80 |
return new AlignedUnsafeArrayTypeWriter();
|
|
81 |
}
|
|
82 |
}
|
|
83 |
|
|
84 |
protected UnsafeArrayTypeWriter() {
|
|
85 |
firstChunk = new Chunk(MIN_CHUNK_LENGTH);
|
|
86 |
writeChunk = firstChunk;
|
|
87 |
}
|
|
88 |
|
|
89 |
@Override
|
|
90 |
public final long getBytesWritten() {
|
|
91 |
return totalSize;
|
|
92 |
}
|
|
93 |
|
|
94 |
/**
|
|
95 |
* Copies the buffer into the provided byte[] array of length {@link #getBytesWritten()}.
|
|
96 |
*/
|
|
97 |
public final byte[] toArray(byte[] result) {
|
|
98 |
assert result.length == totalSize;
|
|
99 |
int resultIdx = 0;
|
|
100 |
for (Chunk cur = firstChunk; cur != null; cur = cur.next) {
|
|
101 |
System.arraycopy(cur.data, 0, result, resultIdx, cur.size);
|
|
102 |
resultIdx += cur.size;
|
|
103 |
}
|
|
104 |
assert resultIdx == totalSize;
|
|
105 |
return result;
|
|
106 |
}
|
|
107 |
|
57537
|
108 |
/** Copies the buffer into the provided ByteBuffer at its current position. */
|
|
109 |
public final ByteBuffer toByteBuffer(ByteBuffer buffer) {
|
|
110 |
assert buffer.remaining() <= totalSize;
|
|
111 |
int initialPos = buffer.position();
|
|
112 |
for (Chunk cur = firstChunk; cur != null; cur = cur.next) {
|
|
113 |
buffer.put(cur.data, 0, cur.size);
|
|
114 |
}
|
|
115 |
assert buffer.position() - initialPos == totalSize;
|
|
116 |
return buffer;
|
|
117 |
}
|
|
118 |
|
43972
|
119 |
@Override
|
|
120 |
public final void putS1(long value) {
|
|
121 |
long offset = writeOffset(Byte.BYTES);
|
54328
|
122 |
UNSAFE.putByte(writeChunk.data, offset, asS1(value));
|
43972
|
123 |
}
|
|
124 |
|
|
125 |
@Override
|
|
126 |
public final void putU1(long value) {
|
|
127 |
long offset = writeOffset(Byte.BYTES);
|
54328
|
128 |
UNSAFE.putByte(writeChunk.data, offset, asU1(value));
|
43972
|
129 |
}
|
|
130 |
|
|
131 |
@Override
|
|
132 |
public final void putU2(long value) {
|
|
133 |
putS2(asU2(value));
|
|
134 |
}
|
|
135 |
|
|
136 |
@Override
|
|
137 |
public final void putU4(long value) {
|
|
138 |
putS4(asU4(value));
|
|
139 |
}
|
|
140 |
|
54601
|
141 |
@Override
|
|
142 |
public void putS2(long value) {
|
|
143 |
long offset = writeOffset(Short.BYTES);
|
|
144 |
putS2(value, writeChunk, offset);
|
|
145 |
}
|
|
146 |
|
|
147 |
@Override
|
|
148 |
public void putS4(long value) {
|
|
149 |
long offset = writeOffset(Integer.BYTES);
|
|
150 |
putS4(value, writeChunk, offset);
|
|
151 |
}
|
|
152 |
|
|
153 |
@Override
|
|
154 |
public void putS8(long value) {
|
|
155 |
long offset = writeOffset(Long.BYTES);
|
|
156 |
putS8(value, writeChunk, offset);
|
|
157 |
}
|
|
158 |
|
|
159 |
protected abstract void putS2(long value, Chunk chunk, long offset);
|
|
160 |
|
|
161 |
protected abstract void putS4(long value, Chunk chunk, long offset);
|
|
162 |
|
|
163 |
protected abstract void putS8(long value, Chunk chunk, long offset);
|
|
164 |
|
43972
|
165 |
protected long writeOffset(int writeBytes) {
|
|
166 |
if (writeChunk.size + writeBytes >= writeChunk.data.length) {
|
|
167 |
Chunk newChunk = new Chunk(Math.min(writeChunk.data.length * 2, MAX_CHUNK_LENGTH));
|
|
168 |
writeChunk.next = newChunk;
|
|
169 |
writeChunk = newChunk;
|
|
170 |
}
|
|
171 |
|
|
172 |
assert Unsafe.ARRAY_BYTE_INDEX_SCALE == 1;
|
|
173 |
long result = writeChunk.size + Unsafe.ARRAY_BYTE_BASE_OFFSET;
|
|
174 |
|
|
175 |
totalSize += writeBytes;
|
|
176 |
writeChunk.size += writeBytes;
|
|
177 |
assert writeChunk.size <= writeChunk.data.length;
|
|
178 |
|
|
179 |
return result;
|
|
180 |
}
|
54601
|
181 |
|
|
182 |
@Override
|
|
183 |
public void patchS4(long value, long offset) {
|
|
184 |
long chunkStartOffset = 0;
|
|
185 |
Chunk chunk = firstChunk;
|
|
186 |
while (chunkStartOffset + chunk.size <= offset) {
|
|
187 |
chunkStartOffset += chunk.size;
|
|
188 |
chunk = chunk.next;
|
|
189 |
}
|
|
190 |
|
|
191 |
long targetOffset = offset - chunkStartOffset;
|
|
192 |
assert targetOffset + Integer.BYTES <= chunk.size : "out of bounds";
|
|
193 |
putS4(value, chunk, Unsafe.ARRAY_BYTE_BASE_OFFSET + targetOffset);
|
|
194 |
}
|
|
195 |
|
|
196 |
@Override
|
|
197 |
public void putSV(long value) {
|
|
198 |
// this is a modified version of the SIGNED5 encoding from Pack200
|
|
199 |
write(encodeSign(value));
|
|
200 |
}
|
|
201 |
|
|
202 |
@Override
|
|
203 |
public void putUV(long value) {
|
|
204 |
// this is a modified version of the UNSIGNED5 encoding from Pack200
|
|
205 |
write(value);
|
|
206 |
}
|
|
207 |
|
|
208 |
private static long encodeSign(long value) {
|
|
209 |
return (value << 1) ^ (value >> 63);
|
|
210 |
}
|
|
211 |
|
|
212 |
private void write(long value) {
|
|
213 |
if (UnsignedMath.belowThan(value, NUM_LOW_CODES)) {
|
|
214 |
putU1(value);
|
|
215 |
} else {
|
|
216 |
writePacked(value);
|
|
217 |
}
|
|
218 |
}
|
|
219 |
|
|
220 |
private void writePacked(long value) {
|
|
221 |
long sum = value;
|
|
222 |
for (int i = 1; UnsignedMath.aboveOrEqual(sum, NUM_LOW_CODES) && i < MAX_BYTES; i++) {
|
|
223 |
sum -= NUM_LOW_CODES;
|
|
224 |
long u1 = NUM_LOW_CODES + (sum & (NUM_HIGH_CODES - 1)); // this is a "high code"
|
|
225 |
sum >>>= HIGH_WORD_SHIFT; // extracted 6 bits
|
|
226 |
putU1(u1);
|
|
227 |
}
|
|
228 |
|
|
229 |
// remainder is either a "low code" or the last byte
|
|
230 |
assert sum == (sum & 0xFF) : "not a byte";
|
|
231 |
putU1(sum & 0xFF);
|
|
232 |
}
|
43972
|
233 |
}
|
|
234 |
|
|
235 |
final class UnalignedUnsafeArrayTypeWriter extends UnsafeArrayTypeWriter {
|
54328
|
236 |
private static final Unsafe UNSAFE = getUnsafe();
|
|
237 |
|
43972
|
238 |
@Override
|
54601
|
239 |
protected void putS2(long value, Chunk chunk, long offset) {
|
|
240 |
UNSAFE.putShort(chunk.data, offset, asS2(value));
|
43972
|
241 |
}
|
|
242 |
|
|
243 |
@Override
|
54601
|
244 |
protected void putS4(long value, Chunk chunk, long offset) {
|
|
245 |
UNSAFE.putInt(chunk.data, offset, asS4(value));
|
43972
|
246 |
}
|
|
247 |
|
|
248 |
@Override
|
54601
|
249 |
protected void putS8(long value, Chunk chunk, long offset) {
|
|
250 |
UNSAFE.putLong(chunk.data, offset, value);
|
43972
|
251 |
}
|
|
252 |
}
|
|
253 |
|
|
254 |
final class AlignedUnsafeArrayTypeWriter extends UnsafeArrayTypeWriter {
|
54328
|
255 |
private static final Unsafe UNSAFE = getUnsafe();
|
|
256 |
|
43972
|
257 |
@Override
|
54601
|
258 |
protected void putS2(long value, Chunk chunk, long offset) {
|
|
259 |
UNSAFE.putByte(chunk.data, offset + 0, (byte) (value >> 0));
|
|
260 |
UNSAFE.putByte(chunk.data, offset + 1, (byte) (value >> 8));
|
43972
|
261 |
}
|
|
262 |
|
|
263 |
@Override
|
54601
|
264 |
protected void putS4(long value, Chunk chunk, long offset) {
|
|
265 |
UNSAFE.putByte(chunk.data, offset + 0, (byte) (value >> 0));
|
|
266 |
UNSAFE.putByte(chunk.data, offset + 1, (byte) (value >> 8));
|
|
267 |
UNSAFE.putByte(chunk.data, offset + 2, (byte) (value >> 16));
|
|
268 |
UNSAFE.putByte(chunk.data, offset + 3, (byte) (value >> 24));
|
43972
|
269 |
}
|
|
270 |
|
|
271 |
@Override
|
54601
|
272 |
protected void putS8(long value, Chunk chunk, long offset) {
|
|
273 |
UNSAFE.putByte(chunk.data, offset + 0, (byte) (value >> 0));
|
|
274 |
UNSAFE.putByte(chunk.data, offset + 1, (byte) (value >> 8));
|
|
275 |
UNSAFE.putByte(chunk.data, offset + 2, (byte) (value >> 16));
|
|
276 |
UNSAFE.putByte(chunk.data, offset + 3, (byte) (value >> 24));
|
|
277 |
UNSAFE.putByte(chunk.data, offset + 4, (byte) (value >> 32));
|
|
278 |
UNSAFE.putByte(chunk.data, offset + 5, (byte) (value >> 40));
|
|
279 |
UNSAFE.putByte(chunk.data, offset + 6, (byte) (value >> 48));
|
|
280 |
UNSAFE.putByte(chunk.data, offset + 7, (byte) (value >> 56));
|
43972
|
281 |
}
|
|
282 |
}
|