# agam-lang/agam architecture

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

- Interactive diagram: https://gitdiagram.com/agam-lang/agam
- Repository: https://github.com/agam-lang/agam

## Overview

#### Purpose and Entry Points
Agam is a Rust workspace implementing the Agam compiled language, its compiler toolchain, runtime, SDK packaging, and Python-facing execution wrapper. The primary user entry point is the `agamc` binary in `crates/tooling/agam_driver/src/main.rs`; command routing, build/run orchestration, daemon, REPL, and compiler pipeline integration are in `crates/tooling/agam_driver/src/dispatch.rs`, `pipeline.rs`, and `cli.rs`. Source programs are `.agam` files such as `examples/hello_base.agam`.

#### Compiler Frontend
Source first crosses a diagnostic-aware frontend boundary: tokenization (`crates/core/agam_lexer/src/lexer.rs`), parsing (`crates/core/agam_parser/src/parser.rs`), and syntax representation (`crates/core/agam_ast/src/lib.rs`). Shared source spans, reports, and machine-readable diagnostics reside in `crates/core/agam_errors/src/span.rs`, `diagnostic.rs`, and `sarif.rs`. This layer accepts the base/dynamic/advance source profiles but produces a common AST.

#### Semantic and IR Middle End
Semantic analysis resolves names, types, effects, ownership/lifetimes, traits, targets, and GPU constraints in `crates/middle/agam_sema/src/checker.rs`, `infer.rs`, and `effects.rs`. Typed AST lowers through HIR (`crates/middle/agam_hir/src/lower.rs`) into MIR (`crates/middle/agam_mir/src/lower.rs`, `ir.rs`). MIR is the primary backend boundary: it supports analysis/verification, monomorphization, evaluation, autodiff-related work, and optimization passes including constant folding, DCE, inlining, loop transformations, escape analysis, and e-graph rewriting under `crates/middle/agam_mir/src/opt/`.

#### Execution and Code Generation
The driver selects `auto`, JIT, LLVM, or C execution/build paths. `crates/backends/agam_jit/src/lib.rs` provides in-memory Cranelift-oriented execution and OSR support (`osr.rs`). `crates/backends/agam_codegen/src/llvm_emitter.rs` and `c_emitter.rs` lower MIR to LLVM IR or portable C; `link_opt.rs` and `llvm_opt.rs` own backend optimization/link concerns. GPU/NPU-oriented lowering is isolated in `gpu_emitter.rs`, `spirv.rs`, and `npu.rs`, preventing hardware target details from leaking into frontend semantics.

#### Runtime and Standard Library Boundary
Generated code and JIT execution depend on `crates/runtime/agam_runtime/src/lib.rs`, which supplies ARC, cache, effects, capabilities, sandbox/security, SIMD, crypto/PQC, hardware information, and coroutine scheduling. `crates/runtime/agam_std/src/lib.rs` exposes language-facing collections, I/O/networking, numerical and tensor/ML modules; representative surfaces are `tensor.rs`, `gpu.rs`, and `io.rs`. C emission additionally ships backend runtime preludes under `crates/backends/agam_codegen/runtime/`.

#### Tooling, Packages, and Integrations
`agam_pkg` handles dependency/build graphs, cross-target support, plugins, federation, reproducibility, and supply-chain concerns in `crates/tooling/agam_pkg/src/build_graph.rs` and `supply_chain.rs`. Adjacent first-party tools provide LSP analysis (`crates/tooling/agam_lsp/src/analysis.rs`), formatting/linting, debugging through DAP/DWARF (`crates/tooling/agam_debug/src/dap.rs`), docs, profiling, and compiler testing. Machine-facing headless execution is `agamc exec --json`; Python clients wrap that process/JSON contract in `integrations/python/src/agam_ffi/tool.py`.

