Phase 11-12: LLVM backend initial, semantics layer, plugin unification

Major changes:
- LLVM backend initial implementation (compiler.rs, llvm mode)
- Semantics layer integration in interpreter (operators.rs)
- Phase 12 plugin architecture revision (3-layer system)
- Builtin box removal preparation
- MIR instruction set documentation (26→Core-15 migration)
- Cross-backend testing infrastructure
- Await/nowait syntax support

New features:
- LLVM AOT compilation support (--backend llvm)
- Semantics layer for interpreter→VM flow
- Tri-backend smoke tests
- Plugin-only registry mode

Bug fixes:
- Interpreter plugin box arithmetic operations
- Branch test returns incorrect values

Documentation:
- Phase 12 README.md updated with new plugin architecture
- Removed obsolete NYIR proposals
- Added LLVM test programs documentation

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Moe Charm
2025-09-01 23:44:34 +09:00
parent fff9749f47
commit 11506cee3b
196 changed files with 10955 additions and 380 deletions

View File

@ -1,10 +1,11 @@
# MIR 35→26命令削減: 詳細分析・移行戦略
# MIR 35→26→15命令削減: 詳細分析・移行戦略
*実装ベース完全マッピング - 2025年8月17日版*
*実装ベース完全マッピング - 2025年8月17日版*
**注意**: このドキュメントは35→26命令への移行計画を記録しています。現在はさらに15命令に削減済みです。
## 🔍 **現在実装35命令 vs ChatGPT5仕様26命令の完全マッピング**
## 🔍 **過去の実装35命令 vs 26命令仕様の完全マッピング(歴史的記録)**
### **維持する命令 (既存実装 → 26命令仕様)**
### **維持する命令 (35命令実装 → 26命令仕様への移行計画)**
| 現在実装 | 26命令仕様 | 効果 | 変更 |
|----------|------------|------|------|

View File

@ -1,10 +0,0 @@
# NyIR ドキュメント索引
- 仕様(骨子): `spec.md`
- フェーズ計画Phase 9.10: `../予定/native-plan/issues/phase_9_10_nyir_spec.md`
- BIDサンプル: `bid_samples/`console.yaml / canvas.yaml
- 計画全体Copilot用フルテキスト 9.7以降): `../予定/native-plan/copilot_issues.txt`
- 関連: ABI/BIDドラフトは `../予定/native-plan/box_ffi_abi.md`
補足:
- NyIR Core25命令は凍結。拡張は NyIR-Extexceptions/concurrency/atomicsで段階導入。

View File

@ -1,26 +0,0 @@
version: 0
interfaces:
- name: env.canvas
box: Canvas
methods:
- name: fillRect
params:
- { string: canvas_id }
- { i32: x }
- { i32: y }
- { i32: w }
- { i32: h }
- { string: color }
returns: void
effect: io
- name: fillText
params:
- { string: canvas_id }
- { string: text }
- { i32: x }
- { i32: y }
- { string: font }
- { string: color }
returns: void
effect: io

View File

@ -1,10 +0,0 @@
version: 0
interfaces:
- name: env.console
box: Console
methods:
- name: log
params:
- { string: msg }
returns: void
effect: io

View File

