Re: How to implement lexical closures?

"BGB / cr88192" <cr88192@hotmail.com>
Wed, 12 May 2010 08:41:09 -0700

          From comp.compilers

Related articles
How to implement lexical closures? grom358@gmail.com (grom) (2010-05-06)
Re: How to implement lexical closures? sleepdev@gmail.com (andy johnson) (2010-05-09)
Re: How to implement lexical closures? gneuner2@comcast.net (George Neuner) (2010-05-09)
Re: How to implement lexical closures? cr88192@hotmail.com (BGB / cr88192) (2010-05-09)
Re: How to implement lexical closures? torbenm@diku.dk (2010-05-10)
Re: How to implement lexical closures? grom358@gmail.com (grom) (2010-05-11)
Re: How to implement lexical closures? cr88192@hotmail.com (BGB / cr88192) (2010-05-12)
Re: How to implement lexical closures? gene.ressler@gmail.com (Gene) (2010-05-12)
Re: How to implement lexical closures? gah@ugcs.caltech.edu (glen herrmannsfeldt) (2010-05-13)
Re: How to implement lexical closures? gneuner2@comcast.net (George Neuner) (2010-05-15)
Re: How to implement lexical closures? gneuner2@comcast.net (George Neuner) (2010-05-15)
Re: How to implement lexical closures? gah@ugcs.caltech.edu (glen herrmannsfeldt) (2010-05-16)
Re: How to implement lexical closures? gneuner2@comcast.net (George Neuner) (2010-05-17)
[3 later articles]
| List of all articles for this month |
From: "BGB / cr88192" <cr88192@hotmail.com>
Newsgroups: comp.compilers
Date: Wed, 12 May 2010 08:41:09 -0700
Organization: albasani.net
References: 10-05-031 10-05-052 10-05-064
Keywords: storage, symbols
Posted-Date: 12 May 2010 17:13:00 EDT

"grom" <grom358@gmail.com> wrote
> Thanks for all the replies. The solution that I am currently using is
> a linked list of symbol tables. Each time a new scope is entered (ie.
> a function) a new symbol table is created that is linked to the outer
> scope symbol table. Then when a variable is referenced it searches the
> linked list of symbol tables starting from the local symbol table and
> working out to the top level symbol table (ie. global symbol table).


<snip>


> My next step will be to try using indirect references to variables in
> the outer scope and only bringing in references that are used.




also possible is to have the compiler keep track of which bindings are
captured:
trivially, if a given name is never seen in the closure body, it can be
safely inferred not to have been used.
slightly less trivial is to determine if a binding is "shadowed", and at
this point one can also know that it is not captured.


this allows separating closed-over variables from non-closed-over variables,
and so only the closed-over variables need to be captured.


note that with indirect references, it is still needed to have somewhere to
keep these references (so, the matter of frame-capture doesn't entirely
disappear).




side note:
one trick I came up with (for doing closures, ... with the C calling
convention), was to essentially make the closure header be an executable
object (which looks to C like a normal function pointer), and calling this
object causes the frame to be reloaded and control to be passed to the
function body (which is shared between all instances of a given closure).


the idea here is that, in the parent function, the closed-over variables are
actually located within an invisible GC'ed struct, and it is this struct
which is captured (rather than the entire call-frame).


however, I never really got around to implementing implicit closures in my C
compiler, and so ended up mostly using the (more traditional compiler
friendly) strategy of explicitly creating such a struct, and using an
API-call to create the "closure", with the "body" being actually just
another function which is called with the struct passed in as a new first
argument.


in this case though, the closure is still an executable object as described
before...


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.