refactor(emit): 寄存器号 u8→u32 全链放宽,vreg 化去 checkpoint/reg_overflow(B005)

This commit is contained in:
爱特每羊队
2026-08-06 16:31:16 +08:00
parent c52bf6313e
commit 38d6c7a60e
54 changed files with 404 additions and 431 deletions
+6 -6
View File
@@ -168,30 +168,30 @@ impl Inst {
}
/// 条件寄存器为 false 时跳转。
pub fn jmp_if_false(cond_reg: u8, label: LabelId) -> Self {
pub fn jmp_if_false(cond_reg: u32, label: LabelId) -> Self {
Self::new(
OpCode::JMP_IF_FALSE,
Operand::Reg(cond_reg as u32),
Operand::Reg(cond_reg),
Operand::None,
Operand::Label(label),
)
}
/// 条件寄存器为 true 时跳转。
pub fn jmp_if_true(cond_reg: u8, label: LabelId) -> Self {
pub fn jmp_if_true(cond_reg: u32, label: LabelId) -> Self {
Self::new(
OpCode::JMP_IF_TRUE,
Operand::Reg(cond_reg as u32),
Operand::Reg(cond_reg),
Operand::None,
Operand::Label(label),
)
}
/// 条件寄存器为 null/undefined 时跳转(`??` / 可选链短路)。
pub fn jmp_if_nullish(cond_reg: u8, label: LabelId) -> Self {
pub fn jmp_if_nullish(cond_reg: u32, label: LabelId) -> Self {
Self::new(
OpCode::JMP_IF_NULLISH,
Operand::Reg(cond_reg as u32),
Operand::Reg(cond_reg),
Operand::None,
Operand::Label(label),
)
+2 -5
View File
@@ -40,20 +40,18 @@ pub struct IRFunction {
// bindings 域
pub param_layout: ParamLayout,
// builtins 域
pub builtin_reg_map: Vec<(String, u8)>,
pub builtin_reg_map: Vec<(String, u32)>,
// closures 域
pub upvalue_captures: Vec<UpvalueCapture>,
pub cells_needed: u8,
// meta 域
pub n_registers: u8,
pub n_registers: u32,
pub is_arrow: bool,
pub is_class_constructor: bool,
pub is_derived_constructor: bool,
pub needs_home_object: bool,
pub captured_this_const_idx: u16,
pub function_name: Option<String>,
/// emit 侧溢出证据,lowering 读标志报错(D-20)。
pub reg_overflow: bool,
pub const_overflow: bool,
// nested 域
pub nested: Vec<IRFunction>,
@@ -78,7 +76,6 @@ impl IRFunction {
needs_home_object: false,
captured_this_const_idx: 0,
function_name: None,
reg_overflow: false,
const_overflow: false,
nested: Vec::new(),
}
+8 -4
View File
@@ -14,9 +14,6 @@ use crate::IRFunction;
/// IRFunction → CompiledModule。错误消息与现状(退役前 compiler.rs)逐字一致。
pub fn lower(f: &IRFunction) -> Result<CompiledModule, String> {
// 溢出检查 1:寄存器(仅 Operand::Reg 变体;This/NewTarget 是语义操作数,映射 254/255 合法)
if f.reg_overflow {
return Err("RangeError: function body uses too many registers (max 253)".into());
}
for inst in &f.insts {
for o in [&inst.rd, &inst.a, &inst.b] {
if let Operand::Reg(r) = o {
@@ -27,6 +24,13 @@ pub fn lower(f: &IRFunction) -> Result<CompiledModule, String> {
}
}
// 溢出检查 1b:n_registers(u32 → CompiledModule u8 前的显式截断检查)。
// n_registers = max_regs = 最高分配号 + 1,合法上限 254(reg 0..253)。
// 纯参数/空体函数可能无指令引用最高号寄存器,逐指令检查漏掉,计数检查兜底防 u8 静默截断。
if f.n_registers > 254 {
return Err("RangeError: function body uses too many registers (max 253)".into());
}
// 溢出检查 2:常量池
if f.const_overflow || f.constants.len() > u16::MAX as usize {
return Err("RangeError: too many constants".into());
@@ -72,7 +76,7 @@ pub fn lower(f: &IRFunction) -> Result<CompiledModule, String> {
Ok(CompiledModule {
bytecode: instrs,
constants: f.constants.clone(),
n_registers: f.n_registers,
n_registers: f.n_registers as u8,
n_args: f.param_layout.count as u8,
param_base: f.param_layout.base as u8,
builtin_reg_map: f.builtin_reg_map.clone(),