Skip to main content

Compilation (-c)

Compile Sector9 source code to WebAssembly for deployment on the Internet Computer.

The -c flag invokes the compilation pipeline, transforming .sr9 or .mo source files into WebAssembly bytecode. Depending on flags, the build can also emit Candid interfaces, stable type signatures, source maps, and debug metadata.

Basic Usage

sector9 -c file.sr9

This compiles file.sr9 and produces file.wasm next to the input file unless -o selects a different path.

For custom output paths:

sector9 -c file.sr9 -o output.wasm

On success, the command exits silently with code 0. On failure, it prints diagnostic messages and exits with code 1.

What -c Does

The compilation pipeline performs these steps:

  1. Parse - Tokenize and parse source files
  2. Import resolution - Load library dependencies
  3. Type checking - Full type inference and validation
  4. Actor validation - Verify actor structure (in IC modes)
  5. Desugaring - Convert to intermediate representation
  6. IR optimization - Apply transformation passes
  7. Code generation - Generate WebAssembly bytecode
  8. Linking - Link in the runtime system
  9. Encoding - Produce final .wasm binary with custom sections

What -c Does NOT Do

  • Does not verify contracts with the SMT solver
  • Does not execute the program
  • Does not deploy to the Internet Computer

Use --verify for formal verification, -r for execution, or dfx for deployment.

-c vs --check vs --verify

Aspect-c--check--verify
Type checkingYesYesYes
Contract parsingYesYesYes
WebAssembly outputYesNoNo
SMT verificationNoNoYes
SpeedMediumFastSlow
Use caseDeploymentDevelopmentValidation

Compilation Targets

Sector9 can compile for different runtime environments:

# Internet Computer (default)
sector9 -c file.sr9

# Standalone WebAssembly (no system imports)
sector9 -c -no-system-api file.sr9

# WASI (for wasmtime, wasmer, etc.)
sector9 -c -wasi-system-api file.sr9

# IC reference implementation (for ic-ref-run)
sector9 -c -ref-system-api file.sr9
TargetFlagSystem APIUse Case
ICMode(default)Internet ComputerProduction deployment
WasmMode-no-system-apiNoneStandalone execution
WASIMode-wasi-system-apiWASILocal testing with wasmtime
RefMode-ref-system-apiIC referenceTesting with ic-ref-run

Output Files

The compiler can generate multiple output files:

# WebAssembly binary
sector9 -c file.sr9 -o output.wasm

# With source map for debugging
sector9 -c --map file.sr9 -o output.wasm
# Produces: output.wasm, output.wasm.map

# With Candid interface definition
sector9 -c --idl file.sr9 -o output.wasm
# Produces: output.wasm, output.did

# With stable type signature
sector9 -c --stable-types file.sr9 -o output.wasm
# Produces: output.wasm, output.most

# All outputs
sector9 -c --map --idl --stable-types file.sr9 -o output.wasm
ExtensionFlagDescription
.wasm(always)WebAssembly binary module
.map--mapSource map for debugging
.did--idlCandid interface definition
.most--stable-typesStable type signatures for upgrades

Use --check and --verify before compiling when contracts are part of the deployment gate.

Debugging and Development

# Include debug information
sector9 -c -g file.sr9

# Ignore Debug.print statements
sector9 -c --release file.sr9

# Enable runtime sanity checks
sector9 -c --sanity-checks file.sr9

# Dump intermediate representations
sector9 -c -dp file.sr9 # Parse tree
sector9 -c -dt file.sr9 # Typed AST
sector9 -c -dl file.sr9 # IR dump

Persistence and Garbage Collection

Sector9 supports two persistence models with different garbage collection options:

Enhanced Orthogonal Persistence (Default)

The default persistence model uses incremental GC and supports efficient upgrades:

# Default behavior
sector9 -c --enhanced-orthogonal-persistence file.sr9

# Incremental GC (default)
sector9 -c --incremental-gc file.sr9

Legacy Persistence

For compatibility, you can use the legacy persistence model:

# Enable legacy mode
sector9 -c --legacy-persistence file.sr9

# Legacy mode with specific GC
sector9 -c --legacy-persistence --copying-gc file.sr9
sector9 -c --legacy-persistence --compacting-gc file.sr9
sector9 -c --legacy-persistence --generational-gc file.sr9
GC StrategyFlagCompatibility
Incremental--incremental-gcBoth modes
Copying--copying-gcLegacy only
Mark-Compact--compacting-gcLegacy only
Generational--generational-gcLegacy only

Memory and Limits

Control memory allocation and stabilization limits:

# Maximum stable memory pages (default: 65536)
sector9 -c --max-stable-pages 32768 file.sr9

# Enable stable regions
sector9 -c --stable-regions file.sr9

# Custom RTS stack size (legacy mode only, default: 32 pages = 2MB)
sector9 -c --legacy-persistence --rts-stack-pages 64 file.sr9

Common Errors

M0336: Declaration-only function

error [M0336], declaration-only functions are verification-only; add a body before compiling

Functions declared without bodies (for verification specifications) cannot be compiled. Add an implementation body.

M0225: Mixin as entry point

error [M0225], a mixin cannot be used as an entry point

Mixins must be included in an actor, not compiled directly. Create an actor that includes the mixin.

M0141: Multiple declarations with actor

error [M0141], an actor must be the only non-imported declaration in a program

When compiling for the IC, a file can only contain one actor (plus imports). Move other declarations inside the actor or to separate modules.

TooLargeDataSegments

Error: The Wasm data segment size exceeds the supported maximum

The compiled module is too large. Reduce the size of inline data or split into multiple modules.

Exit Codes

CodeMeaning
0Success - compilation completed
1Failure - syntax, type, or compilation errors
2Invalid arguments - missing files or conflicting flags

Typical Workflow

# 1. Check syntax and types quickly
sector9 --package core ./core/src --check my_actor.sr9

# 2. Verify contracts formally
sector9 --package core ./core/src --verify my_actor.sr9

# 3. Compile for deployment
sector9 --package core ./core/src -c --idl my_actor.sr9 -o my_actor.wasm

# 4. Deploy with dfx
dfx deploy

Multiple Input Files

When compiling multiple files, you must specify an output file:

sector9 -c main.sr9 lib.sr9 -o output.wasm

Single files derive the output name automatically:

sector9 -c main.sr9
# Produces: main.wasm

Linking Control

By default, the runtime system is statically linked into the output:

# Skip runtime linking (advanced use)
sector9 -c -no-link file.sr9

# Disable global timer endpoint
sector9 -c -no-timer file.sr9

Optimization Flags

# Share low-level utility code (smaller code, more cycles)
sector9 -c -fshared-code file.sr9

# Do not share utility code (larger code, fewer cycles, default)
sector9 -c -fno-shared-code file.sr9

Reproducibility

The --deterministic flag currently controls Viper verification artifacts (stable names and declaration ordering). It is accepted by the CLI in compile mode, but the current implementation does not promise byte-identical .wasm output across machines.

# Useful before verification/golden generation; not a Wasm reproducibility guarantee
sector9 -c --deterministic file.sr9

Summary

  • -c compiles Sector9 source to WebAssembly
  • Default target is the Internet Computer; use flags for other targets
  • Use --idl to generate Candid interfaces for your actor
  • Use --stable-types to track upgrade compatibility
  • Use --map for source-level debugging
  • Verify contracts with --verify before compiling for production