caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: oleg@okmij.org
To: goswin-v-b@web.de
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Why NOT to compile OCaml via C
Date: 10 Dec 2011 07:35:36 -0000	[thread overview]
Message-ID: <20111210073536.48008.qmail@eeoth.pair.com> (raw)
In-Reply-To: <87aa712v26.fsf@frosties.localnet>


> Well, write the code as ONE function and do use lables. Sure, the C
> source will be huge for larger projects but then again you get the
> single source optimization bonus from gcc.

A realistic version of this approach indeed has been used, for
example, in Gambit-C Scheme and Stalin Scheme compilers. The approach as
suggested above has several drawbacks. First of all, it prevents
separate compilation. If we do need separate compilation, we have to
be content with several C functions (one per each separately compiled
unit), and have to arrange for tail-recursive calls among them. So, we
need a fall-back solution. Gambit-C uses trampolining. 

Second, you probably do not appreciate how huge the produced C
functions might become. Past a certain threshold, gcc or other C
compilers slow down, almost to a halt. Subjectively, it seems that the
quality of optimizations deteriorates past a threshold (although I
haven't investigated that). Past another threshold, gcc just
bombs. Therefore, Gambit for example had to take measures limiting the
size of the generated C functions.

> Note: gcc does know something about tail recusion. So it is not
> completly lost there.

One must really draw the distinction between an optional optimization
and a required behavior. GCC indeed _may_ optimize a tail call -- in
simple circumstances and when the stars are well-aligned. BTW,
implementing tail-recursion is much more complex than it may
appear. Consider, for example
	let rec f x y = if y = 0 then x else f (x+y) (x-y)

Here, we can't _just_ jump on the recursive call. We have to mutate
the arguments in the current frame, possibly using stack for scratch
storage. GCC won't do tail-call optimization in this (and many much
simpler) cases.

As I said earlier, there really is huge literature on implementing
functional languages by compiling into C. Lots of approaches have been
suggested and tried -- it is very hard to say anything new here. The
fact that we are still having this discussion tells that no solution
has been truly satisfactory.

BTW, Gambit avoids other pitfalls of C compilation and so does manages
precise GC and very efficient call/cc, exceptions and dynamic
binding. The reason is that the generated C code is essentially a
Gambit Virtual machine interpreter specialized to the source code.  It
does not share its stack with the C stack. Perhaps Gambit is the
first example of the First Futamura Projection that I have seen. It
works out quite well in the end.



  parent reply	other threads:[~2011-12-10  7:35 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-09  6:57 oleg
2011-12-09  7:52 ` Stéphane Glondu
2011-12-09  9:58   ` Gabriel Scherer
2011-12-09 10:06     ` [Caml-devel] " Jonathan Protzenko
2011-12-09 11:03     ` Mehdi Dogguy
2011-12-09 12:08     ` Benedikt Meurer
2011-12-09 12:37       ` Gabriel Scherer
2011-12-09 14:05         ` Benedikt Meurer
2011-12-09 14:30           ` Török Edwin
2011-12-09 14:51             ` Benedikt Meurer
2011-12-09 23:38             ` oliver
2011-12-09 21:22           ` Richard W.M. Jones
2011-12-10  9:36             ` Benedikt Meurer
2011-12-10 11:34   ` Jon Harrop
2011-12-09 23:01 ` oliver
2011-12-09 23:18 ` Goswin von Brederlow
2011-12-10  0:20   ` Till Varoquaux
2011-12-10  7:35   ` oleg [this message]
2011-12-10 15:40   ` Basile Starynkevitch
2011-12-10 23:56     ` Peter Hawkins
2011-12-11  8:24       ` Basile Starynkevitch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20111210073536.48008.qmail@eeoth.pair.com \
    --to=oleg@okmij.org \
    --cc=caml-list@inria.fr \
    --cc=goswin-v-b@web.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).