1 /*
   2  * Copyright (c) 2015, 2016, 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 
  24 /**
  25  * @test
  26  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
  27  * @library /
  28  * @modules jdk.vm.ci/jdk.vm.ci.hotspot
  29  *          jdk.vm.ci/jdk.vm.ci.meta
  30  *          jdk.vm.ci/jdk.vm.ci.code
  31  *          jdk.vm.ci/jdk.vm.ci.code.site
  32  *          jdk.vm.ci/jdk.vm.ci.runtime
  33  *          jdk.vm.ci/jdk.vm.ci.amd64
  34  *          jdk.vm.ci/jdk.vm.ci.sparc
  35  * @compile CodeInstallationTest.java DebugInfoTest.java TestAssembler.java amd64/AMD64TestAssembler.java sparc/SPARCTestAssembler.java
  36  * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI jdk.vm.ci.code.test.SimpleDebugInfoTest
  37  */
  38 
  39 package jdk.vm.ci.code.test;
  40 
  41 import org.junit.Assume;
  42 import org.junit.Test;
  43 
  44 import jdk.vm.ci.code.Register;
  45 import jdk.vm.ci.hotspot.HotSpotConstant;
  46 import jdk.vm.ci.hotspot.HotSpotVMConfig;
  47 import jdk.vm.ci.meta.JavaConstant;
  48 import jdk.vm.ci.meta.JavaKind;
  49 import jdk.vm.ci.meta.ResolvedJavaType;
  50 import jdk.vm.ci.meta.Value;
  51 
  52 public class SimpleDebugInfoTest extends DebugInfoTest {
  53 
  54     public static int intOnStack() {
  55         return 42;
  56     }
  57 
  58     private void testIntOnStack(DebugInfoCompiler compiler) {
  59         test(compiler, getMethod("intOnStack"), 2, JavaKind.Int);
  60     }
  61 
  62     public static int intInLocal() {
  63         int local = 42;
  64         return local;
  65     }
  66 
  67     public void testIntInLocal(DebugInfoCompiler compiler) {
  68         test(compiler, getMethod("intInLocal"), 3, JavaKind.Int);
  69     }
  70 
  71     @Test
  72     public void testConstInt() {
  73         DebugInfoCompiler compiler = (asm, values) -> {
  74             values[0] = JavaConstant.forInt(42);
  75             return null;
  76         };
  77         testIntOnStack(compiler);
  78         testIntInLocal(compiler);
  79     }
  80 
  81     @Test
  82     public void testRegInt() {
  83         DebugInfoCompiler compiler = (asm, values) -> {
  84             Register reg = asm.emitLoadInt(42);
  85             values[0] = reg.asValue(asm.getValueKind(JavaKind.Int));
  86             return null;
  87         };
  88         testIntOnStack(compiler);
  89         testIntInLocal(compiler);
  90     }
  91 
  92     @Test
  93     public void testStackInt() {
  94         DebugInfoCompiler compiler = (asm, values) -> {
  95             Register reg = asm.emitLoadInt(42);
  96             values[0] = asm.emitIntToStack(reg);
  97             return null;
  98         };
  99         testIntOnStack(compiler);
 100         testIntInLocal(compiler);
 101     }
 102 
 103     public static float floatOnStack() {
 104         return 42.0f;
 105     }
 106 
 107     private void testFloatOnStack(DebugInfoCompiler compiler) {
 108         test(compiler, getMethod("floatOnStack"), 2, JavaKind.Float);
 109     }
 110 
 111     public static float floatInLocal() {
 112         float local = 42.0f;
 113         return local;
 114     }
 115 
 116     private void testFloatInLocal(DebugInfoCompiler compiler) {
 117         test(compiler, getMethod("floatInLocal"), 3, JavaKind.Float);
 118     }
 119 
 120     @Test
 121     public void testConstFloat() {
 122         DebugInfoCompiler compiler = (asm, values) -> {
 123             values[0] = JavaConstant.forFloat(42.0f);
 124             return null;
 125         };
 126         testFloatOnStack(compiler);
 127         testFloatInLocal(compiler);
 128     }
 129 
 130     @Test
 131     public void testRegFloat() {
 132         DebugInfoCompiler compiler = (asm, values) -> {
 133             Register reg = asm.emitLoadFloat(42.0f);
 134             values[0] = reg.asValue(asm.getValueKind(JavaKind.Float));
 135             return null;
 136         };
 137         testFloatOnStack(compiler);
 138         testFloatInLocal(compiler);
 139     }
 140 
 141     @Test
 142     public void testStackFloat() {
 143         DebugInfoCompiler compiler = (asm, values) -> {
 144             Register reg = asm.emitLoadFloat(42.0f);
 145             values[0] = asm.emitFloatToStack(reg);
 146             return null;
 147         };
 148         testFloatOnStack(compiler);
 149         testFloatInLocal(compiler);
 150     }
 151 
 152     public static long longOnStack() {
 153         return 42;
 154     }
 155 
 156     private void testLongOnStack(DebugInfoCompiler compiler) {
 157         test(compiler, getMethod("longOnStack"), 3, JavaKind.Long, JavaKind.Illegal);
 158     }
 159 
 160     public static long longInLocal() {
 161         long local = 42;
 162         return local;
 163     }
 164 
 165     private void testLongInLocal(DebugInfoCompiler compiler) {
 166         test(compiler, getMethod("longInLocal"), 4, JavaKind.Long, JavaKind.Illegal);
 167     }
 168 
 169     @Test
 170     public void testConstLong() {
 171         DebugInfoCompiler compiler = (asm, values) -> {
 172             values[0] = JavaConstant.forLong(42);
 173             values[1] = Value.ILLEGAL;
 174             return null;
 175         };
 176         testLongOnStack(compiler);
 177         testLongInLocal(compiler);
 178     }
 179 
 180     @Test
 181     public void testRegLong() {
 182         DebugInfoCompiler compiler = (asm, values) -> {
 183             Register reg = asm.emitLoadLong(42);
 184             values[0] = reg.asValue(asm.getValueKind(JavaKind.Long));
 185             values[1] = Value.ILLEGAL;
 186             return null;
 187         };
 188         testLongOnStack(compiler);
 189         testLongInLocal(compiler);
 190     }
 191 
 192     @Test
 193     public void testStackLong() {
 194         DebugInfoCompiler compiler = (asm, values) -> {
 195             Register reg = asm.emitLoadLong(42);
 196             values[0] = asm.emitLongToStack(reg);
 197             values[1] = Value.ILLEGAL;
 198             return null;
 199         };
 200         testLongOnStack(compiler);
 201         testLongInLocal(compiler);
 202     }
 203 
 204     public static Class<?> objectOnStack() {
 205         return SimpleDebugInfoTest.class;
 206     }
 207 
 208     private void testObjectOnStack(DebugInfoCompiler compiler) {
 209         test(compiler, getMethod("objectOnStack"), 2, JavaKind.Object);
 210     }
 211 
 212     public static Class<?> objectInLocal() {
 213         Class<?> local = SimpleDebugInfoTest.class;
 214         return local;
 215     }
 216 
 217     private void testObjectInLocal(DebugInfoCompiler compiler) {
 218         test(compiler, getMethod("objectInLocal"), 3, JavaKind.Object);
 219     }
 220 
 221     @Test
 222     public void testConstObject() {
 223         ResolvedJavaType type = metaAccess.lookupJavaType(objectOnStack());
 224         DebugInfoCompiler compiler = (asm, values) -> {
 225             values[0] = constantReflection.asJavaClass(type);
 226             return null;
 227         };
 228         testObjectOnStack(compiler);
 229         testObjectInLocal(compiler);
 230     }
 231 
 232     @Test
 233     public void testRegObject() {
 234         ResolvedJavaType type = metaAccess.lookupJavaType(objectOnStack());
 235         DebugInfoCompiler compiler = (asm, values) -> {
 236             Register reg = asm.emitLoadPointer((HotSpotConstant) constantReflection.asJavaClass(type));
 237             values[0] = reg.asValue(asm.getValueKind(JavaKind.Object));
 238             return null;
 239         };
 240         testObjectOnStack(compiler);
 241         testObjectInLocal(compiler);
 242     }
 243 
 244     @Test
 245     public void testStackObject() {
 246         ResolvedJavaType type = metaAccess.lookupJavaType(objectOnStack());
 247         DebugInfoCompiler compiler = (asm, values) -> {
 248             Register reg = asm.emitLoadPointer((HotSpotConstant) constantReflection.asJavaClass(type));
 249             values[0] = asm.emitPointerToStack(reg);
 250             return null;
 251         };
 252         testObjectOnStack(compiler);
 253         testObjectInLocal(compiler);
 254     }
 255 
 256     @Test
 257     public void testRegNarrowObject() {
 258         Assume.assumeTrue(HotSpotVMConfig.config().useCompressedOops);
 259         ResolvedJavaType type = metaAccess.lookupJavaType(objectOnStack());
 260         DebugInfoCompiler compiler = (asm, values) -> {
 261             HotSpotConstant wide = (HotSpotConstant) constantReflection.asJavaClass(type);
 262             Register reg = asm.emitLoadPointer((HotSpotConstant) wide.compress());
 263             values[0] = reg.asValue(asm.narrowOopKind);
 264             return null;
 265         };
 266         testObjectOnStack(compiler);
 267         testObjectInLocal(compiler);
 268     }
 269 
 270     @Test
 271     public void testStackNarrowObject() {
 272         Assume.assumeTrue(HotSpotVMConfig.config().useCompressedOops);
 273         ResolvedJavaType type = metaAccess.lookupJavaType(objectOnStack());
 274         DebugInfoCompiler compiler = (asm, values) -> {
 275             HotSpotConstant wide = (HotSpotConstant) constantReflection.asJavaClass(type);
 276             Register reg = asm.emitLoadPointer((HotSpotConstant) wide.compress());
 277             values[0] = asm.emitNarrowPointerToStack(reg);
 278             return null;
 279         };
 280         testObjectOnStack(compiler);
 281         testObjectInLocal(compiler);
 282     }
 283 }