1 /*
   2  * Copyright (c) 2011, 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 com.oracle.graal.hotspot.bridge;
  25 
  26 import java.lang.reflect.*;
  27 
  28 import com.oracle.graal.api.code.*;
  29 import com.oracle.graal.api.meta.*;
  30 import com.oracle.graal.hotspot.*;
  31 import com.oracle.graal.hotspot.meta.*;
  32 
  33 /**
  34  * Calls from Java into HotSpot.
  35  */
  36 public interface CompilerToVM {
  37 
  38     /**
  39      * Copies the original bytecode of a given method into a given byte array.
  40      * 
  41      * @param metaspaceMethod the metaspace Method object
  42      * @param code the array into which to copy the original bytecode
  43      * @return the value of {@code code}
  44      */
  45     byte[] initializeBytecode(long metaspaceMethod, byte[] code);
  46 
  47     String getSignature(long metaspaceMethod);
  48 
  49     ExceptionHandler[] initializeExceptionHandlers(long metaspaceMethod, ExceptionHandler[] handlers);
  50 
  51     /**
  52      * Determines if a given metaspace Method object has balanced monitors.
  53      * 
  54      * @param metaspaceMethod the metaspace Method object to query
  55      * @return true if the method has balanced monitors
  56      */
  57     boolean hasBalancedMonitors(long metaspaceMethod);
  58 
  59     /**
  60      * Determines if a given metaspace Method object is compilable. A method may not be compilable
  61      * for a number of reasons such as:
  62      * <ul>
  63      * <li>a CompileOracle directive may prevent compilation of methods</li>
  64      * <li>the method may have a bytecode breakpoint set</li>
  65      * <li>the method may have other bytecode features that require special handling by the VM</li>
  66      * </ul>
  67      * 
  68      * A non-compilable method should not be inlined.
  69      * 
  70      * @param metaspaceMethod the metaspace Method object to query
  71      * @return true if the method is compilable
  72      */
  73     boolean isMethodCompilable(long metaspaceMethod);
  74 
  75     /**
  76      * Used to implement {@link ResolvedJavaType#findUniqueConcreteMethod(ResolvedJavaMethod)}.
  77      * 
  78      * @param metaspaceMethod the metaspace Method on which to based the search
  79      * @param resultHolder the holder of the result is put in element 0 of this array
  80      * @return the metaspace Method result or 0 is there is no unique concrete method for
  81      *         {@code metaspaceMethod}
  82      */
  83     long getUniqueConcreteMethod(long metaspaceMethod, HotSpotResolvedObjectType[] resultHolder);
  84 
  85     /**
  86      * Used to determine if an interface has exactly one implementor.
  87      * 
  88      * @param interfaceType interface for which the implementor should be returned
  89      * @return the unique implementor of the interface or null if the interface has 0 or more than 1
  90      *         implementor
  91      */
  92     ResolvedJavaType getUniqueImplementor(HotSpotResolvedObjectType interfaceType);
  93 
  94     /**
  95      * Gets the invocation count for a method.
  96      * 
  97      * @param metaspaceMethod the metaspace Method object to query
  98      * @return the invocation count for the method
  99      */
 100     int getInvocationCount(long metaspaceMethod);
 101 
 102     /**
 103      * Initializes a {@link HotSpotResolvedJavaMethod} object from a metaspace Method object.
 104      * 
 105      * @param metaspaceMethod the metaspace Method object
 106      * @param method address of a metaspace Method object
 107      */
 108     void initializeMethod(long metaspaceMethod, HotSpotResolvedJavaMethod method);
 109 
 110     /**
 111      * Initializes a {@link HotSpotMethodData} object from a metaspace MethodData object.
 112      * 
 113      * @param metaspaceMethodData the metaspace MethodData object
 114      * @param methodData the object to initialize from the metaspace object
 115      */
 116     void initializeMethodData(long metaspaceMethodData, HotSpotMethodData methodData);
 117 
 118     /**
 119      * Converts a name to a Java type.
 120      * 
 121      * @param name a well formed Java type in {@linkplain JavaType#getName() internal} format
 122      * @param accessingClass the context of resolution (may be null)
 123      * @param eagerResolve force resolution to a {@link ResolvedJavaType}. If true, this method will
 124      *            either return a {@link ResolvedJavaType} or throw an exception
 125      * @return a Java type for {@code name} which is guaranteed to be of type
 126      *         {@link ResolvedJavaType} if {@code eagerResolve == true}
 127      * @throws LinkageError if {@code eagerResolve == true} and the resolution failed
 128      */
 129     JavaType lookupType(String name, HotSpotResolvedObjectType accessingClass, boolean eagerResolve);
 130 
 131     Object lookupConstantInPool(HotSpotResolvedObjectType pool, int cpi);
 132 
 133     JavaMethod lookupMethodInPool(HotSpotResolvedObjectType pool, int cpi, byte opcode);
 134 
 135     JavaType lookupTypeInPool(HotSpotResolvedObjectType pool, int cpi);
 136 
 137     JavaField lookupFieldInPool(HotSpotResolvedObjectType pool, int cpi, byte opcode);
 138 
 139     void lookupReferencedTypeInPool(HotSpotResolvedObjectType pool, int cpi, byte opcode);
 140 
 141     Object lookupAppendixInPool(HotSpotResolvedObjectType pool, int cpi, byte opcode);
 142 
 143     // Must be kept in sync with enum in graalEnv.hpp
 144     public enum CodeInstallResult {
 145         OK, DEPENDENCIES_FAILED, CACHE_FULL
 146     }
 147 
 148     /**
 149      * Installs the result of a compilation into the code cache.
 150      * 
 151      * @param compResult the result of a compilation
 152      * @param code if not null, then the code is installed as the non-default compiled code for the
 153      *            associated method and the details of the installation are written to this object
 154      * @return the outcome of the installation as a {@link CodeInstallResult}.
 155      */
 156     CodeInstallResult installCode(HotSpotCompilationResult compResult, HotSpotInstalledCode code, SpeculationLog cache);
 157 
 158     void initializeConfiguration(HotSpotVMConfig config);
 159 
 160     JavaMethod resolveMethod(HotSpotResolvedObjectType klass, String name, String signature);
 161 
 162     boolean isTypeInitialized(HotSpotResolvedObjectType klass);
 163 
 164     void initializeType(HotSpotResolvedObjectType klass);
 165 
 166     ResolvedJavaType getResolvedType(Class<?> javaClass);
 167 
 168     HotSpotResolvedJavaField[] getInstanceFields(HotSpotResolvedObjectType klass);
 169 
 170     /**
 171      * Gets the compiled code size for a method.
 172      * 
 173      * @param metaspaceMethod the metaspace Method object to query
 174      * @return the compiled code size the method
 175      */
 176     int getCompiledCodeSize(long metaspaceMethod);
 177 
 178     /**
 179      * Gets the metaspace Method object corresponding to a given reflection {@link Method} object.
 180      * 
 181      * @param reflectionMethod
 182      * @param resultHolder the holder of the result is put in element 0 of this array
 183      * @return the metaspace Method result for {@code reflectionMethod}
 184      */
 185     long getMetaspaceMethod(Method reflectionMethod, HotSpotResolvedObjectType[] resultHolder);
 186 
 187     long getMetaspaceConstructor(Constructor reflectionConstructor, HotSpotResolvedObjectType[] resultHolder);
 188 
 189     HotSpotResolvedJavaField getJavaField(Field reflectionField);
 190 
 191     long getMaxCallTargetOffset(long stub);
 192 
 193     String disassembleNMethod(long nmethod);
 194 
 195     /**
 196      * Gets a copy of the machine code for an nmethod.
 197      * 
 198      * @return the machine code for {@code nmethod} if it is valid, null otherwise
 199      */
 200     byte[] getCode(long nmethod);
 201 
 202     StackTraceElement getStackTraceElement(long metaspaceMethod, int bci);
 203 
 204     Object executeCompiledMethod(long metaspaceMethod, long nmethod, Object arg1, Object arg2, Object arg3);
 205 
 206     Object executeCompiledMethodVarargs(long metaspaceMethod, long nmethod, Object... args);
 207 
 208     int getVtableEntryOffset(long metaspaceMethod);
 209 
 210     long[] getDeoptedLeafGraphIds();
 211 
 212     long[] getLineNumberTable(HotSpotResolvedJavaMethod method);
 213 
 214     Local[] getLocalVariableTable(HotSpotResolvedJavaMethod method);
 215 
 216     String getFileName(HotSpotResolvedJavaType method);
 217 
 218     void clearQueuedForCompilation(HotSpotResolvedJavaMethod method);
 219 
 220     /**
 221      * Invalidates the profiling information and restarts profiling upon the next invocation.
 222      * 
 223      * @param metaspaceMethod the metaspace Method object
 224      */
 225     void reprofile(long metaspaceMethod);
 226 }