Hello All,
(I asked something similar to poly@prolingua...)
Does PolyML provide facilities to generate (machine) code at runtime (on Linux/AMD64 or Linux/x86), either in a type safe way (in the spirit of MetaML or MetaOcaml) or in a more low level unsafe way (e.g. like LLVM, Libjit, GNU lightning, ....)
Regards
Ref: http://metaocaml.org/ http://llvm.org/ https://savannah.gnu.org/projects/lightning/ http://www.gnu.org/software/lightning/
Regards
On Fri, 7 Dec 2007, Basile STARYNKEVITCH wrote:
Does PolyML provide facilities to generate (machine) code at runtime (on Linux/AMD64 or Linux/x86), either in a type safe way (in the spirit of MetaML or MetaOcaml) or in a more low level unsafe way (e.g. like LLVM, Libjit, GNU lightning, ....)
Poly/ML is an ``incremental compiler'' and always produces native code, even at runtime. The mechanism for this is similar to traditional EVAL in Lisp, but the code is properly compiled and type-checked (only at runtime-compilation-evaluation, though).
While this is not proper meta programming, it works very smoothly. We are using this facility on an everyday basis in the Isabelle system http://isabelle.in.tum.de/
The most basic way is to invoke PolyML.use on source files. Below is a more convenient wrapper taking strings, with optional redirection of output/error, and proper error line numbers:
fun use_text name (print, err) verbose txt = let val in_buffer = ref (SML90.explode txt); val out_buffer = ref ([]: string list); fun output () = SML90.implode (rev (case ! out_buffer of "\n" :: cs => cs | cs => cs));
val line_no = ref 1; fun line () = ! line_no; fun get () = (case ! in_buffer of [] => "" | c :: cs => (in_buffer := cs; if c = "\n" then line_no := ! line_no + 1 else (); c)); fun put s = out_buffer := s :: ! out_buffer;
fun exec () = (case ! in_buffer of [] => () | _ => (PolyML.compilerEx (get, put, line, name) (); exec ())); in exec () handle exn => (err (output ()); raise exn); if verbose then print (output ()) else () end;
Examples:
use_text "test" (TextIO.print, TextIO.print) true "val a = 42";
val a = 42 : int val it = () : unit
use_text "test" (TextIO.print, TextIO.print) true "1 + true";
Error: in 'test', line 1. Can't unify Int32.int/int with bool (Overloading does not include type) Found near +( 1, true) Static errors (pass2)
Makarius
Makarius wrote:
On Fri, 7 Dec 2007, Basile STARYNKEVITCH wrote:
Does PolyML provide facilities to generate (machine) code at runtime (on Linux/AMD64 or Linux/x86), either in a type safe way (in the spirit of MetaML or MetaOcaml) or in a more low level unsafe way (e.g. like LLVM, Libjit, GNU lightning, ....)
Poly/ML is an ``incremental compiler'' and always produces native code, even at runtime. The mechanism for this is similar to traditional EVAL in Lisp, but the code is properly compiled and type-checked (only at runtime-compilation-evaluation, though).
While this is not proper meta programming, it works very smoothly. We are using this facility on an everyday basis in the Isabelle system http://isabelle.in.tum.de/
The most basic way is to invoke PolyML.use on source files.
There is no way to at least work on abstract syntax tree? While generating code as string (or files) is definitely doable, it is not really convenient. Besides, its also have a cost (both in the human developer's coding time and in the machine time to print & reparse it) which I would be interested to avoid.
IIRC, some old SML/NJ implementations had public modules to manipulate these AST without having to print & reparse them.
On Sat, 8 Dec 2007, Basile STARYNKEVITCH wrote:
There is no way to at least work on abstract syntax tree? While generating code as string (or files) is definitely doable, it is not really convenient. Besides, its also have a cost (both in the human developer's coding time and in the machine time to print & reparse it) which I would be interested to avoid.
By delving deeper into Poly/ML internals you should be able to operate on parse trees also, but this is getting very unportable. The source-as-string based approach has the advantage that most SML systems are able to provide such a facility, as we do in Isabelle for SML/NJ, MosML, and Poly/ML.
I don't think you waste much time in printing/parsing the ML sources -- Poly/ML is very fast in this respect. Of course, it depends on the granularity of runtime compilation you have in mind. Ours is relatively coarse.
Makarius