Compiler

How Hew compiles: C prototype backend, LLVM target architecture, Cranelift for development.

Compiler pipeline

The Hew compiler (hewc) transforms source code through a multi-stage pipeline. The compiler is implemented in Rust, chosen for its memory safety, algebraic data types for modeling the AST, and excellent LLVM bindings.

Source Tokens AST Types IR Code

Internally, the IR uses a three-tier design:

  • HIR (High-level IR) — Preserves source semantics, actors, capabilities, and concurrency constructs (select/race/join)
  • MIR (Mid-level IR) — SSA form with explicit mailbox ops, select/race/join lowering, and flow-sensitive analysis
  • LIR (Low-level IR) — Close to LLVM IR, ready for code generation

The LIR is lowered to LLVM IR for production builds, Cranelift IR for fast development builds, or C code for the bootstrap prototype.

C backend (current prototype)

The C backend is the current prototype backend used for bootstrapping and initial development. It generates portable C code that is then compiled by your system's C compiler (GCC or Clang).

Terminal
# Default: C backend
hewc hello.hew

# Explicit: C backend
hewc --backend c hello.hew

This approach provides maximum portability and leverages decades of C compiler optimizations. The C backend serves as the initial bootstrap target while the LLVM backend is developed as the production architecture.

LLVM backend (target architecture)

The LLVM backend is the target production architecture. It generates LLVM IR (.ll files) for production-quality optimizations and access to 20+ target architectures (x86, ARM, RISC-V, WASM, and more).

Terminal
# LLVM backend
hewc --backend llvm hello.hew

LLVM provides strong debugging support (DWARF, PDB) and link-time optimization (LTO).

Cranelift backend (development)

For development, Cranelift provides 4-5x faster compile times than LLVM with ~15-30% slower generated code — acceptable for debug builds and fast iteration cycles.

Terminal
# Cranelift backend (fast development builds)
hewc --backend cranelift hello.hew

Runtime library

The Hew runtime provides the infrastructure that actors depend on at execution time:

  • M:N work-stealing scheduler — Worker threads (one per CPU core), each with a per-worker run queue. Idle workers steal from busy workers' queues. Inspired by Go, Tokio, and BEAM.
  • Message budget / fairness — Each actor activation has a message budget (similar to BEAM reductions). Cooperative preemption at message boundaries and await points prevents starvation through queue aging and round-robin within priority levels.
  • Actor mailboxes — Bounded message queues with configurable overflow policies. Per-actor heaps for memory isolation.
  • Structured concurrency — Scope-based task management with cooperative cancellation at message boundaries and await points.
  • IO integration — Platform-specific event loops (epoll/kqueue/IOCP), io_uring on Linux, and separate thread pools for blocking operations.
  • Timer wheels — Efficient timeouts for supervision windows and scheduled operations.

Current status and roadmap

The implementation follows a phased plan:

  • Phase A — Compiler front-end: lexer, parser, AST, type checking (Send/Frozen rules)
  • Phase B — Runtime: scheduler, actor mailboxes, bounded channels, timers, TCP
  • Phase C — Supervision: supervisor tree runtime and syntax lowering
  • Phase D — Wire tooling: schema compiler, compatibility checker, encoder/decoder
  • Phase E — Native codegen: LLVM backend, LTO, predictable allocation model

Self-hosting is a long-term goal. The bootstrap chain progresses from the current Rust implementation through a kernel-language Hew compiler, culminating in a fully self-sustaining compiler verified through diverse double compilation.