aboutsummaryrefslogtreecommitdiff
path: root/ChocolArm64/Translation/AILEmitter.cs
blob: 8f6e1210f03da1a16d9389d17063649bf7dc94b4 (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
using ChocolArm64.Decoder;
using ChocolArm64.State;
using System;
using System.Collections.Generic;
using System.Reflection.Emit;

namespace ChocolArm64.Translation
{
    class AILEmitter
    {
        public ALocalAlloc LocalAlloc { get; private set; }

        public ILGenerator Generator { get; private set; }

        private Dictionary<ARegister, int> Locals;

        private AILBlock[] ILBlocks;

        private AILBlock Root;

        private ATranslatedSub Subroutine;

        private string SubName;

        private int LocalsCount;

        public AILEmitter(ABlock[] Graph, ABlock Root, string SubName)
        {
            this.SubName = SubName;

            Locals = new Dictionary<ARegister, int>();

            ILBlocks = new AILBlock[Graph.Length];

            AILBlock GetBlock(int Index)
            {
                if (Index < 0 || Index >= ILBlocks.Length)
                {
                    return null;
                }

                if (ILBlocks[Index] == null)
                {
                    ILBlocks[Index] = new AILBlock();
                }

                return ILBlocks[Index];
            }

            for (int Index = 0; Index < ILBlocks.Length; Index++)
            {
                AILBlock Block = GetBlock(Index);

                Block.Next   = GetBlock(Array.IndexOf(Graph, Graph[Index].Next));
                Block.Branch = GetBlock(Array.IndexOf(Graph, Graph[Index].Branch));
            }

            this.Root = ILBlocks[Array.IndexOf(Graph, Root)];
        }

        public ATranslatedSub GetSubroutine()
        {
            LocalAlloc = new ALocalAlloc(ILBlocks, Root);

            InitSubroutine();
            InitLocals();

            foreach (AILBlock ILBlock in ILBlocks)
            {
                ILBlock.Emit(this);
            }

            return Subroutine;
        }

        public AILBlock GetILBlock(int Index) => ILBlocks[Index];

        private void InitLocals()
        {
            int ParamsStart = ATranslatedSub.FixedArgTypes.Length;

            Locals = new Dictionary<ARegister, int>();

            for (int Index = 0; Index < Subroutine.Params.Count; Index++)
            {
                ARegister Reg = Subroutine.Params[Index];

                Generator.EmitLdarg(Index + ParamsStart);
                Generator.EmitStloc(GetLocalIndex(Reg));
            }
        }

        private void InitSubroutine()
        {
            List<ARegister> Params = new List<ARegister>();

            void SetParams(long Inputs, ARegisterType BaseType)
            {
                for (int Bit = 0; Bit < 64; Bit++)
                {
                    long Mask = 1L << Bit;

                    if ((Inputs & Mask) != 0)
                    {
                        Params.Add(GetRegFromBit(Bit, BaseType));
                    }
                }
            }

            SetParams(LocalAlloc.GetIntInputs(Root), ARegisterType.Int);
            SetParams(LocalAlloc.GetVecInputs(Root), ARegisterType.Vector);

            DynamicMethod Mthd = new DynamicMethod(SubName, typeof(long), GetParamTypes(Params));

            Generator = Mthd.GetILGenerator();

            Subroutine = new ATranslatedSub(Mthd, Params);
        }

        private Type[] GetParamTypes(IList<ARegister> Params)
        {
            Type[] FixedArgs = ATranslatedSub.FixedArgTypes;

            Type[] Output = new Type[Params.Count + FixedArgs.Length];

            FixedArgs.CopyTo(Output, 0);

            int TypeIdx = FixedArgs.Length;

            for (int Index = 0; Index < Params.Count; Index++)
            {
                Output[TypeIdx++] = GetFieldType(Params[Index].Type);
            }

            return Output;
        }

        public int GetLocalIndex(ARegister Reg)
        {
            if (!Locals.TryGetValue(Reg, out int Index))
            {
                Generator.DeclareLocal(GetLocalType(Reg));

                Index = LocalsCount++;

                Locals.Add(Reg, Index);
            }

            return Index;
        }

        public Type GetLocalType(ARegister Reg) => GetFieldType(Reg.Type);

        public Type GetFieldType(ARegisterType RegType)
        {
            switch (RegType)
            {
                case ARegisterType.Flag:   return typeof(bool);
                case ARegisterType.Int:    return typeof(ulong);
                case ARegisterType.Vector: return typeof(AVec);
            }

            throw new ArgumentException(nameof(RegType));
        }

        public static ARegister GetRegFromBit(int Bit, ARegisterType BaseType)
        {
            if (Bit < 32)
            {
                return new ARegister(Bit, BaseType);
            }
            else if (BaseType == ARegisterType.Int)
            {
                return new ARegister(Bit & 0x1f, ARegisterType.Flag);
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(Bit));
            }
        }

        public static bool IsRegIndex(int Index)
        {
            return Index >= 0 && Index < 32;
        }
    }
}