caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Writing a JIT compiler
@ 2004-06-25  8:04 Jon Harrop
  2004-06-25  8:52 ` Benjamin Geer
  2004-06-25  9:59 ` Basile Starynkevitch [local]
  0 siblings, 2 replies; 9+ messages in thread
From: Jon Harrop @ 2004-06-25  8:04 UTC (permalink / raw)
  To: caml-list


I'm trying to convert an interpreter written in OCaml into a JIT compiler. 
Generating OCaml code from host code seems to be the best way to do this as 
the generated code can then use the interpreter's data structures and 
functions.

However, I'm currently creating temporary source files, calling ocamlopt and 
using marshalling to another temporary file in order to get the data back and 
forth.

Is there a better way of doing this?

Cheers,
Jon.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Writing a JIT compiler
  2004-06-25  8:04 [Caml-list] Writing a JIT compiler Jon Harrop
@ 2004-06-25  8:52 ` Benjamin Geer
  2004-06-25  9:53   ` Jon Harrop
  2004-06-25  9:59 ` Basile Starynkevitch [local]
  1 sibling, 1 reply; 9+ messages in thread
From: Benjamin Geer @ 2004-06-25  8:52 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

Jon Harrop wrote:
> I'm trying to convert an interpreter written in OCaml into a JIT compiler. 
> Generating OCaml code from host code seems to be the best way to do this as 
> the generated code can then use the interpreter's data structures and 
> functions.

There's already a JIT compiler for OCaml code:

http://cristal.inria.fr/~starynke/ocamljit.html

Ben

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Writing a JIT compiler
  2004-06-25  8:52 ` Benjamin Geer
@ 2004-06-25  9:53   ` Jon Harrop
  2004-06-25 10:56     ` Basile Starynkevitch [local]
  0 siblings, 1 reply; 9+ messages in thread
From: Jon Harrop @ 2004-06-25  9:53 UTC (permalink / raw)
  To: caml-list

On Friday 25 June 2004 09:52, Benjamin Geer wrote:
> There's already a JIT compiler for OCaml code:
>
> http://cristal.inria.fr/~starynke/ocamljit.html

So maybe I could get ocamljitrun to execute my interpreter and write my 
interpreter to dynamically convert host code into OCaml bytecode which can 
then be JIT compiled into native code and dynamically linked back in with the 
running interpreter?

Cheers,
Jon.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Writing a JIT compiler
  2004-06-25  8:04 [Caml-list] Writing a JIT compiler Jon Harrop
  2004-06-25  8:52 ` Benjamin Geer
@ 2004-06-25  9:59 ` Basile Starynkevitch [local]
       [not found]   ` <200406251325.15940.postmaster@jdh30.plus.com>
  1 sibling, 1 reply; 9+ messages in thread
From: Basile Starynkevitch [local] @ 2004-06-25  9:59 UTC (permalink / raw)
  To: Jon Harrop, caml-list

On Fri, Jun 25, 2004 at 09:04:09AM +0100, Jon Harrop wrote:
> 
> I'm trying to convert an interpreter written in OCaml into a JIT compiler. 
> Generating OCaml code from host code seems to be the best way to do this as 
> the generated code can then use the interpreter's data structures and 
> functions.
> 
> However, I'm currently creating temporary source files, calling ocamlopt and 
> using marshalling to another temporary file in order to get the data back and 
> forth.
> 
> Is there a better way of doing this?


Definitely have a look at http://cristal.inria.fr/~starynke/ocamljit.html

You did not explain why are you writing a JIT (speed issues?) and what
is the target language (x86 machine code, assembler, C, Ocaml?)  of
your JIT.

You might generate "lambda" representation (see
ocaml/bytecomp/lambda.mli) of the compiler, or at least typed trees
(see ocaml/typedtree.mli).

You might use MetaOcaml see http://metaocaml.org/

Some of their papers might help:
http://www.cs.rice.edu/%7Etaha/publications/conference/gpce03b.ps
http://www.cs.rice.edu/%7Etaha/publications/preprints/2004-02-16.ps
http://www.cs.rice.edu/%7Etaha/publications/journal/tcs00.pdf

You might generate C-- code: see http://cminusminus.org/

Regards.


-- 
Basile STARYNKEVITCH -- basile dot starynkevitch at inria dot fr
Project cristal.inria.fr - phone +33 1 3963 5197 - mobile 6 8501 2359
http://cristal.inria.fr/~starynke --- all opinions are only mine 

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Writing a JIT compiler
  2004-06-25  9:53   ` Jon Harrop
