Contents

  1. Artifacts

Important: Pyodide takes time to initialize. Initialization completion is indicated by a red border around Run all button.

We previous discussed how to extract the control flow graph from the Python AST. However, the Python AST can change from version to version, with the introduction of new control flow structures. The byte code, in comparison, stays relatively stable. Hence, we can use the bytecode to recover the control flow graph too.

First, we need the following imports. The dis package gives us access to the Python disassembly, and networkx and matplotlib lets us draw.

System Imports These are available from Pyodide, but you may wish to make sure that they are installed if you are attempting to run the program directly on the machine.
  1. matplotlib
  2. networkx

We also need pydot for drawing

Available Packages These are packages that refer either to my previous posts or to pure python packages that I have compiled, and is available in the below locations. As before, install them if you need to run the program directly on the machine.
  1. pydot-1.4.1-py2.py3-none-any.whl


Let us start by defining a few functions that we want to extract the control-flow graphs of.



Next, we define the node in the graph.



Now, we come to the main class



A small helper



We define an entry point for the graph



Next, the meat of generation. We look handle the jump instructions specifically. Everything else is a simple block.



We need to fix the jumps now.



Making the graph to be displayed.



This finishes our implementation.



Show the image



Artifacts

The runnable Python source for this notebook is available here.