[LLVMdev] Efficient implementation of closures?

Nick Johnson npjohnso at cs.princeton.edu
Sun Dec 28 08:11:36 PST 2008


On Sun, Dec 28, 2008 at 12:53 AM, Talin <viridia at gmail.com> wrote:
> A somewhat random question: I'm wondering if there's any kind of trick
> in LLVM that would allow me to implement closures efficiently.
>
There is a third option.   Let me explain using C++ terminology.

I assume that all escaping variables are in a heap-allocated
activation record.  Represent a closure as a class which overloads the
parenthesis operator.  When you construct that class, you give a
reference to the activation record as one of the parameters to the
constructor, and the class holds that reference as a field.  The
closure can be re-written to access escaping variables indirectly
through that reference.

Calling a closure is then no different than a virtual method call, and
there is no need to change the calling convention, or to mix-and-match
calling conventions.

Nick



More information about the llvm-dev mailing list