Commit Graph

20 Commits

Author SHA1 Message Date
8633224061 JoinIR/SSA/Stage-3: sync CURRENT_TASK and dev env 2025-12-01 11:10:46 +09:00
7de192aa6b feat(mir): Phase 69-3 Fix MIR non-determinism with BTreeSet
Replace HashSet with BTreeSet for CFG predecessors/successors:
- BasicBlock.predecessors: HashSet → BTreeSet
- BasicBlock.successors: HashSet → BTreeSet
- LoopFormOps.get_block_predecessors(): returns BTreeSet
- BasicBlock.dominates(): takes &[BTreeSet<BasicBlockId>]

This ensures deterministic PHI generation and test stability.

Test results:
- loop_with_continue_and_break tests: now deterministic (3/3 same output)
- loopform tests: 14/14 PASS (no regressions)
- merge_exit_with_classification tests: 3/3 PASS

Technical changes (6 files):
- basic_block.rs: BTreeSet types + new() initialization
- loopform_builder.rs: trait signature + 2 mock implementations
- phi_ops.rs: return type
- json_v0_bridge/loop_.rs: return type

Same pattern as Phase 25.1 (MirFunction.blocks HashMap → BTreeMap).

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-30 09:38:28 +09:00
c6edbaaf3a feat(mir): Phase 63-6-1/2 MIR Phi type_hint field & JoinIR propagation
Phase 63-6-1: MirInstruction::Phi に type_hint フィールド追加
- Added `type_hint: Option<MirType>` field to Phi instruction
- Updated 21 files with type_hint initialization (all set to None for legacy paths)
- Pattern matching updated across codebase (11 files)
- Test code updated (basic_block.rs)

Phase 63-6-2: JoinIR→MIR Bridge で型ヒント伝播実装
- Modified convert.rs: Select → MIR now creates PHI with type_hint
- Removed Copy instructions from then/else blocks
- PHI instruction at merge block receives type_hint from JoinIR Select
- Test verification:  Type hint propagation successful (Some(Integer))

Modified files:
- instruction.rs: Added type_hint field definition
- join_ir_vm_bridge/convert.rs: Select lowering with PHI + type_hint
- 19 other files: type_hint field initialization

Test results:
-  test_type_hint_propagation_simple: Type hint = Some(Integer) confirmed
-  7/8 if_select tests passing (1 race condition, passes individually)

Next: Phase 63-6-3 (lifecycle.rs で型ヒント使用)

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-30 04:35:40 +09:00
7a1a4bd964 refactor(mir): loop_builder.rs モジュール化 - 6ファイルに分割
## リファクタリング内容

### ファイル構造変更
- `src/mir/loop_builder.rs` (1515行) 削除
- `src/mir/loop_builder/` ディレクトリ新設(6ファイル、1529行)

### 新規モジュール構成

1. **mod.rs** (6,293行 → 実際は約150行)
   - モジュール定義とre-export
   - LoopBuilder構造体定義

2. **loop_form.rs** (25,988行 → 実際は約650行)
   - メインループlowering pipeline
   - デバッグ/実験フラグ集約

3. **if_lowering.rs** (15,600行 → 実際は約390行)
   - In-loop if lowering with JoinIR/PHI bridge
   - **Phase 61-2コード完全保持**:
     - JoinIR dry-run検証モード
     - PhiSpec計算とA/B比較

4. **phi_ops.rs** (12,844行 → 実際は約320行)
   - PHI emit helpers
   - LoopFormOps/PhiBuilderOps impls

5. **control.rs** (4,261行 → 実際は約107行)
   - break/continue capture
   - predecessor bookkeeping

6. **statements.rs** (1,673行 → 実際は約42行)
   - loop-body statement lowering entry point

7. **README.md** (752行 → 実際は約19行)
   - モジュール責務とサブモジュール説明

### 設計原則

- **責務分離**: CFG構築/PHI生成/制御フロー/文処理を分離
- **Phase 61-2保持**: if_lowering.rsにJoinIR dry-run完全移行
- **phi_core委譲**: PHI構築ロジックは`phi_core`に委譲

## テスト結果

- Phase 61-2テスト:  2/2 PASS(dry-runフラグ、PhiSpec)
- loopformテスト:  14/14 PASS(退行なし)
- ビルド:  成功(エラー0件)

## 統計