@ -1,70 +0,0 @@
# Phase 9.10: NyIR v1 仕様・フォーマット・検証器公開IRの確立
目的
- NyashのMIRを公開IRNyIR v1として凍結し、あらゆるフロントエンド/バックエンドの共通契約とする。
- 仕様・テキスト/バイナリフォーマット・厳格検証器・ツール群を整備し、移植性と一貫性を保証する。
成果物Deliverables
- 仕様書: `docs/nyir.md`(命令仕様/効果/未定義なし/検証ルール/等価変換指針)
- フォーマット: `.nyir`(テキスト), `.nybc`(バイナリ)
- 検証器: `nyir-verify`CLI/ライブラリ)
- 変換/実行ツール:
- `nyashel -S`Nyash→NyIRダンプ
- `nyir-run`NyIRインタプリタ
- 参考: `nyir-ll`NyIR→LLVM IR、Phase 10で拡張
- Golden NyIR: `golden/*.nyir`代表サンプルを固定、CIで全バックエンド一致を検証
仕様の要点NyIR v1
- 命令セット: 25命令Tier-0/1/2を凍結
- Tier-0: Const, BinOp, Compare, Branch, Jump, Phi, Call, Return
- Tier-1: NewBox, BoxFieldLoad, BoxFieldStore, BoxCall, Safepoint, RefGet, RefSet, WeakNew, WeakLoad, WeakCheck, Send, Recv
- Tier-2: TailCall, Adopt, Release, MemCopy, AtomicFence
- 効果Effects: pure / mut / io / control再順序化規則を明文化
- 所有フォレスト: 強参照の森strong in-degree ≤ 1、強循環禁止、weakは非伝播
- Weak: 失効時の挙動を決定WeakLoad=null / WeakCheck=false、世代タグ設計を想定
- Bus: ローカルは順序保証、リモートは at-least-onceまたは選択可能
- 未定義動作なし: 各命令の事前条件/失敗時挙動を明示
- バージョニング: `nyir{major.minor}`、featureビットで拡張告知
テキスト形式(.nyir
- 人間可読・差分レビュー向け
- 構造: moduleヘッダ / const pool / functionsblocks, instrs
- コメント/メタデータ/featureビットを扱える簡潔な構文
バイナリ形式(.nybc
- セクション化: header / features / const pool / functions / metadata
- エンコード: LEB128等の可変長を採用、識別子はstring table参照
- 将来の後方互換に備えた保守的設計
検証器Verifier
- 検査: 所有森/強循環/weak規則/効果整合/到達性/終端性/Phi入力整合
- 失敗時は明確なエラーを返し、ロード拒否
- CLI/ライブラリの二態(コンパイラ/実行系どちらからも利用)
Golden / Differential テスト
- `golden/*.nyir` を固定し、interp/vm/wasm/jit/llvm順次で出力一致をCIで検証
- 弱失効/分割fini/境界条件系を重点的に含める
タスクCopilot TODO
1) 仕様スケルトン: `docs/nyir.md` ひな形生成(命令/効果/検証/等価変換の目次)
2) `.nyir` パーサ/プリンタ(最小)
3) `.nybc` エンコーダ/デコーダ(最小)
4) `nyir-verify`(所有森/効果/Phi/到達/終端の基本チェック)
5) `nyashel -S` をNyIR出力対応に既存MIRダンプ経路から移行
6) Goldenサンプル作成3〜5本CIワークフロー雛形
受け入れ基準Acceptance
- 代表サンプルが `.nyir` で表現・検証・実行可能(`nyir-run`
- `.nybc` 読み書き往復で等価
- CIでinterp/vm/wasmの結果一致最小ケース
依存/関連
- 8.5: 25命令の確定仕様
- 9.7: ExternCall/ABINyIRにもmethodードor外部呼を表現。v1ではExternCallは拡張セクションで可
- 10.x: NyIR→LLVM IR別Phase
リスク
- 仕様凍結の硬直化 → 拡張はfeatureビット拡張セクションへ
- 実装の重複 → Verifier/フォーマットは共有ライブラリ化
最終更新: 2025-08-14

View File

@ -1,126 +0,0 @@
# NyIR v1 Specification (Draft Skeleton)
Purpose
- Define NyIR (public intermediate representation) as the portable contract for all frontends/backends.
- Freeze the 25-instruction set, effects, ownership forest, weak semantics, and bus contract.
Status
- Version: nyir1.0 (draft)
- Change policy: backward-compatible extensions via feature bits; no undefined behavior.
1. Instruction Set (26) — Semantic Summary
- Tier-0: Const, BinOp, Compare, Branch, Jump, Phi, Call, Return
- Tier-1: NewBox, BoxFieldLoad, BoxFieldStore, BoxCall, ExternCall, Safepoint, RefGet, RefSet, WeakNew, WeakLoad, WeakCheck, Send, Recv
- Tier-2: TailCall, Adopt, Release, MemCopy, AtomicFence
For each instruction (to be filled):
- Semantics: preconditions, side-effects, failure behavior
- Effects: pure/mut/io/control
- Verification: structural/formal checks
- Example: minimal snippet
2. Effects Model
- pure: no observable side effects; can reorder with pure
- mut: mutates owned state; preserve order per resource
- io: interacts with outside world; preserve program order
- control: affects control flow/terminators
3. Ownership Forest
- Strong edges form a forest (strong in-degree ≤ 1).
- No strong cycles.
- Weak edges do not propagate ownership.
- Adopt/Release rules; Safepoint and split-fini notes.
4. Weak Semantics
- Representation: {ptr, generation}
- WeakLoad: returns null if generation mismatch
- WeakCheck: returns false if invalid
- O(1) on-access validation
5. Bus Contract
- Local: in-order delivery
- Remote: at-least-once (or selectable); specify ordering/dup policy
6. Formats
- Text .nyir: human-readable; module header, features, const pool, functions (blocks, instrs)
- Binary .nybc: sectioned; header, features, const pool, functions, metadata; varint encodings
- Feature bits and extension sections for evolution
7. Verification
- Program well-formedness: reachability, termination, dominance
- Phi input consistency
- Effects ordering constraints
- Ownership forest & weak/generation rules
- Bus safety checks
8. External Calls (Core)
- ExternCall: Interface to external libraries as Everything is Box principle
- Format: ExternCall { dst, iface_name, method_name, args }
- Effect annotation required (pure/mut/io/control)
- BID (Box Interface Definition) provides external API contracts
9. Mapping Guidelines
- WASM: imports, memory rules, (ptr,len) strings
- VM: function table mapping
- LLVM: declare signatures; effect to attributes (readonly/readnone, etc.)
10. Golden/Differential Testing
- Golden .nyir corpus; cross-backend consistency (interp/vm/wasm/llvm)
11. Versioning & Compatibility
- nyir{major.minor}; feature negotiation and safe fallback
12. Glossary
- Box, Effect, Ownership forest, Weak, Safepoint, Bus, Feature bit
Appendix
- Minimal examples (to be added)
- Rationale notes
------------------------------------------------------------
NyIR-Ext拡張セット
------------------------------------------------------------
目的
- NyIR Core26命令は基本セマンティクス凍結。外部世界接続ExternCallを含む基本機能確立。
- 拡張は言語固有機能に限定:「例外」「軽量並行/非同期」「アトミック」の3領域を段階追加。
- Core機能: すべての言語で必要な基本セマンティクス制御フロー・Box操作・外部呼び出し
- Extension機能: 特定言語でのみ必要な高級機能(例外処理・並行処理・アトミック操作)
Ext-1: 例外/アンワインドexceptions
- 命令(案):
- Throw(value)
- TryBegin
- TryEnd(handlers=[type:block])
- 効果: control脱出。mut/io と混在する場合は当該効果も従属。
- 検証: Try の整合(入れ子/終端)、未捕捉 Throw のスコープ明示、no-throw 関数属性の尊重。
- バックエンド指針:
- LLVM: 言語EH or setjmp/longjmp 系へ lower
- WASM: v0は例外未使用なら「エラー戻り値」へ降格no-exception モード)
Ext-2: 軽量並行/非同期concurrency
- 命令(案):
- Spawn(fn, args...)
- Join(handle)
- Await(task|event) // または Wait
- 効果: ioスケジューラ相互作用 control待機・解除
- 検証: Join/Spawn のライフサイクル均衡、待機の整合(任意でデッドロック静的ヒント)。
- バックエンド指針:
- LLVM: pthread/std::thread/独自ランタイム
- WASM: スレッド未使用ならランループ/Promise/MessageChannel 等で近似
Ext-3: アトミックatomics
- 命令(案):
- AtomicRmw(op, addr, val, ordering=seq_cst)
- CAS(addr, expect, replace, ordering=seq_cst)
- 既存の AtomicFence は維持。
- 効果: mutアトミック副作用。必要に応じて atomic/volatile フラグを effect/属性で付与。
- バックエンド指針:
- LLVM: `atomicrmw`, `cmpxchg`
- WASM: Threads 提案が前提。未サポート環境では未実装 or 疑似ロック(デモ用途)
備考
- 可変引数は slice 表現で代替IR 追加不要)。
- クロージャ捕捉は Box + env フィールド + BoxCall で表現IR 追加不要)。
- 動的言語(辞書/配列/プロトタイプは標準Boxstdで受けるIR 追加不要)。
- 関数属性に「エラー戻り値モード/no-exception」などのメタを付与し、例外禁止環境へも対応する。

