Skip to content
3 changes: 3 additions & 0 deletions Backend/Clos/Convert.lean
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ def Op.ofPrim? : Prim → Option Op
| .free => some .free
| .load => some .load
| .host op => some (.host op)
| .tab => some .tab
| .idx => some .idx
| .drop => some .drop
| .store => none
| .lit _ => none

Expand Down
102 changes: 87 additions & 15 deletions Backend/Clos/Interp.lean
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import Std.Data.HashMap
import Support.Dynamics.Host
import Support.Dynamics.Arrays
import Backend.Value
import Backend.Clos.Syntax

/-!
# Backend — closure-IR interpreter

A fuelled interpreter for the closure IR over an explicit heap of objects (pairs, injections,
closures, boxed `i64`s, cells), counting live objects. It checks the ownership discipline as it
closures, boxed `i64`s, cells), counting live objects. An array is consecutive cells, as in the
source machine (SPEC §9). It checks the ownership discipline as it
runs: using a variable consumes it, a consumed variable cannot be used again, and a call or
`main` must end with every variable of its frame consumed. Host operations go through
`Host.call` at `wasmHost`, as in the source interpreter.
Expand Down Expand Up @@ -131,6 +133,18 @@ def cellOf (v : CV) : IM (Nat × CV) := do

def objFuel : Nat := 1 <<< 20

/-- Take a pair apart, freeing it. -/
def unpair (v : CV) : IM (CV × CV) := do
match v with
| .ptr a => match ← getObj a with
| .pair x y => do freeObj a; pure (x, y)
| _ => throw "closure IR: not a pair"
| _ => throw "closure IR: not a pair"

/-- An array of `n` elements is `max n 1` consecutive cells (SPEC §9); the handle is
`(base, i32[n])`. The extra cell of an empty array keeps the base an object. -/
def arrSlots (n : Nat) : Nat := max n 1

def applyOp : Op → List CV → IM CV
| .alloc, [v] => allocObj (.cell v)
| .free, [l] => do let (a, c) ← cellOf l; freeObj a; pure c
Expand All @@ -146,8 +160,58 @@ def applyOp : Op → List CV → IM CV
match wasmHost.call op sv with
| some r => ofSource r.1
| none => throw "closure IR: host operation stuck"
| .idx, [v] => do
let (arr, i) ← unpair v
let (base, n) ← unpair arr
match base, n, i with
| .ptr a, .i32 n, .i32 i =>
if i < n then allocObj (.inj false (.ptr (a + i.toNat)))
else allocObj (.inj true .unit)
| _, _, _ => throw "closure IR: idx of a non-array"
| _, _ => throw "closure IR: primitive arity"

mutual
/-- Call the closure `f` on `a`; the call frees `f`'s environment record. -/
def callClos (codes : Array Code) : Nat → CV → CV → IM CV
| 0, _, _ => throw "closure IR: out of fuel"
| n + 1, vf, va => do
match vf with
| .ptr p =>
match ← getObj p with
| .clos c vs => do
freeObj p
let some code := codes[c]? | throw "closure IR: bad code index"
if code.env.length != vs.length then throw "closure IR: environment size"
let frame := (code.param, va) :: code.env.zip vs
let (r, rest) ← eval codes n frame code.body
if !rest.isEmpty then
throw s!"closure IR: code {c} leaves {rest.map (·.1)} unconsumed"
pure r
| _ => throw "closure IR: call of a non-closure"
| _ => throw "closure IR: call of a non-closure"

/-- `tab`'s fill: cell `a + i` gets `f i`, for `i < k`, each call on a copy of `f`. -/
def tabFill (codes : Array Code) : Nat → CV → Nat → Nat → Nat → IM Unit
| 0, _, _, _, _ => throw "closure IR: out of fuel"
| _ + 1, _, _, _, 0 => pure ()
| n + 1, f, a, i, k + 1 => do
let fc ← deepCopy objFuel f
let r ← callClos codes n fc (.i32 (UInt32.ofNat i))
setObj (a + i) (.cell r)
tabFill codes n f a (i + 1) k

/-- `drop`'s drain: cell `a + i` is freed and its contents handed to a copy of `f`. -/
def dropFill (codes : Array Code) : Nat → CV → Nat → Nat → Nat → IM Unit
| 0, _, _, _, _ => throw "closure IR: out of fuel"
| _ + 1, _, _, _, 0 => pure ()
| n + 1, f, a, i, k + 1 => do
let (_, c) ← cellOf (.ptr (a + i))
freeObj (a + i)
let fc ← deepCopy objFuel f
let r ← callClos codes n fc c
deepDrop objFuel r
dropFill codes n f a (i + 1) k

