# Runtimes à la carte: crossloading native and bytecode OCaml

*2025-04-01 — idea*


Status: Completed
Level: Any
Year: 2025
Project: OxCaml Labs
Supervisors: Anil Madhavapeddy, David Allsopp
Students: Jeremy Chen


In 1998, [Fabrice le Fessant](https://fabrice.lefessant.net/) 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](https://caml.inria.fr/pub/old_caml_site/caml-list/0780.html)[^1].

This nearly a decade before OCaml 3.11 would introduce [Alain Frisch's](https://gallium.inria.fr/~frisch/ndl.txt) 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:
1. Native code OCaml applications could use OCaml as a scripting language without needing to include an assembler toolchain or solutions such as [ocaml-jit](https://github.com/tarides/ocaml-jit).
2. 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:
1. 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 toplevel `ocaml` to be able to load `.cmxs` files.
2. 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 adding `ocaml.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](https://anil.recoil.org/notes/wasm-on-exotic-targets) in the future.

See [David Allsopp](https://www.dra27.uk) write up some of the outcomes [in a blog post](https://www.dra27.uk/blog/platform/2025/09/17/late-to-the-party.html).

[^1]: A version can be found at [jrrk/efuns](https://github.com/jrrk/efuns/tree/master/dynlink)

## Related

- [EEG internships for the summer of 2025](https://anil.recoil.org/notes/eeg-interns-2025) (note, 2025-06-28)
  Coordination note for summer 2025 undergraduate and graduate internships covering projects from evidence databases to remote sensing and embedded systems.
- [Webassembly on exotic architectures (a 2025 roundup)](https://anil.recoil.org/notes/wasm-on-exotic-targets) (note, 2025-04-16)
  Survey of WebAssembly implementations on non-traditional targets including native Linux port, kernel-mode runtime, POSIX browser support and FPGA ports.

---
Canonical: https://anil.recoil.org/ideas/ocaml-bytecode-native-ffi
Type: idea
License: CC BY 4.0 <https://creativecommons.org/licenses/by/4.0/>
Tags: ocaml, functional, effects, urop