View File

@ -1,44 +0,0 @@
# NyIR を共通交換フォーマットにする計画All Languages → NyIR → All Targets
目的Goal
- あらゆる言語のプログラムを NyIR= Nyash MIR の公開版へ落とし、そこから任意の実行形態WASM/LLVM/VM/他言語)に変換する。
- 最適化は二の次。まずは意味保存semantics-preservingを最優先で実現し、可搬性と一貫性を担保する。
中核方針Core Policy
- NyIR Core26命令は基本セマンティクス凍結。ExternCallによる外部世界接続を含む。
- 拡張は NyIR-Extexceptions/concurrency/atomicsで言語固有機能を段階導入。
- Everything is Box哲学: 外部ライブラリもBIDによりBox統一インターフェースで利用。
- 仕様の正本は `docs/nyir/spec.md` に集約CoreExt
必要拡張Minimal Additions
- 例外/アンワインドThrow/TryBegin/TryEnd
- 軽量並行/非同期Spawn/Join/Await
- アトミックAtomicRmw/CAS + ordering
→ 詳細は `docs/nyir/spec.md` の NyIR-Ext 参照
フロントエンド指針Language → NyIR
- C/C++/Rust: 既存IRLLVM IR経由または専用パーサでサブセットから対応
- 例外→NyIR-Ext exceptions or エラー戻り値
- スレッド→Spawn/Join、atomic→CAS/RMW
- Java/Kotlin: JVM bytecode から構造復元→NyIR例外/スレッド/同期をExtへ
- Python/JS/TS: AST→NyIR。辞書/配列/プロトタイプは標準Boxへ写像、例外/非同期はExtへ
- Go: panic/recover→exceptions、goroutine→Spawn/Join へ写像(将来)
バックエンド指針NyIR → Target
- WASM: 同期・非例外・非スレッドの最小路線から段階対応Exceptions/Threads提案に合わせ拡張
- LLVM: 例外/スレッド/アトミックが揃っているため先行実装が容易
- VM: 仕様の正しさ検証の基準(簡易実装でも良い)
- 他言語(ソース生成): 可読性/慣用性は課題だが、機械的変換は可能(優先度低)
検証計画Golden/Diff
- Cサブセット→NyIR→C/WASM例外なし・CASあり
- Python/JSサブセット→NyIR→WASM辞書/例外/非同期のサブセット)
- JVM系→NyIR→JVM bytecode例外/スレッド)
- Rustサブセット→NyIR→LLVM所有・weakの温存
→ Golden NyIR を用い、interp/vm/wasm/llvm で出力一致をCI検証
関連リンク
- NyIR 仕様: `spec.md`
- ABI/BID: `../予定/native-plan/box_ffi_abi.md`
- 9.10 タスクCopilot向け: `../予定/native-plan/issues/phase_9_10_nyir_spec.md`