- **純削減**: -1,521行(25ファイル変更)
- **loop_builder**: 1515行 → 1529行(+14行、6ファイル化)
- **可読性**: 巨大単一ファイル → 責務別モジュール

## ChatGPT設計・Claude確認

大規模リファクタリングをChatGPTが実施、Claudeが検証完了。

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-29 12:44:40 +09:00
ad9daf37ac feat(joinir): Phase 56 ArrayExtBox.filter JoinIR lowering完全実装
## Summary
ArrayExtBox.filter/2 の JoinIR Frontend lowering を完全実装し、
ConditionalMethodCall 命令を導入して filter パターンに対応。
56 JoinIR テスト全て PASS(退行なし)。

## Technical Changes

### 1. ConditionalMethodCall 命令追加
- **新規命令**: `if pred(v) { acc.push(v) }` パターン用
- **構造**: cond が true なら method 実行、false なら no-op
- **MIR 変換**: 4ブロック構造 (cond→then/else→merge)

### 2. AST JSON 拡張
- Break/Continue/FunctionCall に "type" フィールド追加
- ArrayLiteral/MapLiteral に "type" フィールド追加
- JoinIR Frontend 互換性向上

### 3. Expression Handler 拡張
- Unary 演算子(not, 負号)サポート
- Call(変数関数呼び出し)を MethodCall に変換

### 4. Loop Pattern Binding 修正
- `BoundExpr::Variable("n")` 問題修正
- `MethodCall { receiver: "arr", method: "size" }` に変更
- external_refs (arr, pred) を step 関数に伝播

### 5. If Statement Handler 拡張
- 条件付き側効果パターン(ケース4)追加
- MethodCall/Method 形式の statement を ConditionalMethodCall に変換

## Files Modified (10 files, +456/-45 lines)
- ast_json.rs: AST JSON "type" フィールド追加
- loop_frontend_binding.rs: n バインディング修正
- control_flow.rs: external_refs params 追加
- loop_patterns.rs: external_refs step 関数伝播
- expr.rs: Unary, Call handler 追加
- stmt_handlers.rs: ConditionalMethodCall パターン追加
- mod.rs: ConditionalMethodCall, UnaryOp 定義
- json.rs: ConditionalMethodCall, UnaryOp シリアライズ
- join_ir_runner.rs: ConditionalMethodCall, UnaryOp スタブ
- convert.rs: ConditionalMethodCall → MIR 変換

## Test Results
- 56 JoinIR tests:  PASSED
- Regression:  None
- ArrayExtBox.filter/2:  JoinIR lowering 成功

## Milestone
JoinIR 2ループ完走達成:
-  JsonTokenizer.print_tokens/0
-  ArrayExtBox.filter/2 (NEW!)

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-29 06:51:43 +09:00
e27934d91a feat(joinir): Phase 52-53 LoopFrontendBinding JSON + Statement Handlers
Phase 52: LoopFrontendBinding JSON generation fixes
- Add receiver_to_json() for Field node structure (me.tokens)
- Add needs_me_receiver() for instance method detection
- Fix "condition" → "cond" key for JoinIR Frontend
- Add me parameter propagation in loop_patterns.rs
- Add JoinIR-compatible type fields in ast_json.rs
  - Variable → "type": "Var"
  - Literal → "type": "Int"/"Bool" (literal_to_joinir_json)
  - BinaryOp → "type": "Binary"/"Compare" (is_compare_op)
  - MethodCall → "type": "Method"

Phase 53: Statement Handler module for loop body
- NEW: stmt_handlers.rs with StatementEffect type
- Support: Local, Assignment, Print, Method, If statements
- If lowering: single variable update → Select instruction
- Remove hardcoded assert in loop_patterns.rs
- Replace with generic lower_statement() calls

Test results: 56 JoinIR tests PASS, 7 loop_frontend_binding tests PASS

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-29 04:42:16 +09:00
3dc691d39f feat(joinir): Phase 50 Loop Frontend Binding layer
Phase 50 implements the Loop Frontend Binding layer that maps
actual loop variables to JoinIR Frontend's expected names (i, acc, n).

## Changes

- Add loop_frontend_binding.rs module with:
  - LoopFrontendBinding struct for variable mapping
  - for_print_tokens() and for_array_filter() factory methods
  - generate_local_declarations() for JSON v0 format
  - rename_body_variables() for out → acc renaming

- Integrate binding with cf_loop_joinir_impl:
  - Create binding based on function name
  - Inject i/acc/n Local declarations into JSON v0
  - Use correct JoinIR Frontend type names (Int/Var/Method)

