8000 bpo-42349: Clarify basicblocks and give some examples (#714) · python/devguide@af52f4b · GitHub
[go: up one dir, main page]

Skip to content

Commit af52f4b

Browse files
sweeneydeMariatta
andauthored
bpo-42349: Clarify basicblocks and give some examples (#714)
Co-authored-by: Mariatta Wijaya <Mariatta@users.noreply.github.com>
1 parent 0e6f028 commit af52f4b

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

compiler.rst

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -288,26 +288,42 @@ number is passed as the last parameter to each ``stmt_ty`` function.
288288
Control Flow Graphs
289289
-------------------
290290

291-
A control flow graph (often referenced by its acronym, CFG) is a
292-
directed graph that models the flow of a program using basic blocks that
293-
contain the intermediate representation (abbreviated "IR", and in this
294-
case is Python bytecode) within the blocks. Basic blocks themselves are
295-
a block of IR that has a single entry point but possibly multiple exit
296-
points. The single entry point is the key to basic blocks; it all has
297-
to do with jumps. An entry point is the target of something that
298-
changes control flow (such as a function call or a jump) while exit
299-
points are instructions that would change the flow of the program (such
300-
as jumps and 'return' statements). What this means is that a basic
301-
block is a chunk of code that starts at the entry point and runs to an
302-
exit point or the end of the block.
303-
304-
As an example, consider an 'if' statement with an 'else' block. The
305-
guard on the 'if' is a basic block which is pointed to by the basic
306-
block containing the code leading to the 'if' statement. The 'if'
307-
statement block contains jumps (which are exit points) to the true body
308-
of the 'if' and the 'else' body (which may be ``NULL``), each of which are
309-
their own basic blocks. Both of those blocks in turn point to the
310-
basic block representing the code following the entire 'if' statement.
291+
A *control flow graph* (often referenced by its acronym, CFG) is a
292+
directed graph that models the flow of a program. A node of a CFG is
293+
not an individual bytecode instruction, but instead represents a
294+
sequence of bytecode instructions that always execute sequentially.
295+
Each node is called a *basic block* and must always execute from
296+
start to finish, with a single entry point at the beginning and a
297+
single exit point at the end. If some bytecode instruction *a* needs
298+
to jump to some other bytecode instruction *b*, then *a* must occur at
299+
the end of its basic block, and *b* must occur at the start of its
300+
basic block.
301+
302+
As an example, consider the following code snippet:
303+
304+
.. code-block:: Python
305+
306+
if x < 10:
307+
f1()
308+
f2()
309+
else:
310+
g()
311+
end()
312+
313+
The ``x < 10`` guard is represented by its own basic block that
314+
compares ``x`` with ``10`` and then ends in a conditional jump based on
315+
the result of the comparison. This conditional jump allows the block
316+
to point to both the body of the ``if`` and the body of the ``else``. The
317+
``if`` basic block contains the ``f1()`` and ``f2()`` calls and points to
318+
the ``end()`` basic block. The ``else`` basic block contains the ``g()``
319+
call and similarly points to the ``end()`` block.
320+
321+
Note that more complex code in the guard, the ``if`` body, or the ``else``
322+
body may be represented by multiple basic blocks. For instance,
323+
short-circuiting boolean logic in a guard like ``if x or y:``
324+
will produce one basic block that tests the truth value of ``x``
325+
and then points both (1) to the start of the ``if`` body and (2) to
326+
a different basic block that tests the truth value of y.
311327

312328
CFGs are usually one step away from final code output. Code is directly
313329
generated from the basic blocks (with jump targets adjusted based on the

0 commit comments

Comments
 (0)
0