Compilation
This page explains the StoffelLang compilation process, from source code to executable bytecode, including optimization techniques and debugging capabilities.Overview
StoffelLang uses a multi-stage compilation pipeline that transforms human-readable source code into efficient bytecode for the StoffelVM. The compiler is designed to provide excellent error messages, strong type checking, and optimizations specific to MPC computations.Compilation Pipeline
Stage 1: Lexical Analysis
The lexer tokenizes the source code, breaking it into meaningful tokens:- Indentation-based: Uses 2-space indentation (tabs not allowed)
- Unicode support: Full UTF-8 string and identifier support
- Error recovery: Continues parsing after errors to find more issues
Stage 2: Parsing
The parser builds an Abstract Syntax Tree (AST) from the token stream:- Recursive descent: Predictable parsing with clear error messages
- Location tracking: Every AST node includes source location
- Error recovery: Attempts to continue parsing after syntax errors
Stage 3: Semantic Analysis
The semantic analyzer performs type checking and builds the symbol table: Type Checking:Stage 4: Code Generation
Generates StoffelVM bytecode from the validated AST:Using the Compiler
Basic Compilation
Compilation Options
Optimization Levels
Output Formats
Debug Information
Optimization Techniques
Constant Folding
The compiler evaluates constant expressions at compile time:Dead Code Elimination
Removes unreachable code:Function Inlining
Small functions may be inlined at call sites:Secret Operation Optimization
Special optimizations for MPC operations:Error Handling
Syntax Errors
Clear error messages with source locations:Type Errors
Detailed type mismatch information:Semantic Errors
Context-aware error messages:Intermediate Representations
Viewing IR with —print-ir
- Tokens: Lexical analysis output
- AST: Parsed syntax tree
- Type Information: Resolved types and symbols
- Bytecode: Generated VM instructions
Example IR Output
Binary Format
File Structure
StoffelLang produces.stfbin files with the following structure:
Binary Inspection
Compilation Workflow
Single File Compilation
Multi-File Projects
Development Workflow
Performance Considerations
Compilation Speed
| Optimization Level | Compile Time | Runtime Performance |
|---|---|---|
| -O0 | Fastest | Basic |
| -O1 | Fast | Good |
| -O2 | Medium | Better |
| -O3 | Slowest | Best |
Memory Usage
- Compiler memory: Scales with source code size and complexity
- Binary size: Optimizations can reduce final binary size
- Runtime memory: Efficient bytecode reduces VM memory usage
Secret Type Optimization
Special considerations for MPC operations:Troubleshooting
Common Compilation Errors
“Tabs are not allowed”Debugging Tips
- Use —print-ir to see what the compiler generated
- Start with -O0 for debugging, optimize later
- Check types explicitly rather than relying on inference
- Use —verbose to see detailed compilation steps
Performance Issues
- Profile with —trace-instr during execution
- Try different optimization levels
- Review generated bytecode with —disassemble
- Minimize secret operations where possible