diff --git a/cranelift/module/src/backend.rs b/cranelift/module/src/backend.rs index ff1372208b..6602c4cbb7 100644 --- a/cranelift/module/src/backend.rs +++ b/cranelift/module/src/backend.rs @@ -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, + contents: &ModuleContents, code_size: u32, trap_sink: &mut TS, ) -> ModuleResult @@ -94,7 +94,7 @@ where id: FuncId, name: &str, bytes: &[u8], - namespace: &ModuleNamespace, + contents: &ModuleContents, ) -> ModuleResult; /// Define a zero-initialized data object of the given size. @@ -108,7 +108,7 @@ where tls: bool, align: Option, data_ctx: &DataContext, - namespace: &ModuleNamespace, + contents: &ModuleContents, ) -> ModuleResult; /// 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, + contents: &ModuleContents, ) -> 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, + contents: &ModuleContents, ) -> 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::Product; + fn finish(self, contents: &ModuleContents) -> Self::Product; } /// Default names for `ir::LibCall`s. A function by this name is imported into the object as diff --git a/cranelift/module/src/lib.rs b/cranelift/module/src/lib.rs index 25a2759be4..6331cfac1f 100644 --- a/cranelift/module/src/lib.rs +++ b/cranelift/module/src/lib.rs @@ -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; diff --git a/cranelift/module/src/module.rs b/cranelift/module/src/module.rs index 3a6f90fc88..f27cac2012 100644 --- a/cranelift/module/src/module.rs +++ b/cranelift/module/src/module.rs @@ -228,8 +228,9 @@ where } } -/// The functions and data objects belonging to a module. -struct ModuleContents +/// 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 where B: Backend, { @@ -241,7 +242,8 @@ impl ModuleContents 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 { - &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 { - &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, -} - -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:: { - 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:: { - 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:: { - 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:: { - 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:: { - 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:: { - contents: &self.contents, - }) + self.backend.finish(&self.contents) } } diff --git a/cranelift/object/src/backend.rs b/cranelift/object/src/backend.rs index 3cceda4d88..31ec135d24 100644 --- a/cranelift/object/src/backend.rs +++ b/cranelift/object/src/backend.rs @@ -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, + _contents: &ModuleContents, code_size: u32, trap_sink: &mut TS, ) -> ModuleResult @@ -275,7 +275,7 @@ impl Backend for ObjectBackend { func_id: FuncId, _name: &str, bytes: &[u8], - _namespace: &ModuleNamespace, + _contents: &ModuleContents, ) -> ModuleResult { let symbol = self.functions[func_id].unwrap(); @@ -307,7 +307,7 @@ impl Backend for ObjectBackend { tls: bool, align: Option, data_ctx: &DataContext, - _namespace: &ModuleNamespace, + _contents: &ModuleContents, ) -> ModuleResult { let &DataDescription { ref init, @@ -428,7 +428,7 @@ impl Backend for ObjectBackend { &mut self, _id: FuncId, _func: &ObjectCompiledFunction, - _namespace: &ModuleNamespace, + _contents: &ModuleContents, ) { // Nothing to do. } @@ -441,7 +441,7 @@ impl Backend for ObjectBackend { &mut self, _id: DataId, _data: &ObjectCompiledData, - _namespace: &ModuleNamespace, + _contents: &ModuleContents, ) { // Nothing to do. } @@ -454,7 +454,7 @@ impl Backend for ObjectBackend { // Nothing to do. } - fn finish(mut self, namespace: &ModuleNamespace) -> ObjectProduct { + fn finish(mut self, contents: &ModuleContents) -> 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, + contents: &ModuleContents, 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() } } diff --git a/cranelift/simplejit/src/backend.rs b/cranelift/simplejit/src/backend.rs index 6e21e7f154..c790a6bf01 100644 --- a/cranelift/simplejit/src/backend.rs +++ b/cranelift/simplejit/src/backend.rs @@ -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, + contents: &ModuleContents, 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, + _contents: &ModuleContents, code_size: u32, trap_sink: &mut TS, ) -> ModuleResult @@ -321,7 +321,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend { _id: FuncId, name: &str, bytes: &[u8], - _namespace: &ModuleNamespace, + _contents: &ModuleContents, ) -> ModuleResult { let size = bytes.len(); let ptr = self @@ -351,7 +351,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend { tls: bool, align: Option, data: &DataContext, - _namespace: &ModuleNamespace, + _contents: &ModuleContents, ) -> ModuleResult { 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, + contents: &ModuleContents, ) -> 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, + contents: &ModuleContents, ) -> 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::Product { + fn finish(self, _contents: &ModuleContents) -> Self::Product { self.memory } }