Expand description
§QBE Rust
A Rust library for programmatically generating QBE Intermediate Language code.
QBE is a compiler backend that transforms simple intermediate representation (IR) into executable machine code. This library provides Rust data structures and functions to generate valid QBE IL.
§Basic Example
use qbe::{Module, Function, Linkage, Type, Value, Instr};
// Create a new module
let mut module = Module::new();
// Add a simple function that returns the sum of two integers
let mut func = Function::new(
Linkage::public(),
"add",
vec![
(Type::Word, Value::Temporary("a".to_string())),
(Type::Word, Value::Temporary("b".to_string())),
],
Some(Type::Word),
);
// Add a block to the function
let mut block = func.add_block("start");
// Add two arguments and store result in "sum"
block.assign_instr(
Value::Temporary("sum".to_string()),
Type::Word,
Instr::Add(
Value::Temporary("a".to_string()),
Value::Temporary("b".to_string()),
),
);
// Return the sum
block.add_instr(Instr::Ret(Some(Value::Temporary("sum".to_string()))));
// Add the function to the module
module.add_function(func);
// Generate QBE IL code
println!("{}", module);
This generates the following QBE IL:
export function w $add(w %a, w %b) {
@start
%sum =w add %a, %b
ret %sum
}
Structs§
- Block
- A block of QBE instructions with a label.
- DataDef
- QBE data definition
- Function
- A QBE function definition.
- Linkage
- Linkage of a function or data defintion (e.g. section and private/public status)
- Module
- A complete QBE IL module.
- TypeDef
- QBE aggregate type definition
Enums§
- Block
Item - See
Block::items
; - Cmp
- QBE comparison operations used in conditional instructions.
- Data
Item - Data definition item
- Instr
- QBE instructions representing operations in the intermediate language.
- Statement
- An IR statement
- Type
- QBE types used to specify the size and representation of values.
- Value
- QBE value that is accepted by instructions