mirror of
https://github.com/ApfelTeeSaft/NESDecompiler.git
synced 2026-08-26 19:33:30 +00:00
50 lines
1.7 KiB
C#
50 lines
1.7 KiB
C#
using NESDecompiler.Core.Disassembly;
|
|
|
|
namespace NESDecompiler.Core.Decompilation;
|
|
|
|
/// <summary>
|
|
/// Represents an independently decompiled function
|
|
/// </summary>
|
|
public class DecompiledFunction
|
|
{
|
|
/// <summary>
|
|
/// The CPU address where the address' first instruction is located
|
|
/// </summary>
|
|
public ushort Address { get; }
|
|
|
|
/// <summary>
|
|
/// The instructions that make up this function in the correct order in which they should be
|
|
/// executed.
|
|
/// </summary>
|
|
public IReadOnlyList<DisassembledInstruction> OrderedInstructions { get; }
|
|
|
|
/// <summary>
|
|
/// Location and the labels of all jump and branch targets within this function
|
|
/// </summary>
|
|
public IReadOnlyDictionary<ushort, string> JumpTargets { get; }
|
|
|
|
public DecompiledFunction(
|
|
ushort address,
|
|
IReadOnlyList<DisassembledInstruction> instructions,
|
|
IReadOnlySet<ushort> jumpTargets)
|
|
{
|
|
Address = address;
|
|
JumpTargets = instructions
|
|
.Where(x => jumpTargets.Contains(x.CPUAddress))
|
|
.Where(x => x.Label != null)
|
|
.ToDictionary(x => x.CPUAddress, x => x.Label!);
|
|
|
|
// We need to order the instructions so that the starting instruction is the first one encountered.
|
|
// We can't just rely on the CPU address, because a function may jump to a code point earlier than
|
|
// the first instruction.
|
|
var initialInstructions = instructions
|
|
.Where(x => x.CPUAddress >= address)
|
|
.OrderBy(x => x.CPUAddress);
|
|
|
|
var trailingInstructions = instructions
|
|
.Where(x => x.CPUAddress < address)
|
|
.OrderBy(x => x.CPUAddress);
|
|
|
|
OrderedInstructions = initialInstructions.Concat(trailingInstructions).ToArray();
|
|
}
|
|
} |