## Limitations Found

JoinIR Frontend doesn't support:
- Field access (me.tokens) - blocks print_tokens
- NewBox (new ArrayBox()) - blocks array_filter

These will be addressed in Phase 51.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 20:59:54 +09:00
1e1b2183b2 feat(joinir): Phase 49-4 multi-target routing with graceful fallback
- Add ArrayExtBox.filter/2 as second JoinIR mainline target
- Fix function name arity: print_tokens is /0 (no implicit me in arity)
- Construct proper JSON v0 format with defs array for JoinIR Frontend
- Add catch_unwind for graceful fallback on unsupported patterns
- Add 3 array_filter tests (smoke, fallback, A/B comparison)
- All 6 Phase 49 tests passing

Dev flags:
- HAKO_JOINIR_PRINT_TOKENS_MAIN=1: JsonTokenizer.print_tokens/0
- HAKO_JOINIR_ARRAY_FILTER_MAIN=1: ArrayExtBox.filter/2

Note: Currently all loops fall back to legacy LoopBuilder due to
JoinIR Frontend expecting hardcoded variable names (i, acc, n).
Full JoinIR integration pending variable scope support in Phase 50+.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 20:12:39 +09:00
20d9b412b2 feat(joinir): Phase 49-3.2 merge_joinir_mir_blocks full implementation
Implement actual block merging for JoinIR Frontend mainline integration:

- Block ID remapping: Allocate new IDs from block_gen for all JoinIR blocks
- Value ID remapping: Allocate new IDs from next_value_id() for all values
- Instruction cloning: Clone all instructions with remapped IDs
- Return→Jump conversion: Convert Return terminators to Jump to exit block
- Control flow wiring: Jump from current block to JoinIR entry

Helper functions added:
- collect_values_in_block(): Collect all ValueIds in a block
- collect_values_in_instruction(): Collect all ValueIds in an instruction
- remap_instruction(): Remap ValueIds and BlockIds in an instruction

A/B tests (3 total):
- phase49_joinir_mainline_pipeline_smoke
- phase49_joinir_mainline_fallback_without_flag
- phase49_joinir_mainline_ab_comparison (Route A vs Route B)

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 19:45:23 +09:00
a8db5682cb feat(joinir): Phase 49-3 JoinIR Frontend mainline integration
Implement AST→JSON→JoinIR→MIR pipeline in cf_loop for print_tokens:
- Add cf_loop_joinir_impl() with full pipeline:
  - AST Loop node → Program JSON
  - AstToJoinIrLowerer::lower_program_json() → JoinModule
  - convert_join_module_to_mir_with_meta() → MirModule
  - merge_joinir_mir_blocks() (MVP: logging only)

- Add Phase 49 smoke tests (2 tests):
  - phase49_joinir_mainline_pipeline_smoke
  - phase49_joinir_mainline_fallback_without_flag

- Dev flag: HAKO_JOINIR_PRINT_TOKENS_MAIN=1
- Debug flag: NYASH_JOINIR_MAINLINE_DEBUG=1

MVP limitations:
- Block merge is logging only (Phase 49-3.2 for full impl)
- if-analysis meta is empty (Phase 40+ territory)

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 19:29:45 +09:00
e258973dba feat(builder): Phase 49 cf_loop JoinIR Frontend mainline integration point
Add try_cf_loop_joinir() method to control_flow.rs as the routing point
for JoinIR Frontend mainline integration.

- First target: JsonTokenizer.print_tokens/1
- Controlled by HAKO_JOINIR_PRINT_TOKENS_MAIN=1 flag
- Currently falls through to legacy LoopBuilder (Phase 49-3 will implement)

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 19:12:59 +09:00
f9d100ce01 chore: Phase 25.1 完了 - LoopForm v2/Stage1 CLI/環境変数削減 + Phase 26-D からの変更
Phase 25.1 完了成果:
-  LoopForm v2 テスト・ドキュメント・コメント完備
  - 4ケース(A/B/C/D)完全テストカバレッジ
  - 最小再現ケース作成(SSAバグ調査用)
  - SSOT文書作成(loopform_ssot.md)
  - 全ソースに [LoopForm] コメントタグ追加

