My experience using recent versions of gcc as a back end show that gcc
is actually quite good at sibling calls under a few conditions:
make every function in a tail call cycle static (even if you have to
make a public wrapper for one for entry as long as it isn't called
within the cycle), if you can't do this, using -fwhole-program properly
can get the same effect I believe, but I never had to resort to this.
don't use alloca or sized variables and never take the address of an
automatic variable or pass an automatically defined array to another
function.
there are probably other conditions, but these seemed to give me decent
results.
Jhc has since switched to compiling tail calls directly into
C loops. I described the method used in a post to a list a while ago,
basically, I allow local function definitions if they are only called as
proper tail calls. I then perform a series of transformations that
remove parameters to the functions, replacing them with refereces to
variables in scope. (variable is distinct from a name in the core
language, name = pure functional bound value like in haskell, variable =
an haskell IORef effectively). Once all arguments and the return values
have been transformed away, they can be spit out as simple 'goto's in
the code generator. My optimizer can still treat them like any other
function call as long as it is careful to not re-introduce the arguments
I transformed away.
The main disadvantage is that this does not work cross-compilation-unit
and does not work for indirect function calls. but it isn't too hard to
ensure that every cycle takes place within a single compilation unit and
jhc transforms away all indirect function calls. that second issue may
make this scheme a no-go for something like ghc (unless augmented with
another method), but for a generic c-- to C translator, it could be
useful for a wide variety of cases I think.
I should note that jhc's final intermediate language before C is almost
a direct transliteration of C-- (as haskell data structures). pasting a
C-- parser to it would get one pretty close to a basic C-- to C compiler.
John
--
John Meacham - ⑆repetae.net⑆john⑈
_______________________________________________
Cminusminus mailing list
[email protected]
https://cminusminus.org/mailman/listinfo/cminusminus