Files
hakorune/docs/archive/proposals/nyir/spec.md
Moe Charm 11506cee3b 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>
2025-09-01 23:44:34 +09:00

127 lines
5.3 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.

# 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」などのメタを付与し、例外禁止環境へも対応する。