From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id C9DAFBC37 for ; Fri, 1 Jan 2010 18:39:07 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApoDAD7CPUs+BBB3gWdsb2JhbACBSZoDAQEWJLcAhDEE X-IronPort-AV: E=Sophos;i="4.47,486,1257116400"; d="scan'208";a="40591063" Received: from smtp-119-friday.nerim.net (HELO maiev.nerim.net) ([62.4.16.119]) by mail2-smtp-roc.national.inria.fr with ESMTP; 01 Jan 2010 18:39:07 +0100 Received: from hector.lesours (ours.starynkevitch.net [213.41.244.95]) by maiev.nerim.net (Postfix) with ESMTP id E1D0AB8312; Fri, 1 Jan 2010 18:39:06 +0100 (CET) Received: from glinka.lesours ([192.168.0.1]) by hector.lesours with esmtp (Exim 4.71) (envelope-from ) id 1NQlSs-00076J-U4; Fri, 01 Jan 2010 18:39:07 +0100 Message-ID: <4B3E3343.2060809@starynkevitch.net> Date: Fri, 01 Jan 2010 18:39:15 +0100 From: Basile STARYNKEVITCH User-Agent: Mozilla-Thunderbird 2.0.0.22 (X11/20091109) MIME-Version: 1.0 To: Joel Reymont Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] ocaml, llvm and generating code at runtime References: <405ED4B9-9B3C-4BAA-9C79-C18BB8FDDB4D@gmail.com> In-Reply-To: <405ED4B9-9B3C-4BAA-9C79-C18BB8FDDB4D@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; basile:01 basile:01 ocaml:01 runtime:01 ocaml:01 bindings:01 runtime:01 ocamlopt:01 -shared:01 dynlink:01 loadfile:01 iirc:01 compiler:01 scalable:01 ocamlopt:01 Joel Reymont wrote: > Does anybody have example code that shows how to generate OCaml bindings at runtime with LLVM? > > My goal is to compile an AST into code that uses OCaml functions within the same binary that's doing the compiling. > > I don't think it can be done with OCaml since it requires a standalone assembler, linker, etc. You could generate some (Ocaml) source code, compile it (using ocamlopt -shared), and then dynamically load the just generated shared module using Dynlink.loadfile. IIRC, the "experimental" ocamlnat binary did that. > Correct me if I'm wrong, though. Mine is a web-based compiler with potentially many concurrent sessions. Running gas, ld, etc. seems a much heavier and less scalable approach that generating code at runtime. Indeed, forking an ocamlopt & then loading the shared module is more heavy, and you have a slight latency issue: if the generated code is not big (i.e. less than a thousand lines of Ocaml code), it could take a few seconds (or tenths of seconds). LLVM is rumored to be a bit faster, but is also rumored to be slow as a pure JIT (just in time) code generated (w.r.t. to other non Ocaml implementations - eg SBCL or CLISP common lisp). Polyml http://polyml.org/ is also supposed to be a JIT-ed SML implementation (it is SML not Ocaml). A few years ago, metaocaml existed, but seems dead today. However, inside a web server, you also have another issue: garbage collection (or simply disposal) of generated code. And this happens even with LLVM. You probably should dispose explicitly & manually of session data and generated code. Ten years ago, SML/NJ was supposed to also garbage-collect code. Ocaml don't do that, and today's Ocaml Dynlink module don't have any unloading code (it would be unsafe). A possible work-around could be to restart your web compiling server once in a while (e.g. twice a day). Happy new year to everyone! Regards. -- Basile STARYNKEVITCH http://starynkevitch.net/Basile/ email: basilestarynkevitchnet mobile: +33 6 8501 2359 8, rue de la Faiencerie, 92340 Bourg La Reine, France *** opinions {are only mines, sont seulement les miennes} ***