Refactor optimizer with aggressive collapsing
The optimizer has been refactored to separate its phases and introduce more aggressive collapsing capabilities. Phase 2 (Cracking) now focuses on stateless transformations, making it easier to reason about and potentially parallelize. Phase 2.5 (Aggressive Collapsing) has been introduced, enabling optimizations like: - Beta-reduction for lambda literals. - Cracking and inlining for constant closures. - Folding intrinsics for constant arithmetic. - Short-circuiting conditional expressions. These changes aim to improve performance by reducing redundant computations and code bloat. The maximum number of optimization passes has also been increased to 5 to allow for more complex transformations.
This commit is contained in:
+316
-119
@@ -3,17 +3,17 @@ use crate::ast::compiler::bound_nodes::{BoundKind, TypedNode, Address};
|
||||
use crate::ast::types::{Value, StaticType};
|
||||
use crate::ast::vm::Closure;
|
||||
use crate::ast::nodes::Node;
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// The Optimizer performs Phase 2: Partial Evaluation & Closure Cracking.
|
||||
/// The Optimizer performs Phase 2 (Cracking) and Phase 2.5 (Aggressive Collapsing).
|
||||
pub struct Optimizer {
|
||||
/// 0: None, 1: Cracking (Stateless transformation), 2: Aggressive (Collapsing)
|
||||
pub level: u32,
|
||||
max_passes: usize,
|
||||
}
|
||||
|
||||
impl Optimizer {
|
||||
pub fn new(level: u32) -> Self {
|
||||
Self { level, max_passes: 3 }
|
||||
Self { level, max_passes: 5 }
|
||||
}
|
||||
|
||||
pub fn optimize(&self, node: TypedNode) -> TypedNode {
|
||||
@@ -24,6 +24,7 @@ impl Optimizer {
|
||||
let mut current = node;
|
||||
for _ in 0..self.max_passes {
|
||||
let next = self.visit_node(current.clone());
|
||||
// TODO: Structural equality check to stop early
|
||||
current = next;
|
||||
}
|
||||
current
|
||||
@@ -31,84 +32,79 @@ impl Optimizer {
|
||||
|
||||
fn visit_node(&self, node: TypedNode) -> TypedNode {
|
||||
let (new_kind, new_ty) = match node.kind {
|
||||
BoundKind::Call { callee, args } => {
|
||||
BoundKind::Call { callee, args } | BoundKind::TailCall { callee, args } => {
|
||||
let callee = self.visit_node(*callee);
|
||||
let args = self.visit_node(*args);
|
||||
|
||||
// --- Cracking & Collapsing ---
|
||||
if let BoundKind::Constant(Value::Object(ref obj)) = callee.kind {
|
||||
if let Some(closure) = obj.as_any().downcast_ref::<Closure>() {
|
||||
// Level 1+: Inlining Upvalues (Stateless transformation)
|
||||
// Structure is PRESERVED (Call still exists), but code is specialized.
|
||||
let body = (*closure.function_node).clone();
|
||||
let inlined_body = self.inline_upvalues(body, &closure.upvalues, 0);
|
||||
|
||||
// Level 2: Aggressive Collapsing
|
||||
if self.level >= 2 {
|
||||
// 1. Collapse 0-parameter calls
|
||||
if let Some(0) = closure.positional_count {
|
||||
let is_empty_args = match &args.kind {
|
||||
BoundKind::Nop => true,
|
||||
BoundKind::Tuple { elements } => elements.is_empty(),
|
||||
_ => false,
|
||||
};
|
||||
if is_empty_args {
|
||||
return self.visit_node(inlined_body);
|
||||
}
|
||||
if self.level >= 2 {
|
||||
// Case 1: Beta-Reduction for Lambda Literals
|
||||
if let BoundKind::Lambda { params, body, .. } = &callee.kind
|
||||
&& let Some(collapsed) = self.try_beta_reduce(params, &args, (**body).clone()) {
|
||||
return self.visit_node(collapsed);
|
||||
}
|
||||
|
||||
// Case 2: Cracking and Inlining for Constant Closures
|
||||
if let BoundKind::Constant(Value::Object(ref obj)) = callee.kind
|
||||
&& let Some(closure) = obj.as_any().downcast_ref::<Closure>() {
|
||||
let mut sub = SubstitutionMap::new();
|
||||
for (i, cell) in closure.upvalues.iter().enumerate() {
|
||||
sub.add_upvalue(i as u32, cell.borrow().clone());
|
||||
}
|
||||
let inlined_body = sub.inline((*closure.function_node).clone());
|
||||
|
||||
// Try to collapse the now-stateless lambda call
|
||||
if let Some(collapsed) = self.try_beta_reduce(&closure.parameter_node, &args, inlined_body) {
|
||||
return self.visit_node(collapsed);
|
||||
}
|
||||
// 2. Beta-Reduction (TODO: Inlining parameters)
|
||||
}
|
||||
|
||||
// Stateless transformation (Cracking)
|
||||
let cracked_lambda = Node {
|
||||
identity: callee.identity.clone(),
|
||||
ty: callee.ty.clone(),
|
||||
kind: BoundKind::Lambda {
|
||||
params: closure.parameter_node.clone(),
|
||||
upvalues: vec![],
|
||||
body: Rc::new(inlined_body),
|
||||
positional_count: closure.positional_count,
|
||||
},
|
||||
};
|
||||
|
||||
return Node {
|
||||
identity: node.identity,
|
||||
kind: BoundKind::Call {
|
||||
callee: Box::new(cracked_lambda),
|
||||
args: Box::new(args)
|
||||
},
|
||||
ty: node.ty,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Level 2: Intrinsic Folding (Arithmetic on constants)
|
||||
if self.level >= 2 {
|
||||
if let Some(folded) = self.try_fold_intrinsic(&callee, &args) {
|
||||
return folded;
|
||||
}
|
||||
}
|
||||
|
||||
// Level 1: Just Crack closures but keep the Call structure
|
||||
if self.level >= 1
|
||||
&& let BoundKind::Constant(Value::Object(ref obj)) = callee.kind
|
||||
&& let Some(closure) = obj.as_any().downcast_ref::<Closure>() {
|
||||
let mut sub = SubstitutionMap::new();
|
||||
for (i, cell) in closure.upvalues.iter().enumerate() {
|
||||
sub.add_upvalue(i as u32, cell.borrow().clone());
|
||||
}
|
||||
let inlined_body = sub.inline((*closure.function_node).clone());
|
||||
|
||||
let cracked_lambda = Node {
|
||||
identity: callee.identity.clone(),
|
||||
ty: callee.ty.clone(),
|
||||
kind: BoundKind::Lambda {
|
||||
params: closure.parameter_node.clone(),
|
||||
upvalues: vec![], // CRACKED: Stateless
|
||||
body: Rc::new(inlined_body),
|
||||
positional_count: closure.positional_count,
|
||||
},
|
||||
};
|
||||
return Node {
|
||||
identity: node.identity,
|
||||
kind: BoundKind::Call { callee: Box::new(cracked_lambda), args: Box::new(args) },
|
||||
ty: node.ty,
|
||||
};
|
||||
}
|
||||
|
||||
(BoundKind::Call { callee: Box::new(callee), args: Box::new(args) }, node.ty)
|
||||
},
|
||||
|
||||
BoundKind::If { cond, then_br, else_br } => {
|
||||
let cond = self.visit_node(*cond);
|
||||
if self.level >= 2 {
|
||||
if let BoundKind::Constant(ref val) = cond.kind {
|
||||
if self.level >= 2
|
||||
&& let BoundKind::Constant(ref val) = cond.kind {
|
||||
if val.is_truthy() {
|
||||
return self.visit_node(*then_br);
|
||||
} else if let Some(else_node) = else_br {
|
||||
return self.visit_node(*else_node);
|
||||
} else {
|
||||
return Node {
|
||||
identity: node.identity,
|
||||
kind: BoundKind::Nop,
|
||||
ty: StaticType::Void,
|
||||
};
|
||||
return Node { identity: node.identity, kind: BoundKind::Nop, ty: StaticType::Void };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let then_br = Box::new(self.visit_node(*then_br));
|
||||
let else_br = else_br.map(|e| Box::new(self.visit_node(*e)));
|
||||
@@ -137,12 +133,13 @@ impl Optimizer {
|
||||
(BoundKind::Block { exprs: new_exprs }, ty)
|
||||
},
|
||||
|
||||
// Recursive Traversal
|
||||
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.as_ref().clone()));
|
||||
(BoundKind::Lambda { params, upvalues, body, positional_count }, node.ty)
|
||||
},
|
||||
|
||||
// Standard Recursive Traversal
|
||||
BoundKind::DefLocal { name, slot, value, captured_by } => {
|
||||
let value = Box::new(self.visit_node(*value));
|
||||
(BoundKind::DefLocal { name, slot, value, captured_by }, node.ty)
|
||||
@@ -167,108 +164,308 @@ impl Optimizer {
|
||||
let bound_expanded = Box::new(self.visit_node(*bound_expanded));
|
||||
(BoundKind::Expansion { original_call, bound_expanded }, node.ty)
|
||||
},
|
||||
|
||||
k => (k, node.ty),
|
||||
};
|
||||
|
||||
Node {
|
||||
identity: node.identity,
|
||||
kind: new_kind,
|
||||
ty: new_ty,
|
||||
Node { identity: node.identity, kind: new_kind, ty: new_ty }
|
||||
}
|
||||
|
||||
fn try_beta_reduce(&self, params: &TypedNode, args: &TypedNode, body: TypedNode) -> Option<TypedNode> {
|
||||
// SAFETY: Only beta-reduce if the body does not contain local definitions.
|
||||
if self.contains_def_local(&body) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut sub = SubstitutionMap::new();
|
||||
let mut arg_vals = Vec::new();
|
||||
self.flatten_typed_tuple(args, &mut arg_vals);
|
||||
|
||||
let mut slot_index = 0;
|
||||
self.map_params_to_args(params, &arg_vals, &mut slot_index, &mut sub);
|
||||
|
||||
// SAFETY: Only beta-reduce if ALL provided arguments were successfully inlined as constants.
|
||||
// If we leave any Local(i) references, the VM will crash because the frame is gone.
|
||||
if sub.locals.len() < arg_vals.len() {
|
||||
return None;
|
||||
}
|
||||
|
||||
if sub.is_empty() && !arg_vals.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(sub.inline(body))
|
||||
}
|
||||
|
||||
fn flatten_typed_tuple(&self, node: &TypedNode, into: &mut Vec<TypedNode>) {
|
||||
if let BoundKind::Tuple { elements } = &node.kind {
|
||||
for el in elements {
|
||||
self.flatten_typed_tuple(el, into);
|
||||
}
|
||||
} else if !matches!(node.kind, BoundKind::Nop) {
|
||||
into.push(node.clone());
|
||||
}
|
||||
}
|
||||
|
||||
fn map_params_to_args(&self, pattern: &TypedNode, args: &[TypedNode], offset: &mut usize, sub: &mut SubstitutionMap) {
|
||||
match &pattern.kind {
|
||||
BoundKind::Parameter { slot, .. } => {
|
||||
if let Some(arg) = args.get(*offset)
|
||||
&& let BoundKind::Constant(val) = &arg.kind {
|
||||
sub.add_local(*slot, val.clone());
|
||||
}
|
||||
*offset += 1;
|
||||
}
|
||||
BoundKind::Tuple { elements } => {
|
||||
for el in elements {
|
||||
self.map_params_to_args(el, args, offset, sub);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn try_fold_intrinsic(&self, callee: &TypedNode, args: &TypedNode) -> Option<TypedNode> {
|
||||
// Simple constant folding for + and - on Ints
|
||||
if let BoundKind::Get { name, .. } = &callee.kind {
|
||||
if let BoundKind::Tuple { elements } = &args.kind {
|
||||
if elements.len() == 2 {
|
||||
if let (BoundKind::Constant(Value::Int(a)), BoundKind::Constant(Value::Int(b))) = (&elements[0].kind, &elements[1].kind) {
|
||||
let res = match &*name.name {
|
||||
"+" => Some(Value::Int(a + b)),
|
||||
"-" => Some(Value::Int(a - b)),
|
||||
"*" => Some(Value::Int(a * b)),
|
||||
"/" if *b != 0 => Some(Value::Int(a / b)),
|
||||
_ => None
|
||||
};
|
||||
if let Some(val) = res {
|
||||
return Some(Node {
|
||||
identity: callee.identity.clone(),
|
||||
ty: StaticType::Int,
|
||||
kind: BoundKind::Constant(val),
|
||||
});
|
||||
}
|
||||
}
|
||||
if let BoundKind::Get { name, .. } = &callee.kind
|
||||
&& let BoundKind::Tuple { elements } = &args.kind
|
||||
&& elements.len() == 2
|
||||
{
|
||||
let val_a = self.get_const_int(&elements[0]);
|
||||
let val_b = self.get_const_int(&elements[1]);
|
||||
|
||||
if let (Some(a), Some(b)) = (val_a, val_b) {
|
||||
let res = match &*name.name {
|
||||
"+" => Some(Value::Int(a + b)),
|
||||
"-" => Some(Value::Int(a - b)),
|
||||
"*" => Some(Value::Int(a * b)),
|
||||
"/" if b != 0 => Some(Value::Int(a / b)),
|
||||
_ => None
|
||||
};
|
||||
if let Some(val) = res {
|
||||
return Some(Node {
|
||||
identity: callee.identity.clone(),
|
||||
ty: StaticType::Int,
|
||||
kind: BoundKind::Constant(val),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn inline_upvalues(&self, node: TypedNode, upvalues: &[Rc<std::cell::RefCell<Value>>], depth: usize) -> TypedNode {
|
||||
fn get_const_int(&self, node: &TypedNode) -> Option<i64> {
|
||||
match &node.kind {
|
||||
BoundKind::Constant(Value::Int(i)) => Some(*i),
|
||||
BoundKind::Tuple { elements } if elements.len() == 1 => self.get_const_int(&elements[0]),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
fn contains_def_local(&self, node: &TypedNode) -> bool {
|
||||
match &node.kind {
|
||||
BoundKind::DefLocal { .. } => true,
|
||||
BoundKind::If { cond, then_br, else_br } => {
|
||||
self.contains_def_local(cond) || self.contains_def_local(then_br) || else_br.as_ref().is_some_and(|e| self.contains_def_local(e))
|
||||
}
|
||||
BoundKind::Block { exprs } => exprs.iter().any(|e| self.contains_def_local(e)),
|
||||
BoundKind::Call { callee, args } | BoundKind::TailCall { callee, args } => self.contains_def_local(callee) || self.contains_def_local(args),
|
||||
BoundKind::Lambda { .. } => false, // Nested lambda definitions are safe (they have their own frame)
|
||||
BoundKind::Tuple { elements } => elements.iter().any(|e| self.contains_def_local(e)),
|
||||
BoundKind::Record { fields } => fields.iter().any(|(k, v)| self.contains_def_local(k) || self.contains_def_local(v)),
|
||||
BoundKind::Set { value, .. } => self.contains_def_local(value),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper for transitively inlining values and cleaning up capture lists.
|
||||
struct SubstitutionMap {
|
||||
/// Mapping Address::Local(slot) -> Value
|
||||
locals: HashMap<u32, Value>,
|
||||
/// Mapping Address::Upvalue(idx) -> Value
|
||||
upvalues: HashMap<u32, Value>,
|
||||
}
|
||||
|
||||
impl SubstitutionMap {
|
||||
fn new() -> Self {
|
||||
Self { locals: HashMap::new(), upvalues: HashMap::new() }
|
||||
}
|
||||
|
||||
fn add_local(&mut self, slot: u32, val: Value) {
|
||||
self.locals.insert(slot, val);
|
||||
}
|
||||
|
||||
fn add_upvalue(&mut self, idx: u32, val: Value) {
|
||||
self.upvalues.insert(idx, val);
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
self.locals.is_empty() && self.upvalues.is_empty()
|
||||
}
|
||||
|
||||
fn inline(&self, node: TypedNode) -> TypedNode {
|
||||
self.inline_recursive(node, 0, &HashMap::new())
|
||||
}
|
||||
|
||||
fn inline_recursive(&self, node: TypedNode, depth: usize, upvalue_subs: &HashMap<u32, Value>) -> TypedNode {
|
||||
let (new_kind, new_ty) = match node.kind {
|
||||
BoundKind::Get { addr, name } => {
|
||||
match addr {
|
||||
Address::Local(slot) if depth == 0 => {
|
||||
if let Some(val) = self.locals.get(&slot) {
|
||||
(BoundKind::Constant(val.clone()), val.static_type())
|
||||
} else { (BoundKind::Get { addr, name }, node.ty) }
|
||||
}
|
||||
Address::Upvalue(idx) if depth == 0 => {
|
||||
if let Some(val) = self.upvalues.get(&idx) {
|
||||
(BoundKind::Constant(val.clone()), val.static_type())
|
||||
} else { (BoundKind::Get { addr, name }, node.ty) }
|
||||
}
|
||||
Address::Upvalue(idx) if depth > 0 => {
|
||||
if let Some(val) = upvalue_subs.get(&idx) {
|
||||
(BoundKind::Constant(val.clone()), val.static_type())
|
||||
} else { (BoundKind::Get { addr, name }, node.ty) }
|
||||
}
|
||||
_ => (BoundKind::Get { addr, name }, node.ty)
|
||||
}
|
||||
},
|
||||
|
||||
BoundKind::Lambda { params, upvalues: original_upvalues, body, positional_count } => {
|
||||
// Phase 3: Un-Capturing
|
||||
// If this lambda captures variables that we just inlined, we MUST remove them
|
||||
// from its capture list to avoid "Stack underflow capture" errors in the VM.
|
||||
let mut new_upvalues = Vec::new();
|
||||
let mut next_inner_subs = HashMap::new();
|
||||
let mut mapping = Vec::new(); // Old Upvalue Index -> New Upvalue Index (or None)
|
||||
|
||||
for (old_idx, capture_addr) in original_upvalues.iter().enumerate() {
|
||||
let mut inlined_val = None;
|
||||
|
||||
if depth == 0 {
|
||||
match capture_addr {
|
||||
Address::Local(slot) => {
|
||||
if let Some(val) = self.locals.get(slot) { inlined_val = Some(val.clone()); }
|
||||
}
|
||||
Address::Upvalue(idx) => {
|
||||
if let Some(val) = self.upvalues.get(idx) { inlined_val = Some(val.clone()); }
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else if let Address::Upvalue(old_parent_idx) = capture_addr
|
||||
&& let Some(val) = upvalue_subs.get(old_parent_idx) {
|
||||
inlined_val = Some(val.clone());
|
||||
}
|
||||
|
||||
if let Some(val) = inlined_val {
|
||||
next_inner_subs.insert(old_idx as u32, val);
|
||||
mapping.push(None);
|
||||
} else {
|
||||
mapping.push(Some(new_upvalues.len() as u32));
|
||||
new_upvalues.push(*capture_addr);
|
||||
}
|
||||
}
|
||||
|
||||
// If any upvalues were removed, we must re-index the Get(Upvalue) nodes in the body.
|
||||
let body_node = self.inline_recursive(body.as_ref().clone(), depth + 1, &next_inner_subs);
|
||||
let reindexed_body = if new_upvalues.len() != original_upvalues.len() {
|
||||
self.reindex_upvalues(body_node, &mapping)
|
||||
} else {
|
||||
body_node
|
||||
};
|
||||
|
||||
(BoundKind::Lambda { params, upvalues: new_upvalues, body: Rc::new(reindexed_body), positional_count }, node.ty)
|
||||
},
|
||||
|
||||
// Boilerplate Recursive Traversal
|
||||
BoundKind::If { cond, then_br, else_br } => {
|
||||
let cond = Box::new(self.inline_recursive(*cond, depth, upvalue_subs));
|
||||
let then_br = Box::new(self.inline_recursive(*then_br, depth, upvalue_subs));
|
||||
let else_br = else_br.map(|e| Box::new(self.inline_recursive(*e, depth, upvalue_subs)));
|
||||
(BoundKind::If { cond, then_br, else_br }, node.ty)
|
||||
},
|
||||
BoundKind::Block { exprs } => {
|
||||
let exprs = exprs.into_iter().map(|e| self.inline_recursive(e, depth, upvalue_subs)).collect();
|
||||
(BoundKind::Block { exprs }, node.ty)
|
||||
},
|
||||
BoundKind::Call { callee, args } | BoundKind::TailCall { callee, args } => {
|
||||
let callee = Box::new(self.inline_recursive(*callee, depth, upvalue_subs));
|
||||
let args = Box::new(self.inline_recursive(*args, depth, upvalue_subs));
|
||||
(BoundKind::Call { callee, args }, node.ty)
|
||||
},
|
||||
BoundKind::DefLocal { name, slot, value, captured_by } => {
|
||||
let value = Box::new(self.inline_recursive(*value, depth, upvalue_subs));
|
||||
(BoundKind::DefLocal { name, slot, value, captured_by }, node.ty)
|
||||
},
|
||||
BoundKind::Set { addr, value } => {
|
||||
let value = Box::new(self.inline_recursive(*value, depth, upvalue_subs));
|
||||
(BoundKind::Set { addr, value }, node.ty)
|
||||
},
|
||||
BoundKind::Tuple { elements } => {
|
||||
let elements = elements.into_iter().map(|e| self.inline_recursive(e, depth, upvalue_subs)).collect();
|
||||
(BoundKind::Tuple { elements }, node.ty)
|
||||
},
|
||||
BoundKind::Record { fields } => {
|
||||
let fields = fields.into_iter().map(|(k, v)| (self.inline_recursive(k, depth, upvalue_subs), self.inline_recursive(v, depth, upvalue_subs))).collect();
|
||||
(BoundKind::Record { fields }, node.ty)
|
||||
},
|
||||
k => (k, node.ty),
|
||||
};
|
||||
|
||||
Node { identity: node.identity, kind: new_kind, ty: new_ty }
|
||||
}
|
||||
|
||||
/// Re-maps Get(Upvalue(old_idx)) to Get(Upvalue(new_idx)) based on a mapping table.
|
||||
fn reindex_upvalues(&self, node: TypedNode, mapping: &[Option<u32>]) -> TypedNode {
|
||||
let (new_kind, new_ty) = match node.kind {
|
||||
BoundKind::Get { addr: Address::Upvalue(idx), name } => {
|
||||
if depth == 0 {
|
||||
if let Some(cell) = upvalues.get(idx as usize) {
|
||||
let val = cell.borrow().clone();
|
||||
let ty = val.static_type();
|
||||
(BoundKind::Constant(val), ty)
|
||||
} else {
|
||||
(BoundKind::Get { addr: Address::Upvalue(idx), name }, node.ty)
|
||||
}
|
||||
if let Some(Some(new_idx)) = mapping.get(idx as usize) {
|
||||
(BoundKind::Get { addr: Address::Upvalue(*new_idx), name }, node.ty)
|
||||
} else {
|
||||
(BoundKind::Get { addr: Address::Upvalue(idx), name }, node.ty)
|
||||
}
|
||||
},
|
||||
|
||||
// Nested Lambda: Increment depth (we only re-index upvalues of the current scope)
|
||||
BoundKind::Lambda { params, upvalues, body, positional_count } => {
|
||||
// IMPORTANT: In MyC, nested upvalues refer to the capture list of the nested lambda.
|
||||
// We DON'T re-index inside nested lambdas because their upvalue list is separate.
|
||||
(BoundKind::Lambda { params, upvalues, body, positional_count }, node.ty)
|
||||
},
|
||||
|
||||
// Recursive Traversal
|
||||
BoundKind::If { cond, then_br, else_br } => {
|
||||
let cond = Box::new(self.inline_upvalues(*cond, upvalues, depth));
|
||||
let then_br = Box::new(self.inline_upvalues(*then_br, upvalues, depth));
|
||||
let else_br = else_br.map(|e| Box::new(self.inline_upvalues(*e, upvalues, depth)));
|
||||
let cond = Box::new(self.reindex_upvalues(*cond, mapping));
|
||||
let then_br = Box::new(self.reindex_upvalues(*then_br, mapping));
|
||||
let else_br = else_br.map(|e| Box::new(self.reindex_upvalues(*e, mapping)));
|
||||
(BoundKind::If { cond, then_br, else_br }, node.ty)
|
||||
},
|
||||
BoundKind::Block { exprs } => {
|
||||
let exprs = exprs.into_iter().map(|e| self.inline_upvalues(e, upvalues, depth)).collect();
|
||||
let exprs = exprs.into_iter().map(|e| self.reindex_upvalues(e, mapping)).collect();
|
||||
(BoundKind::Block { exprs }, node.ty)
|
||||
},
|
||||
BoundKind::Call { callee, args } => {
|
||||
let callee = Box::new(self.inline_upvalues(*callee, upvalues, depth));
|
||||
let args = Box::new(self.inline_upvalues(*args, upvalues, depth));
|
||||
BoundKind::Call { callee, args } | BoundKind::TailCall { callee, args } => {
|
||||
let callee = Box::new(self.reindex_upvalues(*callee, mapping));
|
||||
let args = Box::new(self.reindex_upvalues(*args, mapping));
|
||||
(BoundKind::Call { callee, args }, node.ty)
|
||||
},
|
||||
BoundKind::TailCall { callee, args } => {
|
||||
let callee = Box::new(self.inline_upvalues(*callee, upvalues, depth));
|
||||
let args = Box::new(self.inline_upvalues(*args, upvalues, depth));
|
||||
(BoundKind::TailCall { callee, args }, node.ty)
|
||||
},
|
||||
BoundKind::DefLocal { name, slot, value, captured_by } => {
|
||||
let value = Box::new(self.inline_upvalues(*value, upvalues, depth));
|
||||
let value = Box::new(self.reindex_upvalues(*value, mapping));
|
||||
(BoundKind::DefLocal { name, slot, value, captured_by }, node.ty)
|
||||
},
|
||||
BoundKind::Set { addr, value } => {
|
||||
let value = Box::new(self.inline_upvalues(*value, upvalues, depth));
|
||||
let value = Box::new(self.reindex_upvalues(*value, mapping));
|
||||
(BoundKind::Set { addr, value }, node.ty)
|
||||
},
|
||||
BoundKind::Tuple { elements } => {
|
||||
let elements = elements.into_iter().map(|e| self.inline_upvalues(e, upvalues, depth)).collect();
|
||||
let elements = elements.into_iter().map(|e| self.reindex_upvalues(e, mapping)).collect();
|
||||
(BoundKind::Tuple { elements }, node.ty)
|
||||
},
|
||||
BoundKind::Record { fields } => {
|
||||
let fields = fields.into_iter().map(|(k, v)| (self.inline_upvalues(k, upvalues, depth), self.inline_upvalues(v, upvalues, depth))).collect();
|
||||
let fields = fields.into_iter().map(|(k, v)| (self.reindex_upvalues(k, mapping), self.reindex_upvalues(v, mapping))).collect();
|
||||
(BoundKind::Record { fields }, node.ty)
|
||||
},
|
||||
BoundKind::Lambda { params, upvalues: lambda_upvalues, body, positional_count } => {
|
||||
let body = Rc::new(self.inline_upvalues(body.as_ref().clone(), upvalues, depth + 1));
|
||||
(BoundKind::Lambda { params, upvalues: lambda_upvalues, body, positional_count }, node.ty)
|
||||
},
|
||||
|
||||
k => (k, node.ty),
|
||||
};
|
||||
|
||||
Node {
|
||||
identity: node.identity,
|
||||
kind: new_kind,
|
||||
ty: new_ty,
|
||||
}
|
||||
Node { identity: node.identity, kind: new_kind, ty: new_ty }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user