pub enum Terminator {
Return(Option<Value>),
Jump(Block, Vec<Value>),
Branch {
cond: Value,
then_block: Block,
then_args: Vec<Value>,
else_block: Block,
else_args: Vec<Value>,
},
}Expand description
The single instruction that ends a basic block and transfers control.
Exactly one terminator ends every block. A Jump or the two
arms of a Branch carry an argument per parameter of the
target block — that is how a value is threaded across a control-flow join in SSA
form, in place of a phi node. A Return leaves the
function.
§Examples
use ir_lang::{Builder, Type, Terminator};
let mut b = Builder::new("f", &[], Type::Unit);
b.ret(None);
let func = b.finish();
assert!(matches!(func.terminator(func.entry()), Some(Terminator::Return(None))));Variants§
Return(Option<Value>)
Return from the function, optionally with a value. Some(v) returns v,
whose type must match the function’s return type; None returns from a
function whose return type is Unit.
Jump(Block, Vec<Value>)
Jump unconditionally to a block, passing one argument per target parameter.
Branch
Branch on a Bool condition: take the first block (and
its arguments) when the condition is true, the second otherwise. Each block’s
arguments are matched against that block’s parameters.
Fields
Implementations§
Source§impl Terminator
impl Terminator
Sourcepub fn each_successor(&self, f: impl FnMut(Block))
pub fn each_successor(&self, f: impl FnMut(Block))
Calls f once for each block this terminator can transfer control to, in
order. A Return calls f zero times.
This is how the control-flow graph is read: the successors of a block are the targets of its terminator.
§Examples
use ir_lang::{Builder, Type};
let mut b = Builder::new("f", &[], Type::Unit);
let exit = b.create_block(&[]);
b.jump(exit, &[]);
b.switch_to(exit);
b.ret(None);
let func = b.finish();
let mut succs = Vec::new();
if let Some(term) = func.terminator(func.entry()) {
term.each_successor(|blk| succs.push(blk));
}
assert_eq!(succs, vec![exit]);Trait Implementations§
Source§impl Clone for Terminator
impl Clone for Terminator
Source§fn clone(&self) -> Terminator
fn clone(&self) -> Terminator
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Terminator
impl Debug for Terminator
Source§impl<'de> Deserialize<'de> for Terminator
impl<'de> Deserialize<'de> for Terminator
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for Terminator
impl PartialEq for Terminator
Source§fn eq(&self, other: &Terminator) -> bool
fn eq(&self, other: &Terminator) -> bool
self and other values to be equal, and is used by ==.