Store Compiled* in the backend instead of Module

This commit is contained in:
bjorn3
2020-09-30 16:18:57 +02:00
parent 405b9e2875
commit 588a4be0b3
6 changed files with 218 additions and 326 deletions

View File

@@ -1,19 +1,19 @@
//! Defines the `Backend` trait. //! Defines the `Backend` trait.
use crate::{DataContext, FuncOrDataId};
use crate::DataId; use crate::DataId;
use crate::FuncId; use crate::FuncId;
use crate::Linkage; use crate::Linkage;
use crate::ModuleContents; use crate::ModuleDeclarations;
use crate::ModuleResult; use crate::ModuleResult;
use crate::{DataContext, FuncOrDataId};
use core::marker; use core::marker;
use cranelift_codegen::isa::TargetIsa; use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::Context; use cranelift_codegen::Context;
use cranelift_codegen::{binemit, ir}; use cranelift_codegen::{binemit, ir};
use std::{borrow::ToOwned, collections::HashMap};
use std::boxed::Box; use std::boxed::Box;
use std::string::String; use std::string::String;
use std::{borrow::ToOwned, collections::HashMap};
/// A `Backend` implements the functionality needed to support a `Module`. /// A `Backend` implements the functionality needed to support a `Module`.
/// ///
@@ -32,12 +32,6 @@ where
/// A builder for constructing `Backend` instances. /// A builder for constructing `Backend` instances.
type Builder; type Builder;
/// The results of compiling a function.
type CompiledFunction;
/// The results of "compiling" a data object.
type CompiledData;
/// This is an object returned by `Module`'s /// This is an object returned by `Module`'s
/// [`finish`](struct.Module.html#method.finish) function, /// [`finish`](struct.Module.html#method.finish) function,
/// if the `Backend` has a purpose for this. /// if the `Backend` has a purpose for this.
@@ -71,10 +65,10 @@ where
id: FuncId, id: FuncId,
name: &str, name: &str,
ctx: &Context, ctx: &Context,
contents: &ModuleContents<Self>, declarations: &ModuleDeclarations,
code_size: u32, code_size: u32,
trap_sink: &mut TS, trap_sink: &mut TS,
) -> ModuleResult<Self::CompiledFunction> ) -> ModuleResult<()>
where where
TS: binemit::TrapSink; TS: binemit::TrapSink;
@@ -86,8 +80,8 @@ where
id: FuncId, id: FuncId,
name: &str, name: &str,
bytes: &[u8], bytes: &[u8],
contents: &ModuleContents<Self>, declarations: &ModuleDeclarations,
) -> ModuleResult<Self::CompiledFunction>; ) -> ModuleResult<()>;
/// Define a zero-initialized data object of the given size. /// Define a zero-initialized data object of the given size.
/// ///
@@ -100,15 +94,15 @@ where
tls: bool, tls: bool,
align: Option<u8>, align: Option<u8>,
data_ctx: &DataContext, data_ctx: &DataContext,
contents: &ModuleContents<Self>, declarations: &ModuleDeclarations,
) -> ModuleResult<Self::CompiledData>; ) -> ModuleResult<()>;
/// 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( fn finish(
self, self,
names: HashMap<String, FuncOrDataId>, names: HashMap<String, FuncOrDataId>,
contents: ModuleContents<Self>, declarations: ModuleDeclarations,
) -> Self::Product; ) -> Self::Product;
} }

View File

@@ -40,8 +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, ModuleContents, ModuleError, ModuleFunction, DataId, FuncId, FuncOrDataId, Linkage, Module, ModuleDeclarations, ModuleError, ModuleResult,
ModuleResult,
}; };
pub use crate::traps::TrapSite; pub use crate::traps::TrapSite;

View File

