Sep 17
2009

How Nested Functions Work, part 2

Posted by Walter Bright in Programming LanguagesProgramming language semanticsProgramming Language Implementationlanguage engineeringengineeringD ProgrammingCompilersArchitecture and DesignApplication Development

WalterBright

In the last installment, we saw how nested functions are implemented. If you're thinking ahead, you'll ask "but what about pointers to nested functions? Does that work?" The problem is that the nested function, in order to work, requires and extra hidden parameter - the static link to its statically enclosing function's stack frame. This extra parameter means an ordinary function pointer won't work.

What's needed is a so-called "fat pointer" - a pointer pair, one of which points to the nested function, the other is the static link. In the D programming language, this "fat pointer" is called a delegate:

int foo(int delegate(int) dg)
{
    return dg(23);
}

int bar(int i)
{
    int abc(int x) { return i + x; }

    return foo(&abc);
}

The function foo takes as its parameter a delegate which can be called. The invocation bar(4) thus returns 27.

But something looks familiar about that fat pointer delegate. Don't struct member functions also have a hidden pointer - the 'this' reference? And doesn't that 'this' reference point to an object with a bunch of fields? Indeed they do. A delegate is indistinguishable from the address of a member function that is bound to a particular 'this' reference. Function foo cannot tell the difference:

struct S {
    int y;
    int mem(int x) { return y + x; }
}

int func()
{
    S s;
    s.y = 3;
    return foo(&s.mem);   // returns 26
}

As we've seen, the stack frame variables are just like a struct with the struct's fields corresponding to the function's local variables. But there's one more little problem. If that delegate (address of abc plus static link to bar's stack frame) is stored somewhere global, then bar returns, isn't the stack frame destroyed? Yes, it is. If the delegate is then called, won't it be referencing garbage on the stack? Yes, it will be.

Something more is needed; we can't tolerate such a gaping hole in our memory model. The answer is to carry the analogy of stack frames being like a struct a bit further. That frame need not actually be allocated on the stack. It can be allocated on the garbage collected heap. Then, it will persist as long as the delegate exists. Voila, no more memory corruption!

Of course, allocating every stack frame for every function on the heap is going to be disastrously slow - we use a stack because it's so fast at allocation and deallocation. What to do, what to do... The solution is to only allocate the frame on the heap if there is a delegate to a nested function and that nested function actually references variables within the frame.

By pulling on the nested functions string, we wound up with delegates and even closures. Adding in a function literal syntax to make passing delegates to other functions even easier, and we've got a truly delightful and powerful tool at our disposal. I learned about nested functions from the original Pascal language, but I hadn't appreciated how neat and elegant they are until rather recently. We keep finding new uses for them!

There is one more thing of note we can do with these:

Locally Instantiated Templates

Let's rewrite our initial example so that foo is a template function instead
of just a function:

int foo(alias dg)()
{
    return dg(23);
}

int bar(int i)
{
    int abc(int x) { return i + x; }

    return foo!(abc)();
}

An alias parameter to a template in D is passed symbolically, i.e. it is passed at compile time and the template foo is instantiated at compile time. The call to dg(23) is rewritten as abc(32). Using templates to pass parameters symbolically is very powerful, as there are no runtime pointers or other indirections involved.

But there's a problem. When foo is instantiated into being a real function, it's at global scope. When it makes the call abc(23), it has no way of getting the static link to bar's frame (so that abc can reference bar's parameter i). This looks like a real stumper. But the solution is an insight - instead of instantiating foo as a global function, instantiate it as a nested function in bar's scope!

