Backend Architecture
The backend system is built around theBackend trait defined in src/backends.rs:21. Each backend implements this trait to translate Dryft’s intermediate representation into platform-specific code.
The Backend Trait
Every backend must implement the following key methods: Core Functionscomplete()- Wraps compiled code with runtime dependencies and boilerplatecreate_function()- Defines callable methods (functions and actions)user_function()- Generates code to call user-defined functions
push_integer()- Push integer literals onto the stackpush_string()- Push string literals onto the stackpush_true()/push_false()- Push boolean valuesfun_copy()/fun_drop()/fun_swap()- Stack manipulation primitives
fun_add(),fun_sub(),fun_mul(),fun_div(),fun_mod()- Math operationsfun_simple_equality()/fun_simple_non_equality()- Equality checksfun_logical_not(),fun_logical_and(),fun_logical_or()- Boolean logicfun_num_greater(),fun_num_less_than(), etc. - Numeric comparisons
create_conditional_statement()- If-then blockscreate_elect_block()- When/elect blocks (switch-like constructs)create_loop_block()- Infinite loopsloop_break()- Break from loopsmethod_return()- Return from functions
create_variable()- Declare and initialize variablesread_variable()- Push variable value onto stackwrite_variable()- Pop stack value into variable
linkin_function()- Link to external native functions
Existing Backends
C99 Backend
The C99 backend (src/backends/c99/mod.rs) transpiles Dryft code to C99-compliant C code. It:
- Uses a stack-based runtime implemented in
base.c - Prefixes all Dryft functions with
fun_to avoid naming conflicts - Implements variables using
size_tC variables withvar_prefix - Uses GCC computed goto labels for the
elect(when) construct - Supports linking to external C functions via
linkin
x86 Backend
The x86 backend (src/backends/x86/mod.rs) generates NASM-compatible x86-64 assembly. This backend:
- Outputs native assembly code for Linux ELF64 targets
- Implements a custom stack in assembly defined in
base.asm - Is currently under development (many functions use
todo!()macros) - Generates position-independent code
How to Add a Backend
Create backend directory
Create a new directory in
src/backends/ with an appropriate name for your target platform.Create mod.rs file
Inside your new directory, create a
mod.rs file. This will contain your backend implementation.Register your backend
In
src/backends.rs, add your module at the top with the other backend imports:Define a codename
In the
select() function in src/backends.rs:90, add a match arm for your backend’s codename:Implement all trait methods
Back in your
mod.rs, implement ALL the required methods from the Backend trait. You can start with todo!() placeholders for incomplete functionality:Create build profile
Create a TOML file in The fields specify:
src/targets/ that defines how to build and run programs using your backend:backend- The codename from theselect()functionintermediate- Path where the backend’s output will be writtenstdlib- Command to compile standard library (if needed)assemble- Command to assemble/compile the intermediate outputlink- Command to link object files into an executableinterpret- Command to run the final executable
Backend Selection in the Compiler
The frontend (src/frontend.rs:27) compiles Dryft source code into an intermediate representation that is backend-agnostic. The backend is selected via the build profile and transforms this IR into the target format.
The compilation flow is:
- Frontend parses
.drysource files into tokens - Tokens are processed into backend method calls stored in
CompileState - Backend generates target-specific code (C, assembly, etc.)
- Build profile commands assemble and link the output
- Final executable is produced
Tips for Backend Development
- Study the C99 backend as a reference implementation - it’s the most complete
- Start with basic operations (integers, arithmetic, functions) before tackling advanced features
- Use
todo!()for unimplemented features so the compiler still builds - Test incrementally with simple Dryft programs
- The
MockBackendinbackends.rs:98shows the minimum structure needed - Remember that Dryft is stack-based - your backend needs to maintain a data stack