📋 Phase 15セルフホスティング戦略整理 & LLVM改善

## Phase 15戦略整理
- セルフホスティング戦略2025年9月版を作成
- Phase 15.2-15.5の段階的実装計画を明確化
  - 15.2: LLVM独立化(nyash-llvm-compiler crate)
  - 15.3: Nyashコンパイラ実装でセルフホスト達成
  - 15.4: VM層のNyash化(革新的アプローチ)
  - 15.5: ABI移行(LLVM完成後)
- ROADMAP.mdの優先順位調整、README.md更新

## LLVM改善(ChatGPT5協力)
- BuilderCursor::with_block改善(状態の適切な保存/復元)
- seal_blockでの挿入位置管理を厳密化
- 前任ブロックのみ処理、重複PHI incoming防止
- defined_in_blockトラッキングで値のスコープ管理

## 洞察
- コンパイル不要のセルフホスティング実現可能
- VM層をNyashで書けば即座実行可能
- Phase 22(Nyash LLVMコンパイラ)への道筋

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Selfhosting Dev
2025-09-12 14:59:03 +09:00
parent f307c4f7b1
commit a530b454f6
7 changed files with 278 additions and 87 deletions

View File

@ -14,6 +14,25 @@ MIR 13命令の美しさを最大限に活かし、外部コンパイラ依存
4. **エコシステムの自立**: Nyashだけで完結する開発環境
5. **劇的なコード圧縮**: 75%削減で保守性・可読性の革命
## 🚀 実装戦略2025年9月更新
### Phase 15.2: LLVM層の独立化実装中
- nyash-llvm-compiler crateの分離
- MIR JSON/バイナリ入力 → ネイティブEXE出力
- 独立したツールとして配布可能
### Phase 15.3: Nyashコンパイラ実装
- NyashでNyashパーサー実装
- AST→MIR変換
- ブートストラップでセルフホスティング達成!
### Phase 15.4: VM層のNyash化革新的
- MIR解釈エンジンをNyashで実装
- コンパイル不要の即座実行
- デバッグ・開発効率の劇的向上
詳細:[セルフホスティング戦略 2025年9月版](implementation/self-hosting-strategy-2025-09.md)
## 📊 主要成果物
### コンパイラコンポーネント
@ -207,10 +226,11 @@ ny_free_buf(buffer)
## 📅 実施時期
- **開始条件**: Phase 10-14完了後
- **推定開始**: 2026年前半
- **推定期間**: 6-8ヶ月
- **早期着手**: YAML自動生成は今すぐ開始可能
- **現在進行中**2025年9月
- **Phase 15.2**: LLVM独立化実装中
- **Phase 15.3**: Nyashコンパイラ2025年後半
- **Phase 15.4**: VM層Nyash化2026年前半
- **Phase 15.5**: ABI移行LLVM完成後
## 💡 期待される成果

View File

@ -18,23 +18,29 @@ This roadmap is a living checklist to advance Phase 15 with small, safe boxes. U
## Next (small boxes)
1) Standard Ny std impl (P0→実体化)
- Implement P0 methods for string/array/map in Nyash (keep NyRT primitives minimal)
- Enable via `nyash.toml` `[ny_plugins]` (optin); extend `tools/jit_smoke.sh`
2) Ny compiler MVP (Ny→MIR on JIT path)
- Ny tokenizer + recursivedescent parser (current subset) in Ny; drive existing MIR builder
- Flag path: `NYASH_USE_NY_COMPILER=1` to switch rust→ny compiler; rust parser as fallback
- Add apps/selfhost-compiler/ and minimal smokes
3) Bootstrap loop (c0→c1→c1)
- Use existing trace/hash harness to compare parity; add optional CI gate
4) Plugins CI split (継続)
- Core alwayson (JIT, plugins disabled); Plugins as optional job (strict off by default)
5) LLVM Native EXE Generation
1) LLVM Native EXE Generation (Phase 15.2) 🚀
- LLVM backend object → executable pipeline completion
- Separate `nyash-llvm-compiler` crate (reduce main build weight)
- Input: MIR (JSON/binary) → Output: native executable
- Link with nyrt runtime (static/dynamic options)
- Integration: `nyash --backend llvm --emit exe program.nyash -o program.exe`
2) Standard Ny std impl (P0→実体化)
- Implement P0 methods for string/array/map in Nyash (keep NyRT primitives minimal)
- Enable via `nyash.toml` `[ny_plugins]` (optin); extend `tools/jit_smoke.sh`
3) Ny compiler MVP (Ny→MIR on JIT path) (Phase 15.3) 🎯
- Ny tokenizer + recursivedescent parser (current subset) in Ny; drive existing MIR builder
- Flag path: `NYASH_USE_NY_COMPILER=1` to switch rust→ny compiler; rust parser as fallback
- Add apps/selfhost-compiler/ and minimal smokes
4) Bootstrap loop (c0→c1→c1')
- Use existing trace/hash harness to compare parity; add optional CI gate
- **This achieves self-hosting!** Nyash compiles Nyash
5) VM Layer in Nyash (Phase 15.4) ⚡
- Implement MIR interpreter in Nyash (13 core instructions)
- BoxCall/ExternCall bridge to existing infrastructure
- Optional LLVM JIT acceleration for hot paths
- Enable instant execution without compilation
6) Plugins CI split (継続)
- Core alwayson (JIT, plugins disabled); Plugins as optional job (strict off by default)
## Later (incremental)

