/ src / ARMeilleure / Translation / RegisterToLocal.cs
RegisterToLocal.cs
 1  using ARMeilleure.IntermediateRepresentation;
 2  using System.Collections.Generic;
 3  
 4  using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
 5  
 6  namespace ARMeilleure.Translation
 7  {
 8      static class RegisterToLocal
 9      {
10          public static void Rename(ControlFlowGraph cfg)
11          {
12              Dictionary<Register, Operand> registerToLocalMap = new();
13  
14              Operand GetLocal(Operand op)
15              {
16                  Register register = op.GetRegister();
17  
18                  if (!registerToLocalMap.TryGetValue(register, out Operand local))
19                  {
20                      local = Local(op.Type);
21  
22                      registerToLocalMap.Add(register, local);
23                  }
24  
25                  return local;
26              }
27  
28              for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
29              {
30                  for (Operation node = block.Operations.First; node != default; node = node.ListNext)
31                  {
32                      Operand dest = node.Destination;
33  
34                      if (dest != default && dest.Kind == OperandKind.Register)
35                      {
36                          node.Destination = GetLocal(dest);
37                      }
38  
39                      for (int index = 0; index < node.SourcesCount; index++)
40                      {
41                          Operand source = node.GetSource(index);
42  
43                          if (source.Kind == OperandKind.Register)
44                          {
45                              node.SetSource(index, GetLocal(source));
46                          }
47                      }
48                  }
49              }
50          }
51      }
52  }