Dynamic virtual-width memory-window adapters #
MemoryWindow.compile translates a width-independent r-register program to run inside a
caller-owned physical memory interval [b, b + 2 ^ w₀) when Reg.base holds b and
Reg.modulus holds 2 ^ w₀. When a caller invokes that translated routine at an unknown physical
word width W ≥ w₀, it computes the virtual modulus 2 ^ w₀ at runtime from an input-derived
scalar size S (2 ≤ S) without reading W:
NatClog.programcomputes⌈log₂ S⌉in5 ⌈log₂ S⌉ + 5instructions;modulusArgsmultiplies⌈log₂ S⌉by the fixed constantwidthCoeffto formw₀ = widthCoeff * ⌈log₂ S⌉and loads base2in3instructions;Power.programcomputes2 ^ w₀in4 w₀ + 5instructions;MemoryWindow.normalizeRegistersreduces allrsource registers modulo2 ^ w₀inr + 1instructions without clearing the caller's dirty memory window;MemoryWindow.compile calleeexecutes the translated callee in3 * Tinstructions, clipping dirty loads modulo2 ^ w₀and preserving all memory outside[b, b + 2 ^ w₀).
This module provides both the in-place Data (r + 3) setup (dynamicSetup, where five low source
registers serve as temporary scratch before register normalization) and the embedded-bank adapter
(dynamicSetupBank and dynamicCallBank, where the window bank Fin (r + 3) → Fin R and five
scratch registers Fin 5 → Fin R sit inside a larger caller register file Data R).
Main definitions #
Cslib.WordRAM.MemoryWindow.dynamicWidth,Cslib.WordRAM.MemoryWindow.dynamicWindowSize: virtual source widthwidthCoeff * Nat.clog 2 Sand window size2 ^ dynamicWidth widthCoeff S.Cslib.WordRAM.MemoryWindow.modulusArgs,Cslib.WordRAM.MemoryWindow.withDynamicModulus: runtime computation of2 ^ (widthCoeff * Nat.clog 2 S)in a 5-register scratch bank.Cslib.WordRAM.MemoryWindow.dynamicSetup: in-place modulus computation and source-register normalization onData (r + 3).Cslib.WordRAM.MemoryWindow.dynamicSetupBank,Cslib.WordRAM.MemoryWindow.dynamicCallBank: embedded-bank dynamic window setup and complete setup-plus-callee execution onData R.
Main results #
Cslib.WordRAM.MemoryWindow.dynamicWindowSize_bounds,Cslib.WordRAM.MemoryWindow.dynamicWidth_fits: parameter and physical-width fit bounds fromFits W (b + 4 * dynamicWindowSize widthCoeff S).Cslib.WordRAM.MemoryWindow.exec_withDynamicModulus: runtime modulus computation in(4 * widthCoeff + 5) * Nat.clog 2 S + 13instructions.Cslib.WordRAM.MemoryWindow.exec_dynamicSetup,Cslib.WordRAM.MemoryWindow.size_dynamicSetup,Cslib.WordRAM.MemoryWindow.dynamicSetup_not_readsWidth: execution, code size, and width blindness of in-place setup.Cslib.WordRAM.MemoryWindow.exec_dynamicSetupBank,Cslib.WordRAM.MemoryWindow.size_dynamicSetupBank,Cslib.WordRAM.MemoryWindow.dynamicSetupBank_not_readsWidth,Cslib.WordRAM.MemoryWindow.preserves_dynamicSetupBank: execution, code size, width blindness, and register preservation of embedded-bank setup.Cslib.WordRAM.MemoryWindow.exec_dynamicCallBank,Cslib.WordRAM.MemoryWindow.size_dynamicCallBank,Cslib.WordRAM.MemoryWindow.dynamicCallBank_not_readsWidth,Cslib.WordRAM.MemoryWindow.preserves_dynamicCallBank: complete dynamic-window call properties.Cslib.WordRAM.MemoryWindow.view_mem_init_of_inputFits: source-view memory agreement on an initialized input prefix admitted at the source width.
Virtual source width widthCoeff * ⌈log₂ S⌉ computed from a runtime scalar size S.
Virtual address-space reservation 2 ^ (widthCoeff * ⌈log₂ S⌉).
For 1 ≤ widthCoeff and 2 ≤ S, the dynamic window covers S, strictly exceeds its
exponent dynamicWidth widthCoeff S, and strictly exceeds widthCoeff.
When b + 4 * dynamicWindowSize widthCoeff S fits in physical width W (with
1 ≤ widthCoeff and 2 ≤ S), dynamicWidth widthCoeff S + 2 ≤ W, the window endpoint
b + dynamicWindowSize widthCoeff S fits in W, and dynamicWindowSize_bounds holds.
Multiply ⌈log₂ S⌉ in scratch 1 by widthCoeff into scratch 3 and load base 2 into
scratch 2 for Power.program.
From S in scratch 0, compute 2 ^ (widthCoeff * Nat.clog 2 S) into scratch 0 using
NatClog.program and Power.program, then execute cont.
Computing the dynamic modulus prepends exactly 20 assembled instructions to cont.
Dynamic modulus computation reads the physical width if and only if cont does.
Dynamic modulus computation preserves every register outside scratch that cont preserves.
Execute NatClog.program, multiply by widthCoeff, and run Power.program in scratch,
delivering dynamicWindowSize widthCoeff S in scratch 0 with unchanged memory and external
registers to cont.
Code size of dynamicSetup.
dynamicSetup never reads the physical word width.
Execute dynamicSetup on Data (r + 3) when Reg.base holds b and scratch 0 holds S.
All memory words survive and the resulting source view is bounded at dynamicWidth widthCoeff S.
Compute 2 ^ (widthCoeff * Nat.clog 2 S) in scratch, copy baseReg and the computed
modulus into winBank, and normalize the r embedded source registers.
Code size of dynamicSetupBank.
dynamicSetupBank never reads the physical word width.
dynamicSetupBank preserves every register outside scratch and winBank.
Execute dynamicSetupBank on Data R when baseReg holds b and scratch 0 holds S.
All memory words and all registers outside scratch and winBank are preserved, and the
embedded window bank holds normalized source registers with modulus
dynamicWindowSize widthCoeff S.
Complete dynamic-window call in an embedded bank: compute 2 ^ (widthCoeff * Nat.clog 2 S) in
scratch, initialize winBank from baseReg and scratch 0, normalize the r source registers,
and execute (MemoryWindow.compile callee).rename winBank.
Code-size bound for dynamicCallBank.
dynamicCallBank never reads the physical word width when callee does not.
dynamicCallBank preserves every register outside scratch and winBank.
Execute a width-independent callee in a dynamic virtual-width window [b, b + 2 ^ w₀)
(with w₀ = dynamicWidth widthCoeff S), preserving all memory outside [b, b + 2 ^ w₀) and all
registers outside scratch and winBank.
Whenever inp fits at width w and physical memory [b, b + L) equals (Data.init inp).mem,
the clipped source view view w b x agrees on [0, L) with Data.init inp.