Path: blob/master/test/hotspot/jtreg/compiler/jvmci/common/CTVMUtilities.java
41155 views
/*1* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package compiler.jvmci.common;2425import jdk.internal.org.objectweb.asm.ClassReader;26import jdk.internal.org.objectweb.asm.ClassVisitor;27import jdk.internal.org.objectweb.asm.ClassWriter;28import jdk.internal.org.objectweb.asm.Label;29import jdk.internal.org.objectweb.asm.MethodVisitor;30import jdk.internal.org.objectweb.asm.Opcodes;31import jdk.internal.org.objectweb.asm.tree.ClassNode;32import jdk.test.lib.Utils;33import jdk.vm.ci.code.InstalledCode;34import jdk.vm.ci.meta.ResolvedJavaMethod;35import jdk.vm.ci.hotspot.CompilerToVMHelper;36import jdk.vm.ci.hotspot.HotSpotNmethod;37import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;3839import java.io.IOException;40import java.lang.reflect.Constructor;41import java.lang.reflect.Executable;42import java.lang.reflect.Field;43import java.lang.reflect.Method;44import java.lang.reflect.Modifier;45import java.lang.reflect.Parameter;46import java.util.HashMap;47import java.util.Map;48import java.util.TreeMap;4950public class CTVMUtilities {51/*52* A method to return HotSpotResolvedJavaMethod object using class object53* and method as input54*/55public static HotSpotResolvedJavaMethod getResolvedMethod(Class<?> cls,56Executable method) {57if (!(method instanceof Method || method instanceof Constructor)) {58throw new Error("wrong executable type " + method.getClass());59}60return CompilerToVMHelper.asResolvedJavaMethod(method);61}6263public static HotSpotResolvedJavaMethod getResolvedMethod(64Executable method) {65return getResolvedMethod(method.getDeclaringClass(), method);66}6768public static InstalledCode getInstalledCode(ResolvedJavaMethod method, String name, long address, long entryPoint) {69return CompilerToVMHelper.getInstalledCode(method, name, address, entryPoint);70}7172public static Map<Integer, Integer> getBciToLineNumber(Executable method) {73Map<Integer, Integer> lineNumbers = new TreeMap<>();74Class<?> aClass = method.getDeclaringClass();75ClassReader cr;76try {77Module aModule = aClass.getModule();78String name = aClass.getName();79cr = new ClassReader(aModule.getResourceAsStream(80name.replace('.', '/') + ".class"));81} catch (IOException e) {82throw new Error("TEST BUG: can read " + aClass.getName() + " : " + e, e);83}84ClassNode cn = new ClassNode();85cr.accept(cn, ClassReader.EXPAND_FRAMES);8687Map<Label, Integer> labels = new HashMap<>();88ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);89ClassVisitor cv = new ClassVisitorForLabels(cw, labels, method);90cr.accept(cv, ClassReader.EXPAND_FRAMES);91labels.forEach((k, v) -> lineNumbers.put(k.getOffset(), v));92boolean isEmptyMethod = Modifier.isAbstract(method.getModifiers())93|| Modifier.isNative(method.getModifiers());94if (lineNumbers.isEmpty() && !isEmptyMethod) {95throw new Error(method + " doesn't contains the line numbers table "96+"(the method marked neither abstract nor native)");97}98return lineNumbers;99}100101private static class ClassVisitorForLabels extends ClassVisitor {102private final Map<Label, Integer> lineNumbers;103private final String targetName;104private final String targetDesc;105106public ClassVisitorForLabels(ClassWriter cw, Map<Label, Integer> lines,107Executable target) {108super(Opcodes.ASM7, cw);109this.lineNumbers = lines;110111StringBuilder builder = new StringBuilder("(");112for (Parameter parameter : target.getParameters()) {113builder.append(Utils.toJVMTypeSignature(parameter.getType()));114}115builder.append(")");116if (target instanceof Constructor) {117targetName = "<init>";118builder.append("V");119} else {120targetName = target.getName();121builder.append(Utils.toJVMTypeSignature(122((Method) target).getReturnType()));123}124targetDesc = builder.toString();125}126127@Override128public final MethodVisitor visitMethod(int access, String name,129String desc, String signature,130String[] exceptions) {131MethodVisitor mv = cv.visitMethod(access, name, desc, signature,132exceptions);133if (targetDesc.equals(desc) && targetName.equals(name)) {134return new MethodVisitor(Opcodes.ASM7, mv) {135@Override136public void visitLineNumber(int i, Label label) {137super.visitLineNumber(i, label);138lineNumbers.put(label, i);139}140};141}142return mv;143}144}145}146147148