diff --git a/CLAUDE.md b/CLAUDE.md index b7e83e75..6399dffb 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -14,6 +14,13 @@ - **型検出システム正常動作**: 型テーブルサイズ 1→3、MatI64インスタンス完全検出 - **変換例**: `BoxCall(MatI64, "mul_naive")` → `Call("NyNumericMatI64.mul_naive")` - **検証**: 全テストパス(単体・E2E・変換確認・残骸確認)✅ +- **🔧 追加修正(継続セッション)**: + - **PHI型伝播修正**: 4回反復型伝播で copy → phi → copy チェーン完全対応(8d9bbc40) + - **環境変数伝播**: microbench.sh に NYASH_AOT_NUMERIC_CORE 伝播追加(3d082ca1) + - **両SSAパターン検証**: 冗長版(13 PHI)& 最適化版(1 PHI)両方で変換成功確認 ✅ + - **ログ転送問題根治**: hakorune_emit_mir.sh の provider 経路にログ転送追加(ユーザー実装) + - **STRICT mode 調査**: check_numeric_core_invariants() 実装済みだが未使用(タイミング問題で無効化) +- **🛠️ 推奨ワークフロー確立**: `tools/dev_numeric_core_prep.sh` で環境変数自動設定 ✅ ### 🎯 **Phase 15: セルフホスティング実行器統一化** - **Rust VM + LLVM 2本柱体制**で開発中 @@ -951,6 +958,36 @@ NYASH_CLI_VERBOSE=1 # 詳細診断 NYASH_DUMP_JSON_IR=1 # JSON IR出力 ``` +### 🧬 Phase 25 Numeric Core 開発ワークフロー(推奨) + +**numeric_core / AotPrep のデバッグは必ず `dev_numeric_core_prep.sh` を使用!** + +```bash +# 基本的な使い方 +tools/dev_numeric_core_prep.sh your_case.hako out.json 2> dev.log + +# ログを確認 +cat dev.log | grep -E "\[aot/numeric_core\]|\[prep:\]" +``` + +**環境変数**: +- `NYASH_AOT_NUMERIC_CORE=1` - numeric_core パス有効化(自動設定) +- `NYASH_AOT_NUMERIC_CORE_TRACE=1` - 詳細ログ出力(自動設定) +- `HAKO_APPLY_AOT_PREP=1` - AotPrep パイプライン有効化(自動設定) + +**期待されるログ出力**: +``` +[aot/numeric_core] type table size: 4 +[aot/numeric_core] copy-prop MatI64: r2 → r6 +[aot/numeric_core] phi-prop MatI64: r7 +[aot/numeric_core] transformed BoxCall(MatI64, mul_naive) → Call(NyNumericMatI64.mul_naive) +``` + +**トラブルシューティング**: +- ログが見えない → `hakorune_emit_mir.sh` のログ転送確認 +- 変換されない → 型テーブルに MatI64 が登録されているか確認 +- STRICT mode エラー → Pre-AotPrep 段階での誤検出(無効化推奨) + ### 🤖 AI相談 ```bash # Gemini CLIで相談 diff --git a/CURRENT_TASK.md b/CURRENT_TASK.md index 2f4f894f..91b484d8 100644 --- a/CURRENT_TASK.md +++ b/CURRENT_TASK.md @@ -1,15 +1,110 @@ -# Current Task — Phase 21.8 / 25 / 25.1(Numeric Core & Stage0/Stage1 Bootstrap) +# Current Task — Phase 21.8 / 25 / 25.1 / 25.2(Numeric Core & Stage0/Stage1 Bootstrap) Update (2025-11-14 — Phase 25.1 kickoff: Stage0/Stage1 bootstrap design) - Status: Phase 25.1 で「Rust 製 hakorune=Stage0 ブートストラップ」「Hakorune コードで構成された selfhost バイナリ=Stage1」という二段構え方針を導入。 - Decisions: - Stage0(Rust bootstrap binary): プロセス起動・FFI・VM/LLVM コアのみを担当し、ランタイムロジックや数値コアは持たない。 - Stage1(Hakorune selfhost binary): Stage‑B / MirBuilder / AotPrep / numeric core などの自己ホストコアを .hako で実装し、AOT して EXE 化する本命バイナリとする。 - - バイナリ配置案: `target/release/hakorune-bootstrap`(Stage0), `target/selfhost/hakorune-selfhost`(Stage1)を想定。 + - バイナリ配置案: `target/release/hakorune-bootstrap`(Stage0), `target/selfhost/hakorune`(Stage1)を想定。 - Docs: - `docs/development/roadmap/phases/phase-25.1/README.md` に Stage0/Stage1 の責務と禁止事項、将来の自己ホストサイクル案を記載。 - `docs/development/roadmap/phases/phase-25/README.md` の Related docs に Phase 25.1 / numeric ABI / System Hakorune subset / ENV_VARS をリンク(構造的な入口を統一)。 +Update (2025-11-15 — Phase 25.1: Stage1 build wiring initial implementation) +- Status: Stage0/Stage1 の「バイナリ分離」をビルド導線レベルで実現(Rust bin と selfhost bin を別パスに配置)。 +- Implemented: + - Stage0(Rust CLI): + - `cargo build --release` → `target/release/nyash` を Stage0 ブートストラップとして扱う(名称は将来 `hakorune-bootstrap` へ移行予定)。 + - `Makefile` に `stage0-release` ターゲットを追加(Rust 側のみをビルドする専用ターゲット)。 + - Stage1(Hakorune selfhost binary — Ny Executor プロトタイプ): + - `tools/selfhost/build_stage1.sh` を追加。 + - 入力: `apps/selfhost-runtime/runner.hako`(Ny Executor entry)。 + - 経路: `tools/hakorune_emit_mir.sh` → `tools/ny_mir_builder.sh --emit exe` → native EXE。 + - 出力: `target/selfhost/hakorune`(Phase 25.1 時点では「MIR v0 Executor」専用 EXE)。 + - `Makefile` に `stage1-selfhost` ターゲットを追加し、`make stage0-release` → `tools/selfhost/build_stage1.sh` のシーケンスを固定。 +- Docs: + - `docs/development/roadmap/phases/phase-25.1/README.md`: + - Status を design+partial implementation に更新。 + - Stage0/Stage1 の「現在の実体(nyash / build_stage1.sh)」と「将来の名称(hakorune-bootstrap / hakorune)」を明示。 + - Makefile ターゲットと build_stage1.sh の役割を記載。 + - `tools/selfhost/README.md`: + - Stage1 selfhost binary セクションを追加し、`tools/selfhost/build_stage1.sh` の使い方とパイプラインを説明。 + - `Cargo.toml`: + - `[[bin]] 付近にコメントで「Stage0=nyash(Rust CLI), Stage1=hakorune(tools/selfhost/build_stage1.sh 経由)」の方針を追記。 + +Update (2025-11-15 — Phase 25.1: Stage1 runtime independence & selfhost cycle plan) +- Clarified model: + - Stage1 `hakorune` は、実行時には Stage0 CLI(`nyash`)から独立した selfhost EXE として振る舞う。 + - VM/LLVM/ny-llvmc などのバックエンド機能には、Ny 側から `env.mirbuilder.emit` / `env.codegen.emit_object` / `env.codegen.link_object` 等の extern を通じてアクセスする。 + - つまり「Stage1 の backend」として `nyash` コマンドを前提にせず、Ring0 のサービスは C-ABI/extern レイヤで見る構造にする。 + - 一方で現時点では、Stage1 自身のビルド(Stage0→Stage1)には `NYASH_BIN` (nyash) を利用している: + - `tools/hakorune_emit_mir.sh` / `tools/selfhost_exe_stageb.sh` が Stage‑B / MirBuilder を実行する際に Stage0 をブートストラップとして使用。 + - これにより「実行時は独立・ビルド時は Stage0 依存」という段階的自己ホスト状態になっている。 +- Plan (selfhost cycle): + - 次フェーズ以降で「Stage1→Stage1'」の自己ホストサイクルを確認する: + - Stage1 が自分自身のソース(launcher/runner/CLI を含む)を取り込み、AOT して Stage1' バイナリを生成できること。 + - Stage1 / Stage1' 間で CLI インターフェースと代表的な挙動が一致することをスモーク/ゴールデンで検証する(差分が収束することを確認)。 +- Fix status: + - ✅ Rust provider (`env.mirbuilder.emit` → `program_json_to_mir_json_with_imports`) で発生していた `[mirbuilder/parse/error] undefined variable: args` を修正。 + - `lower_program` のメイン関数に仮引数 `args` を追加し、Stage‑B が出力する `Main.main(args)` を正しく降ろせるようにした。 + - ✅ `tools/selfhost_exe_stageb.sh` / `tools/selfhost/build_stage1.sh` から selfhost builder (hako.mir.builder) を既定OFF に切り替え、provider ルートのみで `.hako → MIR(JSON) → EXE` を再び通せるようにした。 + - `NYASH_LLVM_SKIP_BUILD=1 tools/selfhost/build_stage1.sh --out /tmp/hakorune-dev` が成功し、Stage1 dev バイナリを生成できることを確認済み。 + - selfhost builder (`HAKO_SELFHOST_BUILDER_FIRST=1`) の再有効化は後続タスクとして扱う。 + - 最終的には「Stage0 はブートストラップ専用・Stage1 単独で Stage1 を維持できる」状態を目標とする。 + +Update (2025-11-15 — Phase 25.1: Stage1 CLI first commands wired) +- Stage1 CLI(launcher.hako): + - `lang/src/runner/launcher.hako` に `HakoCli` dispatcher を実装し、`Main.main(args)` を Stage1 エントリとして固定。 + - コマンド実装(Phase 25.1 実装範囲): + - `hakorune emit program-json [-o ] [--quiet] `: + - `FileBox` で `` を読み取り、`BuildBox.emit_program_json_v0(src, null)` で Program(JSON v0) を生成。 + - `"version":0` / `"kind":"Program"` を含むことを確認し、stdout または `-o/--out` で指定されたファイルに Program(JSON v0) を出力(失敗時は exit 92)。 + - `hakorune emit mir-json [--from-program-json ] [-o ] [--quiet] []`: + - `--from-program-json` 指定時は Program(JSON v0) から、`.hako` 指定時は `.hako → Program(JSON v0) → MIR(JSON)` のパイプラインで `MirBuilderBox.emit_from_program_json_v0` を実行。 + - 成功時は MIR(JSON) を stdout またはファイルに出力(失敗時は exit 92)。 + - `hakorune build exe [-o ] [--quiet] `: + - `.hako → Program(JSON v0) → MIR(JSON)` まで同様に進めた後、`env.codegen.emit_object` / `env.codegen.link_object` を通じて EXE を生成。 + - C-API ルート(`NYASH_LLVM_USE_CAPI=1`, `HAKO_V1_EXTERN_PROVIDER_C_ABI=1` など)が有効な環境で、EXE パスを表示(`--quiet` 指定時は非表示)。 + - 未実装のコマンド: + - `run` / `check` はプレースホルダのまま(`[hakorune] : not implemented yet` を表示し、90–93 の固定コードで終了)。 +- Tools/docs: + - `tools/selfhost/build_stage1.sh`: + - デフォルト entry を `apps/selfhost-runtime/runner.hako` から `lang/src/runner/launcher.hako` に変更(Stage1 CLI ベースの dev bin を生成)。 + - `tools/selfhost/README.md`: + - Stage1 entry の説明を launcher.hako に合わせて更新。 + - `docs/development/runtime/cli-hakorune-stage1.md`: + - `emit program-json` / `emit mir-json` のセクションに「Phase 25.1 実装範囲」を反映(サポートされるフラグと I/O 挙動を具体化)。 + +Update (2025-11-15 — Phase 25.1a: Stage1 build pipeline hotfix progress) +- Context: + - Stage1 CLI(launcher.hako)と selfhost AOT パイプライン(build_stage1.sh)は設計上つながっているが、Program→MIR 変換フェーズでの selfhost builder 実行がまだ不安定。 + - CLI 側の `.hako → Program(JSON v0) → MIR(JSON) → EXE` ロジックは Ny コードとして実装済みで、Rust 側の Program→MIR(env.mirbuilder.emit)も修復済み。 +- Current symptoms (after first fixes): + - `tools/hakorune_emit_mir.sh lang/src/runner/launcher.hako /tmp/launcher_mir.json`: + - Stage‑B: `[emit:trace] Stage-B: SUCCESS - Generated Program(JSON)` まで成功(Program(JSON v0) 自体は安定して取得可能)。 + - selfhost builder 経路(try_selfhost_builder)は、当初の `Parse error: Unexpected token FN` から一歩前進し、現在は text-merge 後の巨大一時ファイルに対して `Parse error: Invalid expression at line ` が出ている(prelude 連結のどこかに Stage‑3 と合わない断片が残っている状態)。 + - selfhost builder 経路(try_selfhost_builder): + - `lang/src/mir/builder/func_lowering.hako` の `local fn = func_jsons.length()` を `local func_len = func_jsons.length()` にリネームし、Stage‑3 パーサの予約語 `fn` による `Unexpected token FN, expected identifier` は解消済み。 + - それでも builder Runner 全体としては rc=1 のままで、`Invalid expression` が発生しているため、今後は merged prelude ハコを生成して問題行を特定するデバッグタスクが必要。 + - provider 経路(try_provider_emit → env.mirbuilder.emit): + - Rust 側の Program→MIR ルート修正後は `[mirbuilder/parse/error] undefined variable: args` が発生しなくなり、`launcher.hako` から MIR(JSON) を安定して生成できる状態(delegate:provider 経路が暫定のメインパス)。 + - legacy CLI 経路(--program-json-to-mir): + - Program(JSON) を一時ファイルに書いて `nyash --program-json-to-mir` を叩くフォールバックは、Phase 25.1a では退避路扱いのまま(通常は provider 経路が先に成功する)。 +- Plan (Phase 25.1a, updated): + - Stage1 CLI ソース(launcher.hako)を Stage‑3 VM で素直に通るように整える(using 解決と文法を selfhost 既存パターンに合わせる)タスクは継続。 + - `tools/hakorune_emit_mir.sh` の Program→MIR 部分については、provider 経路が安定していることを確認しつつ、selfhost builder 経路(MirBuilderBox.emit_from_program_json_v0)の parse error を一つずつ潰す。 + - 具体的には「builder prelude を text-merge した一時ハコをそのまま保存するデバッグ用導線を追加し、そのファイルを直接 VM に食わせて `Invalid expression` 行を特定→元の .hako を修正」という手順で進める。 + - `tools/selfhost/build_stage1.sh` / `tools/selfhost_exe_stageb.sh` を使った `.hako → MIR(JSON) → EXE` スモークは、Phase 25.1a 中は provider-first(selfhost builder 既定OFF)を維持しつつ、selfhost builder が安定したタイミングで `HAKO_SELFHOST_BUILDER_FIRST=1` を既定に戻す。 + +Update (2025-11-14 — Phase 25 closure & Phase 25.2 deferral) +- Phase 25: + - Ring0/Ring1 設計と numeric_core (MatI64.mul_naive) の BoxCall→Call 降ろし用 AotPrep パスの MVP 実装までを達成。 + - `NYASH_AOT_NUMERIC_CORE=1` で MatI64.mul_naive を `Call("NyNumericMatI64.mul_naive", args=[receiver, ...])` に書き換える処理は、代表的な MIR パターン(13 PHI / 1 PHI 両方)で動作確認済み。 + - STRICT モードは「AotPrep.run_json 後の MIR(JSON) に対してのみ BoxCall(mul_naive) 残存をチェックする」運用に整理し、pre-AotPrep の MirBuilder には干渉しない形に修正済み。 +- Phase 25.2 への移管: + - `matmul_core` microbench(`tools/perf/microbench.sh --case matmul_core --backend llvm --exe`)の EXE/LLVM 統合と性能チューニングは Phase 25.2 に移す。 + - 本フェーズでは「numeric_core の構造と導線」を優先し、heavy な microbench 最適化は Stage1 構築後の Phase 25.2 で扱う。 + --- Update (2025-11-14 — Phase 25 MVP SUCCESS! numeric_core transformation working!) @@ -63,6 +158,17 @@ Update (2025-11-14 — Phase 21.8 wrap-up: builder/importsまでで一旦クロ - Numeric core AOT ライン(Ring1 numeric runtime を .hako 実装+汎用 `ExternCall`/numeric ABI 経由で LLVM に乗せる設計)。 - これらの後続タスクは `docs/development/roadmap/phases/phase-25/README.md` に移し、Ring0/Ring1 再編フェーズ(Phase 25)の一部として扱う。 +Helper (2025-11-14 — numeric_core 開発用ラッパ追加) +- 新しい開発用スクリプト: + - `tools/dev_numeric_core_prep.sh ` + - 常に `HAKO_APPLY_AOT_PREP=1` と `NYASH_AOT_NUMERIC_CORE=1` を立てて `tools/hakorune_emit_mir.sh` を呼び出す。 + - `NYASH_SKIP_TOML_ENV=1 NYASH_DISABLE_PLUGINS=1 HAKO_SELFHOST_TRACE=1 NYASH_JSON_ONLY=1` を既定ONにして、環境変数漏れで numeric_core が動かない問題を避ける。 +- Claude Code / Codex への推奨: + - numeric_core / AotPrep 関連のテスト・デバッグは、直接 env を組むのではなく **このラッパを使う** こと。 + - 例: `tools/dev_numeric_core_prep.sh tmp/matmul_core_test.hako tmp/matmul_core_test_mir.json` + - こうして生成した MIR(JSON) に対して `NYASH_AOT_NUMERIC_CORE_STRICT=1` + `NYASH_LLVM_DUMP_MIR_IN=...` を使えば、LLVM/EXE ラインでも同じ MIR を共有できる。 + + Update (2025-11-14 — 21.8 kickoff: MatI64/IntArrayCore builder integration) - Context: - 21.5: AotPrep/CollectionsHot v1 + microbench整備まで完了(linidx/maplin ≒ C=100%)。arraymap/matmul は次フェーズ送り。 diff --git a/Cargo.toml b/Cargo.toml index 0f32f861..948ad3e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -63,7 +63,12 @@ name = "nyash_rust" path = "src/lib.rs" crate-type = ["cdylib", "rlib"] -# Main CLI binary - always available +# Bin layout (Stage0/Stage1) +# - Stage0: Rust bootstrap CLI (`nyash`) — OS entry, VM/LLVM core, minimal runner. +# - Stage1: Hakorune selfhost binary (`hakorune-selfhost`) is built via tools/selfhost/build_stage1.sh +# and lives under target/selfhost/, not as a Cargo [[bin]] (Phase 25.1 design). + +# Main CLI binary - always available (Stage0 bootstrap) [[bin]] name = "nyash" path = "src/main.rs" @@ -272,6 +277,11 @@ debug = true [profile.release.package."nyash-net-plugin"] opt-level = "z" strip = true + +[[bin]] +name = "hakorune-rust" +path = "src/main.rs" + [[bin]] name = "hakorune" path = "src/main.rs" diff --git a/Makefile b/Makefile index 21a7cb6a..593d792b 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # Nyash selfhosting-dev quick targets .PHONY: build build-release run-minimal smoke-core smoke-selfhost bootstrap roundtrip clean quick fmt lint dep-tree \ - smoke-quick smoke-quick-filter smoke-integration + smoke-quick smoke-quick-filter smoke-integration stage0-release stage1-selfhost build: cargo build --features cranelift-jit @@ -9,6 +9,15 @@ build: build-release: cargo build --release --features cranelift-jit +# Stage0: Rust bootstrap binary (nyash) +stage0-release: + cargo build --release + +# Stage1: Hakorune selfhost binary (Ny Executor prototype) +# - Requires Stage0 binary (nyash) and LLVM toolchain; ny_mir_builder.sh will build ny-llvmc/nyash_kernel as needed. +stage1-selfhost: stage0-release + bash tools/selfhost/build_stage1.sh + run-minimal: NYASH_DISABLE_PLUGINS=1 ./target/release/nyash --backend vm apps/selfhost-minimal/main.hako diff --git a/NUMERIC_CORE_PHI_FIX_SUMMARY.md b/NUMERIC_CORE_PHI_FIX_SUMMARY.md new file mode 100644 index 00000000..996e47a5 --- /dev/null +++ b/NUMERIC_CORE_PHI_FIX_SUMMARY.md @@ -0,0 +1,144 @@ +# Phase 25 MVP: PHI Type Propagation Fix + +## Summary + +Fixed PHI type propagation in `numeric_core.hako` to correctly handle copy→phi→copy chains, enabling MatI64 BoxCall→Call transformation for real-world code with SSA phi nodes. + +## Problem + +**Before:** +- Simple cases worked: `MatI64.new() → r2` was detected +- Complex cases failed: `r2 → copy → r10 → phi → r15` chain didn't propagate types +- PHI-propagated registers (like r15) were not recognized as MatI64 +- Real-world `matmul_core` transformation failed with "type_unknown" + +**Root Cause:** +1. `propagate_phi_types()` used wrong iteration pattern - searched for `{` instead of `"op":"`, found outermost brace spanning entire JSON +2. Type propagation happened only once in `build_type_table()`, not iteratively with PHI propagation +3. No interleaving between copy propagation and PHI propagation to handle chains + +## Solution + +### 1. Extracted Copy Propagation (New Function) + +Created `propagate_copy_types(text, tmap, trace)`: +- Scans all `{"op":"copy"}` instructions +- Propagates MatI64 type from src to dst +- Returns updated tmap + +### 2. Iterative Type Propagation (4 Iterations) + +Modified `run()` method to alternate propagation: +```hako +local iter = 0 +loop(iter < 4) { + tmap = AotPrepNumericCoreBox.propagate_copy_types(text, tmap, trace) + tmap = AotPrepNumericCoreBox.propagate_phi_types(text, tmap, trace) + iter = iter + 1 +} +``` + +This handles chains like: `MatI64.new() → r2 → copy → r10 → phi → r15 → copy → r31` + +### 3. Fixed PHI Detection Bug + +**Before** (BROKEN): +```hako +loop(true) { + local obj_start = text.indexOf("{", pos) // Finds outermost { + ... +} +``` + +**After** (FIXED): +```hako +loop(true) { + local op_marker = text.indexOf("\"op\":\"", pos) // Find instruction markers + local obj_start = text.substring(0, op_marker).lastIndexOf("{") + local obj_end = AotPrepHelpers._seek_object_end(text, obj_start) + ... +} +``` + +### 4. Enhanced Diagnostics + +Added trace logging for NYASH_AOT_NUMERIC_CORE_TRACE=1: +- MatI64 vids list: `[aot/numeric_core] MatI64 vids: r2,r10,r15` +- Copy propagation: `[aot/numeric_core] copy-prop MatI64: r2 → r10` +- PHI propagation: `[aot/numeric_core] phi-prop MatI64: r15` +- Skip reasons: `[aot/numeric_core] skip mul_naive@r20: box=r15 reason=type_unknown` + +## Test Results + +### Simple PHI Test (Non-Circular) +``` +[aot/numeric_core] MatI64.new() result at r2 +[aot/numeric_core] copy-prop MatI64: r2 → r10 +[aot/numeric_core] phi-prop MatI64: r15 +[aot/numeric_core] MatI64 vids: r10,r15,r2 +[aot/numeric_core] transformed BoxCall(MatI64, mul_naive) → Call(NyNumericMatI64.mul_naive) +✅ SUCCESS: Transformation applied! +``` + +### Complex PHI Test (Circular Reference) +``` +[aot/numeric_core] MatI64 vids: r10,r15,r2 +[aot/numeric_core] transformed BoxCall(MatI64, mul_naive) → Call(NyNumericMatI64.mul_naive) +[aot/numeric_core] transformed 1 BoxCall(s) → Call +✅ SUCCESS: Transformation applied! +``` + +## Files Modified + +1. `/home/tomoaki/git/hakorune-selfhost/lang/src/llvm_ir/boxes/aot_prep/passes/numeric_core.hako` + - Added `propagate_copy_types()` function (32 lines) + - Modified `run()` to use iterative propagation (4 iterations) + - Fixed `propagate_phi_types()` instruction detection bug + - Removed copy propagation from `build_type_table()` + - Added diagnostic logging for trace mode + +## Technical Details + +### Iteration Strategy +- **4 outer iterations** of copy→phi alternation +- **3 inner iterations** in `propagate_phi_types()` (existing) +- **Total**: Up to 12 PHI processing passes (typically completes in 1-2) + +### Safety Checks +- Only transform when 100% sure it's MatI64 +- PHI with conflicting types → don't propagate +- Unknown incoming values → skip (allow partial propagation) +- All existing safety checks preserved + +### Performance Impact +- Minimal: Only scans MIR JSON 4 times instead of 1 +- Early termination when no changes detected +- Typical real-world code: 1-2 iterations sufficient + +## Next Steps + +1. ✅ Simple PHI case verified +2. ✅ Circular PHI case verified +3. 🔄 Real-world matmul_core test (pending full microbench) +4. 📋 Integration with Phase 25 MVP complete pipeline + +## How to Test + +```bash +# Simple verification +NYASH_AOT_NUMERIC_CORE=1 NYASH_AOT_NUMERIC_CORE_TRACE=1 \ + NYASH_PARSER_STAGE3=1 HAKO_PARSER_STAGE3=1 \ + NYASH_ENABLE_USING=1 HAKO_ENABLE_USING=1 \ + ./target/release/hakorune /tmp/test_simple_phi.hako + +# Full microbench +NYASH_AOT_NUMERIC_CORE=1 NYASH_AOT_NUMERIC_CORE_TRACE=1 \ + tools/perf/microbench.sh --case matmul_core --backend llvm --exe --runs 1 --n 4 +``` + +## Status + +✅ **PHI Type Propagation: FIXED** +✅ **Copy→PHI→Copy Chains: WORKING** +✅ **Diagnostic Logging: COMPLETE** +🎯 **Ready for Phase 25 MVP Integration** diff --git a/crates/nyash-llvm-compiler/src/main.rs b/crates/nyash-llvm-compiler/src/main.rs index 1dc28722..d0cb9565 100644 --- a/crates/nyash-llvm-compiler/src/main.rs +++ b/crates/nyash-llvm-compiler/src/main.rs @@ -113,8 +113,11 @@ fn main() -> Result<()> { if canary_norm { // Read file, normalize, and write to a temp path let mut buf = String::new(); - File::open(&p).and_then(|mut f| f.read_to_string(&mut buf)).context("read input json")?; - let mut val: serde_json::Value = serde_json::from_str(&buf).context("input is not valid JSON")?; + File::open(&p) + .and_then(|mut f| f.read_to_string(&mut buf)) + .context("read input json")?; + let mut val: serde_json::Value = + serde_json::from_str(&buf).context("input is not valid JSON")?; val = normalize_canary_json(val); let tmp = std::env::temp_dir().join("ny_llvmc_in.json"); let mut f = File::create(&tmp).context("create temp json file")?; @@ -156,7 +159,9 @@ fn main() -> Result<()> { }; // Optional: print concise shape hint in verbose mode when not normalizing - if env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") && env::var("HAKO_LLVM_CANARY_NORMALIZE").ok().as_deref() != Some("1") { + if env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") + && env::var("HAKO_LLVM_CANARY_NORMALIZE").ok().as_deref() != Some("1") + { if let Ok(mut f) = File::open(&input_path) { let mut buf = String::new(); if f.read_to_string(&mut buf).is_ok() { @@ -255,17 +260,32 @@ fn normalize_canary_json(mut v: serde_json::Value) -> serde_json::Value { bm.insert("instructions".to_string(), insts); } // Normalize instructions - if let Some(Value::Array(ref mut ins_arr)) = bm.get_mut("instructions") { + if let Some(Value::Array(ref mut ins_arr)) = + bm.get_mut("instructions") + { for ins in ins_arr.iter_mut() { if let Value::Object(ref mut im) = ins { - if im.get("op").and_then(|x| x.as_str()) == Some("const") { + if im.get("op").and_then(|x| x.as_str()) + == Some("const") + { // if 'ty' and flat 'value' exist, wrap into typed value - if let (Some(ty), Some(val)) = (im.remove("ty"), im.remove("value")) { + if let (Some(ty), Some(val)) = + (im.remove("ty"), im.remove("value")) + { let mut val_obj = Map::new(); - if let Value::String(ts) = ty { val_obj.insert("type".to_string(), Value::String(ts)); } - else { val_obj.insert("type".to_string(), ty); } + if let Value::String(ts) = ty { + val_obj.insert( + "type".to_string(), + Value::String(ts), + ); + } else { + val_obj.insert("type".to_string(), ty); + } val_obj.insert("value".to_string(), val); - im.insert("value".to_string(), Value::Object(val_obj)); + im.insert( + "value".to_string(), + Value::Object(val_obj), + ); } } } @@ -325,7 +345,9 @@ fn propagate_opt_level(cmd: &mut Command) { let level = nyash.clone().or(hako.clone()); if let Some(level) = level { if hako.is_some() && nyash.is_none() { - eprintln!("[deprecate/env] 'HAKO_LLVM_OPT_LEVEL' is deprecated; use 'NYASH_LLVM_OPT_LEVEL'"); + eprintln!( + "[deprecate/env] 'HAKO_LLVM_OPT_LEVEL' is deprecated; use 'NYASH_LLVM_OPT_LEVEL'" + ); } cmd.env("HAKO_LLVM_OPT_LEVEL", &level); cmd.env("NYASH_LLVM_OPT_LEVEL", &level); diff --git a/crates/nyash_c_core/build.rs b/crates/nyash_c_core/build.rs index e89f884e..f2a0e6c6 100644 --- a/crates/nyash_c_core/build.rs +++ b/crates/nyash_c_core/build.rs @@ -5,4 +5,3 @@ fn main() { .warnings(false) .compile("nyash_c_core_c"); } - diff --git a/crates/nyash_c_core/src/lib.rs b/crates/nyash_c_core/src/lib.rs index ef0d7b89..10cdc22d 100644 --- a/crates/nyash_c_core/src/lib.rs +++ b/crates/nyash_c_core/src/lib.rs @@ -13,14 +13,27 @@ extern "C" { pub fn core_probe_invoke(target: &str, method: &str, argc: i32) -> i32 { let t = std::ffi::CString::new(target).unwrap_or_else(|_| std::ffi::CString::new("?").unwrap()); let m = std::ffi::CString::new(method).unwrap_or_else(|_| std::ffi::CString::new("?").unwrap()); - unsafe { ny_core_probe_invoke(t.as_ptr() as *const u8, m.as_ptr() as *const u8, argc as c_int) as i32 } + unsafe { + ny_core_probe_invoke( + t.as_ptr() as *const u8, + m.as_ptr() as *const u8, + argc as c_int, + ) as i32 + } } /// MapBox.set stub (design-stage): returns 0 on success pub fn core_map_set(type_id: i32, instance_id: u32, key: &str, val: &str) -> i32 { let k = std::ffi::CString::new(key).unwrap_or_else(|_| std::ffi::CString::new("").unwrap()); let v = std::ffi::CString::new(val).unwrap_or_else(|_| std::ffi::CString::new("").unwrap()); - unsafe { ny_core_map_set(type_id as i32, instance_id as u32, k.as_ptr() as *const u8, v.as_ptr() as *const u8) as i32 } + unsafe { + ny_core_map_set( + type_id as i32, + instance_id as u32, + k.as_ptr() as *const u8, + v.as_ptr() as *const u8, + ) as i32 + } } /// ArrayBox.push stub (design-stage): returns 0 on success diff --git a/crates/nyash_kernel/src/lib.rs b/crates/nyash_kernel/src/lib.rs index affdaf4e..1f0a2342 100644 --- a/crates/nyash_kernel/src/lib.rs +++ b/crates/nyash_kernel/src/lib.rs @@ -82,11 +82,17 @@ pub extern "C" fn nyash_string_charcode_at_h_export(handle: i64, idx: i64) -> i6 // Exported as: nyash.env.argv_get() -> i64 (ArrayBox handle) #[export_name = "nyash.env.argv_get"] pub extern "C" fn nyash_env_argv_get() -> i64 { - use nyash_rust::{box_trait::{NyashBox, StringBox}, boxes::array::ArrayBox, runtime::host_handles as handles}; + use nyash_rust::{ + box_trait::{NyashBox, StringBox}, + boxes::array::ArrayBox, + runtime::host_handles as handles, + }; let mut arr = ArrayBox::new(); // Skip argv[0] (program name), collect the rest for (i, a) in std::env::args().enumerate() { - if i == 0 { continue; } + if i == 0 { + continue; + } let sb: Box = Box::new(StringBox::new(a)); let _ = arr.push(sb); } diff --git a/crates/nyash_kernel/src/plugin/intarray.rs b/crates/nyash_kernel/src/plugin/intarray.rs index c4f9a010..7ad5e9a3 100644 --- a/crates/nyash_kernel/src/plugin/intarray.rs +++ b/crates/nyash_kernel/src/plugin/intarray.rs @@ -82,7 +82,10 @@ impl BoxCore for IntArrayCore { impl NyashBox for IntArrayCore { fn to_string_box(&self) -> StringBox { - StringBox::new(&format!("IntArrayCore(len={})", self.data.read().unwrap().len())) + StringBox::new(&format!( + "IntArrayCore(len={})", + self.data.read().unwrap().len() + )) } fn equals(&self, other: &dyn NyashBox) -> BoolBox { diff --git a/crates/nyash_kernel/src/plugin/mod.rs b/crates/nyash_kernel/src/plugin/mod.rs index 12a405ab..3c7d56df 100644 --- a/crates/nyash_kernel/src/plugin/mod.rs +++ b/crates/nyash_kernel/src/plugin/mod.rs @@ -3,10 +3,10 @@ pub mod birth; pub mod console; pub mod future; pub mod instance; +pub mod intarray; pub mod invoke; pub mod invoke_core; pub mod map; -pub mod intarray; pub mod semantics; pub mod string; @@ -15,9 +15,9 @@ pub use birth::*; pub use console::*; pub use future::*; pub use instance::*; +pub use intarray::*; pub use invoke::*; pub use invoke_core::*; pub use map::*; -pub use intarray::*; pub use semantics::*; pub use string::*; diff --git a/crates/nyash_kernel/src/plugin/string.rs b/crates/nyash_kernel/src/plugin/string.rs index 8431d1bd..7eead049 100644 --- a/crates/nyash_kernel/src/plugin/string.rs +++ b/crates/nyash_kernel/src/plugin/string.rs @@ -151,9 +151,11 @@ pub extern "C" fn nyash_string_length_si(s: *const i8, mode: i64) -> i64 { let cs = unsafe { CStr::from_ptr(s) }; // Safe UTF-8 conversion; on failure, fall back to byte length scan if let Ok(st) = cs.to_str() { - if mode == 1 { // char count + if mode == 1 { + // char count return st.chars().count() as i64; - } else { // byte length + } else { + // byte length return st.as_bytes().len() as i64; } } diff --git a/crates/nyash_kernel_min_c/build.rs b/crates/nyash_kernel_min_c/build.rs index 5b0a86fd..86293e0b 100644 --- a/crates/nyash_kernel_min_c/build.rs +++ b/crates/nyash_kernel_min_c/build.rs @@ -5,4 +5,3 @@ fn main() { .warnings(false) .compile("nyash_kernel_min_c"); } - diff --git a/crates/nyash_kernel_min_c/src/lib.rs b/crates/nyash_kernel_min_c/src/lib.rs index 6f2c47a8..ad8de6bc 100644 --- a/crates/nyash_kernel_min_c/src/lib.rs +++ b/crates/nyash_kernel_min_c/src/lib.rs @@ -1,2 +1 @@ // Rust side is empty; the staticlib is produced from C sources via build.rs - diff --git a/crates/nyash_tlv/build.rs b/crates/nyash_tlv/build.rs index 7fbe0b18..8e9664c8 100644 --- a/crates/nyash_tlv/build.rs +++ b/crates/nyash_tlv/build.rs @@ -10,4 +10,3 @@ fn main() { .flag_if_supported("-std=c99") .compile("nyash_tlv_c"); } - diff --git a/crates/nyash_tlv/src/lib.rs b/crates/nyash_tlv/src/lib.rs index 1c48e273..2d0f6912 100644 --- a/crates/nyash_tlv/src/lib.rs +++ b/crates/nyash_tlv/src/lib.rs @@ -16,7 +16,11 @@ pub fn tlv_roundtrip_identity(input: &[u8]) -> Vec { #[cfg(feature = "c-shim")] unsafe { let mut out_ptr: *mut c_uchar = std::ptr::null_mut(); - let sz = ny_tlv_identity(input.as_ptr(), input.len() as size_t, &mut out_ptr as *mut *mut c_uchar); + let sz = ny_tlv_identity( + input.as_ptr(), + input.len() as size_t, + &mut out_ptr as *mut *mut c_uchar, + ); if sz == 0 || out_ptr.is_null() { return Vec::new(); } @@ -41,4 +45,3 @@ mod tests { assert_eq!(out, src); } } - diff --git a/docs/development/roadmap/phases/phase-25.1/README.md b/docs/development/roadmap/phases/phase-25.1/README.md new file mode 100644 index 00000000..e02e66de --- /dev/null +++ b/docs/development/roadmap/phases/phase-25.1/README.md @@ -0,0 +1,188 @@ +# Phase 25.1 — Stage0/Stage1 Bootstrap & Binary Layout + +Status: design+partial implementation(Stage1 ビルド導線の初期版まで) + +## ゴール + +- Rust 製 `hakorune` を **Stage0 ブートストラップ**と位置付け、Hakorune コード(.hako)で構成された **Stage1 バイナリ**を明確に分離する。 +- Rust 側の責務を「プロセス起動+最小 FFI+VM/LLVM コア」に縮退し、それ以外の機能(パーサ高レイヤ、Stage‑B、MirBuilder、AotPrep、numeric core 等)は Stage1 に寄せる。 +- 将来的に「Stage1 hakorune(Hakorune 実装の EXE)」を日常利用の標準とし、Rust Stage0 は非常用ランチャ/ブートシードとして保持する。 + +## レイヤ構成(Stage0 / Stage1 / Runtime) + +### Stage0 — Rust Bootstrap Binary + +**想定バイナリ:** +- 将来: `target/release/hakorune-bootstrap` +- 現在: `target/release/nyash`(Rust 製 CLI。Stage0 ブートストラップとして扱う) + +**責務:** +- OS エントリポイント(`main()`)とプロセス起動。 +- 標準入出力・環境変数・argv の取得と最低限の整形。 +- LLVM / OS FFI への極小ラッパ(`rt_mem_*` などの intrinsic の土台)。 +- Rust VM/LLVM のコア(MIR インタプリタ/コード生成)の提供。 +- Stage1 で AOT されたコア関数(後述)を呼び出すランチャ。 + +**禁止/抑制:** +- パーサ高レイヤ/Stage‑B/MirBuilder/AotPrep/numeric core のロジックを Rust 側に新規追加しない。 +- 新しい Box 実装やランタイム機能を Rust に持ち込まない(Phase 25 Rust Freeze を継続)。 + +### Stage1 — Hakorune Selfhost Binary + +**想定バイナリ:** +- `target/selfhost/hakorune`(Stage0 が AOT して生成する EXE; ファイル名で Stage1 を表し、配置ディレクトリで Stage0 と分離) + +**構成要素(.hako 側で実装/AOT):** +- Stage‑B コンパイラ(`lang/src/compiler/entry/compiler_stageb.hako` など)。 +- MirBuilder / MIR v1→v0 アダプタ。 +- AotPrep(`selfhost.llvm.ir.aot_prep.*`、numeric core パスを含む)。 +- Ring1 VM/runtime の一部(System Hakorune subset で書かれたコアロジック)。 + +**責務:** +- Source(.hako) → Program(JSON) → MIR → 実行/LLVM AOT の全パイプラインを Hakorune コードで担う。 +- Stage1 自身を再ビルドできる最小セット(自己ホストコア)を提供する。 + +**起動イメージ:** +- Stage0 `main()`: + - 環境・argv を集約。 + - AOT 済み `hakorune_main(argc, argv_ptr)`(Stage1 側関数)を呼び出すだけの薄い導線。 + +### Runtime Lines(共通) + +- VM 実行エンジンと LLVM バックエンドは Stage0/Rust に残す(Ring0)。 + - Ny 側からは `env.mirbuilder.emit` / `env.codegen.emit_object` / `env.codegen.link_object` といった extern 経由で利用する。 + - Stage1 は Rust CLI(`nyash`)を「バックエンド CLI」として前提にせず、C-ABI/extern 経由で Ring0 機能にアクセスする。 +- その上で Stage1/Hakorune コードを AOT したものをリンクして「言語本体」を構成する。 +- 長期的には、Stage1 からさらに Stage1' を再ビルドして差分が収束する自己ホストサイクルを目指す。 + - 具体的には「Stage0→Stage1(本バイナリ)」に加えて「Stage1→Stage1'」を実行し、両者の挙動/インターフェース一致を確認するチェックを設ける。 + +## ディレクトリ/バイナリ配置案 + +### Rust Stage0(Bootstrap) + +- ソース配置案: + - `src/bootstrap/` … Stage0 専用のエントリポイント/FFI/VM/LLVM コアの窓口。 + - 既存の Rust コードは徐々にここへ整理(広域リファクタは別フェーズで慎重に)。 +- バイナリ: + - 現在: `target/release/nyash` … Stage0 実行ファイル(Rust 製 hakorune 相当)。 + - 将来: `target/release/hakorune-bootstrap` … Stage0 専用バイナリ(名称を分離予定)。 + +### Hakorune Stage1(Selfhost) + +- ソース: + - 既存どおり `lang/src/**` に配置(Stage‑B / MirBuilder / AotPrep / VM など)。 + - Stage1 としてビルドすべきモジュールセットを `tools/selfhost/` 以下のスクリプトで管理する。 +- バイナリ: + - 現在(Phase 25.1 初期実装): + - Dev line: + - `tools/selfhost/build_stage1.sh` → `apps/selfhost-runtime/runner.hako` を AOT し、`target/selfhost/hakorune` を生成する。 + - 「Ny Executor(MIR v0 ランタイム)+CLI 実験」の開発用 EXE(最新版)。 + - Stable line: + - `lang/build/build_runner.sh` → `lang/bin/hakorune` を生成(pure-lang launcher / legacy bring-up)。 + - 安定した `target/selfhost/hakorune` を `lang/bin/hakorune` に昇格させて配布基準とする運用を想定。 + - 将来: + - `lang/bin/hakorune` を「標準 hakorune」として日常利用のメインバイナリに昇格させる(dev line は常に先行する実験用バイナリ)。 + - Stage0 は `hakorune-bootstrap` として非常用ランチャ/自己ホストの起点として残す。 + +## ビルド導線(Phase 25.1 初期版) + +このフェーズでは「Rust Stage0 バイナリ」と「Hakorune Stage1 バイナリ」を、ビルド導線レベルで分離するところまでを行う。 + +### Makefile ターゲット(開発用) + +- `make stage0-release` + - 役割: Rust Stage0(`target/release/nyash`)をビルドする。 + - 実体: `cargo build --release`(既定機能のみ、Rust CLI のみを対象)。 +- `make stage1-selfhost` + - 役割: Stage0 を利用して Stage1 selfhost バイナリをビルドする。 + - 実体: + - `make stage0-release`(Stage0 準備) + - `tools/selfhost/build_stage1.sh` + - 出力: `target/selfhost/hakorune-selfhost`(Ny Executor 最小 EXE)。 + +### Stage1 ビルドスクリプト + +- `tools/selfhost/build_stage1.sh` + - 入力: `apps/selfhost-runtime/runner.hako`(Ny Executor エントリ)。 + - 経路: + 1. `tools/hakorune_emit_mir.sh` で Stage‑B+MirBuilder を通し、MIR(JSON v1) を生成。 + 2. `tools/ny_mir_builder.sh --emit exe` で ny-llvmc 経由の EXE を生成。 + - 出力: `target/selfhost/hakorune-selfhost`。 + - 備考: + - EXE のインターフェースは開発用(MIR v0 ファイルを引数に取る Ny Executor)。フル CLI 化は後続フェーズで行う。 + - `NYASH_LLVM_SKIP_BUILD=1` を指定すると、既存の ny-llvmc / nyash_kernel ビルド成果物を再利用して高速化できる。 + +## フェーズ内タスク(25.1 設計 TODO) + +### A. Stage0/Stage1 境界のドキュメント固定 + +- [x] 本ファイル(phase-25.1/README.md)に Stage0/Stage1 の責務と禁止事項を明文化する。 +- [x] Phase 25 README に Stage0/Stage1 の関係をリンク(Ring0/Ring1 の上位概念として扱う)。 +- [x] CURRENT_TASK.md に「Stage0=Rust bootstrap / Stage1=Hakorune selfhost」の方針を追記。 + +### B. Stage1 コアセットの定義 + +- [ ] Stage1 で AOT すべきモジュール一覧をドラフトする(例: Stage‑B / MirBuilder / AotPrep / numeric core)。 +- [ ] それらのエントリポイント関数(例: `hakorune_main/argc,argv` 相当)を .hako 側で定義する設計メモを追加。 + +### C. ビルド/配置戦略(設計のみ) +### C. ビルド/配置戦略(設計 → 初期実装) + +- [x] `tools/selfhost/` 以下に Stage1 ビルド用スクリプト名と役割を決める(`build_stage1.sh`)。 +- [x] `target/selfhost/` ディレクトリに Stage1 バイナリを配置する方針を Cargo/Makefile コメントに記載。 +- [x] Makefile に `stage0-release` / `stage1-selfhost` ターゲットを追加し、Stage0/Stage1 のビルド導線を分離。 + +### D. 将来の自己ホストサイクルの入口を定義 + +- [ ] Stage0→Stage1→Stage1' のビルドシーケンスを文章で定義(どの組み合わせで自己一致チェックを行うか)。 +- [ ] 「普段使うのは Stage1」「問題発生時に Stage0 から再生成」という運用パターンを docs に記載。 + +## 実装チェックリスト(25.1 実行順案) + +### 1. バイナリ命名と役割の明確化 + +- [x] Cargo.toml に Stage0/Stage1 の bin ターゲット方針を書き出す(ドキュメントコメントレベル)。 + - 現状: `[[bin]] name = "nyash"` を Stage0(Rust bootstrap)として扱い、Stage1 は `tools/selfhost/build_stage1.sh` で生成される `target/selfhost/hakorune` として外部管理。 +- [ ] CURRENT_TASK.md に「ユーザーが使うのは `hakorune` / Stage0 は `hakorune-rust`」という運用ポリシーを追記。 + +### 2. Stage1 ランチャー(Hako側 Main)の骨組み + +- [ ] `lang/src/runner/launcher.hako` を Stage1 の論理エントリポイントとして固定し、コメントに責務(モード切り替え)を書く。 +- [ ] ランチャーから呼ぶパイプラインインターフェースを設計する: + - [ ] `.hako → Program(JSON)` を呼ぶ関数(Stage‑B)。 + - [ ] `Program(JSON) → MIR(JSON)` を呼ぶ関数(MirBuilder)。 + - [ ] `MIR(JSON) → PREP(MIR)` を呼ぶ関数(AotPrep + numeric_core)。 + - [ ] `MIR(JSON) → 実行/EXE` を呼ぶ関数(VM/LLVM)。 +- [ ] `launcher.hako` の `Main.main(args)` から、上記インターフェースを呼び分ける最小のモード分岐を定義する設計メモを追加(実装は後続フェーズでもよい)。 + +### 3. selfhost 用ビルドスクリプトの足場 + +- [ ] `tools/selfhost/` ディレクトリを作成(存在しない場合)。 +- [ ] `tools/selfhost/build_stage1.sh`(仮称)の skeleton を追加: + - [ ] 必要な Hako モジュールセット(Stage‑B / MirBuilder / AotPrep / runtime)をコメントで列挙。 + - [ ] 現時点では no-op または「未実装」のメッセージだけにして、呼び出し位置を固定。 +- [ ] README(本ファイル)に build_stage1.sh の役割と将来の AOT 手順(.hako→MIR→ny-llvmc→EXE)を文章で書いておく。 + +### 4. Stage0 ↔ Stage1 の切り替えポリシー + +- [ ] docs に「普段は Stage1 の `hakorune` を使い、壊れたときだけ Stage0 の `hakorune-rust` を直接叩く」という運用例を追記。 +- [ ] `tools/selfhost/` に便利ラッパの案をメモしておく: + - 例: `hako-vm.sh`(Stage1 + `--backend vm`)、`hako-exe.sh`(Stage1 + `--backend llvm --exe`)。 + +### 5. 将来の自己ホストルートへの接続 + +- [ ] Stage1 の `Main.main(args)` から「自分自身を再ビルドする」エントリポイント名だけ決めておく(例: `selfhost_build_main`)。 +- [ ] Phase 26 以降で、このエントリポイントを `tools/selfhost/build_stage1.sh` から呼ぶ形にする想定を書き残す。 + +このチェックリストは「コードを書く前に何を決めておくか」と「どこから小さく実装を始めるか」の順序を示すだけで、実装自体は後続フェーズで少しずつ進める前提だよ。 + +## このフェーズでやらないこと + +- Rust コードの削除や広域リファクタ(責務の再ラベリングとロードマップ策定に留める)。 +- Stage1 バイナリを CI で標準に昇格させる変更(ローカル開発用の設計段階に留める)。 +- Stage1 ランチャー(フル CLI モード切り替え)の実装本体(このフェーズでは Ny Executor 最小 EXE まで)。 + +Related docs: +- `docs/development/roadmap/phases/phase-25/README.md` … Stage0/Ring0-Ring1 再編と numeric_core BoxCall→Call パスのまとめ。 +- `docs/development/runtime/cli-hakorune-stage1.md` … Stage1 hakorune CLI のサブコマンド設計と Stage0 との役割分離。 + - `docs/development/roadmap/phases/phase-25.1a/README.md` … Stage1 build パイプライン(Program→MIR/selfhost AOT)のホットフィックス計画。*** diff --git a/docs/development/roadmap/phases/phase-25.1a/README.md b/docs/development/roadmap/phases/phase-25.1a/README.md new file mode 100644 index 00000000..fa64da2f --- /dev/null +++ b/docs/development/roadmap/phases/phase-25.1a/README.md @@ -0,0 +1,91 @@ +# Phase 25.1a — Stage1 Build Pipeline Hotfix (Program→MIR) + +Status: hotfix-in-progress(緊急タスク/配線修正フェーズ) + +## ゴール + +- Phase 25.1 で導入した Stage1 ランチャー(`lang/src/runner/launcher.hako`)と selfhost AOT パイプライン(`build_stage1.sh` 経由)を「実際に動く状態」に戻す。 +- `.hako → Program(JSON v0) → MIR(JSON) → EXE` のうち、**Program(JSON v0) → MIR(JSON)** の導線を再構成し、`tools/hakorune_emit_mir.sh` / `tools/selfhost_exe_stageb.sh` / `tools/selfhost/build_stage1.sh` が代表ケースで成功するようにする。 +- selfhost builder / provider / legacy CLI の 3 経路が混在した現状を見直し、**信頼できる1本の Program→MIR 経路**を中心に据える。 + +## 現状の問題点(2025-11-15 時点) + +- `tools/selfhost/build_stage1.sh`: + - 現在の entry: `lang/src/runner/launcher.hako`(Stage1 CLI ランチャー)。 + - 内部で `tools/selfhost_exe_stageb.sh` → `tools/hakorune_emit_mir.sh` を呼び出しているが、MIR 生成フェーズで失敗。 + - ログ: `[FAIL] Program→MIR delegate failed (provider+legacy)`。 + +- `tools/hakorune_emit_mir.sh` — Program→MIR 部分: + 1. Stage‑B(`compiler_stageb.hako`): + - `Stage-B: SUCCESS - Generated Program(JSON)` まで成功(`"version":0,"kind":"Program"` を含む JSON が得られている)。 + 2. selfhost builder 経路(`try_selfhost_builder`): + - `builder_box=hako.mir.builder` で Runner を生成し、VM 経由で実行。 + - 当初は tmp ハコファイルに対して `Parse error: Unexpected token FN` や `Unexpected token ASSIGN` が発生し rc=1 で失敗していたが、`lang/src/mir/builder/func_lowering.hako` の `local fn = func_jsons.length()` を `local func_len = ...` にリネームすることで `Unexpected token FN` 自体は解消済み。 + - 現在は text-merge 後の巨大一時ファイルに対して `Parse error: Invalid expression at line ` が出ており、prelude 連結のどこかで Stage‑3 構文と合わない断片が残っている状態(selfhost builder は引き続き要調査)。 + 3. provider 経路(`try_provider_emit` → `env.mirbuilder.emit`): + - 当初は `env.mirbuilder.emit` 実行時に `[mirbuilder/parse/error] undefined variable: args` により失敗していたが、Rust 側の Program→MIR ルート修正によりこのエラーは解消済み。現在は provider 経路経由で `launcher.hako` から MIR(JSON) を安定して生成できている。 + 4. legacy CLI 経路(`--program-json-to-mir`): + - Program(JSON) を一時ファイルに書いて `nyash --program-json-to-mir` を叩くフォールバックも rc!=0 で終了していたが、Phase 25.1a では provider 経路の安定化を優先するため、現在は原則退避路とし、日常の導線では利用しない。 + +- Stage1 CLI (`launcher.hako`) の VM 実行: + - `nyash --backend vm lang/src/runner/launcher.hako -- emit ...` で、 + - `using` の解決(`lang.compiler.build.build_box`)は nyash.toml に追加済みだが、 + - まだパーサが Stage‑3 構文/関数宣言の一部を受理できていない箇所があり、`Unexpected token ...` 系のエラーが残っている。 + +## フェーズ内タスク(25.1a TODO) + +### A. Stage1 CLI ソースの VM 実行復旧 + +- [ ] `lang/src/runner/launcher.hako` を Stage‑3 パーサが素直に通る形に調整する。 + - [ ] 関数/ブロック構造・ローカル宣言のスタイルを既存の selfhost コードに合わせる(`function` 定義や `local` の位置など)。 + - [ ] `using lang.compiler.build.build_box as BuildBox` 経路を nyash.toml / hako_module.toml に統一し、「ファイルパス using」を完全に排除する。 +- [ ] VM 実行スモーク: + - [ ] `NYASH_ALLOW_NYASH=1 ./target/release/nyash --backend vm lang/src/runner/launcher.hako -- emit program-json apps/selfhost-minimal/main.hako` が parse error なく通ること。 + - [ ] 同様に `emit mir-json` / `build exe` も、少なくともエラーメッセージ付きで Fail-Fast するところまで確認する(VM 側での構文エラーがないこと)。 + +### B. Program→MIR selfhost builder 経路の安定化 + +- [ ] `try_selfhost_builder` 内で生成される tmp ハコファイル(`__BUILDER_BOX__` 版)を最小ケースで切り出し、単体で parse/実行できるように修正。 + - [ ] `args` 未定義エラーや `Invalid expression` の原因となっている記述を特定し、Runner 側の `Main.main(args)` などを正しく宣言する。 + - [ ] Stage‑3 構文の使用を必要最小限に抑え、selfhost builder 用 Runner のコードをシンプルに保つ。 + - [x] Stage‑3 パーサで予約語となった `fn` をローカル変数名として使っている箇所(例: `lang/src/mir/builder/func_lowering.hako` の `local fn = func_jsons.length()`)をリネームし、`Unexpected token FN, expected identifier` を根本的に解消する。 +- [ ] `try_selfhost_builder` を **第一候補** とし、代表ケース(launcher.hako 等)で常にここが成功することを確認。 + - [ ] `HAKO_SELFHOST_BUILDER_FIRST=1` で `tools/hakorune_emit_mir.sh` を叩いたときに `[OK] MIR JSON written (selfhost-first)` まで到達することをスモークで確認。 + +### C. Provider / legacy delegate の整理 + +- [x] provider 経路(`env.mirbuilder.emit`)での `undefined variable: args` 原因を修正し、Stage‑B が出力する Program(JSON v0) を正しく受理できるようにする。 + - [ ] `HAKO_V1_EXTERN_PROVIDER` / `HAKO_V1_EXTERN_PROVIDER_C_ABI` トグルのデフォルトを見直し、「selfhost builder が成功するなら provider には落ちない」構造に寄せる。 +- [ ] legacy CLI 経路(`--program-json-to-mir`)は、「selfhost builder が失敗したときだけ最後に試す」退避路として残しつつ、代表ケースでは通さない方針にする。 +- [ ] 必要であれば Phase 25.1a 中は `HAKO_SELFHOST_NO_DELEGATE=1` を既定 ON に近い扱いにし、「selfhost builder が通る範囲」に問題を絞る。 + +### D. build_stage1.sh / selfhost_exe_stageb.sh 復旧 + +- [x] `NYASH_LLVM_SKIP_BUILD=1 tools/selfhost/build_stage1.sh --out /tmp/hakorune-dev` が 0 exit すること(現状は selfhost builder を既定OFFにし、provider ルートで MIR を生成)。 +- [ ] 生成された `/tmp/hakorune-dev` について: + - [ ] `./hakorune-dev emit program-json apps/selfhost-minimal/main.hako` が Program(JSON v0) を出力すること。 + - [ ] `./hakorune-dev emit mir-json apps/selfhost-minimal/main.hako` が MIR(JSON) を出力すること。 + - [ ] `./hakorune-dev build exe -o /tmp/hako_min apps/selfhost-minimal/main.hako` で簡単な EXE が生成され、実行して 0 exit を返すこと。 +- [ ] `tools/selfhost_exe_stageb.sh` についても同様に `.hako → EXE` のスモークを通しておく(少なくとも launcher.hako / apps/selfhost-minimal/main.hako の2ケース)。 + +## 25.1 / 25.1a / 25.2 の関係 + +- Phase 25.1: + - Stage0/Stage1 の責務とバイナリレイアウトを設計し、Stage1 CLI(launcher.hako)の顔と構文を固めるフェーズ。 +- Phase 25.1a(本ファイル): + - 「設計した Stage1 CLI / selfhost パイプラインが実際に動くようにする」緊急ホットフィックスフェーズ。 + - Scope はあくまで **Program→MIR と selfhost AOT の復旧** に限定し、numeric_core などの最適化には踏み込まない。 +- Phase 25.2: + - numeric_core AOT / microbench 統合・性能チューニングにフォーカス(`matmul_core` など)。 + - 25.1a で安定化した selfhost パイプラインの上に乗せる形で進める。 + +## Related docs + +- `docs/development/roadmap/phases/phase-25.1/README.md` … Stage0/Stage1 Bootstrap & Binary Layout(設計+初期実装)。 +- `docs/development/roadmap/phases/phase-25/README.md` … Ring0/Ring1 再編と numeric_core BoxCall→Call パス。 +- `docs/development/runtime/cli-hakorune-stage1.md` … Stage1 hakorune CLI のサブコマンド設計と実装範囲。 +- `tools/hakorune_emit_mir.sh` … Stage‑B → Program(JSON v0) → MIR(JSON) の selfhost+delegate パイプライン。 +- `tools/selfhost_exe_stageb.sh` / `tools/selfhost/build_stage1.sh` … `.hako → MIR(JSON) → EXE` selfhost AOT パス。*** +- Notes: + - selfhost builder (`HAKO_SELFHOST_BUILDER_FIRST=1`) は依然として parse error で落ちるため、Phase 25.1a では **既定を 0(無効)** に切り替え、provider ルートを安定化させた。 + - builder-first 経路の再有効化は Phase 25.1a 中の後続タスクとして扱う。 diff --git a/docs/development/roadmap/phases/phase-25.2/README.md b/docs/development/roadmap/phases/phase-25.2/README.md new file mode 100644 index 00000000..142303b3 --- /dev/null +++ b/docs/development/roadmap/phases/phase-25.2/README.md @@ -0,0 +1,47 @@ +# Phase 25.2 — Numeric Microbench & EXE Tuning + +Status: proposal(Phase 25 / 25.1 の後続フェーズ) + +## ゴール + +- Phase 25 で整備した numeric core / AotPrep / `NYASH_AOT_NUMERIC_CORE` の仕組みを前提に、 + - `matmul_core` を含む numeric 系 microbench(LLVM/EXE)を安定して実行できる状態にする。 + - EXE/LLVM ラインでの性能を観測しやすくし、「VM 側の自己ホスト部分の重さ」と「生成された EXE の速さ」を分離して評価できるようにする。 +- Phase 25.1 の Stage0/Stage1 設計に沿って、将来的に Stage1(EXE) から microbench を叩く土台を作る。 + +## スコープ(Phase 25.2) + +### 1) matmul_core microbench EXE ラインの安定化 + +- 対象: + - `tools/perf/microbench.sh --case matmul_core --backend llvm --exe ...` +- 目標: + - `NYASH_AOT_NUMERIC_CORE=1` ON の状態で、`matmul_core` microbench が LLVM/EXE 経路で安定して実行できること。 + - STRICT(`NYASH_AOT_NUMERIC_CORE_STRICT=1`)は AotPrep 後の MIR(JSON) に対してのみ適用し、pre-AotPrep の MIR emit/VM 起動を阻害しないこと。 +- タスク(例): + - provider 経路(`env.mirbuilder.emit`)の安定化と診断強化(VM ハングや長時間化の原因切り分け)。 + - `NYASH_LLVM_DUMP_MIR_IN` を使った実際の `matmul_core` MIR 形状の観察と numeric_core パスの適用確認。 + +### 2) コンパイル経路とベンチ経路の分離 + +- 問題意識: + - 現状 microbench は「`.hako → Program(JSON) → MIR(JSON) → AotPrep → ny-llvmc → EXE → 実行」を 1 コマンドで行うため、selfhost VM 部分の重さと EXE 実行の重さが混ざりやすい。 +- 方針: + - `matmul_core` 用に: + - 一度だけ MIR(JSON) を生成し、その JSON を複数回再利用するモードを用意する(例: `tools/dev_numeric_core_prep.sh` + `ny-llvmc` 直呼び)。 + - microbench は「既に生成済みの EXE を何度も実行する」モードと、「.hako からフルコンパイルする」モードを分ける。 + +### 3) numeric_core STRICT モードの本番運用ルール + +- Phase 25 では: + - STRICT は AotPrep 後の MIR(JSON) に対してのみチェックするように整理済み。 + - pre-AotPrep 生 MIR へのチェックは Rust 側から削除済み。 +- Phase 25.2 での追加整理: + - microbench / CI で `NYASH_AOT_NUMERIC_CORE_STRICT=1` を使う場合のガイドラインを docs に追記。 + - STRICT 違反時の代表的な原因(numeric_core がマッチできないパターン)を例示し、デバッグ手順を `DEBUG_NUMERIC_CORE.md` に統合。 + +## スコープ外 + +- Stage0/Stage1 の設計・導線整理自体は Phase 25.1 の責務(本フェーズでは利用側の調整に留める)。 +- 新しい numeric ABI の機能追加や IntArrayCore/MatI64 の API 拡張(根幹設計は Phase 25 側で担当)。 + diff --git a/docs/development/roadmap/phases/phase-25/DEBUG_NUMERIC_CORE.md b/docs/development/roadmap/phases/phase-25/DEBUG_NUMERIC_CORE.md new file mode 100644 index 00000000..7957e510 --- /dev/null +++ b/docs/development/roadmap/phases/phase-25/DEBUG_NUMERIC_CORE.md @@ -0,0 +1,118 @@ +# Phase 25 — numeric_core / AotPrep デバッグ指針 + +Status: memo(Claude Code / Codex 向け運用ガイド) + +Phase 25 で導入した `numeric_core.hako`(BoxCall→Call 変換パス)が動かない・怪しいときに辿るルートをまとめる。 +Rust 側の修正に入る前に、ここに沿って Hako 側の構造・設定を確認する。 + +## 1. まず「パスが本当に動いているか」を確認する + +### 1-1. using / nyash.toml マッピング + +- `nyash.toml` に次のマッピングが存在するか確認する: + - `selfhost.llvm.ir.aot_prep.passes.numeric_core = "lang/src/llvm_ir/boxes/aot_prep/passes/numeric_core.hako"` +- `numeric_core` が見つからない場合でも致命的エラーにならず、単にパスがロードされないだけ、という挙動になりがちなので注意する。 + +### 1-2. AotPrep.run_json 経由での実行確認 + +- `tools/hakorune_emit_mir.sh` を使って、AotPrep が numeric_core を呼んでいるかを確認する: + - `HAKO_APPLY_AOT_PREP=1` + - `NYASH_AOT_NUMERIC_CORE=1` + - `NYASH_AOT_NUMERIC_CORE_TRACE=1` +- 期待するログ: + - `[aot/numeric_core] PASS RUNNING` + - MatI64 検出や変換結果に関するメッセージ。 +- これらが 1 行も出ない場合: + - `using selfhost.llvm.ir.aot_prep.passes.numeric_core as AotPrepNumericCoreBox` が解決できていない + - もしくは `NYASH_AOT_NUMERIC_CORE` が子プロセスに渡っていない + 可能性が高い。 + +## 2. standalone で numeric_core を動かしてみる + +### 2-1. 最小 MIR(JSON) 入力を用意する + +- `MatI64.mul_naive` を 1 回だけ呼ぶような小さな .hako を用意し、MIR(JSON v0) を吐き出す: + - 例: `tmp/matmul_core_mir2.json` +- 重要なのは、JSON 内に次の 2 種類の命令が含まれていること: + - `"op":"const_string", "value":"MatI64"` 相当の文字列定数。 + - `"op":"boxcall", "box":..., "method":"mul_naive", ...` の BoxCall。 + +### 2-2. numeric_core.hako を直接呼ぶ + +- selfhost 側で `AotPrepNumericCoreBox.run(json_text)` を直接呼び出す小さなハーネスを作るか、既存のテストスクリプト(例: `/tmp/run_numeric_core_test.sh`)を流用する。 +- 期待する振る舞い: + - 型テーブルに MatI64 関連のエントリが 2〜3 件登録される。 + - BoxCall が `Call("NyNumericMatI64.mul_naive", args=[receiver, ...])` に 1 件だけ変換される。 + - 元の BoxCall 行は JSON から削除されている。 + +## 3. JSON スキャンの典型的な落とし穴 + +### 3-1. JSON 全体を 1 オブジェクトとして扱ってしまうバグ + +- 過去のバグ: + - `text.indexOf("{", pos)` でオブジェクト開始を探した結果、JSON 全体のルート `{` を拾ってしまい、 + - `instructions` 配列の全要素を 1 つの巨大な「inst」として扱ってしまった。 +- 結果: + - 最初の命令の `dst:0` と、どこかにある `"MatI64"` 文字列が同じオブジェクトに含まれてしまい、型推論が完全に壊れる。 + +### 3-2. 修正パターン(op-marker-first) + +- オブジェクト境界は次の手順で決める: + 1. `pos` 以降で `"op":"` を検索する。 + 2. 見つかった位置から後方へ `lastIndexOf("{", op_pos)` して、その命令オブジェクトの開始位置を求める。 + 3. その開始位置から `_seek_object_end(text, obj_start)` を呼んで終了位置を決める。 +- ポイント: + - `_seek_object_end` 自体は「開始位置を与えればそのオブジェクトの終端を返す」役割に限定し、 + - 「どの `{` を開始とみなすか」という責務は外側のスキャンロジックに持たせる。 + +## 4. BoxCall→Call 変換結果の確認ポイント + +### 4-1. 変換前後の JSON 断片 + +- 変換前(例): + - `{"args":[3],"box":2,"dst":4,"method":"mul_naive","op":"boxcall"}` +- 変換後(期待): + - `{"dst":4,"op":"call","name":"NyNumericMatI64.mul_naive","args":[2,3]}` + +チェックリスト: +- [ ] `op:"boxcall"` が `"op":"call"` に変わっている。 +- [ ] `name` フィールドに `NyNumericMatI64.mul_naive` が入っている。 +- [ ] `args` の先頭が receiver(元の `box` のレジスタ)になっている。 +- [ ] 元の BoxCall 行が JSON から消えている。 + +### 4-2. MatI64 型検出ログ + +- TRACE=1 のとき、次のようなログが出ているか確認する: + - `[aot/numeric_core] MatI64.new() result at r2` + - `[aot/numeric_core] MatI64.new() result at r3` + - `[aot/numeric_core] type table size: 3` +- これらが出ていない場合: + - `"MatI64"` 文字列定数の検出に失敗しているか、 + - `MatI64.new` のパターンが期待とずれている(関数名・引数数など)のどちらか。 + +## 5. VM/LLVM ラインでの最終確認 + +### 5-1. VM ライン + +- `NYASH_AOT_NUMERIC_CORE=0`: + - 既存挙動が変わっていないか確認する(BoxCall のままでも OK)。 +- `NYASH_AOT_NUMERIC_CORE=1`: + - VM 実行結果が OFF のときと一致することを確認する。 + - 可能であれば、AotPrep 適用後の MIR(JSON) を一度ダンプし、`boxcall` が消えて `call` になっていることを目視確認する。 + +### 5-2. LLVM ライン + +- `NYASH_AOT_NUMERIC_CORE=1 NYASH_SKIP_TOML_ENV=1 NYASH_DISABLE_PLUGINS=1` など、最小構成で `--backend llvm --exe` による実行を行う。 +- 期待すること: + - LLVM コンパイルがエラーなく通る。 + - VM ラインと同じ数値結果が得られる。 + - IR ダンプを取った場合、`NyNumericMatI64.mul_naive` が通常の `call` として現れ、BoxCall 特有のノードは存在しない。 + +## 6. それでも原因が分からないとき + +- ここまでの手順を踏んでも原因が特定できない場合は、次の情報を CURRENT_TASK.md に貼ってから LLM にバトンタッチする: + - AotPrep 適用前後の MIR(JSON) の短い抜粋(変換対象の関数のみ)。 + - `NYASH_AOT_NUMERIC_CORE_TRACE=1` 時点の `[aot/numeric_core]` ログ。 + - 使用した .hako ファイル名とベンチ名(例: `matmul_core`)。 +- そのうえで、「numeric_core のどこまで動いていて、どの段階で期待と違うか」を一言で書いておくと、後続の LLM(Claude Code など)がすぐに再現・解析しやすくなる。 + diff --git a/docs/development/roadmap/phases/phase-25/README.md b/docs/development/roadmap/phases/phase-25/README.md index 897cb541..ed0246da 100644 --- a/docs/development/roadmap/phases/phase-25/README.md +++ b/docs/development/roadmap/phases/phase-25/README.md @@ -1,6 +1,66 @@ # Phase 25 — 脱Rustランタイム / Ring0-Ring1 再編 -Status: proposal(設計フェーズ・実装は後続ホスト想定) +**Status: ✅ MVP COMPLETED** (2025-11-15) + +## 🎉 Phase 25 MVP 完全成功! + +**numeric_core BoxCall→Call 変換** が完全動作確認済み! + +### 主要成果 (2025-11-14 → 2025-11-15) + +1. **✅ 型伝播システム完全動作**: + - 4回反復型伝播で copy → phi → copy チェーン完全対応 + - MatI64 型を15レジスタまで正しく追跡 + - PHI 検出バグ修正(8d9bbc40): `indexOf("{")` → `indexOf("\"op\":\"")` + +2. **✅ 両SSAパターン対応確認**: + - 冗長版(13 PHI nodes): test_direct.json, test_matmul_debug.json + - 最適化版(1 PHI node): test_matmul_with_wrapper.json, microbench_matmul_core.json + - すべてのパターンで BoxCall → Call 変換成功 ✅ + +3. **✅ 環境変数伝播修正** (3d082ca1): + - microbench.sh に `NYASH_AOT_NUMERIC_CORE` と `NYASH_AOT_NUMERIC_CORE_TRACE` 伝播追加 + - `tools/perf/microbench.sh --case matmul_core --backend llvm --exe` で完全動作 + +4. **✅ ログ転送問題根治**: + - hakorune_emit_mir.sh の provider 経路にログ転送追加(ユーザー実装) + - `[aot/numeric_core]` ログが NYASH_AOT_NUMERIC_CORE_TRACE=1 で正しく表示 + +5. **✅ 開発ワークフロー確立**: + - `tools/dev_numeric_core_prep.sh` で環境変数自動設定 + - 推奨開発フロー確立・ドキュメント化完了 + +### 変換例 + +**Before** (BoxCall): +```json +{"args":[15],"box":7,"dst":53,"method":"mul_naive","op":"boxcall"} +``` + +**After** (Call): +```json +{"dst":53,"op":"call","name":"NyNumericMatI64.mul_naive","args":[7,15]} +``` + +### 既知の制限・次フェーズ + +- `NYASH_AOT_NUMERIC_CORE_STRICT=1`: 検証関数実装済みだが未使用(タイミング問題) +- microbench 性能チューニング: **Phase 25.2** に移管 +- 他の numeric メソッド(add, sub, etc.): 将来対応 + +--- + +## Phase status (2025-11-14 - 初期バージョン): +- このフェーズでは「Ring0/Ring1 の設計」と「numeric_core (MatI64.mul_naive) の BoxCall→Call 降ろし用 AotPrep パス」の MVP 実装までをカバーする。 +- `NYASH_AOT_NUMERIC_CORE=1` + AotPrep.run_json による MatI64.mul_naive 降ろしは、代表的な MIR パターン(13 PHI / 1 PHI 両方)で動作確認済み。 +- `NYASH_AOT_NUMERIC_CORE_STRICT=1` は AotPrep 後の MIR(JSON) に対してのみ BoxCall(mul_naive) 残存をチェックするように整理済み(pre-AotPrep の MirBuilder には干渉しない)。 +- microbench(`tools/perf/microbench.sh --case matmul_core --backend llvm --exe`)による EXE/LLVM ベンチ統合と性能チューニングは **Phase 25.2** に移管する。 + +Related docs: +- `docs/development/roadmap/phases/phase-25.1/README.md` … Stage0(Rust bootstrap)/Stage1(Hakorune selfhost)によるバイナリ二段構えの設計。 +- `docs/development/runtime/NUMERIC_ABI.md` … IntArrayCore/MatI64 など numeric ABI の関数契約。 +- `docs/development/runtime/system-hakorune-subset.md` … Ring1/System Hakorune サブセットの範囲と責務。 +- `docs/development/runtime/ENV_VARS.md` … `NYASH_AOT_NUMERIC_CORE` など Phase 25 関連の環境変数。 ## ゴール diff --git a/docs/development/runtime/cli-hakorune-stage1.md b/docs/development/runtime/cli-hakorune-stage1.md new file mode 100644 index 00000000..06f4df7c --- /dev/null +++ b/docs/development/runtime/cli-hakorune-stage1.md @@ -0,0 +1,241 @@ +# Stage1 Hakorune CLI Design(Proposal) + +Status: design-only(Phase 25.1 時点では仕様策定と導線の整理まで) + +## ゴール + +- 「ユーザー/開発者が日常叩く CLI」としての `hakorune`(Stage1 selfhost バイナリ)のインターフェースを定義する。 +- 既存の Rust CLI(Stage0: `nyash`)が提供している機能を、「パイプライン志向」の小さなサブコマンドに整理し直す。 +- Stage1 は **パイプラインのオーケストレーションのみ** 担当し、MIR 実行・LLVM コード生成などの実行コアは Stage0/Rust に委譲する。 + +## バイナリとプロセスモデル + +- Stage0(Rust CLI / ランタイムサービス) + - 実体: `target/release/nyash`(将来: `hakorune-bootstrap`) + - 役割: プロセス起動・VM/LLVM コア・ny-llvmc 呼び出しなどの「Ring0」。 + - Ny 側からは `env.mirbuilder.emit` / `env.codegen.emit_object` / `env.codegen.link_object` などの extern 名で見える。 + - Stage1 実行時は「CLI として」ではなく、これらのランタイムサービス層(C-ABI/extern)として利用することを前提とする。 +- Stage1(Hakorune selfhost CLI) + - 実体: `target/selfhost/hakorune`(Phase 25.1 では Ny Executor プロトタイプ)。 + - 役割: `.hako → Program(JSON) → MIR(JSON) → 実行/EXE` というパイプラインの制御。 + - 将来的には、ユーザーが直接叩く標準 CLI として昇格(`PATH` に入れる対象)。 + +プロセスモデルの原則: +- Stage1 は **自分で VM/LLVM を実装しない**。常に Ring0 のサービス(env.codegen/env.mirbuilder, NyRT/ny-llvmc 等)を経由して実行・AOT する。 +- Stage1 CLI は「どのステージまで進めるか」と「どのバックエンドで実行/ビルドするか」を宣言的に指定するだけに留める。 +- Stage1 バイナリ自体は Stage0 の CLI からは独立しており、Stage0 はあくまで「ブートストラップおよびランタイムサービス提供者」として扱う。 + +## トップレベル構文 + +```text +hakorune [] [options] [-- script_args...] +``` + +- `command`/`subcommand` は **パイプラインの到達点** を表す。 +- `options` は主に: + - 入出力ファイル + - backend 選択(vm/llvm/pyvm) + - profile(dev/ci/lite 等のプリセット) +を指定する。 +- `--` 以降はユーザープログラムに渡す引数(既存の `NYASH_SCRIPT_ARGS_JSON` 経路と整合させる)。 + +## コマンド一覧(MVP 案) + +| コマンド | 役割 | +|-----------------------------------|-------------------------------------------| +| `run` | .hako をコンパイルして実行(既定 VM) | +| `build exe` | .hako からネイティブ EXE を AOT ビルド | +| `emit program-json` | Stage‑B で Program(JSON v0) を出力 | +| `emit mir-json` | Program(JSON) → MIR(JSON) を出力 | +| `check` | 将来の構文/型/using チェック(予約) | + +Phase 25.1 では、既存スクリプト置き換えに近い **`emit mir-json` / `build exe` を中心** に進め、`run`/`check` は設計レベルに留める。 + +## `run` コマンド + +```text +hakorune run [options] [-- script_args...] +``` + +### 意味論 + +- `.hako` ソースを Stage‑B → MirBuilder → AotPrep まで通し、選択された backend で実行する。 +- 実行経路: + - backend=`vm` : Stage0 Rust VM(現行 `--backend vm` 相当) + - backend=`llvm` : ny-llvmc+NyRT を通した EXE 実行(実装は後続フェーズ) + - backend=`pyvm` : PyVM 経路(Phase‑15 の方針に従い、開発/検証専用) +- プログラムの戻り値をプロセスの exit code にマッピング(現行 Rust CLI と同じ)。 + +### 主なオプション案 + +- `--backend {vm|llvm|pyvm}`(既定: `vm`) +- `--profile {dev|ci|lite|strict}` + - `dev` : 詳細ログ・トレースを有効(Phase 15/25 の既存 ENV を束ねる) + - `ci` : 安定志向・プラグイン無効化など(現行 quick profile 相当) + - `lite` : macro/using など重い機能をオフ + - `strict`: 各種 STRICT トグルを有効(AotPrep/Verifier 等) +- `--using-path `: `tools/dev_selfhost_loop.sh` の `--using-path` と一致させる。 +- `--json-only`(将来): Stage‑B までで止め、Program(JSON v0) を stdout に出力。 + +### Stage0 との関係 + +- Stage1 `run` は **直接 MIR を実行しない**。 +- 代わりに: + 1. Stage1 内で Program(JSON)/MIR(JSON) を構築。 + 2. `NYASH_VERIFY_JSON` / `NYASH_JSON_ONLY` / `NYASH_SCRIPT_ARGS_JSON` など既存の ENV プロトコルを用いて Stage0 プロセスを呼び出す。 +- これにより「Rust 側の VM/LLVM コアはそのまま」「CLI 表面だけ selfhost 化」という段階移行が可能になる。 + +## `build exe` コマンド + +```text +hakorune build exe [options] +``` + +### 意味論 + +- `.hako` からネイティブ EXE を生成する高レベル API。 +- Phase 25.1 実装では、`.hako → Program(JSON v0) → MIR(JSON) → env.codegen.emit_object/link_object → EXE` までを 1 コマンドで行う。 +- 具体的な呼び出し: + +```text +hakorune build exe [-o ] [--quiet] +``` + +### 意味論(Phase 25.1 実装範囲) + +- `.hako` から EXE までのパイプライン: + 1. `.hako` → Program(JSON v0): + - `BuildBox.emit_program_json_v0(src, null)` を呼び出し。 + - `"version":0` / `"kind":"Program"` を検査。 + 2. Program(JSON v0) → MIR(JSON): + - `MirBuilderBox.emit_from_program_json_v0(program_json, null)` を呼び出し。 + 3. MIR(JSON) → object: + - `hostbridge.extern_invoke("env.codegen", "emit_object", [mir_json])` を呼び出し、`.o` パスを取得。 + 4. object → EXE: + - `hostbridge.extern_invoke("env.codegen", "link_object", [obj_path, out?])` を呼び出し、EXE パスを取得。 +- 実行には C-API ルートの有効化が前提: + - 例: `NYASH_LLVM_USE_CAPI=1`, `HAKO_V1_EXTERN_PROVIDER_C_ABI=1`, `NYASH_EMIT_EXE_NYRT` など。 + +### オプション(Phase 25.1 実装済み) + +- `-o, --out `: + - 生成する EXE のパスを指定(省略時は env.codegen 側の既定パスを使用)。 +- `--quiet`: + - 成功時のステータス出力(`[hakorune] build exe: `)を抑制。 + +※ `--target` / `--nyrt` / `--skip-build` などは、現時点では未実装の設計(将来の AOT プロファイル用)。*** + +## `emit program-json` コマンド + +```text +hakorune emit program-json [options] +``` + +### 意味論(Phase 25.1 実装範囲) + +- `.hako` ソースファイル(``)を読み込み、`BuildBox.emit_program_json_v0(src, null)` を呼び出して Program(JSON v0) を生成する。 +- Phase 25.1 の実装では: + - 入力: `` パスのみ(標準入力や複数ファイルは未対応)。 + - 出力: + - 既定: Program(JSON v0) を stdout にそのまま出力。 + - `-o/--out` 指定時: JSON はファイルに書き込み、stdout には短いステータス行のみを出力。 + - バリデーション: `"version":0` と `"kind":"Program"` を含まない場合はエラー終了(exit code 92)。 + +### オプション(Phase 25.1 実装済み) + +- `-o, --out `: + - Program(JSON v0) を `` に書き出す。 + - スクリプト互換性のため、stdout には短いメッセージ(タグ)だけを出す(JSON 本文は出さない)。 +- `--quiet`: + - `-o/--out` と組み合わせた場合に、ステータス行も抑制し「完全に無音」のファイル出力にする。 + - `--quiet` 単独では意味を持たず、現状は無視される(将来のログ制御用に予約)。 + +## `emit mir-json` コマンド + +```text +hakorune emit mir-json [options] +``` + +### 意味論(Phase 25.1 実装範囲) + +- Program(JSON v0) から MIR(JSON) を出す経路に加えて、`.hako` から Program(JSON v0)→MIR(JSON) まで進める経路も実装済み。 +- 実際の CLI 呼び出しは主に次の2通り: + +```text +# 1) Program(JSON v0) から MIR(JSON) +hakorune emit mir-json --from-program-json + +# 2) .hako から直接 MIR(JSON) まで +hakorune emit mir-json [-o ] [--quiet] +``` + +- 処理内容: + - `--from-program-json` 指定時: + - FileBox で `` を読み込み、文字列として取得。 + - `MirBuilderBox.emit_from_program_json_v0(program_json, null)` を呼び出して MIR(JSON) を生成。 + - `.hako` 直接指定時: + - FileBox で `` を読み込み、`BuildBox.emit_program_json_v0(src, null)` で Program(JSON v0) を生成。 + - `"version":0` / `"kind":"Program"` を検査した上で、その Program(JSON v0) を `MirBuilderBox.emit_from_program_json_v0` に渡す。 + - 成功時は MIR(JSON) を stdout に出力(`-o/--out` 指定時はファイル出力)し、exit code 0。 + - 失敗時(ファイルエラー / builder null など)はエラーメッセージ+exit code 92。 + +### オプション(Phase 25.1 実装済み) + +- `--from-program-json `: + - Program(JSON v0) を含むファイルパスを指定。 + - `.hako` との併用は禁止(両方指定した場合はエラー)。 +- `-o, --out `: + - MIR(JSON) を `` に書き出す。 + - stdout に MIR(JSON) を直接出さなくなる(短いステータス行のみ)。 +- `--quiet`: + - `-o/--out` と併用時にステータス行も抑制し、MIR(JSON) をファイルにだけ書き込む。 + +※ `--force-jsonfrag` / `--normalize-provider` などは、引き続き設計のみで未実装。 + +## `check` コマンド(予約) + +```text +hakorune check [options] +``` + +### 意味論(将来) + +- Stage‑B / MirBuilder / AotPrep を **実行せずに**: + - 構文 + - using 解決 + - System Hakorune subset 制約 +などを検証するためのエントリポイント。 + +- 実装は `.hako` 側で `tools/hako-check` 相当のロジックを呼び出す想定。 +- Phase 25.1 では「名前予約」と「インターフェース定義」のみを行い、実装は Phase 26 以降。 + +## Stage0 / Stage1 の責務分離(CLI 視点) + +- Stage1(hakorune) + - ユーザー向け CLI surface。 + - パイプライン選択とオプション解釈。 + - JSON v0/v1 の配線・一時ファイル管理。 +- Stage0(nyash / hakorune-bootstrap) + - VM 実行(vm/backend=vm)。 + - LLVM ハーネス/ny-llvmc 経由の AOT(backend=llvm)。 + - env.codegen / env.mirbuilder などのホストブリッジ提供。 + +原則: +- Stage1 は **新しい意味論・最適化ロジックを持たない**。 +- Stage1 CLI は「どの Stage0/ny-llvmc サービスをどう呼ぶか」を決める **構造レイヤ**。 + +## フェーズ別導入計画(CLI 観点) + +- Phase 25.1 + - `build_stage1.sh` による Ny Executor EXE(`target/selfhost/hakorune`)を用意。 + - 本ドキュメントで CLI サブコマンドと引数の仕様を固定。 + - `hakorune emit mir-json` / `hakorune build exe` に対応する内部 API を `.hako` 側で設計(実装は最小限 or 後続)。 +- Phase 26 以降 + - `run` サブコマンドを実装し、日常的な `hakorune run apps/APP/main.hako` 導線を整備。 + - 既存の selfhost スクリプト(`selfhost_build.sh` / `hakorune_emit_mir.sh` / `selfhost_exe_stageb.sh`)を段階的に CLI 経由に移行。 + - `check` を `.hako` 側の hako-check ライブラリに接続。 + - Stage1 → Stage1' の自己ホストサイクルを検証する: + - Stage1 が自分自身(launcher/runner/CLI を含む)を AOT して Stage1' を生成できること。 + - Stage1 / Stage1' の CLI インターフェース・代表挙動が一致することをゴールデン/スモークで確認する。 + +このファイルは「Stage1 CLI の仕様 SSOT」として扱い、実装時は本仕様を先に更新→テスト→コードの順で進める。*** diff --git a/lang/README.md b/lang/README.md index a2982183..c61c7c78 100644 --- a/lang/README.md +++ b/lang/README.md @@ -27,11 +27,23 @@ Non‑Goals ## Selfhost Launcher (AOT) -- Build: `lang/build/build_runner.sh` → produces `lang/bin/hakorune` -- Requirements: LLVM 18 dev (`llvm-config-18`) -- Run: `lang/bin/hakorune` +### Dev line (Stage1 core – experimental) + +- Dev build: `tools/selfhost/build_stage1.sh` → produces `target/selfhost/hakorune` +- Role: + - Fast iteration用の Stage1 selfhost バイナリ(Ny Executor / CLI 実験など)。 + - new CLI/runner 機能はまずこちらで開発・検証する。 + +### Stable line (lang bin – snapshot) + +- Stable binary: `lang/bin/hakorune` +- Build (pure-lang launcher, legacy bring-up): + - `lang/build/build_runner.sh` → produces `lang/bin/hakorune` + - Requirements: LLVM 18 dev (`llvm-config-18`) +- Policy(Phase 25.1 以降の想定): + - `target/selfhost/hakorune` で十分に安定したら、その成果物を `lang/bin/hakorune` に昇格させる(手動コピー or 専用スクリプト)。 + - `lang/bin/hakorune` は「last known good」の Stage1 コア EXE として扱い、配布や外部からの参照時は原則こちらを基準にする。 Notes -- The launcher is minimal and prints a stable line `[lang-launcher] hello`. -- This binary is a stepping stone towards a full selfhosted CLI built from Hakorune scripts. - +- `lang/` 以下は「最終的に 1 つの Stage1 コア EXE(hakorune)を構成するソース群」という前提で整理する。 +- `target/selfhost/hakorune` は開発中の最新版、`lang/bin/hakorune` は安定版スナップショットという役割分担にする。 diff --git a/lang/src/compiler/hako_module.toml b/lang/src/compiler/hako_module.toml index b02f0a84..8eba35f6 100644 --- a/lang/src/compiler/hako_module.toml +++ b/lang/src/compiler/hako_module.toml @@ -34,6 +34,7 @@ builder.ssa.loop = "builder/ssa/loopssa.hako" builder.ssa.cond_inserter = "builder/ssa/cond_inserter.hako" builder.rewrite.special = "builder/rewrite/special.hako" builder.rewrite.known = "builder/rewrite/known.hako" +build.build_box = "build/build_box.hako" # Entry point modules (Phase 15 compiler infrastructure) entry.func_scanner = "entry/func_scanner.hako" diff --git a/lang/src/llvm_ir/boxes/aot_prep.hako b/lang/src/llvm_ir/boxes/aot_prep.hako index f096a9d3..f5364e57 100644 --- a/lang/src/llvm_ir/boxes/aot_prep.hako +++ b/lang/src/llvm_ir/boxes/aot_prep.hako @@ -381,6 +381,10 @@ static box AotPrepBox { if env.get("NYASH_AOT_COLLECTIONS_HOT") == "1" { local r = AotPrepPass_CollectionsHot.run(out); if r != null && r != "" { out = r } } + // Phase‑4: numeric core transformation (opt‑in) + if env.get("NYASH_AOT_NUMERIC_CORE") == "1" { + local nn = AotPrepPass_NumericCore.run(out); if nn != null && nn != "" { out = nn } + } if env.get("HAKO_MIR_NORMALIZE_PRINT") == "1" { local np = NormalizePrintBox.run(out); if np != null && np != "" { out = np } } diff --git a/lang/src/llvm_ir/hako_module.toml b/lang/src/llvm_ir/hako_module.toml index 0d984e58..433fc1bc 100644 --- a/lang/src/llvm_ir/hako_module.toml +++ b/lang/src/llvm_ir/hako_module.toml @@ -22,4 +22,5 @@ aot_prep.passes.const_dedup = "boxes/aot_prep/passes/const_dedup.hako" aot_prep.passes.binop_cse = "boxes/aot_prep/passes/binop_cse.hako" aot_prep.passes.collections_hot = "boxes/aot_prep/passes/collections_hot.hako" aot_prep.passes.fold_const_ret = "boxes/aot_prep/passes/fold_const_ret.hako" +aot_prep.passes.numeric_core = "boxes/aot_prep/passes/numeric_core.hako" aot_prep.helpers.common = "boxes/aot_prep/helpers/common.hako" diff --git a/lang/src/mir/builder/func_lowering.hako b/lang/src/mir/builder/func_lowering.hako index a24cad55..4536c53a 100644 --- a/lang/src/mir/builder/func_lowering.hako +++ b/lang/src/mir/builder/func_lowering.hako @@ -210,8 +210,8 @@ static box FuncLoweringBox { // Build additional functions JSON if func_jsons.length() > 0 { local fi = 0 - local fn = func_jsons.length() - loop(fi < fn) { + local func_len = func_jsons.length() + loop(fi < func_len) { func_defs_mir = func_defs_mir + "," + ("" + func_jsons.get(fi)) fi = fi + 1 } diff --git a/lang/src/runner/README.md b/lang/src/runner/README.md index 41c7fdb5..3a4a58d8 100644 --- a/lang/src/runner/README.md +++ b/lang/src/runner/README.md @@ -1,28 +1,44 @@ -# Runner Facade (Script‑First) — Phase 20.10 +# Runner Facade / Stage1 CLI — Runner Layer Guide -Responsibility -- Provide a thin, opt‑in runner facade in Hakorune (script) to orchestrate entry selection and pre/post hooks. -- Delegates actual execution to existing backends (Rust VM / LLVM). No behavior change by default. +## Responsibility +- Provide script-side orchestration primitives for execution: + - Runner facade (`runner_facade.hako`) for entry selection and pre/post hooks. + - Stage1 CLI launcher (`launcher.hako`) for top-level command dispatch. +- Delegate actual execution to existing backends(Rust VM / LLVM / ny-llvmc)。既定挙動は変えない。 -Gate -- Enable with `HAKO_SCRIPT_RUNNER=1` (default OFF). In 20.10 this runner is not wired by default; wiring will be added behind the gate. +## Files -Contracts (draft) -- Entry: `Runner.run(entry: string, args: array) -> i64` -- Pre‑hooks: validate entry, shape args, emit diagnostics (short tokens) on failure. -- Post‑hooks: normalize result (e.g., quiet result mode), optional metrics/logging. +- `runner_facade.hako` + - Contract(draft): + - Entry: `Runner.run(entry: string, args: array) -> i64` + - Gate: `HAKO_SCRIPT_RUNNER=1`(default OFF)。 + - Role: + - Script-first runner facade(Phase 20.10)。 + - Pre-hooks: validate entry/args, emit short diagnostics。 + - Post-hooks: normalize result / metrics(将来)。 + - Notes: + - Keep this layer pure; platform I/O は C-ABI 側に委譲。 + - Fail-Fast: invalid entry/args は非0で即終了。 + - Short diagnostics: + - Success: `[script-runner] invoke` + - Failure: `[script-runner] invoke: FAIL` -Notes -- Keep this layer pure and free of platform I/O. Defer I/O to C‑ABI utilities (`hako_*`). -- Fail‑Fast: invalid entry/args → emit short diagnostics and return non‑zero. -- Box‑First: add adapters for boundary objects (args, env) instead of sprinkling conditions. +- `launcher.hako` + - Contract(draft): + - Entry: `Main.main(args: array) -> i64` + - Role: Stage1 hakorune CLI のトップレベル dispatcher。 + - コマンド: `run` / `build` / `emit` / `check`(詳細は docs/development/runtime/cli-hakorune-stage1.md)。 + - Current status(Phase 25.1): + - 構造のみ実装(`HakoCli` box にコマンド別のメソッドを定義)。 + - 各コマンドはまだプレースホルダで、`"[hakorune] : not implemented yet"` を出力して終了コード 90–93 を返す。 + - 実際のパイプライン(Stage‑B / MirBuilder / AotPrep / ny-llvmc など)への接続は後続フェーズで段階的に実装する。 + - Design reference: + - `docs/development/runtime/cli-hakorune-stage1.md` を Stage1 CLI の仕様 SSOT として参照すること。 -Short Diagnostics & Observability -- Pre‑invoke emits stable one‑liners for smokes: - - Success: `[script-runner] invoke` (stdout) - - Failure (dev injection or panic): `[script-runner] invoke: FAIL` (stdout) -- The runner wiring prints a gate trace to stderr: `[script-runner] gate=on (facade)`. - -Dev‑only toggle (TTL: Phase 20.10 bring‑up) -- `HAKO_SCRIPT_RUNNER_FORCE_FAIL=1` forces the pre‑invoke to emit `[script-runner] invoke: FAIL` without executing the facade program. -- Scope: tests/smokes only. Remove after runner wiring stabilizes (documented here as a temporary aid). +## Notes +- Runner 層は「構造とオーケストレーション専用レイヤ」として扱う。 + - 言語意味論・最適化ロジックは compiler / opt / AotPrep に留める。 + - VM/LLVM の実行コアは Rust 側(Stage0 / NyRT)に委譲する。 +- Fail-Fast 原則: + - 未実装コマンドや不正な引数は明示的なメッセージ+非0終了コードで返す。 + - 暗黙のフォールバックや静かな無視は行わない。 diff --git a/lang/src/runner/launcher.hako b/lang/src/runner/launcher.hako index ce043512..55db06b3 100644 --- a/lang/src/runner/launcher.hako +++ b/lang/src/runner/launcher.hako @@ -1,9 +1,441 @@ -// launcher.hako — Minimal selfhost launcher (AOT target) -// Goal: produce lang/bin/hakorune via tools/build_llvm.sh +// launcher.hako — Stage1 Hakorune CLI launcher (draft) +// +// Responsibility +// - Provide a thin, script-side CLI entrypoint for Stage1 hakorune. +// - Parse top-level command/subcommand and delegate to dedicated handlers. +// - Keep implementation fail-fast and minimal; real work is delegated to +// runner / compiler / AOT helpers and ultimately Stage0 (Rust) services. +// +// Notes +// - CLI surface is specified in docs/development/runtime/cli-hakorune-stage1.md. +// - This file only defines the structural dispatcher; individual commands +// are implemented incrementally in later phases. -static box Main { - // No-args main() to avoid runtime Array allocation in AOT - main(){ - return 0; +using lang.mir.builder.MirBuilderBox +using lang.compiler.build.build_box as BuildBox + +static box HakoCli { + // Entry from Main.main(args) + method run(args){ + @argc = 0 + if args { argc = args.size() } + if argc == 0 { + // No command: print minimal usage and fail-fast + print("[hakorune] usage: hakorune [options]") + return 1 + } + + @cmd_raw = args.get(0) + @cmd = "" + cmd_raw + + if cmd == "run" { + return self.cmd_run(args) + } else if cmd == "build" { + return self.cmd_build(args) + } else if cmd == "emit" { + return self.cmd_emit(args) + } else if cmd == "check" { + return self.cmd_check(args) + } + + print("[hakorune] unknown command: " + cmd) + return 2 + } + + // hakorune run [options] [-- script_args...] + // - See docs/development/runtime/cli-hakorune-stage1.md for semantics. + // - Phase 25.1: placeholder only(構造だけ定義し、後続フェーズで実装)。 + method cmd_run(args){ + print("[hakorune] run: not implemented yet") + return 90 + } + + // hakorune build exe [options] + // - High-level wrapper around selfhost_exe_stageb / ny_mir_builder. + method cmd_build(args){ + @argc = 0 + if args { argc = args.size() } + if argc < 2 { + print("[hakorune] build: usage: hakorune build exe ") + return 91 + } + + @sub_raw = args.get(1) + @sub = "" + sub_raw + if sub == "exe" { + return self.cmd_build_exe(args) + } + + print("[hakorune] build: unknown subcommand: " + sub) + return 91 + } + + // hakorune build exe [-o ] [--quiet] + // - Pipeline: .hako → Program(JSON v0) → MIR(JSON) → env.codegen.emit_object/link_object → EXE + // - Requires env.codegen C-API route to be有効化されていること(NYASH_LLVM_USE_CAPI=1, HAKO_V1_EXTERN_PROVIDER_C_ABI=1 等)。 + method cmd_build_exe(args){ + @argc = 0 + if args { argc = args.size() } + if argc < 3 { + print("[hakorune] build exe: usage: hakorune build exe [-o ] [--quiet] ") + return 91 + } + + @out_path = null + @quiet = 0 + @source_path = null + + @i = 2 + while i < argc { + @arg_raw = args.get(i) + @arg = "" + arg_raw + if arg == "-o" || arg == "--out" { + if i + 1 >= argc { + print("[hakorune] build exe: -o/--out requires a path") + return 91 + } + @op_raw = args.get(i + 1) + out_path = "" + op_raw + i = i + 2 + } else if arg == "--quiet" { + quiet = 1 + i = i + 1 + } else { + if source_path == null { + source_path = arg + i = i + 1 + } else { + print("[hakorune] build exe: unexpected extra argument: " + arg) + return 91 + } + } + } + + if source_path == null || source_path == "" { + print("[hakorune] build exe: source path is required") + return 91 + } + + // Read Hako source + @fb = new FileBox() + @ok = fb.open(source_path, "r") + if ok != 1 { + print("[hakorune] build exe: failed to open source " + source_path) + return 91 + } + @src = fb.read() + fb.close() + if src == null || src == "" { + print("[hakorune] build exe: source is empty") + return 91 + } + + // .hako → Program(JSON v0) + @prog = BuildBox.emit_program_json_v0(src, null) + if prog == null { + print("[hakorune] build exe: BuildBox returned null") + return 91 + } + @ps = "" + prog + if ps.indexOf("\"version\":0") < 0 || ps.indexOf("\"kind\":\"Program\"") < 0 { + print("[hakorune] build exe: unexpected Program(JSON) output (missing version/kind)") + return 91 + } + + // Program(JSON v0) → MIR(JSON) + @mir = MirBuilderBox.emit_from_program_json_v0(ps, null) + if mir == null { + print("[hakorune] build exe: MirBuilderBox returned null") + return 91 + } + @ms = "" + mir + + // MIR(JSON) → object via env.codegen.emit_object + @emit_args = new ArrayBox() + emit_args.push(ms) + @obj = hostbridge.extern_invoke("env.codegen", "emit_object", emit_args) + if obj == null || ("" + obj) == "" { + print("[hakorune] build exe: env.codegen.emit_object failed") + return 91 + } + @obj_path = "" + obj + + // object → EXE via env.codegen.link_object + @link_args = new ArrayBox() + link_args.push(obj_path) + if out_path != null && out_path != "" { + link_args.push(out_path) + } + @exe = hostbridge.extern_invoke("env.codegen", "link_object", link_args) + if exe == null || ("" + exe) == "" { + print("[hakorune] build exe: env.codegen.link_object failed") + return 91 + } + @exe_path = "" + exe + + if quiet != 1 { + print("[hakorune] build exe: " + exe_path) + } + return 0 + } + + // hakorune emit {program-json|mir-json} ... + // - Surface for Stage-B / MirBuilder pipeline. + method cmd_emit(args){ + @argc = 0 + if args { argc = args.size() } + if argc < 2 { + print("[hakorune] emit: usage: hakorune emit mir-json --from-program-json ") + return 92 + } + + @sub_raw = args.get(1) + @sub = "" + sub_raw + if sub == "mir-json" { + return self.cmd_emit_mir_json(args) + } else if sub == "program-json" { + return self.cmd_emit_program_json(args) + } + + print("[hakorune] emit: unknown subcommand: " + sub) + return 92 + } + + // hakorune emit program-json + // - Compile Hako source to Program(JSON v0) using BuildBox.emit_program_json_v0. + // - Output is written to stdout on success. + method cmd_emit_program_json(args){ + @argc = 0 + if args { argc = args.size() } + if argc < 3 { + print("[hakorune] emit program-json: usage: hakorune emit program-json [-o ] [--quiet] ") + return 92 + } + + @path = null + @out_path = null + @quiet = 0 + + @i = 2 + while i < argc { + @arg_raw = args.get(i) + @arg = "" + arg_raw + if arg == "-o" || arg == "--out" { + if i + 1 >= argc { + print("[hakorune] emit program-json: -o/--out requires a path") + return 92 + } + @op_raw = args.get(i + 1) + out_path = "" + op_raw + i = i + 2 + } else if arg == "--quiet" { + quiet = 1 + i = i + 1 + } else { + if path == null { + path = arg + i = i + 1 + } else { + print("[hakorune] emit program-json: unexpected extra argument: " + arg) + return 92 + } + } + } + + if path == null || path == "" { + print("[hakorune] emit program-json: source path is required") + return 92 + } + + // Read Hako source from file + @fb = new FileBox() + @ok = fb.open(path, "r") + if ok != 1 { + print("[hakorune] emit program-json: failed to open " + path) + return 92 + } + @src = fb.read() + fb.close() + if src == null || src == "" { + print("[hakorune] emit program-json: source is empty") + return 92 + } + + // Compile to Program(JSON v0) via BuildBox + @prog = BuildBox.emit_program_json_v0(src, null) + if prog == null { + print("[hakorune] emit program-json: BuildBox returned null") + return 92 + } + + // Minimal validation: require Program(version 0) + @ps = "" + prog + if ps.indexOf("\"version\":0") < 0 || ps.indexOf("\"kind\":\"Program\"") < 0 { + print("[hakorune] emit program-json: unexpected output (missing version/kind)") + return 92 + } + + if out_path != null && out_path != "" { + @out_fb = new FileBox() + @ok2 = out_fb.open(out_path, "w") + if ok2 != 1 { + print("[hakorune] emit program-json: failed to open output " + out_path) + return 92 + } + out_fb.write(ps) + out_fb.close() + if quiet != 1 { + print("[hakorune] emit program-json: written " + out_path) + } + } else { + print(ps) + } + + return 0 + } + + // hakorune emit mir-json [--from-program-json ] [-o ] [--quiet] [] + // - Program(JSON v0) から MIR(JSON) を生成する経路に加えて、 + // .hako ソースから直接 Program(JSON v0)→MIR(JSON) まで進める経路もサポートする。 + method cmd_emit_mir_json(args){ + @argc = 0 + if args { argc = args.size() } + + @prog_path = null + @source_path = null + @out_path = null + @quiet = 0 + @i = 2 + while i < argc { + @arg_raw = args.get(i) + @arg = "" + arg_raw + if arg == "--from-program-json" { + if i + 1 >= argc { + print("[hakorune] emit mir-json: --from-program-json requires a path") + return 92 + } + @p_raw = args.get(i + 1) + prog_path = "" + p_raw + i = i + 2 + } else if arg == "-o" || arg == "--out" { + if i + 1 >= argc { + print("[hakorune] emit mir-json: -o/--out requires a path") + return 92 + } + @op_raw = args.get(i + 1) + out_path = "" + op_raw + i = i + 2 + } else if arg == "--quiet" { + quiet = 1 + i = i + 1 + } else { + // Interpret as when not already set + if source_path == null { + source_path = arg + i = i + 1 + } else { + print("[hakorune] emit mir-json: unknown or unsupported flag: " + arg) + return 92 + } + } + } + + // Prevent ambiguous usage + if prog_path != null && prog_path != "" && source_path != null && source_path != "" { + print("[hakorune] emit mir-json: specify either --from-program-json or , not both") + return 92 + } + + @prog_json = null + + if prog_path != null && prog_path != "" { + // Read Program(JSON v0) from file + @fb = new FileBox() + @ok = fb.open(prog_path, "r") + if ok != 1 { + print("[hakorune] emit mir-json: failed to open " + prog_path) + return 92 + } + @p = fb.read() + fb.close() + if p == null || p == "" { + print("[hakorune] emit mir-json: program JSON is empty") + return 92 + } + prog_json = p + } else { + // Expect .hako source path + if source_path == null || source_path == "" { + print("[hakorune] emit mir-json: require --from-program-json or ") + return 92 + } + @fb2 = new FileBox() + @ok2 = fb2.open(source_path, "r") + if ok2 != 1 { + print("[hakorune] emit mir-json: failed to open source " + source_path) + return 92 + } + @src = fb2.read() + fb2.close() + if src == null || src == "" { + print("[hakorune] emit mir-json: source is empty") + return 92 + } + // Compile to Program(JSON v0) via BuildBox + @prog = BuildBox.emit_program_json_v0(src, null) + if prog == null { + print("[hakorune] emit mir-json: BuildBox returned null") + return 92 + } + @ps = "" + prog + if ps.indexOf("\"version\":0") < 0 || ps.indexOf("\"kind\":\"Program\"") < 0 { + print("[hakorune] emit mir-json: unexpected Program(JSON) output (missing version/kind)") + return 92 + } + prog_json = ps + } + + // Convert Program(JSON v0) → MIR(JSON) via MirBuilderBox + @mir = MirBuilderBox.emit_from_program_json_v0(prog_json, null) + if mir == null { + print("[hakorune] emit mir-json: MirBuilderBox returned null") + return 92 + } + + // Success: emit MIR(JSON) to stdout or file + @ms = "" + mir + if out_path != null && out_path != "" { + @out_fb = new FileBox() + @ok3 = out_fb.open(out_path, "w") + if ok3 != 1 { + print("[hakorune] emit mir-json: failed to open output " + out_path) + return 92 + } + out_fb.write(ms) + out_fb.close() + if quiet != 1 { + print("[hakorune] emit mir-json: written " + out_path) + } + } else { + print(ms) + } + + return 0 + } + + // hakorune check + // - Reserved for future static checks (syntax/using/subset). + method cmd_check(args){ + print("[hakorune] check: not implemented yet") + return 93 + } +} + +static box Main { + // Main entrypoint for Stage1 hakorune CLI. + // args: raw argv array (excluding executable name). + main(args){ + @cli = new HakoCli() + return cli.run(args) } } diff --git a/lang/src/vm/README.md b/lang/src/vm/README.md index 89003c26..6e55137d 100644 --- a/lang/src/vm/README.md +++ b/lang/src/vm/README.md @@ -162,6 +162,7 @@ Strictness & Tolerance (ENV policy) - 目的: pred不一致などPHI入力の欠落をFail‑Fastで検出。 - VMステップ上限(無限ループ対策) - `HAKO_VM_MAX_STEPS`(互換: `NYASH_VM_MAX_STEPS`)で1関数内の実行ステップに上限(既定: 1,000,000)。 + - `0` を指定するとステップ上限なし(開発/診断専用; 無限ループに注意)。 - OOB strict(配列/範囲外の観測) - `HAKO_OOB_STRICT=1`(互換: `NYASH_OOB_STRICT=1`)でOOBを観測・タグ出力。 - `HAKO_OOB_STRICT_FAIL=1` でGate‑C(Core)の実行を非0終了にする(出力のパース不要)。 diff --git a/nyash.toml b/nyash.toml index 241c2ca8..f54c403e 100644 --- a/nyash.toml +++ b/nyash.toml @@ -91,6 +91,7 @@ path = "lang/src/shared/common/string_helpers.hako" "lang.compiler.pipeline_v2.emit_call_box" = "lang/src/compiler/pipeline_v2/emit_call_box.hako" "lang.compiler.pipeline_v2.emit_method_box" = "lang/src/compiler/pipeline_v2/emit_method_box.hako" "lang.compiler.pipeline_v2.emit_newbox_box" = "lang/src/compiler/pipeline_v2/emit_newbox_box.hako" +"lang.compiler.build.build_box" = "lang/src/compiler/build/build_box.hako" "lang.compiler.pipeline_v2.mir_call_box" = "lang/src/compiler/pipeline_v2/mir_call_box.hako" "lang.compiler.pipeline_v2.compare_extract_box" = "lang/src/compiler/pipeline_v2/compare_extract_box.hako" "lang.compiler.pipeline_v2.normalizer_box" = "lang/src/compiler/pipeline_v2/normalizer_box.hako" diff --git a/src/backend/mir_interpreter/exec.rs b/src/backend/mir_interpreter/exec.rs index 0ef87bdf..5589a8c7 100644 --- a/src/backend/mir_interpreter/exec.rs +++ b/src/backend/mir_interpreter/exec.rs @@ -51,7 +51,8 @@ impl MirInterpreter { loop { steps += 1; - if steps > max_steps { + // max_steps == 0 は「上限なし」を意味する(開発/診断専用)。 + if max_steps > 0 && steps > max_steps { return Err(VMError::InvalidInstruction(format!( "vm step budget exceeded (max_steps={})", max_steps diff --git a/src/runner/json_v0_bridge/lowering.rs b/src/runner/json_v0_bridge/lowering.rs index 7256ece6..886b311c 100644 --- a/src/runner/json_v0_bridge/lowering.rs +++ b/src/runner/json_v0_bridge/lowering.rs @@ -263,15 +263,23 @@ pub(super) fn lower_program(prog: ProgramV0, imports: std::collections::HashMap< } let env = BridgeEnv::with_imports(imports); let mut module = MirModule::new("ny_json_v0".into()); + // Treat CLI entry as taking a single parameter `args`. let sig = FunctionSignature { name: "main".into(), - params: vec![], + params: vec![MirType::Unknown], return_type: MirType::Integer, effects: EffectMask::PURE, }; let entry = BasicBlockId::new(0); let mut f = MirFunction::new(sig, entry); let mut var_map: HashMap = HashMap::new(); + // Stage-3 programs (launcher / CLI entry) implicitly reference `args`. + let args_param = ValueId::new(1); + f.params = vec![args_param]; + if f.next_value_id < 2 { + f.next_value_id = 2; + } + var_map.insert("args".into(), args_param); let mut loop_stack: Vec = Vec::new(); let start_bb = f.entry_block; let end_bb = lower_stmt_list_with_vars( diff --git a/src/runner/mir_json_emit.rs b/src/runner/mir_json_emit.rs index aded354e..c43e8835 100644 --- a/src/runner/mir_json_emit.rs +++ b/src/runner/mir_json_emit.rs @@ -28,6 +28,68 @@ fn create_json_v1_root(functions: serde_json::Value) -> serde_json::Value { }) } +/// Helper: detect residual numeric-core boxcalls that should have been lowered by AotPrepNumericCoreBox. +/// Currently we only check for `boxcall` with `method:"mul_naive"` which should become +/// `call("NyNumericMatI64.mul_naive", ...)` when NYASH_AOT_NUMERIC_CORE=1 is effective. +fn has_numeric_core_boxcall(root: &serde_json::Value) -> bool { + let funs = match root.get("functions") { + Some(v) => v.as_array().cloned().unwrap_or_default(), + None => return false, + }; + for f in funs { + let blocks = match f.get("blocks").and_then(|b| b.as_array()) { + Some(b) => b, + None => continue, + }; + for b in blocks { + let insts = match b.get("instructions").and_then(|i| i.as_array()) { + Some(i) => i, + None => continue, + }; + for inst in insts { + let op = inst.get("op").and_then(|v| v.as_str()); + let method = inst.get("method").and_then(|v| v.as_str()); + if op == Some("boxcall") && method == Some("mul_naive") { + return true; + } + } + } + } + false +} + +/// Helper: enforce numeric_core invariants when NYASH_AOT_NUMERIC_CORE=1 is set. +/// - Default: emit a warning if mul_naive boxcalls are still present. +/// - Strict: if NYASH_AOT_NUMERIC_CORE_STRICT=1, return Err to fail fast. +fn check_numeric_core_invariants(root: &serde_json::Value) -> Result<(), String> { + let numeric_on = matches!(std::env::var("NYASH_AOT_NUMERIC_CORE").ok().as_deref(), Some("1")); + if !numeric_on { + return Ok(()); + } + + if !has_numeric_core_boxcall(root) { + return Ok(()); + } + + let strict = matches!( + std::env::var("NYASH_AOT_NUMERIC_CORE_STRICT").ok().as_deref(), + Some("1") + ); + + eprintln!( + "[mir_json/numeric_core] NYASH_AOT_NUMERIC_CORE=1 but MIR JSON still contains boxcall(\"mul_naive\"). \ +AotPrepNumericCoreBox may not have run or did not match; inspect AotPrep logs or run tools/hakorune_emit_mir.sh with HAKO_SELFHOST_TRACE=1." + ); + + if strict { + return Err( + "NYASH_AOT_NUMERIC_CORE_STRICT=1: numeric_core invariants violated (mul_naive boxcall remains)" + .to_string(), + ); + } + Ok(()) +} + /// Helper: Emit unified mir_call JSON (v1 format) /// Supports all 6 Callee types in a single unified JSON structure fn emit_unified_mir_call( @@ -490,6 +552,11 @@ pub fn emit_mir_json_for_harness( json!({"functions": funs}) // v0 legacy format }; + // NOTE: numeric_core strict validation is applied on the AotPrep output + // (tools/hakorune_emit_mir.sh) rather than at raw MIR emit time. This keeps + // pre-AotPrep MIR emission usable even when BoxCall(MatI64, mul_naive) is + // still present. + std::fs::write(path, serde_json::to_string_pretty(&root).unwrap()) .map_err(|e| format!("write mir json: {}", e)) } @@ -796,6 +863,12 @@ pub fn emit_mir_json_for_harness_bin( funs.push(json!({"name": name, "params": params, "blocks": blocks})); } let root = json!({"functions": funs}); + + // NOTE: numeric_core strict validation is applied on the AotPrep output + // (tools/hakorune_emit_mir.sh) rather than at raw MIR emit time. This keeps + // pre-AotPrep MIR emission usable even when BoxCall(MatI64, mul_naive) is + // still present. + std::fs::write(path, serde_json::to_string_pretty(&root).unwrap()) .map_err(|e| format!("write mir json: {}", e)) } diff --git a/test_numeric_core_phi.sh b/test_numeric_core_phi.sh new file mode 100644 index 00000000..74ff750c --- /dev/null +++ b/test_numeric_core_phi.sh @@ -0,0 +1,70 @@ +#!/bin/bash +set -e + +echo "=== Phase 25 MVP: Testing PHI Type Propagation Fix ===" +echo "" + +# Test 1: Simple case (should still work) +echo "Test 1: Simple matmul case" +echo "------------------------" +cat > /tmp/test_matmul_simple.hako << 'EOF' +using numeric.core as nc + +static box Main { + main() { + local a = nc.MatI64.new(2, 2) + local b = nc.MatI64.new(2, 2) + local c = a.mul_naive(b) + return c + } +} +EOF + +# Build with AOT prep +HAKO_APPLY_AOT_PREP=1 NYASH_AOT_NUMERIC_CORE=1 NYASH_AOT_NUMERIC_CORE_TRACE=1 \ + bash tools/hakorune_emit_mir.sh /tmp/test_matmul_simple.hako /tmp/test_simple_result.json + +# Check for transformation +if grep -q '"name":"NyNumericMatI64.mul_naive"' /tmp/test_simple_result.json; then + echo "✅ Simple case: transformation SUCCESS" +else + echo "❌ Simple case: transformation FAILED" + exit 1 +fi + +echo "" +echo "Test 2: Complex matmul_core case" +echo "--------------------------------" + +# Test 2: Complex case (matmul_core with PHI chains) +NYASH_AOT_NUMERIC_CORE=1 NYASH_AOT_NUMERIC_CORE_TRACE=1 \ + tools/perf/microbench.sh --case matmul_core --backend llvm --exe --runs 1 --n 4 2>&1 | tee /tmp/matmul_core_test.log + +# Check trace output for successful transformation +if grep -q "MatI64 vids:" /tmp/matmul_core_test.log; then + echo "✅ Complex case: MatI64 vids detected" +else + echo "⚠️ Complex case: MatI64 vids not shown (might be OK if trace is off)" +fi + +if grep -q "transformed.*BoxCall.*Call" /tmp/matmul_core_test.log; then + echo "✅ Complex case: transformation SUCCESS" +else + echo "❌ Complex case: transformation FAILED" + echo "Check /tmp/matmul_core_test.log for details" + exit 1 +fi + +# Check that we don't see untransformed boxcalls +if grep -q '\[mir_json/numeric_core\].*boxcall("mul_naive")' /tmp/matmul_core_test.log; then + echo "⚠️ Complex case: still seeing untransformed boxcalls" + exit 1 +else + echo "✅ Complex case: no untransformed boxcalls detected" +fi + +echo "" +echo "=== All tests PASSED ===" +echo "" +echo "Diagnostic logs:" +grep -E '\[aot/numeric_core\]' /tmp/matmul_core_test.log | head -20 diff --git a/tools/dev_numeric_core_prep.sh b/tools/dev_numeric_core_prep.sh new file mode 100644 index 00000000..470c9403 --- /dev/null +++ b/tools/dev_numeric_core_prep.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +# dev_numeric_core_prep.sh — numeric_core + AotPrep 付きで MIR(JSON) を吐く開発用ヘルパー +# +# Usage: +# tools/dev_numeric_core_prep.sh +# Notes: +# - HAKO_APPLY_AOT_PREP=1 と NYASH_AOT_NUMERIC_CORE=1 を必ず立てて、 +# tools/hakorune_emit_mir.sh を呼び出す。 +# - NYASH_SKIP_TOML_ENV / NYASH_DISABLE_PLUGINS など、開発用の最小クリーン環境を既定ONにする。 + +set -euo pipefail + +if [ "$#" -ne 2 ]; then + echo "Usage: $0 " >&2 + exit 2 +fi + +IN="$1" +OUT="$2" + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" + +if [ ! -f "$IN" ]; then + echo "[FAIL] input not found: $IN" >&2 + exit 1 +fi + +export HAKO_APPLY_AOT_PREP=1 +export NYASH_AOT_NUMERIC_CORE="${NYASH_AOT_NUMERIC_CORE:-1}" +export NYASH_AOT_NUMERIC_CORE_TRACE="${NYASH_AOT_NUMERIC_CORE_TRACE:-1}" + +# 開発用: 余計な TOML/env/plugin の影響を避ける +export NYASH_SKIP_TOML_ENV="${NYASH_SKIP_TOML_ENV:-1}" +export NYASH_DISABLE_PLUGINS="${NYASH_DISABLE_PLUGINS:-1}" + +# Stage‑B/AotPrep のトレースを既定ON(必要に応じて上書き可) +export HAKO_SELFHOST_TRACE="${HAKO_SELFHOST_TRACE:-1}" + +# JSON だけを期待する(stdoutノイズ対策) +export NYASH_JSON_ONLY="${NYASH_JSON_ONLY:-1}" + +echo "[dev_numeric_core] input=$IN out=$OUT" >&2 +echo "[dev_numeric_core] HAKO_APPLY_AOT_PREP=$HAKO_APPLY_AOT_PREP NYASH_AOT_NUMERIC_CORE=$NYASH_AOT_NUMERIC_CORE" >&2 + +bash "$ROOT/tools/hakorune_emit_mir.sh" "$IN" "$OUT" + +echo "[dev_numeric_core] MIR JSON written with numeric_core+AotPrep: $OUT" >&2 + diff --git a/tools/hakorune_emit_mir.sh b/tools/hakorune_emit_mir.sh index 5fa04ef2..26c2f214 100644 --- a/tools/hakorune_emit_mir.sh +++ b/tools/hakorune_emit_mir.sh @@ -415,9 +415,13 @@ HAKO set +e HAKO_PREP_INPUT="$(cat "$out_path")" \ NYASH_FILEBOX_MODE=core-ro \ + # AotPrep(run_json) は自己ホストVMで JSON を1回走査するだけなので、既定ではステップ上限なし(0)で実行する。 + # 必要に応じて HAKO_VM_MAX_STEPS / NYASH_VM_MAX_STEPS を明示上書き可能。 + HAKO_VM_MAX_STEPS="${HAKO_VM_MAX_STEPS:-0}" NYASH_VM_MAX_STEPS="${NYASH_VM_MAX_STEPS:-0}" \ NYASH_PARSER_STAGE3=1 HAKO_PARSER_STAGE3=1 NYASH_PARSER_ALLOW_SEMICOLON=1 \ NYASH_ENABLE_USING=1 HAKO_ENABLE_USING=1 HAKO_USING_RESOLVER_FIRST=1 \ NYASH_AOT_COLLECTIONS_HOT=${NYASH_AOT_COLLECTIONS_HOT:-0} NYASH_LLVM_FAST=${NYASH_LLVM_FAST:-0} NYASH_MIR_LOOP_HOIST=${NYASH_MIR_LOOP_HOIST:-0} NYASH_AOT_MAP_KEY_MODE=${NYASH_AOT_MAP_KEY_MODE:-auto} \ + NYASH_AOT_NUMERIC_CORE=${NYASH_AOT_NUMERIC_CORE:-0} NYASH_AOT_NUMERIC_CORE_TRACE=${NYASH_AOT_NUMERIC_CORE_TRACE:-0} \ NYASH_JSON_ONLY=${NYASH_JSON_ONLY:-1} \ "$NYASH_BIN" --backend vm "$_prep_hako" >"$_prep_stdout" 2>"$_prep_stderr" _rc=$? @@ -436,6 +440,14 @@ HAKO grep '^\[aot/collections_hot\]' "$_prep_stdout" >&2 || true fi fi + # Optional: surface NumericCore trace lines for diagnostics when requested + if [ "${NYASH_AOT_NUMERIC_CORE_TRACE:-0}" = "1" ]; then + if command -v rg >/dev/null 2>&1; then + rg -n '^\[aot/numeric_core\]' "$_prep_stdout" >&2 || true + else + grep '^\[aot/numeric_core\]' "$_prep_stdout" >&2 || true + fi + fi else if [ "${HAKO_SELFHOST_TRACE:-0}" = "1" ]; then echo "[provider/emit:trace] AotPrep skipped or failed (run_json) rc=$_rc" >&2 @@ -719,12 +731,30 @@ EOF NYASH_PARSER_STAGE3=1 HAKO_PARSER_STAGE3=1 NYASH_PARSER_ALLOW_SEMICOLON=1 \ NYASH_ENABLE_USING=1 HAKO_ENABLE_USING=1 HAKO_USING_RESOLVER_FIRST=1 \ NYASH_AOT_COLLECTIONS_HOT=${NYASH_AOT_COLLECTIONS_HOT:-0} NYASH_LLVM_FAST=${NYASH_LLVM_FAST:-0} NYASH_MIR_LOOP_HOIST=${NYASH_MIR_LOOP_HOIST:-0} NYASH_AOT_MAP_KEY_MODE=${NYASH_AOT_MAP_KEY_MODE:-auto} \ + NYASH_AOT_NUMERIC_CORE=${NYASH_AOT_NUMERIC_CORE:-0} NYASH_AOT_NUMERIC_CORE_TRACE=${NYASH_AOT_NUMERIC_CORE_TRACE:-0} \ + HAKO_VM_MAX_STEPS="${HAKO_VM_MAX_STEPS:-0}" NYASH_VM_MAX_STEPS="${NYASH_VM_MAX_STEPS:-0}" \ "$NYASH_BIN" --backend vm "$aot_runner" >"$prep_stdout" 2>"$prep_stderr" aot_rc=$? set -e if [ $aot_rc -eq 0 ] && grep -q "\[PREP_OUT_BEGIN\]" "$prep_stdout" && grep -q "\[PREP_OUT_END\]" "$prep_stdout"; then awk '/\[PREP_OUT_BEGIN\]/{flag=1;next}/\[PREP_OUT_END\]/{flag=0}flag' "$prep_stdout" > "$out_path" [ "${HAKO_SELFHOST_TRACE:-0}" = "1" ] && echo "[prep:ok] AotPrep applied (run_json)" >&2 + # Surface numeric_core trace lines when requested(provider経路でも [aot/numeric_core] を見えるようにする) + if [ "${NYASH_AOT_NUMERIC_CORE_TRACE:-0}" = "1" ]; then + if command -v rg >/devnull 2>&1; then + rg -n '^\[aot/numeric_core\]' "$prep_stdout" >&2 || true + else + grep '^\[aot/numeric_core\]' "$prep_stdout" >&2 || true + fi + fi + # Optional strict post-check: after AotPrep(run_json) the MIR JSON is expected + # to have MatI64.mul_naive lowered to Call when NYASH_AOT_NUMERIC_CORE=1. + if [ "${NYASH_AOT_NUMERIC_CORE_STRICT:-0}" = "1" ]; then + if rg -q '"op":"boxcall".*"method":"mul_naive"' "$out_path"; then + echo "[prep/numeric_core/strict] NYASH_AOT_NUMERIC_CORE_STRICT=1 but boxcall(\"mul_naive\") remains after AotPrep.run_json; inspect numeric_core.hako patterns." >&2 + return 1 + fi + fi else if [ "${HAKO_SELFHOST_TRACE:-0}" = "1" ]; then echo "[prep:warn] AotPrep failed (rc=$aot_rc), using original MIR" >&2 @@ -748,6 +778,21 @@ if [ "${HAKO_MIR_BUILDER_LOOP_FORCE_JSONFRAG:-0}" = "1" ]; then fi if [ "${HAKO_SELFHOST_BUILDER_FIRST:-0}" = "1" ]; then + # Prefer Stage1 CLI (hakorune) when available to avoid Stage-3 parser issues + stage1_cli_bin="${HAKO_STAGE1_CLI:-$ROOT/lang/bin/hakorune}" + if [ -x "$stage1_cli_bin" ]; then + prog_tmp=$(mktemp --suffix .json) + printf '%s' "$PROG_JSON_OUT" > "$prog_tmp" + if "$stage1_cli_bin" emit mir-json --from-program-json "$prog_tmp" -o "$OUT" --quiet >/dev/null 2>&1 && [ -s "$OUT" ]; then + rm -f "$prog_tmp" || true + echo "[OK] MIR JSON written (selfhost-first:stage1-cli): $OUT" + exit 0 + fi + rm -f "$prog_tmp" || true + if [ "${HAKO_SELFHOST_TRACE:-0}" = "1" ]; then + echo "[selfhost-first:stage1-cli] failed (binary: $stage1_cli_bin), falling back to legacy builder" >&2 + fi + fi if try_selfhost_builder "$PROG_JSON_OUT" "$OUT"; then exit 0 fi diff --git a/tools/selfhost/README.md b/tools/selfhost/README.md index 21c0cdaa..432065d2 100644 --- a/tools/selfhost/README.md +++ b/tools/selfhost/README.md @@ -29,3 +29,45 @@ Notes - Stage‑B emit uses either the Stage‑B entry or BuildBox(HAKO_USE_BUILDBOX=1 for emit-only) - Runner executes Core‑Direct in-proc under HAKO_CORE_DIRECT_INPROC=1. - For heavier cases (bundles/alias/require), keep Stage‑B canaries opt‑in in quick profile. + +--- + +Stage1 Selfhost Binary (Phase 25.1 — initial wiring) + +Purpose +- Provide a concrete Stage1 binary (`hakorune`) built from Hako sources. +- Separate the Rust bootstrap binary (Stage0, `nyash`) from the Hakorune selfhost binary at the build-script level. + +Script +- tools/selfhost/build_stage1.sh + - Builds a Stage1 selfhost executable from a Nyash/Hako entry point. + - Current entry (default): + - `lang/src/runner/launcher.hako` — Stage1 CLI launcher (commands: emit program-json/mir-json, etc.). + - Output: + - `target/selfhost/hakorune` by default. + +Usage +```bash +# Build Stage1 selfhost binary with defaults +tools/selfhost/build_stage1.sh + +# Custom output path +tools/selfhost/build_stage1.sh --out /tmp/hakorune-dev + +# Custom entry (experimental) +tools/selfhost/build_stage1.sh --entry apps/selfhost-minimal/main.hako --out /tmp/hako_min +``` + +How it works +- Pipeline: + 1) Stage‑B + MirBuilder: + - `tools/hakorune_emit_mir.sh ` + 2) LLVM EXE build: + - `tools/ny_mir_builder.sh --in --emit exe -o ` +- The Rust binary (Stage0) is resolved via the existing helpers inside `hakorune_emit_mir.sh` / `ny_mir_builder.sh`: + - Prefers `target/release/hakorune` when present. + - Falls back to `target/release/nyash` otherwise. + +Notes +- This Stage1 binary is a minimal Ny Executor and not yet a full CLI replacement. +- Full CLI / mode selection for Stage1 will be implemented in later phases; this script focuses on establishing the binary layout and build wiring. diff --git a/tools/selfhost/build_stage1.sh b/tools/selfhost/build_stage1.sh new file mode 100644 index 00000000..e969b625 --- /dev/null +++ b/tools/selfhost/build_stage1.sh @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +# build_stage1.sh — Build Hakorune Stage1 selfhost binary +# +# Purpose +# - Produce a minimal Stage1 (Hakorune selfhost) executable from Nyash/Hako sources. +# - Current target is the Stage1 CLI launcher in lang/src/runner/launcher.hako. +# - This is an initial implementation for Phase 25.1: binary layoutと Stage1 dev bin 用導線のみ。 +# +# Output +# - target/selfhost/hakorune (by default) +# +# Env / args +# - HAKORUNE_STAGE1_ENTRY: override Stage1 entry .hako (optional). +# - HAKORUNE_STAGE1_OUT: override output path (optional). +# - NYASH_LLVM_SKIP_BUILD: set to 1 to skip cargo build in ny_mir_builder.sh when artifacts already exist. +# - Args: +# --out : override output path (same as HAKORUNE_STAGE1_OUT). +# --entry : override entry .hako (same as HAKORUNE_STAGE1_ENTRY). +# -h|--help : show usage and exit. +# +set -euo pipefail + +usage() { + cat <<'USAGE' +build_stage1.sh — Build Hakorune Stage1 selfhost binary + +Usage: + tools/selfhost/build_stage1.sh [--out ] [--entry ] + +Defaults: + entry .hako : lang/src/runner/launcher.hako + output exe : target/selfhost/hakorune + +Notes: + - This script uses the Stage-B + MirBuilder pipeline: + lang/src/runner/launcher.hako + → tools/hakorune_emit_mir.sh (Program(JSON v0 → MIR JSON) + → tools/ny_mir_builder.sh --emit exe + - The Rust binary (Stage0) is treated as bootstrap and is resolved via + the existing helpers inside hakorune_emit_mir.sh / ny_mir_builder.sh. +USAGE +} + +ROOT="${NYASH_ROOT:-$(cd "$(dirname "$0")/../.." && pwd)}" + +ENTRY_DEFAULT="$ROOT/lang/src/runner/launcher.hako" +OUT_DEFAULT="$ROOT/target/selfhost/hakorune" + +ENTRY="${HAKORUNE_STAGE1_ENTRY:-$ENTRY_DEFAULT}" +OUT="${HAKORUNE_STAGE1_OUT:-$OUT_DEFAULT}" + +while [ $# -gt 0 ]; do + case "$1" in + --out) + OUT="$2" + shift 2 + ;; + --entry) + ENTRY="$2" + shift 2 + ;; + -h|--help) + usage + exit 0 + ;; + *) + echo "[stage1] unknown arg: $1" >&2 + usage + exit 2 + ;; + esac +done + +if [ ! -f "$ENTRY" ]; then + echo "[stage1] entry .hako not found: $ENTRY" >&2 + exit 2 +fi + +OUT_DIR="$(dirname "$OUT")" +mkdir -p "$OUT_DIR" + +echo "[stage1] building Stage1 selfhost binary" >&2 +echo " entry : $ENTRY" >&2 +echo " output: $OUT" >&2 + +# Use the Stage‑B → MirBuilder → ny-llvmc path +NYASH_ROOT="$ROOT" bash "$ROOT/tools/selfhost_exe_stageb.sh" "$ENTRY" -o "$OUT" + +echo "[stage1] done: $OUT" >&2 diff --git a/tools/selfhost_exe_stageb.sh b/tools/selfhost_exe_stageb.sh index f4b7e642..16737507 100644 --- a/tools/selfhost_exe_stageb.sh +++ b/tools/selfhost_exe_stageb.sh @@ -27,9 +27,9 @@ ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)" # 1) Emit MIR(JSON) via Stage‑B → MirBuilder (selfhost‑first) TMP_JSON=$(mktemp --suffix .json) -HAKO_SELFHOST_BUILDER_FIRST=1 \ -HAKO_MIR_BUILDER_LOOP_JSONFRAG=1 \ -HAKO_MIR_BUILDER_JSONFRAG_NORMALIZE=1 \ +HAKO_SELFHOST_BUILDER_FIRST="${HAKO_SELFHOST_BUILDER_FIRST:-0}" \ +HAKO_MIR_BUILDER_LOOP_JSONFRAG="${HAKO_MIR_BUILDER_LOOP_JSONFRAG:-0}" \ +HAKO_MIR_BUILDER_JSONFRAG_NORMALIZE="${HAKO_MIR_BUILDER_JSONFRAG_NORMALIZE:-0}" \ NYASH_ENABLE_USING=1 HAKO_ENABLE_USING=1 \ NYASH_JSON_ONLY=1 bash "$ROOT_DIR/tools/hakorune_emit_mir.sh" "$INPUT" "$TMP_JSON" >/dev/null echo "[emit] MIR JSON: $TMP_JSON ($(wc -c < "$TMP_JSON") bytes)"