1 /* 2 * Copyright (c) 2017, 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 package org.graalvm.compiler.core.test; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.junit.runners.Parameterized; 33 import org.junit.runners.Parameterized.Parameters; 34 import org.objectweb.asm.ClassWriter; 35 import org.objectweb.asm.FieldVisitor; 36 import org.objectweb.asm.MethodVisitor; 37 38 import jdk.vm.ci.meta.JavaKind; 39 import jdk.vm.ci.meta.ResolvedJavaMethod; 40 41 @RunWith(Parameterized.class) 42 public class SubWordReturnTest extends CustomizedBytecodePatternTest { 43 44 private final JavaKind kind; 45 private final int value; 46 47 @Parameters(name = "{0}, {1}") data()48 public static List<Object[]> data() { 49 ArrayList<Object[]> ret = new ArrayList<>(); 50 for (int i : new int[]{1000000, 1000001, -1000000, -1}) { 51 ret.add(new Object[]{JavaKind.Boolean, i}); 52 ret.add(new Object[]{JavaKind.Byte, i}); 53 ret.add(new Object[]{JavaKind.Short, i}); 54 ret.add(new Object[]{JavaKind.Char, i}); 55 } 56 return ret; 57 } 58 SubWordReturnTest(JavaKind kind, int value)59 public SubWordReturnTest(JavaKind kind, int value) { 60 this.kind = kind; 61 this.value = value; 62 } 63 64 @Test testSubWordReturn()65 public void testSubWordReturn() throws ClassNotFoundException { 66 Class<?> testClass = getClass(SubWordReturnTest.class.getName() + "$" + kind.toString() + "Getter"); 67 ResolvedJavaMethod method = getResolvedJavaMethod(testClass, "testSnippet"); 68 test(method, null); 69 } 70 71 /** 72 * {@link #generateClass} generates a class looking like this for the types boolean, byte, 73 * short, and char. 74 */ 75 static class ByteGetter { 76 77 // private static int intField = 1000000; 78 get()79 private static byte get() { 80 // GETSTATIC intField 81 // IRETURN 82 return 0; 83 } 84 testByteSnippet()85 public static int testByteSnippet() { 86 return get(); 87 } 88 } 89 90 @Override generateClass(String internalClassName)91 protected byte[] generateClass(String internalClassName) { 92 ClassWriter cw = new ClassWriter(0); 93 cw.visit(52, ACC_SUPER | ACC_PUBLIC, internalClassName, null, "java/lang/Object", null); 94 95 FieldVisitor intField = cw.visitField(ACC_PRIVATE | ACC_STATIC, "intField", "I", null, value); 96 intField.visitEnd(); 97 98 MethodVisitor get = cw.visitMethod(ACC_PRIVATE | ACC_STATIC, "get", "()" + kind.getTypeChar(), null, null); 99 get.visitCode(); 100 get.visitFieldInsn(GETSTATIC, internalClassName, "intField", "I"); 101 get.visitInsn(IRETURN); 102 get.visitMaxs(1, 0); 103 get.visitEnd(); 104 105 MethodVisitor snippet = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "testSnippet", "()I", null, null); 106 snippet.visitCode(); 107 snippet.visitMethodInsn(INVOKESTATIC, internalClassName, "get", "()" + kind.getTypeChar(), false); 108 snippet.visitInsn(IRETURN); 109 snippet.visitMaxs(1, 0); 110 snippet.visitEnd(); 111 112 cw.visitEnd(); 113 return cw.toByteArray(); 114 } 115 116 } 117