Skip to main content

Function

Struct Function 

Source
pub struct Function { /* private fields */ }
Expand description

One function of a module: its frame layout, its tables, its code, and its debug information.

Built with FunctionBuilder or produced by decode; read-only afterwards.

§Examples

use bytecode_lang::{Inst, ModuleBuilder, ValType};

let mut m = ModuleBuilder::new();
let mut f = m.function("id", &[ValType::I64], &[ValType::I64]);
let x = f.param(0);
f.ret(x);
let id = m.add_function(f).unwrap();
let module = m.finish().unwrap();

let func = module.function(id).unwrap();
assert_eq!(func.regs(), &[ValType::I64]);
assert_eq!(func.code(), &[Inst::Ret { src: x }]);

Implementations§

Source§

impl Function

Source

pub fn name(&self) -> StrId

The function’s name.

§Examples
use bytecode_lang::ModuleBuilder;

let mut m = ModuleBuilder::new();
let mut f = m.function("main", &[], &[]);
f.ret_void();
let id = m.add_function(f).unwrap();
let module = m.finish().unwrap();
let func = module.function(id).unwrap();
assert_eq!(module.string(func.name()), Some("main"));
Source

pub fn sig(&self) -> TypeId

The function’s signature (a TypeDef::Func entry).

§Examples
use bytecode_lang::{FuncType, ModuleBuilder, TypeDef, ValType};

let mut m = ModuleBuilder::new();
let mut f = m.function("main", &[], &[]);
f.ret_void();
let id = m.add_function(f).unwrap();
let module = m.finish().unwrap();
let sig = module.function(id).unwrap().sig();
assert_eq!(module.type_def(sig), Some(&TypeDef::Func(FuncType::default())));
Source

pub fn params(&self) -> Option<&ParamList>

The function’s dynamic-call signature, if it has one (see ParamList); None means dynamic calls must pass exactly its parameters, positionally.

§Examples
use bytecode_lang::{ModuleBuilder, Param, ParamList, ValType};

let mut m = ModuleBuilder::new();
let x = m.string("x");
let mut f = m.function("f", &[ValType::Dyn], &[]);
f.set_params(ParamList::new(vec![Param::normal(x)]));
f.ret_void();
let id = m.add_function(f).unwrap();
let module = m.finish().unwrap();
assert_eq!(module.function(id).unwrap().params().map(|p| p.params.len()), Some(1));
Source

pub fn regs(&self) -> &[ValType]

The declared type of every register; the parameters come first.

§Examples
use bytecode_lang::{ModuleBuilder, ValType};

let mut m = ModuleBuilder::new();
let mut f = m.function("f", &[ValType::Bool], &[]);
let _tmp = f.reg(ValType::Dyn);
f.ret_void();
let id = m.add_function(f).unwrap();
let module = m.finish().unwrap();
assert_eq!(module.function(id).unwrap().regs(), &[ValType::Bool, ValType::Dyn]);
Source

pub fn captures(&self) -> &[ValType]

The types of the values a closure of this function captures, read with Inst::GetUpval.

§Examples
use bytecode_lang::{ModuleBuilder, UpvalIdx, ValType};

let mut m = ModuleBuilder::new();
let mut f = m.function("inner", &[], &[]);
assert_eq!(f.capture(ValType::Dyn), UpvalIdx(0));
f.ret_void();
let id = m.add_function(f).unwrap();
let module = m.finish().unwrap();
assert_eq!(module.function(id).unwrap().captures(), &[ValType::Dyn]);
Source

pub fn names(&self) -> &[StrId]

The function’s name list, indexed by NameRef.

§Examples
use bytecode_lang::ModuleBuilder;

let mut m = ModuleBuilder::new();
let x = m.string("x");
let mut f = m.function("f", &[], &[]);
let n = f.name_ref(x);
f.ret_void();
let id = m.add_function(f).unwrap();
let module = m.finish().unwrap();
assert_eq!(module.function(id).unwrap().names()[n.index()], x);
Source

pub fn type_refs(&self) -> &[TypeId]

The function’s type list, indexed by TypeRef.

§Examples
use bytecode_lang::{ModuleBuilder, TypeDef, ValType};

