Files
hakorune/docs/説明書/reference/mir-instruction-set.md
Moe Charm c7365d3dc9 feat(merge): integrate Phase 8.3 WASM Box Operations + Benchmark System
🎉 Successful merge of Copilot and Claude implementations:

**Copilot Contributions (Phase 8.3):**
-  WASM Box Operations: RefNew/RefGet/RefSet complete implementation
-  Memory management: BoxLayout, MemoryManager with standard types
-  WASM codegen: Box allocation, field access, type-safe operations
-  Runtime support: malloc, heap management, type ID system

**Claude Contributions (Benchmark System):**
-  Comprehensive benchmark framework (src/benchmarks.rs)
-  CLI integration: --benchmark, --iterations, --output options
-  3-backend performance comparison (Interpreter/VM/WASM)
-  280x WASM speedup verification system
-  Golden dump testing infrastructure

**Unified Features:**
- 🔧 execute_wasm_mode: Supports both output file and stdout
- 🔧 CLI arguments: All options preserved and functional
- 🔧 Error handling: Improved MIR verification messages
- 🔧 Build system: All modules properly integrated

**Next Steps Ready:**
- 📊 MIR diet planning (35→20 instructions)
- 🚀 Phase 8.4: AOT WASM native compilation
- 🧪 Golden dump automation

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-14 08:12:36 +09:00

8.5 KiB
Raw Blame History

🔧 Nyash MIR Instruction Set - Complete Reference

ChatGPT5設計・20命令以内コア + intrinsic逃がし戦略

🎯 現在の実装状況分析

⚠️ 「太り過ぎ」問題確認

  • 現在実装: 35命令175%超過)
  • ChatGPT5推奨: 20命令以内
  • 対策: intrinsic逃がし + Tier-0コア集約

📊 命令分類・整理必要度

分類 現在命令数 推奨数 優先度 対策
基本演算 8個 3個 🔴 緊急 BinOp統合
制御フロー 4個 4個 適正 維持
メモリ 12個 3個 🔴 緊急 intrinsic逃がし
Box操作 6個 2個 🟡 要整理 統合検討
Future/Weak 5個 2個 🟡 要整理 段階実装

🔧 ChatGPT5推奨: Tier-0 Core (15命令)

1. 算術・比較3命令

// 統合命令1: 定数ロード
Const { dst: ValueId, value: ConstValue }
// 使用例: %1 = const 42, %2 = const "hello", %3 = const null

// 統合命令2: 二項演算(算術・論理・比較すべて)
BinOp { dst: ValueId, op: BinaryOp, lhs: ValueId, rhs: ValueId }
// 使用例: %4 = %1 add %2, %5 = %1 eq %2, %6 = %1 and %2

// 統合命令3: 単項演算
UnaryOp { dst: ValueId, op: UnaryOp, operand: ValueId }
// 使用例: %7 = not %5, %8 = neg %1

演算子統合戦略

// 現在分離→統合へ
pub enum BinaryOp {
    // 算術現在のBinOp
    Add, Sub, Mul, Div, Mod,
    
    // 比較現在のCompare統合
    Eq, Ne, Lt, Le, Gt, Ge,
    
    // 論理現在のBinOp統合
    And, Or, BitAnd, BitOr, BitXor, Shl, Shr,
}

// 3つの別命令 → 1つのBinOp に統合
// BinOp + Compare + LogicalOp → BinOp

2. 制御フロー4命令

// 条件分岐
Branch { condition: ValueId, then_bb: BasicBlockId, else_bb: BasicBlockId }

// 無条件ジャンプ  
Jump { target: BasicBlockId }

// 関数リターン
Return { value: Option<ValueId> }

// SSA合流必須
Phi { dst: ValueId, inputs: Vec<(BasicBlockId, ValueId)> }

3. 関数・メソッド2命令

// 関数呼び出しstatic関数・ビルトイン
Call { dst: Option<ValueId>, func: ValueId, args: Vec<ValueId>, effects: EffectMask }

// Box メソッド呼び出し(動的ディスパッチ)
BoxCall { dst: Option<ValueId>, box_val: ValueId, method: String, args: Vec<ValueId>, effects: EffectMask }

4. Everything is Box基本3命令

// Box生成統合
NewBox { dst: ValueId, box_type: String, args: Vec<ValueId> }
// 使用例: %obj = new_box "StringBox"("hello"), %arr = new_box "ArrayBox"()

// フィールド読み取り(統合)
Load { dst: ValueId, ptr: ValueId, field: Option<String> }
// 使用例: %val = load %obj.field, %item = load %arr[%idx]

// フィールド書き込み(統合)  
Store { value: ValueId, ptr: ValueId, field: Option<String> }
// 使用例: store %val -> %obj.field, store %item -> %arr[%idx]

5. Bus分散・非同期一次市民2命令

// Bus送信分散通信の核心
Send { bus: ValueId, message: ValueId, effects: EffectMask }
// 使用例: send %p2p_bus, %message effects=[BUS]

