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.core.amd64.test;
  24 
  25 import static org.junit.Assume.assumeTrue;
  26 
  27 import org.junit.Before;
  28 import org.junit.Test;
  29 
  30 import org.graalvm.compiler.core.common.LIRKind;
  31 import org.graalvm.compiler.lir.VirtualStackSlot;
  32 import org.graalvm.compiler.lir.framemap.FrameMapBuilder;
  33 import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
  34 import org.graalvm.compiler.lir.jtt.LIRTest;
  35 import org.graalvm.compiler.lir.jtt.LIRTestSpecification;
  36 
  37 import jdk.vm.ci.amd64.AMD64;
  38 import jdk.vm.ci.amd64.AMD64Kind;
  39 import jdk.vm.ci.meta.JavaConstant;
  40 import jdk.vm.ci.meta.Value;
  41 
  42 public class StackStoreTest extends LIRTest {
  43     @Before
  44     public void checkAMD64() {
  45         assumeTrue("skipping AMD64 specific test", getTarget().arch instanceof AMD64);
  46     }
  47 
  48     private static final LIRTestSpecification stackCopy0 = new LIRTestSpecification() {
  49         @Override
  50         public void generate(LIRGeneratorTool gen, Value a) {
  51             FrameMapBuilder frameMapBuilder = gen.getResult().getFrameMapBuilder();
  52             // create slots
  53             VirtualStackSlot s1 = frameMapBuilder.allocateSpillSlot(a.getValueKind());
  54             VirtualStackSlot s2 = frameMapBuilder.allocateSpillSlot(LIRKind.value(AMD64Kind.WORD));
  55             // move stuff around
  56             gen.emitMove(s1, a);
  57             gen.emitMoveConstant(s2, JavaConstant.forShort(Short.MIN_VALUE));
  58             setResult(gen.emitMove(s1));
  59             gen.emitBlackhole(s1);
  60             gen.emitBlackhole(s2);
  61         }
  62     };
  63 
  64     private static final LIRTestSpecification stackCopy1 = new LIRTestSpecification() {
  65         @Override
  66         public void generate(LIRGeneratorTool gen, Value a) {
  67             FrameMapBuilder frameMapBuilder = gen.getResult().getFrameMapBuilder();
  68             // create slots
  69             VirtualStackSlot s1 = frameMapBuilder.allocateSpillSlot(a.getValueKind());
  70             VirtualStackSlot s2 = frameMapBuilder.allocateSpillSlot(LIRKind.value(AMD64Kind.WORD));
  71             // move stuff around
  72             gen.emitMove(s1, a);
  73             Value v = gen.emitLoadConstant(LIRKind.value(AMD64Kind.WORD), JavaConstant.forShort(Short.MIN_VALUE));
  74             gen.emitMove(s2, v);
  75             setResult(gen.emitMove(s1));
  76             gen.emitBlackhole(s1);
  77             gen.emitBlackhole(s2);
  78         }
  79     };
  80 
  81     private static final LIRTestSpecification stackCopy2 = new LIRTestSpecification() {
  82         @Override
  83         public void generate(LIRGeneratorTool gen, Value a) {
  84             FrameMapBuilder frameMapBuilder = gen.getResult().getFrameMapBuilder();
  85             // create slots
  86             VirtualStackSlot s1 = frameMapBuilder.allocateSpillSlot(a.getValueKind());
  87             VirtualStackSlot s2 = frameMapBuilder.allocateSpillSlot(LIRKind.value(AMD64Kind.WORD));
  88             // move stuff around
  89             gen.emitMoveConstant(s2, JavaConstant.forShort(Short.MIN_VALUE));
  90             gen.emitMove(s1, a);
  91             setResult(gen.emitMove(s2));
  92             gen.emitBlackhole(s1);
  93             gen.emitBlackhole(s2);
  94         }
  95     };
  96 
  97     @SuppressWarnings("unused")
  98     @LIRIntrinsic
  99     public static int testShortStackSlot(LIRTestSpecification spec, int a) {
 100         return a;
 101     }
 102 
 103     @SuppressWarnings("unused")
 104     @LIRIntrinsic
 105     public static short testShortStackSlot2(LIRTestSpecification spec, int a) {
 106         return Short.MIN_VALUE;
 107     }
 108 
 109     public int test0(int a) {
 110         return testShortStackSlot(stackCopy0, a);
 111     }
 112 
 113     @Test
 114     public void run0() throws Throwable {
 115         runTest("test0", 0xDEADDEAD);
 116     }
 117 
 118     public int test1(int a) {
 119         return testShortStackSlot(stackCopy1, a);
 120     }
 121 
 122     @Test
 123     public void run1() throws Throwable {
 124         runTest("test1", 0xDEADDEAD);
 125     }
 126 
 127     public int test2(int a) {
 128         return testShortStackSlot2(stackCopy2, a);
 129     }
 130 
 131     @Test
 132     public void run2() throws Throwable {
 133         runTest("test2", 0xDEADDEAD);
 134     }
 135 
 136 }