using Dna.Extensions; using Rivers; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Dna.ControlFlow { public class BasicBlock : Node { /// /// Gets or sets the address of the basic block. /// public ulong Address { get; set; } /// /// Gets a collection of basic block instructions. /// public List Instructions { get; set; } = new List(); /// /// Gets or sets the first basic block instruction. /// public T EntryInstruction { get => Instructions.First(); set { Instructions.RemoveAt(0); Instructions.Insert(0, value); } } /// /// Gets or sets the last basic block instruction. /// public T ExitInstruction { get => Instructions.Last(); set { Instructions.RemoveAt(Instructions.Count - 1); Instructions.Add(value); } } public BasicBlock(ulong address) : base(address.ToString("X")) { } public IEnumerable> GetIncomingEdges() => IncomingEdges.Select(x => x.ToBlockEdge()); public IEnumerable> GetOutgoingEdges() => OutgoingEdges.Select(x => x.ToBlockEdge()); public void AddIncomingEdge(BlockEdge edge) => IncomingEdges.Add(edge); public void AddOutgoingEdge(BlockEdge edge) => OutgoingEdges.Add(edge); public void AddIncomingEdges(IEnumerable> edges) { foreach (var edge in edges) IncomingEdges.Add(edge); } public void AddOutgoingEdges(IEnumerable> edges) { foreach (var edge in edges) OutgoingEdges.Add(edge); } public override string ToString() => GraphFormatter.FormatBlock(this); } }