// Bus受信
Recv { dst: ValueId, bus: ValueId, effects: EffectMask }
// 使用例: %msg = recv %p2p_bus effects=[BUS]

6. 最適化・デバッグ1命令

// GC・最適化ポイント
Safepoint
// 使用例: safepoint  # GCタイミング・デバッグブレークポイント

🔄 Tier-1: 高度最適化5命令

必要な場合のみ追加

// 型変換(最適化パス用)
Cast { dst: ValueId, value: ValueId, target_type: MirType }

// 動的型チェック(安全性)
TypeCheck { dst: ValueId, value: ValueId, expected_type: String }

// weak参照Ownership-Forest用
WeakNew { dst: ValueId, box_val: ValueId }
WeakLoad { dst: ValueId, weak_ref: ValueId }

// 何でも逃がし(複雑操作用)
Intrinsic { dst: Option<ValueId>, name: String, args: Vec<ValueId>, effects: EffectMask }

🛠️ intrinsic逃がし戦略

現在35命令→20命令削減計画

intrinsicに移行する命令15個削除

// 配列操作 → intrinsic
// 現在: ArrayGet, ArraySet
// 移行後: intrinsic("array_get", [array, index]) 
//        intrinsic("array_set", [array, index, value])

// デバッグ → intrinsic  
// 現在: Debug, Print, Nop
// 移行後: intrinsic("debug", [value, message])
//        intrinsic("print", [value])

// 例外処理 → intrinsic
// 現在: Throw, Catch
// 移行後: intrinsic("throw", [exception])
//        intrinsic("catch", [exception_type])

// 参照詳細 → intrinsic
// 現在: RefNew, RefGet, RefSet, Copy
// 移行後: intrinsic("ref_new", [box])
//        intrinsic("ref_get", [ref, field])
//        intrinsic("ref_set", [ref, field, value])

// バリア → intrinsic
// 現在: BarrierRead, BarrierWrite  
// 移行後: intrinsic("barrier_read", [ptr])
//        intrinsic("barrier_write", [ptr])

// Future → intrinsic
// 現在: FutureNew, FutureSet, Await
// 移行後: intrinsic("future_new", [value])
//        intrinsic("future_set", [future, value])  
//        intrinsic("await", [future])

intrinsic実装例

// src/mir/intrinsics.rs
pub fn execute_intrinsic(name: &str, args: &[ValueId], effects: EffectMask) -> Result<ValueId, String> {
    match name {
        "print" => {
            let value = get_value(args[0]);
            println!("{}", value);
            Ok(ValueId::void())
        }
        
        "array_get" => {
            let array = get_value(args[0]);
            let index = get_value(args[1]);
            Ok(array.get_element(index)?)
        }
        
        "future_new" => {
            let value = get_value(args[0]);
            let future = FutureBox::new_with_value(value);
            Ok(ValueId::from_box(future))
        }
        
        _ => Err(format!("Unknown intrinsic: {}", name))
    }
}

📊 削減効果・期待値

複雑性削減

指標 削減前 削減後 効果
命令数 35個 20個 43%削減
コア実装 分散 統合 保守性向上
バックエンド負荷 35×3=105 20×3=60 43%削減

拡張性向上

  • 新機能追加: intrinsicで実験→安定したらcore昇格
  • バックエンド追加: core 20命令のみ実装すれば基本動作
  • 最適化: intrinsic は必要に応じて最適化・無視可能

🎯 実装戦略・Phase 8.4

段階1: intrinsic基盤1週間

// 1. Intrinsic命令追加
Intrinsic { dst: Option<ValueId>, name: String, args: Vec<ValueId>, effects: EffectMask }

// 2. intrinsic実行エンジン
impl IntrinsicExecutor {
    fn execute(&self, name: &str, args: &[ValueId]) -> Result<ValueId, String>
}

// 3. 基本intrinsic実装
// print, debug, array_get, array_set

段階2: 命令統合1週間

// 1. BinOp統合Compare削除
// 2. Load/Store統合ArrayGet/ArraySet削除
// 3. 複雑操作のintrinsic移行

段階3: Bus命令実装1週間

// 1. Send/Recv命令追加
// 2. Bus-elision基盤
// 3. P2PBox統合

段階4: 検証・テスト1週間

// 1. Golden dump更新
// 2. 全バックエンド互換確認  
// 3. 性能回帰チェック

Phase 8.4完了基準

技術要件

  • 命令数20個以内: ChatGPT5推奨準拠
  • intrinsic基盤: 拡張可能な逃がし仕組み
  • Bus命令: 分散・非同期一次市民化
  • 全バックエンド動作: interp/vm/wasm対応

品質要件

  • Golden dump更新: 新命令セットで標準更新
  • 互換テスト通過: 全バックエンド同一出力
  • 性能維持: 280倍WASM高速化維持
  • 回帰テストPASS: 既存機能への影響なし

📚 関連ドキュメント


最終更新: 2025-08-14 - ChatGPT5「太り過ぎ」対策完全設計

MIR最小コア = Nyash「全バックエンド統一」の技術的基盤