aboutsummaryrefslogtreecommitdiff
path: root/ARMeilleure/Translation/RegisterToLocal.cs
blob: abb9b373c41ee1256dec03a582c4c4b4e0c3e6b8 (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
using ARMeilleure.IntermediateRepresentation;
using System.Collections.Generic;

using static ARMeilleure.IntermediateRepresentation.Operand.Factory;

namespace ARMeilleure.Translation
{
    static class RegisterToLocal
    {
        public static void Rename(ControlFlowGraph cfg)
        {
            Dictionary<Register, Operand> registerToLocalMap = new Dictionary<Register, Operand>();

            Operand GetLocal(Operand op)
            {
                Register register = op.GetRegister();

                if (!registerToLocalMap.TryGetValue(register, out Operand local))
                {
                    local = Local(op.Type);

                    registerToLocalMap.Add(register, local);
                }

                return local;
            }

            for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
            {
                for (Operation node = block.Operations.First; node != default; node = node.ListNext)
                {
                    Operand dest = node.Destination;

                    if (dest != default && dest.Kind == OperandKind.Register)
                    {
                        node.Destination = GetLocal(dest);
                    }

                    for (int index = 0; index < node.SourcesCount; index++)
                    {
                        Operand source = node.GetSource(index);

                        if (source.Kind == OperandKind.Register)
                        {
                            node.SetSource(index, GetLocal(source));
                        }
                    }
                }
            }
        }
    }
}