001/*
002 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023
024package jdk.internal.jvmci.hotspot;
025
026import static jdk.internal.jvmci.hotspot.InitTimer.*;
027import jdk.internal.jvmci.code.*;
028import jdk.internal.jvmci.common.*;
029import jdk.internal.jvmci.meta.*;
030
031/**
032 * Entries into the HotSpot VM from Java code.
033 */
034public class CompilerToVMImpl implements CompilerToVM {
035
036    /**
037     * Initializes the native part of the JVMCI runtime.
038     */
039    private static native void init();
040
041    static {
042        try (InitTimer t = timer("CompilerToVMImpl.init")) {
043            init();
044        }
045    }
046
047    @Override
048    public native int installCode(HotSpotCompiledCode compiledCode, InstalledCode code, SpeculationLog speculationLog);
049
050    @Override
051    public native long getMetaspaceMethod(Class<?> holder, int slot);
052
053    @Override
054    public native byte[] getBytecode(long metaspaceMethod);
055
056    @Override
057    public native int exceptionTableLength(long metaspaceMethod);
058
059    @Override
060    public native long exceptionTableStart(long metaspaceMethod);
061
062    @Override
063    public native boolean hasBalancedMonitors(long metaspaceMethod);
064
065    @Override
066    public native long findUniqueConcreteMethod(long actualHolderMetaspaceKlass, long metaspaceMethod);
067
068    @Override
069    public native long getKlassImplementor(long metaspaceKlass);
070
071    @Override
072    public native long lookupType(String name, Class<?> accessingClass, boolean eagerResolve);
073
074    public native Object resolveConstantInPool(long metaspaceConstantPool, int cpi);
075
076    public Object resolvePossiblyCachedConstantInPool(long metaspaceConstantPool, int cpi) {
077        JVMCIError.guarantee(!HotSpotConstantPool.Options.UseConstantPoolCacheJavaCode.getValue(), "");
078        return resolvePossiblyCachedConstantInPool0(metaspaceConstantPool, cpi);
079    }
080
081    private native Object resolvePossiblyCachedConstantInPool0(long metaspaceConstantPool, int cpi);
082
083    @Override
084    public native int lookupNameAndTypeRefIndexInPool(long metaspaceConstantPool, int cpi);
085
086    @Override
087    public String lookupNameRefInPool(long metaspaceConstantPool, int cpi) {
088        JVMCIError.guarantee(!HotSpotConstantPool.Options.UseConstantPoolCacheJavaCode.getValue(), "");
089        return lookupNameRefInPool0(metaspaceConstantPool, cpi);
090    }
091
092    private native String lookupNameRefInPool0(long metaspaceConstantPool, int cpi);
093
094    @Override
095    public String lookupSignatureRefInPool(long metaspaceConstantPool, int cpi) {
096        JVMCIError.guarantee(!HotSpotConstantPool.Options.UseConstantPoolCacheJavaCode.getValue(), "");
097        return lookupSignatureRefInPool0(metaspaceConstantPool, cpi);
098    }
099
100    private native String lookupSignatureRefInPool0(long metaspaceConstantPool, int cpi);
101
102    @Override
103    public int lookupKlassRefIndexInPool(long metaspaceConstantPool, int cpi) {
104        JVMCIError.guarantee(!HotSpotConstantPool.Options.UseConstantPoolCacheJavaCode.getValue(), "");
105        return lookupKlassRefIndexInPool0(metaspaceConstantPool, cpi);
106    }
107
108    private native int lookupKlassRefIndexInPool0(long metaspaceConstantPool, int cpi);
109
110    public native long constantPoolKlassAt(long metaspaceConstantPool, int cpi);
111
112    @Override
113    public native long lookupKlassInPool(long metaspaceConstantPool, int cpi);
114
115    @Override
116    public native long lookupMethodInPool(long metaspaceConstantPool, int cpi, byte opcode);
117
118    @Override
119    public native long resolveField(long metaspaceConstantPool, int cpi, byte opcode, long[] info);
120
121    public int constantPoolRemapInstructionOperandFromCache(long metaspaceConstantPool, int cpi) {
122        JVMCIError.guarantee(!HotSpotConstantPool.Options.UseConstantPoolCacheJavaCode.getValue(), "");
123        return constantPoolRemapInstructionOperandFromCache0(metaspaceConstantPool, cpi);
124    }
125
126    private native int constantPoolRemapInstructionOperandFromCache0(long metaspaceConstantPool, int cpi);
127
128    @Override
129    public Object lookupAppendixInPool(long metaspaceConstantPool, int cpi) {
130        JVMCIError.guarantee(!HotSpotConstantPool.Options.UseConstantPoolCacheJavaCode.getValue(), "");
131        return lookupAppendixInPool0(metaspaceConstantPool, cpi);
132    }
133
134    private native Object lookupAppendixInPool0(long metaspaceConstantPool, int cpi);
135
136    @Override
137    public native void initializeConfiguration(HotSpotVMConfig config);
138
139    @Override
140    public native long resolveMethod(long metaspaceKlassExactReceiver, long metaspaceMethod, long metaspaceKlassCaller);
141
142    @Override
143    public native boolean hasFinalizableSubclass(long metaspaceKlass);
144
145    public native boolean methodIsIgnoredBySecurityStackWalk(long metaspaceMethod);
146
147    @Override
148    public native long getClassInitializer(long metaspaceKlass);
149
150    @Override
151    public native long getMaxCallTargetOffset(long address);
152
153    // The HotSpot disassembler seems not to be thread safe so it's better to synchronize its usage
154    @Override
155    public synchronized native String disassembleCodeBlob(long codeBlob);
156
157    @Override
158    public native StackTraceElement getStackTraceElement(long metaspaceMethod, int bci);
159
160    @Override
161    public native Object executeCompiledMethodVarargs(Object[] args, InstalledCode hotspotInstalledCode);
162
163    @Override
164    public native long[] getLineNumberTable(long metaspaceMethod);
165
166    @Override
167    public native long getLocalVariableTableStart(long metaspaceMethod);
168
169    @Override
170    public native int getLocalVariableTableLength(long metaspaceMethod);
171
172    @Override
173    public native String getFileName(HotSpotResolvedJavaType method);
174
175    @Override
176    public native void reprofile(long metaspaceMethod);
177
178    @Override
179    public native void invalidateInstalledCode(InstalledCode hotspotInstalledCode);
180
181    @Override
182    public native Class<?> getJavaMirror(long metaspaceKlass);
183
184    @Override
185    public native long readUnsafeKlassPointer(Object o);
186
187    @Override
188    public native Object readUncompressedOop(long address);
189
190    @Override
191    public native void doNotInlineOrCompile(long metaspaceMethod);
192
193    @Override
194    public Object executeCompiledMethod(Object arg1, Object arg2, Object arg3, InstalledCode hotspotInstalledCode) throws InvalidInstalledCodeException {
195        return executeCompiledMethodVarargs(new Object[]{arg1, arg2, arg3}, hotspotInstalledCode);
196    }
197
198    public synchronized native void notifyCompilationStatistics(int id, HotSpotResolvedJavaMethod method, boolean osr, int processedBytecodes, long time, long timeUnitsPerSecond,
199                    InstalledCode installedCode);
200
201    public native void resetCompilationStatistics();
202
203    public native long[] collectCounters();
204
205    public native boolean isMature(long method);
206
207    public native int allocateCompileId(long metaspaceMethod, int entryBCI);
208
209    public String getGPUs() {
210        return "";
211    }
212
213    public native boolean canInlineMethod(long metaspaceMethod);
214
215    public native boolean shouldInlineMethod(long metaspaceMethod);
216
217    public native boolean hasCompiledCodeForOSR(long metaspaceMethod, int entryBCI, int level);
218
219    public native HotSpotStackFrameReference getNextStackFrame(HotSpotStackFrameReference frame, long[] methods, int initialSkip);
220
221    public native void materializeVirtualObjects(HotSpotStackFrameReference stackFrame, boolean invalidate);
222
223    public native long getTimeStamp();
224
225    public String getSymbol(long metaspaceSymbol) {
226        JVMCIError.guarantee(!HotSpotConstantPool.Options.UseConstantPoolCacheJavaCode.getValue(), "");
227        return getSymbol0(metaspaceSymbol);
228    }
229
230    private native String getSymbol0(long metaspaceSymbol);
231
232    public native void resolveInvokeDynamic(long metaspaceConstantPool, int index);
233
234    public native void resolveInvokeHandle(long metaspaceConstantPool, int index);
235
236    public native int getVtableIndexForInterface(long metaspaceKlass, long metaspaceMethod);
237
238    public native boolean shouldDebugNonSafepoints();
239
240    public native void writeDebugOutput(byte[] bytes, int offset, int length);
241
242    public native void flushDebugOutput();
243}