IRDumper.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. public 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.SuccessorsCount > 0)
  51. {
  52. _builder.Append(" (");
  53. for (int i = 0; i < block.SuccessorsCount; i++)
  54. {
  55. DumpBlockName(block.GetSuccessor(i));
  56. if (i < block.SuccessorsCount - 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 == default)
  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 = operand.GetMemory();
  104. _builder.Append('[');
  105. DumpOperand(memOp.BaseAddress);
  106. if (memOp.Index != default)
  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(ControlFlowGraph cfg, Operation 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 Operation operation:
  145. if (operation.Instruction == Instruction.Phi)
  146. {
  147. PhiOperation phi = operation.AsPhi();
  148. _builder.Append("Phi ");
  149. for (int index = 0; index < phi.SourcesCount; index++)
  150. {
  151. _builder.Append('(');
  152. DumpBlockName(phi.GetBlock(cfg, index));
  153. _builder.Append(": ");
  154. DumpOperand(phi.GetSource(index));
  155. _builder.Append(')');
  156. if (index < phi.SourcesCount - 1)
  157. {
  158. _builder.Append(", ");
  159. }
  160. }
  161. break;
  162. }
  163. bool comparison = false;
  164. _builder.Append(operation.Instruction);
  165. if (operation.Instruction == Instruction.Extended)
  166. {
  167. _builder.Append('.').Append(operation.Intrinsic);
  168. }
  169. else if (operation.Instruction == Instruction.BranchIf ||
  170. operation.Instruction == Instruction.Compare)
  171. {
  172. comparison = true;
  173. }
  174. _builder.Append(' ');
  175. for (int index = 0; index < operation.SourcesCount; index++)
  176. {
  177. Operand source = operation.GetSource(index);
  178. if (index < operation.SourcesCount - 1)
  179. {
  180. DumpOperand(source);
  181. _builder.Append(", ");
  182. }
  183. else if (comparison)
  184. {
  185. _builder.Append((Comparison)source.AsInt32());
  186. }
  187. else
  188. {
  189. DumpOperand(source);
  190. }
  191. }
  192. break;
  193. }
  194. if (_symbolNames.Count == 1)
  195. {
  196. _builder.Append(" ;; ").Append(_symbolNames.First().Value);
  197. }
  198. else if (_symbolNames.Count > 1)
  199. {
  200. _builder.Append(" ;;");
  201. foreach ((ulong value, string name) in _symbolNames)
  202. {
  203. _builder.Append(" 0x").Append(value.ToString("X")).Append(" = ").Append(name);
  204. }
  205. }
  206. // Reset the set of symbols for the next Node we're going to dump.
  207. _symbolNames.Clear();
  208. }
  209. public static string GetDump(ControlFlowGraph cfg)
  210. {
  211. var dumper = new IRDumper(1);
  212. for (BasicBlock block = cfg.Blocks.First; block != null; block = block.ListNext)
  213. {
  214. dumper.Indent();
  215. dumper.DumpBlockHeader(block);
  216. dumper._builder.AppendLine();
  217. dumper.IncreaseIndentation();
  218. for (Operation node = block.Operations.First; node != default; node = node.ListNext)
  219. {
  220. dumper.Indent();
  221. dumper.DumpNode(cfg, node);
  222. dumper._builder.AppendLine();
  223. }
  224. dumper.DecreaseIndentation();
  225. }
  226. return dumper._builder.ToString();
  227. }
  228. private static string GetTypeName(OperandType type)
  229. {
  230. return type switch
  231. {
  232. OperandType.None => "none",
  233. OperandType.I32 => "i32",
  234. OperandType.I64 => "i64",
  235. OperandType.FP32 => "f32",
  236. OperandType.FP64 => "f64",
  237. OperandType.V128 => "v128",
  238. _ => throw new ArgumentException($"Invalid operand type \"{type}\"."),
  239. };
  240. }
  241. }
  242. }