説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

BurstDisassembler.cs 62KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597
  1. #if UNITY_EDITOR || BURST_INTERNAL
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using Debug = UnityEngine.Debug;
  8. namespace Unity.Burst.Editor
  9. {
  10. /// <summary>
  11. /// Disassembler for Intel and ARM
  12. /// </summary>
  13. internal partial class BurstDisassembler
  14. {
  15. // The following member need to be reset/clear on each Reset()
  16. private readonly Dictionary<int, string> _fileName;
  17. private readonly Dictionary<int, string[]> _fileList;
  18. private readonly List<AsmToken> _tokens;
  19. private readonly List<AsmBlock> _blocks;
  20. private readonly List<string> _blockToString;
  21. private readonly List<int> _columnIndices;
  22. private readonly List<AsmLine> _lines;
  23. internal UsedRegisters _registersUsedAtLine;
  24. private readonly DictionaryGlobalLabel _globalLabels;
  25. private readonly List<TempLabelRef> _tempLabelRefs;
  26. private readonly Dictionary<int, StringSlice> _mapBlockIndexToGlobalLabel;
  27. private DictionaryLocalLabel _currentDictLocalLabel;
  28. public bool IsInitialized { get; private set; }
  29. // ^^^
  30. private string _input;
  31. private AsmKind _inputAsmKind;
  32. internal readonly StringBuilder _output;
  33. private bool _colored;
  34. // This is used to aligned instructions and there operands so they look like this
  35. //
  36. // mulps x,x,x
  37. // shufbps x,x,x
  38. //
  39. // instead of
  40. //
  41. // mulps x,x,x
  42. // shufbps x,x,x
  43. //
  44. // Notice if instruction name is longer than this no alignment will be done.
  45. private const int InstructionAlignment = 10;
  46. private static readonly StringSlice CVLocDirective = new StringSlice(".cv_loc");
  47. // Colors used for the tokens
  48. // TODO: Make this configurable via some editor settings?
  49. private const string DarkColorLineDirective = "#FFFF00";
  50. private const string DarkColorDirective = "#CCCCCC";
  51. private const string DarkColorIdentifier = "#d4d4d4";
  52. private const string DarkColorQualifier = "#DCDCAA";
  53. private const string DarkColorInstruction = "#4EC9B0";
  54. internal const string DarkColorInstructionSIMD = "#C586C0";
  55. internal const string DarkColorInstructionSIMDPacked = "#A586C0";
  56. internal const string DarkColorInstructionSIMDScalar = "#E586C0";
  57. private const string DarkColorRegister = "#d7ba7d";
  58. private const string DarkColorNumber = "#9cdcfe";
  59. private const string DarkColorString = "#ce9178";
  60. private const string DarkColorComment = "#6A9955";
  61. private const string LightColorLineDirective = "#888800";
  62. private const string LightColorDirective = "#444444";
  63. private const string LightColorIdentifier = "#1c1c1c";
  64. private const string LightColorQualifier = "#267f99";
  65. private const string LightColorInstruction = "#0451a5";
  66. private const string LightColorInstructionSIMD = "#0000ff";
  67. private const string LightColorInstructionSIMDPacked = "#8000ff";
  68. private const string LightColorInstructionSIMDScalar = "#8050ff";
  69. private const string LightColorRegister = "#811f3f";
  70. private const string LightColorNumber = "#007ACC";
  71. private const string LightColorString = "#a31515";
  72. private const string LightColorComment = "#008000";
  73. private string ColorLineDirective;
  74. private string ColorDirective;
  75. private string ColorIdentifier;
  76. private string ColorQualifier;
  77. private string ColorInstruction;
  78. private string ColorInstructionSIMD;
  79. private string ColorInstructionSIMDPacked;
  80. private string ColorInstructionSIMDScalar;
  81. private string ColorRegister;
  82. private string ColorNumber;
  83. private string ColorString;
  84. private string ColorComment;
  85. private char _commentStart;
  86. public BurstDisassembler()
  87. {
  88. _fileName = new Dictionary<int, string>();
  89. _fileList = new Dictionary<int, string[]>();
  90. _tokens = new List<AsmToken>(65536);
  91. _blocks = new List<AsmBlock>(128);
  92. _blockToString = new List<string>(128);
  93. _columnIndices = new List<int>(65536);
  94. _lines = new List<AsmLine>(4096);
  95. _registersUsedAtLine = new UsedRegisters(4096);
  96. _tempLabelRefs = new List<TempLabelRef>(4096);
  97. _globalLabels = new DictionaryGlobalLabel(128);
  98. _mapBlockIndexToGlobalLabel = new Dictionary<int, StringSlice>(128);
  99. _output = new StringBuilder();
  100. }
  101. internal List<int> ColumnIndices => _columnIndices;
  102. /// <summary>
  103. /// Gets all the blocks.
  104. /// </summary>
  105. public List<AsmBlock> Blocks => _blocks;
  106. /// <summary>
  107. /// Gets whether the disassembly is colored.
  108. /// </summary>
  109. public bool IsColored => _colored;
  110. /// <summary>
  111. /// Gets all the lines for all the blocks.
  112. /// </summary>
  113. public List<AsmLine> Lines => _lines;
  114. /// <summary>
  115. /// Gets all the tokens
  116. /// </summary>
  117. public List<AsmToken> Tokens => _tokens;
  118. public int LineUsedReg(int lineIdx, string reg) => _registersUsedAtLine.RegisterMatch(lineIdx, reg);
  119. public bool LineUsesRegs(int lineIdx, out List<string> usedRegs) => _registersUsedAtLine.LineContainsRegs(lineIdx, out usedRegs);
  120. public List<string> CleanRegs(List<string> regs) => _registersUsedAtLine.CleanRegs(regs);
  121. public int GetRegisterTokenIndex(AsmLine line, string reg, int startIndex = 0)
  122. {
  123. var idx = -1;
  124. var i = Math.Max(line.TokenIndex, startIndex);
  125. var len = line.TokenIndex + line.Length;
  126. for (; i < len; i++)
  127. {
  128. var token = Tokens[i];
  129. if (_registersUsedAtLine.RegisterEquality(reg, GetTokenAsText(token)))
  130. {
  131. idx = i;
  132. break;
  133. }
  134. }
  135. return idx;
  136. }
  137. /// <summary>
  138. /// Get a token index for a particular block, line number and column number.
  139. /// </summary>
  140. /// <param name="blockIndex"></param>
  141. /// <param name="line"></param>
  142. /// <param name="column"></param>
  143. /// <param name="lineIndex">Returns the line index to query <see cref="Lines"/></param>
  144. /// <returns>The token index to use with <see cref="GetToken"/> or -1 if the line, column was not found.</returns>
  145. public int GetTokenIndexFromColumn(int blockIndex, int line, int column, out int lineIndex)
  146. {
  147. lineIndex = -1;
  148. var block = _blocks[blockIndex];
  149. var lineStartIndex = block.LineIndex + line;
  150. var asmLine = _lines[lineStartIndex];
  151. if (asmLine.Kind != AsmLineKind.SourceFileLocation)
  152. {
  153. var columnIndex = asmLine.ColumnIndex;
  154. for (int j = 1; j < asmLine.Length; j++)
  155. {
  156. // _columnIndices doesn't have an index for the first token (because the column is always 0)
  157. var tokenColumn = _columnIndices[columnIndex + j - 1];
  158. var token = GetToken(asmLine.TokenIndex + j);
  159. if (tokenColumn <= column && column < tokenColumn + token.Length)
  160. {
  161. lineIndex = lineStartIndex;
  162. return asmLine.TokenIndex + j;
  163. }
  164. }
  165. }
  166. return -1;
  167. }
  168. /// <summary>
  169. /// Gets or renders a particular block to text without caching the result.
  170. /// </summary>
  171. /// <param name="blockIndex">The block to render.</param>
  172. /// <param name="colored">Whether output should be colored.</param>
  173. /// <returns>A string representation of the block.</returns>
  174. public string GetOrRenderBlockToTextUncached(int blockIndex, bool colored)
  175. {
  176. return RenderBlock(blockIndex, colored);
  177. }
  178. /// <summary>
  179. /// Gets or renders a particular block to text (colored if specified at <see cref="Initialize"/> time)
  180. /// </summary>
  181. /// <param name="blockIndex">The block to render.</param>
  182. /// <returns>A string representation of the block.</returns>
  183. public string GetOrRenderBlockToText(int blockIndex)
  184. {
  185. var str = _blockToString[blockIndex];
  186. if (str == null)
  187. {
  188. str = RenderBlock(blockIndex, _colored);
  189. _blockToString[blockIndex] = str;
  190. }
  191. return str;
  192. }
  193. /// <summary>
  194. /// Gets a token at the specified token index.
  195. /// </summary>
  196. /// <param name="tokenIndex">The token index</param>
  197. /// <returns>The token available at the specified index</returns>
  198. public AsmToken GetToken(int tokenIndex)
  199. {
  200. return _tokens[tokenIndex];
  201. }
  202. /// <summary>
  203. /// Returns the text representation of the token at the specified index
  204. /// </summary>
  205. /// <param name="tokenIndex"></param>
  206. /// <returns></returns>
  207. public StringSlice GetTokenAsTextSlice(int tokenIndex)
  208. {
  209. return _tokens[tokenIndex].Slice(_input);
  210. }
  211. /// <summary>
  212. /// Returns the text representation of the specified token.
  213. /// </summary>
  214. public StringSlice GetTokenAsTextSlice(AsmToken token)
  215. {
  216. return token.Slice(_input);
  217. }
  218. /// <summary>
  219. /// Returns the text representation of the specified token.
  220. /// </summary>
  221. public string GetTokenAsText(AsmToken token)
  222. {
  223. return token.ToString(_input);
  224. }
  225. /// <summary>
  226. /// Try and get description of <see cref="instruction"/>.
  227. /// </summary>
  228. /// <param name="instruction">Instruction to query information about.</param>
  229. /// <param name="info">If instruction present the queried information, else default string.</param>
  230. /// <returns>Whether instruction was present in burst disassembler core.</returns>
  231. internal bool GetInstructionInformation(string instruction, out string info)
  232. {
  233. switch (_inputAsmKind)
  234. {
  235. case AsmKind.Intel:
  236. return X86AsmInstructionInfo.GetX86InstructionInfo(instruction, out info);
  237. case AsmKind.ARM:
  238. return ARM64InstructionInfo.GetARM64Info(instruction, out info);
  239. case AsmKind.LLVMIR:
  240. return LLVMIRInstructionInfo.GetLLVMIRInfo(instruction, out info);
  241. case AsmKind.Wasm:
  242. return WasmInstructionInfo.GetWasmInfo(instruction, out info);
  243. default:
  244. throw new InvalidOperationException($"No instruction information for {_inputAsmKind}");
  245. }
  246. }
  247. /// <summary>
  248. /// Initialize the disassembler with the input and parametesr.
  249. /// </summary>
  250. /// <param name="input"></param>
  251. /// <param name="asmKind"></param>
  252. /// <param name="useDarkSkin"></param>
  253. /// <param name="useSyntaxColoring"></param>
  254. /// <param name="smellTest"></param>
  255. /// <returns></returns>
  256. public bool Initialize(string input, AsmKind asmKind, bool useDarkSkin = true, bool useSyntaxColoring = true, bool smellTest = false)
  257. {
  258. try
  259. {
  260. InitializeImpl(input, asmKind, useDarkSkin, useSyntaxColoring, smellTest);
  261. IsInitialized = true;
  262. }
  263. catch (Exception ex)
  264. {
  265. Reset();
  266. #if BURST_INTERNAL
  267. throw new InvalidOperationException($"Error while trying to disassemble the input: {ex}");
  268. #else
  269. UnityEngine.Debug.Log($"Error while trying to disassemble the input: {ex}");
  270. #endif
  271. }
  272. return IsInitialized;
  273. }
  274. /// <summary>
  275. /// Helper method to output the full (colored) text as we did before.
  276. ///
  277. /// This method will be deprecated. Just here for testing during the transition.
  278. /// </summary>
  279. public string RenderFullText()
  280. {
  281. // If not initialized correctly (disassembly failed), return the input string as-is
  282. if (!IsInitialized) return _input ?? string.Empty;
  283. var builder = new StringBuilder();
  284. for (int i = 0; i < _blocks.Count; i++)
  285. {
  286. var text = GetOrRenderBlockToText(i);
  287. builder.Append(text);
  288. }
  289. return builder.ToString();
  290. }
  291. private void Reset()
  292. {
  293. _registersUsedAtLine.Clear();
  294. _fileList.Clear();
  295. _fileName.Clear();
  296. _tokens.Clear();
  297. _blocks.Clear();
  298. _blockTextIdxs.Clear();
  299. _blockToString.Clear();
  300. _columnIndices.Clear();
  301. _lines.Clear();
  302. _tempLabelRefs.Clear();
  303. _globalLabels.Clear();
  304. _mapBlockIndexToGlobalLabel.Clear();
  305. _currentDictLocalLabel = null;
  306. IsInitialized = false;
  307. }
  308. private AsmTokenKindProvider _tokenProvider = null;
  309. private void InitializeImpl(string input, AsmKind asmKind, bool useDarkSkin = true, bool useSyntaxColoring = true, bool smellTest=false)
  310. {
  311. _commentStart = (asmKind == AsmKind.Intel || asmKind == AsmKind.Wasm) ? '#' : ';';
  312. UseSkin(useDarkSkin, smellTest);
  313. _colored = useSyntaxColoring;
  314. _tokenProvider = InitializeInput(input, asmKind);
  315. _registersUsedAtLine.AddTokenProvider(_tokenProvider);
  316. ParseAndProcessTokens(_tokenProvider);
  317. }
  318. /// <summary>
  319. /// Finds the block index encapsulating <see cref="textIdx"/>.
  320. /// </summary>
  321. /// <param name="textIdx">Text index relative to <see cref="_input"/>.</param>
  322. /// <param name="start">Left-most block index to search within.</param>
  323. /// <returns>(block index, blocks start index in <see cref="_input"/>)</returns>
  324. public (int idx, int l) GetBlockIdxFromTextIdx(int textIdx)
  325. {
  326. return GetBlockIdxFromTextIdx(textIdx, 0);
  327. }
  328. /// <summary>
  329. /// Finds the block index encapsulating <see cref="textIdx"/>.
  330. /// </summary>
  331. /// <param name="textIdx">Text index relative to <see cref="_input"/>.</param>
  332. /// <param name="start">Left-most block index to search within.</param>
  333. /// <returns>(block index, blocks start index in <see cref="_input"/>)</returns>
  334. public (int idx, int l) GetBlockIdxFromTextIdx(int textIdx, int start)
  335. {
  336. int end = _blockTextIdxs.Count-1;
  337. while (start <= end)
  338. {
  339. int mid = (end + start) / 2;
  340. var (startIdx, endIdx) = _blockTextIdxs[mid];
  341. if (startIdx <= textIdx && textIdx <= endIdx)
  342. {
  343. return (mid, startIdx);
  344. }
  345. if (endIdx < textIdx)
  346. {
  347. start = mid + 1;
  348. }
  349. else
  350. {
  351. end = mid - 1;
  352. }
  353. }
  354. return (-1, -1);
  355. }
  356. private bool _smellTest;
  357. private void UseSkin(bool useDarkSkin, bool smellTest)
  358. {
  359. _smellTest = smellTest;
  360. if (useDarkSkin)
  361. {
  362. ColorLineDirective = DarkColorLineDirective;
  363. ColorDirective = DarkColorDirective;
  364. ColorIdentifier = DarkColorIdentifier;
  365. ColorQualifier = DarkColorQualifier;
  366. ColorInstruction = DarkColorInstruction;
  367. ColorInstructionSIMD = DarkColorInstructionSIMD;
  368. ColorInstructionSIMDPacked = DarkColorInstructionSIMDPacked;
  369. ColorInstructionSIMDScalar = DarkColorInstructionSIMDScalar;
  370. ColorRegister = DarkColorRegister;
  371. ColorNumber = DarkColorNumber;
  372. ColorString = DarkColorString;
  373. ColorComment = DarkColorComment;
  374. }
  375. else
  376. {
  377. ColorLineDirective = LightColorLineDirective;
  378. ColorDirective = LightColorDirective;
  379. ColorIdentifier = LightColorIdentifier;
  380. ColorQualifier = LightColorQualifier;
  381. ColorInstruction = LightColorInstruction;
  382. ColorInstructionSIMD = LightColorInstructionSIMD;
  383. ColorInstructionSIMDPacked = LightColorInstructionSIMDPacked;
  384. ColorInstructionSIMDScalar = LightColorInstructionSIMDScalar;
  385. ColorRegister = LightColorRegister;
  386. ColorNumber = LightColorNumber;
  387. ColorString = LightColorString;
  388. ColorComment = LightColorComment;
  389. }
  390. }
  391. private int AlignInstruction(StringBuilder output, int instructionLength, AsmKind asmKind)
  392. {
  393. // Only support Intel for now
  394. if (instructionLength >= InstructionAlignment || asmKind != AsmKind.Intel)
  395. return 0;
  396. int align = InstructionAlignment - instructionLength;
  397. output.Append(' ', align);
  398. return align;
  399. }
  400. private AsmTokenKindProvider InitializeInput(string input, AsmKind asmKind)
  401. {
  402. AsmTokenKindProvider asmTokenProvider = null;
  403. _input = input;
  404. _inputAsmKind = asmKind;
  405. switch (asmKind)
  406. {
  407. case AsmKind.Intel:
  408. asmTokenProvider = (AsmTokenKindProvider)X86AsmTokenKindProvider.Instance;
  409. break;
  410. case AsmKind.ARM:
  411. asmTokenProvider = (AsmTokenKindProvider)ARM64AsmTokenKindProvider.Instance;
  412. break;
  413. case AsmKind.Wasm:
  414. asmTokenProvider = (AsmTokenKindProvider)WasmAsmTokenKindProvider.Instance;
  415. break;
  416. case AsmKind.LLVMIR:
  417. asmTokenProvider = (AsmTokenKindProvider)LLVMIRAsmTokenKindProvider.Instance;
  418. break;
  419. default:
  420. throw new InvalidOperationException($"No {nameof(AsmTokenKindProvider)} for {asmKind}");
  421. }
  422. return asmTokenProvider;
  423. }
  424. private int GetLineLen(in AsmLine line)
  425. {
  426. int len = 0;
  427. int offset = line.TokenIndex;
  428. int numLineTokens = line.Length;
  429. for (int i = 0; i < numLineTokens; i++)
  430. {
  431. AsmToken token = _tokens[offset + i];
  432. len += token.Kind != AsmTokenKind.NewLine
  433. ? token.Length
  434. : 1; // We don't use windows line endings, but internal token might,
  435. }
  436. return len;
  437. }
  438. private void ParseAndProcessTokens(AsmTokenKindProvider asmTokenProvider)
  439. {
  440. Reset();
  441. var tokenizer = new AsmTokenizer(_input, _inputAsmKind, asmTokenProvider, _commentStart);
  442. // Adjust token size
  443. var pseudoTokenSizeMax = _input.Length / 7;
  444. if (pseudoTokenSizeMax > _tokens.Capacity)
  445. {
  446. _tokens.Capacity = pseudoTokenSizeMax;
  447. }
  448. // Start the top-block as a directive block
  449. var block = new AsmBlock { Kind = AsmBlockKind.Block };
  450. AsmLine line = default;
  451. var blockKindDetectFlags = BlockKindDetectFlags.None;
  452. // Skip first line
  453. // Don't tokenize the first line that contains e.g:
  454. // While compiling job: System.Single BurstJobTester/MyJob::CheckFmaSlow(System.Single,System.Single,System.Single)
  455. while (tokenizer.TryGetNextToken(out var token))
  456. {
  457. if (token.Kind == AsmTokenKind.NewLine)
  458. {
  459. break;
  460. }
  461. }
  462. // Read all tokens
  463. // Create blocks and lines on the fly, record functions
  464. int totalIdx = 0;
  465. int blockStartIdx = 0;
  466. bool newLine = false;
  467. var (possiblyRemoveAlignment, addedAlignment) = (false, 0);
  468. while (tokenizer.TryGetNextToken(out var token))
  469. {
  470. var tokenIndex = _tokens.Count;
  471. _tokens.Add(token);
  472. if (newLine)
  473. {
  474. if (possiblyRemoveAlignment)
  475. {
  476. // Alignment was added just before a newline
  477. totalIdx -= addedAlignment;
  478. }
  479. // Push new line
  480. if (line.Kind == AsmLineKind.SourceFile)
  481. {
  482. // Have to remove the line from totalIdx, for proper block idx saving.
  483. totalIdx -= GetLineLen(line);
  484. ProcessSourceFile(ref line);
  485. // We drop this line, we don't store SourceFile line as-is but just below as SourceFileLocation
  486. }
  487. else
  488. {
  489. var lineRef = new AsmLineRef(_blocks.Count, block.Length);
  490. if (line.Kind == AsmLineKind.SourceLocation)
  491. {
  492. // Have to remove the line from totalIdx, for proper block idx saving.
  493. totalIdx -= GetLineLen(line);
  494. ProcessSourceLocation(ref line, ref totalIdx);
  495. // after this, the line is now a SourceFileLocation
  496. }
  497. else if (line.Kind == AsmLineKind.LabelDeclaration)
  498. {
  499. // Record labels (global and locals)
  500. ProcessLabelDeclaration(lineRef, line);
  501. }
  502. else if (line.Kind == AsmLineKind.CodeBranch || line.Kind == AsmLineKind.CodeJump)
  503. {
  504. // Record temp branch/jumps
  505. ProcessJumpOrBranch(lineRef, ref line);
  506. }
  507. _lines.Add(line);
  508. _registersUsedAtLine.PushLine();
  509. block.Length++;
  510. }
  511. bool previousLineWasBranch = line.Kind == AsmLineKind.CodeBranch;
  512. // Reset the line
  513. line = default;
  514. line.Kind = AsmLineKind.Empty;
  515. line.TokenIndex = tokenIndex;
  516. // We create a new block when hitting a label declaration
  517. // If the previous line was a conditional branch, it is like having an implicit label
  518. if (previousLineWasBranch || token.Kind == AsmTokenKind.Label)
  519. {
  520. // Refine the kind of block before pushing it
  521. if ((blockKindDetectFlags & BlockKindDetectFlags.Code) != 0)
  522. {
  523. block.Kind = AsmBlockKind.Code;
  524. }
  525. else if ((blockKindDetectFlags & BlockKindDetectFlags.Data) != 0)
  526. {
  527. block.Kind = AsmBlockKind.Data;
  528. }
  529. else if ((blockKindDetectFlags & BlockKindDetectFlags.Directive) != 0)
  530. {
  531. block.Kind = AsmBlockKind.Directive;
  532. }
  533. // Push the current block
  534. _blocks.Add(block);
  535. _blockTextIdxs.Add((blockStartIdx, totalIdx-1));
  536. _blockToString.Add(null);
  537. // Create a new block
  538. blockStartIdx = totalIdx;
  539. block = new AsmBlock
  540. {
  541. Kind = AsmBlockKind.None,
  542. LineIndex = _lines.Count,
  543. Length = 0
  544. };
  545. blockKindDetectFlags = BlockKindDetectFlags.None;
  546. }
  547. }
  548. // If the current line is still undefined try to detect what kind of line we have
  549. var lineKind = line.Kind;
  550. if (lineKind == AsmLineKind.Empty)
  551. {
  552. switch (token.Kind)
  553. {
  554. case AsmTokenKind.Directive:
  555. lineKind = AsmLineKind.Directive;
  556. blockKindDetectFlags |= BlockKindDetectFlags.Directive;
  557. break;
  558. case AsmTokenKind.SourceFile:
  559. lineKind = AsmLineKind.SourceFile;
  560. break;
  561. case AsmTokenKind.SourceLocation:
  562. lineKind = AsmLineKind.SourceLocation;
  563. blockKindDetectFlags |= BlockKindDetectFlags.Code;
  564. break;
  565. case AsmTokenKind.DataDirective:
  566. lineKind = AsmLineKind.Data;
  567. blockKindDetectFlags |= BlockKindDetectFlags.Data;
  568. break;
  569. case AsmTokenKind.Instruction:
  570. case AsmTokenKind.InstructionSIMD:
  571. lineKind = AsmLineKind.Code;
  572. blockKindDetectFlags |= BlockKindDetectFlags.Code;
  573. break;
  574. case AsmTokenKind.BranchInstruction:
  575. lineKind = AsmLineKind.CodeBranch;
  576. blockKindDetectFlags |= BlockKindDetectFlags.Code;
  577. break;
  578. case AsmTokenKind.JumpInstruction:
  579. lineKind = AsmLineKind.CodeJump;
  580. blockKindDetectFlags |= BlockKindDetectFlags.Code;
  581. break;
  582. case AsmTokenKind.CallInstruction:
  583. lineKind = AsmLineKind.CodeCall;
  584. blockKindDetectFlags |= BlockKindDetectFlags.Code;
  585. break;
  586. case AsmTokenKind.ReturnInstruction:
  587. lineKind = AsmLineKind.CodeReturn;
  588. blockKindDetectFlags |= BlockKindDetectFlags.Code;
  589. break;
  590. case AsmTokenKind.Label:
  591. lineKind = newLine ? AsmLineKind.LabelDeclaration : AsmLineKind.Empty;
  592. break;
  593. case AsmTokenKind.Comment:
  594. lineKind = AsmLineKind.Comment;
  595. break;
  596. case AsmTokenKind.FunctionBegin:
  597. lineKind = AsmLineKind.FunctionBegin;
  598. break;
  599. case AsmTokenKind.FunctionEnd:
  600. lineKind = AsmLineKind.FunctionEnd;
  601. break;
  602. }
  603. line.Kind = lineKind;
  604. }
  605. // Add alignment for it to match the output BurstDisassembler gives to the outside world
  606. switch (token.Kind)
  607. {
  608. case AsmTokenKind.Instruction:
  609. case AsmTokenKind.CallInstruction:
  610. case AsmTokenKind.BranchInstruction:
  611. case AsmTokenKind.JumpInstruction:
  612. case AsmTokenKind.ReturnInstruction:
  613. case AsmTokenKind.InstructionSIMD:
  614. if (!(token.Length >= InstructionAlignment || _inputAsmKind != AsmKind.Intel))
  615. {
  616. totalIdx += (InstructionAlignment - token.Length);
  617. possiblyRemoveAlignment = true;
  618. addedAlignment = InstructionAlignment - token.Length;
  619. }
  620. break;
  621. // If new line is hit do not set to false, as to carry the information
  622. // into the next iteration.
  623. case AsmTokenKind.NewLine:
  624. break;
  625. default:
  626. possiblyRemoveAlignment = false;
  627. break;
  628. }
  629. // Add used registers to the index appropriate for specific line.
  630. if (token.Kind == AsmTokenKind.Register)
  631. {
  632. _registersUsedAtLine.Add(_lines.Count, GetTokenAsText(token));
  633. }
  634. line.Length++;
  635. newLine = token.Kind == AsmTokenKind.NewLine;
  636. totalIdx += newLine ? 1 : token.Length;
  637. }
  638. // Process the remaining line
  639. if (line.Length > 0)
  640. {
  641. _lines.Add(line);
  642. block.Length++;
  643. _registersUsedAtLine.PushLine();
  644. }
  645. if (block.Length > 0)
  646. {
  647. _blocks.Add(block);
  648. _blockTextIdxs.Add((blockStartIdx, totalIdx - 1));
  649. _blockToString.Add(null);
  650. }
  651. ProcessLabelsAndCreateEdges();
  652. }
  653. private void ProcessLabelDeclaration(in AsmLineRef lineRef, in AsmLine line)
  654. {
  655. var iterator = GetIterator(line);
  656. iterator.TryGetNext(out var token); // label
  657. var text = token.Slice(_input);
  658. if (IsLabelLocal(text))
  659. {
  660. // if ´_currentDictLocalLabel==null´ we just hit a local label prior to any global labels.
  661. // So we simply create a empty global label, to hold this local:
  662. if (_currentDictLocalLabel is null)
  663. {
  664. _currentDictLocalLabel = _globalLabels.GetOrCreate(new StringSlice(""), lineRef);
  665. _mapBlockIndexToGlobalLabel[lineRef.BlockIndex] = text;
  666. }
  667. // Record local labels to the current global label dictionary
  668. _currentDictLocalLabel.Add(text, lineRef);
  669. }
  670. else
  671. {
  672. // Create a local label dictionary per global label
  673. _currentDictLocalLabel = _globalLabels.GetOrCreate(text, lineRef);
  674. // Associate the current block index to this global index
  675. _mapBlockIndexToGlobalLabel[lineRef.BlockIndex] = text;
  676. }
  677. }
  678. private void ProcessJumpOrBranch(in AsmLineRef lineRef, ref AsmLine line)
  679. {
  680. var iterator = GetIterator(line);
  681. iterator.TryGetNext(out _); // branch/jump instruction
  682. if (iterator.TryGetNext(out var label, out var labelTokenIndex))
  683. {
  684. if (label.Kind == AsmTokenKind.String || label.Kind == AsmTokenKind.Identifier || label.Kind == AsmTokenKind.Label)
  685. {
  686. // In case the token is not a label, convert it to a label after this
  687. if (label.Kind != AsmTokenKind.Label)
  688. {
  689. var token = _tokens[labelTokenIndex];
  690. token = new AsmToken(AsmTokenKind.Label, token.Position, token.AlignedPosition, token.Length);
  691. _tokens[labelTokenIndex] = token;
  692. }
  693. var currentGlobalBlockIndex = _currentDictLocalLabel.GlobalLabelLineRef.BlockIndex;
  694. _tempLabelRefs.Add(new TempLabelRef(currentGlobalBlockIndex, lineRef, label.Position, label.Length));
  695. }
  696. }
  697. }
  698. private void ProcessSourceFile(ref AsmLine line)
  699. {
  700. var it = GetIterator(line);
  701. it.TryGetNext(out _); // skip .file or .cv_file
  702. int index = 0;
  703. if (it.TryGetNext(out var token) && token.Kind == AsmTokenKind.Number)
  704. {
  705. var numberAsStr = GetTokenAsText(token);
  706. index = int.Parse(numberAsStr);
  707. }
  708. if (it.TryGetNext(out token) && token.Kind == AsmTokenKind.String)
  709. {
  710. var filename = GetTokenAsText(token).Trim('"').Replace('\\', '/');
  711. string[] fileLines = null;
  712. //blockIdx += 4 + System.IO.Path.GetFileName(filename).Length;// ("=== " + filename).Length
  713. try
  714. {
  715. if (System.IO.File.Exists(filename))
  716. {
  717. fileLines = System.IO.File.ReadAllLines(filename);
  718. }
  719. }
  720. catch
  721. {
  722. fileLines = null;
  723. }
  724. _fileName.Add(index, filename);
  725. _fileList.Add(index, fileLines);
  726. }
  727. }
  728. private void ProcessSourceLocation(ref AsmLine line, ref int blockIdx)
  729. {
  730. var it = GetIterator(line);
  731. // .loc {fileno} {lineno} [column] [options] -
  732. // .cv_loc funcid fileno lineno [column]
  733. int fileno = 0;
  734. int colno = 0;
  735. int lineno = 0; // NB 0 indicates no information given
  736. if (it.TryGetNext(out var token))
  737. {
  738. var tokenSlice = GetTokenAsTextSlice(token);
  739. if (tokenSlice == CVLocDirective)
  740. {
  741. // skip funcId
  742. it.TryGetNext(out token);
  743. }
  744. }
  745. if (it.TryGetNext(out token) && token.Kind == AsmTokenKind.Number)
  746. {
  747. var numberAsStr = GetTokenAsText(token);
  748. fileno = int.Parse(numberAsStr);
  749. }
  750. if (it.TryGetNext(out token) && token.Kind == AsmTokenKind.Number)
  751. {
  752. var numberAsStr = GetTokenAsText(token);
  753. lineno = int.Parse(numberAsStr);
  754. }
  755. if (it.TryGetNext(out token) && token.Kind == AsmTokenKind.Number)
  756. {
  757. var numberAsStr = GetTokenAsText(token);
  758. colno = int.Parse(numberAsStr);
  759. }
  760. // Transform the SourceLocation into a SourceFileLocation
  761. line.Kind = AsmLineKind.SourceFileLocation;
  762. line.SourceFileNumber = fileno;
  763. line.SourceLineNumber = lineno;
  764. line.SourceColumnNumber = colno;
  765. // Make sure blockTextIdxs are correct
  766. if (fileno == 0) return;
  767. blockIdx += 2 + System.IO.Path.GetFileName(_fileName[fileno]).Length; // ("; " + filename).length
  768. if (lineno != 0)
  769. {
  770. blockIdx += 4 + lineno.ToString().Length + (colno + 1).ToString().Length;// "(x, y)"
  771. if (_fileList.ContainsKey(fileno) && _fileList[fileno] != null && lineno - 1 < _fileList[fileno].Length)
  772. {
  773. blockIdx += _fileList[fileno][lineno - 1].Length;
  774. }
  775. }
  776. blockIdx++; // \n
  777. }
  778. private static bool IsLabelLocal(in StringSlice slice)
  779. {
  780. return slice.StartsWith(".L");
  781. }
  782. private void ProcessLabelsAndCreateEdges()
  783. {
  784. foreach (var tempLabelRef in _tempLabelRefs)
  785. {
  786. var globalBlockIndex = tempLabelRef.GlobalBlockIndex;
  787. // Source Block + Line
  788. var srcRef = tempLabelRef.LineRef;
  789. var srcBlockIndex = srcRef.BlockIndex;
  790. var srcLineIndex = srcRef.LineIndex;
  791. var srcBlock = _blocks[srcBlockIndex];
  792. // Line where the edge occurs
  793. var srcLine = _lines[srcBlock.LineIndex + srcLineIndex];
  794. var label = new StringSlice(_input, tempLabelRef.StringIndex, tempLabelRef.StringLength);
  795. var isLocal = IsLabelLocal(label);
  796. AsmLineRef destRef;
  797. if (isLocal)
  798. {
  799. var globalLabel = _mapBlockIndexToGlobalLabel[globalBlockIndex];
  800. var localLabel = _globalLabels[globalLabel];
  801. destRef = localLabel[label];
  802. }
  803. else
  804. {
  805. if (_globalLabels.TryGetValue(label, out var entry))
  806. {
  807. destRef = entry.GlobalLabelLineRef;
  808. }
  809. else
  810. {
  811. continue; // Some global labels (at least on arm) e.g. __divsi3 are runtime library defined and not present at all in the source
  812. }
  813. }
  814. // Destination Block + Line
  815. var dstBlock = _blocks[destRef.BlockIndex];
  816. // Create edges
  817. srcBlock.AddEdge(new AsmEdge(AsmEdgeKind.OutBound, srcRef, destRef));
  818. dstBlock.AddEdge(new AsmEdge(AsmEdgeKind.InBound, destRef, srcRef));
  819. // For conditional branches, add the false branch as well
  820. // TODO: should we comment that in the meantime or?
  821. if (srcLine.Kind == AsmLineKind.CodeBranch)
  822. {
  823. // The implicit destination block for the false branch is the next block of the source
  824. // TODO: we pickup the line 0, while we might want to select the first code of line or first Label declaration
  825. var blockFalseRef = new AsmLineRef(srcRef.BlockIndex + 1, 0);
  826. dstBlock = _blocks[blockFalseRef.BlockIndex];
  827. srcBlock.AddEdge(new AsmEdge(AsmEdgeKind.OutBound, srcRef, blockFalseRef));
  828. dstBlock.AddEdge(new AsmEdge(AsmEdgeKind.InBound, blockFalseRef, srcRef));
  829. }
  830. }
  831. // Sort all edges
  832. foreach (var block in Blocks)
  833. {
  834. block.SortEdges();
  835. }
  836. }
  837. private List<(int startIdx, int endIdx)> _blockTextIdxs = new List<(int startIdx, int endIdx)>(128);
  838. public List<(int startIdx, int endIdx)> BlockIdxs => _blockTextIdxs;
  839. private string RenderBlock(int blockIndex, bool colored)
  840. {
  841. var block = _blocks[blockIndex];
  842. _output.Clear();
  843. var lineStart = block.LineIndex;
  844. var length = block.Length;
  845. for (int i = 0; i < length; i++)
  846. {
  847. var line = _lines[lineStart + i];
  848. RenderLine(ref line, colored);
  849. // write back the line that has been modified. But only if we run with the same color mode,
  850. // that the disassembler was initialized with.
  851. if (colored == _colored) _lines[lineStart + i] = line;
  852. }
  853. var str = _output.ToString();
  854. _output.Length = 0;
  855. return str;
  856. }
  857. internal void RenderLine(ref AsmLine line, bool colored)
  858. {
  859. // Render this line with a specific renderer
  860. if (line.Kind == AsmLineKind.SourceFileLocation)
  861. {
  862. RenderSourceFileLocation(ref line, colored);
  863. return;
  864. }
  865. // Process all tokens
  866. var length = line.Length;
  867. int column = 0;
  868. for (int i = 0; i < length; i++)
  869. {
  870. var token = _tokens[line.TokenIndex + i];
  871. var slice = token.Slice(_input);
  872. // We don't record the first column because it is always 0
  873. if (column > 0)
  874. {
  875. if (line.ColumnIndex == 0)
  876. {
  877. line.ColumnIndex = _columnIndices.Count;
  878. }
  879. _columnIndices.Add(column);
  880. }
  881. if (colored)
  882. {
  883. switch (token.Kind)
  884. {
  885. case AsmTokenKind.DataDirective:
  886. case AsmTokenKind.Directive:
  887. case AsmTokenKind.FunctionBegin:
  888. case AsmTokenKind.FunctionEnd:
  889. _output.Append("<color=").Append(ColorDirective).Append('>');
  890. _output.Append(_input, slice.Position, slice.Length);
  891. column += slice.Length;
  892. _output.Append("</color>");
  893. break;
  894. case AsmTokenKind.Label:
  895. case AsmTokenKind.Identifier:
  896. _output.Append("<color=").Append(ColorIdentifier).Append('>');
  897. _output.Append(_input, slice.Position, slice.Length);
  898. column += slice.Length;
  899. _output.Append("</color>");
  900. break;
  901. case AsmTokenKind.Qualifier:
  902. _output.Append("<color=").Append(ColorQualifier).Append('>');
  903. _output.Append(_input, slice.Position, slice.Length);
  904. column += slice.Length;
  905. _output.Append("</color>");
  906. break;
  907. case AsmTokenKind.Instruction:
  908. case AsmTokenKind.CallInstruction:
  909. case AsmTokenKind.BranchInstruction:
  910. case AsmTokenKind.JumpInstruction:
  911. case AsmTokenKind.ReturnInstruction:
  912. _output.Append("<color=").Append(ColorInstruction).Append('>');
  913. _output.Append(_input, slice.Position, slice.Length);
  914. column += slice.Length;
  915. _output.Append("</color>");
  916. if (i == length - 2) // last slice always a newline
  917. break;
  918. column += AlignInstruction(_output, slice.Length, _inputAsmKind);
  919. break;
  920. case AsmTokenKind.InstructionSIMD:
  921. // Perform smell test for simd instructions:
  922. var col = ColorInstructionSIMD;
  923. if (_smellTest)
  924. {
  925. switch (_tokenProvider.SimdKind(slice))
  926. {
  927. case SIMDkind.Packed:
  928. col = ColorInstructionSIMDPacked;
  929. break;
  930. case SIMDkind.Scalar:
  931. col = ColorInstructionSIMDScalar;
  932. break;
  933. case SIMDkind.Infrastructure:
  934. break;
  935. }
  936. }
  937. _output.Append("<color=").Append(col).Append('>');
  938. _output.Append(_input, slice.Position, slice.Length);
  939. column += slice.Length;
  940. _output.Append("</color>");
  941. if (i == length - 2) // last slice always newline
  942. break;
  943. column += AlignInstruction(_output, slice.Length, _inputAsmKind);
  944. break;
  945. case AsmTokenKind.Register:
  946. _output.Append("<color=").Append(ColorRegister).Append('>');
  947. _output.Append(_input, slice.Position, slice.Length);
  948. column += slice.Length;
  949. _output.Append("</color>");
  950. break;
  951. case AsmTokenKind.Number:
  952. _output.Append("<color=").Append(ColorNumber).Append('>');
  953. _output.Append(_input, slice.Position, slice.Length);
  954. column += slice.Length;
  955. _output.Append("</color>");
  956. break;
  957. case AsmTokenKind.String:
  958. _output.Append("<color=").Append(ColorString).Append('>');
  959. _output.Append(_input, slice.Position, slice.Length);
  960. column += slice.Length;
  961. _output.Append("</color>");
  962. break;
  963. case AsmTokenKind.Comment:
  964. _output.Append("<color=").Append(ColorComment).Append('>');
  965. _output.Append(_input, slice.Position, slice.Length);
  966. column += slice.Length;
  967. _output.Append("</color>");
  968. break;
  969. case AsmTokenKind.NewLine:
  970. _output.Append('\n');
  971. break;
  972. default:
  973. _output.Append(_input, slice.Position, slice.Length);
  974. column += slice.Length;
  975. break;
  976. }
  977. }
  978. else
  979. {
  980. if (token.Kind == AsmTokenKind.NewLine)
  981. {
  982. _output.Append('\n');
  983. }
  984. else
  985. {
  986. _output.Append(_input, slice.Position, slice.Length);
  987. column += slice.Length;
  988. }
  989. // Also wants to align instructions in uncolored mode the same way as colored.
  990. switch (token.Kind)
  991. {
  992. case AsmTokenKind.Instruction:
  993. case AsmTokenKind.CallInstruction:
  994. case AsmTokenKind.BranchInstruction:
  995. case AsmTokenKind.JumpInstruction:
  996. case AsmTokenKind.ReturnInstruction:
  997. case AsmTokenKind.InstructionSIMD:
  998. // Do not add alignment to instruction with no arguments
  999. // last slice always a newline
  1000. if (i == length - 2) break;
  1001. column += AlignInstruction(_output, slice.Length, _inputAsmKind);
  1002. break;
  1003. }
  1004. }
  1005. }
  1006. }
  1007. private void RenderSourceFileLocation(ref AsmLine line, bool colored)
  1008. {
  1009. char[] comment = {_commentStart, ' '};
  1010. var fileno = line.SourceFileNumber;
  1011. var lineno = line.SourceLineNumber;
  1012. var colno = line.SourceColumnNumber;
  1013. // If the file number is 0, skip the line
  1014. if (fileno == 0)
  1015. {
  1016. }
  1017. // If the line number is 0, then we can update the file tracking, but still not output a line
  1018. else if (lineno == 0)
  1019. {
  1020. if (colored) _output.Append("<color=").Append(ColorLineDirective).Append('>');
  1021. _output.Append(comment).Append(System.IO.Path.GetFileName(_fileName[fileno]));
  1022. if (colored) _output.Append("</color>");
  1023. }
  1024. // We have a source line and number -- can we load file and extract this line?
  1025. else
  1026. {
  1027. if (_fileList.ContainsKey(fileno) && _fileList[fileno] != null && lineno - 1 < _fileList[fileno].Length)
  1028. {
  1029. if (colored) _output.Append("<color=").Append(ColorLineDirective).Append('>');
  1030. _output.Append(comment).Append(System.IO.Path.GetFileName(_fileName[fileno])).Append('(').Append(lineno).Append(", ").Append(colno + 1).Append(')').Append(_fileList[fileno][lineno - 1]);
  1031. if (colored) _output.Append("</color>");
  1032. }
  1033. else
  1034. {
  1035. if (colored) _output.Append("<color=").Append(ColorLineDirective).Append('>');
  1036. _output.Append(comment).Append(System.IO.Path.GetFileName(_fileName[fileno])).Append('(').Append(lineno).Append(", ").Append(colno + 1).Append(')');
  1037. if (colored) _output.Append("</color>");
  1038. }
  1039. }
  1040. _output.Append('\n');
  1041. }
  1042. private AsmTokenIterator GetIterator(in AsmLine line)
  1043. {
  1044. return new AsmTokenIterator(_tokens, line.TokenIndex, line.Length);
  1045. }
  1046. public enum AsmKind
  1047. {
  1048. Intel,
  1049. ARM,
  1050. Wasm,
  1051. LLVMIR
  1052. }
  1053. [Flags]
  1054. enum BlockKindDetectFlags
  1055. {
  1056. None = 0,
  1057. Code = 1 << 0,
  1058. Data = 1 << 1,
  1059. Directive = 1 << 2,
  1060. }
  1061. public enum AsmBlockKind
  1062. {
  1063. None,
  1064. Block,
  1065. Directive,
  1066. Code,
  1067. Data
  1068. }
  1069. [DebuggerDisplay("Block {Kind} LineIndex = {LineIndex} Length = {Length}")]
  1070. public class AsmBlock
  1071. {
  1072. public AsmBlockKind Kind;
  1073. public int LineIndex;
  1074. public int Length;
  1075. // Edges attached to this block, might be null if no edges
  1076. public List<AsmEdge> Edges;
  1077. public void AddEdge(in AsmEdge edge)
  1078. {
  1079. var edges = Edges;
  1080. if (edges == null)
  1081. {
  1082. edges = new List<AsmEdge>();
  1083. Edges = edges;
  1084. }
  1085. edges.Add(edge);
  1086. }
  1087. /// <summary>
  1088. /// Sort edges by in-bound first, block index, line index
  1089. /// </summary>
  1090. public void SortEdges()
  1091. {
  1092. var edges = Edges;
  1093. if (edges == null) return;
  1094. edges.Sort(EdgeComparer.Instance);
  1095. }
  1096. private class EdgeComparer : IComparer<AsmEdge>
  1097. {
  1098. public static readonly EdgeComparer Instance = new EdgeComparer();
  1099. public int Compare(AsmEdge x, AsmEdge y)
  1100. {
  1101. // Order by kind first (InBound first, outbound first)
  1102. if (x.Kind != y.Kind)
  1103. {
  1104. return x.Kind == AsmEdgeKind.InBound ? -1 : 1;
  1105. }
  1106. // Order by Block Index
  1107. if (x.LineRef.BlockIndex != y.LineRef.BlockIndex) return x.LineRef.BlockIndex.CompareTo(y.LineRef.BlockIndex);
  1108. // Then order by Line Index
  1109. return x.LineRef.LineIndex.CompareTo(y.LineRef.LineIndex);
  1110. }
  1111. }
  1112. }
  1113. public enum AsmLineKind
  1114. {
  1115. Empty = 0,
  1116. Comment,
  1117. Directive,
  1118. SourceFile,
  1119. SourceLocation,
  1120. SourceFileLocation, // computed line
  1121. FunctionBegin,
  1122. FunctionEnd,
  1123. LabelDeclaration,
  1124. Code,
  1125. CodeCall,
  1126. CodeBranch,
  1127. CodeJump,
  1128. CodeReturn,
  1129. Data,
  1130. }
  1131. /// <summary>
  1132. /// An <see cref="AsmToken"/> iterator skipping spaces.
  1133. /// </summary>
  1134. struct AsmTokenIterator
  1135. {
  1136. private readonly List<AsmToken> _tokens;
  1137. private readonly int _startIndex;
  1138. private readonly int _endIndex;
  1139. private int _index;
  1140. public AsmTokenIterator(List<AsmToken> tokens, int index, int length)
  1141. {
  1142. if (tokens == null) throw new ArgumentNullException(nameof(tokens));
  1143. _tokens = tokens;
  1144. if (index < 0 || index >= tokens.Count) throw new ArgumentOutOfRangeException(nameof(index), $"Invalid index {index}. Must be >= 0 and < {tokens.Count}");
  1145. if (length < 0) throw new ArgumentOutOfRangeException(nameof(length), $"Invalid length {length}. Must be >=0");
  1146. _startIndex = index;
  1147. _endIndex = index + length - 1;
  1148. if (_endIndex >= tokens.Count) throw new ArgumentOutOfRangeException(nameof(length), $"Invalid length {length}. The final index {_endIndex} cannot be >= {tokens.Count}");
  1149. _index = index;
  1150. }
  1151. public void Reset()
  1152. {
  1153. _index = _startIndex;
  1154. }
  1155. public bool TryGetNext(out AsmToken token)
  1156. {
  1157. while (_index <= _endIndex)
  1158. {
  1159. var nextToken = _tokens[_index++];
  1160. if (nextToken.Kind == AsmTokenKind.Misc) continue;
  1161. token = nextToken;
  1162. return true;
  1163. }
  1164. token = default;
  1165. return false;
  1166. }
  1167. public bool TryGetNext(out AsmToken token, out int tokenIndex)
  1168. {
  1169. while (_index <= _endIndex)
  1170. {
  1171. tokenIndex = _index;
  1172. var nextToken = _tokens[_index++];
  1173. if (nextToken.Kind == AsmTokenKind.Misc) continue;
  1174. token = nextToken;
  1175. return true;
  1176. }
  1177. tokenIndex = -1;
  1178. token = default;
  1179. return false;
  1180. }
  1181. }
  1182. [DebuggerDisplay("{ToDebuggerDisplay(),nq}")]
  1183. [StructLayout(LayoutKind.Explicit)]
  1184. public struct AsmLine
  1185. {
  1186. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1187. // CAUTION: It is important to not put *any managed objects*
  1188. // into this struct for GC efficiency
  1189. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1190. [FieldOffset(0)] public AsmLineKind Kind;
  1191. [FieldOffset(4)] public int TokenIndex;
  1192. // only valid when Kind == SourceFileLocation
  1193. [FieldOffset(4)] public int SourceFileNumber;
  1194. [FieldOffset(8)] public int Length;
  1195. // only valid when Kind == SourceFileLocation
  1196. [FieldOffset(8)] public int SourceLineNumber;
  1197. // only valid when Kind == SourceFileLocation
  1198. [FieldOffset(12)] public int SourceColumnNumber;
  1199. /// <summary>
  1200. /// Index into <see cref="_columnIndices"/>, the column indices will then contain <see cref="Length"/> minus 1 of column ints,
  1201. /// each column corresponding the horizontal offset to a token.
  1202. /// The first column is always 0 for the first token, hence the minus 1.
  1203. /// Only get filled when asking for the text for a block.
  1204. /// </summary>
  1205. [FieldOffset(16)] public int ColumnIndex;
  1206. private string ToDebuggerDisplay()
  1207. {
  1208. if (Kind == AsmLineKind.SourceFileLocation)
  1209. {
  1210. return $"Line {Kind} File={SourceFileNumber} Line={SourceLineNumber} Column={SourceColumnNumber}";
  1211. }
  1212. else
  1213. {
  1214. return $"Line {Kind} TokenIndex={TokenIndex} Length={Length} ColumnIndex={ColumnIndex}";
  1215. }
  1216. }
  1217. }
  1218. public enum AsmEdgeKind
  1219. {
  1220. InBound,
  1221. OutBound,
  1222. }
  1223. /// <summary>
  1224. /// An inbound or outbound connection for a block to another block+line
  1225. /// </summary>
  1226. [DebuggerDisplay("Edge {Kind} Origin: {OriginRef} LineRef: {LineRef}")]
  1227. public struct AsmEdge : IEquatable<AsmEdge>
  1228. {
  1229. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1230. // CAUTION: It is important to not put *any managed objects*
  1231. // into this struct for GC efficiency
  1232. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1233. public AsmEdge(AsmEdgeKind kind, AsmLineRef originRef, AsmLineRef lineRef)
  1234. {
  1235. Kind = kind;
  1236. OriginRef = originRef;
  1237. LineRef = lineRef;
  1238. }
  1239. public AsmEdgeKind Kind;
  1240. public AsmLineRef OriginRef;
  1241. public AsmLineRef LineRef;
  1242. public override string ToString()
  1243. {
  1244. return Kind == AsmEdgeKind.InBound ?
  1245. $"Edge {Kind} {LineRef} => {OriginRef}"
  1246. : $"Edge {Kind} {OriginRef} => {LineRef}";
  1247. }
  1248. public bool Equals(AsmEdge obj) => Kind == obj.Kind && OriginRef.Equals(obj.OriginRef) && LineRef.Equals(obj.LineRef);
  1249. public override bool Equals(object obj) => obj is AsmEdge other && Equals(other);
  1250. public override int GetHashCode() => base.GetHashCode();
  1251. }
  1252. public readonly struct AsmLineRef: IEquatable<AsmLineRef>
  1253. {
  1254. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1255. // CAUTION: It is important to not put *any managed objects*
  1256. // into this struct for GC efficiency
  1257. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1258. public AsmLineRef(int blockIndex, int lineIndex)
  1259. {
  1260. BlockIndex = blockIndex;
  1261. LineIndex = lineIndex;
  1262. }
  1263. public readonly int BlockIndex;
  1264. public readonly int LineIndex;
  1265. public override string ToString()
  1266. {
  1267. return $"Block: {BlockIndex}, Line: {LineIndex}";
  1268. }
  1269. public bool Equals(AsmLineRef obj) => BlockIndex == obj.BlockIndex && LineIndex == obj.LineIndex;
  1270. public override bool Equals(object obj) => obj is AsmLineRef other && Equals(other);
  1271. public override int GetHashCode() => base.GetHashCode();
  1272. }
  1273. /// <summary>
  1274. /// Structure used to store all label references before they are getting fully resolved
  1275. /// </summary>
  1276. [DebuggerDisplay("TempLabelRef {LineRef} - String {StringIndex}, {StringLength}")]
  1277. private readonly struct TempLabelRef
  1278. {
  1279. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1280. // CAUTION: It is important to not put *any managed objects*
  1281. // into this struct for GC efficiency
  1282. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1283. public TempLabelRef(int globalBlockIndex, AsmLineRef lineRef, int stringIndex, int stringLength)
  1284. {
  1285. GlobalBlockIndex = globalBlockIndex;
  1286. LineRef = lineRef;
  1287. StringIndex = stringIndex;
  1288. StringLength = stringLength;
  1289. }
  1290. public readonly int GlobalBlockIndex;
  1291. public readonly AsmLineRef LineRef;
  1292. public readonly int StringIndex;
  1293. public readonly int StringLength;
  1294. }
  1295. private class DictionaryLocalLabel : Dictionary<StringSlice, AsmLineRef>
  1296. {
  1297. public DictionaryLocalLabel()
  1298. {
  1299. }
  1300. public DictionaryLocalLabel(int capacity) : base(capacity)
  1301. {
  1302. }
  1303. public AsmLineRef GlobalLabelLineRef;
  1304. }
  1305. private class DictionaryGlobalLabel : Dictionary<StringSlice, DictionaryLocalLabel>
  1306. {
  1307. public DictionaryGlobalLabel()
  1308. {
  1309. }
  1310. public DictionaryGlobalLabel(int capacity) : base(capacity)
  1311. {
  1312. }
  1313. public DictionaryLocalLabel GetOrCreate(StringSlice label, AsmLineRef globalLineRef)
  1314. {
  1315. if (!TryGetValue(label, out var dictLabel))
  1316. {
  1317. dictLabel = new DictionaryLocalLabel();
  1318. Add(label, dictLabel);
  1319. }
  1320. dictLabel.GlobalLabelLineRef = globalLineRef;
  1321. return dictLabel;
  1322. }
  1323. }
  1324. internal struct UsedRegisters
  1325. {
  1326. private AsmTokenKindProvider _tokenProvider;
  1327. /// <summary>
  1328. /// Dictionary<lineNr, List<reg>>
  1329. /// </summary>
  1330. internal readonly Dictionary<int, List<string>> _linesRegisters;
  1331. private readonly List<string> _tmp;
  1332. private int _currentLineIdx;
  1333. public UsedRegisters(int count)
  1334. {
  1335. _linesRegisters = new Dictionary<int, List<string>>(count);
  1336. _tmp = new List<string>(2);
  1337. _currentLineIdx = -1;
  1338. _tokenProvider = null;
  1339. }
  1340. public void AddTokenProvider(AsmTokenKindProvider provider)
  1341. {
  1342. _tokenProvider = provider;
  1343. }
  1344. private int NumberOfOcurences(List<string> regs, string target)
  1345. {
  1346. var count = 0;
  1347. foreach (var elm in regs)
  1348. {
  1349. if (_tokenProvider.RegisterEqual(elm, target))
  1350. {
  1351. count++;
  1352. }
  1353. }
  1354. return count;
  1355. }
  1356. public int RegisterMatch(int lineIdx, string reg)
  1357. {
  1358. return LineContainsRegs(lineIdx, out var actualRegs)
  1359. ? NumberOfOcurences(actualRegs, reg)
  1360. : 0;
  1361. }
  1362. public bool RegisterEquality(string regA, string regB) => _tokenProvider.RegisterEqual(regA, regB);
  1363. public List<string> CleanRegs(List<string> regs)
  1364. {
  1365. var tmpTokenProvider = _tokenProvider;
  1366. var retVal = new List<string>(regs.Count);
  1367. foreach (var reg in regs)
  1368. {
  1369. if (!retVal.Exists(elm => tmpTokenProvider.RegisterEqual(reg, elm)))
  1370. {
  1371. retVal.Add(reg);
  1372. }
  1373. }
  1374. return retVal;
  1375. }
  1376. public bool LineContainsRegs(int lineIdx, out List<string> value)
  1377. {
  1378. return _linesRegisters.TryGetValue(lineIdx, out value);
  1379. }
  1380. public void Add(int lineIdx, string reg)
  1381. {
  1382. _currentLineIdx = lineIdx;
  1383. _tmp.Add(reg);
  1384. }
  1385. public void PushLine()
  1386. {
  1387. if (_currentLineIdx == -1)
  1388. {
  1389. // We haven't actually tried to add anything.
  1390. return;
  1391. }
  1392. _linesRegisters[_currentLineIdx] = new List<string>(_tmp);
  1393. _tmp.Clear();
  1394. _currentLineIdx = -1;
  1395. }
  1396. public int Count => _linesRegisters.Count;
  1397. public void Clear()
  1398. {
  1399. _linesRegisters.Clear();
  1400. _tmp.Clear();
  1401. }
  1402. }
  1403. }
  1404. }
  1405. #endif