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§

Sets the name of the module being built.

Example
let builder = ModuleBuilder::default()
    .with_name("uwu");

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();

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);

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);

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()?;

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);

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()
);

Sets the Terminator of the current basic block.

Example
builder.set_terminator(Terminator::ReturnVoid);

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));

Gets the currently selected function.

Example
let current = builder.get_function();

Gets the arguments of the given function as variables

Example
let current = builder.get_function()?;
let args = builder.get_function_args(current)?;

Gets the currently selected basic block.

Example
let current = builder.get_block()?;

Trait Implementations§

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.