[cfe-dev] Nested functions in Clang?

David Chisnall via cfe-dev cfe-dev at lists.llvm.org
Mon Sep 14 01:32:45 PDT 2015


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.

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




More information about the cfe-dev mailing list