aboutsummaryrefslogtreecommitdiff
path: root/ChocolArm64/Instructions/InstEmitFlow32.cs
blob: dea490c7759589ab9a700e181f9668b22c3d3a32 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using ChocolArm64.Decoders;
using ChocolArm64.State;
using ChocolArm64.Translation;
using System.Reflection.Emit;

using static ChocolArm64.Instructions.InstEmit32Helper;

namespace ChocolArm64.Instructions
{
    static partial class InstEmit32
    {
        public static void B(ILEmitterCtx context)
        {
            IOpCode32BImm op = (IOpCode32BImm)context.CurrOp;

            if (context.CurrBlock.Branch != null)
            {
                context.Emit(OpCodes.Br, context.GetLabel(op.Imm));
            }
            else
            {
                context.EmitStoreState();
                context.EmitLdc_I8(op.Imm);

                context.Emit(OpCodes.Ret);
            }
        }

        public static void Bl(ILEmitterCtx context)
        {
            Blx(context, x: false);
        }

        public static void Blx(ILEmitterCtx context)
        {
            Blx(context, x: true);
        }

        public static void Bx(ILEmitterCtx context)
        {
            IOpCode32BReg op = (IOpCode32BReg)context.CurrOp;

            context.EmitStoreState();

            EmitLoadFromRegister(context, op.Rm);

            EmitBxWritePc(context);
        }

        private static void Blx(ILEmitterCtx context, bool x)
        {
            IOpCode32BImm op = (IOpCode32BImm)context.CurrOp;

            uint pc = op.GetPc();

            bool isThumb = IsThumb(context.CurrOp);

            if (!isThumb)
            {
                context.EmitLdc_I(op.GetPc() - 4);
            }
            else
            {
                context.EmitLdc_I(op.GetPc() | 1);
            }

            context.EmitStint(GetBankedRegisterAlias(context.Mode, RegisterAlias.Aarch32Lr));

            //If x is true, then this is a branch with link and exchange.
            //In this case we need to swap the mode between Arm <-> Thumb.
            if (x)
            {
                context.EmitLdc_I4(isThumb ? 0 : 1);

                context.EmitStflg((int)PState.TBit);
            }

            InstEmitFlowHelper.EmitCall(context, op.Imm);
        }
    }
}