@ 2004-06-25 10:56     ` Basile Starynkevitch [local]
  0 siblings, 0 replies; 9+ messages in thread
From: Basile Starynkevitch [local] @ 2004-06-25 10:56 UTC (permalink / raw)
  To: caml-list

On Fri, Jun 25, 2004 at 10:53:14AM +0100, Jon Harrop wrote:
> On Friday 25 June 2004 09:52, Benjamin Geer wrote:
> > There's already a JIT compiler for OCaml code:
> >
> > http://cristal.inria.fr/~starynke/ocamljit.html
> 
> So maybe I could get ocamljitrun to execute my interpreter and write my 
> interpreter to dynamically convert host code into OCaml bytecode which can 
> then be JIT compiled into native code and dynamically linked back in with the 

Ocamljitrun is a plugin replacement for ocamlrun (and libcamlrun.a,
renamed as libcamljitrun.a) - it executes Ocaml bytecode (by
translating them transparently into native code). It works with
dynamically linked ocaml modules and toplevels.

So your interpreter might generate Ocaml bytecode (perhaps reusing the
"lambda" representation of Ocamlc) - and ocamljitrun will, when
interpreting it, translate the bytecode to machine code.

Ocamljitrun requires latest Ocaml CVS or future Ocaml 3.08. (the
current 3.07 release won't work with it).

Feel free to ask me additional questions about it.

As I told in my previous posting, you might have a look into MetaOcaml
(see http://metaocaml.org/ for more)

Regards.
-- 
Basile STARYNKEVITCH -- basile dot starynkevitch at inria dot fr
Project cristal.inria.fr - phone +33 1 3963 5197 - mobile 6 8501 2359
http://cristal.inria.fr/~starynke --- all opinions are only mine 

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Writing a JIT compiler
       [not found]   ` <200406251325.15940.postmaster@jdh30.plus.com>
