MIR builder (modularized/control_flow): replace LoopBuilder dependency with simplified loop lowering to decouple types; add TODO for SSA phi reintroduction.

This commit is contained in:
Moe Charm
2025-08-25 19:54:59 +09:00
parent 8fb99d0763
commit 06cdc7aff6

View File

@ -94,10 +94,38 @@ impl MirBuilder {
}
/// Build a loop statement: loop(condition) { body }
///
/// Note: This modularized builder uses a simplified lowering that avoids
/// coupling to mir::loop_builder (which expects mir::builder::MirBuilder).
/// Proper SSA phi construction should be reintroduced via an adapter/facade.
pub(super) fn build_loop_statement(&mut self, condition: ASTNode, body: Vec<ASTNode>) -> Result<ValueId, String> {
// Use the specialized LoopBuilder for proper SSA loop construction
let mut loop_builder = super::loop_builder::LoopBuilder::new(self);
loop_builder.build_loop(condition, body)
// Blocks: preheader (current), header, body, after
let header_bb = self.block_gen.next();
let body_bb = self.block_gen.next();
let after_bb = self.block_gen.next();
// Jump to header from current block
self.emit_instruction(MirInstruction::Jump { target: header_bb })?;
// Header: evaluate condition and branch
self.start_new_block(header_bb)?;
let cond_val = self.build_expression(condition)?;
self.emit_instruction(MirInstruction::Branch { condition: cond_val, then_bb: body_bb, else_bb: after_bb })?;
// Body: build statements, then jump back to header
self.start_new_block(body_bb)?;
for stmt in body {
let _ = self.build_expression(stmt)?;
}
if !self.is_current_block_terminated() {
self.emit_instruction(MirInstruction::Jump { target: header_bb })?;
}
// After: return void value
self.start_new_block(after_bb)?;
let void_id = self.value_gen.next();
self.emit_instruction(MirInstruction::Const { dst: void_id, value: ConstValue::Void })?;
Ok(void_id)
}
/// Build a try/catch statement
@ -191,4 +219,4 @@ impl MirBuilder {
}
false
}
}
}