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::DataId;
use crate::FuncId; use crate::FuncId;
use crate::Linkage; use crate::Linkage;
use crate::ModuleNamespace; use crate::ModuleContents;
use crate::ModuleResult; use crate::ModuleResult;
use core::marker; use core::marker;
use cranelift_codegen::isa::TargetIsa; use cranelift_codegen::isa::TargetIsa;
@@ -79,7 +79,7 @@ where
id: FuncId, id: FuncId,
name: &str, name: &str,
ctx: &Context, ctx: &Context,
namespace: &ModuleNamespace<Self>, contents: &ModuleContents<Self>,
code_size: u32, code_size: u32,
trap_sink: &mut TS, trap_sink: &mut TS,
) -> ModuleResult<Self::CompiledFunction> ) -> ModuleResult<Self::CompiledFunction>
@@ -94,7 +94,7 @@ where
id: FuncId, id: FuncId,
name: &str, name: &str,
bytes: &[u8], bytes: &[u8],
namespace: &ModuleNamespace<Self>, contents: &ModuleContents<Self>,
) -> ModuleResult<Self::CompiledFunction>; ) -> ModuleResult<Self::CompiledFunction>;
/// Define a zero-initialized data object of the given size. /// Define a zero-initialized data object of the given size.
@@ -108,7 +108,7 @@ where
tls: bool, tls: bool,
align: Option<u8>, align: Option<u8>,
data_ctx: &DataContext, data_ctx: &DataContext,
namespace: &ModuleNamespace<Self>, contents: &ModuleContents<Self>,
) -> ModuleResult<Self::CompiledData>; ) -> ModuleResult<Self::CompiledData>;
/// Write the address of `what` into the data for `data` at `offset`. `data` must refer to a /// Write the address of `what` into the data for `data` at `offset`. `data` must refer to a
@@ -139,7 +139,7 @@ where
&mut self, &mut self,
id: FuncId, id: FuncId,
func: &Self::CompiledFunction, func: &Self::CompiledFunction,
namespace: &ModuleNamespace<Self>, contents: &ModuleContents<Self>,
) -> Self::FinalizedFunction; ) -> Self::FinalizedFunction;
/// Return the finalized artifact from the backend, if relevant. /// Return the finalized artifact from the backend, if relevant.
@@ -154,7 +154,7 @@ where
&mut self, &mut self,
id: DataId, id: DataId,
data: &Self::CompiledData, data: &Self::CompiledData,
namespace: &ModuleNamespace<Self>, contents: &ModuleContents<Self>,
) -> Self::FinalizedData; ) -> Self::FinalizedData;
/// Return the finalized artifact from the backend, if relevant. /// Return the finalized artifact from the backend, if relevant.
@@ -168,7 +168,7 @@ where
/// Consume this `Backend` and return a result. Some implementations may /// Consume this `Backend` and return a result. Some implementations may
/// provide additional functionality through this result. /// 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 /// 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::backend::{default_libcall_names, Backend};
pub use crate::data_context::{DataContext, DataDescription, Init}; pub use crate::data_context::{DataContext, DataDescription, Init};
pub use crate::module::{ pub use crate::module::{
DataId, FuncId, FuncOrDataId, Linkage, Module, ModuleError, ModuleFunction, ModuleNamespace, DataId, FuncId, FuncOrDataId, Linkage, Module, ModuleContents, ModuleError, ModuleFunction,
ModuleResult, ModuleResult,
}; };
pub use crate::traps::TrapSite; pub use crate::traps::TrapSite;

View File

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

View File

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

View File

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