Merge ModuleNamespace and ModuleContents

This commit is contained in:
bjorn3
2020-09-30 12:29:22 +02:00
parent a04001211c
commit b1187b5507
5 changed files with 52 additions and 97 deletions

View File

@@ -4,7 +4,7 @@ use crate::DataContext;
use crate::DataId;
use crate::FuncId;
use crate::Linkage;
use crate::ModuleNamespace;
use crate::ModuleContents;
use crate::ModuleResult;
use core::marker;
use cranelift_codegen::isa::TargetIsa;
@@ -79,7 +79,7 @@ where
id: FuncId,
name: &str,
ctx: &Context,
namespace: &ModuleNamespace<Self>,
contents: &ModuleContents<Self>,
code_size: u32,
trap_sink: &mut TS,
) -> ModuleResult<Self::CompiledFunction>
@@ -94,7 +94,7 @@ where
id: FuncId,
name: &str,
bytes: &[u8],
namespace: &ModuleNamespace<Self>,
contents: &ModuleContents<Self>,
) -> ModuleResult<Self::CompiledFunction>;
/// Define a zero-initialized data object of the given size.
@@ -108,7 +108,7 @@ where
tls: bool,
align: Option<u8>,
data_ctx: &DataContext,
namespace: &ModuleNamespace<Self>,
contents: &ModuleContents<Self>,
) -> ModuleResult<Self::CompiledData>;
/// Write the address of `what` into the data for `data` at `offset`. `data` must refer to a
@@ -139,7 +139,7 @@ where
&mut self,
id: FuncId,
func: &Self::CompiledFunction,
namespace: &ModuleNamespace<Self>,
contents: &ModuleContents<Self>,
) -> Self::FinalizedFunction;
/// Return the finalized artifact from the backend, if relevant.
@@ -154,7 +154,7 @@ where
&mut self,
id: DataId,
data: &Self::CompiledData,
namespace: &ModuleNamespace<Self>,
contents: &ModuleContents<Self>,
) -> Self::FinalizedData;
/// Return the finalized artifact from the backend, if relevant.
@@ -168,7 +168,7 @@ where
/// Consume this `Backend` and return a result. Some implementations may
/// provide additional functionality through this result.
fn finish(self, namespace: &ModuleNamespace<Self>) -> Self::Product;
fn finish(self, contents: &ModuleContents<Self>) -> Self::Product;
}
/// Default names for `ir::LibCall`s. A function by this name is imported into the object as

View File

@@ -40,7 +40,7 @@ mod traps;
pub use crate::backend::{default_libcall_names, Backend};
pub use crate::data_context::{DataContext, DataDescription, Init};
pub use crate::module::{
DataId, FuncId, FuncOrDataId, Linkage, Module, ModuleError, ModuleFunction, ModuleNamespace,
DataId, FuncId, FuncOrDataId, Linkage, Module, ModuleContents, ModuleError, ModuleFunction,
ModuleResult,
};
pub use crate::traps::TrapSite;

View File

