Files
hakorune/src/mir/builder/control_flow/joinir/routing.rs

166 lines
7.3 KiB
Rust
Raw Normal View History

//! JoinIR routing logic for loop lowering
use super::trace;
use crate::ast::ASTNode;
use crate::mir::builder::MirBuilder;
use crate::mir::ValueId;
impl MirBuilder {
/// Phase 49: Try JoinIR Frontend for mainline integration
///
/// Returns `Ok(Some(value))` if the loop is successfully lowered via JoinIR,
/// `Ok(None)` if no JoinIR pattern matched (unsupported loop structure).
/// Phase 187-2: Legacy LoopBuilder removed - all loops must use JoinIR.
///
/// # Phase 49-4: Multi-target support
///
/// Targets are enabled via separate dev flags:
/// - `HAKO_JOINIR_PRINT_TOKENS_MAIN=1`: JsonTokenizer.print_tokens/0
/// - `HAKO_JOINIR_ARRAY_FILTER_MAIN=1`: ArrayExtBox.filter/2
///
/// Note: Arity in function names does NOT include implicit `me` receiver.
/// - Instance method `print_tokens()` → `/0` (no explicit params)
/// - Static method `filter(arr, pred)` → `/2` (two params)
pub(in crate::mir::builder) fn try_cf_loop_joinir(
&mut self,
condition: &ASTNode,
body: &[ASTNode],
) -> Result<Option<ValueId>, String> {
// Get current function name
let func_name = self
.current_function
.as_ref()
.map(|f| f.signature.name.clone())
.unwrap_or_default();
feat(joinir): Phase 195 - Unified JoinLoopTrace for all JoinIR debug output Created centralized tracing module for JoinIR loop lowering operations, consolidating scattered eprintln! calls into a single SSOT interface. # Implementation 1. **Created trace.rs module** (~233 lines) - JoinLoopTrace struct with env var controls - Unified API: pattern(), varmap(), joinir_stats(), phi(), merge(), etc. - Global singleton via trace() function - Supports 5 env vars: NYASH_TRACE_VARMAP, NYASH_JOINIR_DEBUG, NYASH_OPTION_C_DEBUG, NYASH_JOINIR_MAINLINE_DEBUG, NYASH_LOOPFORM_DEBUG 2. **Updated debug.rs** - Delegates trace_varmap() to JoinLoopTrace 3. **Updated routing.rs** - All eprintln! replaced with trace calls (10 instances) 4. **Updated pattern routers** - All 3 patterns now use unified trace - pattern1_minimal.rs: 6 replacements - pattern2_with_break.rs: 6 replacements - pattern3_with_if_phi.rs: 6 replacements - router.rs: 2 replacements 5. **Updated merge/block_allocator.rs** - 6 eprintln! → trace calls # Benefits - **Single Source of Truth**: All trace control through environment variables - **Consistent Format**: Unified [trace:*] prefix for easy filtering - **Zero Overhead**: No output when env vars unset - **Easy Extension**: Add new trace points via existing API - **Better Debug Experience**: Structured output with clear categories # Testing ✅ cargo build --release - Success ✅ NYASH_TRACE_VARMAP=1 - Shows varmap traces only ✅ NYASH_JOINIR_DEBUG=1 - Shows joinir + blocks + routing traces ✅ No env vars - No debug output ✅ apps/tests/loop_min_while.hako - All tests pass # Related - Phase 191-194 groundwork (modular merge structure) - NYASH_TRACE_VARMAP added today for variable_map debugging - Consolidates ~80 scattered eprintln! calls across JoinIR 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:23:51 +09:00
// Phase 195: Use unified trace
trace::trace().routing("router", &func_name, "try_cf_loop_joinir called");
// Phase 170-4: Structure-based routing option
// When NYASH_JOINIR_STRUCTURE_ONLY=1, skip function name whitelist
// and route purely based on loop structure analysis
// Phase 196: Default to structure-first routing now that LoopBuilder is removed.
// - Default: ON (structure_only = true) to allow JoinIR patterns to run for all loops.
// - To revert to the previous whitelist-only behavior, set NYASH_JOINIR_STRUCTURE_ONLY=0.
let structure_only = match std::env::var("NYASH_JOINIR_STRUCTURE_ONLY").ok().as_deref() {
Some("0") | Some("off") => false,
_ => true,
};
if structure_only {
trace::trace().routing(
"router",
&func_name,
"Structure-only mode enabled, skipping whitelist",
);
} else {
// Phase 49-4 + Phase 80: Multi-target routing (legacy whitelist)
// - JoinIR は常時 ON。legacy LoopBuilder は削除済み。
// - 代表2本print_tokens / ArrayExt.filterも常に JoinIR で試行する。
// Note: Arity does NOT include implicit `me` receiver
// Phase 188: Add "main" routing for loop pattern expansion
// Phase 170: Add JsonParserBox methods for selfhost validation
let is_target = match func_name.as_str() {
"main" => true, // Phase 188-Impl-1: Enable JoinIR for main function (Pattern 1)
"JoinIrMin.main/0" => true, // Phase 188-Impl-2: Enable JoinIR for JoinIrMin.main/0 (Pattern 2)
"JsonTokenizer.print_tokens/0" => true,
"ArrayExtBox.filter/2" => true,
// Phase 170-A-1: Enable JsonParserBox methods for JoinIR routing
"JsonParserBox._trim/1" => true,
"JsonParserBox._skip_whitespace/2" => true,
"JsonParserBox._match_literal/3" => true, // Phase 182: Fixed arity (s, pos, literal)
"JsonParserBox._parse_string/2" => true,
"JsonParserBox._parse_array/2" => true,
"JsonParserBox._parse_object/2" => true,
// Phase 182: Add simple loop methods
"JsonParserBox._parse_number/2" => true, // P2 Break (s, pos)
"JsonParserBox._atoi/1" => true, // P2 Break (s)
// Phase 170-A-1: Test methods (simplified versions)
"TrimTest.trim/1" => true,
"Main.trim/1" => true, // Phase 171-fix: Main box variant
"Main.trim_string_simple/1" => true, // Phase 33-13: Simple trim variant
"TrimTest.main/0" => true, // Phase 170: TrimTest.main for loop pattern test
feat(joinir): Phase 173 - JsonParser P5 expansion with _skip_whitespace - Task 173-1: JsonParser loop inventory recheck - Observed 6 loops: _trim (2 loops, already working), _skip_whitespace, _parse_string, _parse_array, _unescape_string - Created comprehensive observation report with Trim similarity ratings - Discovered that JsonParser._trim already uses P5 pipeline successfully - Task 173-2: Selected _skip_whitespace as Trim-equivalent pattern - Perfect structural match with Trim (100% identical) - Independent helper method, easy to test - Frequently used in JsonParser (7 call sites) - Task 173-3: Design doc for P5 pipeline extension to JsonParser - Confirmed existing TrimLoopHelper works without modification - No charAt() support needed (_skip_whitespace uses substring()) - Documented data flow and risk analysis - Task 173-4: Successfully converted _skip_whitespace to JoinIR - Created test case: local_tests/test_jsonparser_skip_whitespace.hako - Added routing whitelist: JsonParserTest._skip_whitespace/3 - Pattern detection SUCCESS: * LoopBodyLocal 'ch' detected * Carrier promotion to 'is_ch_match' * Trim pattern recognized * JoinIR generation successful - Verified P5 pipeline works for both static box methods and helper methods - Task 173-5: Documentation updates - phase173-jsonparser-loop-recheck.md: Loop observation report - phase173-jsonparser-p5-design.md: P5 extension design - phase173-jsonparser-p5-impl.md: Implementation results - CURRENT_TASK.md: Phase 173 completion record Key achievement: Proven that Trim P5 pipeline is fully generic - works for both TrimTest (static box method) and JsonParser (helper method). LoopBodyLocal carrier promotion is production-ready for Trim-like patterns. Changes: - src/mir/builder/control_flow/joinir/routing.rs: Add JsonParserTest whitelist - docs/development/current/main/*173*.md: 3 new documentation files - CURRENT_TASK.md: Phase 173 completion entry
2025-12-08 10:13:34 +09:00
// Phase 173: JsonParser P5 expansion test
"JsonParserTest._skip_whitespace/3" => true,
"JsonParserTest.main/0" => true,
feat(joinir): Phase 174 - JsonParser complex loop P5 extension design - Task 174-1: Complex loop inventory (_parse_string/_parse_array/_parse_object) - Analyzed remaining JsonParser loops for P5 expansion potential - Identified _parse_string as most Trim-like structure (99% similarity) - Documented complexity scores and minimization potential - Task 174-2: Selected _parse_string as next P5 target (closest to Trim) - Reason: LoopBodyLocal 'ch' usage matches Trim pattern exactly - Structure: loop(pos < len) + substring + char comparison + break - Minimization: Remove escape/buffer/continue → identical to Trim - Task 174-3: Design doc for P5B extension (TrimLoopHelper reuse strategy) - File: docs/development/current/main/phase174-jsonparser-p5b-design.md - Strategy: Reuse existing TrimLoopHelper without modifications - Proven: Pattern applies to any character comparison, not just whitespace - Task 174-4: Minimal PoC (_parse_string without escape) successful - Test: local_tests/test_jsonparser_parse_string_min.hako - Result: [pattern2/trim] Safe Trim pattern detected ✅ - Detection: Trim with literals=['"'] (quote instead of whitespace) - Routing: Added whitelist entries for JsonParserStringTest methods - Task 174-5: Documentation updates - Updated CURRENT_TASK.md with Phase 174 summary - Updated joinir-architecture-overview.md with P5 generality proof - Created phase174-jsonparser-loop-inventory-2.md (detailed analysis) - Created phase174-jsonparser-p5b-design.md (implementation strategy) Success Criteria Met: ✅ _parse_string minimized version runs on P5 pipeline ✅ TrimLoopHelper works with '"' (non-whitespace character) ✅ Proven: Trim pattern is character-comparison-generic, not whitespace-specific ✅ Two new design docs (inventory + design) ✅ Phase 175+ roadmap established (multi-carrier, escape sequences) Technical Achievement: The P5 Trim pipeline successfully handled a quote-detection loop with zero code changes, proving the architecture's generality beyond whitespace trimming.
2025-12-08 13:08:44 +09:00
// Phase 174: JsonParser complex loop P5B extension test
"JsonParserStringTest.parse_string_min/0" => true,
"JsonParserStringTest.main/0" => true,
feat(joinir): Phase 175 - P5 multi-carrier architecture validation Task 175-1: Analyzed _parse_string carrier candidates - Identified 3 carriers: pos (position), result (buffer), is_ch_match (promoted) - Categorized as: required carriers (pos, result), promoted carrier (is_ch_match) Task 175-2: Validated existing boxes support multi-carrier - CarrierInfo: Vec<CarrierData> supports arbitrary carriers ✅ - LoopUpdateAnalyzer: Loops over all carriers ✅ - ExitMeta: Vec<(String, ValueId)> supports all exit bindings ✅ - ExitLineReconnector: Reconnects all carriers to variable_map ✅ - No code changes needed - architecture already supports it! Task 175-3: PoC test revealed Pattern2 limitation - Test: test_jsonparser_parse_string_min2.hako (pos + result carriers) - CarrierInfo detected 3 carriers correctly (pos, result, is_ch_match) - variable_map contains all carriers at pattern2_start - BUT: Pattern2's Trim optimization only emits pos carrier in MIR - MIR shows result stays as empty string (no loop update emitted) - Root cause: Trim pattern focuses on position-only optimization Task 175-4: Documentation updates - Created: phase175-multicarrier-design.md (comprehensive analysis) - Updated: CURRENT_TASK.md (Phase 175 completion) - Updated: routing.rs (added JsonParserStringTest2 whitelist) Key Finding: - Architecture is sound ✅ - all boxes support multi-carrier - Pattern2 implementation gap ❌ - Trim optimization ignores non-position carriers - Phase 176 scope: Extend Pattern2 to emit all carrier updates Next: Phase 176 for escape sequence handling and full multi-carrier emission
2025-12-08 13:34:43 +09:00
// Phase 175: P5 multi-carrier support (2 carriers: pos + result)
"JsonParserStringTest2.parse_string_min2/0" => true,
"JsonParserStringTest2.main/0" => true,
_ => false,
};
if !is_target {
return Ok(None);
}
}
// Debug log when routing through JoinIR Frontend
feat(joinir): Phase 195 - Unified JoinLoopTrace for all JoinIR debug output Created centralized tracing module for JoinIR loop lowering operations, consolidating scattered eprintln! calls into a single SSOT interface. # Implementation 1. **Created trace.rs module** (~233 lines) - JoinLoopTrace struct with env var controls - Unified API: pattern(), varmap(), joinir_stats(), phi(), merge(), etc. - Global singleton via trace() function - Supports 5 env vars: NYASH_TRACE_VARMAP, NYASH_JOINIR_DEBUG, NYASH_OPTION_C_DEBUG, NYASH_JOINIR_MAINLINE_DEBUG, NYASH_LOOPFORM_DEBUG 2. **Updated debug.rs** - Delegates trace_varmap() to JoinLoopTrace 3. **Updated routing.rs** - All eprintln! replaced with trace calls (10 instances) 4. **Updated pattern routers** - All 3 patterns now use unified trace - pattern1_minimal.rs: 6 replacements - pattern2_with_break.rs: 6 replacements - pattern3_with_if_phi.rs: 6 replacements - router.rs: 2 replacements 5. **Updated merge/block_allocator.rs** - 6 eprintln! → trace calls # Benefits - **Single Source of Truth**: All trace control through environment variables - **Consistent Format**: Unified [trace:*] prefix for easy filtering - **Zero Overhead**: No output when env vars unset - **Easy Extension**: Add new trace points via existing API - **Better Debug Experience**: Structured output with clear categories # Testing ✅ cargo build --release - Success ✅ NYASH_TRACE_VARMAP=1 - Shows varmap traces only ✅ NYASH_JOINIR_DEBUG=1 - Shows joinir + blocks + routing traces ✅ No env vars - No debug output ✅ apps/tests/loop_min_while.hako - All tests pass # Related - Phase 191-194 groundwork (modular merge structure) - NYASH_TRACE_VARMAP added today for variable_map debugging - Consolidates ~80 scattered eprintln! calls across JoinIR 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:23:51 +09:00
// Phase 195: Check trace flags directly from JoinLoopTrace
let debug = trace::trace().is_loopform_enabled() || trace::trace().is_mainline_enabled();
trace::trace().routing(
"router",
&func_name,
"Routing through JoinIR Frontend mainline",
);
// Phase 49-3: Implement JoinIR Frontend integration
self.cf_loop_joinir_impl(condition, body, &func_name, debug)
}
/// Phase 49-3: JoinIR Frontend integration implementation
///
/// Routes loop compilation through either:
/// 1. Pattern-based router (Phase 194+) - preferred for new patterns
/// 2. Legacy binding path (Phase 49-3) - for whitelisted functions only
pub(in crate::mir::builder) fn cf_loop_joinir_impl(
&mut self,
condition: &ASTNode,
body: &[ASTNode],
func_name: &str,
debug: bool,
) -> Result<Option<ValueId>, String> {
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
// Phase 194: Use table-driven router instead of if/else chain
use super::patterns::{route_loop_pattern, LoopPatternContext};
// Phase 200-C: Pass fn_body_ast to LoopPatternContext if available
// Clone fn_body_ast to avoid borrow checker issues
let fn_body_clone = self.fn_body_ast.clone();
refactor(joinir): Box-First cleanup - trace unification + SSOT + Fail-Fast ## Changes ### 1. eprintln! to trace.rs unification **File**: src/mir/builder/control_flow/joinir/routing.rs - Replaced direct eprintln! with trace::trace().routing() - Box-First principle: Log responsibility centralized in trace.rs - Enables filtering via HAKO_JOINIR_DEBUG=1 ### 2. Priority field removal (SSOT) **File**: src/mir/builder/control_flow/joinir/patterns/router.rs - Removed #[allow(dead_code)] priority field - Array order is SSOT (Single Source of Truth) for pattern priority - Pattern try order: Pattern5 → Pattern4 → Pattern3 → Pattern1 → Pattern2 - Eliminated dead code warning ### 3. catch_unwind removal (Fail-Fast) **File**: src/mir/builder/control_flow/joinir/routing_legacy_binding.rs - Removed catch_unwind + Ok(None) silent error swallowing - Fail-Fast principle: Panics propagate explicitly - Enables early detection of panic sources ## Verification ✅ Build: cargo build --release (0 errors) ✅ Tests: 71 JoinIR tests all PASS ✅ VM: /tmp/p1_return_i.hako → Result: 3 ✅ LLVM: /tmp/p1_return_i.hako → Mock exit code: 0 ## Design Principles Applied - **Box-First**: Log responsibility → trace.rs - **SSOT**: Array order defines priority (no redundant field) - **Fail-Fast**: Explicit failures, no silent error swallowing ## Statistics - 3 files changed - 53 deletions, 28 insertions - Net: -25 lines (code reduction) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-15 03:27:47 +09:00
trace::trace().routing(
"router",
func_name,
&format!(
"fn_body_ast is {}",
if fn_body_clone.is_some() { "SOME" } else { "NONE" }
),
);
let ctx = if let Some(ref fn_body) = fn_body_clone {
refactor(joinir): Box-First cleanup - trace unification + SSOT + Fail-Fast ## Changes ### 1. eprintln! to trace.rs unification **File**: src/mir/builder/control_flow/joinir/routing.rs - Replaced direct eprintln! with trace::trace().routing() - Box-First principle: Log responsibility centralized in trace.rs - Enables filtering via HAKO_JOINIR_DEBUG=1 ### 2. Priority field removal (SSOT) **File**: src/mir/builder/control_flow/joinir/patterns/router.rs - Removed #[allow(dead_code)] priority field - Array order is SSOT (Single Source of Truth) for pattern priority - Pattern try order: Pattern5 → Pattern4 → Pattern3 → Pattern1 → Pattern2 - Eliminated dead code warning ### 3. catch_unwind removal (Fail-Fast) **File**: src/mir/builder/control_flow/joinir/routing_legacy_binding.rs - Removed catch_unwind + Ok(None) silent error swallowing - Fail-Fast principle: Panics propagate explicitly - Enables early detection of panic sources ## Verification ✅ Build: cargo build --release (0 errors) ✅ Tests: 71 JoinIR tests all PASS ✅ VM: /tmp/p1_return_i.hako → Result: 3 ✅ LLVM: /tmp/p1_return_i.hako → Mock exit code: 0 ## Design Principles Applied - **Box-First**: Log responsibility → trace.rs - **SSOT**: Array order defines priority (no redundant field) - **Fail-Fast**: Explicit failures, no silent error swallowing ## Statistics - 3 files changed - 53 deletions, 28 insertions - Net: -25 lines (code reduction) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-15 03:27:47 +09:00
trace::trace().routing(
"router",
func_name,
&format!("Creating ctx with fn_body ({} nodes)", fn_body.len()),
);
LoopPatternContext::with_fn_body(condition, body, &func_name, debug, fn_body)
} else {
LoopPatternContext::new(condition, body, &func_name, debug)
};
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
if let Some(result) = route_loop_pattern(self, &ctx)? {
feat(joinir): Phase 195 - Unified JoinLoopTrace for all JoinIR debug output Created centralized tracing module for JoinIR loop lowering operations, consolidating scattered eprintln! calls into a single SSOT interface. # Implementation 1. **Created trace.rs module** (~233 lines) - JoinLoopTrace struct with env var controls - Unified API: pattern(), varmap(), joinir_stats(), phi(), merge(), etc. - Global singleton via trace() function - Supports 5 env vars: NYASH_TRACE_VARMAP, NYASH_JOINIR_DEBUG, NYASH_OPTION_C_DEBUG, NYASH_JOINIR_MAINLINE_DEBUG, NYASH_LOOPFORM_DEBUG 2. **Updated debug.rs** - Delegates trace_varmap() to JoinLoopTrace 3. **Updated routing.rs** - All eprintln! replaced with trace calls (10 instances) 4. **Updated pattern routers** - All 3 patterns now use unified trace - pattern1_minimal.rs: 6 replacements - pattern2_with_break.rs: 6 replacements - pattern3_with_if_phi.rs: 6 replacements - router.rs: 2 replacements 5. **Updated merge/block_allocator.rs** - 6 eprintln! → trace calls # Benefits - **Single Source of Truth**: All trace control through environment variables - **Consistent Format**: Unified [trace:*] prefix for easy filtering - **Zero Overhead**: No output when env vars unset - **Easy Extension**: Add new trace points via existing API - **Better Debug Experience**: Structured output with clear categories # Testing ✅ cargo build --release - Success ✅ NYASH_TRACE_VARMAP=1 - Shows varmap traces only ✅ NYASH_JOINIR_DEBUG=1 - Shows joinir + blocks + routing traces ✅ No env vars - No debug output ✅ apps/tests/loop_min_while.hako - All tests pass # Related - Phase 191-194 groundwork (modular merge structure) - NYASH_TRACE_VARMAP added today for variable_map debugging - Consolidates ~80 scattered eprintln! calls across JoinIR 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:23:51 +09:00
trace::trace().routing("router", func_name, "Pattern router succeeded");
feat(joinir): Phase 194 - Table-driven loop pattern router Replace if/else chain with table-driven pattern dispatch for easier pattern addition and maintenance. # Changes ## New Files - router.rs (137 lines): Pattern router table and dispatch logic - LoopPatternContext: Context passed to detect/lower functions - LoopPatternEntry: Pattern registration structure - LOOP_PATTERNS: Static table with 3 registered patterns - route_loop_pattern(): Single dispatch function ## Modified Files - patterns/mod.rs (+8 lines): Export router module - pattern1_minimal.rs (+19 lines): Added can_lower() + lower() wrapper - pattern2_with_break.rs (+17 lines): Added can_lower() + lower() wrapper - pattern3_with_if_phi.rs (+22 lines): Added can_lower() + lower() wrapper - routing.rs (-21 lines): Replaced if/else chain with router call # Architecture Improvement ## Before (if/else chain) ```rust if func_name == "main" && has_sum { return pattern3(...); } else if func_name == "main" { return pattern1(...); } else if func_name == "JoinIrMin.main/0" { return pattern2(...); } ``` ## After (table-driven) ```rust let ctx = LoopPatternContext::new(...); route_loop_pattern(self, &ctx)? ``` # Adding New Patterns (Now Trivial!) 1. Create pattern4_your_name.rs 2. Implement can_lower() + lower() 3. Add entry to LOOP_PATTERNS table That's it! No routing logic changes needed. # Testing ✅ Pattern 1 (loop_min_while.hako): PASSED ✅ Pattern 2 (joinir_min_loop.hako): PASSED ✅ Pattern 3 (loop_if_phi.hako): Routes correctly (VM error is pre-existing) Router logging verified: ``` NYASH_TRACE_VARMAP=1 ./target/release/hakorune apps/tests/*.hako [route] Pattern 'Pattern1_Minimal' matched for function 'main' [route] Pattern 'Pattern2_WithBreak' matched for function 'JoinIrMin.main/0' [route] Pattern 'Pattern3_WithIfPhi' matched for function 'main' ``` # Line Counts - router.rs: 137 lines (new) - Total pattern files: 491 lines (3 patterns) - routing.rs: Reduced by 21 lines (-6%) - Net addition: +137 lines (infrastructure investment) # Documentation See router.rs header for: - Architecture overview - How to add new patterns - Priority ordering (Pattern3=30, Pattern1=10, Pattern2=20) Phase 194 complete - pattern addition is now a trivial task! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 22:11:39 +09:00
return Ok(Some(result));
}
// Phase 187-2: Pattern router failed, try legacy whitelist
trace::trace().routing(
"router",
func_name,
"Pattern router found no match, trying legacy whitelist",
);
// Delegate to legacy binding path (routing_legacy_binding.rs)
self.cf_loop_joinir_legacy_binding(condition, body, func_name, debug)
}
}