Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any free OCaml to C translator? [closed]

Tags:

c++

c

ocaml

So I have nice OCaml code (50000 lines). I want to port it to C. So Is there any free OCaml to C translator?

like image 386
Rella Avatar asked Apr 14 '10 15:04

Rella


People also ask

Does OCaml compile to C?

All of the OCaml modules are linked into this object file as bytecode, just as they would be for an executable. This object file can then be linked with C code using the standard C compiler, needing only the bytecode runtime library (which is installed as libcamlrun.

Is OCaml as fast as C?

Speed. According to The great computer language shootout, (see also the newer Computer language shootout benchmarks) Ocaml is the second fastest language - slower than C, but faster than C++.

What does OCaml compile to?

The ocamlc and ocamlopt compilers The compiler produces an executable named program or program.exe . The order of the source files matters, and so module1.ml cannot depend upon things that are defined in module2.ml .


2 Answers

There is an OCaml bytecode executable file to C source code compiler: https://github.com/ocaml-bytes/ocamlcc

So, first compile your code to a bytecode executable then use ocamlcc.

like image 119
daruma Avatar answered Sep 20 '22 21:09

daruma


This probably isn't what you want, but you can get the OCaml compiler to dump its runtime code in C:

ocamlc -output-obj -o foo.c foo.ml

What you get is basically a static dump of the bytecode. The result will look something like:

#include <caml/mlvalues.h>
CAMLextern void caml_startup_code(
           code_t code, asize_t code_size,
           char *data, asize_t data_size,
           char *section_table, asize_t section_table_size,
           char **argv);
static int caml_code[] = {
0x00000054, 0x000003df, 0x00000029, 0x0000002a, 0x00000001, 0x00000000, 
/* ... */
}

static char caml_data[] = {
132, 149, 166, 190, 0, 0, 3, 153, 0, 0, 0, 118, 
/* ... */
};

static char caml_sections[] = {
132, 149, 166, 190, 0, 0, 21, 203, 0, 0, 0, 117, 
/* ... */
};

/* ... */

void caml_startup(char ** argv)
{
    caml_startup_code(caml_code, sizeof(caml_code),
                      caml_data, sizeof(caml_data),
                      caml_sections, sizeof(caml_sections),
                      argv);
}

You can compile it with

gcc -L/usr/lib/ocaml foo.c -lcamlrun -lm -lncurses

For more information, see the OCaml manual.

like image 42
Chris Conway Avatar answered Sep 21 '22 21:09

Chris Conway