How agam-lang/agam works
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: main.rs agamc driver · ir.rs MIR & optimization · parser.rs Lexer & parser · checker.rs Semantic analysis
Components 19
Developer Tooling
- Agam source · Base, dynamic, and advance profile programs.
examples/hello_base.agam - agamc driver · Routes commands and orchestrates builds, runs, daemon, and REPL.
crates/tooling/agam_driver/src/main.rs - Language server
crates/tooling/agam_lsp/src/analysis.rs
Compiler Pipeline
- Lexer & parser · Produces a common syntax tree from all source profiles.
crates/core/agam_parser/src/parser.rs - Syntax AST
crates/core/agam_ast/src/lib.rs - Diagnostics · Shared spans, reports, and SARIF diagnostics.
crates/core/agam_errors/src/diagnostic.rs - Semantic analysis · Resolves names, types, ownership, traits, targets, and GPU constraints.
crates/middle/agam_sema/src/checker.rs - HIR lowering
crates/middle/agam_hir/src/lower.rs - MIR & optimization · Verification, monomorphization, evaluation, autodiff work, and optimization meet here.
crates/middle/agam_mir/src/ir.rs
Execution & Runtime
- JIT executor · In-memory execution with OSR support.
crates/backends/agam_jit/src/lib.rs - LLVM/C codegen · Lowers MIR to LLVM IR or portable C and owns link optimization.
crates/backends/agam_codegen/src/llvm_emitter.rs - GPU/NPU lowering · Isolates SPIR-V and NPU target details.
crates/backends/agam_codegen/src/gpu_emitter.rs - Runtime · Provides ARC, effects, capabilities, security, SIMD, and scheduling.
crates/runtime/agam_runtime/src/lib.rs - Standard library · Language-facing collections, I/O, numerical, tensor, and ML APIs.
crates/runtime/agam_std/src/lib.rs - LLVM/clang toolchain · Builds native AOT artifacts for active targets.
Packaging & Operations
- Package manager · Build graphs, targets, plugins, reproducibility, and supply-chain controls.
crates/tooling/agam_pkg/src/build_graph.rs - Python wrapper · Wraps the headless agamc execution contract.
integrations/python/src/agam_ffi/tool.py - CI & SDK release · Validates builds and publishes SDK distributions.
.github/workflows/ci.yml - Benchmark harness · Runs Agam and comparator implementations and emits measurements.
benchmarks/infrastructure/benchmark_harness.py
Connections 20
- 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