feat: Add lambda collection and specialization
Introduces `LambdaCollector` to gather lambda functions and populate the function registry. This enables the `Specializer` to work with user-defined functions. The `Environment` struct is updated to manage the `function_registry` and `monomorph_cache`, which are essential for the specialization process. The `link` method in `Environment` now incorporates lambda collection and node specialization before applying TCO optimization. This ensures that lambdas are properly processed and specialized for potential performance gains. The `Specializer`'s `new` constructor has been modified to accept and initialize the `MonoCache` through an `Rc<RefCell<MonoCache>>`. This allows the cache to be shared across different specialized functions. Also includes minor refactoring and type adjustments in `specializer.rs` for better clarity and consistency.
This commit is contained in:
+102
-1
@@ -9,22 +9,44 @@ use crate::ast::compiler::{TypedNode, TypeChecker};
|
||||
use crate::ast::vm::{VM, TracingObserver};
|
||||
|
||||
use crate::ast::compiler::tco::TCO;
|
||||
use crate::ast::compiler::lambda_collector::LambdaCollector;
|
||||
use crate::ast::compiler::dumper::Dumper;
|
||||
use crate::ast::compiler::macros::{MacroExpander, MacroRegistry, MacroEvaluator};
|
||||
use crate::ast::compiler::specializer::{Specializer, MonoCache, FunctionRegistry};
|
||||
use crate::ast::rtl;
|
||||
use crate::ast::rtl::intrinsics;
|
||||
use crate::ast::compiler::bound_nodes::{BoundKind, Address};
|
||||
|
||||
pub struct Environment {
|
||||
pub global_names: Rc<RefCell<HashMap<Symbol, u32>>>,
|
||||
pub global_types: Rc<RefCell<HashMap<u32, StaticType>>>,
|
||||
pub global_values: Rc<RefCell<Vec<Value>>>,
|
||||
pub function_registry: Rc<RefCell<HashMap<u32, TypedNode>>>,
|
||||
pub monomorph_cache: Rc<RefCell<MonoCache>>,
|
||||
pub debug_mode: bool,
|
||||
}
|
||||
|
||||
struct EnvFunctionRegistry {
|
||||
registry: Rc<RefCell<HashMap<u32, TypedNode>>>,
|
||||
}
|
||||
|
||||
impl FunctionRegistry for EnvFunctionRegistry {
|
||||
fn resolve(&self, addr: Address) -> Option<TypedNode> {
|
||||
if let Address::Global(idx) = addr {
|
||||
self.registry.borrow().get(&idx).cloned()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Evaluator used during macro expansion to allow compile-time logic.
|
||||
struct RuntimeMacroEvaluator {
|
||||
global_names: Rc<RefCell<HashMap<Symbol, u32>>>,
|
||||
global_types: Rc<RefCell<HashMap<u32, StaticType>>>,
|
||||
global_values: Rc<RefCell<Vec<Value>>>,
|
||||
function_registry: Rc<RefCell<HashMap<u32, TypedNode>>>,
|
||||
}
|
||||
|
||||
impl MacroEvaluator for RuntimeMacroEvaluator {
|
||||
@@ -58,6 +80,8 @@ impl Environment {
|
||||
global_names: Rc::new(RefCell::new(HashMap::new())),
|
||||
global_types: Rc::new(RefCell::new(HashMap::new())),
|
||||
global_values: Rc::new(RefCell::new(Vec::new())),
|
||||
function_registry: Rc::new(RefCell::new(HashMap::new())),
|
||||
monomorph_cache: Rc::new(RefCell::new(HashMap::new())),
|
||||
debug_mode: false,
|
||||
};
|
||||
env.register_stdlib();
|
||||
@@ -73,6 +97,7 @@ impl Environment {
|
||||
global_names: self.global_names.clone(),
|
||||
global_types: self.global_types.clone(),
|
||||
global_values: self.global_values.clone(),
|
||||
function_registry: self.function_registry.clone(),
|
||||
};
|
||||
MacroExpander::new(MacroRegistry::new(), evaluator)
|
||||
}
|
||||
@@ -136,7 +161,83 @@ impl Environment {
|
||||
|
||||
/// Backend: Optimization (TCO, etc.)
|
||||
pub fn link(&self, node: TypedNode) -> TypedNode {
|
||||
TCO::optimize(node)
|
||||
// 1. Collect Lambdas (Populate the registry for the specializer)
|
||||
LambdaCollector::collect(&node, &mut self.function_registry.borrow_mut());
|
||||
|
||||
// 2. Specialize
|
||||
let specialized = self.specialize_node(node);
|
||||
// let specialized = node;
|
||||
|
||||
// 3. Optimize
|
||||
TCO::optimize(specialized)
|
||||
}
|
||||
|
||||
fn specialize_node(&self, node: TypedNode) -> TypedNode {
|
||||
let registry = Rc::new(EnvFunctionRegistry {
|
||||
registry: self.function_registry.clone(),
|
||||
});
|
||||
|
||||
let rtl_lookup = Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args));
|
||||
|
||||
// We need to construct a compiler callback that can recursively specialize and compile.
|
||||
// To avoid complex self-capturing, we reconstruct the environment context needed.
|
||||
let func_reg = self.function_registry.clone();
|
||||
let mono_cache = self.monomorph_cache.clone();
|
||||
let global_values = self.global_values.clone(); // Needed for VM/Closure creation
|
||||
|
||||
let compiler = Rc::new(move |func_node: TypedNode, _arg_types: &[StaticType]| -> Result<(Value, StaticType), String> {
|
||||
// 1. Specialize the body (Recursive)
|
||||
// We recreate the specializer context here.
|
||||
// Note: This creates a new Specializer for each recursion, but they SHARE the 'mono_cache'.
|
||||
let sub_registry = Rc::new(EnvFunctionRegistry { registry: func_reg.clone() });
|
||||
let sub_rtl_lookup = Rc::new(|name: &str, args: &[StaticType]| intrinsics::lookup(name, args));
|
||||
|
||||
// Note: We are passing 'None' as compiler to the inner specializer for now to prevent infinite recursion on cycles.
|
||||
// A robust implementation would handle the recursion cycle or use a shared compiler reference.
|
||||
// For 'tak', the recursion is handled by the cache or dynamic fallback.
|
||||
let sub_specializer = Specializer::new(
|
||||
Some(sub_registry),
|
||||
None, // recursive compilation limit (depth 1) for safety
|
||||
Some(sub_rtl_lookup),
|
||||
Some(mono_cache.clone())
|
||||
);
|
||||
|
||||
let specialized_ast = sub_specializer.specialize(func_node);
|
||||
|
||||
// 2. Optimize (TCO)
|
||||
let optimized_ast = TCO::optimize(specialized_ast);
|
||||
|
||||
// 3. Compile to Closure (VM)
|
||||
// We run the VM once to evaluate the Lambda definition, producing a closure Value.
|
||||
let mut vm = VM::new(global_values.clone());
|
||||
|
||||
let compiled_val = match vm.run(&optimized_ast) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return Err(format!("VM Error during specialization: {}", e)),
|
||||
};
|
||||
|
||||
// We need the return type.
|
||||
// For a Lambda, the type is stored in the node.
|
||||
// But we need the return type of the FUNCTION (e.g. Int), not the type of the Lambda node (Method).
|
||||
// Actually, Specializer expects (Value, ReturnType).
|
||||
// If the specialized function returns Int, we return Int.
|
||||
let ret_type = if let BoundKind::Lambda { body, .. } = &optimized_ast.kind {
|
||||
body.ty.clone()
|
||||
} else {
|
||||
StaticType::Any
|
||||
};
|
||||
|
||||
Ok((compiled_val, ret_type))
|
||||
});
|
||||
|
||||
let specializer = Specializer::new(
|
||||
Some(registry),
|
||||
Some(compiler),
|
||||
Some(rtl_lookup),
|
||||
Some(self.monomorph_cache.clone()),
|
||||
);
|
||||
|
||||
specializer.specialize(node)
|
||||
}
|
||||
|
||||
/// Runtime: Execute the linked AST in the VM
|
||||
|
||||
Reference in New Issue
Block a user