Skip to content

Control-flow graph

The basic blocks of one method and the edges between them. See control-flow graph in the code guide.

Cfg

The control-flow graph of one method.

The blocks are ordered by offset, and block 0 is where the method starts. len(cfg) is the number of blocks.

blocks property

The basic blocks, as a read-only sequence that copies nothing.

entry property

entry: Optional[BasicBlock]

The block where the method starts, or None when the graph is empty.

empty property

empty: bool

True when there are no blocks: the method is abstract or native.

block_at_offset

block_at_offset(offset: int) -> Optional[BasicBlock]

The block that starts at this code-unit offset, or None when no block starts there. Use it to follow a branch target to its block.

BasicBlock

A run of instructions that is always entered at its first instruction and left at its last one.

index property

index: int

Position of the block in Cfg.blocks.

start_offset property

start_offset: int

Offset of the block's first instruction, in code units.

end_offset property

end_offset: int

Offset just past the block's last instruction, in code units.

first_insn property

first_insn: int

Index in Method.instructions of the block's first instruction.

insn_count property

insn_count: int

Number of instructions in the block.

successors property

successors: list[CfgEdge]

The edges that leave the block. A block that returns or throws has none.

predecessors property

predecessors: list[int]

Indices in Cfg.blocks of the blocks with an edge to this one.

is_handler_entry property

is_handler_entry: bool

True when the block is the start of a catch handler.

CfgEdge

An edge from one basic block to another.

target_block property

target_block: int

Index in Cfg.blocks of the block the edge leads to.

kind property

Why execution can go along this edge.

case_key property

case_key: Optional[int]

The value a switch case matches, for a SWITCH_CASE edge. None for every other kind.

catch_type property

catch_type: Optional[str]

Descriptor of the exception class the handler catches, such as "Ljava/io/IOException;", for an EXCEPTION edge. None for every other kind, including EXCEPTION_CATCH_ALL, which catches everything.

CfgEdgeKind

Bases: Enum

The kind of edge between two basic blocks of a control-flow graph.

FALLTHROUGH class-attribute instance-attribute

FALLTHROUGH = 0

Execution runs on into the next block. The block's last instruction is not a branch, a return or a throw.

GOTO class-attribute instance-attribute

GOTO = 1

An unconditional goto.

BRANCH_TAKEN class-attribute instance-attribute

BRANCH_TAKEN = 2

The condition of an if instruction holds, and it jumps.

BRANCH_NOT_TAKEN class-attribute instance-attribute

BRANCH_NOT_TAKEN = 3

The condition of an if instruction does not hold, and execution continues after it.

SWITCH_CASE class-attribute instance-attribute

SWITCH_CASE = 4

One case of a switch. The edge's case_key is the value the case matches.

SWITCH_DEFAULT class-attribute instance-attribute

SWITCH_DEFAULT = 5

No case of a switch matches, and execution continues after it.

EXCEPTION class-attribute instance-attribute

EXCEPTION = 6

From a block inside a try range to the handler of one exception class. The edge's catch_type is the descriptor of that class.

EXCEPTION_CATCH_ALL class-attribute instance-attribute

EXCEPTION_CATCH_ALL = 7

From a block inside a try range to a handler that catches every exception. Java compiles a finally block to such a handler.

BasicBlockView

Bases: Sequence[BasicBlock]

Read-only, zero-copy sequence of a CFG's blocks.