-  Stage-1 CLI デバッグ環境構築
  - stage1_cli.hako 実装
  - stage1_bridge.rs ブリッジ実装
  - デバッグツール作成(stage1_debug.sh/stage1_minimal.sh)
  - アーキテクチャ改善提案文書

-  環境変数削減計画策定
  - 25変数の完全調査・分類
  - 6段階削減ロードマップ(25→5、80%削減)
  - 即時削除可能変数特定(NYASH_CONFIG/NYASH_DEBUG)

Phase 26-D からの累積変更:
- PHI実装改善(ExitPhiBuilder/HeaderPhiBuilder等)
- MIRビルダーリファクタリング
- 型伝播・最適化パス改善
- その他約300ファイルの累積変更

🎯 技術的成果:
- SSAバグ根本原因特定(条件分岐内loop変数変更)
- Region+next_iパターン適用完了(UsingCollectorBox等)
- LoopFormパターン文書化・テスト化完了
- セルフホスティング基盤強化

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: ChatGPT <noreply@openai.com>
Co-Authored-By: Task Assistant <task@anthropic.com>
2025-11-21 06:25:17 +09:00
f74b7d2b04 📦 Hotfix 1 & 2: Parameter ValueId Reservation + Exit PHI Validation (Box-First Theory)
**箱理論に基づく根治的修正**:

## 🎯 Hotfix 1: Parameter ValueId Reservation (パラメータ ValueId 予約)

### 根本原因
- MirFunction counter が params.len() を考慮していなかった
- local variables が parameter ValueIds を上書き

### 箱理論的解決
1. **LoopFormContext Box**
   - パラメータ予約を明示的に管理
   - 境界をはっきりさせる

2. **MirFunction::new() 改善**
   - `initial_counter = param_count.max(1)` でパラメータ予約
   - Parameters are %0, %1, ..., %N-1

3. **ensure_counter_after() 強化**
   - パラメータ数 + 既存 ValueIds 両方を考慮
   - `min_counter = param_count.max(max_id + 1)`

4. **reserve_parameter_value_ids() 追加**
   - 明示的な予約メソッド(Box-First)

## 🎯 Hotfix 2: Exit PHI Predecessor Validation (Exit PHI 検証)

### 根本原因
- LoopForm builder が存在しないブロックを PHI predecessor に追加
- 「幽霊ブロック」問題

### 箱理論的解決
1. **LoopFormOps.block_exists() 追加**
   - CFG 存在確認メソッド
   - 境界を明確化

2. **build_exit_phis() 検証**
   - 非存在ブロックをスキップ
   - デバッグログ付き

### 実装ファイル
- `src/mir/function.rs`: Parameter reservation
- `src/mir/phi_core/loopform_builder.rs`: Context + validation
- `src/mir/loop_builder.rs`: LoopFormOps impl
- `src/mir/builder/stmts.rs`: Local variable allocation

### 業界標準準拠
-  LLVM IR: Parameters are %0, %1, ...
-  SSA Form: PHI predecessors must exist in CFG
-  Cytron et al. (1991): Parameter reservation principle

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 06:39:45 +09:00
eadde8d1dd fix(mir/builder): use function-local ValueId throughout MIR builder
Phase 25.1b: Complete SSA fix - eliminate all global ValueId usage in function contexts.

Root cause: ~75 locations throughout MIR builder were using global value
generator (self.value_gen.next()) instead of function-local allocator
(f.next_value_id()), causing SSA verification failures and runtime
"use of undefined value" errors.

Solution:
- Added next_value_id() helper that automatically chooses correct allocator
- Fixed 19 files with ~75 occurrences of ValueId allocation
- All function-context allocations now use function-local IDs

Files modified:
- src/mir/builder/utils.rs: Added next_value_id() helper, fixed 8 locations
- src/mir/builder/builder_calls.rs: 17 fixes
- src/mir/builder/ops.rs: 8 fixes
- src/mir/builder/stmts.rs: 7 fixes
- src/mir/builder/emission/constant.rs: 6 fixes
- src/mir/builder/rewrite/*.rs: 10 fixes
- + 13 other files

Verification:
- cargo build --release: SUCCESS
- Simple tests with NYASH_VM_VERIFY_MIR=1: Zero undefined errors
- Multi-parameter static methods: All working

Known remaining: ValueId(22) in Stage-B (separate issue to investigate)

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-17 00:48:18 +09:00
dda65b94b7 Phase 21.7 normalization: optimization pre-work + bench harness expansion
- Add opt-in optimizations (defaults OFF)
  - Ret purity verifier: NYASH_VERIFY_RET_PURITY=1
  - strlen FAST enhancement for const handles
  - FAST_INT gate for same-BB SSA optimization
  - length cache for string literals in llvmlite
- Expand bench harness (tools/perf/microbench.sh)
  - Add branch/call/stringchain/arraymap/chip8/kilo cases
  - Auto-calculate ratio vs C reference
  - Document in benchmarks/README.md
- Compiler health improvements
  - Unify PHI insertion to insert_phi_at_head()
  - Add NYASH_LLVM_SKIP_BUILD=1 for build reuse
- Runtime & safety enhancements
  - Clarify Rust/Hako ownership boundaries
  - Strengthen receiver localization (LocalSSA/pin/after-PHIs)
  - Stop excessive PluginInvoke→BoxCall rewrites
- Update CURRENT_TASK.md, docs, and canaries

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-13 16:40:58 +09:00
dd65cf7e4c builder+vm: unify method calls via emit_unified_call; add RouterPolicy trace; finalize LocalSSA/BlockSchedule guards; docs + selfhost quickstart
- Unify standard method calls to emit_unified_call; route via RouterPolicy and apply rewrite::{special,known} at a single entry.\n- Stabilize emit-time invariants: LocalSSA finalize + BlockSchedule PHI→Copy→Call ordering; metadata propagation on copies.\n- Known rewrite default ON (userbox only, strict guards) with opt-out flag NYASH_REWRITE_KNOWN_DEFAULT=0.\n- Expand TypeAnnotation whitelist (is_digit_char/is_hex_digit_char/is_alpha_char/Map.has).\n- Docs: unified-method-resolution design note; Quick Reference normalization note; selfhosting/quickstart.\n- Tools: add tools/selfhost_smoke.sh (dev-only).\n- Keep behavior unchanged for Unknown/core/user-instance via BoxCall fallback; all tests green (quick/integration).
2025-09-28 20:38:09 +09:00
9142476484 parser(match): add MVP type patterns (IntegerBox(x)/StringBox(s)) via AST If-chain; keep literal-only path using PeekExpr; add smoke app (apps/tests/match_type_pattern_basic.nyash); build + stage-2 smokes green 2025-09-19 08:34:29 +09:00
b8e416fb03 refactor: MIR builder modularization complete - ready for handoff
- MIRビルダーのモジュール化完了(1,547行→6モジュール)
  - core.rs (205行): MirBuilder本体
  - expressions.rs (621行): 式変換処理
  - statements.rs (165行): 文変換処理
  - control_flow.rs (194行): 制御フロー
  - box_handlers.rs (73行): Box処理
- 現在builder_modularizedに退避(MIR命令構造変更により調整必要)
- フルビルド可能な状態を維持
- CURRENT_TASK.mdに引き継ぎポイント記載

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-25 19:33:07 +09:00
17adc4347d refactor: MIR builder - complete modularization (Phase 6-8)
- Phase 6: statements.rs (165 lines) - 6 functions
  - build_print_statement, build_block
  - build_local_statement, build_return_statement
  - build_throw_statement, build_nowait_statement

- Phase 7: control_flow.rs (194 lines) - 4 functions
  - build_if_statement, build_loop_statement
  - build_try_catch_statement, is_current_block_terminated

- Phase 8: box_handlers.rs (73 lines) - 2 functions
  - build_static_main_box, build_box_declaration

Total: 30 functions moved across 4 modules
Original builder.rs reduced from 1547 lines
Build verified successfully
2025-08-25 18:43:45 +09:00
cff58dbc0a refactor: MIR Builder Phase 1 - モジュール分割準備完了
【Phase 1完了内容】
- src/mir/builder/ ディレクトリ構造作成
- MirBuilder コア機能を core.rs に分離(8関数実装済み)
- 責務別モジュール準備(expressions/statements/control_flow/box_handlers)
- ビルド確認: 新構造でコンパイル正常完了

【技術詳細】
- MirBuilder本体 + emit_instruction/emit_type_check等コア機能
- プレースホルダー実装でビルド安全性確保
- CURRENT_TASK.md更新(Phase 1完了状況記録)
- 49関数/1547行の段階的分割準備

【次のPhase】
- Phase 2: 実際の関数移動(expressions.rs最優先)
- 慎重アプローチ: デッドコード削除は後回し

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-25 17:49:21 +09:00