| @@ -175,6 +175,8 @@ pub struct Lowerer<'a> { |
| 175 | 175 | /// The functions on row evidence (ADR 0066), made once a program uses |
| 176 | 176 | /// it: lookup and removal of an entry. |
| 177 | 177 | evidence_fns: Option<(FnId, FnId)>, |
| 178 | + /// `list$append`, made once a list literal spreads before its end. |
| 179 | + list_append_fn: Option<FnId>, |
| 178 | 180 | /// The std items lowering treats specially. |
| 179 | 181 | known: WellKnown, |
| 180 | 182 | stubs: HashMap<DefId, FnId>, |
| @@ -232,6 +234,7 @@ pub fn lower_with(hir: &Hir, typeck: &TypeckOutput, settings: &Settings) -> Prog |
| 232 | 234 | in_scope: None, |
| 233 | 235 | taggers: [None; 2], |
| 234 | 236 | evidence_fns: None, |
| 237 | + list_append_fn: None, |
| 235 | 238 | known: WellKnown::find(hir), |
| 236 | 239 | stubs: HashMap::new(), |
| 237 | 240 | body: None, |
| @@ -392,6 +395,30 @@ fn bool_value(b: bool) -> Value { |
| 392 | 395 | } |
| 393 | 396 | } |
| 394 | 397 | |
| 398 | +/// A function lowering writes in Core itself (`ev$lookup`, `list$append`). |
| 399 | +fn helper_decl(name: &str, params: Vec<VarId>, body: Expr, n_vars: u32) -> FnDecl { |
| 400 | + FnDecl { |
| 401 | + name: name.into(), |
| 402 | + params, |
| 403 | + body, |
| 404 | + n_vars, |
| 405 | + borrowed: Default::default(), |
| 406 | + span: None, |
| 407 | + module: None, |
| 408 | + shapes: Default::default(), |
| 409 | + fip: false, |
| 410 | + cpr_arity: None, |
| 411 | + unbox_sig: None, |
| 412 | + } |
| 413 | +} |
| 414 | + |
| 415 | +/// `binds`, in order, then `end`. |
| 416 | +fn lets(binds: Vec<(VarId, Value)>, end: Expr) -> Expr { |
| 417 | + binds.into_iter().rev().fold(end, |rest, (var, value)| { |
| 418 | + Expr::Let(var, Box::new(value), Box::new(rest), None) |
| 419 | + }) |
| 420 | +} |
| 421 | + |
| 395 | 422 | impl<'a> Lowerer<'a> { |
| 396 | 423 | // ---- tables ------------------------------------------------------------ |
| 397 | 424 | |
| @@ -1063,25 +1090,7 @@ impl<'a> Lowerer<'a> { |
| 1063 | 1090 | } |
| 1064 | 1091 | let lookup = self.placeholder_in("ev$lookup".into(), None); |
| 1065 | 1092 | let remove = self.placeholder_in("ev$remove".into(), None); |
| 1066 | | − let decl = |name: &str, params: Vec<VarId>, body: Expr, n_vars: u32| FnDecl { |
| 1067 | | − name: name.into(), |
| 1068 | | − params, |
| 1069 | | − body, |
| 1070 | | − n_vars, |
| 1071 | | − borrowed: Default::default(), |
| 1072 | | − span: None, |
| 1073 | | − module: None, |
| 1074 | | − shapes: Default::default(), |
| 1075 | | − fip: false, |
| 1076 | | − cpr_arity: None, |
| 1077 | | − unbox_sig: None, |
| 1078 | | − }; |
| 1079 | 1093 | let v = Atom::Var; |
| 1080 | | − let lets = |binds: Vec<(VarId, Value)>, end: Expr| { |
| 1081 | | − binds.into_iter().rev().fold(end, |rest, (var, value)| { |
| 1082 | | − Expr::Let(var, Box::new(value), Box::new(rest), None) |
| 1083 | | − }) |
| 1084 | | − }; |
| 1085 | 1094 | let compare = |a: VarId, b: VarId| { |
| 1086 | 1095 | Value::CallExtern("tl_int_compare".into(), vec![Atom::Var(a), Atom::Var(b)]) |
| 1087 | 1096 | }; |
| @@ -1125,7 +1134,7 @@ impl<'a> Lowerer<'a> { |
| 1125 | 1134 | cases: vec![(1, entry)], |
| 1126 | 1135 | default: Some(Box::new(missing())), |
| 1127 | 1136 | }; |
| 1128 | | − self.define(lookup, decl("ev$lookup", vec![0, 1, 2], body, 11)); |
| 1137 | + self.define(lookup, helper_decl("ev$lookup", vec![0, 1, 2], body, 11)); |
| 1129 | 1138 | // ev$remove(v0 ev, v1 key) |
| 1130 | 1139 | let keep = lets( |
| 1131 | 1140 | vec![ |
| @@ -1159,7 +1168,7 @@ impl<'a> Lowerer<'a> { |
| 1159 | 1168 | cases: vec![(1, entry)], |
| 1160 | 1169 | default: Some(Box::new(missing())), |
| 1161 | 1170 | }; |
| 1162 | | − self.define(remove, decl("ev$remove", vec![0, 1], body, 8)); |
| 1171 | + self.define(remove, helper_decl("ev$remove", vec![0, 1], body, 8)); |
| 1163 | 1172 | self.evidence_fns = Some((lookup, remove)); |
| 1164 | 1173 | (lookup, remove) |
| 1165 | 1174 | } |
| @@ -1508,7 +1517,7 @@ impl<'a> Lowerer<'a> { |
| 1508 | 1517 | let atoms = elems.iter().map(|x| self.expr(*x)).collect(); |
| 1509 | 1518 | self.emit(Value::Tuple(atoms)) |
| 1510 | 1519 | } |
| 1511 | | − hb::Expr::List { elems, tail } => self.list_literal(elems, *tail), |
| 1520 | + hb::Expr::List(elems) => self.list_literal(elems), |
| 1512 | 1521 | hb::Expr::Handle { |
| 1513 | 1522 | body: handled, |
| 1514 | 1523 | clauses, |
| @@ -1603,28 +1612,81 @@ impl<'a> Lowerer<'a> { |
| 1603 | 1612 | } |
| 1604 | 1613 | } |
| 1605 | 1614 | |
| 1606 | | − /// `[elems]` or `[elems, ..tail]`: the elements consed, last first, |
| 1607 | | − /// onto the tail (`Nil` without one). The elements are lowered first. |
| 1608 | | − fn list_literal(&mut self, elems: &[ExprId], tail: Option<ExprId>) -> Atom { |
| 1609 | | − let atoms: Vec<Atom> = elems.iter().map(|x| self.expr(*x)).collect(); |
| 1610 | | − let mut list = match tail { |
| 1611 | | − Some(t) => self.expr(t), |
| 1615 | + /// `[a, ..xs, b]`: every element and spread is lowered first, in |
| 1616 | + /// source order; then the list is built from its end. A last spread |
| 1617 | + /// is the tail as is (`[a, b, ..tail]` conses onto it, no copy); an |
| 1618 | + /// item is consed on; any other spread is appended (`list$append`, |
| 1619 | + /// which copies it). |
| 1620 | + fn list_literal(&mut self, elems: &[hb::ListElem]) -> Atom { |
| 1621 | + let parts: Vec<(bool, Atom)> = elems |
| 1622 | + .iter() |
| 1623 | + .map(|e| match e { |
| 1624 | + hb::ListElem::Item(x) => (false, self.expr(*x)), |
| 1625 | + hb::ListElem::Spread(xs) => (true, self.expr(*xs)), |
| 1626 | + }) |
| 1627 | + .collect(); |
| 1628 | + let mut parts = parts.into_iter().rev().peekable(); |
| 1629 | + let mut list = match parts.next_if(|(spread, _)| *spread) { |
| 1630 | + Some((_, tail)) => tail, |
| 1612 | 1631 | None => self.emit(Value::Construct { |
| 1613 | 1632 | tag: 0, |
| 1614 | 1633 | name: "Nil".into(), |
| 1615 | 1634 | args: Vec::new(), |
| 1616 | 1635 | }), |
| 1617 | 1636 | }; |
| 1618 | | − for a in atoms.into_iter().rev() { |
| 1619 | | − list = self.emit(Value::Construct { |
| 1620 | | − tag: 1, |
| 1621 | | − name: "Cons".into(), |
| 1622 | | − args: vec![a, list], |
| 1623 | | − }); |
| 1637 | + for (spread, a) in parts { |
| 1638 | + list = if spread { |
| 1639 | + let append = self.list_append_fn(); |
| 1640 | + self.emit(Value::CallFn(append, vec![a, list])) |
| 1641 | + } else { |
| 1642 | + self.emit(Value::Construct { |
| 1643 | + tag: 1, |
| 1644 | + name: "Cons".into(), |
| 1645 | + args: vec![a, list], |
| 1646 | + }) |
| 1647 | + }; |
| 1624 | 1648 | } |
| 1625 | 1649 | list |
| 1626 | 1650 | } |
| 1627 | 1651 | |
| 1652 | + /// `list$append(xs, ys)`: the elements of `xs` consed onto `ys`, the |
| 1653 | + /// prelude's `append_lists` written in Core, so a list literal does |
| 1654 | + /// not depend on a prelude function's calling convention. TRMC makes |
| 1655 | + /// it run in constant stack; a unique `xs` has its cells reused. Made |
| 1656 | + /// once per program. |
| 1657 | + fn list_append_fn(&mut self) -> FnId { |
| 1658 | + if let Some(append) = self.list_append_fn { |
| 1659 | + return append; |
| 1660 | + } |
| 1661 | + let append = self.placeholder_in("list$append".into(), None); |
| 1662 | + let v = Atom::Var; |
| 1663 | + // list$append(v0 xs, v1 ys) |
| 1664 | + let cons = lets( |
| 1665 | + vec![ |
| 1666 | + (2, Value::Field(v(0), 0)), |
| 1667 | + (3, Value::Field(v(0), 1)), |
| 1668 | + (4, Value::CallFn(append, vec![v(3), v(1)])), |
| 1669 | + ( |
| 1670 | + 5, |
| 1671 | + Value::Construct { |
| 1672 | + tag: 1, |
| 1673 | + name: "Cons".into(), |
| 1674 | + args: vec![v(2), v(4)], |
| 1675 | + }, |
| 1676 | + ), |
| 1677 | + ], |
| 1678 | + Expr::Return(v(5)), |
| 1679 | + ); |
| 1680 | + let body = Expr::Switch { |
| 1681 | + on: v(0), |
| 1682 | + cases: vec![(1, cons)], |
| 1683 | + default: Some(Box::new(Expr::Return(v(1)))), |
| 1684 | + }; |
| 1685 | + self.define(append, helper_decl("list$append", vec![0, 1], body, 6)); |
| 1686 | + self.list_append_fn = Some(append); |
| 1687 | + append |
| 1688 | + } |
| 1689 | + |
| 1628 | 1690 | /// `region { body }`: the body as a closure taking the region's |
| 1629 | 1691 | /// capability, run in a fresh region of `std/ref`'s `alloc` and `st`. |
| 1630 | 1692 | fn region(&mut self, region_body: ExprId) -> Atom { |