## Summary Refactored Phase 284 P1 codebase to consolidate scattered logic into SSOT (Single Source of Truth) modules, improving maintainability and enabling Pattern4/5 code reuse. ## New Modules ### 1. Block Remapper SSOT **File**: `src/mir/builder/control_flow/joinir/merge/block_remapper.rs` (152 lines) - **Purpose**: Consolidate block ID remapping logic (Phase 284 P1 fix) - **Function**: `remap_block_id(block_id, local_block_map, skipped_entry_redirects)` - **Rule**: local_block_map priority > skipped_entry_redirects (prevents ID collision) - **Tests**: 4 unit tests (priority cascade, collision handling, etc.) ### 2. Return Jump Emitter **File**: `src/mir/join_ir/lowering/return_jump_emitter.rs` (354 lines) - **Purpose**: Reusable return handling helper for Pattern4/5 - **Function**: `emit_return_conditional_jump(loop_step_func, return_info, k_return_id, ...)` - **Scope**: P1 - unconditional return + conditional (loop_var == N) only - **Tests**: 3 unit tests (unconditional, no return, conditional) ## Modified Files **merge/instruction_rewriter.rs** (-15 lines): - Replaced inline block remapping with `remap_block_id()` call - Cleaner Branch remap logic **merge/rewriter/terminator.rs** (-43 lines): - Delegates remap_jump/remap_branch to block_remapper SSOT - Simplified duplicate logic **lowering/loop_with_continue_minimal.rs** (-108 lines): - Replaced ~100 line return handling with `emit_return_conditional_jump()` call - Extracted helper functions to return_jump_emitter.rs - Line reduction: 57% decrease in function complexity **merge/mod.rs, lowering/mod.rs**: - Added new module exports (block_remapper, return_jump_emitter) **phase-284/README.md**: - Updated completion status (P1 Complete + Refactored) - Added SSOT consolidation notes - Documented module architecture ## Code Quality Improvements | Metric | Before | After | Change | |--------|--------|-------|--------| | Duplicate block remap logic | 2 places | SSOT | -15 lines | | Return handling code | inline (100L) | helper call | -99 lines | | Testability | Limited | Unit tests (7) | +7 tests | | Module cohesion | Low (scattered) | High (consolidated) | Better | ## Testing ✅ Build: Success (cargo build --release) ✅ Smoke tests: All pass (46 PASS, 1 pre-existing FAIL) ✅ Regression: Zero ✅ Unit tests: 7 new tests added ## Future Benefits 1. **Pattern5 Reuse**: Direct use of `emit_return_conditional_jump()` helper 2. **Phase 285 (P2)**: Nested if/loop returns via same infrastructure 3. **Maintainability**: SSOT reduces debugging surface area 4. **Clarity**: Each module has single responsibility ## Architectural Notes **Block Remapper SSOT Rule**: ``` remap_block_id(id, local_block_map, skipped_entry_redirects): 1. Check local_block_map (function-local priority) 2. Fall back to skipped_entry_redirects (global redirects) 3. Return original if not found ``` Prevents function-local block ID collisions with global remap entries. **Return Emitter Pattern**: ``` emit_return_conditional_jump(func, return_info, k_return_id, alloc_value): - Pattern1-5: All use same infrastructure - P1 scope: top-level return only (nested if/loop → P2) - Returns: JoinInst::Jump(cont=k_return, cond=Some(return_cond)) ``` 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Phase ドキュメント
このフォルダは、実装フェーズ(Phase 131, Phase 33 等)ごとの詳細記録を保管します。
現在の Phase
- Phase 139(DONE): post-if
post_kの return lowering をReturnValueLowererBoxに統一(出口 SSOT 完成) - Phase 140(DONE):
NormalizedExprLowererBox初版(pure expression のみ) - Phase 141 P0(DONE): impure 拡張点(contract)を SSOT 化(Call/MethodCall はまだ out-of-scope)
- Phase 141 P1(DONE): “既知 intrinsic だけ” を許可して段階投入(length0)
- Phase 141 P1.5(DONE): known intrinsic registry + available_inputs 3-source merge + diagnostics
- Phase 141 P2+(planned): Call/MethodCall 対応(effects + typing の段階投入)
- Phase 142-loopstmt P0(DONE): 正規化単位を statement(loop 1個)へ寄せる(パターン爆発を止める)
- Phase 142-loopstmt P1(DONE): LLVM EXE smoke(同 fixture)を追加
- Phase 143-loopvocab(planned): StepTree の語彙拡張(loop 内 if/break/continue を「語彙追加」で吸収)
- Phase 91–92: Selfhost depth‑2 coverage(P5b escape recognition → lowering)
- Phase 94–100: P5b escape E2E / Trim policy / pinned + accumulator(VM/LLVM EXE parity)
- Phase 102: real-app read_quoted loop regression(VM + LLVM EXE)
- Phase 103: if-only regression baseline(VM + LLVM EXE / plan)
- Phase 113: if-only partial assign parity(片側代入の保持 merge)
- Phase 107–109: real-app depth-scan / policy router SSOT / error hint SSOT
- Phase 110–112: ControlTree / StepTree(構造SSOT, dev-only)※設計SSOTは
../design/control-tree.md
Phase フォルダ構成(推奨)
phases/phase-131/
├── README.md (Phase 全体概要)
├── 131-03-llvm-lowering-inventory.md (LLVM 部分のテスト・検証)
├── 131-11-case-c-summary.md (Case C 実装サマリー)
└── phase131-11-case-c-root-cause-analysis.md (根本原因分析)
参照方法
- 現在の Phase を知りたい → ../10-Now.md
- 該当 Phase を詳しく知りたい → フォルダを開く
- 設計背景を知りたい → ../design/
- 調査ログを見たい → ../investigations/
Phase 命名規則
- ファイル名:
phase-<N>-<title>/(例:phase-131/) - 文書名:
<N>-<NN>-<topic>.md(例:131-11-case-c-summary.md)- Phase 番号で自然にソート可能
- 同一 Phase 内で段階的に追跡可能
作成ルール(SSOT)
詳しくは ../DOCS_LAYOUT.md を参照。
- ✅ 置き場所:
phases/phase-<N>/配下のみ - ✅ 内容: Phase の実装記録・進捗・チェックリスト・検証結果
- ❌ 避けるべき: 複数 Phase で参照される設計・アーキテクチャ(→ design/ へ)
最終更新: 2025-12-19