Supercompilation.
A multi-pass optimizer and a polyhedral loop transformer. Two real compiler passes that rewrite the AST before code generation.
4-Pass Supercompiler
Implemented in supercompiler.c (730 lines). Runs iteratively up to 10 passes until fixpoint:
1. Constant Folding
Arithmetic on literals: 3 + 4 → 7. Works on ints, floats, and booleans. Division-by-zero guard.
2. Dead Code Elimination
Removes if(false), while(false), statements after unconditional return. Recursive.
3. Partial Evaluation
Resolves match(42) { 42 => body } at compile time. Clones the matching arm, replaces the match node.
4. Function Specialization
When a call has constant args, clones the function with substituted params: f(42, y) → f_spec_0_42(y).
Polyhedral Loop Optimizer
Implemented in polyhedral.c (761 lines). Detects canonical for-loops and applies real loop transformations:
Loop Tiling
Splits 2-deep nests into tile-sized chunks (default 32). Rewrites the AST with tile iterators for better cache locality.
Loop Fusion
Merges adjacent loops with identical iteration domains into a single loop body.
Parallelism Detection
Conservative write-set analysis marks loops as parallelizable when no write conflicts exist.
Runtime Self-Modification
The std.compiler module lets Braid programs compile and execute new Braid source at runtime. The VM parses the source string, lexes it, generates bytecode, and loads it into live memory.
import std.compiler;
// Compile and execute new Braid code at runtime
let logic = "fn optimize(a, b) { return a * b / math.pi; }";
let compiled_closure = compiler.eval(logic);
let result = compiled_closure(100, 200); // 6366.25 Compilation Targets
The supercompiler feeds into multiple backends. From one source file, Braid can target: