001/*
002 * Copyright (c) 2015, 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.compiler.amd64.test;
024
025import jdk.internal.jvmci.amd64.*;
026import jdk.internal.jvmci.code.*;
027import jdk.internal.jvmci.meta.*;
028import static org.junit.Assume.*;
029
030import org.junit.*;
031
032import com.oracle.graal.lir.framemap.*;
033import com.oracle.graal.lir.gen.*;
034import com.oracle.graal.lir.jtt.*;
035
036public class ConstantStackMoveTest extends LIRTest {
037    @Before
038    public void checkAMD64() {
039        assumeTrue("skipping AMD64 specific test", getTarget().arch instanceof AMD64);
040    }
041
042    private static class LoadConstantStackSpec extends LIRTestSpecification {
043        protected final Object primitive;
044
045        public LoadConstantStackSpec(Object primitive) {
046            this.primitive = primitive;
047        }
048
049        @Override
050        public void generate(LIRGeneratorTool gen) {
051            FrameMapBuilder frameMapBuilder = gen.getResult().getFrameMapBuilder();
052            // create slots
053            PrimitiveConstant constantValue = JavaConstant.forBoxedPrimitive(primitive);
054            StackSlotValue s1 = frameMapBuilder.allocateSpillSlot(constantValue.getLIRKind());
055            // move stuff around
056            gen.emitMove(s1, constantValue);
057            gen.emitBlackhole(s1);
058            setResult(gen.emitMove(s1));
059        }
060    }
061
062    private static final class LoadConstantStackSpecByte extends LoadConstantStackSpec {
063        public LoadConstantStackSpecByte(byte primitive) {
064            super(primitive);
065        }
066
067        byte get() {
068            return (Byte) primitive;
069        }
070    }
071
072    private static final class LoadConstantStackSpecShort extends LoadConstantStackSpec {
073        public LoadConstantStackSpecShort(short primitive) {
074            super(primitive);
075        }
076
077        short get() {
078            return (Short) primitive;
079        }
080    }
081
082    private static final class LoadConstantStackSpecInteger extends LoadConstantStackSpec {
083        public LoadConstantStackSpecInteger(int primitive) {
084            super(primitive);
085        }
086
087        int get() {
088            return (Integer) primitive;
089        }
090    }
091
092    private static final class LoadConstantStackSpecLong extends LoadConstantStackSpec {
093        public LoadConstantStackSpecLong(long primitive) {
094            super(primitive);
095        }
096
097        long get() {
098            return (Long) primitive;
099        }
100    }
101
102    private static final class LoadConstantStackSpecFloat extends LoadConstantStackSpec {
103        public LoadConstantStackSpecFloat(float primitive) {
104            super(primitive);
105        }
106
107        float get() {
108            return (Float) primitive;
109        }
110    }
111
112    private static final class LoadConstantStackSpecDouble extends LoadConstantStackSpec {
113        public LoadConstantStackSpecDouble(double primitive) {
114            super(primitive);
115        }
116
117        double get() {
118            return (Double) primitive;
119        }
120    }
121
122    private static final LoadConstantStackSpecByte stackCopyByte = new LoadConstantStackSpecByte(Byte.MAX_VALUE);
123    private static final LoadConstantStackSpecShort stackCopyShort = new LoadConstantStackSpecShort(Short.MAX_VALUE);
124    private static final LoadConstantStackSpecInteger stackCopyInt = new LoadConstantStackSpecInteger(Integer.MAX_VALUE);
125    private static final LoadConstantStackSpecLong stackCopyLong = new LoadConstantStackSpecLong(Long.MAX_VALUE);
126    private static final LoadConstantStackSpecFloat stackCopyFloat = new LoadConstantStackSpecFloat(Float.MAX_VALUE);
127    private static final LoadConstantStackSpecDouble stackCopyDouble = new LoadConstantStackSpecDouble(Double.MAX_VALUE);
128
129    @LIRIntrinsic
130    public static byte testCopyByte(LoadConstantStackSpecByte spec) {
131        return spec.get();
132    }
133
134    public byte testByte() {
135        return testCopyByte(stackCopyByte);
136    }
137
138    @Test
139    public void runByte() throws Throwable {
140        runTest("testByte");
141    }
142
143    @LIRIntrinsic
144    public static short testCopyShort(LoadConstantStackSpecShort spec) {
145        return spec.get();
146    }
147
148    public short testShort() {
149        return testCopyShort(stackCopyShort);
150    }
151
152    @Test
153    public void runShort() throws Throwable {
154        runTest("testShort");
155    }
156
157    @LIRIntrinsic
158    public static int testCopyInt(LoadConstantStackSpecInteger spec) {
159        return spec.get();
160    }
161
162    public int testInt() {
163        return testCopyInt(stackCopyInt);
164    }
165
166    @Test
167    public void runInt() throws Throwable {
168        runTest("testInt");
169    }
170
171    @LIRIntrinsic
172    public static long testCopyLong(LoadConstantStackSpecLong spec) {
173        return spec.get();
174    }
175
176    public long testLong() {
177        return testCopyLong(stackCopyLong);
178    }
179
180    @Test
181    public void runLong() throws Throwable {
182        runTest("testLong");
183    }
184
185    @LIRIntrinsic
186    public static float testCopyFloat(LoadConstantStackSpecFloat spec) {
187        return spec.get();
188    }
189
190    public float testFloat() {
191        return testCopyFloat(stackCopyFloat);
192    }
193
194    @Test
195    public void runFloat() throws Throwable {
196        runTest("testFloat");
197    }
198
199    @LIRIntrinsic
200    public static double testCopyDouble(LoadConstantStackSpecDouble spec) {
201        return spec.get();
202    }
203
204    public double testDouble() {
205        return testCopyDouble(stackCopyDouble);
206    }
207
208    @Test
209    public void runDouble() throws Throwable {
210        runTest("testDouble");
211    }
212
213}