43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2013, 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 |
package org.graalvm.compiler.replacements.test;
|
|
24 |
|
|
25 |
import org.graalvm.compiler.api.replacements.Snippet;
|
|
26 |
import org.graalvm.compiler.nodes.NamedLocationIdentity;
|
48190
|
27 |
import org.graalvm.compiler.nodes.NodeView;
|
43972
|
28 |
import org.graalvm.compiler.nodes.ReturnNode;
|
|
29 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
30 |
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
|
50609
|
31 |
import org.graalvm.compiler.nodes.ValueNode;
|
|
32 |
import org.graalvm.compiler.nodes.calc.ConvertNode;
|
43972
|
33 |
import org.graalvm.compiler.nodes.calc.SignExtendNode;
|
|
34 |
import org.graalvm.compiler.nodes.extended.JavaReadNode;
|
|
35 |
import org.graalvm.compiler.nodes.extended.JavaWriteNode;
|
|
36 |
import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
|
|
37 |
import org.graalvm.compiler.phases.OptimisticOptimizations;
|
|
38 |
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
|
|
39 |
import org.graalvm.compiler.phases.tiers.HighTierContext;
|
|
40 |
import org.graalvm.compiler.word.Word;
|
46459
|
41 |
import org.graalvm.compiler.word.WordCastNode;
|
49873
|
42 |
import jdk.internal.vm.compiler.word.LocationIdentity;
|
|
43 |
import jdk.internal.vm.compiler.word.Pointer;
|
|
44 |
import jdk.internal.vm.compiler.word.WordFactory;
|
46344
|
45 |
import org.junit.Assert;
|
|
46 |
import org.junit.Test;
|
43972
|
47 |
|
|
48 |
import jdk.vm.ci.code.BytecodeFrame;
|
|
49 |
import jdk.vm.ci.code.TargetDescription;
|
|
50 |
import jdk.vm.ci.meta.JavaKind;
|
|
51 |
|
|
52 |
/**
|
|
53 |
* Tests for the {@link Pointer} read and write operations.
|
|
54 |
*/
|
46344
|
55 |
public class PointerTest extends SnippetsTest {
|
43972
|
56 |
|
|
57 |
private static final LocationIdentity ID = NamedLocationIdentity.mutable("ID");
|
|
58 |
private static final JavaKind[] KINDS = new JavaKind[]{JavaKind.Byte, JavaKind.Char, JavaKind.Short, JavaKind.Int, JavaKind.Long, JavaKind.Float, JavaKind.Double, JavaKind.Object};
|
|
59 |
private final TargetDescription target;
|
|
60 |
|
|
61 |
public PointerTest() {
|
|
62 |
target = getCodeCache().getTarget();
|
|
63 |
}
|
|
64 |
|
|
65 |
@Test
|
|
66 |
public void testRead1() {
|
|
67 |
for (JavaKind kind : KINDS) {
|
|
68 |
assertRead(parseEager("read" + kind.name() + "1", AllowAssumptions.YES), kind, true, ID);
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
@Test
|
|
73 |
public void testRead2() {
|
|
74 |
for (JavaKind kind : KINDS) {
|
|
75 |
assertRead(parseEager("read" + kind.name() + "2", AllowAssumptions.YES), kind, true, ID);
|
|
76 |
}
|
|
77 |
}
|
|
78 |
|
|
79 |
@Test
|
|
80 |
public void testRead3() {
|
|
81 |
for (JavaKind kind : KINDS) {
|
|
82 |
assertRead(parseEager("read" + kind.name() + "3", AllowAssumptions.YES), kind, true, LocationIdentity.any());
|
|
83 |
}
|
|
84 |
}
|
|
85 |
|
|
86 |
@Test
|
|
87 |
public void testWrite1() {
|
|
88 |
for (JavaKind kind : KINDS) {
|
50609
|
89 |
assertWrite(parseEager("write" + kind.name() + "1", AllowAssumptions.YES), kind, true, ID);
|
43972
|
90 |
}
|
|
91 |
}
|
|
92 |
|
|
93 |
@Test
|
|
94 |
public void testWrite2() {
|
|
95 |
for (JavaKind kind : KINDS) {
|
50609
|
96 |
assertWrite(parseEager("write" + kind.name() + "2", AllowAssumptions.YES), kind, true, ID);
|
43972
|
97 |
}
|
|
98 |
}
|
|
99 |
|
|
100 |
@Test
|
|
101 |
public void testWrite3() {
|
|
102 |
for (JavaKind kind : KINDS) {
|
50609
|
103 |
assertWrite(parseEager("write" + kind.name() + "3", AllowAssumptions.YES), kind, true, LocationIdentity.any());
|
43972
|
104 |
}
|
|
105 |
}
|
|
106 |
|
|
107 |
private void assertRead(StructuredGraph graph, JavaKind kind, boolean indexConvert, LocationIdentity locationIdentity) {
|
|
108 |
WordCastNode cast = (WordCastNode) graph.start().next();
|
|
109 |
|
|
110 |
JavaReadNode read = (JavaReadNode) cast.next();
|
48190
|
111 |
Assert.assertEquals(kind.getStackKind(), read.stamp(NodeView.DEFAULT).getStackKind());
|
43972
|
112 |
|
|
113 |
OffsetAddressNode address = (OffsetAddressNode) read.getAddress();
|
|
114 |
Assert.assertEquals(cast, address.getBase());
|
|
115 |
Assert.assertEquals(graph.getParameter(0), cast.getInput());
|
48190
|
116 |
Assert.assertEquals(target.wordJavaKind, cast.stamp(NodeView.DEFAULT).getStackKind());
|
43972
|
117 |
|
|
118 |
Assert.assertEquals(locationIdentity, read.getLocationIdentity());
|
|
119 |
|
|
120 |
if (indexConvert) {
|
|
121 |
SignExtendNode convert = (SignExtendNode) address.getOffset();
|
|
122 |
Assert.assertEquals(convert.getInputBits(), 32);
|
|
123 |
Assert.assertEquals(convert.getResultBits(), 64);
|
|
124 |
Assert.assertEquals(graph.getParameter(1), convert.getValue());
|
|
125 |
} else {
|
|
126 |
Assert.assertEquals(graph.getParameter(1), address.getOffset());
|
|
127 |
}
|
|
128 |
|
|
129 |
ReturnNode ret = (ReturnNode) read.next();
|
|
130 |
Assert.assertEquals(read, ret.result());
|
|
131 |
}
|
|
132 |
|
50609
|
133 |
private void assertWrite(StructuredGraph graph, JavaKind kind, boolean indexConvert, LocationIdentity locationIdentity) {
|
43972
|
134 |
WordCastNode cast = (WordCastNode) graph.start().next();
|
|
135 |
|
|
136 |
JavaWriteNode write = (JavaWriteNode) cast.next();
|
50609
|
137 |
ValueNode valueNode = write.value();
|
|
138 |
if (kind != kind.getStackKind()) {
|
|
139 |
while (valueNode instanceof ConvertNode) {
|
|
140 |
valueNode = ((ConvertNode) valueNode).getValue();
|
|
141 |
}
|
|
142 |
}
|
|
143 |
Assert.assertEquals(graph.getParameter(2), valueNode);
|
43972
|
144 |
Assert.assertEquals(BytecodeFrame.AFTER_BCI, write.stateAfter().bci);
|
|
145 |
|
|
146 |
OffsetAddressNode address = (OffsetAddressNode) write.getAddress();
|
|
147 |
Assert.assertEquals(cast, address.getBase());
|
|
148 |
Assert.assertEquals(graph.getParameter(0), cast.getInput());
|
48190
|
149 |
Assert.assertEquals(target.wordJavaKind, cast.stamp(NodeView.DEFAULT).getStackKind());
|
43972
|
150 |
|
|
151 |
Assert.assertEquals(locationIdentity, write.getLocationIdentity());
|
|
152 |
|
|
153 |
if (indexConvert) {
|
|
154 |
SignExtendNode convert = (SignExtendNode) address.getOffset();
|
|
155 |
Assert.assertEquals(convert.getInputBits(), 32);
|
|
156 |
Assert.assertEquals(convert.getResultBits(), 64);
|
|
157 |
Assert.assertEquals(graph.getParameter(1), convert.getValue());
|
|
158 |
} else {
|
|
159 |
Assert.assertEquals(graph.getParameter(1), address.getOffset());
|
|
160 |
}
|
|
161 |
|
|
162 |
ReturnNode ret = (ReturnNode) write.next();
|
|
163 |
Assert.assertEquals(null, ret.result());
|
|
164 |
}
|
|
165 |
|
|
166 |
@Snippet
|
|
167 |
public static byte readByte1(Object o, int offset) {
|
|
168 |
return Word.objectToTrackedPointer(o).readByte(offset, ID);
|
|
169 |
}
|
|
170 |
|
|
171 |
@Snippet
|
|
172 |
public static byte readByte2(Object o, int offset) {
|
46459
|
173 |
return Word.objectToTrackedPointer(o).readByte(WordFactory.signed(offset), ID);
|
43972
|
174 |
}
|
|
175 |
|
|
176 |
@Snippet
|
|
177 |
public static byte readByte3(Object o, int offset) {
|
|
178 |
return Word.objectToTrackedPointer(o).readByte(offset);
|
|
179 |
}
|
|
180 |
|
|
181 |
@Snippet
|
|
182 |
public static void writeByte1(Object o, int offset, byte value) {
|
|
183 |
Word.objectToTrackedPointer(o).writeByte(offset, value, ID);
|
|
184 |
}
|
|
185 |
|
|
186 |
@Snippet
|
|
187 |
public static void writeByte2(Object o, int offset, byte value) {
|
46459
|
188 |
Word.objectToTrackedPointer(o).writeByte(WordFactory.signed(offset), value, ID);
|
43972
|
189 |
}
|
|
190 |
|
|
191 |
@Snippet
|
|
192 |
public static void writeByte3(Object o, int offset, byte value) {
|
|
193 |
Word.objectToTrackedPointer(o).writeByte(offset, value);
|
|
194 |
}
|
|
195 |
|
|
196 |
@Snippet
|
|
197 |
public static char readChar1(Object o, int offset) {
|
|
198 |
return Word.objectToTrackedPointer(o).readChar(offset, ID);
|
|
199 |
}
|
|
200 |
|
|
201 |
@Snippet
|
|
202 |
public static char readChar2(Object o, int offset) {
|
46459
|
203 |
return Word.objectToTrackedPointer(o).readChar(WordFactory.signed(offset), ID);
|
43972
|
204 |
}
|
|
205 |
|
|
206 |
@Snippet
|
|
207 |
public static char readChar3(Object o, int offset) {
|
|
208 |
return Word.objectToTrackedPointer(o).readChar(offset);
|
|
209 |
}
|
|
210 |
|
|
211 |
@Snippet
|
|
212 |
public static void writeChar1(Object o, int offset, char value) {
|
|
213 |
Word.objectToTrackedPointer(o).writeChar(offset, value, ID);
|
|
214 |
}
|
|
215 |
|
|
216 |
@Snippet
|
|
217 |
public static void writeChar2(Object o, int offset, char value) {
|
46459
|
218 |
Word.objectToTrackedPointer(o).writeChar(WordFactory.signed(offset), value, ID);
|
43972
|
219 |
}
|
|
220 |
|
|
221 |
@Snippet
|
|
222 |
public static void writeChar3(Object o, int offset, char value) {
|
|
223 |
Word.objectToTrackedPointer(o).writeChar(offset, value);
|
|
224 |
}
|
|
225 |
|
|
226 |
@Snippet
|
|
227 |
public static short readShort1(Object o, int offset) {
|
|
228 |
return Word.objectToTrackedPointer(o).readShort(offset, ID);
|
|
229 |
}
|
|
230 |
|
|
231 |
@Snippet
|
|
232 |
public static short readShort2(Object o, int offset) {
|
46459
|
233 |
return Word.objectToTrackedPointer(o).readShort(WordFactory.signed(offset), ID);
|
43972
|
234 |
}
|
|
235 |
|
|
236 |
@Snippet
|
|
237 |
public static short readShort3(Object o, int offset) {
|
|
238 |
return Word.objectToTrackedPointer(o).readShort(offset);
|
|
239 |
}
|
|
240 |
|
|
241 |
@Snippet
|
|
242 |
public static void writeShort1(Object o, int offset, short value) {
|
|
243 |
Word.objectToTrackedPointer(o).writeShort(offset, value, ID);
|
|
244 |
}
|
|
245 |
|
|
246 |
@Snippet
|
|
247 |
public static void writeShort2(Object o, int offset, short value) {
|
46459
|
248 |
Word.objectToTrackedPointer(o).writeShort(WordFactory.signed(offset), value, ID);
|
43972
|
249 |
}
|
|
250 |
|
|
251 |
@Snippet
|
|
252 |
public static void writeShort3(Object o, int offset, short value) {
|
|
253 |
Word.objectToTrackedPointer(o).writeShort(offset, value);
|
|
254 |
}
|
|
255 |
|
|
256 |
@Snippet
|
|
257 |
public static int readInt1(Object o, int offset) {
|
|
258 |
return Word.objectToTrackedPointer(o).readInt(offset, ID);
|
|
259 |
}
|
|
260 |
|
|
261 |
@Snippet
|
|
262 |
public static int readInt2(Object o, int offset) {
|
46459
|
263 |
return Word.objectToTrackedPointer(o).readInt(WordFactory.signed(offset), ID);
|
43972
|
264 |
}
|
|
265 |
|
|
266 |
@Snippet
|
|
267 |
public static int readInt3(Object o, int offset) {
|
|
268 |
return Word.objectToTrackedPointer(o).readInt(offset);
|
|
269 |
}
|
|
270 |
|
|
271 |
@Snippet
|
|
272 |
public static void writeInt1(Object o, int offset, int value) {
|
|
273 |
Word.objectToTrackedPointer(o).writeInt(offset, value, ID);
|
|
274 |
}
|
|
275 |
|
|
276 |
@Snippet
|
|
277 |
public static void writeInt2(Object o, int offset, int value) {
|
46459
|
278 |
Word.objectToTrackedPointer(o).writeInt(WordFactory.signed(offset), value, ID);
|
43972
|
279 |
}
|
|
280 |
|
|
281 |
@Snippet
|
|
282 |
public static void writeInt3(Object o, int offset, int value) {
|
|
283 |
Word.objectToTrackedPointer(o).writeInt(offset, value);
|
|
284 |
}
|
|
285 |
|
|
286 |
@Snippet
|
|
287 |
public static long readLong1(Object o, int offset) {
|
|
288 |
return Word.objectToTrackedPointer(o).readLong(offset, ID);
|
|
289 |
}
|
|
290 |
|
|
291 |
@Snippet
|
|
292 |
public static long readLong2(Object o, int offset) {
|
46459
|
293 |
return Word.objectToTrackedPointer(o).readLong(WordFactory.signed(offset), ID);
|
43972
|
294 |
}
|
|
295 |
|
|
296 |
@Snippet
|
|
297 |
public static long readLong3(Object o, int offset) {
|
|
298 |
return Word.objectToTrackedPointer(o).readLong(offset);
|
|
299 |
}
|
|
300 |
|
|
301 |
@Snippet
|
|
302 |
public static void writeLong1(Object o, int offset, long value) {
|
|
303 |
Word.objectToTrackedPointer(o).writeLong(offset, value, ID);
|
|
304 |
}
|
|
305 |
|
|
306 |
@Snippet
|
|
307 |
public static void writeLong2(Object o, int offset, long value) {
|
46459
|
308 |
Word.objectToTrackedPointer(o).writeLong(WordFactory.signed(offset), value, ID);
|
43972
|
309 |
}
|
|
310 |
|
|
311 |
@Snippet
|
|
312 |
public static void writeLong3(Object o, int offset, long value) {
|
|
313 |
Word.objectToTrackedPointer(o).writeLong(offset, value);
|
|
314 |
}
|
|
315 |
|
|
316 |
@Snippet
|
|
317 |
public static float readFloat1(Object o, int offset) {
|
|
318 |
return Word.objectToTrackedPointer(o).readFloat(offset, ID);
|
|
319 |
}
|
|
320 |
|
|
321 |
@Snippet
|
|
322 |
public static float readFloat2(Object o, int offset) {
|
46459
|
323 |
return Word.objectToTrackedPointer(o).readFloat(WordFactory.signed(offset), ID);
|
43972
|
324 |
}
|
|
325 |
|
|
326 |
@Snippet
|
|
327 |
public static float readFloat3(Object o, int offset) {
|
|
328 |
return Word.objectToTrackedPointer(o).readFloat(offset);
|
|
329 |
}
|
|
330 |
|
|
331 |
@Snippet
|
|
332 |
public static void writeFloat1(Object o, int offset, float value) {
|
|
333 |
Word.objectToTrackedPointer(o).writeFloat(offset, value, ID);
|
|
334 |
}
|
|
335 |
|
|
336 |
@Snippet
|
|
337 |
public static void writeFloat2(Object o, int offset, float value) {
|
46459
|
338 |
Word.objectToTrackedPointer(o).writeFloat(WordFactory.signed(offset), value, ID);
|
43972
|
339 |
}
|
|
340 |
|
|
341 |
@Snippet
|
|
342 |
public static void writeFloat3(Object o, int offset, float value) {
|
|
343 |
Word.objectToTrackedPointer(o).writeFloat(offset, value);
|
|
344 |
}
|
|
345 |
|
|
346 |
@Snippet
|
|
347 |
public static double readDouble1(Object o, int offset) {
|
|
348 |
return Word.objectToTrackedPointer(o).readDouble(offset, ID);
|
|
349 |
}
|
|
350 |
|
|
351 |
@Snippet
|
|
352 |
public static double readDouble2(Object o, int offset) {
|
46459
|
353 |
return Word.objectToTrackedPointer(o).readDouble(WordFactory.signed(offset), ID);
|
43972
|
354 |
}
|
|
355 |
|
|
356 |
@Snippet
|
|
357 |
public static double readDouble3(Object o, int offset) {
|
|
358 |
return Word.objectToTrackedPointer(o).readDouble(offset);
|
|
359 |
}
|
|
360 |
|
|
361 |
@Snippet
|
|
362 |
public static void writeDouble1(Object o, int offset, double value) {
|
|
363 |
Word.objectToTrackedPointer(o).writeDouble(offset, value, ID);
|
|
364 |
}
|
|
365 |
|
|
366 |
@Snippet
|
|
367 |
public static void writeDouble2(Object o, int offset, double value) {
|
46459
|
368 |
Word.objectToTrackedPointer(o).writeDouble(WordFactory.signed(offset), value, ID);
|
43972
|
369 |
}
|
|
370 |
|
|
371 |
@Snippet
|
|
372 |
public static void writeDouble3(Object o, int offset, double value) {
|
|
373 |
Word.objectToTrackedPointer(o).writeDouble(offset, value);
|
|
374 |
}
|
|
375 |
|
|
376 |
@Snippet
|
|
377 |
public static Object readObject1(Object o, int offset) {
|
|
378 |
return Word.objectToTrackedPointer(o).readObject(offset, ID);
|
|
379 |
}
|
|
380 |
|
|
381 |
@Snippet
|
|
382 |
public static Object readObject2(Object o, int offset) {
|
46459
|
383 |
return Word.objectToTrackedPointer(o).readObject(WordFactory.signed(offset), ID);
|
43972
|
384 |
}
|
|
385 |
|
|
386 |
@Snippet
|
|
387 |
public static Object readObject3(Object o, int offset) {
|
|
388 |
return Word.objectToTrackedPointer(o).readObject(offset);
|
|
389 |
}
|
|
390 |
|
|
391 |
@Snippet
|
|
392 |
public static void writeObject1(Object o, int offset, Object value) {
|
|
393 |
Word.objectToTrackedPointer(o).writeObject(offset, value, ID);
|
|
394 |
}
|
|
395 |
|
|
396 |
@Snippet
|
|
397 |
public static void writeObject2(Object o, int offset, Object value) {
|
46459
|
398 |
Word.objectToTrackedPointer(o).writeObject(WordFactory.signed(offset), value, ID);
|
43972
|
399 |
}
|
|
400 |
|
|
401 |
@Snippet
|
|
402 |
public static void writeObject3(Object o, int offset, Object value) {
|
|
403 |
Word.objectToTrackedPointer(o).writeObject(offset, value);
|
|
404 |
}
|
|
405 |
|
|
406 |
private void assertNumWordCasts(String snippetName, int expectedWordCasts) {
|
|
407 |
HighTierContext context = new HighTierContext(getProviders(), null, OptimisticOptimizations.ALL);
|
|
408 |
|
|
409 |
StructuredGraph graph = parseEager(snippetName, AllowAssumptions.YES);
|
|
410 |
new CanonicalizerPhase().apply(graph, context);
|
|
411 |
Assert.assertEquals(expectedWordCasts, graph.getNodes().filter(WordCastNode.class).count());
|
|
412 |
}
|
|
413 |
|
|
414 |
@Test
|
|
415 |
public void testUnusedFromObject() {
|
|
416 |
assertNumWordCasts("unusedFromObject", 0);
|
|
417 |
}
|
|
418 |
|
|
419 |
@Snippet
|
|
420 |
public static void unusedFromObject(Object o) {
|
|
421 |
Word.objectToTrackedPointer(o);
|
|
422 |
}
|
|
423 |
|
|
424 |
@Test
|
|
425 |
public void testUnusedRawValue() {
|
|
426 |
assertNumWordCasts("unusedRawValue", 0);
|
|
427 |
}
|
|
428 |
|
|
429 |
@Snippet
|
|
430 |
public static void unusedRawValue(Object o) {
|
|
431 |
Word.objectToTrackedPointer(o).rawValue();
|
|
432 |
}
|
|
433 |
|
|
434 |
@Test
|
|
435 |
public void testUsedRawValue() {
|
|
436 |
assertNumWordCasts("usedRawValue", 1);
|
|
437 |
}
|
|
438 |
|
|
439 |
@Snippet
|
|
440 |
public static long usedRawValue(Object o) {
|
|
441 |
return Word.objectToTrackedPointer(o).rawValue();
|
|
442 |
}
|
|
443 |
|
|
444 |
@Test
|
|
445 |
public void testUnusedToObject() {
|
|
446 |
assertNumWordCasts("unusedToObject", 0);
|
|
447 |
}
|
|
448 |
|
|
449 |
@Snippet
|
|
450 |
public static void unusedToObject(Word w) {
|
|
451 |
w.toObject();
|
|
452 |
}
|
|
453 |
|
|
454 |
@Test
|
|
455 |
public void testUsedToObject() {
|
|
456 |
assertNumWordCasts("usedToObject", 1);
|
|
457 |
}
|
|
458 |
|
|
459 |
@Snippet
|
|
460 |
public static Object usedToObject(Word w) {
|
|
461 |
return w.toObject();
|
|
462 |
}
|
|
463 |
}
|