1 /* 2 * Copyright (c) 2012, 2016, 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 org.graalvm.compiler.hotspot.stubs; 24 25 import static org.graalvm.compiler.core.GraalCompiler.emitBackEnd; 26 import static org.graalvm.compiler.core.GraalCompiler.emitFrontEnd; 27 import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC; 28 import static org.graalvm.compiler.hotspot.HotSpotHostBackend.UNCOMMON_TRAP_HANDLER; 29 30 import java.util.ArrayList; 31 import java.util.Collection; 32 import java.util.Collections; 33 import java.util.List; 34 import java.util.ListIterator; 35 import java.util.Set; 36 37 import org.graalvm.compiler.code.CompilationResult; 38 import org.graalvm.compiler.core.common.CompilationIdentifier; 39 import org.graalvm.compiler.core.target.Backend; 40 import org.graalvm.compiler.debug.Debug; 41 import org.graalvm.compiler.debug.Debug.Scope; 42 import org.graalvm.compiler.debug.internal.DebugScope; 43 import org.graalvm.compiler.hotspot.HotSpotCompiledCodeBuilder; 44 import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage; 45 import org.graalvm.compiler.hotspot.meta.HotSpotProviders; 46 import org.graalvm.compiler.hotspot.nodes.StubStartNode; 47 import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory; 48 import org.graalvm.compiler.lir.phases.LIRPhase; 49 import org.graalvm.compiler.lir.phases.LIRSuites; 50 import org.graalvm.compiler.lir.phases.PostAllocationOptimizationPhase.PostAllocationOptimizationContext; 51 import org.graalvm.compiler.lir.profiling.MoveProfilingPhase; 52 import org.graalvm.compiler.nodes.StructuredGraph; 53 import org.graalvm.compiler.phases.OptimisticOptimizations; 54 import org.graalvm.compiler.phases.PhaseSuite; 55 import org.graalvm.compiler.phases.tiers.Suites; 56 57 import jdk.vm.ci.code.CodeCacheProvider; 58 import jdk.vm.ci.code.InstalledCode; 59 import jdk.vm.ci.code.Register; 60 import jdk.vm.ci.code.RegisterConfig; 61 import jdk.vm.ci.code.site.Call; 62 import jdk.vm.ci.code.site.ConstantReference; 63 import jdk.vm.ci.code.site.DataPatch; 64 import jdk.vm.ci.code.site.Infopoint; 65 import jdk.vm.ci.hotspot.HotSpotCompiledCode; 66 import jdk.vm.ci.hotspot.HotSpotMetaspaceConstant; 67 import jdk.vm.ci.meta.DefaultProfilingInfo; 68 import jdk.vm.ci.meta.ResolvedJavaMethod; 69 import jdk.vm.ci.meta.TriState; 70 71 //JaCoCo Exclude 72 73 /** 74 * Base class for implementing some low level code providing the out-of-line slow path for a snippet 75 * and/or a callee saved call to a HotSpot C/C++ runtime function or even a another compiled Java 76 * method. 77 */ 78 public abstract class Stub { 79 80 private static final List<Stub> stubs = new ArrayList<>(); 81 82 /** 83 * The linkage information for a call to this stub from compiled code. 84 */ 85 protected final HotSpotForeignCallLinkage linkage; 86 87 /** 88 * The code installed for the stub. 89 */ 90 protected InstalledCode code; 91 92 /** 93 * Compilation result from which {@link #code} was created. 94 */ 95 protected CompilationResult compResult; 96 97 /** 98 * The registers destroyed by this stub (from the caller's perspective). 99 */ 100 private Set<Register> destroyedCallerRegisters; 101 102 private HotSpotCompiledCode compiledCode; 103 104 public void initDestroyedCallerRegisters(Set<Register> registers) { 105 assert registers != null; 106 assert destroyedCallerRegisters == null || registers.equals(destroyedCallerRegisters) : "cannot redefine"; 107 destroyedCallerRegisters = registers; 108 } 109 110 /** 111 * Gets the registers destroyed by this stub from a caller's perspective. These are the 112 * temporaries of this stub and must thus be caller saved by a callers of this stub. 113 */ 114 public Set<Register> getDestroyedCallerRegisters() { 115 assert destroyedCallerRegisters != null : "not yet initialized"; 116 return destroyedCallerRegisters; 117 } 118 119 /** 120 * Determines if this stub preserves all registers apart from those it 121 * {@linkplain #getDestroyedCallerRegisters() destroys}. 122 */ 123 public boolean preservesRegisters() { 124 return true; 125 } 126 127 protected final HotSpotProviders providers; 128 129 /** 130 * Creates a new stub. 131 * 132 * @param linkage linkage details for a call to the stub 133 */ 134 public Stub(HotSpotProviders providers, HotSpotForeignCallLinkage linkage) { 135 this.linkage = linkage; 136 this.providers = providers; 137 stubs.add(this); 138 } 139 140 /** 141 * Gets an immutable view of all stubs that have been created. 142 */ 143 public static Collection<Stub> getStubs() { 144 return Collections.unmodifiableList(stubs); 145 } 146 147 /** 148 * Gets the linkage for a call to this stub from compiled code. 149 */ 150 public HotSpotForeignCallLinkage getLinkage() { 151 return linkage; 152 } 153 154 public RegisterConfig getRegisterConfig() { 155 return null; 156 } 157 158 /** 159 * Gets the graph that from which the code for this stub will be compiled. 160 * 161 * @param compilationId unique compilation id for the stub 162 */ 163 protected abstract StructuredGraph getGraph(CompilationIdentifier compilationId); 164 165 @Override 166 public String toString() { 167 return "Stub<" + linkage.getDescriptor() + ">"; 168 } 169 170 /** 171 * Gets the method the stub's code will be associated with once installed. This may be null. 172 */ 173 protected abstract ResolvedJavaMethod getInstalledCodeOwner(); 174 175 /** 176 * Gets a context object for the debug scope created when producing the code for this stub. 177 */ 178 protected abstract Object debugScopeContext(); 179 180 /** 181 * Gets the code for this stub, compiling it first if necessary. 182 */ 183 @SuppressWarnings("try") 184 public synchronized InstalledCode getCode(final Backend backend) { 185 if (code == null) { 186 try (Scope d = Debug.sandbox("CompilingStub", DebugScope.getConfig(), providers.getCodeCache(), debugScopeContext())) { 187 final StructuredGraph graph = getGraph(getStubCompilationId()); 188 189 // Stubs cannot be recompiled so they cannot be compiled with assumptions 190 assert graph.getAssumptions() == null; 191 192 if (!(graph.start() instanceof StubStartNode)) { 193 StubStartNode newStart = graph.add(new StubStartNode(Stub.this)); 194 newStart.setStateAfter(graph.start().stateAfter()); 195 graph.replaceFixed(graph.start(), newStart); 196 } 197 198 CodeCacheProvider codeCache = providers.getCodeCache(); 199 200 compResult = new CompilationResult(toString(), GeneratePIC.getValue()); 201 try (Scope s0 = Debug.scope("StubCompilation", graph, providers.getCodeCache())) { 202 Suites suites = createSuites(); 203 emitFrontEnd(providers, backend, graph, providers.getSuites().getDefaultGraphBuilderSuite(), OptimisticOptimizations.ALL, DefaultProfilingInfo.get(TriState.UNKNOWN), suites); 204 LIRSuites lirSuites = createLIRSuites(); 205 emitBackEnd(graph, Stub.this, getInstalledCodeOwner(), backend, compResult, CompilationResultBuilderFactory.Default, getRegisterConfig(), lirSuites); 206 assert checkStubInvariants(); 207 } catch (Throwable e) { 208 throw Debug.handle(e); 209 } 210 211 assert destroyedCallerRegisters != null; 212 try (Scope s = Debug.scope("CodeInstall", compResult)) { 213 // Add a GeneratePIC check here later, we don't want to install 214 // code if we don't have a corresponding VM global symbol. 215 compiledCode = HotSpotCompiledCodeBuilder.createCompiledCode(null, null, compResult); 216 code = codeCache.installCode(null, compiledCode, null, null, false); 217 } catch (Throwable e) { 218 throw Debug.handle(e); 219 } 220 } catch (Throwable e) { 221 throw Debug.handle(e); 222 } 223 assert code != null : "error installing stub " + this; 224 } 225 226 return code; 227 } 228 229 public CompilationIdentifier getStubCompilationId() { 230 return new StubCompilationIdentifier(this); 231 } 232 233 /** 234 * Checks the conditions a compilation must satisfy to be installed as a RuntimeStub. 235 */ 236 private boolean checkStubInvariants() { 237 assert compResult.getExceptionHandlers().isEmpty() : this; 238 239 // Stubs cannot be recompiled so they cannot be compiled with 240 // assumptions and there is no point in recording evol_method dependencies 241 assert compResult.getAssumptions() == null : "stubs should not use assumptions: " + this; 242 243 for (DataPatch data : compResult.getDataPatches()) { 244 if (data.reference instanceof ConstantReference) { 245 ConstantReference ref = (ConstantReference) data.reference; 246 if (ref.getConstant() instanceof HotSpotMetaspaceConstant) { 247 HotSpotMetaspaceConstant c = (HotSpotMetaspaceConstant) ref.getConstant(); 248 if (c.asResolvedJavaType() != null && c.asResolvedJavaType().getName().equals("[I")) { 249 // special handling for NewArrayStub 250 // embedding the type '[I' is safe, since it is never unloaded 251 continue; 252 } 253 } 254 } 255 256 assert !(data.reference instanceof ConstantReference) : this + " cannot have embedded object or metadata constant: " + data.reference; 257 } 258 for (Infopoint infopoint : compResult.getInfopoints()) { 259 assert infopoint instanceof Call : this + " cannot have non-call infopoint: " + infopoint; 260 Call call = (Call) infopoint; 261 assert call.target instanceof HotSpotForeignCallLinkage : this + " cannot have non runtime call: " + call.target; 262 HotSpotForeignCallLinkage callLinkage = (HotSpotForeignCallLinkage) call.target; 263 assert !callLinkage.isCompiledStub() || callLinkage.getDescriptor().equals(UNCOMMON_TRAP_HANDLER) : this + " cannot call compiled stub " + callLinkage; 264 } 265 return true; 266 } 267 268 protected Suites createSuites() { 269 Suites defaultSuites = providers.getSuites().getDefaultSuites(); 270 return new Suites(new PhaseSuite<>(), defaultSuites.getMidTier(), defaultSuites.getLowTier()); 271 } 272 273 protected LIRSuites createLIRSuites() { 274 LIRSuites lirSuites = new LIRSuites(providers.getSuites().getDefaultLIRSuites()); 275 ListIterator<LIRPhase<PostAllocationOptimizationContext>> moveProfiling = lirSuites.getPostAllocationOptimizationStage().findPhase(MoveProfilingPhase.class); 276 if (moveProfiling != null) { 277 moveProfiling.remove(); 278 } 279 return lirSuites; 280 } 281 282 /** 283 * Gets the HotSpotCompiledCode that was created during installation. 284 */ 285 public synchronized HotSpotCompiledCode getCompiledCode(final Backend backend) { 286 getCompilationResult(backend); 287 assert compiledCode != null; 288 return compiledCode; 289 } 290 291 /** 292 * Gets the compilation result for this stub, compiling it first if necessary, and installing it 293 * in code. 294 */ 295 public synchronized CompilationResult getCompilationResult(final Backend backend) { 296 if (code == null) { 297 getCode(backend); 298 } 299 return compResult; 300 } 301 }