Tangu combines rapid dynamic iteration with ahead-of-time and JIT compilation. A single source file moves through a uniform compiler front-end before lowering to the Bytecode VM, Cranelift native machine code, Rust crates, or ES modules.

## Compiler architecture pipeline

1. **Lexer & Parser**: Tokenizes UTF-8 source, resolves reader tags (`#pub`, `#shtml`, `#css`, `#svg`, `#sql`), and builds a span-preserving `Expr` AST.
2. **Macro Expander**: Expands syntactic forms (`|>`, comprehensions, compile-time intrinsics `%file`, `%dir`, `%module`, `%profile`).
3. **Type Checker & Optimizer**: Checks gradual type annotations, performs flow-sensitive narrowing, folds constant literals, and eliminates dead branches according to the active optimization level.
4. **Typed IR (TIR)**: Lowers checked AST modules into a typed intermediate representation shared across JIT, native AOT, Rust, and JavaScript backends.

## Execution targets & compilation flags

Run code directly during development or emit production artifacts with `tangu compile`:

| Target | Command | Description |
| :--- | :--- | :--- |
| Bytecode VM | `tangu run main.tan` | Instant startup using compact 16-byte `Value` representation |
| Cranelift JIT | `tangu run --jit main.tan` | Compiles functions to in-memory native machine code via Cranelift |
| Strict JIT | `tangu run --jit --strict main.tan` | Rejects dynamic boxing fallbacks for predictable hot loops |
| Native AOT | `tangu compile -t native main.tan` | Emits standalone native binaries via Cranelift object codegen |
| Cranelift Object / IR | `tangu compile -t cranelift --emit-obj main.tan` | Produces ELF object files (`.o`) or `--emit-clif` textual IR |
| Rust Crate / Binary | `tangu compile -t rust --cargo main.tan` | Transpiles TIR into a Cargo project (`--bin` or `--lib`, `--rustfmt`) |
| JavaScript ESM | `tangu compile -t js -f esm --emit js,dts,map main.tan` | Emits standard ES modules, TypeScript `.d.ts` declarations, and source maps (`--bundle`, `--minify`) |

Inspect any intermediate representation directly from the command line:

```sh
tangu dump --ast main.tan
tangu dump --expanded main.tan
tangu dump --tir main.tan
tangu dump --clif main.tan
tangu dump --js main.tan
```

## Runtime profiles & optimization levels

Tangu configures execution safety checks and optimization passes via profiles (`TANGU_PROFILE=dev|test|release|analysis`) and `--opt`:

- **`dev` / `test` (`--opt none`)**: Enables full call-stack tracing, type checking, and exact source range annotations inside collections for clear stack traces. Performs array/map lowering without constant folding or dead-branch removal.
- **`release` (`--opt basic`)**: Default production runtime profile (`tangu run --release`). Enables constant folding and literal branch elimination while preserving collection item source annotations, disabling call-stack tracing and runtime type-check overhead unless requested.
- **`--opt aggressive`**: Applies constant folding and branch elimination and strips item-level metadata/range wrappers from collections to minimize allocations and memory footprint.
- **`compile --opt speed | speed_and_size`**: Controls backend optimization priority when compiling to Cranelift/native, Rust, or minified JS.

## Writing & running `#bench` suites

Annotate microbenchmarks and pipeline benchmarks with `#bench` inside `benchmarks/` (or any directory passed via `--dir`):

```tangu
(use {fold range} /iter)

#bench
(def sum-one-to-ten-thousand (Func ()
    (|> (range 0 10000)
        (fold %0 0 (Func [acc x] (+ acc x)))
    )
))
```

Compare VM execution, optimization levels, Cranelift JIT, and AOT native binaries using reproducible iteration counts and JSON output for CI regression tracking:

```sh
# Run matching benchmarks on the Bytecode VM (10 iterations by default)
tangu bench --only sum- --iters 20

# Compare optimization passes and native backends
tangu bench --only sum- --opt none
tangu bench --only sum- --opt aggressive
tangu bench --only sum- --jit
tangu bench --only sum- --native --json
```

Benchmark timings vary by host CPU, OS scheduler, and workload characteristics—always evaluate `--opt`, `--jit`, and `--native` side by side on your target deployment hardware.
