[cfe-dev] Nested functions in Clang?

Rodney M. Bates via cfe-dev cfe-dev at lists.llvm.org
Mon Sep 14 17:11:53 PDT 2015

On 09/14/2015 03:32 AM, David Chisnall wrote:
> On 13 Sep 2015, at 22:03, Rodney M. Bates <rodney_bates at lcwb.coop> wrote:
>> I intend to pass some kind of environment pointer, like a static link.  The real question
>> is, what does it point to?  As I understand, llvm locals can not be used outside the
>> function they are created in, so using a static link from outside can't access the
>> alloca values.  I am thinking the best way is to wrap at least up-level referenceable
>> formals and locals  in a struct, and have the static link point to that.  This would
>> include the next outer static link.
> The pointer will be to a struct that contains either the variables or the addresses of the variables, depending on the binding rules of your language.  Appleā€™s blocks ABI promotes all local accesses to use an indirection pointer and puts pointers in the (on-stack) context structure, and updates all of the pointers to be heap values if the block is copied.  If your blocks are only downward funargs, then allocating all relevant locals in the struct on the stack and passing a pointer to it down can have the same effect.  If your blocks are expected to persist after the function returns then you will need some form of memory management.

We don't need persistence of activation records, so a single pointer to one of them will work, if
it can access all the variables.  It looks like wrapping everything explicitly declared in a scope
into a struct is the way to do it.

> The MysoreScript language that I use for teaching implements a simple form of closure and has heavily commented code, which you might want to use as an example:
> https://github.com/CompilerTeaching/MysoreScript
> David

Rodney Bates
rodney.m.bates at acm.org

More information about the cfe-dev mailing list