Files
hakorune/src/runner/json_v0_bridge/lowering/throw_ctx.rs
nyash-codex d3cbc71c9b feat(mir): Phase 25.1f完了 - Conservative PHI + ControlForm観測レイヤー
🎉 Conservative PHI Box理論による完全SSA構築

**Phase 7-B: Conservative PHI実装**
- 片方branchのみ定義変数に対応(emit_void使用)
- 全変数にPHI生成(Conservative Box理論)
- Stage-1 resolver全テスト緑化(3/3 PASS)

**Phase 25.1f: ControlForm観測レイヤー**
- LoopShape/IfShape/ControlForm構造定義
- Loop/If統一インターフェース実装
- debug_dump/debug_validate機能追加
- NYASH_CONTROL_FORM_TRACE環境変数対応

**主な変更**:
- src/mir/builder/phi.rs: Conservative PHI実装
- src/mir/control_form.rs: ControlForm構造(NEW)
- src/mir/loop_builder.rs: LoopForm v2デフォルト化

**テスト結果**:
 mir_stage1_using_resolver_min_fragment_verifies
 mir_stage1_using_resolver_full_collect_entries_verifies
 mir_parserbox_parse_program2_harness_parses_minimal_source

🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: ChatGPT <chatgpt@openai.com>
2025-11-18 18:56:35 +09:00

48 lines
1.4 KiB
Rust

use crate::mir::{BasicBlockId, MirFunction, ValueId};
use std::cell::RefCell;
thread_local! {
static THROW_CTX: RefCell<Option<ThrowCtx>> = RefCell::new(None);
}
#[derive(Clone, Debug)]
pub(super) struct ThrowCtx {
pub(super) catch_bb: BasicBlockId,
pub(super) incoming: Vec<(BasicBlockId, ValueId)>,
}
impl ThrowCtx {
fn new(catch_bb: BasicBlockId) -> Self {
Self { catch_bb, incoming: Vec::new() }
}
}
pub(super) fn set(catch_bb: BasicBlockId) {
THROW_CTX.with(|slot| {
*slot.borrow_mut() = Some(ThrowCtx::new(catch_bb));
});
}
pub(super) fn take() -> Option<ThrowCtx> {
THROW_CTX.with(|slot| slot.borrow_mut().take())
}
pub(super) fn is_active() -> bool {
THROW_CTX.with(|slot| slot.borrow().is_some())
}
/// Record a throw from `from_bb` with value `exc_val`. Sets terminator Jump to catch and
/// appends predecessor+value to the incoming list. Returns the catch block id if active.
pub(super) fn record_throw(f: &mut MirFunction, from_bb: BasicBlockId, exc_val: ValueId) -> Option<BasicBlockId> {
THROW_CTX.with(|slot| {
if let Some(ctx) = slot.borrow_mut().as_mut() {
let target = ctx.catch_bb;
crate::mir::ssot::cf_common::set_jump(f, from_bb, target);
ctx.incoming.push((from_bb, exc_val));
Some(target)
} else {
None
}
})
}