1 /* 2 * Copyright (c) 2013, 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 package jdk.vm.ci.hotspot; 24 25 import java.lang.reflect.Field; 26 27 import jdk.vm.ci.code.BailoutException; 28 import jdk.vm.ci.code.BytecodeFrame; 29 import jdk.vm.ci.code.CodeCacheProvider; 30 import jdk.vm.ci.code.CompiledCode; 31 import jdk.vm.ci.code.InstalledCode; 32 import jdk.vm.ci.code.RegisterConfig; 33 import jdk.vm.ci.code.TargetDescription; 34 import jdk.vm.ci.code.site.Call; 35 import jdk.vm.ci.code.site.Mark; 36 import jdk.vm.ci.meta.ResolvedJavaMethod; 37 import jdk.vm.ci.meta.SpeculationLog; 38 39 /** 40 * HotSpot implementation of {@link CodeCacheProvider}. 41 */ 42 public class HotSpotCodeCacheProvider implements CodeCacheProvider { 43 44 protected final HotSpotJVMCIRuntimeProvider runtime; 45 public final HotSpotVMConfig config; 46 protected final TargetDescription target; 47 protected final RegisterConfig regConfig; 48 49 public HotSpotCodeCacheProvider(HotSpotJVMCIRuntimeProvider runtime, HotSpotVMConfig config, TargetDescription target, RegisterConfig regConfig) { 50 this.runtime = runtime; 51 this.config = config; 52 this.target = target; 53 this.regConfig = regConfig; 54 } 55 56 @Override 57 public String getMarkName(Mark mark) { 58 int markId = (int) mark.id; 59 Field[] fields = runtime.getConfig().getClass().getDeclaredFields(); 60 for (Field f : fields) { 61 if (f.getName().startsWith("MARKID_")) { 62 f.setAccessible(true); 63 try { 64 if (f.getInt(runtime.getConfig()) == markId) { 65 return f.getName(); 66 } 67 } catch (Exception e) { 68 } 69 } 70 } 71 return CodeCacheProvider.super.getMarkName(mark); 72 } 73 74 /** 75 * Decodes a call target to a mnemonic if possible. 76 */ 77 @Override 78 public String getTargetName(Call call) { 79 Field[] fields = runtime.getConfig().getClass().getDeclaredFields(); 80 for (Field f : fields) { 81 if (f.getName().endsWith("Stub")) { 82 f.setAccessible(true); 83 try { 84 Object address = f.get(runtime.getConfig()); 85 if (address.equals(call.target)) { 86 return f.getName() + ":0x" + Long.toHexString((Long) address); 87 } 88 } catch (Exception e) { 89 } 90 } 91 } 92 return CodeCacheProvider.super.getTargetName(call); 93 } 94 95 @Override 96 public RegisterConfig getRegisterConfig() { 97 return regConfig; 98 } 99 100 @Override 101 public int getMinimumOutgoingSize() { 102 return runtime.getConfig().runtimeCallStackSize; 103 } 104 105 private InstalledCode logOrDump(InstalledCode installedCode, CompiledCode compiledCode) { 106 ((HotSpotJVMCIRuntime) runtime).notifyInstall(this, installedCode, compiledCode); 107 return installedCode; 108 } 109 110 public InstalledCode installCode(ResolvedJavaMethod method, CompiledCode compiledCode, InstalledCode installedCode, SpeculationLog log, boolean isDefault) { 111 InstalledCode resultInstalledCode; 112 if (installedCode == null) { 113 if (method == null) { 114 // Must be a stub 115 resultInstalledCode = new HotSpotRuntimeStub(((HotSpotCompiledCode) compiledCode).getName()); 116 } else { 117 resultInstalledCode = new HotSpotNmethod((HotSpotResolvedJavaMethod) method, ((HotSpotCompiledCode) compiledCode).getName(), isDefault); 118 } 119 } else { 120 resultInstalledCode = installedCode; 121 } 122 123 int result = runtime.getCompilerToVM().installCode(target, (HotSpotCompiledCode) compiledCode, resultInstalledCode, (HotSpotSpeculationLog) log); 124 if (result != config.codeInstallResultOk) { 125 String resultDesc = config.getCodeInstallResultDescription(result); 126 if (compiledCode instanceof HotSpotCompiledNmethod) { 127 HotSpotCompiledNmethod compiledNmethod = (HotSpotCompiledNmethod) compiledCode; 128 String msg = compiledNmethod.getInstallationFailureMessage(); 129 if (msg != null) { 130 msg = String.format("Code installation failed: %s%n%s", resultDesc, msg); 131 } else { 132 msg = String.format("Code installation failed: %s", resultDesc); 133 } 134 if (result == config.codeInstallResultDependenciesInvalid) { 135 throw new AssertionError(resultDesc + " " + msg); 136 } 137 throw new BailoutException(result != config.codeInstallResultDependenciesFailed, msg); 138 } else { 139 throw new BailoutException("Error installing %s: %s", ((HotSpotCompiledCode) compiledCode).getName(), resultDesc); 140 } 141 } 142 return logOrDump(resultInstalledCode, compiledCode); 143 } 144 145 public void invalidateInstalledCode(InstalledCode installedCode) { 146 runtime.getCompilerToVM().invalidateInstalledCode(installedCode); 147 } 148 149 @Override 150 public TargetDescription getTarget() { 151 return target; 152 } 153 154 public String disassemble(InstalledCode code) { 155 if (code.isValid()) { 156 return runtime.getCompilerToVM().disassembleCodeBlob(code); 157 } 158 return null; 159 } 160 161 public SpeculationLog createSpeculationLog() { 162 return new HotSpotSpeculationLog(); 163 } 164 165 public long getMaxCallTargetOffset(long address) { 166 return runtime.getCompilerToVM().getMaxCallTargetOffset(address); 167 } 168 169 public boolean shouldDebugNonSafepoints() { 170 return runtime.getCompilerToVM().shouldDebugNonSafepoints(); 171 } 172 173 /** 174 * Notifies the VM of statistics for a completed compilation. 175 * 176 * @param id the identifier of the compilation 177 * @param method the method compiled 178 * @param osr specifies if the compilation was for on-stack-replacement 179 * @param processedBytecodes the number of bytecodes processed during the compilation, including 180 * the bytecodes of all inlined methods 181 * @param time the amount time spent compiling {@code method} 182 * @param timeUnitsPerSecond the granularity of the units for the {@code time} value 183 * @param installedCode the nmethod installed as a result of the compilation 184 */ 185 public void notifyCompilationStatistics(int id, HotSpotResolvedJavaMethod method, boolean osr, int processedBytecodes, long time, long timeUnitsPerSecond, InstalledCode installedCode) { 186 runtime.getCompilerToVM().notifyCompilationStatistics(id, (HotSpotResolvedJavaMethodImpl) method, osr, processedBytecodes, time, timeUnitsPerSecond, installedCode); 187 } 188 189 public int interpreterFrameSize(BytecodeFrame pos) { 190 return runtime.getCompilerToVM().interpreterFrameSize(pos); 191 } 192 193 /** 194 * Resets all compilation statistics. 195 */ 196 public void resetCompilationStatistics() { 197 runtime.getCompilerToVM().resetCompilationStatistics(); 198 } 199 }