aboutsummaryrefslogtreecommitdiff
path: root/ChocolArm64/Translation/ILMethodBuilder.cs
blob: 98b5052043e9f552a102399c138ebc4393008f50 (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
using ChocolArm64.State;
using System;
using System.Collections.Generic;
using System.Reflection.Emit;
using System.Runtime.Intrinsics;

namespace ChocolArm64.Translation
{
    class ILMethodBuilder
    {
        private const int RegsCount = 32;
        private const int RegsMask  = RegsCount - 1;

        public RegisterUsage RegUsage { get; private set; }

        public ILGenerator Generator { get; private set; }

        private Dictionary<Register, int> _locals;

        private ILBlock[] _ilBlocks;

        private string _subName;

        public bool IsAarch64 { get; }

        public bool IsSubComplete { get; }

        private int _localsCount;

        public ILMethodBuilder(
            ILBlock[] ilBlocks,
            string    subName,
            bool      isAarch64,
            bool      isSubComplete = false)
        {
            _ilBlocks     = ilBlocks;
            _subName      = subName;
            IsAarch64     = isAarch64;
            IsSubComplete = isSubComplete;
        }

        public TranslatedSub GetSubroutine(TranslationTier tier, bool isWorthOptimizing)
        {
            RegUsage = new RegisterUsage();

            RegUsage.BuildUses(_ilBlocks[0]);

            DynamicMethod method = new DynamicMethod(_subName, typeof(long), TranslatedSub.FixedArgTypes);

            long intNiRegsMask = RegUsage.GetIntNotInputs(_ilBlocks[0]);
            long vecNiRegsMask = RegUsage.GetVecNotInputs(_ilBlocks[0]);

            TranslatedSub subroutine = new TranslatedSub(
                method,
                intNiRegsMask,
                vecNiRegsMask,
                tier,
                isWorthOptimizing);

            _locals = new Dictionary<Register, int>();

            _localsCount = 0;

            Generator = method.GetILGenerator();

            foreach (ILBlock ilBlock in _ilBlocks)
            {
                ilBlock.Emit(this);
            }

            subroutine.PrepareMethod();

            return subroutine;
        }

        public int GetLocalIndex(Register reg)
        {
            if (!_locals.TryGetValue(reg, out int index))
            {
                Generator.DeclareLocal(GetFieldType(reg.Type));

                index = _localsCount++;

                _locals.Add(reg, index);
            }

            return index;
        }

        private static Type GetFieldType(RegisterType regType)
        {
            switch (regType)
            {
                case RegisterType.Flag:   return typeof(bool);
                case RegisterType.Int:    return typeof(ulong);
                case RegisterType.Vector: return typeof(Vector128<float>);
            }

            throw new ArgumentException(nameof(regType));
        }

        public static Register GetRegFromBit(int bit, RegisterType baseType)
        {
            if (bit < RegsCount)
            {
                return new Register(bit, baseType);
            }
            else if (baseType == RegisterType.Int)
            {
                return new Register(bit & RegsMask, RegisterType.Flag);
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(bit));
            }
        }

        public static bool IsRegIndex(int index)
        {
            return (uint)index < RegsCount;
        }
    }
}