# Generate and execute byte code at runtime

**URL:** <https://discuss.ocaml.org/t/generate-and-execute-byte-code-at-runtime/7318>\
**Category:** Learning\
**Tags:** compiler\
**Created:** [February 20, 2021, 3:38pm UTC](https://discuss.ocaml.org/t/generate-and-execute-byte-code-at-runtime/7318 "2021-02-20T15:38:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![LdBeth](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ldbeth/32/2680_2.png) [@LdBeth](https://discuss.ocaml.org/u/LdBeth)\
**Post date:** [February 20, 2021, 3:38pm UTC](https://discuss.ocaml.org/t/generate-and-execute-byte-code-at-runtime/7318/1 "2021-02-20T15:38:25Z")

</div>

There’s a dissertation [Rhiger 2001](https://www.brics.dk/DS/01/4/BRICS-DS-01-4.pdf) 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.

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [February 20, 2021, 4:26pm UTC](https://discuss.ocaml.org/t/generate-and-execute-byte-code-at-runtime/7318/2 "2021-02-20T16:26:29Z")

</div>

Not sure I fully understood your question, but:

- you can execute a bytecode program `foo.exe` using the bytecode interpreter `ocamlrun`: `ocamlrun foo.exe`.
- 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 `-dinstr`: `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 `ocamlrund` not `ocamlrun`).

Hope that helps,

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![EduardoRFS](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/eduardorfs/32/2259_2.png) [@EduardoRFS](https://discuss.ocaml.org/u/EduardoRFS)\
**Post date:** [February 20, 2021, 4:44pm UTC](https://discuss.ocaml.org/t/generate-and-execute-byte-code-at-runtime/7318/3 "2021-02-20T16:44:34Z")

</div>

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](https://github.com/EduardoRFS/ocaml-jit-example/blob/main/src/Compiler.ml), 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](https://github.com/ocaml/ocaml/blob/trunk/runtime/caml/interp.h)

To execute native code(you need to know what you’re doing), you can look on this project

> **[EduardoRFS/ocaml-jit-example](https://github.com/EduardoRFS/ocaml-jit-example)**
>
> unloading modules with ocaml native. Contribute to EduardoRFS/ocaml-jit-example development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![zhtprog](https://avatars.discourse-cdn.com/v4/letter/z/58f4c7/32.png) [@zhtprog](https://discuss.ocaml.org/u/zhtprog)\
**Post date:** [February 20, 2021, 7:15pm UTC](https://discuss.ocaml.org/t/generate-and-execute-byte-code-at-runtime/7318/4 "2021-02-20T19:15:25Z")

</div>

Is what you want similar to toplevel? If yes that might be a good place to start “poking”.

---

<div class="post-metadata">

**Author:** ![gasche](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gasche/32/4_2.png) [@gasche](https://discuss.ocaml.org/u/gasche)\
**Post date:** [February 20, 2021, 8:15pm UTC](https://discuss.ocaml.org/t/generate-and-execute-byte-code-at-runtime/7318/5 "2021-02-20T20:15:19Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![LdBeth](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ldbeth/32/2680_2.png) [@LdBeth](https://discuss.ocaml.org/u/LdBeth)\
**Post date:** [February 21, 2021, 2:03am UTC](https://discuss.ocaml.org/t/generate-and-execute-byte-code-at-runtime/7318/6 "2021-02-21T02:03:30Z")

</div>

Yes, what I’m looking for is something similar to JIT, thank you for all the information.
