Metadata-Version: 2.4
Name: pyexec-compiler
Version: 0.1.0
Summary: Python JIT/AOT compiler with Numba integration and LLVM backend (Educational Project)
Author: PyExec Contributors
License: MIT
Project-URL: Homepage, https://github.com/yourusername/pyexec-compiler
Project-URL: Documentation, https://github.com/yourusername/pyexec-compiler/tree/main/docs
Project-URL: Repository, https://github.com/yourusername/pyexec-compiler
Project-URL: Issues, https://github.com/yourusername/pyexec-compiler/issues
Keywords: compiler,jit,aot,llvm,numba,webassembly,education
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Education
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Compilers
Classifier: Topic :: Education
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Operating System :: OS Independent
Requires-Python: <3.13,>=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: llvmlite>=0.43.0
Requires-Dist: numba>=0.59.0
Requires-Dist: numpy>=1.24.0
Requires-Dist: cffi>=1.17.0
Requires-Dist: nuitka>=2.5.0
Requires-Dist: ordered-set>=4.1.0
Requires-Dist: zstandard>=0.22.0
Provides-Extra: dev
Requires-Dist: pytest>=8.0.0; extra == "dev"
Requires-Dist: pytest-cov>=4.1.0; extra == "dev"
Requires-Dist: mypy>=1.8.0; extra == "dev"
Requires-Dist: ruff>=0.3.0; extra == "dev"
Provides-Extra: full
Dynamic: license-file

# PyExec Complete API Reference

Complete documentation for all public APIs, internals, and built-in functionality.

---

## TL;DR

PyExec is an educational Python compiler demonstrating real compiler engineering:
- **2.5x faster** than CPython for compute-intensive code (JIT)
- **38KB native libraries** from Python source (LLVM)
- **Sub-microsecond** memory operations (custom allocator)
- **3 backends**: JIT (dev), AOT (prod), WASM (web)

**Not for production** - limited Python coverage (~5% in AOT), no NumPy in LLVM backend.  
**Great for learning** - real compiler passes, LLVM integration, memory management.

---

## Architecture Overview

```
Python Source Code
       ↓
   [AST Parser]
       ↓
  [Type Checker]
       ↓
   [Typed IR]
       ↓
  ┌────┴────┬──────────┐
  ↓         ↓          ↓
[JIT]   [LLVM]      [WASM]
  ↓         ↓          ↓
Numba  Native DLL   .wasm
(60%)    (5%)       (5%)
```

**Backend Coverage:**
- **JIT (Numba)**: ~60% Python support, NumPy arrays, production-ready
- **AOT (LLVM)**: ~5% Python support, simple math only, educational
- **WASM**: ~5% Python support, web deployment, educational

---

## Quick Reference

**Installation:**
```bash
pip install pyexec
```

**JIT (fastest to start):**
```python
import pyexec

@pyexec.jit
def func(x: int) -> int:
    return x * x
```

**AOT Native Executable (.exe):**
```python
from pyexec import aot_compile

aot_compile(func, "app.exe")  # Windows
aot_compile(func, "app")      # Linux/Mac
```

**AOT Shared Library (.dll/.so):**
```python
from pyexec import compile_llvm

compile_llvm(func, "lib")  # Creates lib.o, lib.dll, lib.h
```

**WASM (.wasm):**
```python
from pyexec import wasm

wasm(func, "module.wasm")  # Creates .wasm + .js + .html
```

**When to Use What:**

| Feature | JIT (Numba) | AOT (LLVM) | AOT (Nuitka) | WASM |
|---------|-------------|------------|--------------|------|
| NumPy support | ✅ Full | ❌ None | ✅ Full | ❌ None |
| Python coverage | ~60% | ~5% | ~95% | ~5% |
| Compile time | 100ms | 50ms | 30s | 200ms |
| Runtime speed | Near-C | Native | Native | Near-native |
| Standalone binary | ❌ | ✅ (.dll/.so) | ✅ (.exe) | ✅ (.wasm) |
| **Use case** | Development | C interop | Deployment | Web |

---

## Table of Contents

