# Phase 20.5 โ€” Index **Escape from Rust: Bootstrap Compiler Implementation** Status: Planning (Gate plan active) Duration: 10 weeks (2025-12-21 - 2026-02-28) --- ## ๐Ÿ“š Documentation Structure ### Core Documents 1. **[README.md](README.md)** โญ START HERE - Phase overview and goals - Weekly breakdown (Week 1-10) - Implementation strategy decision (Option B) - Success criteria and DoD 2. **[MILESTONE.md](MILESTONE.md)** - Objectives and deliverables - Weekly milestones - Success criteria - Risk analysis 3. **[BOOTSTRAP_CHAIN_ANALYSIS.md](BOOTSTRAP_CHAIN_ANALYSIS.md)** - 3-stage bootstrap chain detailed design - Stage 1 (Rust) โ†’ Stage 2 (Hako v1) โ†’ Stage 3 (Hako v2) - Data flow analysis - Verification strategy 4. **[C_CODE_GENERATOR_DESIGN.md](C_CODE_GENERATOR_DESIGN.md)** - MIR โ†’ C conversion design - 16-instruction mapping table - PHI resolution strategy - Test strategy (43 test cases) 5. **[PLAN.md](PLAN.md)** โœ… Gate-based execution plan๏ผˆ็Ÿญ็ธฎ็‰ˆ๏ผ‰ - 5่กŒใ‚ตใƒžใƒช / ๆœ€ๅฐๅ‘ฝไปคใ‚ปใƒƒใƒˆ / DoD - Gate Aใ€œE๏ผˆParserโ†’MIRโ†’VM PoCโ†’op_eqโ†’็ตฑๅˆ๏ผ‰ - ใƒ†ใ‚นใƒˆ/CI/ใƒชใ‚นใ‚ฏ/ๆฌกใ‚นใƒ†ใƒƒใƒ— --- ## ๐ŸŽฏ Quick Reference ### What is Phase 20.5? (5-line summary) 1) Goal: ่„ฑRustใ€‚ๅ‡็ตEXEใ‚’ๅœŸๅฐใซ่‡ชๅทฑใƒ›ใ‚นใƒˆใธๅ‰้€ฒใ€‚ 2) Strategy: Gateๆ–นๅผ๏ผˆParserโ†’MIRโ†’VM PoCโ†’op_eqโ†’็ตฑๅˆ๏ผ‰ใ€‚ 3) Boundary: Cโ€‘ABI/HostBridgeใฎใฟๅค–้ƒจๅขƒ็•Œใ€‚ไธญใฏEverything is Boxใ€‚ 4) Proof: ๆฑบๅฎšๆ€ง๏ผˆJSONๆญฃ่ฆๅŒ–๏ผ‰ใ€Golden๏ผ†ๅ›บๅฎš็‚นใงๆคœ่จผใ€‚ 5) Policy: ๅฐใ•ใใ€้ †ๅบใ‚ˆใใ€SKIPใฏWARNใ€ๅ›žๅธฐใฎใฟFAILใ€‚ ### Key Deliverables 1. **Bootstrap Compiler** (`apps/bootstrap-compiler/`) - Written in Hakorune - Runs on frozen EXE - Outputs C code 2. **C Code Generator** - MIR JSON โ†’ C source - 16 instructions fully supported - NyRT function calls 3. **Verification** - v1 == v2 (compiler parity) - v2 == v3 (fixed point) - 10 test programs PASS --- ## ๐Ÿ“Š Timeline at a Glance | Weeks | Focus | Deliverable | |-------|-------|-------------| | 1-2 | Design & Analysis | Design docs complete | | 3-4 | Parser Adaptation | Parser works on frozen EXE | | 5-6 | MIR Builder Migration | MIR JSON generation works | | 7-8 | C Code Generator | C code emission works | | 9 | Bootstrap Integration | v1 == v2 verified | | 10 | Documentation & Review | Phase complete | --- ## ๐Ÿ”„ Bootstrap Chain Overview ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ hako-frozen-v1 โ”‚ โ”‚ (Rust, 724KB) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ compiles v โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ bootstrap_v1 โ”‚ โ”‚ (Hakorune code) โ”‚ โ”‚ runs on frozen โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ compiles itself v โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ bootstrap_v2.c โ”‚ โ”‚ (C source) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ clang + NyRT v โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ bootstrap_v2 โ”‚ โ”‚ (native binary) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ Verify: v1 output == v2 output โœ… ``` --- ## ๐Ÿ’ก Implementation Strategy ### Decision: Option B (Reuse apps/selfhost-compiler/) **Rationale**: - 2500 lines of existing, tested code - 170 smoke tests already PASS - 90%+ reusability - Proven architecture **Code Reuse Breakdown**: ``` โœ… 90% Reusable: Parser, Emitter, MIR Builder (2250 lines) โš ๏ธ 10% Adaptation: using paths, Box constraints (250 lines) โŒ New Code: C Code Generator (500 lines) Total Effort: ~750 lines (vs 2500+ for Option A) ``` --- ## ๐Ÿงช Test Strategy ### Test Pyramid ``` /\ / \ Level 3: Self-Compilation (1 test, slow) /____\ - v1 compiles itself โ†’ v2 / \ - v2 compiles itself โ†’ v3 / \ - Verify: v2 == v3 /__________\ / \ Level 2: Comprehensive (10 tests, medium) / \ - If/else, loops, functions, boxes, arrays /________________\ / \ Level 1: Smoke Tests (43 tests, fast) - Each MIR instruction - Basic parsing - C generation ``` --- ## ๐Ÿ“ฆ Directory Structure ``` Phase 20.5 Planning: docs/private/roadmap/phases/phase-20.5/ โ”œโ”€โ”€ INDEX.md # โ† You are here โ”œโ”€โ”€ README.md # Phase overview โ”œโ”€โ”€ MILESTONE.md # Milestones & DoD โ”œโ”€โ”€ BOOTSTRAP_CHAIN_ANALYSIS.md # 3-stage bootstrap โ”œโ”€โ”€ C_CODE_GENERATOR_DESIGN.md # C codegen design โ”œโ”€โ”€ REUSABILITY_ANALYSIS.md # (Week 1-2 deliverable) โ”œโ”€โ”€ RISK_ASSESSMENT.md # (Week 1-2 deliverable) โ”œโ”€โ”€ IMPLEMENTATION_GUIDE.md # (Week 3+ deliverable) โ””โ”€โ”€ COMPLETION_REPORT.md # (Week 10 deliverable) Implementation: apps/bootstrap-compiler/ โ”œโ”€โ”€ parser/ # Week 3-4 โ”‚ โ”œโ”€โ”€ parser_box.hako โ”‚ โ””โ”€โ”€ lexer_box.hako โ”œโ”€โ”€ mir_builder/ # Week 5-6 โ”‚ โ””โ”€โ”€ builder_box.hako โ”œโ”€โ”€ codegen/ # Week 7-8 โ”‚ โ”œโ”€โ”€ c_emitter_box.hako โ”‚ โ””โ”€โ”€ c_runtime_box.hako โ”œโ”€โ”€ tests/ # Week 3-9 โ”‚ โ”œโ”€โ”€ smoke/ # 43 tests โ”‚ โ”œโ”€โ”€ integration/ # 10 tests โ”‚ โ””โ”€โ”€ bootstrap/ # 1 test (v1==v2==v3) โ”œโ”€โ”€ main.hako # Entry point โ””โ”€โ”€ README.md # User guide ``` --- ## ๐Ÿš€ Quick Start (After Phase Complete) ### Compile a Hakorune Program ```bash # Stage 1: Use frozen EXE to run bootstrap compiler ./hako-frozen-v1 apps/bootstrap-compiler/main.hako \ --input my_program.hako \ --output my_program.c # Stage 2: Compile C to native binary clang my_program.c -o my_program \ -L /path/to/hako_kernel \ -lhako_kernel \ -lpthread -ldl -lm # Stage 3: Run ./my_program ``` ### Verify Bootstrap Chain ```bash # Run verification script bash tools/verify_bootstrap_chain.sh # Expected output: # โœ… Stage 1 โ†’ Stage 2: OK # โœ… Stage 2 โ†’ Stage 3: OK # โœ… v1 == v2: VERIFIED # โœ… v2 == v3: VERIFIED (fixed point) # โœ… Bootstrap chain complete! ``` --- ## โš ๏ธ Prerequisites ### From Phase 15.77 - [x] Frozen EXE built and tested (`hako-frozen-v1.exe`) - [x] NyRT function calls working (Result: 6 test PASS) - [x] MIR JSON โ†’ .o โ†’ EXE pipeline verified - [x] Windows (MSVC/MinGW) + Linux support ### For Phase 20.5 - [ ] apps/selfhost-compiler/ analysis complete (Week 1-2) - [ ] Frozen EXE constraints documented (Week 1-2) - [ ] C Code Generator design approved (Week 1-2) - [ ] Test infrastructure ready (Week 3+) --- ## ๐ŸŽฏ Success Criteria Summary ### Technical - [ ] Bootstrap chain works: Stage 1 โ†’ 2 โ†’ 3 - [ ] C Code Generator: 16/16 instructions supported - [ ] Verification: v1 == v2 == v3 (identical output) - [ ] Tests: 43 smoke + 10 integration + 1 bootstrap PASS ### Performance - [ ] Stage 2 compile time: < 30s for self-compilation - [ ] Stage 3 compile time: < 5s for self-compilation - [ ] Memory usage: < 100MB ### Quality - [ ] Documentation complete (user guide + design docs) - [ ] Code is modular (Box-based) - [ ] Edge cases covered - [ ] Review approved (ChatGPT + Claude) --- ## ๐Ÿ“š Related Phases ### Previous - [Phase 15.77 - Frozen EXE Finalization](../phase-15.77/) - [Phase 15.76 - extern_c & Frozen Toolchain](../phase-15.76/) - [Phase 15.75 - Escape from Rust Planning](../phase-15.75/) ### Next - **Phase 20.6 - Complete Rust Removal** - VM executor โ†’ Hakorune implementation - Rust codebase โ†’ 0 lines - Pure Hakorune self-hosting ### Parallel - [Phase 15.78 - Frozen UX Polish](../phase-15.78/) - Distribution packaging - Doctor improvements - Windows polish --- ## ๐Ÿ’ฌ Communication ### Weekly Sync Points - **Monday**: Week start, goal setting - **Wednesday**: Mid-week progress check - **Friday**: Week review, next week planning ### Issue Tracking - Use GitHub issues with label `phase-20.5` - Prefix: `[20.5]` in commit messages - Milestone: `Phase 20.5 - Bootstrap Compiler` ### Review Process - Each week: Self-review + smoke tests - Week 5, 10: Full review with ChatGPT/Claude - Blocking issues: Immediate escalation --- ## ๐Ÿ”— External Resources ### Industry Examples - **Rust Bootstrap**: [Rust stage0 documentation](https://rustc-dev-guide.rust-lang.org/building/bootstrapping.html) - **Go Bootstrap**: [Go 1.5 Bootstrap Process](https://go.dev/doc/go1.5#bootstrap) - **OCaml Bootstrap**: [OCaml self-hosting](https://ocaml.org/docs/compiling-ocaml-projects) ### Papers - [Rapid Self-Hosting Paper](../../../../private/papers-active/rapid-selfhost-ai-collaboration/) - [Reflections on Trusting Trust](https://www.cs.cmu.edu/~rdriley/487/papers/Thompson_1984_ReflectionsonTrustingTrust.pdf) (Ken Thompson, 1984) --- ## ๐Ÿ“ Notes ### Naming Conventions - **Stage 1**: Rust compiler, frozen EXE (`hako-frozen-v1`) - **Stage 2**: Hakorune compiler v1 (`bootstrap_v1`) - **Stage 3**: Hakorune compiler v2 (`bootstrap_v2`) - **Fixed Point**: v2 == v3 (self-consistency) ### File Naming - Test outputs: `test_v1.c`, `test_v2.c` - Bootstrap outputs: `bootstrap_v2.c`, `bootstrap_v3.c` - Always include version suffix for clarity ### Verification - Use `diff` for exact comparison - Use `md5sum` for quick hash checks - Use `tools/verify_bootstrap_chain.sh` for automation --- **Created**: 2025-10-14 **Last Updated**: 2025-10-14 **Status**: Planning (Phase not yet started) **Next Review**: 2025-12-21 (Phase start)