refactor: 拆分 oxide_ir 为独立 crate

This commit is contained in:
2026-08-05 19:34:44 +08:00
parent 3ae2eeee42
commit 4f670eb7fd
46 changed files with 105 additions and 85 deletions
Generated
+8
View File
@@ -1111,11 +1111,19 @@ version = "0.1.0"
dependencies = [
"oxide_bytecode",
"oxide_code_cache",
"oxide_ir",
"oxide_log",
"oxide_parser",
"rustc-hash",
"serde",
"serde_json",
]
[[package]]
name = "oxide_ir"
version = "0.1.0"
dependencies = [
"oxide_bytecode",
"smallvec",
]
+2
View File
@@ -6,6 +6,7 @@ members = [
"crates/oxide_code_cache",
"crates/oxide_log",
"crates/oxide_compiler",
"crates/oxide_ir",
"crates/oxide_types",
"crates/oxide_vm",
"crates/oxide_kernel",
@@ -29,6 +30,7 @@ oxc_ast = "0.134.0"
oxc_span = "0.134.0"
oxc_diagnostics = "0.134.0"
oxc_syntax = "0.134.0"
oxide_ir = { path = "crates/oxide_ir" }
clap = { version = "4", features = ["derive"] }
rustyline = "14"
ansi_term = "0.12"
+1 -1
View File
@@ -10,10 +10,10 @@ description = "OxideJS - AST to bytecode compiler"
oxide_bytecode = { path = "../oxide_bytecode" }
oxide_parser = { path = "../oxide_parser" }
oxide_log = { path = "../oxide_log" }
oxide_ir.workspace = true
serde.workspace = true
serde_json.workspace = true
rustc-hash.workspace = true
smallvec.workspace = true
[dev-dependencies]
oxide_code_cache = { path = "../oxide_code_cache" }
+2 -2
View File
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler, FunctionBodyContext};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::{Class, ClassElement, MethodDefinitionKind, PropertyKey};
+1 -1
View File
@@ -1,5 +1,5 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::operand::Operand;
use oxide_ir::operand::Operand;
use oxide_parser::{ClassElement, PropertyKey};
impl Compiler {
+2 -2
View File
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler, FunctionBodyContext};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::{ClassElement, MethodDefinitionKind, PropertyKey};
+2 -2
View File
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
+7 -7
View File
@@ -1,10 +1,10 @@
use std::collections::{BTreeMap, HashMap, HashSet};
use crate::emit_ctx::{LabelCtx, ScopeCtx};
use crate::ir::inst::Inst;
use crate::ir::lower::lower;
use crate::ir::operand::{LabelId, Operand};
use crate::ir::IRFunction;
use oxide_ir::inst::Inst;
use oxide_ir::lower::lower;
use oxide_ir::operand::{LabelId, Operand};
use oxide_ir::IRFunction;
use crate::symbol_table::{Binding, SymbolTable};
use oxide_bytecode::module::CompiledModule;
use oxide_bytecode::module::UpvalueCapture;
@@ -464,7 +464,7 @@ impl CompileCtx {
/// 组装 IRFunction(两出口共用),take 走编译产物状态。
/// `parent_ctx` 用于补全 upvalue_captures 的 enclosing_reg(父符号表在父 emit 完成后完整)。
fn assemble_ir(&mut self, param_layout: crate::ir::ParamLayout, parent_ctx: Option<&CompileCtx>) -> IRFunction {
fn assemble_ir(&mut self, param_layout: oxide_ir::ParamLayout, parent_ctx: Option<&CompileCtx>) -> IRFunction {
let upvalue_captures = self
.current_upvalue_captures
.iter()
@@ -716,7 +716,7 @@ impl Compiler {
}
let ir = ctx.assemble_ir(
crate::ir::ParamLayout {
oxide_ir::ParamLayout {
base: param_base as u32,
count: param_specs.len() as u32,
},
@@ -889,7 +889,7 @@ impl Compiler {
crate::compiler_debug!("compile: done, {} instructions, {} constants", ctx.insts.len(), ctx.constants.len());
let ir = ctx.assemble_ir(
crate::ir::ParamLayout {
oxide_ir::ParamLayout {
base: ctx.scopes.builtin_reg_map.len() as u32,
count: 0,
},
+1 -1
View File
@@ -7,7 +7,7 @@
//! `CompileCtx`.
use crate::compiler::LabelScope;
use crate::ir::operand::LabelId;
use oxide_ir::operand::LabelId;
use crate::symbol_table::SymbolTable;
/// Jump-target / labeled-statement resolution state.
+2 -2
View File
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::AssignmentOperator;
+2 -2
View File
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::Expression;
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler, ParamSpec};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_parser::{Class, Expression, Statement};
impl Compiler {
+2 -2
View File
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::opcode::OpCode;
impl Compiler {
+2 -2
View File
@@ -1,6 +1,6 @@
use crate::compiler::{is_int_literal, CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::Expression;
+2 -2
View File
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::Expression;
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
impl Compiler {
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::{Expression, ObjectPropertyKind, PropertyKey, PropertyKind};
@@ -1,6 +1,6 @@
use crate::compiler::{is_side_effect_free, BinaryOperator, CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::{ChainElement, Expression, LogicalOperator, SimpleAssignmentTarget, UnaryOperator, UpdateOperator};
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
impl Compiler {
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::module::Constant;
impl Compiler {
+2 -2
View File
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::opcode::OpCode;
impl Compiler {
-1
View File
@@ -7,7 +7,6 @@ pub mod compiler_log;
pub mod emit_ctx;
pub mod expr;
pub mod hash;
pub mod ir;
pub mod prepass;
pub mod shared;
pub mod stmt;
+2 -2
View File
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::{
+2 -2
View File
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::{LabelId, Operand};
use oxide_ir::inst::Inst;
use oxide_ir::operand::{LabelId, Operand};
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::{ChainElement, Expression, LogicalOperator, PropertyKey};
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::{Expression, PropertyKey};
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::module::Constant;
use oxide_parser::PropertyKey;
@@ -1,5 +1,5 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use oxide_ir::inst::Inst;
impl Compiler {
pub(crate) fn emit_break_statement(
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::Statement;
+2 -2
View File
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::Statement;
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::{Statement, VariableDeclarationKind};
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler, ParamSpec};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::Statement;
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::{BindingPattern, Expression, Statement, VariableDeclarationKind};
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::Statement;
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::{Statement, VariableDeclarationKind};
@@ -1,5 +1,5 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use oxide_ir::inst::Inst;
use oxide_parser::Statement;
impl Compiler {
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::{BindingPattern, ForStatementInit, Statement, VariableDeclarationKind};
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::{ForStatementLeft, Statement, VariableDeclarationKind};
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::{ForStatementLeft, Statement};
@@ -1,5 +1,5 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use oxide_ir::inst::Inst;
use oxide_parser::Statement;
impl Compiler {
+2 -2
View File
@@ -1,6 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
use crate::ir::inst::Inst;
use crate::ir::operand::Operand;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_bytecode::opcode::OpCode;
use oxide_parser::Statement;
@@ -2,10 +2,10 @@
use oxide_bytecode::module::{CompiledModule, Constant};
use oxide_bytecode::opcode::{self, OpCode};
use oxide_compiler::ir::inst::Inst;
use oxide_compiler::ir::lower::lower;
use oxide_compiler::ir::operand::Operand;
use oxide_compiler::ir::IRFunction;
use oxide_ir::inst::Inst;
use oxide_ir::lower::lower;
use oxide_ir::operand::Operand;
use oxide_ir::IRFunction;
fn base_module() -> IRFunction {
IRFunction::new()
@@ -166,7 +166,7 @@ fn module_fields_are_copied_through() {
let mut f = base_module();
f.insts.push(Inst::new(OpCode::NOP, Operand::None, Operand::None, Operand::None));
f.constants.push(Constant::Int(42));
f.param_layout = oxide_compiler::ir::ParamLayout { base: 2, count: 3 };
f.param_layout = oxide_ir::ParamLayout { base: 2, count: 3 };
f.n_registers = 8;
f.is_arrow = true;
f.builtin_reg_map.push(("Math".to_string(), 1));
@@ -250,7 +250,7 @@ fn ir_function_domain_assemble_default_clone() {
let mut f = IRFunction::new();
f.insts.push(Inst::new(OpCode::NOP, Operand::None, Operand::None, Operand::None));
f.constants.push(Constant::Int(1));
f.param_layout = oxide_compiler::ir::ParamLayout { base: 0, count: 1 };
f.param_layout = oxide_ir::ParamLayout { base: 0, count: 1 };
f.n_registers = 2;
f.is_arrow = true;
f.builtin_reg_map.push(("Math".to_string(), 1));
+11
View File
@@ -0,0 +1,11 @@
[package]
name = "oxide_ir"
version.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
description = "OxideJS - IR intermediate representation and lowering to bytecode"
[dependencies]
oxide_bytecode = { path = "../oxide_bytecode" }
smallvec.workspace = true
@@ -6,7 +6,7 @@
use oxide_bytecode::opcode::OpCode;
use smallvec::SmallVec;
use super::operand::{LabelId, Operand};
use crate::operand::{LabelId, Operand};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Inst {
@@ -17,7 +17,7 @@ pub mod operand;
use oxide_bytecode::module::{Constant, UpvalueCapture};
use self::inst::Inst;
use crate::inst::Inst;
/// 参数段布局:base 起 count 个寄存器连续(VM 调用契约),RegAlloc 输入。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -7,9 +7,9 @@
use oxide_bytecode::module::CompiledModule;
use oxide_bytecode::opcode::{self, OpCode};
use super::inst::Inst;
use super::operand::Operand;
use super::IRFunction;
use crate::inst::Inst;
use crate::operand::Operand;
use crate::IRFunction;
/// IRFunction → CompiledModule。错误消息与现状(退役前 compiler.rs)逐字一致。
pub fn lower(f: &IRFunction) -> Result<CompiledModule, String> {