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