aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx/Cpu/Translation/AILEmitterCtx.cs
blob: 88841db77c7e7d75203a9b041fbfcdba86c483d9 (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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
using ChocolArm64.Decoder;
using ChocolArm64.Instruction;
using ChocolArm64.State;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;

namespace ChocolArm64.Translation
{
    class AILEmitterCtx
    {
        private ATranslator Translator;

        private Dictionary<long, AILLabel> Labels;        

        private AILEmitter Emitter;

        private AILBlock ILBlock;

        private AOpCode LastCmpOp;
        private AOpCode LastFlagOp;

        private int BlkIndex;
        private int OpcIndex;

        private ABlock[] Graph;
        private ABlock   Root;
        public  ABlock   CurrBlock => Graph[BlkIndex];
        public  AOpCode  CurrOp    => Graph[BlkIndex].OpCodes[OpcIndex];

        //This is the index of the temporary register, used to store temporary
        //values needed by some functions, since IL doesn't have a swap instruction.
        //You can use any value here as long it doesn't conflict with the indices
        //for the other registers. Any value >= 64 or < 0 will do.
        private const int Tmp1Index = -1;
        private const int Tmp2Index = -2;
        private const int Tmp3Index = -3;
        private const int Tmp4Index = -4;

        public AILEmitterCtx(ATranslator Translator, ABlock[] Graph, ABlock Root)
        {
            this.Translator = Translator;
            this.Graph      = Graph;
            this.Root       = Root;

            string SubName = $"Sub{Root.Position:X16}";

            Labels = new Dictionary<long, AILLabel>();

            Emitter = new AILEmitter(Graph, Root, SubName);

            ILBlock = Emitter.GetILBlock(0);          

            OpcIndex = -1;

            if (!AdvanceOpCode())
            {
                throw new ArgumentException(nameof(Graph));
            }
        }

        public ATranslatedSub GetSubroutine() => Emitter.GetSubroutine();

        public bool AdvanceOpCode()
        {
            while (++OpcIndex >= (CurrBlock?.OpCodes.Count ?? 0))
            {
                if (BlkIndex + 1 >= Graph.Length)
                {
                    return false;
                }

                BlkIndex++;
                OpcIndex = -1;

                ILBlock = Emitter.GetILBlock(BlkIndex);
            }

            return true;
        }

        public void EmitOpCode()
        {
            if (OpcIndex == 0)
            {
                MarkLabel(GetLabel(CurrBlock.Position));
            }

            CurrOp.Emitter(this);
        }

        public bool TryOptEmitSubroutineCall()
        {           
            if (!Translator.TryGetCachedSub(CurrOp, out ATranslatedSub Sub))
            {
                return false;
            }

            for (int Index = 0; Index < ATranslatedSub.FixedArgTypes.Length; Index++)
            {
                EmitLdarg(Index);
            }

            foreach (ARegister Reg in Sub.Params)
            {
                switch (Reg.Type)
                {
                    case ARegisterType.Flag:   Ldloc(Reg.Index, AIoType.Flag);   break;
                    case ARegisterType.Int:    Ldloc(Reg.Index, AIoType.Int);    break;
                    case ARegisterType.Vector: Ldloc(Reg.Index, AIoType.Vector); break;
                }
            }

            EmitCall(Sub.Method);

            return true;
        }

        public void TryOptMarkCondWithoutCmp()
        {
            LastCmpOp = CurrOp;

            AInstEmitAluHelper.EmitDataLoadOpers(this);

            Stloc(Tmp4Index, AIoType.Int);
            Stloc(Tmp3Index, AIoType.Int);
        }

        private Dictionary<ACond, OpCode> BranchOps = new Dictionary<ACond, OpCode>()
        {
            { ACond.Eq,    OpCodes.Beq    },
            { ACond.Ne,    OpCodes.Bne_Un },
            { ACond.Ge_Un, OpCodes.Bge_Un },
            { ACond.Lt_Un, OpCodes.Blt_Un },
            { ACond.Gt_Un, OpCodes.Bgt_Un },
            { ACond.Le_Un, OpCodes.Ble_Un },
            { ACond.Ge,    OpCodes.Bge    },
            { ACond.Lt,    OpCodes.Blt    },
            { ACond.Gt,    OpCodes.Bgt    },
            { ACond.Le,    OpCodes.Ble    }
        };

        public void EmitCondBranch(AILLabel Target, ACond Cond)
        {
            OpCode ILOp;

            int IntCond = (int)Cond;            

            if (LastFlagOp == LastCmpOp && BranchOps.ContainsKey(Cond))
            {
                Ldloc(Tmp3Index, AIoType.Int, GetIntType(LastCmpOp));
                Ldloc(Tmp4Index, AIoType.Int, GetIntType(LastCmpOp));

                if (LastCmpOp.Emitter == AInstEmit.Adds)
                {
                    Emit(OpCodes.Neg);
                }

                ILOp = BranchOps[Cond];
            }
            else if (IntCond < 14)
            {
                int CondTrue = IntCond >> 1;

                switch (CondTrue)
                {
                    case 0: EmitLdflg((int)APState.ZBit); break;
                    case 1: EmitLdflg((int)APState.CBit); break;
                    case 2: EmitLdflg((int)APState.NBit); break;
                    case 3: EmitLdflg((int)APState.VBit); break;

                    case 4:
                        EmitLdflg((int)APState.CBit);
                        EmitLdflg((int)APState.ZBit);

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

                    case 5:
                    case 6:
                        EmitLdflg((int)APState.NBit);
                        EmitLdflg((int)APState.VBit);

                        Emit(OpCodes.Ceq);

                        if (CondTrue == 6)
                        {
                            EmitLdflg((int)APState.ZBit);

                            Emit(OpCodes.Not);
                            Emit(OpCodes.And);
                        }
                        break;
                }

                ILOp = (IntCond & 1) != 0
                    ? OpCodes.Brfalse
                    : OpCodes.Brtrue;
            }
            else
            {
                ILOp = OpCodes.Br;
            }

            Emit(ILOp, Target);
        }

        public void EmitCast(AIntType IntType)
        {
            switch (IntType)
            {
                case AIntType.UInt8:  Emit(OpCodes.Conv_U1); break;
                case AIntType.UInt16: Emit(OpCodes.Conv_U2); break;
                case AIntType.UInt32: Emit(OpCodes.Conv_U4); break;
                case AIntType.UInt64: Emit(OpCodes.Conv_U8); break;
                case AIntType.Int8:   Emit(OpCodes.Conv_I1); break;
                case AIntType.Int16:  Emit(OpCodes.Conv_I2); break;
                case AIntType.Int32:  Emit(OpCodes.Conv_I4); break;
                case AIntType.Int64:  Emit(OpCodes.Conv_I8); break;
            }

            if (IntType == AIntType.UInt64 ||
                IntType == AIntType.Int64)
            {
                return;
            }

            if (CurrOp.RegisterSize != ARegisterSize.Int32)
            {
                Emit(IntType >= AIntType.Int8
                    ? OpCodes.Conv_I8
                    : OpCodes.Conv_U8);
            }
        }

        public void EmitLsl(int Amount) => EmitILShift(Amount, OpCodes.Shl);
        public void EmitLsr(int Amount) => EmitILShift(Amount, OpCodes.Shr_Un);
        public void EmitAsr(int Amount) => EmitILShift(Amount, OpCodes.Shr);

        private void EmitILShift(int Amount, OpCode ILOp)
        {
            if (Amount > 0)
            {
                EmitLdc_I4(Amount);

                Emit(ILOp);
            }
        }

        public void EmitRor(int Amount)
        {
            if (Amount > 0)
            {
                Stloc(Tmp2Index, AIoType.Int);
                Ldloc(Tmp2Index, AIoType.Int);

                EmitLdc_I4(Amount);

                Emit(OpCodes.Shr_Un);
                
                Ldloc(Tmp2Index, AIoType.Int);

                EmitLdc_I4(CurrOp.GetBitsCount() - Amount);

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

        public AILLabel GetLabel(long Position)
        {
            if (!Labels.TryGetValue(Position, out AILLabel Output))
            {
                Output = new AILLabel();

                Labels.Add(Position, Output);
            }

            return Output;
        }

        public void MarkLabel(AILLabel Label)
        {
            ILBlock.Add(Label);
        }

        public void Emit(OpCode ILOp)
        {
            ILBlock.Add(new AILOpCode(ILOp));
        }

        public void Emit(OpCode ILOp, AILLabel Label)
        {
            ILBlock.Add(new AILOpCodeBranch(ILOp, Label));
        }

        public void Emit(string Text)
        {
            ILBlock.Add(new AILOpCodeLog(Text));
        }

        public void EmitLdarg(int Index)
        {
            ILBlock.Add(new AILOpCodeLoad(Index, AIoType.Arg));
        }

        public void EmitLdintzr(int Index)
        {
            if (Index != ARegisters.ZRIndex)
            {
                EmitLdint(Index);
            }
            else
            {
                EmitLdc_I(0);
            }
        }

        public void EmitStintzr(int Index)
        {
            if (Index != ARegisters.ZRIndex)
            {
                EmitStint(Index);
            }
            else
            {
                Emit(OpCodes.Pop);
            }
        }

        public void EmitLoadState(ABlock RetBlk)
        {
            ILBlock.Add(new AILOpCodeLoad(Array.IndexOf(Graph, RetBlk), AIoType.Fields));
        }

        public void EmitStoreState()
        {
            ILBlock.Add(new AILOpCodeStore(Array.IndexOf(Graph, CurrBlock), AIoType.Fields));
        }

        public void EmitLdtmp() => EmitLdint(Tmp1Index);
        public void EmitSttmp() => EmitStint(Tmp1Index);

        public void EmitLdint(int Index) => Ldloc(Index, AIoType.Int);
        public void EmitStint(int Index) => Stloc(Index, AIoType.Int);

        public void EmitLdvec(int Index) => Ldloc(Index, AIoType.Vector);
        public void EmitStvec(int Index) => Stloc(Index, AIoType.Vector);

        public void EmitLdvecsi(int Index) => Ldloc(Index, AIoType.VectorI);
        public void EmitStvecsi(int Index) => Stloc(Index, AIoType.VectorI);

        public void EmitLdvecsf(int Index) => Ldloc(Index, AIoType.VectorF);
        public void EmitStvecsf(int Index) => Stloc(Index, AIoType.VectorF);

        public void EmitLdflg(int Index) => Ldloc(Index, AIoType.Flag);
        public void EmitStflg(int Index)
        {
            LastFlagOp = CurrOp;

            Stloc(Index, AIoType.Flag);
        }

        private void Ldloc(int Index, AIoType IoType)
        {
            ILBlock.Add(new AILOpCodeLoad(Index, IoType, GetOperType(IoType)));
        }

        private void Ldloc(int Index, AIoType IoType, Type Type)
        {
            ILBlock.Add(new AILOpCodeLoad(Index, IoType, Type));
        }

        private void Stloc(int Index, AIoType IoType)
        {
            ILBlock.Add(new AILOpCodeStore(Index, IoType, GetOutOperType(IoType)));
        }

        private Type GetOutOperType(AIoType IoType)
        {
            //This instruction is used to convert between floating point
            //types, so the input and output types are different.
            if (CurrOp.Emitter == AInstEmit.Fcvt_S)
            {
                return GetFloatType(((AOpCodeSimd)CurrOp).Opc);
            }
            else
            {
                return GetOperType(IoType);
            }
        }

        private Type GetOperType(AIoType IoType)
        {
            switch (IoType & AIoType.Mask)
            {
                case AIoType.Flag:   return typeof(bool);
                case AIoType.Int:    return GetIntType(CurrOp);
                case AIoType.Vector: return GetVecType(CurrOp, IoType);
            }

            throw new ArgumentException(nameof(IoType));
        }

        private Type GetIntType(AOpCode OpCode)
        {
            //Always default to 64-bits.
            return OpCode.RegisterSize == ARegisterSize.Int32
                ? typeof(uint)
                : typeof(ulong);
        }

        private Type GetVecType(AOpCode OpCode, AIoType IoType)
        {
            if (!(OpCode is IAOpCodeSimd Op))
            {
                return typeof(AVec);
            }

            int Size = Op.Size;

            if (Op.Emitter == AInstEmit.Fmov_Ftoi ||
                Op.Emitter == AInstEmit.Fmov_Itof)
            {
                Size |= 2;
            }

            if (Op is AOpCodeMem || Op is IAOpCodeLit)
            {
                return Size < 4 ? typeof(ulong) : typeof(AVec);
            }
            else if (IoType == AIoType.VectorI)
            {
                return GetIntType(Size);
            }
            else if (IoType == AIoType.VectorF)
            {
                return GetFloatType(Size);
            }

            return typeof(AVec);
        }

        private static Type GetIntType(int Size)
        {
            switch (Size)
            {
                case 0: return typeof(byte);
                case 1: return typeof(ushort);
                case 2: return typeof(uint);
                case 3: return typeof(ulong);
            }

            throw new ArgumentOutOfRangeException(nameof(Size));
        }

        private static Type GetFloatType(int Size)
        {
            switch (Size)
            {
                case 0: return typeof(float);
                case 1: return typeof(double);
            }

            throw new ArgumentOutOfRangeException(nameof(Size));
        }

        public void EmitCall(Type MthdType, string MthdName)
        {
            if (MthdType == null)
            {
                throw new ArgumentNullException(nameof(MthdType));
            }

            if (MthdName == null)
            {
                throw new ArgumentNullException(nameof(MthdName));
            }

            EmitCall(MthdType.GetMethod(MthdName));
        }

        public void EmitCall(MethodInfo MthdInfo)
        {
            if (MthdInfo == null)
            {
                throw new ArgumentNullException(nameof(MthdInfo));
            }

            ILBlock.Add(new AILOpCodeCall(MthdInfo));
        }

        public void EmitLdc_I(long Value)
        {
            if (CurrOp.RegisterSize == ARegisterSize.Int32)
            {
                EmitLdc_I4((int)Value);
            }
            else
            {
                EmitLdc_I8(Value);
            }
        }

        public void EmitLdc_I4(int Value)
        {
            ILBlock.Add(new AILOpCodeConst(Value));
        }

        public void EmitLdc_I8(long Value)
        {
            ILBlock.Add(new AILOpCodeConst(Value));
        }

        public void EmitLdc_R4(float Value)
        {
            ILBlock.Add(new AILOpCodeConst(Value));
        }

        public void EmitLdc_R8(double Value)
        {
            ILBlock.Add(new AILOpCodeConst(Value));
        }

        public void EmitZNFlagCheck()
        {
            EmitZNCheck(OpCodes.Ceq, (int)APState.ZBit);
            EmitZNCheck(OpCodes.Clt, (int)APState.NBit);
        }

        private void EmitZNCheck(OpCode ILCmpOp, int Flag)
        {
            Emit(OpCodes.Dup);
            Emit(OpCodes.Ldc_I4_0);

            if (CurrOp.RegisterSize != ARegisterSize.Int32)
            {
                Emit(OpCodes.Conv_I8);
            }

            Emit(ILCmpOp);

            EmitStflg(Flag);
        }
    }
}