1 /*
   2  * Copyright (c) 2015, 2015, 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.lir.jtt;
  24 
  25 import static org.graalvm.compiler.lir.LIRValueUtil.asJavaConstant;
  26 import static org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant;
  27 
  28 import org.junit.Before;
  29 import org.junit.Test;
  30 
  31 import org.graalvm.compiler.core.common.LIRKind;
  32 import org.graalvm.compiler.debug.GraalError;
  33 import org.graalvm.compiler.lir.ConstantValue;
  34 import org.graalvm.compiler.lir.VirtualStackSlot;
  35 import org.graalvm.compiler.lir.framemap.FrameMapBuilder;
  36 import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
  37 
  38 import jdk.vm.ci.meta.JavaConstant;
  39 import jdk.vm.ci.meta.JavaKind;
  40 import jdk.vm.ci.meta.PlatformKind;
  41 import jdk.vm.ci.meta.Value;
  42 
  43 /**
  44  * Tests move from a constant to a wider stack slot (e.g. byte constant to integer stack slot).
  45  */
  46 public class ConstantStackCastTest extends LIRTest {
  47     private static PlatformKind byteKind;
  48     private static final LoadConstantStackSpec stackCopyByte = new LoadConstantStackSpec();
  49 
  50     @Before
  51     public void setup() {
  52         // Necessary to get the PlatformKind on which we're currently running on
  53         byteKind = getBackend().getTarget().arch.getPlatformKind(JavaKind.Byte);
  54         stackCopyByte.dstKind = LIRKind.fromJavaKind(getBackend().getTarget().arch, JavaKind.Int);
  55         stackCopyByte.srcKind = LIRKind.fromJavaKind(getBackend().getTarget().arch, JavaKind.Byte);
  56     }
  57 
  58     private static class LoadConstantStackSpec extends LIRTestSpecification {
  59         LIRKind dstKind;
  60         LIRKind srcKind;
  61 
  62         @Override
  63         public void generate(LIRGeneratorTool gen, Value value) {
  64             FrameMapBuilder frameMapBuilder = gen.getResult().getFrameMapBuilder();
  65             // create slots
  66             VirtualStackSlot s1 = frameMapBuilder.allocateSpillSlot(dstKind);
  67             // move stuff around
  68             Value srcValue;
  69             if (isJavaConstant(value)) {
  70                 srcValue = getConstant(srcKind, asJavaConstant(value));
  71             } else {
  72                 srcValue = value;
  73             }
  74             gen.emitMove(s1, srcValue);
  75             gen.emitBlackhole(s1);
  76             setResult(gen.emitMove(s1));
  77         }
  78 
  79         private static ConstantValue getConstant(LIRKind srcKind, JavaConstant c) {
  80             if (srcKind.getPlatformKind() == byteKind) {
  81                 JavaConstant byteConst = JavaConstant.forByte((byte) c.asInt());
  82                 return new ConstantValue(srcKind, byteConst);
  83             } else {
  84                 throw GraalError.shouldNotReachHere("Kind not supported: " + srcKind);
  85             }
  86         }
  87 
  88     }
  89 
  90     @LIRIntrinsic
  91     public static byte testCopyByte(@SuppressWarnings("unused") LoadConstantStackSpec spec, byte value) {
  92         return value;
  93     }
  94 
  95     public byte testByte(byte value) {
  96         return testCopyByte(stackCopyByte, value);
  97     }
  98 
  99     @Test
 100     public void runByte() throws Throwable {
 101         runTest("testByte", (byte) 0);
 102     }
 103 
 104 }