In CPython, the compilation from source code to bytecode involves several steps:
- Parse source code into a parse tree (
- Transform parse tree into an Abstract Syntax Tree (
- Transform AST into a Control Flow Graph (
- Emit bytecode based on the Control Flow Graph (
The purpose of this document is to outline how these steps of the process work.
This document does not touch on how parsing works beyond what is needed to explain what is needed for compilation. It is also not exhaustive in terms of the how the entire system works. You will most likely need to read some source to have an exact understanding of all details.