Closure Conversion and Free-Variable Layout
Rewrite nested functions into code plus an explicit environment and account for copied values, shared locations, and recursive binding.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 2
- Reading time
- 1 min
In this article2 chapters
Replace symbolic free occurrences with environment accesses
For a nested function computing \(a+x\), where \(x\) is its argument and \(a\) comes from outside, create a closed function taking an environment and \(x\). Replace the occurrence of \(a\) by a read from a designated environment slot. At the original function creation point, allocate or reuse an environment storing that binding. Calls pass the environment alongside the ordinary arguments.
Shape the environment around lifetime and aliasing
Two closures sharing a mutable source binding must refer to the same location. With immutable \(a\), a copied value can be enough. Mutually recursive closures may need a group of associated code pointers and environments so recursive references are initialized consistently. Lexical closure semantics determines the meaning to preserve. Escape analysis can remove some allocations when their identity does not become observable.[1][2]