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)
}
}

View File

@@ -9,7 +9,7 @@ use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::{self, binemit, ir};
use cranelift_module::{
Backend, DataContext, DataDescription, DataId, FuncId, Init, Linkage, ModuleError,
ModuleNamespace, ModuleResult,
ModuleContents, ModuleResult,
};
use object::write::{
Object, Relocation, SectionId, StandardSection, Symbol, SymbolId, SymbolSection,
@@ -218,7 +218,7 @@ impl Backend for ObjectBackend {
func_id: FuncId,
_name: &str,
ctx: &cranelift_codegen::Context,
_namespace: &ModuleNamespace<Self>,
_contents: &ModuleContents<Self>,
code_size: u32,
trap_sink: &mut TS,
) -> ModuleResult<ObjectCompiledFunction>
@@ -275,7 +275,7 @@ impl Backend for ObjectBackend {
func_id: FuncId,
_name: &str,
bytes: &[u8],
_namespace: &ModuleNamespace<Self>,
_contents: &ModuleContents<Self>,
) -> ModuleResult<ObjectCompiledFunction> {
let symbol = self.functions[func_id].unwrap();
@@ -307,7 +307,7 @@ impl Backend for ObjectBackend {
tls: bool,
align: Option<u8>,
data_ctx: &DataContext,
_namespace: &ModuleNamespace<Self>,
_contents: &ModuleContents<Self>,
) -> ModuleResult<ObjectCompiledData> {
let &DataDescription {
ref init,
@@ -428,7 +428,7 @@ impl Backend for ObjectBackend {
&mut self,
_id: FuncId,
_func: &ObjectCompiledFunction,
_namespace: &ModuleNamespace<Self>,
_contents: &ModuleContents<Self>,
) {
// Nothing to do.
}
@@ -441,7 +441,7 @@ impl Backend for ObjectBackend {
&mut self,
_id: DataId,
_data: &ObjectCompiledData,
_namespace: &ModuleNamespace<Self>,
_contents: &ModuleContents<Self>,
) {
// Nothing to do.
}
@@ -454,7 +454,7 @@ impl Backend for ObjectBackend {
// Nothing to do.
}
fn finish(mut self, namespace: &ModuleNamespace<Self>) -> ObjectProduct {
fn finish(mut self, contents: &ModuleContents<Self>) -> ObjectProduct {
let mut symbol_relocs = Vec::new();
mem::swap(&mut symbol_relocs, &mut self.relocs);
for symbol in symbol_relocs {
@@ -467,7 +467,7 @@ impl Backend for ObjectBackend {
addend,
} in &symbol.relocs
{
let target_symbol = self.get_symbol(namespace, name);
let target_symbol = self.get_symbol(contents, name);
self.object
.add_relocation(
symbol.section,
@@ -506,16 +506,16 @@ impl ObjectBackend {
// symbols for missing libcalls.
fn get_symbol(
&mut self,
namespace: &ModuleNamespace<Self>,
contents: &ModuleContents<Self>,
name: &ir::ExternalName,
) -> SymbolId {
match *name {
ir::ExternalName::User { .. } => {
if namespace.is_function(name) {
let id = namespace.get_function_id(name);
if contents.is_function(name) {
let id = contents.get_function_id(name);
self.functions[id].unwrap()
} else {
let id = namespace.get_data_id(name);
let id = contents.get_data_id(name);
self.data_objects[id].unwrap()
}
}

View File

@@ -8,7 +8,7 @@ use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::settings::Configurable;
use cranelift_codegen::{self, ir, settings};
use cranelift_module::{
Backend, DataContext, DataDescription, DataId, FuncId, Init, Linkage, ModuleNamespace,
Backend, DataContext, DataDescription, DataId, FuncId, Init, Linkage, ModuleContents,
ModuleResult,
};
use cranelift_native;
@@ -170,19 +170,19 @@ impl SimpleJITBackend {
fn get_definition(
&self,
namespace: &ModuleNamespace<Self>,
contents: &ModuleContents<Self>,
name: &ir::ExternalName,
) -> *const u8 {
match *name {
ir::ExternalName::User { .. } => {
if namespace.is_function(name) {
let (def, name_str, _signature) = namespace.get_function_definition(&name);
if contents.is_function(name) {
let (def, name_str, _signature) = contents.get_function_definition(&name);
match def {
Some(compiled) => compiled.code,
None => self.lookup_symbol(name_str),
}
} else {
let (def, name_str, _writable) = namespace.get_data_definition(&name);
let (def, name_str, _writable) = contents.get_data_definition(&name);
match def {
Some(compiled) => compiled.storage,
None => self.lookup_symbol(name_str),
@@ -281,7 +281,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
_id: FuncId,
name: &str,
ctx: &cranelift_codegen::Context,
_namespace: &ModuleNamespace<Self>,
_contents: &ModuleContents<Self>,
code_size: u32,
trap_sink: &mut TS,
) -> ModuleResult<Self::CompiledFunction>
@@ -321,7 +321,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
_id: FuncId,
name: &str,
bytes: &[u8],
_namespace: &ModuleNamespace<Self>,
_contents: &ModuleContents<Self>,
) -> ModuleResult<Self::CompiledFunction> {
let size = bytes.len();
let ptr = self
@@ -351,7 +351,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
tls: bool,
align: Option<u8>,
data: &DataContext,
_namespace: &ModuleNamespace<Self>,
_contents: &ModuleContents<Self>,
) -> ModuleResult<Self::CompiledData> {
assert!(!tls, "SimpleJIT doesn't yet support TLS");
@@ -443,7 +443,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
&mut self,
_id: FuncId,
func: &Self::CompiledFunction,
namespace: &ModuleNamespace<Self>,
contents: &ModuleContents<Self>,
) -> Self::FinalizedFunction {
use std::ptr::write_unaligned;
@@ -457,7 +457,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
let ptr = func.code;
debug_assert!((offset as usize) < func.size);
let at = unsafe { ptr.offset(offset as isize) };
let base = self.get_definition(namespace, name);
let base = self.get_definition(contents, name);
// TODO: Handle overflow.
let what = unsafe { base.offset(addend as isize) };
match reloc {
@@ -497,7 +497,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
&mut self,
_id: DataId,
data: &Self::CompiledData,
namespace: &ModuleNamespace<Self>,
contents: &ModuleContents<Self>,
) -> Self::FinalizedData {
use std::ptr::write_unaligned;
@@ -511,7 +511,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
let ptr = data.storage;
debug_assert!((offset as usize) < data.size);
let at = unsafe { ptr.offset(offset as isize) };
let base = self.get_definition(namespace, name);
let base = self.get_definition(contents, name);
// TODO: Handle overflow.
let what = unsafe { base.offset(addend as isize) };
match reloc {
@@ -555,7 +555,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
///
/// This method does not need to be called when access to the memory
/// handle is not required.
fn finish(self, _namespace: &ModuleNamespace<Self>) -> Self::Product {
fn finish(self, _contents: &ModuleContents<Self>) -> Self::Product {
self.memory
}
}