| @@ -18,8 +18,9 @@ |
| 18 | 18 | //! - **Borrowed positions** leave it owned: the base of a field access, the |
| 19 | 19 | //! value a `switch` inspects, arguments of externs and primitives, the |
| 20 | 20 | //! capability of an operation, and borrowed parameters of direct calls. |
| 21 | | −//! Externs and the runtime never take ownership of their arguments; |
| 22 | | −//! results are always owned. |
| 21 | +//! Externs and the runtime never take ownership of their arguments, |
| 22 | +//! except externs declared `owned` ([`Program::owned`], ADR 0014), |
| 23 | +//! whose arguments are owned positions; results are always owned. |
| 23 | 24 | //! - **Dead variables** are dropped as early as possible: on entry to each |
| 24 | 25 | //! expression, every owned variable it does not mention is dropped. So |
| 25 | 26 | //! drops happen at the start of the branch or the `let` where the |
| @@ -36,7 +37,7 @@ mod verify; |
| 36 | 37 | |
| 37 | 38 | use std::collections::{BTreeSet, HashMap}; |
| 38 | 39 | |
| 39 | | −use tilc_core::ir::{Atom, Expr, FnDecl, FnId, Program, Value, VarId}; |
| 40 | +use tilc_core::ir::{Atom, Expr, FnId, Program, Value, VarId}; |
| 40 | 41 | |
| 41 | 42 | pub use verify::verify; |
| 42 | 43 | |
| @@ -60,7 +61,7 @@ pub fn insert_rc(program: &mut Program) { |
| 60 | 61 | .filter(|p| !borrowed.contains(p)) |
| 61 | 62 | .copied() |
| 62 | 63 | .collect(); |
| 63 | | − let body = Rc { fns: &program.fns }.expr(body, owned, &borrowed); |
| 64 | + let body = Rc { program }.expr(body, owned, &borrowed); |
| 64 | 65 | let mut n_vars = f.n_vars; |
| 65 | 66 | let body = specialise::specialise(body, &mut n_vars); |
| 66 | 67 | let f = &mut program.fns[id]; |
| @@ -69,9 +70,10 @@ pub fn insert_rc(program: &mut Program) { |
| 69 | 70 | } |
| 70 | 71 | } |
| 71 | 72 | |
| 72 | | −/// Dup/drop insertion, with the functions' signatures (borrowed parameters). |
| 73 | +/// Dup/drop insertion, with the functions' signatures (borrowed |
| 74 | +/// parameters) and the owned externs. |
| 73 | 75 | struct Rc<'a> { |
| 74 | | − fns: &'a [FnDecl], |
| 76 | + program: &'a Program, |
| 75 | 77 | } |
| 76 | 78 | |
| 77 | 79 | impl Rc<'_> { |
| @@ -138,7 +140,7 @@ impl Rc<'_> { |
| 138 | 140 | } |
| 139 | 141 | } |
| 140 | 142 | value => { |
| 141 | | − let (owned_uses, borrowed_uses) = uses(&value, self.fns); |
| 143 | + let (owned_uses, borrowed_uses) = uses(&value, self.program); |
| 142 | 144 | let mut remaining: HashMap<VarId, usize> = HashMap::new(); |
| 143 | 145 | for x in &owned_uses { |
| 144 | 146 | *remaining.entry(*x).or_default() += 1; |
| @@ -200,14 +202,24 @@ impl Rc<'_> { |
| 200 | 202 | } |
| 201 | 203 | |
| 202 | 204 | /// Variables a value uses in owned positions (in order, with repeats) and |
| 203 | | −/// in borrowed positions, given the functions' signatures. |
| 204 | | −pub fn uses(value: &Value, fns: &[FnDecl]) -> (Vec<VarId>, BTreeSet<VarId>) { |
| 205 | | − uses_with(value, |f, i| fns[f as usize].borrowed.contains(&i)) |
| 205 | +/// in borrowed positions, given the functions' signatures and the owned |
| 206 | +/// externs. |
| 207 | +pub fn uses(value: &Value, program: &Program) -> (Vec<VarId>, BTreeSet<VarId>) { |
| 208 | + uses_with( |
| 209 | + value, |
| 210 | + |f, i| program.fns[f as usize].borrowed.contains(&i), |
| 211 | + &program.owned, |
| 212 | + ) |
| 206 | 213 | } |
| 207 | 214 | |
| 208 | 215 | /// [`uses`], where `borrows(f, i)` tells whether function `f` borrows its |
| 209 | | −/// parameter `i`. |
| 210 | | −fn uses_with(value: &Value, borrows: impl Fn(FnId, usize) -> bool) -> (Vec<VarId>, Set) { |
| 216 | +/// parameter `i`, and `owned_externs` lists the externs that consume |
| 217 | +/// their arguments. |
| 218 | +fn uses_with( |
| 219 | + value: &Value, |
| 220 | + borrows: impl Fn(FnId, usize) -> bool, |
| 221 | + owned_externs: &BTreeSet<String>, |
| 222 | +) -> (Vec<VarId>, Set) { |
| 211 | 223 | let vars = |atoms: &[Atom]| -> Vec<VarId> { |
| 212 | 224 | atoms |
| 213 | 225 | .iter() |
| @@ -242,6 +254,7 @@ fn uses_with(value: &Value, borrows: impl Fn(FnId, usize) -> bool) -> (Vec<VarId |
| 242 | 254 | Value::Construct { args, .. } | Value::Tuple(args) | Value::Closure(_, args) => { |
| 243 | 255 | (vars(args), Set::new()) |
| 244 | 256 | } |
| 257 | + Value::CallExtern(sym, args) if owned_externs.contains(sym) => (vars(args), Set::new()), |
| 245 | 258 | Value::CallExtern(_, args) | Value::Prim(_, args) => { |
| 246 | 259 | (Vec::new(), vars(args).into_iter().collect()) |
| 247 | 260 | } |
| @@ -259,7 +272,7 @@ fn uses_with(value: &Value, borrows: impl Fn(FnId, usize) -> bool) -> (Vec<VarId |
| 259 | 272 | } |
| 260 | 273 | Value::Region { body, .. } => (one(body), Set::new()), |
| 261 | 274 | Value::Reuse { token, value } => { |
| 262 | | − let (mut owned, borrowed) = uses_with(value, borrows); |
| 275 | + let (mut owned, borrowed) = uses_with(value, borrows, owned_externs); |
| 263 | 276 | owned.insert(0, *token); |
| 264 | 277 | (owned, borrowed) |
| 265 | 278 | } |
| @@ -289,7 +302,7 @@ fn free_into(e: &Expr, out: &mut Set) { |
| 289 | 302 | Value::Block(b) => free_into(b, out), |
| 290 | 303 | // Which positions are owned does not matter here. |
| 291 | 304 | value => { |
| 292 | | − let (owned, borrowed) = uses_with(value, |_, _| false); |
| 305 | + let (owned, borrowed) = uses_with(value, |_, _| false, &BTreeSet::new()); |
| 293 | 306 | out.extend(owned); |
| 294 | 307 | out.extend(borrowed); |
| 295 | 308 | } |