RegisterToLocal.cs 1.5 KB

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