Skip to content

Commit

Permalink
Show instructions when clicking on addresses in the JumpTableDialog (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
uxmal committed Sep 19, 2016
1 parent ae50baa commit 1275a7e
Show file tree
Hide file tree
Showing 18 changed files with 126 additions and 24 deletions.
9 changes: 6 additions & 3 deletions src/Core/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,17 @@ public override int GetHashCode()
return (long) a.ToLinear() - (long) b.ToLinear();
}

public int CompareTo(Address a)
public int CompareTo(Address that)
{
return this.ToLinear().CompareTo(a.ToLinear());
return this.ToLinear().CompareTo(that.ToLinear());
}

public int CompareTo(object a)
{
return this.ToLinear().CompareTo(((Address)a).ToLinear());
var that = a as Address;
if (that == null)
return 1;
return this.ToLinear().CompareTo(that.ToLinear());
}

public abstract Constant ToConstant();
Expand Down
1 change: 1 addition & 0 deletions src/Gui/Controls/IControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ public interface IControl
{
Color ForeColor { get; set; }
Color BackColor { get; set; }
bool Enabled { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Gui/Controls/ILabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

namespace Reko.Gui.Controls
{
public interface ILabel
public interface ILabel : IControl
{
string Text { get; set; }
}
Expand Down
3 changes: 2 additions & 1 deletion src/Gui/Controls/IListBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public interface IListBox

object DataSource { get; set; }
IList Items { get; }
object SelectedItem { get; }
int SelectedIndex { get; set; }
object SelectedItem { get; set; }

void AddItems(IEnumerable items);
}
Expand Down
1 change: 0 additions & 1 deletion src/Gui/Controls/ITextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public interface ITextBox : IControl
event KeyEventHandler KeyDown;
event EventHandler LostFocus;

bool Enabled { get; set; }
string Text { get; set; }

void SelectAll();
Expand Down
1 change: 1 addition & 0 deletions src/Gui/Gui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@
<Compile Include="Windows\Controls\RegexValidator.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Windows\ControlWrapper.cs" />
<Compile Include="Windows\Forms\CallSiteDialog.cs">
<SubType>Form</SubType>
</Compile>
Expand Down
44 changes: 44 additions & 0 deletions src/Gui/Windows/ControlWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#region License
/*
* Copyright (C) 1999-2016 John Källén.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#endregion

using Reko.Gui.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Reko.Gui.Windows
{
public class ControlWrapper : IControl
{
private Control ctrl;

public ControlWrapper(Control ctrl)
{
this.ctrl = ctrl;
}

public Color BackColor { get { return ctrl.BackColor; } set { ctrl.BackColor = value; } }
public Color ForeColor { get { return ctrl.ForeColor; } set { ctrl.ForeColor = value; } }
public bool Enabled { get { return ctrl.Enabled; } set { ctrl.Enabled = value; } }
}
}
3 changes: 2 additions & 1 deletion src/Gui/Windows/Controls/TreeViewWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace Reko.Gui.Windows.Controls
/// <summary>
/// Wraps a Windows forms TreeView in the platform independent ITreeView interface.
/// </summary>
public class TreeViewWrapper : ITreeView
public class TreeViewWrapper : ControlWrapper, ITreeView
{
public event EventHandler AfterSelect;
public event DragEventHandler DragEnter;
Expand All @@ -45,6 +45,7 @@ public class TreeViewWrapper : ITreeView
private TreeView treeView;

public TreeViewWrapper(TreeView treeView)
: base(treeView)
{
this.treeView = treeView;
this.Nodes = new WrappedNodeList(treeView.Nodes);
Expand Down
22 changes: 22 additions & 0 deletions src/Gui/Windows/Forms/JumpTableInteractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

using Reko.Core;
using Reko.Core.Lib;
using Reko.Core.Machine;
using Reko.Scanning;
using System;
using System.Collections.Generic;
Expand All @@ -41,6 +42,7 @@ public void Attach(JumpTableDialog dlg)
dlg.Load += Dlg_Load;
dlg.FormClosing += Dlg_FormClosing;
dlg.EntryCount.ValueChanged += EntryCount_ValueChanged;
dlg.Entries.SelectedIndexChanged += Entries_SelectedIndexChanged;
}

private void Dlg_Load(object sender, EventArgs e)
Expand Down Expand Up @@ -85,6 +87,25 @@ private void EntryCount_ValueChanged(object sender, EventArgs e)
dlg.Entries.DataSource = addresses;
}

private void Entries_SelectedIndexChanged(object sender, EventArgs e)
{
var addr = (Address)dlg.Entries.SelectedItem;
string text;
if (addr != null)
{
var dasm = dlg.Program.CreateDisassembler(addr);
text = string.Join(
Environment.NewLine,
dasm.TakeWhile(i => (i.InstructionClass & InstructionClass.Transfer) == 0)
.Select(i => i.ToString()));
}
else
{
text = "";
}
dlg.Disassembly.Text = text;
}

private void EnableSegmentedPanel(bool hasValue)
{
//foreach (Control control in dlg.SegmentedAddressPanel.Controls)
Expand All @@ -102,6 +123,7 @@ private void EnableSegmentedPanel(bool hasValue)
private void EnableControls()
{
dlg.IndirectTable.Enabled = dlg.IsIndirectTable.Checked;
dlg.IndirectLabel.Enabled = dlg.IsIndirectTable.Checked;
}

private void IsIndirectTable_CheckedChanged(object sender, EventArgs e)
Expand Down
3 changes: 2 additions & 1 deletion src/Gui/Windows/LabelWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@

namespace Reko.Gui.Windows
{
public class LabelWrapper : ILabel
public class LabelWrapper : ControlWrapper, ILabel
{
private Label lbl;

public LabelWrapper(Label lbl)
: base(lbl)
{
this.lbl = lbl;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Gui/Windows/ListboxWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public event EventHandler SelectedIndexChanged

public object DataSource { get { return lbx.DataSource; } set { lbx.DataSource = value; } }
public IList Items { get { return lbx.Items; } }
public object SelectedItem { get { return lbx.SelectedItem; } }
public int SelectedIndex { get { return lbx.SelectedIndex; } set { lbx.SelectedIndex = value; } }
public object SelectedItem { get { return lbx.SelectedItem; } set { lbx.SelectedItem = value; } }

public void AddItems(IEnumerable items)
{
Expand Down
6 changes: 2 additions & 4 deletions src/Gui/Windows/NumericUpDownWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@

namespace Reko.Gui.Windows
{
public class NumericUpDownWrapper : INumericUpDown
public class NumericUpDownWrapper : ControlWrapper, INumericUpDown
{
private NumericUpDown num;

public NumericUpDownWrapper(NumericUpDown num)
: base(num)
{
this.num = num;
}
Expand All @@ -43,9 +44,6 @@ public event EventHandler ValueChanged
remove { num.ValueChanged -= value; }
}

public Color BackColor { get { return num.BackColor; } set { num.BackColor = value; } }
public Color ForeColor { get { return num.ForeColor; } set { num.ForeColor = value; } }
public decimal Value { get { return num.Value; } set { num.Value = value; } }

}
}
6 changes: 2 additions & 4 deletions src/Gui/Windows/RadioButtonWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@

namespace Reko.Gui.Windows
{
public class RadioButtonWrapper : IRadioButton
public class RadioButtonWrapper : ControlWrapper, IRadioButton
{
private RadioButton rdb;

public RadioButtonWrapper(RadioButton rdb)
: base(rdb)
{
this.rdb = rdb;
}

public Color BackColor { get { return rdb.BackColor; } set { rdb.BackColor = value; } }
public Color ForeColor { get { return rdb.ForeColor; } set { rdb.ForeColor = value; } }
}
}
6 changes: 2 additions & 4 deletions src/Gui/Windows/TextBoxWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,17 @@ namespace Reko.Gui.Windows
/// <summary>
/// Wraps the Windows Forms textbox with the ITextBox interface.
/// </summary>
public class TextBoxWrapper : ITextBox
public class TextBoxWrapper : ControlWrapper, ITextBox
{
private TextBox text;

public TextBoxWrapper(TextBox text)
: base(text)
{
this.text = text;
}

public bool Enabled { get { return text.Enabled; } set { text.Enabled = value; } }
public string Text { get { return text.Text; } set { text.Text = value; } }
public Color BackColor { get { return text.BackColor; } set { text.BackColor = value; } }
public Color ForeColor { get { return text.ForeColor; } set { text.ForeColor = value; } }

public void SelectAll() {
text.SelectAll();
Expand Down
30 changes: 29 additions & 1 deletion src/UnitTests/Gui/Windows/Forms/JumpTableInteractorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

using NUnit.Framework;
using Reko.Core;
using Reko.Core.Machine;
using Reko.Gui.Windows.Forms;
using Reko.Scanning;
using Reko.UnitTests.Mocks;
Expand All @@ -34,7 +35,10 @@ namespace Reko.UnitTests.Gui.Windows.Forms
[Category(Categories.UserInterface)]
public class JumpTableInteractorTests
{
private readonly string nl = Environment.NewLine;

private JumpTableDialog dlg;
private FakeArchitecture arch;
private Program program;

[SetUp]
Expand All @@ -60,7 +64,7 @@ private void Given_Dialog_32Bit()

private void Given_Program()
{
var arch = new FakeArchitecture();
this.arch = new FakeArchitecture();
var platform = new FakePlatform(null, arch);

this.program = new Program
Expand All @@ -87,6 +91,15 @@ private void Given_Table_UInt32(Address address, params uint[] entries)
}
}

private void Given_Disassembly(Address addr)
{
arch.Test_DisassemblyStream = new List<MachineInstruction>
{
new FakeInstruction(Operation.Add),
new FakeInstruction(Operation.Branch),
};
}

[Test]
public void Jti_GetAddresses_Linear()
{
Expand Down Expand Up @@ -143,5 +156,20 @@ public void Jti_Item_Count_Changed()

Assert.AreEqual(2, dlg.Entries.Items.Count);
}

[Test]
public void Jti_Entry_Selected()
{
Given_Program();
Given_Dialog_32Bit();
Given_Table_UInt32(Address.Ptr32(0x1000), 0x1010, 0x01023, 0x01018);
Given_Disassembly(Address.Ptr32(0x1010));
dlg.Show();
dlg.JumpTableStartAddress.Text = "001000";
dlg.EntryCount.Value = 2;
dlg.Entries.SelectedIndex = 1;

Assert.AreEqual("add" + nl + "branch", dlg.Disassembly.Text);
}
}
}
1 change: 1 addition & 0 deletions src/UnitTests/Gui/Windows/ProjectBrowserServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public FakeTreeView()
private ITreeNode selectedItem;

public bool Focused { get; set; }
public bool Enabled { get; set; }
public ContextMenu ContextMenu { get; set; }
public bool ShowRootLines { get; set; }
public bool ShowNodeToolTips { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions src/UnitTests/Mocks/FakeArchitecture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static FakeArchitecture()
public string Name { get; set; }
public string Description { get; set; }

public IEnumerable<MachineInstruction> DisassemblyStream { get; set; }
public IEnumerable<MachineInstruction> Test_DisassemblyStream { get; set; }

public void Test_AddTrace(RtlTrace trace)
{
Expand Down Expand Up @@ -215,7 +215,7 @@ public string RegisterToString(int reg)

public IEnumerable<MachineInstruction> CreateDisassembler(ImageReader rdr)
{
return new FakeDisassembler(rdr.Address, DisassemblyStream.GetEnumerator());
return new FakeDisassembler(rdr.Address, Test_DisassemblyStream.GetEnumerator());
}

public IEqualityComparer<MachineInstruction> CreateInstructionComparer(Normalize norm)
Expand Down
5 changes: 5 additions & 0 deletions src/UnitTests/Mocks/FakeInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public override MachineOperand GetOperand(int i)
{
throw new NotImplementedException();
}

public override void Render(MachineInstructionWriter writer)
{
writer.WriteOpcode(operation.ToString().ToLower());
}
}

public enum Operation
Expand Down

0 comments on commit 1275a7e

Please sign in to comment.