diff --git a/crates/oxide_cli/src/main.rs b/crates/oxide_cli/src/main.rs index 64b90aa9..83e10efc 100644 --- a/crates/oxide_cli/src/main.rs +++ b/crates/oxide_cli/src/main.rs @@ -51,6 +51,9 @@ enum Commands { file: Option, #[arg(long)] no_dce: bool, + /// D-18:关闭 liveness/精确 DCE/RegAlloc 链,vreg 原样当物理号。 + #[arg(long)] + no_regalloc: bool, }, Bench { #[arg(default_value = "js")] @@ -96,7 +99,7 @@ fn main() -> ExitCode { } ExitCode::SUCCESS } - Some(Commands::Compile { expr, file, no_dce }) => compile(expr, file, no_dce), + Some(Commands::Compile { expr, file, no_dce, no_regalloc }) => compile(expr, file, no_dce, no_regalloc), Some(Commands::Bench { mode, filter, @@ -261,7 +264,7 @@ fn run(file: &str, kernel: &Arc, pool: &Arc) -> ExitCode { } } -fn compile(expr: Option, file: Option, no_dce: bool) -> ExitCode { +fn compile(expr: Option, file: Option, no_dce: bool, no_regalloc: bool) -> ExitCode { let source = if let Some(code) = expr { code } else if let Some(path) = file { @@ -291,7 +294,13 @@ fn compile(expr: Option, file: Option, no_dce: bool) -> ExitCode } }; - let compiler = if no_dce { Compiler::new().with_dce(false) } else { Compiler::new() }; + let mut compiler = Compiler::new(); + if no_dce { + compiler = compiler.with_dce(false); + } + if no_regalloc { + compiler = compiler.with_regalloc(false); + } match compiler.compile(&program) { Ok(module) => { print!("{module}"); diff --git a/crates/oxide_compiler/Cargo.toml b/crates/oxide_compiler/Cargo.toml index edb9dbaf..db8965ee 100644 --- a/crates/oxide_compiler/Cargo.toml +++ b/crates/oxide_compiler/Cargo.toml @@ -11,6 +11,9 @@ oxide_bytecode = { path = "../oxide_bytecode" } oxide_parser = { path = "../oxide_parser" } oxide_log = { path = "../oxide_log" } oxide_dce = { path = "../oxide_dce" } +oxide_cfg = { path = "../oxide_cfg" } +oxide_liveness = { path = "../oxide_liveness" } +oxide_regalloc = { path = "../oxide_regalloc" } oxide_ir.workspace = true oxide_emit.workspace = true serde.workspace = true diff --git a/crates/oxide_compiler/src/compiler.rs b/crates/oxide_compiler/src/compiler.rs index 4b8b7b2e..37d21a43 100644 --- a/crates/oxide_compiler/src/compiler.rs +++ b/crates/oxide_compiler/src/compiler.rs @@ -7,30 +7,50 @@ use oxide_ir::lower::lower; /// 结构哈希 / 编译模块哈希 re-export(供编译缓存作键)。 pub use crate::hash::{compiled_module_hash, structural_hash}; -/// 编译入口:状态 = DCE 开关,编译其余状态在 emit/lower 内部。 +/// 编译入口:状态 = DCE 开关 + RegAlloc 开关,编译其余状态在 emit/lower 内部。 pub struct Compiler { /// 是否在 emit 与 lower 之间运行死代码消除(D-10:默认开启)。 enable_dce: bool, + /// 是否在保守 DCE 后运行 liveness→精确 DCE→RegAlloc 链(D-18:默认开启)。 + /// off 时 vreg 原样当物理号走 lower 降级路径(>253 → RangeError)。 + enable_regalloc: bool, } impl Compiler { - /// 构造 `Compiler`:默认开启 DCE(D-10)。 + /// 构造 `Compiler`:默认开启 DCE 与 RegAlloc(D-10/D-18)。 pub fn new() -> Self { - Self { enable_dce: true } + Self { enable_dce: true, enable_regalloc: true } } /// 显式设置 DCE 开关(`with_dce(false)` 关闭死代码消除)。 pub fn with_dce(self, enable: bool) -> Self { - Self { enable_dce: enable } + Self { enable_dce: enable, ..self } } - /// 编译整个 program:AST → IR(`Emitter::emit_program`)→(可选 DCE)→ bytecode(`lower`)。 + /// 显式设置 RegAlloc 开关(`with_regalloc(false)` 关闭 liveness/精确 DCE/RegAlloc 链)。 + pub fn with_regalloc(self, enable: bool) -> Self { + Self { enable_regalloc: enable, ..self } + } + + /// 编译整个 program:AST → IR(`Emitter::emit_program`)→(可选 DCE)→(可选 + /// liveness/精确 DCE/RegAlloc)→ bytecode(`lower`)。 pub fn compile(&self, program: &oxide_parser::Program) -> Result { crate::compiler_debug!("compile: starting..."); let mut ir = Emitter::new().emit_program(program)?; if self.enable_dce { oxide_dce::dce(&mut ir); // 顶层函数;nested 不递归(D-03) } + if self.enable_regalloc && !ir.const_overflow { + // D-17 管线:liveness → 精确 DCE → 重算 liveness → RegAlloc + // const_overflow 时跳过:程序已知无效(lower 报 "too many constants"), + // 且超大常量池伴生的海量 vreg 会使 liveness 稠密 bitset 爆内存。 + let cfg = oxide_cfg::build_cfg(&ir); + let live = oxide_liveness::liveness(&ir, &cfg); + oxide_dce::dce_precise(&mut ir, &live); // 精确二轮消费第一轮 LiveInfo + let cfg2 = oxide_cfg::build_cfg(&ir); // 精确 DCE 删指令后 inst 下标位移,CFG 重建 + let live2 = oxide_liveness::liveness(&ir, &cfg2); // 重算,绝不用过期 live + oxide_regalloc::alloc(&mut ir, &live2)?; // D-02:无可行染色 → RangeError 上抛 + } crate::compiler_debug!("compile: done, {} instructions", ir.insts.len()); lower(&ir) } diff --git a/crates/oxide_compiler/tests/compile_expr_tests.rs b/crates/oxide_compiler/tests/compile_expr_tests.rs index 5bbcab66..0c8f6191 100644 --- a/crates/oxide_compiler/tests/compile_expr_tests.rs +++ b/crates/oxide_compiler/tests/compile_expr_tests.rs @@ -502,7 +502,8 @@ fn compile_builtin_globals_are_registered_lazily() { #[test] fn compile_comparison_complement_ops() { - let module = compile_source("let a = 1, b = 2; a > b; a <= b; a >= b; a != b;"); + // 结果被使用(赋值)→ 比较指令不被 DCE 删除(精确 DCE 会删结果丢弃的死比较) + let module = compile_source("let a = 1, b = 2; var r = a > b; var s = a <= b; var t = a >= b; var u = a != b;"); assert!(module.bytecode.iter().any(|&i| opcode::opcode(i) == OpCode::GT), "a > b should emit GT"); assert!(module.bytecode.iter().any(|&i| opcode::opcode(i) == OpCode::LTE), "a <= b should emit LTE"); assert!(module.bytecode.iter().any(|&i| opcode::opcode(i) == OpCode::GTE), "a >= b should emit GTE"); diff --git a/crates/oxide_ir/src/lower.rs b/crates/oxide_ir/src/lower.rs index 6341cccf..da2618dc 100644 --- a/crates/oxide_ir/src/lower.rs +++ b/crates/oxide_ir/src/lower.rs @@ -13,7 +13,13 @@ use crate::IRFunction; /// IRFunction → CompiledModule。错误消息与现状(退役前 compiler.rs)逐字一致。 pub fn lower(f: &IRFunction) -> Result { - // 溢出检查 1:寄存器(仅 Operand::Reg 变体;This/NewTarget 是语义操作数,映射 254/255 合法) + // 溢出检查 1:常量池(先于寄存器——超大常量池伴生的海量 vreg 会使后续检查的 + // 稠密 bitset 表示爆内存;常量池超限是更基础的失效,先报它) + if f.const_overflow || f.constants.len() > u16::MAX as usize { + return Err("RangeError: too many constants".into()); + } + + // 溢出检查 2:寄存器(仅 Operand::Reg 变体;This/NewTarget 是语义操作数,映射 254/255 合法) for inst in &f.insts { for o in [&inst.rd, &inst.a, &inst.b] { if let Operand::Reg(r) = o { @@ -24,18 +30,13 @@ pub fn lower(f: &IRFunction) -> Result { } } - // 溢出检查 1b:n_registers(u32 → CompiledModule u8 前的显式截断检查)。 + // 溢出检查 2b: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()); - } - let mut instrs: Vec = Vec::new(); let mut inst_to_instr: Vec = Vec::with_capacity(f.insts.len()); // (当前指令的起始 Instr index, label id) diff --git a/crates/oxide_regalloc/src/finish.rs b/crates/oxide_regalloc/src/finish.rs index 47e1dc75..5845d89c 100644 --- a/crates/oxide_regalloc/src/finish.rs +++ b/crates/oxide_regalloc/src/finish.rs @@ -32,7 +32,9 @@ pub(super) fn run(f: &mut IRFunction, map: &AllocMap) { } } None => { - debug_assert!(false, "builtin vreg {v} 不在 AllocMap"); + // 未使用的 builtin(引用被 DCE 删除,vreg 不在 AllocMap)。 + // builtin 槽是预注册低号,保持原值无害(VM 入口写入,无读取者)。 + // 保持原项(*vreg 不变)。 } } }