Table of Contents

Class ControlFlowGraph

Namespace
RDCore.SDK.Semantics.Flow
Assembly
RDCore.SDK.dll

The code paths of a procedure: every way control can pass from one Instruction of an InstructionList to another, from the first instruction to the end of the activation (RD-VBAL §5.0.3).

public sealed class ControlFlowGraph
Inheritance
ControlFlowGraph
Inherited Members

Remarks

A code path is a walk from Entry along the edges of the graph. The graph does not evaluate anything, so every branch of a condition is a path, except where the condition is the literal True or False; a path that exists in the graph may be one that no input could take, and the questions the graph answers are the ones whose answer does not depend on that - whether there is a way to reach something, never whether it will be taken.

The activation ends at Exit: a virtual node that is not an instruction, which control reaches by falling off the end of the body and by an Exit Sub, Exit Function or Exit Property. End halts the whole program and does not reach it; neither does an error that no handler takes.

An On Error GoTo handler is reached from every instruction that can raise an error while the statement is in effect, before the instruction completes (Error). What the graph could not describe exactly is Imprecision.

Properties

Entry

The node control starts at: the first instruction, or Exit when the body has none.

public int Entry { get; }

Property Value

int

Exit

The node the activation ends at, which is one past the last instruction.

public int Exit { get; }

Property Value

int

Imprecision

What the graph could not describe exactly.

public ControlFlowImprecision Imprecision { get; }

Property Value

ControlFlowImprecision

Instructions

The instructions the nodes of the graph are the offsets of.

public InstructionList Instructions { get; }

Property Value

InstructionList

Methods

CanReach(int, Func<Instruction, bool>)

Whether there is a code path from the start of the procedure to target on which no instruction satisfies occurs.

public bool CanReach(int target, Func<Instruction, bool> occurs)

Parameters

target int

The offset of an instruction, or Exit.

occurs Func<Instruction, bool>

What the path must avoid: an instruction that satisfies it, once it completes, ends every path through it. An instruction that raises an error does not complete, so the path to a handler that leaves it is not ended by it; and the target itself, which the path arrives at and does not execute, is not tested.

Returns

bool

Remarks

"Is the return value assigned on every path to the end?" is the question !CanReach(Exit, assigns); "is this statement dead code?" is !IsReachable(node). The answer is exact for the paths the graph has (see Imprecision for those it approximates).

IsReachable(int)

Whether control can ever arrive at node.

public bool IsReachable(int node)

Parameters

node int

The offset of an instruction, or Exit.

Returns

bool

Of(InstructionList)

Builds the graph of a procedure body.

public static ControlFlowGraph Of(InstructionList instructions)

Parameters

instructions InstructionList

The lowered body of a procedure.

Returns

ControlFlowGraph

SuccessorsOf(int)

The ways control can pass from node to another.

public ImmutableArray<ControlFlowEdge> SuccessorsOf(int node)

Parameters

node int

The offset of an instruction; Exit has none.

Returns

ImmutableArray<ControlFlowEdge>