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