Overview
Leo is a high-level language that compiles to Aleo instructions, the low-level bytecode executed by the Aleo Virtual Machine (AVM). Understanding this compilation process helps you write efficient Leo programs and debug issues.Compilation Pipeline
The Leo compiler transforms your code through several stages:Leo to Aleo Mapping
Program Declaration
Leo:.aleo.
Function Definitions
Leo:Records
Leo:Mappings
Leo:Finalizers
Leo:Aleo Instruction Set
The Aleo VM provides a rich instruction set. Here are the most commonly generated instructions:Arithmetic Instructions
Comparison Instructions
Logical Instructions
Bitwise Instructions
Type Casting
Assertions
Optimization Passes
The Leo compiler performs several optimization passes:1. Loop Unrolling
Leo unrolls all loops at compile time since circuits cannot have dynamic control flow. Leo:2. Constant Folding
Constant expressions are evaluated at compile time. Leo:3. Dead Code Elimination
Unreachable code is removed. Leo:4. Static Single Assignment (SSA)
Variables are converted to SSA form for easier analysis. Leo:5. Flattening
Nested expressions are flattened into a sequence of simple operations. Leo:Register Allocation
Aleo uses a register-based architecture. The Leo compiler allocates registers efficiently:- r0, r1, r2, …: Function inputs
- rN: Temporary values
- Output: Final result register
Visibility Modifiers
Leo’spublic keyword affects Aleo instruction visibility:
Leo:
Viewing Generated Code
To see the Aleo instructions generated from your Leo program:.aleo file will be in the build/ directory:
Debugging Tips
Use leo fmt
Format your code consistently:Check Generated Instructions
Review the generated.aleo file to understand how your code compiles:
Use Assertions Liberally
Assertions help catch errors early:Profile Circuit Size
Large circuits take longer to prove. Keep functions small and focused.Aleo VM Execution
When your Leo program runs:- Synthesis: Aleo instructions are synthesized into a constraint system
- Proving: A zero-knowledge proof is generated
- Verification: The proof is verified on-chain
- Finalization: If present, finalizer functions execute on-chain
Performance Considerations
Circuit Complexity
Each instruction adds constraints to the circuit. Minimize:- Nested loops (they unroll completely)
- Complex conditionals
- Large data structures
Prefer Simpler Operations
- Addition/subtraction: ~1 constraint
- Multiplication: ~1 constraint
- Division/modulo: More expensive
- Comparisons: Multiple constraints
Batch Operations
Group related operations in a single function to reduce proof overhead.Disassembling Aleo Code
Leo includes a disassembler to convert Aleo bytecode back to readable instructions:Related Resources
Aleo Documentation
Learn more about Aleo instructions
Grammar Reference
Leo syntax specification
Code Generation Implementation
The code generation pass is located incrates/passes/src/code_generation/. Key files:
program.rs: Generates program-level instructionsexpression.rs: Handles expression compilationstatement.rs: Compiles statementstype_.rs: Type conversions
/home/daytona/workspace/source/crates/passes/src/code_generation/.