pub mod codegen;
pub mod error;
pub mod ir;
pub mod lowering;
pub mod parser;
pub mod regalloc;
pub mod semantic;
pub mod symbol;
use std::collections::HashMap;
use crate::bytecode::{FunctionId, Module, Register};
use crate::compiler::error::CompileError;
use crate::compiler::ir::{FuncSignature, FunctionBuilder, IrFunction, IrUnit, SSABuilder};
use crate::compiler::lowering::JSASTLower;
use crate::compiler::parser::parse_js;
use crate::compiler::symbol::SymbolTable;
use codegen::Codegen;
use oxc_allocator::Allocator;
pub struct Compiler {
}
impl Compiler {
pub fn new() -> Self {
Self {}
}
pub fn compile(&mut self, source: &str) -> Result<Module, CompileError> {
let allocator = Allocator::default();
let program = parse_js(&allocator, source).map_err(|parse_errors| {
if let Some(first_err) = parse_errors.errors.first() {
CompileError::syntax(
format!("{}", parse_errors),
first_err.line,
first_err.column,
)
} else {
CompileError::syntax("Parse error", 1, 1)
}
})?;
let mut semantic = crate::compiler::semantic::SemanticAnalyzer::new();
let semantic_errors = semantic.analyze(&program);
if !semantic_errors.is_empty() {
return Err(semantic_errors.into_iter().next().unwrap());
}
let mut unit = IrUnit::new();
let main_sig = FuncSignature::new("main", vec![]);
let main_id = unit.declare_function(main_sig.clone());
let mut main_func = IrFunction::new(main_id, main_sig);
{
let mut builder = FunctionBuilder::new(&mut unit, &mut main_func);
let symbols = SymbolTable::new();
let mut lower = JSASTLower::new_script(&mut builder, symbols);
lower.lower_program(&program);
}
unit.define_function(main_id, main_func);
let registers = Register::general();
let mut all_codes: Vec<crate::bytecode::Bytecode> = Vec::new();
let mut symtab = HashMap::new();
let function_count = unit.functions.len();
let mut func_info: HashMap<u32, (String, usize)> = HashMap::new();
let mut generators: std::collections::HashSet<u32> = std::collections::HashSet::new();
let mut derived_ctors: std::collections::HashSet<u32> = std::collections::HashSet::new();
for func in &unit.functions {
func_info.insert(
func.id.as_usize() as u32,
(func.signature.name.to_string(), func.signature.arity),
);
if func.signature.is_generator {
generators.insert(func.id.as_usize() as u32);
}
if func.signature.is_derived_ctor {
derived_ctors.insert(func.id.as_usize() as u32);
}
}
for idx in 0..function_count {
let func_id = FunctionId::new(idx as u32);
let throw_to_handlers = {
let func = unit.get_function_mut(func_id).ok_or_else(|| {
CompileError::internal(format!("function {func_id} not found"))
})?;
let mut ssa = SSABuilder::new(&mut func.control_flow_graph);
ssa.convert_to_ssa();
ssa.into_throw_to_handlers()
};
let cfg = {
let func = unit.get_function_mut(func_id).ok_or_else(|| {
CompileError::internal(format!("function {func_id} not found"))
})?;
std::mem::replace(&mut func.control_flow_graph, ir::ControlFlowGraph::new())
};
let mut codegen = Codegen::new(®isters, throw_to_handlers);
let func_codes = codegen.generate_code(cfg).to_vec();
let offset = all_codes.len();
symtab.insert(func_id, offset);
all_codes.extend(func_codes);
}
Ok(Module::new(
Some("main".to_string()),
unit.constants,
symtab,
func_info,
generators,
derived_ctors,
all_codes,
))
}
}
impl Default for Compiler {
fn default() -> Self {
Self::new()
}
}