Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control flow graphing & dump format plugin #318

Merged
merged 11 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions Cpp2IL.Core/Graphs/BasicBlock.cs

This file was deleted.

83 changes: 83 additions & 0 deletions Cpp2IL.Core/Graphs/Block.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cpp2IL.Core.ISIL;

namespace Cpp2IL.Core.Graphs;

public class Block
{
public BlockType BlockType { get; set; }
public List<Block> Predecessors;
public List<Block> Successors;

public List<InstructionSetIndependentInstruction> isilInstructions;

public int ID { get; set; }

public bool Dirty { get; set; }
public bool Visited = false;



public override string ToString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("Type: " + BlockType);
stringBuilder.AppendLine();
foreach(var instruction in isilInstructions)
{
stringBuilder.AppendLine(instruction.ToString());
}
return stringBuilder.ToString();
}

public Block()
{
BlockType = BlockType.Unknown;
Predecessors = new();
Successors = new();
isilInstructions = new();
ID = -1;
}

public void AddInstruction(InstructionSetIndependentInstruction instruction)
{
isilInstructions.Add(instruction);
}

public void CaculateBlockType()
{
// This enum is kind of redundant, can be possibly swapped for IsilFlowControl and no need for BlockType?
if (isilInstructions.Count > 0) {
var instruction = isilInstructions.Last();
switch (instruction.FlowControl)
{
case IsilFlowControl.UnconditionalJump:
BlockType = BlockType.OneWay;
break;
case IsilFlowControl.ConditionalJump:
BlockType = BlockType.TwoWay;
break;
case IsilFlowControl.IndexedJump:
BlockType = BlockType.NWay;
break;
case IsilFlowControl.MethodCall:
BlockType = BlockType.Call;
break;
case IsilFlowControl.MethodReturn:
BlockType = BlockType.Return;
break;
case IsilFlowControl.Interrupt:
BlockType = BlockType.Interrupt;
break;
case IsilFlowControl.Continue:
BlockType = BlockType.Fall;
break;
default:
BlockType = BlockType.Unknown;
break;
}

}
}
}
34 changes: 26 additions & 8 deletions Cpp2IL.Core/Graphs/BlockType.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
namespace Cpp2IL.Core.Graphs;
namespace Cpp2IL.Core.Graphs;

public enum BlockType : byte
{
OneWay,
TwoWay,
NWay,
Call,
Return,
Fall
}
OneWay, // etc. Jumps to another block
TwoWay, // etc. Jumps conditionally to two blocks
NWay, // switch statement nonsense I think
Call, // Block finishes with call
Return, // Block finishes with return
// we fall into next block, for example block A has
// mov reg1, reg2
// mov reg2, reg3
//
// block b has
// mov reg3, reg4
// mov reg2, reg4
// and another block finishes with a jump to start instruction of block b meaning block a falls into b (bad explanation)
Fall,

// Block type is not known yet
Unknown,

// Exception or something raised
Interrupt,

// Empty blocks that serve as entry and exit markers
Entry,
Exit,
}
Loading
Loading