Runtimes à la carte: crossloading native and bytecode OCaml
This is an idea proposed in 2025 as a good starter project, and is available for being worked on. It may be co-supervised with David Allsopp.
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[1].
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 (
.cmxs
files) 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
Dynlink
to be able to load.cmxs
files. This feature would be validated by extending the#load
directive of the bytecode toplevelocaml
to be able to load.cmxs
files. - Extending the native version of
Dynlink
to be able to load bytecode units, both from.cmo
/.cma
files but also directly generated in the native code program itself. This feature would be validated by addingocaml.opt
to the distribution - i.e. the bytecode toplevel compiled in native code, acting as the bytecode toplevel today, but also capable of#load
ing.cmxs
files, 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 webassembly in the future.
-
A version can be found at jrrk/efuns
↩︎︎
Related News
- Webassembly on exotic architectures (a 2025 roundup) / Apr 2025
- OCaml Labs / Jan 2012