IRDumper.cs 9.0 KB

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