/-- Evaluate `t` in the frame `env`: its value and what is left of the frame. -/
def eval (codes : Array Code) : Nat → Env → Tm → IM (CV × Env)
| 0, _, _ => throw "closure IR: out of fuel"
Expand All @@ -167,26 +231,33 @@ def eval (codes : Array Code) : Nat → Env → Tm → IM (CV × Env)
| .clos c xs => do
let (vs, env) ← takeAll env xs
pure (← allocObj (.clos c vs), env)
| .prim .tab [x] => do
let (v, env) ← take env x
let (c, f) ← unpair v
let .i32 c := c | throw "closure IR: tab of a non-length"
let len := (arrLen c).toNat
let base := (← get).next
for _ in List.range (arrSlots len) do discard (allocObj (.cell .unit))
tabFill codes n f base 0 len
deepDrop objFuel f
pure (← allocObj (.pair (.ptr base) (.i32 (arrLen c))), env)
| .prim .drop [x] => do
let (v, env) ← take env x
let (f, arr) ← unpair v
let (base, len) ← unpair arr
let .ptr a := base | throw "closure IR: drop of a non-array"
let .i32 len := len | throw "closure IR: drop of a non-array"
dropFill codes n f a 0 len.toNat
deepDrop objFuel f
if len.toNat = 0 then freeObj a
pure (.unit, env)
| .prim op xs => do
let (vs, env) ← takeAll env xs
pure (← applyOp op vs, env)
| .app f a => do
let (vf, env) ← take env f
let (va, env) ← take env a
match vf with
| .ptr p =>
match ← getObj p with
| .clos c vs => do
freeObj p
let some code := codes[c]? | throw "closure IR: bad code index"
if code.env.length != vs.length then throw "closure IR: environment size"
let frame := (code.param, va) :: code.env.zip vs
let (r, rest) ← eval codes n frame code.body
if !rest.isEmpty then
throw s!"closure IR: code {c} leaves {rest.map (·.1)} unconsumed"
pure (r, env)
| _ => throw "closure IR: call of a non-closure"
| _ => throw "closure IR: call of a non-closure"
pure (← callClos codes n vf va, env)
| .letv x e b => do
let (v, env) ← eval codes n env e
eval codes n ((x, v) :: env) b
Expand Down Expand Up @@ -215,6 +286,7 @@ def eval (codes : Array Code) : Nat → Env → Tm → IM (CV × Env)
let (v, env) ← take env x
deepDrop objFuel v
eval codes n env b
end

/-- The observable form of a word. -/
def observe : Nat → CV → IM PV
Expand Down
8 changes: 7 additions & 1 deletion Backend/Clos/Syntax.lean
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ open BoCa

abbrev Var := Nat

/-- The primitives of `[TR]` §1 and §8, saturated. -/
/-- The primitives of `[TR]` §1, §8 and §9, saturated. -/
inductive Op where
/-- `alloc v`: a fresh cell holding `v`. -/
| alloc
Expand All @@ -35,6 +35,12 @@ inductive Op where
| store
/-- A host operation of §8, applied to its (consumed) argument. -/
| host (op : HostOp)
/-- `tab (n, f)`: an array of `len(n)` slots, slot `i` holding `f i` (§9). -/
| tab
/-- `idx ((ℓ, n), i)`: `inl` of the `i`-th slot's location when `i < n`, else `inr ()` (§9). -/
| idx
/-- `drop (f, (ℓ, n))`: each slot's contents to `f`, in order, then the array freed (§9). -/
| drop
deriving Repr, DecidableEq

inductive Tm where
Expand Down
3 changes: 3 additions & 0 deletions Backend/Flat/Lower.lean
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def lowerTm (hostIdx : HostOp → Nat) : Tm → Dest → LM (List Stmt)
return [.eval (.call fDrop [.load l 1]), .store l 1 (← lv v)] ++ emit d (.c64 unitW)
| .prim (.host op) [v], d => do
return emit d (.call (hostIdx op) [← lv v])
| .prim .tab [v], d => do return emit d (.call fArrTab [← lv v])
| .prim .idx [v], d => do return emit d (.call fArrIdx [← lv v])
| .prim .drop [v], d => do return emit d (.call fArrDrop [← lv v])
| .prim _ _, _ => throw "flat IR: primitive arity"
| .app f a, d => do
let f ← lv f
Expand Down
114 changes: 111 additions & 3 deletions Backend/Flat/Runtime.lean
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Backend.Flat.Syntax
/-!
# Backend — the flat IR's runtime functions

