Skip to main content

Compiled

Struct Compiled 

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

A compiled unit: the LSB module, its entry points, and the map from every instruction back to the HIR node it was compiled from.

§Examples

use hir_lang::{Builder, Name};
use intern_lang::Interner;

let mut names = Interner::new();
let mut b = Builder::new();
let one = b.int(1);
let body = b.block(&[], Some(one));
let f = b.func(Name::new(names.intern("one")), &[], body);
let root = b.module(None, &[f]);
let hir = b.finish(root)?;

let out = bcgen_lang::compile(&hir, &names)?;
let func = out.function(f).unwrap();
// Every instruction maps back to a node of the unit.
for pc in 0..out.module().function(func).unwrap().code().len() {
    assert!(out.origin(func, pc as u32).is_some());
}

Implementations§

Source§

impl Compiled

Source

pub fn module(&self) -> &Module

The LSB module.

§Examples
let hir = { let mut b = hir_lang::Builder::new(); let r = b.module(None, &[]); b.finish(r)? };
let out = bcgen_lang::compile(&hir, &intern_lang::Interner::new())?;
assert_eq!(out.module().functions().len(), 1); // the entry
Source

pub fn into_module(self) -> Module

The LSB module, by value (to hand to a VM or an encoder).

§Examples
let hir = { let mut b = hir_lang::Builder::new(); let r = b.module(None, &[]); b.finish(r)? };
let module = bcgen_lang::compile(&hir, &intern_lang::Interner::new())?.into_module();
assert!(!bytecode_lang::encode(&module).is_empty());
Source

pub fn entry(&self) -> FuncId

The entry function, () -> dyn, exported as <entry>: it returns the value of the module body (nil without one). Under BodyMode::Start the body runs once, in the start function, and the entry reads its kept value; under BodyMode::Entry the entry is the body.

§Examples
let hir = { let mut b = hir_lang::Builder::new(); let r = b.module(None, &[]); b.finish(r)? };
let out = bcgen_lang::compile(&hir, &intern_lang::Interner::new())?;
assert!(out.module().function(out.entry()).is_some());
Source

pub fn start(&self) -> Option<FuncId>

The module’s start function, if it has one: global and constant initializers, defaults evaluated once, and (under BodyMode::Start) the body.

§Examples
let hir = { let mut b = hir_lang::Builder::new(); let r = b.module(None, &[]); b.finish(r)? };
assert_eq!(bcgen_lang::compile(&hir, &intern_lang::Interner::new())?.start(), None);
Source

pub fn result_global(&self) -> Option<GlobalId>

The global holding the module body’s value under BodyMode::Start.

§Examples
let hir = { let mut b = hir_lang::Builder::new(); let r = b.module(None, &[]); b.finish(r)? };
assert!(bcgen_lang::compile(&hir, &intern_lang::Interner::new())?.result_global().is_none());
Source

pub fn function(&self, item: ItemId) -> Option<FuncId>

The function compiled for a HIR function item: its parameters are dyn (a by-reference parameter is a ref cell dyn holding an LSB reference), followed by an i64 presence mask when any parameter has a default; it returns dyn (a generator’s returns its coroutine). It carries the item’s LSB parameter list, so it can be called typed (call, Vm::run) or bound dynamically. Root-level functions are exported under their names.

§Examples

See Compiled.

Source

pub fn origin(&self, func: FuncId, pc: u32) -> Option<NodeRef>

The HIR node instruction pc of func was compiled from.

§Examples

See Compiled.

Source

pub fn origins(&self, func: FuncId) -> &[NodeRef]

The HIR node of every instruction of func, indexed by pc.

§Examples
let hir = { let mut b = hir_lang::Builder::new(); let r = b.module(None, &[]); b.finish(r)? };
let out = bcgen_lang::compile(&hir, &intern_lang::Interner::new())?;
assert_eq!(out.origins(out.entry()).len(), out.module().function(out.entry()).unwrap().code().len());
Source

pub fn class_type(&self, class: ItemId) -> Option<TypeId>

The LSB struct type of a class or interface item (mixins have none).

§Examples
use hir_lang::{Builder, ClassDef, Generics, Item, ItemKind, List, Name};
use intern_lang::Interner;

let mut names = Interner::new();
let mut b = Builder::new();
let class = ClassDef {
    generics: Generics::default(),
    bases: List::EMPTY,
    interfaces: List::EMPTY,
    fields: List::EMPTY,
    items: List::EMPTY,
    is_abstract: false,
    is_final: false,
    mixin: false,
};
let c = b.item(Item::new(Some(Name::new(names.intern("Point"))), ItemKind::Class(class)));
let root = b.module(None, &[c]);
let out = bcgen_lang::compile(&b.finish(root)?, &names)?;
let ty = out.class_type(c).unwrap();
assert!(matches!(out.module().types()[ty.index()], bytecode_lang::TypeDef::Struct(_)));
Source

pub fn method(&self, class: ItemId, method: ItemId) -> Option<FuncId>

The function a method item was compiled to for a class (a mixin’s method has one per class using it; an abstract method has none).

§Examples

See tests/classes.rs; a class without methods has none:

let hir = { let mut b = hir_lang::Builder::new(); let r = b.module(None, &[]); b.finish(r)? };
let out = bcgen_lang::compile(&hir, &intern_lang::Interner::new())?;
assert_eq!(out.method(hir.root(), hir.root()), None);
Source

pub fn destructor(&self) -> Option<&str>

The LSB method name of the destructor (Options::destructor, __destruct() in the PHP profile), when a class of the unit defines one. LSB format 2 has no destructor flag: the host passes this name to bvm-lang’s Vm::set_destructor on a VM made with the reference-counting memory profile, so destructors run where PHP runs them.

§Examples
let hir = { let mut b = hir_lang::Builder::new(); let r = b.module(None, &[]); b.finish(r)? };
let out = bcgen_lang::compile(&hir, &intern_lang::Interner::new())?;
assert_eq!(out.destructor(), None);

Trait Implementations§

Source§

impl Clone for Compiled

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 Compiled

Source§

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

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

impl Eq for Compiled

Source§

impl PartialEq for Compiled

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 Compiled

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.