fix(joinir): Phase 118 Pattern3 exit carrier PHI SSOT

This commit is contained in:
nyash-codex
2025-12-18 03:43:00 +09:00
parent 2c7f5f7a5e
commit 1080dee58f
4 changed files with 161 additions and 134 deletions

View File

@ -127,13 +127,11 @@ pub fn lower_if_sum_pattern(
// main() locals
let i_init_val = alloc_value(); // i = 0
let sum_init_val = alloc_value(); // sum = 0
let count_init_val = alloc_value(); // count = 0 (optional)
let loop_result = alloc_value(); // result from loop_step
// loop_step params
let i_param = alloc_value();
let sum_param = alloc_value();
let count_param = alloc_value();
// loop_step locals
// Phase 220-D: loop_limit_val and if_value_val are already allocated by extract_*_condition()
@ -142,19 +140,14 @@ pub fn lower_if_sum_pattern(
let exit_cond = alloc_value(); // negated loop condition
let if_cmp = alloc_value(); // if condition comparison
let sum_then = alloc_value(); // sum + update_addend
let count_const = alloc_value(); // count increment (1)
let count_then = alloc_value(); // count + 1
let const_0 = alloc_value(); // 0 for else branch
let sum_else = alloc_value(); // sum + 0 (identity)
let count_else = alloc_value(); // count + 0 (identity)
let sum_new = alloc_value(); // Select result for sum
let count_new = alloc_value(); // Select result for count
let step_const = alloc_value(); // counter step
let i_next = alloc_value(); // i + step
// k_exit params
let sum_final = alloc_value();
let count_final = alloc_value();
// === main() function ===
let mut main_func = JoinFunction::new(main_id, "main".to_string(), vec![]);
@ -171,16 +164,10 @@ pub fn lower_if_sum_pattern(
value: ConstValue::Integer(0),
}));
// count_init = 0
main_func.body.push(JoinInst::Compute(MirLikeInst::Const {
dst: count_init_val,
value: ConstValue::Integer(0),
}));
// result = loop_step(i_init, sum_init, count_init)
// result = loop_step(i_init, sum_init)
main_func.body.push(JoinInst::Call {
func: loop_step_id,
args: vec![i_init_val, sum_init_val, count_init_val],
args: vec![i_init_val, sum_init_val],
k_next: None,
dst: Some(loop_result),
});
@ -191,11 +178,11 @@ pub fn lower_if_sum_pattern(
join_module.add_function(main_func);
// === loop_step(i, sum, count) function ===
// === loop_step(i, sum) function ===
let mut loop_step_func = JoinFunction::new(
loop_step_id,
"loop_step".to_string(),
vec![i_param, sum_param, count_param],
vec![i_param, sum_param],
);
// --- Exit Condition Check ---
@ -230,7 +217,7 @@ pub fn lower_if_sum_pattern(
// Jump to exit if condition is false
loop_step_func.body.push(JoinInst::Jump {
cont: k_exit_id.as_cont(),
args: vec![sum_param, count_param],
args: vec![sum_param],
cond: Some(exit_cond),
});
@ -270,22 +257,6 @@ pub fn lower_if_sum_pattern(
rhs: const_0,
}));
// count_then = count + 1
loop_step_func
.body
.push(JoinInst::Compute(MirLikeInst::Const {
dst: count_const,
value: ConstValue::Integer(1),
}));
loop_step_func
.body
.push(JoinInst::Compute(MirLikeInst::BinOp {
dst: count_then,
op: BinOpKind::Add,
lhs: count_param,
rhs: count_const,
}));
// --- Else Branch ---
// sum_else = sum + 0 (identity)
loop_step_func
@ -303,16 +274,6 @@ pub fn lower_if_sum_pattern(
rhs: step_const,
}));
// count_else = count + 0 (identity)
loop_step_func
.body
.push(JoinInst::Compute(MirLikeInst::BinOp {
dst: count_else,
op: BinOpKind::Add,
lhs: count_param,
rhs: step_const, // 0
}));
// --- Select ---
loop_step_func
.body
@ -323,15 +284,6 @@ pub fn lower_if_sum_pattern(
else_val: sum_else,
}));
loop_step_func
.body
.push(JoinInst::Compute(MirLikeInst::Select {
dst: count_new,
cond: if_cmp,
then_val: count_then,
else_val: count_else,
}));
// --- Counter Update ---
let step_const2 = alloc_value();
loop_step_func
@ -352,19 +304,15 @@ pub fn lower_if_sum_pattern(
// --- Tail Recursion ---
loop_step_func.body.push(JoinInst::Call {
func: loop_step_id,
args: vec![i_next, sum_new, count_new],
args: vec![i_next, sum_new],
k_next: None,
dst: None,
});
join_module.add_function(loop_step_func);
// === k_exit(sum_final, count_final) function ===
let mut k_exit_func = JoinFunction::new(
k_exit_id,
"k_exit".to_string(),
vec![sum_final, count_final],
);
// === k_exit(sum_final) function ===
let mut k_exit_func = JoinFunction::new(k_exit_id, "k_exit".to_string(), vec![sum_final]);
k_exit_func.body.push(JoinInst::Ret {
value: Some(sum_final),
@ -374,9 +322,9 @@ pub fn lower_if_sum_pattern(
join_module.entry = Some(main_id);
// Build ExitMeta
// SSOT: use the accumulator name extracted from AST (no hardcoded carrier names).
let mut exit_values = vec![];
exit_values.push(("sum".to_string(), sum_final));
exit_values.push(("count".to_string(), count_final));
exit_values.push((update_var.clone(), sum_final));
let exit_meta = ExitMeta::multiple(exit_values);