aboutsummaryrefslogtreecommitdiff
path: root/src/ARMeilleure/Instructions/InstEmitCcmp.cs
blob: 7f0beb6cb106d126044e66690968d5f5a0c67903 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using ARMeilleure.Decoders;
using ARMeilleure.IntermediateRepresentation;
using ARMeilleure.State;
using ARMeilleure.Translation;

using static ARMeilleure.Instructions.InstEmitAluHelper;
using static ARMeilleure.Instructions.InstEmitFlowHelper;
using static ARMeilleure.Instructions.InstEmitHelper;
using static ARMeilleure.IntermediateRepresentation.Operand.Factory;

namespace ARMeilleure.Instructions
{
    static partial class InstEmit
    {
        public static void Ccmn(ArmEmitterContext context) => EmitCcmp(context, isNegated: true);
        public static void Ccmp(ArmEmitterContext context) => EmitCcmp(context, isNegated: false);

        private static void EmitCcmp(ArmEmitterContext context, bool isNegated)
        {
            OpCodeCcmp op = (OpCodeCcmp)context.CurrOp;

            Operand lblTrue = Label();
            Operand lblEnd  = Label();

            EmitCondBranch(context, lblTrue, op.Cond);

            SetFlag(context, PState.VFlag, Const((op.Nzcv >> 0) & 1));
            SetFlag(context, PState.CFlag, Const((op.Nzcv >> 1) & 1));
            SetFlag(context, PState.ZFlag, Const((op.Nzcv >> 2) & 1));
            SetFlag(context, PState.NFlag, Const((op.Nzcv >> 3) & 1));

            context.Branch(lblEnd);

            context.MarkLabel(lblTrue);

            Operand n = GetAluN(context);
            Operand m = GetAluM(context);

            if (isNegated)
            {
                Operand d = context.Add(n, m);

                EmitNZFlagsCheck(context, d);

                EmitAddsCCheck(context, n, d);
                EmitAddsVCheck(context, n, m, d);
            }
            else
            {
                Operand d = context.Subtract(n, m);

                EmitNZFlagsCheck(context, d);

                EmitSubsCCheck(context, n, m);
                EmitSubsVCheck(context, n, m, d);
            }

            context.MarkLabel(lblEnd);
        }
    }
}