Skip to content

Commit

Permalink
Refactor: move MixedCodeDataModel into Reko.Gui
Browse files Browse the repository at this point in the history
  • Loading branch information
uxmal committed Aug 7, 2023
1 parent db8e00b commit d908e34
Show file tree
Hide file tree
Showing 5 changed files with 646 additions and 559 deletions.
4 changes: 2 additions & 2 deletions src/Gui/TextViewing/AbstractDisassemblyTextModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public LineSpan[] GetLineSpans(int count)
if (arch is not null)
{
var addr = Align(arch, addrStart + offset, arch.CodeMemoryGranularity);
if (program.SegmentMap.TryFindSegment(addr, out ImageSegment seg) &&
if (program.SegmentMap.TryFindSegment(addr, out ImageSegment? seg) &&
seg.MemoryArea != null &&
seg.MemoryArea.IsValidAddress(addr))
{
Expand Down Expand Up @@ -124,7 +124,7 @@ private IProcessorArchitecture GetArchitectureForAddress(Address addr)
{
if (this.arch is not null)
return this.arch;
IProcessorArchitecture arch = null;
IProcessorArchitecture? arch = null;
// Try to find a basic block at this address and use its architecture.
if (program.ImageMap.TryFindItem(addr, out var item) &&
item is ImageMapBlock imb &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
using System.Drawing;
using System.Text;

namespace Reko.UserInterfaces.WindowsForms.Controls
namespace Reko.Gui.TextViewing
{
public partial class MixedCodeDataModel
public abstract partial class AbstractMixedCodeDataModel
{
private bool TryReadComment(out LineSpan line)
{
Expand All @@ -44,7 +44,7 @@ private bool TryReadComment(out LineSpan line)
line = new LineSpan(
curPos,
curPos.Address,
new MemoryTextSpan(
CreateMemoryTextSpan(
$"; {commentLines[curPos.Offset]}",
UiStyles.CodeComment));
curPos = Pos(curPos.Address, curPos.Offset + 1);
Expand Down Expand Up @@ -130,9 +130,10 @@ private SpanGenerator CreateSpanifier(
ModelPosition pos)
{
SpanGenerator sp;
if (item is ImageMapBlock b && b.Block.Procedure != null)
if (item is ImageMapBlock b && b.Block?.Procedure != null)
{
sp = new AsmSpanifyer(
this,
program,
b.Block.Procedure.Architecture,
instructions[b],
Expand All @@ -141,7 +142,7 @@ private SpanGenerator CreateSpanifier(
}
else
{
sp = new MemSpanifyer(program, mem, item, pos);
sp = new MemSpanifyer(this,program, mem, item, pos);
}
return sp;
}
Expand All @@ -165,6 +166,7 @@ public void DecorateLastLine(LineSpan line)

private class AsmSpanifyer : SpanGenerator
{
private readonly AbstractMixedCodeDataModel model;
private readonly Program program;
private readonly IProcessorArchitecture arch;
private readonly MachineInstruction[] instrs;
Expand All @@ -173,12 +175,14 @@ private class AsmSpanifyer : SpanGenerator
private readonly Address addrSelected;

public AsmSpanifyer(
AbstractMixedCodeDataModel model,
Program program,
IProcessorArchitecture arch,
MachineInstruction[] instrs,
ModelPosition pos,
Address addrSelected)
{
this.model = model;
this.instrs = instrs;
this.arch = arch;
var addr = pos.Address;
Expand All @@ -196,7 +200,8 @@ public override (ModelPosition, LineSpan)? GenerateSpan()
++offset;
var options = new MachineInstructionRendererOptions(
flags: MachineInstructionRendererFlags.ResolvePcRelativeAddress);
var asmLine = DisassemblyTextModel.RenderAsmLine(

var asmLine = model.RenderAssemblerLine(
position,
program,
arch,
Expand All @@ -213,26 +218,31 @@ public override (ModelPosition, LineSpan)? GenerateSpan()
}
}

protected abstract LineSpan RenderAssemblerLine(object position, Program program, IProcessorArchitecture arch, MachineInstruction instr, MachineInstructionRendererOptions options);

private class MemSpanifyer : SpanGenerator, IMemoryFormatterOutput
{
private readonly AbstractMixedCodeDataModel model;
private readonly Program program;
private readonly MemoryArea mem;
private readonly ImageMapItem item;
private readonly List<TextSpan> line;
private readonly List<ITextSpan> line;
private readonly StringBuilder sbText;
private ModelPosition position;

public MemSpanifyer(
AbstractMixedCodeDataModel model,
Program program,
MemoryArea mem,
ImageMapItem item,
ModelPosition pos)
{
this.model = model;
this.program = program;
this.mem = mem;
this.item = item;
this.position = pos;
this.line = new List<TextSpan>();
this.line = new List<ITextSpan>();
this.sbText = new StringBuilder();
}

Expand Down Expand Up @@ -261,19 +271,19 @@ public void BeginLine()

public void RenderAddress(Address addr)
{
line.Add(new AddressSpan(addr.ToString(), addr, UiStyles.MemoryWindow));
line.Add(model.CreateAddressSpan(addr.ToString(), addr, UiStyles.MemoryWindow));
}

public void RenderUnit(Address addr, string sUnit)
{
line.Add(new MemoryTextSpan(" ", UiStyles.MemoryWindow));
line.Add(new MemoryTextSpan(addr, sUnit, UiStyles.MemoryWindow));
line.Add(model.CreateMemoryTextSpan(" ", UiStyles.MemoryWindow));
line.Add(model.CreateMemoryTextSpan(addr, sUnit, UiStyles.MemoryWindow));
}

public void RenderFillerSpan(int nChunks, int nCellsPerChunk)
{
var nCells = (1 + nCellsPerChunk) * nChunks;
line.Add(new MemoryTextSpan(new string(' ', nCells), UiStyles.MemoryWindow));
line.Add(model.CreateMemoryTextSpan(new string(' ', nCells), UiStyles.MemoryWindow));
}

public void RenderUnitAsText(Address addr, string sUnit)
Expand All @@ -288,7 +298,7 @@ public void RenderTextFillerSpan(int padding)

public void EndLine(Constant[] bytes)
{
line.Add(new MemoryTextSpan(sbText.ToString(), UiStyles.MemoryWindow));
line.Add(model.CreateMemoryTextSpan(sbText.ToString(), UiStyles.MemoryWindow));
}
}

Expand All @@ -301,63 +311,5 @@ public static int FindIndexOfInstructionAddress(MachineInstruction[] instrs, Add
i => i.Contains(addr));
}

/// <summary>
/// An segment of memory
/// </summary>
public class MemoryTextSpan : TextSpan
{
private readonly string text;

public Address Address { get; private set; }

public MemoryTextSpan(string text, string style)
{
this.text = text;
base.Style = style;
}

public MemoryTextSpan(Address address, string text, string style) : this(text, style)
{
this.Tag = this;
this.Address = address;
}

public override string GetText()
{
return text;
}

public override SizeF GetSize(string text, Font font, Graphics g)
{
SizeF sz = base.GetSize(text, font, g);
return sz;
}
}

/// <summary>
/// An inert text span is not clickable nor has a context menu.
/// </summary>
public class InertTextSpan : TextSpan
{
private readonly string text;

public InertTextSpan(string text, string style)
{
this.text = text;
base.Style = style;
}

public override string GetText()
{
return text;
}

public override SizeF GetSize(string text, Font font, Graphics g)
{
SizeF sz = base.GetSize(text, font, g);
return sz;
}
}

}
}
Loading

0 comments on commit d908e34

Please sign in to comment.