Nested function decomposition for GIMPLE.
Copyright (C) 2004-2013 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>.
The object of this pass is to lower the representation of a set of nested
functions in order to expose all of the gory details of the various
nonlocal references. We want to do this sooner rather than later, in
order to give us more freedom in emitting all of the functions in question.
Back in olden times, when gcc was young, we developed an insanely
complicated scheme whereby variables which were referenced nonlocally
were forced to live in the stack of the declaring function, and then
the nested functions magically discovered where these variables were
placed. In order for this scheme to function properly, it required
that the outer function be partially expanded, then we switch to
compiling the inner function, and once done with those we switch back
to compiling the outer function. Such delicate ordering requirements
makes it difficult to do whole translation unit optimizations
involving such functions.
The implementation here is much more direct. Everything that can be
referenced by an inner function is a member of an explicitly created
structure herein called the "nonlocal frame struct". The incoming
static chain for a nested function is a pointer to this struct in
the parent. In this way, we settle on known offsets from a known
base, and so are decoupled from the logic that places objects in the
function's stack frame. More importantly, we don't have to wait for
that to happen -- since the compilation of the inner function is no
longer tied to a real stack frame, the nonlocal frame struct can be
allocated anywhere. Which means that the outer function is now
inlinable.
Theory of operation here is very simple. Iterate over all the
statements in all the functions (depth first) several times,
allocating structures and fields on demand. In general we want to
examine inner functions first, so that we can avoid making changes
to outer functions which are unnecessary.
The order of the passes matters a bit, in that later passes will be
skipped if it is discovered that the functions don't actually interact
at all. That is, they're nested in the lexical sense but could have
been written as independent functions without change.