1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
************************
Control Flow Graph (CFG)
************************
To analyze or optimize existing code, ``bytecode`` provides a
:class:`ControlFlowGraph` class which is a `control flow graph (CFG)
<https://en.wikipedia.org/wiki/Control_flow_graph>`_.
The control flow graph is used to perform the stack depth analysis when
converting to code. Because it is better at identifying dead code than CPython
it can lead to reduced stack size.
Example
=======
Dump the control flow graph of the :ref:`conditional jump example
<ex-cond-jump>`::
from bytecode import Label, Instr, Bytecode, ControlFlowGraph, dump_bytecode
label_else = Label()
label_print = Label()
bytecode = Bytecode([Instr('LOAD_GLOBAL', (True, 'print')),
Instr('LOAD_NAME', 'test'),
Instr('POP_JUMP_IF_FALSE', label_else),
Instr('LOAD_CONST', 'yes'),
Instr('JUMP_FORWARD', label_print),
label_else,
Instr('LOAD_CONST', 'no'),
label_print,
Instr('CALL', 1),
Instr('LOAD_CONST', None),
Instr('RETURN_VALUE')])
blocks = ControlFlowGraph.from_bytecode(bytecode)
dump_bytecode(blocks)
Output::
block1:
LOAD_GLOBAL (True, 'print')
LOAD_NAME 'test'
POP_JUMP_IF_FALSE <block3>
-> block2
block2:
LOAD_CONST 'yes'
JUMP_FORWARD <block4>
block3:
LOAD_CONST 'no'
-> block4
block4:
CALL 1
LOAD_CONST None
RETURN_VALUE
We get 4 blocks:
* block #1 is the start block and ends with ``POP_JUMP_IF_FALSE`` conditional
jump and is followed by the block #2
* block #2 ends with ``JUMP_FORWARD`` unconditional jump
* block #3 does not contain jump and is followed by the block #4
* block #4 is the final block
The start block is always the first block.
Analyze the control flow graph
==============================
The ``bytecode`` module provides two ways to iterate on blocks:
* iterate on the basic block as a sequential list
* browse the graph by following jumps and links to next blocks
Iterate on basic blocks
-----------------------
Iterating on basic blocks is a simple as this loop::
for block in blocks:
...
Example of a ``display_blocks()`` function::
from bytecode import UNSET, Label, Instr, Bytecode, BasicBlock, ControlFlowGraph
def display_blocks(blocks):
for block in blocks:
print("Block #%s" % (1 + blocks.get_block_index(block)))
for instr in block:
if isinstance(instr.arg, BasicBlock):
arg = "<block #%s>" % (1 + blocks.get_block_index(instr.arg))
elif instr.arg is not UNSET:
arg = repr(instr.arg)
else:
arg = ''
print(" %s %s" % (instr.name, arg))
if block.next_block is not None:
print(" => <block #%s>"
% (1 + blocks.get_block_index(block.next_block)))
print()
label_else = Label()
label_print = Label()
bytecode = Bytecode([Instr('LOAD_GLOBAL', (True, 'print')),
Instr('LOAD_NAME', 'test'),
Instr('POP_JUMP_IF_FALSE', label_else),
Instr('LOAD_CONST', 'yes'),
Instr('JUMP_FORWARD', label_print),
label_else,
Instr('LOAD_CONST', 'no'),
label_print,
Instr('CALL', 1),
Instr('LOAD_CONST', None),
Instr('RETURN_VALUE')])
blocks = ControlFlowGraph.from_bytecode(bytecode)
display_blocks(blocks)
Output::
Block #1
LOAD_GLOBAL (True, 'print')
LOAD_NAME 'test'
POP_JUMP_IF_FALSE <block #3>
=> <block #2>
Block #2
LOAD_CONST 'yes'
JUMP_FORWARD <block #4>
Block #3
LOAD_CONST 'no'
=> <block #4>
Block #4
CALL 1
LOAD_CONST None
RETURN_VALUE
.. note::
:class:`SetLineno` is not handled in the example to keep it simple.
Browse the graph
----------------
Recursive function is a simple solution to browse the control flow graph.
Example to a recursive ``display_block()`` function::
from bytecode import UNSET, Label, Instr, Bytecode, BasicBlock, ControlFlowGraph
def display_block(blocks, block, seen=None):
# avoid loop: remember which blocks were already seen
if seen is None:
seen = set()
if id(block) in seen:
return
seen.add(id(block))
# display instructions of the block
print("Block #%s" % (1 + blocks.get_block_index(block)))
for instr in block:
if isinstance(instr.arg, BasicBlock):
arg = "<block #%s>" % (1 + blocks.get_block_index(instr.arg))
elif instr.arg is not UNSET:
arg = repr(instr.arg)
else:
arg = ''
print(" %s %s" % (instr.name, arg))
# is the block followed directly by another block?
if block.next_block is not None:
print(" => <block #%s>"
% (1 + blocks.get_block_index(block.next_block)))
print()
# display the next block
if block.next_block is not None:
display_block(blocks, block.next_block, seen)
# display the block linked by jump (if any)
target_block = block.get_jump()
if target_block is not None:
display_block(blocks, target_block, seen)
label_else = Label()
label_print = Label()
bytecode = Bytecode([Instr('LOAD_GLOBAL', (True, 'print')),
Instr('LOAD_NAME', 'test'),
Instr('POP_JUMP_IF_FALSE', label_else),
Instr('LOAD_CONST', 'yes'),
Instr('JUMP_FORWARD', label_print),
label_else,
Instr('LOAD_CONST', 'no'),
label_print,
Instr('CALL', 1),
Instr('LOAD_CONST', None),
Instr('RETURN_VALUE')])
blocks = ControlFlowGraph.from_bytecode(bytecode)
display_block(blocks, blocks[0])
Output::
Block #1
LOAD_GLOBAL (True, 'print')
LOAD_NAME 'test'
POP_JUMP_IF_FALSE <block #3>
=> <block #2>
Block #2
LOAD_CONST 'yes'
JUMP_FORWARD <block #4>
Block #4
CALL 1
LOAD_CONST None
RETURN_VALUE
Block #3
LOAD_CONST 'no'
=> <block #4>
Block numbers are no displayed in the sequential order: block #4 is displayed
before block #3.
.. note::
Dead code (unreachable blocks) is not displayed by ``display_block``.
|