refactor: extract helpers (builder_calls: math/env/me; json_v0_bridge: new_block/merge_values) — non-functional cleanup; cargo check + smokes green

This commit is contained in:
Selfhosting Dev
2025-09-17 08:20:44 +09:00
parent a3282abdd9
commit 31f90012e0
2 changed files with 141 additions and 250 deletions

View File

@ -4,6 +4,7 @@ use crate::mir::{
MirInstruction, MirModule, MirPrinter, MirType, ValueId,
};
use std::collections::HashMap;
use std::collections::HashSet;
#[derive(Clone, Copy)]
pub(super) struct LoopContext {
@ -110,6 +111,34 @@ fn new_block(f: &mut MirFunction) -> BasicBlockId {
id
}
/// Merge two incoming values either by inserting Copy on predecessor edges
/// (no_phi mode) or by adding a Phi at the merge block head.
fn merge_values(
f: &mut MirFunction,
no_phi: bool,
merge_bb: BasicBlockId,
pred_a: BasicBlockId,
val_a: ValueId,
pred_b: BasicBlockId,
val_b: ValueId,
) -> ValueId {
if val_a == val_b {
return val_a;
}
let dst = f.next_value_id();
if no_phi {
if let Some(bb) = f.get_block_mut(pred_a) {
bb.add_instruction(MirInstruction::Copy { dst, src: val_a });
}
if let Some(bb) = f.get_block_mut(pred_b) {
bb.add_instruction(MirInstruction::Copy { dst, src: val_b });
}
} else if let Some(bb) = f.get_block_mut(merge_bb) {
bb.insert_instruction_after_phis(MirInstruction::Phi { dst, inputs: vec![(pred_a, val_a), (pred_b, val_b)] });
}
dst
}
fn lower_throw(
env: &BridgeEnv,
f: &mut MirFunction,
@ -802,7 +831,6 @@ fn lower_stmt_with_vars(
}
(else_bb, base_vars.clone())
};
use std::collections::HashSet;
let no_phi = env.mir_no_phi;
let mut names: HashSet<String> = base_vars.keys().cloned().collect();
for k in then_vars.keys() {
@ -817,72 +845,18 @@ fn lower_stmt_with_vars(
let exists_base = base_vars.contains_key(&name);
match (tv, ev, exists_base) {
(Some(tval), Some(eval), _) => {
let merged = if tval == eval {
tval
} else {
let dst = f.next_value_id();
if no_phi {
if let Some(bb) = f.get_block_mut(tend) {
bb.add_instruction(MirInstruction::Copy { dst, src: tval });
}
if let Some(bb) = f.get_block_mut(else_end_pred) {
bb.add_instruction(MirInstruction::Copy { dst, src: eval });
}
} else if let Some(bb) = f.get_block_mut(merge_bb) {
bb.insert_instruction_after_phis(MirInstruction::Phi {
dst,
inputs: vec![(tend, tval), (else_end_pred, eval)],
});
}
dst
};
let merged = merge_values(f, no_phi, merge_bb, tend, tval, else_end_pred, eval);
vars.insert(name, merged);
}
(Some(tval), None, true) => {
if let Some(&bval) = base_vars.get(&name) {
let merged = if tval == bval {
tval
} else {
let dst = f.next_value_id();
if no_phi {
if let Some(bb) = f.get_block_mut(tend) {
bb.add_instruction(MirInstruction::Copy { dst, src: tval });
}
if let Some(bb) = f.get_block_mut(else_end_pred) {
bb.add_instruction(MirInstruction::Copy { dst, src: bval });
}
} else if let Some(bb) = f.get_block_mut(merge_bb) {
bb.insert_instruction_after_phis(MirInstruction::Phi {
dst,
inputs: vec![(tend, tval), (else_end_pred, bval)],
});
}
dst
};
let merged = merge_values(f, no_phi, merge_bb, tend, tval, else_end_pred, bval);
vars.insert(name, merged);
}
}
(None, Some(eval), true) => {
if let Some(&bval) = base_vars.get(&name) {
let merged = if eval == bval {
eval
} else {
let dst = f.next_value_id();
if no_phi {
if let Some(bb) = f.get_block_mut(tend) {
bb.add_instruction(MirInstruction::Copy { dst, src: bval });
}
if let Some(bb) = f.get_block_mut(else_end_pred) {
bb.add_instruction(MirInstruction::Copy { dst, src: eval });
}
} else if let Some(bb) = f.get_block_mut(merge_bb) {
bb.insert_instruction_after_phis(MirInstruction::Phi {
dst,
inputs: vec![(tend, bval), (else_end_pred, eval)],
});
}
dst
};
let merged = merge_values(f, no_phi, merge_bb, tend, bval, else_end_pred, eval);
vars.insert(name, merged);
}
}