Skip to main content

Terminator

Enum Terminator 

Source
#[non_exhaustive]
pub enum Terminator { Jump(BlockCall), Branch { cond: Value, then_dest: BlockCall, else_dest: BlockCall, }, Switch { value: Value, cases: Vec<SwitchCase>, default: BlockCall, }, Return { values: Vec<Value>, }, Unreachable, Trap { code: u32, }, TailCall(Box<Call>), Invoke { call: Box<Call>, normal: BlockCall, unwind: BlockCall, }, Check { op: Box<InstData>, normal: BlockCall, error: BlockCall, }, Resume { payload: Value, }, }
Expand description

The instruction that ends a block and says where control goes next.

§Examples

use ir_lang::{Block, BlockCall, Terminator};

let t = Terminator::Jump(BlockCall::new(Block::from_u32(1), &[]));
assert_eq!(t.successors().count(), 1);
assert!(Terminator::Unreachable.successors().next().is_none());

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Jump(BlockCall)

Unconditional jump.

§

Branch

Two-way branch on a bool.

Fields

§cond: Value

The condition.

§then_dest: BlockCall

Taken when cond is true.

§else_dest: BlockCall

Taken when cond is false.

§

Switch

Multi-way branch on an integer; case values are distinct.

Fields

§value: Value

The scrutinee.

§cases: Vec<SwitchCase>

The cases.

§default: BlockCall

Taken when no case matches.

§

Return

Return from the function with values matching its signature’s returns.

Fields

§values: Vec<Value>

The returned values.

§

Unreachable

Control never reaches here; reaching it is undefined behaviour.

§

Trap

Abort the program deterministically with code (OPS §6 codes 1–5 for operation errors; other codes are the language’s own).

Fields

§code: u32

The trap code.

§

TailCall(Box<Call>)

A tail call: the callee’s returns become this function’s returns. The callee’s convention and returns must equal this function’s; gc must be empty (the frame is gone).

§

Invoke

A call with an exception edge: on normal return control goes to normal, if the callee unwinds it goes to unwind, a landing pad.

Fields

§call: Box<Call>

The call.

§normal: BlockCall

The normal-return edge.

§unwind: BlockCall

The unwind edge.

§

Check

An operation whose policy has an error field (or char_from_u32): on success control goes to normal (its result is Result(0)); on failure to error with the error code as ErrorCode.

Fields

§op: Box<InstData>

The operation: a Unary, Binary, or Convert instruction.

§normal: BlockCall

The success edge.

§error: BlockCall

The error edge.

§

Resume

Unwind out of this function with payload (a ptr): rethrows after cleanup in a landing pad, or raises an exception the runtime prepared.

Fields

§payload: Value

The exception payload.

Implementations§

Source§

impl Terminator

Source

pub fn successors(&self) -> Successors<'_> ⓘ

The outgoing edges, in a fixed order (switch cases first, then the default; then before else; normal before unwind/error).

§Examples
use ir_lang::{Block, BlockCall, Terminator, Value};

let t = Terminator::Branch {
    cond: Value::from_u32(0),
    then_dest: BlockCall::new(Block::from_u32(1), &[]),
    else_dest: BlockCall::new(Block::from_u32(2), &[]),
};
let targets: Vec<_> = t.successors().map(|c| c.block.index()).collect();
assert_eq!(targets, [1, 2]);
Source

pub fn for_each_successor_mut(&mut self, f: impl FnMut(&mut BlockCall))

Mutable access to the outgoing edges, in the same order as successors.

§Examples
use ir_lang::{Block, BlockCall, Terminator};

let mut t = Terminator::Jump(BlockCall::new(Block::from_u32(1), &[]));
t.for_each_successor_mut(|c| c.block = Block::from_u32(5));
assert_eq!(t.successors().next().unwrap().block, Block::from_u32(5));
Source

pub fn for_each_operand(&self, f: impl FnMut(Value))

Calls f on every SSA value the terminator reads, including the values passed along its edges.

§Examples
use ir_lang::{Terminator, Value};

let mut n = 0;
Terminator::Return { values: vec![Value::from_u32(0)] }.for_each_operand(|_| n += 1);
assert_eq!(n, 1);
Source

pub fn map_operands(&mut self, f: impl FnMut(Value) -> Value)

Rewrites every SSA value the terminator reads, including edge arguments.

§Examples
use ir_lang::{Terminator, Value};

let mut t = Terminator::Resume { payload: Value::from_u32(0) };
t.map_operands(|_| Value::from_u32(9));
assert_eq!(t, Terminator::Resume { payload: Value::from_u32(9) });
Source

pub fn opcode(&self) -> &'static str

The textual opcode (jump, br, invoke, …).

§Examples
use ir_lang::Terminator;

assert_eq!(Terminator::Unreachable.opcode(), "unreachable");

Trait Implementations§

Source§

impl Clone for Terminator

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Terminator

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Terminator

Source§

impl Hash for Terminator

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Terminator

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Terminator

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.