But how does the compiler know to do that? When instantiating a template, it examines the arguments to the alias parameters, and if any of them are nested functions, the template is instantiated in the same scope as those nested functions. (Of course, if there are alias arguments that nest in different scopes, a compile time error results as there's no scope to instantiate the template in that would work for all the arguments.)

These are called locally instantiated templates. As far as I know, they are unique to D's template system.


If you want to learn more about how real compilers work, I am hosting a seminar in the fall on compiler construction.

 



Comments (5)Add Comment
Wow
written by cypher punks, September 19, 2009
Wow. That is truly retarted. You get the syntax of closures, while missing 98% of the power of closures. The first approach is what everybody else uses, and with good reason. It's slightly more complex (you need to either have a garbage-collected language, or you need to manage the memory manually, as with any other structures), but it has the virtue of working, and of being useful for making large-scale code much cleaner (SICP gives many theoretical examples, SICM gives an example of a large-scale impossible without them, and Ruby-on-Rails and much Python code give many more practical examples).

In the garbage collected case, you allocate all variables referenced by a closure on the heap, rather than on the stack. They keep existing when the function terminates if there are any closures still holding on to them.

The manually managed case is a little bit messier, and varies from implementation to implementation.
locally instantiated templates
written by Lutger Blijdestijn, September 19, 2009
This is a very cool feature, thanks for explaining it. It would be interesting to note what kind of problems it can solve.

@cypher punks: wow, did you even read the article?
Re: Wow
written by Walter Bright, September 19, 2009
I don't understand your comment. What power of closures is missing? Also, the article discusses a garbage collected implementation, not a manual one.
cypher punks
written by Frank Hileman, September 24, 2009
Walter, I don't think "cypher punks" read your article.

Regarding your comment, "allocating every stack frame for every function on the heap is going to be disastrously slow," it depends entirely how the heap is garbage collected. If most functions don't need the stack frame on exit, it seems they could allocate and free from a dedicated heap about as quickly as a stack. It seems the slow part of garbage collection is not the allocation but the freeing part.
nested functions in visual c++ 2008
written by j lp, September 28, 2009
I'm using a c library containing nested functions compiled with MingW.
However, using this library, my applications crash when calling nested functions.
(In test i did it was by reference in another function)

Does anybody know if/how it is possible to enable this in visual c++ (compiled for C)

Write comment
You must be logged in to a comment. Please register if you do not have an account yet.

busy

Get your FREE Subscription to Dr. Dobb’s Digest today!

Dobbs Code Talk Quick Poll

This time next year, your most important operating system (host and/or target) will be:

Look Who's Code Talking


Lisa Michele Marino
City: Pittsburgh

Eric Bruno
City: Shirley

Brendan LeFebvre
City: Billerica

Ron Le Blanc
City: Augusta

Diego Silang
City: Pittsburgh

Robert Santuci
City: Orlando

Dobbs Code Talk Tags

.NET abstraction Ada Adobe Agile Ajax algorithm Algorithmic complexity ALM Analogical reasoning Android Anecdotes Apple Application Development AppStore Architecture and Design ARM Artificial Intelligence Artificial Life Assembler Programming Audio files AVX AWK Banking Bazaar Best Practices Blender Books Brain computer interfacing Build C C Programming C Sharp Cartoon Category theory Cellular automata Clojure Cloud Computing Cobol Cocoa Coder Of The Month Cognition as compression Collaboration Common Process/Frameworks Compilers Computational humour Computational narrative Computational politics Computer Science Computers in art computing pioneers concurrency Conferences Consciousness research Contest Contest140 contests CPlusPlus crime CSharp D Programming Data Centers Databases Debugging Delphi Deployment design Design Patterns Digital Signal Processing Distributed Django Documentation DSL dynamic language Eclipse EDA education Emacs Embedded Systems Encryption engineering Erlang Etymology Excel exception handling Facebook Financial computing Five Questions Flash Flash Lite Flex Forth Fortran Fraud FreeBSD Fun Functional Programming gadgets Games Gender Git gnuplot Go Google Graphics GUI hardware Heron High School High-Performance Computing History Holographic reduced representations HTML5 Humanity Humour Hungarian Notation Identity Inkscape Innovation Intel Interview iPhone J2EE Java JavaFX JavaOne JavaScript language engineering Legal lex LINQ Linux Lisp Literate Programming Logic Programming m4 Mainframes Make Mathematica Mercurial Mesh messaging Metaprogramming Microsoft MID Miscellaneous Musings ML Mobile Software Mobility modeling modular programming multicore Music MVC myblog Natural Language Processing Networking Neural networks newspeak Nokia numerical computing Object Rexx ObjectiveC Office Office 2007 Online spreadsheets OOP Open Source Openaccess publishing OpenBSD OpenSolaris Operating Systems Optimization Oracle Pair Programming Parallelism Concurrency Parsing Pascal Patents Patterns Performance Perl PHP Podcast Pop11 Poplog Privacy Processing Productivity Programming Language Implementation Programming Language One Programming language semantics Programming Languages Programming Style Project Management Prolog Psychology Public understanding of science puzzle Python QA Quantum Computing Quotes Rails Realtime recls Requirements Research practice REST Review RIA rich internet applications Robotics Ruby SaaS Software as a service Scala Schadenfreude Science fiction Screencast Scripting SD Best Practices Search Security Semantic Web Silverlight Snobol SOA social Social Networks Society for the Study of Artificial Intelligence a Software Development Methodology and Management Songs and poems Spending Priorities Spreadsheets SQL Startups Statistics Storage String pattern matching Survey Teaching Testing The Business of Programming The Dobbs Challenge The Future Theory Topology Transhumanism Travel on the Job Twitter Types Unix Upgrade Usability Use Cases USENET User Experience User Interface Design Version Control video virtual machines Virtualization Visual Studio Visual Studio Sponsored Post WCF Web Development Windows Windows 7 Windows Live Wireless WOA WPF X Window System yacc

Subscribe to Dr. Dobbs Newsletter

Email:
Dr. Dobb's Update
Delivered twice a week, Dr. Dobb's Update provides unbiased and objective news, commentary and technical features spanning the entire software development marketplace.

Latest Comments

Jonathan's Last Day at Sun
For the 8 years I worked there, it was fantastic. I worked there under McNealy and I have undying admiration for the guy. I only knew Jonathan periphe...
Implementing Thread Local Storage on OS ...
Back in the day, I did a fair amount of work with PThreads. Wonderful design. Some quirks, but basically really, really nice. Although I wrote a lot ...
More Technonecrophilia with Snobol One-L...
Yeah, It's probably identical except for the (embedded) copy number, I would think. Once it became freely distributable, the copy I've been distribut...
More Technonecrophilia with Snobol One-L...
There's a spitbol-3.7-win.exe at http://code.google.com/p/spitbol/downloads/list . I found it via Dave Shield's blog page http://daveshields.wordpress...
Jonathan's Last Day at Sun
Sadness.

The Latest From Our Member Blogs

How To Select Trainees
Written by Joel Wiesen   
01/27/10
Hiring the right trainee can be harder than hiring a trained programmer.  One approach is described at my website: http://www.aprtestingservices.com/business/lpat/
 
Technical Job Interviews
Written by Keith Kerlan   
01/20/10
What is the best way to interview for software developer positions?  I've been on both sides of the job interviewing table, but have been on the interviewee side of some not too  great inter
 
Timers/timeouts in multi-threaded event-loops
Written by Christof Meerwald   
01/03/10
The traditional way to integrate timeout handling (or timers) in (single-threaded) event loops was to just pass the appropriate timeout value to the select/poll/epoll syscall. While this works fine
 
C vs C++
Written by Issam Lahlali   
12/04/09
I think that the debate "C vs C++" will end when the two langages died, and each one have its advantages and inconvenients, the choice of one instead of another depend on the application c
 
Great Jobs at CISCO
Written by Brent Rogers   
11/30/09
Hello! I am a recruiter at CISCO. We have a number of great jobopportunities at CISCO right now. Please take a look at the job links listedbelow and please send me an updated resume if you are interes
 
OK Labs, ST-Ericsson, and the Mobile/Wireless Ecosystem
Written by Steve Subar   
11/17/09
Two weeks ago, OK Labs and ST-Ericsson announced the selection of OK Labs as ST-Ericsson's mobile virtualization partner. To earn this coveted position, OK Labs prevailed in a rigorous evaluation
 
C++ Ninjas Needed in Santa Clara, California
Written by Brent Rogers   
09/30/09
Hello! I am a recruiter at CISCO. Our PostPath teamin Santa Clara is building a new Email SaaS business at CISCO. We are looking forsenior developers with Zimbra expertise to help us accomplish this t
 
Fighting Fragmentation with Mobile Virtualization
Written by Steve Subar   
09/21/09
Last week Motorola and T-Mobile announced the launch of a new and innovative Android-based smartphone, the Cliq. This attractive, feature-rich slider handset happens to build on a chipset and firmware
 
Insights into Router Design: Unit Testing of Networking Protocols
Written by Rajesh Kumar Venkateswaran   
09/07/09
  Unit testing is a software validation methodology through which a programmer tests individual modules or units of source code. If the programmer has been responsible for developing a networ
 
Insights into Router Design: Implementation of Networking Protocols
Written by Rajesh Kumar Venkateswaran   
09/06/09
  Modern data networking consists of a large number of networking protocols, each of which has its own domain of applicability. Some run on end stations (also called hosts), some on enterp
 
Insights and Innovations in Networking
Written by Rajesh Kumar Venkateswaran   
09/05/09
Networking devices such as routers and switches have evolved quite a bit over the past years, both in the service provider network and in the enterprise. It is a challenge to build these devices, bo
 
reddit threads community
Written by Christof Meerwald   
08/30/09
I have just started a threads community over at reddit to cover topics such as multithreading, concurrency and parallel programming. Feel free to join if you are interested. -- cmeerw.org 
 

The Latest From Dr. Dobbs

DDJ