1 /*
   2  * Copyright (c) 2015, 2019, 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 jdk.vm.ci.hotspot;
  25 
  26 import jdk.vm.ci.code.InstalledCode;
  27 import jdk.vm.ci.code.InvalidInstalledCodeException;
  28 import jdk.vm.ci.code.TargetDescription;
  29 import jdk.vm.ci.code.stack.InspectedFrameVisitor;
  30 import jdk.vm.ci.meta.ConstantPool;
  31 import jdk.vm.ci.meta.JavaConstant;
  32 import jdk.vm.ci.meta.ResolvedJavaMethod;
  33 import jdk.vm.ci.meta.MetaAccessProvider;
  34 import jdk.vm.ci.runtime.JVMCI;
  35 import java.lang.reflect.Executable;
  36 
  37 /**
  38  * A simple "proxy" class to get test access to CompilerToVM package-private methods
  39  */
  40 public class CompilerToVMHelper {
  41     public static final CompilerToVM CTVM = new CompilerToVM();
  42     private static final MetaAccessProvider metaAccess = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess();
  43 
  44     public static byte[] getBytecode(HotSpotResolvedJavaMethod method) {
  45         return CTVM.getBytecode((HotSpotResolvedJavaMethodImpl)method);
  46     }
  47 
  48     public static int getExceptionTableLength(HotSpotResolvedJavaMethod method) {
  49         return CTVM.getExceptionTableLength((HotSpotResolvedJavaMethodImpl)method);
  50     }
  51 
  52     public static long getExceptionTableStart(HotSpotResolvedJavaMethod method) {
  53         return CTVM.getExceptionTableStart((HotSpotResolvedJavaMethodImpl)method);
  54     }
  55 
  56     public static Object getFlagValue(String name) {
  57         return CTVM.getFlagValue(name);
  58     }
  59 
  60     public static boolean isCompilable(HotSpotResolvedJavaMethod method) {
  61         return CTVM.isCompilable((HotSpotResolvedJavaMethodImpl)method);
  62     }
  63 
  64     public static boolean hasNeverInlineDirective(HotSpotResolvedJavaMethod method) {
  65         return CTVM.hasNeverInlineDirective((HotSpotResolvedJavaMethodImpl)method);
  66     }
  67 
  68     public static boolean shouldInlineMethod(HotSpotResolvedJavaMethod method) {
  69         return CTVM.shouldInlineMethod((HotSpotResolvedJavaMethodImpl)method);
  70     }
  71 
  72     public static HotSpotResolvedJavaMethod findUniqueConcreteMethod(
  73             HotSpotResolvedObjectType actualHolderType,
  74             HotSpotResolvedJavaMethod method) {
  75         return CTVM.findUniqueConcreteMethod((HotSpotResolvedObjectTypeImpl) actualHolderType, (HotSpotResolvedJavaMethodImpl)method);
  76     }
  77 
  78     public static HotSpotResolvedObjectType getImplementor(HotSpotResolvedObjectType type) {
  79         return CTVM.getImplementor((HotSpotResolvedObjectTypeImpl) type);
  80     }
  81 
  82     public static boolean methodIsIgnoredBySecurityStackWalk(HotSpotResolvedJavaMethod method) {
  83         return CTVM.methodIsIgnoredBySecurityStackWalk((HotSpotResolvedJavaMethodImpl)method);
  84     }
  85 
  86     public static HotSpotResolvedObjectType lookupType(String name,
  87             Class<?> accessClass, boolean resolve) throws ClassNotFoundException {
  88         if (accessClass == null) {
  89             throw new NullPointerException();
  90         }
  91         HotSpotResolvedObjectTypeImpl accessingClass = (HotSpotResolvedObjectTypeImpl) metaAccess.lookupJavaType(accessClass);
  92         return (HotSpotResolvedObjectType) CTVM.lookupType(name, accessingClass, resolve);
  93     }
  94 
  95     public static HotSpotResolvedObjectType lookupTypeHelper(String name,
  96             Class<?> accessingClass, boolean resolve) {
  97         try {
  98             return lookupType(name, accessingClass, resolve);
  99         } catch (ClassNotFoundException e) {
 100             throw (NoClassDefFoundError) new NoClassDefFoundError().initCause(e);
 101         }
 102     }
 103 
 104     public static Object resolveConstantInPool(ConstantPool constantPool, int cpi) {
 105         DirectHotSpotObjectConstantImpl obj = (DirectHotSpotObjectConstantImpl) CTVM.resolveConstantInPool((HotSpotConstantPool) constantPool, cpi);
 106         return obj.object;
 107     }
 108 
 109     public static Object resolvePossiblyCachedConstantInPool(ConstantPool constantPool, int cpi) {
 110         DirectHotSpotObjectConstantImpl obj = (DirectHotSpotObjectConstantImpl) CTVM.resolvePossiblyCachedConstantInPool((HotSpotConstantPool) constantPool, cpi);
 111         return obj.object;
 112     }
 113 
 114     public static int lookupNameAndTypeRefIndexInPool(ConstantPool constantPool, int cpi) {
 115         return CTVM.lookupNameAndTypeRefIndexInPool((HotSpotConstantPool) constantPool, cpi);
 116     }
 117 
 118     public static String lookupNameInPool(ConstantPool constantPool, int cpi) {
 119         return CTVM.lookupNameInPool((HotSpotConstantPool) constantPool, cpi);
 120     }
 121 
 122     public static String lookupSignatureInPool(ConstantPool constantPool, int cpi) {
 123         return CTVM.lookupSignatureInPool((HotSpotConstantPool) constantPool, cpi);
 124     }
 125 
 126     public static int lookupKlassRefIndexInPool(ConstantPool constantPool, int cpi) {
 127         return CTVM.lookupKlassRefIndexInPool((HotSpotConstantPool) constantPool, cpi);
 128     }
 129 
 130     public static Object lookupKlassInPool(ConstantPool constantPool, int cpi) {
 131         return CTVM.lookupKlassInPool((HotSpotConstantPool) constantPool, cpi);
 132     }
 133 
 134     public static HotSpotResolvedJavaMethod lookupMethodInPool(
 135             ConstantPool constantPool, int cpi, byte opcode) {
 136         return CTVM.lookupMethodInPool((HotSpotConstantPool) constantPool, cpi, opcode);
 137     }
 138 
 139     public static void resolveInvokeDynamicInPool(
 140             ConstantPool constantPool, int cpi) {
 141         CTVM.resolveInvokeDynamicInPool((HotSpotConstantPool) constantPool, cpi);
 142     }
 143 
 144     public static void resolveInvokeHandleInPool(
 145             ConstantPool constantPool, int cpi) {
 146         CTVM.resolveInvokeHandleInPool((HotSpotConstantPool) constantPool, cpi);
 147     }
 148 
 149     public static HotSpotResolvedObjectType resolveTypeInPool(
 150             ConstantPool constantPool, int cpi) {
 151         return CTVM.resolveTypeInPool((HotSpotConstantPool) constantPool, cpi);
 152     }
 153 
 154     public static HotSpotResolvedObjectType resolveFieldInPool(
 155             ConstantPool constantPool, int cpi, ResolvedJavaMethod method, byte opcode, int[] info) {
 156         return CTVM.resolveFieldInPool((HotSpotConstantPool) constantPool, cpi, (HotSpotResolvedJavaMethodImpl) method, opcode, info);
 157     }
 158 
 159     public static int constantPoolRemapInstructionOperandFromCache(
 160             ConstantPool constantPool, int cpci) {
 161         return CTVM.constantPoolRemapInstructionOperandFromCache((HotSpotConstantPool) constantPool, cpci);
 162     }
 163 
 164     public static Object lookupAppendixInPool(
 165             ConstantPool constantPool, int cpi) {
 166         return CTVM.lookupAppendixInPool((HotSpotConstantPool) constantPool, cpi);
 167     }
 168 
 169     public static int installCode(TargetDescription target,
 170             HotSpotCompiledCode compiledCode, InstalledCode code, HotSpotSpeculationLog speculationLog) {
 171         byte[] speculations;
 172         long failedSpeculationsAddress;
 173         if (speculationLog != null) {
 174             speculations = speculationLog.getFlattenedSpeculations(true);
 175             failedSpeculationsAddress = speculationLog.getFailedSpeculationsAddress();
 176         } else {
 177             speculations = new byte[0];
 178             failedSpeculationsAddress = 0L;
 179         }
 180         return CTVM.installCode(target, compiledCode, code, failedSpeculationsAddress, speculations);
 181     }
 182 
 183     public static int getMetadata(TargetDescription target,
 184             HotSpotCompiledCode compiledCode, HotSpotMetaData metaData) {
 185         return CTVM.getMetadata(target, compiledCode, metaData);
 186     }
 187 
 188     public static void resetCompilationStatistics() {
 189         CTVM.resetCompilationStatistics();
 190     }
 191 
 192     public static Object[] readConfiguration() {
 193         return CTVM.readConfiguration();
 194     }
 195 
 196     public static HotSpotResolvedJavaMethod resolveMethod(
 197             HotSpotResolvedObjectType exactReceiver,
 198             HotSpotResolvedJavaMethod method,
 199             HotSpotResolvedObjectType caller) {
 200         return CTVM.resolveMethod((HotSpotResolvedObjectTypeImpl) exactReceiver, (HotSpotResolvedJavaMethodImpl) method, (HotSpotResolvedObjectTypeImpl) caller);
 201     }
 202 
 203     public static HotSpotResolvedJavaMethod getClassInitializer(
 204             HotSpotResolvedObjectType type) {
 205         return CTVM.getClassInitializer((HotSpotResolvedObjectTypeImpl) type);
 206     }
 207 
 208     public static boolean hasFinalizableSubclass(HotSpotResolvedObjectType type) {
 209         return CTVM.hasFinalizableSubclass((HotSpotResolvedObjectTypeImpl) type);
 210     }
 211 
 212     public static HotSpotResolvedJavaMethodImpl asResolvedJavaMethod(
 213             Executable executable) {
 214         return CTVM.asResolvedJavaMethod(executable);
 215     }
 216 
 217     public static long getMaxCallTargetOffset(long address) {
 218         return CTVM.getMaxCallTargetOffset(address);
 219     }
 220 
 221     public static String disassembleCodeBlob(InstalledCode codeBlob) {
 222         return CTVM.disassembleCodeBlob(codeBlob);
 223     }
 224 
 225     public static StackTraceElement getStackTraceElement(
 226             HotSpotResolvedJavaMethod method, int bci) {
 227         return CTVM.getStackTraceElement((HotSpotResolvedJavaMethodImpl)method, bci);
 228     }
 229 
 230     public static Object executeHotSpotNmethod(Object[] args,
 231             HotSpotNmethod nmethodMirror) throws InvalidInstalledCodeException {
 232         return CTVM.executeHotSpotNmethod(args, nmethodMirror);
 233     }
 234 
 235     public static long[] getLineNumberTable(HotSpotResolvedJavaMethod method) {
 236         return CTVM.getLineNumberTable((HotSpotResolvedJavaMethodImpl)method);
 237     }
 238 
 239     public static int getLocalVariableTableLength(HotSpotResolvedJavaMethod method) {
 240         return CTVM.getLocalVariableTableLength((HotSpotResolvedJavaMethodImpl)method);
 241     }
 242 
 243     public static long getLocalVariableTableStart(HotSpotResolvedJavaMethod method) {
 244         return CTVM.getLocalVariableTableStart((HotSpotResolvedJavaMethodImpl)method);
 245     }
 246 
 247     public static void setNotInlinableOrCompilable(HotSpotResolvedJavaMethod method) {
 248         CTVM.setNotInlinableOrCompilable((HotSpotResolvedJavaMethodImpl)method);
 249     }
 250 
 251     public static void reprofile(HotSpotResolvedJavaMethod method) {
 252         CTVM.reprofile((HotSpotResolvedJavaMethodImpl)method);
 253     }
 254 
 255     public static void invalidateHotSpotNmethod(HotSpotNmethod nmethodMirror) {
 256         CTVM.invalidateHotSpotNmethod(nmethodMirror);
 257     }
 258 
 259     public static long[] collectCounters() {
 260         return CTVM.collectCounters();
 261     }
 262 
 263     public static boolean isMature(long metaspaceMethodData) {
 264         return CTVM.isMature(metaspaceMethodData);
 265     }
 266 
 267     public static int allocateCompileId(HotSpotResolvedJavaMethod method,
 268             int entryBCI) {
 269         return CTVM.allocateCompileId((HotSpotResolvedJavaMethodImpl) method, entryBCI);
 270     }
 271 
 272     public static boolean hasCompiledCodeForOSR(
 273             HotSpotResolvedJavaMethod method, int entryBCI, int level) {
 274         return CTVM.hasCompiledCodeForOSR((HotSpotResolvedJavaMethodImpl) method, entryBCI, level);
 275     }
 276 
 277     public static String getSymbol(long metaspaceSymbol) {
 278         return CTVM.getSymbol(metaspaceSymbol);
 279     }
 280 
 281     public static <T> T iterateFrames(
 282             ResolvedJavaMethod[] initialMethods,
 283             ResolvedJavaMethod[] matchingMethods,
 284             int initialSkip,
 285             InspectedFrameVisitor<T> visitor) {
 286         return CTVM.iterateFrames(initialMethods, matchingMethods, initialSkip, visitor);
 287     }
 288 
 289     public static void materializeVirtualObjects(
 290             HotSpotStackFrameReference stackFrame, boolean invalidate) {
 291         CTVM.materializeVirtualObjects(stackFrame, invalidate);
 292     }
 293 
 294     public static int getVtableIndexForInterfaceMethod(HotSpotResolvedObjectType type,
 295             HotSpotResolvedJavaMethod method) {
 296         return CTVM.getVtableIndexForInterfaceMethod((HotSpotResolvedObjectTypeImpl) type, (HotSpotResolvedJavaMethodImpl) method);
 297     }
 298 
 299     public static boolean shouldDebugNonSafepoints() {
 300         return CTVM.shouldDebugNonSafepoints();
 301     }
 302 
 303     public static void writeDebugOutput(byte[] bytes, int offset, int length) {
 304         CTVM.writeDebugOutput(bytes, offset, length, true, true);
 305     }
 306 
 307     public static void flushDebugOutput() {
 308         CTVM.flushDebugOutput();
 309     }
 310 
 311     public static HotSpotResolvedJavaMethod getResolvedJavaMethod(HotSpotObjectConstantImpl base,
 312             long displacement) {
 313         return CTVM.getResolvedJavaMethod(base, displacement);
 314     }
 315 
 316     public static HotSpotConstantPool getConstantPool(MetaspaceObject object) {
 317         return CTVM.getConstantPool(object);
 318     }
 319 
 320     public static HotSpotResolvedObjectType getResolvedJavaType(MetaspaceObject base,
 321             long displacement, boolean compressed) {
 322         return CTVM.getResolvedJavaType(base, displacement, compressed);
 323     }
 324 
 325     public static long getMetaspacePointer(Object o) {
 326         return ((MetaspaceObject) o).getMetaspacePointer();
 327     }
 328 
 329     public static Class<?> CompilerToVMClass() {
 330         return CompilerToVM.class;
 331     }
 332 
 333     public static JavaConstant getJavaMirror(HotSpotResolvedObjectType type) {
 334         return ((HotSpotResolvedJavaType) type).getJavaMirror();
 335     }
 336 
 337     public static HotSpotResolvedObjectType fromObjectClass(Class<?> theClass) {
 338           return (HotSpotResolvedObjectType) metaAccess.lookupJavaType(theClass);
 339     }
 340 }