docs: 补充注释与格式化
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
//! Pass C:mark-sweep 一次性重建 insts + label_pos 重映射(D-13,不用就地逐删平移——
|
||||
//! Pass C:mark-sweep 一次性重建 insts + label_pos 重映射(不用就地逐删平移——
|
||||
//! 多删点叠加偏移易错)。sweep:keep 序 compact 重建;label_pos 逐 id 重写,
|
||||
//! 目标被删的 label 置 None 保留 id 槽位(lowering 只读 label_pos 不读 label_count)。
|
||||
|
||||
@@ -17,13 +17,13 @@ pub(super) fn pass_c_sweep(f: &mut IRFunction, keep: &[bool]) {
|
||||
}
|
||||
}
|
||||
f.insts = new_insts;
|
||||
// label_pos 逐 id 重写:get + usize::MAX 过滤做越界守卫(T-04-03,不信任 label_pos 长度)
|
||||
// label_pos 逐 id 重写:get + usize::MAX 过滤做越界守卫(不信任 label_pos 长度)
|
||||
for pos in f.label_pos.iter_mut() {
|
||||
if let Some(old) = *pos {
|
||||
*pos = old_to_new.get(old).filter(|n| **n != usize::MAX).copied();
|
||||
}
|
||||
}
|
||||
// 防御性校验(T-04-03):存活 label 新下标不越界(空尾块映射 new insts.len() 合法透传)
|
||||
// 防御性校验:存活 label 新下标不越界(空尾块映射 new insts.len() 合法透传)
|
||||
let new_len = f.insts.len();
|
||||
debug_assert!(f.label_pos.iter().all(|p| p.map_or(true, |np| np <= new_len)));
|
||||
}
|
||||
@@ -42,7 +42,8 @@ mod tests {
|
||||
let mut f = IRFunction::new();
|
||||
f.insts.push(Inst::load_const(Operand::Reg(5), 1)); // 0: 存活
|
||||
f.insts.push(Inst::load_const(Operand::Reg(9), 2)); // 1: 死(删)
|
||||
f.insts.push(Inst::new(OpCode::RETURN, Operand::Reg(5), Operand::None, Operand::None)); // 2
|
||||
f.insts
|
||||
.push(Inst::new(OpCode::RETURN, Operand::Reg(5), Operand::None, Operand::None)); // 2
|
||||
f.label_pos = vec![Some(2)]; // label 0 → inst 2(RETURN)
|
||||
f.label_count = 1;
|
||||
|
||||
@@ -53,14 +54,15 @@ mod tests {
|
||||
assert_eq!(f.label_pos, vec![Some(1)], "存活 label 2 → 新下标 1");
|
||||
}
|
||||
|
||||
/// 不动域断言(D-14/D-04/D-03/D-13):constants / n_registers / nested / label_count
|
||||
/// 一律不动;死 label 置 None、存活 label 重映射、原 None 保持。
|
||||
/// 不动域断言:constants / n_registers / nested / label_count 一律不动;
|
||||
/// 死 label 置 None、存活 label 重映射、原 None 保持。
|
||||
#[test]
|
||||
fn untouched_domains_asserted() {
|
||||
let mut f = IRFunction::new();
|
||||
f.insts.push(Inst::load_const(Operand::Reg(1), 0)); // 0: label 0 目标(死值,保守保留)
|
||||
f.insts.push(Inst::load_const(Operand::Reg(2), 1)); // 1: 非 label 目标的死指令(删)
|
||||
f.insts.push(Inst::new(OpCode::RETURN, Operand::Reg(5), Operand::None, Operand::None)); // 2
|
||||
f.insts
|
||||
.push(Inst::new(OpCode::RETURN, Operand::Reg(5), Operand::None, Operand::None)); // 2
|
||||
f.label_pos = vec![Some(0), Some(2), None];
|
||||
f.label_count = 3;
|
||||
f.n_registers = 10;
|
||||
@@ -72,9 +74,9 @@ mod tests {
|
||||
assert_eq!(f.insts[0].op, OpCode::LOAD_CONST);
|
||||
assert_eq!(f.insts[1].op, OpCode::RETURN);
|
||||
assert_eq!(f.label_pos, vec![Some(0), Some(1), None], "存活 label→新下标、原 None 保持");
|
||||
assert_eq!(f.label_count, 3, "label_count 不动(D-13)");
|
||||
assert_eq!(f.n_registers, 10, "n_registers 不收缩(D-04)");
|
||||
assert_eq!(f.constants, vec![Constant::Number(1.0)], "常量池不清理(D-14)");
|
||||
assert_eq!(f.nested.len(), 1, "nested 不递归不回收(D-03)");
|
||||
assert_eq!(f.label_count, 3, "label_count 保持原值,不参与重建");
|
||||
assert_eq!(f.n_registers, 10, "n_registers 不收缩");
|
||||
assert_eq!(f.constants, vec![Constant::Number(1.0)], "常量池不清理");
|
||||
assert_eq!(f.nested.len(), 1, "nested 不递归不回收");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user