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
| Leo Operator | Aleo Instruction | Description |
|---|---|---|
a + b | add r0 r1 into r2 | Addition |
a - b | sub r0 r1 into r2 | Subtraction |
a * b | mul r0 r1 into r2 | Multiplication |
a / b | div r0 r1 into r2 | Division |
a % b | mod r0 r1 into r2 | Modulo |
a ** b | pow r0 r1 into r2 | Exponentiation |
Comparison Instructions
| Leo Operator | Aleo Instruction | Description |
|---|---|---|
a == b | is.eq r0 r1 into r2 | Equality |
a != b | is.neq r0 r1 into r2 | Inequality |
a < b | lt r0 r1 into r2 | Less than |
a <= b | lte r0 r1 into r2 | Less than or equal |
a > b | gt r0 r1 into r2 | Greater than |
a >= b | gte r0 r1 into r2 | Greater than or equal |
Logical Instructions
| Leo Operator | Aleo Instruction | Description |
|---|---|---|
a && b | and r0 r1 into r2 | Logical AND |
a || b | or r0 r1 into r2 | Logical OR |
!a | not r0 into r1 | Logical NOT |
Bitwise Instructions
| Leo Operator | Aleo Instruction | Description |
|---|---|---|
a & b | and r0 r1 into r2 | Bitwise AND |
a | b | or r0 r1 into r2 | Bitwise OR |
a ^ b | xor r0 r1 into r2 | Bitwise XOR |
a << b | shl r0 r1 into r2 | Shift left |
a >> b | shr r0 r1 into r2 | Shift right |
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/.