Repository navigation
Compiler Architecture
0x4171341 edited this page Jul 23, 2026
·
2 revisions
Source (.lpp) → Lexer → Parser → Semantic → Typecheck → Escape → MIR → Cranelift → lpp-link → Executable
Converts source text to tokens. Handles significant whitespace (Indent/Dedent), 21 keywords, string/int/float/bool literals, operators.
Recursive descent parser. Produces AST with: functions, structs, enums, imports, statements (if/while/for/match/break/continue), expressions (binary ops, calls, field access, closures, enum constructors, try operator).
- Scope resolution (lexical scoping with function/closure/block nesting)
- Binding tracking (every variable gets a unique BindingId)
- Name resolution (identifiers → binding lookups)
- Import resolution (multi-file, dotted paths, stdlib)
- Type inference for
:=declarations - Explicit type checking at function boundaries
- Struct field type resolution
- Enum variant type checking
- Builtin function parameter/return type validation
- Classifies every binding as Stack, Heap (ARC), or Arena
- Rule 1: Values returned from functions → promoted to Heap
- Rule 2: Values stored in containers → promoted to Heap
- Rule 3: Self-referential structs → Arena allocation
- Cycle detection and rejection
Lowers AST to Mid-level IR (typed SSA-like representation). 7 optimization passes:
| Pass | File | What it does |
|---|---|---|
| ARC | pass_arc.rs |
Insert retain/release for ownership |
| Closure | pass_closure.rs |
Lift closures to top-level functions |
| Const Prop | pass_constprop.rs |
Fold constant expressions |
| DCE | pass_dce.rs |
Remove dead code |
| Branch | pass_branch.rs |
Simplify conditional branches |
| Peephole | pass_peephole.rs |
Local instruction optimization |
| Inline | pass_inline.rs |
Inline small functions |
-
compiler.rs— Module setup, function declarations, entry point wrapper -
lower.rs— MIR → Cranelift IR translation, register allocation -
types.rs— L++ type → Cranelift type mapping
Custom linker producing standalone executables:
- Linux ELF — Program headers, segments, entry point
- Windows PE — COFF sections (.text/.rdata/.data/.idata/.reloc), IAT, base relocations
- macOS Mach-O — Load commands, segments
| Component | Lines |
|---|---|
| Compiler total | ~14,800 |
| Builtins declarations | ~1,800 |
| Linker (lpp-link) | ~2,000 |
| Package manager | ~1,800 |
| Cranelift backend | ~1,300 |
| MIR (IR + passes) | ~2,500 |
| Parser | ~900 |
| Type checker | ~800 |
| Semantic analysis | ~600 |
| Escape analysis | ~600 |