docs: Phase 15.5 Core Box Unification計画策定

- コアBox(nyrt内蔵)削除→プラグイン/ユーザーBoxの2層構造へ
- 約700行削減見込み(nyrt実装600行+特別扱い100行)
- DLL動作確認→Nyashコード化の段階的移行戦略
- ChatGPTの革命的提案を実装計画に落とし込み
- ROADMAP.mdにPhase 15.5として正式追加

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Selfhosting Dev
2025-09-24 04:39:50 +09:00
parent 868519c691
commit 57599c174d
2 changed files with 226 additions and 4 deletions

View File

@ -50,20 +50,26 @@ This roadmap is a living checklist to advance Phase 15 with small, safe boxes. U
- [ ] local/if/loop/call/method/new/var/logical/compare
- [ ] PHI 合流は Bridge に委譲If/Loop
- [ ] Smokes: nested if / loop 累積 / and/or × if/loop
5) PHI 自動化は Phase15LoopForm = MIR18
5) Phase 15.5: Core Box Unification3層→2層革命🎯
- コアBoxnyrt内蔵削除、プラグインBox/ユーザーBoxの2層に統一
- 環境変数制御で段階的移行: `NYASH_USE_PLUGIN_CORE_BOXES=1`
- 削減目標: 約700行nyrt実装600行 + 特別扱い100行
- DLL動作確認→Nyashコード化の安全な移行戦略
- 詳細: [phase-15.5-core-box-unification.md](phase-15.5-core-box-unification.md)
6) PHI 自動化は Phase15 後LoopForm = MIR18
- Phase15: 現行の BridgePHI を維持し、E2E 緑とパリティを最優先
- MIR18 (LoopForm): LoopForm 強化逆Loweringで PHI を自動生成(合流点の定型化)
6) Bootstrap loop (c0→c1→c1')
7) Bootstrap loop (c0→c1→c1')
- Use existing trace/hash harness to compare parity; add optional CI gate
- **This achieves self-hosting!** Nyash compiles Nyash
7) VM Layer in Nyash (Phase 15.4) ⚡
8) VM Layer in Nyash (Phase 15.4) ⚡
- Implement MIR interpreter in Nyash (13 core instructions)
- Dynamic dispatch via MapBox for instruction handlers
- BoxCall/ExternCall bridge to existing infrastructure
- Optional LLVM JIT acceleration for hot paths
- Enable instant execution without compilation
- Expected: 5000 lines for complete VM implementation
6) Plugins CI split (継続)
9) Plugins CI split (継続)
- Core alwayson (JIT, plugins disabled); Plugins as optional job (strict off by default)
## Later (incremental)

View File