Deep copy, deep drop and the printer, as flat-IR functions over the representation of
`Backend/Flat/Syntax.lean`. Cells are shared by a copy and left alone by a drop: a location is
Deep copy, deep drop, the printer and the array operations, as flat-IR functions over the
representation of `Backend/Flat/Syntax.lean`. Cells are shared by a copy and left alone by a drop: a location is
copied as a word, and cells are freed only by `free`. The printer writes the text of
`Backend.PV.render`.

Expand All @@ -20,6 +20,12 @@ def fDropEnv : Nat := 3
def fPrint : Nat := 4
def fPrintAtom : Nat := 5
def fPrintDec : Nat := 6
def fArrTab : Nat := 7
def fArrInit : Nat := 8
def fArrTabLoop : Nat := 9
def fArrIdx : Nat := 10
def fArrDrop : Nat := 11
def fArrDropLoop : Nat := 12

/-- `copy w`: locals `w`, `h`, `t`, `n`. -/
def copyFn : Fn where
Expand Down Expand Up @@ -141,7 +147,109 @@ def printDecFn : Fn where
.out (add64 (.c64 48) (.op (.div .w64 .rem_u) [n, .c64 10])),
.ret (.c64 0) ]

/-! ### Arrays `[spec §9]`

An array of `n` elements is one range of `max n 1` slots of two words each: a cell header and
the contents. Slot `i` is at `base + 16·i`, and is an ordinary cell object, so its address is
the element's location and `load`, `store`, a copy and a drop treat it as any cell. The array
value is the pair `(base, i32[n])`. An empty array keeps one slot, so the base is an object. -/

/-- The address of slot `i`. -/
def slotAddr (b i : Exp) : Exp := add64 b (shl64 i (.c64 4))

/-- `i32[i]` from a 64-bit `i` below `2³²`. -/
def boxIdx (i : Exp) : Exp := or64 (shl64 i (.c64 32)) (.c64 3)

/-- `array_tab p`, `p = (i32[c], f)`: `len(c)` slots filled by `f i` in order. Locals `p`,
`n`, `f`, `b`, `s` (the slot count), `t`. -/
def arrTabFn : Fn where
name := "array_tab"
params := 1
locals := 5
body :=
let p := Exp.loc 0; let n := Exp.loc 1; let f := Exp.loc 2; let b := Exp.loc 3
let s := Exp.loc 4
[ .set 1 (shr64 (.load p 1) (.c64 32)), .set 2 (.load p 2), .free p,
.ite (ltu64 n (.c64 0x80000000)) [] [.set 1 (.c64 0)],
.set 4 n, .ite (eq64 n (.c64 0)) [.set 4 (.c64 1)] [],
.set 3 (.alloc (shl64 s (.c64 1))),
.eval (.call fArrInit [b, .c64 0, s]),
.eval (.call fArrTabLoop [b, f, .c64 0, n]),
.eval (.call fDrop [f]) ] ++
mkObj 5 hPair [b, boxIdx n] ++ [.ret (.loc 5)]

/-- `arrInit b i s`: slots `i … s-1` become cells holding `()`. -/
def arrInitFn : Fn where
name := "array_init"
params := 3
locals := 0
body :=
let b := Exp.loc 0; let i := Exp.loc 1; let s := Exp.loc 2
[ .ite (ltu64 i s)
[ .store (slotAddr b i) 0 (.c64 hCell), .store (slotAddr b i) 1 (.c64 unitW),
.tailCall fArrInit [b, add64 i (.c64 1), s] ] [],
.ret (.c64 0) ]

/-- `arrTabLoop b f i n`: slot `i` gets a copy of `f` applied to `i32[i]`, for `i < n`. -/
def arrTabLoopFn : Fn where
name := "array_tab_loop"
params := 4
locals := 2
body :=
let b := Exp.loc 0; let f := Exp.loc 1; let i := Exp.loc 2; let n := Exp.loc 3
let fc := Exp.loc 4
[ .ite (ltu64 i n)
[ .set 4 (.call fCopy [f]),
.set 5 (.callInd (.load fc 1) [fc, boxIdx i]),
.store (slotAddr b i) 1 (.loc 5),
.tailCall fArrTabLoop [b, f, add64 i (.c64 1), n] ] [],
.ret (.c64 0) ]

