Python compiler written in Rust that transpiles Python bytecode to LLVM IR for optimized native compilation.
The tool's goal is to speed up Python code by:
- Generating Python bytecode through CPython's .pycache files
- Parsing and mapping the bytecode structure in Rust
- Converting the bytecode to LLVM Intermediate Representation (IR)
- Using LLVM's powerful optimization passes to generate highly optimized native code
This approach leverages LLVM's mature optimization pipeline and cross-platform capabilities, allowing for efficient native code generation across different architectures.
- Python 3.10 (CPython implementation) - enforced via a bytecode
magic-number check; the opcode tables live in
src/utils/bytecode_v310.rsso support for newer versions can be added as sibling modules - LLVM 16.0.4 (
llcon PATH) - GCC (for linking)
# Compile a Python file to a native executable and run it
pytc compile foo.py
# See the decoded bytecode with ✓/✗ markers showing which opcodes the
# compiler supports - the to-do list for making a new file compile
pytc inspect foo.py
# Compare pytc's decoding against CPython's own disassembly
pytc inspect foo.py --dis
# Verbose compilation (per-instruction log + CPython dis dump)
pytc compile foo.py --debug --out-dir build/- Generation and decompilation of .pycache files based on the .py files
- Variable declaration
- Integer addition and subtraction
- Printing to console
- Mapping of all primitive and non-primitive types
- Mapping of all operations
- User-defined functions
- Python standard library mapping
- Imports and modules
- Comprehensive test suite (
cargo test, see tests/README.md) - Benchmarks
- Python bytecode explanation on example 1
- Python bytecode explanation on example 2
- Article on bytecode decompilation
- Opcodes
- Opcode Ids
- Code object bit flags
- Exception handling encoding
- Bytecode address-to-line encoding
- Python bytecode instructions
- https://github.com/python/cpython/blob/main/Python/marshal.c
- https://github.com/python/cpython/blob/main/Include/cpython/code.h
- Unicode character values lookup
- https://github.com/ThePrimeagen/ts-rust-zig-deez/blob/master/rust/src/lexer/lexer.rs#L175
- https://github.com/ThePrimeagen/ts-rust-zig-deez/blob/master/python/deez_py/tokens.py
- https://nedbatchelder.com/blog/200804/the_structure_of_pyc_files.html
- https://nowave.it/python-bytecode-analysis-1.html