@ -0,0 +1,216 @@
# Phase 15.5: Core Box Unification - 3層→2層革命
## 📅 実施予定
2025年9月24日〜10月15日3週間
## 🎯 目標
コアBoxnyrt内蔵を削除し、プラグインBox・ユーザーBoxの2層構造に統一
### 削減見込み
- **コード削減**: 約600行削除nyrt実装
- **特別扱い削除**: MIRビルダー/バックエンドから約100行
- **全体寄与**: Phase 15目標80k→20kの約1%
## 📊 現状の3層構造削除対象
```
1. コアBoxnyrt内蔵← 削除対象
- StringBox, IntegerBox, BoolBox
- ArrayBox, MapBox
- 特別な最適化パス
2. プラグインBox.so/.dll
- FileBox, NetBox, ConsoleBox等
- FFI TypeBox v2インターフェース
3. ユーザー定義BoxNyashコード
- アプリケーション固有Box
```
## 🚀 実装計画
### Phase A: プラグイン版動作確認1週目
#### 1. 環境変数制御の実装
```rust
// src/mir/builder/utils.rs に追加
fn get_box_provider(box_type: &str) -> BoxProvider {
if env::var("NYASH_USE_PLUGIN_CORE_BOXES").is_ok() {
// プラグイン版を優先
if plugin_registry::exists(box_type) {
return BoxProvider::Plugin(box_type);
}
}
// nyrt内蔵にフォールバック
BoxProvider::Builtin(box_type)
}
```
#### 2. プラグイン版テスト
```bash
# Step 1: 単体テスト
NYASH_USE_PLUGIN_CORE_BOXES=1 ./target/release/nyash test_integer.nyash
NYASH_USE_PLUGIN_CORE_BOXES=1 ./target/release/nyash test_string.nyash
# Step 2: スモークテスト
NYASH_USE_PLUGIN_CORE_BOXES=1 ./tools/jit_smoke.sh
# Step 3: 包括テスト
NYASH_USE_PLUGIN_CORE_BOXES=1 cargo test
```
#### 3. パフォーマンス測定
```bash
# ベンチマーク
./target/release/nyash --benchmark core_vs_plugin.nyash
```
### Phase B: MIRビルダー統一2週目
#### 削除対象ファイル・行
1. **src/mir/builder/utils.rs** (行134-156)
- StringBox/IntegerBox等の特別な型推論削除
- すべてのBoxを統一的に扱う
2. **src/mir/builder.rs** (行407-424)
- build_new_expression の特別扱い削除
3. **src/mir/builder/builder_calls.rs**
- parse_type_name_to_mir の修正
#### 実装変更
```rust
// Before特別扱いあり
match class.as_str() {
"StringBox" => MirType::String,
"IntegerBox" => MirType::Integer,
// ...
}
// After統一
MirType::Box(class.to_string())
```
### Phase C: nyrt実装削除3週目
#### 削除対象
1. **crates/nyrt/src/lib.rs**
- StringBox関連: 約150行
- IntegerBox関連: 約50行
- ArrayBox関連: 約50行
- MapBox関連: 約50行
2. **crates/nyrt/src/plugin/**
- array.rs: 143行完全削除
- string.rs: 173行完全削除
3. **src/backend/llvm/compiler/codegen/instructions/newbox.rs**
- コアBox最適化パス削除
### Phase D: Nyashコード実装将来
```nyash
// apps/lib/core_boxes/string_box.nyash
box StringBox {
data: InternalString // 内部表現
birth(init_val) {
me.data = init_val or ""
}
length() {
return me.data.len()
}
concat(other) {
return me.data + other
}
substring(start, end) {
return me.data.slice(start, end)
}
}
```
```nyash
// apps/lib/core_boxes/integer_box.nyash
box IntegerBox {
value: i64
birth(init_val) {
me.value = init_val or 0
}
get() {
return me.value
}
set(new_val) {
me.value = new_val
}
}
```
## 📋 チェックリスト
### 準備段階
- [ ] プラグイン版の存在確認
- [x] nyash-string-plugin
- [x] nyash-integer-plugin
- [x] nyash-array-plugin
- [x] nyash-map-plugin
- [ ] nyash-bool-plugin作成必要
### Phase A
- [ ] 環境変数NYASH_USE_PLUGIN_CORE_BOXES実装
- [ ] フォールバック機構実装
- [ ] プラグイン版動作テスト
- [ ] パフォーマンス測定
### Phase B
- [ ] MIRビルダー特別扱い削除
- [ ] 型推論ロジック統一
- [ ] テスト通過確認
### Phase C
- [ ] nyrt StringBox実装削除
- [ ] nyrt IntegerBox実装削除
- [ ] nyrt ArrayBox実装削除
- [ ] nyrt MapBox実装削除
- [ ] LLVM最適化パス削除
### Phase D将来
- [ ] Nyashコード版StringBox実装
- [ ] Nyashコード版IntegerBox実装
- [ ] Nyashコード版ArrayBox実装
- [ ] Nyashコード版MapBox実装
## 🎯 成功基準
1. **機能的完全性**: すべてのテストがプラグイン版で通過
2. **パフォーマンス**: FFIオーバーヘッドが30%以内
3. **コード削減**: 700行以上の削減達成
4. **アーキテクチャ**: 3層→2層の簡潔化達成
## ⚠️ リスクと対策
| リスク | 影響 | 対策 |
|-------|------|------|
| FFIオーバーヘッド | 10-30%性能低下 | 静的リンク最適化 |
| プラグイン依存関係 | 配布複雑化 | 単一EXE生成.a静的リンク |
| デバッグ困難 | FFI境界問題 | 詳細ログ・トレース追加 |
## 📝 関連ドキュメント
- [MIR Call統一計画](mir-call-unification-master-plan.md)
- [Phase 15 全体計画](README.md)
- [セルフホスティング計画](self-hosting-plan.txt)
## 💡 技術的洞察
ChatGPTの「コアBox削除」提案は、以下の理由で革命的
1. **全チェック地獄の解消**: 3層構造による複雑な型チェック削除
2. **統一ABI**: すべてのBoxが同じインターフェース
3. **静的リンク活用**: .so→.a変換で性能維持
4. **セルフホスティング加速**: Nyashコード化への道筋
この統一により、Phase 15の80k→20k行削減目標に向けた重要な一歩となる。