Struct budvm::ir::CodeBlockBuilder

source ·
pub struct CodeBlockBuilder<Intrinsic> { /* private fields */ }
Expand description

A type that helps build CodeBlocks.

Implementations§

source§

impl<Intrinsic> CodeBlockBuilder<Intrinsic>

source

pub fn new_argument(&mut self, name: impl Into<Symbol>) -> Argument

Adds a new argument with the given name.

source

pub fn argument(&self, name: &Symbol) -> Option<Argument>

Finds an argument by a given name, or None if not found.

source

pub fn new_label(&mut self) -> Label

Create a new label.

source

pub fn named_label(&mut self, name: impl Into<Symbol>) -> Label

Create a new label with a given name.

source

pub fn push(&mut self, operation: Instruction<Intrinsic>)

Push an instruction.

To push a value to the stack use push_to_stack.

source

pub fn lookup(&self, symbol: &Symbol) -> Option<&ScopeSymbol>

Looks up a symbol.

source

pub fn loop_info(&self, name: Option<&Symbol>) -> Option<&LoopInfo>

Returns the loop information for a given name, or the current loop if no name is provided.

source

pub fn store_into_destination( &mut self, value: LiteralOrSource, destination: Destination )

Adds the appropriate instruction to store value into destination.

source

pub fn load_from_symbol( &mut self, symbol: &Symbol, destination: Destination ) -> Result<(), LinkError>

Adds the correct instruction to load a value from symbol and store it in destination.

source

pub fn variable_index_from_name(&mut self, name: &Symbol) -> Variable

Looks up an existing location for a variable with the provided name. If an existing location is not found, new space will be allocated for it and returned.

source

pub fn new_temporary_variable(&mut self) -> Variable

Creates a new temporary variable.

Internally this simply uses a counter to create a new variable each time this is called named $1, $2, and so on.

source

pub fn finish(self) -> CodeBlock<Intrinsic>

Returns the completed code block.

source

pub fn variables(&self) -> &HashMap<Symbol, Variable>

Returns the collection of known variables.

source§

impl<Intrinsic> CodeBlockBuilder<Intrinsic>

Builder like functions adding instructions skipping the need of pushing the instructions manually.

source

pub fn add( &mut self, left: impl Into<LiteralOrSource>, right: impl Into<LiteralOrSource>, destination: impl Into<Destination> )

Adds left and right and places the result in destination.

If this operation causes an overflow, Value::Void will be stored in the destination instead.

source

pub fn sub( &mut self, left: impl Into<LiteralOrSource>, right: impl Into<LiteralOrSource>, destination: impl Into<Destination> )

Subtracts right from left and places the result in destination.

If this operation causes an overflow, Value::Void will be stored in the destination instead.

source

pub fn multiply( &mut self, left: impl Into<LiteralOrSource>, right: impl Into<LiteralOrSource>, destination: impl Into<Destination> )

Multiply left by right and places the result in destination.

If this operation causes an overflow, Value::Void will be stored in the destination instead.

source

pub fn divide( &mut self, left: impl Into<LiteralOrSource>, right: impl Into<LiteralOrSource>, destination: impl Into<Destination> )

Divides left by right and places the result in destination.

If this operation causes an overflow, Value::Void will be stored in the destination instead.

source

pub fn logical_and( &mut self, left: impl Into<LiteralOrSource>, right: impl Into<LiteralOrSource>, destination: impl Into<Destination> )

Performs a logical and of left and right and places the result in destination. This operation always results in a Value::Boolean.

left and right will be checked for thruthyness. If both are truthy, this operation will store true in destination. Otherwise, false will be stored.

Short Circuiting

This instruction will not evaluate right’s truthiness if left is false.

source

pub fn logical_or( &mut self, left: impl Into<LiteralOrSource>, right: impl Into<LiteralOrSource>, destination: impl Into<Destination> )

Performs a logical or of left and right and places the result in destination. This operation always results in a Value::Boolean.

left and right will be checked for thruthyness. If either are truthy, this operation will store true in destination. Otherwise, false will be stored.

Short Circuiting

This instruction will not evaluate right’s truthiness if left is true.

source

pub fn logical_xor( &mut self, left: impl Into<LiteralOrSource>, right: impl Into<LiteralOrSource>, destination: impl Into<Destination> )