let mut m = ModuleBuilder::new();
let arr = m.add_type(TypeDef::Array(ValType::I64));
let mut f = m.function("f", &[], &[]);
let t = f.type_ref(arr);
f.ret_void();
let id = m.add_function(f).unwrap();
let module = m.finish().unwrap();
assert_eq!(module.function(id).unwrap().type_refs()[t.index()], arr);
Source

pub fn tables(&self) -> &[JumpTable]

The function’s jump tables, indexed by TableId.

§Examples
use bytecode_lang::{IntTy, ModuleBuilder, Target, ValType};

let mut m = ModuleBuilder::new();
let mut f = m.function("f", &[ValType::I64], &[]);
let (a, b) = (f.label(), f.label());
f.switch(IntTy::I64, f.param(0), &[a], b);
f.bind(a);
f.bind(b);
f.ret_void();
let id = m.add_function(f).unwrap();
let module = m.finish().unwrap();
let table = &module.function(id).unwrap().tables()[0];
assert_eq!((table.targets.as_slice(), table.default), (&[Target(1)][..], Target(1)));
Source

pub fn shapes(&self) -> &[CallShape]

The function’s call shapes, indexed by ShapeId.

§Examples
use bytecode_lang::{ArgKind, ModuleBuilder};

let mut m = ModuleBuilder::new();
let mut f = m.function("f", &[], &[]);
let s = f.call_shape(&[ArgKind::Positional, ArgKind::Spread]);
f.ret_void();
let id = m.add_function(f).unwrap();
let module = m.finish().unwrap();
assert_eq!(module.function(id).unwrap().shapes()[s.index()].args.len(), 2);
Source

pub fn handlers(&self) -> &[Handler]

The function’s exception-handling regions, innermost first.

§Examples
use bytecode_lang::{Inst, ModuleBuilder, ValType};

let mut m = ModuleBuilder::new();
let mut f = m.function("f", &[], &[]);
let err = f.reg(ValType::Dyn);
let (start, end, catch) = (f.label(), f.label(), f.label());
f.bind(start);
f.emit(Inst::Nop {});
f.bind(end);
f.ret_void();
f.bind(catch);
f.ret_void();
f.try_region(start, end, catch, err);
let id = m.add_function(f).unwrap();
let module = m.finish().unwrap();
let h = module.function(id).unwrap().handlers()[0];
assert_eq!((h.start, h.end, h.target.0), (0, 1, 2));
Source

pub fn code(&self) -> &[Inst]

The function’s instructions; code()[pc] is the instruction at pc.

§Examples
use bytecode_lang::{Inst, ModuleBuilder};

let mut m = ModuleBuilder::new();
let mut f = m.function("f", &[], &[]);
f.ret_void();
let id = m.add_function(f).unwrap();
let module = m.finish().unwrap();
assert_eq!(module.function(id).unwrap().code(), &[Inst::RetVoid {}]);
Source

pub fn lines(&self) -> &[LineRow]

The line table, ordered by pc.

§Examples
use bytecode_lang::ModuleBuilder;

let mut m = ModuleBuilder::new();
let file = m.string("main.mox");
let mut f = m.function("f", &[], &[]);
f.set_location(file, 3, 1);
f.ret_void();
let id = m.add_function(f).unwrap();
let module = m.finish().unwrap();
assert_eq!(module.function(id).unwrap().lines()[0].line, 3);
Source

pub fn locals(&self) -> &[LocalVar]

The named source variables.

§Examples
use bytecode_lang::{ModuleBuilder, ValType};

let mut m = ModuleBuilder::new();
let name = m.string("x");
let mut f = m.function("f", &[ValType::I64], &[]);
let (start, end) = (f.label(), f.label());
f.bind(start);
f.ret_void();
f.bind(end);
f.local(f.param(0), name, start, end);
let id = m.add_function(f).unwrap();
let module = m.finish().unwrap();
assert_eq!(module.function(id).unwrap().locals()[0].end, 1);

Trait Implementations§

Source§

impl Clone for Function

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 Function

Source§

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

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

impl Eq for Function

Source§

impl PartialEq for Function

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 Function

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.