[WIP] Module API (#294)

* Initial skeleton.

* Add basic faerie support.

This adds enough functionality to enable simple .o file writing through
faerie. This included adding the functionality to Module to support
RelocSink implementations.

* Add basic SimpleJIT support.

This adds enough functionality to enable a simple program to be jitted
and executed.

* Make declare_func_in_func take a Function instead of a Context.

It only needs the Function, and sometimes it's useful to call it from
places that don't have a full Context.

* Temporarily disable local and exported global variables in the Faerie backend.

Faerie assumes these variables use pc-relative offset instructions, and
Cretonne is not yet emitting those instructions.

* FaerieBackend depends on PIC.

Faerie itself only supports PIC objects for now, so add an assert to
Cretonne to check that it's using a PIC target flag.

* SimpleJIT support for data objects.

* Preliminary faerie support for data objects.

* Support for data objects in faerie using the new colocated flag.

* Unit tests for DataContext and friends.

* Add a Module::consume() function.

This consumes the Module and returns the contained Backend, so that
users can call Backend-specific functions with it. For example, the
Faerie backend has functions to write an object file.

* Update the new crates to version 0.4.4.

* Make FaerieBackend own its TargetIsa.

This simplifies its interface, as it eliminates a lifetime parameter.
While we may eventually want to look into allowing multiple clients to
share a TargetIsa, it isn't worth the complexity for FaerieBackend
right now.

* Don't try to protect faerie from multiple declarations.

Let faerie decide for itself whether it wants to consider two
declarations to be compatible.

* Use debug_assert_eq rather than debug_assert with ==.

* Fix FaerieRelocSink's reloc_external to handle data object names.

* Relax the asserts in get_function_definition and get_data_definition.

These functions don't require definable symbols, but they do require
that definable symbols be defined. This is needed for the simplejit
backend.

* Add a function to the faerie backend to retrieve the artifact name.

* Sync up with cretonne changes.
This commit is contained in:
Dan Gohman
2018-04-17 10:52:36 -07:00
committed by GitHub
parent a9edb28414
commit 76db9f022d
21 changed files with 1793 additions and 7 deletions

97
lib/module/src/backend.rs Normal file
View File

@@ -0,0 +1,97 @@
//! Defines the `Backend` trait.
use DataContext;
use Linkage;
use ModuleNamespace;
use cretonne_codegen::Context;
use cretonne_codegen::isa::TargetIsa;
use cretonne_codegen::result::CtonError;
use cretonne_codegen::{binemit, ir};
use std::marker;
/// A `Backend` implements the functionality needed to support a `Module`.
pub trait Backend
where
Self: marker::Sized,
{
/// The results of compiling a function.
type CompiledFunction;
/// The results of "compiling" a data object.
type CompiledData;
/// The completed output artifact for a function, if this is meaningful for
/// the Backend.
type FinalizedFunction;
/// The completed output artifact for a data object, if this is meaningful for
/// the Backend.
type FinalizedData;
/// Return the `TargetIsa` to compile for.
fn isa(&self) -> &TargetIsa;
/// Declare a function.
fn declare_function(&mut self, name: &str, linkage: Linkage);
/// Declare a data object.
fn declare_data(&mut self, name: &str, linkage: Linkage, writable: bool);
/// Define a function, producing the function body from the given `Context`.
///
/// Functions must be declared before being defined.
fn define_function(
&mut self,
name: &str,
ctx: &Context,
namespace: &ModuleNamespace<Self>,
code_size: u32,
) -> Result<Self::CompiledFunction, CtonError>;
/// Define a zero-initialized data object of the given size.
///
/// Data objects must be declared before being defined.
///
/// TODO: Is CtonError the right error code here?
fn define_data(
&mut self,
name: &str,
data_ctx: &DataContext,
namespace: &ModuleNamespace<Self>,
) -> Result<Self::CompiledData, CtonError>;
/// Write the address of `what` into the data for `data` at `offset`. `data` must refer to a
/// defined data object.
fn write_data_funcaddr(
&mut self,
data: &mut Self::CompiledData,
offset: usize,
what: ir::FuncRef,
);
/// Write the address of `what` plus `addend` into the data for `data` at `offset`. `data` must
/// refer to a defined data object.
fn write_data_dataaddr(
&mut self,
data: &mut Self::CompiledData,
offset: usize,
what: ir::GlobalVar,
addend: binemit::Addend,
);
/// Perform all outstanding relocations on the given function. This requires all `Local`
/// and `Export` entities referenced to be defined.
fn finalize_function(
&mut self,
func: &Self::CompiledFunction,
namespace: &ModuleNamespace<Self>,
) -> Self::FinalizedFunction;
/// Perform all outstanding relocations on the given data object. This requires all
/// `Local` and `Export` entities referenced to be defined.
fn finalize_data(
&mut self,
data: &Self::CompiledData,
namespace: &ModuleNamespace<Self>,
) -> Self::FinalizedData;
}

View File

@@ -0,0 +1,205 @@
//! Defines `DataContext`.
use cretonne_codegen::entity::PrimaryMap;
use cretonne_codegen::binemit::{CodeOffset, Addend};
use cretonne_codegen::ir;
/// This specifies how data is to be initialized.
#[derive(PartialEq, Eq, Debug)]
pub enum Init {
/// This indicates that no initialization has been specified yet.
Uninitialized,
/// Initialize the data with all zeros.
Zeros {
/// The size of the data.
size: usize,
},
/// Initialize the data with the specified contents.
Bytes {
/// The contents, which also implies the size of the data.
contents: Box<[u8]>,
},
}
impl Init {
/// Return the size of the data to be initialized.
pub fn size(&self) -> usize {
match *self {
Init::Uninitialized => panic!("data size not initialized yet"),
Init::Zeros { size } => size,
Init::Bytes { ref contents } => contents.len(),
}
}
}
/// A flag specifying whether data is readonly or may be written to.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Writability {
/// Data is readonly, meaning writes to it will trap.
Readonly,
/// Data is writable.
Writable,
}
/// A description of a data object.
pub struct DataDescription {
/// Whether the data readonly or writable.
pub writable: Writability,
/// How the data should be initialized.
pub init: Init,
/// External function declarations.
pub function_decls: PrimaryMap<ir::FuncRef, ir::ExternalName>,
/// External data object declarations.
pub data_decls: PrimaryMap<ir::GlobalVar, ir::ExternalName>,
/// Function addresses to write at specified offsets.
pub function_relocs: Vec<(CodeOffset, ir::FuncRef)>,
/// Data addresses to write at specified offsets.
pub data_relocs: Vec<(CodeOffset, ir::GlobalVar, Addend)>,
}
/// This is to data objects what cretonne_codegen::Context is to functions.
pub struct DataContext {
description: DataDescription,
}
impl DataContext {
/// Allocate a new context.
pub fn new() -> Self {
Self {
description: DataDescription {
writable: Writability::Readonly,
init: Init::Uninitialized,
function_decls: PrimaryMap::new(),
data_decls: PrimaryMap::new(),
function_relocs: Vec::new(),
data_relocs: Vec::new(),
},
}
}
/// Clear all data structures in this context.
pub fn clear(&mut self) {
self.description.writable = Writability::Readonly;
self.description.init = Init::Uninitialized;
self.description.function_decls.clear();
self.description.data_decls.clear();
self.description.function_relocs.clear();
self.description.data_relocs.clear();
}
/// Define a zero-initialized object with the given size.
pub fn define_zeroinit(&mut self, size: usize, writable: Writability) {
debug_assert_eq!(self.description.init, Init::Uninitialized);
self.description.writable = writable;
self.description.init = Init::Zeros { size };
}
/// Define a zero-initialized object with the given size.
///
/// TODO: Can we avoid a Box here?
pub fn define(&mut self, contents: Box<[u8]>, writable: Writability) {
debug_assert_eq!(self.description.init, Init::Uninitialized);
self.description.writable = writable;
self.description.init = Init::Bytes { contents };
}
/// Declare an external function import.
pub fn import_function(&mut self, name: ir::ExternalName) -> ir::FuncRef {
self.description.function_decls.push(name)
}
/// Declares a global variable import.
///
/// TODO: Rename to import_data?
pub fn import_global_var(&mut self, name: ir::ExternalName) -> ir::GlobalVar {
self.description.data_decls.push(name)
}
/// Write the address of `func` into the data at offset `offset`.
pub fn write_function_addr(&mut self, offset: CodeOffset, func: ir::FuncRef) {
self.description.function_relocs.push((offset, func))
}
/// Write the address of `data` into the data at offset `offset`.
pub fn write_data_addr(&mut self, offset: CodeOffset, data: ir::GlobalVar, addend: Addend) {
self.description.data_relocs.push((offset, data, addend))
}
/// Reference the initializer data.
pub fn description(&self) -> &DataDescription {
debug_assert!(
self.description.init != Init::Uninitialized,
"data must be initialized first"
);
&self.description
}
}
#[cfg(test)]
mod tests {
use {DataContext, Writability, Init};
use cretonne_codegen::ir;
#[test]
fn basic_data_context() {
let mut data_ctx = DataContext::new();
{
let description = data_ctx.description();
assert_eq!(description.writable, Writability::Readonly);
assert_eq!(description.init, Init::Uninitialized);
assert!(description.function_decls.is_empty());
assert!(description.data_decls.is_empty());
assert!(description.function_relocs.is_empty());
assert!(description.data_relocs.is_empty());
}
data_ctx.define_zeroinit(256, Writability::Writable);
let _func_a = data_ctx.import_function(ir::ExternalName::user(0, 0));
let func_b = data_ctx.import_function(ir::ExternalName::user(0, 1));
let func_c = data_ctx.import_function(ir::ExternalName::user(1, 0));
let _data_a = data_ctx.import_global_var(ir::ExternalName::user(2, 2));
let data_b = data_ctx.import_global_var(ir::ExternalName::user(2, 3));
data_ctx.write_function_addr(8, func_b);
data_ctx.write_function_addr(16, func_c);
data_ctx.write_data_addr(32, data_b, 27);
{
let description = data_ctx.description();
assert_eq!(description.writable, Writability::Writable);
assert_eq!(description.init, Init::Zeros { size: 256 });
assert_eq!(description.function_decls.len(), 3);
assert_eq!(description.data_decls.len(), 2);
assert_eq!(description.function_relocs.len(), 2);
assert_eq!(description.data_relocs.len(), 1);
}
data_ctx.clear();
{
let description = data_ctx.description();
assert_eq!(description.writable, Writability::Readonly);
assert_eq!(description.init, Init::Uninitialized);
assert!(description.function_decls.is_empty());
assert!(description.data_decls.is_empty());
assert!(description.function_relocs.is_empty());
assert!(description.data_relocs.is_empty());
}
let contents = vec![33, 34, 35, 36];
let contents_clone = contents.clone();
data_ctx.define(contents.into_boxed_slice(), Writability::Readonly);
{
let description = data_ctx.description();
assert_eq!(description.writable, Writability::Readonly);
assert_eq!(
description.init,
Init::Bytes { contents: contents_clone.into_boxed_slice() }
);
assert_eq!(description.function_decls.len(), 0);
assert_eq!(description.data_decls.len(), 0);
assert_eq!(description.function_relocs.len(), 0);
assert_eq!(description.data_relocs.len(), 0);
}
}
}

