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.lir.jtt;
024
025import static jdk.internal.jvmci.code.ValueUtil.*;
026import jdk.internal.jvmci.code.*;
027import jdk.internal.jvmci.common.*;
028import jdk.internal.jvmci.meta.*;
029
030import org.junit.*;
031
032import com.oracle.graal.lir.framemap.*;
033import com.oracle.graal.lir.gen.*;
034
035/**
036 * Tests move from a constant to a wider stack slot (e.g. byte constant to integer stack slot).
037 */
038public class ConstantStackCastTest extends LIRTest {
039
040    private static class LoadConstantStackSpec extends LIRTestSpecification {
041        protected final LIRKind dstKind;
042        protected final LIRKind srcKind;
043
044        public LoadConstantStackSpec(LIRKind dstKind, LIRKind srcKind) {
045            this.dstKind = dstKind;
046            this.srcKind = srcKind;
047        }
048
049        @Override
050        public void generate(LIRGeneratorTool gen, Value value) {
051            FrameMapBuilder frameMapBuilder = gen.getResult().getFrameMapBuilder();
052            // create slots
053            StackSlotValue s1 = frameMapBuilder.allocateSpillSlot(dstKind);
054            // move stuff around
055            Value srcValue = isConstant(value) ? getConstant(srcKind, value) : value;
056            gen.emitMove(s1, srcValue);
057            gen.emitBlackhole(s1);
058            setResult(gen.emitMove(s1));
059        }
060
061        private static PrimitiveConstant getConstant(LIRKind srcKind, Value value) {
062
063            switch ((Kind) srcKind.getPlatformKind()) {
064                case Byte:
065                    return JavaConstant.forByte((byte) asConstant(value).asInt());
066                default:
067                    throw JVMCIError.shouldNotReachHere("Kind not supported: " + srcKind);
068            }
069        }
070    }
071
072    private static final LoadConstantStackSpec stackCopyByte = new LoadConstantStackSpec(LIRKind.value(Kind.Int), LIRKind.value(Kind.Byte));
073
074    @LIRIntrinsic
075    public static byte testCopyByte(@SuppressWarnings("unused") LoadConstantStackSpec spec, byte value) {
076        return value;
077    }
078
079    public byte testByte(byte value) {
080        return testCopyByte(stackCopyByte, value);
081    }
082
083    @Test
084    public void runByte() throws Throwable {
085        runTest("testByte", (byte) 0);
086    }
087
088}