Struct codegem::ir::ModuleBuilder
source · pub struct ModuleBuilder { /* private fields */ }Expand description
ModuleBuilder is used to build a module.
Example
let mut builder = ModuleBuilder::default()
.with_name("uwu");
let main = builder.new_function("main", &[], &Type::Void);
builder.switch_to_function(main);
let entry = builder.push_block()?;
builder.switch_to_block(entry);
let val = builder.push_instruction(
&Type::Integer(true, 32),
69u32.to_integer_operation()
)?;
builder.set_terminator(Terminator::Return(val));
let module = builder.build();Implementations§
source§impl ModuleBuilder
impl ModuleBuilder
sourcepub fn with_name(self, name: &str) -> Self
pub fn with_name(self, name: &str) -> Self
Sets the name of the module being built.
Example
let builder = ModuleBuilder::default()
.with_name("uwu");sourcepub fn build(self) -> Module
pub fn build(self) -> Module
Consumes the builder and returns a module, performing some mandatory transformations such as dead code elimination and Phi operation lowering, as well as checking for malformed IR.
Example
let empty_module = ModuleBuilder::default()
.build();sourcepub fn new_function(
&mut self,
name: &str,
args: &[(&str, Type)],
ret_type: &Type
) -> FunctionId
pub fn new_function(
&mut self,
name: &str,
args: &[(&str, Type)],
ret_type: &Type
) -> FunctionId
Adds a new function to the module being built. Returns a FunctionId, which can be used
to reference the built function.
Example
let main_func = builder.new_function("main", &[], &Type::Void);sourcepub fn switch_to_function(&mut self, id: FunctionId)
pub fn switch_to_function(&mut self, id: FunctionId)
Switches the function to which the builder is currently adding blocks and instructions.
Example
let main_func = builder.new_function("main", &[], &Type::Void);
builder.switch_to_function(main_func);sourcepub fn push_block(&mut self) -> Option<BasicBlockId>
pub fn push_block(&mut self) -> Option<BasicBlockId>
Adds a new basic block to the current function. Returns a BasicBlockId, which can be used
to reference the built basic block. Returns None if there is no function currently
selected.
A basic block is a single strand of code that does not have any control flow within it. Function calls do not count as control flow in this case. Each basic block contains instructions and ends with a single terminator, which may jump to one or more basic blocks, or exit from the current function or program.
Example
let entry_block = builder.push_block()?;sourcepub fn switch_to_block(&mut self, id: BasicBlockId)
pub fn switch_to_block(&mut self, id: BasicBlockId)
Switches the basic block to which the builder is currently adding blocks and instructions.
Example
let entry_block = builder.push_block()?;
builder.switch_to_block(entry_block);sourcepub fn push_instruction(
&mut self,
type_: &Type,
instr: Operation
) -> Option<Value>
pub fn push_instruction(
&mut self,
type_: &Type,
instr: Operation
) -> Option<Value>
Pushes an instruction to the current block in the current function. Returns an optional
Value depending on if there is a selected function and block, and if the instruction
does yield a value. (Some Operations, such as Operation::SetVar, do not return
anything.)
Example
builder.push_instruction(
&Type::Integer(true, 32),
69i32.to_integer_operation()
);sourcepub fn set_terminator(&mut self, terminator: Terminator)
pub fn set_terminator(&mut self, terminator: Terminator)
Sets the Terminator of the current basic block.
Example
builder.set_terminator(Terminator::ReturnVoid);sourcepub fn push_variable(&mut self, name: &str, type_: &Type) -> Option<VariableId>
pub fn push_variable(&mut self, name: &str, type_: &Type) -> Option<VariableId>
Pushes a variable of the given name and type to the current function. This variable can be
used anywhere in the function it is defined in. Currently, creating an Operation::GetVar
instruction before an Operation::SetVar instruction is undefined behaviour. Returns a
VariableId if there is a currently selected function, which can be used to reference
the variable.
Example
let i = builder.push_variable("i", &Type::Integer(true, 32));sourcepub fn get_function(&self) -> Option<FunctionId>
pub fn get_function(&self) -> Option<FunctionId>
sourcepub fn get_function_args(&self, func: FunctionId) -> Option<Vec<VariableId>>
pub fn get_function_args(&self, func: FunctionId) -> Option<Vec<VariableId>>
Gets the arguments of the given function as variables
Example
let current = builder.get_function()?;
let args = builder.get_function_args(current)?;