aboutsummaryrefslogtreecommitdiff
path: root/ChocolArm64/Instructions/InstEmitAlu.cs
blob: d5d9cd65414ff3acfc782fe2348495e7f025b5f5 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
using ChocolArm64.Decoders;
using ChocolArm64.State;
using ChocolArm64.Translation;
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.Intrinsics.X86;

using static ChocolArm64.Instructions.InstEmitAluHelper;

namespace ChocolArm64.Instructions
{
    static partial class InstEmit
    {
        public static void Adc(ILEmitterCtx context)  => EmitAdc(context, false);
        public static void Adcs(ILEmitterCtx context) => EmitAdc(context, true);

        private static void EmitAdc(ILEmitterCtx context, bool setFlags)
        {
            EmitAluLoadOpers(context);

            context.Emit(OpCodes.Add);

            context.EmitLdflg((int)PState.CBit);

            Type[] mthdTypes  = new Type[] { typeof(bool) };

            MethodInfo mthdInfo = typeof(Convert).GetMethod(nameof(Convert.ToInt32), mthdTypes);

            context.EmitCall(mthdInfo);

            if (context.CurrOp.RegisterSize != RegisterSize.Int32)
            {
                context.Emit(OpCodes.Conv_U8);
            }

            context.Emit(OpCodes.Add);

            if (setFlags)
            {
                context.EmitZnFlagCheck();

                EmitAdcsCCheck(context);
                EmitAddsVCheck(context);
            }

            EmitAluStore(context);
        }

        public static void Add(ILEmitterCtx context) => EmitAluOp(context, OpCodes.Add);

        public static void Adds(ILEmitterCtx context)
        {
            EmitAluLoadOpers(context);

            context.Emit(OpCodes.Add);

            context.EmitZnFlagCheck();

            EmitAddsCCheck(context);
            EmitAddsVCheck(context);
            EmitAluStoreS(context);
        }

        public static void And(ILEmitterCtx context) => EmitAluOp(context, OpCodes.And);

        public static void Ands(ILEmitterCtx context)
        {
            EmitAluLoadOpers(context);

            context.Emit(OpCodes.And);

            EmitZeroCvFlags(context);

            context.EmitZnFlagCheck();

            EmitAluStoreS(context);
        }

        public static void Asrv(ILEmitterCtx context) => EmitAluOpShift(context, OpCodes.Shr);

        public static void Bic(ILEmitterCtx context)  => EmitBic(context, false);
        public static void Bics(ILEmitterCtx context) => EmitBic(context, true);

        private static void EmitBic(ILEmitterCtx context, bool setFlags)
        {
            EmitAluLoadOpers(context);

            context.Emit(OpCodes.Not);
            context.Emit(OpCodes.And);

            if (setFlags)
            {
                EmitZeroCvFlags(context);

                context.EmitZnFlagCheck();
            }

            EmitAluStore(context, setFlags);
        }

        public static void Cls(ILEmitterCtx context)
        {
            OpCodeAlu64 op = (OpCodeAlu64)context.CurrOp;

            context.EmitLdintzr(op.Rn);

            context.EmitLdc_I4(op.RegisterSize == RegisterSize.Int32 ? 32 : 64);

            SoftFallback.EmitCall(context, nameof(SoftFallback.CountLeadingSigns));

            context.EmitStintzr(op.Rd);
        }

        public static void Clz(ILEmitterCtx context)
        {
            OpCodeAlu64 op = (OpCodeAlu64)context.CurrOp;

            context.EmitLdintzr(op.Rn);

            if (Lzcnt.IsSupported)
            {
                Type tValue = op.RegisterSize == RegisterSize.Int32 ? typeof(uint) : typeof(ulong);

                context.EmitCall(typeof(Lzcnt).GetMethod(nameof(Lzcnt.LeadingZeroCount), new Type[] { tValue }));
            }
            else
            {
                context.EmitLdc_I4(op.RegisterSize == RegisterSize.Int32 ? 32 : 64);

                SoftFallback.EmitCall(context, nameof(SoftFallback.CountLeadingZeros));
            }

            context.EmitStintzr(op.Rd);
        }