Performs a logical exclusive-or of left and right and places the result in destination. This operation always results in a Value::Boolean.

left and right will be checked for thruthyness. If one is truthy and the other is not, this operation will store true in destination. Otherwise, false will be stored.

source

pub fn logical_not( &mut self, value: impl Into<LiteralOrSource>, destination: impl Into<Destination> )

Performs a logical not operation for value, storing the result in destination.

If the value is truthy, false will be stored in the destination. If the value is falsey, true will be stored in the destination.

source

pub fn bitwise_and( &mut self, left: impl Into<LiteralOrSource>, right: impl Into<LiteralOrSource>, destination: impl Into<Destination> )

Performs a bitwise and of left and right and places the result in destination. This operation always results in a Value::Integer.

If either left or right cannot be coerced to an integer, a fault will be returned.

The result will have each bit set based on whether the corresponding bit in both left and right are both 1.

source

pub fn bitwise_or( &mut self, left: impl Into<LiteralOrSource>, right: impl Into<LiteralOrSource>, destination: impl Into<Destination> )

Performs a bitwise or of left and right and places the result in destination. This operation always results in a Value::Integer.

If either left or right cannot be coerced to an integer, a fault will be returned.

The result will have each bit set based on whether either corresponding bit in left or right are 1.

source

pub fn bitwise_xor( &mut self, left: impl Into<LiteralOrSource>, right: impl Into<LiteralOrSource>, destination: impl Into<Destination> )

Performs a bitwise exclusive-or of left and right and places the result in destination. This operation always results in a Value::Integer.

If either left or right cannot be coerced to an integer, a fault will be returned.

The result will have each bit set based on whether only one corresponding bit in either left or right are 1.

source

pub fn bitwise_not( &mut self, value: impl Into<LiteralOrSource>, destination: impl Into<Destination> )

Performs a bitwise not operation for value, storing the result in destination. This operation always results in a Value::Integer.

If value cannot be coerced to an integer, a fault will be returned.

The result will be value with each bit flipped.

source

pub fn convert( &mut self, value: impl Into<LiteralOrSource>, kind: impl Into<ValueKind>, destination: impl Into<Destination> )

Converts a value to another type, storing the result in destination.

If value cannot be converted, a fault will be returned.

source

pub fn shift_left( &mut self, left: impl Into<LiteralOrSource>, right: impl Into<LiteralOrSource>, destination: impl Into<Destination> )

Performs a bitwise shift left of left by right bits, storing the result in destination.

This operation requires both operands to be integers. If either are not integers, a fault will be returned.

source

pub fn shift_right( &mut self, left: impl Into<LiteralOrSource>, right: impl Into<LiteralOrSource>, destination: impl Into<Destination> )

Performs a bitwise shift right of left by right bits, storing the result in destination.

This operation requires both operands to be integers. If either are not integers, a fault will be returned.

source

pub fn jump_if_false( &mut self, condition: impl Into<LiteralOrSource>, false_jump_to: impl Into<Label> )

Checks condition.is_truthy(), jumping to the target label if false.

If truthy, the virtual machine continues executing the next instruction in sequence.

If not truthy, the virtual machine jumps to label false_jump_to.

Consider using begin_if() instead for a more convinient option.

source

pub fn begin_if( &mut self, condition: impl Into<LiteralOrSource> ) -> If<'_, Intrinsic>

Begins an if block with the given condition.

If the condition is the result of a comparsion consider begin_if_comparison().

source

pub fn jump_to(&mut self, label: impl Into<Label>)

Jump execution to the address of the given Label.

Consider using begin_loop() instead of using jump_to for looping.

source

pub fn label(&mut self, label: impl Into<Label>)

Labels the next instruction with the given Label.

source

pub fn begin_loop( &mut self, name: Option<Symbol>, result: Destination ) -> LoopScope<'_, Self, Intrinsic>

Begins a loop with the given name. The result of the loop will be stored in result. If the loop does not return a result, the destination will be untouched.

source

pub fn compare( &mut self, comparison: impl Into<Comparison>, left: impl Into<LiteralOrSource>, right: impl Into<LiteralOrSource>, action: impl Into<CompareAction> )

Compares left and right using comparison to evaluate a boolean result.

If CompareAction::Store is used, the boolean result will be stored in the provided destination.

