| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- using ChocolArm64.Decoders;
- using ChocolArm64.State;
- using ChocolArm64.Translation;
- using System;
- using System.Reflection.Emit;
- namespace ChocolArm64.Instructions
- {
- static class InstEmitAluHelper
- {
- public static void EmitAdcsCCheck(ILEmitterCtx context)
- {
- // C = (Rd == Rn && CIn) || Rd < Rn
- context.EmitSttmp();
- context.EmitLdtmp();
- context.EmitLdtmp();
- EmitAluLoadRn(context);
- context.Emit(OpCodes.Ceq);
- context.EmitLdflg((int)PState.CBit);
- context.Emit(OpCodes.And);
- context.EmitLdtmp();
- EmitAluLoadRn(context);
- context.Emit(OpCodes.Clt_Un);
- context.Emit(OpCodes.Or);
- context.EmitStflg((int)PState.CBit);
- }
- public static void EmitAddsCCheck(ILEmitterCtx context)
- {
- // C = Rd < Rn
- context.Emit(OpCodes.Dup);
- EmitAluLoadRn(context);
- context.Emit(OpCodes.Clt_Un);
- context.EmitStflg((int)PState.CBit);
- }
- public static void EmitAddsVCheck(ILEmitterCtx context)
- {
- // V = (Rd ^ Rn) & ~(Rn ^ Rm) < 0
- context.Emit(OpCodes.Dup);
- EmitAluLoadRn(context);
- context.Emit(OpCodes.Xor);
- EmitAluLoadOpers(context);
- context.Emit(OpCodes.Xor);
- context.Emit(OpCodes.Not);
- context.Emit(OpCodes.And);
- context.EmitLdc_I(0);
- context.Emit(OpCodes.Clt);
- context.EmitStflg((int)PState.VBit);
- }
- public static void EmitSbcsCCheck(ILEmitterCtx context)
- {
- // C = (Rn == Rm && CIn) || Rn > Rm
- EmitAluLoadOpers(context);
- context.Emit(OpCodes.Ceq);
- context.EmitLdflg((int)PState.CBit);
- context.Emit(OpCodes.And);
- EmitAluLoadOpers(context);
- context.Emit(OpCodes.Cgt_Un);
- context.Emit(OpCodes.Or);
- context.EmitStflg((int)PState.CBit);
- }
- public static void EmitSubsCCheck(ILEmitterCtx context)
- {
- // C = Rn == Rm || Rn > Rm = !(Rn < Rm)
- EmitAluLoadOpers(context);
- context.Emit(OpCodes.Clt_Un);
- context.EmitLdc_I4(1);
- context.Emit(OpCodes.Xor);
- context.EmitStflg((int)PState.CBit);
- }
- public static void EmitSubsVCheck(ILEmitterCtx context)
- {
- // V = (Rd ^ Rn) & (Rn ^ Rm) < 0
- context.Emit(OpCodes.Dup);
- EmitAluLoadRn(context);
- context.Emit(OpCodes.Xor);
- EmitAluLoadOpers(context);
- context.Emit(OpCodes.Xor);
- context.Emit(OpCodes.And);
- context.EmitLdc_I(0);
- context.Emit(OpCodes.Clt);
- context.EmitStflg((int)PState.VBit);
- }
- public static void EmitAluLoadRm(ILEmitterCtx context)
- {
- if (context.CurrOp is IOpCodeAluRs64 op)
- {
- context.EmitLdintzr(op.Rm);
- }
- else if (context.CurrOp is OpCode32AluRsImm op32)
- {
- InstEmit32Helper.EmitLoadFromRegister(context, op32.Rm);
- }
- else
- {
- throw new InvalidOperationException();
- }
- }
- public static void EmitAluLoadOpers(ILEmitterCtx context, bool setCarry = true)
- {
- EmitAluLoadRn(context);
- EmitAluLoadOper2(context, setCarry);
- }
- public static void EmitAluLoadRn(ILEmitterCtx context)
- {
- if (context.CurrOp is IOpCodeAlu64 op)
- {
- if (op.DataOp == DataOp.Logical || op is IOpCodeAluRs64)
- {
- context.EmitLdintzr(op.Rn);
- }
- else
- {
- context.EmitLdint(op.Rn);
- }
- }
- else if (context.CurrOp is IOpCode32Alu op32)
- {
- InstEmit32Helper.EmitLoadFromRegister(context, op32.Rn);
- }
- else
- {
- throw new InvalidOperationException();
- }
- }
- public static void EmitAluLoadOper2(ILEmitterCtx context, bool setCarry = true)
- {
- switch (context.CurrOp)
- {
- // ARM32.
- case OpCode32AluImm op:
- context.EmitLdc_I4(op.Imm);
- if (op.SetFlags && op.IsRotated)
- {
- context.EmitLdc_I4((int)((uint)op.Imm >> 31));
- context.EmitStflg((int)PState.CBit);
- }
- break;
- case OpCode32AluRsImm op:
- EmitLoadRmShiftedByImmediate(context, op, setCarry);
- break;
- case OpCodeT16AluImm8 op:
- context.EmitLdc_I4(op.Imm);
- break;
- // ARM64.
- case IOpCodeAluImm64 op:
- context.EmitLdc_I(op.Imm);
- break;
- case IOpCodeAluRs64 op:
- context.EmitLdintzr(op.Rm);
- switch (op.ShiftType)
- {
- case ShiftType.Lsl: context.EmitLsl(op.Shift); break;
- case ShiftType.Lsr: context.EmitLsr(op.Shift); break;
- case ShiftType.Asr: context.EmitAsr(op.Shift); break;
- case ShiftType.Ror: context.EmitRor(op.Shift); break;
- }
- break;
- case IOpCodeAluRx64 op:
- context.EmitLdintzr(op.Rm);
- context.EmitCast(op.IntType);
- context.EmitLsl(op.Shift);
- break;
- default: throw new InvalidOperationException();
- }
- }
- public static void EmitSetNzcv(ILEmitterCtx context)
- {
- context.Emit(OpCodes.Dup);
- context.Emit(OpCodes.Ldc_I4_1);
- context.Emit(OpCodes.And);
- context.EmitStflg((int)PState.VBit);
- context.Emit(OpCodes.Ldc_I4_1);
- context.Emit(OpCodes.Shr);
- context.Emit(OpCodes.Dup);
- context.Emit(OpCodes.Ldc_I4_1);
- context.Emit(OpCodes.And);
- context.EmitStflg((int)PState.CBit);
- context.Emit(OpCodes.Ldc_I4_1);
- context.Emit(OpCodes.Shr);
- context.Emit(OpCodes.Dup);
- context.Emit(OpCodes.Ldc_I4_1);
- context.Emit(OpCodes.And);
- context.EmitStflg((int)PState.ZBit);
- context.Emit(OpCodes.Ldc_I4_1);
- context.Emit(OpCodes.Shr);
- context.Emit(OpCodes.Ldc_I4_1);
- context.Emit(OpCodes.And);
- context.EmitStflg((int)PState.NBit);
- }
- // ARM32 helpers.
- private static void EmitLoadRmShiftedByImmediate(ILEmitterCtx context, OpCode32AluRsImm op, bool setCarry)
- {
- int shift = op.Imm;
- if (shift == 0)
- {
- switch (op.ShiftType)
- {
- case ShiftType.Lsr: shift = 32; break;
- case ShiftType.Asr: shift = 32; break;
- case ShiftType.Ror: shift = 1; break;
- }
- }
- context.EmitLdint(op.Rm);
- if (shift != 0)
- {
- setCarry &= op.SetFlags;
- switch (op.ShiftType)
- {
- case ShiftType.Lsl: EmitLslC(context, setCarry, shift); break;
- case ShiftType.Lsr: EmitLsrC(context, setCarry, shift); break;
- case ShiftType.Asr: EmitAsrC(context, setCarry, shift); break;
- case ShiftType.Ror:
- if (op.Imm != 0)
- {
- EmitRorC(context, setCarry, shift);
- }
- else
- {
- EmitRrxC(context, setCarry);
- }
- break;
- }
- }
- }
- private static void EmitLslC(ILEmitterCtx context, bool setCarry, int shift)
- {
- if ((uint)shift > 32)
- {
- EmitShiftByMoreThan32(context, setCarry);
- }
- else if (shift == 32)
- {
- if (setCarry)
- {
- context.EmitLdc_I4(1);
- context.Emit(OpCodes.And);
- context.EmitStflg((int)PState.CBit);
- }
- else
- {
- context.Emit(OpCodes.Pop);
- }
- context.EmitLdc_I4(0);
- }
- else
- {
- if (setCarry)
- {
- context.Emit(OpCodes.Dup);
- context.EmitLsr(32 - shift);
- context.EmitLdc_I4(1);
- context.Emit(OpCodes.And);
- context.EmitStflg((int)PState.CBit);
- }
- context.EmitLsl(shift);
- }
- }
- private static void EmitLsrC(ILEmitterCtx context, bool setCarry, int shift)
- {
- if ((uint)shift > 32)
- {
- EmitShiftByMoreThan32(context, setCarry);
- }
- else if (shift == 32)
- {
- if (setCarry)
- {
- context.EmitLsr(31);
- context.EmitStflg((int)PState.CBit);
- }
- else
- {
- context.Emit(OpCodes.Pop);
- }
- context.EmitLdc_I4(0);
- }
- else
- {
- context.Emit(OpCodes.Dup);
- context.EmitLsr(shift - 1);
- context.EmitLdc_I4(1);
- context.Emit(OpCodes.And);
- context.EmitStflg((int)PState.CBit);
- context.EmitLsr(shift);
- }
- }
- private static void EmitShiftByMoreThan32(ILEmitterCtx context, bool setCarry)
- {
- context.Emit(OpCodes.Pop);
- context.EmitLdc_I4(0);
- if (setCarry)
- {
- context.Emit(OpCodes.Dup);
- context.EmitStflg((int)PState.CBit);
- }
- }
- private static void EmitAsrC(ILEmitterCtx context, bool setCarry, int shift)
- {
- if ((uint)shift >= 32)
- {
- context.EmitAsr(31);
- if (setCarry)
- {
- context.Emit(OpCodes.Dup);
- context.EmitLdc_I4(1);
- context.Emit(OpCodes.And);
- context.EmitStflg((int)PState.CBit);
- }
- }
- else
- {
- if (setCarry)
- {
- context.Emit(OpCodes.Dup);
- context.EmitLsr(shift - 1);
- context.EmitLdc_I4(1);
- context.Emit(OpCodes.And);
- context.EmitStflg((int)PState.CBit);
- }
- context.EmitAsr(shift);
- }
- }
- private static void EmitRorC(ILEmitterCtx context, bool setCarry, int shift)
- {
- shift &= 0x1f;
- context.EmitRor(shift);
- if (setCarry)
- {
- context.Emit(OpCodes.Dup);
- context.EmitLsr(31);
- context.EmitStflg((int)PState.CBit);
- }
- }
- private static void EmitRrxC(ILEmitterCtx context, bool setCarry)
- {
- // Rotate right by 1 with carry.
- if (setCarry)
- {
- context.Emit(OpCodes.Dup);
- context.EmitLdc_I4(1);
- context.Emit(OpCodes.And);
- context.EmitSttmp();
- }
- context.EmitLsr(1);
- context.EmitLdflg((int)PState.CBit);
- context.EmitLsl(31);
- context.Emit(OpCodes.Or);
- if (setCarry)
- {
- context.EmitLdtmp();
- context.EmitStflg((int)PState.CBit);
- }
- }
- }
- }
|