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         Field slotField;
  61         int slot;
  62         try {
  63             slotField = method.getClass().getDeclaredField("slot");
  64             boolean old = slotField.isAccessible();
  65             slotField.setAccessible(true);
  66             slot = slotField.getInt(method);
  67             slotField.setAccessible(old);
  68         } catch (ReflectiveOperationException e) {
  69             throw new Error("TEST BUG: Can't get slot field", e);
  70         }
  71         return CompilerToVMHelper.getResolvedJavaMethodAtSlot(cls, slot);
  72     }
  73 
  74     public static HotSpotResolvedJavaMethod getResolvedMethod(
  75             Executable method) {
  76         return getResolvedMethod(method.getDeclaringClass(), method);
  77     }
  78 
  79     public static InstalledCode getInstalledCode(String name, long address,
  80             long entryPoint) {
  81         return new InstalledCodeStub(name, address, entryPoint);
  82     }
  83     private static class InstalledCodeStub extends InstalledCode {
  84         private InstalledCodeStub(String name, long address, long entryPoint) {
  85             super(name);
  86             this.address = address;
  87             this.entryPoint = entryPoint;
  88         }
  89     }
  90     public static Map<Integer, Integer> getBciToLineNumber(Executable method) {
  91         Map<Integer, Integer> lineNumbers = new TreeMap<>();
  92         Class<?> aClass = method.getDeclaringClass();
  93         ClassReader cr;
  94         try {
  95             Module aModule = aClass.getModule();
  96             String name = aClass.getName();
  97             cr = new ClassReader(aModule.getResourceAsStream(
  98                     name.replace('.', '/') + ".class"));
  99         } catch (IOException e) {
 100                         throw new Error("TEST BUG: can read " + aClass.getName() + " : " + e, e);
 101         }
 102         ClassNode cn = new ClassNode();
 103         cr.accept(cn, ClassReader.EXPAND_FRAMES);
 104 
 105         Map<Label, Integer> labels = new HashMap<>();
 106         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
 107         ClassVisitor cv = new ClassVisitorForLabels(cw, labels, method);
 108         cr.accept(cv, ClassReader.EXPAND_FRAMES);
 109         labels.forEach((k, v) -> lineNumbers.put(k.getOffset(), v));
 110         boolean isEmptyMethod = Modifier.isAbstract(method.getModifiers())
 111                 || Modifier.isNative(method.getModifiers());
 112         if (lineNumbers.isEmpty() && !isEmptyMethod) {
 113             throw new Error(method + " doesn't contains the line numbers table "
 114                     +"(the method marked neither abstract nor native)");
 115         }
 116         return lineNumbers;
 117     }
 118 
 119     private static class ClassVisitorForLabels extends ClassVisitor {
 120         private final Map<Label, Integer> lineNumbers;
 121         private final String targetName;
 122         private final String targetDesc;
 123 
 124         public ClassVisitorForLabels(ClassWriter cw, Map<Label, Integer> lines,
 125                                      Executable target) {
 126             super(Opcodes.ASM5, cw);
 127             this.lineNumbers = lines;
 128 
 129             StringBuilder builder = new StringBuilder("(");
 130             for (Parameter parameter : target.getParameters()) {
 131                 builder.append(Utils.toJVMTypeSignature(parameter.getType()));
 132             }
 133             builder.append(")");
 134             if (target instanceof Constructor) {
 135                 targetName = "<init>";
 136                 builder.append("V");
 137             } else {
 138                 targetName = target.getName();
 139                 builder.append(Utils.toJVMTypeSignature(
 140                         ((Method) target).getReturnType()));
 141             }
 142             targetDesc = builder.toString();
 143         }
 144 
 145         @Override
 146         public final MethodVisitor visitMethod(int access, String name,
 147                                                String desc, String signature,
 148                                                String[] exceptions) {
 149             MethodVisitor mv = cv.visitMethod(access, name, desc, signature,
 150                     exceptions);
 151             if (targetDesc.equals(desc) && targetName.equals(name)) {
 152                 return new MethodVisitor(Opcodes.ASM5, mv) {
 153                     @Override
 154                     public void visitLineNumber(int i, Label label) {
 155                         super.visitLineNumber(i, label);
 156                         lineNumbers.put(label, i);
 157                     }
 158                 };
 159             }
 160             return  mv;
 161         }
 162     }
 163 }