Skip to main content

Program

Struct Program 

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

A lowered function: a flat bytecode program ready to be inspected, serialized, or run.

A program is produced by a Backend — for the bytecode target, by Bytecode or the compile shortcut. It owns the function’s name, the registers holding its parameters, a count of every register it uses, and the op stream. Control-flow ops refer to positions in that stream through Labels, which label_offset resolves to op indices. Execution begins at the first op, the entry block.

The Display implementation renders the program as a readable disassembly, which is the easiest way to see what a backend produced.

§Examples

use codegen_lang::compile;
use ir_lang::{Builder, BinOp, Type};

// fn double(x: int) -> int { x + x }
let mut b = Builder::new("double", &[Type::Int], Type::Int);
let x = b.block_params(b.entry())[0];
let sum = b.bin(BinOp::Add, x, x);
b.ret(Some(sum));
let program = compile(&b.finish()).expect("double is well-formed");

assert_eq!(program.name(), "double");
assert_eq!(program.params().len(), 1);
assert_eq!(program.register_count(), 2); // x and the sum
assert!(!program.is_empty());

Implementations§

Source§

impl Program

Source

pub fn name(&self) -> &str

Returns the function’s name.

§Examples
use codegen_lang::compile;
use ir_lang::{Builder, Type};

let mut b = Builder::new("main", &[], Type::Unit);
b.ret(None);
assert_eq!(compile(&b.finish()).unwrap().name(), "main");
Source

pub fn params(&self) -> &[Reg]

Returns the registers holding the function’s parameters, in declaration order.

These are the registers an interpreter writes the call arguments into before it begins executing the op stream.

§Examples
use codegen_lang::compile;
use ir_lang::{Builder, Type};

let mut b = Builder::new("f", &[Type::Int, Type::Bool], Type::Unit);
b.ret(None);
let program = compile(&b.finish()).unwrap();
assert_eq!(program.params().len(), 2);
Source

pub const fn register_count(&self) -> u32

Returns the number of registers the program uses; valid register numbers are 0..register_count.

§Examples
use codegen_lang::compile;
use ir_lang::{Builder, Type};

let mut b = Builder::new("f", &[Type::Int], Type::Int);
let x = b.block_params(b.entry())[0];
let one = b.iconst(1);
let r = b.bin(ir_lang::BinOp::Add, x, one);
b.ret(Some(r));
// x, the constant, and the sum.
assert_eq!(compile(&b.finish()).unwrap().register_count(), 3);
Source

pub fn ops(&self) -> &[Op]

Returns the program’s ops, in execution order.

§Examples
use codegen_lang::{compile, Op};
use ir_lang::{Builder, Type};

let mut b = Builder::new("f", &[], Type::Unit);
b.ret(None);
let program = compile(&b.finish()).unwrap();
assert!(matches!(program.ops(), [Op::Return { value: None }]));
Source

pub fn len(&self) -> usize

Returns the number of ops in the program.

Source

pub fn is_empty(&self) -> bool

Returns true if the program has no ops. A program lowered from a valid function is never empty: its entry block always ends in a terminator op.

Source

pub fn label_offset(&self, label: Label) -> Option<usize>

Resolves a label to the index of the op it points at, or None if the label does not belong to this program.

§Examples
use codegen_lang::compile;
use ir_lang::{Builder, Type};

let mut b = Builder::new("f", &[], Type::Unit);
b.ret(None);
let program = compile(&b.finish()).unwrap();
// Execution starts at the entry label, which is the first op.
assert_eq!(program.label_offset(program.entry()), Some(0));
Source

pub const fn entry(&self) -> Label

Returns the entry label, where execution begins: always L0, the source function’s entry block, which lowers to the first op.

Trait Implementations§

Source§

impl Clone for Program

Source§

fn clone(&self) -> Program

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 Program

Source§

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

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

impl<'de> Deserialize<'de> for Program

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Program

Source§

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

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

impl PartialEq for Program

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Program

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Program

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

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

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.