/-- `array_idx p`, `p = ((b, i32[n]), i32[i])`: `inl` of slot `i` when `i < n` unsigned, else
`inr ()`. Locals `p`, `q`, `w`, `b`, `n`, `t`. -/
def arrIdxFn : Fn where
name := "array_idx"
params := 1
locals := 5
body :=
let p := Exp.loc 0; let q := Exp.loc 1; let w := Exp.loc 2; let b := Exp.loc 3
let n := Exp.loc 4
[ .set 1 (.load p 1), .set 2 (.load p 2), .free p,
.set 3 (.load q 1), .set 4 (shr64 (.load q 2) (.c64 32)), .free q,
.ite (ltu64 (shr64 w (.c64 32)) n)
(mkObj 5 hInj₁ [slotAddr b (shr64 w (.c64 32))] ++ [.ret (.loc 5)])
(mkObj 5 hInj₂ [.c64 unitW] ++ [.ret (.loc 5)]) ]

/-- `array_drop p`, `p = (f, (b, i32[n]))`: each element to a copy of `f`, in order, then the
range freed. Locals `p`, `f`, `q`, `b`, `n`. -/
def arrDropFn : Fn where
name := "array_drop"
params := 1
locals := 4
body :=
let p := Exp.loc 0; let f := Exp.loc 1; let q := Exp.loc 2; let b := Exp.loc 3
let n := Exp.loc 4
[ .set 1 (.load p 1), .set 2 (.load p 2), .free p,
.set 3 (.load q 1), .set 4 (shr64 (.load q 2) (.c64 32)), .free q,
.eval (.call fArrDropLoop [b, f, .c64 0, n]),
.eval (.call fDrop [f]), .free b, .ret (.c64 unitW) ]

/-- `arrDropLoop b f i n`: slot `i`'s contents, moved out, to a copy of `f`, for `i < n`. -/
def arrDropLoopFn : Fn where
name := "array_drop_loop"
params := 4
locals := 1
body :=
let b := Exp.loc 0; let f := Exp.loc 1; let i := Exp.loc 2; let n := Exp.loc 3
let fc := Exp.loc 4
[ .ite (ltu64 i n)
[ .set 4 (.call fCopy [f]),
.eval (.call fDrop [.callInd (.load fc 1) [fc, .load (slotAddr b i) 1]]),
.tailCall fArrDropLoop [b, f, add64 i (.c64 1), n] ] [],
.ret (.c64 0) ]

def runtimeFns : List Fn :=
[copyFn, dropFn, copyEnvFn, dropEnvFn, printFn, printAtomFn, printDecFn]
[copyFn, dropFn, copyEnvFn, dropEnvFn, printFn, printAtomFn, printDecFn,
arrTabFn, arrInitFn, arrTabLoopFn, arrIdxFn, arrDropFn, arrDropLoopFn]

end Backend.Flat
3 changes: 3 additions & 0 deletions Backend/Flat/Syntax.lean
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Objects start with a header word; bits 0–7 are the kind:
| `i64[n]` | `5` | the raw 64 bits of `n` |
| cell | `6` | the contents |

An array (SPEC §9) is one range of `max n 1` cells, two words each, and its value is the pair
`(base, i32[n])`; the cell at `base + 16·i` is element `i` (`Backend/Flat/Runtime.lean`).

`i64` is boxed because the erasure is untyped: a deep copy or drop must tell a pointer from an
immediate by the word alone.
-/
Expand Down
9 changes: 9 additions & 0 deletions Backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ byte is its kind, and its fields:
| `i64[n]` | `5` | the 64 bits |
| cell | `6` | the contents |

An array (SPEC §9) is one allocation of `max n 1` slots of two words, each an ordinary cell
(header `6`, then the contents); element `i` is the slot at `base + 16·i`, so its location is a
cell address and `load`, `store`, copies and drops treat it as any cell. The array value is the
pair `(base, i32[n])`. `idx` compares the index with `n` unsigned and computes the address;
`tab` allocates the slots and fills them in a loop, calling a copy of the tabulator per element;
`drop` moves each element to a copy of the consumer and frees the allocation once
(`Backend/Flat/Runtime.lean`). The closure IR keeps an array as consecutive cells, as the source
machine does. A type-directed layout would drop the header, making the slot `sizeof A`.

`i64` is boxed: the erasure is untyped, so a deep copy or drop tells a pointer from an
immediate by the word alone, and a full-width `i64` leaves no bit for that. A type-directed
layout would unbox it.
Expand Down
4 changes: 4 additions & 0 deletions Core.lean
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Core.Types
import Core.SepRange
import Core.FrameRecords
import Core.World
import Core.ShapeMu
Expand Down Expand Up @@ -51,3 +52,6 @@ import Core.StreamPrograms
import Core.Integers
import Core.IntPrograms
import Core.Dtor
import Core.ArrayWp
import Core.Arrays
import Core.ArrayPrograms
Loading
Loading