aboutsummaryrefslogtreecommitdiff
path: root/ChocolArm64/Instruction/AInstEmitMemoryHelper.cs
blob: df091bd514155adf8652e291d30f93104e96d68c (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using ChocolArm64.Decoder;
using ChocolArm64.Memory;
using ChocolArm64.Translation;
using System;
using System.Reflection.Emit;

namespace ChocolArm64.Instruction
{
    static class AInstEmitMemoryHelper
    {
        private enum Extension
        {
            Zx,
            Sx32,
            Sx64
        }

        public static void EmitReadZxCall(AILEmitterCtx Context, int Size)
        {
            EmitReadCall(Context, Extension.Zx, Size);
        }

        public static void EmitReadSx32Call(AILEmitterCtx Context, int Size)
        {
            EmitReadCall(Context, Extension.Sx32, Size);
        }

        public static void EmitReadSx64Call(AILEmitterCtx Context, int Size)
        {
            EmitReadCall(Context, Extension.Sx64, Size);
        }

        private static void EmitReadCall(AILEmitterCtx Context, Extension Ext, int Size)
        {
            bool IsSimd = GetIsSimd(Context);

            string Name = null;

            if (Size < 0 || Size > (IsSimd ? 4 : 3))
            {
                throw new ArgumentOutOfRangeException(nameof(Size));
            }

            if (IsSimd)
            {
                switch (Size)
                {
                    case 0: Name = AOptimizations.DisableMemoryChecks
                        ? nameof(AMemory.ReadVector8Unchecked)
                        : nameof(AMemory.ReadVector8); break;

                    case 1: Name = AOptimizations.DisableMemoryChecks
                        ? nameof(AMemory.ReadVector16Unchecked)
                        : nameof(AMemory.ReadVector16); break;

                    case 2: Name = AOptimizations.DisableMemoryChecks
                        ? nameof(AMemory.ReadVector32Unchecked)
                        : nameof(AMemory.ReadVector32); break;

                    case 3: Name = AOptimizations.DisableMemoryChecks
                        ? nameof(AMemory.ReadVector64Unchecked)
                        : nameof(AMemory.ReadVector64); break;

                    case 4: Name = AOptimizations.DisableMemoryChecks
                        ? nameof(AMemory.ReadVector128Unchecked)
                        : nameof(AMemory.ReadVector128); break;
                }
            }
            else
            {
                switch (Size)
                {
                    case 0: Name = AOptimizations.DisableMemoryChecks
                        ? nameof(AMemory.ReadByteUnchecked)
                        : nameof(AMemory.ReadByte); break;

                    case 1: Name = AOptimizations.DisableMemoryChecks
                        ? nameof(AMemory.ReadUInt16Unchecked)
                        : nameof(AMemory.ReadUInt16); break;

                    case 2: Name = AOptimizations.DisableMemoryChecks
                        ? nameof(AMemory.ReadUInt32Unchecked)
                        : nameof(AMemory.ReadUInt32); break;

                    case 3: Name = AOptimizations.DisableMemoryChecks
                        ? nameof(AMemory.ReadUInt64Unchecked)
                        : nameof(AMemory.ReadUInt64); break;
                }
            }

            Context.EmitCall(typeof(AMemory), Name);

            if (!IsSimd)
            {
                if (Ext == Extension.Sx32 ||
                    Ext == Extension.Sx64)
                {
                    switch (Size)
                    {
                        case 0: Context.Emit(OpCodes.Conv_I1); break;
                        case 1: Context.Emit(OpCodes.Conv_I2); break;
                        case 2: Context.Emit(OpCodes.Conv_I4); break;
                    }
                }

                if (Size < 3)
                {
                    Context.Emit(Ext == Extension.Sx64
                        ? OpCodes.Conv_I8
                        : OpCodes.Conv_U8);
                }
            }
        }

        public static void EmitWriteCall(AILEmitterCtx Context, int Size)
        {
            bool IsSimd = GetIsSimd(Context);

            string Name = null;

            if (Size < 0 || Size > (IsSimd ? 4 : 3))
            {
                throw new ArgumentOutOfRangeException(nameof(Size));
            }

            if (Size < 3 && !IsSimd)
            {
                Context.Emit(OpCodes.Conv_I4);
            }

            if (IsSimd)
            {
                switch (Size)
                {
                    case 0: Name = AOptimizations.DisableMemoryChecks
                        ? nameof(AMemory.WriteVector8Unchecked)
                        : nameof(AMemory.WriteVector8); break;

                    case 1: Name = AOptimizations.DisableMemoryChecks
                        ? nameof(AMemory.WriteVector16Unchecked)
                        : nameof(AMemory.WriteVector16); break;

                    case 2: Name = AOptimizations.DisableMemoryChecks
                        ? nameof(AMemory.WriteVector32Unchecked)
                        : nameof(AMemory.WriteVector32); break;

                    case 3: Name = AOptimizations.DisableMemoryChecks
                        ? nameof(AMemory.WriteVector64Unchecked)
                        : nameof(AMemory.WriteVector64); break;

                    case 4: Name = AOptimizations.DisableMemoryChecks
                        ? nameof(AMemory.WriteVector128Unchecked)
                        : nameof(AMemory.WriteVector128); break;
                }
            }
            else
            {
                switch (Size)
                {
                    case 0: Name = AOptimizations.DisableMemoryChecks
                        ? nameof(AMemory.WriteByteUnchecked)
                        : nameof(AMemory.WriteByte); break;

                    case 1: Name = AOptimizations.DisableMemoryChecks
                        ? nameof(AMemory.WriteUInt16Unchecked)
                        : nameof(AMemory.WriteUInt16); break;

                    case 2: Name = AOptimizations.DisableMemoryChecks
                        ? nameof(AMemory.WriteUInt32Unchecked)
                        : nameof(AMemory.WriteUInt32); break;

                    case 3: Name = AOptimizations.DisableMemoryChecks
                        ? nameof(AMemory.WriteUInt64Unchecked)
                        : nameof(AMemory.WriteUInt64); break;
                }
            }

            Context.EmitCall(typeof(AMemory), Name);
        }

        private static bool GetIsSimd(AILEmitterCtx Context)
        {
            return Context.CurrOp is IAOpCodeSimd &&
                 !(Context.CurrOp is AOpCodeSimdMemMs ||
                   Context.CurrOp is AOpCodeSimdMemSs);
        }
    }
}