Formatting

This commit is contained in:
Michael Schimmel
2026-02-24 07:27:13 +01:00
parent 9b7ef5080c
commit eeb6621280
7 changed files with 713 additions and 334 deletions
+122 -36
View File
@@ -16,7 +16,9 @@ pub type RtlLookupFunc = Rc<dyn Fn(&str, &[StaticType]) -> Option<(Value, Static
pub trait FunctionRegistry {
fn resolve(&self, addr: Address) -> Option<BoundNode>;
fn resolve_analyzed(&self, _addr: Address) -> Option<AnalyzedNode> { None }
fn resolve_analyzed(&self, _addr: Address) -> Option<AnalyzedNode> {
None
}
}
pub type MonoCache = HashMap<MonoCacheKey, (Value, StaticType)>;
@@ -52,7 +54,7 @@ impl Specializer {
BoundKind::Call { callee, args } => {
let (new_callee, new_args, _ret_ty) =
self.specialize_call_logic(*callee, *args, node.ty.original.ty.clone());
let new_metrics = node.ty.clone();
(
BoundKind::Call {
@@ -63,28 +65,76 @@ impl Specializer {
)
}
BoundKind::If { cond, then_br, else_br } => {
BoundKind::If {
cond,
then_br,
else_br,
} => {
let cond = Box::new(self.visit_node(*cond));
let then_br = Box::new(self.visit_node(*then_br));
let else_br = else_br.map(|e| Box::new(self.visit_node(*e)));
(BoundKind::If { cond, then_br, else_br }, node.ty.clone())
(
BoundKind::If {
cond,
then_br,
else_br,
},
node.ty.clone(),
)
}
BoundKind::Block { exprs } => {
let exprs = exprs.into_iter().map(|e| self.visit_node(e)).collect();
(BoundKind::Block { exprs }, node.ty.clone())
}
BoundKind::Lambda { params, upvalues, body, positional_count } => {
BoundKind::Lambda {
params,
upvalues,
body,
positional_count,
} => {
let params = Rc::new(self.visit_node(params.as_ref().clone()));
let body = Rc::new(self.visit_node((*body).clone()));
(BoundKind::Lambda { params, upvalues, body, positional_count }, node.ty.clone())
(
BoundKind::Lambda {
params,
upvalues,
body,
positional_count,
},
node.ty.clone(),
)
}
BoundKind::DefLocal { name, slot, value, captured_by } => {
BoundKind::DefLocal {
name,
slot,
value,
captured_by,
} => {
let value = Box::new(self.visit_node(*value));
(BoundKind::DefLocal { name, slot, value, captured_by }, node.ty.clone())
(
BoundKind::DefLocal {
name,
slot,
value,
captured_by,
},
node.ty.clone(),
)
}
BoundKind::DefGlobal { name, global_index, value } => {
BoundKind::DefGlobal {
name,
global_index,
value,
} => {
let value = Box::new(self.visit_node(*value));
(BoundKind::DefGlobal { name, global_index, value }, node.ty.clone())
(
BoundKind::DefGlobal {
name,
global_index,
value,
},
node.ty.clone(),
)
}
BoundKind::Set { addr, value } => {
let value = Box::new(self.visit_node(*value));
@@ -101,9 +151,18 @@ impl Specializer {
.collect();
(BoundKind::Record { fields }, node.ty.clone())
}
BoundKind::Expansion { original_call, bound_expanded } => {
BoundKind::Expansion {
original_call,
bound_expanded,
} => {
let bound_expanded = Box::new(self.visit_node(*bound_expanded));
(BoundKind::Expansion { original_call, bound_expanded }, node.ty.clone())
(
BoundKind::Expansion {
original_call,
bound_expanded,
},
node.ty.clone(),
)
}
k => (k, node.ty.clone()),
};
@@ -130,11 +189,12 @@ impl Specializer {
return (new_callee, new_args, original_ty);
};
let arg_types: Vec<StaticType> = if let StaticType::Tuple(elements) = &new_args.ty.original.ty {
elements.clone()
} else {
vec![new_args.ty.original.ty.clone()]
};
let arg_types: Vec<StaticType> =
if let StaticType::Tuple(elements) = &new_args.ty.original.ty {
elements.clone()
} else {
vec![new_args.ty.original.ty.clone()]
};
if arg_types.iter().any(|t| matches!(t, StaticType::Any)) {
return (new_callee, new_args, original_ty);
@@ -146,10 +206,14 @@ impl Specializer {
};
if let Some((val, ret_ty)) = self.cache.borrow().get(&key) {
let specialized_callee = self.make_constant_node(val.clone(), StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(arg_types),
ret: ret_ty.clone(),
})), &new_callee);
let specialized_callee = self.make_constant_node(
val.clone(),
StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(arg_types),
ret: ret_ty.clone(),
})),
&new_callee,
);
return (specialized_callee, new_args, ret_ty.clone());
}
@@ -157,11 +221,17 @@ impl Specializer {
&& let BoundKind::Get { name, .. } = &new_callee.kind
&& let Some((val, ret_ty)) = rtl_lookup(&name.name, &arg_types)
{
self.cache.borrow_mut().insert(key.clone(), (val.clone(), ret_ty.clone()));
let specialized_callee = self.make_constant_node(val.clone(), StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(arg_types),
ret: ret_ty.clone(),
})), &new_callee);
self.cache
.borrow_mut()
.insert(key.clone(), (val.clone(), ret_ty.clone()));
let specialized_callee = self.make_constant_node(
val.clone(),
StaticType::Function(Box::new(Signature {
params: StaticType::Tuple(arg_types),
ret: ret_ty.clone(),
})),
&new_callee,
);
return (specialized_callee, new_args, ret_ty);
}
@@ -172,20 +242,27 @@ impl Specializer {
return (new_callee, new_args, original_ty);
}
if let Some(compiler) = &self.compiler
if let Some(compiler) = &self.compiler
&& let Some(func_node) = self.registry.as_ref().and_then(|r| r.resolve(address))
&& let Ok((compiled_val, ret_ty)) = compiler(func_node, &arg_types)
{
self.cache.borrow_mut().insert(key, (compiled_val.clone(), ret_ty.clone()));
self.cache
.borrow_mut()
.insert(key, (compiled_val.clone(), ret_ty.clone()));
let flat_elements = self.flatten_tuple(new_args.clone());
let flat_types = flat_elements.iter().map(|e| e.ty.original.ty.clone()).collect();
let flat_types = flat_elements
.iter()
.map(|e| e.ty.original.ty.clone())
.collect();
let flattened_args = Node {
identity: new_args.identity.clone(),
kind: BoundKind::Tuple { elements: flat_elements },
kind: BoundKind::Tuple {
elements: flat_elements,
},
ty: NodeMetrics {
original: Rc::new(Node {
identity: new_args.identity.clone(),
kind: BoundKind::Tuple { elements: vec![] },
kind: BoundKind::Tuple { elements: vec![] },
ty: StaticType::Tuple(flat_types),
}),
purity: new_args.ty.purity,
@@ -193,17 +270,26 @@ impl Specializer {
},
};
let specialized_callee = self.make_constant_node(compiled_val, StaticType::Function(Box::new(Signature {
params: flattened_args.ty.original.ty.clone(),
ret: ret_ty.clone(),
})), &new_callee);
let specialized_callee = self.make_constant_node(
compiled_val,
StaticType::Function(Box::new(Signature {
params: flattened_args.ty.original.ty.clone(),
ret: ret_ty.clone(),
})),
&new_callee,
);
return (specialized_callee, flattened_args, ret_ty);
}
(new_callee, new_args, original_ty)
}
fn make_constant_node(&self, val: Value, ty: StaticType, template: &AnalyzedNode) -> AnalyzedNode {
fn make_constant_node(
&self,
val: Value,
ty: StaticType,
template: &AnalyzedNode,
) -> AnalyzedNode {
let typed_original = Rc::new(Node {
identity: template.identity.clone(),
kind: BoundKind::Constant(val.clone()),