//! Contexts of tail recursion modulo cons (ADR 0039): the slow paths of //! `tl_ctx_extend` and `tl_ctx_apply` (`louise_rt.h`), and new contexts. //! //! A context is unit (empty) or a 3-field block: the root cell (owned), //! then `int(address of the last cell)` and `int(its hole field)`. Each cell //! owns the next through its hole field; the last cell's hole is unit. The //! two int fields are immediates, so dropping, sending or copying the block //! treats it as an ordinary value. //! //! Every cell a function's contexts hold has the same hole field (the pass //! checks it), so the path from the root to the last cell follows that //! field. A context that is not unique (a multi-shot resumption kept a copy, //! ADR 0017) is copied along that path before it changes: the other holder //! keeps the original, untouched. use crate::abi::{Block, Ctx, UNIT, Value, int, int_get}; use crate::heap::{drop_value, dup_value, tl_alloc}; /// A new context of one cell, `cell`, whose hole is field `hole`. Consumes /// `cell`. #[unsafe(no_mangle)] pub extern "C" fn tl_ctx_new(ctx: *mut Ctx, cell: Value, hole: i64) -> Value { block(ctx, cell, cell as *mut Block, hole) } /// `tl_ctx_extend` of a shared context: copy it, then extend the copy. /// /// # Safety /// `k` must be a live context, `cell` a live constructor cell or tuple /// with unit in field `hole`; both are consumed. #[unsafe(no_mangle)] pub unsafe extern "C" fn tl_ctx_extend_slow( ctx: *mut Ctx, k: Value, cell: Value, hole: i64, ) -> Value { // SAFETY: forwarded caller guarantees. unsafe { let k = unshare(ctx, k); let b = k as *mut Block; let fields = Block::fields_ptr(b); let last = int_get(fields.add(1).read()) as *mut Block; let last_hole = int_get(fields.add(2).read()) as usize; Block::fields_ptr(last).add(last_hole).write(cell); fields.add(1).write(int(cell as i64)); fields.add(2).write(int(hole)); k } } /// `tl_ctx_apply` of a shared context: copy it, then fill the copy. /// /// # Safety /// `k` must be a live context and `value` a live value; both are consumed. #[unsafe(no_mangle)] pub unsafe extern "C" fn tl_ctx_apply_slow(ctx: *mut Ctx, k: Value, value: Value) -> Value { // SAFETY: forwarded caller guarantees. unsafe { let k = unshare(ctx, k); let fields = Block::fields_ptr(k as *mut Block); let last = int_get(fields.add(1).read()) as *mut Block; let hole = int_get(fields.add(2).read()) as usize; Block::fields_ptr(last).add(hole).write(value); let root = fields.read(); fields.write(UNIT); drop_value(ctx, k); root } } /// A context block: `root` (owned), the last cell and its hole. fn block(ctx: *mut Ctx, root: Value, last: *mut Block, hole: i64) -> Value { let b = tl_alloc(ctx, 0, 3, 0); // SAFETY: a fresh block with 3 fields. unsafe { let fields = Block::fields_ptr(b); fields.write(root); fields.add(1).write(int(last as i64)); fields.add(2).write(int(hole)); } b as Value } /// A unique context with the same cells as `k` (consumed): `k` itself if /// it is unique, else a copy of its path from the root to the last cell. /// /// # Safety /// `k` must be a live context. unsafe fn unshare(ctx: *mut Ctx, k: Value) -> Value { // SAFETY: caller guarantees a live context; its cells are live while // it is. unsafe { let b = k as *mut Block; if (*b).header.rc == 1 { return k; } let fields = Block::fields_ptr(b); let last = int_get(fields.add(1).read()) as *mut Block; let hole = int_get(fields.add(2).read()) as usize; let mut old = fields.read() as *mut Block; let root = copy_cell(ctx, old, hole); let mut new = root; while old != last { old = Block::fields_ptr(old).add(hole).read() as *mut Block; let next = copy_cell(ctx, old, hole); Block::fields_ptr(new).add(hole).write(next as Value); new = next; } let copy = block(ctx, root as Value, new, hole as i64); drop_value(ctx, k); copy } } /// A new cell like `cell` (borrowed), its fields dup'd, but unit in field /// `hole` (the caller links the next cell there). /// /// # Safety /// `cell` must be a live constructor cell or tuple (no raw area). unsafe fn copy_cell(ctx: *mut Ctx, cell: *mut Block, hole: usize) -> *mut Block { // SAFETY: caller guarantees a live cell of `n` scanned fields. unsafe { let n = (*cell).header.scan_fsize; let copy = tl_alloc(ctx, (*cell).header.tag, n, 0); let from = Block::fields_ptr(cell); let to = Block::fields_ptr(copy); for i in 0..n as usize { let v = if i == hole { UNIT } else { dup_value(from.add(i).read()) }; to.add(i).write(v); } copy } } #[cfg(test)] mod tests { use super::*; use crate::abi::Header; /// A cons cell `Cons(x, unit)`. fn cons(ctx: *mut Ctx, x: i64) -> Value { crate::values::cell(ctx, 1, &[int(x), UNIT]) } /// The ints of a list built by applying a context to `Nil`. fn ints(mut v: Value) -> Vec { let mut out = Vec::new(); while v & 1 == 0 { // SAFETY: a live cons cell. unsafe { let f = Block::fields_ptr(v as *mut Block); out.push(int_get(f.read())); v = f.add(1).read(); } } out } extern "C" fn contexts(ctx: *mut Ctx) -> Value { // SAFETY: fresh values, each consumed once; the shared context is // counted twice, as a multi-shot resumption's copy makes it. unsafe { // Extending, then applying, builds the list in order. let mut k = tl_ctx_new(ctx, cons(ctx, 1), 1); for x in 2..=3 { k = tl_ctx_extend_slow(ctx, k, cons(ctx, x), 1); } let list = tl_ctx_apply_slow(ctx, k, int(0)); assert_eq!(ints(list), [1, 2, 3]); drop_value(ctx, list); // A shared context is copied; the other holder keeps its own. let k = tl_ctx_new(ctx, cons(ctx, 1), 1); let k = tl_ctx_extend_slow(ctx, k, cons(ctx, 2), 1); (*(k as *mut Block).cast::
()).rc += 1; let first = tl_ctx_extend_slow(ctx, k, cons(ctx, 3), 1); assert_ne!(first, k, "copied"); let a = tl_ctx_apply_slow(ctx, first, int(0)); // The original, now unique, changes in place. let second = tl_ctx_extend_slow(ctx, k, cons(ctx, 9), 1); assert_eq!(second, k, "in place"); let b = tl_ctx_apply_slow(ctx, second, int(0)); assert_eq!(ints(a), [1, 2, 3]); assert_eq!(ints(b), [1, 2, 9]); drop_value(ctx, a); drop_value(ctx, b); } assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); UNIT } #[test] fn contexts_build_in_order_and_copy_when_shared() { // SAFETY: the test follows the generated-code convention. let code = unsafe { crate::sched::run_test_actor(contexts) }; assert_eq!(code, 0); } }