Zig Sema: ZIR => AIR
2 days ago
- Sema is the compiler stage after AstGen that produces AIR (Analyzed Intermediate Representation) from ZIR, performing both type analysis and comptime evaluation.
- AIR is fully typed, generated per-function, and consists of instructions, extra data, and a values array for comptime-known constants.
- Key data structures include Value (comptime values), Type (comptime types), and TypedValue (value paired with its exact type).
- The core function analyzeBodyInner loops over ZIR instructions, mapping each to zero or more AIR instructions using the inst_map.
- Comptime evaluation enables constant folding, type coercion, and target-specific emulation, as demonstrated in the addition and type conversion examples.
- Sema is invoked lazily per referenced declaration, supporting Zig's lazy analysis for fast compilation and minimal codegen.
- After Sema, AIR is passed to codegen backends (LLVM, WASM, etc.) for final lowering to machine code.