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 * The registers destroyed by this stub (from the caller's perspective). 94 */ 95 private Set<Register> destroyedCallerRegisters; 96 97 public void initDestroyedCallerRegisters(Set<Register> registers) { 98 assert registers != null; 99 assert destroyedCallerRegisters == null || registers.equals(destroyedCallerRegisters) : "cannot redefine"; 100 destroyedCallerRegisters = registers; 101 } 102 103 /** 104 * Gets the registers destroyed by this stub from a caller's perspective. These are the 105 * temporaries of this stub and must thus be caller saved by a callers of this stub. 106 */ 107 public Set<Register> getDestroyedCallerRegisters() { 108 assert destroyedCallerRegisters != null : "not yet initialized"; 109 return destroyedCallerRegisters; 110 } 111 112 /** 113 * Determines if this stub preserves all registers apart from those it 114 * {@linkplain #getDestroyedCallerRegisters() destroys}. 115 */ 116 public boolean preservesRegisters() { 117 return true; 118 } 119 120 protected final HotSpotProviders providers; 121 122 /** 123 * Creates a new stub. 124 * 125 * @param linkage linkage details for a call to the stub 126 */ 127 public Stub(HotSpotProviders providers, HotSpotForeignCallLinkage linkage) { 128 this.linkage = linkage; 129 this.providers = providers; 130 stubs.add(this); 131 } 132 133 /** 134 * Gets an immutable view of all stubs that have been created. 135 */ 136 public static Collection<Stub> getStubs() { 137 return Collections.unmodifiableList(stubs); 138 } 139 140 /** 141 * Gets the linkage for a call to this stub from compiled code. 142 */ 143 public HotSpotForeignCallLinkage getLinkage() { 144 return linkage; 145 } 146 147 public RegisterConfig getRegisterConfig() { 148 return null; 149 } 150 151 /** 152 * Gets the graph that from which the code for this stub will be compiled. 153 * 154 * @param compilationId unique compilation id for the stub 155 */ 156 protected abstract StructuredGraph getGraph(CompilationIdentifier compilationId); 157 158 @Override 159 public String toString() { 160 return "Stub<" + linkage.getDescriptor() + ">"; 161 } 162 163 /** 164 * Gets the method the stub's code will be associated with once installed. This may be null. 165 */ 166 protected abstract ResolvedJavaMethod getInstalledCodeOwner(); 167 168 /** 169 * Gets a context object for the debug scope created when producing the code for this stub. 170 */ 171 protected abstract Object debugScopeContext(); 172 173 /** 174 * Gets the code for this stub, compiling it first if necessary. 175 */ 176 @SuppressWarnings("try") 177 public synchronized InstalledCode getCode(final Backend backend) { 178 if (code == null) { 179 try (Scope d = Debug.sandbox("CompilingStub", DebugScope.getConfig(), providers.getCodeCache(), debugScopeContext())) { 180 CodeCacheProvider codeCache = providers.getCodeCache(); 181 CompilationResult compResult = buildCompilationResult(backend); 182 try (Scope s = Debug.scope("CodeInstall", compResult)) { 183 assert destroyedCallerRegisters != null; 184 // Add a GeneratePIC check here later, we don't want to install 185 // code if we don't have a corresponding VM global symbol. 186 HotSpotCompiledCode compiledCode = HotSpotCompiledCodeBuilder.createCompiledCode(null, null, compResult); 187 code = codeCache.installCode(null, compiledCode, null, null, false); 188 } catch (Throwable e) { 189 throw Debug.handle(e); 190 } 191 } catch (Throwable e) { 192 throw Debug.handle(e); 193 } 194 assert code != null : "error installing stub " + this; 195 } 196 197 return code; 198 } 199 200 @SuppressWarnings("try") 201 private CompilationResult buildCompilationResult(final Backend backend) { 202 CompilationResult compResult = new CompilationResult(toString(), GeneratePIC.getValue()); 203 final StructuredGraph graph = getGraph(getStubCompilationId()); 204 205 // Stubs cannot be recompiled so they cannot be compiled with assumptions 206 assert graph.getAssumptions() == null; 207 208 if (!(graph.start() instanceof StubStartNode)) { 209 StubStartNode newStart = graph.add(new StubStartNode(Stub.this)); 210 newStart.setStateAfter(graph.start().stateAfter()); 211 graph.replaceFixed(graph.start(), newStart); 212 } 213 214 try (Scope s0 = Debug.scope("StubCompilation", graph, providers.getCodeCache())) { 215 Suites suites = createSuites(); 216 emitFrontEnd(providers, backend, graph, providers.getSuites().getDefaultGraphBuilderSuite(), OptimisticOptimizations.ALL, DefaultProfilingInfo.get(TriState.UNKNOWN), suites); 217 LIRSuites lirSuites = createLIRSuites(); 218 emitBackEnd(graph, Stub.this, getInstalledCodeOwner(), backend, compResult, CompilationResultBuilderFactory.Default, getRegisterConfig(), lirSuites); 219 assert checkStubInvariants(compResult); 220 } catch (Throwable e) { 221 throw Debug.handle(e); 222 } 223 return compResult; 224 } 225 226 /** 227 * Gets a {@link CompilationResult} that can be used for code generation. Required for AOT. 228 */ 229 @SuppressWarnings("try") 230 public CompilationResult getCompilationResult(final Backend backend) { 231 try (Scope d = Debug.sandbox("CompilingStub", DebugScope.getConfig(), providers.getCodeCache(), debugScopeContext())) { 232 return buildCompilationResult(backend); 233 } catch (Throwable e) { 234 throw Debug.handle(e); 235 } 236 } 237 238 public CompilationIdentifier getStubCompilationId() { 239 return new StubCompilationIdentifier(this); 240 } 241 242 /** 243 * Checks the conditions a compilation must satisfy to be installed as a RuntimeStub. 244 */ 245 private boolean checkStubInvariants(CompilationResult compResult) { 246 assert compResult.getExceptionHandlers().isEmpty() : this; 247 248 // Stubs cannot be recompiled so they cannot be compiled with 249 // assumptions and there is no point in recording evol_method dependencies 250 assert compResult.getAssumptions() == null : "stubs should not use assumptions: " + this; 251 252 for (DataPatch data : compResult.getDataPatches()) { 253 if (data.reference instanceof ConstantReference) { 254 ConstantReference ref = (ConstantReference) data.reference; 255 if (ref.getConstant() instanceof HotSpotMetaspaceConstant) { 256 HotSpotMetaspaceConstant c = (HotSpotMetaspaceConstant) ref.getConstant(); 257 if (c.asResolvedJavaType() != null && c.asResolvedJavaType().getName().equals("[I")) { 258 // special handling for NewArrayStub 259 // embedding the type '[I' is safe, since it is never unloaded 260 continue; 261 } 262 } 263 } 264 265 assert !(data.reference instanceof ConstantReference) : this + " cannot have embedded object or metadata constant: " + data.reference; 266 } 267 for (Infopoint infopoint : compResult.getInfopoints()) { 268 assert infopoint instanceof Call : this + " cannot have non-call infopoint: " + infopoint; 269 Call call = (Call) infopoint; 270 assert call.target instanceof HotSpotForeignCallLinkage : this + " cannot have non runtime call: " + call.target; 271 HotSpotForeignCallLinkage callLinkage = (HotSpotForeignCallLinkage) call.target; 272 assert !callLinkage.isCompiledStub() || callLinkage.getDescriptor().equals(UNCOMMON_TRAP_HANDLER) : this + " cannot call compiled stub " + callLinkage; 273 } 274 return true; 275 } 276 277 protected Suites createSuites() { 278 Suites defaultSuites = providers.getSuites().getDefaultSuites(); 279 return new Suites(new PhaseSuite<>(), defaultSuites.getMidTier(), defaultSuites.getLowTier()); 280 } 281 282 protected LIRSuites createLIRSuites() { 283 LIRSuites lirSuites = new LIRSuites(providers.getSuites().getDefaultLIRSuites()); 284 ListIterator<LIRPhase<PostAllocationOptimizationContext>> moveProfiling = lirSuites.getPostAllocationOptimizationStage().findPhase(MoveProfilingPhase.class); 285 if (moveProfiling != null) { 286 moveProfiling.remove(); 287 } 288 return lirSuites; 289 } 290 }