15
lib/module/src/lib.rs Normal file
View File

@@ -0,0 +1,15 @@
//! Top-level lib.rs for `cretonne_module`.
#![deny(missing_docs, trivial_numeric_casts, unused_extern_crates)]
extern crate cretonne_codegen;
#[macro_use]
extern crate cretonne_entity;
mod backend;
mod data_context;
mod module;
pub use backend::Backend;
pub use data_context::{DataContext, Writability, DataDescription, Init};
pub use module::{DataId, FuncId, Linkage, Module, ModuleNamespace};

541
lib/module/src/module.rs Normal file
View File

@@ -0,0 +1,541 @@
//! Defines `Module` and related types.
// TODO: Should `ir::Function` really have a `name`?
// TODO: Factor out `ir::Function`'s `ext_funcs` and `global_vars` into a struct
// shared with `DataContext`?
use Backend;
use cretonne_codegen::entity::{EntityRef, PrimaryMap};
use cretonne_codegen::result::{CtonError, CtonResult};
use cretonne_codegen::{binemit, ir, Context};
use data_context::DataContext;
use std::collections::HashMap;
/// A function identifier for use in the `Module` interface.
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct FuncId(u32);
entity_impl!(FuncId, "funcid");
/// A data object identifier for use in the `Module` interface.
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct DataId(u32);
entity_impl!(DataId, "dataid");
/// Linkage refers to where an entity is defined and who can see it.
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum Linkage {
/// Defined outside of a module.
Import,
/// Defined inside the module, but not visible outside it.
Local,
/// Defined inside the module, visible outside it, and may be preempted.
Preemptible,
/// Defined inside the module, and visible outside it.
Export,
}
impl Linkage {
fn merge(a: Linkage, b: Linkage) -> Linkage {
match a {
Linkage::Export => Linkage::Export,
Linkage::Preemptible => {
match b {
Linkage::Export => Linkage::Export,
_ => Linkage::Preemptible,
}
}
Linkage::Local => {
match b {
Linkage::Export => Linkage::Export,
Linkage::Preemptible => Linkage::Preemptible,
_ => Linkage::Local,
}
}
Linkage::Import => b,
}
}
/// Test whether this linkage can have a definition.
pub fn is_definable(&self) -> bool {
match *self {
Linkage::Import => false,
Linkage::Local | Linkage::Preemptible | Linkage::Export => true,
}
}
/// Test whether this linkage will have a definition that cannot be preempted.
pub fn is_final(&self) -> bool {
match *self {
Linkage::Import | Linkage::Preemptible => false,
Linkage::Local | Linkage::Export => true,
}
}
}
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
enum FuncOrDataId {
Func(FuncId),
Data(DataId),
}
pub struct FunctionDeclaration {
pub name: String,
pub linkage: Linkage,
pub signature: ir::Signature,
}
struct ModuleFunction<B>
where
B: Backend,
{
decl: FunctionDeclaration,
compiled: Option<B::CompiledFunction>,
finalized: bool,
}
impl<B> ModuleFunction<B>
where
B: Backend,
{
fn merge(&mut self, linkage: Linkage) {
self.decl.linkage = Linkage::merge(self.decl.linkage, linkage);
}
}
pub struct DataDeclaration {
pub name: String,
pub linkage: Linkage,
pub writable: bool,
}
struct ModuleData<B>
where
B: Backend,
{
decl: DataDeclaration,
compiled: Option<B::CompiledData>,
finalized: bool,
}
impl<B> ModuleData<B>
where
B: Backend,
{
fn merge(&mut self, linkage: Linkage, writable: bool) {
self.decl.linkage = Linkage::merge(self.decl.linkage, linkage);
self.decl.writable = self.decl.writable || writable;
}
}
struct ModuleContents<B>
where
B: Backend,
{
functions: PrimaryMap<FuncId, ModuleFunction<B>>,
data_objects: PrimaryMap<DataId, ModuleData<B>>,
}
impl<B> ModuleContents<B>
where
B: Backend,
{
fn get_function_info(&self, name: &ir::ExternalName) -> &ModuleFunction<B> {
if let ir::ExternalName::User { namespace, index } = *name {
debug_assert_eq!(namespace, 0);
let func = FuncId::new(index as usize);
&self.functions[func]
} else {
panic!("unexpected ExternalName kind")
}
}
/// Get the `DataDeclaration` for the function named by `name`.
fn get_data_info(&self, name: &ir::ExternalName) -> &ModuleData<B> {
if let ir::ExternalName::User { namespace, index } = *name {
debug_assert_eq!(namespace, 1);
let data = DataId::new(index as usize);
&self.data_objects[data]
} else {
panic!("unexpected ExternalName kind")
}
}
}
/// This provides a view to the state of a module which allows `ir::ExternalName`s to be translated
/// into `FunctionDeclaration`s and `DataDeclaration`s.
pub struct ModuleNamespace<'a, B: 'a>
where
B: Backend,
{
contents: &'a ModuleContents<B>,
}
impl<'a, B> ModuleNamespace<'a, B>
where
B: Backend,
{
/// Get the `FunctionDeclaration` for the function named by `name`.
pub fn get_function_decl(&self, name: &ir::ExternalName) -> &FunctionDeclaration {
&self.contents.get_function_info(name).decl
}
/// Get the `DataDeclaration` for the function named by `name`.
pub fn get_data_decl(&self, name: &ir::ExternalName) -> &DataDeclaration {
&self.contents.get_data_info(name).decl
}
/// Get the definition for the function named by `name`, along with its name
/// and signature.
pub fn get_function_definition(
&self,
name: &ir::ExternalName,
) -> (Option<&B::CompiledFunction>, &str, &ir::Signature) {
let info = self.contents.get_function_info(name);
debug_assert_eq!(info.decl.linkage.is_definable(), info.compiled.is_some());
(
info.compiled.as_ref(),
&info.decl.name,
&info.decl.signature,
)
}
/// Get the definition for the data object named by `name`, along with its name
/// and writable flag
pub fn get_data_definition(
&self,
name: &ir::ExternalName,
) -> (Option<&B::CompiledData>, &str, bool) {
let info = self.contents.get_data_info(name);
debug_assert_eq!(info.decl.linkage.is_definable(), info.compiled.is_some());
(info.compiled.as_ref(), &info.decl.name, info.decl.writable)
}
/// Return whether `name` names a function, rather than a data object.
pub fn is_function(&self, name: &ir::ExternalName) -> bool {
if let ir::ExternalName::User { namespace, .. } = *name {
namespace == 0
} else {
panic!("unexpected ExternalName kind")
}
}
}
/// A `Module` is a utility for collecting functions and data objects, and linking them together.
pub struct Module<B>
where
B: Backend,
{
names: HashMap<String, FuncOrDataId>,
contents: ModuleContents<B>,
backend: B,
}
impl<B> Module<B>
where
B: Backend,
{
/// Create a new `Module`.
pub fn new(backend: B) -> Self {
Self {
names: HashMap::new(),
contents: ModuleContents {
functions: PrimaryMap::new(),
data_objects: PrimaryMap::new(),
},
backend,
}
}
/// Return then pointer type for the current target.
pub fn pointer_type(&self) -> ir::types::Type {
if self.backend.isa().flags().is_64bit() {
ir::types::I64
} else {
ir::types::I32
}
}
/// Declare a function in this module.
pub fn declare_function(
&mut self,
name: &str,
linkage: Linkage,
signature: &ir::Signature,
) -> Result<FuncId, CtonError> {
// TODO: Can we avoid allocating names so often?
use std::collections::hash_map::Entry::*;
match self.names.entry(name.to_owned()) {
Occupied(entry) => {
match *entry.get() {
FuncOrDataId::Func(id) => {
let existing = &mut self.contents.functions[id];
existing.merge(linkage);
self.backend.declare_function(name, existing.decl.linkage);
Ok(id)
}
FuncOrDataId::Data(..) => unimplemented!(),
}
}
Vacant(entry) => {
let id = self.contents.functions.push(ModuleFunction {
decl: FunctionDeclaration {
name: name.to_owned(),
linkage,
signature: signature.clone(),
},
compiled: None,
finalized: false,
});
entry.insert(FuncOrDataId::Func(id));
self.backend.declare_function(name, linkage);
Ok(id)
}
}
}
/// Declare a data object in this module.
pub fn declare_data(
&mut self,
name: &str,
linkage: Linkage,
writable: bool,
) -> Result<DataId, CtonError> {
// TODO: Can we avoid allocating names so often?
use std::collections::hash_map::Entry::*;
match self.names.entry(name.to_owned()) {
Occupied(entry) => {
match *entry.get() {
FuncOrDataId::Data(id) => {
let existing = &mut self.contents.data_objects[id];
existing.merge(linkage, writable);
self.backend.declare_data(
name,
existing.decl.linkage,
existing.decl.writable,
);
Ok(id)
}
FuncOrDataId::Func(..) => unimplemented!(),
}
}
Vacant(entry) => {
let id = self.contents.data_objects.push(ModuleData {
decl: DataDeclaration {
name: name.to_owned(),
linkage,
writable,
},
compiled: None,
finalized: false,
});
entry.insert(FuncOrDataId::Data(id));
self.backend.declare_data(name, linkage, writable);
Ok(id)
}
}
}
/// Use this when you're building the IR of a function to reference a function.
///
/// TODO: Coalesce redundant decls and signatures.
/// TODO: Look into ways to reduce the risk of using a FuncRef in the wrong function.
pub fn declare_func_in_func(&self, func: FuncId, in_func: &mut ir::Function) -> ir::FuncRef {
let decl = &self.contents.functions[func].decl;
let signature = in_func.import_signature(decl.signature.clone());
let colocated = decl.linkage.is_final();
in_func.import_function(ir::ExtFuncData {
name: ir::ExternalName::user(0, func.index() as u32),
signature,
colocated,
})
}
/// Use this when you're building the IR of a function to reference a data object.
///
/// TODO: Same as above.
pub fn declare_data_in_func(&self, data: DataId, func: &mut ir::Function) -> ir::GlobalVar {
let decl = &self.contents.data_objects[data].decl;
let colocated = decl.linkage.is_final();
func.create_global_var(ir::GlobalVarData::Sym {
name: ir::ExternalName::user(1, data.index() as u32),
colocated,
})
}
/// TODO: Same as above.
pub fn declare_func_in_data(&self, func: FuncId, ctx: &mut DataContext) -> ir::FuncRef {
ctx.import_function(ir::ExternalName::user(0, func.index() as u32))
}
/// TODO: Same as above.
pub fn declare_data_in_data(&self, data: DataId, ctx: &mut DataContext) -> ir::GlobalVar {
ctx.import_global_var(ir::ExternalName::user(1, data.index() as u32))
}
/// Define a function, producing the function body from the given `Context`.
pub fn define_function(&mut self, func: FuncId, ctx: &mut Context) -> CtonResult {
let compiled = {
let code_size = ctx.compile(self.backend.isa())?;
let info = &self.contents.functions[func];
debug_assert!(
info.compiled.is_none(),
"functions can be defined only once"
);
debug_assert!(
info.decl.linkage.is_definable(),
"imported functions cannot be defined"
);
Some(self.backend.define_function(
&info.decl.name,
ctx,
&ModuleNamespace::<B> {
contents: &self.contents,
},
code_size,
)?)
};
self.contents.functions[func].compiled = compiled;
Ok(())
}
/// Define a function, producing the data contents from the given `DataContext`.
pub fn define_data(&mut self, data: DataId, data_ctx: &DataContext) -> CtonResult {
let compiled = {
let info = &self.contents.data_objects[data];
debug_assert!(
info.compiled.is_none(),
"functions can be defined only once"
);
debug_assert!(
info.decl.linkage.is_definable(),
"imported functions cannot be defined"
);
Some(self.backend.define_data(
&info.decl.name,
data_ctx,
&ModuleNamespace::<B> {
contents: &self.contents,
},
)?)
};
self.contents.data_objects[data].compiled = compiled;
Ok(())
}
/// Write the address of `what` into the data for `data` at `offset`. `data` must refer to a
/// defined data object.
pub fn write_data_funcaddr(&mut self, data: DataId, offset: usize, what: ir::FuncRef) {
let info = &mut self.contents.data_objects[data];
debug_assert!(
info.decl.linkage.is_definable(),
"imported data cannot contain references"
);
self.backend.write_data_funcaddr(
&mut info.compiled.as_mut().expect(
"`data` must refer to a defined data object",
),
offset,
what,
);
}
/// Write the address of `what` plus `addend` into the data for `data` at `offset`. `data` must
/// refer to a defined data object.
pub fn write_data_dataaddr(
&mut self,
data: DataId,
offset: usize,
what: ir::GlobalVar,
addend: binemit::Addend,
) {
let info = &mut self.contents.data_objects[data];
debug_assert!(
info.decl.linkage.is_definable(),
"imported data cannot contain references"
);
self.backend.write_data_dataaddr(
&mut info.compiled.as_mut().expect(
"`data` must refer to a defined data object",
),
offset,
what,
addend,
);
}
/// Perform all outstanding relocations on the given function. This requires all `Local`
/// and `Export` entities referenced to be defined.
pub fn finalize_function(&mut self, func: FuncId) -> B::FinalizedFunction {
let output = {
let info = &self.contents.functions[func];
debug_assert!(
info.decl.linkage.is_definable(),
"imported data cannot be finalized"
);
self.backend.finalize_function(
info.compiled.as_ref().expect(
"function must be compiled before it can be finalized",
),
&ModuleNamespace::<B> { contents: &self.contents },
)
};
self.contents.functions[func].finalized = true;
output
}
/// Perform all outstanding relocations on the given data object. This requires all
/// `Local` and `Export` entities referenced to be defined.
pub fn finalize_data(&mut self, data: DataId) -> B::FinalizedData {
let output = {
let info = &self.contents.data_objects[data];
debug_assert!(
info.decl.linkage.is_definable(),
"imported data cannot be finalized"
);
self.backend.finalize_data(
info.compiled.as_ref().expect(
"data object must be compiled before it can be finalized",
),
&ModuleNamespace::<B> { contents: &self.contents },
)
};
self.contents.data_objects[data].finalized = true;
output
}
/// Finalize all functions and data objects. Note that this doesn't return the
/// final artifacts returned from `finalize_function` or `finalize_data`.
pub fn finalize_all(&mut self) {
// TODO: Could we use something like `into_iter()` here?
for info in self.contents.functions.values() {
if info.decl.linkage.is_definable() && !info.finalized {
self.backend.finalize_function(
info.compiled.as_ref().expect(
"function must be compiled before it can be finalized",
),
&ModuleNamespace::<B> { contents: &self.contents },
);
}
}
for info in self.contents.data_objects.values() {
if info.decl.linkage.is_definable() && !info.finalized {
self.backend.finalize_data(
info.compiled.as_ref().expect(
"data object must be compiled before it can be finalized",
),
&ModuleNamespace::<B> { contents: &self.contents },
);
}
}
}
/// Consume the module and return its contained `Backend`. Some `Backend`
/// implementations have additional features not available through the
/// `Module` interface.
pub fn consume(self) -> B {
self.backend
}
}