package org.graalvm.compiler.lir.aarch64;
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.ILLEGAL;
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.STACK;
import static jdk.vm.ci.aarch64.AArch64.r8;
import static jdk.vm.ci.code.ValueUtil.asRegister;
import static jdk.vm.ci.code.ValueUtil.isRegister;
import org.graalvm.compiler.asm.Label;
import org.graalvm.compiler.asm.aarch64.AArch64Assembler;
import org.graalvm.compiler.asm.aarch64.AArch64MacroAssembler;
import org.graalvm.compiler.core.common.spi.ForeignCallLinkage;
import org.graalvm.compiler.lir.LIRFrameState;
import org.graalvm.compiler.lir.LIRInstructionClass;
import org.graalvm.compiler.lir.Opcode;
import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
import jdk.vm.ci.code.Register;
import jdk.vm.ci.meta.InvokeTarget;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.Value;
public class AArch64Call {
public abstract static class CallOp extends AArch64LIRInstruction {
@Def({REG, ILLEGAL}) protected Value result;
@Use({REG, STACK}) protected Value[] parameters;
@Temp({REG, STACK}) protected Value[] temps;
@State protected LIRFrameState state;
protected CallOp(LIRInstructionClass<? extends CallOp> c, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
super(c);
this.result = result;
this.parameters = parameters;
this.state = state;
this.temps = addStackSlotsToTemporaries(parameters, temps);
assert temps != null;
}
@Override
public boolean destroysCallerSavedRegisters() {
return true;
}
}
public abstract static class MethodCallOp extends CallOp {
protected final ResolvedJavaMethod callTarget;
protected MethodCallOp(LIRInstructionClass<? extends MethodCallOp> c, ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
super(c, result, parameters, temps, state);
this.callTarget = callTarget;
}
}
@Opcode("CALL_INDIRECT")
public static class IndirectCallOp extends MethodCallOp {
public static final LIRInstructionClass<IndirectCallOp> TYPE = LIRInstructionClass.create(IndirectCallOp.class);
@Use({REG}) protected Value targetAddress;
public IndirectCallOp(ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, Value targetAddress, LIRFrameState state) {
this(TYPE, callTarget, result, parameters, temps, targetAddress, state);
}
protected IndirectCallOp(LIRInstructionClass<? extends IndirectCallOp> c, ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, Value targetAddress,
LIRFrameState state) {
super(c, callTarget, result, parameters, temps, state);
this.targetAddress = targetAddress;
}
@Override
public void emitCode(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
Register target = asRegister(targetAddress);
indirectCall(crb, masm, target, callTarget, state);
}
@Override
public void verify() {
super.verify();
assert isRegister(targetAddress) : "The current register allocator cannot handle variables to be used at call sites, " + "it must be in a fixed register for now";
}
}
@Opcode("CALL_DIRECT")
public abstract static class DirectCallOp extends MethodCallOp {
public static final LIRInstructionClass<DirectCallOp> TYPE = LIRInstructionClass.create(DirectCallOp.class);
public DirectCallOp(ResolvedJavaMethod target, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
super(TYPE, target, result, parameters, temps, state);
}
protected DirectCallOp(LIRInstructionClass<? extends DirectCallOp> c, ResolvedJavaMethod callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state) {
super(c, callTarget, result, parameters, temps, state);
}
@Override
public void emitCode(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
directCall(crb, masm, callTarget, null, state);
}
}
public abstract static class ForeignCallOp extends CallOp {
protected final ForeignCallLinkage callTarget;
protected final Label label;
protected ForeignCallOp(LIRInstructionClass<? extends ForeignCallOp> c, ForeignCallLinkage callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state, Label label) {
super(c, result, parameters, temps, state);
this.callTarget = callTarget;
this.label = label;
}
@Override
public boolean destroysCallerSavedRegisters() {
return callTarget.destroysRegisters();
}
@Override
public void emitCode(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
emitCall(crb, masm);
}
protected abstract void emitCall(CompilationResultBuilder crb, AArch64MacroAssembler masm);
}
@Opcode("NEAR_FOREIGN_CALL")
public static class DirectNearForeignCallOp extends ForeignCallOp {
public static final LIRInstructionClass<DirectNearForeignCallOp> TYPE = LIRInstructionClass.create(DirectNearForeignCallOp.class);
public DirectNearForeignCallOp(ForeignCallLinkage callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state, Label label) {
super(TYPE, callTarget, result, parameters, temps, state, label);
}
@Override
protected void emitCall(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
directCall(crb, masm, callTarget, null, state, label);
}
}
@Opcode("FAR_FOREIGN_CALL")
public static class DirectFarForeignCallOp extends ForeignCallOp {
public static final LIRInstructionClass<DirectFarForeignCallOp> TYPE = LIRInstructionClass.create(DirectFarForeignCallOp.class);
public DirectFarForeignCallOp(ForeignCallLinkage callTarget, Value result, Value[] parameters, Value[] temps, LIRFrameState state, Label label) {
super(TYPE, callTarget, result, parameters, temps, state, label);
}
@Override
protected void emitCall(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
directCall(crb, masm, callTarget, r8, state, label);
}
}
public static boolean isNearCall(ForeignCallLinkage linkage) {
long maxOffset = linkage.getMaxCallTargetOffset();
return maxOffset != -1 && AArch64MacroAssembler.isBranchImmediateOffset(maxOffset);
}
public static void directCall(CompilationResultBuilder crb, AArch64MacroAssembler masm, InvokeTarget callTarget, Register scratch, LIRFrameState info) {
directCall(crb, masm, callTarget, scratch, info, null);
}
public static void directCall(CompilationResultBuilder crb, AArch64MacroAssembler masm, InvokeTarget callTarget, Register scratch, LIRFrameState info, Label label) {
int before = masm.position();
if (scratch != null) {
masm.movNativeAddress(scratch, 0L);
masm.blr(scratch);
} else {
masm.bl(0);
}
if (label != null) {
masm.bind(label);
}
int after = masm.position();
crb.recordDirectCall(before, after, callTarget, info);
crb.recordExceptionHandlers(after, info);
masm.ensureUniquePC();
}
public static void indirectCall(CompilationResultBuilder crb, AArch64MacroAssembler masm, Register dst, InvokeTarget callTarget, LIRFrameState info) {
int before = masm.position();
masm.blr(dst);
int after = masm.position();
crb.recordIndirectCall(before, after, callTarget, info);
crb.recordExceptionHandlers(after, info);
masm.ensureUniquePC();
}
public static void directJmp(CompilationResultBuilder crb, AArch64MacroAssembler masm, InvokeTarget target) {
int before = masm.position();
masm.jmp();
int after = masm.position();
crb.recordDirectCall(before, after, target, null);
masm.ensureUniquePC();
}
public static void indirectJmp(CompilationResultBuilder crb, AArch64MacroAssembler masm, Register dst, InvokeTarget target) {
int before = masm.position();
masm.jmp(dst);
int after = masm.position();
crb.recordIndirectCall(before, after, target, null);
masm.ensureUniquePC();
}
public static void directConditionalJmp(CompilationResultBuilder crb, AArch64MacroAssembler masm, InvokeTarget target, AArch64Assembler.ConditionFlag cond) {
int before = masm.position();
masm.branchConditionally(cond);
int after = masm.position();
crb.recordDirectCall(before, after, target, null);
masm.ensureUniquePC();
}
}