001/*
002 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.replacements.test;
024
025import jdk.internal.jvmci.code.*;
026import jdk.internal.jvmci.meta.*;
027
028import org.junit.*;
029
030import com.oracle.graal.compiler.test.*;
031import com.oracle.graal.nodes.*;
032import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
033import com.oracle.graal.nodes.calc.*;
034import com.oracle.graal.nodes.extended.*;
035import com.oracle.graal.nodes.memory.address.*;
036import com.oracle.graal.replacements.*;
037import com.oracle.graal.word.*;
038
039/**
040 * Tests for the {@link Pointer} read and write operations.
041 */
042public class ObjectAccessTest extends GraalCompilerTest implements Snippets {
043
044    private static final LocationIdentity ID = NamedLocationIdentity.mutable("ObjectAccessTestID");
045    private static final Kind[] KINDS = new Kind[]{Kind.Byte, Kind.Char, Kind.Short, Kind.Int, Kind.Long, Kind.Float, Kind.Double, Kind.Object};
046    private final ReplacementsImpl installer;
047
048    public ObjectAccessTest() {
049        installer = (ReplacementsImpl) getReplacements();
050    }
051
052    @Override
053    protected StructuredGraph parseEager(ResolvedJavaMethod m, AllowAssumptions allowAssumptions) {
054        return installer.makeGraph(m, null, null);
055    }
056
057    @Test
058    public void testRead1() {
059        for (Kind kind : KINDS) {
060            assertRead(parseEager("read" + kind.name() + "1", AllowAssumptions.YES), kind, true, ID);
061        }
062    }
063
064    @Test
065    public void testRead2() {
066        for (Kind kind : KINDS) {
067            assertRead(parseEager("read" + kind.name() + "2", AllowAssumptions.YES), kind, true, ID);
068        }
069    }
070
071    @Test
072    public void testRead3() {
073        for (Kind kind : KINDS) {
074            assertRead(parseEager("read" + kind.name() + "3", AllowAssumptions.YES), kind, true, LocationIdentity.any());
075        }
076    }
077
078    @Test
079    public void testWrite1() {
080        for (Kind kind : KINDS) {
081            assertWrite(parseEager("write" + kind.name() + "1", AllowAssumptions.YES), true, ID);
082        }
083    }
084
085    @Test
086    public void testWrite2() {
087        for (Kind kind : KINDS) {
088            assertWrite(parseEager("write" + kind.name() + "2", AllowAssumptions.YES), true, ID);
089        }
090    }
091
092    @Test
093    public void testWrite3() {
094        for (Kind kind : KINDS) {
095            assertWrite(parseEager("write" + kind.name() + "3", AllowAssumptions.YES), true, LocationIdentity.any());
096        }
097    }
098
099    private static void assertRead(StructuredGraph graph, Kind kind, boolean indexConvert, LocationIdentity locationIdentity) {
100        JavaReadNode read = (JavaReadNode) graph.start().next();
101        Assert.assertEquals(kind.getStackKind(), read.stamp().getStackKind());
102
103        OffsetAddressNode address = (OffsetAddressNode) read.getAddress();
104        Assert.assertEquals(graph.getParameter(0), address.getBase());
105        Assert.assertEquals(locationIdentity, read.getLocationIdentity());
106
107        if (indexConvert) {
108            SignExtendNode convert = (SignExtendNode) address.getOffset();
109            Assert.assertEquals(convert.getInputBits(), 32);
110            Assert.assertEquals(convert.getResultBits(), 64);
111            Assert.assertEquals(graph.getParameter(1), convert.getValue());
112        } else {
113            Assert.assertEquals(graph.getParameter(1), address.getOffset());
114        }
115
116        ReturnNode ret = (ReturnNode) read.next();
117        Assert.assertEquals(read, ret.result());
118    }
119
120    private static void assertWrite(StructuredGraph graph, boolean indexConvert, LocationIdentity locationIdentity) {
121        JavaWriteNode write = (JavaWriteNode) graph.start().next();
122        Assert.assertEquals(graph.getParameter(2), write.value());
123
124        OffsetAddressNode address = (OffsetAddressNode) write.getAddress();
125        Assert.assertEquals(graph.getParameter(0), address.getBase());
126        Assert.assertEquals(BytecodeFrame.AFTER_BCI, write.stateAfter().bci);
127
128        Assert.assertEquals(locationIdentity, write.getLocationIdentity());
129
130        if (indexConvert) {
131            SignExtendNode convert = (SignExtendNode) address.getOffset();
132            Assert.assertEquals(convert.getInputBits(), 32);
133            Assert.assertEquals(convert.getResultBits(), 64);
134            Assert.assertEquals(graph.getParameter(1), convert.getValue());
135        } else {
136            Assert.assertEquals(graph.getParameter(1), address.getOffset());
137        }
138
139        ReturnNode ret = (ReturnNode) write.next();
140        Assert.assertEquals(null, ret.result());
141    }
142
143    @Snippet
144    public static byte readByte1(Object o, int offset) {
145        return ObjectAccess.readByte(o, offset, ID);
146    }
147
148    @Snippet
149    public static byte readByte2(Object o, int offset) {
150        return ObjectAccess.readByte(o, Word.signed(offset), ID);
151    }
152
153    @Snippet
154    public static byte readByte3(Object o, int offset) {
155        return ObjectAccess.readByte(o, offset);
156    }
157
158    @Snippet
159    public static void writeByte1(Object o, int offset, byte value) {
160        ObjectAccess.writeByte(o, offset, value, ID);
161    }
162
163    @Snippet
164    public static void writeByte2(Object o, int offset, byte value) {
165        ObjectAccess.writeByte(o, Word.signed(offset), value, ID);
166    }
167
168    @Snippet
169    public static void writeByte3(Object o, int offset, byte value) {
170        ObjectAccess.writeByte(o, offset, value);
171    }
172
173    @Snippet
174    public static char readChar1(Object o, int offset) {
175        return ObjectAccess.readChar(o, offset, ID);
176    }
177
178    @Snippet
179    public static char readChar2(Object o, int offset) {
180        return ObjectAccess.readChar(o, Word.signed(offset), ID);
181    }
182
183    @Snippet
184    public static char readChar3(Object o, int offset) {
185        return ObjectAccess.readChar(o, offset);
186    }
187
188    @Snippet
189    public static void writeChar1(Object o, int offset, char value) {
190        ObjectAccess.writeChar(o, offset, value, ID);
191    }
192
193    @Snippet
194    public static void writeChar2(Object o, int offset, char value) {
195        ObjectAccess.writeChar(o, Word.signed(offset), value, ID);
196    }
197
198    @Snippet
199    public static void writeChar3(Object o, int offset, char value) {
200        ObjectAccess.writeChar(o, offset, value);
201    }
202
203    @Snippet
204    public static short readShort1(Object o, int offset) {
205        return ObjectAccess.readShort(o, offset, ID);
206    }
207
208    @Snippet
209    public static short readShort2(Object o, int offset) {
210        return ObjectAccess.readShort(o, Word.signed(offset), ID);
211    }
212
213    @Snippet
214    public static short readShort3(Object o, int offset) {
215        return ObjectAccess.readShort(o, offset);
216    }
217
218    @Snippet
219    public static void writeShort1(Object o, int offset, short value) {
220        ObjectAccess.writeShort(o, offset, value, ID);
221    }
222
223    @Snippet
224    public static void writeShort2(Object o, int offset, short value) {
225        ObjectAccess.writeShort(o, Word.signed(offset), value, ID);
226    }
227
228    @Snippet
229    public static void writeShort3(Object o, int offset, short value) {
230        ObjectAccess.writeShort(o, offset, value);
231    }
232
233    @Snippet
234    public static int readInt1(Object o, int offset) {
235        return ObjectAccess.readInt(o, offset, ID);
236    }
237
238    @Snippet
239    public static int readInt2(Object o, int offset) {
240        return ObjectAccess.readInt(o, Word.signed(offset), ID);
241    }
242
243    @Snippet
244    public static int readInt3(Object o, int offset) {
245        return ObjectAccess.readInt(o, offset);
246    }
247
248    @Snippet
249    public static void writeInt1(Object o, int offset, int value) {
250        ObjectAccess.writeInt(o, offset, value, ID);
251    }
252
253    @Snippet
254    public static void writeInt2(Object o, int offset, int value) {
255        ObjectAccess.writeInt(o, Word.signed(offset), value, ID);
256    }
257
258    @Snippet
259    public static void writeInt3(Object o, int offset, int value) {
260        ObjectAccess.writeInt(o, offset, value);
261    }
262
263    @Snippet
264    public static long readLong1(Object o, int offset) {
265        return ObjectAccess.readLong(o, offset, ID);
266    }
267
268    @Snippet
269    public static long readLong2(Object o, int offset) {
270        return ObjectAccess.readLong(o, Word.signed(offset), ID);
271    }
272
273    @Snippet
274    public static long readLong3(Object o, int offset) {
275        return ObjectAccess.readLong(o, offset);
276    }
277
278    @Snippet
279    public static void writeLong1(Object o, int offset, long value) {
280        ObjectAccess.writeLong(o, offset, value, ID);
281    }
282
283    @Snippet
284    public static void writeLong2(Object o, int offset, long value) {
285        ObjectAccess.writeLong(o, Word.signed(offset), value, ID);
286    }
287
288    @Snippet
289    public static void writeLong3(Object o, int offset, long value) {
290        ObjectAccess.writeLong(o, offset, value);
291    }
292
293    @Snippet
294    public static float readFloat1(Object o, int offset) {
295        return ObjectAccess.readFloat(o, offset, ID);
296    }
297
298    @Snippet
299    public static float readFloat2(Object o, int offset) {
300        return ObjectAccess.readFloat(o, Word.signed(offset), ID);
301    }
302
303    @Snippet
304    public static float readFloat3(Object o, int offset) {
305        return ObjectAccess.readFloat(o, offset);
306    }
307
308    @Snippet
309    public static void writeFloat1(Object o, int offset, float value) {
310        ObjectAccess.writeFloat(o, offset, value, ID);
311    }
312
313    @Snippet
314    public static void writeFloat2(Object o, int offset, float value) {
315        ObjectAccess.writeFloat(o, Word.signed(offset), value, ID);
316    }
317
318    @Snippet
319    public static void writeFloat3(Object o, int offset, float value) {
320        ObjectAccess.writeFloat(o, offset, value);
321    }
322
323    @Snippet
324    public static double readDouble1(Object o, int offset) {
325        return ObjectAccess.readDouble(o, offset, ID);
326    }
327
328    @Snippet
329    public static double readDouble2(Object o, int offset) {
330        return ObjectAccess.readDouble(o, Word.signed(offset), ID);
331    }
332
333    @Snippet
334    public static double readDouble3(Object o, int offset) {
335        return ObjectAccess.readDouble(o, offset);
336    }
337
338    @Snippet
339    public static void writeDouble1(Object o, int offset, double value) {
340        ObjectAccess.writeDouble(o, offset, value, ID);
341    }
342
343    @Snippet
344    public static void writeDouble2(Object o, int offset, double value) {
345        ObjectAccess.writeDouble(o, Word.signed(offset), value, ID);
346    }
347
348    @Snippet
349    public static void writeDouble3(Object o, int offset, double value) {
350        ObjectAccess.writeDouble(o, offset, value);
351    }
352
353    @Snippet
354    public static Object readObject1(Object o, int offset) {
355        return ObjectAccess.readObject(o, offset, ID);
356    }
357
358    @Snippet
359    public static Object readObject2(Object o, int offset) {
360        return ObjectAccess.readObject(o, Word.signed(offset), ID);
361    }
362
363    @Snippet
364    public static Object readObject3(Object o, int offset) {
365        return ObjectAccess.readObject(o, offset);
366    }
367
368    @Snippet
369    public static void writeObject1(Object o, int offset, Object value) {
370        ObjectAccess.writeObject(o, offset, value, ID);
371    }
372
373    @Snippet
374    public static void writeObject2(Object o, int offset, Object value) {
375        ObjectAccess.writeObject(o, Word.signed(offset), value, ID);
376    }
377
378    @Snippet
379    public static void writeObject3(Object o, int offset, Object value) {
380        ObjectAccess.writeObject(o, offset, value);
381    }
382}