package org.graalvm.compiler.hotspot.amd64;
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
import static jdk.vm.ci.amd64.AMD64.rax;
import static jdk.vm.ci.amd64.AMD64.rbp;
import static jdk.vm.ci.amd64.AMD64.rip;
import static jdk.vm.ci.code.ValueUtil.asRegister;
import org.graalvm.compiler.asm.amd64.AMD64Address;
import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler;
import org.graalvm.compiler.hotspot.HotSpotBackend;
import org.graalvm.compiler.lir.LIRInstructionClass;
import org.graalvm.compiler.lir.Opcode;
import org.graalvm.compiler.lir.StandardOp.SaveRegistersOp;
import org.graalvm.compiler.lir.amd64.AMD64LIRInstruction;
import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
import org.graalvm.compiler.lir.framemap.FrameMap;
import jdk.vm.ci.code.Register;
import jdk.vm.ci.code.RegisterConfig;
import jdk.vm.ci.code.RegisterSaveLayout;
import jdk.vm.ci.meta.AllocatableValue;
import jdk.vm.ci.meta.JavaKind;
@Opcode("ENTER_UNPACK_FRAMES_STACK_FRAME")
final class AMD64HotSpotEnterUnpackFramesStackFrameOp extends AMD64LIRInstruction {
public static final LIRInstructionClass<AMD64HotSpotEnterUnpackFramesStackFrameOp> TYPE = LIRInstructionClass.create(AMD64HotSpotEnterUnpackFramesStackFrameOp.class);
private final Register threadRegister;
private final int threadLastJavaSpOffset;
private final int threadLastJavaPcOffset;
private final int threadLastJavaFpOffset;
@Alive(REG) AllocatableValue framePc;
@Alive(REG) AllocatableValue senderSp;
@Alive(REG) AllocatableValue senderFp;
private final SaveRegistersOp saveRegisterOp;
AMD64HotSpotEnterUnpackFramesStackFrameOp(Register threadRegister, int threadLastJavaSpOffset, int threadLastJavaPcOffset, int threadLastJavaFpOffset, AllocatableValue framePc,
AllocatableValue senderSp, AllocatableValue senderFp, SaveRegistersOp saveRegisterOp) {
super(TYPE);
this.threadRegister = threadRegister;
this.threadLastJavaSpOffset = threadLastJavaSpOffset;
this.threadLastJavaPcOffset = threadLastJavaPcOffset;
this.threadLastJavaFpOffset = threadLastJavaFpOffset;
this.framePc = framePc;
this.senderSp = senderSp;
this.senderFp = senderFp;
this.saveRegisterOp = saveRegisterOp;
}
@Override
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
FrameMap frameMap = crb.frameMap;
RegisterConfig registerConfig = frameMap.getRegisterConfig();
RegisterSaveLayout registerSaveLayout = saveRegisterOp.getMap(frameMap);
Register stackPointerRegister = registerConfig.getFrameRegister();
final int totalFrameSize = frameMap.totalFrameSize();
masm.push(asRegister(framePc));
masm.push(asRegister(senderFp));
masm.movq(rbp, stackPointerRegister);
masm.decrementq(stackPointerRegister, totalFrameSize - 2 * crb.target.wordSize);
final int stackSlotSize = frameMap.getTarget().wordSize;
Register integerResultRegister = registerConfig.getReturnRegister(JavaKind.Long);
masm.movptr(new AMD64Address(stackPointerRegister, registerSaveLayout.registerToSlot(integerResultRegister) * stackSlotSize), integerResultRegister);
Register floatResultRegister = registerConfig.getReturnRegister(JavaKind.Double);
masm.movdbl(new AMD64Address(stackPointerRegister, registerSaveLayout.registerToSlot(floatResultRegister) * stackSlotSize), floatResultRegister);
masm.movq(new AMD64Address(threadRegister, threadLastJavaSpOffset), stackPointerRegister);
masm.leaq(rax, new AMD64Address(rip, 0));
masm.movq(new AMD64Address(threadRegister, threadLastJavaPcOffset), rax);
masm.movq(new AMD64Address(threadRegister, threadLastJavaFpOffset), rbp);
masm.andq(stackPointerRegister, -(crb.target.stackAlignment));
}
}