fix(phase-4.3c-3): Fix StringBox literal handling in MIR builder
Phase 4-3c-3 Complete: WASM host functions now correctly output string content ## Changes: - Fixed MIR builder to handle StringBox with string literal arguments - Special case for to generate proper string constants - Removed debug output after successful verification - WASM now correctly outputs "Hello MIR!" instead of "StringBox" ## Test Results: - MIR generation: ✅ Generates correctly - WASM compilation: ✅ String data correctly placed at offset 4096 - WASM execution: ✅ Outputs "Hello MIR\!" as expected ## Technical Details: - Modified build_new_expression() to detect StringBox with literal arguments - Generates Const instruction with actual string content - Host function reads StringBox memory layout correctly This completes the WASM string output functionality for Phase 4. 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
277
docs/archive/mir-docs-old/mir-instruction-set.md
Normal file
277
docs/archive/mir-docs-old/mir-instruction-set.md
Normal file
@ -0,0 +1,277 @@
|
||||
# 🔧 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命令)**
|
||||
```mir
|
||||
// 統合命令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
|
||||
```
|
||||
|
||||
#### **演算子統合戦略**
|
||||
```rust
|
||||
// 現在分離→統合へ
|
||||
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命令)**
|
||||
```mir
|
||||
// 条件分岐
|
||||
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命令)**
|
||||
```mir
|
||||
// 関数呼び出し(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命令)**
|
||||
```mir
|
||||
// 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命令)**
|
||||
```mir
|
||||
// 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命令)**
|
||||
```mir
|
||||
// GC・最適化ポイント
|
||||
Safepoint
|
||||
// 使用例: safepoint # GCタイミング・デバッグブレークポイント
|
||||
```
|
||||
|
||||
## 🔄 **Tier-1: 高度最適化(5命令)**
|
||||
|
||||
### **必要な場合のみ追加**
|
||||
```mir
|
||||
// 型変換(最適化パス用)
|
||||
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個削除)**
|
||||
```rust
|
||||
// 配列操作 → 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実装例**
|
||||
```rust
|
||||
// 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週間)**
|
||||
```rust
|
||||
// 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週間)**
|
||||
```rust
|
||||
// 1. BinOp統合(Compare削除)
|
||||
// 2. Load/Store統合(ArrayGet/ArraySet削除)
|
||||
// 3. 複雑操作のintrinsic移行
|
||||
```
|
||||
|
||||
### **段階3: Bus命令実装(1週間)**
|
||||
```rust
|
||||
// 1. Send/Recv命令追加
|
||||
// 2. Bus-elision基盤
|
||||
// 3. P2PBox統合
|
||||
```
|
||||
|
||||
### **段階4: 検証・テスト(1週間)**
|
||||
```rust
|
||||
// 1. Golden dump更新
|
||||
// 2. 全バックエンド互換確認
|
||||
// 3. 性能回帰チェック
|
||||
```
|
||||
|
||||
## ✅ **Phase 8.4完了基準**
|
||||
|
||||
### **技術要件**
|
||||
- [ ] **命令数20個以内**: ChatGPT5推奨準拠
|
||||
- [ ] **intrinsic基盤**: 拡張可能な逃がし仕組み
|
||||
- [ ] **Bus命令**: 分散・非同期一次市民化
|
||||
- [ ] **全バックエンド動作**: interp/vm/wasm対応
|
||||
|
||||
### **品質要件**
|
||||
- [ ] **Golden dump更新**: 新命令セットで標準更新
|
||||
- [ ] **互換テスト通過**: 全バックエンド同一出力
|
||||
- [ ] **性能維持**: 280倍WASM高速化維持
|
||||
- [ ] **回帰テストPASS**: 既存機能への影響なし
|
||||
|
||||
---
|
||||
|
||||
## 📚 **関連ドキュメント**
|
||||
|
||||
- **MIR設計思想**: [mir-reference.md](mir-reference.md)
|
||||
- **互換性契約**: [portability-contract.md](portability-contract.md)
|
||||
- **テスト仕様**: [golden-dump-testing.md](golden-dump-testing.md)
|
||||
- **現在実装**: [../../../src/mir/instruction.rs](../../../src/mir/instruction.rs)
|
||||
|
||||
---
|
||||
|
||||
*最終更新: 2025-08-14 - ChatGPT5「太り過ぎ」対策完全設計*
|
||||
|
||||
*MIR最小コア = Nyash「全バックエンド統一」の技術的基盤*
|
||||
190
docs/archive/mir-docs-old/mir-reference.md
Normal file
190
docs/archive/mir-docs-old/mir-reference.md
Normal file
@ -0,0 +1,190 @@
|
||||
# 🤖 Nyash MIR (Mid-level Intermediate Representation) - Complete Reference
|
||||
|
||||
*ChatGPT5アドバイス基盤設計・Everything is Box最適化対応*
|
||||
|
||||
## 🎯 Nyashのユニークな点(短く)
|
||||
|
||||
### 🌟 **4つの革新的特徴**
|
||||
|
||||
1. **Ownership-Forest + weak**: GCなしで確定破棄、学習コスト低(Rustより軽い)
|
||||
2. **Effect注釈**: pure/mut/io が MIR に入り、Busを消せる/elide基盤に
|
||||
3. **Busを命令系に内蔵**: 分散・非同期が"あと付け"じゃなく言語仕様の一次市民
|
||||
4. **バックエンド設計が最初から同居**: Interp→VM→JIT/AOT/WASMを同じMIRで回せる
|
||||
|
||||
### 🚀 **差別化ポイント**
|
||||
```bash
|
||||
# 全バックエンド統一実行
|
||||
nyash --target interp program.nyash # デバッグ
|
||||
nyash --target vm program.nyash # 高速実行
|
||||
nyash --target wasm program.nyash # Web配布
|
||||
nyash --target aot-rust program.nyash # ネイティブ
|
||||
nyash --target jit-cranelift program.nyash # JIT
|
||||
```
|
||||
|
||||
それぞれに**ベンチ + 互換テスト**が通る統一設計
|
||||
|
||||
## ⚠️ "化け物"への落とし穴(と対策)
|
||||
|
||||
### 🚨 **現在の問題状況**
|
||||
- **MIRが太り過ぎ**: 35命令(ChatGPT5推奨20命令の175%)
|
||||
- **仕様が揺れる可能性**: 互換テスト未整備
|
||||
- **バックエンドごとの差**: 効果・所有の最低保証未定義
|
||||
|
||||
### ✅ **ChatGPT5対策実装**
|
||||
1. **命令20個以内 + intrinsic逃がし**で開始
|
||||
2. **MIRの互換テスト**(golden dump)&ポータビリティ契約を先に切る
|
||||
3. **効果&所有の"最低保証"**を定義(Tier-0)
|
||||
|
||||
## 🔧 **いま決めておくと強い"3点セット"**
|
||||
|
||||
### 1️⃣ **MIR最小コア(20命令以内)**
|
||||
|
||||
#### **Tier-0: 絶対必要コア(15命令)**
|
||||
```mir
|
||||
// 算術・比較
|
||||
Const { dst, value } // 定数
|
||||
BinOp { dst, op, lhs, rhs } // 二項演算(算術・論理)
|
||||
Compare { dst, op, lhs, rhs } // 比較演算
|
||||
|
||||
// 制御フロー
|
||||
Branch { condition, then_bb, else_bb } // 条件分岐
|
||||
Jump { target } // 無条件ジャンプ
|
||||
Return { value? } // 関数リターン
|
||||
Phi { dst, inputs } // SSA合流
|
||||
|
||||
// 関数・メソッド
|
||||
Call { dst?, func, args, effects } // 関数呼び出し
|
||||
BoxCall { dst?, box_val, method, args, effects } // メソッド呼び出し
|
||||
|
||||
// Everything is Box基本操作
|
||||
NewBox { dst, box_type, args } // Box生成
|
||||
Load { dst, ptr } // フィールド読み取り
|
||||
Store { value, ptr } // フィールド書き込み
|
||||
|
||||
// Bus(分散・非同期一次市民)
|
||||
Send { bus, message, effects } // Bus送信
|
||||
Recv { dst, bus, effects } // Bus受信
|
||||
|
||||
// Effect制御
|
||||
Safepoint // GC・最適化ポイント
|
||||
```
|
||||
|
||||
#### **Tier-1: 高度最適化(5命令)**
|
||||
```mir
|
||||
Cast { dst, value, target_type } // 型変換(最適化用)
|
||||
TypeCheck { dst, value, expected_type } // 動的型チェック
|
||||
WeakNew { dst, box_val } // weak参照(Forest用)
|
||||
WeakLoad { dst, weak_ref } // weak読み取り
|
||||
Intrinsic { dst?, name, args, effects } // intrinsic逃がし
|
||||
```
|
||||
|
||||
### 2️⃣ **Portability Contract v0**
|
||||
|
||||
#### **決定的破棄保証**
|
||||
```rust
|
||||
// 強参照のみ伝播
|
||||
pub struct OwnershipRule {
|
||||
strong_propagation: true, // 強参照は破棄連鎖
|
||||
weak_non_propagation: true, // weak参照は非伝播
|
||||
deterministic_finalization: true, // 確定的破棄順序
|
||||
}
|
||||
```
|
||||
|
||||
#### **Effect意味論**
|
||||
```rust
|
||||
pub enum EffectContract {
|
||||
Pure, // 副作用なし→最適化可能
|
||||
Mut, // メモリ変更→順序保証必要
|
||||
Io, // I/O操作→Bus統合
|
||||
Bus, // 分散通信→elision対象
|
||||
}
|
||||
```
|
||||
|
||||
#### **weakは非伝播+生存チェック**
|
||||
```mir
|
||||
// weak生存チェックは必須
|
||||
%alive = weak_load %weak_ref
|
||||
br %alive -> %use_bb, %null_bb
|
||||
```
|
||||
|
||||
### 3️⃣ **互換テスト仕様**
|
||||
|
||||
#### **Golden Dump検証**
|
||||
```bash
|
||||
# MIR出力の一致検証
|
||||
nyash --dump-mir program.nyash > expected.mir
|
||||
nyash --dump-mir program.nyash > actual.mir
|
||||
diff expected.mir actual.mir # 0でなければ回帰
|
||||
|
||||
# 全バックエンド同一出力
|
||||
nyash --target interp program.nyash > interp.out
|
||||
nyash --target vm program.nyash > vm.out
|
||||
nyash --target wasm program.nyash > wasm.out
|
||||
diff interp.out vm.out && diff vm.out wasm.out
|
||||
```
|
||||
|
||||
#### **Bus-elision検証**
|
||||
```bash
|
||||
# Bus最適化のon/off切り替え
|
||||
nyash --elide-bus program.nyash > optimized.out
|
||||
nyash --no-elide-bus program.nyash > reference.out
|
||||
diff optimized.out reference.out # 結果は同一であるべき
|
||||
```
|
||||
|
||||
## 📊 **現在の実装状況**
|
||||
|
||||
### ✅ **完成済み**
|
||||
- SSA-form MIR基盤(ChatGPT5設計)
|
||||
- Effect追跡システム
|
||||
- 3バックエンド(Interp/VM/WASM)
|
||||
- 280倍WASM高速化実証
|
||||
|
||||
### 🚧 **緊急改善必要**
|
||||
- [ ] **命令数削減**: 35個→20個(intrinsic逃がし)
|
||||
- [ ] **Bus命令実装**: Send/Recv(分散一次市民化)
|
||||
- [ ] **互換テスト**: Golden dump自動化
|
||||
- [ ] **Portability Contract**: v0仕様策定
|
||||
|
||||
### 🎯 **Phase 8.4実装推奨**
|
||||
```bash
|
||||
# Bus統合MIR設計
|
||||
Bus { dst?, target, operation, args, effects }
|
||||
|
||||
# Bus-elision最適化
|
||||
--elide-bus / --no-elide-bus フラグ実装
|
||||
|
||||
# 性能数値提示(WASM速いデータ活用)
|
||||
Bus-elision ON: 280倍高速化(現在実証済み)
|
||||
Bus-elision OFF: 分散通信フルサポート
|
||||
```
|
||||
|
||||
## 🚀 **これで "全部に変換できる" を名乗れる**
|
||||
|
||||
### **統一コマンド体系**
|
||||
```bash
|
||||
nyash --target interp program.nyash # インタープリター
|
||||
nyash --target vm program.nyash # 仮想マシン
|
||||
nyash --target wasm program.nyash # WebAssembly
|
||||
nyash --target aot-rust program.nyash # AOTネイティブ
|
||||
nyash --target jit-cranelift program.nyash # JITコンパイル
|
||||
```
|
||||
|
||||
### **品質保証体系**
|
||||
- **ベンチマーク**: 各ターゲットの性能測定
|
||||
- **互換テスト**: 同一入力→同一出力検証
|
||||
- **回帰テスト**: Golden dump差分チェック
|
||||
|
||||
---
|
||||
|
||||
## 📚 **関連ドキュメント**
|
||||
|
||||
- **実装仕様**: [MIR命令セット詳細](mir-instruction-set.md)
|
||||
- **最適化戦略**: [Everything is Box最適化](optimization-strategies.md)
|
||||
- **互換性**: [Portability Contract v0](portability-contract.md)
|
||||
- **テスト**: [Golden Dump検証システム](golden-dump-testing.md)
|
||||
|
||||
---
|
||||
|
||||
*最終更新: 2025-08-14 - ChatGPT5アドバイス基盤設計完了*
|
||||
|
||||
*「Everything is Box」哲学 × MIR最小コア = Nyashの差別化核心*
|
||||
Reference in New Issue
Block a user