Skip to content

Commit

Permalink
Synchronize changes with WinForms version + more changes (#1228)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnErrupTion committed May 8, 2024
1 parent 279a4c1 commit 6785f14
Show file tree
Hide file tree
Showing 15 changed files with 460 additions and 386 deletions.
6 changes: 0 additions & 6 deletions Source/Mosa.Tool.Explorer.Avalonia/CompilerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public void UpdateLog(string section, List<string> lines, bool dirty)
}

lock (log)
{
log.AddRange(lines);
}

DirtyLog = dirty;
}
Expand All @@ -59,9 +57,7 @@ private void UpdateLog(string section, string line)
}

lock (log)
{
log.Add(line);
}

DirtyLog = true;
}
Expand All @@ -70,9 +66,7 @@ private void UpdateLog(string section, string line)
public List<string> GetLog(string section)
{
lock (Logs)
{
return Logs.GetValueOrDefault(section);
}
}

public void AddTraceEvent(CompilerEvent compilerEvent, string message, int threadID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ protected override void Finalization()
var log = new TraceLog(TraceType.GlobalDebug, null, null, "Compiler Time");

log.Log("Ticks\tMilliseconds\tCompiler Count\tMethod");

foreach (var data in methods)
log.Log($"{data.ElapsedTicks}{'\t'}{data.ElapsedTicks / TimeSpan.TicksPerMillisecond}{'\t'}{data.Version}{'\t'}{data.Method.FullName}");

Expand Down
193 changes: 193 additions & 0 deletions Source/Mosa.Tool.Explorer.Avalonia/FormatInstructions.cs
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);
}
95 changes: 95 additions & 0 deletions Source/Mosa.Tool.Explorer.Avalonia/InstructionRecord.cs
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]);
}
}
}

0 comments on commit 6785f14

Please sign in to comment.