Files
hakorune/docs/ideas/improvements/2025-08-25-unified-box-mir-vm.md
Moe Charm 391a095f4c Phase 9.78h: Stabilize MIR26 pipeline
- Add lib test to sync Core Instruction names with docs (INSTRUCTION_SET.md)
- Optimizer: Pass 0 normalizes legacy ops -> unified (TypeCheck/Cast->TypeOp,
  WeakNew/WeakLoad->WeakRef, BarrierRead/Write->Barrier)
- Optimizer: diagnostics for legacy ops; forbid via NYASH_OPT_DIAG_FORBID_LEGACY=1
- Runner: treat diagnostics (unlowered/legacy) as fatal when enabled
- Printer: unify legacy print style to TypeOp/WeakRef/Barrier
- Verifier: treat Phi inputs correctly (skip merge/dominance violations for Phi)
- Docs: update PHI_NORMALIZATION_PLAN; CURRENT_TASK: add risk note for op duplication
- Misc: PHI debugging/logs kept stable; snapshots still green
2025-08-26 06:30:01 +09:00

82 lines
2.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# MIR/VMでの統一Box処理の簡素化
Status: Pending
Created: 2025-08-25
Priority: High
Related-Code: src/backend/vm_instructions.rs::execute_boxcall()
## 現状の問題
VMのBoxCall実装で、InstanceBoxユーザー定義Boxだけ特別扱いされている
```rust
// 現在の実装
if let Some(inst) = arc_box.as_any().downcast_ref::<InstanceBox>() {
// ユーザー定義Boxは関数呼び出しに変換
let func_name = format!("{}.{}/{}", inst.class_name, method, args.len());
// ...
} else {
// ビルトイン/プラグインBoxは直接メソッド呼び出し
self.call_box_method(cloned_box, method, nyash_args)?
}
```
## 理想的な統一実装
すべてのBoxが統一的にデリゲーションできるなら、実装も統一できるはず
### 案1: すべてのBoxでメソッドディスパッチ統一
```rust
// すべてのBoxで同じインターフェース
pub trait UnifiedBox: NyashBox {
fn dispatch_method(&self, method: &str, args: Vec<Box<dyn NyashBox>>) -> Result<Box<dyn NyashBox>, String>;
}
// VMでの統一処理
let result = match &recv {
VMValue::BoxRef(arc_box) => {
arc_box.dispatch_method(method, nyash_args)?
}
_ => {
let box_value = recv.to_nyash_box();
box_value.dispatch_method(method, nyash_args)?
}
};
```
### 案2: メソッドレジストリの統合
```rust
struct UnifiedMethodRegistry {
// Box型名 → メソッド名 → 実装
methods: HashMap<String, HashMap<String, MethodImpl>>,
}
enum MethodImpl {
Native(fn(&dyn NyashBox, Vec<Box<dyn NyashBox>>) -> Result<Box<dyn NyashBox>, String>),
MirFunction(String), // MIR関数名
Plugin(PluginMethodId),
}
```
## メリット
1. **コードの簡素化**
- Box型による分岐が不要
- 統一的なメソッドディスパッチ
2. **保守性の向上**
- 新しいBox型追加が容易
- ビルトイン/プラグイン/ユーザー定義の区別が不要
3. **パフォーマンス**
- 統一的な最適化が可能
- メソッドキャッシュの効率化
## 実装タイミング
- [ ] Phase 10での大規模リファクタリング時
- [ ] 新しいBox型追加時
- [ ] パフォーマンス最適化時
## 関連する改善案
- InstanceBoxの廃止すべてのBoxを統一表現
- MIRでのBox型情報の統一
- プラグインシステムの簡素化