Move a bit more logic out of Module
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -489,6 +489,7 @@ dependencies = [
|
|||||||
"anyhow",
|
"anyhow",
|
||||||
"cranelift-codegen",
|
"cranelift-codegen",
|
||||||
"cranelift-module",
|
"cranelift-module",
|
||||||
|
"log",
|
||||||
"object 0.21.1",
|
"object 0.21.1",
|
||||||
"target-lexicon",
|
"target-lexicon",
|
||||||
]
|
]
|
||||||
@@ -535,6 +536,7 @@ dependencies = [
|
|||||||
"cranelift-native",
|
"cranelift-native",
|
||||||
"errno",
|
"errno",
|
||||||
"libc",
|
"libc",
|
||||||
|
"log",
|
||||||
"memmap",
|
"memmap",
|
||||||
"region",
|
"region",
|
||||||
"target-lexicon",
|
"target-lexicon",
|
||||||
|
|||||||
@@ -1,19 +1,20 @@
|
|||||||
//! Defines the `Backend` trait.
|
//! Defines the `Backend` trait.
|
||||||
|
|
||||||
|
use crate::module::ModuleCompiledFunction;
|
||||||
|
use crate::DataContext;
|
||||||
use crate::DataId;
|
use crate::DataId;
|
||||||
use crate::FuncId;
|
use crate::FuncId;
|
||||||
use crate::Linkage;
|
use crate::Linkage;
|
||||||
use crate::ModuleDeclarations;
|
use crate::ModuleDeclarations;
|
||||||
use crate::ModuleResult;
|
use crate::ModuleResult;
|
||||||
use crate::DataContext;
|
|
||||||
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;
|
||||||
use std::boxed::Box;
|
use std::boxed::Box;
|
||||||
use std::string::String;
|
use std::string::String;
|
||||||
use std::borrow::ToOwned;
|
|
||||||
|
|
||||||
/// A `Backend` implements the functionality needed to support a `Module`.
|
/// A `Backend` implements the functionality needed to support a `Module`.
|
||||||
///
|
///
|
||||||
@@ -63,11 +64,10 @@ where
|
|||||||
fn define_function<TS>(
|
fn define_function<TS>(
|
||||||
&mut self,
|
&mut self,
|
||||||
id: FuncId,
|
id: FuncId,
|
||||||
ctx: &Context,
|
ctx: &mut Context,
|
||||||
declarations: &ModuleDeclarations,
|
declarations: &ModuleDeclarations,
|
||||||
code_size: u32,
|
|
||||||
trap_sink: &mut TS,
|
trap_sink: &mut TS,
|
||||||
) -> ModuleResult<()>
|
) -> ModuleResult<ModuleCompiledFunction>
|
||||||
where
|
where
|
||||||
TS: binemit::TrapSink;
|
TS: binemit::TrapSink;
|
||||||
|
|
||||||
@@ -79,7 +79,7 @@ where
|
|||||||
id: FuncId,
|
id: FuncId,
|
||||||
bytes: &[u8],
|
bytes: &[u8],
|
||||||
declarations: &ModuleDeclarations,
|
declarations: &ModuleDeclarations,
|
||||||
) -> ModuleResult<()>;
|
) -> ModuleResult<ModuleCompiledFunction>;
|
||||||
|
|
||||||
/// Define a zero-initialized data object of the given size.
|
/// Define a zero-initialized data object of the given size.
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -40,7 +40,8 @@ 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, ModuleDeclarations, ModuleError, ModuleResult,
|
DataId, FuncId, FuncOrDataId, Linkage, Module, ModuleCompiledFunction, ModuleDeclarations,
|
||||||
|
ModuleError, ModuleResult,
|
||||||
};
|
};
|
||||||
pub use crate::traps::TrapSite;
|
pub use crate::traps::TrapSite;
|
||||||
|
|
||||||
|
|||||||
@@ -8,12 +8,10 @@
|
|||||||
use super::HashMap;
|
use super::HashMap;
|
||||||
use crate::data_context::DataContext;
|
use crate::data_context::DataContext;
|
||||||
use crate::Backend;
|
use crate::Backend;
|
||||||
use cranelift_codegen::binemit::{self, CodeInfo};
|
use cranelift_codegen::binemit;
|
||||||
use cranelift_codegen::entity::{entity_impl, PrimaryMap};
|
use cranelift_codegen::entity::{entity_impl, PrimaryMap};
|
||||||
use cranelift_codegen::{ir, isa, CodegenError, Context};
|
use cranelift_codegen::{ir, isa, CodegenError, Context};
|
||||||
use log::info;
|
|
||||||
use std::borrow::ToOwned;
|
use std::borrow::ToOwned;
|
||||||
use std::convert::TryInto;
|
|
||||||
use std::string::String;
|
use std::string::String;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
@@ -333,7 +331,9 @@ where
|
|||||||
backend: B,
|
backend: B,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Information about the compiled function.
|
||||||
pub struct ModuleCompiledFunction {
|
pub struct ModuleCompiledFunction {
|
||||||
|
/// The size of the compiled function.
|
||||||
pub size: binemit::CodeOffset,
|
pub size: binemit::CodeOffset,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -488,17 +488,8 @@ where
|
|||||||
where
|
where
|
||||||
TS: binemit::TrapSink,
|
TS: binemit::TrapSink,
|
||||||
{
|
{
|
||||||
info!(
|
|
||||||
"defining function {}: {}",
|
|
||||||
func,
|
|
||||||
ctx.func.display(self.backend.isa())
|
|
||||||
);
|
|
||||||
let CodeInfo { total_size, .. } = ctx.compile(self.backend.isa())?;
|
|
||||||
|
|
||||||
self.backend
|
self.backend
|
||||||
.define_function(func, ctx, &self.declarations, total_size, trap_sink)?;
|
.define_function(func, ctx, &self.declarations, trap_sink)
|
||||||
|
|
||||||
Ok(ModuleCompiledFunction { size: total_size })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Define a function, taking the function body from the given `bytes`.
|
/// Define a function, taking the function body from the given `bytes`.
|
||||||
@@ -513,27 +504,13 @@ where
|
|||||||
func: FuncId,
|
func: FuncId,
|
||||||
bytes: &[u8],
|
bytes: &[u8],
|
||||||
) -> ModuleResult<ModuleCompiledFunction> {
|
) -> ModuleResult<ModuleCompiledFunction> {
|
||||||
info!("defining function {} with bytes", func);
|
|
||||||
let decl = &self.declarations.functions[func];
|
|
||||||
|
|
||||||
let total_size: u32 = match bytes.len().try_into() {
|
|
||||||
Ok(total_size) => total_size,
|
|
||||||
_ => Err(ModuleError::FunctionTooLarge(decl.name.clone()))?,
|
|
||||||
};
|
|
||||||
|
|
||||||
self.backend
|
self.backend
|
||||||
.define_function_bytes(func, bytes, &self.declarations)?;
|
.define_function_bytes(func, bytes, &self.declarations)
|
||||||
|
|
||||||
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<()> {
|
||||||
self.backend.define_data(
|
self.backend.define_data(data, data_ctx, &self.declarations)
|
||||||
data,
|
|
||||||
data_ctx,
|
|
||||||
&self.declarations,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the target isa
|
/// Return the target isa
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ cranelift-codegen = { path = "../codegen", version = "0.67.0", default-features
|
|||||||
object = { version = "0.21.1", default-features = false, features = ["write"] }
|
object = { version = "0.21.1", default-features = false, features = ["write"] }
|
||||||
target-lexicon = "0.11"
|
target-lexicon = "0.11"
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
|
log = { version = "0.4.6", default-features = false }
|
||||||
|
|
||||||
[badges]
|
[badges]
|
||||||
maintenance = { status = "experimental" }
|
maintenance = { status = "experimental" }
|
||||||
|
|||||||
@@ -2,15 +2,16 @@
|
|||||||
|
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use cranelift_codegen::binemit::{
|
use cranelift_codegen::binemit::{
|
||||||
Addend, CodeOffset, NullStackMapSink, Reloc, RelocSink, TrapSink,
|
Addend, CodeInfo, CodeOffset, NullStackMapSink, Reloc, RelocSink, TrapSink,
|
||||||
};
|
};
|
||||||
use cranelift_codegen::entity::SecondaryMap;
|
use cranelift_codegen::entity::SecondaryMap;
|
||||||
use cranelift_codegen::isa::TargetIsa;
|
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, Init, Linkage, ModuleDeclarations,
|
Backend, DataContext, DataDescription, DataId, FuncId, Init, Linkage, ModuleCompiledFunction,
|
||||||
ModuleError, ModuleResult,
|
ModuleDeclarations, ModuleError, ModuleResult,
|
||||||
};
|
};
|
||||||
|
use log::info;
|
||||||
use object::write::{
|
use object::write::{
|
||||||
Object, Relocation, SectionId, StandardSection, Symbol, SymbolId, SymbolSection,
|
Object, Relocation, SectionId, StandardSection, Symbol, SymbolId, SymbolSection,
|
||||||
};
|
};
|
||||||
@@ -18,6 +19,7 @@ use object::{
|
|||||||
RelocationEncoding, RelocationKind, SectionKind, SymbolFlags, SymbolKind, SymbolScope,
|
RelocationEncoding, RelocationKind, SectionKind, SymbolFlags, SymbolKind, SymbolScope,
|
||||||
};
|
};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::convert::TryInto;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use target_lexicon::PointerWidth;
|
use target_lexicon::PointerWidth;
|
||||||
|
|
||||||
@@ -208,14 +210,23 @@ impl Backend for ObjectBackend {
|
|||||||
fn define_function<TS>(
|
fn define_function<TS>(
|
||||||
&mut self,
|
&mut self,
|
||||||
func_id: FuncId,
|
func_id: FuncId,
|
||||||
ctx: &cranelift_codegen::Context,
|
ctx: &mut cranelift_codegen::Context,
|
||||||
declarations: &ModuleDeclarations,
|
declarations: &ModuleDeclarations,
|
||||||
code_size: u32,
|
|
||||||
trap_sink: &mut TS,
|
trap_sink: &mut TS,
|
||||||
) -> ModuleResult<()>
|
) -> ModuleResult<ModuleCompiledFunction>
|
||||||
where
|
where
|
||||||
TS: TrapSink,
|
TS: TrapSink,
|
||||||
{
|
{
|
||||||
|
info!(
|
||||||
|
"defining function {}: {}",
|
||||||
|
func_id,
|
||||||
|
ctx.func.display(self.isa())
|
||||||
|
);
|
||||||
|
let CodeInfo {
|
||||||
|
total_size: code_size,
|
||||||
|
..
|
||||||
|
} = ctx.compile(self.isa())?;
|
||||||
|
|
||||||
let decl = declarations.get_function_decl(func_id);
|
let decl = declarations.get_function_decl(func_id);
|
||||||
if !decl.linkage.is_definable() {
|
if !decl.linkage.is_definable() {
|
||||||
return Err(ModuleError::InvalidImportDefinition(decl.name.clone()));
|
return Err(ModuleError::InvalidImportDefinition(decl.name.clone()));
|
||||||
@@ -267,7 +278,8 @@ impl Backend for ObjectBackend {
|
|||||||
relocs: reloc_sink.relocs,
|
relocs: reloc_sink.relocs,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
|
Ok(ModuleCompiledFunction { size: code_size })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn define_function_bytes(
|
fn define_function_bytes(
|
||||||
@@ -275,12 +287,19 @@ impl Backend for ObjectBackend {
|
|||||||
func_id: FuncId,
|
func_id: FuncId,
|
||||||
bytes: &[u8],
|
bytes: &[u8],
|
||||||
declarations: &ModuleDeclarations,
|
declarations: &ModuleDeclarations,
|
||||||
) -> ModuleResult<()> {
|
) -> ModuleResult<ModuleCompiledFunction> {
|
||||||
|
info!("defining function {} with bytes", func_id);
|
||||||
|
|
||||||
let decl = declarations.get_function_decl(func_id);
|
let decl = declarations.get_function_decl(func_id);
|
||||||
if !decl.linkage.is_definable() {
|
if !decl.linkage.is_definable() {
|
||||||
return Err(ModuleError::InvalidImportDefinition(decl.name.clone()));
|
return Err(ModuleError::InvalidImportDefinition(decl.name.clone()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let total_size: u32 = match bytes.len().try_into() {
|
||||||
|
Ok(total_size) => total_size,
|
||||||
|
_ => Err(ModuleError::FunctionTooLarge(decl.name.clone()))?,
|
||||||
|
};
|
||||||
|
|
||||||
let &mut (symbol, ref mut defined) = self.functions[func_id].as_mut().unwrap();
|
let &mut (symbol, ref mut defined) = self.functions[func_id].as_mut().unwrap();
|
||||||
if *defined {
|
if *defined {
|
||||||
return Err(ModuleError::DuplicateDefinition(decl.name.clone()));
|
return Err(ModuleError::DuplicateDefinition(decl.name.clone()));
|
||||||
@@ -304,7 +323,7 @@ impl Backend for ObjectBackend {
|
|||||||
.add_symbol_data(symbol, section, bytes, self.function_alignment);
|
.add_symbol_data(symbol, section, bytes, self.function_alignment);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(ModuleCompiledFunction { size: total_size })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn define_data(
|
fn define_data(
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ libc = { version = "0.2.42" }
|
|||||||
errno = "0.2.4"
|
errno = "0.2.4"
|
||||||
target-lexicon = "0.11"
|
target-lexicon = "0.11"
|
||||||
memmap = { version = "0.7.0", optional = true }
|
memmap = { version = "0.7.0", optional = true }
|
||||||
|
log = { version = "0.4.6", default-features = false }
|
||||||
|
|
||||||
[target.'cfg(target_os = "windows")'.dependencies]
|
[target.'cfg(target_os = "windows")'.dependencies]
|
||||||
winapi = { version = "0.3", features = ["winbase", "memoryapi"] }
|
winapi = { version = "0.3", features = ["winbase", "memoryapi"] }
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use crate::memory::Memory;
|
use crate::memory::Memory;
|
||||||
use cranelift_codegen::binemit::{
|
use cranelift_codegen::binemit::{
|
||||||
Addend, CodeOffset, Reloc, RelocSink, StackMap, StackMapSink, TrapSink,
|
Addend, CodeInfo, CodeOffset, Reloc, RelocSink, StackMap, StackMapSink, TrapSink,
|
||||||
};
|
};
|
||||||
use cranelift_codegen::isa::TargetIsa;
|
use cranelift_codegen::isa::TargetIsa;
|
||||||
use cranelift_codegen::settings::Configurable;
|
use cranelift_codegen::settings::Configurable;
|
||||||
@@ -10,12 +10,14 @@ use cranelift_codegen::{self, ir, settings};
|
|||||||
use cranelift_entity::SecondaryMap;
|
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,
|
||||||
ModuleDeclarations, ModuleError, ModuleResult,
|
ModuleCompiledFunction, ModuleDeclarations, ModuleError, ModuleResult,
|
||||||
};
|
};
|
||||||
use cranelift_native;
|
use cranelift_native;
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
use libc;
|
use libc;
|
||||||
|
use log::info;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::convert::TryInto;
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
@@ -413,14 +415,19 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
|
|||||||
fn define_function<TS>(
|
fn define_function<TS>(
|
||||||
&mut self,
|
&mut self,
|
||||||
id: FuncId,
|
id: FuncId,
|
||||||
ctx: &cranelift_codegen::Context,
|
ctx: &mut cranelift_codegen::Context,
|
||||||
declarations: &ModuleDeclarations,
|
declarations: &ModuleDeclarations,
|
||||||
code_size: u32,
|
|
||||||
trap_sink: &mut TS,
|
trap_sink: &mut TS,
|
||||||
) -> ModuleResult<()>
|
) -> ModuleResult<ModuleCompiledFunction>
|
||||||
where
|
where
|
||||||
TS: TrapSink,
|
TS: TrapSink,
|
||||||
{
|
{
|
||||||
|
info!("defining function {}: {}", id, ctx.func.display(self.isa()));
|
||||||
|
let CodeInfo {
|
||||||
|
total_size: code_size,
|
||||||
|
..
|
||||||
|
} = ctx.compile(self.isa())?;
|
||||||
|
|
||||||
let decl = declarations.get_function_decl(id);
|
let decl = declarations.get_function_decl(id);
|
||||||
if !decl.linkage.is_definable() {
|
if !decl.linkage.is_definable() {
|
||||||
return Err(ModuleError::InvalidImportDefinition(decl.name.clone()));
|
return Err(ModuleError::InvalidImportDefinition(decl.name.clone()));
|
||||||
@@ -458,7 +465,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
|
|||||||
relocs: reloc_sink.relocs,
|
relocs: reloc_sink.relocs,
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(())
|
Ok(ModuleCompiledFunction { size: code_size })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn define_function_bytes(
|
fn define_function_bytes(
|
||||||
@@ -466,12 +473,17 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
|
|||||||
id: FuncId,
|
id: FuncId,
|
||||||
bytes: &[u8],
|
bytes: &[u8],
|
||||||
declarations: &ModuleDeclarations,
|
declarations: &ModuleDeclarations,
|
||||||
) -> ModuleResult<()> {
|
) -> ModuleResult<ModuleCompiledFunction> {
|
||||||
let decl = declarations.get_function_decl(id);
|
let decl = declarations.get_function_decl(id);
|
||||||
if !decl.linkage.is_definable() {
|
if !decl.linkage.is_definable() {
|
||||||
return Err(ModuleError::InvalidImportDefinition(decl.name.clone()));
|
return Err(ModuleError::InvalidImportDefinition(decl.name.clone()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let total_size: u32 = match bytes.len().try_into() {
|
||||||
|
Ok(total_size) => total_size,
|
||||||
|
_ => Err(ModuleError::FunctionTooLarge(decl.name.clone()))?,
|
||||||
|
};
|
||||||
|
|
||||||
if !self.functions[id].is_none() {
|
if !self.functions[id].is_none() {
|
||||||
return Err(ModuleError::DuplicateDefinition(decl.name.to_owned()));
|
return Err(ModuleError::DuplicateDefinition(decl.name.to_owned()));
|
||||||
}
|
}
|
||||||
@@ -496,7 +508,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
|
|||||||
relocs: vec![],
|
relocs: vec![],
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(())
|
Ok(ModuleCompiledFunction { size: total_size })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn define_data(
|
fn define_data(
|
||||||
|
|||||||
Reference in New Issue
Block a user