Giao diện
Compiler Internals Deep Tech
Magic behind the curtain — Cách rustc biến code thành binary
Module Overview
"Để viết Rust tốt hơn, hãy hiểu cách Rust được compile."
Module này đi sâu vào compilation pipeline của Rust — từ source code qua các Intermediate Representations đến machine code.
⚠️ TARGET AUDIENCE
Module này dành cho những ai muốn hiểu tại sao Rust compile chậm, cách borrow checker hoạt động, và làm thế nào để đọc assembly output.
Kiến trúc Module
Cấu trúc Nội dung
📝 Compilation Pipeline
HIR (High-Level IR), MIR (Mid-Level IR), LLVM IR. Hiểu từng stage trong pipeline.
🔧 Assembly Analysis
Đọc x86_64 assembly từ Rust code. SIMD optimizations với std::arch.
Compilation Stages
┌─────────────────────────────────────────────────────────────────┐
│ RUST COMPILATION PIPELINE │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Source Code (.rs) │
│ │ │
│ ▼ │
│ ┌─────────┐ Parsing │
│ │ AST │ Abstract Syntax Tree │
│ └────┬────┘ │
│ │ │
│ ▼ │
│ ┌─────────┐ Desugaring (for loops, ? operator) │
│ │ HIR │ High-Level IR │
│ └────┬────┘ │
│ │ Type checking, trait resolution │
│ ▼ │
│ ┌─────────┐ Borrow checking, optimization │
│ │ MIR │ Mid-Level IR (Control Flow Graph) │
│ └────┬────┘ │
│ │ │
│ ▼ │
│ ┌─────────┐ LLVM optimizations │
│ │LLVM IR │ Platform-agnostic │
│ └────┬────┘ │
│ │ │
│ ▼ │
│ ┌─────────┐ Final binary │
│ │ Machine │ x86_64, ARM, WASM... │
│ │ Code │ │
│ └─────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘Tools
| Tool | Command | Purpose |
|---|---|---|
| cargo expand | cargo expand | Xem macro expansion |
| rustc --emit | rustc --emit=hir,mir,llvm-ir | Xem IR stages |
| cargo-show-asm | cargo asm | Xem assembly |
| Compiler Explorer | godbolt.org | Online assembly viewer |
"The borrow checker doesn't run on source code — it runs on MIR."