< prev index next >

test/compiler/jvmci/compilerToVM/GetLineNumberTableTest.java

Print this page
rev 9261 : 8138815: improve tests CompilerToVM::getStackTraceElement


  23  */
  24 
  25 /**
  26  * @test
  27  * @bug 8136421
  28  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
  29  * @library /testlibrary /../../test/lib /
  30  * @compile ../common/CompilerToVMHelper.java
  31  * @run main ClassFileInstaller jdk.vm.ci.hotspot.CompilerToVMHelper
  32  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  33  *      -Xbootclasspath/a:.
  34  *      compiler.jvmci.compilerToVM.GetLineNumberTableTest
  35  */
  36 
  37 package compiler.jvmci.compilerToVM;
  38 
  39 import compiler.jvmci.common.CTVMUtilities;
  40 import compiler.jvmci.common.testcases.TestCase;
  41 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  42 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  43 import jdk.internal.org.objectweb.asm.ClassReader;
  44 import jdk.internal.org.objectweb.asm.ClassVisitor;
  45 import jdk.internal.org.objectweb.asm.ClassWriter;
  46 import jdk.internal.org.objectweb.asm.Label;
  47 import jdk.internal.org.objectweb.asm.MethodVisitor;
  48 import jdk.internal.org.objectweb.asm.Opcodes;
  49 import jdk.internal.org.objectweb.asm.tree.ClassNode;
  50 import jdk.test.lib.Asserts;
  51 import jdk.test.lib.Utils;
  52 
  53 import java.io.IOException;
  54 import java.lang.reflect.Constructor;
  55 import java.lang.reflect.Executable;
  56 import java.lang.reflect.Method;
  57 import java.lang.reflect.Parameter;
  58 import java.util.Arrays;
  59 import java.util.HashMap;
  60 import java.util.Map;
  61 import java.util.TreeMap;
  62 
  63 public class GetLineNumberTableTest {
  64     public static void main(String[] args) {
  65         TestCase.getAllExecutables()
  66                 .forEach(GetLineNumberTableTest::runSanityTest);
  67     }
  68 
  69     public static void runSanityTest(Executable aMethod) {
  70         HotSpotResolvedJavaMethod method = CTVMUtilities
  71                 .getResolvedMethod(aMethod);
  72         long[] lineNumbers = CompilerToVMHelper.getLineNumberTable(method);
  73         long[] expectedLineNumbers = getExpectedLineNumbers(aMethod);
  74 
  75         Asserts.assertTrue(Arrays.equals(lineNumbers, expectedLineNumbers),
  76                 String.format("%s : unequal table values : %n%s%n%s%n",
  77                         aMethod,
  78                         Arrays.toString(lineNumbers),
  79                         Arrays.toString(expectedLineNumbers)));
  80     }
  81 
  82     public static long[] getExpectedLineNumbers(Executable aMethod) {
  83         try {
  84             ClassReader cr = new ClassReader(aMethod.getDeclaringClass()
  85                     .getName());
  86             ClassNode cn = new ClassNode();
  87             cr.accept(cn, ClassReader.EXPAND_FRAMES);
  88 
  89             Map<Label, Integer> lineNumbers = new HashMap<>();
  90             ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
  91             ClassVisitor cv = new ClassVisitorForLabels(cw, lineNumbers,
  92                     aMethod);
  93             cr.accept(cv, ClassReader.EXPAND_FRAMES);
  94 
  95             long[] result = null;
  96             if (!lineNumbers.isEmpty()) {
  97                 Map<Integer, Integer> labels = new TreeMap<>();
  98                 lineNumbers.forEach((k, v) -> labels.put(k.getOffset(), v));
  99 
 100                 result = new long[2 * labels.size()];
 101                 int i = 0;
 102                 for (Integer key : labels.keySet()) {
 103                     result[i++] = key.longValue();
 104                     result[i++] = labels.get(key).longValue();
 105                 }
 106             }
 107             // compilerToVM::getLineNumberTable returns null in case empty table
 108             return result;
 109         } catch (IOException e) {
 110             throw new Error("TEST BUG " + e, e);
 111         }
 112     }
 113 
 114     private static class ClassVisitorForLabels extends ClassVisitor {
 115         private final Map<Label, Integer> lineNumbers;
 116         private final String targetName;
 117         private final String targetDesc;
 118 
 119         public ClassVisitorForLabels(ClassWriter cw, Map<Label, Integer> lines,
 120                                      Executable target) {
 121             super(Opcodes.ASM5, cw);
 122             this.lineNumbers = lines;
 123 
 124             StringBuilder builder = new StringBuilder("(");
 125             for (Parameter parameter : target.getParameters()) {
 126                 builder.append(Utils.toJVMTypeSignature(parameter.getType()));
 127             }
 128             builder.append(")");
 129             if (target instanceof Constructor) {
 130                 targetName = "<init>";
 131                 builder.append("V");
 132             } else {
 133                 targetName = target.getName();
 134                 builder.append(Utils.toJVMTypeSignature(
 135                         ((Method) target).getReturnType()));
 136             }
 137             targetDesc = builder.toString();
 138         }
 139 
 140         @Override
 141         public final MethodVisitor visitMethod(int access, String name,
 142                                                String desc, String signature,
 143                                                String[] exceptions) {
 144             MethodVisitor mv = cv.visitMethod(access, name, desc, signature,
 145                     exceptions);
 146             if (targetDesc.equals(desc) && targetName.equals(name)) {
 147                 return new MethodVisitor(Opcodes.ASM5, mv) {
 148                     @Override
 149                     public void visitLineNumber(int i, Label label) {
 150                         super.visitLineNumber(i, label);
 151                         lineNumbers.put(label, i);
 152                     }
 153                 };
 154             }
 155             return  mv;
 156         }
 157     }
 158 }


  23  */
  24 
  25 /**
  26  * @test
  27  * @bug 8136421
  28  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
  29  * @library /testlibrary /../../test/lib /
  30  * @compile ../common/CompilerToVMHelper.java
  31  * @run main ClassFileInstaller jdk.vm.ci.hotspot.CompilerToVMHelper
  32  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  33  *      -Xbootclasspath/a:.
  34  *      compiler.jvmci.compilerToVM.GetLineNumberTableTest
  35  */
  36 
  37 package compiler.jvmci.compilerToVM;
  38 
  39 import compiler.jvmci.common.CTVMUtilities;
  40 import compiler.jvmci.common.testcases.TestCase;
  41 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  42 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;







  43 import jdk.test.lib.Asserts;

  44 


  45 import java.lang.reflect.Executable;


  46 import java.util.Arrays;

  47 import java.util.Map;

  48 
  49 public class GetLineNumberTableTest {
  50     public static void main(String[] args) {
  51         TestCase.getAllExecutables()
  52                 .forEach(GetLineNumberTableTest::runSanityTest);
  53     }
  54 
  55     public static void runSanityTest(Executable aMethod) {
  56         HotSpotResolvedJavaMethod method = CTVMUtilities
  57                 .getResolvedMethod(aMethod);
  58         long[] lineNumbers = CompilerToVMHelper.getLineNumberTable(method);
  59         long[] expectedLineNumbers = getExpectedLineNumbers(aMethod);
  60 
  61         Asserts.assertTrue(Arrays.equals(lineNumbers, expectedLineNumbers),
  62                 String.format("%s : unequal table values : %n%s%n%s%n",
  63                         aMethod,
  64                         Arrays.toString(lineNumbers),
  65                         Arrays.toString(expectedLineNumbers)));
  66     }
  67 
  68     public static long[] getExpectedLineNumbers(Executable aMethod) {
  69         Map<Integer, Integer> bciToLine = CTVMUtilities
  70                 .getBciToLineNumber(aMethod);










  71         long[] result = null;
  72         if (!bciToLine.isEmpty()) {
  73             result = new long[2 * bciToLine.size()];



  74             int i = 0;
  75             for (Integer key : bciToLine.keySet()) {
  76                 result[i++] = key.longValue();
  77                 result[i++] = bciToLine.get(key).longValue();
  78             }
  79         }
  80         // compilerToVM::getLineNumberTable returns null in case empty table
  81         return result;



  82     }
  83 












































  84 }
< prev index next >