# ocaml/ocaml architecture

> Architecture diagram of the GitHub repository ocaml/ocaml, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ocaml/ocaml
- Repository: https://github.com/ocaml/ocaml

## Overview

OCaml is a statically typed functional language distribution with bytecode and native compilation. Its principal user workflow is to compile a program, then either run bytecode through the interpreter or execute native code; it also offers a bytecode REPL. The tree excerpt is runtime-heavy and omits the compiler implementations, so compiler-stage wiring is based on the README, not sampled source. Runtime components below reflect the available paths and documented domain, dynamic-loading, Unix, and event capabilities; their detailed call relationships were not fully sampled.

Main entry points: [`runtime/interp.c`](https://github.com/ocaml/ocaml/blob/trunk/runtime/interp.c) (Bytecode interpreter), [`runtime/startup_byt.c`](https://github.com/ocaml/ocaml/blob/trunk/runtime/startup_byt.c) (Runtime startup), [`bytecomp/opcodes.ml.c`](https://github.com/ocaml/ocaml/blob/trunk/bytecomp/opcodes.ml.c) (Bytecode compiler), [`runtime/main.c`](https://github.com/ocaml/ocaml/blob/trunk/runtime/main.c) (Runtime entry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_compile["Compilation"]
  node_bytecode_compiler["Bytecode compiler<br/>[opcodes.ml.c]"]
  node_native_compiler["Native compiler"]
  node_bytecode["Bytecode program"]
  node_native_program["Native program"]
  node_repl["Interactive REPL"]
end

subgraph group_runtime["Runtime execution"]
  node_runtime_entry["Runtime entry<br/>[main.c]"]
  node_interpreter["Bytecode interpreter<br/>[interp.c]"]
  node_startup["Runtime startup<br/>[startup_byt.c]"]
  node_memory["Memory allocation<br/>[alloc.c]"]
  node_minor_gc["Minor collector<br/>[minor_gc.c]"]
  node_major_gc["Major collector<br/>[major_gc.c]"]
  node_dynlink["Dynamic loading<br/>[dynlink.c]"]
  node_code_fragments[("Loaded code registry<br/>[codefrag.c]")]
end

subgraph group_concurrency["Concurrency"]
  node_domains["Domains and fibers<br/>[domain.c]"]
  node_threads["System threads<br/>[st_stubs.c]"]
end

subgraph group_interfaces["System interfaces"]
  node_unix_api["Unix interface"]
  node_runtime_events["Runtime events<br/>[runtime_events.c]"]
  node_event_consumer["Event consumer"]
end

node_developer(("OCaml developer"))
node_os(("Operating system"))

node_developer -->|"compiles bytecode"| node_bytecode_compiler
node_developer -->|"compiles native"| node_native_compiler
node_bytecode_compiler -->|"emits bytecode"| node_bytecode
node_native_compiler -->|"emits native code"| node_native_program
node_developer -->|"uses interactively"| node_repl
node_bytecode -->|"runs with"| node_runtime_entry
node_runtime_entry -->|"starts runtime"| node_startup
node_startup -->|"enters interpreter"| node_interpreter
node_native_program -.->|"starts runtime"| node_startup
node_interpreter -->|"allocates values"| node_memory
node_memory -->|"requests collection"| node_minor_gc
node_minor_gc -->|"promotes work"| node_major_gc
node_startup -->|"initializes domains"| node_domains
node_domains -.->|"coordinates threads"| node_threads
node_dynlink -->|"registers code"| node_code_fragments
node_interpreter -.->|"loads modules"| node_dynlink
node_interpreter -.->|"calls system API"| node_unix_api
node_unix_api -->|"uses services"| node_os
node_runtime_events -->|"publishes events"| node_event_consumer
node_event_consumer -->|"reads event ring"| node_os

click node_bytecode_compiler "https://github.com/ocaml/ocaml/blob/trunk/bytecomp/opcodes.ml.c"
click node_runtime_entry "https://github.com/ocaml/ocaml/blob/trunk/runtime/main.c"
click node_interpreter "https://github.com/ocaml/ocaml/blob/trunk/runtime/interp.c"
click node_startup "https://github.com/ocaml/ocaml/blob/trunk/runtime/startup_byt.c"
click node_memory "https://github.com/ocaml/ocaml/blob/trunk/runtime/alloc.c"
click node_minor_gc "https://github.com/ocaml/ocaml/blob/trunk/runtime/minor_gc.c"
click node_major_gc "https://github.com/ocaml/ocaml/blob/trunk/runtime/major_gc.c"
click node_domains "https://github.com/ocaml/ocaml/blob/trunk/runtime/domain.c"
click node_threads "https://github.com/ocaml/ocaml/blob/trunk/otherlibs/systhreads/st_stubs.c"
click node_dynlink "https://github.com/ocaml/ocaml/blob/trunk/runtime/dynlink.c"
click node_code_fragments "https://github.com/ocaml/ocaml/blob/trunk/runtime/codefrag.c"
click node_unix_api "https://github.com/ocaml/ocaml/tree/trunk/otherlibs/unix"
click node_runtime_events "https://github.com/ocaml/ocaml/blob/trunk/runtime/runtime_events.c"
click node_event_consumer "https://github.com/ocaml/ocaml/blob/trunk/otherlibs/runtime_events/runtime_events_consumer.c"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_bytecode_compiler,node_native_compiler,node_bytecode,node_native_program,node_repl toneBlue
class node_runtime_entry,node_interpreter,node_startup,node_memory,node_minor_gc,node_major_gc,node_dynlink,node_code_fragments toneAmber
class node_domains,node_threads toneMint
class node_unix_api,node_runtime_events,node_event_consumer toneRose
class node_developer,node_os toneIndigo
```

## Components

### Compilation

- [Bytecode compiler](https://github.com/ocaml/ocaml/blob/trunk/bytecomp/opcodes.ml.c): `bytecomp/opcodes.ml.c`
- **Native compiler**
- **Bytecode program**
- **Native program**
- **Interactive REPL**

### Runtime execution

- [Runtime entry](https://github.com/ocaml/ocaml/blob/trunk/runtime/main.c): `runtime/main.c`
- [Bytecode interpreter](https://github.com/ocaml/ocaml/blob/trunk/runtime/interp.c): `runtime/interp.c`
- [Runtime startup](https://github.com/ocaml/ocaml/blob/trunk/runtime/startup_byt.c): `runtime/startup_byt.c`
- [Memory allocation](https://github.com/ocaml/ocaml/blob/trunk/runtime/alloc.c): `runtime/alloc.c`
- [Minor collector](https://github.com/ocaml/ocaml/blob/trunk/runtime/minor_gc.c): `runtime/minor_gc.c`
- [Major collector](https://github.com/ocaml/ocaml/blob/trunk/runtime/major_gc.c): `runtime/major_gc.c`
- [Dynamic loading](https://github.com/ocaml/ocaml/blob/trunk/runtime/dynlink.c): `runtime/dynlink.c`
- [Loaded code registry](https://github.com/ocaml/ocaml/blob/trunk/runtime/codefrag.c): `runtime/codefrag.c`

### Concurrency

- [Domains and fibers](https://github.com/ocaml/ocaml/blob/trunk/runtime/domain.c): `runtime/domain.c`
- [System threads](https://github.com/ocaml/ocaml/blob/trunk/otherlibs/systhreads/st_stubs.c): `otherlibs/systhreads/st_stubs.c`

### System interfaces

- [Unix interface](https://github.com/ocaml/ocaml/tree/trunk/otherlibs/unix): `otherlibs/unix`
- [Runtime events](https://github.com/ocaml/ocaml/blob/trunk/runtime/runtime_events.c): `runtime/runtime_events.c`
- [Event consumer](https://github.com/ocaml/ocaml/blob/trunk/otherlibs/runtime_events/runtime_events_consumer.c): `otherlibs/runtime_events/runtime_events_consumer.c`

### Other components

- **OCaml developer**
- **Operating system**

## Connections

- OCaml developer → Bytecode compiler: compiles bytecode
- OCaml developer → Native compiler: compiles native
- Bytecode compiler → Bytecode program: emits bytecode
- Native compiler → Native program: emits native code
- OCaml developer → Interactive REPL: uses interactively
- Bytecode program → Runtime entry: runs with
- Runtime entry → Runtime startup: starts runtime
- Runtime startup → Bytecode interpreter: enters interpreter
- Native program → Runtime startup: starts runtime
- Bytecode interpreter → Memory allocation: allocates values
- Memory allocation → Minor collector: requests collection
- Minor collector → Major collector: promotes work
- Runtime startup → Domains and fibers: initializes domains
- Domains and fibers → System threads: coordinates threads
- Dynamic loading → Loaded code registry: registers code
- Bytecode interpreter → Dynamic loading: loads modules
- Bytecode interpreter → Unix interface: calls system API
- Unix interface → Operating system: uses services
- Runtime events → Event consumer: publishes events
- Event consumer → Operating system: reads event ring