        public static void Eon(ILEmitterCtx context)
        {
            EmitAluLoadOpers(context);

            context.Emit(OpCodes.Not);
            context.Emit(OpCodes.Xor);

            EmitAluStore(context);
        }

        public static void Eor(ILEmitterCtx context) => EmitAluOp(context, OpCodes.Xor);

        public static void Extr(ILEmitterCtx context)
        {
            //TODO: Ensure that the Shift is valid for the Is64Bits.
            OpCodeAluRs64 op = (OpCodeAluRs64)context.CurrOp;

            context.EmitLdintzr(op.Rm);

            if (op.Shift > 0)
            {
                context.EmitLdc_I4(op.Shift);

                context.Emit(OpCodes.Shr_Un);

                context.EmitLdintzr(op.Rn);
                context.EmitLdc_I4(op.GetBitsCount() - op.Shift);

                context.Emit(OpCodes.Shl);
                context.Emit(OpCodes.Or);
            }

            EmitAluStore(context);
        }

        public static void Lslv(ILEmitterCtx context) => EmitAluOpShift(context, OpCodes.Shl);
        public static void Lsrv(ILEmitterCtx context) => EmitAluOpShift(context, OpCodes.Shr_Un);

        public static void Sbc(ILEmitterCtx context)  => EmitSbc(context, false);
        public static void Sbcs(ILEmitterCtx context) => EmitSbc(context, true);

        private static void EmitSbc(ILEmitterCtx context, bool setFlags)
        {
            EmitAluLoadOpers(context);

            context.Emit(OpCodes.Sub);

            context.EmitLdflg((int)PState.CBit);

            Type[] mthdTypes  = new Type[] { typeof(bool) };

            MethodInfo mthdInfo = typeof(Convert).GetMethod(nameof(Convert.ToInt32), mthdTypes);

            context.EmitCall(mthdInfo);

            context.EmitLdc_I4(1);

            context.Emit(OpCodes.Xor);

            if (context.CurrOp.RegisterSize != RegisterSize.Int32)
            {
                context.Emit(OpCodes.Conv_U8);
            }

            context.Emit(OpCodes.Sub);

            if (setFlags)
            {
                context.EmitZnFlagCheck();

                EmitSbcsCCheck(context);
                EmitSubsVCheck(context);
            }

            EmitAluStore(context);
        }

        public static void Sub(ILEmitterCtx context) => EmitAluOp(context, OpCodes.Sub);

        public static void Subs(ILEmitterCtx context)
        {
            context.TryOptMarkCondWithoutCmp();

            EmitAluLoadOpers(context);

            context.Emit(OpCodes.Sub);

            context.EmitZnFlagCheck();

            EmitSubsCCheck(context);
            EmitSubsVCheck(context);
            EmitAluStoreS(context);
        }

        public static void Orn(ILEmitterCtx context)
        {
            EmitAluLoadOpers(context);

            context.Emit(OpCodes.Not);
            context.Emit(OpCodes.Or);

            EmitAluStore(context);
        }

        public static void Orr(ILEmitterCtx context) => EmitAluOp(context, OpCodes.Or);

        public static void Rbit(ILEmitterCtx context) => EmitFallback32_64(context,
            nameof(SoftFallback.ReverseBits32),
            nameof(SoftFallback.ReverseBits64));

        public static void Rev16(ILEmitterCtx context) => EmitFallback32_64(context,
            nameof(SoftFallback.ReverseBytes16_32),
            nameof(SoftFallback.ReverseBytes16_64));

        public static void Rev32(ILEmitterCtx context) => EmitFallback32_64(context,
            nameof(SoftFallback.ReverseBytes32_32),
            nameof(SoftFallback.ReverseBytes32_64));

        private static void EmitFallback32_64(ILEmitterCtx context, string name32, string name64)
        {
            OpCodeAlu64 op = (OpCodeAlu64)context.CurrOp;

            context.EmitLdintzr(op.Rn);

            if (op.RegisterSize == RegisterSize.Int32)
            {
                SoftFallback.EmitCall(context, name32);
            }
            else
            {
                SoftFallback.EmitCall(context, name64);
            }

            context.EmitStintzr(op.Rd);
        }

