caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Why NOT to compile OCaml via C
@ 2011-12-09  6:57 oleg
  2011-12-09  7:52 ` Stéphane Glondu
                   ` (2 more replies)
  0 siblings, 3 replies; 21+ messages in thread
From: oleg @ 2011-12-09  6:57 UTC (permalink / raw)
  To: caml-list; +Cc: ontologiae


Pierre-Alexandre Voye wrote:

> Note that if Ocaml compiler would have a C backend, all these problems or
> architecture port would disappear...
> Ocaml would have more than 30 target[1]
> In my Opinion, trying to generate assembler is a bad idea because modern CPU
> require a lot of work to generate good assembler.

There are many good reasons to avoid C when compiling functional
languages, especially strict ones.

One often hears that ``C is a portable assembler''. That has never
been true. One of the reasons is that every assembler I know has the
"jmp" instruction, which, without affecting SP, transfers control
anywhere, out of a procedure or in the middle of a procedure, out of a
module or into a module. C is built around the stack discipline --
after all, C is a descendant of Algol 60. (Although C has labels, they
are limited, even in GCC). Although Algol-60 researchers quickly
recognized the value of tail recursion, all that knowledge was lost in
the Dark Ages.

In strict functional languages, tail-recursion is a big deal. 
There is *huge* amount of literature on emulating tail-recursion in
standard C or GCC. That alone should tell that there is no simple
solution.  When GHC (Glasgow Haskell Compiler) was emitting C, it was
targeting specifically GCC. Furthermore, it employed a Perl program,
aptly named `evil mangler', to post-process the assembly code
generated by GCC. One can only shudder when thinking about Simon
Marlow's maintaining it.

There are at least two other reasons ocamlopt emitting assembly. One
is garbage collection. OCaml GC is precise. Therefore, when sweeping
through the stack, GC has to know if 0x80aa4000 is an unboxed integer
or a heap pointer. So-called frame tables generated by the compiler
tell GC which stack slots contain OCaml values. GC ignores other slots
and hence expensive tests it had to do otherwise. One can build frame
tables only when one has full control of the generated code and the
frame layout. The third reason is exceptions. In OCaml, exceptions are
pervasive and should be fast. They are indeed well implemented, via
special exception frames and a dedicated exception frame `register'
(which is a real register on x64).




^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2011-12-11  8:24 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-09  6:57 [Caml-list] Why NOT to compile OCaml via C 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
2011-12-10 15:40   ` Basile Starynkevitch
2011-12-10 23:56     ` Peter Hawkins
2011-12-11  8:24       ` Basile Starynkevitch

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).