caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Basile Starynkevitch <basile@starynkevitch.net>
To: Vincent Gripon <vincent.gripon@telecom-bretagne.eu>,
	caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Compiling Ocaml sources to c sources
Date: Tue, 14 Sep 2010 14:43:41 +0200	[thread overview]
Message-ID: <20100914124341.GA11500@hector.lesours> (raw)
In-Reply-To: <4C8F660B.4060901@telecom-bretagne.eu>

On Tue, Sep 14, 2010 at 02:09:47PM +0200, Vincent Gripon wrote:
>  Hello,
> 
> We are currently planing to participate to a programming contest.

It would be interesting for us to know more about that contest.

> This contest allows the use of four languages (C/C#/C++/java) but
> not OCaml.

That does not mean much. What about libraries you are calling from C?

In particular:

  * can you use any library? This seems strange (because the
    organizers very probably won't have the same libraries or versions
    as you have).

  * do you have any restrictions on the libraries you are using?

  * at the extreme case, the competition might require you to provide
    all the sources of every non standard (in the sense of ISO C
    standard) libraries you are using, and a verified mean to build
    them. What about POSIX system calls?

> 
> We would like to use Ocaml as it is to us the language that fits the
> most the kind of exercises proposed. The organizers don't mind if we
> use OCaml as long as we provide an easily compilable C source to
> them, even if it is not readable.
> 
> Is there any platform independent way to compile OCaml sources to C
> sources? 

I don't see any easy way to do that. There are several issues

  * the Ocaml runtime environment, which not only includes its garbage
    collector but also all the ocaml runtime library.

  * Hacking a C generator inside Ocaml is non-trivial, because of the
    garbage collector, currified function calls, and tail recursion
    etc.

  * If you really insist on coding in Ocaml and if you don't care much
    about performance, you could take my bit-rotten Ocamljit work (I
    forgot where you can find it, and I don't have it anymore, but
    google should help finding it) and make something which transform
    the entire bytecode of an Ocaml program into a gigantic single C
    function (translating straightforwardly every byte code into a
    small chunk of C code). This is not easy to do, and it might not
    be fun neither. Very probably, a C compiler would have pain to
    optimize such a big function (by personal experience, the GCC
    compiler usually take O(n^2) time to optimize with -O2 a
    sufficiently large function of size n, and may also need O(n^2)
    memory). And such an approach might not produce portable code
    (perhaps there are some issues w.r.t. 32 vs 64 bits
    systems). Actually, portable C is a fiction: only ported C
    programs can exist.

Did you also consider using Scheme, it has several implementations
generating C code (Chicken & Stalin come to mind).

But still, programming in C does not mean much! What about
portability? building? external libraries? linking other stuff? target
system?

If you have complete freedom on what you link with your program, you
could hack a libocamlrun to be linkable, and link the bytecode as an
array, as suggested previously.

I would imagine that preparing stuff to make you code in Ocaml and fit
into the contest rules is a big lot of work.

Cheers.

-- 
Basile STARYNKEVITCH         http://starynkevitch.net/Basile/
email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
8, rue de la Faiencerie, 92340 Bourg La Reine, France
*** opinions {are only mines, sont seulement les miennes} ***


  parent reply	other threads:[~2010-09-14 12:43 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-14 12:09 Vincent Gripon
2010-09-14 12:16 ` [Caml-list] Compiling Ocaml sources to c sources [NC] Rabih CHAAR
2010-09-14 12:35 ` [Caml-list] Compiling Ocaml sources to c sources David MENTRE
2010-09-15 13:18   ` Vincent Gripon
2010-09-15 19:59     ` Basile Starynkevitch
2010-09-15 22:16       ` Vincent Gripon
2010-09-14 12:43 ` Basile Starynkevitch [this message]
2010-09-15 12:59   ` Vincent Gripon
2010-09-15 17:36   ` Jon Harrop
2010-09-15 18:17     ` Eray Ozkural
2010-09-15 20:37       ` Jon Harrop
2010-09-16  0:38         ` Eray Ozkural
2010-09-16  9:05           ` Fabrice Le Fessant
2010-09-16 10:46             ` Eray Ozkural
2010-09-16 11:11               ` Fabrice Le Fessant
2010-09-14 12:47 ` Thomas Gazagnaire
2010-09-14 12:48   ` Grant Rettke
2010-09-15 13:04   ` Vincent Gripon
2010-09-15 19:57     ` 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=20100914124341.GA11500@hector.lesours \
    --to=basile@starynkevitch.net \
    --cc=caml-list@yquem.inria.fr \
    --cc=vincent.gripon@telecom-bretagne.eu \
    /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).