# Phase 20.6 โ€” Index **VM Core Complete + Dispatch Unification** Status: Planning Duration: 12 weeks (2026-03-01 - 2026-05-24) Prerequisite: Phase 20.5 complete --- ## ๐Ÿ“š Documentation Structure ### Core Documents 1. **[README.md](README.md)** โญ START HERE (ๆ—ฅๆœฌ่ชž) - Phase overview and goals - Architecture overview (16 instructions + Dispatch) - Weekly breakdown (Week 1-12) - Success criteria and DoD - Risk mitigation 2. **[PLAN.md](PLAN.md)** (English) - Executive summary (5 lines) - Phase breakdown (Phase B + Phase C) - Week-by-week implementation plan - Golden Test strategy - CI integration 3. **[CHECKLIST.md](CHECKLIST.md)** - Week 1-6: VM Core Complete tasks - Week 7-12: Dispatch Unification tasks - Golden Test checklist (100+ tests) - Documentation checklist 4. **[COMPLETION_REPORT.md](COMPLETION_REPORT.md)** (Week 12 deliverable) - Phase 20.6 results - Performance measurements - Lessons learned - Phase 20.7 recommendations --- ## ๐ŸŽฏ Quick Reference ### What is Phase 20.6? (5-line summary) 1. **Goal**: Complete Hakorune VM Core (16 MIR instructions) + Unify Dispatch (single Resolver path). 2. **Strategy**: Phase B (Week 1-6) = All instructions; Phase C (Week 7-12) = Resolver integration. 3. **Verification**: Golden Tests (100+ cases) ensure Rust-VM vs Hako-VM 100% output parity. 4. **Performance**: Target โ‰ฅ 50% of Rust-VM speed (acceptable for PoC, optimize later). 5. **Policy**: Fail-Fast (unknown methods โ†’ RuntimeError), no special-case dispatch, Everything is Box. ### Key Deliverables **Phase B Complete (Week 1-6)**: 1. **MIR Core + Unified Call (MirCall)** in Hakorune: - const, binop, compare, jump, branch, phi, ret โœ… (from 20.5) - MirCall(callee=Method|ModuleFunction|Extern|Constructor) โฌœ (Week 3-4) - load, store, copy, typeop โฌœ (Week 1-2, 5) - barrier, safepoint, loopform, unaryop โฌœ (Week 6) 2. **Control Flow**: - Basic blocks - Branch/jump handling - PHI node resolution - Loop detection 3. **Golden Tests**: - 100+ test cases: Rust-VM vs Hako-VM parity - All outputs must match exactly - Performance: Hako-VM โ‰ฅ 50% of Rust-VM speed **Phase C: Dispatch Unification (Week 7-12)**: 1. **Resolver Integration**: - `Resolver.lookup(type_id, method, arity) -> MethodHandle` - All method calls go through Resolver - No special-case dispatch 2. **CallableBox Refactoring**: - `ExecBox.call_by_handle(handle, args, NoOperatorGuard)` - Single entry point for all invocations - Macro desugaring: `arr.methodRef("push",1)` โ†’ `Callable.ref_method(arr, :push, 1)` 3. **Universal Route Minimization**: - Remove pseudo-method implementations - Everything delegates to Resolver - Fail-Fast: Unknown methods โ†’ RuntimeError --- ## ๐Ÿ“Š Timeline at a Glance | Weeks | Focus | Deliverable | |-------|-------|-------------| | 1-2 | Memory Operations | load, store, copy working | | 3-4 | Unified Call | MirCall(callee=Method/Module/Extern/Constructor) working | | 5 | Type Operations | typeop working๏ผˆnewใฏConstructor็ตŒ็”ฑ๏ผ‰ | | 6 | Control + Golden Tests | All 16 instructions + 100 tests PASS | | 7-8 | Resolver Integration | Resolver.lookup working | | 9-10 | CallableBox Refactoring | ExecBox.call_by_handle working | | 11 | Route Minimization | Special-case dispatch removed | | 12 | Integration + Docs | Phase complete, Phase 20.7 planned | --- ## ๐Ÿ—๏ธ Architecture Overview ### Phase B: MIR Core + Unified Call ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Phase 20.5 Complete (5 instructions)โ”‚ โ”‚ โœ… const, ret, jump, branch, phi โ”‚ โ”‚ โœ… binop (Add/Sub/Mul/Div/Mod) โ”‚ โ”‚ โœ… compare (Eq/Ne/Lt/Le/Gt/Ge) โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ Phase 20.6 Additions (11 instructions) โ”‚ โฌœ load, store, copy (Week 1-2) โ”‚ โ”‚ โฌœ MirCall (Week 3-4) โ”‚ โ”‚ โฌœ typeop๏ผˆnewใฏConstructor๏ผ‰ (Week 5)โ”‚ โ”‚ โฌœ barrier, safepoint, loopform, โ”‚ โ”‚ unaryop (Week 6) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` ### Phase C: Dispatch Unification ``` Before (multiple dispatch paths): โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Method Call Dispatch โ”‚ โ”‚ โ”œโ”€ Global Function (special) โ”‚ โ”‚ โ”œโ”€ Box Method (special) โ”‚ โ”‚ โ”œโ”€ Closure (special) โ”‚ โ”‚ โ”œโ”€ Constructor (special) โ”‚ โ”‚ โ””โ”€ Module Function (special) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ After (single dispatch path): โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Unified Resolver Path โ”‚ โ”‚ โ”‚ โ”‚ Resolver.lookup(type_id, method, arity) โ”‚ โ†“ โ”‚ โ”‚ MethodHandle โ”‚ โ”‚ โ†“ โ”‚ โ”‚ ExecBox.call_by_handle( โ”‚ โ”‚ handle, args, NoOperatorGuard)โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` --- ## ๐Ÿงช Test Strategy Overview ### Golden Test Pyramid ``` /\ / \ Integration (30 tests) /____\ Complex scenarios / \ / \ Functional (70 tests) /__________\ Individual instruction tests / \ / \ Unit (per-handler) /________________\ Each handler has dedicated tests ``` ### Test Categories 1. **Memory Operations** (10 tests): - load, store, copy - Memory aliasing - Edge cases 2. **Method Calls** (15 tests): - call (Global/Module/Closure) - boxcall - externcall 3. **Type Operations** (10 tests): - typeop - newbox 4. **Control Flow** (15 tests): - barrier, safepoint, loopform - Branch/jump combinations 5. **Resolver** (20 tests): - lookup success/failure - Arity mismatches - Type not found 6. **Callable** (25 tests): - call_by_handle - NoOperatorGuard - Macro desugaring 7. **Integration** (30 tests): - Arithmetic - Control flow - Collections - Recursion - Closures - Strings **Total**: 100+ Golden Tests --- ## ๐Ÿ“ฆ Implementation Structure ``` Phase 20.6 Planning: docs/private/roadmap/phases/phase-20.6/ โ”œโ”€โ”€ INDEX.md # โ† You are here โ”œโ”€โ”€ README.md # Phase overview (ๆ—ฅๆœฌ่ชž) โ”œโ”€โ”€ PLAN.md # Execution plan (English) โ”œโ”€โ”€ CHECKLIST.md # Week-by-week checklist โ””โ”€โ”€ COMPLETION_REPORT.md # (Week 12 deliverable) Hakorune VM Implementation: selfhost/hakorune-vm/ โ”œโ”€โ”€ handlers/ # Week 1-6 deliverables โ”‚ โ”œโ”€โ”€ load_handler.hako # Week 1-2 โ”‚ โ”œโ”€โ”€ store_handler.hako # Week 1-2 โ”‚ โ”œโ”€โ”€ copy_handler.hako # Week 1-2 โ”‚ โ”œโ”€โ”€ call_handler.hako # Week 3-4 (Unified MirCall) โ”‚ โ”œโ”€โ”€ boxcall_handler.hako # Week 3-4 โ”‚ โ”œโ”€โ”€ externcall_handler.hako # Week 3-4 โ”‚ โ”œโ”€โ”€ typeop_handler.hako # Week 5 โ”‚ โ”œโ”€โ”€ newbox_handler.hako # Week 5 โ”‚ โ”œโ”€โ”€ barrier_handler.hako # Week 6 โ”‚ โ”œโ”€โ”€ safepoint_handler.hako # Week 6 โ”‚ โ”œโ”€โ”€ loopform_handler.hako # Week 6 โ”‚ โ””โ”€โ”€ unaryop_handler.hako # Week 6 โ”œโ”€โ”€ resolver/ # Week 7-8 deliverables โ”‚ โ”œโ”€โ”€ resolver_box.hako โ”‚ โ”œโ”€โ”€ method_handle_box.hako โ”‚ โ””โ”€โ”€ type_registry_box.hako โ”œโ”€โ”€ callable/ # Week 9-10 deliverables โ”‚ โ”œโ”€โ”€ exec_box.hako โ”‚ โ”œโ”€โ”€ no_operator_guard_box.hako โ”‚ โ””โ”€โ”€ macro_desugar.hako โ””โ”€โ”€ tests/ # Week 1-12 โ”œโ”€โ”€ golden/ # 100+ tests โ”‚ โ”œโ”€โ”€ phase-b/ # Week 1-6 โ”‚ โ”‚ โ”œโ”€โ”€ memory/ # 10 tests โ”‚ โ”‚ โ”œโ”€โ”€ calls/ # 15 tests โ”‚ โ”‚ โ”œโ”€โ”€ types/ # 10 tests โ”‚ โ”‚ โ””โ”€โ”€ control/ # 15 tests โ”‚ โ”œโ”€โ”€ phase-c/ # Week 7-12 โ”‚ โ”‚ โ”œโ”€โ”€ resolver/ # 20 tests โ”‚ โ”‚ โ”œโ”€โ”€ callable/ # 25 tests โ”‚ โ”‚ โ””โ”€โ”€ unification/ # 30 tests โ”‚ โ””โ”€โ”€ integration/ # Week 12 โ”‚ โ”œโ”€โ”€ arithmetic.hako โ”‚ โ”œโ”€โ”€ control_flow.hako โ”‚ โ”œโ”€โ”€ collections.hako โ”‚ โ”œโ”€โ”€ recursion.hako โ”‚ โ”œโ”€โ”€ strings.hako โ”‚ โ””โ”€โ”€ closures.hako โ””โ”€โ”€ benchmark/ # Performance tests โ”œโ”€โ”€ benchmark_suite.sh โ””โ”€โ”€ performance_report.md ``` --- ## ๐Ÿš€ Quick Start (After Phase Complete) ### Run Golden Tests ```bash # Run all Golden Tests bash tools/golden_test_hakorune_vm.sh # Expected output: # โœ… phase-b/memory: 10/10 PASS # โœ… phase-b/calls: 15/15 PASS # โœ… phase-b/types: 10/10 PASS # โœ… phase-b/control: 15/15 PASS # โœ… phase-c/resolver: 20/20 PASS # โœ… phase-c/callable: 25/25 PASS # โœ… phase-c/unification: 30/30 PASS # โœ… integration: 30/30 PASS # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ # Total: 155/155 PASS (100%) ``` ### Run Performance Benchmark ```bash # Benchmark Hako-VM vs Rust-VM bash tools/benchmark_hakorune_vm.sh # Expected output: # Test: arithmetic.hako # Rust-VM: 1.2s # Hako-VM: 2.1s (57% speed) โœ… # # Test: control_flow.hako # Rust-VM: 0.8s # Hako-VM: 1.5s (53% speed) โœ… # # Average: Hako-VM = 55% of Rust-VM โœ… # Memory: 180MB (< 200MB target) โœ… ``` ### Use Unified Dispatch ```bash # Run program with unified dispatch ./target/release/hako --backend vm program.hako # Debug Resolver HAKO_RESOLVER_TRACE=1 ./target/release/hako --backend vm program.hako # Output: # [resolver] lookup type_id=42 method="push" arity=1 โ†’ handle=0x123 # [exec] call_by_handle handle=0x123 args=[value] โ†’ OK ``` --- ## โš ๏ธ Prerequisites ### From Phase 20.5 - [x] HostBridge API complete - [x] op_eq Migration complete (NoOperatorGuard) - [x] VM Foundations PoC complete (5 instructions) - [x] Golden Test infrastructure ready ### For Phase 20.6 - [ ] Week 1: Memory operation design approved - [ ] Week 3: Method call design approved (Unified MirCall) - [ ] Week 7: Resolver design approved - [ ] Week 9: CallableBox design approved --- ## ๐ŸŽฏ Success Criteria Summary ### Technical - [ ] All 16 MIR instructions working - [ ] Control flow complete (branch, phi, loopform) - [ ] Golden Tests: 100+ cases ALL PASS - [ ] Dispatch unified (single Resolver path) - [ ] Special-case dispatch removed ### Performance - [ ] Hako-VM โ‰ฅ 50% of Rust-VM speed โœ… - [ ] Memory usage < 200MB โœ… - [ ] Compile time < 10s (small programs) โœ… ### Quality - [ ] Test coverage: All instructions covered - [ ] Documentation: Complete architecture docs - [ ] Code review: ChatGPT + Claude approved - [ ] CI: All tests passing --- ## ๐Ÿ“š Related Phases ### Previous - [Phase 20.5 - VM Foundations](../phase-20.5/) - HostBridge API - op_eq Migration - VM Foundations PoC (5 instructions) ### Next - **Phase 20.7 - Collections in Hakorune** (8 weeks, 2026-05-25 โ†’ 2026-07-19) - MapBox in Hakorune - ArrayBox in Hakorune - Deterministic iteration order - Performance: โ‰ฅ 70% of Rust-Collections ### Parallel - [Phase 15.78 - Frozen UX Polish](../phase-15.78/) - Distribution packaging - Doctor improvements --- ## ๐Ÿ’ฌ Communication ### Weekly Sync Points - **Monday**: Week start, goal setting - **Wednesday**: Mid-week progress check - **Friday**: Week review, performance measurement ### Issue Tracking - Use GitHub issues with label `phase-20.6` - Prefix: `[20.6]` in commit messages - Milestone: `Phase 20.6 - VM Core Complete` ### Review Process - Each week: Self-review + Golden Tests - Week 6, 12: Full review with ChatGPT/Claude - Blocking issues: Immediate escalation --- ## ๐Ÿ”— External Resources ### Reference Implementations - **Rust VM**: `/src/backend/mir_interpreter/` (reference implementation) - **MIR Spec**: [INSTRUCTION_SET.md](../../../../reference/mir/INSTRUCTION_SET.md) - **Phase 20.5 Roadmap**: [PURE_HAKORUNE_ROADMAP.md](../phase-20.5/PURE_HAKORUNE_ROADMAP.md) ### Related Documents - [Hakorune VM Discovery](../phase-20.5/HAKORUNE_VM_DISCOVERY.md) - VM implementation analysis - [HostBridge API Design](../phase-20.5/HOSTBRIDGE_API_DESIGN.md) - C-ABI boundary - [Op_eq Migration](../phase-20.5/OP_EQ_MIGRATION.md) - NoOperatorGuard pattern --- ## ๐Ÿ“ Notes ### Naming Conventions - **Phase B**: VM Core Complete (Week 1-6) - **Phase C**: Dispatch Unification (Week 7-12) - **Golden Tests**: Rust-VM vs Hako-VM parity tests - **Resolver**: Single resolution path for all method calls ### File Naming - Handlers: `{instruction}_handler.hako` - Tests: `test_{feature}_{case}.hako` - Golden outputs: `{test}_rust.txt`, `{test}_hako.txt` ### Verification - Use `diff` for exact output comparison - Use Golden Test script for automation - Use benchmark script for performance measurement --- **Created**: 2025-10-14 **Last Updated**: 2025-10-14 **Status**: Planning (Phase not yet started) **Phase Start**: 2026-03-01 **Phase End**: 2026-05-24 **Next Review**: 2026-03-01 (Phase start) 5. **ABI๏ผˆdesign only๏ผ‰** - Phase 20.6 ใงใฏ Cโ€‘ABI ใฎ SSOT/ใƒใƒชใ‚ทใƒผ/ใƒ•ใƒฉใ‚ฐใ‚’่ฟฝๅŠ ๏ผˆๆŒ™ๅ‹•ใฏไธๅค‰๏ผ‰ - ๅฎŸ่ฃ…ๅˆ‡ๆ›ฟ๏ผˆString/Array/Map๏ผ‰ใฏ Phase 20.7 ใงๆฎต้šŽๅฐŽๅ…ฅ