@ 2004-06-25 15:55     ` Basile Starynkevitch [local]
  2004-06-25 17:05       ` Daniel Bünzli
  2004-06-26 13:45       ` [Caml-list] " Julian Brown
  0 siblings, 2 replies; 9+ messages in thread
From: Basile Starynkevitch [local] @ 2004-06-25 15:55 UTC (permalink / raw)
  To: caml-list

On Fri, Jun 25, 2004 at 01:25:15PM +0100, Jon Harrop wrote:

> If ocamljitrun is based upon a GNU library then it must be GPL, is
> that right?

Ocamljitrun & GNU lightning - the library used by Ocamljitrun - are
under LGPL = GNU *lesser* general public license. Also, ocamljitrun is
slower (on running your Ocaml source code) than code compiled with
ocamlopt, but faster than bytecode interpreted by
ocamlrun. Ocamljitrun has the usual LGPL exception common in Ocaml
INRIA code. (I don't know if it apply to the GNU lightning itself).

Because of a bug in GNU lightning, ocamljitrun currently crashes on
PowerPC.


I am definitely not a lawyer (and the constitution, laws and contracts
I am under - french law and INRIA temporary work contract - are
probably not the laws and contracts which govern you - probably US
laws) - but as far as I know ocamjitrun is under GNU LGPL - ie Lesser
GPL licence, like GNU lightning which it is using.

However, the usual meta-advice is that for legal advice, you should
ask professional lawyers. Never take any word I am telling on legal
stuff seriously - I AM NOT A LAWYER!




> On Friday 25 June 2004 10:59, Basile Starynkevitch wrote:

>> You did not explain why are you writing a JIT (speed issues?)

> Yes, purely for speed.

J-M.Eber also have similar concerns (he is writing a commercial
product in and with Ocaml). I think that his company (Lexifi) is
member of the Ocaml consortium.... His concern is to accelerate a
function doing many floating point operations and a few Ocaml function
application. The function is dynamically generated at runtime by his
complex product. I suggested him to generate C code (calling the
appropriate OCaml caml_callback* when needed to apply functions) and
then compile it (by forking a gcc command) as a shared library, and
then dlopen it and using dlsym to get the function pointer and at last
call it. You'll have to dlclose the library after using it. In
principle, it is like using the Dynlink module, but ugly details
differ.


>> and what is the target language (x86 machine code, assembler, C,
>> Ocaml?)  of your JIT.


> Anything which can be efficiently compiled into x86 and PPC machine
> code using tools which are free for commercial use. Currently, I'm
> outputting OCaml and using ocamlopt which produces great code but
> isn't free (enough) and can't dynamically link the JIT compiled code
> back in. I should probably investigate more efficient ways to
> marshal data between two programs.

I believe that existing marshalling routines are quite fast. I'm not
sure you'll easily recode equivalent functionality (in particular
polymorphism) running much faster.


> I think that the bits which I want to compile are quite simple -
> just functions to do tree manipulations which I want to partially
> specialise over the manipulations which they perform.

>> You might generate "lambda" representation (see
>> ocaml/bytecomp/lambda.mli) of the compiler, or at least typed trees
>> (see ocaml/typedtree.mli).
 
> I'll check these out, thank you.
> 
> > You might use MetaOcaml see http://metaocaml.org/

> AFAIK there still isn't a native-code version.

I suppose that next version of MetaOcaml should be compatible with
Ocamljitrun (because it is compatible with next release of Ocaml).

> > You might generate C-- code: see http://cminusminus.org/

> This seems to be my best bet so far. How easy would be it to retarget 
> ocamljitrun to use C-- (it's totally free)?

It is not worth doing it (and it should be boring). My suggestion was
to generate C-- code independently of ocamljitrun. But details are
probably clever. Regarding licensing issues of C--, go the C-- web
page or mailing list.


>> So your interpreter might generate Ocaml bytecode (perhaps reusing
>> the "lambda" representation of Ocamlc) - and ocamljitrun will, when
>> interpreting it, translate the bytecode to machine code.

> The two main problems with that are the speed hit that my
> interpreter would take (because it could no longer be from ocamlopt)
> and, I assume, the GPL on GNU lightning.

You should be aware that GNU lightning is LGPL not GPL and that there
are big licensing differences between both. Also, ocamljitrun is a
standalone executable - which you can run in many ways, much like you
can use GNU emacs (which is under GPL, not LGPL) to edit whatever
material you want.

Ask your lawyer about LGPL licensing issues - from my "academic" point
of view, I see none. but I am not a lawyer!

Also note that ocamljitrun is a binary which is a plugin replacement
for ocamlrun, this means that (as far as I understand your needs) you
don't need to link it with your program. This means that the
hypothetical linking issues of the LGPL is not a concern. In my
non-lawyer opinion, ocamljitrun is like ocamlrun so can run any
program you like, including commercial ones.

But I tend to believe that asking a lawyer or a legal expert is the
best solution to legal problems. Before going to your lawyer, read
careful the many materials on GPL & LGPL available on the Web.

All opinions here are mine only....


-- 
Basile STARYNKEVITCH -- basile dot starynkevitch at inria dot fr
Project cristal.inria.fr - phone +33 1 3963 5197 - mobile 6 8501 2359
http://cristal.inria.fr/~starynke --- all opinions are only mine 

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Writing a JIT compiler
  2004-06-25 15:55     ` Basile Starynkevitch [local]
@ 2004-06-25 17:05       ` Daniel Bünzli
  2004-06-26 13:45       ` [Caml-list] " Julian Brown
  1 sibling, 0 replies; 9+ messages in thread
From: Daniel Bünzli @ 2004-06-25 17:05 UTC (permalink / raw)
  To: caml-list


Le 25 juin 04, à 17:55, Basile Starynkevitch [local] a écrit :

> I suggested him to generate C code (calling the
> appropriate OCaml caml_callback* when needed to apply functions) and
> then compile it (by forking a gcc command) as a shared library, and
> then dlopen it and using dlsym to get the function pointer and at last
> call it. You'll have to dlclose the library after using it. In
> principle, it is like using the Dynlink module, but ugly details
> differ.

