1/* 2 * Copyright (c) 2015, 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 24package compiler.jvmci.common; 25 26import jdk.internal.org.objectweb.asm.ClassReader; 27import jdk.internal.org.objectweb.asm.ClassVisitor; 28import jdk.internal.org.objectweb.asm.ClassWriter; 29import jdk.internal.org.objectweb.asm.Label; 30import jdk.internal.org.objectweb.asm.MethodVisitor; 31import jdk.internal.org.objectweb.asm.Opcodes; 32import jdk.internal.org.objectweb.asm.tree.ClassNode; 33import jdk.test.lib.Utils; 34import jdk.vm.ci.code.InstalledCode; 35import jdk.vm.ci.hotspot.CompilerToVMHelper; 36import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; 37 38import java.io.IOException; 39import java.lang.reflect.Constructor; 40import java.lang.reflect.Executable; 41import java.lang.reflect.Field; 42import java.lang.reflect.Method; 43import java.lang.reflect.Modifier; 44import java.lang.reflect.Parameter; 45import java.util.HashMap; 46import java.util.Map; 47import java.util.TreeMap; 48 49public class CTVMUtilities { 50 /* 51 * A method to return HotSpotResolvedJavaMethod object using class object 52 * and method as input 53 */ 54 public static HotSpotResolvedJavaMethod getResolvedMethod(Class<?> cls, 55 Executable method) { 56 if (!(method instanceof Method || method instanceof Constructor)) { 57 throw new Error("wrong executable type " + method.getClass()); 58 } 59 return CompilerToVMHelper.asResolvedJavaMethod(method); 60 } 61 62 public static HotSpotResolvedJavaMethod getResolvedMethod( 63 Executable method) { 64 return getResolvedMethod(method.getDeclaringClass(), method); 65 } 66 67 public static InstalledCode getInstalledCode(String name, long address, 68 long entryPoint) { 69 return new InstalledCodeStub(name, address, entryPoint); 70 } 71 private static class InstalledCodeStub extends InstalledCode { 72 private InstalledCodeStub(String name, long address, long entryPoint) { 73 super(name); 74 this.address = address; 75 this.entryPoint = entryPoint; 76 } 77 } 78 public static Map<Integer, Integer> getBciToLineNumber(Executable method) { 79 Map<Integer, Integer> lineNumbers = new TreeMap<>(); 80 Class<?> aClass = method.getDeclaringClass(); 81 ClassReader cr; 82 try { 83 Module aModule = aClass.getModule(); 84 String name = aClass.getName(); 85 cr = new ClassReader(aModule.getResourceAsStream( 86 name.replace('.', '/') + ".class")); 87 } catch (IOException e) { 88 throw new Error("TEST BUG: can read " + aClass.getName() + " : " + e, e); 89 } 90 ClassNode cn = new ClassNode(); 91 cr.accept(cn, ClassReader.EXPAND_FRAMES); 92 93 Map<Label, Integer> labels = new HashMap<>(); 94 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); 95 ClassVisitor cv = new ClassVisitorForLabels(cw, labels, method); 96 cr.accept(cv, ClassReader.EXPAND_FRAMES); 97 labels.forEach((k, v) -> lineNumbers.put(k.getOffset(), v)); 98 boolean isEmptyMethod = Modifier.isAbstract(method.getModifiers()) 99 || Modifier.isNative(method.getModifiers()); 100 if (lineNumbers.isEmpty() && !isEmptyMethod) { 101 throw new Error(method + " doesn't contains the line numbers table " 102 +"(the method marked neither abstract nor native)"); 103 } 104 return lineNumbers; 105 } 106 107 private static class ClassVisitorForLabels extends ClassVisitor { 108 private final Map<Label, Integer> lineNumbers; 109 private final String targetName; 110 private final String targetDesc; 111 112 public ClassVisitorForLabels(ClassWriter cw, Map<Label, Integer> lines, 113 Executable target) { 114 super(Opcodes.ASM5, cw); 115 this.lineNumbers = lines; 116 117 StringBuilder builder = new StringBuilder("("); 118 for (Parameter parameter : target.getParameters()) { 119 builder.append(Utils.toJVMTypeSignature(parameter.getType())); 120 } 121 builder.append(")"); 122 if (target instanceof Constructor) { 123 targetName = "<init>"; 124 builder.append("V"); 125 } else { 126 targetName = target.getName(); 127 builder.append(Utils.toJVMTypeSignature( 128 ((Method) target).getReturnType())); 129 } 130 targetDesc = builder.toString(); 131 } 132 133 @Override 134 public final MethodVisitor visitMethod(int access, String name, 135 String desc, String signature, 136 String[] exceptions) { 137 MethodVisitor mv = cv.visitMethod(access, name, desc, signature, 138 exceptions); 139 if (targetDesc.equals(desc) && targetName.equals(name)) { 140 return new MethodVisitor(Opcodes.ASM5, mv) { 141 @Override 142 public void visitLineNumber(int i, Label label) { 143 super.visitLineNumber(i, label); 144 lineNumbers.put(label, i); 145 } 146 }; 147 } 148 return mv; 149 } 150 } 151} 152