        public static void Rev64(ILEmitterCtx context)
        {
            OpCodeAlu64 op = (OpCodeAlu64)context.CurrOp;

            context.EmitLdintzr(op.Rn);

            SoftFallback.EmitCall(context, nameof(SoftFallback.ReverseBytes64));

            context.EmitStintzr(op.Rd);
        }

        public static void Rorv(ILEmitterCtx context)
        {
            EmitAluLoadRn(context);
            EmitAluLoadShift(context);

            context.Emit(OpCodes.Shr_Un);

            EmitAluLoadRn(context);

            context.EmitLdc_I4(context.CurrOp.GetBitsCount());

            EmitAluLoadShift(context);

            context.Emit(OpCodes.Sub);
            context.Emit(OpCodes.Shl);
            context.Emit(OpCodes.Or);

            EmitAluStore(context);
        }

        public static void Sdiv(ILEmitterCtx context) => EmitDiv(context, OpCodes.Div);
        public static void Udiv(ILEmitterCtx context) => EmitDiv(context, OpCodes.Div_Un);

        private static void EmitDiv(ILEmitterCtx context, OpCode ilOp)
        {
            //If Rm == 0, Rd = 0 (division by zero).
            context.EmitLdc_I(0);

            EmitAluLoadRm(context);

            context.EmitLdc_I(0);

            ILLabel badDiv = new ILLabel();

            context.Emit(OpCodes.Beq_S, badDiv);
            context.Emit(OpCodes.Pop);

            if (ilOp == OpCodes.Div)
            {
                //If Rn == INT_MIN && Rm == -1, Rd = INT_MIN (overflow).
                long intMin = 1L << (context.CurrOp.GetBitsCount() - 1);

                context.EmitLdc_I(intMin);

                EmitAluLoadRn(context);

                context.EmitLdc_I(intMin);

                context.Emit(OpCodes.Ceq);

                EmitAluLoadRm(context);

                context.EmitLdc_I(-1);

                context.Emit(OpCodes.Ceq);
                context.Emit(OpCodes.And);
                context.Emit(OpCodes.Brtrue_S, badDiv);
                context.Emit(OpCodes.Pop);
            }

            EmitAluLoadRn(context);
            EmitAluLoadRm(context);

            context.Emit(ilOp);

            context.MarkLabel(badDiv);

            EmitAluStore(context);
        }

        private static void EmitAluOp(ILEmitterCtx context, OpCode ilOp)
        {
            EmitAluLoadOpers(context);

            context.Emit(ilOp);

            EmitAluStore(context);
        }

        private static void EmitAluOpShift(ILEmitterCtx context, OpCode ilOp)
        {
            EmitAluLoadRn(context);
            EmitAluLoadShift(context);

            context.Emit(ilOp);

            EmitAluStore(context);
        }

        private static void EmitAluLoadShift(ILEmitterCtx context)
        {
            EmitAluLoadRm(context);

            context.EmitLdc_I(context.CurrOp.GetBitsCount() - 1);

            context.Emit(OpCodes.And);

            //Note: Only 32-bits shift values are valid, so when the value is 64-bits
            //we need to cast it to a 32-bits integer. This is fine because we
            //AND the value and only keep the lower 5 or 6 bits anyway -- it
            //could very well fit on a byte.
            if (context.CurrOp.RegisterSize != RegisterSize.Int32)
            {
                context.Emit(OpCodes.Conv_I4);
            }
        }

        private static void EmitZeroCvFlags(ILEmitterCtx context)
        {
            context.EmitLdc_I4(0);

            context.EmitStflg((int)PState.VBit);

            context.EmitLdc_I4(0);

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

        public static void EmitAluStore(ILEmitterCtx context)  => EmitAluStore(context, false);
        public static void EmitAluStoreS(ILEmitterCtx context) => EmitAluStore(context, true);

        public static void EmitAluStore(ILEmitterCtx context, bool setFlags)
        {
            IOpCodeAlu64 op = (IOpCodeAlu64)context.CurrOp;

            if (setFlags || op is IOpCodeAluRs64)
            {
                context.EmitStintzr(op.Rd);
            }
            else
            {
                context.EmitStint(op.Rd);
            }
        }
    }
}