A Type-based Analysis for Stack Allocation in Functional Languages

We consider the problem of detecting when bindings for variables in a higher-order, call-by-value functional language can be allocated on a stack. We use an annotated type system to infer information about the occurrence and extent of variables in a simple higher-order functional language and combine this system with a translation to an annotated language which explicitly indicates when stack allocation can be performed. This technique supports both stack and heap allocation of variable bindings. Only the stack allocated bindings need follow the protocol for stacks: their extent must coincide with their scope. Heap allocated bindings can have any extent and their allocation has no impact on the stack allocated ones. The type system uses a simple notion of annotated types in which types encode not only the typical simple type information, but also information about the use of variables. We demonstrate the use of our analysis by defining both an operational semantics and an abstract machine which utilize both a stack and an environment for variable allocation.