Documentation

CslibFrontier.Computability.PointerMachine.WordRAM.Memory

Pointer-machine heaps in word memory #

A node occupies a fixed block of words. Null is zero; node a starts at 1 + stride L * a. Pointer fields precede data fields. The register layout keeps the source banks, the next free address, a zero register, and four temporary registers.

Repr describes the represented state without constraining temporary registers or unused memory. The update lemmas connect source register and field updates to word-memory updates.

@[reducible, inline]

Source register banks, next-free and zero registers, and four temporary registers.

The target register containing a source pointer register.

The target register containing a source data register.

The first address after all allocated records.

A register that remains zero.

Temporary address register.

Temporary value register.

@[simp]
theorem Cslib.PointerMachine.WordRAM.ptrReg_val (L : Layout) (r : Fin L.ptrRegs) :
↑(ptrReg L r) = ↑r
@[simp]

Each node occupies at least one cell, even when it has no fields.

The base address of node a, reserving zero for null.

Null becomes zero and nonnull pointers become node base addresses.

Address of offset f within node a. Pointer offsets precede data offsets.

structure Cslib.PointerMachine.WordRAM.Repr {M : Model} {L : Layout} (e : M.Value ↪ ℕ) (x : State M L) (y : WordRAM.Data (regCount L)) :

A source state represented in target registers and allocated memory.

structure Cslib.PointerMachine.WordRAM.Fits {M : Model} {L : Layout} (e : M.Value ↪ ℕ) (w : ℕ) (x : State M L) :

The next-free address and every represented datum fit in a word. Validity separately ensures pointer words and occupied cell addresses are below the next-free address.

Pointer fields lie within their node's block.

Data fields follow the pointer fields within their node's block.

theorem Cslib.PointerMachine.WordRAM.cellAddr_lt {L : Layout} {a n i : ℕ} (ha : a < n) (hi : i < stride L) :
cellAddr L a i < nodeAddr L n

A cell of an allocated node precedes the first free address.

theorem Cslib.PointerMachine.WordRAM.cellAddr_injective {L : Layout} {a b i j : ℕ} (hi : i < stride L) (hj : j < stride L) (h : cellAddr L a i = cellAddr L b j) :
a = b ∧ i = j

Different nodes and offsets designate different cells.

Pointer words retain pointer equality, including null.

theorem Cslib.PointerMachine.WordRAM.Fits.cellAddr {M : Model} {L : Layout} {e : M.Value ↪ ℕ} {w : ℕ} {x : State M L} (h : Fits e w x) {a i : ℕ} (ha : a < x.heap.length) (hi : i < stride L) :

Occupied field addresses fit whenever the next-free address does.

theorem Cslib.PointerMachine.WordRAM.Fits.ptrWord {M : Model} {L : Layout} {e : M.Value ↪ ℕ} {w : ℕ} {x : State M L} (h : Fits e w x) {p : Pointer} (hp : ∀ (a : ℕ), p = some a → a < x.heap.length) :

Null or a pointer to an allocated record fits in the target word size.

theorem Cslib.PointerMachine.WordRAM.Repr.with_pc {M : Model} {L : Layout} {e : M.Value ↪ ℕ} {x : State M L} {y : WordRAM.Data (regCount L)} (h : Repr e x y) (pc : ℕ) :
Repr e { pc := pc, ptr := x.ptr, data := x.data, heap := x.heap } y

The representation ignores the source control label.

theorem Cslib.PointerMachine.WordRAM.Repr.of_frame {M : Model} {L : Layout} {e : M.Value ↪ ℕ} {x : State M L} {y z : WordRAM.Data (regCount L)} (h : Repr e x y) (hr : ∀ (r : Fin (regCount L)), ↑r < L.ptrRegs + L.dataRegs + 2 → z.regs r = y.regs r) (hm : z.mem = y.mem) :
Repr e x z

Changing temporary registers preserves the represented state.

theorem Cslib.PointerMachine.WordRAM.Repr.scratch {M : Model} {L : Layout} {e : M.Value ↪ ℕ} {x : State M L} {y : WordRAM.Data (regCount L)} (h : Repr e x y) (r : Fin (regCount L)) (hr : L.ptrRegs + L.dataRegs + 2 ≤ ↑r) (v : ℕ) :
Repr e x (y.set r v)

