From -9136162920771194970
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,a69618ea97f65037
X-Google-Attributes: gidf78e5,public
From: fjh@cs.mu.OZ.AU (Fergus Henderson)
Subject: Re: nested functions
Date: 1998/04/29
Message-ID: <6i7h14$e33$1@mulga.cs.mu.OZ.AU>#1/1
X-Deja-AN: 348806543
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
References: <6hac23$8lb$1@news2.isdnet.net> <353b85b9.0@news.iprolink.ch> <jbk98hm400.fsf@pc0067.pica.nl> <Es4HFB.Gz3@research.att.com>
X-Original-Date: 29 Apr 1998 15:29:40 GMT
Organization: Computer Science, The University of Melbourne
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANUdlqOEDnX0m9pzZAQEjSwGAmJq7JZsgTRio0I3metE94Hc4rb+LW3z1 a1vphTLlh6YvLv0Nn6L7rEry2hxY5Coa =alHY
Newsgroups: comp.std.c++


ark@research.att.com (Andrew Koenig) writes:

>The reason C++ does not have nested functions is that implementing them
>in full generality would make every function pointer into a two-word
>object -- it would need the address of the code and the address of the
>corresponding activation record.  It might be possible to avoid the
>space overhead by using the `trampoline' technique, but that requires
>the ability to generate code in data space, which not every processor
>supports.

There are trampoline-based techniques that do not require the
ability to generate code in data space.

For example, the system can have a fixed-size array of trampoline code
fragments, computed at code generation time or link time, and a
corresponding array of two-word (code address, data address) pairs.
Each trampoline fragment looks up the corresponding slot in the array,
puts the data address in a register, and jumps to the code address.  In
addition to these two arrays, you have a trampoline stack pointer.
Trampolines are allocated from the trampoline stack when needed, and
deallocated when the containing function exits.

The drawback of this approach is that the size of the trampoline stack must
be fixed at link time.  This does not seem too bad -- after all, many
C implementations and thread libraries used fixed-sized stacks.
Most programs would use very few trampolines at any given point in time,
so a single page (or less) would suffice for the vast majority of programs.

In addition, if the processor / OS has appropriate virtual memory
support, then you can avoid the need for a fixed-size trampoline stack,
still without doing any code generation at runtime, and with no need to
flush the instruction cache.  I'll leave that one as an exercise for
the reader.

--
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3        |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]