If CompareAction::JumpIfFalse is used and the result is false, execution will jump to the label specified. If the result is true, the next instruction will continue executing. Consider begin_if_comparison for more convinient jumping.

source

pub fn begin_if_comparison( &mut self, comparison: Comparison, left: impl Into<LiteralOrSource>, right: impl Into<LiteralOrSource> ) -> If<'_, Intrinsic>

Begins an if block with the given comparison.

source

pub fn push_to_stack(&mut self, value: impl Into<LiteralOrSource>)

Pushes a value to the stack.

source

pub fn load( &mut self, value: impl Into<LiteralOrSource>, variable: impl Into<Variable> )

Loads a value into a variable.

source

pub fn return_value(&mut self, value: impl Into<LiteralOrSource>)

Returns a value from the current stack frame.

source

pub fn return_void(&mut self)

Returns from the current stack frame without a value.

source

pub fn call( &mut self, function: impl Into<Symbol>, arg_count: usize, destination: impl Into<Destination> )

Calls a function.

When calling a function, values on the stack are “passed” to the function being pushed to the stack before calling the function. To ensure the correct number of arguments are taken even when variable argument lists are supported, the number of arguments is passed and controls the baseline of the stack.

Upon returning from a function call, the arguments will no longer be on the stack. The value returned from the function (or Value::Void if no value was returned) will be placed in destination.

There is also recurse() always recursing the current function.

source

pub fn recurse(&mut self, arg_count: usize, destination: impl Into<Destination>)

Calls the current function recursively.

When calling a function, values on the stack are “passed” to the function being pushed to the stack before calling the function. To ensure the correct number of arguments are taken even when variable argument lists are supported, the number of arguments is passed and controls the baseline of the stack.

Upon returning from a function call, the arguments will no longer be on the stack. The value returned from the function (or Value::Void if no value was returned) will be placed in destination.

source

pub fn call_intrinsic( &mut self, intrinsic: impl Into<Intrinsic>, arg_count: usize, destination: impl Into<Destination> )

Calls an intrinsic runtime function.

When calling a function, values on the stack are “passed” to the function being pushed to the stack before calling the function. To ensure the correct number of arguments are taken even when variable argument lists are supported, the number of arguments is passed and controls the baseline of the stack.

Upon returning from a function call, the arguments will no longer be on the stack. The value returned from the function (or Value::Void if no value was returned) will be placed in destination.

source

pub fn call_instance( &mut self, target: impl Into<LiteralOrSource>, name: impl Into<Symbol>, arg_count: usize, destination: impl Into<Destination> )

Calls a function by name on a value.

When calling a function, values on the stack are “passed” to the function being pushed to the stack before calling the function. To ensure the correct number of arguments are taken even when variable argument lists are supported, the number of arguments is passed and controls the baseline of the stack.

Upon returning from a function call, the arguments will no longer be on the stack. The value returned from the function (or Value::Void if no value was returned) will be placed in destination.

Trait Implementations§

source§

impl<'a, T, Intrinsic> Borrow<CodeBlockBuilder<Intrinsic>> for LoopScope<'a, T, Intrinsic>where T: BorrowMut<CodeBlockBuilder<Intrinsic>>,

source§

fn borrow(&self) -> &CodeBlockBuilder<Intrinsic>

Immutably borrows from an owned value. Read more
source§

impl<'a, T, Intrinsic> BorrowMut<CodeBlockBuilder<Intrinsic>> for LoopScope<'a, T, Intrinsic>where T: BorrowMut<CodeBlockBuilder<Intrinsic>>,

source§

fn borrow_mut(&mut self) -> &mut CodeBlockBuilder<Intrinsic>

Mutably borrows from an owned value. Read more
source§

impl<Intrinsic> Default for CodeBlockBuilder<Intrinsic>

source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<Intrinsic> RefUnwindSafe for CodeBlockBuilder<Intrinsic>where Intrinsic: RefUnwindSafe,

§

impl<Intrinsic> Send for CodeBlockBuilder<Intrinsic>where Intrinsic: Send,

§

impl<Intrinsic> Sync for CodeBlockBuilder<Intrinsic>where Intrinsic: Sync,

§

impl<Intrinsic> Unpin for CodeBlockBuilder<Intrinsic>where Intrinsic: Unpin,

§

impl<Intrinsic> UnwindSafe for CodeBlockBuilder<Intrinsic>where Intrinsic: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.