feat(joinir): Phase 41-4.1 Add NestedIfMerge JoinInst variant

Add structural support for nested if patterns with PHI-sensitive variables:

1. JoinInst::NestedIfMerge in mod.rs
   - conds: Vec<VarId> (outer to inner conditions)
   - merges: Vec<MergePair> (variable updates at deepest level)
   - k_next: Option<JoinContId> (continuation after merge)

2. JSON serialization in json.rs
   - Type: "nested_if_merge"
   - Fields: conds[], merges[], k_next

3. Runner/Bridge stubs
   - JoinIR Runner: Returns error (use VM Bridge instead)
   - VM Bridge: panic placeholder (41-4.3 will implement)

Target: ParserControlBox.parse_loop() (4-level nested if)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-11-28 12:32:25 +09:00
parent 1b9589e9a3
commit d8a1d97222
4 changed files with 83 additions and 0 deletions

View File

@ -180,6 +180,38 @@ fn write_inst<W: Write>(inst: &JoinInst, out: &mut W) -> std::io::Result<()> {
write!(out, "]")?; write!(out, "]")?;
write!(out, "}}")?; write!(out, "}}")?;
} }
// Phase 41-4: NestedIfMerge instruction JSON serialization
JoinInst::NestedIfMerge { conds, merges, k_next } => {
write!(out, "{{\"type\":\"nested_if_merge\"")?;
// conds array
write!(out, ",\"conds\":[")?;
for (i, cond) in conds.iter().enumerate() {
if i > 0 {
write!(out, ",")?;
}
write!(out, "{}", cond.0)?;
}
write!(out, "]")?;
// merges array
write!(out, ",\"merges\":[")?;
for (i, merge) in merges.iter().enumerate() {
if i > 0 {
write!(out, ",")?;
}
write!(out, "{{")?;
write!(out, "\"dst\":{}", merge.dst.0)?;
write!(out, ",\"then_val\":{}", merge.then_val.0)?;
write!(out, ",\"else_val\":{}", merge.else_val.0)?;
write!(out, "}}")?;
}
write!(out, "]")?;
// k_next
match k_next {
Some(k) => write!(out, ",\"k_next\":{}", k.0)?,
None => write!(out, ",\"k_next\":null")?,
}
write!(out, "}}")?;
}
JoinInst::Compute(mir_like) => { JoinInst::Compute(mir_like) => {
write!(out, "{{\"type\":\"compute\",\"op\":")?; write!(out, "{{\"type\":\"compute\",\"op\":")?;
write_mir_like_inst(mir_like, out)?; write_mir_like_inst(mir_like, out)?;

View File

@ -322,6 +322,38 @@ pub enum JoinInst {
args: Vec<VarId>, args: Vec<VarId>,
}, },
/// Phase 41-4: 深いネスト if の複数変数 mergeelse なし)
///
/// # Pattern
/// ```text
/// if cond1 {
/// if cond2 {
/// if cond3 {
/// x = new_val // modifications only at deepest level
/// }
/// }
/// }
/// // merge: x = phi(new_val if all conds true, original otherwise)
/// ```
///
/// # Semantics
/// - `conds`: 外側から内側への条件リスト
/// - `merges`: 最深レベルでの変数更新
/// - then_val: 全ての conds が true の場合の値
/// - else_val: いずれかの cond が false の場合の値(元の値)
/// - MIR 変換時に多段 Branch + PHI を生成
///
/// # Target
/// ParserControlBox.parse_loop() の 4 レベルネスト if パターン
NestedIfMerge {
/// 条件リスト(外側から内側へ)
conds: Vec<VarId>,
/// 変数更新(全条件 true 時 → then_val、いずれか false 時 → else_val
merges: Vec<MergePair>,
/// merge 後の継続
k_next: Option<JoinContId>,
},
/// それ以外の演算は、現行 MIR の算術/比較/boxcall を再利用 /// それ以外の演算は、現行 MIR の算術/比較/boxcall を再利用
Compute(MirLikeInst), Compute(MirLikeInst),
} }

View File

@ -190,6 +190,14 @@ fn execute_function(
"MethodCall is not supported in JoinIR Runner (use JoinIR→MIR→VM bridge instead)" "MethodCall is not supported in JoinIR Runner (use JoinIR→MIR→VM bridge instead)"
)); ));
} }
// Phase 41-4: NestedIfMerge instruction execution
JoinInst::NestedIfMerge { .. } => {
// Phase 41-4: NestedIfMerge は JoinIR Runner では未対応
// JoinIR → MIR 変換経由で VM が実行する
return Err(JoinRuntimeError::new(
"NestedIfMerge is not supported in JoinIR Runner (use JoinIR→MIR→VM bridge instead)"
));
}
} }
} }

View File

@ -604,6 +604,17 @@ Call {{\n\
current_instructions = Vec::new(); current_instructions = Vec::new();
} }
// Phase 41-4: NestedIfMerge instruction
JoinInst::NestedIfMerge { conds, merges, k_next } => {
// Phase 41-4.3 で実装予定
// TODO: 多段 Branch + PHI を生成
// 今は dev フラグ (HAKO_JOINIR_NESTED_IF) でしか到達しないので panic で OK
let _ = (conds, merges, k_next); // suppress unused warnings
panic!(
"[Phase 41-4] NestedIfMerge is not yet implemented in VM Bridge. \
This instruction is Phase 41-4.3 work-in-progress."
);
}
} }
} }