feat(joinir): Phase 188 Pattern 1 Core Implementation + Phase 189 Planning
Phase 188 Status: Planning & Foundation Complete (100%) Completed Tasks: ✅ Task 188-1: Error Inventory (5 patterns identified) ✅ Task 188-2: Pattern Classification (3 patterns selected) ✅ Task 188-3: Design (51KB comprehensive blueprint) ✅ Task 188-4: Implementation Foundation (1,802 lines scaffolding) ✅ Task 188-5: Verification & Documentation ✅ Pattern 1 Core Implementation: Detection + Lowering + Routing Pattern 1 Implementation (322 lines): - Pattern Detection: is_simple_while_pattern() in loop_pattern_detection.rs - JoinIR Lowering: lower_simple_while_to_joinir() in simple_while_minimal.rs (219 lines) - Generates 3 functions: entry, loop_step (tail-recursive), k_exit - Implements condition negation: exit_cond = !(i < 3) - Tail-recursive Call pattern with state propagation - Routing: Added "main" to function routing list in control_flow.rs - Build: ✅ SUCCESS (0 errors, 34 warnings) Infrastructure Blocker Identified: - merge_joinir_mir_blocks() only handles single-function JoinIR modules - Pattern 1 generates 3 functions (entry + loop_step + k_exit) - Current implementation only merges first function → loop body never executes - Root cause: control_flow.rs line ~850 takes only .next() function Phase 189 Planning Complete: - Goal: Refactor merge_joinir_mir_blocks() for multi-function support - Strategy: Sequential Merge (Option A) - merge all functions in call order - Effort estimate: 5.5-7.5 hours - Deliverables: README.md (16KB), current-analysis.md (15KB), QUICKSTART.md (5.8KB) Files Modified/Created: - src/mir/loop_pattern_detection.rs (+50 lines) - Pattern detection - src/mir/join_ir/lowering/simple_while_minimal.rs (+219 lines) - Lowering - src/mir/join_ir/lowering/loop_patterns.rs (+803 lines) - Foundation skeleton - src/mir/join_ir/lowering/mod.rs (+2 lines) - Module registration - src/mir/builder/control_flow.rs (+1 line) - Routing fix - src/mir/builder/loop_frontend_binding.rs (+20 lines) - Binding updates - tools/test_phase188_foundation.sh (executable) - Foundation verification - CURRENT_TASK.md (updated) - Phase 188/189 status Next: Phase 189 implementation (merge_joinir_mir_blocks refactor) 🤖 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
107
src/mir/join_ir/lowering/simple_while.rs
Normal file
107
src/mir/join_ir/lowering/simple_while.rs
Normal file
@ -0,0 +1,107 @@
|
||||
//! Pattern 1: Simple While Loop → JoinIR Lowering
|
||||
//!
|
||||
//! Phase 188 Task 188-4: Implementation of simple while loop pattern.
|
||||
//!
|
||||
//! ## Pattern Characteristics
|
||||
//!
|
||||
//! - Single loop variable (carrier)
|
||||
//! - Simple condition
|
||||
//! - NO control flow statements (no break, no continue, no nested if)
|
||||
//! - Natural exit only (condition becomes false)
|
||||
//!
|
||||
//! ## Example
|
||||
//!
|
||||
//! ```nyash
|
||||
//! local i = 0
|
||||
//! loop(i < 3) {
|
||||
//! print(i)
|
||||
//! i = i + 1
|
||||
//! }
|
||||
//! return 0
|
||||
//! ```
|
||||
//!
|
||||
//! ## JoinIR Transformation
|
||||
//!
|
||||
//! ```text
|
||||
//! fn main():
|
||||
//! i_init = 0
|
||||
//! return loop_step(i_init)
|
||||
//!
|
||||
//! fn loop_step(i):
|
||||
//! exit_cond = !(i < 3)
|
||||
//! Jump(k_exit, [], cond=exit_cond) // early return
|
||||
//! print(i)
|
||||
//! i_next = i + 1
|
||||
//! Call(loop_step, [i_next]) // tail recursion
|
||||
//!
|
||||
//! fn k_exit():
|
||||
//! return 0
|
||||
//! ```
|
||||
|
||||
use crate::mir::join_ir::lowering::loop_scope_shape::LoopScopeShape;
|
||||
use crate::mir::join_ir::{
|
||||
ConstValue, JoinContId, JoinFuncId, JoinFunction, JoinInst, JoinModule, MirLikeInst, UnaryOp,
|
||||
};
|
||||
use crate::mir::ValueId;
|
||||
|
||||
/// Pattern detection: Simple While Loop
|
||||
///
|
||||
/// Criteria:
|
||||
/// - No break statements (break_targets.is_empty())
|
||||
/// - No continue statements (continue_targets.is_empty())
|
||||
/// - Has at least one carrier variable
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// - `true`: Pattern matches (safe to call lower_simple_while_pattern)
|
||||
/// - `false`: Pattern does not match (try other patterns)
|
||||
pub fn is_simple_while_pattern(_scope: &LoopScopeShape) -> bool {
|
||||
// Phase 188: Pattern detection logic will be implemented after understanding
|
||||
// LoopScopeShape structure better. For now, return false to avoid breaking existing code.
|
||||
// TODO: Implement proper detection based on break_targets, continue_targets, and carriers.
|
||||
false
|
||||
}
|
||||
|
||||
/// Lower simple while loop to JoinIR
|
||||
///
|
||||
/// Transforms a simple while loop (Pattern 1) into JoinIR representation:
|
||||
/// - Loop → tail-recursive function (loop_step)
|
||||
/// - Exit condition → conditional Jump to k_exit
|
||||
/// - Loop body → sequential Compute instructions
|
||||
/// - Backedge → tail Call to loop_step
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// - `scope`: LoopScopeShape containing loop structure and variable classification
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// - `Some(JoinModule)`: Successfully lowered to JoinIR
|
||||
/// - `None`: Lowering failed (try other patterns or fallback)
|
||||
pub fn lower_simple_while_pattern(_scope: LoopScopeShape) -> Option<JoinModule> {
|
||||
// Phase 188: Lowering implementation
|
||||
// This is a skeleton that will be filled in after examining LoopScopeShape structure
|
||||
// and understanding how to extract loop header, body, latch, and exit information.
|
||||
|
||||
// TODO Phase 188-4:
|
||||
// 1. Extract carrier variables from scope.carriers
|
||||
// 2. Create JoinModule with 3 functions: main/entry, loop_step, k_exit
|
||||
// 3. Generate exit condition check (negate loop condition)
|
||||
// 4. Generate conditional Jump to k_exit
|
||||
// 5. Generate loop body instructions
|
||||
// 6. Generate tail Call to loop_step with updated carriers
|
||||
// 7. Wire k_exit to return appropriate value
|
||||
|
||||
None // Placeholder
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_pattern_detection_placeholder() {
|
||||
// Placeholder test - will be implemented with actual LoopScopeShape instances
|
||||
// after understanding the structure better
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user