I am implementing a ISO/IEC 13211-1:1995 compliant Prolog system in OCaml.
For the code generation (a.k.a. JIT) part I ...
ocamlopt
, ...The approach is somewhat crude but good enough as a proof-of-concept.
Lately, it occurred to me that compiling to OCaml bytecode could be an alternative1.
So my actual question is two-fold:
How can I compile OCaml bytecode to native code?
Are there OCaml libraries for emitting/handling bytecode?
1 And a nice one, too, as this would allow using js_of_ocaml
.
Well, while technically it is possible to compile bytecode to C and then to native code, e.g., there was a project doing this, I would advice against using bytecode as the target. First of all, the bytecode is less stable and rather undocumented. Next, you would find it harder to emit the bytecode, since you will be doing the work of ocamlc compiler. Again, going back to the stability question, your project will bitrot very fast and will be very painful to maintain, as every change to bytecode will break your code, and you will have to either support multiple branches for different versions or drop support for older versions of the compiler.
Finally, emitting ml will give you choice to use both bytecode and nativecode by just choosing an appropriate compiler. You could also use js_of_ocaml or any other backends. Also, historically, ML and OCaml was specifically designed to be a target of code generation, especially for the needs of proof assistance, see Coq and F* for example, and they use ML not bytecode.
Are there OCaml libraries for emitting/handling bytecode?
There is some number of internal to the compiler tools, that I usually use as the reference implementation (and instead of the absent documentation of bytecode), see the tools folder, e.g., this is the bytecode disassembler.