2018-10-30 21:43:02 -04:00
|
|
|
using ChocolArm64.Decoders;
|
2019-04-26 00:55:12 -04:00
|
|
|
using ChocolArm64.IntermediateRepresentation;
|
2018-10-30 21:43:02 -04:00
|
|
|
using ChocolArm64.State;
|
|
|
|
using ChocolArm64.Translation;
|
|
|
|
using System.Reflection.Emit;
|
|
|
|
|
2019-02-04 16:26:05 -05:00
|
|
|
using static ChocolArm64.Instructions.InstEmitFlowHelper;
|
|
|
|
|
2018-10-30 21:43:02 -04:00
|
|
|
namespace ChocolArm64.Instructions
|
|
|
|
{
|
|
|
|
static partial class InstEmit
|
|
|
|
{
|
|
|
|
public static void B(ILEmitterCtx context)
|
|
|
|
{
|
|
|
|
OpCodeBImmAl64 op = (OpCodeBImmAl64)context.CurrOp;
|
|
|
|
|
|
|
|
if (context.CurrBlock.Branch != null)
|
|
|
|
{
|
|
|
|
context.Emit(OpCodes.Br, context.GetLabel(op.Imm));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-26 00:55:12 -04:00
|
|
|
context.EmitStoreContext();
|
2018-10-30 21:43:02 -04:00
|
|
|
context.EmitLdc_I8(op.Imm);
|
|
|
|
|
|
|
|
context.Emit(OpCodes.Ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void B_Cond(ILEmitterCtx context)
|
|
|
|
{
|
|
|
|
OpCodeBImmCond64 op = (OpCodeBImmCond64)context.CurrOp;
|
|
|
|
|
|
|
|
EmitBranch(context, op.Cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Bl(ILEmitterCtx context)
|
|
|
|
{
|
|
|
|
OpCodeBImmAl64 op = (OpCodeBImmAl64)context.CurrOp;
|
|
|
|
|
|
|
|
context.EmitLdc_I(op.Position + 4);
|
2019-01-24 20:59:53 -05:00
|
|
|
context.EmitStint(RegisterAlias.Lr);
|
2018-10-30 21:43:02 -04:00
|
|
|
|
2019-02-04 16:26:05 -05:00
|
|
|
EmitCall(context, op.Imm);
|
2018-10-30 21:43:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void Blr(ILEmitterCtx context)
|
|
|
|
{
|
|
|
|
OpCodeBReg64 op = (OpCodeBReg64)context.CurrOp;
|
|
|
|
|
2018-11-09 13:41:20 -05:00
|
|
|
context.EmitLdintzr(op.Rn);
|
2018-10-30 21:43:02 -04:00
|
|
|
context.EmitLdc_I(op.Position + 4);
|
2019-01-24 20:59:53 -05:00
|
|
|
context.EmitStint(RegisterAlias.Lr);
|
2019-04-26 00:55:12 -04:00
|
|
|
context.EmitStoreContext();
|
2018-10-30 21:43:02 -04:00
|
|
|
|
2019-02-04 16:26:05 -05:00
|
|
|
EmitVirtualCall(context);
|
2018-10-30 21:43:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void Br(ILEmitterCtx context)
|
|
|
|
{
|
|
|
|
OpCodeBReg64 op = (OpCodeBReg64)context.CurrOp;
|
|
|
|
|
2019-02-27 21:03:31 -05:00
|
|
|
context.HasIndirectJump = true;
|
|
|
|
|
2019-04-26 00:55:12 -04:00
|
|
|
context.EmitStoreContext();
|
2018-10-30 21:43:02 -04:00
|
|
|
context.EmitLdintzr(op.Rn);
|
|
|
|
|
2019-02-04 16:26:05 -05:00
|
|
|
EmitVirtualJump(context);
|
2018-10-30 21:43:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void Cbnz(ILEmitterCtx context) => EmitCb(context, OpCodes.Bne_Un);
|
|
|
|
public static void Cbz(ILEmitterCtx context) => EmitCb(context, OpCodes.Beq);
|
|
|
|
|
|
|
|
private static void EmitCb(ILEmitterCtx context, OpCode ilOp)
|
|
|
|
{
|
|
|
|
OpCodeBImmCmp64 op = (OpCodeBImmCmp64)context.CurrOp;
|
|
|
|
|
|
|
|
context.EmitLdintzr(op.Rt);
|
|
|
|
context.EmitLdc_I(0);
|
|
|
|
|
|
|
|
EmitBranch(context, ilOp);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Ret(ILEmitterCtx context)
|
|
|
|
{
|
2019-04-26 00:55:12 -04:00
|
|
|
context.EmitStoreContext();
|
2019-01-24 20:59:53 -05:00
|
|
|
context.EmitLdint(RegisterAlias.Lr);
|
2018-10-30 21:43:02 -04:00
|
|
|
|
|
|
|
context.Emit(OpCodes.Ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Tbnz(ILEmitterCtx context) => EmitTb(context, OpCodes.Bne_Un);
|
|
|
|
public static void Tbz(ILEmitterCtx context) => EmitTb(context, OpCodes.Beq);
|
|
|
|
|
|
|
|
private static void EmitTb(ILEmitterCtx context, OpCode ilOp)
|
|
|
|
{
|
|
|
|
OpCodeBImmTest64 op = (OpCodeBImmTest64)context.CurrOp;
|
|
|
|
|
|
|
|
context.EmitLdintzr(op.Rt);
|
|
|
|
context.EmitLdc_I(1L << op.Pos);
|
|
|
|
|
|
|
|
context.Emit(OpCodes.And);
|
|
|
|
|
|
|
|
context.EmitLdc_I(0);
|
|
|
|
|
|
|
|
EmitBranch(context, ilOp);
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:59:53 -05:00
|
|
|
private static void EmitBranch(ILEmitterCtx context, Condition cond)
|
2018-10-30 21:43:02 -04:00
|
|
|
{
|
|
|
|
OpCodeBImm64 op = (OpCodeBImm64)context.CurrOp;
|
|
|
|
|
2019-02-04 16:26:05 -05:00
|
|
|
if (context.CurrBlock.Branch != null)
|
2018-10-30 21:43:02 -04:00
|
|
|
{
|
|
|
|
context.EmitCondBranch(context.GetLabel(op.Imm), cond);
|
2019-02-04 16:26:05 -05:00
|
|
|
|
|
|
|
if (context.CurrBlock.Next == null)
|
|
|
|
{
|
2019-04-26 00:55:12 -04:00
|
|
|
context.EmitStoreContext();
|
2019-02-04 16:26:05 -05:00
|
|
|
context.EmitLdc_I8(op.Position + 4);
|
|
|
|
|
|
|
|
context.Emit(OpCodes.Ret);
|
|
|
|
}
|
2018-10-30 21:43:02 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-26 00:55:12 -04:00
|
|
|
context.EmitStoreContext();
|
2018-10-30 21:43:02 -04:00
|
|
|
|
|
|
|
ILLabel lblTaken = new ILLabel();
|
|
|
|
|
|
|
|
context.EmitCondBranch(lblTaken, cond);
|
|
|
|
|
|
|
|
context.EmitLdc_I8(op.Position + 4);
|
|
|
|
|
|
|
|
context.Emit(OpCodes.Ret);
|
|
|
|
|
|
|
|
context.MarkLabel(lblTaken);
|
|
|
|
|
|
|
|
context.EmitLdc_I8(op.Imm);
|
|
|
|
|
|
|
|
context.Emit(OpCodes.Ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void EmitBranch(ILEmitterCtx context, OpCode ilOp)
|
|
|
|
{
|
|
|
|
OpCodeBImm64 op = (OpCodeBImm64)context.CurrOp;
|
|
|
|
|
2019-02-04 16:26:05 -05:00
|
|
|
if (context.CurrBlock.Branch != null)
|
2018-10-30 21:43:02 -04:00
|
|
|
{
|
|
|
|
context.Emit(ilOp, context.GetLabel(op.Imm));
|
2019-02-04 16:26:05 -05:00
|
|
|
|
|
|
|
if (context.CurrBlock.Next == null)
|
|
|
|
{
|
2019-04-26 00:55:12 -04:00
|
|
|
context.EmitStoreContext();
|
2019-02-04 16:26:05 -05:00
|
|
|
context.EmitLdc_I8(op.Position + 4);
|
|
|
|
|
|
|
|
context.Emit(OpCodes.Ret);
|
|
|
|
}
|
2018-10-30 21:43:02 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-26 00:55:12 -04:00
|
|
|
context.EmitStoreContext();
|
2018-10-30 21:43:02 -04:00
|
|
|
|
|
|
|
ILLabel lblTaken = new ILLabel();
|
|
|
|
|
|
|
|
context.Emit(ilOp, lblTaken);
|
|
|
|
|
|
|
|
context.EmitLdc_I8(op.Position + 4);
|
|
|
|
|
|
|
|
context.Emit(OpCodes.Ret);
|
|
|
|
|
|
|
|
context.MarkLabel(lblTaken);
|
|
|
|
|
|
|
|
context.EmitLdc_I8(op.Imm);
|
|
|
|
|
|
|
|
context.Emit(OpCodes.Ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|