What is Sleigh?
Sleigh is Ghidra’s domain-specific language for defining processor instruction sets. It enables:- Instruction Decoding - Pattern matching for instruction bytes
- Semantics - P-code translation for decompilation
- Register Definitions - Processor register architecture
- Address Spaces - Memory and register space layouts
- Disassembly - Human-readable instruction formatting
Architecture Overview
Language Files
A processor language consists of several files:Language Definition (.ldefs)
Defines available language variants:Processor Specification (.pspec)
Defines processor properties:Compiler Specification (.cspec)
Defines calling conventions:Sleigh Specification (.slaspec)
Basic Structure
Token Definitions
Tokens represent instruction bit fields:Register Definitions
Define processor registers with offsets:Attach Variables
Map token fields to registers:Constructors
Define instruction patterns and semantics:Display Sections
Control instruction display format:Context Variables
Maintain decoding state:Macros
Define reusable code snippets:Subtables
Organize complex instruction sets:P-code Semantics
Arithmetic Operations
Logical Operations
Shifts and Rotates
Memory Access
Control Flow
Real-World Example: 6502 Processor
Based on Ghidra’s 6502 implementation:Compiling Sleigh Specifications
From Gradle
From Ghidra
Ghidra automatically compiles.slaspec files at runtime if .sla is missing or outdated.
Testing Sleigh Languages
Disassembly Testing
- Create test binary with known instruction bytes
- Import into Ghidra with your language
- Verify disassembly matches expected output
- Check p-code generation in Listing window
Decompiler Testing
- Import or create test programs
- Analyze with auto-analysis
- Open in decompiler
- Verify high-level code makes sense
- Check for incorrect semantics
Best Practices
Do:
- Define all status flags correctly
- Use signed/unsigned operators appropriately (
s<,s>>, etc.) - Test with real binaries
- Document instruction semantics
- Use meaningful register and field names
- Include test cases
- Forget to update flag bits
- Ignore overflow and carry
- Use incorrect operator sizes
- Assume instruction alignment
- Skip context variables when needed
Resources
- Sleigh Documentation
- Language examples:
Ghidra/Processors/*/data/languages/ - x86 Sleigh:
Ghidra/Processors/x86/data/languages/ - ARM Sleigh:
Ghidra/Processors/ARM/data/languages/ - Sleigh compiler:
Ghidra/Features/SleighDevTools/
Next Steps
Development Overview
Return to development overview
Loader Development
Create loaders for your architecture
