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