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 java.io.IOException;
  27 import java.lang.reflect.Field;
  28 import java.lang.reflect.Executable;
  29 import java.lang.reflect.Constructor;
  30 import java.lang.reflect.Method;
  31 import java.lang.reflect.Modifier;
  32 import java.lang.reflect.Parameter;
  33 import java.util.HashMap;
  34 import java.util.Map;
  35 import java.util.TreeMap;
  36 
  37 import jdk.internal.org.objectweb.asm.ClassReader;
  38 import jdk.internal.org.objectweb.asm.ClassVisitor;
  39 import jdk.internal.org.objectweb.asm.ClassWriter;
  40 import jdk.internal.org.objectweb.asm.Label;
  41 import jdk.internal.org.objectweb.asm.MethodVisitor;
  42 import jdk.internal.org.objectweb.asm.Opcodes;
  43 import jdk.internal.org.objectweb.asm.tree.ClassNode;
  44 import jdk.test.lib.Utils;
  45 import jdk.vm.ci.code.InstalledCode;
  46 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  47 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  48 
  49 public 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         Field slotField;
  60         int slot;
  61         try {
  62             slotField = method.getClass().getDeclaredField("slot");
  63             boolean old = slotField.isAccessible();
  64             slotField.setAccessible(true);
  65             slot = slotField.getInt(method);
  66             slotField.setAccessible(old);
  67         } catch (ReflectiveOperationException e) {
  68             throw new Error("TEST BUG: Can't get slot field", e);
  69         }
  70         return CompilerToVMHelper.getResolvedJavaMethodAtSlot(cls, slot);
  71     }
  72 
  73     public static HotSpotResolvedJavaMethod getResolvedMethod(
  74             Executable method) {
  75         return getResolvedMethod(method.getDeclaringClass(), method);
  76     }
  77 
  78     public static InstalledCode getInstalledCode(String name, long address,
  79             long entryPoint) {
  80         return new InstalledCodeStub(name, address, entryPoint);
  81     }
  82     private static class InstalledCodeStub extends InstalledCode {
  83         private InstalledCodeStub(String name, long address, long entryPoint) {
  84             super(name);
  85             this.address = address;
  86             this.entryPoint = entryPoint;
  87         }
  88     }
  89     public static Map<Integer, Integer> getBciToLineNumber(Executable method) {
  90         Map<Integer, Integer> lineNumbers = new TreeMap<>();
  91         try {
  92             ClassReader cr = new ClassReader(method.getDeclaringClass()
  93                     .getName());
  94             ClassNode cn = new ClassNode();
  95             cr.accept(cn, ClassReader.EXPAND_FRAMES);
  96 
  97             Map<Label, Integer> labels = new HashMap<>();
  98             ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
  99             ClassVisitor cv = new ClassVisitorForLabels(cw, labels, method);
 100             cr.accept(cv, ClassReader.EXPAND_FRAMES);
 101             labels.forEach((k, v) -> lineNumbers.put(k.getOffset(), v));
 102         } catch (IOException e) {
 103             throw new Error("TEST BUG " + e, e);
 104         }
 105         boolean isEmptyMethod = Modifier.isAbstract(method.getModifiers())
 106                 || Modifier.isNative(method.getModifiers());
 107         if (lineNumbers.isEmpty() && !isEmptyMethod) {
 108             throw new Error(method + " doesn't contains the line numbers table "
 109                     +"(the method marked neither abstract nor native)");
 110         }
 111         return lineNumbers;
 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 }