From d1bfae2723f276813f1439acfcba7b0995320dbf Mon Sep 17 00:00:00 2001 From: pjh456 <147148383@qq.com> Date: Thu, 6 Aug 2026 11:45:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Compiler=20=E5=A2=9E=E5=8A=A0=20enable?= =?UTF-8?q?=5Fdce=20=E5=AD=97=E6=AE=B5=E4=B8=8E=20with=5Fdce=20=E5=BC=80?= =?UTF-8?q?=E5=85=B3=EF=BC=88D-10=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - unit struct 改带字段 struct,new() 默认开启 DCE - compile 在 emit 与 lower 之间插入 oxide_dce::dce(nested 不递归 D-03) - Cargo.toml 追加 oxide_dce path 依赖;dev 环经 resolver v2 解析通过 --- crates/oxide_compiler/Cargo.toml | 1 + crates/oxide_compiler/src/compiler.rs | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/oxide_compiler/Cargo.toml b/crates/oxide_compiler/Cargo.toml index 28488734..edb9dbaf 100644 --- a/crates/oxide_compiler/Cargo.toml +++ b/crates/oxide_compiler/Cargo.toml @@ -10,6 +10,7 @@ description = "OxideJS - AST to bytecode compiler" oxide_bytecode = { path = "../oxide_bytecode" } oxide_parser = { path = "../oxide_parser" } oxide_log = { path = "../oxide_log" } +oxide_dce = { path = "../oxide_dce" } 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 0a9ac450..4b8b7b2e 100644 --- a/crates/oxide_compiler/src/compiler.rs +++ b/crates/oxide_compiler/src/compiler.rs @@ -7,19 +7,30 @@ use oxide_ir::lower::lower; /// 结构哈希 / 编译模块哈希 re-export(供编译缓存作键)。 pub use crate::hash::{compiled_module_hash, structural_hash}; -/// 编译入口(marker 类型):无状态,编译状态在 emit/lower 内部。 -pub struct Compiler; +/// 编译入口:状态 = DCE 开关,编译其余状态在 emit/lower 内部。 +pub struct Compiler { + /// 是否在 emit 与 lower 之间运行死代码消除(D-10:默认开启)。 + enable_dce: bool, +} impl Compiler { - /// 构造 `Compiler`(无状态,恒返回空实例)。 + /// 构造 `Compiler`:默认开启 DCE(D-10)。 pub fn new() -> Self { - Self + Self { enable_dce: true } } - /// 编译整个 program:AST → IR(`Emitter::emit_program`)→ bytecode(`lower`)。 + /// 显式设置 DCE 开关(`with_dce(false)` 关闭死代码消除)。 + pub fn with_dce(self, enable: bool) -> Self { + Self { enable_dce: enable } + } + + /// 编译整个 program:AST → IR(`Emitter::emit_program`)→(可选 DCE)→ bytecode(`lower`)。 pub fn compile(&self, program: &oxide_parser::Program) -> Result { crate::compiler_debug!("compile: starting..."); - let ir = Emitter::new().emit_program(program)?; + let mut ir = Emitter::new().emit_program(program)?; + if self.enable_dce { + oxide_dce::dce(&mut ir); // 顶层函数;nested 不递归(D-03) + } crate::compiler_debug!("compile: done, {} instructions", ir.insts.len()); lower(&ir) }