There’s a dissertation Rhiger 2001 that has a chapter dictated to a simple combinator style interface to runtime byte code generation for OCaml, although it can be recognized that data types defined under
ocaml/bytecomp are used to actually generate the byte code, the papar didn’t mention how to execute the generated code.
Is there anything I can reference on how to make use of the OCaml compiler to “poke” and execute byte code when running an OCaml program? I guess today we can probably write a
.cmo and load as dynamic library, but I don’t think this feature was present at 2001, is there a more direct approach?
I already noticed the realworldocaml site has some infos on how to understand the byte code instructions, but seems it does not cover much about the compiler infrastructure.
Not sure I fully understood your question, but:
- you can execute a bytecode program
foo.exe using the bytecode interpreter
- it is not possible to execute a
.cmo file in general as it may reference other compilation units,
- the bytecode compiler
ocamlc can show you the generated bytecode if you pass
ocamlc -dinstr foo.ml
- the debug version of the bytecode interpreter can show you a trace of each instruction as it executes by passing the
-t flag, eg
ocamlrund -t foo.exe (note that you must use
Hope that helps,
Yes you can, and some of the infrastructure for even loading and unloading native code is in place.
You can do something similar to this file, to generate the code, just instead of going Lambda → Middle_end → Asmcomp, you do Lambda → Bytecomp. Then you can make a
cmo and load it.
To execute bytecode you can look on this file, ocaml/interp.h at trunk · ocaml/ocaml · GitHub
To execute native code(you need to know what you’re doing), you can look on this project
Is what you want similar to toplevel? If yes that might be a good place to start “poking”.
Rhiger’s dissertation is not very specific (at least on page 70 that is about this), but it says that it
Using functions provided by OCaml’s interactive run-time system […] this function […] writes a list of symbolic byte-code instructions to the memory, relocates global pointers in the allocated block, and passes it to the virtual machine for execution.
It sounds like the bytecode-dynamic-linking system is used, the same that is used in the bytecode toplevel indeed, and I believe that it definitely was available in 2001. (The first commit in git history is from 1995, and it already has a toplevel.)
Yes, what I’m looking for is something similar to JIT, thank you for all the information.