🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #4130

Re: Nested functions (was: Re: GOTO single exit point)

#4130
From: Stephen Baynes
Date: Wed, 27 Sep 1995 07:22
47 lines
2958 bytes
Wil Evers (wil@ittpub.nl) wrote:
: In article <KANZE.95Sep25162631@slsvhdt.lts.sel.alcatel.de>  
: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763) writes:

[Discussion over the pros, cons and howtos of nested functions accessing  
the local variables of enclosing functions...]

: > I believe that trampolines have also been used to overcome this
: > problem.  When the address of a nested function is taken, a small
: > initialization program is generated on the fly, to set up the extra
: > pointers, and then jump to the actual function.  The pointer to
: > function points to this program.

: Now you've gotten me curious. I thought the extra information passed to  
: the called function depended on the nesting level at the point from which  
: it is called, not on the nesting level at the point where its address is  
: taken. If that's true, then how can the trampoline set up this  
: information?

If one takes the address of an auto variable one gets a pointer which points 
to a specific instance of that variable in a specific call of the owning
function. The same is expected of the address of a nested function, it points
to a specific instance of that function in a specific call of the owning 
function. [A nested function that uses auto variables from its parent is
effectively aoto too as you need a new version each time.]

Trampoline code would look something like:
        trampoline_start:
                Load register R with immediate value.
                        The value is the start of stack frame
                        for particular instance of calling function.
                Jump to start of actual function code.

The nested function would reference auto variables from the parent function
by an offset from R, in much the same way as it referes to its own auto 
variables by an offset from the stack pointer or frame pointer.
NOTE the parents auto variables must be written to memory, they can't be
in registers at the time. I don't think there are any sequece point problems
here. In C terms I think this also means one would disallow a 
nested function from accessing 'register' variables of the parent. Not a great
loss.

--
Stephen Baynes                              baynes@mulsoc2.serigate.philips.nl
Philips Semiconductors Ltd
Southampton                                 My views are my own.
United Kingdom
Technical Details
Message-ID: <DFJz4y.Iyy@ukpsshp1.serigate.philips.nl>
Path: arc1-ca.pugleaf.net!phcoms4.seri.philips.nl!panther!baynes
References: <KANZE.95Sep25162631@slsvhdt.lts.sel.alcatel.de> <1995Sep26.112649.1576@ittpub>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!phcoms4.seri.philips.nl!panther!baynes
From: baynes@ukpsshp1.serigate.philips.nl (Stephen Baynes)
Subject: Re: Nested functions (was: Re: GOTO single exit point)
Sender: news@ukpsshp1.serigate.philips.nl (account for localnews)
Message-ID: <DFJz4y.Iyy@ukpsshp1.serigate.philips.nl>
Date: Wed, 27 Sep 1995 07:22:10 GMT
References: <KANZE.95Sep25162631@slsvhdt.lts.sel.alcatel.de> <1995Sep26.112649.1576@ittpub>
Organization: Philips Semiconductors, Southampton, UK
X-Newsreader: TIN [version 1.2 PL2]