2025-09-28 20:38:09 +09:00
|
|
|
use crate::mir::builder::MirBuilder;
|
2025-11-17 07:58:44 +09:00
|
|
|
use crate::mir::ValueId;
|
2025-09-28 20:38:09 +09:00
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
|
|
|
|
|
pub enum LocalKind {
|
|
|
|
|
Recv,
|
|
|
|
|
Arg,
|
|
|
|
|
CompareOperand,
|
|
|
|
|
Cond,
|
|
|
|
|
FieldBase,
|
|
|
|
|
Other(u8),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl LocalKind {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn tag(self) -> u8 {
|
|
|
|
|
match self {
|
|
|
|
|
LocalKind::Recv => 0,
|
|
|
|
|
LocalKind::Arg => 1,
|
|
|
|
|
LocalKind::CompareOperand => 2,
|
|
|
|
|
LocalKind::Cond => 4,
|
|
|
|
|
LocalKind::FieldBase => 0, // share recv slot for bases
|
|
|
|
|
LocalKind::Other(k) => k,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Ensure a value has an in-block definition and cache it per (bb, orig, kind).
|
|
|
|
|
/// Always emits a Copy in the current block when not cached.
|
|
|
|
|
pub fn ensure(builder: &mut MirBuilder, v: ValueId, kind: LocalKind) -> ValueId {
|
|
|
|
|
let bb_opt = builder.current_block;
|
|
|
|
|
if let Some(bb) = bb_opt {
|
|
|
|
|
if std::env::var("NYASH_LOCAL_SSA_TRACE").ok().as_deref() == Some("1") {
|
|
|
|
|
eprintln!("[local-ssa] ensure bb={:?} kind={:?} v=%{}", bb, kind, v.0);
|
|
|
|
|
}
|
|
|
|
|
let key = (bb, v, kind.tag());
|
|
|
|
|
if let Some(&loc) = builder.local_ssa_map.get(&key) {
|
|
|
|
|
return loc;
|
|
|
|
|
}
|
2025-11-17 03:19:03 +09:00
|
|
|
|
2025-11-17 07:58:44 +09:00
|
|
|
// Ensure the current basic block exists in the function before emitting a Copy.
|
|
|
|
|
// Stage-B 経路などでは current_block が割り当て済みでも、ブロック自体が
|
|
|
|
|
// function にまだ追加されていない場合があり、そのまま emit_instruction すると
|
|
|
|
|
// Copy が黙って落ちてしまう。ここで best-effort で作成しておく。
|
2025-11-17 08:41:50 +09:00
|
|
|
// CRITICAL: Check for errors - if block creation fails, return original value.
|
|
|
|
|
if let Err(e) = builder.ensure_block_exists(bb) {
|
|
|
|
|
if std::env::var("NYASH_LOCAL_SSA_TRACE").ok().as_deref() == Some("1") {
|
2025-11-21 06:25:17 +09:00
|
|
|
eprintln!(
|
|
|
|
|
"[local-ssa] ensure_block_exists FAILED bb={:?} kind={:?} v=%{} err={}",
|
|
|
|
|
bb, kind, v.0, e
|
|
|
|
|
);
|
2025-11-17 08:41:50 +09:00
|
|
|
}
|
|
|
|
|
return v;
|
|
|
|
|
}
|
2025-11-17 07:58:44 +09:00
|
|
|
|
2025-11-17 03:19:03 +09:00
|
|
|
// CRITICAL FIX: If `v` is from a pinned slot, check if there's a PHI value for that slot
|
|
|
|
|
// in the current block's variable_map. If so, use the PHI value directly instead of
|
|
|
|
|
// emitting a Copy from the old value (which might not be defined in this block).
|
2025-11-19 02:44:40 +09:00
|
|
|
// Try to detect pinned slots for this value and redirect to the latest slot value.
|
|
|
|
|
// 1) First, look for "__pin$" entries in variable_map that still point to v.
|
|
|
|
|
// 2) If not found, consult builder.pin_slot_names to recover the slot name
|
|
|
|
|
// and then look up the current ValueId for that slot.
|
|
|
|
|
let mut slot_name_opt: Option<String> = None;
|
|
|
|
|
|
2025-11-21 06:25:17 +09:00
|
|
|
let names_for_v: Vec<String> = builder
|
|
|
|
|
.variable_map
|
|
|
|
|
.iter()
|
2025-11-17 03:19:03 +09:00
|
|
|
.filter(|(k, &vid)| vid == v && k.starts_with("__pin$"))
|
|
|
|
|
.map(|(k, _)| k.clone())
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
if let Some(first_pin_name) = names_for_v.first() {
|
2025-11-19 02:44:40 +09:00
|
|
|
slot_name_opt = Some(first_pin_name.clone());
|
|
|
|
|
} else if let Some(name) = builder.pin_slot_names.get(&v) {
|
|
|
|
|
slot_name_opt = Some(name.clone());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(slot_name) = slot_name_opt {
|
|
|
|
|
if let Some(¤t_val) = builder.variable_map.get(&slot_name) {
|
2025-11-17 03:19:03 +09:00
|
|
|
if current_val != v {
|
2025-11-19 02:44:40 +09:00
|
|
|
// The slot has been updated (likely by a PHI or header rewrite).
|
|
|
|
|
// Use the updated value instead of the stale pinned ValueId.
|
2025-11-17 03:19:03 +09:00
|
|
|
if std::env::var("NYASH_LOCAL_SSA_TRACE").ok().as_deref() == Some("1") {
|
2025-11-21 06:25:17 +09:00
|
|
|
eprintln!(
|
|
|
|
|
"[local-ssa] phi-redirect bb={:?} kind={:?} slot={} %{} -> %{}",
|
|
|
|
|
bb, kind, slot_name, v.0, current_val.0
|
|
|
|
|
);
|
2025-11-17 03:19:03 +09:00
|
|
|
}
|
|
|
|
|
builder.local_ssa_map.insert(key, current_val);
|
|
|
|
|
return current_val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 00:48:18 +09:00
|
|
|
let loc = builder.next_value_id();
|
2025-11-17 08:41:50 +09:00
|
|
|
// CRITICAL: Check emit_instruction result - if Copy fails, return original value
|
|
|
|
|
// to avoid returning undefined ValueId.
|
2025-11-21 06:25:17 +09:00
|
|
|
if let Err(e) =
|
|
|
|
|
builder.emit_instruction(crate::mir::MirInstruction::Copy { dst: loc, src: v })
|
|
|
|
|
{
|
2025-11-17 08:41:50 +09:00
|
|
|
if std::env::var("NYASH_LOCAL_SSA_TRACE").ok().as_deref() == Some("1") {
|
|
|
|
|
eprintln!("[local-ssa] emit_instruction Copy FAILED bb={:?} kind={:?} v=%{} dst=%{} err={}",
|
|
|
|
|
bb, kind, v.0, loc.0, e);
|
|
|
|
|
}
|
|
|
|
|
// Failed to emit Copy - return original value instead of undefined dst
|
|
|
|
|
return v;
|
|
|
|
|
}
|
2025-09-28 20:38:09 +09:00
|
|
|
if std::env::var("NYASH_LOCAL_SSA_TRACE").ok().as_deref() == Some("1") {
|
2025-11-21 06:25:17 +09:00
|
|
|
eprintln!(
|
|
|
|
|
"[local-ssa] copy bb={:?} kind={:?} %{} -> %{}",
|
|
|
|
|
bb, kind, v.0, loc.0
|
|
|
|
|
);
|
2025-09-28 20:38:09 +09:00
|
|
|
}
|
2025-11-17 08:41:50 +09:00
|
|
|
// Success: register metadata and cache
|
2025-09-28 20:38:09 +09:00
|
|
|
if let Some(t) = builder.value_types.get(&v).cloned() {
|
|
|
|
|
builder.value_types.insert(loc, t);
|
|
|
|
|
}
|
|
|
|
|
if let Some(cls) = builder.value_origin_newbox.get(&v).cloned() {
|
|
|
|
|
builder.value_origin_newbox.insert(loc, cls);
|
|
|
|
|
}
|
|
|
|
|
builder.local_ssa_map.insert(key, loc);
|
|
|
|
|
loc
|
|
|
|
|
} else {
|
|
|
|
|
v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2025-11-21 06:25:17 +09:00
|
|
|
pub fn recv(builder: &mut MirBuilder, v: ValueId) -> ValueId {
|
|
|
|
|
ensure(builder, v, LocalKind::Recv)
|
|
|
|
|
}
|
2025-09-28 20:38:09 +09:00
|
|
|
|
|
|
|
|
#[inline]
|
2025-11-21 06:25:17 +09:00
|
|
|
pub fn arg(builder: &mut MirBuilder, v: ValueId) -> ValueId {
|
|
|
|
|
ensure(builder, v, LocalKind::Arg)
|
|
|
|
|
}
|
2025-09-28 20:38:09 +09:00
|
|
|
|
|
|
|
|
#[inline]
|
2025-11-21 06:25:17 +09:00
|
|
|
pub fn cond(builder: &mut MirBuilder, v: ValueId) -> ValueId {
|
|
|
|
|
ensure(builder, v, LocalKind::Cond)
|
|
|
|
|
}
|
2025-09-28 20:38:09 +09:00
|
|
|
|
|
|
|
|
#[inline]
|
2025-11-21 06:25:17 +09:00
|
|
|
pub fn field_base(builder: &mut MirBuilder, v: ValueId) -> ValueId {
|
|
|
|
|
ensure(builder, v, LocalKind::FieldBase)
|
|
|
|
|
}
|
2025-09-28 20:38:09 +09:00
|
|
|
|
|
|
|
|
#[inline]
|
2025-11-21 06:25:17 +09:00
|
|
|
pub fn cmp_operand(builder: &mut MirBuilder, v: ValueId) -> ValueId {
|
|
|
|
|
ensure(builder, v, LocalKind::CompareOperand)
|
|
|
|
|
}
|
2025-09-28 20:38:09 +09:00
|
|
|
|
|
|
|
|
/// Finalize only the args (legacy Call paths)
|
|
|
|
|
pub fn finalize_args(builder: &mut MirBuilder, args: &mut Vec<ValueId>) {
|
|
|
|
|
for a in args.iter_mut() {
|
|
|
|
|
*a = arg(builder, *a);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Finalize a single branch condition just before emitting a Branch.
|
|
|
|
|
/// Ensures the condition has a definition in the current block.
|
|
|
|
|
pub fn finalize_branch_cond(builder: &mut MirBuilder, condition_v: &mut ValueId) {
|
|
|
|
|
*condition_v = cond(builder, *condition_v);
|
|
|
|
|
if std::env::var("NYASH_LOCAL_SSA_TRACE").ok().as_deref() == Some("1") {
|
2025-11-21 06:25:17 +09:00
|
|
|
if let Some(bb) = builder.current_block {
|
|
|
|
|
eprintln!(
|
|
|
|
|
"[local-ssa] finalize-branch bb={:?} cond=%{}",
|
|
|
|
|
bb, condition_v.0
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-09-28 20:38:09 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Finalize compare operands just before emitting a Compare.
|
|
|
|
|
/// Applies in-block materialization to both lhs and rhs.
|
|
|
|
|
pub fn finalize_compare(builder: &mut MirBuilder, lhs: &mut ValueId, rhs: &mut ValueId) {
|
|
|
|
|
*lhs = cmp_operand(builder, *lhs);
|
|
|
|
|
*rhs = cmp_operand(builder, *rhs);
|
|
|
|
|
if std::env::var("NYASH_LOCAL_SSA_TRACE").ok().as_deref() == Some("1") {
|
2025-11-21 06:25:17 +09:00
|
|
|
if let Some(bb) = builder.current_block {
|
|
|
|
|
eprintln!(
|
|
|
|
|
"[local-ssa] finalize-compare bb={:?} lhs=%{} rhs=%{}",
|
|
|
|
|
bb, lhs.0, rhs.0
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-09-28 20:38:09 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Finalize field use sites: ensure base and all args are in the current block.
|
2025-11-21 06:25:17 +09:00
|
|
|
pub fn finalize_field_base_and_args(
|
|
|
|
|
builder: &mut MirBuilder,
|
|
|
|
|
base: &mut ValueId,
|
|
|
|
|
args: &mut Vec<ValueId>,
|
|
|
|
|
) {
|
2025-09-28 20:38:09 +09:00
|
|
|
*base = field_base(builder, *base);
|
2025-11-21 06:25:17 +09:00
|
|
|
for a in args.iter_mut() {
|
|
|
|
|
*a = arg(builder, *a);
|
|
|
|
|
}
|
2025-09-28 20:38:09 +09:00
|
|
|
if std::env::var("NYASH_LOCAL_SSA_TRACE").ok().as_deref() == Some("1") {
|
2025-11-21 06:25:17 +09:00
|
|
|
if let Some(bb) = builder.current_block {
|
|
|
|
|
eprintln!(
|
|
|
|
|
"[local-ssa] finalize-field bb={:?} base=%{} argc={}",
|
|
|
|
|
bb,
|
|
|
|
|
base.0,
|
|
|
|
|
args.len()
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-09-28 20:38:09 +09:00
|
|
|
}
|
|
|
|
|
}
|