Writing a temporary register leaves the represented state unchanged.

theorem Cslib.PointerMachine.WordRAM.Repr.setPtr {M : Model} {L : Layout} {e : M.Value ↪ ℕ} {x : State M L} {y : WordRAM.Data (regCount L)} (h : Repr e x y) (r : Fin L.ptrRegs) (p : Pointer) :
Repr e (x.setPtr r p) (y.set (ptrReg L r) (ptrWord L p))

A pointer-register update is one corresponding word-register update.

theorem Cslib.PointerMachine.WordRAM.Repr.setData {M : Model} {L : Layout} {e : M.Value ↪ ℕ} {x : State M L} {y : WordRAM.Data (regCount L)} (h : Repr e x y) (s : Fin L.dataRegs) (v : M.Value) :
Repr e (x.setData s v) (y.set (dataReg L s) (e v))

A data-register update is one corresponding word-register update.

theorem Cslib.PointerMachine.WordRAM.Repr.setData_of_frame {M : Model} {L : Layout} {e : M.Value ↪ ℕ} {x : State M L} {y z : WordRAM.Data (regCount L)} (h : Repr e x y) (s : Fin L.dataRegs) (v : M.Value) (hv : z.regs (dataReg L s) = e v) (hr : ∀ (r : Fin (regCount L)), ↑r < L.ptrRegs + L.dataRegs + 2 → r ≠ dataReg L s → z.regs r = y.regs r) (hm : z.mem = y.mem) :
Repr e (x.setData s v) z

Transfer a scalar result while allowing temporary registers to change.

theorem Cslib.PointerMachine.WordRAM.Repr.writePtr {M : Model} {L : Layout} {e : M.Value ↪ ℕ} {x : State M L} {y : WordRAM.Data (regCount L)} (h : Repr e x y) (a : ℕ) (f : Fin L.ptrFields) (p : Pointer) :
Repr e { pc := x.pc, ptr := x.ptr, data := x.data, heap := x.heap.modify a fun (n : Record M L) => { ptr := Function.update n.ptr f p, data := n.data } } (y.write (cellAddr L a ↑f) (ptrWord L p))

Storing a pointer field is a single word-memory update.

theorem Cslib.PointerMachine.WordRAM.Repr.writeData {M : Model} {L : Layout} {e : M.Value ↪ ℕ} {x : State M L} {y : WordRAM.Data (regCount L)} (h : Repr e x y) (a : ℕ) (f : Fin L.dataFields) (v : M.Value) :
Repr e { pc := x.pc, ptr := x.ptr, data := x.data, heap := x.heap.modify a fun (n : Record M L) => { ptr := n.ptr, data := Function.update n.data f v } } (y.write (cellAddr L a (L.ptrFields + ↑f)) (e v))

Storing a data field is a single word-memory update.

theorem Cslib.PointerMachine.WordRAM.Repr.append {M : Model} {L : Layout} {e : M.Value ↪ ℕ} {x : State M L} {y z : WordRAM.Data (regCount L)} (h : Repr e x y) (n : Record M L) (hr : ∀ (r : Fin (regCount L)), ↑r < L.ptrRegs + L.dataRegs + 2 → r ≠ freeReg L → z.regs r = y.regs r) (hf : z.regs (freeReg L) = nodeAddr L (x.heap.length + 1)) (hm : ∀ a < nodeAddr L x.heap.length, z.mem a = y.mem a) (hp : ∀ (f : Fin L.ptrFields), z.mem (cellAddr L x.heap.length ↑f) = ptrWord L (n.ptr f)) (hd : ∀ (f : Fin L.dataFields), z.mem (cellAddr L x.heap.length (L.ptrFields + ↑f)) = e (n.data f)) :
Repr e { pc := x.pc, ptr := x.ptr, data := x.data, heap := x.heap ++ [n] } z

Appending an initialized record preserves representation. The implementation must preserve old occupied cells, initialize the new fields, and advance the next-free register.