refactor: lower 编码实现按值移交,原 lower 降为克隆包装

This commit is contained in:
2026-10-12 14:01:37 +00:00
parent 7cf3020bde
commit b7b6d43d05
4 changed files with 40 additions and 32 deletions
Generated
+20 -20
View File
@@ -1160,7 +1160,7 @@ dependencies = [
[[package]]
name = "oxide_builtins"
version = "0.16.19"
version = "0.16.20"
dependencies = [
"chrono",
"fast-float",
@@ -1185,7 +1185,7 @@ dependencies = [
[[package]]
name = "oxide_bytecode"
version = "0.16.19"
version = "0.16.20"
dependencies = [
"num-bigint",
"oxide_log",
@@ -1193,7 +1193,7 @@ dependencies = [
[[package]]
name = "oxide_cfg"
version = "0.16.19"
version = "0.16.20"
dependencies = [
"oxide_bytecode",
"oxide_emit",
@@ -1204,7 +1204,7 @@ dependencies = [
[[package]]
name = "oxide_cli"
version = "0.16.19"
version = "0.16.20"
dependencies = [
"ansi_term",
"bumpalo",
@@ -1228,7 +1228,7 @@ dependencies = [
[[package]]
name = "oxide_code_cache"
version = "0.16.19"
version = "0.16.20"
dependencies = [
"lru",
"oxide_bytecode",
@@ -1237,7 +1237,7 @@ dependencies = [
[[package]]
name = "oxide_compiler"
version = "0.16.19"
version = "0.16.20"
dependencies = [
"oxide_bytecode",
"oxide_cfg",
@@ -1257,7 +1257,7 @@ dependencies = [
[[package]]
name = "oxide_dce"
version = "0.16.19"
version = "0.16.20"
dependencies = [
"oxide_bytecode",
"oxide_cfg",
@@ -1271,7 +1271,7 @@ dependencies = [
[[package]]
name = "oxide_emit"
version = "0.16.19"
version = "0.16.20"
dependencies = [
"num-bigint",
"oxc_allocator",
@@ -1287,7 +1287,7 @@ dependencies = [
[[package]]
name = "oxide_engine"
version = "0.16.19"
version = "0.16.20"
dependencies = [
"oxide_bytecode",
"oxide_compiler",
@@ -1299,7 +1299,7 @@ dependencies = [
[[package]]
name = "oxide_ir"
version = "0.16.19"
version = "0.16.20"
dependencies = [
"oxide_bytecode",
"oxide_log",
@@ -1308,7 +1308,7 @@ dependencies = [
[[package]]
name = "oxide_kernel"
version = "0.16.19"
version = "0.16.20"
dependencies = [
"bumpalo",
"dashmap",
@@ -1322,7 +1322,7 @@ dependencies = [
[[package]]
name = "oxide_liveness"
version = "0.16.19"
version = "0.16.20"
dependencies = [
"oxide_bytecode",
"oxide_cfg",
@@ -1334,7 +1334,7 @@ dependencies = [
[[package]]
name = "oxide_log"
version = "0.16.19"
version = "0.16.20"
dependencies = [
"tracing",
"tracing-appender",
@@ -1343,11 +1343,11 @@ dependencies = [
[[package]]
name = "oxide_net"
version = "0.16.19"
version = "0.16.20"
[[package]]
name = "oxide_parser"
version = "0.16.19"
version = "0.16.20"
dependencies = [
"oxc_allocator",
"oxc_ast",
@@ -1362,7 +1362,7 @@ dependencies = [
[[package]]
name = "oxide_regalloc"
version = "0.16.19"
version = "0.16.20"
dependencies = [
"oxide_bytecode",
"oxide_cfg",
@@ -1378,7 +1378,7 @@ dependencies = [
[[package]]
name = "oxide_runtime_api"
version = "0.16.19"
version = "0.16.20"
dependencies = [
"num-bigint",
"num-traits",
@@ -1391,7 +1391,7 @@ dependencies = [
[[package]]
name = "oxide_test262"
version = "0.16.19"
version = "0.16.20"
dependencies = [
"oxide_bytecode",
"oxide_compiler",
@@ -1411,7 +1411,7 @@ dependencies = [
[[package]]
name = "oxide_types"
version = "0.16.19"
version = "0.16.20"
dependencies = [
"bumpalo",
"dashmap",
@@ -1424,7 +1424,7 @@ dependencies = [
[[package]]
name = "oxide_vm"
version = "0.16.19"
version = "0.16.20"
dependencies = [
"bumpalo",
"chrono",
+1 -1
View File
@@ -24,7 +24,7 @@ members = [
]
[workspace.package]
version = "0.16.19"
version = "0.16.20"
edition = "2021"
license = "MIT"
rust-version = "1.80"
+2 -2
View File
@@ -2,7 +2,7 @@
use oxide_bytecode::module::CompiledModule;
use oxide_emit::Emitter;
use oxide_ir::lower::lower;
use oxide_ir::lower::lower_owned;
/// 结构哈希 / 编译模块哈希 re-export(供编译缓存作键)。
pub use crate::hash::{compiled_module_hash, structural_hash};
@@ -134,7 +134,7 @@ impl Compiler {
crate::compiler_debug!("compile: after regalloc {} insts", ir.insts.len());
}
crate::compiler_debug!("compile: done, {} instructions", ir.insts.len());
let mut module = lower(&ir)?;
let mut module = lower_owned(ir)?;
crate::flatten::flatten_submodules(&mut module);
Ok(module)
}
+17 -9
View File
@@ -13,8 +13,10 @@ use crate::inst::Inst;
use crate::operand::Operand;
use crate::IRFunction;
/// IRFunction → CompiledModule 等价转换。溢出/标签错误消息串是下游解析的契约键,改动须同步更新下游匹配。
pub fn lower(f: &IRFunction) -> Result<CompiledModule, String> {
/// IRFunction → CompiledModule 等价转换(所有权版,编码单源)。
/// 五个堆字段由克隆改为移动,nested 递归由借用改为移动;
/// 产物字节码与借用版逐字节完全一致。溢出/标签错误消息串是下游解析的契约键,改动须同步更新下游匹配。
pub fn lower_owned(f: IRFunction) -> Result<CompiledModule, String> {
crate::ir_debug!("lower: {} insts", f.insts.len());
// 常量池检查(先于寄存器——超大常量池伴生的海量 vreg 会使后续检查的
// 稠密 bitset 表示爆内存;常量池超限是更基础的失效,先报它)
@@ -73,22 +75,22 @@ pub fn lower(f: &IRFunction) -> Result<CompiledModule, String> {
}
let mut sub_modules = Vec::with_capacity(f.nested.len());
for nested in &f.nested {
sub_modules.push(Arc::new(lower(nested)?));
for nested in f.nested {
sub_modules.push(Arc::new(lower_owned(nested)?));
}
Ok(CompiledModule {
bytecode: Arc::from(instrs),
constants: f.constants.clone(),
constants: f.constants,
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(),
builtin_reg_map: f.builtin_reg_map,
sub_modules,
is_arrow: f.is_arrow,
is_strict: f.is_strict,
captured_this_const_idx: f.captured_this_const_idx,
function_name: f.function_name.clone(),
function_name: f.function_name,
function_name_reg: f.function_name_reg,
function_length: f.function_length,
has_simple_params: f.has_simple_params,
@@ -97,14 +99,20 @@ pub fn lower(f: &IRFunction) -> Result<CompiledModule, String> {
needs_home_object: f.needs_home_object,
is_generator: f.is_generator,
is_async: f.is_async,
upvalue_captures: f.upvalue_captures.clone(),
upvalue_captures: f.upvalue_captures,
cells_needed: f.cells_needed,
cell_names: f.cell_names.clone(),
cell_names: f.cell_names,
flat_id: 0,
is_es_module: false,
})
}
/// IRFunction → CompiledModule 等价转换(借用版,向后兼容既有调用点)。
/// 委托所有权版:整结构克隆后移交,编码逻辑单源。
pub fn lower(f: &IRFunction) -> Result<CompiledModule, String> {
lower_owned(f.clone())
}
/// Operand → u8 槽位。Const/Imm 在 a 槽走拆字(operand_pair_to_u8);
/// 出现在 b/rd 槽时按低字节直写(const_flag、cell_idx、uv_idx 等一字节立即数)。
fn operand_to_u8(o: &Operand) -> u8 {