[LLVMdev] Nested functions
Vikram S. Adve
vadve at cs.uiuc.edu
Mon Nov 22 08:00:43 PST 2004
On Nov 21, 2004, at 4:11 AM, Dirk Muysers wrote:
> Was there already some reflection about how to lower the concept of
> nested
> functions (and the corresponding static links) into llvm?
>
> Dirk Muysers.
I have not seen a discussion of this and none of our current front-ends
need it. A straightforward way to add support for this would be:
(a) Lower nested procedures into ordinary top-level functions in LLVM,
with name-mangling to enforce scopes for each name.
(b) I assume you don't want to use a display (a global static link
array) because of threads. For explicit static links:
(i) Add an intrinsic function to get/set the access link out of the
current stack frame. It should take the current stack frame as an
argument (so you can walk the access link chain on the stack). Also,
add an intrinsic to get a pointer to the current stack frame. I don't
know a portable way to implement this in C, but the other back-ends
should not have a problem.
(ii) Add the access link as the first parameter to each nested
function and use the intrinsic to set the link in the stack frame on
entry. This argument is not traditionally needed but it makes things
easier in LLVM, and may even make the common case (accessing a variable
in the immediate parent function) faster.
(iii) Before a call, walk the link chain to find the right access link
(or the current stack frame) and pass it to the callee.
As an optimization for shallow nested functions (e.g., 3 levels or
less), it seems to me you could just avoid the stack walking entirely
and add $k-1$ arguments to each function at level $k$, i.e., at most 2
arguments in all. This may even be an easy first implementation.
--Vikram
http://www.cs.uiuc.edu/~vadve
http://llvm.cs.uiuc.edu/
More information about the llvm-dev
mailing list