If I remember well, Christophe Raffalli adopts a similar technique in 
his program GlSurf. You may want to have a look to his (LGPL'd) code 
[1].

Daniel

[1] <http://www.lama.univ-savoie.fr/~raffalli/glsurf>

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* [Caml-list] Re: Writing a JIT compiler
  2004-06-25 15:55     ` Basile Starynkevitch [local]
  2004-06-25 17:05       ` Daniel Bünzli
@ 2004-06-26 13:45       ` Julian Brown
  2004-06-26 19:18         ` Basile Starynkevitch [local]
  1 sibling, 1 reply; 9+ messages in thread
From: Julian Brown @ 2004-06-26 13:45 UTC (permalink / raw)
  To: caml-list

On 2004-06-25, Basile Starynkevitch [local]
<basile.starynkevitch@inria.fr> wrote:
> Also note that ocamljitrun is a binary which is a plugin replacement
> for ocamlrun, this means that (as far as I understand your needs) you
> don't need to link it with your program. This means that the
> hypothetical linking issues of the LGPL is not a concern. In my
> non-lawyer opinion, ocamljitrun is like ocamlrun so can run any
> program you like, including commercial ones.

Sorry for changing the subject, but can ocamljitrun be used with
"ocamlc -custom"? I have a program which uses some small C modules,
and I couldn't find an obvious way to make it compile or run using
ocamljitrun. AFAIK, -custom links the bytecode interpreter with the
binary as part of its magic.

On topic, does that affect licencing? It doesn't really matter to me,
but it might for some people I guess...

Julian

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Re: Writing a JIT compiler
  2004-06-26 13:45       ` [Caml-list] " Julian Brown
@ 2004-06-26 19:18         ` Basile Starynkevitch [local]
  0 siblings, 0 replies; 9+ messages in thread
From: Basile Starynkevitch [local] @ 2004-06-26 19:18 UTC (permalink / raw)
  To: caml-list

On Sat, Jun 26, 2004 at 01:45:02PM +0000, Julian Brown wrote:
> On 2004-06-25, Basile Starynkevitch [local]
> <basile.starynkevitch@inria.fr> wrote:
> > Also note that ocamljitrun is a binary which is a plugin replacement
> > for ocamlrun, this means that (as far as I understand your needs) you
> > don't need to link it with your program. This means that the
> > hypothetical linking issues of the LGPL is not a concern. In my
> > non-lawyer opinion, ocamljitrun is like ocamlrun so can run any
> > program you like, including commercial ones.

My feeling is that custom linking is nearly obsolete - better use dll
libraries which are well supported in ocaml now.


> Sorry for changing the subject, but can ocamljitrun be used with
> "ocamlc -custom"? I have a program which uses some small C modules,
> and I couldn't find an obvious way to make it compile or run using
> ocamljitrun. AFAIK, -custom links the bytecode interpreter with the
> binary as part of its magic.



It is doable by linking the libcamljitrun.a instead of
libcamlrun.a. Since -lcamlrun is a builtin library name (in ocamlc
-custom), the trick (suggested by Xavier Leroy) is to copy
libcamljitrun.a into a unique directory like
/usr/local/lib/ocamljitdir/ and pass -cclib
-L/usr/local/lib/ocamljitdir/ to your ocamlc command.


> On topic, does that affect licencing? It doesn't really matter to me,
> but it might for some people I guess...

Ocamljitrun is under LGPL as is GNU lightning. I believe that the
special exception applying to Ocaml like software like ocamljitrun do
not apply to the GNU lightning library, which is LGPL, but as I said
many times, I AM NOT A LAWYER.

the Ocamljit library should be buildable as a shared library - just
edit the GNUUmakefile of ocamljit appropriately to add -fPIC to the
compilation command, and build a shared library instead of a static
one. As usual on x86 there is a small penalty (because of register
shortage for global offset table access) in shared libraries.



-- 
Basile STARYNKEVITCH -- basile dot starynkevitch at inria dot fr
Project cristal.inria.fr - temporarily 
http://cristal.inria.fr/~starynke --- all opinions are only mine 

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

end of thread, other threads:[~2004-06-26 19:19 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-06-25  8:04 [Caml-list] Writing a JIT compiler Jon Harrop
2004-06-25  8:52 ` Benjamin Geer
2004-06-25  9:53   ` Jon Harrop
2004-06-25 10:56     ` Basile Starynkevitch [local]
2004-06-25  9:59 ` Basile Starynkevitch [local]
     [not found]   ` <200406251325.15940.postmaster@jdh30.plus.com>
2004-06-25 15:55     ` Basile Starynkevitch [local]
2004-06-25 17:05       ` Daniel Bünzli
2004-06-26 13:45       ` [Caml-list] " Julian Brown
2004-06-26 19:18         ` Basile Starynkevitch [local]

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