#### Infrastructure and External Dependencies
The material build runtime is Rust/Cargo (`Cargo.toml`, `rust-toolchain.toml`), with Python used for benchmarks, SDK scripts, and integration packaging. Native AOT relies on LLVM/clang toolchains, with Windows, Linux, and Android as active targets; environment selection is mediated by the driver and SDK scripts. CI/release infrastructure is GitHub Actions (`.github/workflows/ci.yml`, `sdk-dist.yml`, `agam-ffi-python.yml`); canonical operational automation and release validation live in `devops/scripts/package_sdk.py` and `devops/runbooks/releases/release-readiness.md`. Benchmark infrastructure compiles/runs Agam and C/C++/Rust/Go/Python comparators, then emits raw and aggregate measurement artifacts via `benchmarks/infrastructure/benchmark_harness.py` and `statistical_analyzer.py`.

Main entry points: [`crates/tooling/agam_driver/src/main.rs`](https://github.com/agam-lang/agam/blob/main/crates/tooling/agam_driver/src/main.rs) (agamc driver), [`crates/middle/agam_mir/src/ir.rs`](https://github.com/agam-lang/agam/blob/main/crates/middle/agam_mir/src/ir.rs) (MIR & optimization), [`crates/core/agam_parser/src/parser.rs`](https://github.com/agam-lang/agam/blob/main/crates/core/agam_parser/src/parser.rs) (Lexer & parser), [`crates/middle/agam_sema/src/checker.rs`](https://github.com/agam-lang/agam/blob/main/crates/middle/agam_sema/src/checker.rs) (Semantic analysis)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_tooling["Developer Tooling"]
  node_source["Agam source<br/>source files<br/>[hello_base.agam]"]
  node_driver["agamc driver<br/>Rust CLI<br/>[main.rs]"]
  node_lsp["Language server<br/>editor integration<br/>[analysis.rs]"]
end

subgraph group_compiler["Compiler Pipeline"]
  node_frontend["Lexer &amp; parser<br/>frontend<br/>[parser.rs]"]
  node_ast["Syntax AST<br/>syntax model<br/>[lib.rs]"]
  node_diagnostics["Diagnostics<br/>error reporting<br/>[diagnostic.rs]"]
  node_sema["Semantic analysis<br/>type/effect checker<br/>[checker.rs]"]
  node_hir["HIR lowering<br/>typed IR<br/>[lower.rs]"]
  node_mir["MIR &amp; optimization<br/>backend IR<br/>[ir.rs]"]
end

subgraph group_execution["Execution &amp; Runtime"]
  node_jit["JIT executor<br/>Cranelift backend<br/>[lib.rs]"]
  node_codegen["LLVM/C codegen<br/>AOT backend<br/>[llvm_emitter.rs]"]
  node_accelerators["GPU/NPU lowering<br/>accelerator backend<br/>[gpu_emitter.rs]"]
  node_runtime["Runtime<br/>[lib.rs]"]
  node_stdlib["Standard library<br/>language library<br/>[lib.rs]"]
  node_native{{"LLVM/clang toolchain<br/>external native toolchain"}}
end

subgraph group_ecosystem["Packaging &amp; Operations"]
  node_pkg["Package manager<br/>dependency builder<br/>[build_graph.rs]"]
  node_python["Python wrapper<br/>process JSON client<br/>[tool.py]"]
  node_ci["CI &amp; SDK release<br/>automation<br/>[ci.yml]"]
  node_benchmarks["Benchmark harness<br/>measurement pipeline"]
end

node_source -->|"compile/run"| node_driver
node_driver -->|"resolves build graph"| node_pkg
node_driver -->|"invokes pipeline"| node_frontend
node_frontend -->|"parses"| node_ast
node_frontend -->|"reports"| node_diagnostics
node_ast -->|"checked by"| node_sema
node_sema -->|"reports"| node_diagnostics
node_sema -->|"typed AST"| node_hir
node_hir -->|"lowers"| node_mir
node_mir -->|"JIT path"| node_jit
node_mir -->|"AOT path"| node_codegen
node_mir -->|"accelerator targets"| node_accelerators
node_jit -->|"executes with"| node_runtime
node_codegen -->|"links runtime"| node_runtime
node_codegen -->|"emits for"| node_native
node_runtime -->|"backs APIs"| node_stdlib
node_lsp -->|"analyzes source"| node_frontend
node_python -->|"agamc exec --json"| node_driver
node_ci -->|"builds and releases"| node_pkg
node_benchmarks -->|"compiles and runs"| node_driver

click node_source "https://github.com/agam-lang/agam/blob/main/examples/hello_base.agam"
click node_driver "https://github.com/agam-lang/agam/blob/main/crates/tooling/agam_driver/src/main.rs"
click node_frontend "https://github.com/agam-lang/agam/blob/main/crates/core/agam_parser/src/parser.rs"
click node_ast "https://github.com/agam-lang/agam/blob/main/crates/core/agam_ast/src/lib.rs"
click node_diagnostics "https://github.com/agam-lang/agam/blob/main/crates/core/agam_errors/src/diagnostic.rs"
click node_sema "https://github.com/agam-lang/agam/blob/main/crates/middle/agam_sema/src/checker.rs"
click node_hir "https://github.com/agam-lang/agam/blob/main/crates/middle/agam_hir/src/lower.rs"
click node_mir "https://github.com/agam-lang/agam/blob/main/crates/middle/agam_mir/src/ir.rs"
click node_jit "https://github.com/agam-lang/agam/blob/main/crates/backends/agam_jit/src/lib.rs"
click node_codegen "https://github.com/agam-lang/agam/blob/main/crates/backends/agam_codegen/src/llvm_emitter.rs"
click node_accelerators "https://github.com/agam-lang/agam/blob/main/crates/backends/agam_codegen/src/gpu_emitter.rs"
click node_runtime "https://github.com/agam-lang/agam/blob/main/crates/runtime/agam_runtime/src/lib.rs"
click node_stdlib "https://github.com/agam-lang/agam/blob/main/crates/runtime/agam_std/src/lib.rs"
click node_pkg "https://github.com/agam-lang/agam/blob/main/crates/tooling/agam_pkg/src/build_graph.rs"
click node_lsp "https://github.com/agam-lang/agam/blob/main/crates/tooling/agam_lsp/src/analysis.rs"
click node_python "https://github.com/agam-lang/agam/blob/main/integrations/python/src/agam_ffi/tool.py"
click node_ci "https://github.com/agam-lang/agam/blob/main/.github/workflows/ci.yml"
click node_benchmarks "https://github.com/agam-lang/agam/blob/main/benchmarks/infrastructure/benchmark_harness.py"

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_source,node_driver,node_lsp toneBlue
class node_frontend,node_ast,node_diagnostics,node_sema,node_hir,node_mir toneAmber
class node_jit,node_codegen,node_accelerators,node_runtime,node_stdlib,node_native toneMint
class node_pkg,node_python,node_ci,node_benchmarks toneRose
```

## Components

### Developer Tooling

- [Agam source](https://github.com/agam-lang/agam/blob/main/examples/hello_base.agam): source files · Base, dynamic, and advance profile programs. · `examples/hello_base.agam`
- [agamc driver](https://github.com/agam-lang/agam/blob/main/crates/tooling/agam_driver/src/main.rs): Rust CLI · Routes commands and orchestrates builds, runs, daemon, and REPL. · `crates/tooling/agam_driver/src/main.rs`
- [Language server](https://github.com/agam-lang/agam/blob/main/crates/tooling/agam_lsp/src/analysis.rs): editor integration · `crates/tooling/agam_lsp/src/analysis.rs`

### Compiler Pipeline

- [Lexer & parser](https://github.com/agam-lang/agam/blob/main/crates/core/agam_parser/src/parser.rs): frontend · Produces a common syntax tree from all source profiles. · `crates/core/agam_parser/src/parser.rs`
- [Syntax AST](https://github.com/agam-lang/agam/blob/main/crates/core/agam_ast/src/lib.rs): syntax model · `crates/core/agam_ast/src/lib.rs`
- [Diagnostics](https://github.com/agam-lang/agam/blob/main/crates/core/agam_errors/src/diagnostic.rs): error reporting · Shared spans, reports, and SARIF diagnostics. · `crates/core/agam_errors/src/diagnostic.rs`
- [Semantic analysis](https://github.com/agam-lang/agam/blob/main/crates/middle/agam_sema/src/checker.rs): type/effect checker · Resolves names, types, ownership, traits, targets, and GPU constraints. · `crates/middle/agam_sema/src/checker.rs`
- [HIR lowering](https://github.com/agam-lang/agam/blob/main/crates/middle/agam_hir/src/lower.rs): typed IR · `crates/middle/agam_hir/src/lower.rs`
- [MIR & optimization](https://github.com/agam-lang/agam/blob/main/crates/middle/agam_mir/src/ir.rs): backend IR · Verification, monomorphization, evaluation, autodiff work, and optimization meet here. · `crates/middle/agam_mir/src/ir.rs`

### Execution & Runtime

- [JIT executor](https://github.com/agam-lang/agam/blob/main/crates/backends/agam_jit/src/lib.rs): Cranelift backend · In-memory execution with OSR support. · `crates/backends/agam_jit/src/lib.rs`
- [LLVM/C codegen](https://github.com/agam-lang/agam/blob/main/crates/backends/agam_codegen/src/llvm_emitter.rs): AOT backend · Lowers MIR to LLVM IR or portable C and owns link optimization. · `crates/backends/agam_codegen/src/llvm_emitter.rs`
- [GPU/NPU lowering](https://github.com/agam-lang/agam/blob/main/crates/backends/agam_codegen/src/gpu_emitter.rs): accelerator backend · Isolates SPIR-V and NPU target details. · `crates/backends/agam_codegen/src/gpu_emitter.rs`
- [Runtime](https://github.com/agam-lang/agam/blob/main/crates/runtime/agam_runtime/src/lib.rs): execution runtime · Provides ARC, effects, capabilities, security, SIMD, and scheduling. · `crates/runtime/agam_runtime/src/lib.rs`
- [Standard library](https://github.com/agam-lang/agam/blob/main/crates/runtime/agam_std/src/lib.rs): language library · Language-facing collections, I/O, numerical, tensor, and ML APIs. · `crates/runtime/agam_std/src/lib.rs`
- **LLVM/clang toolchain**: external native toolchain · Builds native AOT artifacts for active targets.

### Packaging & Operations

- [Package manager](https://github.com/agam-lang/agam/blob/main/crates/tooling/agam_pkg/src/build_graph.rs): dependency builder · Build graphs, targets, plugins, reproducibility, and supply-chain controls. · `crates/tooling/agam_pkg/src/build_graph.rs`
- [Python wrapper](https://github.com/agam-lang/agam/blob/main/integrations/python/src/agam_ffi/tool.py): process JSON client · Wraps the headless agamc execution contract. · `integrations/python/src/agam_ffi/tool.py`
- [CI & SDK release](https://github.com/agam-lang/agam/blob/main/.github/workflows/ci.yml): automation · Validates builds and publishes SDK distributions. · `.github/workflows/ci.yml`
- [Benchmark harness](https://github.com/agam-lang/agam/blob/main/benchmarks/infrastructure/benchmark_harness.py): measurement pipeline · Runs Agam and comparator implementations and emits measurements. · `benchmarks/infrastructure/benchmark_harness.py`

## Connections

- Agam source → agamc driver: compile/run
- agamc driver → Package manager: resolves build graph
- agamc driver → Lexer & parser: invokes pipeline
- Lexer & parser → Syntax AST: parses
- Lexer & parser → Diagnostics: reports
- Syntax AST → Semantic analysis: checked by
- Semantic analysis → Diagnostics: reports
- Semantic analysis → HIR lowering: typed AST
- HIR lowering → MIR & optimization: lowers
- MIR & optimization → JIT executor: JIT path
- MIR & optimization → LLVM/C codegen: AOT path
- MIR & optimization → GPU/NPU lowering: accelerator targets
- JIT executor → Runtime: executes with
- LLVM/C codegen → Runtime: links runtime
- LLVM/C codegen → LLVM/clang toolchain: emits for
- Runtime → Standard library: backs APIs
- Language server → Lexer & parser: analyzes source
- Python wrapper → agamc driver: agamc exec --json
- CI & SDK release → Package manager: builds and releases
- Benchmark harness → agamc driver: compiles and runs
