
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1201 · main · 9 min 57 s
1Étoiles
0Forks
0Issues
| 1 | //! Contexts of tail recursion modulo cons (ADR 0039): the slow paths of |
| 2 | //! `tl_ctx_extend` and `tl_ctx_apply` (`louise_rt.h`), and new contexts. |
| 3 | //! |
| 4 | //! A context is unit (empty) or a 3-field block: the root cell (owned), |
| 5 | //! then `int(address of the last cell)` and `int(its hole field)`. Each cell |
| 6 | //! owns the next through its hole field; the last cell's hole is unit. The |
| 7 | //! two int fields are immediates, so dropping, sending or copying the block |
| 8 | //! treats it as an ordinary value. |
| 9 | //! |
| 10 | //! Every cell a function's contexts hold has the same hole field (the pass |
| 11 | //! checks it), so the path from the root to the last cell follows that |
| 12 | //! field. A context that is not unique (a multi-shot resumption kept a copy, |
| 13 | //! ADR 0017) is copied along that path before it changes: the other holder |
| 14 | //! keeps the original, untouched. |
| 15 | |
| 16 | use crate::abi::{Block, Ctx, UNIT, Value, int, int_get}; |
| 17 | use crate::heap::{drop_value, dup_value, tl_alloc}; |
| 18 | |
| 19 | /// A new context of one cell, `cell`, whose hole is field `hole`. Consumes |
| 20 | /// `cell`. |
| 21 | #[unsafe(no_mangle)] |
| 22 | pub extern "C" fn tl_ctx_new(ctx: *mut Ctx, cell: Value, hole: i64) -> Value { |
| 23 | block(ctx, cell, cell as *mut Block, hole) |
| 24 | } |
| 25 | |
| 26 | /// `tl_ctx_extend` of a shared context: copy it, then extend the copy. |
| 27 | /// |
| 28 | /// # Safety |
| 29 | /// `k` must be a live context, `cell` a live constructor cell or tuple |
| 30 | /// with unit in field `hole`; both are consumed. |
| 31 | #[unsafe(no_mangle)] |
| 32 | pub unsafe extern "C" fn tl_ctx_extend_slow( |
| 33 | ctx: *mut Ctx, |
| 34 | k: Value, |
| 35 | cell: Value, |
| 36 | hole: i64, |
| 37 | ) -> Value { |
| 38 | // SAFETY: forwarded caller guarantees. |
| 39 | unsafe { |
| 40 | let k = unshare(ctx, k); |
| 41 | let b = k as *mut Block; |
| 42 | let fields = Block::fields_ptr(b); |
| 43 | let last = int_get(fields.add(1).read()) as *mut Block; |
| 44 | let last_hole = int_get(fields.add(2).read()) as usize; |
| 45 | Block::fields_ptr(last).add(last_hole).write(cell); |
| 46 | fields.add(1).write(int(cell as i64)); |
| 47 | fields.add(2).write(int(hole)); |
| 48 | k |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /// `tl_ctx_apply` of a shared context: copy it, then fill the copy. |
| 53 | /// |
| 54 | /// # Safety |
| 55 | /// `k` must be a live context and `value` a live value; both are consumed. |
| 56 | #[unsafe(no_mangle)] |
| 57 | pub unsafe extern "C" fn tl_ctx_apply_slow(ctx: *mut Ctx, k: Value, value: Value) -> Value { |
| 58 | // SAFETY: forwarded caller guarantees. |
| 59 | unsafe { |
| 60 | let k = unshare(ctx, k); |
| 61 | let fields = Block::fields_ptr(k as *mut Block); |
| 62 | let last = int_get(fields.add(1).read()) as *mut Block; |
| 63 | let hole = int_get(fields.add(2).read()) as usize; |
| 64 | Block::fields_ptr(last).add(hole).write(value); |
| 65 | let root = fields.read(); |
| 66 | fields.write(UNIT); |
| 67 | drop_value(ctx, k); |
| 68 | root |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /// A context block: `root` (owned), the last cell and its hole. |
| 73 | fn block(ctx: *mut Ctx, root: Value, last: *mut Block, hole: i64) -> Value { |
| 74 | let b = tl_alloc(ctx, 0, 3, 0); |
| 75 | // SAFETY: a fresh block with 3 fields. |
| 76 | unsafe { |
| 77 | let fields = Block::fields_ptr(b); |
| 78 | fields.write(root); |
| 79 | fields.add(1).write(int(last as i64)); |
| 80 | fields.add(2).write(int(hole)); |
| 81 | } |
| 82 | b as Value |
| 83 | } |
| 84 | |
| 85 | /// A unique context with the same cells as `k` (consumed): `k` itself if |
| 86 | /// it is unique, else a copy of its path from the root to the last cell. |
| 87 | /// |
| 88 | /// # Safety |
| 89 | /// `k` must be a live context. |
| 90 | unsafe fn unshare(ctx: *mut Ctx, k: Value) -> Value { |
| 91 | // SAFETY: caller guarantees a live context; its cells are live while |
| 92 | // it is. |
| 93 | unsafe { |
| 94 | let b = k as *mut Block; |
| 95 | if (*b).header.rc == 1 { |
| 96 | return k; |
| 97 | } |
| 98 | let fields = Block::fields_ptr(b); |
| 99 | let last = int_get(fields.add(1).read()) as *mut Block; |
| 100 | let hole = int_get(fields.add(2).read()) as usize; |
| 101 | let mut old = fields.read() as *mut Block; |
| 102 | let root = copy_cell(ctx, old, hole); |
| 103 | let mut new = root; |
| 104 | while old != last { |
| 105 | old = Block::fields_ptr(old).add(hole).read() as *mut Block; |
| 106 | let next = copy_cell(ctx, old, hole); |
| 107 | Block::fields_ptr(new).add(hole).write(next as Value); |
| 108 | new = next; |
| 109 | } |
| 110 | let copy = block(ctx, root as Value, new, hole as i64); |
| 111 | drop_value(ctx, k); |
| 112 | copy |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /// A new cell like `cell` (borrowed), its fields dup'd, but unit in field |
| 117 | /// `hole` (the caller links the next cell there). |
| 118 | /// |
| 119 | /// # Safety |
| 120 | /// `cell` must be a live constructor cell or tuple (no raw area). |
| 121 | unsafe fn copy_cell(ctx: *mut Ctx, cell: *mut Block, hole: usize) -> *mut Block { |
| 122 | // SAFETY: caller guarantees a live cell of `n` scanned fields. |
| 123 | unsafe { |
| 124 | let n = (*cell).header.scan_fsize; |
| 125 | let copy = tl_alloc(ctx, (*cell).header.tag, n, 0); |
| 126 | let from = Block::fields_ptr(cell); |
| 127 | let to = Block::fields_ptr(copy); |
| 128 | for i in 0..n as usize { |
| 129 | let v = if i == hole { |
| 130 | UNIT |
| 131 | } else { |
| 132 | dup_value(from.add(i).read()) |
| 133 | }; |
| 134 | to.add(i).write(v); |
| 135 | } |
| 136 | copy |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | #[cfg(test)] |
| 141 | mod tests { |
| 142 | use super::*; |
| 143 | use crate::abi::Header; |
| 144 | |
| 145 | /// A cons cell `Cons(x, unit)`. |
| 146 | fn cons(ctx: *mut Ctx, x: i64) -> Value { |
| 147 | crate::values::cell(ctx, 1, &[int(x), UNIT]) |
| 148 | } |
| 149 | |
| 150 | /// The ints of a list built by applying a context to `Nil`. |
| 151 | fn ints(mut v: Value) -> Vec<i64> { |
| 152 | let mut out = Vec::new(); |
| 153 | while v & 1 == 0 { |
| 154 | // SAFETY: a live cons cell. |
| 155 | unsafe { |
| 156 | let f = Block::fields_ptr(v as *mut Block); |
| 157 | out.push(int_get(f.read())); |
| 158 | v = f.add(1).read(); |
| 159 | } |
| 160 | } |
| 161 | out |
| 162 | } |
| 163 | |
| 164 | extern "C" fn contexts(ctx: *mut Ctx) -> Value { |
| 165 | // SAFETY: fresh values, each consumed once; the shared context is |
| 166 | // counted twice, as a multi-shot resumption's copy makes it. |
| 167 | unsafe { |
| 168 | // Extending, then applying, builds the list in order. |
| 169 | let mut k = tl_ctx_new(ctx, cons(ctx, 1), 1); |
| 170 | for x in 2..=3 { |
| 171 | k = tl_ctx_extend_slow(ctx, k, cons(ctx, x), 1); |
| 172 | } |
| 173 | let list = tl_ctx_apply_slow(ctx, k, int(0)); |
| 174 | assert_eq!(ints(list), [1, 2, 3]); |
| 175 | drop_value(ctx, list); |
| 176 | |
| 177 | // A shared context is copied; the other holder keeps its own. |
| 178 | let k = tl_ctx_new(ctx, cons(ctx, 1), 1); |
| 179 | let k = tl_ctx_extend_slow(ctx, k, cons(ctx, 2), 1); |
| 180 | (*(k as *mut Block).cast::<Header>()).rc += 1; |
| 181 | let first = tl_ctx_extend_slow(ctx, k, cons(ctx, 3), 1); |
| 182 | assert_ne!(first, k, "copied"); |
| 183 | let a = tl_ctx_apply_slow(ctx, first, int(0)); |
| 184 | // The original, now unique, changes in place. |
| 185 | let second = tl_ctx_extend_slow(ctx, k, cons(ctx, 9), 1); |
| 186 | assert_eq!(second, k, "in place"); |
| 187 | let b = tl_ctx_apply_slow(ctx, second, int(0)); |
| 188 | assert_eq!(ints(a), [1, 2, 3]); |
| 189 | assert_eq!(ints(b), [1, 2, 9]); |
| 190 | drop_value(ctx, a); |
| 191 | drop_value(ctx, b); |
| 192 | } |
| 193 | assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); |
| 194 | UNIT |
| 195 | } |
| 196 | |
| 197 | #[test] |
| 198 | fn contexts_build_in_order_and_copy_when_shared() { |
| 199 | // SAFETY: the test follows the generated-code convention. |
| 200 | let code = unsafe { crate::sched::run_test_actor(contexts) }; |
| 201 | assert_eq!(code, 0); |
| 202 | } |
| 203 | } |