package org.graalvm.compiler.hotspot.amd64;
import static org.graalvm.compiler.core.common.GraalOptions.CanOmitFrame;
import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
import static org.graalvm.compiler.core.common.GraalOptions.ZapStackOnMethodEntry;
import static jdk.vm.ci.amd64.AMD64.r10;
import static jdk.vm.ci.amd64.AMD64.rax;
import static jdk.vm.ci.amd64.AMD64.rsp;
import static jdk.vm.ci.code.ValueUtil.asRegister;
import java.util.Set;
import org.graalvm.compiler.asm.Assembler;
import org.graalvm.compiler.asm.Label;
import org.graalvm.compiler.asm.amd64.AMD64Address;
import org.graalvm.compiler.asm.amd64.AMD64Assembler.ConditionFlag;
import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler;
import org.graalvm.compiler.code.CompilationResult;
import org.graalvm.compiler.core.amd64.AMD64NodeMatchRules;
import org.graalvm.compiler.core.common.LIRKind;
import org.graalvm.compiler.core.common.CompilationIdentifier;
import org.graalvm.compiler.core.common.alloc.RegisterAllocationConfig;
import org.graalvm.compiler.core.target.Backend;
import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
import org.graalvm.compiler.hotspot.HotSpotDataBuilder;
import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
import org.graalvm.compiler.hotspot.HotSpotHostBackend;
import org.graalvm.compiler.hotspot.HotSpotLIRGenerationResult;
import org.graalvm.compiler.hotspot.meta.HotSpotConstantLoadAction;
import org.graalvm.compiler.hotspot.meta.HotSpotForeignCallsProvider;
import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
import org.graalvm.compiler.hotspot.stubs.Stub;
import org.graalvm.compiler.lir.LIR;
import org.graalvm.compiler.lir.amd64.AMD64Call;
import org.graalvm.compiler.lir.amd64.AMD64FrameMap;
import org.graalvm.compiler.lir.amd64.AMD64FrameMapBuilder;
import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory;
import org.graalvm.compiler.lir.asm.DataBuilder;
import org.graalvm.compiler.lir.asm.FrameContext;
import org.graalvm.compiler.lir.framemap.FrameMap;
import org.graalvm.compiler.lir.framemap.FrameMapBuilder;
import org.graalvm.compiler.lir.gen.LIRGenerationResult;
import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool;
import jdk.vm.ci.amd64.AMD64;
import jdk.vm.ci.amd64.AMD64Kind;
import jdk.vm.ci.code.CallingConvention;
import jdk.vm.ci.code.Register;
import jdk.vm.ci.code.RegisterConfig;
import jdk.vm.ci.code.StackSlot;
import jdk.vm.ci.hotspot.HotSpotCallingConventionType;
import jdk.vm.ci.hotspot.HotSpotSentinelConstant;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.JavaType;
import jdk.vm.ci.meta.ResolvedJavaMethod;
public class AMD64HotSpotBackend extends HotSpotHostBackend {
public AMD64HotSpotBackend(GraalHotSpotVMConfig config, HotSpotGraalRuntimeProvider runtime, HotSpotProviders providers) {
super(config, runtime, providers);
}
@Override
public FrameMapBuilder newFrameMapBuilder(RegisterConfig registerConfig) {
RegisterConfig registerConfigNonNull = registerConfig == null ? getCodeCache().getRegisterConfig() : registerConfig;
return new AMD64FrameMapBuilder(newFrameMap(registerConfigNonNull), getCodeCache(), registerConfigNonNull);
}
@Override
public FrameMap newFrameMap(RegisterConfig registerConfig) {
return new AMD64FrameMap(getCodeCache(), registerConfig, this);
}
@Override
public LIRGeneratorTool newLIRGenerator(LIRGenerationResult lirGenRes) {
return new AMD64HotSpotLIRGenerator(getProviders(), config, lirGenRes);
}
@Override
public LIRGenerationResult newLIRGenerationResult(CompilationIdentifier compilationId, LIR lir, FrameMapBuilder frameMapBuilder, StructuredGraph graph, Object stub) {
return new HotSpotLIRGenerationResult(compilationId, lir, frameMapBuilder, makeCallingConvention(graph, (Stub) stub), stub);
}
@Override
public NodeLIRBuilderTool newNodeLIRBuilder(StructuredGraph graph, LIRGeneratorTool lirGen) {
return new AMD64HotSpotNodeLIRBuilder(graph, lirGen, new AMD64NodeMatchRules(lirGen));
}
@Override
protected void bangStackWithOffset(CompilationResultBuilder crb, int bangOffset) {
AMD64MacroAssembler asm = (AMD64MacroAssembler) crb.asm;
int pos = asm.position();
asm.movl(new AMD64Address(rsp, -bangOffset), AMD64.rax);
assert asm.position() - pos >= PATCHED_VERIFIED_ENTRY_POINT_INSTRUCTION_SIZE;
}
public static final int PATCHED_VERIFIED_ENTRY_POINT_INSTRUCTION_SIZE = 5;
class HotSpotFrameContext implements FrameContext {
final boolean isStub;
final boolean omitFrame;
HotSpotFrameContext(boolean isStub, boolean omitFrame) {
this.isStub = isStub;
this.omitFrame = omitFrame;
}
@Override
public boolean hasFrame() {
return !omitFrame;
}
@Override
public void enter(CompilationResultBuilder crb) {
FrameMap frameMap = crb.frameMap;
int frameSize = frameMap.frameSize();
AMD64MacroAssembler asm = (AMD64MacroAssembler) crb.asm;
if (omitFrame) {
if (!isStub) {
asm.nop(PATCHED_VERIFIED_ENTRY_POINT_INSTRUCTION_SIZE);
}
} else {
int verifiedEntryPointOffset = asm.position();
if (!isStub) {
emitStackOverflowCheck(crb);
}
if (!isStub && asm.position() == verifiedEntryPointOffset) {
asm.subqWide(rsp, frameSize);
assert asm.position() - verifiedEntryPointOffset >= PATCHED_VERIFIED_ENTRY_POINT_INSTRUCTION_SIZE;
} else {
asm.decrementq(rsp, frameSize);
}
if (ZapStackOnMethodEntry.getValue()) {
final int intSize = 4;
for (int i = 0; i < frameSize / intSize; ++i) {
asm.movl(new AMD64Address(rsp, i * intSize), 0xC1C1C1C1);
}
}
assert frameMap.getRegisterConfig().getCalleeSaveRegisters() == null;
}
}
@Override
public void leave(CompilationResultBuilder crb) {
if (!omitFrame) {
AMD64MacroAssembler asm = (AMD64MacroAssembler) crb.asm;
assert crb.frameMap.getRegisterConfig().getCalleeSaveRegisters() == null;
int frameSize = crb.frameMap.frameSize();
asm.incrementq(rsp, frameSize);
}
}
}
@Override
protected Assembler createAssembler(FrameMap frameMap) {
return new AMD64MacroAssembler(getTarget());
}
@Override
public CompilationResultBuilder newCompilationResultBuilder(LIRGenerationResult lirGenRen, FrameMap frameMap, CompilationResult compilationResult, CompilationResultBuilderFactory factory) {
HotSpotLIRGenerationResult gen = (HotSpotLIRGenerationResult) lirGenRen;
LIR lir = gen.getLIR();
assert gen.getDeoptimizationRescueSlot() == null || frameMap.frameNeedsAllocating() : "method that can deoptimize must have a frame";
boolean omitFrame = CanOmitFrame.getValue() && !frameMap.frameNeedsAllocating() && !lir.hasArgInCallerFrame() && !gen.hasForeignCall();
Stub stub = gen.getStub();
Assembler masm = createAssembler(frameMap);
HotSpotFrameContext frameContext = new HotSpotFrameContext(stub != null, omitFrame);
DataBuilder dataBuilder = new HotSpotDataBuilder(getCodeCache().getTarget());
CompilationResultBuilder crb = factory.createBuilder(getCodeCache(), getForeignCalls(), frameMap, masm, dataBuilder, frameContext, compilationResult);
crb.setTotalFrameSize(frameMap.totalFrameSize());
crb.setMaxInterpreterFrameSize(gen.getMaxInterpreterFrameSize());
StackSlot deoptimizationRescueSlot = gen.getDeoptimizationRescueSlot();
if (deoptimizationRescueSlot != null && stub == null) {
crb.compilationResult.setCustomStackAreaOffset(deoptimizationRescueSlot);
}
if (stub != null) {
Set<Register> destroyedCallerRegisters = gatherDestroyedCallerRegisters(lir);
updateStub(stub, destroyedCallerRegisters, gen.getCalleeSaveInfo(), frameMap);
}
return crb;
}
@Override
public void emitCode(CompilationResultBuilder crb, LIR lir, ResolvedJavaMethod installedCodeOwner) {
AMD64MacroAssembler asm = (AMD64MacroAssembler) crb.asm;
FrameMap frameMap = crb.frameMap;
RegisterConfig regConfig = frameMap.getRegisterConfig();
Label verifiedEntry = new Label();
emitCodePrefix(installedCodeOwner, crb, asm, regConfig, verifiedEntry);
emitCodeBody(installedCodeOwner, crb, lir);
emitCodeSuffix(installedCodeOwner, crb, asm, frameMap);
profileInstructions(lir, crb);
}
public void emitCodePrefix(ResolvedJavaMethod installedCodeOwner, CompilationResultBuilder crb, AMD64MacroAssembler asm, RegisterConfig regConfig, Label verifiedEntry) {
HotSpotProviders providers = getProviders();
if (installedCodeOwner != null && !installedCodeOwner.isStatic()) {
crb.recordMark(config.MARKID_UNVERIFIED_ENTRY);
CallingConvention cc = regConfig.getCallingConvention(HotSpotCallingConventionType.JavaCallee, null, new JavaType[]{providers.getMetaAccess().lookupJavaType(Object.class)}, this);
Register inlineCacheKlass = rax;
Register receiver = asRegister(cc.getArgument(0));
AMD64Address src = new AMD64Address(receiver, config.hubOffset);
if (config.useCompressedClassPointers) {
Register register = r10;
AMD64HotSpotMove.decodeKlassPointer(crb, asm, register, providers.getRegisters().getHeapBaseRegister(), src, config);
if (GeneratePIC.getValue()) {
asm.movq(providers.getRegisters().getHeapBaseRegister(), asm.getPlaceholder(-1));
crb.recordMark(config.MARKID_NARROW_OOP_BASE_ADDRESS);
} else {
if (config.narrowKlassBase != 0) {
asm.movq(providers.getRegisters().getHeapBaseRegister(), config.narrowOopBase);
}
}
asm.cmpq(inlineCacheKlass, register);
} else {
asm.cmpq(inlineCacheKlass, src);
}
AMD64Call.directConditionalJmp(crb, asm, getForeignCalls().lookupForeignCall(IC_MISS_HANDLER), ConditionFlag.NotEqual);
}
asm.align(config.codeEntryAlignment);
crb.recordMark(config.MARKID_OSR_ENTRY);
asm.bind(verifiedEntry);
crb.recordMark(config.MARKID_VERIFIED_ENTRY);
if (GeneratePIC.getValue()) {
HotSpotFrameContext frameContext = (HotSpotFrameContext) crb.frameContext;
if (!frameContext.isStub) {
crb.recordInlineDataInCodeWithNote(new HotSpotSentinelConstant(LIRKind.value(AMD64Kind.QWORD), JavaKind.Long), HotSpotConstantLoadAction.MAKE_NOT_ENTRANT);
asm.movq(AMD64.rax, asm.getPlaceholder(-1));
asm.testq(AMD64.rax, AMD64.rax);
AMD64Call.directConditionalJmp(crb, asm, getForeignCalls().lookupForeignCall(WRONG_METHOD_HANDLER), ConditionFlag.NotZero);
}
}
}
public void emitCodeBody(ResolvedJavaMethod installedCodeOwner, CompilationResultBuilder crb, LIR lir) {
crb.emit(lir);
}
public void emitCodeSuffix(ResolvedJavaMethod installedCodeOwner, CompilationResultBuilder crb, AMD64MacroAssembler asm, FrameMap frameMap) {
HotSpotProviders providers = getProviders();
HotSpotFrameContext frameContext = (HotSpotFrameContext) crb.frameContext;
if (!frameContext.isStub) {
HotSpotForeignCallsProvider foreignCalls = providers.getForeignCalls();
crb.recordMark(config.MARKID_EXCEPTION_HANDLER_ENTRY);
AMD64Call.directCall(crb, asm, foreignCalls.lookupForeignCall(EXCEPTION_HANDLER), null, false, null);
crb.recordMark(config.MARKID_DEOPT_HANDLER_ENTRY);
AMD64Call.directCall(crb, asm, foreignCalls.lookupForeignCall(DEOPTIMIZATION_HANDLER), null, false, null);
} else {
if (frameContext.omitFrame) {
assert !frameMap.accessesCallerFrame();
}
}
}
@Override
public RegisterAllocationConfig newRegisterAllocationConfig(RegisterConfig registerConfig) {
RegisterConfig registerConfigNonNull = registerConfig == null ? getCodeCache().getRegisterConfig() : registerConfig;
return new AMD64HotSpotRegisterAllocationConfig(registerConfigNonNull);
}
@Override
public Set<Register> translateToCallerRegisters(Set<Register> calleeRegisters) {
return calleeRegisters;
}
}