package org.graalvm.compiler.hotspot.stubs;
import static org.graalvm.compiler.core.GraalCompiler.emitBackEnd;
import static org.graalvm.compiler.core.GraalCompiler.emitFrontEnd;
import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
import static org.graalvm.compiler.hotspot.HotSpotHostBackend.UNCOMMON_TRAP_HANDLER;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import org.graalvm.compiler.code.CompilationResult;
import org.graalvm.compiler.core.common.CompilationIdentifier;
import org.graalvm.compiler.core.target.Backend;
import org.graalvm.compiler.debug.Debug;
import org.graalvm.compiler.debug.Debug.Scope;
import org.graalvm.compiler.debug.internal.DebugScope;
import org.graalvm.compiler.hotspot.HotSpotCompiledCodeBuilder;
import org.graalvm.compiler.hotspot.HotSpotForeignCallLinkage;
import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
import org.graalvm.compiler.hotspot.nodes.StubStartNode;
import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory;
import org.graalvm.compiler.lir.phases.LIRPhase;
import org.graalvm.compiler.lir.phases.LIRSuites;
import org.graalvm.compiler.lir.phases.PostAllocationOptimizationPhase.PostAllocationOptimizationContext;
import org.graalvm.compiler.lir.profiling.MoveProfilingPhase;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.phases.OptimisticOptimizations;
import org.graalvm.compiler.phases.PhaseSuite;
import org.graalvm.compiler.phases.tiers.Suites;
import jdk.vm.ci.code.CodeCacheProvider;
import jdk.vm.ci.code.InstalledCode;
import jdk.vm.ci.code.Register;
import jdk.vm.ci.code.RegisterConfig;
import jdk.vm.ci.code.site.Call;
import jdk.vm.ci.code.site.ConstantReference;
import jdk.vm.ci.code.site.DataPatch;
import jdk.vm.ci.code.site.Infopoint;
import jdk.vm.ci.hotspot.HotSpotCompiledCode;
import jdk.vm.ci.hotspot.HotSpotMetaspaceConstant;
import jdk.vm.ci.meta.DefaultProfilingInfo;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.TriState;
public abstract class Stub {
private static final List<Stub> stubs = new ArrayList<>();
protected final HotSpotForeignCallLinkage linkage;
protected InstalledCode code;
private Set<Register> destroyedCallerRegisters;
public void initDestroyedCallerRegisters(Set<Register> registers) {
assert registers != null;
assert destroyedCallerRegisters == null || registers.equals(destroyedCallerRegisters) : "cannot redefine";
destroyedCallerRegisters = registers;
}
public Set<Register> getDestroyedCallerRegisters() {
assert destroyedCallerRegisters != null : "not yet initialized";
return destroyedCallerRegisters;
}
public boolean preservesRegisters() {
return true;
}
protected final HotSpotProviders providers;
public Stub(HotSpotProviders providers, HotSpotForeignCallLinkage linkage) {
this.linkage = linkage;
this.providers = providers;
stubs.add(this);
}
public static Collection<Stub> getStubs() {
return Collections.unmodifiableList(stubs);
}
public HotSpotForeignCallLinkage getLinkage() {
return linkage;
}
public RegisterConfig getRegisterConfig() {
return null;
}
protected abstract StructuredGraph getGraph(CompilationIdentifier compilationId);
@Override
public String toString() {
return "Stub<" + linkage.getDescriptor() + ">";
}
protected abstract ResolvedJavaMethod getInstalledCodeOwner();
protected abstract Object debugScopeContext();
@SuppressWarnings("try")
public synchronized InstalledCode getCode(final Backend backend) {
if (code == null) {
try (Scope d = Debug.sandbox("CompilingStub", DebugScope.getConfig(), providers.getCodeCache(), debugScopeContext())) {
CodeCacheProvider codeCache = providers.getCodeCache();
CompilationResult compResult = buildCompilationResult(backend);
try (Scope s = Debug.scope("CodeInstall", compResult)) {
assert destroyedCallerRegisters != null;
HotSpotCompiledCode compiledCode = HotSpotCompiledCodeBuilder.createCompiledCode(null, null, compResult);
code = codeCache.installCode(null, compiledCode, null, null, false);
} catch (Throwable e) {
throw Debug.handle(e);
}
} catch (Throwable e) {
throw Debug.handle(e);
}
assert code != null : "error installing stub " + this;
}
return code;
}
@SuppressWarnings("try")
private CompilationResult buildCompilationResult(final Backend backend) {
CompilationResult compResult = new CompilationResult(toString(), GeneratePIC.getValue());
final StructuredGraph graph = getGraph(getStubCompilationId());
assert graph.getAssumptions() == null;
if (!(graph.start() instanceof StubStartNode)) {
StubStartNode newStart = graph.add(new StubStartNode(Stub.this));
newStart.setStateAfter(graph.start().stateAfter());
graph.replaceFixed(graph.start(), newStart);
}
try (Scope s0 = Debug.scope("StubCompilation", graph, providers.getCodeCache())) {
Suites suites = createSuites();
emitFrontEnd(providers, backend, graph, providers.getSuites().getDefaultGraphBuilderSuite(), OptimisticOptimizations.ALL, DefaultProfilingInfo.get(TriState.UNKNOWN), suites);
LIRSuites lirSuites = createLIRSuites();
emitBackEnd(graph, Stub.this, getInstalledCodeOwner(), backend, compResult, CompilationResultBuilderFactory.Default, getRegisterConfig(), lirSuites);
assert checkStubInvariants(compResult);
} catch (Throwable e) {
throw Debug.handle(e);
}
return compResult;
}
@SuppressWarnings("try")
public CompilationResult getCompilationResult(final Backend backend) {
try (Scope d = Debug.sandbox("CompilingStub", DebugScope.getConfig(), providers.getCodeCache(), debugScopeContext())) {
return buildCompilationResult(backend);
} catch (Throwable e) {
throw Debug.handle(e);
}
}
public CompilationIdentifier getStubCompilationId() {
return new StubCompilationIdentifier(this);
}
private boolean checkStubInvariants(CompilationResult compResult) {
assert compResult.getExceptionHandlers().isEmpty() : this;
assert compResult.getAssumptions() == null : "stubs should not use assumptions: " + this;
for (DataPatch data : compResult.getDataPatches()) {
if (data.reference instanceof ConstantReference) {
ConstantReference ref = (ConstantReference) data.reference;
if (ref.getConstant() instanceof HotSpotMetaspaceConstant) {
HotSpotMetaspaceConstant c = (HotSpotMetaspaceConstant) ref.getConstant();
if (c.asResolvedJavaType() != null && c.asResolvedJavaType().getName().equals("[I")) {
continue;
}
}
}
assert !(data.reference instanceof ConstantReference) : this + " cannot have embedded object or metadata constant: " + data.reference;
}
for (Infopoint infopoint : compResult.getInfopoints()) {
assert infopoint instanceof Call : this + " cannot have non-call infopoint: " + infopoint;
Call call = (Call) infopoint;
assert call.target instanceof HotSpotForeignCallLinkage : this + " cannot have non runtime call: " + call.target;
HotSpotForeignCallLinkage callLinkage = (HotSpotForeignCallLinkage) call.target;
assert !callLinkage.isCompiledStub() || callLinkage.getDescriptor().equals(UNCOMMON_TRAP_HANDLER) : this + " cannot call compiled stub " + callLinkage;
}
return true;
}
protected Suites createSuites() {
Suites defaultSuites = providers.getSuites().getDefaultSuites();
return new Suites(new PhaseSuite<>(), defaultSuites.getMidTier(), defaultSuites.getLowTier());
}
protected LIRSuites createLIRSuites() {
LIRSuites lirSuites = new LIRSuites(providers.getSuites().getDefaultLIRSuites());
ListIterator<LIRPhase<PostAllocationOptimizationContext>> moveProfiling = lirSuites.getPostAllocationOptimizationStage().findPhase(MoveProfilingPhase.class);
if (moveProfiling != null) {
moveProfiling.remove();
}
return lirSuites;
}
}