Skip to content

Commit

Permalink
Feature: partial implementation of #3
Browse files Browse the repository at this point in the history
On some architectures, we can fit a whole address in an instruction operand
(e.g. X86, Vax, M68k). On other, Risc-y architectures the addresses are
commonly split into `load-high` + `load-low` pairs. More work is needed to
address those.
  • Loading branch information
uxmal committed Oct 14, 2024
1 parent d4db24f commit e4e973f
Show file tree
Hide file tree
Showing 38 changed files with 2,910 additions and 5,211 deletions.
2 changes: 1 addition & 1 deletion src/Core/Output/Dumper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public void DumpAssembler(
var flags = MachineInstructionRendererFlags.ResolvePcRelativeAddress;
if (this.RenderInstructionsCanonically)
flags |= MachineInstructionRendererFlags.RenderInstructionsCanonically;
var writer = new FormatterInstructionWriter(formatter, program.Procedures, true);
var writer = new FormatterInstructionWriter(formatter, program.Procedures, program.ImageSymbols, true);
var options = new MachineInstructionRendererOptions(
flags: flags,
syntax: "");
Expand Down
15 changes: 14 additions & 1 deletion src/Core/Output/FormatterInstructionWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
*/
#endregion

using Reko.Core.Loading;
using Reko.Core.Machine;
using System;
using System.Collections.Generic;

namespace Reko.Core.Output
Expand All @@ -31,6 +33,7 @@ public class FormatterInstructionWriter : MachineInstructionRenderer
{
private readonly Formatter formatter;
private readonly IDictionary<Address, Procedure> procedures;
private readonly IDictionary<Address, ImageSymbol> symbols;
private readonly bool separateWithTab;
private int chars;
private readonly List<string> annotations;
Expand All @@ -43,10 +46,15 @@ public class FormatterInstructionWriter : MachineInstructionRenderer
/// machine rendering is sent.</param>
/// <param name="separateWithTab">If true, separate the mnemonic and the
/// first operand with a tab, otherwise use a single space.</param>
public FormatterInstructionWriter(Formatter formatter, IDictionary<Address, Procedure> procedures, bool separateWithTab)
public FormatterInstructionWriter(
Formatter formatter,
IDictionary<Address, Procedure> procedures,
IDictionary<Address, ImageSymbol> symbols,
bool separateWithTab)
{
this.formatter = formatter;
this.procedures = procedures;
this.symbols = symbols;
this.separateWithTab = separateWithTab;
this.annotations = new List<string>();
this.addrInstr = Address.Ptr32(0);
Expand Down Expand Up @@ -118,6 +126,11 @@ public void WriteAddress(string formattedAddress, Address addr)
{
formattedAddress = proc.Name;
}
else if (symbols.TryGetValue(addr, out var symbol))
{
formattedAddress = symbol.Name ?? formattedAddress;
}

chars += formattedAddress.Length;
formatter.WriteHyperlink(formattedAddress, addr);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Gui/TextViewing/DisassemblyFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public void WriteAddress(string formattedAddress, Address addr)
{
span = factory.CreateProcedureTextSpan(proc, addr);
}
else if (program.ImageSymbols.TryGetValue(addr, out var symbol))
{
span = factory.CreateAddressTextSpan(addr, symbol.Name ?? formattedAddress);
}
else
{
span = factory.CreateAddressTextSpan(addr, formattedAddress);
Expand Down
9 changes: 7 additions & 2 deletions src/ImageLoaders/WebAssembly/Output/CodeSectionRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,14 @@ public void RenderFunction(FunctionDefinition function, Formatter formatter)
var mem = new ByteMemoryArea(Address.Ptr32(0x1_0000), codeSection.Bytes);
var rdr = new LeImageReader(mem, function.Start, function.End);
var dasm = arch.CreateDisassembler(rdr);
//$TODO provide real procedures.
//$TODO provide real procedures and image symbols.
var procedures = new Dictionary<Address, Procedure>();
var instrRenderer = new FormatterInstructionWriter(formatter, procedures, false);
var symbols = new Dictionary<Address, ImageSymbol>();
var instrRenderer = new FormatterInstructionWriter(
formatter,
procedures,
symbols,
false);
var options = new MachineInstructionRendererOptions();
var dataStack = new List<DataType>();
var blockStack = new List<WasmInstruction>();
Expand Down
43 changes: 22 additions & 21 deletions src/UnitTests/Decompiler/Scanning/BackwardSlicerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1576,12 +1576,12 @@ 0000000000401211 goto rax
Assert.AreEqual("1[0,2]", bwslc.JumpTableIndexInterval.ToString());
Assert.AreEqual("004011FB", bwslc.GuardInstrAddress.ToString());
}
/*
[Test]
public void Bwslc_RiscV_64bit()
{
/*
*
[Test]
public void Bwslc_RiscV_64bit()
{
*
0000000000015214 F0EF DDDF jal getopt_long
0000000000015218 0713 FFF0 li a4,-0x1
000000000001521C 0793 0005 mv a5,a0
Expand All @@ -1597,24 +1597,24 @@ 0000000000015214 F0EF DDDF jal getopt_long
000000000001523C 87B3 00E7 add a5,a5,a4
0000000000015240 A783 0007 lw a5,(a5)
0000000000015244 8067 0007 jalr zero,a5,0x0
*
call get_optt
a4 = -1
a5 = a0
branch a0 == a4 l0000000000015348
*
call get_optt
a4 = -1
a5 = a0
branch a0 == a4 l0000000000015348
l0000000000015224:
a4 = 118
branch a4 <u a0 l0000000000015200
a4 = 118
branch a4 <u a0 l0000000000015200
l000000000001522C:
a5 = a5 << 0x20
a4 = 0x00010000
a5 = a5 >>u 0x1E
a4 = a4 + 672
a5 = a5 + a4
a5 = CONVERT(Mem0[a5:int32], int32, int64)
call a5 (retsize: 0;)
*/
var l15214 = Given_Block(0x0000000000015214ul);
a5 = a5 << 0x20
a4 = 0x00010000
a5 = a5 >>u 0x1E
a4 = a4 + 672
a5 = a5 + a4
a5 = CONVERT(Mem0[a5:int32], int32, int64)
call a5 (retsize: 0;)
var l15214 = Given_Block(0x0000000000015214ul);
var l15224 = Given_Block(0x0000000000015224ul);
var l1522C = Given_Block(0x000000000001522Cul);
var bye = Given_Block(0x0000000000015200ul);
Expand Down Expand Up @@ -1668,6 +1668,7 @@ branch a4 <u a0 l0000000000015200
Assert.AreEqual("1[0,2]", bwslc.JumpTableIndexInterval.ToString());
Assert.AreEqual("004011FB", bwslc.GuardInstrAddress.ToString());
}
*/
}
// Test cases
// A one-level jump table from MySQL. JTT represents the jump table.
Expand Down
6 changes: 5 additions & 1 deletion src/UserInterfaces/WindowsForms/DisassemblyViewInteractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ public void DumpAssembler()
var options = new MachineInstructionRendererOptions(platform: program.Platform);
if (program.SegmentMap.TryFindSegment(StartAddress, out ImageSegment segment))
{
var formatter = new FormatterInstructionWriter(new TextFormatter(writer), program.Procedures, true);
var formatter = new FormatterInstructionWriter(
new TextFormatter(writer),
program.Procedures,
program.ImageSymbols,
true);
var dasm = program.CreateDisassembler(program.Architecture, StartAddress).GetEnumerator();
while (dasm.MoveNext())
{
Expand Down
2 changes: 1 addition & 1 deletion subjects/Archives/ArTar/hello.reko/hello_O1_init.asm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions subjects/Archives/ArTar/hello.reko/hello_O1_text.asm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions subjects/Elf/Msp430/a.reko/a_text.asm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e4e973f

Please sign in to comment.