Code Generation

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 conceptCode generation responsibility
Values and typesChoose LLVM representations, layouts and operations
Calls and closuresEmit calls, receivers and captured environments
OwnershipEmit moves and cleanup according to checked ownership rules
Control flow and errorsEmit branches, loops, returns and exception unwinding
GenericsEmit the concrete implementations prepared during analysis
Imported codeReference 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.