aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Cpu/LightningJit/Arm32/RegisterAllocator.cs
blob: 4a3f03b8a21bf381c2feb41fdc00cac542828647 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using Ryujinx.Cpu.LightningJit.CodeGen;
using Ryujinx.Cpu.LightningJit.CodeGen.Arm64;
using System;
using System.Numerics;

namespace Ryujinx.Cpu.LightningJit.Arm32
{
    class RegisterAllocator
    {
        public const int MaxTemps = 1;

        private uint _gprMask;
        private uint _fpSimdMask;

        public int FixedContextRegister { get; }
        public int FixedPageTableRegister { get; }

        public uint UsedGprsMask { get; private set; }
        public uint UsedFpSimdMask { get; private set; }

        public RegisterAllocator()
        {
            _gprMask = ushort.MaxValue;
            _fpSimdMask = ushort.MaxValue;

            FixedContextRegister = AllocateTempRegisterWithPreferencing();
            FixedPageTableRegister = AllocateTempRegisterWithPreferencing();
        }

        public void MarkGprAsUsed(int index)
        {
            UsedGprsMask |= 1u << index;
        }

        public void MarkFpSimdAsUsed(int index)
        {
            UsedFpSimdMask |= 1u << index;
        }

        public void MarkFpSimdRangeAsUsed(int index, int count)
        {
            UsedFpSimdMask |= (uint.MaxValue >> (32 - count)) << index;
        }

        public Operand RemapGprRegister(int index)
        {
            MarkGprAsUsed(index);

            return new Operand(OperandKind.Register, OperandType.I32, (ulong)index);
        }

        public Operand RemapFpRegister(int index, bool isFP32)
        {
            MarkFpSimdAsUsed(index);

            return new Operand(OperandKind.Register, isFP32 ? OperandType.FP32 : OperandType.FP64, (ulong)index);
        }

        public Operand RemapSimdRegister(int index)
        {
            MarkFpSimdAsUsed(index);

            return new Operand(OperandKind.Register, OperandType.V128, (ulong)index);
        }

        public Operand RemapSimdRegister(int index, int count)
        {
            MarkFpSimdRangeAsUsed(index, count);

            return new Operand(OperandKind.Register, OperandType.V128, (ulong)index);
        }

        public void EnsureTempGprRegisters(int count)
        {
            if (count != 0)
            {
                Span<int> registers = stackalloc int[count];

                for (int index = 0; index < count; index++)
                {
                    registers[index] = AllocateTempGprRegister();
                }

                for (int index = 0; index < count; index++)
                {
                    FreeTempGprRegister(registers[index]);
                }
            }
        }

        public int AllocateTempGprRegister()
        {
            int index = AllocateTempRegister(ref _gprMask, AbiConstants.ReservedRegsMask);

            MarkGprAsUsed(index);

            return index;
        }

        private int AllocateTempRegisterWithPreferencing()
        {
            int firstCalleeSaved = BitOperations.TrailingZeroCount(~_gprMask & AbiConstants.GprCalleeSavedRegsMask);
            if (firstCalleeSaved < 32)
            {
                uint regMask = 1u << firstCalleeSaved;
                if ((regMask & AbiConstants.ReservedRegsMask) == 0)
                {
                    _gprMask |= regMask;
                    UsedGprsMask |= regMask;

                    return firstCalleeSaved;
                }
            }

            return AllocateTempRegister(ref _gprMask, AbiConstants.ReservedRegsMask);
        }

        public int AllocateTempFpSimdRegister()
        {
            int index = AllocateTempRegister(ref _fpSimdMask, 0);

            MarkFpSimdAsUsed(index);

            return index;
        }

        public ScopedRegister AllocateTempGprRegisterScoped()
        {
            return new(this, new(OperandKind.Register, OperandType.I32, (ulong)AllocateTempGprRegister()));
        }

        public ScopedRegister AllocateTempFpRegisterScoped(bool isFP32)
        {
            return new(this, new(OperandKind.Register, isFP32 ? OperandType.FP32 : OperandType.FP64, (ulong)AllocateTempFpSimdRegister()));
        }

        public ScopedRegister AllocateTempSimdRegisterScoped()
        {
            return new(this, new(OperandKind.Register, OperandType.V128, (ulong)AllocateTempFpSimdRegister()));
        }

        public void FreeTempGprRegister(int index)
        {
            FreeTempRegister(ref _gprMask, index);
        }

        public void FreeTempFpSimdRegister(int index)
        {
            FreeTempRegister(ref _fpSimdMask, index);
        }

        private static int AllocateTempRegister(ref uint mask, uint reservedMask)
        {
            int index = BitOperations.TrailingZeroCount(~(mask | reservedMask));
            if (index == sizeof(uint) * 8)
            {
                throw new InvalidOperationException("No free registers.");
            }

            mask |= 1u << index;

            return index;
        }

        private static void FreeTempRegister(ref uint mask, int index)
        {
            mask &= ~(1u << index);
        }
    }
}