pub trait Backend {
type Output;
// Required method
fn compile(&self, func: &Function) -> Result<Self::Output, CodegenError>;
}Expand description
A code generator: lowers a function in SSA form to a concrete target representation.
This is the abstraction the crate is built around. A backend reads the IR a
front-end produced and turns it into the form a particular target consumes. The
Bytecode backend shipped here emits a flat Program; a backend layered on a
native code generator later — LLVM, Cranelift — produces that generator’s own module
type, named by the Output associated type. Drawing the boundary
as a trait is what lets a front-end be written against codegen without committing to
any one target.
A backend does not own the function and keeps no per-call state, so a single instance compiles many functions and is cheap to pass around and share.
§Implementing a backend
A backend’s compile is responsible for rejecting input it cannot lower. The
convention the bytecode backend follows is to call
Function::validate first and return
CodegenError::InvalidIr on failure, so the lowering proper only ever runs on
well-formed SSA.
§Examples
Compile with the shipped backend through the trait:
use codegen_lang::{Backend, Bytecode};
use ir_lang::{Builder, Type};
let mut b = Builder::new("noop", &[], Type::Unit);
b.ret(None);
let program = Bytecode.compile(&b.finish()).expect("noop is well-formed");
assert_eq!(program.name(), "noop");Required Associated Types§
Required Methods§
Sourcefn compile(&self, func: &Function) -> Result<Self::Output, CodegenError>
fn compile(&self, func: &Function) -> Result<Self::Output, CodegenError>
Lowers func to this backend’s Output.
§Errors
Returns CodegenError::InvalidIr when func is not well-formed SSA, and any
target-specific failure a particular backend defines.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".