View File

@ -0,0 +1,132 @@
# Phase 15 セルフホスティング戦略 2025年9月版
## 🎯 セルフホスティングの段階的実現戦略
### 現在地
- ✅ v0 NyパーサーNy→JSON IR完成
- ✅ MIR生成基盤ありRust実装
- 🚧 LLVM層改善中ChatGPT5協力
- 📅 NyコンパイラMVP計画中
### 君の提案の妥当性検証
## 📊 セルフホスティングの段階
### Stage 1: LLVM層の独立最優先
```
現在: Rustモリス → MIR → LLVM → オブジェクト
提案: Rustモリス → MIR(JSON) → [LLVM EXE] → ネイティブEXE
```
**実装詳細**
1. `nyash-llvm-compiler` crateを分離
2. 入力MIRJSON/バイナリ)
3. 出力:ネイティブ実行ファイル
4. nyrtランタイムとのリンク
**メリット**
- ビルド時間短縮Rust側は軽量化
- 独立したツールとして配布可能
- パイプライン明確化
### Stage 2: Nyashコンパイラ実装現在計画中
```
現在: Rustパーサー → MIR
提案: Nyashコンパイラ → AST/JSON → MIR生成層
```
**実装詳細**
1. Nyashで再帰下降パーサー実装
2. AST構造をJSONで出力
3. 既存のMIR生成層に接続
4. `NYASH_USE_NY_COMPILER=1`で切替
**これでセルフホスティング達成!**
- Nyashで書いたコンパイラがNyashをコンパイル
- Rustコンパイラは不要に
### Stage 3: VM層のNyash実装革新的
```
現在: Rust VM → MIR解釈
提案: Nyash VM → MIR解釈 → (必要時LLVM呼び出し)
```
**実装詳細**
```nyash
box NyashVMBox {
mir_module: MIRModuleBox
pc_stack: ArrayBox
value_stack: ArrayBox
frame_stack: ArrayBox
execute(mir_json) {
me.mir_module = MIRModuleBox.parse(mir_json)
me.runFunction("main")
}
runFunction(name) {
local func = me.mir_module.getFunction(name)
local frame = new FrameBox(func)
me.frame_stack.push(frame)
loop(frame.pc < func.instructions.length()) {
local inst = func.instructions[frame.pc]
me.executeInstruction(inst, frame)
frame.pc = frame.pc + 1
}
}
}
```
**メリット**
- **コンパイル不要**で即実行
- VMロジックを動的に変更可能
- デバッグ・実験が容易
## 🚀 実現順序の提案
### Phase 15.2: LLVM独立化
1. LLVM層をcrateに分離
2. MIR JSONインターフェース確立
3. スタンドアロンEXE生成
### Phase 15.3: Nyashコンパイラ
1. パーサー実装Nyashで
2. AST→MIRブリッジ
3. ブートストラップテスト
### Phase 15.4: VM層Nyash化
1. MIR解釈エンジン基本13命令
2. BoxCall/ExternCallブリッジ
3. パフォーマンス最適化JIT連携
## 💡 ABI移行タイミング
**LLVM独立化完了後が最適**理由:
1. インターフェース確定後に最適化
2. 独立EXEならABI変更の影響限定的
3. パフォーマンス測定してから判断
## 📋 検証結果
**君の提案は正しい!**
1. **LLVM EXE独立** → MIR JSONで疎結合
2. **Nyashコンパイラ** → AST/JSONでMIR生成
3. **セルフホスト完了** → Rustコンパイラ不要
4. **VM層Nyash化** → 究極の柔軟性
この順序なら:
- 段階的に実現可能
- 各段階で動作確認
- リスク最小化
- 最終的に完全セルフホスト
## 🎯 次のアクション
1. **LLVM crateの設計開始**
2. **MIR JSONスキーマ確定**
3. **Nyパーサー拡張計画**
4. **VMプロトタイプ設計**
これが現実的で革新的なロードマップにゃ!