@@ -15,7 +15,6 @@ use log::info;
use std::borrow::ToOwned; use std::borrow::ToOwned;
use std::convert::TryInto; use std::convert::TryInto;
use std::string::String; use std::string::String;
use std::vec::Vec;
use thiserror::Error; use thiserror::Error;
/// A function identifier for use in the `Module` interface. /// A function identifier for use in the `Module` interface.
@@ -132,6 +131,20 @@ pub struct FunctionDeclaration {
pub signature: ir::Signature, pub signature: ir::Signature,
} }
impl FunctionDeclaration {
fn merge(&mut self, linkage: Linkage, sig: &ir::Signature) -> Result<(), ModuleError> {
self.linkage = Linkage::merge(self.linkage, linkage);
if &self.signature != sig {
return Err(ModuleError::IncompatibleSignature(
self.name.clone(),
self.signature.clone(),
sig.clone(),
));
}
Ok(())
}
}
/// Error messages for all `Module` and `Backend` methods /// Error messages for all `Module` and `Backend` methods
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum ModuleError { pub enum ModuleError {
@@ -165,44 +178,6 @@ pub enum ModuleError {
/// A convenient alias for a `Result` that uses `ModuleError` as the error type. /// A convenient alias for a `Result` that uses `ModuleError` as the error type.
pub type ModuleResult<T> = Result<T, ModuleError>; pub type ModuleResult<T> = Result<T, ModuleError>;
/// A function belonging to a `Module`.
pub struct ModuleFunction<B>
where
B: Backend,
{
/// The function declaration.
pub decl: FunctionDeclaration,
/// The compiled artifact, once it's available.
pub compiled: Option<B::CompiledFunction>,
}
impl<B> ModuleFunction<B>
where
B: Backend,
{
fn merge(&mut self, linkage: Linkage, sig: &ir::Signature) -> Result<(), ModuleError> {
self.decl.linkage = Linkage::merge(self.decl.linkage, linkage);
if &self.decl.signature != sig {
return Err(ModuleError::IncompatibleSignature(
self.decl.name.clone(),
self.decl.signature.clone(),
sig.clone(),
));
}
Ok(())
}
fn validate_for_define(&self) -> ModuleResult<()> {
if self.compiled.is_some() {
return Err(ModuleError::DuplicateDefinition(self.decl.name.clone()));
}
if !self.decl.linkage.is_definable() {
return Err(ModuleError::InvalidImportDefinition(self.decl.name.clone()));
}
Ok(())
}
}
/// Information about a data object which can be accessed. /// Information about a data object which can be accessed.
pub struct DataDeclaration { pub struct DataDeclaration {
pub name: String, pub name: String,
@@ -212,56 +187,26 @@ pub struct DataDeclaration {
pub align: Option<u8>, pub align: Option<u8>,
} }
/// A data object belonging to a `Module`. impl DataDeclaration {
pub struct ModuleData<B>
where
B: Backend,
{
/// The data object declaration.
pub decl: DataDeclaration,
/// The "compiled" artifact, once it's available.
pub compiled: Option<B::CompiledData>,
}
impl<B> ModuleData<B>
where
B: Backend,
{
fn merge(&mut self, linkage: Linkage, writable: bool, tls: bool, align: Option<u8>) { fn merge(&mut self, linkage: Linkage, writable: bool, tls: bool, align: Option<u8>) {
self.decl.linkage = Linkage::merge(self.decl.linkage, linkage); self.linkage = Linkage::merge(self.linkage, linkage);
self.decl.writable = self.decl.writable || writable; self.writable = self.writable || writable;
self.decl.align = self.decl.align.max(align); self.align = self.align.max(align);
assert_eq!( assert_eq!(
self.decl.tls, tls, self.tls, tls,
"Can't change TLS data object to normal or in the opposite way", "Can't change TLS data object to normal or in the opposite way",
); );
} }
fn validate_for_define(&self) -> ModuleResult<()> {
if self.compiled.is_some() {
return Err(ModuleError::DuplicateDefinition(self.decl.name.clone()));
}
if !self.decl.linkage.is_definable() {
return Err(ModuleError::InvalidImportDefinition(self.decl.name.clone()));
}
Ok(())
}
} }
/// This provides a view to the state of a module which allows `ir::ExternalName`s to be translated /// This provides a view to the state of a module which allows `ir::ExternalName`s to be translated
/// into `FunctionDeclaration`s and `DataDeclaration`s. /// into `FunctionDeclaration`s and `DataDeclaration`s.
pub struct ModuleContents<B> pub struct ModuleDeclarations {
where functions: PrimaryMap<FuncId, FunctionDeclaration>,
B: Backend, data_objects: PrimaryMap<DataId, DataDeclaration>,
{
functions: PrimaryMap<FuncId, ModuleFunction<B>>,
data_objects: PrimaryMap<DataId, ModuleData<B>>,
} }
impl<B> ModuleContents<B> impl ModuleDeclarations {
where
B: Backend,
{
/// Get the `FuncId` for the function named by `name`. /// Get the `FuncId` for the function named by `name`.
pub fn get_function_id(&self, name: &ir::ExternalName) -> FuncId { 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 {
@@ -282,62 +227,14 @@ where
} }
} }
/// Get the `ModuleFunction` for the given function. /// Get the `FunctionDeclaration` for the function named by `name`.
pub fn get_function_info(&self, func_id: FuncId) -> &ModuleFunction<B> { pub fn get_function_decl(&self, func_id: FuncId) -> &FunctionDeclaration {
&self.functions[func_id] &self.functions[func_id]
} }
/// Get the `FunctionDeclaration` for the function named by `name`.
pub fn get_function_decl(&self, name: &ir::ExternalName) -> &FunctionDeclaration {
&self.functions[self.get_function_id(name)].decl
}
/// Get the `ModuleData` for the given data object.
pub fn get_data_info(&self, data_id: DataId) -> &ModuleData<B> {
&self.data_objects[data_id]
}
/// 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, data_id: DataId) -> &DataDeclaration {
&self.data_objects[self.get_data_id(name)].decl &self.data_objects[data_id]
}
/// 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.functions[self.get_function_id(name)];
debug_assert!(
!info.decl.linkage.is_definable() || info.compiled.is_some(),
"Finalization requires a definition for function {}.",
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.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 {}.",
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. /// Return whether `name` names a function, rather than a data object.
@@ -356,9 +253,7 @@ where
B: Backend, B: Backend,
{ {
names: HashMap<String, FuncOrDataId>, names: HashMap<String, FuncOrDataId>,
contents: ModuleContents<B>, declarations: ModuleDeclarations,
functions_to_finalize: Vec<FuncId>,
data_objects_to_finalize: Vec<DataId>,
backend: B, backend: B,
} }
@@ -374,12 +269,10 @@ where
pub fn new(backend_builder: B::Builder) -> Self { pub fn new(backend_builder: B::Builder) -> Self {
Self { Self {
names: HashMap::new(), names: HashMap::new(),
contents: ModuleContents { declarations: ModuleDeclarations {
functions: PrimaryMap::new(), functions: PrimaryMap::new(),
data_objects: PrimaryMap::new(), data_objects: PrimaryMap::new(),
}, },
functions_to_finalize: Vec::new(),
data_objects_to_finalize: Vec::new(),
backend: B::new(backend_builder), backend: B::new(backend_builder),
} }
} }
@@ -442,10 +335,9 @@ where
match self.names.entry(name.to_owned()) { match self.names.entry(name.to_owned()) {
Occupied(entry) => match *entry.get() { Occupied(entry) => match *entry.get() {
FuncOrDataId::Func(id) => { FuncOrDataId::Func(id) => {
let existing = &mut self.contents.functions[id]; let existing = &mut self.declarations.functions[id];
existing.merge(linkage, signature)?; existing.merge(linkage, signature)?;
self.backend self.backend.declare_function(id, name, existing.linkage);
.declare_function(id, name, existing.decl.linkage);
Ok(id) Ok(id)
} }
FuncOrDataId::Data(..) => { FuncOrDataId::Data(..) => {
@@ -453,13 +345,10 @@ where
} }
}, },
Vacant(entry) => { Vacant(entry) => {
let id = self.contents.functions.push(ModuleFunction { let id = self.declarations.functions.push(FunctionDeclaration {
decl: FunctionDeclaration { name: name.to_owned(),
name: name.to_owned(), linkage,
linkage, signature: signature.clone(),
signature: signature.clone(),
},
compiled: None,
}); });
entry.insert(FuncOrDataId::Func(id)); entry.insert(FuncOrDataId::Func(id));
self.backend.declare_function(id, name, linkage); self.backend.declare_function(id, name, linkage);
@@ -469,8 +358,8 @@ where
} }
/// An iterator over functions that have been declared in this module. /// An iterator over functions that have been declared in this module.
pub fn declared_functions(&self) -> core::slice::Iter<'_, ModuleFunction<B>> { pub fn declared_functions(&self) -> core::slice::Iter<'_, FunctionDeclaration> {
self.contents.functions.values() self.declarations.functions.values()
} }
/// Declare a data object in this module. /// Declare a data object in this module.
@@ -487,15 +376,15 @@ where
match self.names.entry(name.to_owned()) { match self.names.entry(name.to_owned()) {
Occupied(entry) => match *entry.get() { Occupied(entry) => match *entry.get() {
FuncOrDataId::Data(id) => { FuncOrDataId::Data(id) => {
let existing = &mut self.contents.data_objects[id]; let existing = &mut self.declarations.data_objects[id];
existing.merge(linkage, writable, tls, align); existing.merge(linkage, writable, tls, align);
self.backend.declare_data( self.backend.declare_data(
id, id,
name, name,
existing.decl.linkage, existing.linkage,
existing.decl.writable, existing.writable,
existing.decl.tls, existing.tls,
existing.decl.align, existing.align,
); );
Ok(id) Ok(id)
} }
@@ -505,15 +394,12 @@ where
} }
}, },
Vacant(entry) => { Vacant(entry) => {
let id = self.contents.data_objects.push(ModuleData { let id = self.declarations.data_objects.push(DataDeclaration {
decl: DataDeclaration { name: name.to_owned(),
name: name.to_owned(), linkage,
linkage, writable,
writable, tls,
tls, align,
align,
},
compiled: None,
}); });
entry.insert(FuncOrDataId::Data(id)); entry.insert(FuncOrDataId::Data(id));
self.backend self.backend
@@ -528,7 +414,7 @@ where
/// TODO: Coalesce redundant decls and signatures. /// TODO: Coalesce redundant decls and signatures.
/// TODO: Look into ways to reduce the risk of using a FuncRef in the wrong function. /// 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 { pub fn declare_func_in_func(&self, func: FuncId, in_func: &mut ir::Function) -> ir::FuncRef {
let decl = &self.contents.functions[func].decl; let decl = &self.declarations.functions[func];
let signature = in_func.import_signature(decl.signature.clone()); let signature = in_func.import_signature(decl.signature.clone());
let colocated = decl.linkage.is_final(); let colocated = decl.linkage.is_final();
in_func.import_function(ir::ExtFuncData { in_func.import_function(ir::ExtFuncData {
@@ -542,7 +428,7 @@ where
/// ///
/// TODO: Same as above. /// TODO: Same as above.
pub fn declare_data_in_func(&self, data: DataId, func: &mut ir::Function) -> ir::GlobalValue { pub fn declare_data_in_func(&self, data: DataId, func: &mut ir::Function) -> ir::GlobalValue {
let decl = &self.contents.data_objects[data].decl; let decl = &self.declarations.data_objects[data];
let colocated = decl.linkage.is_final(); let colocated = decl.linkage.is_final();
func.create_global_value(ir::GlobalValueData::Symbol { func.create_global_value(ir::GlobalValueData::Symbol {
name: ir::ExternalName::user(1, data.as_u32()), name: ir::ExternalName::user(1, data.as_u32()),
@@ -582,20 +468,20 @@ where
ctx.func.display(self.backend.isa()) ctx.func.display(self.backend.isa())
); );
let CodeInfo { total_size, .. } = ctx.compile(self.backend.isa())?; let CodeInfo { total_size, .. } = ctx.compile(self.backend.isa())?;
let info = &self.contents.functions[func]; let decl = &self.declarations.functions[func];
info.validate_for_define()?; if !decl.linkage.is_definable() {
return Err(ModuleError::InvalidImportDefinition(decl.name.clone()));
}
let compiled = self.backend.define_function( self.backend.define_function(
func, func,
&info.decl.name, &decl.name,
ctx, ctx,
&self.contents, &self.declarations,
total_size, total_size,
trap_sink, trap_sink,
)?; )?;
self.contents.functions[func].compiled = Some(compiled);
self.functions_to_finalize.push(func);
Ok(ModuleCompiledFunction { size: total_size }) Ok(ModuleCompiledFunction { size: total_size })
} }
@@ -612,40 +498,38 @@ where
bytes: &[u8], bytes: &[u8],
) -> ModuleResult<ModuleCompiledFunction> { ) -> ModuleResult<ModuleCompiledFunction> {
info!("defining function {} with bytes", func); info!("defining function {} with bytes", func);
let info = &self.contents.functions[func]; let decl = &self.declarations.functions[func];
info.validate_for_define()?; if !decl.linkage.is_definable() {
return Err(ModuleError::InvalidImportDefinition(decl.name.clone()));
}
let total_size: u32 = match bytes.len().try_into() { let total_size: u32 = match bytes.len().try_into() {
Ok(total_size) => total_size, Ok(total_size) => total_size,
_ => Err(ModuleError::FunctionTooLarge(info.decl.name.clone()))?, _ => Err(ModuleError::FunctionTooLarge(decl.name.clone()))?,
}; };
let compiled = self.backend
self.backend .define_function_bytes(func, &decl.name, bytes, &self.declarations)?;
.define_function_bytes(func, &info.decl.name, bytes, &self.contents)?;
self.contents.functions[func].compiled = Some(compiled);
self.functions_to_finalize.push(func);
Ok(ModuleCompiledFunction { size: total_size }) Ok(ModuleCompiledFunction { size: total_size })
} }
/// Define a data object, producing the data contents from the given `DataContext`. /// Define a data object, producing the data contents from the given `DataContext`.
pub fn define_data(&mut self, data: DataId, data_ctx: &DataContext) -> ModuleResult<()> { pub fn define_data(&mut self, data: DataId, data_ctx: &DataContext) -> ModuleResult<()> {
let compiled = { let decl = &self.declarations.data_objects[data];
let info = &self.contents.data_objects[data]; if !decl.linkage.is_definable() {
info.validate_for_define()?; return Err(ModuleError::InvalidImportDefinition(decl.name.clone()));
Some(self.backend.define_data( }
data,
&info.decl.name, self.backend.define_data(
info.decl.writable, data,
info.decl.tls, &decl.name,
info.decl.align, decl.writable,
data_ctx, decl.tls,
&self.contents, decl.align,
)?) data_ctx,
}; &self.declarations,
self.contents.data_objects[data].compiled = compiled; )?;
self.data_objects_to_finalize.push(data);
Ok(()) Ok(())
} }
@@ -658,6 +542,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(self.names, self.contents) self.backend.finish(self.names, self.declarations)
} }
} }

View File

@@ -9,7 +9,7 @@ use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::{self, ir}; use cranelift_codegen::{self, ir};
use cranelift_module::{ use cranelift_module::{
Backend, DataContext, DataDescription, DataId, FuncId, FuncOrDataId, Init, Linkage, Backend, DataContext, DataDescription, DataId, FuncId, FuncOrDataId, Init, Linkage,
ModuleContents, ModuleError, ModuleResult, ModuleDeclarations, ModuleError, ModuleResult,
}; };
use object::write::{ use object::write::{
Object, Relocation, SectionId, StandardSection, Symbol, SymbolId, SymbolSection, Object, Relocation, SectionId, StandardSection, Symbol, SymbolId, SymbolSection,
@@ -112,8 +112,8 @@ impl ObjectBuilder {
pub struct ObjectBackend { pub struct ObjectBackend {
isa: Box<dyn TargetIsa>, isa: Box<dyn TargetIsa>,
object: Object, object: Object,
functions: SecondaryMap<FuncId, Option<SymbolId>>, functions: SecondaryMap<FuncId, Option<(SymbolId, bool)>>,
data_objects: SecondaryMap<DataId, Option<SymbolId>>, data_objects: SecondaryMap<DataId, Option<(SymbolId, bool)>>,
relocs: Vec<SymbolRelocs>, relocs: Vec<SymbolRelocs>,
libcalls: HashMap<ir::LibCall, SymbolId>, libcalls: HashMap<ir::LibCall, SymbolId>,
libcall_names: Box<dyn Fn(ir::LibCall) -> String>, libcall_names: Box<dyn Fn(ir::LibCall) -> String>,
@@ -124,9 +124,6 @@ pub struct ObjectBackend {
impl Backend for ObjectBackend { impl Backend for ObjectBackend {
type Builder = ObjectBuilder; type Builder = ObjectBuilder;
type CompiledFunction = ObjectCompiledFunction;
type CompiledData = ObjectCompiledData;
type Product = ObjectProduct; type Product = ObjectProduct;
/// Create a new `ObjectBackend` using the given Cranelift target. /// Create a new `ObjectBackend` using the given Cranelift target.
@@ -153,7 +150,7 @@ impl Backend for ObjectBackend {
fn declare_function(&mut self, id: FuncId, name: &str, linkage: Linkage) { fn declare_function(&mut self, id: FuncId, name: &str, linkage: Linkage) {
let (scope, weak) = translate_linkage(linkage); let (scope, weak) = translate_linkage(linkage);
if let Some(function) = self.functions[id] { if let Some((function, _defined)) = self.functions[id] {
let symbol = self.object.symbol_mut(function); let symbol = self.object.symbol_mut(function);
symbol.scope = scope; symbol.scope = scope;
symbol.weak = weak; symbol.weak = weak;
@@ -168,7 +165,7 @@ impl Backend for ObjectBackend {
section: SymbolSection::Undefined, section: SymbolSection::Undefined,
flags: SymbolFlags::None, flags: SymbolFlags::None,
}); });
self.functions[id] = Some(symbol_id); self.functions[id] = Some((symbol_id, false));
} }
} }
@@ -188,7 +185,7 @@ impl Backend for ObjectBackend {
}; };
let (scope, weak) = translate_linkage(linkage); let (scope, weak) = translate_linkage(linkage);
if let Some(data) = self.data_objects[id] { if let Some((data, _defined)) = self.data_objects[id] {
let symbol = self.object.symbol_mut(data); let symbol = self.object.symbol_mut(data);
symbol.kind = kind; symbol.kind = kind;
symbol.scope = scope; symbol.scope = scope;
@@ -204,22 +201,28 @@ impl Backend for ObjectBackend {
section: SymbolSection::Undefined, section: SymbolSection::Undefined,
flags: SymbolFlags::None, flags: SymbolFlags::None,
}); });
self.data_objects[id] = Some(symbol_id); self.data_objects[id] = Some((symbol_id, false));
} }
} }
fn define_function<TS>( fn define_function<TS>(
&mut self, &mut self,
func_id: FuncId, func_id: FuncId,
_name: &str, name: &str,
ctx: &cranelift_codegen::Context, ctx: &cranelift_codegen::Context,
_contents: &ModuleContents<Self>, _declarations: &ModuleDeclarations,
code_size: u32, code_size: u32,
trap_sink: &mut TS, trap_sink: &mut TS,
) -> ModuleResult<ObjectCompiledFunction> ) -> ModuleResult<()>
where where
TS: TrapSink, TS: TrapSink,
{ {
let &mut (symbol, ref mut defined) = self.functions[func_id].as_mut().unwrap();
if *defined {
return Err(ModuleError::DuplicateDefinition(name.to_owned()));
}
*defined = true;
let mut code: Vec<u8> = vec![0; code_size as usize]; let mut code: Vec<u8> = vec![0; code_size as usize];
let mut reloc_sink = ObjectRelocSink::new(self.object.format()); let mut reloc_sink = ObjectRelocSink::new(self.object.format());
let mut stack_map_sink = NullStackMapSink {}; let mut stack_map_sink = NullStackMapSink {};
@@ -234,8 +237,6 @@ impl Backend for ObjectBackend {
) )
}; };
let symbol = self.functions[func_id].unwrap();
let (section, offset) = if self.per_function_section { let (section, offset) = if self.per_function_section {
let symbol_name = self.object.symbol(symbol).name.clone(); let symbol_name = self.object.symbol(symbol).name.clone();
let (section, offset) = self.object.add_subsection( let (section, offset) = self.object.add_subsection(
@@ -262,17 +263,21 @@ impl Backend for ObjectBackend {
relocs: reloc_sink.relocs, relocs: reloc_sink.relocs,
}); });
} }
Ok(ObjectCompiledFunction) Ok(())
} }
fn define_function_bytes( fn define_function_bytes(
&mut self, &mut self,
func_id: FuncId, func_id: FuncId,
_name: &str, name: &str,
bytes: &[u8], bytes: &[u8],
_contents: &ModuleContents<Self>, _declarations: &ModuleDeclarations,
) -> ModuleResult<ObjectCompiledFunction> { ) -> ModuleResult<()> {
let symbol = self.functions[func_id].unwrap(); let &mut (symbol, ref mut defined) = self.functions[func_id].as_mut().unwrap();
if *defined {
return Err(ModuleError::DuplicateDefinition(name.to_owned()));
}
*defined = true;
if self.per_function_section { if self.per_function_section {
let symbol_name = self.object.symbol(symbol).name.clone(); let symbol_name = self.object.symbol(symbol).name.clone();
@@ -291,19 +296,25 @@ impl Backend for ObjectBackend {
.add_symbol_data(symbol, section, bytes, self.function_alignment); .add_symbol_data(symbol, section, bytes, self.function_alignment);
} }
Ok(ObjectCompiledFunction) Ok(())
} }
fn define_data( fn define_data(
&mut self, &mut self,
data_id: DataId, data_id: DataId,
_name: &str, name: &str,
writable: bool, writable: bool,
tls: bool, tls: bool,
align: Option<u8>, align: Option<u8>,
data_ctx: &DataContext, data_ctx: &DataContext,
_contents: &ModuleContents<Self>, _declarations: &ModuleDeclarations,
) -> ModuleResult<ObjectCompiledData> { ) -> ModuleResult<()> {
let &mut (symbol, ref mut defined) = self.data_objects[data_id].as_mut().unwrap();
if *defined {
return Err(ModuleError::DuplicateDefinition(name.to_owned()));
}
*defined = true;
let &DataDescription { let &DataDescription {
ref init, ref init,
ref function_decls, ref function_decls,
@@ -340,7 +351,6 @@ impl Backend for ObjectBackend {
}); });
} }
let symbol = self.data_objects[data_id].unwrap();
let section = if custom_segment_section.is_none() { let section = if custom_segment_section.is_none() {
let section_kind = if let Init::Zeros { .. } = *init { let section_kind = if let Init::Zeros { .. } = *init {
if tls { if tls {
@@ -397,13 +407,13 @@ impl Backend for ObjectBackend {
relocs, relocs,
}); });
} }
Ok(ObjectCompiledData) Ok(())
} }
fn finish( fn finish(
mut self, mut self,
_names: HashMap<String, FuncOrDataId>, _names: HashMap<String, FuncOrDataId>,
contents: ModuleContents<Self>, declarations: ModuleDeclarations,
) -> ObjectProduct { ) -> ObjectProduct {
let symbol_relocs = mem::take(&mut self.relocs); let symbol_relocs = mem::take(&mut self.relocs);
for symbol in symbol_relocs { for symbol in symbol_relocs {
@@ -416,7 +426,7 @@ impl Backend for ObjectBackend {
addend, addend,
} in &symbol.relocs } in &symbol.relocs
{ {
let target_symbol = self.get_symbol(&contents, name); let target_symbol = self.get_symbol(&declarations, name);
self.object self.object
.add_relocation( .add_relocation(
symbol.section, symbol.section,
@@ -453,15 +463,19 @@ impl Backend for ObjectBackend {
impl ObjectBackend { impl ObjectBackend {
// This should only be called during finish because it creates // This should only be called during finish because it creates
// symbols for missing libcalls. // symbols for missing libcalls.
fn get_symbol(&mut self, contents: &ModuleContents<Self>, name: &ir::ExternalName) -> SymbolId { fn get_symbol(
&mut self,
declarations: &ModuleDeclarations,
name: &ir::ExternalName,
) -> SymbolId {
match *name { match *name {
ir::ExternalName::User { .. } => { ir::ExternalName::User { .. } => {
if contents.is_function(name) { if declarations.is_function(name) {
let id = contents.get_function_id(name); let id = declarations.get_function_id(name);
self.functions[id].unwrap() self.functions[id].unwrap().0
} else { } else {
let id = contents.get_data_id(name); let id = declarations.get_data_id(name);
self.data_objects[id].unwrap() self.data_objects[id].unwrap().0
} }
} }
ir::ExternalName::LibCall(ref libcall) => { ir::ExternalName::LibCall(ref libcall) => {
@@ -502,9 +516,6 @@ fn translate_linkage(linkage: Linkage) -> (SymbolScope, bool) {
(scope, weak) (scope, weak)
} }
pub struct ObjectCompiledFunction;
pub struct ObjectCompiledData;
/// This is the output of `Module`'s /// This is the output of `Module`'s
/// [`finish`](../cranelift_module/struct.Module.html#method.finish) function. /// [`finish`](../cranelift_module/struct.Module.html#method.finish) function.
/// It contains the generated `Object` and other information produced during /// It contains the generated `Object` and other information produced during
@@ -513,22 +524,22 @@ pub struct ObjectProduct {
/// Object artifact with all functions and data from the module defined. /// Object artifact with all functions and data from the module defined.
pub object: Object, pub object: Object,
/// Symbol IDs for functions (both declared and defined). /// Symbol IDs for functions (both declared and defined).
pub functions: SecondaryMap<FuncId, Option<SymbolId>>, pub functions: SecondaryMap<FuncId, Option<(SymbolId, bool)>>,
/// Symbol IDs for data objects (both declared and defined). /// Symbol IDs for data objects (both declared and defined).
pub data_objects: SecondaryMap<DataId, Option<SymbolId>>, pub data_objects: SecondaryMap<DataId, Option<(SymbolId, bool)>>,
} }
impl ObjectProduct { impl ObjectProduct {
/// Return the `SymbolId` for the given function. /// Return the `SymbolId` for the given function.
#[inline] #[inline]
pub fn function_symbol(&self, id: FuncId) -> SymbolId { pub fn function_symbol(&self, id: FuncId) -> SymbolId {
self.functions[id].unwrap() self.functions[id].unwrap().0
} }
/// Return the `SymbolId` for the given data object. /// Return the `SymbolId` for the given data object.
#[inline] #[inline]
pub fn data_symbol(&self, id: DataId) -> SymbolId { pub fn data_symbol(&self, id: DataId) -> SymbolId {
self.data_objects[id].unwrap() self.data_objects[id].unwrap().0
} }
/// Write the object bytes in memory. /// Write the object bytes in memory.

View File

@@ -13,6 +13,7 @@ edition = "2018"
cranelift-module = { path = "../module", version = "0.67.0" } cranelift-module = { path = "../module", version = "0.67.0" }
cranelift-native = { path = "../native", version = "0.67.0" } cranelift-native = { path = "../native", version = "0.67.0" }
cranelift-codegen = { path = "../codegen", version = "0.67.0", default-features = false, features = ["std"] } cranelift-codegen = { path = "../codegen", version = "0.67.0", default-features = false, features = ["std"] }
cranelift-entity = { path = "../entity", version = "0.67.0" }
region = "2.2.0" region = "2.2.0"
libc = { version = "0.2.42" } libc = { version = "0.2.42" }
errno = "0.2.4" errno = "0.2.4"

View File

@@ -7,9 +7,10 @@ use cranelift_codegen::binemit::{
use cranelift_codegen::isa::TargetIsa; 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_entity::SecondaryMap;
use cranelift_module::{ use cranelift_module::{
Backend, DataContext, DataDescription, DataId, FuncId, FuncOrDataId, Init, Linkage, Backend, DataContext, DataDescription, DataId, FuncId, FuncOrDataId, Init, Linkage,
ModuleContents, ModuleResult, ModuleDeclarations, ModuleError, ModuleResult,
}; };
use cranelift_native; use cranelift_native;
#[cfg(not(windows))] #[cfg(not(windows))]
@@ -124,11 +125,14 @@ pub struct SimpleJITBackend {
symbols: HashMap<String, *const u8>, symbols: HashMap<String, *const u8>,
libcall_names: Box<dyn Fn(ir::LibCall) -> String>, libcall_names: Box<dyn Fn(ir::LibCall) -> String>,
memory: SimpleJITMemoryHandle, memory: SimpleJITMemoryHandle,
functions: SecondaryMap<FuncId, Option<SimpleJITCompiledFunction>>,
data_objects: SecondaryMap<DataId, Option<SimpleJITCompiledData>>,
functions_to_finalize: Vec<FuncId>, functions_to_finalize: Vec<FuncId>,
data_objects_to_finalize: Vec<DataId>, data_objects_to_finalize: Vec<DataId>,
} }
/// A record of a relocation to perform. /// A record of a relocation to perform.
#[derive(Clone)]
struct RelocRecord { struct RelocRecord {
offset: CodeOffset, offset: CodeOffset,
reloc: Reloc, reloc: Reloc,
@@ -143,12 +147,14 @@ struct StackMapRecord {
stack_map: StackMap, stack_map: StackMap,
} }
#[derive(Clone)]
pub struct SimpleJITCompiledFunction { pub struct SimpleJITCompiledFunction {
code: *mut u8, code: *mut u8,
size: usize, size: usize,
relocs: Vec<RelocRecord>, relocs: Vec<RelocRecord>,
} }
#[derive(Clone)]
pub struct SimpleJITCompiledData { pub struct SimpleJITCompiledData {
storage: *mut u8, storage: *mut u8,
size: usize, size: usize,
@@ -167,7 +173,8 @@ struct SimpleJITMemoryHandle {
pub struct SimpleJITProduct { pub struct SimpleJITProduct {
memory: SimpleJITMemoryHandle, memory: SimpleJITMemoryHandle,
names: HashMap<String, FuncOrDataId>, names: HashMap<String, FuncOrDataId>,
contents: ModuleContents<SimpleJITBackend>, functions: SecondaryMap<FuncId, Option<SimpleJITCompiledFunction>>,
data_objects: SecondaryMap<DataId, Option<SimpleJITCompiledData>>,
} }
impl SimpleJITProduct { impl SimpleJITProduct {
@@ -192,19 +199,16 @@ impl SimpleJITProduct {
/// Return the address of a function. /// Return the address of a function.
pub fn lookup_func(&self, func_id: FuncId) -> *const u8 { pub fn lookup_func(&self, func_id: FuncId) -> *const u8 {
self.contents self.functions[func_id]
.get_function_definition(&func_id.into()) .as_ref()
.0
.unwrap_or_else(|| panic!("{} is not defined", func_id)) .unwrap_or_else(|| panic!("{} is not defined", func_id))
.code .code
} }
/// Return the address and size of a data object. /// Return the address and size of a data object.
pub fn lookup_data(&self, data_id: DataId) -> (*const u8, usize) { pub fn lookup_data(&self, data_id: DataId) -> (*const u8, usize) {
let data = self let data = self.data_objects[data_id]
.contents .as_ref()
.get_data_definition(&data_id.into())
.0
.unwrap_or_else(|| panic!("{} is not defined", data_id)); .unwrap_or_else(|| panic!("{} is not defined", data_id));
(data.storage, data.size) (data.storage, data.size)
} }
@@ -220,22 +224,22 @@ impl SimpleJITBackend {
fn get_definition( fn get_definition(
&self, &self,
contents: &ModuleContents<Self>, declarations: &ModuleDeclarations,
name: &ir::ExternalName, name: &ir::ExternalName,
) -> *const u8 { ) -> *const u8 {
match *name { match *name {
ir::ExternalName::User { .. } => { ir::ExternalName::User { .. } => {
if contents.is_function(name) { if declarations.is_function(name) {
let (def, name_str, _signature) = contents.get_function_definition(&name); let func_id = declarations.get_function_id(name);
match def { match &self.functions[func_id] {
Some(compiled) => compiled.code, Some(compiled) => compiled.code,
None => self.lookup_symbol(name_str), None => self.lookup_symbol(&declarations.get_function_decl(func_id).name),
} }
} else { } else {
let (def, name_str, _writable) = contents.get_data_definition(&name); let data_id = declarations.get_data_id(name);
match def { match &self.data_objects[data_id] {
Some(compiled) => compiled.storage, Some(compiled) => compiled.storage,
None => self.lookup_symbol(name_str), None => self.lookup_symbol(&declarations.get_data_decl(data_id).name),
} }
} }
} }
@@ -264,15 +268,13 @@ impl SimpleJITBackend {
} }
} }
fn finalize_function(&mut self, id: FuncId, declarations: &ModuleDeclarations) {
fn finalize_function(
&mut self,
_id: FuncId,
func: &SimpleJITCompiledFunction,
contents: &ModuleContents<Self>,
) {
use std::ptr::write_unaligned; use std::ptr::write_unaligned;
let func = self.functions[id]
.as_ref()
.expect("function must be compiled before it can be finalized");
for &RelocRecord { for &RelocRecord {
reloc, reloc,
offset, offset,
@@ -283,7 +285,7 @@ impl 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(contents, name); let base = self.get_definition(declarations, 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 {
@@ -314,14 +316,13 @@ impl SimpleJITBackend {
} }
} }
fn finalize_data( fn finalize_data(&mut self, id: DataId, declarations: &ModuleDeclarations) {
&mut self,
_id: DataId,
data: &SimpleJITCompiledData,
contents: &ModuleContents<Self>,
) {
use std::ptr::write_unaligned; use std::ptr::write_unaligned;
let data = self.data_objects[id]
.as_ref()
.expect("data object must be compiled before it can be finalized");
for &RelocRecord { for &RelocRecord {
reloc, reloc,
offset, offset,
@@ -332,7 +333,7 @@ impl 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(contents, name); let base = self.get_definition(declarations, 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 {
@@ -362,13 +363,6 @@ impl SimpleJITBackend {
impl<'simple_jit_backend> Backend for SimpleJITBackend { impl<'simple_jit_backend> Backend for SimpleJITBackend {
type Builder = SimpleJITBuilder; type Builder = SimpleJITBuilder;
/// SimpleJIT compiled function and data objects may have outstanding
/// relocations that need to be performed before the memory can be used.
/// These relocations are performed within `finalize_function` and
/// `finalize_data`.
type CompiledFunction = SimpleJITCompiledFunction;
type CompiledData = SimpleJITCompiledData;
/// SimpleJIT emits code and data into memory as it processes them, so it /// SimpleJIT emits code and data into memory as it processes them, so it
/// doesn't need to provide anything after the `Module` is complete. /// doesn't need to provide anything after the `Module` is complete.
/// The handle object that is returned can optionally be used to free /// The handle object that is returned can optionally be used to free
@@ -388,6 +382,8 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
symbols: builder.symbols, symbols: builder.symbols,
libcall_names: builder.libcall_names, libcall_names: builder.libcall_names,
memory, memory,
functions: SecondaryMap::new(),
data_objects: SecondaryMap::new(),
functions_to_finalize: Vec::new(), functions_to_finalize: Vec::new(),
data_objects_to_finalize: Vec::new(), data_objects_to_finalize: Vec::new(),
} }
@@ -419,13 +415,17 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
id: FuncId, id: FuncId,
name: &str, name: &str,
ctx: &cranelift_codegen::Context, ctx: &cranelift_codegen::Context,
_contents: &ModuleContents<Self>, _declarations: &ModuleDeclarations,
code_size: u32, code_size: u32,
trap_sink: &mut TS, trap_sink: &mut TS,
) -> ModuleResult<Self::CompiledFunction> ) -> ModuleResult<()>
where where
TS: TrapSink, TS: TrapSink,
{ {
if !self.functions[id].is_none() {
return Err(ModuleError::DuplicateDefinition(name.to_owned()));
}
self.functions_to_finalize.push(id); self.functions_to_finalize.push(id);
let size = code_size as usize; let size = code_size as usize;
let ptr = self let ptr = self
@@ -448,11 +448,13 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
) )
}; };
Ok(Self::CompiledFunction { self.functions[id] = Some(SimpleJITCompiledFunction {
code: ptr, code: ptr,
size, size,
relocs: reloc_sink.relocs, relocs: reloc_sink.relocs,
}) });
Ok(())
} }
fn define_function_bytes( fn define_function_bytes(
@@ -460,8 +462,12 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
id: FuncId, id: FuncId,
name: &str, name: &str,
bytes: &[u8], bytes: &[u8],
_contents: &ModuleContents<Self>, _declarations: &ModuleDeclarations,
) -> ModuleResult<Self::CompiledFunction> { ) -> ModuleResult<()> {
if !self.functions[id].is_none() {
return Err(ModuleError::DuplicateDefinition(name.to_owned()));
}
self.functions_to_finalize.push(id); self.functions_to_finalize.push(id);
let size = bytes.len(); let size = bytes.len();
let ptr = self let ptr = self
@@ -476,23 +482,29 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
ptr::copy_nonoverlapping(bytes.as_ptr(), ptr, size); ptr::copy_nonoverlapping(bytes.as_ptr(), ptr, size);
} }
Ok(Self::CompiledFunction { self.functions[id] = Some(SimpleJITCompiledFunction {
code: ptr, code: ptr,
size, size,
relocs: vec![], relocs: vec![],
}) });
Ok(())
} }
fn define_data( fn define_data(
&mut self, &mut self,
id: DataId, id: DataId,
_name: &str, name: &str,
writable: bool, writable: bool,
tls: bool, tls: bool,
align: Option<u8>, align: Option<u8>,
data: &DataContext, data: &DataContext,
_contents: &ModuleContents<Self>, _declarations: &ModuleDeclarations,
) -> ModuleResult<Self::CompiledData> { ) -> ModuleResult<()> {
if !self.data_objects[id].is_none() {
return Err(ModuleError::DuplicateDefinition(name.to_owned()));
}
assert!(!tls, "SimpleJIT doesn't yet support TLS"); assert!(!tls, "SimpleJIT doesn't yet support TLS");
self.data_objects_to_finalize.push(id); self.data_objects_to_finalize.push(id);
@@ -555,11 +567,13 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
}); });
} }
Ok(Self::CompiledData { self.data_objects[id] = Some(SimpleJITCompiledData {
storage, storage,
size, size,
relocs, relocs,
}) });
Ok(())
} }
/// SimpleJIT emits code and data into memory as it processes them. This /// SimpleJIT emits code and data into memory as it processes them. This
@@ -572,29 +586,17 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
fn finish( fn finish(
mut self, mut self,
names: HashMap<String, FuncOrDataId>, names: HashMap<String, FuncOrDataId>,
contents: ModuleContents<Self>, declarations: ModuleDeclarations,
) -> Self::Product { ) -> Self::Product {
for func in std::mem::take(&mut self.functions_to_finalize) { for func in std::mem::take(&mut self.functions_to_finalize) {
let info = contents.get_function_info(func); let decl = declarations.get_function_decl(func);
debug_assert!(info.decl.linkage.is_definable()); debug_assert!(decl.linkage.is_definable());
self.finalize_function( self.finalize_function(func, &declarations);
func,
info.compiled
.as_ref()
.expect("function must be compiled before it can be finalized"),
&contents,
);
} }
for data in std::mem::take(&mut self.data_objects_to_finalize) { for data in std::mem::take(&mut self.data_objects_to_finalize) {
let info = contents.get_data_info(data); let decl = declarations.get_data_decl(data);
debug_assert!(info.decl.linkage.is_definable()); debug_assert!(decl.linkage.is_definable());
self.finalize_data( self.finalize_data(data, &declarations);
data,
info.compiled
.as_ref()
.expect("data object must be compiled before it can be finalized"),
&contents,
);
} }
// Now that we're done patching, prepare the memory for execution! // Now that we're done patching, prepare the memory for execution!
@@ -604,7 +606,8 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
SimpleJITProduct { SimpleJITProduct {
memory: self.memory, memory: self.memory,
names, names,
contents, functions: self.functions,
data_objects: self.data_objects,
} }
} }
} }