IRDumper.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using ARMeilleure.IntermediateRepresentation;
  2. using ARMeilleure.Translation;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. namespace ARMeilleure.Diagnostics
  8. {
  9. class IRDumper
  10. {
  11. private const string Indentation = " ";
  12. private int _indentLevel;
  13. private readonly StringBuilder _builder;
  14. private readonly Dictionary<Operand, string> _localNames;
  15. private readonly Dictionary<ulong, string> _symbolNames;
  16. private IRDumper(int indent)
  17. {
  18. _indentLevel = indent;
  19. _builder = new StringBuilder();
  20. _localNames = new Dictionary<Operand, string>();
  21. _symbolNames = new Dictionary<ulong, string>();
  22. }
  23. private void Indent()
  24. {
  25. _builder.EnsureCapacity(_builder.Capacity + _indentLevel * Indentation.Length);
  26. for (int index = 0; index < _indentLevel; index++)
  27. {
  28. _builder.Append(Indentation);
  29. }
  30. }
  31. private void IncreaseIndentation()
  32. {
  33. _indentLevel++;
  34. }
  35. private void DecreaseIndentation()
  36. {
  37. _indentLevel--;
  38. }
  39. private void DumpBlockName(BasicBlock block)
  40. {
  41. _builder.Append("block").Append(block.Index);
  42. }
  43. private void DumpBlockHeader(BasicBlock block)
  44. {
  45. DumpBlockName(block);
  46. if (block.Frequency == BasicBlockFrequency.Cold)
  47. {
  48. _builder.Append(" cold");
  49. }
  50. if (block.SuccessorCount > 0)
  51. {
  52. _builder.Append(" (");
  53. for (int i = 0; i < block.SuccessorCount; i++)
  54. {
  55. DumpBlockName(block.GetSuccessor(i));
  56. if (i < block.SuccessorCount - 1)
  57. {
  58. _builder.Append(", ");
  59. }
  60. }
  61. _builder.Append(')');
  62. }
  63. _builder.Append(':');
  64. }
  65. private void DumpOperand(Operand operand)
  66. {
  67. if (operand == null)
  68. {
  69. _builder.Append("<NULL>");
  70. return;
  71. }
  72. _builder.Append(GetTypeName(operand.Type)).Append(' ');
  73. switch (operand.Kind)
  74. {
  75. case OperandKind.LocalVariable:
  76. if (!_localNames.TryGetValue(operand, out string localName))
  77. {
  78. localName = $"%{_localNames.Count}";
  79. _localNames.Add(operand, localName);
  80. }
  81. _builder.Append(localName);
  82. break;
  83. case OperandKind.Register:
  84. Register reg = operand.GetRegister();
  85. switch (reg.Type)
  86. {
  87. case RegisterType.Flag: _builder.Append('b'); break;
  88. case RegisterType.FpFlag: _builder.Append('f'); break;
  89. case RegisterType.Integer: _builder.Append('r'); break;
  90. case RegisterType.Vector: _builder.Append('v'); break;
  91. }
  92. _builder.Append(reg.Index);
  93. break;
  94. case OperandKind.Constant:
  95. string symbolName = Symbols.Get(operand.Value);
  96. if (symbolName != null && !_symbolNames.ContainsKey(operand.Value))
  97. {
  98. _symbolNames.Add(operand.Value, symbolName);
  99. }
  100. _builder.Append("0x").Append(operand.Value.ToString("X"));
  101. break;
  102. case OperandKind.Memory:
  103. var memOp = (MemoryOperand)operand;
  104. _builder.Append('[');
  105. DumpOperand(memOp.BaseAddress);
  106. if (memOp.Index != null)
  107. {
  108. _builder.Append(" + ");
  109. DumpOperand(memOp.Index);
  110. switch (memOp.Scale)
  111. {
  112. case Multiplier.x2: _builder.Append("*2"); break;
  113. case Multiplier.x4: _builder.Append("*4"); break;
  114. case Multiplier.x8: _builder.Append("*8"); break;
  115. }
  116. }
  117. if (memOp.Displacement != 0)
  118. {
  119. _builder.Append(" + 0x").Append(memOp.Displacement.ToString("X"));
  120. }
  121. _builder.Append(']');
  122. break;
  123. default:
  124. _builder.Append(operand.Type);
  125. break;
  126. }
  127. }
  128. private void DumpNode(Node node)
  129. {
  130. for (int index = 0; index < node.DestinationsCount; index++)
  131. {
  132. DumpOperand(node.GetDestination(index));
  133. if (index == node.DestinationsCount - 1)
  134. {
  135. _builder.Append(" = ");
  136. }
  137. else
  138. {
  139. _builder.Append(", ");
  140. }
  141. }
  142. switch (node)
  143. {
  144. case PhiNode phi:
  145. _builder.Append("Phi ");
  146. for (int index = 0; index < phi.SourcesCount; index++)
  147. {
  148. _builder.Append('(');
  149. DumpBlockName(phi.GetBlock(index));
  150. _builder.Append(": ");
  151. DumpOperand(phi.GetSource(index));
  152. _builder.Append(')');
  153. if (index < phi.SourcesCount - 1)
  154. {
  155. _builder.Append(", ");
  156. }
  157. }
  158. break;
  159. case Operation operation:
  160. bool comparison = false;
  161. _builder.Append(operation.Instruction);
  162. if (operation.Instruction == Instruction.Extended)
  163. {
  164. var intrinOp = (IntrinsicOperation)operation;
  165. _builder.Append('.').Append(intrinOp.Intrinsic);
  166. }
  167. else if (operation.Instruction == Instruction.BranchIf ||
  168. operation.Instruction == Instruction.Compare)
  169. {
  170. comparison = true;
  171. }
  172. _builder.Append(' ');
  173. for (int index = 0; index < operation.SourcesCount; index++)
  174. {
  175. Operand source = operation.GetSource(index);
  176. if (index < operation.SourcesCount - 1)
  177. {
  178. DumpOperand(source);
  179. _builder.Append(", ");
  180. }
  181. else if (comparison)
  182. {
  183. _builder.Append((Comparison)source.AsInt32());
  184. }
  185. else
  186. {
  187. DumpOperand(source);
  188. }
  189. }
  190. break;
  191. }
  192. if (_symbolNames.Count == 1)
  193. {
  194. _builder.Append(" ;; ").Append(_symbolNames.First().Value);
  195. }
  196. else if (_symbolNames.Count > 1)
  197. {
  198. _builder.Append(" ;;");
  199. foreach ((ulong value, string name) in _symbolNames)
  200. {
  201. _builder.Append(" 0x").Append(value.ToString("X")).Append(" = ").Append(name);
  202. }
  203. }
  204. // Reset the set of symbols for the next Node we're going to dump.
  205. _symbolNames.Clear();
  206. }
  207. public static string GetDump(ControlFlowGraph cfg)
  208. {
  209. var dumper = new IRDumper(1);
  210. for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
  211. {
  212. dumper.Indent();
  213. dumper.DumpBlockHeader(block);
  214. dumper._builder.AppendLine();
  215. dumper.IncreaseIndentation();
  216. for (Node node = block.Operations.First; node != null; node = node.ListNext)
  217. {
  218. dumper.Indent();
  219. dumper.DumpNode(node);
  220. dumper._builder.AppendLine();
  221. }
  222. dumper.DecreaseIndentation();
  223. }
  224. return dumper._builder.ToString();
  225. }
  226. private static string GetTypeName(OperandType type)
  227. {
  228. return type switch
  229. {
  230. OperandType.None => "none",
  231. OperandType.I32 => "i32",
  232. OperandType.I64 => "i64",
  233. OperandType.FP32 => "f32",
  234. OperandType.FP64 => "f64",
  235. OperandType.V128 => "v128",
  236. _ => throw new ArgumentException($"Invalid operand type \"{type}\"."),
  237. };
  238. }
  239. }
  240. }