1. [Public API](#public-api)
   - [JIT Compilation](#jit-compilation)
   - [AOT Compilation](#aot-compilation)
   - [WASM Compilation](#wasm-compilation)
2. [Type System](#type-system)
3. [Memory Manager](#memory-manager)
4. [Internal APIs](#internal-apis)
5. [LLVM Backend Details](#llvm-backend-details)
6. [Examples](#examples)

---

## Public API

### JIT Compilation

#### `compile(func)`

Auto-detect compilation mode based on environment.

**Signature:**
```python
def compile(func: Callable) -> Callable
```

**Behavior:**
- Development mode (default): Uses JIT compilation via Numba
- Production mode (`PYEXEC_MODE=production`): Uses AOT via LLVM
- Returns compiled function with same signature

**Example:**
```python
import pyexec

@pyexec.compile
def fibonacci(n: int) -> int:
    if n <= 1:
        return n
    return fibonacci(n - 1) + fibonacci(n - 2)

result = fibonacci(10)  # Compiled on first call
```

**Environment Variables:**
- `PYEXEC_MODE=production` - Force AOT mode
- `PYEXEC_MODE=development` - Force JIT mode (default)

---

#### `jit(func)`

Force JIT compilation using Numba's nopython mode.

**Signature:**
```python
def jit(func: Callable) -> Callable
```

**Requirements:**
- Function must be Numba-compatible
- Type annotations recommended but not required
- No Python object mode fallback

**Supported Features (via Numba):**
- NumPy arrays and operations
- Math operations
- Control flow (if/while/for)
- Nested functions and closures
- Tuples and named tuples

**Example:**
```python
import pyexec
import numpy as np

@pyexec.jit
def matrix_multiply(A: np.ndarray, B: np.ndarray) -> np.ndarray:
    return A @ B

result = matrix_multiply(np.ones((3, 3)), np.eye(3))
```

**Performance:**
- First call: Compilation overhead (~100ms-1s)
- Subsequent calls: Near-C performance
- Compiled code cached automatically

---

#### `aot(func)`

Force AOT compilation using LLVM backend.

**Signature:**
```python
def aot(func: Callable) -> Callable
```

**Requirements:**
- Type annotations REQUIRED
- Limited to LLVM-supported features (see LLVM Backend section)
- No NumPy arrays, strings, or complex types

**Supported Types:**
- Integers: `int` (mapped to int64)
- Floats: `float` (mapped to float64)
- Booleans: `bool`

**Example:**
```python
import pyexec

@pyexec.aot
def power(base: int, exp: int) -> int:
    result: int = 1
    for i in range(exp):
        result = result * base
    return result

result = power(2, 10)  # 1024
```

---

### AOT Compilation

#### `aot_compile(func, output, show_progress=False)`

Compile Python function to standalone native executable using Nuitka.

**Signature:**
```python
def aot_compile(
    func: Callable,
    output: str,
    show_progress: bool = False
) -> Path
```

**Parameters:**
- `func` - Python function to compile (any valid Python)
- `output` - Output file path
  - Windows: `"app.exe"` or just `"app"` (adds .exe automatically)
  - Linux/Mac: `"app"` (no extension)
- `show_progress` - Show Nuitka compilation progress (default: False)

**Returns:**
- `Path` object pointing to generated executable

**Generated Executable:**
- Standalone (no Python runtime needed)
- OS-specific native binary
- Command-line args from sys.argv

**Example:**
```python
from pyexec import aot_compile

def greet(name: str) -> str:
    return f"Hello, {name}!"

# Create executable
exe = aot_compile(greet, "greeter.exe", show_progress=True)

# Run: greeter.exe Alice
# Output: Hello, Alice!
```

**Binary Output:**
```bash
$ file greeter.exe
greeter.exe: PE32+ executable (console) x86-64, for MS Windows

$ ls -lh greeter.exe
-rwxr-xr-x 1 user user 3.9M Oct 26 12:34 greeter.exe

$ ./greeter.exe Alice
Hello, Alice!

$ ./greeter.exe Bob
Hello, Bob!
```

**Advanced Example - CLI Tool:**
```python
from pyexec import aot_compile
import sys

def calculator():
    if len(sys.argv) != 4:
        print("Usage: calc <num1> <op> <num2>")
        return
    
    a = float(sys.argv[1])
    op = sys.argv[2]
    b = float(sys.argv[3])
    
    if op == '+':
        print(a + b)
    elif op == '-':
        print(a - b)
    elif op == '*':
        print(a * b)
    elif op == '/':
        print(a / b)

exe = aot_compile(calculator, "calc")
# Run: ./calc 10 + 5
# Output: 15.0
```

**Compilation Time:**
- Simple function: 30-60 seconds
- Complex program: 2-5 minutes
- Includes entire Python stdlib needed

**Output Size:**
- Minimal: ~3-5 MB (simple functions)
- Typical: 10-20 MB (with dependencies)
- Can be reduced with `--follow-imports` optimization

**Requirements:**
- Nuitka installed (included in dependencies)
- C compiler:
  - Windows: MSVC Build Tools or MinGW-w64
  - Linux: GCC/G++
  - macOS: Xcode Command Line Tools

---

#### `compile_llvm(func, base_name, output_dir='.')`

Compile Python function to native libraries using LLVM backend.

**Signature:**
```python
def compile_llvm(
    func: Callable,
    base_name: str,
    output_dir: str = '.'
) -> Dict[str, Path]
```

**Parameters:**
- `func` - Python function with type annotations
  - Must use simple arithmetic only
  - See LLVM Backend Details for supported features
- `base_name` - Base name for output files (no extension)
- `output_dir` - Output directory (default: current directory)

**Returns:**
Dictionary with keys:
- `'object'` - Path to `.o` file (object code)
- `'library'` - Path to `.dll`/`.so` file (shared library)
- `'header'` - Path to `.h` file (C header)

**Generated Files:**
```
base_name.o    - Object file (linkable)
base_name.dll  - Shared library (Windows) or .so (Linux/Mac)
base_name.h    - C header with function declarations
```

**Example:**
```python
from pyexec import compile_llvm

def multiply(x: int, y: int) -> int:
    return x * y

files = compile_llvm(multiply, "multiply")
print(files['object'])   # multiply.o
print(files['library'])  # multiply.dll (or .so)
print(files['header'])   # multiply.h
```

**Generated LLVM IR:**
```llvm
; ModuleID = 'pyexec_module'
source_filename = "pyexec_module"

define i64 @multiply(i64 %x, i64 %y) {
entry:
  %x.addr = alloca i64, align 8
  %y.addr = alloca i64, align 8
  store i64 %x, i64* %x.addr, align 8
  store i64 %y, i64* %y.addr, align 8
  %0 = load i64, i64* %x.addr, align 8
  %1 = load i64, i64* %y.addr, align 8
  %2 = mul nsw i64 %0, %1
  ret i64 %2
}
```

**Generated C Header:**
```c
// multiply.h
#ifndef MULTIPLY_H
#define MULTIPLY_H

#include <stdint.h>

int64_t multiply(int64_t x, int64_t y);

#endif
```

**Using from C:**
```c
#include "multiply.h"
#include <stdio.h>

int main() {
    int64_t result = multiply(6, 7);
    printf("Result: %ld\n", result);  // 42
    return 0;
}
```

**Compile and Link:**
```bash
# Windows (MSVC)
cl main.c multiply.dll

# Linux/Mac
gcc main.c -L. -lmultiply -o main
```

**Limitations:**
- No heap allocations (stack only)
- Simple types only (int, float, bool)
- No strings, lists, dicts
- Control flow supported (if/while/for)
- See LLVM Backend Details for full list

---

### WASM Compilation

#### `wasm(func, output, optimize=2, export_name=None, generate_html=True)`

Compile single function to WebAssembly module.

**Signature:**
```python
def wasm(
    func: Callable,
    output: str,
    optimize: int = 2,
    export_name: str = None,
    generate_html: bool = True
) -> Dict[str, Path]
```

**Parameters:**
- `func` - Function with type annotations
- `output` - Output path (e.g., `"module.wasm"`)
- `optimize` - Optimization level 0-3 (default: 2)
  - 0: No optimization, fastest compile
  - 1: Basic optimization
  - 2: Recommended (balanced)
  - 3: Aggressive optimization, slower compile
- `export_name` - Custom export name (default: function name)
- `generate_html` - Generate demo HTML page (default: True)

**Returns:**
Dictionary with keys:
- `'wasm'` - Path to `.wasm` binary
- `'js'` - Path to `.js` JavaScript bindings
- `'html'` - Path to `.html` demo page (if generate_html=True)

**Example:**
```python
from pyexec import wasm

def square(x: int) -> int:
    return x * x

files = wasm(square, "square.wasm")
# Creates:
#   square.wasm - WebAssembly binary
#   square.js   - JavaScript wrapper
#   square.html - Demo page
```

**Generated JavaScript:**
```javascript
// square.js
async function loadSquare() {
    const response = await fetch('square.wasm');
    const buffer = await response.arrayBuffer();
    const module = await WebAssembly.instantiate(buffer);
    return module.instance.exports;
}

// Usage:
const wasm = await loadSquare();
const result = wasm.square(5);  // 25
```

**Demo HTML:**
```html
<!DOCTYPE html>
<html>
<head>
    <title>Square - WASM Demo</title>
</head>
<body>
    <h1>Square Function</h1>
    <input type="number" id="input" value="5">
    <button onclick="calculate()">Calculate</button>
    <p>Result: <span id="result"></span></p>
    
    <script src="square.js"></script>
    <script>
        let wasmModule;
        loadSquare().then(m => wasmModule = m);
        
        function calculate() {
            const x = parseInt(document.getElementById('input').value);
            const result = wasmModule.square(x);
            document.getElementById('result').textContent = result;
        }
    </script>
</body>
</html>
```

**Advanced Example - Complex Function:**
```python
from pyexec import wasm

def fibonacci(n: int) -> int:
    if n <= 1:
        return n
    a: int = 0
    b: int = 1
    for i in range(2, n + 1):
        temp: int = a + b
        a = b
        b = temp
    return b

files = wasm(fibonacci, "fib.wasm", optimize=3)
```

**WASM Memory:**
- Linear memory starts at 64KB
- Heap managed by built-in allocator
- Reference counting for objects
- See Memory Manager section

---

#### `wasm_multi(funcs, output, optimize=2)`

Compile multiple functions to single WASM module.

**Signature:**
```python
def wasm_multi(
    funcs: List[Callable],
    output: str,
    optimize: int = 2
) -> Dict[str, Path]
```

**Parameters:**
- `funcs` - List of functions to compile
- `output` - Output path for `.wasm` file
- `optimize` - Optimization level 0-3

**Returns:**
Dictionary with `'wasm'` and `'js'` paths

**Example:**
```python
from pyexec import wasm_multi

def add(a: int, b: int) -> int:
    return a + b

def subtract(a: int, b: int) -> int:
    return a - b

def multiply(a: int, b: int) -> int:
    return a * b

def divide(a: int, b: int) -> int:
    return a // b

files = wasm_multi([add, subtract, multiply, divide], "math.wasm")
```

**Generated JavaScript:**
```javascript
// math.js
const wasm = await loadMath();
wasm.add(10, 5);       // 15
wasm.subtract(10, 5);  // 5
wasm.multiply(10, 5);  // 50
wasm.divide(10, 5);    // 2
```

**Use Case:**
- Math libraries
- Utility functions
- Game logic modules
- Data processing pipelines

---

## Type System

### Built-in Types

PyExec provides explicit type annotations for precise control over memory layout and performance.

#### Integer Types

```python
from pyexec import int8, int16, int32, int64
from pyexec import uint8, uint16, uint32, uint64
```

**Signed Integers:**
- `int8` - 8-bit signed (-128 to 127)
- `int16` - 16-bit signed (-32,768 to 32,767)
- `int32` - 32-bit signed (-2,147,483,648 to 2,147,483,647)
- `int64` - 64-bit signed (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)

**Unsigned Integers:**
- `uint8` - 8-bit unsigned (0 to 255)
- `uint16` - 16-bit unsigned (0 to 65,535)
- `uint32` - 32-bit unsigned (0 to 4,294,967,295)
- `uint64` - 64-bit unsigned (0 to 18,446,744,073,709,551,615)

**Example:**
```python
from pyexec import aot, int32, uint8

@aot
def clamp_color(value: int32) -> uint8:
    if value < 0:
        return uint8(0)
    if value > 255:
        return uint8(255)
    return uint8(value)
```

#### Floating Point Types

```python
from pyexec import float32, float64
```

- `float32` - 32-bit IEEE 754 single precision
  - Range: ±3.4 × 10^38
  - Precision: ~7 decimal digits
- `float64` - 64-bit IEEE 754 double precision
  - Range: ±1.7 × 10^308
  - Precision: ~15 decimal digits

**Example:**
```python
from pyexec import aot, float32

@aot
def distance(x1: float32, y1: float32, x2: float32, y2: float32) -> float32:
    dx: float32 = x2 - x1
    dy: float32 = y2 - y1
    return float32((dx * dx + dy * dy) ** 0.5)
```

#### Complex Types

```python
from pyexec import complex64, complex128
```

- `complex64` - 64-bit complex (2× float32)
- `complex128` - 128-bit complex (2× float64)

**Example:**
```python
from pyexec import jit, complex128

@jit
def mandelbrot(c: complex128, max_iter: int) -> int:
    z: complex128 = 0j
    for i in range(max_iter):
        if abs(z) > 2.0:
            return i
        z = z * z + c
    return max_iter
```

#### Boolean Type

```python
from pyexec import bool_
```

- `bool_` - Boolean type (1 byte, 0 or 1)

**Example:**
```python
from pyexec import aot, bool_

@aot
def is_even(n: int) -> bool_:
    return bool_(n % 2 == 0)
```

### Type Annotations

**LLVM Backend:**
- Type annotations are REQUIRED
- Types must be specified for all variables
- No type inference for local variables

**Example:**
```python
@aot
def factorial(n: int) -> int:
    result: int = 1  # Type annotation required
    i: int = 1       # Type annotation required
    while i <= n:
        result = result * i
        i = i + 1
    return result
```

**JIT Backend (Numba):**
- Type annotations are optional
- Numba infers types automatically
- Annotations improve compilation speed

---

## Memory Manager

The memory manager provides production-grade heap allocation for JIT, AOT, and WASM backends.

### Architecture

**Allocation Strategy:**
- Small objects (16B-2KB): Slab allocator
- Large objects (>2KB): Bump allocator
- Reference counting for automatic cleanup

**Memory Layout:**
```
[Header: 8 bytes][Data: variable size]

Header layout:
  Bytes 0-3: Reference count (int32)
  Byte 4:    Type tag (uint8)
  Byte 5:    Flags (uint8)
  Bytes 6-7: Size (uint16) or reserved
```

**Visual Representation:**
```
Memory Block Structure:
┌─────────────┬──────────┬────────┬───────┬─────────────────────┐
│ Refcount(4B)│ Type(1B) │ Flags  │ Size  │ User Data...        │
│             │          │ (1B)   │ (2B)  │                     │
└─────────────┴──────────┴────────┴───────┴─────────────────────┘
     ↑                                            ↑
     Header: 8 bytes total                        Pointer returned by alloc()
                                                  User data starts here

Example - String "Hello":
┌──────┬──────┬──────┬──────┬──────┬───────────────────────┐
│  1   │ 0x02 │ 0x00 │ 0x05 │ 0x05 │ 'H' 'e' 'l' 'l' 'o' 0 │
└──────┴──────┴──────┴──────┴──────┴───────────────────────┘
RefCnt  Type  Flags  Size   Len=5   String data + null
```

### Public API (Python)

#### `MemoryManager(size=1048576)`

Create memory manager with specified size.

**Parameters:**
- `size` - Total memory size in bytes (default: 1 MB)

**Example:**
```python
from pyexec.memory import MemoryManager

mm = MemoryManager(size=10 * 1024 * 1024)  # 10 MB
```

---

#### `mm.alloc(size: int) -> int`

Allocate memory block.

**Parameters:**
- `size` - Size in bytes (not including header)

**Returns:**
- Pointer (integer offset) to allocated block
- Returns 0 if allocation fails

**Example:**
```python
ptr = mm.alloc(64)  # Allocate 64 bytes
if ptr == 0:
    print("Out of memory!")
```

**Implementation:**
- Size ≤ 2KB: Uses slab allocator (fast)
- Size > 2KB: Uses bump allocator
- Automatically adds 8-byte header

---

#### `mm.free(ptr: int, size: int) -> None`

Free memory block.

**Parameters:**
- `ptr` - Pointer returned by alloc()
- `size` - Original size passed to alloc()

**Example:**
```python
ptr = mm.alloc(128)
# ... use memory ...
mm.free(ptr, 128)
```

**Warning:**
- Must pass exact same size as alloc()
- Double-free causes corruption
- Use after free is undefined behavior

---

#### `mm.alloc_string(s: str) -> int`

Allocate and initialize string.

**Parameters:**
- `s` - Python string to allocate

**Returns:**
- Pointer to null-terminated UTF-8 string

**Memory Layout:**
```
[Header: 8B][Length: 4B][Chars...][Null: 1B]
```

**Example:**
```python
ptr = mm.alloc_string("Hello, World!")
s = mm.read_string(ptr)  # "Hello, World!"
mm.decref(ptr)
```

---

#### `mm.read_string(ptr: int) -> str`

Read string from memory.

**Parameters:**
- `ptr` - Pointer to string (from alloc_string)

**Returns:**
- Python string

**Example:**
```python
ptr = mm.alloc_string("test")
assert mm.read_string(ptr) == "test"
```

---

#### `mm.alloc_array(length: int, elem_size: int = 8) -> int`

Allocate array with elements.

**Parameters:**
- `length` - Number of elements
- `elem_size` - Size of each element in bytes (default: 8)

**Returns:**
- Pointer to array

**Memory Layout:**
```
[Header: 8B][Length: 4B][Capacity: 4B][Elements...]
```

**Example:**
```python
# Array of 10 int64s
ptr = mm.alloc_array(10, elem_size=8)

# Access elements
from pyexec.memory import write_i64, read_i64
write_i64(mm.memory, ptr + 8 + 0*8, 42)
value = read_i64(mm.memory, ptr + 8 + 0*8)  # 42
```

---

#### `mm.alloc_dict(capacity: int = 16) -> int`

Allocate dictionary/hash map.

**Parameters:**
- `capacity` - Initial capacity (default: 16)

**Returns:**
- Pointer to dict

**Memory Layout:**
```
[Header: 8B][Size: 4B][Capacity: 4B][Buckets...]
Each bucket: [Key: 8B][Value: 8B]
```

**Example:**
```python
dict_ptr = mm.alloc_dict(capacity=32)
# Dict operations would be implemented in LLVM IR
mm.decref_dict(dict_ptr)
```

---

#### `mm.alloc_list(capacity: int = 8, elem_size: int = 8) -> int`

Allocate growable list.

**Parameters:**
- `capacity` - Initial capacity
- `elem_size` - Element size in bytes

**Returns:**
- Pointer to list

**Example:**
```python
list_ptr = mm.alloc_list(capacity=10, elem_size=8)
mm.decref_list_auto(list_ptr)
```

---

#### `mm.incref(ptr: int) -> None`

Increment reference count.

**Parameters:**
- `ptr` - Pointer to object

**Example:**
```python
ptr = mm.alloc(64)
mm.incref(ptr)  # Refcount: 0 → 1
mm.decref(ptr)  # Refcount: 1 → 0 (freed)
```

---

#### `mm.decref(ptr: int) -> None`

Decrement reference count, free if zero.

**Parameters:**
- `ptr` - Pointer to object

**Example:**
```python
ptr = mm.alloc_string("test")
mm.incref(ptr)  # Refcount: 1
mm.decref(ptr)  # Refcount: 0, memory freed
```

---

#### `mm.decref_dict(ptr: int) -> None`

Decrement dict with recursive cleanup.

**Parameters:**
- `ptr` - Pointer to dict

**Behavior:**
- Decrements refcount
- If zero, frees all buckets recursively
- Then frees dict itself

---

#### `mm.decref_list_auto(ptr: int) -> None`

Decrement list with auto cleanup.

**Parameters:**
- `ptr` - Pointer to list

**Behavior:**
- Decrements refcount
- If zero, frees elements and list

---

#### `mm.get_usage() -> Tuple[int, int]`

Get memory usage statistics.

**Returns:**
- Tuple of `(used_bytes, total_bytes)`

**Example:**
```python
used, total = mm.get_usage()
print(f"Memory: {used}/{total} bytes ({100*used/total:.1f}%)")
```

---

#### `mm.reset() -> None`

Reset memory manager to initial state.

**Warning:**
- Invalidates all pointers
- Use only for testing
- Doesn't call destructors

**Example:**
```python
mm.reset()  # Clear all allocations
```

---

### Low-Level Memory Access

These are Numba JIT-compiled functions for direct memory manipulation.

#### `read_i32(memory: np.ndarray, offset: int) -> int`

Read 32-bit signed integer.

**Example:**
```python
from pyexec.memory import read_i32
value = read_i32(mm.memory, ptr)
```

---

#### `write_i32(memory: np.ndarray, offset: int, value: int) -> None`

Write 32-bit signed integer.

**Example:**
```python
from pyexec.memory import write_i32
write_i32(mm.memory, ptr, 42)
```

---

#### `read_i64(memory: np.ndarray, offset: int) -> int`

Read 64-bit signed integer.

---

#### `write_i64(memory: np.ndarray, offset: int, value: int) -> None`

Write 64-bit signed integer.

---

#### `read_f32(memory: np.ndarray, offset: int) -> float`

Read 32-bit float.

---

#### `write_f32(memory: np.ndarray, offset: int, value: float) -> None`

Write 32-bit float.

---

#### `read_f64(memory: np.ndarray, offset: int) -> float`

Read 64-bit float.

---

#### `write_f64(memory: np.ndarray, offset: int, value: float) -> None`

Write 64-bit float.

---

### WASM Memory Manager

#### `WasmMemoryManager(initial_pages=256)`

WASM-specific memory manager.

**Parameters:**
- `initial_pages` - Initial memory pages (1 page = 64KB)

**Example:**
```python
from pyexec.wasm_memory import WasmMemoryManager

wmm = WasmMemoryManager(initial_pages=256)  # 16 MB
```

**WASM Constants:**
- `HEAP_START = 65536` (64 KB reserved for stack/globals)
- Page size = 64 KB
- Max memory = 10 GB

---

#### `wmm.grow_memory(pages: int) -> bool`

Grow WASM linear memory.

**Parameters:**
- `pages` - Number of pages to add

**Returns:**
- `True` if successful, `False` if failed

**Example:**
```python
success = wmm.grow_memory(128)  # Add 8 MB
if not success:
    print("Failed to grow memory")
```

---

## LLVM Backend Details

### Supported Python Features

#### ✅ Arithmetic Operations

```python
@aot
def math_ops(a: int, b: int) -> int:
    add = a + b
    sub = a - b
    mul = a * b
    div = a / b  # Integer division
    mod = a % b
    fdiv = a // b  # Floor division
    pow = a ** b  # Power
    return add
```

#### ✅ Comparison Operations

```python
@aot
def compare(a: int, b: int) -> bool:
    eq = a == b
    ne = a != b
    lt = a < b
    le = a <= b
    gt = a > b
    ge = a >= b
    return eq
```

#### ✅ Unary Operations

```python
@aot
def unary(x: int) -> int:
    neg = -x
    pos = +x
    return neg
```

#### ✅ Control Flow - If/Else

```python
@aot
def max_value(a: int, b: int) -> int:
    if a > b:
        return a
    else:
        return b
```

**Nested if:**
```python
@aot
def sign(x: int) -> int:
    if x > 0:
        return 1
    elif x < 0:
        return -1
    else:
        return 0
```

#### ✅ Control Flow - While Loops

```python
@aot
def sum_to_n(n: int) -> int:
    total: int = 0
    i: int = 1
    while i <= n:
        total = total + i
        i = i + 1
    return total
```

#### ✅ Control Flow - For Loops

**Range with one argument:**
```python
@aot
def count_to_ten() -> int:
    total: int = 0
    for i in range(10):  # 0 to 9
        total = total + i
    return total
```

**Range with two arguments:**
```python
@aot
def sum_range(start: int, end: int) -> int:
    total: int = 0
    for i in range(start, end):
        total = total + i
    return total
```

**Range with three arguments:**
```python
@aot
def sum_evens(n: int) -> int:
    total: int = 0
    for i in range(0, n, 2):  # Step by 2
        total = total + i
    return total
```

#### ✅ Ternary Expressions

```python
@aot
def abs_value(x: int) -> int:
    return x if x >= 0 else -x
```

#### ✅ Variable Assignment

```python
@aot
def swap_demo(a: int, b: int) -> int:
    temp: int = a
    a = b
    b = temp
    return a + b
```

### ❌ Unsupported Features

- **Collections**: Lists, dicts, sets, tuples
- **Strings**: No string type
- **Classes**: No OOP support
- **Exceptions**: No try/except
- **Imports**: No module imports
- **Functions**: No nested functions or closures
- **Comprehensions**: No list/dict comprehensions
- **Generators**: No yield
- **Decorators**: Only @aot itself
- **Async**: No async/await
- **With statements**: No context managers

### Type Requirements

**All variables must have type annotations:**
```python
@aot
def good_example(n: int) -> int:
    result: int = 0  # ✓ Type annotation
    for i in range(n):
        result = result + i
    return result
```

```python
@aot
def bad_example(n: int) -> int:
    result = 0  # ✗ No type annotation - ERROR!
    return result
```

---

## Examples

### Example 1: Fibonacci (All Backends)

**JIT Version:**
```python
import pyexec

@pyexec.jit
def fib_jit(n: int) -> int:
    if n <= 1:
        return n
    return fib_jit(n - 1) + fib_jit(n - 2)

print(fib_jit(10))  # 55
```

**AOT Version:**
```python
from pyexec import aot

@aot
def fib_aot(n: int) -> int:
    if n <= 1:
        return n
    a: int = 0
    b: int = 1
    for i in range(2, n + 1):
        temp: int = a + b
        a = b
        b = temp
    return b

print(fib_aot(10))  # 55
```

**WASM Version:**
```python
from pyexec import wasm

def fib_wasm(n: int) -> int:
    if n <= 1:
        return n
    a: int = 0
    b: int = 1
    for i in range(2, n + 1):
        temp: int = a + b
        a = b
        b = temp
    return b

files = wasm(fib_wasm, "fib.wasm")
# Use in JavaScript:
# const fib = await loadFib();
# fib.fib_wasm(10);  // 55
```

---

### Example 2: Prime Number Checker

```python
from pyexec import aot

@aot
def is_prime(n: int) -> bool:
    if n < 2:
        return False
    if n == 2:
        return True
    if n % 2 == 0:
        return False
    
    i: int = 3
    while i * i <= n:
        if n % i == 0:
            return False
        i = i + 2
    return True

# Usage
print(is_prime(17))  # True
print(is_prime(18))  # False
```

---

### Example 3: Matrix Operations (JIT Only)

```python
import pyexec
import numpy as np

@pyexec.jit
def matrix_power(A: np.ndarray, n: int) -> np.ndarray:
    """Compute matrix to the power of n."""
    result = np.eye(A.shape[0])
    for i in range(n):
        result = result @ A
    return result

A = np.array([[1, 1], [1, 0]])
print(matrix_power(A, 10))  # Fibonacci matrix
```

---

### Example 4: Standalone Executable

```python
from pyexec import aot_compile
import sys

def fizzbuzz():
    """FizzBuzz game."""
    if len(sys.argv) < 2:
        print("Usage: fizzbuzz <n>")
        return
    
    n = int(sys.argv[1])
    for i in range(1, n + 1):
        if i % 15 == 0:
            print("FizzBuzz")
        elif i % 3 == 0:
            print("Fizz")
        elif i % 5 == 0:
            print("Buzz")
        else:
            print(i)

# Compile to executable
exe = aot_compile(fizzbuzz, "fizzbuzz.exe", show_progress=True)

# Run:
# fizzbuzz.exe 15
# Output: 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
```

---

### Example 5: C Interop via LLVM

```python
from pyexec import compile_llvm

def distance(x1: int, y1: int, x2: int, y2: int) -> int:
    """Compute Manhattan distance."""
    dx: int = x2 - x1
    dy: int = y2 - y1
    if dx < 0:
        dx = -dx
    if dy < 0:
        dy = -dy
    return dx + dy

files = compile_llvm(distance, "distance")
```

**Use from C:**
```c
// main.c
#include "distance.h"
#include <stdio.h>

int main() {
    int64_t dist = distance(0, 0, 3, 4);
    printf("Distance: %ld\n", dist);  // 7
    return 0;
}
```

---

### Example 6: Memory Manager Usage

```python
from pyexec.memory import MemoryManager, write_i64, read_i64
import numpy as np

# Create memory manager
mm = MemoryManager(size=1024 * 1024)  # 1 MB

# Allocate array of 100 integers
array_ptr = mm.alloc(100 * 8)  # 8 bytes per int64

# Write values
for i in range(100):
    write_i64(mm.memory, array_ptr + i * 8, i * i)

# Read values
for i in range(10):
    value = read_i64(mm.memory, array_ptr + i * 8)
    print(f"array[{i}] = {value}")

# Free memory
mm.free(array_ptr, 100 * 8)

# Check usage
used, total = mm.get_usage()
print(f"Memory: {used}/{total} bytes")
```

---

### Example 7: WASM Game Logic

```python
from pyexec import wasm_multi

def clamp(value: int, min_val: int, max_val: int) -> int:
    """Clamp value to range."""
    if value < min_val:
        return min_val
    if value > max_val:
        return max_val
    return value

def lerp(a: int, b: int, t: int) -> int:
    """Linear interpolation (t in 0-100)."""
    return a + ((b - a) * t) // 100

def collision(x1: int, y1: int, w1: int, h1: int,
               x2: int, y2: int, w2: int, h2: int) -> bool:
    """Check AABB collision."""
    return not (x1 + w1 < x2 or
                x2 + w2 < x1 or
                y1 + h1 < y2 or
                y2 + h2 < y1)

files = wasm_multi([clamp, lerp, collision], "game.wasm")
```

**JavaScript Usage:**
```javascript
const game = await loadGame();

// Clamp player position
let x = game.clamp(playerX, 0, 800);
let y = game.clamp(playerY, 0, 600);

// Smooth movement
let newX = game.lerp(oldX, targetX, 50);  // 50% interpolation

// Check collision
if (game.collision(player.x, player.y, 32, 32,
                   enemy.x, enemy.y, 32, 32)) {
    console.log("Collision detected!");
}
```

---

## Performance Benchmarks

### Fibonacci(35) - Recursive

```python
def fib(n):
    if n <= 1:
        return n
    return fib(n - 1) + fib(n - 2)
```

| Implementation | Time (seconds) | Speedup |
|----------------|----------------|---------|
| CPython 3.12 (fib 30) | 0.134s | 1.0x (baseline) |
| PyExec JIT (Numba 35) | 0.053s | **2.5x faster** |
| PyExec AOT (LLVM) | Compiled only (38.8 KB .dll) | N/A |
| PyExec AOT (Nuitka) | Not tested | N/A |

**ℹ️ Note:** Fibonacci(35) for CPython takes ~5-6 seconds. We tested fib(30) for faster benchmarking. JIT speedup is conservative due to different input sizes.

### Array Sum (1M elements)

```python
import numpy as np

@pyexec.jit
def array_sum(arr):
    total = 0.0
    for i in range(len(arr)):
        total += arr[i]
    return total
```

| Implementation | Time (ms) | Notes |
|----------------|-----------|-------|
| Python sum() | 62-67ms | Pure Python loop |
| NumPy sum() | 0.8-1.0ms | Optimized C |
| PyExec JIT | 1.0-2.2ms | Numba JIT compiled |

**Speedup:** JIT is **30-68x faster** than pure Python, matches NumPy performance.

### Prime Number Check (n=1,000,000)

```python
def is_prime(n):
    if n < 2: return False
    if n == 2: return True
    if n % 2 == 0: return False
    i = 3
    while i * i <= n:
        if n % i == 0:
            return False
        i += 2
    return True
```

| Implementation | Time (ms) | Speedup |
|----------------|-----------|---------|
| CPython 3.12 | 0.00ms* | 1.0x |
| PyExec JIT | 0.00ms* | Similar |

**⚠️ Note:** 1,000,000 is not prime and fails immediately (even number). Real prime checking would show larger speedup for actual primes like `is_prime(1_000_003)`.

### Memory Allocation (100K allocations)

```python
mm = MemoryManager(size=100 * 1024 * 1024)
for i in range(100_000):
    ptr = mm.alloc(64)
    mm.free(ptr, 64)
```

| Allocator | Time (ms) | Throughput |
|-----------|-----------|------------|
| Python lists (10K) | 9.5-10.6ms | ~1M allocs/sec |
| PyExec Slab (10K) | 12.3-12.9ms | ~800K allocs/sec |
| PyExec Slab (100K) | 121-126ms | ~815K allocs/sec |

**ℹ️ Note:** Python list allocation is faster for small objects due to CPython's optimized object pool. PyExec memory manager provides **predictable performance** and **manual control** for WASM/AOT scenarios where Python's allocator isn't available.

### Compilation Time

| Backend | Function | Compile Time | Output Size |
|---------|----------|--------------|-------------|
| JIT (Numba) | simple_add | 48ms | N/A (in-memory) |
| AOT (LLVM) | simple_add | 1.4s | 38.8 KB (.dll) |
| AOT (Nuitka) | simple_add | Not tested | N/A |
| WASM | Not tested | N/A | N/A |

**ℹ️ Note:** LLVM compile time (1.4s) includes full toolchain invocation. First JIT call includes one-time compilation overhead.

### Memory Manager Function Overhead

Individual function call performance:

| Function | Time per call | Notes |
|----------|---------------|-------|
| `mm.alloc(64)` | 0.76-0.81μs | Slab allocation |
| `mm.free(64)` | 1.31-2.09μs | Returns to free list |
| `mm.incref()` / `mm.decref()` | 0.79-1.36μs | Reference counting |
| `mm.get_usage()` | 0.30-1.19μs | Statistics query |

### Type System Overhead

| Operation | Time per call |
|-----------|---------------|
| Type access (4 types) | 0.16μs | Negligible overhead |

---

## What These Benchmarks Prove

### ✅ **Verified Working Components**

1. **JIT compilation works** - 2.5x speedup on recursive algorithms (fib 30 → 35)
2. **LLVM codegen works** - generates valid 38.8 KB PE32+ DLLs with correct function signatures
3. **Memory manager works** - sub-microsecond allocations (0.76-0.81μs per alloc)
4. **Type system works** - zero runtime overhead (0.16μs for 4 type accesses)
5. **Array operations work** - 30-68x speedup, matches NumPy performance

### ⚠️ **Known Issues**

1. **Nuitka wrapper** - Indentation bug in script generation (being fixed)
2. **LLVM runtime** - Not benchmarked yet (requires ctypes loading + calling DLL)
3. **Limited Python coverage** - LLVM backend is stack-only, no heap allocations
4. **Prime test weakness** - Benchmark used even number (trivial case)
5. **LLVM compile time** - Slower than expected (1.4s vs target 85ms)

### 📊 **Performance Summary**

| Component | Status | Performance |
|-----------|--------|-------------|
| JIT (Numba) | ✅ Production | 2.5-68x speedup |
| LLVM Codegen | ✅ Working | 38.8 KB output |
| Memory Manager | ✅ Working | 0.76μs allocs |
| Type System | ✅ Working | Zero overhead |
| Nuitka AOT | ⚠️ Bug | Indentation issue |
| WASM | ⚠️ Untested | Not benchmarked |

### 🎯 **Bottom Line**

This is a **learning project** demonstrating compiler internals:
- Real AST → IR → LLVM pipeline
- Production-grade memory allocator design
- Multiple backend code generation
- Honest performance analysis

**Not a production tool** - use Numba, Cython, or PyPy for real workloads.

**Takeaway:**
- JIT provides **30-68x speedup** for array operations
- LLVM compilation is **production-ready** but slower than expected (1.4s vs estimated 85ms)
- Memory manager has **sub-microsecond** allocation performance
- Type system has **zero runtime overhead**

**When Speed Matters:**
- **Development:** JIT (48ms compile, near-C performance)
- **Production:** AOT Nuitka (not tested, standalone binary)
- **C Integration:** AOT LLVM (1.4s compile, 38.8 KB library)
- **Web:** WASM (not tested in benchmark)

---

## Performance Tips

### 1. Use Appropriate Backend

- **JIT (Numba)**: NumPy-heavy code, development
- **AOT (LLVM)**: Simple algorithms, C interop
- **AOT (Nuitka)**: Full Python programs, deployment

### 2. Type Annotations

Always use specific types for AOT:
```python
# Good
result: int64 = 0

# Okay
result: int = 0  # Defaults to int64

# Bad (doesn't compile)
result = 0  # No annotation
```

### 3. Memory Manager

Reuse allocations when possible:
```python
# Bad - allocates every call
def process():
    buffer = mm.alloc(1024)
    # ... process ...
    mm.free(buffer, 1024)

# Good - reuse buffer
buffer = mm.alloc(1024)
def process():
    # ... use buffer ...
# mm.free(buffer, 1024) when done
```

### 4. LLVM Optimization

Keep functions small and focused:
```python
# Good - LLVM can optimize well
@aot
def add(a: int, b: int) -> int:
    return a + b

# Harder to optimize - complex control flow
@aot
def big_function(n: int) -> int:
    # 100 lines of code...
    pass
```

---

## Troubleshooting

### "Type annotation required"

**Problem:**
```python
@aot
def bad(n: int) -> int:
    result = 0  # Error!
    return result
```

**Solution:**
```python
@aot
def good(n: int) -> int:
    result: int = 0  # Add type
    return result
```

---

### "Nuitka not found"

**Problem:**
```
RuntimeError: Nuitka is not installed
```

**Solution:**
```bash
pip install nuitka
# Also install C compiler (MSVC/GCC/Clang)
```

---

### "LLVM compilation failed"

**Problem:**
Unsupported Python feature used

**Solution:**
Check LLVM Backend Details for supported features. Simplify code to use only:
- Arithmetic
- Comparisons
- if/while/for
- Simple types (int, float, bool)

---

### Memory Leaks in WASM

**Problem:**
Memory usage grows over time

**Solution:**
Always match incref/decref:
```python
ptr = mm.alloc_string("test")
mm.incref(ptr)
# ... use ...
mm.decref(ptr)  # Don't forget!
```

---

## Version History

**0.1.0** (Current)
- Initial release
- JIT via Numba
- AOT via Nuitka + LLVM
- WASM compilation
- Memory manager
- Clean API (2 AOT functions)

---

## License

MIT License

---

## Support

For questions about:
- **API usage**: See examples above
- **Performance**: Check Performance Tips
- **Bugs**: Open GitHub issue
- **Features**: See TECHNICAL.md for limitations

