Skip to content

Tomek20225/pytc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PyTC

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:

  1. Generating Python bytecode through CPython's .pycache files
  2. Parsing and mapping the bytecode structure in Rust
  3. Converting the bytecode to LLVM Intermediate Representation (IR)
  4. 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.

Requirements:

  • Python 3.10 (CPython implementation) - enforced via a bytecode magic-number check; the opcode tables live in src/utils/bytecode_v310.rs so support for newer versions can be added as sibling modules
  • LLVM 16.0.4 (llc on PATH)
  • GCC (for linking)

Usage:

# 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/

Todo:

  • 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

References:

Examples

Docs

Random

About

Python compiler written in Rust and LLVM

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages