From b0ca375bb8d9ac6ac2241d51685604604b983638 Mon Sep 17 00:00:00 2001 From: Jaap Frolich Date: Tue, 17 Nov 2026 21:42:30 +0000 Subject: [PATCH 1/2] Lowering: the statement stack owns statement handles Why: Phase T6x of the three trees removes the J rows the lowering stores and passes. Its first row (T6x-a1) puts the statement stack itself on the interface: the stack owns the statements of its runs as handles of the implementation, so a statement built through the interface is pushed as it is, not turned into a row first. Representation modified (StatementStack): Before: struct StatementStack { slots: Vec>, held: Vec, .. } enum Slot { Statement(NodeId), Held(Span), Dead(u32) } push(NodeId), fill(slot, NodeId), fill_held(slot, &[NodeId]) take_list(&mut Bir, Span) -> ListId drop_empty_blocks(&mut Bir, Span) // View match, Bir::release After: struct StatementStack { slots: Vec>>, held: Vec>, .. } enum Slot { Statement(S), Held(Span), Dead(u32) } push(B::S), fill(slot, B::S), fill_held(slot, impl IntoIterator) take_list(&mut B, &mut Pools, Span) -> B::L // JsStmt::list over the moved run drop_empty_blocks(&mut B, &mut Pools, Span) // is_empty_block, release_stmt `hold_run` and `take_list` move each statement out of its slot (a hold inside a run entry by entry). The reads keep their results as rows (`first`, `first_single_slot`, `only`, `last`, `statements`, `statements_rev`) and read a statement in place through one private `peek`, which T6x-a3 deletes. `close` keeps its signature (T6x-b), so `take_list` gives it `B::list_id` of the list. Added, each with its cost in its doc comment: `statement_refs` (the statements of a run read in place; O(slots + held statements)); `take_run` (private: the statements of a run moved out; same cost); `slot_len` (O(1)) and `slot_row` (O(1), through `peek`) in place of `slot`; `peek` (O(1)); `Default` written out (O(1)). In `cond.rs` the implementation of `norm::IfLists` moves from the stack to `StackLists`, the stack and the implementation (16 bytes), with the same six bodies; T6x-a3 deletes it. Callers wrap at the call: a push or fill of `B::stmt_node(x)` gives `x`; of a row, `B::stmt_from_node(row)`; a `take_list` whose result went into `B::list_from` loses both wraps, one that is stored or passed on as a `ListId` is `B::list_id(..)`. `args.rs::declares_pure` takes `&B::S` and reads the runs of the arguments with `statement_refs`. Where a caller pushed a handle and then noted it as an empty block, the row it notes is `JsStmt::stmt_id` of the handle, read before the push (on rows the row), so no `stmt_node`/`stmt_from_node` pair is left: `cond.rs` `place`, `block_statement` and the guard of an `if` without an else. Generated JavaScript does not change; the default build does not run this code. Tests: `--lib bir::lower` 568 passed, `--lib bir::j` 58 passed, none ignored. The tests of the stack and of its callers changed only to compile (`StatementStack::`, `Rows::stmt_from_node` around a pushed row, `take_list` with an implementation); no expected value changed; the two size assertions keep 96 and 12. Co-authored-by: Jaap Frolich Session: https://os.submodule.nl/session/bks-6e0f5c7f-c4b3-74aa-868a-9195e58e2297 Signed-off-by: Jaap Frolich --- compiler-rust/src/bir/lower/args.rs | 15 +- compiler-rust/src/bir/lower/assign.rs | 2 +- compiler-rust/src/bir/lower/cond.rs | 98 +++--- compiler-rust/src/bir/lower/exit.rs | 16 +- compiler-rust/src/bir/lower/export.rs | 2 +- compiler-rust/src/bir/lower/expr.rs | 8 +- compiler-rust/src/bir/lower/external.rs | 8 +- compiler-rust/src/bir/lower/ffi_args.rs | 2 +- compiler-rust/src/bir/lower/loops.rs | 18 +- compiler-rust/src/bir/lower/mod.rs | 15 +- compiler-rust/src/bir/lower/oracle_tests.rs | 4 +- compiler-rust/src/bir/lower/out.rs | 359 +++++++++++++------- compiler-rust/src/bir/lower/prim.rs | 2 +- compiler-rust/src/bir/lower/rec.rs | 8 +- compiler-rust/src/bir/lower/switch.rs | 16 +- compiler-rust/src/bir/lower/tail.rs | 12 +- compiler-rust/src/bir/lower/try_.rs | 4 +- compiler-rust/src/bir/lower/untagged.rs | 24 +- compiler-rust/src/bir/lower/variant.rs | 4 +- 19 files changed, 374 insertions(+), 243 deletions(-) diff --git a/compiler-rust/src/bir/lower/args.rs b/compiler-rust/src/bir/lower/args.rs index 6c43a0d188..d499958549 100644 --- a/compiler-rust/src/bir/lower/args.rs +++ b/compiler-rust/src/bir/lower/args.rs @@ -29,19 +29,18 @@ impl State<'_, B> { /// Whether `statement` declares a variable whose value, if it has one, /// runs no code. One step per member of the value's chain of field /// reads with a position (`runs_no_code`); O(1) otherwise. - fn declares_pure(&self, statement: NodeId) -> bool { - let statement = B::stmt_from_node(statement); - self.js.stmt_kind(&self.arena, &statement) == Kind::Declare + fn declares_pure(&self, statement: &B::S) -> bool { + self.js.stmt_kind(&self.arena, statement) == Kind::Declare && self .js - .value_of(&self.arena, &statement) + .value_of(&self.arena, statement) .is_none_or(|value| self.js.runs_no_code(&self.arena, value)) } /// A:96 defines_pure_values: each statement is examined once by sequencing. /// Cost: O(statements), each `declares_pure`. fn defines_pure_values(&self, statements: &[NodeId]) -> bool { - statements.iter().all(|&statement| self.declares_pure(statement)) + statements.iter().all(|&statement| self.declares_pure(&B::stmt_from_node(statement))) } /// X:1445 fresh_ident: unit-local naming order, without a global counter. @@ -200,11 +199,11 @@ impl State<'_, B> { later_defines_pure &= self.defines_pure_values(&self.evaluation.statements[held.start as usize..(held.start + held.len) as usize]) && self .statements - .statements(arg.stmts) + .statement_refs(arg.stmts) .all(|statement| self.declares_pure(statement)); if held.len != 0 { self.statements - .fill_held(arg.stmts.start + arg.stmts.len - 1, &self.evaluation.statements[held.start as usize..(held.start + held.len) as usize]); + .fill_held(arg.stmts.start + arg.stmts.len - 1, self.evaluation.statements[held.start as usize..(held.start + held.len) as usize].iter().map(|&row| B::stmt_from_node(row))); } self.evaluation.statements.truncate(held.start as usize); values.push(value); @@ -442,7 +441,7 @@ mod tests { ); let statement = state.arena.new_node(View::SExp { value }, 0); let start = state.statements.mark(); - state.statements.push(statement); + state.statements.push(::stmt_from_node(statement)); state.statements.reserve(); let text = state.arena.intern_text("v"); let value = state.pure_expression(View::JStr { text }, 0); diff --git a/compiler-rust/src/bir/lower/assign.rs b/compiler-rust/src/bir/lower/assign.rs index c3103befdc..a2d8aebfd7 100644 --- a/compiler-rust/src/bir/lower/assign.rs +++ b/compiler-rust/src/bir/lower/assign.rs @@ -17,7 +17,7 @@ impl State<'_, B> { None => self.js.undefined(&mut self.arena, false), }; let statement = self.js.assign(&mut self.arena, var, value); - self.statements.push(B::stmt_node(statement)); + self.statements.push(statement); let unit = self.js.undefined(&mut self.arena, false); let tail = self.leaf(cx, B::node(unit)); out.stmts = self.statements.since(Span { diff --git a/compiler-rust/src/bir/lower/cond.rs b/compiler-rust/src/bir/lower/cond.rs index 35ce8744e7..722e6f3985 100644 --- a/compiler-rust/src/bir/lower/cond.rs +++ b/compiler-rust/src/bir/lower/cond.rs @@ -64,25 +64,33 @@ struct Parts { pub(super) const EMPTY: Span = Span { start: 0, len: 0 }; -impl norm::IfLists for super::out::StatementStack { +/// The statement stack with the implementation, as `norm::simplify_if_head` +/// reads and takes branches. Two references, 16 bytes. Transition: deleted +/// by T6x-a3, when the stack implements `Runs`. +struct StackLists<'s, B: super::js_stmt::JsStmt> { + stack: &'s mut super::out::StatementStack, + js: &'s mut B, +} + +impl norm::IfLists for StackLists<'_, B> { type Handle = Span; /// O(recorded empty blocks), with no retained-tail scan. - fn trim(&mut self, b: &mut Bir, span: Span) { self.drop_empty_blocks(b, span); } + fn trim(&mut self, b: &mut Bir, span: Span) { self.stack.drop_empty_blocks(self.js, b, span); } /// O(1): a zero-length run. fn empty(&mut self, _: &mut Bir) -> Span { EMPTY } /// O(1) on trimmed runs, plus counted dead-slot skips. - fn last(&mut self, _: &Bir, span: Span) -> Option { self.last(span) } + fn last(&mut self, _: &Bir, span: Span) -> Option { self.stack.last(span) } /// O(1) on trimmed runs, plus counted dead-slot skips. fn only(&mut self, _: &Bir, span: Span) -> Option { - self.only(span).map(|(_, statement)| statement) + self.stack.only(span).map(|(_, statement)| statement) } /// O(released nodes), leaving other stack runs in place. fn release(&mut self, b: &mut Bir, span: Span) { - for statement in self.statements(span) { b.release(statement); } - self.kill(span); + for statement in self.stack.statements(span) { b.release(statement); } + self.stack.kill(span); } - /// O(run length): copy each surviving statement into the pool once. - fn take(&mut self, b: &mut Bir, span: Span) -> ListId { self.take_list(b, span) } + /// O(run length): move each surviving statement into the pool once. + fn take(&mut self, b: &mut Bir, span: Span) -> ListId { B::list_id(self.stack.take_list(self.js, b, span)) } } impl State<'_, B> { @@ -364,8 +372,7 @@ impl State<'_, B> { pub(super) fn place(&mut self, slot: u32, statement: NodeId) { let handle = B::stmt_from_node(statement); let empty = self.js.is_empty_block(&self.arena, &handle); - let statement = B::stmt_node(handle); - self.statements.fill(slot, statement); + self.statements.fill(slot, handle); if empty { self.statements.note_empty_block(slot, statement); } @@ -460,24 +467,25 @@ impl State<'_, B> { /// arena once; all rules receive pool handles, never branch vectors. /// Cost: O(yes + no) statements copied, plus J-S3's. pub(super) fn if_statement(&mut self, test: NodeId, yes: Span, no: Span) -> NodeId { - self.statements.drop_empty_blocks(&mut self.arena, yes); - self.statements.drop_empty_blocks(&mut self.arena, no); + self.statements.drop_empty_blocks(&mut self.js, &mut self.arena, yes); + self.statements.drop_empty_blocks(&mut self.js, &mut self.arena, no); let (yes, no) = if yes.start >= no.start { - let yes = self.statements.take_list(&mut self.arena, yes); - (yes, self.statements.take_list(&mut self.arena, no)) + let yes = self.statements.take_list(&mut self.js, &mut self.arena, yes); + (yes, self.statements.take_list(&mut self.js, &mut self.arena, no)) } else { - let no = self.statements.take_list(&mut self.arena, no); - (self.statements.take_list(&mut self.arena, yes), no) + let no = self.statements.take_list(&mut self.js, &mut self.arena, no); + (self.statements.take_list(&mut self.js, &mut self.arena, yes), no) }; - let statement = self.js.if_(&mut self.arena, B::from_node(test), B::list_from(yes), B::list_from(no)); + let statement = self.js.if_(&mut self.arena, B::from_node(test), yes, no); B::stmt_node(statement) } /// Shared J-S3 rules 0..6 leave a retained tail on the statement stack. /// O(consumed statements + test negations), never the untouched tail. fn if_in_place(&mut self, head: u32, mut test: NodeId, mut yes: Span, mut no: Span) { + let mut lists = StackLists { stack: &mut self.statements, js: &mut self.js }; if let Some(result) = norm::simplify_if_head( - &mut self.arena, &mut self.statements, &mut test, &mut yes, &mut no, + &mut self.arena, &mut lists, &mut test, &mut yes, &mut no, ) { if let Some(statement) = result.statement { self.place(head, statement); @@ -495,12 +503,13 @@ impl State<'_, B> { if self.statements.only(run).is_some() { return; } - let list = self.statements.take_list(&mut self.arena, run); - let block = self.js.explicit_block(&mut self.arena, B::list_from(list)); - let empty = self.js.is_empty_block(&self.arena, &block); - let block = B::stmt_node(block); + let list = self.statements.take_list(&mut self.js, &mut self.arena, run); + let block = self.js.explicit_block(&mut self.arena, list); + // The empty-block table is keyed by the statement's id (on rows its + // row), read before the stack takes the handle. + let empty = self.js.is_empty_block(&self.arena, &block).then(|| NodeId(self.js.stmt_id(&self.arena, &block))); self.statements.push(block); - if empty { + if let Some(block) = empty { self.statements.note_empty_block(head, block); } } @@ -518,7 +527,7 @@ impl State<'_, B> { } self.statements.clear(slot); let statements = &mut self.statements; - self.js.unblock(&mut self.arena, block, |s| statements.push(B::stmt_node(s))); + self.js.unblock(&mut self.arena, block, |s| statements.push(s)); } } @@ -543,7 +552,7 @@ impl State<'_, B> { /// a pure test released. fn push_s_if_empty(&mut self, test: NodeId) { let statements = &mut self.statements; - self.js.effect_if_impure(&mut self.arena, B::from_node(test), |s| statements.push(B::stmt_node(s))); + self.js.effect_if_impure(&mut self.arena, B::from_node(test), |s| statements.push(s)); } /// Rows R1 to R3 on the two runs that assign `target`. Cost: @@ -562,7 +571,7 @@ impl State<'_, B> { let statement = self.if_statement(test, yes, no); self.place(parts.head, statement); let declaration = self.js.declare(&mut self.arena, LetKind::Strict, target, None); - self.statements.fill(parts.head - 1, B::stmt_node(declaration)); + self.statements.fill(parts.head - 1, declaration); let value = self.js.var(&mut self.arena, target); return Probed::Out(self.output(parts.start, Some(B::node(value)), Finished::False)); } @@ -571,14 +580,14 @@ impl State<'_, B> { // R2, C:5049 compile_declared_if_block. (Cont::Declare(kind, _), Ok(value)) => { let statement = self.js.declare(&mut self.arena, kind, target, Some(B::from_node(value))); - self.statements.fill(parts.head - 1, B::stmt_node(statement)); + self.statements.fill(parts.head - 1, statement); self.statements.trim(Self::from(parts.head)); } (Cont::Declare(kind, _), Err((test, yes, no))) => { let statement = self.if_statement(test, yes, no); self.place(parts.head, statement); let declaration = self.js.declare(&mut self.arena, kind, target, None); - self.statements.fill(parts.head - 1, B::stmt_node(declaration)); + self.statements.fill(parts.head - 1, declaration); } // R3, C:5018 compile_test_assignment_block. (_, merged) => { @@ -712,14 +721,13 @@ impl State<'_, B> { }; let mark = self.statements.mark(); let statement = self.js.exp_rule(&mut self.arena, value); - self.statements.push(B::stmt_node(statement)); + self.statements.push(statement); let run = self.statements.since(mark); let statement = B::stmt_from_node(self.if_statement(B::node(test), run, EMPTY)); - let empty = self.js.is_empty_block(&self.arena, &statement); - let statement = B::stmt_node(statement); + let empty = self.js.is_empty_block(&self.arena, &statement).then(|| NodeId(self.js.stmt_id(&self.arena, &statement))); let slot = self.statements.mark().start; self.statements.push(statement); - if empty { + if let Some(statement) = empty { self.statements.note_empty_block(slot, statement); } } @@ -751,7 +759,7 @@ impl State<'_, B> { self.kill(yes, no); let value = self.js.cond(&mut self.arena, B::from_node(test), B::from_node(then_), B::from_node(else_)); let statement = self.js.return_(&mut self.arena, value); - self.statements.fill(parts.head, B::stmt_node(statement)); + self.statements.fill(parts.head, statement); return Probed::Out(self.output(parts.start, None, Finished::True)); } // Step 3, C:5368 compile_test_return_block_with_fallback. @@ -823,14 +831,14 @@ impl State<'_, B> { if let Some(empty) = empty { self.js.release_stmt(&mut self.arena, empty); } - let rest = self.statements.take_list(&mut self.arena, run); - let else_ = self.js.block_of(&mut self.arena, B::list_from(rest)); + let rest = self.statements.take_list(&mut self.js, &mut self.arena, run); + let else_ = self.js.block_of(&mut self.arena, rest); let statement = self.js.if_blocks(&mut self.arena, condition, then_, else_); let list = self.js.list(&mut self.arena, 1, std::iter::once(statement)); B::stmt_node(self.js.block_of(&mut self.arena, list)) } else { - let list = self.statements.take_list(&mut self.arena, run); - B::stmt_node(self.js.block_of(&mut self.arena, B::list_from(list))) + let list = self.statements.take_list(&mut self.js, &mut self.arena, run); + B::stmt_node(self.js.block_of(&mut self.arena, list)) } } @@ -922,7 +930,7 @@ impl State<'_, B> { self.js.release(&mut self.arena, B::from_node(value)); } else { let statement = self.js.assign(&mut self.arena, target, B::from_node(value)); - self.statements.push(B::stmt_node(statement)); + self.statements.push(statement); } } let yes = self.statements.since(Self::from(right.stmts.start)); @@ -931,7 +939,7 @@ impl State<'_, B> { Cont::Assign(_) => { let mark = self.statements.mark(); let statement = self.js.assign(&mut self.arena, target, default); - self.statements.push(B::stmt_node(statement)); + self.statements.push(statement); self.statements.since(mark) } Cont::Declare(..) | Cont::Value(_) | Cont::Effect(_) => { @@ -942,7 +950,7 @@ impl State<'_, B> { LetKind::Strict }; let statement = self.js.declare(&mut self.arena, kind, target, Some(default)); - self.statements.fill(parts.head - 1, B::stmt_node(statement)); + self.statements.fill(parts.head - 1, statement); EMPTY } }; @@ -983,7 +991,7 @@ impl State<'_, B> { let merged = self.merge_assignments(test, target, yes.stmts, no.stmts); let statement = self.assignment_statement(target, merged); self.statements.trim(Self::from(start)); - self.statements.push(statement); + self.statements.push(B::stmt_from_node(statement)); self.output(start, None, Finished::False) } Cont::Value(_) | Cont::Declare(..) | Cont::Effect(Tail::Return(_)) => { @@ -1641,7 +1649,7 @@ mod tests { for kind in kinds.chars() { let statement = self.statement(kind); let slot = self.state.statements.mark().start; - self.state.statements.push(statement); + self.state.statements.push(::stmt_from_node(statement)); if kind == 'o' { self.state.statements.note_empty_block(slot, statement); } @@ -1679,7 +1687,7 @@ mod tests { let (yes, no) = (hand.list(0, yes), hand.list(1, no)); let test = hand.test(test); let statement = pooled_if(&mut hand.state.arena, test, yes, no); - hand.state.statements.push(statement); + hand.state.statements.push(::stmt_from_node(statement)); hand.text(None) } @@ -1722,7 +1730,7 @@ mod tests { let (yes_run, no_run) = (hand.run(0, yes), hand.run(1, no)); let condition = hand.test(test); let statement = hand.state.if_statement(condition, yes_run, no_run); - hand.state.statements.push(statement); + hand.state.statements.push(::stmt_from_node(statement)); assert_eq!( hand.text(None), reference(test, yes, no), @@ -1753,7 +1761,7 @@ mod tests { vec![pooled_if(arena, test, then_, else_)] }; for statement in statements { - hand.state.statements.push(statement); + hand.state.statements.push(::stmt_from_node(statement)); } hand.text(None) } diff --git a/compiler-rust/src/bir/lower/exit.rs b/compiler-rust/src/bir/lower/exit.rs index 5da3d95f14..1d7d43dad8 100644 --- a/compiler-rust/src/bir/lower/exit.rs +++ b/compiler-rust/src/bir/lower/exit.rs @@ -184,7 +184,7 @@ impl State<'_, B> { /// one declaration built and pushed. pub(super) fn declare_empty(&mut self, kind: LetKind, var: VarId) { let declaration = self.js.declare(&mut self.arena, kind, var, None); - self.statements.push(B::stmt_node(declaration)); + self.statements.push(declaration); } /// 4.1 `lower_catch` on entering the node (OCaml @@ -247,7 +247,7 @@ impl State<'_, B> { let exit = self.fresh_variable("exit"); let zero = self.js.int_literal(&mut self.arena, 0); let declaration = self.js.declare(&mut self.arena, LetKind::Variable, exit, Some(zero)); - self.statements.push(B::stmt_node(declaration)); + self.statements.push(declaration); (target.map(|(_, var)| var), Some(exit)) }; // 4.2 invariant 3: a table starts above every visible entry. @@ -345,8 +345,8 @@ impl State<'_, B> { // break only when the handler holds an exit (4.2 invariant 5). let should_break = finished != Finished::True && (!matches!(cx.cont, Cont::Effect(Tail::Return(_))) || self.has_exit(handler)); - let list = self.statements.take_list(&mut self.arena, run); - let block = self.js.block_of(&mut self.arena, B::list_from(list)); + let list = self.statements.take_list(&mut self.js, &mut self.arena, run); + let block = self.js.block_of(&mut self.arena, list); let plan = self.catches[frame.0 as usize].as_mut().expect("live catch frame"); let slot = plan.output.start as usize + index - 1; self.switch_output_nodes[slot] = B::stmt_node(block); @@ -420,7 +420,7 @@ impl State<'_, B> { let order = self.js.int_literal(&mut self.arena, plan.first); let test = self.js.bin(&mut self.arena, crate::js_ir::BinOp::EqEqEq, exit, order); let statement = self.if_statement(B::node(test), run, super::cond::EMPTY); - self.statements.push(statement); + self.statements.push(B::stmt_from_node(statement)); return; } let keys = crate::bir::KeySpan { start: self.arena.pools.switch_keys.len() as u32, len: plan.len }; @@ -455,7 +455,7 @@ impl State<'_, B> { }); let scrutinee = self.js.var(&mut self.arena, exit); let statement = self.js.switch(&mut self.arena, scrutinee, B::list_from(actions), header, false); - self.statements.push(B::stmt_node(statement)); + self.statements.push(statement); } /// 4.1 `lower_exit` on entering the node (OCaml @@ -561,7 +561,7 @@ impl State<'_, B> { if jump.order != CHEAP { let order = self.js.int_literal(&mut self.arena, jump.order); let statement = self.js.assign(&mut self.arena, jump.exit, order); - self.statements.push(B::stmt_node(statement)); + self.statements.push(statement); } Finished::False } @@ -580,7 +580,7 @@ impl State<'_, B> { if finished != Finished::True { let undefined = self.js.undefined(&mut self.arena, false); let statement = self.js.declare(&mut self.arena, kind, var, Some(undefined)); - self.statements.push(B::stmt_node(statement)); + self.statements.push(statement); } None } diff --git a/compiler-rust/src/bir/lower/export.rs b/compiler-rust/src/bir/lower/export.rs index 8b49046b37..97d8fba69f 100644 --- a/compiler-rust/src/bir/lower/export.rs +++ b/compiler-rust/src/bir/lower/export.rs @@ -272,7 +272,7 @@ impl State<'_, B> { /// allocation once the stack has its capacity. fn push_effect(&mut self, value: NodeId) { let statements = &mut self.statements; - self.js.effect(&mut self.arena, B::from_node(value), |s| statements.push(B::stmt_node(s))); + self.js.effect(&mut self.arena, B::from_node(value), |s| statements.push(s)); } /// The variables of the export block `exports`, as the importer recorded diff --git a/compiler-rust/src/bir/lower/expr.rs b/compiler-rust/src/bir/lower/expr.rs index b5acb48319..4e07d3dfe3 100644 --- a/compiler-rust/src/bir/lower/expr.rs +++ b/compiler-rust/src/bir/lower/expr.rs @@ -280,8 +280,8 @@ impl State<'_, B> { unreachable!("return was realized") }; assert!(body.value.is_none()); - let list = self.statements.take_list(&mut self.arena, body.stmts); - let body = self.js.block_of(&mut self.arena, B::list_from(list)); + let list = self.statements.take_list(&mut self.js, &mut self.arena, body.stmts); + let body = self.js.block_of(&mut self.arena, list); let body = match loop_ { Some(frame) => B::stmt_from_node(self.loop_function_body(frame, header, B::stmt_node(body))), None => body, @@ -308,7 +308,7 @@ impl State<'_, B> { fn hold_effect(&mut self, slot: u32, value: NodeId) { let mut held = std::mem::take(&mut self.scratch); self.js.effect(&mut self.arena, B::from_node(value), |s| held.push(B::stmt_node(s))); - self.statements.fill_held(slot, &held); + self.statements.fill_held(slot, held.iter().map(|&row| B::stmt_from_node(row))); held.clear(); self.scratch = held; } @@ -318,7 +318,7 @@ impl State<'_, B> { /// decision reads one cached flag. pub(super) fn drop_pure_effect(&mut self, value: NodeId) { let statements = &mut self.statements; - self.js.effect(&mut self.arena, B::from_node(value), |s| statements.push(B::stmt_node(s))); + self.js.effect(&mut self.arena, B::from_node(value), |s| statements.push(s)); } } diff --git a/compiler-rust/src/bir/lower/external.rs b/compiler-rust/src/bir/lower/external.rs index a7fae68e66..a532b2d999 100644 --- a/compiler-rust/src/bir/lower/external.rs +++ b/compiler-rust/src/bir/lower/external.rs @@ -33,7 +33,7 @@ impl State<'_, B> { self.js.declare(&mut self.arena, LetKind::Strict, var, Some(B::from_node(value))); self.statements.fill_held( arg.stmts.start + arg.stmts.len - 1, - &[B::stmt_node(statement)], + [statement], ); let mut held = self.js.var(&mut self.arena, var); self.js.assume_strict(&mut self.arena, &mut held); @@ -178,7 +178,7 @@ impl State<'_, B> { let obj = self.fresh_variable("tmp"); let declaration = self.js.declare(&mut self.arena, LetKind::Variable, obj, Some(B::from_node(object))); - self.statements.push(B::stmt_node(declaration)); + self.statements.push(declaration); for (i, mut arg) in pending.drain(..) { self.primitive_steps += 1; let spec = &specs[i as usize]; @@ -193,7 +193,7 @@ impl State<'_, B> { let tmp = self.fresh_variable("tmp"); let declaration = self.js.declare(&mut self.arena, LetKind::Strict, tmp, Some(B::from_node(arg))); - self.statements.push(B::stmt_node(declaration)); + self.statements.push(declaration); let mut held = self.js.var(&mut self.arena, tmp); self.js.assume_strict(&mut self.arena, &mut held); arg = B::node(held); @@ -227,7 +227,7 @@ impl State<'_, B> { let yes = self.js.list(&mut self.arena, 1, std::iter::once(statement)); let no = self.js.list(&mut self.arena, 0, std::iter::empty()); let branch = self.js.if_(&mut self.arena, test, yes, no); - self.statements.push(B::stmt_node(branch)); + self.statements.push(branch); } self.external_pending = pending; let mut object = self.js.var(&mut self.arena, obj); diff --git a/compiler-rust/src/bir/lower/ffi_args.rs b/compiler-rust/src/bir/lower/ffi_args.rs index 4c84daa0fa..c30515a4da 100644 --- a/compiler-rust/src/bir/lower/ffi_args.rs +++ b/compiler-rust/src/bir/lower/ffi_args.rs @@ -99,7 +99,7 @@ impl State<'_, B> { let statement = self.js.declare(&mut self.arena, LetKind::Strict, var, Some(value)); self.statements.fill_held( arg.stmts.start + arg.stmts.len - 1, - &[B::stmt_node(statement)], + [statement], ); let mut replacement = self.js.var(&mut self.arena, var); self.js.assume_strict(&mut self.arena, &mut replacement); diff --git a/compiler-rust/src/bir/lower/loops.rs b/compiler-rust/src/bir/lower/loops.rs index df4de094d2..15ba0c4e86 100644 --- a/compiler-rust/src/bir/lower/loops.rs +++ b/compiler-rust/src/bir/lower/loops.rs @@ -83,7 +83,7 @@ impl State<'_, B> { self.loops[frame.0 as usize].values[index] = out.value; } let span = self.statements.since(Span { start: out.stmts.start, len: 0 }); - let list = self.statements.take_list(&mut self.arena, span); + let list = B::list_id(self.statements.take_list(&mut self.js, &mut self.arena, span)); self.loops[frame.0 as usize].blocks[index] = Some(list); if index == 0 { let plan = &self.loops[frame.0 as usize]; @@ -111,7 +111,7 @@ impl State<'_, B> { }; let start = self.statements.mark(); let statement = if continue_ { self.js.continue_(&mut self.arena, label) } else { self.js.break_(&mut self.arena, label) }; - self.statements.push(B::stmt_node(statement)); + self.statements.push(statement); let value = self.js.undefined(&mut self.arena, false); Ok(Probed::Out(Out { stmts: self.statements.since(start), value: Some(B::node(value)), finished: Finished::True })) } @@ -122,7 +122,7 @@ impl State<'_, B> { let list = B::list_from(list); for index in 0..self.js.len(&self.arena, list) { let statement = self.js.take_at(&mut self.arena, list, index); - self.statements.push(B::stmt_node(statement)); + self.statements.push(statement); } } @@ -147,17 +147,17 @@ impl State<'_, B> { let negated = self.js.not(&mut self.arena, end); let mark = self.statements.mark(); let break_ = self.js.break_(&mut self.arena, None); - self.statements.push(B::stmt_node(break_)); + self.statements.push(break_); let yes = self.statements.since(mark); let no = self.statements.mark(); let guard = self.if_statement(B::node(negated), yes, no); let mark = self.statements.mark(); self.push_loop_list(blocks[0].unwrap()); - self.statements.push(guard); + self.statements.push(B::stmt_from_node(guard)); self.push_loop_list(blocks[1].unwrap()); - let list = self.statements.take_list(&mut self.arena, self.statements.since(mark)); + let list = self.statements.take_list(&mut self.js, &mut self.arena, self.statements.since(mark)); let condition = self.js.boolean(&mut self.arena, true); - self.js.while_(&mut self.arena, condition, B::list_from(list), label) + self.js.while_(&mut self.arena, condition, list, label) } else { self.js.while_(&mut self.arena, end, B::list_from(blocks[1].unwrap()), label) } @@ -177,7 +177,7 @@ impl State<'_, B> { && !self.js.has(&self.arena, B::of(&init), flag::DELAY_SAFE); let init = if hold { let declaration = self.js.declare(&mut self.arena, LetKind::Variable, var, Some(init)); - self.statements.push(B::stmt_node(declaration)); + self.statements.push(declaration); None } else { Some(init) @@ -186,7 +186,7 @@ impl State<'_, B> { let range = Range { init, end, direction }; self.js.for_range(&mut self.arena, var, range, B::list_from(blocks[2].unwrap()), label) }; - self.statements.push(B::stmt_node(statement)); + self.statements.push(statement); let unit = self.js.undefined(&mut self.arena, false); let mut out = self.leaf(cx, B::node(unit)); out.stmts = self.statements.since(start); diff --git a/compiler-rust/src/bir/lower/mod.rs b/compiler-rust/src/bir/lower/mod.rs index 8bccb49265..5fb283e6a0 100644 --- a/compiler-rust/src/bir/lower/mod.rs +++ b/compiler-rust/src/bir/lower/mod.rs @@ -488,7 +488,7 @@ struct State<'a, B: js_stmt::JsStmt = crate::bir::j::rows::Rows> { source: Bir, arena: Bir, idents: Vec, - statements: StatementStack, + statements: StatementStack, free: FreePool, traversal: SourceProbe, list_block_info: Option, @@ -806,7 +806,7 @@ impl State<'_, B> { (list, coerced) } _ => ( - self.statements.take_list(&mut self.arena, run), + B::list_id(self.statements.take_list(&mut self.js, &mut self.arena, run)), export::Coerced::default(), ), }; @@ -822,12 +822,12 @@ impl State<'_, B> { .value .unwrap_or_else(|| self.pure_expression(View::JUndefined, 0)); let value = self.arena.new_node(View::SExp { value }, 0); - self.statements.push(value); + self.statements.push(B::stmt_from_node(value)); let run = self.statements.since(Span { start: out.stmts.start, len: 0, }); - let list = self.statements.take_list(&mut self.arena, run); + let list = B::list_id(self.statements.take_list(&mut self.js, &mut self.arena, run)); self.finish(list, out.finished, export::Coerced::default()) } @@ -1140,6 +1140,7 @@ impl ProbeRules for State<'_, B> { impl Realizer for State<'_, B> { /// K4/K5/K6: C:6648 declare_compiled, C:6667 assign_compiled, C:6658 return_compiled. + /// O(1): at most one statement built and pushed. fn leaf(&mut self, cx: Cx, value: NodeId) -> Out { let start = self.statements.mark(); let mut finished = Finished::False; @@ -1149,17 +1150,17 @@ impl Realizer for State<'_, B> { } Cont::Declare(kind, var) => { let stmt = super::j::declare(&mut self.arena, kind, var, value); - self.statements.push(stmt); + self.statements.push(B::stmt_from_node(stmt)); None } Cont::Assign(var) => { let stmt = super::j::assign(&mut self.arena, var, value); - self.statements.push(stmt); + self.statements.push(B::stmt_from_node(stmt)); None } Cont::Effect(Tail::Return(_)) => { let stmt = super::j::return_value(&mut self.arena, value); - self.statements.push(stmt); + self.statements.push(B::stmt_from_node(stmt)); finished = Finished::True; None } diff --git a/compiler-rust/src/bir/lower/oracle_tests.rs b/compiler-rust/src/bir/lower/oracle_tests.rs index 2b922a39bd..f9760bd948 100644 --- a/compiler-rust/src/bir/lower/oracle_tests.rs +++ b/compiler-rust/src/bir/lower/oracle_tests.rs @@ -1446,7 +1446,7 @@ fn a_copied_function_doubles_the_legacy_count_of_the_variables_declared_inside_i let out = state.realize(Cx::value(), probed); let copy = crate::bir::j::logic::copy_j(&mut state.arena, out.value.unwrap()); let statement = state.arena.new_node(View::SExp { value: copy }, 0); - state.statements.push(statement); + state.statements.push(::stmt_from_node(statement)); let lowered = state.complete(Cx::value(), Probed::Out(out)); lowered.program.arena.validate(Some(lowered.program.root)).unwrap(); let scope = lowered.program.arena.js_scope.as_ref().unwrap(); @@ -1596,7 +1596,7 @@ fn new_text(build: impl Fn(&mut LamArena) -> Lam, resolver: Option<&crate::js_ir // As `bir_lower_oracle`: the statements, without a value. let out = state.realize(cx, probed); let run = state.statements.since(Span { start: out.stmts.start, len: 0 }); - let list = state.statements.take_list(&mut state.arena, run); + let list = ::list_id(state.statements.take_list(&mut state.js, &mut state.arena, run)); let lowered = state.finish(list, out.finished, Default::default()); lowered.program.arena.validate(Some(lowered.program.root)).unwrap(); let bridge_j::Exported { mut program, qualified } = bridge_j::export_lowered(&lowered.program).unwrap(); diff --git a/compiler-rust/src/bir/lower/out.rs b/compiler-rust/src/bir/lower/out.rs index 5e1adba386..367cf869c3 100644 --- a/compiler-rust/src/bir/lower/out.rs +++ b/compiler-rust/src/bir/lower/out.rs @@ -1,4 +1,6 @@ //! K0 output algebra: C:89–135 BlockOutput, BACKEND_LOWERING §§1.3–1.5. +use super::js::Pools; +use super::js_stmt::JsStmt; use super::{Cx, ProbeInput, Realizer}; use crate::bir::{Bir, ListId, NodeId}; @@ -38,19 +40,21 @@ pub struct Out { } #[derive(Debug, PartialEq, Eq)] -enum Slot { - Statement(NodeId), +enum Slot { + Statement(S), Held(Span), /// A run consumed below the top: its first slot holds the run's length /// and every other slot 1, so a forward scan crosses the run in one step /// and a backward trim pops it slot by slot. Dead(u32), } -/// One stack per lowering. A hole is distinct from any valid node handle. -#[derive(Default)] -pub struct StatementStack { - slots: Vec>, - held: Vec, +/// One stack per lowering. A hole is distinct from any statement. The slots +/// own the statement handles of the runs (THREE_TREES 11.4, "T6x-a: the +/// statement stack, after the probe"). +pub struct StatementStack { + slots: Vec>>, + /// Held statements; one that was moved out leaves `None`. + held: Vec>, /// Statements written, excluding holes. pub pushed: usize, /// Statements copied into final lists. @@ -61,7 +65,13 @@ pub struct StatementStack { /// stale once its slot was consumed; `drop_empty_blocks` checks the slot. empties: Vec<(u32, NodeId)>, } -impl StatementStack { +impl Default for StatementStack { + /// An empty stack without capacity. O(1); allocates nothing. + fn default() -> Self { + Self { slots: Vec::new(), held: Vec::new(), pushed: 0, copied: 0, scanned: 0, empties: Vec::new() } + } +} +impl StatementStack { /// Reserve statement and hole slots from the imported output-node estimate. /// Cost: Reserves O(N) statement/hole slots for the imported output estimate N; no /// statements are visited or initialized. @@ -77,14 +87,23 @@ impl StatementStack { [self.slots.capacity(), self.held.capacity(), self.empties.capacity()] } + /// The row of a statement the stack holds, the handle staying + /// in place. O(1). Transition: deleted by T6x-a3. + fn peek(s: &mut B::S) -> NodeId { + let id = B::stmt_node(std::mem::replace(s, B::stmt_from_node(NodeId(u32::MAX)))); + *s = B::stmt_from_node(id); + id + } + /// K0: C:89 BlockOutput::make, start an empty output at the current top. + /// O(1). pub fn mark(&self) -> Span { Span { start: u32::try_from(self.slots.len()).expect("statement stack capacity"), len: 0, } } - /// K0: C:89 BlockOutput::make, complete the run since a saved mark. + /// K0: C:89 BlockOutput::make, complete the run since a saved mark. O(1). pub fn since(&self, mark: Span) -> Span { assert!(mark.end() <= self.slots.len()); Span { @@ -93,50 +112,133 @@ impl StatementStack { .expect("statement span capacity"), } } - /// A:191 sequence_arguments: skip reserved holes without copying statements. - pub(super) fn statements(&self, span: Span) -> impl Iterator + '_ { + /// The statements of a run, read in place, in order. Cost: O(slots of + /// the run + held statements): one step per slot, a dead range crossed + /// in one, and one per held statement. + pub(super) fn statement_refs(&self, span: Span) -> impl Iterator + '_ { let (mut at, end) = (span.start as usize, span.end()); - std::iter::from_fn(move || { - while at < end { - let slot = &self.slots[at]; - at += match slot { - Some(Slot::Dead(len)) => *len as usize, - _ => 1, - }; - match slot { - Some(Slot::Statement(node)) => return Some(std::slice::from_ref(node)), - Some(Slot::Held(span)) => { - return Some(&self.held[span.start as usize..span.end()]); - } - Some(Slot::Dead(_)) | None => {} - } + let (mut inner, mut inner_end) = (0usize, 0usize); + std::iter::from_fn(move || loop { + if inner < inner_end { + inner += 1; + return Some(self.held[inner - 1].as_ref().expect("held statement")); + } + if at >= end { + return None; + } + let slot = &self.slots[at]; + at += match slot { + Some(Slot::Dead(len)) => *len as usize, + _ => 1, + }; + match slot { + Some(Slot::Statement(s)) => return Some(s), + Some(Slot::Held(span)) => (inner, inner_end) = (span.start as usize, span.end()), + Some(Slot::Dead(_)) | None => {} + } + }) + } + /// A:191 sequence_arguments: the rows of a run, in order, skipping + /// reserved holes without copying statements. Cost: O(slots of the run + /// + held statements), each statement read through `peek`. + pub(super) fn statements(&mut self, span: Span) -> impl Iterator + '_ { + let (mut at, end) = (span.start as usize, span.end()); + let (mut inner, mut inner_end) = (0usize, 0usize); + std::iter::from_fn(move || loop { + if inner < inner_end { + inner += 1; + return Some(Self::peek(self.held[inner - 1].as_mut().expect("held statement"))); + } + if at >= end { + return None; + } + let current = at; + at += match self.slots[current] { + Some(Slot::Dead(len)) => len as usize, + _ => 1, + }; + match &mut self.slots[current] { + Some(Slot::Statement(s)) => return Some(Self::peek(s)), + Some(Slot::Held(span)) => (inner, inner_end) = (span.start as usize, span.end()), + Some(Slot::Dead(_)) | None => {} + } + }) + } + /// C:5241 try_fold_effect_only_return_tail folds a run from its end: + /// the rows of a run, last first. Cost: O(slots of the run + held + /// statements): one step per slot and per held statement. + pub(super) fn statements_rev(&mut self, span: Span) -> impl Iterator + '_ { + let (start, mut at) = (span.start as usize, span.end()); + let (mut inner_start, mut inner) = (0usize, 0usize); + std::iter::from_fn(move || loop { + if inner > inner_start { + inner -= 1; + return Some(Self::peek(self.held[inner].as_mut().expect("held statement"))); + } + if at <= start { + return None; + } + at -= 1; + match &mut self.slots[at] { + Some(Slot::Statement(s)) => return Some(Self::peek(s)), + Some(Slot::Held(span)) => (inner_start, inner) = (span.start as usize, span.end()), + Some(Slot::Dead(_)) | None => {} } - None }) - .flatten() - .copied() - } - /// C:5241 try_fold_effect_only_return_tail folds a run from its end. - /// One step per slot of the run. - pub(super) fn statements_rev(&self, span: Span) -> impl Iterator + '_ { - (span.start as usize..span.end()) - .rev() - .flat_map(|at| self.slot(at).iter().rev()) - .copied() - } - /// The statements of one slot: none for a hole, a dead slot or an empty hold. - fn slot(&self, at: usize) -> &[NodeId] { + } + /// The statements of a run moved out of their slots, in order; a slot + /// or a hold that gave its statements is left a hole, and the caller + /// kills the run. Cost: O(slots of the run + held statements), each + /// statement moved once. + fn take_run(&mut self, span: Span) -> impl Iterator + '_ { + let (mut at, end) = (span.start as usize, span.end()); + let (mut inner, mut inner_end) = (0usize, 0usize); + std::iter::from_fn(move || loop { + if inner < inner_end { + inner += 1; + return Some(self.held[inner - 1].take().expect("held statement")); + } + if at >= end { + return None; + } + let current = at; + at += match self.slots[current] { + Some(Slot::Dead(len)) => len as usize, + _ => 1, + }; + match self.slots[current].take() { + Some(Slot::Statement(s)) => return Some(s), + Some(Slot::Held(span)) => (inner, inner_end) = (span.start as usize, span.end()), + other => self.slots[current] = other, + } + }) + } + /// The number of statements of one slot: none for a hole, a dead slot or + /// an empty hold. O(1). + fn slot_len(&self, at: usize) -> usize { match &self.slots[at] { - Some(Slot::Statement(node)) => std::slice::from_ref(node), - Some(Slot::Held(span)) => &self.held[span.start as usize..span.end()], - Some(Slot::Dead(_)) | None => &[], + Some(Slot::Statement(_)) => 1, + Some(Slot::Held(span)) => span.len as usize, + Some(Slot::Dead(_)) | None => 0, + } + } + /// The row of statement `index` of slot `at`, which holds more than + /// `index` statements, read through `peek`. O(1). + fn slot_row(&mut self, at: usize, index: usize) -> NodeId { + match &mut self.slots[at] { + Some(Slot::Statement(s)) => Self::peek(s), + Some(Slot::Held(span)) => { + let i = span.start as usize + index; + Self::peek(self.held[i].as_mut().expect("held statement")) + } + Some(Slot::Dead(_)) | None => unreachable!("a slot without a statement"), } } /// The first slot of `[at, end)` that holds a statement. One step per /// hole, empty hold or dead range before it, counted in `scanned`. fn live(&mut self, mut at: usize, end: usize) -> Option { while at < end { - if !self.slot(at).is_empty() { + if self.slot_len(at) != 0 { return Some(at); } self.scanned += 1; @@ -148,30 +250,31 @@ impl StatementStack { None } /// B:1575 simplify_if reads the head of a branch. Costs the holes and - /// dead ranges before the first statement; nothing is copied. + /// dead ranges before the first statement, one step each; nothing is + /// copied. pub(super) fn first(&mut self, span: Span) -> Option { let at = self.live(span.start as usize, span.end())?; - Some(self.slot(at)[0]) + Some(self.slot_row(at, 0)) } /// First slot when it contains one statement. O(leading holes/dead ranges), /// using the same bounded scan as first; held multi-statement slots stay put. pub(super) fn first_single_slot(&mut self, span: Span) -> Option<(u32, NodeId)> { let at = self.live(span.start as usize, span.end())?; - let &[statement] = self.slot(at) else { return None }; - Some((at as u32, statement)) + if self.slot_len(at) != 1 { + return None; + } + Some((at as u32, self.slot_row(at, 0))) } /// B:1575 simplify_if, `[statement]` patterns: the slot and the statement /// when the run holds exactly one. Costs the holes and dead ranges of the - /// run, which a trimmed run has only before its first statement. + /// run, one step each, which a trimmed run has only before its first + /// statement. pub(super) fn only(&mut self, span: Span) -> Option<(u32, NodeId)> { let at = self.live(span.start as usize, span.end())?; - let &[statement] = self.slot(at) else { + if self.slot_len(at) != 1 || self.live(at + 1, span.end()).is_some() { return None; - }; - let next = at + 1; - self.live(next, span.end()) - .is_none() - .then_some((at as u32, statement)) + } + Some((at as u32, self.slot_row(at, 0))) } /// B:1575 simplify_if, `block_last_is_return_throw_or_continue`: the last /// statement. One step on a trimmed run; otherwise one per trailing hole @@ -180,8 +283,9 @@ impl StatementStack { let mut at = span.end(); while at > span.start as usize { at -= 1; - if let Some(&statement) = self.slot(at).last() { - return Some(statement); + let len = self.slot_len(at); + if len != 0 { + return Some(self.slot_row(at, len - 1)); } self.scanned += 1; } @@ -189,10 +293,10 @@ impl StatementStack { } /// K0: C:119 append. Pop the holes and dead slots at the top, down to /// `floor`, so the run above `floor` ends in a statement or is empty. - /// Each popped slot is popped once. + /// One step per popped slot; each is popped once. pub(super) fn trim(&mut self, floor: Span) -> Span { while self.slots.len() > floor.start as usize - && self.slot(self.slots.len() - 1).is_empty() + && self.slot_len(self.slots.len() - 1) == 0 { self.slots.pop(); self.scanned += 1; @@ -218,24 +322,27 @@ impl StatementStack { } } /// B:1225 block: record that `slot` holds an explicit empty block. - /// A binary search and an insertion among the unit's empty blocks. + /// Cost: O(log E) for a binary search among the unit's E empty blocks, + /// and an insertion. pub(super) fn note_empty_block(&mut self, slot: u32, statement: NodeId) { let at = self.empties.partition_point(|&(other, _)| other < slot); self.empties.insert(at, (slot, statement)); } /// B:1575 simplify_if drops the empty blocks of both branches first. - /// Two binary searches among the unit's empty blocks, then one step per - /// empty block of the run. The run itself is not scanned. - pub(super) fn drop_empty_blocks(&mut self, bir: &mut Bir, span: Span) { + /// Cost: two binary searches among the unit's empty blocks, then one + /// step per empty block of the run, each released through + /// `JsStmt::release_stmt`. The run itself is not scanned. + pub(super) fn drop_empty_blocks(&mut self, js: &mut B, a: &mut Pools, span: Span) { let from = self.empties.partition_point(|&(slot, _)| slot < span.start); let to = self.empties.partition_point(|&(slot, _)| (slot as usize) < span.end()); for (slot, statement) in self.empties.drain(from..to) { let slot = &mut self.slots[slot as usize]; - if *slot == Some(Slot::Statement(statement)) - && matches!(bir.view(statement), crate::bir::View::SBlock { statements } if statements.is_empty()) - { - *slot = Some(Slot::Dead(1)); - bir.release(statement); + let Some(Slot::Statement(held)) = slot else { continue }; + if Self::peek(held) == statement && js.is_empty_block(a, held) { + let Some(Slot::Statement(held)) = slot.replace(Slot::Dead(1)) else { + unreachable!("a statement slot") + }; + js.release_stmt(a, held); } } } @@ -244,29 +351,33 @@ impl StatementStack { pub(super) fn clear(&mut self, slot: u32) { self.slots[slot as usize] = Some(Slot::Dead(1)); } - /// A:191 sequence_arguments: a reserved hold is not an argument statement. + /// A:191 sequence_arguments: a reserved hold is not an argument + /// statement. O(holes and dead ranges before the first statement). pub(super) fn has_statements(&self, span: Span) -> bool { - self.statements(span).next().is_some() + self.statement_refs(span).next().is_some() } /// C:1144 compile_apply_call_inner: empty reserved holds have no output. + /// O(slots of the run). pub(super) fn remove_empty(&mut self, span: Span) { assert_eq!(span.end(), self.slots.len()); assert!(!self.has_statements(span)); self.slots.truncate(span.start as usize); } - /// K0: C:119 append, push a statement once. - pub fn push(&mut self, statement: NodeId) { + /// K0: C:119 append, push a statement once. One write. + pub fn push(&mut self, statement: B::S) { self.slots.push(Some(Slot::Statement(statement))); self.pushed += 1; } - /// K12: C:6441 emit_multi_staticcatch_prelude; reserve before a child run. + /// K12: C:6441 emit_multi_staticcatch_prelude; reserve before a child + /// run. One write. pub fn reserve(&mut self) -> u32 { let slot = self.mark().start; self.slots.push(None); slot } - /// K12: C:6441 emit_multi_staticcatch_prelude; fill without moving the run. - pub fn fill(&mut self, slot: u32, statement: NodeId) { + /// K12: C:6441 emit_multi_staticcatch_prelude; fill without moving the + /// run. One write. + pub fn fill(&mut self, slot: u32, statement: B::S) { let hole = &mut self.slots[slot as usize]; assert!(hole.is_none(), "reserved slot filled twice"); *hole = Some(Slot::Statement(statement)); @@ -274,30 +385,36 @@ impl StatementStack { } /// A:191 sequence_arguments: fill an argument's reserved suffix after all /// arguments were lowered. Held statements enter the list exactly once. - pub(super) fn fill_held(&mut self, slot: u32, statements: &[NodeId]) { + /// Cost: one step per statement, each moved once. + pub(super) fn fill_held(&mut self, slot: u32, statements: impl IntoIterator) { assert!(self.slots[slot as usize].is_none()); - let span = Span { start: self.held.len() as u32, len: statements.len() as u32 }; - self.pushed += statements.len(); - self.held.extend_from_slice(statements); - self.slots[slot as usize] = Some(Slot::Held(span)); + let start = self.held.len(); + self.held.extend(statements.into_iter().map(Some)); + let len = self.held.len() - start; + self.pushed += len; + self.slots[slot as usize] = Some(Slot::Held(Span { start: start as u32, len: len as u32 })); } /// C:3285 compile_static_raise_jump_output: an argument lowered after a /// later one comes before it in the output. Move the top run into the - /// slot reserved for it. One step per slot of the run; a statement is - /// moved once and still enters its list once. + /// slot reserved for it. Cost: one step per slot of the run and per + /// statement of a hold inside it; a statement is moved once and still + /// enters its list once. pub(super) fn hold_run(&mut self, slot: u32, run: Span) { assert_eq!(run.end(), self.slots.len()); assert!(slot < run.start && self.slots[slot as usize].is_none()); let start = self.held.len(); let mut at = run.start as usize; while at < run.end() { - at += match self.slots[at] { - Some(Slot::Statement(node)) => { - self.held.push(node); + at += match self.slots[at].take() { + Some(Slot::Statement(statement)) => { + self.held.push(Some(statement)); 1 } Some(Slot::Held(span)) => { - self.held.extend_from_within(span.start as usize..span.end()); + for i in span.start as usize..span.end() { + let statement = self.held[i].take(); + self.held.push(statement); + } 1 } Some(Slot::Dead(len)) => len as usize, @@ -311,7 +428,9 @@ impl StatementStack { })); } /// K0: C:119 append. Both children have already been lowered, even when - /// the first finishes. Value combination belongs to K2's expression policy. + /// the first finishes. Value combination belongs to K2's expression + /// policy. O(1), and one write per slot of the second run when a + /// finished first run pops it. pub fn append(&mut self, first: Out, second: Out, value: Option) -> Out { assert_eq!(first.stmts.end(), second.stmts.start as usize); assert_eq!(second.stmts.end(), self.slots.len()); @@ -334,25 +453,28 @@ impl StatementStack { finished: second.finished, } } - /// K2: C:4752 compiled_to_effect_block. Copy each statement of the run - /// into the child-list pool once: one step per statement, hole and dead - /// range of the run. The top run is popped; a run below the top becomes - /// one dead range, so that what follows it is not moved. Never flattens - /// SBlock here. The copied run is closed (`close::decide`: one more step - /// per statement) when the arena counts JavaScript occurrences. - pub fn take_list(&mut self, bir: &mut Bir, span: Span) -> ListId { + /// K2: C:4752 compiled_to_effect_block. Move each statement of the run + /// into a list in flight once ([`JsStmt::list`]): one step per + /// statement, hole and dead range of the run to count them, and one per + /// slot and statement to move them. The top run is popped; a run below + /// the top becomes one dead range, so that what follows it is not + /// moved. Never flattens SBlock here. The list is closed + /// (`close::decide`: one more step per statement) when the arena counts + /// JavaScript occurrences. + pub fn take_list(&mut self, js: &mut B, a: &mut Pools, span: Span) -> B::L { if span.len == 0 { // An empty run holds no slot. Its start is above the top when // the run before it was the top and has been taken. - return bir.lists.push_iter(0, std::iter::empty()); + return js.list(a, 0, std::iter::empty()); } assert!(span.end() <= self.slots.len()); - let count = self.statements(span).count(); - let id = bir.lists.push_iter(count, self.statements(span)); + let count = self.statement_refs(span).count(); + let list = js.list(a, count as u32, self.take_run(span)); self.copied += count; self.kill(span); - self.close(bir, id); - id + // T6x-b: `close.rs` takes the interface; until then the pool list. + self.close(a, B::list_id(list)); + list } /// THREE_TREES 11.4, step A: decide the declarations of a copied list. /// Cost: that of `close::decide`; nothing on an arena without counts. @@ -549,6 +671,7 @@ impl FreePool { #[cfg(test)] mod tests { use super::*; + use crate::bir::j::rows::Rows; fn output(span: Span, value: Option, finished: Finished) -> Out { Out { stmts: span, @@ -587,23 +710,23 @@ mod tests { assert_eq!(size_of::(), 4); assert_eq!(size_of::(), 16); assert_eq!(size_of::(), 20); - assert_eq!(size_of::(), 96); - assert_eq!(size_of::>(), 12); + assert_eq!(size_of::>(), 96); + assert_eq!(size_of::::S>>>(), 12); assert_eq!(size_of::(), 88); } #[test] fn a_statement_is_copied_once() { - let mut stack = StatementStack::default(); + let mut stack = StatementStack::::default(); let mut bir = Bir::default(); let mark = stack.mark(); let hole = stack.reserve(); stack.reserve(); for n in 0..10_000 { - stack.push(NodeId(n)); + stack.push(Rows::stmt_from_node(NodeId(n))); } - stack.fill(hole, NodeId(10_000)); + stack.fill(hole, Rows::stmt_from_node(NodeId(10_000))); let span = stack.since(mark); - let list = stack.take_list(&mut bir, span); + let list = stack.take_list(&mut Rows::default(), &mut bir, span); assert_eq!(bir.lists.get(list).len(), 10_001); assert_eq!(bir.lists.get(list)[0], NodeId(10_000)); assert_eq!(stack.pushed, 10_001); @@ -618,7 +741,7 @@ mod tests { /// slots of the run before it is taken and what reading its last /// statement scanned. fn switches_with_actions_on_the_stack(switches: u32, clauses: u32, width: u32) -> (u32, usize) { - let mut stack = StatementStack::default(); + let mut stack = StatementStack::::default(); let mut bir = Bir::default(); let body = stack.mark(); let mut node = 0; @@ -633,7 +756,7 @@ mod tests { .map(|_| { let mark = stack.mark(); for _ in 0..width { - stack.push(next()); + stack.push(Rows::stmt_from_node(next())); } stack.since(mark) }) @@ -644,13 +767,13 @@ mod tests { } // The clauses in order, then the default, as `untagged.rs`. for (at, &run) in runs[1..].iter().chain(&runs[..1]).enumerate() { - let list = stack.take_list(&mut bir, run); + let list = stack.take_list(&mut Rows::default(), &mut bir, run); assert_eq!(bir.lists.get(list).len(), width as usize); - stack.fill(actions.start + at as u32, next()); + stack.fill(actions.start + at as u32, Rows::stmt_from_node(next())); } - let list = stack.take_list(&mut bir, stack.since(actions)); + let list = stack.take_list(&mut Rows::default(), &mut bir, stack.since(actions)); assert_eq!(bir.lists.get(list).len(), clauses as usize + 1); - stack.fill(head, next()); + stack.fill(head, Rows::stmt_from_node(next())); // The switch and the dead ranges of its runs: the holes of the // actions are gone, and nothing else stays. assert_eq!(stack.mark().start, before + 1 + (clauses + 1) * width); @@ -659,7 +782,7 @@ mod tests { let scanned = stack.scanned; assert!(stack.last(run).is_some()); let scanned = stack.scanned - scanned; - let list = stack.take_list(&mut bir, run); + let list = stack.take_list(&mut Rows::default(), &mut bir, run); assert_eq!(bir.lists.get(list).len(), switches as usize); assert_eq!(stack.mark(), Span::default()); (run.len, scanned) @@ -680,23 +803,23 @@ mod tests { } #[test] fn append_finished_truncates_and_dummy_preserves() { - let mut stack = StatementStack::default(); - stack.push(NodeId(0)); - stack.push(NodeId(1)); + let mut stack = StatementStack::::default(); + stack.push(Rows::stmt_from_node(NodeId(0))); + stack.push(Rows::stmt_from_node(NodeId(1))); let first = output(Span { start: 0, len: 1 }, None, Finished::True); let next = output(Span { start: 1, len: 1 }, Some(NodeId(2)), Finished::False); let out = stack.append(first, next, None); assert_eq!(out.finished, Finished::True); - assert_eq!(stack.slots, vec![Some(Slot::Statement(NodeId(0)))]); + assert_eq!(stack.slots, vec![Some(Slot::Statement(Rows::stmt_from_node(NodeId(0))))]); let dummy = output(stack.mark(), None, Finished::Dummy); let out = stack.append(out, dummy, None); assert_eq!(out.stmts.len, 1); } #[test] fn append_adjacent_runs_does_not_copy() { - let mut stack = StatementStack::default(); - stack.push(NodeId(0)); - stack.push(NodeId(1)); + let mut stack = StatementStack::::default(); + stack.push(Rows::stmt_from_node(NodeId(0))); + stack.push(Rows::stmt_from_node(NodeId(1))); let a = output(Span { start: 0, len: 1 }, None, Finished::False); let b = output(Span { start: 1, len: 1 }, Some(NodeId(2)), Finished::False); let out = stack.append(a, b, Some(NodeId(2))); diff --git a/compiler-rust/src/bir/lower/prim.rs b/compiler-rust/src/bir/lower/prim.rs index 7342f1d85a..3a39647469 100644 --- a/compiler-rust/src/bir/lower/prim.rs +++ b/compiler-rust/src/bir/lower/prim.rs @@ -113,7 +113,7 @@ impl State<'_, B> { { self.js.locate_stmt(&mut self.arena, &mut statement, loc); } - self.statements.push(B::stmt_node(statement)); + self.statements.push(statement); } /// P: compile_primitive_js. O(a + emitted metadata bytes), plus sequencing diff --git a/compiler-rust/src/bir/lower/rec.rs b/compiler-rust/src/bir/lower/rec.rs index e84bf540d7..62ed0efb01 100644 --- a/compiler-rust/src/bir/lower/rec.rs +++ b/compiler-rust/src/bir/lower/rec.rs @@ -119,7 +119,7 @@ impl State<'_, B> { } else { let dummy = self.recursive_dummy(tag); let declaration = self.js.declare(&mut self.arena, LetKind::Variable, var, Some(B::from_node(dummy))); - self.statements.push(B::stmt_node(declaration)); + self.statements.push(declaration); let mut shell = B::from_node(value); assert!(self.js.kind(&self.arena, B::of(&shell)) == Kind::BlockLit, "recursive self block"); let count = self.js.take_operands(&mut self.arena, &mut shell) as usize; @@ -165,14 +165,14 @@ impl State<'_, B> { out.finished = Finished::False; } let span = self.statements.since(Span { start: out.stmts.start, len: 0 }); - let list = self.statements.take_list(&mut self.arena, span); + let list = B::list_id(self.statements.take_list(&mut self.js, &mut self.arena, span)); self.rec_members[member].code = Some(list); self.rec_members[member].finished = out.finished; if index + 1 == count { let mut finished = Finished::False; for i in group.base as usize..group.base as usize + count { self.rec_steps += 1; - if let Some(declaration) = self.rec_members[i].placeholder.take() { self.statements.push(declaration); } + if let Some(declaration) = self.rec_members[i].placeholder.take() { self.statements.push(B::stmt_from_node(declaration)); } } for i in group.base as usize..group.base as usize + count { self.rec_steps += 1; @@ -181,7 +181,7 @@ impl State<'_, B> { self.rec_steps += 1; let statement = self.js.take_at(&mut self.arena, list, k); if finished == Finished::True { self.js.release_stmt(&mut self.arena, statement); } - else { self.statements.push(B::stmt_node(statement)); } + else { self.statements.push(statement); } } if self.rec_members[i].finished == Finished::True { finished = Finished::True; } } diff --git a/compiler-rust/src/bir/lower/switch.rs b/compiler-rust/src/bir/lower/switch.rs index 352848a5c4..1959579c33 100644 --- a/compiler-rust/src/bir/lower/switch.rs +++ b/compiler-rust/src/bir/lower/switch.rs @@ -157,7 +157,7 @@ impl State<'_, B> { let declaration = (string && binding.is_some()).then(|| self.statements.reserve()); if let Some((kind, var)) = binding.filter(|_| !string) { let declaration = self.js.declare(&mut self.arena, kind, var, None); - self.statements.push(B::stmt_node(declaration)); + self.statements.push(declaration); } let frame = Switch(self.switches.len() as u32); self.switches.push(Some(Plan { @@ -711,11 +711,11 @@ impl State<'_, B> { let explicit = should_break && matches!(cx.cont, Cont::Assign(_)); if explicit { let statement = self.js.break_(&mut self.arena, None); - self.statements.push(B::stmt_node(statement)); + self.statements.push(statement); } let run = self.statements.since(Span { start, len: 0 }); - let list = self.statements.take_list(&mut self.arena, run); - let block = self.js.block_of(&mut self.arena, B::list_from(list)); + let list = self.statements.take_list(&mut self.js, &mut self.arena, run); + let block = self.js.block_of(&mut self.arena, list); let loc = self.switch_location(source); let plan = self.switches[frame.0 as usize].as_mut().unwrap(); let slot = plan.output.start as usize + index - 1; @@ -757,7 +757,7 @@ impl State<'_, B> { self.js.release_stmt(&mut self.arena, body); } let statement = self.js.exp_rule(&mut self.arena, scrutinee); - self.statements.push(B::stmt_node(statement)); + self.statements.push(statement); } else { let source_keys = self.arena.pools.switches[plan.header.index()].keys; let mut keys = crate::bir::KeySpan { @@ -842,7 +842,7 @@ impl State<'_, B> { header, plan.kind != Kind::Integer, ); - self.statements.push(B::stmt_node(statement)); + self.statements.push(statement); } self.switch_result(cx, &plan) } @@ -886,10 +886,10 @@ impl State<'_, B> { let value = self.js.keep_operand(&mut self.arena, assignment, 1); self.statements.kill(candidate.unwrap()); let statement = self.js.declare(&mut self.arena, kind, target, Some(value)); - self.statements.push(B::stmt_node(statement)); + self.statements.push(statement); } else { let declaration = self.js.declare(&mut self.arena, kind, target, None); - self.statements.fill(slot, B::stmt_node(declaration)); + self.statements.fill(slot, declaration); } } if plan.kind != Kind::Named && matches!(cx.cont, Cont::Effect(Tail::Return(_))) { diff --git a/compiler-rust/src/bir/lower/tail.rs b/compiler-rust/src/bir/lower/tail.rs index 71fe5f01e5..372c154832 100644 --- a/compiler-rust/src/bir/lower/tail.rs +++ b/compiler-rust/src/bir/lower/tail.rs @@ -64,10 +64,10 @@ impl State<'_, B> { let assignment = self.js.assign(&mut self.arena, copy, B::from_node(value)); assignments.push(B::stmt_node(assignment)); } - for statement in assignments.drain(..).rev() { self.statements.push(statement); } + for statement in assignments.drain(..).rev() { self.statements.push(B::stmt_from_node(statement)); } self.tail_assignments = assignments; let jump = self.js.continue_(&mut self.arena, None); - self.statements.push(B::stmt_node(jump)); + self.statements.push(jump); Probed::Out(Out { stmts: self.statements.since(Span { start: statements.start, len: 0 }), value: None, finished: Finished::True }) } @@ -98,17 +98,17 @@ impl State<'_, B> { let copy = self.tail_copies[param.index()].unwrap(); let value = self.js.var(&mut self.arena, copy); let statement = self.js.declare(&mut self.arena, LetKind::Alias, param, Some(value)); - self.statements.push(B::stmt_node(statement)); + self.statements.push(statement); } self.tail_order = ordered; let (statements, moved) = (&mut self.statements, &mut self.tail_body_statements); self.js.unblock(&mut self.arena, B::stmt_from_node(body), |s| { *moved += 1; - statements.push(B::stmt_node(s)); + statements.push(s); }); - let list = self.statements.take_list(&mut self.arena, self.statements.since(mark)); + let list = self.statements.take_list(&mut self.js, &mut self.arena, self.statements.since(mark)); let condition = self.js.boolean(&mut self.arena, true); - let loop_ = self.js.while_(&mut self.arena, condition, B::list_from(list), None); + let loop_ = self.js.while_(&mut self.arena, condition, list, None); let loop_ = self.js.list(&mut self.arena, 1, std::iter::once(loop_)); B::stmt_node(self.js.block_of(&mut self.arena, loop_)) } diff --git a/compiler-rust/src/bir/lower/try_.rs b/compiler-rust/src/bir/lower/try_.rs index 5939dbcfde..2d9efe9edb 100644 --- a/compiler-rust/src/bir/lower/try_.rs +++ b/compiler-rust/src/bir/lower/try_.rs @@ -92,7 +92,7 @@ impl State<'_, B> { start: out.stmts.start, len: 0, }); - let list = self.statements.take_list(&mut self.arena, span); + let list = B::list_id(self.statements.take_list(&mut self.js, &mut self.arena, span)); self.tries[frame.0 as usize].blocks[index] = Some(list); Probed::Out(Out { stmts: self.statements.mark(), @@ -117,7 +117,7 @@ impl State<'_, B> { None => (None, Some(second)), }; let statement = self.js.try_(&mut self.arena, body, handler, finaliser); - self.statements.push(B::stmt_node(statement)); + self.statements.push(statement); let value = if matches!(cx.cont, Cont::Value(_)) { let var = self .js diff --git a/compiler-rust/src/bir/lower/untagged.rs b/compiler-rust/src/bir/lower/untagged.rs index 81ce36f5c8..0c57654504 100644 --- a/compiler-rust/src/bir/lower/untagged.rs +++ b/compiler-rust/src/bir/lower/untagged.rs @@ -413,7 +413,7 @@ impl State<'_, B> { }; if let Some((kind, var)) = binding { let declaration = self.js.declare(&mut self.arena, kind, var, None); - self.statements.push(B::stmt_node(declaration)); + self.statements.push(declaration); } let frame = Untagged(self.untagged.len() as u32); let states = Run::new( @@ -609,7 +609,7 @@ impl State<'_, B> { if !p.payload_only && self.js.kind(&self.arena, B::of(&value)) != row::Kind::Var { let var = self.fresh_variable("tmp"); let declaration = self.js.declare(&mut self.arena, LetKind::Strict, var, Some(value)); - self.statements.push(B::stmt_node(declaration)); + self.statements.push(declaration); value = self.js.var(&mut self.arena, var); } p.scrutinee = Some(B::node(value)); @@ -645,11 +645,11 @@ impl State<'_, B> { .uses > 1 { - let list = self.statements.take_list(&mut self.arena, out.stmts); + let list = self.statements.take_list(&mut self.js, &mut self.arena, out.stmts); self.untagged_storage .states .get_mut(&p.states, action as usize) - .cached = Some(B::stmt_node(self.js.block_of(&mut self.arena, B::list_from(list)))); + .cached = Some(B::stmt_node(self.js.block_of(&mut self.arena, list))); self.emit_cached_action(&mut p, action); } else { self.untagged_storage @@ -722,7 +722,7 @@ impl State<'_, B> { }; let start = self.statements.mark(); let statements = &mut self.statements; - self.js.unblock(&mut self.arena, B::stmt_from_node(root), |s| statements.push(B::stmt_node(s))); + self.js.unblock(&mut self.arena, B::stmt_from_node(root), |s| statements.push(s)); self.untagged_storage.outputs.push( &mut p.outputs, Output { @@ -793,8 +793,8 @@ impl State<'_, B> { self.statements.kill(run); statement } else { - let list = self.statements.take_list(&mut self.arena, run); - B::stmt_node(self.js.explicit_block(&mut self.arena, B::list_from(list))) + let list = self.statements.take_list(&mut self.js, &mut self.arena, run); + B::stmt_node(self.js.explicit_block(&mut self.arena, list)) }; self.place(start.start, block); self.untagged_storage.outputs.push( @@ -871,9 +871,9 @@ impl State<'_, B> { } else { None }; - let list = self.statements.take_list(&mut self.arena, out.run); + let list = self.statements.take_list(&mut self.js, &mut self.arena, out.run); ( - B::stmt_node(self.js.block_of(&mut self.arena, B::list_from(list))), + B::stmt_node(self.js.block_of(&mut self.arena, list)), ClauseMeta { should_break, comment: None, @@ -952,8 +952,8 @@ impl State<'_, B> { self.arena.pools.switch_clauses.push(meta); } if has_default { - let list = self.statements.take_list(&mut self.arena, default.run); - let body = self.js.block_of(&mut self.arena, B::list_from(list)); + let list = self.statements.take_list(&mut self.js, &mut self.arena, default.run); + let body = self.js.block_of(&mut self.arena, list); self.arena.lists.set(actions, count, B::stmt_node(body)); self.arena.pools.switch_clauses.push(ClauseMeta { should_break: false, @@ -978,7 +978,7 @@ impl State<'_, B> { dispatch: SwitchDispatch::Direct, }); let statement = self.js.switch(&mut self.arena, scrutinee, B::list_from(actions), header, true); - self.statements.fill(start.start, B::stmt_node(statement)); + self.statements.fill(start.start, statement); self.untagged_storage.outputs.push( &mut p.outputs, Output { diff --git a/compiler-rust/src/bir/lower/variant.rs b/compiler-rust/src/bir/lower/variant.rs index 3394c01b3d..188cb3e3d3 100644 --- a/compiler-rust/src/bir/lower/variant.rs +++ b/compiler-rust/src/bir/lower/variant.rs @@ -184,7 +184,7 @@ impl State<'_, B> { }; if let Some((kind, var)) = binding { let statement = self.js.declare(&mut self.arena, kind, var, None); - self.statements.push(B::stmt_node(statement)); + self.statements.push(statement); } let target = binding.map(|(_, var)| var); let body_cx = Cx { @@ -295,7 +295,7 @@ impl State<'_, B> { let var = self.fresh_variable("tmp"); let statement = self.js.declare(&mut self.arena, LetKind::Strict, var, Some(B::from_node(value))); - self.statements.push(B::stmt_node(statement)); + self.statements.push(statement); B::node(self.js.var(&mut self.arena, var)) } else { value From 36d7b9107c56e9ee1da3830e14f5fbf2559cfb6c Mon Sep 17 00:00:00 2001 From: Jaap Frolich Date: Tue, 17 Nov 2026 21:42:46 +0000 Subject: [PATCH 2/2] Changelog fragment: the statement stack owns statement handles Internal release note for the statement stack of the backend lowering on statement handles (three trees, T6x-a1). Co-authored-by: Jaap Frolich Session: https://os.submodule.nl/session/bks-6e0f5c7f-c4b3-74aa-868a-9195e58e2297 Signed-off-by: Jaap Frolich --- changes/statement-stack-on-handles.internal.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/statement-stack-on-handles.internal.md diff --git a/changes/statement-stack-on-handles.internal.md b/changes/statement-stack-on-handles.internal.md new file mode 100644 index 0000000000..2d3b689276 --- /dev/null +++ b/changes/statement-stack-on-handles.internal.md @@ -0,0 +1 @@ +- The backend lowering's statement stack owns statement handles of its JavaScript interface instead of J rows.