Regions as Continuation Marks
Abstract
Region-based memory management started as a compiler pass to optimize Standard ML programs. Garbage collection was expensive, manual memory management was unsafe, and so instead one infers a stack of memory regions to enable the compiler to insert explicit memory allocation and deallocation instructions. The result of region inference is a ML program with lexically-scoped regions. Unfortunately, the stack-based region inference interferes with the correct behaviour of tail recursion: that tail recursion must run in constant space, and much work was required to correct their behaviour in practice. We show a formal connection between these lexical regions and continuation marks, which elucidates the behaviour of regions with respect to tail calls, and provide insights into possible implementation techniques and new semantics. Continuation marks enable placing a value on the dynamic program frame, and were designed and implemented to behave correctly with respect to tail recursion. We provide a translation from regions to continuation marks and theorize what this connection could entail for both languages with region-based memory management and our favourite Schemes (and Racket) with continuation marks.