@@ -228,8 +228,9 @@ where
}
}
/// The functions and data objects belonging to a module.
struct ModuleContents<B>
/// 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 ModuleContents<B>
where
B: Backend,
{
@@ -241,7 +242,8 @@ impl<B> ModuleContents<B>
where
B: Backend,
{
fn get_function_id(&self, name: &ir::ExternalName) -> FuncId {
/// Get the `FuncId` for the function named by `name`.
pub fn get_function_id(&self, name: &ir::ExternalName) -> FuncId {
if let ir::ExternalName::User { namespace, index } = *name {
debug_assert_eq!(namespace, 0);
FuncId::from_u32(index)
@@ -250,7 +252,8 @@ where
}
}
fn get_data_id(&self, name: &ir::ExternalName) -> DataId {
/// Get the `DataId` for the data object named by `name`.
pub fn get_data_id(&self, name: &ir::ExternalName) -> DataId {
if let ir::ExternalName::User { namespace, index } = *name {
debug_assert_eq!(namespace, 1);
DataId::from_u32(index)
@@ -259,47 +262,14 @@ where
}
}
fn get_function_info(&self, name: &ir::ExternalName) -> &ModuleFunction<B> {
&self.functions[self.get_function_id(name)]
}
/// Get the `DataDeclaration` for the function named by `name`.
fn get_data_info(&self, name: &ir::ExternalName) -> &ModuleData<B> {
&self.data_objects[self.get_data_id(name)]
}
}
/// 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 `FuncId` for the function named by `name`.
pub fn get_function_id(&self, name: &ir::ExternalName) -> FuncId {
self.contents.get_function_id(name)
}
/// Get the `DataId` for the data object named by `name`.
pub fn get_data_id(&self, name: &ir::ExternalName) -> DataId {
self.contents.get_data_id(name)
}
/// 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
&self.functions[self.get_function_id(name)].decl
}
/// Get the `DataDeclaration` for the data object named by `name`.
pub fn get_data_decl(&self, name: &ir::ExternalName) -> &DataDeclaration {
&self.contents.get_data_info(name).decl
&self.data_objects[self.get_data_id(name)].decl
}
/// Get the definition for the function named by `name`, along with its name
@@ -308,7 +278,7 @@ where
&self,
name: &ir::ExternalName,
) -> (Option<&B::CompiledFunction>, &str, &ir::Signature) {
let info = self.contents.get_function_info(name);
let info = &self.functions[self.get_function_id(name)];
debug_assert!(
!info.decl.linkage.is_definable() || info.compiled.is_some(),
"Finalization requires a definition for function {}.",
@@ -329,7 +299,7 @@ where
&self,
name: &ir::ExternalName,
) -> (Option<&B::CompiledData>, &str, bool) {
let info = self.contents.get_data_info(name);
let info = &self.data_objects[self.get_data_id(name)];
debug_assert!(
!info.decl.linkage.is_definable() || info.compiled.is_some(),
"Finalization requires a definition for data object {}.",
@@ -594,9 +564,7 @@ where
func,
&info.decl.name,
ctx,
&ModuleNamespace::<B> {
contents: &self.contents,
},
&self.contents,
total_size,
trap_sink,
)?;
@@ -632,14 +600,9 @@ where
_ => Err(ModuleError::FunctionTooLarge(info.decl.name.clone()))?,
};
let compiled = self.backend.define_function_bytes(
func,
&info.decl.name,
bytes,
&ModuleNamespace::<B> {
contents: &self.contents,
},
)?;
let compiled =
self.backend
.define_function_bytes(func, &info.decl.name, bytes, &self.contents)?;
self.contents.functions[func].compiled = Some(compiled);
self.functions_to_finalize.push(func);
@@ -663,9 +626,7 @@ where
info.decl.tls,
info.decl.align,
data_ctx,
&ModuleNamespace::<B> {
contents: &self.contents,
},
&self.contents,
)?)
};
self.contents.data_objects[data].compiled = compiled;
@@ -734,9 +695,7 @@ where
info.compiled
.as_ref()
.expect("function must be compiled before it can be finalized"),
&ModuleNamespace::<B> {
contents: &self.contents,
},
&self.contents,
);
}
for data in self.data_objects_to_finalize.drain(..) {
@@ -747,9 +706,7 @@ where
info.compiled
.as_ref()
.expect("data object must be compiled before it can be finalized"),
&ModuleNamespace::<B> {
contents: &self.contents,
},
&self.contents,
);
}
self.backend.publish();
@@ -792,8 +749,6 @@ where
/// implementations may provide additional functionality available after
/// a `Module` is complete.
pub fn finish(self) -> B::Product {
self.backend.finish(&ModuleNamespace::<B> {
contents: &self.contents,
})
self.backend.finish(&self.contents)
}
}