Runtimes à la carte: crossloading native and bytecode OCaml
This is an idea proposed in 2025 as a good starter project, and is currently being worked on by
In 1998, Fabrice le Fessant released Efuns ("Emacs for Functions"), an implementation of an Emacs-like editor entire in OCaml and which included a library for loading bytecode within native code programs A version can be found at jrrk/efuns
This nearly a decade before OCaml 3.11 would introduce Alain Frisch's native Dynlink support to OCaml. Natdynlink means that this original work has been largely forgotten, but there remain two interesting applications for being able to "cross-load" code compiled for the OCaml bytecode runtime in an OCaml native code application and vice versa:
- Native code OCaml applications could use OCaml as a scripting language without needing to include an assembler toolchain or solutions such as ocaml-jit.
- The existing bytecode REPL could use OCaml natdynlink plugins (
.cmxsfiles) directly, allowing more dynamic programming and exploration of high-performance libraries with the ease of the bytecode interpreter, but retaining the runtime performance of the libraries themselves.
This project aims to implement these two features directly in the OCaml distribution by:
- Extending the bytecode version of
Dynlinkto be able to load.cmxsfiles. This feature would be validated by extending the#loaddirective of the bytecode toplevelocamlto be able to load.cmxsfiles. - Extending the native version of
Dynlinkto be able to load bytecode units, both from.cmo/.cmafiles but also directly generated in the native code program itself. This feature would be validated by addingocaml.optto the distribution - i.e. the bytecode toplevel compiled in native code, acting as the bytecode toplevel today, but also capable of#loading.cmxsfiles, and still converting toplevel phrases for execution by the bytecode interpreter
This is a good student project for anyone seeking to gain more familiarity with a "real" compiler codebase, and to learn more about how these work towards (e.g.) hacking on