-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Synchronize changes with WinForms version + more changes (#1228)
1 parent
279a4c1
commit 6785f14
Showing
15 changed files
with
460 additions
and
386 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
Source/Mosa.Tool.Explorer.Avalonia/FormatInstructions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
// Copyright (c) MOSA Project. Licensed under the New BSD License. | ||
|
||
using System.Text; | ||
|
||
namespace Mosa.Tool.Explorer.Avalonia; | ||
|
||
public static class FormatInstructions | ||
{ | ||
private const int Padding = 34; | ||
|
||
public static string Format(List<InstructionRecord> records, string blockLabel, bool strip, bool removeNop, bool lineBetweenBlocks) | ||
{ | ||
var sb = new StringBuilder(); | ||
var blocks = new StringBuilder(); | ||
|
||
if (records == null || records.Count == 0) | ||
return string.Empty; | ||
|
||
var allLines = string.IsNullOrWhiteSpace(blockLabel); | ||
var inBlock = allLines; | ||
|
||
foreach (var record in records) | ||
{ | ||
switch (record.Type) | ||
{ | ||
case "M": | ||
{ | ||
sb.AppendLine($"{record.MethodName} [v{record.Version}] @ {record.Stage}:"); | ||
sb.AppendLine(); | ||
break; | ||
} | ||
case "S": | ||
{ | ||
inBlock = record.BlockLabel == blockLabel; | ||
|
||
if (!inBlock && !allLines) | ||
continue; | ||
|
||
sb.Append($"{record.BlockLabel}:"); | ||
|
||
blocks.Clear(); | ||
|
||
// Previous Branch Targets | ||
if (record.PreviousBlockCount != 0) | ||
{ | ||
for (var i = 0; i < record.PreviousBlockCount; i++) | ||
{ | ||
if (i != 0) | ||
blocks.Append(' '); | ||
|
||
var op = record.GetPreviousBlocks(i); | ||
|
||
blocks.Append(op); | ||
} | ||
|
||
sb.Append("".PadRight(record.PreviousBlockCount == 1 ? Padding : Padding - 8)); | ||
|
||
sb.Append(blocks); | ||
} | ||
|
||
sb.AppendLine(); | ||
break; | ||
} | ||
case "I": | ||
{ | ||
if (!inBlock && !allLines) | ||
continue; | ||
|
||
if (record.Instruction is "IR.BlockStart" or "IR.BlockEnd") | ||
break; | ||
|
||
if (removeNop && record.Instruction == "IR.Nop") | ||
continue; | ||
|
||
sb.Append(" "); | ||
|
||
var instruction = record.Instruction; | ||
var condition = !string.IsNullOrEmpty(record.Condition) ? " [" + record.Condition + "]" : string.Empty; | ||
|
||
var both = $"{instruction}{condition}"; | ||
|
||
blocks.Clear(); | ||
|
||
// Branch Targets | ||
if (record.BranchTargetCount != 0) | ||
{ | ||
if (condition.Length != 0) | ||
blocks.Append(' '); | ||
|
||
for (var i = 0; i < record.BranchTargetCount; i++) | ||
{ | ||
if (i != 0) | ||
blocks.Append(' '); | ||
|
||
var op = record.GetBranchTarget(i); | ||
|
||
blocks.Append(op); | ||
} | ||
} | ||
|
||
var count = Padding + 2 - both.Length - blocks.Length; | ||
if (count < 0) | ||
count = 1; | ||
|
||
var padding = string.Empty.PadRight(count); | ||
|
||
sb.Append($"{record.Label[..5]}:{record.Mark.PadLeft(1)}{both}{padding}{blocks} "); | ||
|
||
// Result | ||
for (var i = 0; i < record.ResultCount; i++) | ||
{ | ||
if (i != 0) | ||
sb.Append(", "); | ||
|
||
var op = record.GetResult(i); | ||
|
||
op = Simplify(op); | ||
|
||
if (strip) | ||
op = StripBracketData(op); | ||
|
||
sb.Append(op); | ||
} | ||
|
||
if (record.ResultCount != 0 && record.OperandCount != 0) | ||
sb.Append(" <= "); | ||
|
||
// Operands | ||
for (var i = 0; i < record.OperandCount; i++) | ||
{ | ||
if (i != 0) | ||
sb.Append(", "); | ||
|
||
var op = record.GetOperand(i); | ||
|
||
op = Simplify(op); | ||
|
||
if (strip) | ||
op = StripBracketData(op); | ||
|
||
sb.Append(op); | ||
} | ||
|
||
// Phi Blocks | ||
if (record.PhiBlockCount != 0) | ||
{ | ||
sb.Append(" ("); | ||
|
||
for (var i = 0; i < record.PhiBlockCount; i++) | ||
{ | ||
if (i != 0) | ||
sb.Append(", "); | ||
|
||
var op = record.GetPhilBlock(i); | ||
|
||
sb.Append(op); | ||
} | ||
|
||
sb.Append(") "); | ||
} | ||
|
||
while (sb.Length > 0 && sb[^1] == ' ') | ||
sb.Length--; | ||
|
||
sb.AppendLine(); | ||
break; | ||
} | ||
case "E": | ||
{ | ||
inBlock = false; | ||
|
||
if (!inBlock && !allLines) | ||
continue; | ||
|
||
if (lineBetweenBlocks) | ||
sb.AppendLine(); | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
|
||
private static string StripBracketData(string s) | ||
{ | ||
var i = s.IndexOf('['); | ||
return i <= 0 ? s : s[..(i - 1)].Trim(); | ||
} | ||
|
||
private static string Simplify(string s) => s.Replace("const=", string.Empty); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright (c) MOSA Project. Licensed under the New BSD License. | ||
|
||
namespace Mosa.Tool.Explorer.Avalonia; | ||
|
||
public class InstructionRecord | ||
{ | ||
public readonly string Data; | ||
|
||
public readonly string[] Parts; | ||
|
||
public readonly string Type; | ||
|
||
public readonly bool IsInstruction; | ||
|
||
public readonly bool IsMethod; | ||
|
||
public readonly bool IsStartBlock; | ||
|
||
public readonly bool IsEndBlock; | ||
|
||
public string Label => Parts[1]; | ||
|
||
// Block | ||
public string BlockLabel => Parts[1]; | ||
|
||
// Start Block | ||
public string BlockSequence => Parts[2]; | ||
|
||
public string BlockType => Parts[3]; | ||
|
||
public readonly int PreviousBlockCount; | ||
|
||
public string GetPreviousBlocks(int index) => Parts[5 + index]; | ||
|
||
// End Block | ||
public int NextBlockCount => Convert.ToInt32(Parts[2]); | ||
|
||
// Instruction | ||
public string Mark => Parts[2]; | ||
|
||
public string Instruction => Parts[3]; | ||
|
||
public string Condition => Parts[4]; | ||
|
||
public readonly int ResultCount = 0; | ||
|
||
public readonly int OperandCount = 0; | ||
|
||
public readonly int BranchTargetCount = 0; | ||
|
||
public readonly int PhiBlockCount = 0; | ||
|
||
public string GetResult(int index) => Parts[9 + index]; | ||
|
||
public string GetOperand(int index) => Parts[9 + ResultCount + index]; | ||
|
||
public string GetBranchTarget(int index) => Parts[9 + ResultCount + OperandCount + index]; | ||
|
||
public string GetPhilBlock(int index) => Parts[9 + ResultCount + OperandCount + BranchTargetCount + index]; | ||
|
||
// Method | ||
|
||
public string MethodName => Parts[1]; | ||
|
||
public int Version => Convert.ToInt32(Parts[2]); | ||
|
||
public string Stage => Parts[3]; | ||
|
||
public int Step => Convert.ToInt32(Parts[4]); | ||
|
||
public InstructionRecord(string data) | ||
{ | ||
Data = data; | ||
Parts = data.Split('\t'); | ||
|
||
Type = Parts[0]; | ||
|
||
IsInstruction = Type == "I"; | ||
IsMethod = Type == "M"; | ||
IsStartBlock = Type == "S"; | ||
IsEndBlock = Type == "E"; | ||
|
||
if (IsInstruction) | ||
{ | ||
ResultCount = Convert.ToInt32(Parts[5]); | ||
OperandCount = Convert.ToInt32(Parts[6]); | ||
BranchTargetCount = Convert.ToInt32(Parts[7]); | ||
PhiBlockCount = Convert.ToInt32(Parts[8]); | ||
} | ||
else if (IsStartBlock) | ||
{ | ||
PreviousBlockCount = Convert.ToInt32(Parts[4]); | ||
} | ||
} | ||
} |
Oops, something went wrong.