Code Generation
Code generation translates the checked program into LLVM IR. It reads the analyzed AST, declaration table and type registry produced by semantic analysis.
The analysis contract
By this stage, calls have selected declarations, expressions have resolved types, and required generic bodies have been specialized and checked. Codegen uses those decisions to choose LLVM types, locate functions and storage, and emit operations. It does not repeat name lookup, overload selection or type inference.
Declarations are emitted before bodies that may reference them, so source order does not restrict calls between functions.
Lowering the program
| Sun concept | Code generation responsibility |
|---|---|
| Values and types | Choose LLVM representations, layouts and operations |
| Calls and closures | Emit calls, receivers and captured environments |
| Ownership | Emit moves and cleanup according to checked ownership rules |
| Control flow and errors | Emit branches, loops, returns and exception unwinding |
| Generics | Emit the concrete implementations prepared during analysis |
| Imported code | Reference compiled declarations using stable identities |
Global initialization also follows an analysis decision: known values become static data; other initializers become startup code. Calls across the C boundary use the target platform's calling convention.
Executable output
The resulting LLVM IR is optimized, then either executed through the JIT or compiled to object files and linked into a native executable. Moon bundles keep compiled code together with the metadata needed by later importers.